https://mooseframework.inl.gov
PiecewiseLinearBase.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 "PiecewiseLinearBase.h"
11 
14 {
16  params.addClassDescription("Linearly interpolates between pairs of x-y data");
17  return params;
18 }
19 
21  : PiecewiseTabularBase(parameters), _linear_interp(nullptr), _interpolation_created(false)
22 {
23 }
24 
25 void
27 {
28  if (!_linear_interp)
29  mooseError("Classes derived from PiecewiseLinearBase need to call buildInterpolation()");
30 }
31 
32 void
34 {
35  // try building a linear interpolation object
36  try
37  {
38  _linear_interp = std::make_unique<LinearInterpolation>(_raw_x, _raw_y, extrap);
39  }
40  catch (std::domain_error & e)
41  {
42  mooseError("In PiecewiseLinearBase ", _name, ": ", e.what());
43  }
44 }
45 
46 Real
47 PiecewiseLinearBase::value(Real t, const Point & p) const
48 {
49  const auto x = _has_axis ? p(_axis) : t;
50  return _scale_factor * _linear_interp->sample(x);
51 }
52 
53 ADReal
54 PiecewiseLinearBase::value(const ADReal & t, const ADPoint & p) const
55 {
56  const auto x = _has_axis ? p(_axis) : t;
57  return _scale_factor * _linear_interp->sample(x);
58 }
59 
60 Real
61 PiecewiseLinearBase::timeDerivative(Real t, const Point &) const
62 {
63  return _has_axis ? 0.0 : _scale_factor * _linear_interp->sampleDerivative(t);
64 }
65 
67 PiecewiseLinearBase::gradient(Real, const Point & p) const
68 {
69  RealGradient ret;
70  if (_has_axis)
71  ret(_axis) = _scale_factor * _linear_interp->sampleDerivative(p(_axis));
72  return ret;
73 }
74 
75 Real
77 {
78  return _scale_factor * _linear_interp->integrate();
79 }
80 
81 Real
83 {
84  return integral() /
85  (_linear_interp->domain(_linear_interp->getSampleSize() - 1) - _linear_interp->domain(0));
86 }
87 
88 Real
89 PiecewiseLinearBase::timeIntegral(Real t1, Real t2, const Point & p) const
90 {
91  if (_has_axis)
92  // function is constant in time; evaluate at arbitrary point in time
93  // and multiply by time integral domain width
94  return value(t1, p) * (t2 - t1);
95  else
96  // function is piecewise linear in time
97  return _scale_factor * _linear_interp->integratePartial(t1, t2);
98 }
99 
100 void
101 PiecewiseLinearBase::setData(const std::vector<Real> & x, const std::vector<Real> & y)
102 {
105 }
std::unique_ptr< LinearInterpolation > _linear_interp
helper object to perform the linear interpolation of the function data
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual void setData(const std::vector< Real > &x, const std::vector< Real > &y)
Provides a means for explicitly setting the x and y data.
Definition: PiecewiseBase.C:40
Function base which provides a piecewise approximation to a provided (x,y) point data set via input p...
virtual void setData(const std::vector< Real > &x, const std::vector< Real > &y) override
Provides a means for explicitly setting the x and y data.
PiecewiseLinearBase(const InputParameters &parameters)
DualNumber< Real, DNDerivativeType, true > ADReal
Definition: ADRealForward.h:46
static InputParameters validParams()
std::vector< Real > _raw_x
raw function data as read
Definition: PiecewiseBase.h:39
virtual RealGradient gradient(Real, const Point &p) const override
Function objects can optionally provide a gradient at a point.
virtual Real average() const override
Returns the average of the function over its domain.
void buildInterpolation(const bool extrap=false)
Builds the linear interpolation object from the x/y data.
const std::string _name
The name of this class.
Definition: MooseBase.h:90
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...
int _axis
if _has_axis is true point component to use as function argument, otherwise use t ...
virtual Real timeDerivative(Real t, const Point &) const override
Get the time derivative of the function.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Real integral() const override
Returns the integral of the function over its domain.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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 Real & _scale_factor
function value scale factor
std::vector< Real > _raw_y
Definition: PiecewiseBase.h:40
virtual Real timeIntegral(Real t1, Real t2, const Point &p) const override
Computes the time integral at a spatial point between two time values.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...