https://mooseframework.inl.gov
Loading...
Searching...
No Matches
PiecewiseConstant.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 "PiecewiseConstant.h"
11#include "MathUtils.h"
12
14
17{
19 MooseEnum direction("LEFT RIGHT LEFT_INCLUSIVE RIGHT_INCLUSIVE", "LEFT");
20 params.addParam<MooseEnum>(
21 "direction", direction, "Direction to look to find value: " + direction.getRawNames());
22 params.addClassDescription("Defines piece-wise constant data using a set of x-y data pairs");
23 return params;
24}
25
27 : PiecewiseTabularBase(parameters),
28 _direction(getParam<MooseEnum>("direction").getEnum<Direction>())
29{
30}
31
32Real
33PiecewiseConstant::value(Real t, const Point & p) const
34{
35 const Real x = _has_axis ? p(_axis) : t;
36
37 unsigned i = 1;
38 const unsigned len = functionSize();
39 const Real tolerance = 1.0e-14;
40
41 // endpoint cases
43 x < (1 + tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
45 x < (1 - tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
47 x < (1 - tolerance * MathUtils::sign(domain(0))) * domain(0)) ||
49 x < (1 + tolerance * MathUtils::sign(domain(0))) * domain(0)))
50 return _scale_factor * range(0);
51 else if ((_direction == Direction::LEFT &&
52 x > (1 + tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
54 x > (1 - tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
56 x > (1 - tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)) ||
58 x > (1 + tolerance * MathUtils::sign(domain(len - 1))) * domain(len - 1)))
59 return _scale_factor * range(len - 1);
60
61 for (; i < len; ++i)
62 {
64 x < (1 + tolerance * MathUtils::sign(domain(i))) * domain(i))
65 return _scale_factor * range(i - 1);
67 x < (1 - tolerance * MathUtils::sign(domain(i))) * domain(i))
68 return _scale_factor * range(i - 1);
69 else if ((_direction == Direction::RIGHT &&
70 x < (1 - tolerance * MathUtils::sign(domain(i))) * domain(i)))
71 return _scale_factor * range(i);
73 x < (1 + tolerance * MathUtils::sign(domain(i))) * domain(i)))
74 return _scale_factor * range(i);
75 }
76
77 return 0.0;
78}
79
81PiecewiseConstant::value(const ADReal & t, const ADPoint & p) const
82{
83 // piecewise constant has all zero derivatives (ignoring discontinuities)
85}
86
87Real
88PiecewiseConstant::timeDerivative(Real /*t*/, const Point & /*p*/) const
89{
90 return 0;
91}
92
93Real
95{
96 const unsigned len = functionSize();
97 Real sum = 0;
98 unsigned offset = 0;
99
101 offset = 1;
102
103 for (unsigned i = 0; i < len - 1; ++i)
104 sum += range(i + offset) * (domain(i + 1) - domain(i));
105
106 return _scale_factor * sum;
107}
108
109Real
111{
112 return integral() / (domain(functionSize() - 1) - domain(0));
113}
DualNumber< Real, DNDerivativeType, true > ADReal
registerMooseObject("MooseApp", PiecewiseConstant)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
virtual Real functionSize() const
virtual Real domain(const int i) const
virtual Real range(const int i) const
Function which provides a piecewise constant interpolation of a provided (x,y) point data set.
virtual Real average() const override
Returns the average of the function over its domain.
enum PiecewiseConstant::Direction _direction
virtual Real integral() const override
Returns the integral of the function over its domain.
virtual Real timeDerivative(Real t, const Point &pt) const override
Get the time derivative of the function (based on time only)
Direction
Enum for which direction to apply values.
PiecewiseConstant(const InputParameters &parameters)
virtual Real value(Real t, const Point &p) const override
Get the value of the function (based on time only)
static InputParameters validParams()
Function base which provides a piecewise approximation to a provided (x,y) point data set via input p...
static InputParameters validParams()
const Real & _scale_factor
function value scale factor
unsigned int _axis
if _has_axis is true point component to use as function argument, otherwise use t
T sign(T x)
Definition MathUtils.h:84
auto raw_value(const Eigen::Map< T > &in)