www.mooseframework.org
ParsedODEKernel.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 "ParsedODEKernel.h"
11 
12 // MOOSE includes
13 #include "MooseVariableScalar.h"
14 #include "SystemBase.h"
15 
16 #include "libmesh/fparser_ad.hh"
17 
19 
20 template <>
23 {
26  params.addClassDescription("Parsed ODE function kernel.");
27 
28  params.addRequiredParam<std::string>("function", "function expression");
29  params.addCoupledVar("args", "additional coupled variables");
30  params.addParam<std::vector<std::string>>(
31  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
32  params.addParam<std::vector<std::string>>(
33  "constant_expressions",
34  "Vector of values for the constants in constant_names (can be an FParser expression)");
35 
36  return params;
37 }
38 
40  : ODEKernel(parameters),
41  FunctionParserUtils(parameters),
42  _function(getParam<std::string>("function")),
43  _nargs(coupledScalarComponents("args")),
44  _args(_nargs),
45  _arg_names(_nargs),
46  _func_dFdarg(_nargs),
47  _number_of_nl_variables(_sys.nVariables()),
48  _arg_index(_number_of_nl_variables, -1)
49 {
50  // build variables argument (start with variable the kernel is operating on)
51  std::string variables = _var.name();
52 
53  // add additional coupled variables
54  for (unsigned int i = 0; i < _nargs; ++i)
55  {
56  _arg_names[i] = getScalarVar("args", i)->name();
57  variables += "," + _arg_names[i];
58  _args[i] = &coupledScalarValue("args", i);
59 
60  // populate number -> arg index lookup table skipping aux variables
61  unsigned int number = coupledScalar("args", i);
62  if (number < _number_of_nl_variables)
63  _arg_index[number] = i;
64  }
65 
66  // base function object
68 
69  // set FParser interneal 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("Invalid function\n",
80  _function,
81  "\nin ParsedODEKernel ",
82  name(),
83  ".\n",
84  _func_F->ErrorMsg());
85 
86  // on-diagonal derivative
88 
89  if (_func_dFdu->AutoDiff(_var.name()) != -1)
90  mooseError("Failed to take first derivative w.r.t. ", _var.name());
91 
92  // off-diagonal derivatives
93  for (unsigned int i = 0; i < _nargs; ++i)
94  {
96 
97  if (_func_dFdarg[i]->AutoDiff(_arg_names[i]) != -1)
98  mooseError("Failed to take first derivative w.r.t. ", _arg_names[i]);
99  }
100 
101  // optimize
103  {
104  _func_F->Optimize();
105  _func_dFdu->Optimize();
106  for (unsigned int i = 0; i < _nargs; ++i)
107  _func_dFdarg[i]->Optimize();
108  }
109 
110  // just-in-time compile
111  if (_enable_jit)
112  {
113  _func_F->JITCompile();
114  _func_dFdu->JITCompile();
115  for (unsigned int i = 0; i < _nargs; ++i)
116  _func_dFdarg[i]->JITCompile();
117  }
118 
119  // reserve storage for parameter passing buffer
120  _func_params.resize(_nargs + 1);
121 }
122 
123 void
125 {
126  _func_params[0] = _u[_i];
127 
128  for (unsigned int j = 0; j < _nargs; ++j)
129  _func_params[j + 1] = (*_args[j])[_i];
130 }
131 
132 Real
134 {
135  updateParams();
136  return evaluate(_func_F);
137 }
138 
139 Real
141 {
142  updateParams();
143  return evaluate(_func_dFdu);
144 }
145 
146 Real
148 {
149  int i = _arg_index[jvar];
150  if (i < 0)
151  return 0.0;
152 
153  updateParams();
154  return evaluate(_func_dFdarg[i]);
155 }
std::vector< std::string > _arg_names
ADFunctionPtr _func_F
function parser object for the residual and on-diagonal Jacobian
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
InputParameters validParams< ParsedODEKernel >()
std::vector< ADFunctionPtr > _func_dFdarg
function parser objects for the Jacobian
FunctionParserADBase< Real > ADFunction
Shorthand for an autodiff function parser object.
virtual Real computeQpOffDiagJacobian(unsigned int jvar) override
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.
std::string _function
function expression
std::vector< unsigned int > _arg_index
Vector to look up the internal coupled variable index into arg* through the libMesh variable number...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
registerMooseObject("MooseApp", ParsedODEKernel)
virtual unsigned int coupledScalar(const std::string &var_name, unsigned int comp=0)
Returns the index for a scalar coupled variable by name.
ParsedODEKernel(const InputParameters &parameters)
std::vector< VariableValue * > _args
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
virtual Real computeQpJacobian() override
MooseVariableScalar * getScalarVar(const std::string &var_name, unsigned int comp)
Extract pointer to a scalar coupled variable.
MooseVariableScalar & _var
Scalar variable.
Definition: ScalarKernel.h:75
InputParameters validParams< ODEKernel >()
Definition: ODEKernel.C:19
const unsigned int _number_of_nl_variables
number of non-linear variables in the problem
VariableValue & _u
Value(s) of the scalar variable.
Definition: ScalarKernel.h:81
virtual VariableValue & coupledScalarValue(const std::string &var_name, unsigned int comp=0)
Returns value of a scalar coupled variable.
virtual Real computeQpResidual() override
ADFunctionPtr _func_dFdu
InputParameters validParams< FunctionParserUtils >()
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.
unsigned int _nargs
coupled variables
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
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...
unsigned int _i
Definition: ScalarKernel.h:78