www.mooseframework.org
PiecewiseConstant.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 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 
32 Real
33 PiecewiseConstant::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
42  if ((_direction == Direction::LEFT &&
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  {
63  if (_direction == Direction::LEFT &&
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 
80 ADReal
81 PiecewiseConstant::value(const ADReal & t, const ADPoint & p) const
82 {
83  // piecewise constant has all zero derivatives (ignoring discontinuities)
85 }
86 
87 Real
88 PiecewiseConstant::timeDerivative(Real /*t*/, const Point & /*p*/) const
89 {
90  return 0;
91 }
92 
93 Real
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 
109 Real
111 {
112  return integral() / (domain(functionSize() - 1) - domain(0));
113 }
registerMooseObject("MooseApp", PiecewiseConstant)
virtual Real integral() const override
auto raw_value(const Eigen::Map< T > &in)
Definition: ADReal.h:73
Function which provides a piecewise constant interpolation of a provided (x,y) point data set...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Function base which provides a piecewise approximation to a provided (x,y) point data set via input p...
Direction
Enum for which direction to apply values.
virtual Real domain(const int i) const
Definition: PiecewiseBase.C:28
virtual Real range(const int i) const
Definition: PiecewiseBase.C:34
std::string getRawNames() const
Method for returning the raw name strings for this instance.
static InputParameters validParams()
virtual Real value(Real t, const Point &p) const override
Get the value of the function (based on time only)
virtual Real functionSize() const
Definition: PiecewiseBase.C:22
T sign(T x)
Definition: MathUtils.h:83
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
virtual Real timeDerivative(Real t, const Point &pt) const override
Get the time derivative of the function (based on time only)
DualReal ADReal
Definition: ADRealForward.h:14
int _axis
if _has_axis is true point component to use as function argument, otherwise use t ...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Real average() const override
PiecewiseConstant(const InputParameters &parameters)
enum PiecewiseConstant::Direction _direction
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...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
const Real & _scale_factor
function value scale factor
static InputParameters validParams()