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

#include <FunctionParserUtils.h>

Inheritance diagram for FunctionParserUtils:
[legend]

Public Types

typedef FunctionParserADBase< Real > ADFunction
 Shorthand for an autodiff function parser object. More...
 
typedef std::shared_ptr< ADFunctionADFunctionPtr
 Shorthand for an smart pointer to an autodiff function parser object. More...
 

Public Member Functions

 FunctionParserUtils (const InputParameters &parameters)
 
void setParserFeatureFlags (ADFunctionPtr &)
 apply input paramters to internal feature flags of the parser object More...
 

Protected Member Functions

Real evaluate (ADFunctionPtr &)
 Evaluate FParser object and check EvalError. More...
 
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 More...
 

Protected Attributes

const Real _nan
 appropriate not a number value to return More...
 
std::vector< Real > _func_params
 Array to stage the parameters passed to the functions when calling Eval. More...
 
bool _enable_jit
 
bool _enable_ad_cache
 
bool _disable_fpoptimizer
 
bool _enable_auto_optimize
 
bool _fail_on_evalerror
 

Static Protected Attributes

static const char * _eval_error_msg []
 table of FParser eval error codes More...
 

Detailed Description

Definition at line 30 of file FunctionParserUtils.h.

Member Typedef Documentation

◆ ADFunction

typedef FunctionParserADBase<Real> FunctionParserUtils::ADFunction

Shorthand for an autodiff function parser object.

Definition at line 36 of file FunctionParserUtils.h.

◆ ADFunctionPtr

Shorthand for an smart pointer to an autodiff function parser object.

Definition at line 39 of file FunctionParserUtils.h.

Constructor & Destructor Documentation

◆ FunctionParserUtils()

FunctionParserUtils::FunctionParserUtils ( const InputParameters parameters)

Definition at line 56 of file FunctionParserUtils.C.

57  : _enable_jit(parameters.isParamValid("enable_jit") && parameters.get<bool>("enable_jit")),
58  _enable_ad_cache(parameters.get<bool>("enable_ad_cache")),
59  _disable_fpoptimizer(parameters.get<bool>("disable_fpoptimizer")),
60  _enable_auto_optimize(parameters.get<bool>("enable_auto_optimize") && !_disable_fpoptimizer),
61  _fail_on_evalerror(parameters.get<bool>("fail_on_evalerror")),
62  _nan(std::numeric_limits<Real>::quiet_NaN())
63 {
64 #ifndef LIBMESH_HAVE_FPARSER_JIT
65  if (_enable_jit)
66  {
67  mooseWarning("Tried to enable FParser JIT but libmesh does not have it compiled in.");
68  _enable_jit = false;
69  }
70 #endif
71 }
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:220
const Real _nan
appropriate not a number value to return
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

Member Function Documentation

◆ addFParserConstants()

void FunctionParserUtils::addFParserConstants ( ADFunctionPtr parser,
const std::vector< std::string > &  constant_names,
const std::vector< std::string > &  constant_expressions 
)
protected

add constants (which can be complex expressions) to the parser object

Definition at line 106 of file FunctionParserUtils.C.

Referenced by ParsedMaterialHelper::functionParse(), ParsedAddSideset::ParsedAddSideset(), ParsedAux::ParsedAux(), ParsedGenerateSideset::ParsedGenerateSideset(), ParsedODEKernel::ParsedODEKernel(), ParsedSubdomainMeshGenerator::ParsedSubdomainMeshGenerator(), and ParsedSubdomainMeshModifier::ParsedSubdomainMeshModifier().

109 {
110  // check constant vectors
111  unsigned int nconst = constant_expressions.size();
112  if (nconst != constant_expressions.size())
113  mooseError("The parameter vectors constant_names and constant_values must have equal length.");
114 
115  // previously evaluated constant_expressions may be used in following constant_expressions
116  std::vector<Real> constant_values(nconst);
117 
118  for (unsigned int i = 0; i < nconst; ++i)
119  {
120  ADFunctionPtr expression = ADFunctionPtr(new ADFunction());
121 
122  // set FParser internal feature flags
123  setParserFeatureFlags(expression);
124 
125  // add previously evaluated constants
126  for (unsigned int j = 0; j < i; ++j)
127  if (!expression->AddConstant(constant_names[j], constant_values[j]))
128  mooseError("Invalid constant name in ParsedMaterialHelper");
129 
130  // build the temporary comnstant expression function
131  if (expression->Parse(constant_expressions[i], "") >= 0)
132  mooseError("Invalid constant expression\n",
133  constant_expressions[i],
134  "\n in parsed function object.\n",
135  expression->ErrorMsg());
136 
137  constant_values[i] = expression->Eval(NULL);
138 
139  if (!parser->AddConstant(constant_names[i], constant_values[i]))
140  mooseError("Invalid constant name in parsed function object");
141  }
142 }
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:208
std::shared_ptr< ADFunction > ADFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
void setParserFeatureFlags(ADFunctionPtr &)
apply input paramters to internal feature flags of the parser object

◆ evaluate()

Real FunctionParserUtils::evaluate ( ADFunctionPtr parser)
protected

Evaluate FParser object and check EvalError.

Definition at line 81 of file FunctionParserUtils.C.

Referenced by DerivativeParsedMaterialHelper::computeProperties(), ParsedMaterialHelper::computeProperties(), ParsedODEKernel::computeQpJacobian(), ParsedODEKernel::computeQpOffDiagJacobian(), ParsedODEKernel::computeQpResidual(), ParsedAux::computeValue(), ParsedSubdomainMeshGenerator::generate(), ParsedGenerateSideset::generate(), ParsedAddSideset::modify(), and ParsedSubdomainMeshModifier::modify().

82 {
83  // null pointer is a shortcut for vanishing derivatives, see functionsOptimize()
84  if (parser == NULL)
85  return 0.0;
86 
87  // evaluate expression
88  Real result = parser->Eval(_func_params.data());
89 
90  // fetch fparser evaluation error
91  int error_code = parser->EvalError();
92 
93  // no error
94  if (error_code == 0)
95  return result;
96 
97  // hard fail or return not a number
99  mooseError("DerivativeParsedMaterial function evaluation encountered an error: ",
100  _eval_error_msg[(error_code < 0 || error_code > 5) ? 0 : error_code]);
101 
102  return _nan;
103 }
static const char * _eval_error_msg[]
table of FParser eval error codes
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
const Real _nan
appropriate not a number value to return
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.

◆ setParserFeatureFlags()

void FunctionParserUtils::setParserFeatureFlags ( ADFunctionPtr parser)

Member Data Documentation

◆ _disable_fpoptimizer

bool FunctionParserUtils::_disable_fpoptimizer
protected

◆ _enable_ad_cache

bool FunctionParserUtils::_enable_ad_cache
protected

Definition at line 55 of file FunctionParserUtils.h.

Referenced by setParserFeatureFlags().

◆ _enable_auto_optimize

bool FunctionParserUtils::_enable_auto_optimize
protected

Definition at line 57 of file FunctionParserUtils.h.

Referenced by setParserFeatureFlags().

◆ _enable_jit

bool FunctionParserUtils::_enable_jit
protected

◆ _eval_error_msg

const char * FunctionParserUtils::_eval_error_msg
staticprotected
Initial value:
= {
"Unknown",
"Division by zero",
"Square root of a negative value",
"Logarithm of negative value",
"Trigonometric error (asin or acos of illegal value)",
"Maximum recursion level reached"}

table of FParser eval error codes

Definition at line 65 of file FunctionParserUtils.h.

Referenced by evaluate().

◆ _fail_on_evalerror

bool FunctionParserUtils::_fail_on_evalerror
protected

Definition at line 58 of file FunctionParserUtils.h.

Referenced by evaluate().

◆ _func_params

std::vector<Real> FunctionParserUtils::_func_params
protected

◆ _nan

const Real FunctionParserUtils::_nan
protected

appropriate not a number value to return

Definition at line 62 of file FunctionParserUtils.h.

Referenced by evaluate().


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