Line data Source code
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" 12 : #include "SolutionUserObjectBase.h" 13 : #include "MooseMesh.h" 14 : 15 : registerMooseObject("MooseApp", SolutionFunction); 16 : 17 : InputParameters 18 14610 : SolutionFunction::validParams() 19 : { 20 : // Get the Function input parameters 21 14610 : InputParameters params = Function::validParams(); 22 14610 : params.addClassDescription("Function for reading a solution from file."); 23 : 24 : // Add required parameters 25 14610 : params.addRequiredParam<UserObjectName>("solution", 26 : "The SolutionUserObject to extract data from."); 27 14610 : 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 43830 : params.addParam<Real>( 32 : "scale_factor", 33 29220 : 1.0, 34 : "Scale factor (a) to be applied to the solution (x): ax+b, where b is the 'add_factor'"); 35 43830 : params.addParam<Real>( 36 : "add_factor", 37 29220 : 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 14610 : return params; 42 0 : } 43 : 44 185 : SolutionFunction::SolutionFunction(const InputParameters & parameters) 45 : : Function(parameters), 46 185 : _solution_object_ptr(NULL), 47 185 : _scale_factor(getParam<Real>("scale_factor")), 48 370 : _add_factor(getParam<Real>("add_factor")) 49 : { 50 638 : for (unsigned int d = 0; d < _ti_feproblem.mesh().dimension(); ++d) 51 453 : _add_grad(d) = _add_factor; 52 185 : } 53 : 54 : void 55 184 : 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 184 : _solution_object_ptr = &getUserObject<SolutionUserObjectBase>("solution"); 61 : 62 184 : std::string var_name; 63 : 64 : // If 'from_variable' is supplied, use the value 65 184 : if (isParamValid("from_variable")) 66 82 : 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 102 : const std::vector<std::string> & vars = _solution_object_ptr->variableNames(); 73 : 74 : // If there are more than one, throw an error 75 102 : if (vars.size() > 1) 76 0 : 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 102 : var_name = vars[0]; 81 : } 82 184 : _solution_object_var_index = _solution_object_ptr->getLocalVarIndex(var_name); 83 180 : } 84 : 85 : Real 86 213120 : SolutionFunction::value(Real t, const Point & p) const 87 : { 88 213120 : return _scale_factor * (_solution_object_ptr->pointValue(t, p, _solution_object_var_index)) + 89 213120 : _add_factor; 90 : } 91 : 92 : RealGradient 93 56628 : SolutionFunction::gradient(Real t, const Point & p) const 94 : { 95 0 : return _scale_factor * 96 113256 : (_solution_object_ptr->pointValueGradient(t, p, _solution_object_var_index)) + 97 169884 : _add_grad; 98 : }