www.mooseframework.org
PiecewiseConstant.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "PiecewiseConstant.h"
16 
17 template <>
20 {
22  MooseEnum direction("left right", "left");
23  params.addParam<MooseEnum>(
24  "direction", direction, "Direction to look to find value: " + direction.getRawNames());
25  params.addClassDescription("Defines data using a set of x-y data pairs");
26  return params;
27 }
28 
30 PiecewiseConstant::getDirection(const std::string & direction)
31 {
33  if (direction == "left")
34  {
36  }
37  else if (direction == "right")
38  {
40  }
41  else
42  {
43  mooseError("Unknown direction in PiecewiseConstant");
44  }
45  return dir;
46 }
47 
49  : Piecewise(parameters), _direction(getDirection(getParam<MooseEnum>("direction")))
50 {
51 }
52 
53 Real
54 PiecewiseConstant::value(Real t, const Point & p)
55 {
56  Real func_value(0);
57  Real x = t;
58  if (_has_axis)
59  {
60  x = p(_axis);
61  }
62 
63  unsigned i = 1;
64  const unsigned len = functionSize();
65  const Real toler = 1e-14;
66 
67  // endpoint cases
68  if ((_direction == LEFT && x < (1 + toler) * domain(0)) ||
69  (_direction == RIGHT && x < (1 - toler) * domain(0)))
70  {
71  func_value = range(0);
72  i = len;
73  }
74  if ((_direction == LEFT && x > (1 + toler) * domain(len - 1)) ||
75  (_direction == RIGHT && x > (1 - toler) * domain(len - 1)))
76  {
77  func_value = range(len - 1);
78  i = len;
79  }
80 
81  for (; i < len; ++i)
82  {
83  if (_direction == LEFT && x < (1 + toler) * domain(i))
84  {
85  func_value = range(i - 1);
86  break;
87  }
88  else if ((_direction == RIGHT && x < (1 - toler) * domain(i)))
89  {
90  func_value = range(i);
91  break;
92  }
93  }
94 
95  return _scale_factor * func_value;
96 }
97 
98 Real
99 PiecewiseConstant::timeDerivative(Real /*t*/, const Point & /*p*/)
100 {
101  return 0;
102 }
103 
104 Real
106 {
107  const unsigned len = functionSize();
108  Real sum = 0;
109  unsigned offset = 0;
110 
111  if (_direction == RIGHT)
112  offset = 1;
113 
114  for (unsigned i = 0; i < len - 1; ++i)
115  sum += range(i + offset) * (domain(i + 1) - domain(i));
116 
117  return _scale_factor * sum;
118 }
119 
120 Real
122 {
123  return integral() / (domain(functionSize() - 1) - domain(0));
124 }
InputParameters validParams< PiecewiseConstant >()
virtual Real integral() override
DirectionEnum getDirection(const std::string &direction)
int _axis
Definition: Piecewise.h:63
bool _has_axis
Definition: Piecewise.h:64
const Real _scale_factor
Definition: Piecewise.h:61
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...
static PetscErrorCode Vec x
InputParameters validParams< Piecewise >()
Definition: Piecewise.C:22
virtual Real domain(int i)
Definition: Piecewise.C:114
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
virtual Real average() override
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
virtual Real range(int i)
Definition: Piecewise.C:120
std::string getRawNames() const
Method for returning the raw name strings for this instance.
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 InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseObject.h:53
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
const DirectionEnum _direction
Function which provides a piecewise approximation to a provided (x,y) point data set.
Definition: Piecewise.h:31
virtual Real functionSize()
Definition: Piecewise.C:108