www.mooseframework.org
ParsedODEKernel.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "ParsedODEKernel.h"
16 
17 // MOOSE includes
18 #include "MooseVariableScalar.h"
19 #include "SystemBase.h"
20 
21 #include "libmesh/fparser_ad.hh"
22 
23 template <>
26 {
29  params.addClassDescription("Parsed ODE function kernel.");
30 
31  params.addRequiredParam<std::string>("function", "function expression");
32  params.addCoupledVar("args", "additional coupled variables");
33  params.addParam<std::vector<std::string>>(
34  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
35  params.addParam<std::vector<std::string>>(
36  "constant_expressions",
37  "Vector of values for the constants in constant_names (can be an FParser expression)");
38 
39  return params;
40 }
41 
43  : ODEKernel(parameters),
44  FunctionParserUtils(parameters),
45  _function(getParam<std::string>("function")),
46  _nargs(coupledScalarComponents("args")),
47  _args(_nargs),
48  _arg_names(_nargs),
49  _func_dFdarg(_nargs),
50  _number_of_nl_variables(_sys.nVariables()),
51  _arg_index(_number_of_nl_variables, -1)
52 {
53  // build variables argument (start with variable the kernel is operating on)
54  std::string variables = _var.name();
55 
56  // add additional coupled variables
57  for (unsigned int i = 0; i < _nargs; ++i)
58  {
59  _arg_names[i] = getScalarVar("args", i)->name();
60  variables += "," + _arg_names[i];
61  _args[i] = &coupledScalarValue("args", i);
62 
63  // populate number -> arg index lookup table skipping aux variables
64  unsigned int number = coupledScalar("args", i);
65  if (number < _number_of_nl_variables)
66  _arg_index[number] = i;
67  }
68 
69  // base function object
71 
72  // set FParser interneal feature flags
74 
75  // add the constant expressions
77  getParam<std::vector<std::string>>("constant_names"),
78  getParam<std::vector<std::string>>("constant_expressions"));
79 
80  // parse function
81  if (_func_F->Parse(_function, variables) >= 0)
82  mooseError("Invalid function\n",
83  _function,
84  "\nin ParsedODEKernel ",
85  name(),
86  ".\n",
87  _func_F->ErrorMsg());
88 
89  // on-diagonal derivative
91 
92  if (_func_dFdu->AutoDiff(_var.name()) != -1)
93  mooseError("Failed to take first derivative w.r.t. ", _var.name());
94 
95  // off-diagonal derivatives
96  for (unsigned int i = 0; i < _nargs; ++i)
97  {
99 
100  if (_func_dFdarg[i]->AutoDiff(_arg_names[i]) != -1)
101  mooseError("Failed to take first derivative w.r.t. ", _arg_names[i]);
102  }
103 
104  // optimize
106  {
107  _func_F->Optimize();
108  _func_dFdu->Optimize();
109  for (unsigned int i = 0; i < _nargs; ++i)
110  _func_dFdarg[i]->Optimize();
111  }
112 
113  // just-in-time compile
114  if (_enable_jit)
115  {
116  _func_F->JITCompile();
117  _func_dFdu->JITCompile();
118  for (unsigned int i = 0; i < _nargs; ++i)
119  _func_dFdarg[i]->JITCompile();
120  }
121 
122  // reserve storage for parameter passing buffer
123  _func_params.resize(_nargs + 1);
124 }
125 
126 void
128 {
129  _func_params[0] = _u[_i];
130 
131  for (unsigned int j = 0; j < _nargs; ++j)
132  _func_params[j + 1] = (*_args[j])[_i];
133 }
134 
135 Real
137 {
138  updateParams();
139  return evaluate(_func_F);
140 }
141 
142 Real
144 {
145  updateParams();
146  return evaluate(_func_dFdu);
147 }
148 
149 Real
151 {
152  int i = _arg_index[jvar];
153  if (i < 0)
154  return 0.0;
155 
156  updateParams();
157  return evaluate(_func_dFdarg[i]);
158 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
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...
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 std::string & name() const
Get the variable name.
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:81
InputParameters validParams< ODEKernel >()
Definition: ODEKernel.C:24
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:87
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
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 >()
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
unsigned int _nargs
coupled variables
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
unsigned int _i
Definition: ScalarKernel.h:84