www.mooseframework.org
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
ParsedMaterialHelper Class Reference

Helper class to perform the parsing and optimization of the function expression. More...

#include <ParsedMaterialHelper.h>

Inheritance diagram for ParsedMaterialHelper:
[legend]

Public Types

enum  VariableNameMappingMode { USE_MOOSE_NAMES, USE_PARAM_NAMES }
 

Public Member Functions

 ParsedMaterialHelper (const InputParameters &parameters, VariableNameMappingMode map_mode)
 
void functionParse (const std::string &function_expression)
 
void functionParse (const std::string &function_expression, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions)
 
void functionParse (const std::string &function_expression, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions, const std::vector< std::string > &mat_prop_names, const std::vector< std::string > &tol_names, const std::vector< Real > &tol_values)
 

Protected Types

typedef std::vector< FunctionMaterialPropertyDescriptorMatPropDescriptorList
 convenience typedef for the material property descriptors More...
 

Protected Member Functions

virtual void computeProperties ()
 
virtual void functionsPostParse ()
 
virtual void functionsOptimize ()
 
unsigned int argIndex (unsigned int i_var) const
 FunctionMaterialBase keeps an internal list of all the variables the derivatives are taken w.r.t. More...
 

Protected Attributes

ADFunctionPtr _func_F
 The undiffed free energy function parser object. More...
 
std::vector< std::string > _variable_names
 variable names used in the expression (depends on the map_mode) More...
 
MatPropDescriptorList _mat_prop_descriptors
 Material property descriptors (obtained by parsing _mat_prop_expressions) More...
 
std::vector< Real > _tol
 Tolerance values for all arguments (to protect from log(0)). More...
 
const VariableNameMappingMode _map_mode
 Flag to indicate if MOOSE nonlinear variable names should be used as FParser variable names. More...
 
std::vector< const VariableValue * > _args
 Coupled variables for function arguments. More...
 
std::string _F_name
 Name of the function value material property and used as a base name to concatenate the material property names for the derivatives. More...
 
bool _mapping_is_unique
 Flag that indicates if exactly one linear variable is coupled per input file coupling parameter. More...
 
unsigned int _nargs
 Number of coupled arguments. More...
 
std::vector< std::string > _arg_names
 String vector of all argument names. More...
 
std::vector< unsigned int > _arg_numbers
 Vector of all argument MOOSE variable numbers. More...
 
std::vector< std::string > _arg_param_names
 String vector of the input file coupling parameter name for each argument. More...
 
std::vector< std::string > _arg_constant_defaults
 coupled variables with default values More...
 
bool _third_derivatives
 Calculate (and allocate memory for) the third derivatives of the free energy. More...
 
MaterialProperty< Real > * _prop_F
 Material property to store the function value. More...
 

Detailed Description

Helper class to perform the parsing and optimization of the function expression.

Definition at line 26 of file ParsedMaterialHelper.h.

Member Typedef Documentation

convenience typedef for the material property descriptors

Definition at line 64 of file ParsedMaterialHelper.h.

Member Enumeration Documentation

Enumerator
USE_MOOSE_NAMES 
USE_PARAM_NAMES 

Definition at line 29 of file ParsedMaterialHelper.h.

Constructor & Destructor Documentation

ParsedMaterialHelper::ParsedMaterialHelper ( const InputParameters &  parameters,
VariableNameMappingMode  map_mode 
)

Definition at line 22 of file ParsedMaterialHelper.C.

24  : FunctionMaterialBase(parameters),
25  FunctionParserUtils(parameters),
28  _tol(0),
29  _map_mode(map_mode)
30 {
31 }
std::vector< std::string > _variable_names
variable names used in the expression (depends on the map_mode)
FunctionMaterialBase(const InputParameters &parameters)
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)).
unsigned int _nargs
Number of coupled arguments.
MatPropDescriptorList _mat_prop_descriptors
Material property descriptors (obtained by parsing _mat_prop_expressions)

Member Function Documentation

unsigned int FunctionMaterialBase::argIndex ( unsigned int  i_var) const
inlineprotectedinherited

FunctionMaterialBase keeps an internal list of all the variables the derivatives are taken w.r.t.

We provide the MOOSE variable bames in _arg_names, the libMesh variable numbers in _arg_numbers, and the input file parameter names in _arg_param_names. All are indexed by the argument index. This method returns the argument index for a given the libMesh variable number.

This mapping is necessary for internal classes which maintain lists of derivatives indexed by argument index and need to pull from those lists from the computeDF, computeD2F, and computeD3F methods, which receive libMesh variable numbers as parameters.

Definition at line 43 of file FunctionMaterialBase.h.

Referenced by DerivativeMultiPhaseMaterial::computeD2F(), ElasticEnergyMaterial::computeD2F(), DerivativeTwoPhaseMaterial::computeD2F(), DerivativeMultiPhaseMaterial::computeD3F(), DerivativeTwoPhaseMaterial::computeD3F(), DerivativeMultiPhaseMaterial::computeDF(), ElasticEnergyMaterial::computeDF(), DerivativeTwoPhaseMaterial::computeDF(), DerivativeMultiPhaseBase::DerivativeMultiPhaseBase(), and DiscreteNucleation::DiscreteNucleation().

44  {
45  const unsigned int idx = libMeshVarNumberRemap(i_var);
46  mooseAssert(idx < _arg_index.size() && _arg_numbers[_arg_index[idx]] == i_var,
47  "Requesting argIndex() for a derivative w.r.t. a variable not coupled to.");
48  return _arg_index[idx];
49  }
std::vector< unsigned int > _arg_index
Vector to look up the internal coupled variable index into arg* through the libMesh variable number...
unsigned int libMeshVarNumberRemap(unsigned int var) const
map the variable numbers to an even/odd interspersed pattern
std::vector< unsigned int > _arg_numbers
Vector of all argument MOOSE variable numbers.
void ParsedMaterialHelper::computeProperties ( )
protectedvirtual

Reimplemented in DerivativeParsedMaterialHelper.

Definition at line 179 of file ParsedMaterialHelper.C.

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 }
std::vector< Real > _tol
Tolerance values for all arguments (to protect from log(0)).
MaterialProperty< Real > * _prop_F
Material property to store the function value.
unsigned int _nargs
Number of coupled arguments.
MatPropDescriptorList _mat_prop_descriptors
Material property descriptors (obtained by parsing _mat_prop_expressions)
std::vector< const VariableValue * > _args
Coupled variables for function arguments.
ADFunctionPtr _func_F
The undiffed free energy function parser object.
void ParsedMaterialHelper::functionParse ( const std::string &  function_expression)
void ParsedMaterialHelper::functionParse ( const std::string &  function_expression,
const std::vector< std::string > &  constant_names,
const std::vector< std::string > &  constant_expressions 
)

Definition at line 41 of file ParsedMaterialHelper.C.

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 }
void functionParse(const std::string &function_expression)
void ParsedMaterialHelper::functionParse ( const std::string &  function_expression,
const std::vector< std::string > &  constant_names,
const std::vector< std::string > &  constant_expressions,
const std::vector< std::string > &  mat_prop_names,
const std::vector< std::string > &  tol_names,
const std::vector< Real > &  tol_values 
)

Definition at line 56 of file ParsedMaterialHelper.C.

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 }
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...
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)).
std::vector< std::string > _arg_param_names
String vector of the input file coupling parameter name for each argument.
virtual void functionsPostParse()
unsigned int _nargs
Number of coupled arguments.
MatPropDescriptorList _mat_prop_descriptors
Material property descriptors (obtained by parsing _mat_prop_expressions)
std::vector< std::string > _arg_names
String vector of all argument names.
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 ParsedMaterialHelper::functionsOptimize ( )
protectedvirtual

Definition at line 169 of file ParsedMaterialHelper.C.

Referenced by DerivativeParsedMaterialHelper::functionsPostParse(), and functionsPostParse().

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 }
ADFunctionPtr _func_F
The undiffed free energy function parser object.
void ParsedMaterialHelper::functionsPostParse ( )
protectedvirtual

Reimplemented in DerivativeParsedMaterialHelper.

Definition at line 158 of file ParsedMaterialHelper.C.

Referenced by functionParse().

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 }
MatPropDescriptorList _mat_prop_descriptors
Material property descriptors (obtained by parsing _mat_prop_expressions)
virtual void functionsOptimize()

Member Data Documentation

std::vector<std::string> FunctionMaterialBase::_arg_constant_defaults
protectedinherited

coupled variables with default values

Definition at line 76 of file FunctionMaterialBase.h.

Referenced by FunctionMaterialBase::FunctionMaterialBase(), and functionParse().

std::vector<std::string> FunctionMaterialBase::_arg_names
protectedinherited
std::vector<unsigned int> FunctionMaterialBase::_arg_numbers
protectedinherited

Vector of all argument MOOSE variable numbers.

Definition at line 70 of file FunctionMaterialBase.h.

Referenced by FunctionMaterialBase::argIndex(), DerivativeFunctionMaterialBase::computeProperties(), and FunctionMaterialBase::FunctionMaterialBase().

std::vector<std::string> FunctionMaterialBase::_arg_param_names
protectedinherited

String vector of the input file coupling parameter name for each argument.

Definition at line 73 of file FunctionMaterialBase.h.

Referenced by FunctionMaterialBase::FunctionMaterialBase(), and functionParse().

std::vector<const VariableValue *> FunctionMaterialBase::_args
protectedinherited
std::string FunctionMaterialBase::_F_name
protectedinherited

Name of the function value material property and used as a base name to concatenate the material property names for the derivatives.

Definition at line 58 of file FunctionMaterialBase.h.

Referenced by DerivativeParsedMaterialHelper::assembleDerivatives(), DerivativeFunctionMaterialBase::DerivativeFunctionMaterialBase(), and DerivativeFunctionMaterialBase::initialSetup().

ADFunctionPtr ParsedMaterialHelper::_func_F
protected
const VariableNameMappingMode ParsedMaterialHelper::_map_mode
protected

Flag to indicate if MOOSE nonlinear variable names should be used as FParser variable names.

This should be true only for DerivativeParsedMaterial. If set to false, this class looks up the input parameter name for each coupled variable and uses it as the FParser parameter name when parsing the FParser expression.

Definition at line 78 of file ParsedMaterialHelper.h.

Referenced by functionParse().

bool FunctionMaterialBase::_mapping_is_unique
protectedinherited

Flag that indicates if exactly one linear variable is coupled per input file coupling parameter.

Definition at line 61 of file FunctionMaterialBase.h.

Referenced by FunctionMaterialBase::FunctionMaterialBase(), and functionParse().

MatPropDescriptorList ParsedMaterialHelper::_mat_prop_descriptors
protected
unsigned int FunctionMaterialBase::_nargs
protectedinherited
MaterialProperty<Real>* FunctionMaterialBase::_prop_F
protectedinherited
bool FunctionMaterialBase::_third_derivatives
protectedinherited

Calculate (and allocate memory for) the third derivatives of the free energy.

Definition at line 79 of file FunctionMaterialBase.h.

std::vector<Real> ParsedMaterialHelper::_tol
protected

Tolerance values for all arguments (to protect from log(0)).

Definition at line 70 of file ParsedMaterialHelper.h.

Referenced by DerivativeParsedMaterialHelper::computeProperties(), computeProperties(), and functionParse().

std::vector<std::string> ParsedMaterialHelper::_variable_names
protected

variable names used in the expression (depends on the map_mode)

Definition at line 61 of file ParsedMaterialHelper.h.

Referenced by DerivativeParsedMaterialHelper::assembleDerivatives(), and functionParse().


The documentation for this class was generated from the following files: