https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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
27 "Function which provides a piecewise representation of arbitrary functions");
28
29 return params;
30}
31
33 : Function(parameters),
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)
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)
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
70unsigned int
71PiecewiseFunction::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
84Real
85PiecewiseFunction::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
91RealVectorValue
92PiecewiseFunction::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
98Real
99PiecewiseFunction::timeDerivative(Real t, const Point & p) const
100{
101 const unsigned int i = getFunctionIndex(t, p);
102 return _functions[i]->timeDerivative(t, p);
103}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
registerMooseObject("MooseApp", PiecewiseFunction)
Interface for objects that need to use functions.
const Function & getFunctionByName(const FunctionName &name) const
Get a function with a given name.
Base class for function objects.
Definition Function.h:30
static InputParameters validParams()
Class constructor.
Definition Function.C:16
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...
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.
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Function which provides a piecewise representation of arbitrary functions.
unsigned int getFunctionIndex(Real t, const Point &p) const
Gets the index of the function at the specified time and spatial point.
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 unsigned int _component
Axis on which the N-1 delimiting points lie.
std::vector< const Function * > _functions
Functions in the N regions.
const std::vector< FunctionName > & _function_names
Names of the functions in the N regions.
const bool _use_time
Use the time axis?
static InputParameters validParams()
virtual Real value(Real t, const Point &p) const override
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero,...
virtual Real timeDerivative(Real t, const Point &p) const override
Get the time derivative of the function.
PiecewiseFunction(const InputParameters &parameters)
virtual RealVectorValue gradient(Real t, const Point &p) const override
Function objects can optionally provide a gradient at a point.