12 #include "libmesh/quadrature.h" 21 params.
addParam<
bool>(
"error_on_missing_material_properties",
23 "Throw an error if any explicitly requested material property does not " 24 "exist. Otherwise assume it to be zero.");
29 "Special symbols, like point coordinates, time, and timestep size.");
38 _symbol_names(_nargs),
43 _error_on_missing_material_properties(
44 this->template getParam<bool>(
"error_on_missing_material_properties"))
52 const std::vector<std::string> empty_string_vector;
53 functionParse(function_expression, empty_string_vector, empty_string_vector);
59 const std::vector<std::string> & constant_names,
60 const std::vector<std::string> & constant_expressions)
62 const std::vector<std::string> empty_string_vector;
63 const std::vector<Real> empty_real_vector;
64 functionParse(function_expression,
75 const std::vector<std::string> & constant_names,
76 const std::vector<std::string> & constant_expressions,
77 const std::vector<std::string> & mat_prop_expressions,
78 const std::vector<std::string> & tol_names,
79 const std::vector<Real> & tol_values)
81 const std::vector<PostprocessorName> empty_pp_name_vector;
82 functionParse(function_expression,
94 const std::string & function_expression,
95 const std::vector<std::string> & constant_names,
96 const std::vector<std::string> & constant_expressions,
97 const std::vector<std::string> & mat_prop_expressions,
98 const std::vector<PostprocessorName> & postprocessor_names,
99 const std::vector<std::string> & tol_names,
100 const std::vector<Real> & tol_values)
103 _func_F = std::make_shared<SymFunction>();
106 setParserFeatureFlags(_func_F);
109 addFParserConstants(_func_F, constant_names, constant_expressions);
112 if (_map_mode == VariableNameMappingMode::USE_PARAM_NAMES)
113 for (
const auto & acd : _arg_constant_defaults)
114 if (!_func_F->AddConstant(acd, this->_pars.defaultCoupledValue(acd)))
115 mooseError(
"Invalid constant name in parsed function object");
120 case VariableNameMappingMode::USE_MOOSE_NAMES:
121 for (
unsigned int i = 0; i < _nargs; ++i)
122 _symbol_names[i] = _arg_names[i];
125 case VariableNameMappingMode::USE_PARAM_NAMES:
126 for (
unsigned i = 0; i < _nargs; ++i)
128 if (_arg_param_numbers[i] < 0)
129 _symbol_names[i] = _arg_param_names[i];
131 _symbol_names[i] = _arg_param_names[i] + std::to_string(_arg_param_numbers[i]);
140 if (tol_names.size() != tol_values.size())
141 mooseError(
"The parameter vectors tol_names and tol_values must have equal length.");
151 if (_symbol_names[i] == tol_names[j])
153 _tol[i] = tol_values[j];
159 unsigned int nmat_props = mat_prop_expressions.size();
163 _mat_prop_descriptors.emplace_back(
164 mat_prop_expressions[i],
this, _error_on_missing_material_properties);
167 _symbol_names.push_back(_mat_prop_descriptors.back().getSymbolName());
171 for (
const auto & pp : postprocessor_names)
173 _postprocessor_values.push_back(&this->getPostprocessorValueByName(pp));
174 _symbol_names.push_back(pp);
178 for (
const auto symbol : _extra_symbols)
181 case ExtraSymbols::x:
182 _symbol_names.push_back(
"x");
184 case ExtraSymbols::y:
185 _symbol_names.push_back(
"y");
187 case ExtraSymbols::z:
188 _symbol_names.push_back(
"z");
190 case ExtraSymbols::t:
191 _symbol_names.push_back(
"t");
193 case ExtraSymbols::dt:
194 _symbol_names.push_back(
"dt");
202 if (_func_F->Parse(function_expression, variables) >= 0)
207 "\nin ParsedMaterialHelper.\n",
208 _func_F->ErrorMsg());
211 _func_params.resize(_nargs + nmat_props + _postprocessor_values.size() + _extra_symbols.size());
216 if (_communicator.rank() != 0)
217 _communicator.barrier();
219 functionsPostParse();
222 if (_communicator.rank() == 0)
223 _communicator.barrier();
226 template <
bool is_ad>
230 functionsOptimize(_func_F);
233 for (
auto & mpd : _mat_prop_descriptors)
237 template <
bool is_ad>
241 computeQpProperties();
244 template <
bool is_ad>
249 for (
unsigned int i = 0; i < _nargs; ++i)
252 _func_params[i] = (*_args[i])[_qp];
255 auto a = (*_args[i])[_qp];
256 _func_params[i] = a < _tol[i] ? _tol[i] : (a > 1.0 - _tol[i] ? 1.0 - _tol[i] : a);
261 for (
const auto i :
index_range(_mat_prop_descriptors))
262 _func_params[i + _nargs] = _mat_prop_descriptors[i].
value(_qp);
265 auto npps = _postprocessor_values.size();
266 for (MooseIndex(_postprocessor_values) i = 0; i < npps; ++i)
267 _func_params[i + _nargs + _mat_prop_descriptors.size()] = *_postprocessor_values[i];
272 const auto j = _nargs + _mat_prop_descriptors.size() + _postprocessor_values.size() + i;
273 switch (_extra_symbols[i])
275 case ExtraSymbols::x:
276 _func_params[j] = _q_point[_qp](0);
278 case ExtraSymbols::y:
279 _func_params[j] = _q_point[_qp](1);
281 case ExtraSymbols::z:
282 _func_params[j] = _q_point[_qp](2);
284 case ExtraSymbols::t:
285 _func_params[j] = _t;
287 case ExtraSymbols::dt:
288 _func_params[j] = _dt;
294 (*_prop_F)[_qp] =
evaluate(_func_F, _name);
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
void computeQpProperties() override
Users must override this method.
Material base class, central to all Materials that provide a Function as a material property value...
static InputParameters validParams()
static InputParameters validParams()
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
T evaluate(Real, const Point &)
The general evaluation method is not defined.
void initQpStatefulProperties() override
Initialize stateful properties at quadrature points.
std::string stringify(const T &t)
conversion to string
Helper class to perform the parsing and optimization of the function expression.
IntRange< T > make_range(T beg, T end)
static InputParameters validParams()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
auto index_range(const T &sizable)
ParsedMaterialHelper(const InputParameters ¶meters, VariableNameMappingMode map_mode)
virtual void functionsPostParse()
void functionParse(const std::string &function_expression)