https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
25void
27{
28 if (!_linear_interp)
29 mooseError("Classes derived from PiecewiseLinearBase need to call buildInterpolation()");
30}
31
32void
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
46Real
47PiecewiseLinearBase::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
54PiecewiseLinearBase::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
60Real
61PiecewiseLinearBase::timeDerivative(Real t, const Point &) const
62{
63 return _has_axis ? 0.0 : _scale_factor * _linear_interp->sampleDerivative(t);
64}
65
66RealGradient
67PiecewiseLinearBase::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
75Real
77{
78 return _scale_factor * _linear_interp->integrate();
79}
80
81Real
83{
84 return integral() /
85 (_linear_interp->domain(_linear_interp->getSampleSize() - 1) - _linear_interp->domain(0));
86}
87
88Real
89PiecewiseLinearBase::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
100void
101PiecewiseLinearBase::setData(const std::vector<Real> & x, const std::vector<Real> & y)
102{
105}
DualNumber< Real, DNDerivativeType, true > ADReal
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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 std::string & _name
The name of this class.
Definition MooseBase.h:381
virtual void setData(const std::vector< Real > &x, const std::vector< Real > &y)
Provides a means for explicitly setting the x and y data.
std::vector< Real > _raw_x
raw function data as read
std::vector< Real > _raw_y
virtual Real integral() const override
Returns the integral of the function over its domain.
std::unique_ptr< LinearInterpolation > _linear_interp
helper object to perform the linear interpolation of the function data
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
virtual Real average() const override
Returns the average of the function over its domain.
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,...
virtual RealGradient gradient(Real, const Point &p) const override
Function objects can optionally provide a gradient at a point.
virtual Real timeIntegral(Real t1, Real t2, const Point &p) const override
Computes the time integral at a spatial point between two time values.
PiecewiseLinearBase(const InputParameters &parameters)
void buildInterpolation(const bool extrap=false)
Builds the linear interpolation object from the x/y data.
static InputParameters validParams()
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.
virtual Real timeDerivative(Real t, const Point &) const override
Get the time derivative of the function.
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