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 
13 
14 template <>
17 {
19  MooseEnum direction("left right", "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 PiecewiseConstant::getDirection(const std::string & direction)
28 {
30  if (direction == "left")
31  {
33  }
34  else if (direction == "right")
35  {
37  }
38  else
39  {
40  mooseError("Unknown direction in PiecewiseConstant");
41  }
42  return dir;
43 }
44 
46  : Piecewise(parameters), _direction(getDirection(getParam<MooseEnum>("direction")))
47 {
48 }
49 
50 Real
51 PiecewiseConstant::value(Real t, const Point & p)
52 {
53  Real func_value(0);
54  Real x = t;
55  if (_has_axis)
56  {
57  x = p(_axis);
58  }
59 
60  unsigned i = 1;
61  const unsigned len = functionSize();
62  const Real toler = 1e-14;
63 
64  // endpoint cases
65  if ((_direction == LEFT && x < (1 + toler) * domain(0)) ||
66  (_direction == RIGHT && x < (1 - toler) * domain(0)))
67  {
68  func_value = range(0);
69  i = len;
70  }
71  if ((_direction == LEFT && x > (1 + toler) * domain(len - 1)) ||
72  (_direction == RIGHT && x > (1 - toler) * domain(len - 1)))
73  {
74  func_value = range(len - 1);
75  i = len;
76  }
77 
78  for (; i < len; ++i)
79  {
80  if (_direction == LEFT && x < (1 + toler) * domain(i))
81  {
82  func_value = range(i - 1);
83  break;
84  }
85  else if ((_direction == RIGHT && x < (1 - toler) * domain(i)))
86  {
87  func_value = range(i);
88  break;
89  }
90  }
91 
92  return _scale_factor * func_value;
93 }
94 
95 Real
96 PiecewiseConstant::timeDerivative(Real /*t*/, const Point & /*p*/)
97 {
98  return 0;
99 }
100 
101 Real
103 {
104  const unsigned len = functionSize();
105  Real sum = 0;
106  unsigned offset = 0;
107 
108  if (_direction == RIGHT)
109  offset = 1;
110 
111  for (unsigned i = 0; i < len - 1; ++i)
112  sum += range(i + offset) * (domain(i + 1) - domain(i));
113 
114  return _scale_factor * sum;
115 }
116 
117 Real
119 {
120  return integral() / (domain(functionSize() - 1) - domain(0));
121 }
InputParameters validParams< PiecewiseConstant >()
registerMooseObject("MooseApp", PiecewiseConstant)
virtual Real integral() override
DirectionEnum getDirection(const std::string &direction)
const Real _scale_factor
Definition: Piecewise.h:46
Function which provides a piecewise continuous constant interpolation of a provided (x...
virtual Real value(Real t, const Point &pt) override
Get the value of the function (based on time only)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual Real domain(const int i)
Definition: PiecewiseBase.C:70
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
static PetscErrorCode Vec x
std::string getRawNames() const
Method for returning the raw name strings for this instance.
virtual Real functionSize()
Definition: PiecewiseBase.C:64
InputParameters validParams< Piecewise >()
Definition: Piecewise.C:15
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 average() override
virtual Real range(const int i)
Definition: PiecewiseBase.C:76
virtual Real timeDerivative(Real t, const Point &pt) override
Get the time derivative of the function (based on time only)
PiecewiseConstant(const InputParameters &parameters)
const bool _has_axis
Definition: PiecewiseBase.h:45
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 DirectionEnum _direction
Function which provides a piecewise approximation to a provided (x,y) point data set generated from f...
Definition: Piecewise.h:25