https://mooseframework.inl.gov
ParsedAux.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 #include "ParsedAux.h"
11 
12 registerMooseObject("MooseApp", ParsedAux);
13 
16 {
19  params.addClassDescription(
20  "Sets a field variable value to the evaluation of a parsed expression.");
21 
22  params.addRequiredCustomTypeParam<std::string>(
23  "function", "FunctionExpression", "Parsed function expression to compute");
24  params.deprecateParam("function", "expression", "02/07/2024");
25  params.addCoupledVar("args", "Vector of coupled variable names");
26  params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
27 
28  params.addParam<std::vector<MaterialPropertyName>>(
29  "material_properties", {}, "Material properties (Real-valued) in the expression");
30  params.addParam<std::vector<MaterialPropertyName>>(
31  "ad_material_properties", {}, "AD material properties (ADReal-valued) in the expression");
32 
33  params.addParam<bool>(
34  "use_xyzt",
35  false,
36  "Make coordinate (x,y,z) and time (t) variables available in the function expression.");
37  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  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  params.addParam<std::vector<MooseFunctorName>>(
46  "functor_names", {}, "Functors to use in the parsed expression");
47  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  return params;
54 }
55 
57  : AuxKernel(parameters),
58  FunctionParserUtils(parameters),
59  _function(getParam<std::string>("expression")),
60  _nargs(coupledComponents("coupled_variables")),
61  _args(coupledValues("coupled_variables")),
62  _matprop_names(getParam<std::vector<MaterialPropertyName>>("material_properties")),
63  _ad_matprop_names(getParam<std::vector<MaterialPropertyName>>("ad_material_properties")),
64  _n_matprops(_matprop_names.size()),
65  _n_ad_matprops(_ad_matprop_names.size()),
66  _use_xyzt(getParam<bool>("use_xyzt")),
67  _xyzt({"x", "y", "z", "t"}),
68  _functor_names(getParam<std::vector<MooseFunctorName>>("functor_names")),
69  _n_functors(_functor_names.size()),
70  _functor_symbols(getParam<std::vector<std::string>>("functor_symbols"))
71 {
72 
73  for (const auto i : make_range(_nargs))
74  _coupled_variable_names.push_back(getFieldVar("coupled_variables", i)->name());
75 
76  // sanity checks
77  if (!_functor_symbols.empty() && (_functor_symbols.size() != _n_functors))
78  paramError("functor_symbols", "functor_symbols must be the same length as functor_names.");
79 
80  validateFunctorSymbols();
81  validateFunctorNames();
82 
83  // build variables argument
84  std::string variables;
85 
86  // coupled field variables
87  for (const auto i : index_range(_coupled_variable_names))
88  variables += (i == 0 ? "" : ",") + _coupled_variable_names[i];
89 
90  // adding functors to the expression
91  if (_functor_symbols.size())
92  for (const auto & symbol : _functor_symbols)
93  variables += (variables.empty() ? "" : ",") + symbol;
94  else
95  for (const auto & name : _functor_names)
96  variables += (variables.empty() ? "" : ",") + name;
97 
98  // material properties
99  for (const auto & matprop : _matprop_names)
100  variables += (variables.empty() ? "" : ",") + matprop;
101  for (const auto & matprop : _ad_matprop_names)
102  variables += (variables.empty() ? "" : ",") + matprop;
103  if (isNodal() && (_matprop_names.size() || _ad_matprop_names.size()))
104  mooseError("Material properties cannot be retrieved in a nodal auxkernel. Use a different "
105  "auxiliary variable family.");
106 
107  // positions and time
108  if (_use_xyzt)
109  for (auto & v : _xyzt)
110  variables += (variables.empty() ? "" : ",") + v;
111 
112  // Create parsed function
113  _func_F = std::make_shared<SymFunction>();
114  parsedFunctionSetup(_func_F,
115  _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  _func_params.resize(_nargs + _n_functors + _n_matprops + _n_ad_matprops + (_use_xyzt ? 4 : 0));
123 
124  // keep pointers to the material properties
125  for (const auto & name : _matprop_names)
126  _matprops.push_back(&getMaterialProperty<Real>(name));
127  for (const auto & name : _ad_matprop_names)
128  _ad_matprops.push_back(&getADMaterialProperty<Real>(name));
129 
130  // keep pointers to the functors
131  for (const auto & name : _functor_names)
132  _functors.push_back(&getFunctor<Real>(name));
133 }
134 
135 Real
137 {
138  // Variables
139  for (const auto j : make_range(_nargs))
140  _func_params[j] = (*_args[j])[_qp];
141 
142  // Functors
143  const auto & state = determineState();
144  if (isNodal())
145  {
146  const Moose::NodeArg node_arg = {_current_node,
148  for (const auto i : index_range(_functors))
149  _func_params[_nargs + i] = (*_functors[i])(node_arg, state);
150  }
151  else
152  {
153  const Moose::ElemQpArg qp_arg = {_current_elem, _qp, _qrule, _q_point[_qp]};
154  for (const auto i : index_range(_functors))
155  _func_params[_nargs + i] = (*_functors[i])(qp_arg, state);
156  }
157 
158  // Material properties
159  for (const auto j : make_range(_n_matprops))
160  _func_params[_nargs + _n_functors + j] = (*_matprops[j])[_qp];
161  for (const auto j : make_range(_n_ad_matprops))
162  _func_params[_nargs + _n_functors + _n_matprops + j] = (*_ad_matprops[j])[_qp].value();
163 
164  // Positions and time
165  if (_use_xyzt)
166  {
167  for (const auto j : make_range(Moose::dim))
169  isNodal() ? (*_current_node)(j) : _q_point[_qp](j);
171  }
172 
173  return evaluate(_func_F);
174 }
175 
176 void
178 {
179  validateGenericVectorNames(_functor_symbols, "functor_symbols");
180 }
181 
182 void
184 {
185  validateGenericVectorNames(_functor_names, "functor_names");
186 }
std::string name(const ElemQuality q)
void validateFunctorNames()
Function to validate the names in _functor_names.
Definition: ParsedAux.C:183
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
static const std::set< SubdomainID > undefined_subdomain_connection
A static member that can be used when the connection of a node to subdomains is unknown.
const unsigned int _nargs
coupled variables
Definition: ParsedAux.h:47
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
const Node *const & _current_node
Current node (valid only for nodal kernels)
Definition: AuxKernel.h:214
Moose::StateArg determineState() const
Create a functor state argument that corresponds to the implicit state of this object.
std::vector< const Moose::Functor< Real > * > _functors
Vector of pointers to functors.
Definition: ParsedAux.h:79
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:159
const bool _use_xyzt
import coordinates and time
Definition: ParsedAux.h:59
const std::vector< MooseFunctorName > & _functor_names
Functors to use in the parsed expression.
Definition: ParsedAux.h:70
std::vector< const ADMaterialProperty< Real > * > _ad_matprops
Definition: ParsedAux.h:56
AuxKernel that evaluates a parsed function expression.
Definition: ParsedAux.h:18
SymFunctionPtr _func_F
function parser object to compute the local value of the aux-variable
Definition: ParsedAux.h:65
const std::vector< std::string > _functor_symbols
Symbolic name to use for each functor.
Definition: ParsedAux.h:76
void deprecateParam(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
const unsigned int _n_matprops
Definition: ParsedAux.h:53
const unsigned int _n_functors
Number of functors.
Definition: ParsedAux.h:73
void deprecateCoupledVar(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
Argument for requesting functor evaluation at a quadrature point location in an element.
std::vector< const MaterialProperty< Real > * > _matprops
Definition: ParsedAux.h:55
registerMooseObject("MooseApp", ParsedAux)
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const QBase *const & _qrule
Quadrature rule being used.
Definition: AuxKernel.h:198
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
IntRange< T > make_range(T beg, T end)
const Elem *const & _current_elem
Current element (valid only for elemental kernels)
Definition: AuxKernel.h:204
unsigned int _qp
Quadrature point index.
Definition: AuxKernel.h:230
const std::vector< const VariableValue * > _args
Definition: ParsedAux.h:48
void addRequiredCustomTypeParam(const std::string &name, const std::string &custom_type, const std::string &doc_string)
These methods add an option parameter and with a customer type to the InputParameters object...
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
static InputParameters validParams()
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
static InputParameters validParams()
Definition: AuxKernel.C:27
static InputParameters validParams()
Definition: ParsedAux.C:15
void validateGenericVectorNames(const std::vector< T > &names_vec, const std::string &param_name)
Function to ensure vector entries (names) do not overlap with xyzt or coupled variable names...
Definition: ParsedAux.h:87
const unsigned int _n_ad_matprops
Definition: ParsedAux.h:54
ParsedAux(const InputParameters &parameters)
Definition: ParsedAux.C:56
virtual Real computeValue() override
Compute and return the value of the aux variable.
Definition: ParsedAux.C:136
const MooseArray< Point > & _q_point
Active quadrature points.
Definition: AuxKernel.h:196
auto index_range(const T &sizable)
void validateFunctorSymbols()
Function to validate the symbols in _functor_symbols.
Definition: ParsedAux.C:177
bool isNodal() const
Nodal or elemental kernel?
Definition: AuxKernel.h:86