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 
12 registerMooseObject("MooseApp", ParsedAux);
13 
14 template <>
17 {
20  params.addClassDescription("Parsed function AuxKernel.");
21 
22  params.addRequiredCustomTypeParam<std::string>(
23  "function", "FunctionExpression", "function expression");
24  params.addCoupledVar("args", "coupled variables");
25  params.addParam<std::vector<std::string>>(
26  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
27  params.addParam<std::vector<std::string>>(
28  "constant_expressions",
29  "Vector of values for the constants in constant_names (can be an FParser expression)");
30 
31  return params;
32 }
33 
35  : AuxKernel(parameters),
36  FunctionParserUtils(parameters),
37  _function(getParam<std::string>("function")),
38  _nargs(coupledComponents("args")),
39  _args(_nargs)
40 {
41  // build variables argument
42  std::string variables;
43  for (unsigned int i = 0; i < _nargs; ++i)
44  {
45  variables += (i == 0 ? "" : ",") + getVar("args", i)->name();
46  _args[i] = &coupledValue("args", i);
47  }
48 
49  // base function object
50  _func_F = ADFunctionPtr(std::make_shared<ADFunction>());
51 
52  // set FParser interneal feature flags
54 
55  // add the constant expressions
57  getParam<std::vector<std::string>>("constant_names"),
58  getParam<std::vector<std::string>>("constant_expressions"));
59 
60  // parse function
61  if (_func_F->Parse(_function, variables) >= 0)
62  mooseError(
63  "Invalid function\n", _function, "\nin ParsedAux ", name(), ".\n", _func_F->ErrorMsg());
64 
65  // optimize
67  _func_F->Optimize();
68 
69  // just-in-time compile
70  if (_enable_jit)
71  _func_F->JITCompile();
72 
73  // reserve storage for parameter passing bufefr
74  _func_params.resize(_nargs);
75 }
76 
77 Real
79 {
80  for (unsigned int j = 0; j < _nargs; ++j)
81  _func_params[j] = (*_args[j])[_qp];
82 
83  return evaluate(_func_F);
84 }
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
unsigned int _nargs
coupled variables
Definition: ParsedAux.h:36
ADFunctionPtr _func_F
function parser object for the resudual and on-diagonal Jacobian
Definition: ParsedAux.h:40
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.
MooseVariable * getVar(const std::string &var_name, unsigned int comp)
Extract pointer to a coupled variable.
Definition: Coupleable.C:241
std::vector< const VariableValue * > _args
Definition: ParsedAux.h:37
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
AuxKernel that evaluates a parsed function expression.
Definition: ParsedAux.h:24
std::string _function
function expression
Definition: ParsedAux.h:33
InputParameters validParams< AuxKernel >()
Definition: AuxKernel.C:25
virtual const VariableValue & coupledValue(const std::string &var_name, unsigned int comp=0)
Returns value of a coupled variable.
Definition: Coupleable.C:361
registerMooseObject("MooseApp", ParsedAux)
InputParameters validParams< FunctionParserUtils >()
InputParameters validParams< ParsedAux >()
Definition: ParsedAux.C:16
const std::string & name() const
Get the variable name.
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
void setParserFeatureFlags(ADFunctionPtr &)
apply input paramters to internal feature flags of the parser object
unsigned int _qp
Quadrature point index.
Definition: AuxKernel.h:219
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...
Base class for creating new auxiliary kernels and auxiliary boundary conditions.
Definition: AuxKernel.h:33
ParsedAux(const InputParameters &parameters)
Definition: ParsedAux.C:34
virtual Real computeValue() override
Compute and return the value of the aux variable.
Definition: ParsedAux.C:78