www.mooseframework.org
ParsedMaterialHelper.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 #include "ParsedMaterialHelper.h"
9 
10 #include "libmesh/quadrature.h"
11 
12 template <>
13 InputParameters
15 {
16  InputParameters params = validParams<FunctionMaterialBase>();
17  params += validParams<FunctionParserUtils>();
18  params.addClassDescription("Parsed Function Material.");
19  return params;
20 }
21 
22 ParsedMaterialHelper::ParsedMaterialHelper(const InputParameters & parameters,
23  VariableNameMappingMode map_mode)
24  : FunctionMaterialBase(parameters),
25  FunctionParserUtils(parameters),
26  _variable_names(_nargs),
27  _mat_prop_descriptors(0),
28  _tol(0),
29  _map_mode(map_mode)
30 {
31 }
32 
33 void
34 ParsedMaterialHelper::functionParse(const std::string & function_expression)
35 {
36  std::vector<std::string> empty_string_vector;
37  functionParse(function_expression, empty_string_vector, empty_string_vector);
38 }
39 
40 void
41 ParsedMaterialHelper::functionParse(const std::string & function_expression,
42  const std::vector<std::string> & constant_names,
43  const std::vector<std::string> & constant_expressions)
44 {
45  std::vector<std::string> empty_string_vector;
46  std::vector<Real> empty_real_vector;
47  functionParse(function_expression,
48  constant_names,
49  constant_expressions,
50  empty_string_vector,
51  empty_string_vector,
52  empty_real_vector);
53 }
54 
55 void
56 ParsedMaterialHelper::functionParse(const std::string & function_expression,
57  const std::vector<std::string> & constant_names,
58  const std::vector<std::string> & constant_expressions,
59  const std::vector<std::string> & mat_prop_expressions,
60  const std::vector<std::string> & tol_names,
61  const std::vector<Real> & tol_values)
62 {
63  // build base function object
64  _func_F = ADFunctionPtr(new ADFunction());
65 
66  // set FParser internal feature flags
67  setParserFeatureFlags(_func_F);
68 
69  // initialize constants
70  addFParserConstants(_func_F, constant_names, constant_expressions);
71 
72  // add further constants coming from default value coupling
74  for (std::vector<std::string>::iterator it = _arg_constant_defaults.begin();
75  it != _arg_constant_defaults.end();
76  ++it)
77  if (!_func_F->AddConstant(*it, _pars.defaultCoupledValue(*it)))
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 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 throug the entire tolerance vector to find a match
113  for (unsigned int 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  unsigned int nmat_props = _mat_prop_descriptors.size();
164  for (unsigned int i = 0; i < nmat_props; ++i)
165  _mat_prop_descriptors[i].value();
166 }
167 
168 void
170 {
171  // base function
172  if (!_disable_fpoptimizer)
173  _func_F->Optimize();
174  if (_enable_jit && !_func_F->JITCompile())
175  mooseInfo("Failed to JIT compile expression, falling back to byte code interpretation.");
176 }
177 
178 void
180 {
181  Real a;
182 
183  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
184  {
185  // fill the parameter vector, apply tolerances
186  for (unsigned int i = 0; i < _nargs; ++i)
187  {
188  if (_tol[i] < 0.0)
189  _func_params[i] = (*_args[i])[_qp];
190  else
191  {
192  a = (*_args[i])[_qp];
193  _func_params[i] = a < _tol[i] ? _tol[i] : (a > 1.0 - _tol[i] ? 1.0 - _tol[i] : a);
194  }
195  }
196 
197  // insert material property values
198  unsigned int nmat_props = _mat_prop_descriptors.size();
199  for (unsigned int i = 0; i < nmat_props; ++i)
200  _func_params[i + _nargs] = _mat_prop_descriptors[i].value()[_qp];
201 
202  // TODO: computeQpProperties()
203 
204  // set function value
205  if (_prop_F)
206  (*_prop_F)[_qp] = evaluate(_func_F);
207  }
208 }
virtual void computeProperties()
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 >()
std::vector< std::string > _arg_param_names
String vector of the input file coupling parameter name for each argument.
MaterialProperty< Real > * _prop_F
Material property to store the function value.
virtual void functionsPostParse()
unsigned int _nargs
Number of coupled arguments.
InputParameters validParams< FunctionMaterialBase >()
MatPropDescriptorList _mat_prop_descriptors
Material property descriptors (obtained by parsing _mat_prop_expressions)
std::vector< const VariableValue * > _args
Coupled variables for function arguments.
virtual void functionsOptimize()
std::vector< std::string > _arg_names
String vector of all argument names.
ParsedMaterialHelper(const InputParameters &parameters, VariableNameMappingMode map_mode)
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)