www.mooseframework.org
Public Member Functions | Protected Member Functions | Static Protected Attributes | List of all members
SplineInterpolationBase Class Reference

#include <SplineInterpolationBase.h>

Inheritance diagram for SplineInterpolationBase:
[legend]

Public Member Functions

 SplineInterpolationBase ()
 
virtual ~SplineInterpolationBase ()=default
 
Real sample (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
 
ADReal sample (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, const ADReal &x_int) const
 
Real sampleDerivative (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
 
Real sample2ndDerivative (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
 

Protected Member Functions

void spline (const std::vector< Real > &x, const std::vector< Real > &y, std::vector< Real > &y2, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
 This function calculates the second derivatives based on supplied x and y-vectors. More...
 
void findInterval (const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
 
template<typename T >
void computeCoeffs (const std::vector< Real > &x, unsigned int klo, unsigned int khi, const T &x_int, Real &h, T &a, T &b) const
 
template<typename T >
sample (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, const T &x_int, unsigned int klo, unsigned int khi) const
 Sample value at point x_int given the indices of the vector of dependent values that bound the point. More...
 

Static Protected Attributes

static const Real _deriv_bound = std::numeric_limits<Real>::max()
 

Detailed Description

Definition at line 15 of file SplineInterpolationBase.h.

Constructor & Destructor Documentation

◆ SplineInterpolationBase()

SplineInterpolationBase::SplineInterpolationBase ( )

Definition at line 16 of file SplineInterpolationBase.C.

16 {}

◆ ~SplineInterpolationBase()

virtual SplineInterpolationBase::~SplineInterpolationBase ( )
virtualdefault

Member Function Documentation

◆ computeCoeffs()

template<typename T >
void SplineInterpolationBase::computeCoeffs ( const std::vector< Real > &  x,
unsigned int  klo,
unsigned int  khi,
const T &  x_int,
Real h,
T &  a,
T &  b 
) const
protected

Definition at line 85 of file SplineInterpolationBase.C.

Referenced by sample(), sample2ndDerivative(), and sampleDerivative().

92 {
93  h = x[khi] - x[klo];
94  if (h == 0)
95  mooseError("The values of x must be distinct");
96  a = (x[khi] - x_int) / h;
97  b = (x_int - x[klo]) / h;
98 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284

◆ findInterval()

void SplineInterpolationBase::findInterval ( const std::vector< Real > &  x,
Real  x_int,
unsigned int klo,
unsigned int khi 
) const
protected

Definition at line 65 of file SplineInterpolationBase.C.

Referenced by BicubicSplineInterpolation::constructColumnSpline(), BicubicSplineInterpolation::constructRowSpline(), sample(), sample2ndDerivative(), and sampleDerivative().

69 {
70  klo = 0;
71  mooseAssert(x.size() >= 2, "You must have at least two knots to create a spline.");
72  khi = x.size() - 1;
73  while (khi - klo > 1)
74  {
75  unsigned int k = (khi + klo) >> 1;
76  if (x[k] > x_int)
77  khi = k;
78  else
79  klo = k;
80  }
81 }

◆ sample() [1/3]

Real SplineInterpolationBase::sample ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
Real  x_int 
) const

Definition at line 101 of file SplineInterpolationBase.C.

Referenced by BicubicSplineInterpolation::constructColumnSpline(), BicubicSplineInterpolation::constructRowSpline(), sample(), SplineInterpolation::sample(), BicubicSplineInterpolation::sample(), and BicubicSplineInterpolation::sampleValueAndDerivatives().

105 {
106  unsigned int klo, khi;
107  findInterval(x, x_int, klo, khi);
108 
109  return sample(x, y, y2, x_int, klo, khi);
110 }
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const

◆ sample() [2/3]

ADReal SplineInterpolationBase::sample ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
const ADReal x_int 
) const

Definition at line 113 of file SplineInterpolationBase.C.

117 {
118  unsigned int klo, khi;
119  findInterval(x, MetaPhysicL::raw_value(x_int), klo, khi);
120 
121  return sample(x, y, y2, x_int, klo, khi);
122 }
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
auto raw_value(const Eigen::Map< T > &in)
Definition: ADReal.h:73
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const

◆ sample() [3/3]

template<typename T >
T SplineInterpolationBase::sample ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
const T &  x_int,
unsigned int  klo,
unsigned int  khi 
) const
protected

Sample value at point x_int given the indices of the vector of dependent values that bound the point.

This method is useful in bicubic spline interpolation, where several spline evaluations are needed to sample from a 2D point.

Definition at line 157 of file SplineInterpolationBase.C.

163 {
164  Real h;
165  T a, b;
166  computeCoeffs(x, klo, khi, x_int, h, a, b);
167 
168  return a * y[klo] + b * y[khi] +
169  ((a * a * a - a) * y2[klo] + (b * b * b - b) * y2[khi]) * (h * h) / 6.0;
170 }
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, const T &x_int, Real &h, T &a, T &b) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ sample2ndDerivative()

Real SplineInterpolationBase::sample2ndDerivative ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
Real  x_int 
) const

Definition at line 141 of file SplineInterpolationBase.C.

Referenced by SplineInterpolation::sample2ndDerivative(), and BicubicSplineInterpolation::sample2ndDerivative().

145 {
146  unsigned int klo, khi;
147  findInterval(x, x_int, klo, khi);
148 
149  Real h, a, b;
150  computeCoeffs(x, klo, khi, x_int, h, a, b);
151 
152  return a * y2[klo] + b * y2[khi];
153 }
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, const T &x_int, Real &h, T &a, T &b) const
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ sampleDerivative()

Real SplineInterpolationBase::sampleDerivative ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
Real  x_int 
) const

Definition at line 125 of file SplineInterpolationBase.C.

Referenced by SplineInterpolation::sampleDerivative(), BicubicSplineInterpolation::sampleDerivative(), and BicubicSplineInterpolation::sampleValueAndDerivatives().

129 {
130  unsigned int klo, khi;
131  findInterval(x, x_int, klo, khi);
132 
133  Real h, a, b;
134  computeCoeffs(x, klo, khi, x_int, h, a, b);
135 
136  return (y[khi] - y[klo]) / h -
137  (((3.0 * a * a - 1.0) * y2[klo] + (3.0 * b * b - 1.0) * -y2[khi]) * h / 6.0);
138 }
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, const T &x_int, Real &h, T &a, T &b) const
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ spline()

void SplineInterpolationBase::spline ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
std::vector< Real > &  y2,
Real  yp1 = _deriv_bound,
Real  ypn = _deriv_bound 
)
protected

This function calculates the second derivatives based on supplied x and y-vectors.

Definition at line 19 of file SplineInterpolationBase.C.

Referenced by BicubicSplineInterpolation::constructColumnSpline(), BicubicSplineInterpolation::constructColumnSplineSecondDerivativeTable(), BicubicSplineInterpolation::constructRowSpline(), BicubicSplineInterpolation::constructRowSplineSecondDerivativeTable(), and SplineInterpolation::solve().

24 {
25  auto n = x.size();
26  if (n < 2)
27  mooseError("You must have at least two knots to create a spline.");
28 
29  std::vector<Real> u(n, 0.);
30  y2.assign(n, 0.);
31 
32  if (yp1 >= 1e30)
33  y2[0] = u[0] = 0.;
34  else
35  {
36  y2[0] = -0.5;
37  u[0] = (3.0 / (x[1] - x[0])) * ((y[1] - y[0]) / (x[1] - x[0]) - yp1);
38  }
39  // decomposition of tri-diagonal algorithm (y2 and u are used for temporary storage)
40  for (decltype(n) i = 1; i < n - 1; i++)
41  {
42  Real sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
43  Real p = sig * y2[i - 1] + 2.0;
44  y2[i] = (sig - 1.0) / p;
45  u[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]);
46  u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
47  }
48 
49  Real qn, un;
50  if (ypn >= 1e30)
51  qn = un = 0.;
52  else
53  {
54  qn = 0.5;
55  un = (3.0 / (x[n - 1] - x[n - 2])) * (ypn - (y[n - 1] - y[n - 2]) / (x[n - 1] - x[n - 2]));
56  }
57 
58  y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.);
59  // back substitution
60  for (auto k = n - 1; k >= 1; k--)
61  y2[k - 1] = y2[k - 1] * y2[k] + u[k - 1];
62 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

Member Data Documentation

◆ _deriv_bound

const Real SplineInterpolationBase::_deriv_bound = std::numeric_limits<Real>::max()
staticprotected

Definition at line 79 of file SplineInterpolationBase.h.

Referenced by BicubicSplineInterpolation::errorCheck().


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