www.mooseframework.org
Public Member Functions | Private Attributes | List of all members
LinearInterpolation Class Reference

This class interpolates values given a set of data pairs and an abscissa. More...

#include <LinearInterpolation.h>

Inheritance diagram for LinearInterpolation:
[legend]

Public Member Functions

 LinearInterpolation (const std::vector< Real > &X, const std::vector< Real > &Y, const bool extrap=false)
 
 LinearInterpolation ()
 
virtual ~LinearInterpolation ()=default
 
void setData (const std::vector< Real > &X, const std::vector< Real > &Y)
 Set the x and y values. More...
 
void errorCheck ()
 
template<typename T >
sample (const T &x) const
 This function will take an independent variable input and will return the dependent variable based on the generated fit. More...
 
template<typename T >
sampleDerivative (const T &x) const
 This function will take an independent variable input and will return the derivative of the dependent variable with respect to the independent variable based on the generated fit. More...
 
unsigned int getSampleSize () const
 This function returns the size of the array holding the points, i.e. More...
 
Real integrate ()
 This function returns the integral of the function. More...
 
Real domain (int i) const
 
Real range (int i) const
 

Private Attributes

std::vector< Real_x
 
std::vector< Real_y
 
bool _extrap
 

Detailed Description

This class interpolates values given a set of data pairs and an abscissa.

Definition at line 21 of file LinearInterpolation.h.

Constructor & Destructor Documentation

◆ LinearInterpolation() [1/2]

LinearInterpolation::LinearInterpolation ( const std::vector< Real > &  X,
const std::vector< Real > &  Y,
const bool  extrap = false 
)

Definition at line 19 of file LinearInterpolation.C.

22  : _x(x), _y(y), _extrap(extrap)
23 {
24  errorCheck();
25 }
std::vector< Real > _x
std::vector< Real > _y

◆ LinearInterpolation() [2/2]

LinearInterpolation::LinearInterpolation ( )
inline

Definition at line 31 of file LinearInterpolation.h.

31 : _x(std::vector<Real>()), _y(std::vector<Real>()), _extrap(false) {}
std::vector< Real > _x
std::vector< Real > _y

◆ ~LinearInterpolation()

virtual LinearInterpolation::~LinearInterpolation ( )
virtualdefault

Member Function Documentation

◆ domain()

Real LinearInterpolation::domain ( int  i) const

Definition at line 127 of file LinearInterpolation.C.

128 {
129  return _x[i];
130 }
std::vector< Real > _x

◆ errorCheck()

void LinearInterpolation::errorCheck ( )

Definition at line 28 of file LinearInterpolation.C.

Referenced by LinearInterpolation(), and setData().

29 {
30  if (_x.size() != _y.size())
31  throw std::domain_error("Vectors are not the same length");
32 
33  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
34  if (_x[i] >= _x[i + 1])
35  {
36  std::ostringstream oss;
37  oss << "x-values are not strictly increasing: x[" << i << "]: " << _x[i] << " x[" << i + 1
38  << "]: " << _x[i + 1];
39  throw std::domain_error(oss.str());
40  }
41 }
std::vector< Real > _x
std::vector< Real > _y

◆ getSampleSize()

unsigned int LinearInterpolation::getSampleSize ( ) const

This function returns the size of the array holding the points, i.e.

the number of sample points

Definition at line 139 of file LinearInterpolation.C.

140 {
141  return _x.size();
142 }
std::vector< Real > _x

◆ integrate()

Real LinearInterpolation::integrate ( )

This function returns the integral of the function.

Definition at line 117 of file LinearInterpolation.C.

118 {
119  Real answer = 0;
120  for (unsigned int i = 1; i < _x.size(); ++i)
121  answer += 0.5 * (_y[i] + _y[i - 1]) * (_x[i] - _x[i - 1]);
122 
123  return answer;
124 }
std::vector< Real > _x
std::vector< Real > _y
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ range()

Real LinearInterpolation::range ( int  i) const

Definition at line 133 of file LinearInterpolation.C.

134 {
135  return _y[i];
136 }
std::vector< Real > _y

◆ sample()

template<typename T >
template ChainedReal LinearInterpolation::sample< ChainedReal > ( const T &  x) const

This function will take an independent variable input and will return the dependent variable based on the generated fit.

Definition at line 45 of file LinearInterpolation.C.

Referenced by IterationAdaptiveDT::computeDT(), IterationAdaptiveDT::computeInitialDT(), and IterationAdaptiveDT::computeInterpolationDT().

46 {
47  // this is a hard error
48  if (_x.empty())
49  mooseError("Trying to evaluate an empty LinearInterpolation");
50 
51  // special case for single function nodes
52  if (_x.size() == 1)
53  return _y[0];
54 
55  // endpoint cases
56  if (_extrap)
57  {
58  if (x < _x[0])
59  return _y[0] + (x - _x[0]) / (_x[1] - _x[0]) * (_y[1] - _y[0]);
60  if (x >= _x.back())
61  return _y.back() +
62  (x - _x.back()) / (_x[_x.size() - 2] - _x.back()) * (_y[_y.size() - 2] - _y.back());
63  }
64  else
65  {
66  if (x < _x[0])
67  return _y[0];
68  if (x >= _x.back())
69  return _y.back();
70  }
71 
72  auto upper = std::upper_bound(_x.begin(), _x.end(), x);
73  int i = std::distance(_x.begin(), upper) - 1;
74  return _y[i] + (_y[i + 1] - _y[i]) * (x - _x[i]) / (_x[i + 1] - _x[i]);
75 
76  // If this point is reached, x must be a NaN.
77  mooseException("Sample point in LinearInterpolation is a NaN.");
78 }
std::vector< Real > _x
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::vector< Real > _y

◆ sampleDerivative()

template<typename T >
template ChainedReal LinearInterpolation::sampleDerivative< ChainedReal > ( const T &  x) const

This function will take an independent variable input and will return the derivative of the dependent variable with respect to the independent variable based on the generated fit.

Definition at line 86 of file LinearInterpolation.C.

87 {
88  // endpoint cases
89  if (_extrap)
90  {
91  if (x <= _x[0])
92  return (_y[1] - _y[0]) / (_x[1] - _x[0]);
93  if (x >= _x.back())
94  return (_y[_y.size() - 2] - _y.back()) / (_x[_x.size() - 2] - _x.back());
95  }
96  else
97  {
98  if (x < _x[0])
99  return 0.0;
100  if (x >= _x.back())
101  return 0.0;
102  }
103 
104  auto upper = std::upper_bound(_x.begin(), _x.end(), x);
105  int i = std::distance(_x.begin(), upper) - 1;
106  return (_y[i + 1] - _y[i]) / (_x[i + 1] - _x[i]);
107 
108  // If this point is reached, x must be a NaN.
109  mooseException("Sample point in LinearInterpolation is a NaN.");
110 }
std::vector< Real > _x
std::vector< Real > _y

◆ setData()

void LinearInterpolation::setData ( const std::vector< Real > &  X,
const std::vector< Real > &  Y 
)
inline

Set the x and y values.

Definition at line 38 of file LinearInterpolation.h.

39  {
40  _x = X;
41  _y = Y;
42  errorCheck();
43  }
std::vector< Real > _x
std::vector< Real > _y

Member Data Documentation

◆ _extrap

bool LinearInterpolation::_extrap
private

Definition at line 80 of file LinearInterpolation.h.

Referenced by sample(), and sampleDerivative().

◆ _x

std::vector<Real> LinearInterpolation::_x
private

◆ _y

std::vector<Real> LinearInterpolation::_y
private

Definition at line 78 of file LinearInterpolation.h.

Referenced by errorCheck(), integrate(), range(), sample(), sampleDerivative(), and setData().


The documentation for this class was generated from the following files: