www.mooseframework.org
SolutionFunction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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"
12 #include "SolutionUserObject.h"
13 #include "MooseMesh.h"
14 
16 
18 SolutionFunction::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 
30  // Add optional paramters
31  params.addParam<Real>(
32  "scale_factor",
33  1.0,
34  "Scale factor (a) to be applied to the solution (x): ax+b, where b is the 'add_factor'");
35  params.addParam<Real>(
36  "add_factor",
37  0.0,
38  "Add this value (b) to the solution (x): ax+b, where a is the 'scale_factor'");
39 
40  // Return the parameters object
41  return params;
42 }
43 
45  : Function(parameters),
46  _solution_object_ptr(NULL),
47  _scale_factor(getParam<Real>("scale_factor")),
48  _add_factor(getParam<Real>("add_factor"))
49 {
50  for (unsigned int d = 0; d < _ti_feproblem.mesh().dimension(); ++d)
51  _add_grad(d) = _add_factor;
52 }
53 
54 void
55 SolutionFunction::initialSetup()
56 {
57  // Get a pointer to the SolutionUserObject. A pointer is used because the UserObject is not
58  // available during the
59  // construction of the function
60  _solution_object_ptr = &getUserObject<SolutionUserObject>("solution");
61 
62  std::string var_name;
63 
64  // If 'from_variable' is supplied, use the value
65  if (isParamValid("from_variable"))
66  var_name = getParam<std::string>("from_variable");
67 
68  // If not, get the value from the SolutionUserObject
69  else
70  {
71  // Get all the variables from the SolutionUserObject
72  const std::vector<std::string> & vars = _solution_object_ptr->variableNames();
73 
74  // If there are more than one, throw an error
75  if (vars.size() > 1)
76  mooseError("The SolutionUserObject contains multiple variables, the SolutionFunction must "
77  "specifiy the desired variable in the input file with 'from_variable'");
78 
79  // Define the variable
80  var_name = vars[0];
81  }
82  _solution_object_var_index = _solution_object_ptr->getLocalVarIndex(var_name);
83 }
84 
85 Real
86 SolutionFunction::value(Real t, const Point & p) const
87 {
88  return _scale_factor * (_solution_object_ptr->pointValue(t, p, _solution_object_var_index)) +
89  _add_factor;
90 }
91 
93 SolutionFunction::gradient(Real t, const Point & p) const
94 {
95  return _scale_factor *
96  (_solution_object_ptr->pointValueGradient(t, p, _solution_object_var_index)) +
97  _add_grad;
98 }
Base class for function objects.
Definition: Function.h:37
const std::vector< std::string > & variableNames() const
FEProblemBase & _ti_feproblem
unsigned int getLocalVarIndex(const std::string &var_name) const
Returns the local index for a given variable name.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
RealGradient pointValueGradient(Real t, const Point &p, const std::string &var_name, const std::set< subdomain_id_type > *subdomain_ids=nullptr) const
Returns the gradient at a specific location and variable (see SolutionFunction)
SolutionFunction()=default
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
Definition: MooseMesh.C:2678
Function for reading a solution from file Creates a function that extracts values from a solution rea...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual MooseMesh & mesh() override
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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 addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
registerMooseObject("MooseApp", SolutionFunction)
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)
static InputParameters validParams()
Class constructor.
Definition: Function.C:15