https://mooseframework.inl.gov
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 // MOOSE includes
11 #include "AddVariableAction.h"
12 #include "FEProblem.h"
13 #include "Factory.h"
14 #include "MooseEnum.h"
15 #include "MooseEigenSystem.h"
16 #include "MooseObjectAction.h"
17 #include "MooseMesh.h"
18 #include "CopyNodalVarsAction.h"
19 
20 #include "libmesh/string_to_enum.h"
21 #include "libmesh/fe_interface.h"
22 
23 using namespace libMesh;
24 
25 registerMooseAction("MooseApp", AddVariableAction, "add_variable");
26 
29 {
30  auto params = MooseObjectAction::validParams();
31  params.addClassDescription("Add a non-linear variable to the simulation.");
32 
33  // The user may specify a type in the Variables block, but if they don't we'll just use all the
34  // parameters available from MooseVariableBase
35  params.set<std::string>("type") = "MooseVariableBase";
36 
37  // The below is for backwards compatibility
40  params.addParam<MooseEnum>(
41  "family", families, "Specifies the family of FE shape functions to use for this variable");
42  params.addParam<MooseEnum>("order",
43  orders,
44  "Specifies the order of the FE shape function to use "
45  "for this variable (additional orders not listed are "
46  "allowed)");
47  params.addParam<std::vector<Real>>("scaling",
48  "Specifies a scaling factor to apply to this variable");
49  params.addParam<std::vector<Real>>("initial_condition",
50  "Specifies a constant initial condition for this variable");
51  params.transferParam<std::string>(CopyNodalVarsAction::validParams(), "initial_from_file_var");
52  return params;
53 }
54 
56  : MooseObjectAction(params),
57  _fe_type(feType(params)),
58  _scalar_var(_fe_type.family == SCALAR),
59  _fv_var(false),
60  _components(1)
61 {
62 }
63 
66 {
67  return MooseEnum("LAGRANGE MONOMIAL HERMITE SCALAR HIERARCHIC CLOUGH XYZ SZABAB BERNSTEIN "
68  "L2_LAGRANGE L2_HIERARCHIC NEDELEC_ONE LAGRANGE_VEC MONOMIAL_VEC "
69  "RAVIART_THOMAS RATIONAL_BERNSTEIN SIDE_HIERARCHIC L2_HIERARCHIC_VEC "
70  "L2_LAGRANGE_VEC L2_RAVIART_THOMAS",
71  "LAGRANGE");
72 }
73 
76 {
77  return MooseEnum("CONSTANT FIRST SECOND THIRD FOURTH", "FIRST", true);
78 }
79 
80 FEType
82 {
83  return {Utility::string_to_enum<Order>(params.get<MooseEnum>("order")),
84  Utility::string_to_enum<FEFamily>(params.get<MooseEnum>("family"))};
85 }
86 
87 void
89 {
90  _components = _moose_object_pars.get<unsigned int>("components");
91  if (_components == 0)
92  mooseError("There must be at least one variable component, but somehow 0 has been specified");
93 
94  // We have to do some sanity checks because of our work to maintain backwards compatibility.
95  // `family`, `order`, and `scaling` are all parameters duplicated between this action and the
96  // `MooseVariable*` object itself. Consequently during input file parsing, the params objects for
97  // both the action and MooseVariable object can be populated with the exact same parameters.
98  // However, some applications actually create their variables solely through creation and setting
99  // of `AddVariableAction` parameters which means that the `MooseVariableBase*` params will never
100  // be populated. So we should apply the parameters directly from the action. There should be no
101  // case in which both params objects get set by the user and they have different values
102 
103  if (isParamSetByUser("family") && _moose_object_pars.isParamSetByUser("family") &&
104  !getParam<MooseEnum>("family").compareCurrent(_moose_object_pars.get<MooseEnum>("family")))
105  mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
106  "`family` parameter set, and they are different values: ",
107  _moose_object_pars.get<MooseEnum>("family"),
108  " and ",
109  getParam<MooseEnum>("family"),
110  " respectively. I don't know how you achieved this, but you need to rectify it.");
111 
112  if (isParamSetByUser("order") && _moose_object_pars.isParamSetByUser("order") &&
113  !getParam<MooseEnum>("order").compareCurrent(_moose_object_pars.get<MooseEnum>("order")))
114  mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
115  "`order` parameter set, and they are different values: ",
116  _moose_object_pars.get<MooseEnum>("order"),
117  " and ",
118  getParam<MooseEnum>("order"),
119  " respectively. I don't know how you achieved this, but you need to rectify it.");
120 
121  if (isParamSetByUser("scaling") && _moose_object_pars.isParamSetByUser("scaling") &&
122  getParam<std::vector<Real>>("scaling") !=
123  _moose_object_pars.get<std::vector<Real>>("scaling"))
124  mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
125  "`scaling` parameter set, and they are different values. I don't know how you "
126  "achieved this, but you need to rectify it.");
127 
128  if (isParamSetByUser("initial_condition") && isParamSetByUser("initial_from_file_var"))
129  paramError("initial_condition",
130  "Two initial conditions have been provided for the variable ",
131  name(),
132  " using the 'initial_condition' and 'initial_from_file_var' parameters. Please "
133  "remove one of them.");
134 
135  _moose_object_pars.applySpecificParameters(_pars, {"order", "family", "scaling"});
136 
137  // Determine the MooseVariable type
138  _fv_var = _moose_object_pars.get<bool>("fv");
139  const auto is_array = _components > 1 || _moose_object_pars.get<bool>("array");
140  if (_type == "MooseVariableBase")
141  _type = variableType(_fe_type, _fv_var, is_array);
142  if (_fv_var)
143  _problem->needFV();
144 
145  // Need static_cast to resolve overloads
146  _problem_add_var_method = static_cast<void (FEProblemBase::*)(
147  const std::string &, const std::string &, InputParameters &)>(&FEProblemBase::addVariable);
148 }
149 
150 void
152 {
153  // If we've been called that means that current_task == "add_variable"
154  init();
155 
156  // Get necessary data for creating a variable
157  const auto var_name = varName();
158  addVariable(var_name);
159 
160  // Set the initial condition
161  if (isParamValid("initial_condition"))
162  {
163  const auto & value = getParam<std::vector<Real>>("initial_condition");
165  }
166 }
167 
168 void
169 AddVariableAction::createInitialConditionAction(const std::vector<Real> & value)
170 {
171  // Variable name
172  const auto var_name = varName();
173 
174  // Create the object name
175  std::string long_name("");
176  long_name += var_name;
177  long_name += "_moose";
178 
179  // Set the parameters for the action
180  InputParameters action_params = _action_factory.getValidParams("AddOutputAction");
181  action_params.set<ActionWarehouse *>("awh") = &_awh;
182 
183  // Associate all action and initial condition errors with "initial_condition"
184  associateWithParameter("initial_condition", action_params);
185 
186  const auto fe_field_type = FEInterface::field_type(_fe_type);
187  const bool is_vector = fe_field_type == TYPE_VECTOR;
188 
189  if (_scalar_var)
190  action_params.set<std::string>("type") = "ScalarConstantIC";
191  else if (_components == 1)
192  {
193  if (is_vector)
194  action_params.set<std::string>("type") = "VectorConstantIC";
195  else
196  {
197  if (_fv_var)
198  action_params.set<std::string>("type") = "FVConstantIC";
199  else
200  action_params.set<std::string>("type") = "ConstantIC";
201  }
202  }
203  else
204  {
205  action_params.set<std::string>("type") = "ArrayConstantIC";
206  if (value.size() != _components)
207  mooseError("Size of 'initial_condition' is not consistent");
208  }
209 
210  // Create the action
211  std::shared_ptr<MooseObjectAction> action;
212  if (_fv_var)
213  action = std::static_pointer_cast<MooseObjectAction>(
214  _action_factory.create("AddFVInitialConditionAction", long_name, action_params));
215  else
216  action = std::static_pointer_cast<MooseObjectAction>(
217  _action_factory.create("AddInitialConditionAction", long_name, action_params));
218 
219  // Set the required parameters for the object to be created
220  action->getObjectParams().set<VariableName>("variable") = var_name;
221  if (_components > 1)
222  {
224  for (unsigned int i = 0; i < _components; ++i)
225  v(i) = value[i];
226  action->getObjectParams().set<RealEigenVector>("value") = v;
227  }
228  else if (is_vector)
229  {
230  action->getObjectParams().set<Real>("x_value") = value[0];
231  if (value.size() > 0)
232  action->getObjectParams().set<Real>("y_value") = value[1];
233  if (value.size() > 1)
234  action->getObjectParams().set<Real>("z_value") = value[2];
235  }
236  else
237  action->getObjectParams().set<Real>("value") = value[0];
238 
239  // Store the action in the ActionWarehouse
240  _awh.addActionBlock(action);
241 }
242 
243 std::string
244 AddVariableAction::determineType(const FEType & fe_type, unsigned int components, bool is_fv)
245 {
246  ::mooseDeprecated("AddVariableAction::determineType() is deprecated. Use "
247  "AddVariableAction::variableType() instead.");
248  return variableType(fe_type, is_fv, components > 1);
249 }
250 
251 std::string
252 AddVariableAction::variableType(const FEType & fe_type, const bool is_fv, const bool is_array)
253 {
254  if (is_fv)
255  return "MooseVariableFVReal";
256 
257  const auto fe_field_type = FEInterface::field_type(fe_type);
258 
259  if (is_array)
260  {
261  if (fe_field_type == TYPE_VECTOR)
262  ::mooseError("Vector finite element families do not currently have ArrayVariable support");
263  else
264  return "ArrayMooseVariable";
265  }
266  else if (fe_type == FEType(0, MONOMIAL))
267  return "MooseVariableConstMonomial";
268  else if (fe_type.family == SCALAR)
269  return "MooseVariableScalar";
270  else if (fe_field_type == TYPE_VECTOR)
271  return "VectorMooseVariable";
272  else
273  return "MooseVariable";
274 }
275 
276 void
277 AddVariableAction::addVariable(const std::string & var_name)
278 {
279  // Compare sizes of scaling_factor and components for Array Variables
280  const auto & scale_factor = _moose_object_pars.isParamValid("scaling")
281  ? _moose_object_pars.get<std::vector<Real>>("scaling")
282  : std::vector<Real>(_components, 1);
283  if (scale_factor.size() != _components)
284  mooseError("Size of 'scaling' is not consistent");
285 
287 
288  if (_moose_object_pars.get<bool>("eigen"))
289  {
290  // MooseEigenSystem will be eventually removed. NonlinearEigenSystem will be used intead.
291  // It is legal for NonlinearEigenSystem to specify a variable as eigen in input file,
292  // but we do not need to do anything here.
293  MooseEigenSystem * esys =
294  dynamic_cast<MooseEigenSystem *>(&_problem->getNonlinearSystemBase(/*nl_sys=*/0));
295  if (esys)
296  esys->markEigenVariable(var_name);
297  }
298 }
299 
300 std::set<SubdomainID>
302 {
303  // Extract and return the block ids supplied in the input
304  std::set<SubdomainID> blocks;
305  std::vector<SubdomainName> block_param =
306  _moose_object_pars.get<std::vector<SubdomainName>>("block");
307  for (const auto & subdomain_name : block_param)
308  {
309  SubdomainID blk_id = _problem->mesh().getSubdomainID(subdomain_name);
310  blocks.insert(blk_id);
311  }
312  return blocks;
313 }
static InputParameters validParams()
std::set< SubdomainID > getSubdomainIDs()
Get the block ids from the input parameters.
virtual std::string varName() const
Return the name of the nonlinear variable to be created.
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.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
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)
void createInitialConditionAction(const std::vector< Real > &value)
Create the action to generate the InitialCondition object.
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 set by the user.
bool isParamSetByUser(const std::string &nm) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
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
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.