www.mooseframework.org
FunctionParserUtils.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 "FunctionParserUtils.h"
11 
12 // MOOSE includes
13 #include "InputParameters.h"
14 
16 
19 {
21 
22  params.addParam<bool>(
23  "enable_jit",
24 #ifdef LIBMESH_HAVE_FPARSER_JIT
25  true,
26 #else
27  false,
28 #endif
29  "Enable just-in-time compilation of function expressions for faster evaluation");
30  params.addParamNamesToGroup("enable_jit", "Advanced");
31  params.addParam<bool>(
32  "enable_ad_cache", true, "Enable cacheing of function derivatives for faster startup time");
33  params.addParam<bool>(
34  "enable_auto_optimize", true, "Enable automatic immediate optimization of derivatives");
35  params.addParam<bool>(
36  "disable_fpoptimizer", false, "Disable the function parser algebraic optimizer");
37  params.addParam<bool>(
38  "fail_on_evalerror",
39  false,
40  "Fail fatally if a function evaluation returns an error code (otherwise just pass on NaN)");
41  params.addParamNamesToGroup("enable_ad_cache", "Advanced");
42  params.addParamNamesToGroup("enable_auto_optimize", "Advanced");
43  params.addParamNamesToGroup("disable_fpoptimizer", "Advanced");
44  params.addParamNamesToGroup("fail_on_evalerror", "Advanced");
45 
46  return params;
47 }
48 
49 const char * FunctionParserUtils::_eval_error_msg[] = {
50  "Unknown",
51  "Division by zero",
52  "Square root of a negative value",
53  "Logarithm of negative value",
54  "Trigonometric error (asin or acos of illegal value)",
55  "Maximum recursion level reached"};
56 
58  : _enable_jit(parameters.isParamValid("enable_jit") && parameters.get<bool>("enable_jit")),
59  _enable_ad_cache(parameters.get<bool>("enable_ad_cache")),
60  _disable_fpoptimizer(parameters.get<bool>("disable_fpoptimizer")),
61  _enable_auto_optimize(parameters.get<bool>("enable_auto_optimize") && !_disable_fpoptimizer),
62  _fail_on_evalerror(parameters.get<bool>("fail_on_evalerror")),
63  _nan(std::numeric_limits<Real>::quiet_NaN())
64 {
65 #ifndef LIBMESH_HAVE_FPARSER_JIT
66  if (_enable_jit)
67  {
68  mooseWarning("Tried to enable FParser JIT but libmesh does not have it compiled in.");
69  _enable_jit = false;
70  }
71 #endif
72 }
73 
74 void
76 {
77  parser->SetADFlags(ADFunction::ADCacheDerivatives, _enable_ad_cache);
78  parser->SetADFlags(ADFunction::ADAutoOptimize, _enable_auto_optimize);
79 }
80 
81 Real
83 {
84  // null pointer is a shortcut for vanishing derivatives, see functionsOptimize()
85  if (parser == NULL)
86  return 0.0;
87 
88  // evaluate expression
89  Real result = parser->Eval(_func_params.data());
90 
91  // fetch fparser evaluation error
92  int error_code = parser->EvalError();
93 
94  // no error
95  if (error_code == 0)
96  return result;
97 
98  // hard fail or return not a number
100  mooseError("DerivativeParsedMaterial function evaluation encountered an error: ",
101  _eval_error_msg[(error_code < 0 || error_code > 5) ? 0 : error_code]);
102 
103  return _nan;
104 }
105 
106 void
108  const std::vector<std::string> & constant_names,
109  const std::vector<std::string> & constant_expressions)
110 {
111  // check constant vectors
112  unsigned int nconst = constant_expressions.size();
113  if (nconst != constant_names.size())
114  mooseError("The parameter vectors constant_names and constant_values must have equal length.");
115 
116  // previously evaluated constant_expressions may be used in following constant_expressions
117  std::vector<Real> constant_values(nconst);
118 
119  for (unsigned int i = 0; i < nconst; ++i)
120  {
121  ADFunctionPtr expression = std::make_shared<ADFunction>();
122 
123  // set FParser internal feature flags
124  setParserFeatureFlags(expression);
125 
126  // add previously evaluated constants
127  for (unsigned int j = 0; j < i; ++j)
128  if (!expression->AddConstant(constant_names[j], constant_values[j]))
129  mooseError("Invalid constant name in ParsedMaterialHelper");
130 
131  // build the temporary comnstant expression function
132  if (expression->Parse(constant_expressions[i], "") >= 0)
133  mooseError("Invalid constant expression\n",
134  constant_expressions[i],
135  "\n in parsed function object.\n",
136  expression->ErrorMsg());
137 
138  constant_values[i] = expression->Eval(NULL);
139 
140  if (!parser->AddConstant(constant_names[i], constant_values[i]))
141  mooseError("Invalid constant name in parsed function object");
142  }
143 }
emptyInputParameters
InputParameters emptyInputParameters()
Definition: InputParameters.C:24
InputParameters::addParam
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.
Definition: InputParameters.h:1198
FunctionParserUtils.h
FunctionParserUtils::_func_params
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
Definition: FunctionParserUtils.h:69
mooseError
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition: MooseError.h:210
InputParameters
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Definition: InputParameters.h:53
mooseWarning
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:222
FunctionParserUtils::_fail_on_evalerror
bool _fail_on_evalerror
Definition: FunctionParserUtils.h:59
FunctionParserUtils::addFParserConstants
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
Definition: FunctionParserUtils.C:107
FunctionParserUtils::ADFunctionPtr
std::shared_ptr< ADFunction > ADFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
Definition: FunctionParserUtils.h:40
InputParameters.h
defineLegacyParams
defineLegacyParams(FunctionParserUtils)
FunctionParserUtils::FunctionParserUtils
FunctionParserUtils(const InputParameters &parameters)
Definition: FunctionParserUtils.C:57
FunctionParserUtils
Definition: FunctionParserUtils.h:29
FunctionParserUtils::validParams
static InputParameters validParams()
Definition: FunctionParserUtils.C:18
FunctionParserUtils::_nan
const Real _nan
appropriate not a number value to return
Definition: FunctionParserUtils.h:63
FunctionParserUtils::_eval_error_msg
static const char * _eval_error_msg[]
table of FParser eval error codes
Definition: FunctionParserUtils.h:66
InputParameters::addParamNamesToGroup
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...
Definition: InputParameters.C:590
std
Definition: TheWarehouse.h:80
FunctionParserUtils::_enable_ad_cache
bool _enable_ad_cache
Definition: FunctionParserUtils.h:56
FunctionParserUtils::_enable_jit
bool _enable_jit
Definition: FunctionParserUtils.h:55
FunctionParserUtils::evaluate
Real evaluate(ADFunctionPtr &)
Evaluate FParser object and check EvalError.
Definition: FunctionParserUtils.C:82
FunctionParserUtils::_enable_auto_optimize
bool _enable_auto_optimize
Definition: FunctionParserUtils.h:58
FunctionParserUtils::setParserFeatureFlags
void setParserFeatureFlags(ADFunctionPtr &)
apply input paramters to internal feature flags of the parser object
Definition: FunctionParserUtils.C:75