Line data Source code
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 :
27 : InputParameters
28 1140794 : AddVariableAction::validParams()
29 : {
30 1140794 : auto params = MooseObjectAction::validParams();
31 2281588 : 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 1140794 : params.set<std::string>("type") = "MooseVariableBase";
36 :
37 : // The below is for backwards compatibility
38 1140794 : MooseEnum families(AddVariableAction::getNonlinearVariableFamilies());
39 1140794 : MooseEnum orders(AddVariableAction::getNonlinearVariableOrders());
40 4563176 : params.addParam<MooseEnum>(
41 : "family", families, "Specifies the family of FE shape functions to use for this variable");
42 4563176 : 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 4563176 : params.addParam<std::vector<Real>>("scaling",
48 : "Specifies a scaling factor to apply to this variable");
49 4563176 : params.addParam<std::vector<Real>>("initial_condition",
50 : "Specifies a constant initial condition for this variable");
51 5703970 : params.transferParam<std::string>(CopyNodalVarsAction::validParams(), "initial_from_file_var");
52 2281588 : return params;
53 1140794 : }
54 :
55 126098 : AddVariableAction::AddVariableAction(const InputParameters & params)
56 : : MooseObjectAction(params),
57 126098 : _fe_type(feType(params)),
58 126098 : _scalar_var(_fe_type.family == SCALAR),
59 126098 : _fv_var(false),
60 126098 : _components(1)
61 : {
62 126098 : }
63 :
64 : MooseEnum
65 1647895 : AddVariableAction::getNonlinearVariableFamilies()
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 6591580 : "LAGRANGE");
72 : }
73 :
74 : MooseEnum
75 1184282 : AddVariableAction::getNonlinearVariableOrders()
76 : {
77 4737128 : return MooseEnum("CONSTANT FIRST SECOND THIRD FOURTH", "FIRST", true);
78 : }
79 :
80 : FEType
81 126200 : AddVariableAction::feType(const InputParameters & params)
82 : {
83 126200 : return {Utility::string_to_enum<Order>(params.get<MooseEnum>("order")),
84 126200 : Utility::string_to_enum<FEFamily>(params.get<MooseEnum>("family"))};
85 : }
86 :
87 : void
88 122276 : AddVariableAction::init()
89 : {
90 122276 : _components = _moose_object_pars.get<unsigned int>("components");
91 122276 : if (_components == 0)
92 0 : 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 668894 : if (isParamSetByUser("family") && _moose_object_pars.isParamSetByUser("family") &&
104 302066 : !getParam<MooseEnum>("family").compareCurrent(_moose_object_pars.get<MooseEnum>("family")))
105 0 : mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
106 : "`family` parameter set, and they are different values: ",
107 0 : _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 671921 : if (isParamSetByUser("order") && _moose_object_pars.isParamSetByUser("order") &&
113 305093 : !getParam<MooseEnum>("order").compareCurrent(_moose_object_pars.get<MooseEnum>("order")))
114 0 : mooseError("Both the MooseVariable* and Add*VariableAction parameters objects have had the "
115 : "`order` parameter set, and they are different values: ",
116 0 : _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 490361 : if (isParamSetByUser("scaling") && _moose_object_pars.isParamSetByUser("scaling") &&
122 123533 : getParam<std::vector<Real>>("scaling") !=
123 272 : _moose_object_pars.get<std::vector<Real>>("scaling"))
124 0 : 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 399556 : if (isParamSetByUser("initial_condition") && isParamSetByUser("initial_from_file_var"))
129 8 : paramError("initial_condition",
130 : "Two initial conditions have been provided for the variable ",
131 4 : name(),
132 : " using the 'initial_condition' and 'initial_from_file_var' parameters. Please "
133 : "remove one of them.");
134 :
135 244544 : _moose_object_pars.applySpecificParameters(_pars, {"order", "family", "scaling"});
136 :
137 : // Determine the MooseVariable type
138 122272 : _fv_var = _moose_object_pars.get<bool>("fv");
139 122272 : const auto is_array = _components > 1 || _moose_object_pars.get<bool>("array");
140 122272 : if (_type == "MooseVariableBase")
141 115461 : _type = variableType(_fe_type, _fv_var, is_array);
142 122272 : if (_fv_var)
143 7791 : _problem->needFV();
144 :
145 : // Need static_cast to resolve overloads
146 244544 : _problem_add_var_method = static_cast<void (FEProblemBase::*)(
147 122272 : const std::string &, const std::string &, InputParameters &)>(&FEProblemBase::addVariable);
148 122272 : }
149 :
150 : void
151 122276 : AddVariableAction::act()
152 : {
153 : // If we've been called that means that current_task == "add_variable"
154 122276 : init();
155 :
156 : // Get necessary data for creating a variable
157 122268 : const auto var_name = varName();
158 122268 : addVariable(var_name);
159 :
160 : // Set the initial condition
161 366708 : if (isParamValid("initial_condition"))
162 : {
163 32712 : const auto & value = getParam<std::vector<Real>>("initial_condition");
164 16356 : createInitialConditionAction(value);
165 : }
166 122236 : }
167 :
168 : void
169 16356 : AddVariableAction::createInitialConditionAction(const std::vector<Real> & value)
170 : {
171 : // Variable name
172 16356 : const auto var_name = varName();
173 :
174 : // Create the object name
175 16356 : std::string long_name("");
176 16356 : long_name += var_name;
177 16356 : long_name += "_moose";
178 :
179 : // Set the parameters for the action
180 32712 : InputParameters action_params = _action_factory.getValidParams("AddOutputAction");
181 32712 : action_params.set<ActionWarehouse *>("awh") = &_awh;
182 :
183 : // Associate all action and initial condition errors with "initial_condition"
184 32712 : associateWithParameter("initial_condition", action_params);
185 :
186 16356 : const auto fe_field_type = FEInterface::field_type(_fe_type);
187 16356 : const bool is_vector = fe_field_type == TYPE_VECTOR;
188 :
189 16356 : if (_scalar_var)
190 2360 : action_params.set<std::string>("type") = "ScalarConstantIC";
191 15176 : else if (_components == 1)
192 : {
193 14695 : if (is_vector)
194 182 : action_params.set<std::string>("type") = "VectorConstantIC";
195 : else
196 : {
197 14604 : if (_fv_var)
198 5210 : action_params.set<std::string>("type") = "FVConstantIC";
199 : else
200 23998 : action_params.set<std::string>("type") = "ConstantIC";
201 : }
202 : }
203 : else
204 : {
205 481 : action_params.set<std::string>("type") = "ArrayConstantIC";
206 481 : if (value.size() != _components)
207 0 : mooseError("Size of 'initial_condition' is not consistent");
208 : }
209 :
210 : // Create the action
211 16356 : std::shared_ptr<MooseObjectAction> action;
212 16356 : if (_fv_var)
213 5210 : action = std::static_pointer_cast<MooseObjectAction>(
214 13025 : _action_factory.create("AddFVInitialConditionAction", long_name, action_params));
215 : else
216 27502 : action = std::static_pointer_cast<MooseObjectAction>(
217 68755 : _action_factory.create("AddInitialConditionAction", long_name, action_params));
218 :
219 : // Set the required parameters for the object to be created
220 49068 : action->getObjectParams().set<VariableName>("variable") = var_name;
221 16356 : if (_components > 1)
222 : {
223 481 : RealEigenVector v(_components);
224 1589 : for (unsigned int i = 0; i < _components; ++i)
225 1108 : v(i) = value[i];
226 962 : action->getObjectParams().set<RealEigenVector>("value") = v;
227 481 : }
228 15875 : else if (is_vector)
229 : {
230 182 : action->getObjectParams().set<Real>("x_value") = value[0];
231 91 : if (value.size() > 0)
232 273 : action->getObjectParams().set<Real>("y_value") = value[1];
233 91 : if (value.size() > 1)
234 273 : action->getObjectParams().set<Real>("z_value") = value[2];
235 : }
236 : else
237 47352 : action->getObjectParams().set<Real>("value") = value[0];
238 :
239 : // Store the action in the ActionWarehouse
240 16356 : _awh.addActionBlock(action);
241 16356 : }
242 :
243 : std::string
244 0 : AddVariableAction::determineType(const FEType & fe_type, unsigned int components, bool is_fv)
245 : {
246 0 : ::mooseDeprecated("AddVariableAction::determineType() is deprecated. Use "
247 : "AddVariableAction::variableType() instead.");
248 0 : return variableType(fe_type, is_fv, components > 1);
249 : }
250 :
251 : std::string
252 115602 : AddVariableAction::variableType(const FEType & fe_type, const bool is_fv, const bool is_array)
253 : {
254 115602 : if (is_fv)
255 3270 : return "MooseVariableFVReal";
256 :
257 113967 : const auto fe_field_type = FEInterface::field_type(fe_type);
258 :
259 113967 : if (is_array)
260 : {
261 2232 : if (fe_field_type == TYPE_VECTOR)
262 0 : ::mooseError("Vector finite element families do not currently have ArrayVariable support");
263 : else
264 4464 : return "ArrayMooseVariable";
265 : }
266 111735 : else if (fe_type == FEType(0, MONOMIAL))
267 40256 : return "MooseVariableConstMonomial";
268 91607 : else if (fe_type.family == SCALAR)
269 6124 : return "MooseVariableScalar";
270 88545 : else if (fe_field_type == TYPE_VECTOR)
271 3062 : return "VectorMooseVariable";
272 : else
273 174028 : return "MooseVariable";
274 : }
275 :
276 : void
277 125038 : AddVariableAction::addVariable(const std::string & var_name)
278 : {
279 : // Compare sizes of scaling_factor and components for Array Variables
280 250076 : const auto & scale_factor = _moose_object_pars.isParamValid("scaling")
281 125310 : ? _moose_object_pars.get<std::vector<Real>>("scaling")
282 374276 : : std::vector<Real>(_components, 1);
283 125038 : if (scale_factor.size() != _components)
284 0 : mooseError("Size of 'scaling' is not consistent");
285 :
286 125038 : _problem_add_var_method(*_problem, _type, var_name, _moose_object_pars);
287 :
288 125006 : 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 24 : dynamic_cast<MooseEigenSystem *>(&_problem->getNonlinearSystemBase(/*nl_sys=*/0));
295 24 : if (esys)
296 11 : esys->markEigenVariable(var_name);
297 : }
298 125006 : }
299 :
300 : std::set<SubdomainID>
301 0 : AddVariableAction::getSubdomainIDs()
302 : {
303 : // Extract and return the block ids supplied in the input
304 0 : std::set<SubdomainID> blocks;
305 : std::vector<SubdomainName> block_param =
306 0 : _moose_object_pars.get<std::vector<SubdomainName>>("block");
307 0 : for (const auto & subdomain_name : block_param)
308 : {
309 0 : SubdomainID blk_id = _problem->mesh().getSubdomainID(subdomain_name);
310 0 : blocks.insert(blk_id);
311 : }
312 0 : return blocks;
313 0 : }
|