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 : #include "ParsedAux.h"
11 :
12 : registerMooseObject("MooseApp", ParsedAux);
13 :
14 : InputParameters
15 17839 : ParsedAux::validParams()
16 : {
17 17839 : InputParameters params = AuxKernel::validParams();
18 17839 : params += FunctionParserUtils<false>::validParams();
19 35678 : params.addClassDescription(
20 : "Sets a field variable value to the evaluation of a parsed expression.");
21 :
22 107034 : params.addRequiredCustomTypeParam<std::string>(
23 : "expression", "FunctionExpression", "Parsed function expression to compute");
24 71356 : params.addCoupledVar("coupled_variables", "Vector of coupled variable names");
25 :
26 71356 : params.addParam<std::vector<MaterialPropertyName>>(
27 : "material_properties", {}, "Material properties (Real-valued) in the expression");
28 71356 : params.addParam<std::vector<MaterialPropertyName>>(
29 : "ad_material_properties", {}, "AD material properties (ADReal-valued) in the expression");
30 :
31 53517 : params.addParam<bool>(
32 : "use_xyzt",
33 35678 : false,
34 : "Make coordinate (x,y,z) and time (t) variables available in the function expression.");
35 71356 : params.addParam<std::vector<std::string>>(
36 : "constant_names",
37 : {},
38 : "Vector of constants used in the parsed function (use this for kB etc.)");
39 71356 : params.addParam<std::vector<std::string>>(
40 : "constant_expressions",
41 : {},
42 : "Vector of values for the constants in constant_names (can be an FParser expression)");
43 71356 : params.addParam<std::vector<MooseFunctorName>>(
44 : "functor_names", {}, "Functors to use in the parsed expression");
45 71356 : params.addParam<std::vector<std::string>>(
46 : "functor_symbols",
47 : {},
48 : "Symbolic name to use for each functor in 'functor_names' in the parsed expression. If not "
49 : "provided, then the actual functor names will be used in the parsed expression.");
50 35678 : params.addParam<bool>(
51 : "evaluate_functors_on_qp",
52 35678 : true,
53 : "Whether to evaluate functors using the ElemQpArg/ElemSideQpArg or the ElemArg/FaceArg "
54 : "functor arguments. The behavior of a functor for each argument is implementation-defined by "
55 : "the functor. But for most functors, the former two use the quadrature rule points as "
56 : "evaluation locations, while the latter two use the element centroid/face centroid.");
57 17839 : return params;
58 0 : }
59 :
60 1552 : ParsedAux::ParsedAux(const InputParameters & parameters)
61 : : AuxKernel(parameters),
62 : FunctionParserUtils(parameters),
63 1548 : _function(getParam<std::string>("expression")),
64 3096 : _nargs(coupledComponents("coupled_variables")),
65 3096 : _args(coupledValues("coupled_variables")),
66 3096 : _matprop_names(getParam<std::vector<MaterialPropertyName>>("material_properties")),
67 3096 : _ad_matprop_names(getParam<std::vector<MaterialPropertyName>>("ad_material_properties")),
68 1548 : _n_matprops(_matprop_names.size()),
69 1548 : _n_ad_matprops(_ad_matprop_names.size()),
70 3096 : _use_xyzt(getParam<bool>("use_xyzt")),
71 9288 : _xyzt({"x", "y", "z", "t"}),
72 3096 : _functor_names(getParam<std::vector<MooseFunctorName>>("functor_names")),
73 1548 : _n_functors(_functor_names.size()),
74 3096 : _functor_symbols(getParam<std::vector<std::string>>("functor_symbols")),
75 9292 : _use_qp_functor_arguments(getParam<bool>("evaluate_functors_on_qp"))
76 : {
77 :
78 2253 : for (const auto i : make_range(_nargs))
79 2115 : _coupled_variable_names.push_back(getFieldVar("coupled_variables", i)->name());
80 :
81 : // sanity checks
82 1548 : if (!_functor_symbols.empty() && (_functor_symbols.size() != _n_functors))
83 8 : paramError("functor_symbols", "functor_symbols must be the same length as functor_names.");
84 3686 : if (isNodal() && _use_qp_functor_arguments && isParamSetByUser("evaluate_functors_on_qp"))
85 0 : paramError("evaluate_functors_on_qp", "QPs are not used for computing variable nodal values.");
86 :
87 1544 : validateFunctorSymbols();
88 1536 : validateFunctorNames();
89 : // We need the FaceInfo generated
90 1528 : if (_bnd && !_use_qp_functor_arguments)
91 28 : _subproblem.needFV();
92 :
93 : // build variables argument
94 1528 : std::string variables;
95 :
96 : // coupled field variables
97 2217 : for (const auto i : index_range(_coupled_variable_names))
98 689 : variables += (i == 0 ? "" : ",") + _coupled_variable_names[i];
99 :
100 : // adding functors to the expression
101 1528 : if (_functor_symbols.size())
102 212 : for (const auto & symbol : _functor_symbols)
103 115 : variables += (variables.empty() ? "" : ",") + symbol;
104 : else
105 1487 : for (const auto & name : _functor_names)
106 56 : variables += (variables.empty() ? "" : ",") + name;
107 :
108 : // material properties
109 1556 : for (const auto & matprop : _matprop_names)
110 28 : variables += (variables.empty() ? "" : ",") + matprop;
111 1556 : for (const auto & matprop : _ad_matprop_names)
112 28 : variables += (variables.empty() ? "" : ",") + matprop;
113 1528 : if (isNodal() && (_matprop_names.size() || _ad_matprop_names.size()))
114 0 : mooseError("Material properties cannot be retrieved in a nodal auxkernel. Use a different "
115 : "auxiliary variable family.");
116 :
117 : // positions and time
118 1528 : if (_use_xyzt)
119 4480 : for (auto & v : _xyzt)
120 3584 : variables += (variables.empty() ? "" : ",") + v;
121 :
122 : // Create parsed function
123 1528 : _func_F = std::make_shared<SymFunction>();
124 7632 : parsedFunctionSetup(_func_F,
125 1528 : _function,
126 : variables,
127 : getParam<std::vector<std::string>>("constant_names"),
128 : getParam<std::vector<std::string>>("constant_expressions"),
129 : comm());
130 :
131 : // reserve storage for parameter passing buffer
132 1524 : _func_params.resize(_nargs + _n_functors + _n_matprops + _n_ad_matprops + (_use_xyzt ? 4 : 0));
133 :
134 : // keep pointers to the material properties
135 1552 : for (const auto & name : _matprop_names)
136 28 : _matprops.push_back(&getMaterialProperty<Real>(name));
137 1552 : for (const auto & name : _ad_matprop_names)
138 28 : _ad_matprops.push_back(&getADMaterialProperty<Real>(name));
139 :
140 : // keep pointers to the functors
141 1687 : for (const auto & name : _functor_names)
142 163 : _functors.push_back(&getFunctor<Real>(name));
143 6168 : }
144 :
145 : Real
146 4801866 : ParsedAux::computeValue()
147 : {
148 : // Variables
149 5489237 : for (const auto j : make_range(_nargs))
150 687371 : _func_params[j] = (*_args[j])[_qp];
151 :
152 : // Functors
153 4801866 : const auto & state = determineState();
154 4801866 : if (isNodal())
155 : {
156 1558984 : const Moose::NodeArg node_arg = {_current_node,
157 1558984 : &Moose::NodeArg::undefined_subdomain_connection};
158 1645251 : for (const auto i : index_range(_functors))
159 86267 : _func_params[_nargs + i] = (*_functors[i])(node_arg, state);
160 : }
161 3242882 : else if (_bnd && _use_qp_functor_arguments)
162 : {
163 : const Moose::ElemSideQpArg side_qp_arg = {
164 5208 : _current_elem, _current_side, _qp, _qrule, _q_point[_qp]};
165 10416 : for (const auto i : index_range(_functors))
166 5208 : _func_params[_nargs + i] = (*_functors[i])(side_qp_arg, state);
167 5208 : }
168 3237674 : else if (_bnd)
169 : {
170 5208 : const Moose::FaceArg face_arg = {_mesh.faceInfo(_current_elem, _current_side),
171 : Moose::FV::LimiterType::CentralDifference,
172 : /*elem_is_upwind*/ true,
173 : /*correct skewness*/ true,
174 : /*face_side*/ nullptr,
175 5208 : /*state_limiter*/ nullptr};
176 10416 : for (const auto i : index_range(_functors))
177 5208 : _func_params[_nargs + i] = (*_functors[i])(face_arg, state);
178 : }
179 3232466 : else if (_use_qp_functor_arguments)
180 : {
181 3232466 : const Moose::ElemQpArg qp_arg = {_current_elem, _qp, _qrule, _q_point[_qp]};
182 3232466 : for (const auto i : index_range(_functors))
183 0 : _func_params[_nargs + i] = (*_functors[i])(qp_arg, state);
184 : }
185 : else
186 : {
187 0 : const Moose::ElemArg elem_arg = {_current_elem, /*correct skewness*/ true};
188 0 : for (const auto i : index_range(_functors))
189 0 : _func_params[_nargs + i] = (*_functors[i])(elem_arg, state);
190 : }
191 :
192 : // Material properties
193 5018666 : for (const auto j : make_range(_n_matprops))
194 216800 : _func_params[_nargs + _n_functors + j] = (*_matprops[j])[_qp];
195 5018666 : for (const auto j : make_range(_n_ad_matprops))
196 216800 : _func_params[_nargs + _n_functors + _n_matprops + j] = (*_ad_matprops[j])[_qp].value();
197 :
198 : // Positions and time
199 4801866 : if (_use_xyzt)
200 : {
201 15950048 : for (const auto j : make_range(Moose::dim))
202 11962536 : _func_params[_nargs + _n_functors + _n_matprops + _n_ad_matprops + j] =
203 11962536 : isNodal() ? (*_current_node)(j) : _q_point[_qp](j);
204 3987512 : _func_params[_nargs + _n_functors + _n_matprops + _n_ad_matprops + 3] = _t;
205 : }
206 :
207 14405598 : return evaluate(_func_F);
208 : }
209 :
210 : void
211 1544 : ParsedAux::validateFunctorSymbols()
212 : {
213 1544 : validateGenericVectorNames(_functor_symbols, "functor_symbols");
214 1536 : }
215 :
216 : void
217 1536 : ParsedAux::validateFunctorNames()
218 : {
219 1536 : validateGenericVectorNames(_functor_names, "functor_names");
220 1528 : }
|