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

This class interpolates tabulated functions with cubic splines. More...

#include <SplineInterpolation.h>

Inheritance diagram for SplineInterpolation:
[legend]

Public Member Functions

 SplineInterpolation ()
 
 SplineInterpolation (const std::vector< Real > &x, const std::vector< Real > &y, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
 Construct the object. More...
 
virtual ~SplineInterpolation ()=default
 
void setData (const std::vector< Real > &x, const std::vector< Real > &y, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
 Set the x-, y- values and first derivatives. 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
 
Real sample2ndDerivative (Real x) const
 
void dumpSampleFile (std::string base_name, std::string x_label="X", std::string y_label="Y", float xmin=0, float xmax=0, float ymin=0, float ymax=0)
 This function will dump GNUPLOT input files that can be run to show the data points and function fits. More...
 
unsigned int getSampleSize ()
 This function returns the size of the array holding the points, i.e. More...
 
Real domain (int i) const
 
Real range (int i) const
 
Real sample (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real 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 solve ()
 
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
 
void computeCoeffs (const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
 

Protected Attributes

std::vector< Real > _x
 
std::vector< Real > _y
 
Real _yp1
 boundary conditions More...
 
Real _ypn
 
std::vector< Real > _y2
 Second derivatives. More...
 

Static Protected Attributes

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

Detailed Description

This class interpolates tabulated functions with cubic splines.

Adopted from Numerical Recipes in C (section 3.3).

Definition at line 26 of file SplineInterpolation.h.

Constructor & Destructor Documentation

SplineInterpolation::SplineInterpolation ( )

Definition at line 25 of file SplineInterpolation.C.

25 {}
SplineInterpolation::SplineInterpolation ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
Real  yp1 = _deriv_bound,
Real  ypn = _deriv_bound 
)

Construct the object.

Parameters
xTabulated function (x-positions)
yTabulated function (y-positions)
yp1First derivative of the interpolating function at point 1
ypnFirst derivative of the interpolating function at point n

If yp1, ypn are not specified or greater or equal that _deriv_bound, we use natural spline

Definition at line 27 of file SplineInterpolation.C.

31  : SplineInterpolationBase(), _x(x), _y(y), _yp1(yp1), _ypn(ypn)
32 {
33  errorCheck();
34  solve();
35 }
Real _yp1
boundary conditions
static PetscErrorCode Vec x
std::vector< Real > _x
std::vector< Real > _y
virtual SplineInterpolation::~SplineInterpolation ( )
virtualdefault

Member Function Documentation

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

Definition at line 89 of file SplineInterpolationBase.C.

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

96 {
97  h = x[khi] - x[klo];
98  if (h == 0)
99  mooseError("The values of x must be distinct");
100  a = (x[khi] - x_int) / h;
101  b = (x_int - x[klo]) / h;
102 }
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
static PetscErrorCode Vec x
Real SplineInterpolation::domain ( int  i) const

Definition at line 91 of file SplineInterpolation.C.

92 {
93  return _x[i];
94 }
std::vector< Real > _x
void SplineInterpolation::dumpSampleFile ( std::string  base_name,
std::string  x_label = "X",
std::string  y_label = "Y",
float  xmin = 0,
float  xmax = 0,
float  ymin = 0,
float  ymax = 0 
)

This function will dump GNUPLOT input files that can be run to show the data points and function fits.

Definition at line 103 of file SplineInterpolation.C.

110 {
111  std::stringstream filename, filename_pts;
112  const unsigned char fill_character = '0';
113  const unsigned int field_width = 4;
114 
115  filename.fill(fill_character);
116  filename << base_name;
117  filename.width(field_width);
118  filename << _file_number << ".plt";
119 
120  filename_pts.fill(fill_character);
121  filename_pts << base_name << "_pts";
122  filename_pts.width(field_width);
123  filename_pts << _file_number << ".dat";
124 
125  /* First dump the GNUPLOT file with the Piecewise Linear Equations */
126  std::ofstream out(filename.str().c_str());
127  out.precision(15);
128  out.fill(fill_character);
129 
130  out << "set terminal postscript color enhanced\n"
131  << "set output \"" << base_name;
132  out.width(field_width);
133  out << _file_number << ".eps\"\n"
134  << "set xlabel \"" << x_label << "\"\n"
135  << "set ylabel \"" << y_label << "\"\n";
136  if (xmin != 0 && xmax != 0)
137  out << "set xrange [" << xmin << ":" << xmax << "]\n";
138  if (ymin != 0 && ymax != 0)
139  out << "set yrange [" << ymin << ":" << ymax << "]\n";
140  out << "set key left top\n"
141  << "f(x)=";
142 
143  for (unsigned int i = 1; i < _x.size(); ++i)
144  {
145  Real m = (_y[i] - _y[i - 1]) / (_x[i] - _x[i - 1]);
146  Real b = (_y[i] - m * _x[i]);
147 
148  out << _x[i - 1] << "<=x && x<" << _x[i] << " ? " << m << "*x+(" << b << ") : ";
149  }
150  out << " 1/0\n";
151 
152  out << "\nplot f(x) with lines, '" << filename_pts.str() << "' using 1:2 title \"Points\"\n";
153  out.close();
154 
155  libmesh_assert(_x.size() == _y.size());
156 
157  out.open(filename_pts.str().c_str());
158  /* Next dump the data points into a seperate file */
159  for (unsigned int i = 0; i < _x.size(); ++i)
160  out << _x[i] << " " << _y[i] << "\n";
161  out << std::endl;
162 
163  ++_file_number;
164  out.close();
165 }
PetscInt m
std::vector< Real > _x
std::vector< Real > _y
void SplineInterpolation::errorCheck ( )

Definition at line 52 of file SplineInterpolation.C.

Referenced by setData(), and SplineInterpolation().

53 {
54  if (_x.size() != _y.size())
55  mooseError("SplineInterpolation: vectors are not the same length");
56 
57  bool error = false;
58  for (unsigned i = 0; !error && i + 1 < _x.size(); ++i)
59  if (_x[i] >= _x[i + 1])
60  error = true;
61 
62  if (error)
63  mooseError("x-values are not strictly increasing");
64 }
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
std::vector< Real > _x
std::vector< Real > _y
void SplineInterpolationBase::findInterval ( const std::vector< Real > &  x,
Real  x_int,
unsigned int &  klo,
unsigned int &  khi 
) const
protectedinherited

Definition at line 70 of file SplineInterpolationBase.C.

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

74 {
75  klo = 0;
76  mooseAssert(x.size() >= 2, "You must have at least two knots to create a spline.");
77  khi = x.size() - 1;
78  while (khi - klo > 1)
79  {
80  unsigned int k = (khi + klo) >> 1;
81  if (x[k] > x_int)
82  khi = k;
83  else
84  klo = k;
85  }
86 }
static PetscErrorCode Vec x
unsigned int SplineInterpolation::getSampleSize ( )

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

the number of sample points

Definition at line 168 of file SplineInterpolation.C.

169 {
170  return _x.size();
171 }
std::vector< Real > _x
Real SplineInterpolation::range ( int  i) const

Definition at line 97 of file SplineInterpolation.C.

98 {
99  return _y[i];
100 }
std::vector< Real > _y
Real SplineInterpolationBase::sample ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
Real  x_int 
) const
inherited

Definition at line 105 of file SplineInterpolationBase.C.

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

109 {
110  unsigned int klo, khi;
111  findInterval(x, x_int, klo, khi);
112 
113  Real h, a, b;
114  computeCoeffs(x, klo, khi, x_int, h, a, b);
115 
116  return a * y[klo] + b * y[khi] +
117  ((a * a * a - a) * y2[klo] + (b * b * b - b) * y2[khi]) * (h * h) / 6.0;
118 }
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
static PetscErrorCode Vec x
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
Real SplineInterpolation::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 73 of file SplineInterpolation.C.

Referenced by SplineFunction::value().

74 {
76 }
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
static PetscErrorCode Vec x
std::vector< Real > _y2
Second derivatives.
std::vector< Real > _x
std::vector< Real > _y
Real SplineInterpolationBase::sample2ndDerivative ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
Real  x_int 
) const
inherited

Definition at line 137 of file SplineInterpolationBase.C.

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

141 {
142  unsigned int klo, khi;
143  findInterval(x, x_int, klo, khi);
144 
145  Real h, a, b;
146  computeCoeffs(x, klo, khi, x_int, h, a, b);
147 
148  return a * y2[klo] + b * y2[khi];
149 }
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
static PetscErrorCode Vec x
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
Real SplineInterpolation::sample2ndDerivative ( Real  x) const

Definition at line 85 of file SplineInterpolation.C.

Referenced by SplineFunction::secondDerivative().

86 {
88 }
Real sample2ndDerivative(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
static PetscErrorCode Vec x
std::vector< Real > _y2
Second derivatives.
std::vector< Real > _x
std::vector< Real > _y
Real SplineInterpolationBase::sampleDerivative ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
const std::vector< Real > &  y2,
Real  x_int 
) const
inherited

Definition at line 121 of file SplineInterpolationBase.C.

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

125 {
126  unsigned int klo, khi;
127  findInterval(x, x_int, klo, khi);
128 
129  Real h, a, b;
130  computeCoeffs(x, klo, khi, x_int, h, a, b);
131 
132  return (y[khi] - y[klo]) / h -
133  (((3.0 * a * a - 1.0) * y2[klo] + (3.0 * b * b - 1.0) * -y2[khi]) * h / 6.0);
134 }
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
static PetscErrorCode Vec x
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
Real SplineInterpolation::sampleDerivative ( Real  x) const

Definition at line 79 of file SplineInterpolation.C.

Referenced by SplineFunction::derivative().

80 {
82 }
static PetscErrorCode Vec x
std::vector< Real > _y2
Second derivatives.
std::vector< Real > _x
std::vector< Real > _y
Real sampleDerivative(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
void SplineInterpolation::setData ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
Real  yp1 = _deriv_bound,
Real  ypn = _deriv_bound 
)

Set the x-, y- values and first derivatives.

Definition at line 38 of file SplineInterpolation.C.

42 {
43  _x = x;
44  _y = y;
45  _yp1 = yp1;
46  _ypn = ypn;
47  errorCheck();
48  solve();
49 }
Real _yp1
boundary conditions
static PetscErrorCode Vec x
std::vector< Real > _x
std::vector< Real > _y
void SplineInterpolation::solve ( )
protected

Definition at line 67 of file SplineInterpolation.C.

Referenced by setData(), and SplineInterpolation().

68 {
69  spline(_x, _y, _y2, _yp1, _ypn);
70 }
Real _yp1
boundary conditions
std::vector< Real > _y2
Second derivatives.
std::vector< Real > _x
std::vector< Real > _y
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.
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 
)
protectedinherited

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

Definition at line 24 of file SplineInterpolationBase.C.

Referenced by BicubicSplineInterpolation::constructColumnSplineSecondDerivativeTable(), BicubicSplineInterpolation::constructRowSplineSecondDerivativeTable(), BicubicSplineInterpolation::sample(), BicubicSplineInterpolation::sample2ndDerivative(), BicubicSplineInterpolation::sampleDerivative(), and solve().

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

Member Data Documentation

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

Definition at line 64 of file SplineInterpolationBase.h.

Referenced by BicubicSplineInterpolation::errorCheck().

int SplineInterpolation::_file_number = 0
staticprotected

Definition at line 97 of file SplineInterpolation.h.

Referenced by dumpSampleFile().

std::vector<Real> SplineInterpolation::_x
protected
std::vector<Real> SplineInterpolation::_y
protected
std::vector<Real> SplineInterpolation::_y2
protected

Second derivatives.

Definition at line 93 of file SplineInterpolation.h.

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

Real SplineInterpolation::_yp1
protected

boundary conditions

Definition at line 91 of file SplineInterpolation.h.

Referenced by setData(), and solve().

Real SplineInterpolation::_ypn
protected

Definition at line 91 of file SplineInterpolation.h.

Referenced by setData(), and solve().


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