https://mooseframework.inl.gov
MooseParsedFunctionWrapper.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 
11 #include "FEProblem.h"
12 #include "MooseVariableScalar.h"
13 #include "Function.h"
14 #include "MooseUtils.h"
15 
16 using namespace libMesh;
17 
19  const std::string & function_str,
20  const std::vector<std::string> & vars,
21  const std::vector<std::string> & vals,
22  const THREAD_ID tid)
23  : _feproblem(feproblem), _function_str(function_str), _vars(vars), _vals_input(vals), _tid(tid)
24 {
25  initialize();
26 
28  std::make_unique<ParsedFunction<Real, RealGradient>>(_function_str, &_vars, &_initial_vals);
29 
30  for (auto & v : _vars)
31  _addr.push_back(&_function_ptr->getVarAddress(v));
32 }
33 
35 
36 template <>
37 Real
39 {
40  update();
42  return (*_function_ptr)(p, t);
43 }
44 
45 template <>
48 {
49  update();
51  DenseVector<Real> output(LIBMESH_DIM);
52  (*_function_ptr)(p, t, output);
53  return output;
54 }
55 
56 template <>
59 {
60  DenseVector<Real> output = evaluate<DenseVector<Real>>(t, p);
61 
62  return RealVectorValue(output(0)
63 #if LIBMESH_DIM > 1
64  ,
65  output(1)
66 #endif
67 #if LIBMESH_DIM > 2
68  ,
69  output(2)
70 #endif
71  );
72 }
73 
76 {
77  update();
79  return _function_ptr->gradient(p, t);
80 }
81 
82 Real
84 {
85  update();
87  return _function_ptr->dot(p, t);
88 }
89 
90 void
92 {
93  // Loop through all the input values supplied by the users.
94  for (unsigned int i = 0; i < _vals_input.size(); ++i)
95  {
96  // Case when a Postprocessor is found by the name given in the input values
97  ReporterName r_name(_vals_input[i], "value");
99  {
101  _initial_vals.push_back(pp_val);
102  _pp_vals.push_back(&pp_val);
103  _pp_index.push_back(i);
104  }
105 
106  // Case when a scalar variable is found by the name given in the input values
108  {
109  auto & scalar_val = _feproblem.getScalarVariable(_tid, _vals_input[i]).sln()[0];
110  _initial_vals.push_back(scalar_val);
111  _scalar_vals.push_back(&scalar_val);
112  _scalar_index.push_back(i);
113  }
114 
115  // Case when a function is found by the name given in the input values
116  else if (_feproblem.hasFunction(_vals_input[i]))
117  {
119  _initial_vals.push_back(0);
120  _functions.push_back(&fn);
121  _function_index.push_back(i);
122  }
123 
124  // Case when a Real is supplied
125  else
126  {
127  Real val;
128  try
129  {
130  val = MooseUtils::convert<Real>(_vals_input[i], true);
131  }
132  catch (const std::invalid_argument & e)
133  {
134  mooseError("'No postprocessor, scalar variable, or function with the name '",
135  _vals_input[i],
136  "' found. ",
137  e.what());
138  }
139  _initial_vals.push_back(val);
140  }
141  }
142 }
143 
144 void
146 {
147  for (unsigned int i = 0; i < _pp_index.size(); ++i)
148  (*_addr[_pp_index[i]]) = (*_pp_vals[i]);
149 
150  for (unsigned int i = 0; i < _scalar_index.size(); ++i)
151  (*_addr[_scalar_index[i]]) = (*_scalar_vals[i]);
152 }
153 
154 void
156 {
157  for (unsigned int i = 0; i < _function_index.size(); ++i)
158  (*_addr[_function_index[i]]) = _functions[i]->value(t, pt);
159 }
Base class for function objects.
Definition: Function.h:36
const THREAD_ID _tid
The thread id passed from owning Function object.
std::vector< const Real * > _scalar_vals
Vector of pointers to scalar variables values.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
char ** vars
Real evaluateDot(Real t, const Point &p)
Evaluate the time derivative of the function which libMesh provides through automatic differentiation...
const std::vector< std::string > & _vals_input
List of the values for the variables supplied by the user.
virtual bool hasScalarVariable(const std::string &var_name) const override
Returns a Boolean indicating whether any system contains a variable with the name provided...
void updateFunctionValues(Real t, const Point &pt)
Updates function values for use in the libMesh::ParsedFunction.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
RealGradient evaluateGradient(Real t, const Point &p)
Evaluate the gradient of the function which libMesh provides through automatic differentiation.
const std::vector< std::string > & _vars
List of variables supplied from the user.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual Function & getFunction(const std::string &name, const THREAD_ID tid=0)
const ReporterData & getReporterData() const
Provides const access the ReporterData object.
std::vector< const Function * > _functions
Vector of Functions this parsed function is using.
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:202
virtual MooseVariableScalar & getScalarVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the scalar variable reference from whichever system contains it.
MooseParsedFunctionWrapper(FEProblemBase &feproblem, const std::string &function_str, const std::vector< std::string > &vars, const std::vector< std::string > &vals, const THREAD_ID tid=0)
Class constructor.
std::vector< Real > _initial_vals
Storage for the initial values of _vars variables used by the libMesh::ParsedFunction object...
const std::string & _function_str
Reference to the string containing the function to evaluate.
void update()
Updates postprocessor and scalar values for use in the libMesh::ParsedFunction.
virtual ~MooseParsedFunctionWrapper()
Class destruction Deletes the pointer to the dynamically allocated instance of the underlying libMesh...
const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name, std::size_t t_index=0) const
Get a read-only reference to the value associated with a Postprocessor that exists.
std::unique_ptr< libMesh::ParsedFunction< Real > > _function_ptr
Pointer to the libMesh::ParsedFunction object.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< Real * > _addr
Pointers to the variables that store the values of _vars inside the libMesh::ParsedFunction object...
std::vector< unsigned int > _pp_index
Stores indices into _addr variable that are connected to Postprocessors.
FEProblemBase & _feproblem
Reference to the FEProblemBase object.
std::vector< unsigned int > _function_index
Stores indices into _addr that are connected to Functions this libMesh::ParsedFunction is using...
std::vector< unsigned int > _scalar_index
Stores indicies into _addr variable that are connected to Scalar Variables.
bool hasReporterValue(const ReporterName &reporter_name) const
Return True if a Reporter value with the given type and name have been created.
Definition: ReporterData.h:445
T evaluate(Real t, const Point &p)
A template method for performing the evaluation of the libMesh::ParsedFunction Within the source two ...
void initialize()
Initialization method that prepares the _vars and _initial_vals for use by the libMesh::ParsedFunctio...
std::vector< const Real * > _pp_vals
Vector of pointers to postprocessor values this parsed function is using.
virtual bool hasFunction(const std::string &name, const THREAD_ID tid=0)
const VariableValue & sln() const
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
unsigned int THREAD_ID
Definition: MooseTypes.h:209