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 
17 template <>
20 {
21  // Get the Function input parameters
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)
52 }
53 
54 void
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  }
83 }
84 
85 Real
86 SolutionFunction::value(Real t, const Point & p)
87 {
90 }
91 
92 RealGradient
93 SolutionFunction::gradient(Real t, const Point & p)
94 {
95  return _scale_factor *
97  _add_grad;
98 }
Base class for function objects.
Definition: Function.h:40
const std::vector< std::string > & variableNames() const
FEProblemBase & _ti_feproblem
Real pointValue(Real t, const Point &p, const unsigned int local_var_index) const
Returns a value at a specific location and variable (see SolutionFunction)
virtual Real value(Real t, const Point &p) override
Extract a value from the solution.
RealGradient _add_grad
Factor to add to the solution if gradient is requested (default = {0})
unsigned int getLocalVarIndex(const std::string &var_name) const
Returns the local index for a given variable name.
virtual RealGradient gradient(Real t, const Point &p) override
Extract a gradient from the solution.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const SolutionUserObject * _solution_object_ptr
Pointer to SolutionUserObject containing the solution of interest.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
InputParameters validParams< SolutionFunction >()
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...
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimsension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh m...
Definition: MooseMesh.C:2133
const Real _scale_factor
Factor to scale the solution by (default = 1)
Function for reading a solution from file Creates a function that extracts values from a solution rea...
const Real _add_factor
Factor to add to the solution (default = 0)
RealGradient pointValueGradient(Real t, const Point &p, const std::string &var_name) const
Returns the gradient at a specific location and variable (see SolutionFunction)
unsigned int _solution_object_var_index
The local SolutionUserObject index for the variable extracted from the file.
virtual MooseMesh & mesh() override
SolutionFunction(const InputParameters &parameters)
Constructor.
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)
InputParameters validParams< Function >()
Definition: Function.C:14
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
virtual void initialSetup() override
Setup the function for use Gathers a pointer to the SolutionUserObject containing the solution that w...