www.mooseframework.org
ParsedMaterialHelper.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "ParsedMaterialHelper.h"
11 
12 #include "libmesh/quadrature.h"
13 
14 template <>
17 {
20  params.addClassDescription("Parsed Function Material.");
21  return params;
22 }
23 
25  VariableNameMappingMode map_mode)
26  : FunctionMaterialBase(parameters),
27  FunctionParserUtils(parameters),
28  _variable_names(_nargs),
29  _mat_prop_descriptors(0),
30  _tol(0),
31  _map_mode(map_mode)
32 {
33 }
34 
35 void
36 ParsedMaterialHelper::functionParse(const std::string & function_expression)
37 {
38  std::vector<std::string> empty_string_vector;
39  functionParse(function_expression, empty_string_vector, empty_string_vector);
40 }
41 
42 void
43 ParsedMaterialHelper::functionParse(const std::string & function_expression,
44  const std::vector<std::string> & constant_names,
45  const std::vector<std::string> & constant_expressions)
46 {
47  std::vector<std::string> empty_string_vector;
48  std::vector<Real> empty_real_vector;
49  functionParse(function_expression,
50  constant_names,
51  constant_expressions,
52  empty_string_vector,
53  empty_string_vector,
54  empty_real_vector);
55 }
56 
57 void
58 ParsedMaterialHelper::functionParse(const std::string & function_expression,
59  const std::vector<std::string> & constant_names,
60  const std::vector<std::string> & constant_expressions,
61  const std::vector<std::string> & mat_prop_expressions,
62  const std::vector<std::string> & tol_names,
63  const std::vector<Real> & tol_values)
64 {
65  // build base function object
67 
68  // set FParser internal feature flags
70 
71  // initialize constants
72  addFParserConstants(_func_F, constant_names, constant_expressions);
73 
74  // add further constants coming from default value coupling
76  for (const auto & acd : _arg_constant_defaults)
77  if (!_func_F->AddConstant(acd, _pars.defaultCoupledValue(acd)))
78  mooseError("Invalid constant name in parsed function object");
79 
80  // set variable names based on map_mode
81  switch (_map_mode)
82  {
83  case USE_MOOSE_NAMES:
84  for (unsigned int i = 0; i < _nargs; ++i)
86  break;
87 
88  case USE_PARAM_NAMES:
89  // we do not allow vector coupling in this mode
90  if (!_mapping_is_unique)
91  mooseError("Derivative parsed materials must couple exactly one non-linear variable per "
92  "coupled variable input parameter.");
93 
94  for (unsigned i = 0; i < _nargs; ++i)
96  break;
97 
98  default:
99  mooseError("Unnknown variable mapping mode.");
100  }
101 
102  // tolerance vectors
103  if (tol_names.size() != tol_values.size())
104  mooseError("The parameter vectors tol_names and tol_values must have equal length.");
105 
106  // set tolerances
107  _tol.resize(_nargs);
108  for (unsigned int i = 0; i < _nargs; ++i)
109  {
110  _tol[i] = -1.0;
111 
112  // for every argument look through the entire tolerance vector to find a match
113  for (MooseIndex(tol_names) j = 0; j < tol_names.size(); ++j)
114  if (_variable_names[i] == tol_names[j])
115  {
116  _tol[i] = tol_values[j];
117  break;
118  }
119  }
120 
121  // build 'variables' argument for fparser
122  std::string variables;
123  for (unsigned i = 0; i < _nargs; ++i)
124  variables += "," + _variable_names[i];
125 
126  // get all material properties
127  unsigned int nmat_props = mat_prop_expressions.size();
128  _mat_prop_descriptors.resize(nmat_props);
129  for (unsigned int i = 0; i < nmat_props; ++i)
130  {
131  // parse the material property parameter entry into a FunctionMaterialPropertyDescriptor
132  _mat_prop_descriptors[i] = FunctionMaterialPropertyDescriptor(mat_prop_expressions[i], this);
133 
134  // get the fparser symbol name for the new material property
135  variables += "," + _mat_prop_descriptors[i].getSymbolName();
136  }
137 
138  // erase leading comma
139  variables.erase(0, 1);
140 
141  // build the base function
142  if (_func_F->Parse(function_expression, variables) >= 0)
143  mooseError("Invalid function\n",
144  function_expression,
145  '\n',
146  variables,
147  "\nin ParsedMaterialHelper.\n",
148  _func_F->ErrorMsg());
149 
150  // create parameter passing buffer
151  _func_params.resize(_nargs + nmat_props);
152 
153  // perform next steps (either optimize or take derivatives and then optimize)
155 }
156 
157 void
159 {
161 
162  // force a value update to get the property at least once and register it for the dependencies
163  for (auto & mpd : _mat_prop_descriptors)
164  mpd.value();
165 }
166 
167 void
169 {
170  // base function
172  _func_F->Optimize();
173  if (_enable_jit && !_func_F->JITCompile())
174  mooseInfo("Failed to JIT compile expression, falling back to byte code interpretation.");
175 }
176 
177 void
179 {
180  if (_prop_F)
181  (*_prop_F)[_qp] = 0.0;
182 }
183 
184 void
186 {
187  Real a;
188 
189  // fill the parameter vector, apply tolerances
190  for (unsigned int i = 0; i < _nargs; ++i)
191  {
192  if (_tol[i] < 0.0)
193  _func_params[i] = (*_args[i])[_qp];
194  else
195  {
196  a = (*_args[i])[_qp];
197  _func_params[i] = a < _tol[i] ? _tol[i] : (a > 1.0 - _tol[i] ? 1.0 - _tol[i] : a);
198  }
199  }
200 
201  // insert material property values
202  auto nmat_props = _mat_prop_descriptors.size();
203  for (MooseIndex(_mat_prop_descriptors) i = 0; i < nmat_props; ++i)
204  _func_params[i + _nargs] = _mat_prop_descriptors[i].value()[_qp];
205 
206  // set function value
207  if (_prop_F)
208  (*_prop_F)[_qp] = evaluate(_func_F);
209 }
virtual void computeQpProperties()
Users must override this method.
Real evaluate(ADFunctionPtr &)
Evaluate FParser object and check EvalError.
void addFParserConstants(ADFunctionPtr &parser, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions)
add constants (which can be complex expressions) to the parser object
FunctionParserADBase< Real > ADFunction
Shorthand for an autodiff function parser object.
std::vector< std::string > _variable_names
variable names used in the expression (depends on the map_mode)
std::vector< std::string > _arg_constant_defaults
coupled variables with default values
Material properties get fully described using this structure, including their dependent variables and...
Material base class central for all Materials that provide a Function as a material property value...
const VariableNameMappingMode _map_mode
Flag to indicate if MOOSE nonlinear variable names should be used as FParser variable names...
std::vector< Real > _tol
Tolerance values for all arguments (to protect from log(0)).
InputParameters validParams< ParsedMaterialHelper >()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< std::string > _arg_param_names
String vector of the input file coupling parameter name for each argument.
std::shared_ptr< ADFunction > ADFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
MaterialProperty< Real > * _prop_F
Material property to store the function value.
unsigned int _qp
Definition: Material.h:222
virtual void functionsPostParse()
Real defaultCoupledValue(const std::string &coupling_name, unsigned int i=0) const
Get the default value for an optionally coupled variable.
unsigned int _nargs
Number of coupled arguments.
InputParameters validParams< FunctionMaterialBase >()
virtual void initQpStatefulProperties()
Initialize stateful properties at quadrature points.
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
Definition: MooseObject.h:174
MatPropDescriptorList _mat_prop_descriptors
Material property descriptors (obtained by parsing _mat_prop_expressions)
InputParameters validParams< FunctionParserUtils >()
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
std::vector< const VariableValue * > _args
Coupled variables for function arguments.
void setParserFeatureFlags(ADFunctionPtr &)
apply input paramters to internal feature flags of the parser object
virtual void functionsOptimize()
std::vector< std::string > _arg_names
String vector of all argument names.
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...
ParsedMaterialHelper(const InputParameters &parameters, VariableNameMappingMode map_mode)
void mooseInfo(Args &&... args) const
Definition: MooseObject.h:167
bool _mapping_is_unique
Flag that indicates if exactly one linear variable is coupled per input file coupling parameter...
ADFunctionPtr _func_F
The undiffed free energy function parser object.
void functionParse(const std::string &function_expression)