https://mooseframework.inl.gov
PiecewiseFunction.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 "PiecewiseFunction.h"
11 
12 registerMooseObject("ThermalHydraulicsApp", PiecewiseFunction);
13 
16 {
18 
19  MooseEnum axis("x=0 y=1 z=2 t=3");
20  params.addRequiredParam<MooseEnum>("axis", axis, "Axis on which the N-1 delimiting points lie");
21  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  params.addRequiredParam<std::vector<FunctionName>>("functions", "Functions in the N regions");
25 
26  params.addClassDescription(
27  "Function which provides a piecewise representation of arbitrary functions");
28 
29  return params;
30 }
31 
33  : Function(parameters),
34  FunctionInterface(this),
35 
36  _component(getParam<MooseEnum>("axis")),
37  _use_time(_component == 3),
38  _axis_coordinates(getParam<std::vector<Real>>("axis_coordinates")),
39 
40  _function_names(getParam<std::vector<FunctionName>>("functions")),
41  _n_functions(_function_names.size()),
42  _functions(_n_functions)
43 {
44  // Check that number of points is consistent with number of functions.
45  if (_axis_coordinates.size() != _n_functions - 1)
46  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  for (unsigned int i = 0; i < _axis_coordinates.size(); i++)
53  {
54  if (i != 0 && _axis_coordinates[i] < previous_coordinate)
55  mooseError(name(),
56  ": The entries in the parameter 'axis_coordinates' must be in ascending order.");
57  previous_coordinate = _axis_coordinates[i];
58  }
59 
60  // Store functions and check to make sure there is no self-reference.
61  for (unsigned int i = 0; i < _n_functions; i++)
62  {
63  if (_function_names[i] == name())
64  mooseError(name(), ": This function cannot use its own name in the 'functions' parameter.");
65 
67  }
68 }
69 
70 unsigned int
71 PiecewiseFunction::getFunctionIndex(Real t, const Point & p) const
72 {
73  const Real x = _use_time ? t : p(_component);
74 
75  // Check if position is in the first N-1 regions.
76  for (unsigned int i = 0; i < _n_functions - 1; i++)
77  if (x < _axis_coordinates[i])
78  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 PiecewiseFunction::value(Real t, const Point & p) const
86 {
87  const unsigned int i = getFunctionIndex(t, p);
88  return _functions[i]->value(t, p);
89 }
90 
92 PiecewiseFunction::gradient(Real t, const Point & p) const
93 {
94  const unsigned int i = getFunctionIndex(t, p);
95  return _functions[i]->gradient(t, p);
96 }
97 
98 Real
99 PiecewiseFunction::timeDerivative(Real t, const Point & p) const
100 {
101  const unsigned int i = getFunctionIndex(t, p);
102  return _functions[i]->timeDerivative(t, p);
103 }
const std::vector< Real > & _axis_coordinates
N-1 coordinates in the chosen axis, in increasing order, delimiting the N function regions...
const unsigned int _n_functions
Number of functions, N.
const std::vector< FunctionName > & _function_names
Names of the functions in the N regions.
virtual Real timeDerivative(Real t, const Point &p) const override
static const std::string axis
Definition: NS.h:27
virtual Real value(Real t, const Point &p) const override
const bool _use_time
Use the time axis?
virtual const std::string & name() const
void addRequiredParam(const std::string &name, const std::string &doc_string)
const std::vector< double > x
virtual RealVectorValue gradient(Real t, const Point &p) const override
static InputParameters validParams()
PiecewiseFunction(const InputParameters &parameters)
Function which provides a piecewise representation of arbitrary functions.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Function & getFunctionByName(const FunctionName &name) const
unsigned int getFunctionIndex(Real t, const Point &p) const
Gets the index of the function at the specified time and spatial point.
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
std::vector< const Function * > _functions
Functions in the N regions.
static InputParameters validParams()
const unsigned int _component
Axis on which the N-1 delimiting points lie.
registerMooseObject("ThermalHydraulicsApp", PiecewiseFunction)