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 16698 : ParsedAux::validParams()
16 : {
17 16698 : InputParameters params = AuxKernel::validParams();
18 16698 : params += FunctionParserUtils<false>::validParams();
19 16698 : params.addClassDescription(
20 : "Sets a field variable value to the evaluation of a parsed expression.");
21 :
22 16698 : params.addRequiredCustomTypeParam<std::string>(
23 : "function", "FunctionExpression", "Parsed function expression to compute");
24 16698 : params.deprecateParam("function", "expression", "02/07/2024");
25 16698 : params.addCoupledVar("args", "Vector of coupled variable names");
26 16698 : params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
27 :
28 16698 : params.addParam<std::vector<MaterialPropertyName>>(
29 : "material_properties", {}, "Material properties (Real-valued) in the expression");
30 16698 : params.addParam<std::vector<MaterialPropertyName>>(
31 : "ad_material_properties", {}, "AD material properties (ADReal-valued) in the expression");
32 :
33 50094 : params.addParam<bool>(
34 : "use_xyzt",
35 33396 : false,
36 : "Make coordinate (x,y,z) and time (t) variables available in the function expression.");
37 16698 : params.addParam<std::vector<std::string>>(
38 : "constant_names",
39 : {},
40 : "Vector of constants used in the parsed function (use this for kB etc.)");
41 16698 : params.addParam<std::vector<std::string>>(
42 : "constant_expressions",
43 : {},
44 : "Vector of values for the constants in constant_names (can be an FParser expression)");
45 16698 : params.addParam<std::vector<MooseFunctorName>>(
46 : "functor_names", {}, "Functors to use in the parsed expression");
47 16698 : params.addParam<std::vector<std::string>>(
48 : "functor_symbols",
49 : {},
50 : "Symbolic name to use for each functor in 'functor_names' in the parsed expression. If not "
51 : "provided, then the actual functor names will be used in the parsed expression.");
52 :
53 16698 : return params;
54 0 : }
55 :
56 1275 : ParsedAux::ParsedAux(const InputParameters & parameters)
57 : : AuxKernel(parameters),
58 : FunctionParserUtils(parameters),
59 1271 : _function(getParam<std::string>("expression")),
60 1271 : _nargs(coupledComponents("coupled_variables")),
61 1271 : _args(coupledValues("coupled_variables")),
62 1271 : _matprop_names(getParam<std::vector<MaterialPropertyName>>("material_properties")),
63 1271 : _ad_matprop_names(getParam<std::vector<MaterialPropertyName>>("ad_material_properties")),
64 1271 : _n_matprops(_matprop_names.size()),
65 1271 : _n_ad_matprops(_ad_matprop_names.size()),
66 1271 : _use_xyzt(getParam<bool>("use_xyzt")),
67 7626 : _xyzt({"x", "y", "z", "t"}),
68 1271 : _functor_names(getParam<std::vector<MooseFunctorName>>("functor_names")),
69 1271 : _n_functors(_functor_names.size()),
70 5088 : _functor_symbols(getParam<std::vector<std::string>>("functor_symbols"))
71 : {
72 :
73 1903 : for (const auto i : make_range(_nargs))
74 632 : _coupled_variable_names.push_back(getFieldVar("coupled_variables", i)->name());
75 :
76 : // sanity checks
77 1271 : if (!_functor_symbols.empty() && (_functor_symbols.size() != _n_functors))
78 4 : paramError("functor_symbols", "functor_symbols must be the same length as functor_names.");
79 :
80 1267 : validateFunctorSymbols();
81 1259 : validateFunctorNames();
82 :
83 : // build variables argument
84 1251 : std::string variables;
85 :
86 : // coupled field variables
87 1867 : for (const auto i : index_range(_coupled_variable_names))
88 616 : variables += (i == 0 ? "" : ",") + _coupled_variable_names[i];
89 :
90 : // adding functors to the expression
91 1251 : if (_functor_symbols.size())
92 129 : for (const auto & symbol : _functor_symbols)
93 73 : variables += (variables.empty() ? "" : ",") + symbol;
94 : else
95 1195 : for (const auto & name : _functor_names)
96 0 : variables += (variables.empty() ? "" : ",") + name;
97 :
98 : // material properties
99 1277 : for (const auto & matprop : _matprop_names)
100 26 : variables += (variables.empty() ? "" : ",") + matprop;
101 1277 : for (const auto & matprop : _ad_matprop_names)
102 26 : variables += (variables.empty() ? "" : ",") + matprop;
103 1251 : if (isNodal() && (_matprop_names.size() || _ad_matprop_names.size()))
104 0 : mooseError("Material properties cannot be retrieved in a nodal auxkernel. Use a different "
105 : "auxiliary variable family.");
106 :
107 : // positions and time
108 1251 : if (_use_xyzt)
109 3870 : for (auto & v : _xyzt)
110 3096 : variables += (variables.empty() ? "" : ",") + v;
111 :
112 : // Create parsed function
113 1251 : _func_F = std::make_shared<SymFunction>();
114 2498 : parsedFunctionSetup(_func_F,
115 1251 : _function,
116 : variables,
117 : getParam<std::vector<std::string>>("constant_names"),
118 : getParam<std::vector<std::string>>("constant_expressions"),
119 : comm());
120 :
121 : // reserve storage for parameter passing buffer
122 1247 : _func_params.resize(_nargs + _n_functors + _n_matprops + _n_ad_matprops + (_use_xyzt ? 4 : 0));
123 :
124 : // keep pointers to the material properties
125 1273 : for (const auto & name : _matprop_names)
126 26 : _matprops.push_back(&getMaterialProperty<Real>(name));
127 1273 : for (const auto & name : _ad_matprop_names)
128 26 : _ad_matprops.push_back(&getADMaterialProperty<Real>(name));
129 :
130 : // keep pointers to the functors
131 1312 : for (const auto & name : _functor_names)
132 65 : _functors.push_back(&getFunctor<Real>(name));
133 3789 : }
134 :
135 : Real
136 4240575 : ParsedAux::computeValue()
137 : {
138 : // Variables
139 4843665 : for (const auto j : make_range(_nargs))
140 603090 : _func_params[j] = (*_args[j])[_qp];
141 :
142 : // Functors
143 4240575 : const auto & state = determineState();
144 4240575 : if (isNodal())
145 : {
146 1378031 : const Moose::NodeArg node_arg = {_current_node,
147 1378031 : &Moose::NodeArg::undefined_subdomain_connection};
148 1454745 : for (const auto i : index_range(_functors))
149 76714 : _func_params[_nargs + i] = (*_functors[i])(node_arg, state);
150 : }
151 : else
152 : {
153 2862544 : const Moose::ElemQpArg qp_arg = {_current_elem, _qp, _qrule, _q_point[_qp]};
154 2862544 : for (const auto i : index_range(_functors))
155 0 : _func_params[_nargs + i] = (*_functors[i])(qp_arg, state);
156 : }
157 :
158 : // Material properties
159 4436575 : for (const auto j : make_range(_n_matprops))
160 196000 : _func_params[_nargs + _n_functors + j] = (*_matprops[j])[_qp];
161 4436575 : for (const auto j : make_range(_n_ad_matprops))
162 196000 : _func_params[_nargs + _n_functors + _n_matprops + j] = (*_ad_matprops[j])[_qp].value();
163 :
164 : // Positions and time
165 4240575 : if (_use_xyzt)
166 : {
167 14117460 : for (const auto j : make_range(Moose::dim))
168 10588095 : _func_params[_nargs + _n_functors + _n_matprops + _n_ad_matprops + j] =
169 10588095 : isNodal() ? (*_current_node)(j) : _q_point[_qp](j);
170 3529365 : _func_params[_nargs + _n_functors + _n_matprops + _n_ad_matprops + 3] = _t;
171 : }
172 :
173 4240575 : return evaluate(_func_F);
174 : }
175 :
176 : void
177 1267 : ParsedAux::validateFunctorSymbols()
178 : {
179 1267 : validateGenericVectorNames(_functor_symbols, "functor_symbols");
180 1259 : }
181 :
182 : void
183 1259 : ParsedAux::validateFunctorNames()
184 : {
185 1259 : validateGenericVectorNames(_functor_names, "functor_names");
186 1251 : }
|