Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
AddVariableAction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 // Standard includes
11 #include <sstream>
12 #include <stdexcept>
13 
14 // MOOSE includes
15 #include "AddVariableAction.h"
16 #include "FEProblem.h"
17 #include "Factory.h"
18 #include "MooseEnum.h"
19 #include "MooseEigenSystem.h"
20 #include "MooseObjectAction.h"
21 #include "MooseMesh.h"
22 #include "CopyNodalVarsAction.h"
23 
24 #include "libmesh/libmesh.h"
25 #include "libmesh/exodusII_io.h"
26 #include "libmesh/equation_systems.h"
27 #include "libmesh/nonlinear_implicit_system.h"
28 #include "libmesh/explicit_system.h"
29 #include "libmesh/string_to_enum.h"
30 #include "libmesh/fe_interface.h"
31 
32 using namespace libMesh;
33 
34 registerMooseAction("MooseApp", AddVariableAction, "add_variable");
35 
38 {
39  auto params = MooseObjectAction::validParams();
40  params.addClassDescription("Add a non-linear variable to the simulation.");
41 
42  // The user may specify a type in the Variables block, but if they don't we'll just use all the
43  // parameters available from MooseVariableBase
44  params.set<std::string>("type") = "MooseVariableBase";
45 
46  // The below is for backwards compatibility
49  params.addParam<MooseEnum>(
50  "family", families, "Specifies the family of FE shape functions to use for this variable");
51  params.addParam<MooseEnum>("order",
52  orders,
53  "Specifies the order of the FE shape function to use "
54  "for this variable (additional orders not listed are "
55  "allowed)");
56  params.addParam<std::vector<Real>>("scaling",
57  "Specifies a scaling factor to apply to this variable");
58  params.addParam<std::vector<Real>>("initial_condition",
59  "Specifies a constant initial condition for this variable");
60  params.transferParam<std::string>(CopyNodalVarsAction::validParams(), "initial_from_file_var");
61  return params;
62 }
63 
65  : MooseObjectAction(params),
66  _fe_type(feType(params)),
67  _scalar_var(_fe_type.family == SCALAR),
68  _fv_var(false),
69  _components(1)
70 {
71 }
72 
75 {
76  return MooseEnum("LAGRANGE MONOMIAL HERMITE SCALAR HIERARCHIC CLOUGH XYZ SZABAB BERNSTEIN "
77  "L2_LAGRANGE L2_HIERARCHIC NEDELEC_ONE LAGRANGE_VEC MONOMIAL_VEC "
78  "RAVIART_THOMAS RATIONAL_BERNSTEIN SIDE_HIERARCHIC L2_HIERARCHIC_VEC "
79  "L2_LAGRANGE_VEC L2_RAVIART_THOMAS",
80  "LAGRANGE");
81 }
82 
85 {
86  return MooseEnum("CONSTANT FIRST SECOND THIRD FOURTH", "FIRST", true);
87 }
88 
89 FEType
91 {
92  return {Utility::string_to_enum<Order>(params.get<MooseEnum>("order")),
93  Utility::string_to_enum<FEFamily>(params.get<MooseEnum>("family"))};
94 }
95 
96 void
98 {
99  _components = _moose_object_pars.get<unsigned int>("components");
100  if (_components == 0)
101  mooseError("There must be at least one variable component, but somehow 0 has been specified");
102 
103  // We have to do some sanity checks because of our work to maintain backwards compatibility.
104  // `family`, `order`, and `scaling` are all parameters duplicated between this action and the
105  // `MooseVariable*` object itself. Consequently during input file parsing, the params objects for
106  // both the action and MooseVariable object can be populated with the exact same parameters.
107  // However, some applications actually create their variables solely through creation and setting
108  // of `AddVariableAction` parameters which means that the `MooseVariableBase*` params will never
109  // be populated. So we should apply the parameters directly from the action. There should be no
110  // case in which both params objects get set by the user and they have different values
111 
112  if (_pars.isParamSetByUser("family") && _moose_object_pars.isParamSetByUser("family") &&
113  !_pars.get<MooseEnum>("family").compareCurrent(_moose_object_pars.get<MooseEnum>("family")))
114  mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
115  "`family` parameter set, and they are different values: ",
116  _moose_object_pars.get<MooseEnum>("family"),
117  " and ",
118  _pars.get<MooseEnum>("family"),
119  " respectively. I don't know how you achieved this, but you need to rectify it.");
120 
121  if (_pars.isParamSetByUser("order") && _moose_object_pars.isParamSetByUser("order") &&
122  !_pars.get<MooseEnum>("order").compareCurrent(_moose_object_pars.get<MooseEnum>("order")))
123  mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
124  "`order` parameter set, and they are different values: ",
125  _moose_object_pars.get<MooseEnum>("order"),
126  " and ",
127  _pars.get<MooseEnum>("order"),
128  " respectively. I don't know how you achieved this, but you need to rectify it.");
129 
130  if (_pars.isParamSetByUser("scaling") && _moose_object_pars.isParamSetByUser("scaling") &&
131  _pars.get<std::vector<Real>>("scaling") !=
132  _moose_object_pars.get<std::vector<Real>>("scaling"))
133  mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
134  "`scaling` parameter set, and they are different values. I don't know how you "
135  "achieved this, but you need to rectify it.");
136 
137  if (_pars.isParamSetByUser("initial_condition") &&
138  _pars.isParamSetByUser("initial_from_file_var"))
139  paramError("initial_condition",
140  "Two initial conditions have been provided for the variable ",
141  name(),
142  " using the 'initial_condition' and 'initial_from_file_var' parameters. Please "
143  "remove one of them.");
144 
145  _moose_object_pars.applySpecificParameters(_pars, {"order", "family", "scaling"});
146 
147  // Determine the MooseVariable type
148  _fv_var = _moose_object_pars.get<bool>("fv");
149  const auto is_array = _components > 1 || _moose_object_pars.get<bool>("array");
150  if (_type == "MooseVariableBase")
151  _type = variableType(_fe_type, _fv_var, is_array);
152  if (_fv_var)
153  _problem->needFV();
154 
155  // Need static_cast to resolve overloads
156  _problem_add_var_method = static_cast<void (FEProblemBase::*)(
157  const std::string &, const std::string &, InputParameters &)>(&FEProblemBase::addVariable);
158 }
159 
160 void
162 {
163  // If we've been called that means that current_task == "add_variable"
164  init();
165 
166  // Get necessary data for creating a variable
167  std::string var_name = name();
168  addVariable(var_name);
169 
170  // Set the initial condition
171  if (_pars.isParamValid("initial_condition"))
173 }
174 
175 void
177 {
178  // Variable name
179  std::string var_name = name();
180 
181  auto value = _pars.get<std::vector<Real>>("initial_condition");
182 
183  // Create the object name
184  std::string long_name("");
185  long_name += var_name;
186  long_name += "_moose";
187 
188  // Set the parameters for the action
189  InputParameters action_params = _action_factory.getValidParams("AddOutputAction");
190  action_params.set<ActionWarehouse *>("awh") = &_awh;
191 
192  // Associate all action and initial condition errors with "initial_condition"
193  associateWithParameter("initial_condition", action_params);
194 
195  const auto fe_field_type = FEInterface::field_type(_fe_type);
196  const bool is_vector = fe_field_type == TYPE_VECTOR;
197 
198  if (_scalar_var)
199  action_params.set<std::string>("type") = "ScalarConstantIC";
200  else if (_components == 1)
201  {
202  if (is_vector)
203  action_params.set<std::string>("type") = "VectorConstantIC";
204  else
205  {
206  if (_fv_var)
207  action_params.set<std::string>("type") = "FVConstantIC";
208  else
209  action_params.set<std::string>("type") = "ConstantIC";
210  }
211  }
212  else
213  {
214  action_params.set<std::string>("type") = "ArrayConstantIC";
215  if (value.size() != _components)
216  mooseError("Size of 'initial_condition' is not consistent");
217  }
218 
219  // Create the action
220  std::shared_ptr<MooseObjectAction> action;
221  if (_fv_var)
222  action = std::static_pointer_cast<MooseObjectAction>(
223  _action_factory.create("AddFVInitialConditionAction", long_name, action_params));
224  else
225  action = std::static_pointer_cast<MooseObjectAction>(
226  _action_factory.create("AddInitialConditionAction", long_name, action_params));
227 
228  // Set the required parameters for the object to be created
229  action->getObjectParams().set<VariableName>("variable") = var_name;
230  if (_components > 1)
231  {
233  for (unsigned int i = 0; i < _components; ++i)
234  v(i) = value[i];
235  action->getObjectParams().set<RealEigenVector>("value") = v;
236  }
237  else if (is_vector)
238  {
239  action->getObjectParams().set<Real>("x_value") = value[0];
240  if (value.size() > 0)
241  action->getObjectParams().set<Real>("y_value") = value[1];
242  if (value.size() > 1)
243  action->getObjectParams().set<Real>("z_value") = value[2];
244  }
245  else
246  action->getObjectParams().set<Real>("value") = value[0];
247 
248  // Store the action in the ActionWarehouse
249  _awh.addActionBlock(action);
250 }
251 
252 std::string
253 AddVariableAction::determineType(const FEType & fe_type, unsigned int components, bool is_fv)
254 {
255  ::mooseDeprecated("AddVariableAction::determineType() is deprecated. Use "
256  "AddVariableAction::variableType() instead.");
257  return variableType(fe_type, is_fv, components > 1);
258 }
259 
260 std::string
261 AddVariableAction::variableType(const FEType & fe_type, const bool is_fv, const bool is_array)
262 {
263  if (is_fv)
264  return "MooseVariableFVReal";
265 
266  const auto fe_field_type = FEInterface::field_type(fe_type);
267 
268  if (is_array)
269  {
270  if (fe_field_type == TYPE_VECTOR)
271  ::mooseError("Vector finite element families do not currently have ArrayVariable support");
272  else
273  return "ArrayMooseVariable";
274  }
275  else if (fe_type == FEType(0, MONOMIAL))
276  return "MooseVariableConstMonomial";
277  else if (fe_type.family == SCALAR)
278  return "MooseVariableScalar";
279  else if (fe_field_type == TYPE_VECTOR)
280  return "VectorMooseVariable";
281  else
282  return "MooseVariable";
283 }
284 
285 void
286 AddVariableAction::addVariable(const std::string & var_name)
287 {
288  // Compare sizes of scaling_factor and components for Array Variables
289  const auto & scale_factor = _moose_object_pars.isParamValid("scaling")
290  ? _moose_object_pars.get<std::vector<Real>>("scaling")
291  : std::vector<Real>(_components, 1);
292  if (scale_factor.size() != _components)
293  mooseError("Size of 'scaling' is not consistent");
294 
296 
297  if (_moose_object_pars.get<bool>("eigen"))
298  {
299  // MooseEigenSystem will be eventually removed. NonlinearEigenSystem will be used intead.
300  // It is legal for NonlinearEigenSystem to specify a variable as eigen in input file,
301  // but we do not need to do anything here.
302  MooseEigenSystem * esys =
303  dynamic_cast<MooseEigenSystem *>(&_problem->getNonlinearSystemBase(/*nl_sys=*/0));
304  if (esys)
305  esys->markEigenVariable(var_name);
306  }
307 }
308 
309 std::set<SubdomainID>
311 {
312  // Extract and return the block ids supplied in the input
313  std::set<SubdomainID> blocks;
314  std::vector<SubdomainName> block_param =
315  _moose_object_pars.get<std::vector<SubdomainName>>("block");
316  for (const auto & subdomain_name : block_param)
317  {
318  SubdomainID blk_id = _problem->mesh().getSubdomainID(subdomain_name);
319  blocks.insert(blk_id);
320  }
321  return blocks;
322 }
static InputParameters validParams()
std::set< SubdomainID > getSubdomainIDs()
Get the block ids from the input parameters.
bool _scalar_var
True if the variable being created is a scalar.
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
void mooseDeprecated(Args &&... args) const
ActionWarehouse & _awh
Reference to ActionWarehouse where we store object build by actions.
Definition: Action.h:159
Adds nonlinear variable.
char ** blocks
void applySpecificParameters(const InputParameters &common, const std::vector< std::string > &include, bool allow_private=false)
Method for applying common parameters.
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:92
static InputParameters validParams()
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
static InputParameters validParams()
registerMooseAction("MooseApp", AddVariableAction, "add_variable")
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters &parameters)
Definition: ActionFactory.C:39
static MooseEnum getNonlinearVariableFamilies()
Get the possible variable families.
libMesh::FEType _fe_type
FEType for the variable being created.
bool _fv_var
True if the variable being created is finite volume.
InputParameters & getObjectParams()
Retrieve the parameters of the object to be created by this action.
static MooseEnum getNonlinearVariableOrders()
Get the possible variable orders.
unsigned int _components
Number of components for an array variable.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
static std::string variableType(const libMesh::FEType &fe_type, const bool is_fv=false, const bool is_array=false)
Determines a variable type.
virtual void markEigenVariable(const VariableName &var_name)
Mark a variable as a variable of the eigen system.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
ActionFactory & _action_factory
Builds Actions.
virtual void addVariable(const std::string &var_type, const std::string &var_name, InputParameters &params)
Canonical method for adding a non-linear variable.
MONOMIAL
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was by the user.
static libMesh::FEType feType(const InputParameters &params)
determine the FEType by examining family and order in the provided parameters
std::string _type
The Object type that is being created.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
InputParameters _moose_object_pars
The parameters for the object to be created.
virtual void init()
Initialize the action&#39;s member variables.
void associateWithParameter(const std::string &param_name, InputParameters &params) const
Associates the object&#39;s parameters params with the input location from this Action&#39;s parameter with t...
Definition: Action.C:158
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:168
void addVariable(const std::string &var_name)
Adds a nonlinear variable to the system.
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealEigenVector
Definition: MooseTypes.h:146
AddVariableAction(const InputParameters &params)
static std::string determineType(const libMesh::FEType &fe_type, unsigned int components, bool is_fv=false)
DEPRECATED: Use variableType instead.
TYPE_VECTOR
void createInitialConditionAction()
Create the action to generate the InitialCondition object.
std::function< void(FEProblemBase &, const std::string &, const std::string &, InputParameters &)> _problem_add_var_method
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.