www.mooseframework.org
Public Member Functions | Private Attributes | Static 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>

Public Member Functions

 LinearInterpolation (const std::vector< Real > &X, const std::vector< Real > &Y)
 
 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 ()
 
Real sample (Real x) const
 This function will take an independent variable input and will return the dependent variable based on the generated fit. More...
 
Real sampleDerivative (Real 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 ()
 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
 

Static Private Attributes

static int _file_number = 0
 

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 
)

Definition at line 18 of file LinearInterpolation.C.

19  : _x(x), _y(y)
20 {
21  errorCheck();
22 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x

◆ LinearInterpolation() [2/2]

LinearInterpolation::LinearInterpolation ( )
inline

Definition at line 29 of file LinearInterpolation.h.

29 : _x(std::vector<Real>()), _y(std::vector<Real>()) {}
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 89 of file LinearInterpolation.C.

90 {
91  return _x[i];
92 }
std::vector< Real > _x

◆ errorCheck()

void LinearInterpolation::errorCheck ( )

Definition at line 25 of file LinearInterpolation.C.

Referenced by LinearInterpolation(), and setData().

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

◆ getSampleSize()

unsigned int LinearInterpolation::getSampleSize ( )

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

the number of sample points

Definition at line 101 of file LinearInterpolation.C.

102 {
103  return _x.size();
104 }
std::vector< Real > _x

◆ integrate()

Real LinearInterpolation::integrate ( )

This function returns the integral of the function.

Definition at line 79 of file LinearInterpolation.C.

80 {
81  Real answer = 0;
82  for (unsigned int i = 1; i < _x.size(); ++i)
83  answer += 0.5 * (_y[i] + _y[i - 1]) * (_x[i] - _x[i - 1]);
84 
85  return answer;
86 }
std::vector< Real > _x
std::vector< Real > _y

◆ range()

Real LinearInterpolation::range ( int  i) const

Definition at line 95 of file LinearInterpolation.C.

96 {
97  return _y[i];
98 }
std::vector< Real > _y

◆ sample()

Real LinearInterpolation::sample ( Real  x) const

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

Definition at line 41 of file LinearInterpolation.C.

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

42 {
43  // sanity check (empty LinearInterpolations get constructed in many places
44  // so we cannot put this into the errorCheck)
45  assert(_x.size() > 0);
46 
47  // endpoint cases
48  if (x <= _x[0])
49  return _y[0];
50  if (x >= _x.back())
51  return _y.back();
52 
53  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
54  if (x >= _x[i] && x < _x[i + 1])
55  return _y[i] + (_y[i + 1] - _y[i]) * (x - _x[i]) / (_x[i + 1] - _x[i]);
56 
57  throw std::out_of_range("Unreachable");
58  return 0;
59 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x

◆ sampleDerivative()

Real LinearInterpolation::sampleDerivative ( Real  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 62 of file LinearInterpolation.C.

63 {
64  // endpoint cases
65  if (x < _x[0])
66  return 0.0;
67  if (x >= _x[_x.size() - 1])
68  return 0.0;
69 
70  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
71  if (x >= _x[i] && x < _x[i + 1])
72  return (_y[i + 1] - _y[i]) / (_x[i + 1] - _x[i]);
73 
74  throw std::out_of_range("Unreachable");
75  return 0;
76 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x

◆ setData()

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

Set the x and y values.

Definition at line 36 of file LinearInterpolation.h.

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

Member Data Documentation

◆ _file_number

int LinearInterpolation::_file_number = 0
staticprivate

Definition at line 76 of file LinearInterpolation.h.

◆ _x

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

◆ _y

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

Definition at line 74 of file LinearInterpolation.h.

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


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