www.mooseframework.org
FunctionParserUtils.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "FunctionParserUtils.h"
16 
17 // MOOSE includes
18 #include "InputParameters.h"
19 
20 template <>
23 {
25 
26 #ifdef LIBMESH_HAVE_FPARSER_JIT
27  params.addParam<bool>(
28  "enable_jit",
29  true,
30  "Enable just-in-time compilation of function expressions for faster evaluation");
31  params.addParamNamesToGroup("enable_jit", "Advanced");
32 #endif
33  params.addParam<bool>(
34  "enable_ad_cache", true, "Enable cacheing of function derivatives for faster startup time");
35  params.addParam<bool>(
36  "enable_auto_optimize", true, "Enable automatic immediate optimization of derivatives");
37  params.addParam<bool>(
38  "disable_fpoptimizer", false, "Disable the function parser algebraic optimizer");
39  params.addParam<bool>(
40  "fail_on_evalerror",
41  false,
42  "Fail fatally if a function evaluation returns an error code (otherwise just pass on NaN)");
43  params.addParamNamesToGroup("enable_ad_cache", "Advanced");
44  params.addParamNamesToGroup("enable_auto_optimize", "Advanced");
45  params.addParamNamesToGroup("disable_fpoptimizer", "Advanced");
46  params.addParamNamesToGroup("fail_on_evalerror", "Advanced");
47 
48  return params;
49 }
50 
51 const char * FunctionParserUtils::_eval_error_msg[] = {
52  "Unknown",
53  "Division by zero",
54  "Square root of a negative value",
55  "Logarithm of negative value",
56  "Trigonometric error (asin or acos of illegal value)",
57  "Maximum recursion level reached"};
58 
60  : _enable_jit(parameters.isParamValid("enable_jit") && parameters.get<bool>("enable_jit")),
61  _enable_ad_cache(parameters.get<bool>("enable_ad_cache")),
62  _disable_fpoptimizer(parameters.get<bool>("disable_fpoptimizer")),
63  _enable_auto_optimize(parameters.get<bool>("enable_auto_optimize") && !_disable_fpoptimizer),
64  _fail_on_evalerror(parameters.get<bool>("fail_on_evalerror")),
65  _nan(std::numeric_limits<Real>::quiet_NaN())
66 {
67 }
68 
69 void
71 {
72  parser->SetADFlags(ADFunction::ADCacheDerivatives, _enable_ad_cache);
73  parser->SetADFlags(ADFunction::ADAutoOptimize, _enable_auto_optimize);
74 }
75 
76 Real
78 {
79  // null pointer is a shortcut for vanishing derivatives, see functionsOptimize()
80  if (parser == NULL)
81  return 0.0;
82 
83  // evaluate expression
84  Real result = parser->Eval(_func_params.data());
85 
86  // fetch fparser evaluation error
87  int error_code = parser->EvalError();
88 
89  // no error
90  if (error_code == 0)
91  return result;
92 
93  // hard fail or return not a number
95  mooseError("DerivativeParsedMaterial function evaluation encountered an error: ",
96  _eval_error_msg[(error_code < 0 || error_code > 5) ? 0 : error_code]);
97 
98  return _nan;
99 }
100 
101 void
103  const std::vector<std::string> & constant_names,
104  const std::vector<std::string> & constant_expressions)
105 {
106  // check constant vectors
107  unsigned int nconst = constant_expressions.size();
108  if (nconst != constant_expressions.size())
109  mooseError("The parameter vectors constant_names and constant_values must have equal length.");
110 
111  // previously evaluated constant_expressions may be used in following constant_expressions
112  std::vector<Real> constant_values(nconst);
113 
114  for (unsigned int i = 0; i < nconst; ++i)
115  {
116  ADFunctionPtr expression = ADFunctionPtr(new ADFunction());
117 
118  // set FParser internal feature flags
119  setParserFeatureFlags(expression);
120 
121  // add previously evaluated constants
122  for (unsigned int j = 0; j < i; ++j)
123  if (!expression->AddConstant(constant_names[j], constant_values[j]))
124  mooseError("Invalid constant name in ParsedMaterialHelper");
125 
126  // build the temporary comnstant expression function
127  if (expression->Parse(constant_expressions[i], "") >= 0)
128  mooseError("Invalid constant expression\n",
129  constant_expressions[i],
130  "\n in parsed function object.\n",
131  expression->ErrorMsg());
132 
133  constant_values[i] = expression->Eval(NULL);
134 
135  if (!parser->AddConstant(constant_names[i], constant_values[i]))
136  mooseError("Invalid constant name in parsed function object");
137  }
138 }
static const char * _eval_error_msg[]
table of FParser eval error codes
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.
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::shared_ptr< ADFunction > ADFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
FunctionParserUtils(const InputParameters &parameters)
InputParameters emptyInputParameters()
const Real _nan
appropriate not a number value to return
InputParameters validParams< FunctionParserUtils >()
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
void setParserFeatureFlags(ADFunctionPtr &)
apply input paramters to internal feature flags of the parser object
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...