https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SolutionFunction.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
10#include "MooseError.h"
11#include "SolutionFunction.h"
13#include "MooseMesh.h"
14
16
18SolutionFunction::validParams()
19{
20 // Get the Function input parameters
22 params.addClassDescription("Function for reading a solution from file.");
23
24 // Add required parameters
25 params.addRequiredParam<UserObjectName>("solution",
26 "The SolutionUserObject to extract data from.");
27 params.addParam<std::string>("from_variable",
28 "The name of the variable in the file that is to be extracted");
29 params.addParam<MooseEnum>(
30 "weighting_type",
32 "The policy used to select a unique value when the imported solution is multivalued.");
33
34 // Add optional paramters
35 params.addParam<Real>(
36 "scale_factor",
37 1.0,
38 "Scale factor (a) to be applied to the solution (x): ax+b, where b is the 'add_factor'");
39 params.addParam<Real>(
40 "add_factor",
41 0.0,
42 "Add this value (b) to the solution (x): ax+b, where a is the 'scale_factor'");
43
44 // Return the parameters object
45 return params;
46}
47
49 : Function(parameters),
50 _solution_object_ptr(nullptr),
51 _weighting_type(isParamSetByUser("weighting_type")
52 ? std::make_optional(getParam<MooseEnum>("weighting_type")
53 .getEnum<SolutionUserObjectBase::WeightingType>())
54 : std::nullopt),
55 _scale_factor(getParam<Real>("scale_factor")),
56 _add_factor(getParam<Real>("add_factor"))
57{
58 for (unsigned int d = 0; d < _ti_feproblem.mesh().dimension(); ++d)
59 _add_grad(d) = _add_factor;
60}
61
62void
63SolutionFunction::initialSetup()
64{
65 // Get a pointer to the SolutionUserObject. A pointer is used because the UserObject is not
66 // available during the
67 // construction of the function
68 _solution_object_ptr = &getUserObject<SolutionUserObjectBase>("solution");
69
70 // If 'from_variable' is supplied, use the value
71 if (isParamValid("from_variable"))
72 _solution_object_var_name = getParam<std::string>("from_variable");
73
74 // If not, get the value from the SolutionUserObject
75 else
76 {
77 // Get all the variables from the SolutionUserObject
78 const std::vector<std::string> & vars = _solution_object_ptr->variableNames();
79
80 // If no variables are available, there is nothing for this function to extract
81 if (vars.empty())
83 "The SolutionUserObject contains no variables for the SolutionFunction to extract");
84
85 // If there is more than one, the desired source variable must be specified
86 if (vars.size() > 1)
87 mooseError("The SolutionUserObject contains multiple variables, the SolutionFunction must "
88 "specify the desired variable in the input file with 'from_variable'");
89
90 // Define the variable
91 _solution_object_var_name = vars[0];
92 }
93
94 if (!_solution_object_ptr->isVariableScalarValued(_solution_object_var_name))
96 "from_variable",
97 "The imported variable '",
98 _solution_object_var_name,
99 "' is vector-valued, but SolutionFunction supports only scalar-valued imported variables.");
100
101 // Warn when no explicit weighting policy is provided for a spatially discontinuous variable
102 if (_solution_object_ptr->isVariableADiscontinuousScalarField(_solution_object_var_name) &&
103 !_weighting_type)
105 "weighting_type",
106 "A weighting policy should be specified when the imported variable '",
107 _solution_object_var_name,
108 "' is spatially discontinuous. Values evaluated on element interfaces may depend on "
109 "source element ordering.");
110}
111
112Real
113SolutionFunction::value(Real t, const Point & p) const
114{
115 if (_weighting_type)
116 return _scale_factor *
117 _solution_object_ptr->pointValue(t, p, _solution_object_var_name, *_weighting_type) +
118 _add_factor;
119
120 return _scale_factor * _solution_object_ptr->pointValue(t, p, _solution_object_var_name) +
121 _add_factor;
122}
123
125SolutionFunction::gradient(Real t, const Point & p) const
126{
127 if (_weighting_type)
128 return _scale_factor * _solution_object_ptr->pointValueGradient(
129 t, p, _solution_object_var_name, *_weighting_type) +
130 _add_grad;
131
132 return _scale_factor * _solution_object_ptr->pointValueGradient(t, p, _solution_object_var_name) +
133 _add_grad;
134}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
char ** vars
registerMooseObject("MooseApp", SolutionFunction)
Base class for function objects.
Definition Function.h:30
static InputParameters validParams()
Class constructor.
Definition Function.C:16
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
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 paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
SolutionFunction()=default
void paramWarning(const std::string &param, Args... args) const
User object that reads an existing solution from an input file and uses it in the current simulation.
Real pointValue(Real t, const Point &p, const unsigned int local_var_index, const std::set< subdomain_id_type > *subdomain_ids=nullptr) const
Returns a value at a specific location and variable (see SolutionFunction)
bool isVariableADiscontinuousScalarField(const std::string &var_name) const
Returns whether the imported variable is a discontinuous scalar finite element field.
bool isVariableScalarValued(const std::string &var_name) const
Returns whether the imported variable has a scalar finite element field type.
const std::vector< std::string > & variableNames() const
libMesh::RealGradient pointValueGradient(Real t, const Point &p, const std::string &var_name, WeightingType weighting_type, const std::set< subdomain_id_type > *subdomain_ids=nullptr) const
Returns the gradient at a specific location and variable, reducing multiple gradients according to th...
RealVectorValue RealGradient
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real