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