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 "PiecewiseFunction.h" 11 : 12 : registerMooseObject("ThermalHydraulicsApp", PiecewiseFunction); 13 : 14 : InputParameters 15 47 : PiecewiseFunction::validParams() 16 : { 17 47 : InputParameters params = Function::validParams(); 18 : 19 94 : MooseEnum axis("x=0 y=1 z=2 t=3"); 20 94 : params.addRequiredParam<MooseEnum>("axis", axis, "Axis on which the N-1 delimiting points lie"); 21 94 : params.addRequiredParam<std::vector<Real>>( 22 : "axis_coordinates", 23 : "N-1 coordinates in the chosen axis, in increasing order, delimiting the N function regions"); 24 94 : params.addRequiredParam<std::vector<FunctionName>>("functions", "Functions in the N regions"); 25 : 26 47 : params.addClassDescription( 27 : "Function which provides a piecewise representation of arbitrary functions"); 28 : 29 47 : return params; 30 47 : } 31 : 32 28 : PiecewiseFunction::PiecewiseFunction(const InputParameters & parameters) 33 : : Function(parameters), 34 : FunctionInterface(this), 35 : 36 28 : _component(getParam<MooseEnum>("axis")), 37 28 : _use_time(_component == 3), 38 56 : _axis_coordinates(getParam<std::vector<Real>>("axis_coordinates")), 39 : 40 56 : _function_names(getParam<std::vector<FunctionName>>("functions")), 41 28 : _n_functions(_function_names.size()), 42 56 : _functions(_n_functions) 43 : { 44 : // Check that number of points is consistent with number of functions. 45 28 : if (_axis_coordinates.size() != _n_functions - 1) 46 2 : mooseError(name(), 47 : ": The number of entries in the parameter 'axis_coordinates' must" 48 : " equal the number of entries in the parameter 'functions' minus one."); 49 : 50 : // Check that coordinates are in ascending order. 51 : Real previous_coordinate = 0; 52 76 : for (unsigned int i = 0; i < _axis_coordinates.size(); i++) 53 : { 54 52 : if (i != 0 && _axis_coordinates[i] < previous_coordinate) 55 2 : mooseError(name(), 56 : ": The entries in the parameter 'axis_coordinates' must be in ascending order."); 57 50 : previous_coordinate = _axis_coordinates[i]; 58 : } 59 : 60 : // Store functions and check to make sure there is no self-reference. 61 92 : for (unsigned int i = 0; i < _n_functions; i++) 62 : { 63 70 : if (_function_names[i] == name()) 64 2 : mooseError(name(), ": This function cannot use its own name in the 'functions' parameter."); 65 : 66 68 : _functions[i] = &getFunctionByName(_function_names[i]); 67 : } 68 22 : } 69 : 70 : unsigned int 71 180 : PiecewiseFunction::getFunctionIndex(Real t, const Point & p) const 72 : { 73 180 : const Real x = _use_time ? t : p(_component); 74 : 75 : // Check if position is in the first N-1 regions. 76 306 : for (unsigned int i = 0; i < _n_functions - 1; i++) 77 270 : if (x < _axis_coordinates[i]) 78 144 : return i; 79 : 80 : // If function has not yet returned, it must be in the last region. 81 : return _n_functions - 1; 82 : } 83 : 84 : Real 85 180 : PiecewiseFunction::value(Real t, const Point & p) const 86 : { 87 180 : const unsigned int i = getFunctionIndex(t, p); 88 180 : return _functions[i]->value(t, p); 89 : } 90 : 91 : RealVectorValue 92 0 : PiecewiseFunction::gradient(Real t, const Point & p) const 93 : { 94 0 : const unsigned int i = getFunctionIndex(t, p); 95 0 : return _functions[i]->gradient(t, p); 96 : } 97 : 98 : Real 99 0 : PiecewiseFunction::timeDerivative(Real t, const Point & p) const 100 : { 101 0 : const unsigned int i = getFunctionIndex(t, p); 102 0 : return _functions[i]->timeDerivative(t, p); 103 : }