24 #ifdef LIBMESH_HAVE_FPARSER_JIT 29 "Enable just-in-time compilation of function expressions for faster evaluation");
31 "enable_ad_cache",
true,
"Enable caching of function derivatives for faster startup time");
33 "enable_auto_optimize",
true,
"Enable automatic immediate optimization of derivatives");
35 "disable_fpoptimizer",
false,
"Disable the function parser algebraic optimizer");
36 MooseEnum evalerror(
"nan nan_warning error exception",
"nan");
39 "What to do if evaluation error occurs. Options are to pass a nan, " 40 "pass a nan with a warning, throw a error, or throw an exception");
43 "enable_jit enable_ad_cache enable_auto_optimize disable_fpoptimizer evalerror_behavior",
44 "Parsed expression advanced");
45 params.
addParam<
Real>(
"epsilon", 0,
"Fuzzy comparison tolerance");
53 "Square root of a negative value",
54 "Logarithm of negative value",
55 "Trigonometric error (asin or acos of illegal value)",
56 "Maximum recursion level reached"};
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),
65 _quiet_nan(
std::numeric_limits<
Real>::quiet_NaN()),
66 _epsilon(parameters.
get<
Real>(
"epsilon"))
68 #ifndef LIBMESH_HAVE_FPARSER_JIT 71 mooseWarning(
"Tried to enable FParser JIT but libmesh does not have it compiled in.");
81 parser->SetADFlags(SymFunction::ADCacheDerivatives, _enable_ad_cache);
82 parser->SetADFlags(SymFunction::ADAutoOptimize, _enable_auto_optimize);
96 const std::string & name)
103 auto tmp_eps = parser->epsilon();
104 parser->setEpsilon(_epsilon);
107 auto result = parser->Eval(func_params.data());
110 parser->setEpsilon(tmp_eps);
113 int error_code = _enable_jit ? (std::isnan(result) ? -1 : 0) : parser->EvalError();
120 switch (_evalerror_behavior)
122 case FailureMethod::nan:
125 case FailureMethod::nan_warning:
128 ": Parsed function evaluation encountered an error: ",
129 _eval_error_msg[(error_code < 0 || error_code > 5) ? 0 : error_code]);
132 case FailureMethod::error:
135 ": Parsed function evaluation encountered an error: ",
136 _eval_error_msg[(error_code < 0 || error_code > 5) ? 0 : error_code]);
138 case FailureMethod::exception:
139 mooseException(
"In ",
141 ": Parsed function evaluation encountered an error: ",
142 _eval_error_msg[(error_code < 0 || error_code > 5) ? 0 : error_code],
143 "\n Cutting timestep");
149 template <
bool is_ad>
153 const std::vector<std::string> & constant_names,
154 const std::vector<std::string> & constant_expressions)
const 157 unsigned int nconst = constant_expressions.size();
158 if (nconst != constant_names.size())
159 mooseError(
"The parameter vectors constant_names (size " +
160 std::to_string(constant_names.size()) +
") and constant_expressions (size " +
161 std::to_string(nconst) +
") must have equal length.");
164 std::vector<Real> constant_values(nconst);
166 for (
unsigned int i = 0; i < nconst; ++i)
169 auto expression = std::make_shared<FunctionParserADBase<Real>>();
172 for (
unsigned int j = 0; j < i; ++j)
173 if (!expression->AddConstant(constant_names[j], constant_values[j]))
174 mooseError(
"Invalid constant name: ", constant_names[j],
" and value ", constant_values[j]);
177 if (expression->Parse(constant_expressions[i],
"") >= 0)
179 constant_expressions[i],
180 "\n in parsed function object.\n",
181 expression->ErrorMsg());
183 constant_values[i] = expression->Eval(NULL);
185 if (!parser->AddConstant(constant_names[i], constant_values[i]))
186 mooseError(
"Invalid constant name in parsed function object");
195 auto tmp_eps = parsed_function->epsilon();
196 parsed_function->setEpsilon(
_epsilon);
200 parsed_function->Optimize();
202 mooseInfo(
"Failed to JIT compile expression, falling back to byte code interpretation.");
204 parsed_function->setEpsilon(tmp_eps);
212 auto tmp_eps = parsed_function->epsilon();
213 parsed_function->setEpsilon(_epsilon);
216 if (!_disable_fpoptimizer)
217 parsed_function->Optimize();
218 if (!_enable_jit || !parsed_function->JITCompile())
219 mooseError(
"AD parsed objects require JIT compilation to be enabled and working.");
221 parsed_function->setEpsilon(tmp_eps);
224 template <
bool is_ad>
228 const std::string & expression,
229 const std::string & variables,
230 const std::vector<std::string> & constant_names,
231 const std::vector<std::string> & constant_expressions,
235 setParserFeatureFlags(
function);
238 addFParserConstants(
function, constant_names, constant_expressions);
241 if (function->Parse(expression, variables) >= 0)
242 mooseError(
"Invalid function\n", expression,
"\nError:\n", function->ErrorMsg());
245 if (!_disable_fpoptimizer)
246 function->Optimize();
252 if (comm.
rank() != 0)
255 function->JITCompile();
258 if (comm.
rank() == 0)
std::string name(const ElemQuality q)
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
void addFParserConstants(SymFunctionPtr &parser, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions) const
add constants (which can be complex expressions) to the parser object
Moose::GenericType< Real, is_ad > GenericReal
std::shared_ptr< SymFunction > SymFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
FunctionParserUtils(const InputParameters ¶meters)
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
virtual void functionsOptimize(SymFunctionPtr &parsed_function)
run FPOptimizer on the parsed function
processor_id_type rank() const
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
bool _enable_jit
feature flags
FailureMethod
Enum for failure method.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
void parsedFunctionSetup(SymFunctionPtr &function, const std::string &expression, const std::string &variables, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions, const libMesh::Parallel::Communicator &comm) const
Performs setup steps on a SymFunction.
T evaluate(Real, const Point &)
The general evaluation method is not defined.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
bool _disable_fpoptimizer
static InputParameters validParams()
const Real _epsilon
fuzzy comparison tolerance
void setParserFeatureFlags(SymFunctionPtr &) const
apply input parameters to internal feature flags of the parser object