www.mooseframework.org
MooseParsedFunctionWrapper.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 
16 
17 #include "FEProblem.h"
18 #include "MooseVariableScalar.h"
19 
21  const std::string & function_str,
22  const std::vector<std::string> & vars,
23  const std::vector<std::string> & vals,
24  const THREAD_ID tid)
25  : _feproblem(feproblem), _function_str(function_str), _vars(vars), _vals_input(vals), _tid(tid)
26 {
27  // Initialize (prepares Postprocessor values)
28  initialize();
29 
30  // Create the libMesh::ParsedFunction
32  libmesh_make_unique<ParsedFunction<Real, RealGradient>>(_function_str, &_vars, &_vals);
33 
34  // Loop through the Postprocessor and Scalar variables and point the libMesh::ParsedFunction to
35  // the PostprocessorValue
36  for (const auto & index : _pp_index)
37  _addr.push_back(&_function_ptr->getVarAddress(_vars[index]));
38 
39  for (const auto & index : _scalar_index)
40  _addr.push_back(&_function_ptr->getVarAddress(_vars[index]));
41 }
42 
44 
45 template <>
46 Real
47 MooseParsedFunctionWrapper::evaluate(Real t, const Point & p)
48 {
49  // Update the postprocessor / libMesh::ParsedFunction references for the desired function
50  update();
51 
52  // Evalute the function that returns a scalar
53  return (*_function_ptr)(p, t);
54 }
55 
56 template <>
57 DenseVector<Real>
58 MooseParsedFunctionWrapper::evaluate(Real t, const Point & p)
59 {
60  update();
61  DenseVector<Real> output(LIBMESH_DIM);
62  (*_function_ptr)(p, t, output);
63  return output;
64 }
65 
66 template <>
68 MooseParsedFunctionWrapper::evaluate(Real t, const Point & p)
69 {
70  DenseVector<Real> output = evaluate<DenseVector<Real>>(t, p);
71 
72  return RealVectorValue(output(0)
73 #if LIBMESH_DIM > 1
74  ,
75  output(1)
76 #endif
77 #if LIBMESH_DIM > 2
78  ,
79  output(2)
80 #endif
81  );
82 }
83 
86 {
87  // Update the postprocessor / libMesh::ParsedFunction references for the desired function
88  update();
89 
90  // Evalute the gradient of the function
91  return _function_ptr->gradient(p, t);
92 }
93 
94 Real
96 {
97  // Update the postprocessor / libMesh::ParsedFunction references for the desired function
98  update();
99 
100  // Evalute the time derivative
101  return _function_ptr->dot(p, t);
102 }
103 
104 void
106 {
107  // Loop through all the input values supplied by the users.
108  for (unsigned int i = 0; i < _vals_input.size(); ++i)
109  {
110  Real tmp; // desired type
111  std::istringstream ss(
112  _vals_input[i]); // istringstream object for conversion from std::string to Real
113 
114  // Case when a Postprocessor is found by the name given in the input values
116  {
117  // The PP value
118  Real & pp_val = _feproblem.getPostprocessorValue(_vals_input[i]);
119 
120  // Store a pointer to the Postprocessor value
121  _pp_vals.push_back(&pp_val);
122 
123  // Store the value for passing to the the libMesh::ParsedFunction
124  _vals.push_back(pp_val);
125 
126  // Store the location of this variable
127  _pp_index.push_back(i);
128  }
129 
130  // Case when a scalar variable is bound by the name given in the input values
132  {
133  // The scalar variable
134  Real & scalar_val = _feproblem.getScalarVariable(_tid, _vals_input[i]).sln()[0];
135 
136  // Store a pointer to the scalar value
137  _scalar_vals.push_back(&scalar_val);
138 
139  // Store the value for passing to the the libMesh::ParsedFunction
140  _vals.push_back(scalar_val);
141 
142  // Store the location of this variable
143  _scalar_index.push_back(i);
144  }
145 
146  // Case when a Real is supplied, convert std::string to Real
147  else
148  {
149  // Use istringstream to convert, if it fails produce an error, otherwise add the variable to
150  // the _vals variable
151  if (!(ss >> tmp) || !ss.eof())
152  mooseError(
153  "The input value '",
154  _vals_input[i],
155  "' was not understood, it must be a Real Number, Postprocessor, or Scalar Variable");
156  else
157  _vals.push_back(tmp);
158  }
159  }
160 }
161 
162 void
164 {
165  for (unsigned int i = 0; i < _pp_index.size(); ++i)
166  (*_addr[i]) = (*_pp_vals[i]);
167 
168  for (unsigned int i = 0; i < _scalar_index.size(); ++i)
169  (*_addr[i]) = (*_scalar_vals[i]);
170 }
RealVectorValue RealGradient
Definition: Assembly.h:43
const THREAD_ID _tid
The thread id passed from owning Function object.
VectorValue< Real > RealVectorValue
Definition: Assembly.h:40
VariableValue & sln()
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
bool hasPostprocessor(const std::string &name)
Check existence of the postprocessor.
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.
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 MooseVariableScalar & getScalarVariable(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.
const std::string & _function_str
Reference to the string containing the function to evaluate.
std::vector< Real * > _scalar_vals
Vector of pointers to PP values.
void update()
Updates postprocessor values for use in the libMesh::ParsedFunction.
virtual ~MooseParsedFunctionWrapper()
Class destruction Deletes the pointer to the dynamically allocated instance of the underlying libMesh...
std::vector< Real * > _addr
Vector of pointers to the variables in libMesh::ParsedFunction.
std::vector< unsigned int > _pp_index
Stores the relative location of variables (in _vars) that are connected to Postprocessors.
FEProblemBase & _feproblem
Reference to the FEProblemBase object.
std::unique_ptr< ParsedFunction< Real > > _function_ptr
Pointer to the libMesh::ParsedFunction object.
virtual bool hasScalarVariable(const std::string &var_name) override
Returns a Boolean indicating whether any system contains a variable with the name provided...
PostprocessorValue & getPostprocessorValue(const PostprocessorName &name)
Get a reference to the value associated with the postprocessor.
std::vector< unsigned int > _scalar_index
Stores the relative location of variables (in _vars) that are connected to Scalar Variables...
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 vals for use by the libMesh::ParsedFunction object a...
std::vector< Real > _vals
Storage for the values.
std::vector< const Real * > _pp_vals
Vector of pointers to PP values.
unsigned int THREAD_ID
Definition: MooseTypes.h:79