www.mooseframework.org
ParsedAux.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 "ParsedAux.h"
11 #include "MooseApp.h"
12 
13 registerMooseObject("MooseApp", ParsedAux);
14 
17 {
20  params.addClassDescription(
21  "Sets a field variable value to the evaluation of a parsed expression.");
22 
23  params.addRequiredCustomTypeParam<std::string>(
24  "function", "FunctionExpression", "Parsed function expression to compute");
25  params.deprecateParam("function", "expression", "02/07/2024");
26  params.addCoupledVar("args", "Vector of coupled variable names");
27  params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
28 
29  params.addParam<bool>(
30  "use_xyzt",
31  false,
32  "Make coordinate (x,y,z) and time (t) variables available in the function expression.");
33  params.addParam<std::vector<std::string>>(
34  "constant_names",
35  {},
36  "Vector of constants used in the parsed function (use this for kB etc.)");
37  params.addParam<std::vector<std::string>>(
38  "constant_expressions",
39  {},
40  "Vector of values for the constants in constant_names (can be an FParser expression)");
41 
42  return params;
43 }
44 
46  : AuxKernel(parameters),
47  FunctionParserUtils(parameters),
48  _function(getParam<std::string>("expression")),
49  _nargs(coupledComponents("coupled_variables")),
50  _args(coupledValues("coupled_variables")),
51  _use_xyzt(getParam<bool>("use_xyzt"))
52 {
53  // build variables argument
54  std::string variables;
55 
56  // coupled field variables
57  for (std::size_t i = 0; i < _nargs; ++i)
58  variables += (i == 0 ? "" : ",") + getFieldVar("coupled_variables", i)->name();
59 
60  // "system" variables
61  const std::vector<std::string> xyzt = {"x", "y", "z", "t"};
62  if (_use_xyzt)
63  for (auto & v : xyzt)
64  variables += (variables.empty() ? "" : ",") + v;
65 
66  // base function object
67  _func_F = std::make_shared<SymFunction>();
68 
69  // set FParser internal feature flags
71 
72  // add the constant expressions
74  getParam<std::vector<std::string>>("constant_names"),
75  getParam<std::vector<std::string>>("constant_expressions"));
76 
77  // parse function
78  if (_func_F->Parse(_function, variables) >= 0)
79  mooseError(
80  "Invalid function\n", _function, "\nin ParsedAux ", name(), ".\n", _func_F->ErrorMsg());
81 
82  // optimize
84  _func_F->Optimize();
85 
86  // just-in-time compile
87  if (_enable_jit)
88  {
89  // let rank 0 do the JIT compilation first
90  if (_communicator.rank() != 0)
92 
93  _func_F->JITCompile();
94 
95  // wait for ranks > 0 to catch up
96  if (_communicator.rank() == 0)
98  }
99 
100  // reserve storage for parameter passing buffer
101  _func_params.resize(_nargs + (_use_xyzt ? 4 : 0));
102 }
103 
104 Real
106 {
107  for (std::size_t j = 0; j < _nargs; ++j)
108  _func_params[j] = (*_args[j])[_qp];
109 
110  if (_use_xyzt)
111  {
112  for (std::size_t j = 0; j < LIBMESH_DIM; ++j)
113  _func_params[_nargs + j] = isNodal() ? (*_current_node)(j) : _q_point[_qp](j);
114  _func_params[_nargs + 3] = _t;
115  }
116 
117  return evaluate(_func_F);
118 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
const unsigned int _nargs
coupled variables
Definition: ParsedAux.h:32
const std::string & name() const override
Get the variable name.
processor_id_type rank() const
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & _communicator
const bool _use_xyzt
import coordinates and time
Definition: ParsedAux.h:36
void addFParserConstants(SymFunctionPtr &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
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
AuxKernel that evaluates a parsed function expression.
Definition: ParsedAux.h:18
SymFunctionPtr _func_F
function parser object for the resudual and on-diagonal Jacobian
Definition: ParsedAux.h:39
std::string _function
function expression
Definition: ParsedAux.h:29
void deprecateParam(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
void deprecateCoupledVar(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
registerMooseObject("MooseApp", ParsedAux)
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
unsigned int _qp
Quadrature point index.
Definition: AuxKernel.h:230
const std::vector< const VariableValue * > _args
Definition: ParsedAux.h:33
void addRequiredCustomTypeParam(const std::string &name, const std::string &custom_type, const std::string &doc_string)
These methods add an option parameter and with a customer type to the InputParameters object...
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
static InputParameters validParams()
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...
static InputParameters validParams()
Definition: AuxKernel.C:27
static InputParameters validParams()
Definition: ParsedAux.C:16
ParsedAux(const InputParameters &parameters)
Definition: ParsedAux.C:45
virtual Real computeValue() override
Compute and return the value of the aux variable.
Definition: ParsedAux.C:105
const MooseArray< Point > & _q_point
Active quadrature points.
Definition: AuxKernel.h:196
bool isNodal() const
Nodal or elemental kernel?
Definition: AuxKernel.h:86
void setParserFeatureFlags(SymFunctionPtr &)
apply input paramters to internal feature flags of the parser object
const MooseVariableFieldBase * getFieldVar(const std::string &var_name, unsigned int comp) const
Definition: Coupleable.C:277