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

This class interpolates tabulated functions with a bi-cubic spline. More...

#include <BicubicSplineInterpolation.h>

Inheritance diagram for BicubicSplineInterpolation:
[legend]

Public Member Functions

 BicubicSplineInterpolation ()
 
 BicubicSplineInterpolation (const std::vector< Real > &x1, const std::vector< Real > &x2, const std::vector< std::vector< Real >> &y, const std::vector< Real > &yx11=std::vector< Real >(), const std::vector< Real > &yx1n=std::vector< Real >(), const std::vector< Real > &yx21=std::vector< Real >(), const std::vector< Real > &yx2n=std::vector< Real >())
 In the future, may add interface that allows necessary vector of boundary conditions to be supplied for each edge of grid; however, for now we just use natural splines at the grid boundaries. More...
 
virtual ~BicubicSplineInterpolation ()=default
 
void setData (const std::vector< Real > &x1, const std::vector< Real > &x2, const std::vector< std::vector< Real >> &y, const std::vector< Real > &yx11=std::vector< Real >(), const std::vector< Real > &yx1n=std::vector< Real >(), const std::vector< Real > &yx21=std::vector< Real >(), const std::vector< Real > &yx2n=std::vector< Real >())
 Set the x1, x2 and y values, and first derivatives at the edges. More...
 
void errorCheck ()
 Sanity checks on input data. More...
 
Real sample (Real x1, Real x2, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
 Samples value at point (x1, x2) More...
 
void sampleValueAndDerivatives (Real x1, Real x2, Real &y, Real &dy1, Real &dy2, Real yx11=_deriv_bound, Real yx1n=_deriv_bound, Real yx21=_deriv_bound, Real yx2n=_deriv_bound)
 Samples value and first derivatives at point (x1, x2) Use this function for speed when computing both value and derivatives, as it minimizes the amount of spline evaluation. More...
 
Real sampleDerivative (Real x1, Real x2, unsigned int deriv_var, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
 Samples first derivative at point (x1, x2) More...
 
Real sample2ndDerivative (Real x1, Real x2, unsigned int deriv_var, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
 Samples second derivative at point (x1, x2) More...
 
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 constructRowSplineSecondDerivativeTable ()
 Precompute tables of row (column) spline second derivatives and store them to reduce computational demands. More...
 
void constructColumnSplineSecondDerivativeTable ()
 
void solve ()
 Calculates the tables of second derivatives. More...
 
void constructRowSpline (Real x1, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
 Helper functions to evaluate column splines and construct row spline for the given point. More...
 
void constructColumnSpline (Real x2, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx21=_deriv_bound, Real yx2n=_deriv_bound)
 Helper functions to evaluate row splines and construct column spline for the given point. More...
 
Real sample (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real 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...
 
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 > _x1
 Independent values in the x1 direction. More...
 
std::vector< Real > _x2
 Independent values in the x2 direction. More...
 
std::vector< std::vector< Real > > _y
 The dependent values at (x1, x2) points. More...
 
std::vector< std::vector< Real > > _y_trans
 Transpose of _y. More...
 
std::vector< Real > _yx11
 Boundary conditions. More...
 
std::vector< Real > _yx1n
 
std::vector< Real > _yx21
 
std::vector< Real > _yx2n
 
std::vector< std::vector< Real > > _y2_rows
 Second derivatives. More...
 
std::vector< std::vector< Real > > _y2_columns
 
std::vector< Real > _row_spline_second_derivs
 Vectors used during sampling. More...
 
std::vector< Real > _row_spline_eval
 
std::vector< Real > _column_spline_second_derivs
 
std::vector< Real > _column_spline_eval
 

Static Protected Attributes

static int _file_number = 0
 File number for data dump. More...
 
static const Real _deriv_bound = std::numeric_limits<Real>::max()
 

Detailed Description

This class interpolates tabulated functions with a bi-cubic spline.

Adopted from Numerical Recipes in C (section 3.6). Consistent with the terminology in Numerical Recipes, moving over a column spline means moving over the x1 coord Likewise, moving over a row spline means moving over the x2 coord

Definition at line 23 of file BicubicSplineInterpolation.h.

Constructor & Destructor Documentation

◆ BicubicSplineInterpolation() [1/2]

BicubicSplineInterpolation::BicubicSplineInterpolation ( )

Definition at line 15 of file BicubicSplineInterpolation.C.

15 {}

◆ BicubicSplineInterpolation() [2/2]

BicubicSplineInterpolation::BicubicSplineInterpolation ( const std::vector< Real > &  x1,
const std::vector< Real > &  x2,
const std::vector< std::vector< Real >> &  y,
const std::vector< Real > &  yx11 = std::vector<Real>(),
const std::vector< Real > &  yx1n = std::vector<Real>(),
const std::vector< Real > &  yx21 = std::vector<Real>(),
const std::vector< Real > &  yx2n = std::vector<Real>() 
)

In the future, may add interface that allows necessary vector of boundary conditions to be supplied for each edge of grid; however, for now we just use natural splines at the grid boundaries.

Definition at line 17 of file BicubicSplineInterpolation.C.

25  _x1(x1),
26  _x2(x2),
27  _y(y),
28  _yx11(yx11),
29  _yx1n(yx1n),
30  _yx21(yx21),
31  _yx2n(yx2n)
32 {
33  auto n = _x2.size();
35  _column_spline_eval.resize(n);
36 
37  auto m = _x1.size();
39  _row_spline_eval.resize(m);
40 
41  errorCheck();
42  solve();
43 }
std::vector< Real > _x1
Independent values in the x1 direction.
void errorCheck()
Sanity checks on input data.
std::vector< std::vector< Real > > _y
The dependent values at (x1, x2) points.
std::vector< Real > _x2
Independent values in the x2 direction.
std::vector< Real > _yx11
Boundary conditions.
PetscInt m
void solve()
Calculates the tables of second derivatives.
std::vector< Real > _row_spline_second_derivs
Vectors used during sampling.
PetscInt n
std::vector< Real > _column_spline_second_derivs

◆ ~BicubicSplineInterpolation()

virtual BicubicSplineInterpolation::~BicubicSplineInterpolation ( )
virtualdefault

Member Function Documentation

◆ computeCoeffs()

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 84 of file SplineInterpolationBase.C.

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

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

◆ constructColumnSpline()

void BicubicSplineInterpolation::constructColumnSpline ( Real  x2,
std::vector< Real > &  spline_eval,
std::vector< Real > &  spline_second_derivs,
Real  yx21 = _deriv_bound,
Real  yx2n = _deriv_bound 
)
protected

Helper functions to evaluate row splines and construct column spline for the given point.

Definition at line 278 of file BicubicSplineInterpolation.C.

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

283 {
284  auto m = _x1.size();
285 
286  // Find the indices that bound the point x2
287  unsigned int klo, khi;
288  findInterval(_x2, x2, klo, khi);
289 
290  // Evaluate m row-splines to get y-values for column spline construction using
291  // the indices above to avoid computing them for each j
292  for (decltype(m) j = 0; j < m; ++j)
293  _row_spline_eval[j] = SplineInterpolationBase::sample(_x2, _y[j], _y2_rows[j], x2, klo, khi);
294 
295  // Construct single column spline; get back the second derivatives wrt x1 coord
296  // on the x1 grid points
297  spline(_x1, row_spline_eval, column_spline_second_derivs, yx21, yx2n);
298 }
std::vector< Real > _x1
Independent values in the x1 direction.
std::vector< std::vector< Real > > _y
The dependent values at (x1, x2) points.
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
std::vector< Real > _x2
Independent values in the x2 direction.
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
std::vector< std::vector< Real > > _y2_rows
Second derivatives.
PetscInt m
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.

◆ constructColumnSplineSecondDerivativeTable()

void BicubicSplineInterpolation::constructColumnSplineSecondDerivativeTable ( )
protected

Definition at line 127 of file BicubicSplineInterpolation.C.

Referenced by solve().

128 {
129  auto m = _x1.size();
130  auto n = _x2.size();
131  _y2_columns.resize(n);
132  _y_trans.resize(n);
133 
134  for (decltype(n) j = 0; j < n; ++j)
135  _y_trans[j].resize(m);
136 
137  // transpose the _y values so the columns can be easily iterated over
138  for (decltype(n) i = 0; i < _y.size(); ++i)
139  for (decltype(n) j = 0; j < _y[0].size(); ++j)
140  _y_trans[j][i] = _y[i][j];
141 
142  if (_yx11.empty())
143  for (decltype(n) j = 0; j < n; ++j)
144  spline(_x1, _y_trans[j], _y2_columns[j]);
145 
146  else
147  for (decltype(n) j = 0; j < n; ++j)
148  spline(_x1, _y_trans[j], _y2_columns[j], _yx11[j], _yx1n[j]);
149 }
std::vector< Real > _x1
Independent values in the x1 direction.
std::vector< std::vector< Real > > _y_trans
Transpose of _y.
std::vector< std::vector< Real > > _y
The dependent values at (x1, x2) points.
std::vector< Real > _x2
Independent values in the x2 direction.
std::vector< Real > _yx11
Boundary conditions.
PetscInt m
PetscInt n
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.
std::vector< std::vector< Real > > _y2_columns

◆ constructRowSpline()

void BicubicSplineInterpolation::constructRowSpline ( Real  x1,
std::vector< Real > &  spline_eval,
std::vector< Real > &  spline_second_derivs,
Real  yx11 = _deriv_bound,
Real  yx1n = _deriv_bound 
)
protected

Helper functions to evaluate column splines and construct row spline for the given point.

Definition at line 254 of file BicubicSplineInterpolation.C.

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

259 {
260  auto n = _x2.size();
261 
262  // Find the indices that bound the point x1
263  unsigned int klo, khi;
264  findInterval(_x1, x1, klo, khi);
265 
266  // Evaluate n column-splines to get y-values for row spline construction using
267  // the indices above to avoid computing them for each j
268  for (decltype(n) j = 0; j < n; ++j)
271 
272  // Construct single row spline; get back the second derivatives wrt x2 coord
273  // on the x2 grid points
274  spline(_x2, column_spline_eval, row_spline_second_derivs, yx11, yx1n);
275 }
std::vector< Real > _x1
Independent values in the x1 direction.
std::vector< std::vector< Real > > _y_trans
Transpose of _y.
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
std::vector< Real > _x2
Independent values in the x2 direction.
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const
PetscInt n
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.
std::vector< std::vector< Real > > _y2_columns

◆ constructRowSplineSecondDerivativeTable()

void BicubicSplineInterpolation::constructRowSplineSecondDerivativeTable ( )
protected

Precompute tables of row (column) spline second derivatives and store them to reduce computational demands.

Definition at line 112 of file BicubicSplineInterpolation.C.

Referenced by solve().

113 {
114  auto m = _x1.size();
115  _y2_rows.resize(m);
116 
117  if (_yx21.empty())
118  for (decltype(m) j = 0; j < m; ++j)
119  spline(_x2, _y[j], _y2_rows[j]);
120 
121  else
122  for (decltype(m) j = 0; j < m; ++j)
123  spline(_x2, _y[j], _y2_rows[j], _yx21[j], _yx2n[j]);
124 }
std::vector< Real > _x1
Independent values in the x1 direction.
std::vector< std::vector< Real > > _y
The dependent values at (x1, x2) points.
std::vector< Real > _x2
Independent values in the x2 direction.
std::vector< std::vector< Real > > _y2_rows
Second derivatives.
PetscInt m
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.

◆ errorCheck()

void BicubicSplineInterpolation::errorCheck ( )

Sanity checks on input data.

Definition at line 75 of file BicubicSplineInterpolation.C.

Referenced by BicubicSplineInterpolation(), and setData().

76 {
77  auto m = _x1.size(), n = _x2.size();
78 
79  if (_y.size() != m)
80  mooseError("y row dimension does not match the size of x1.");
81  else
82  for (decltype(m) i = 0; i < _y.size(); ++i)
83  if (_y[i].size() != n)
84  mooseError("y column dimension does not match the size of x2.");
85 
86  if (_yx11.empty())
87  _yx11.resize(n, _deriv_bound);
88  else if (_yx11.size() != n)
89  mooseError("The length of the vectors holding the first derivatives of y with respect to x1 "
90  "must match the length of x2.");
91 
92  if (_yx1n.empty())
93  _yx1n.resize(n, _deriv_bound);
94  else if (_yx1n.size() != n)
95  mooseError("The length of the vectors holding the first derivatives of y with respect to x1 "
96  "must match the length of x2.");
97 
98  if (_yx21.empty())
99  _yx21.resize(m, _deriv_bound);
100  else if (_yx21.size() != m)
101  mooseError("The length of the vectors holding the first derivatives of y with respect to x2 "
102  "must match the length of x1.");
103 
104  if (_yx2n.empty())
105  _yx2n.resize(m, _deriv_bound);
106  else if (_yx2n.size() != m)
107  mooseError("The length of the vectors holding the first derivatives of y with respect to x2 "
108  "must match the length of x1.");
109 }
std::vector< Real > _x1
Independent values in the x1 direction.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::vector< std::vector< Real > > _y
The dependent values at (x1, x2) points.
std::vector< Real > _x2
Independent values in the x2 direction.
std::vector< Real > _yx11
Boundary conditions.
PetscInt m
PetscInt n

◆ findInterval()

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

Definition at line 65 of file SplineInterpolationBase.C.

Referenced by constructColumnSpline(), constructRowSpline(), SplineInterpolationBase::sample(), SplineInterpolationBase::sample2ndDerivative(), and SplineInterpolationBase::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 }
static PetscErrorCode Vec x

◆ 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
inherited

Definition at line 100 of file SplineInterpolationBase.C.

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

104 {
105  unsigned int klo, khi;
106  findInterval(x, x_int, klo, khi);
107 
108  return sample(x, y, y2, x_int, klo, khi);
109 }
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
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const

◆ sample() [2/3]

Real BicubicSplineInterpolation::sample ( Real  x1,
Real  x2,
Real  yx11 = _deriv_bound,
Real  yx1n = _deriv_bound 
)

Samples value at point (x1, x2)

Definition at line 159 of file BicubicSplineInterpolation.C.

Referenced by BicubicSplineFunction::value().

163 {
165 
166  // Evaluate newly constructed column spline
168 }
std::vector< Real > _x1
Independent values in the x1 direction.
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
void constructColumnSpline(Real x2, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx21=_deriv_bound, Real yx2n=_deriv_bound)
Helper functions to evaluate row splines and construct column spline for the given point...
std::vector< Real > _column_spline_second_derivs

◆ sample() [3/3]

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

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 143 of file SplineInterpolationBase.C.

149 {
150  Real h, a, b;
151  computeCoeffs(x, klo, khi, x_int, h, a, b);
152 
153  return a * y[klo] + b * y[khi] +
154  ((a * a * a - a) * y2[klo] + (b * b * b - b) * y2[khi]) * (h * h) / 6.0;
155 }
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
static PetscErrorCode Vec x

◆ sample2ndDerivative() [1/2]

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 128 of file SplineInterpolationBase.C.

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

132 {
133  unsigned int klo, khi;
134  findInterval(x, x_int, klo, khi);
135 
136  Real h, a, b;
137  computeCoeffs(x, klo, khi, x_int, h, a, b);
138 
139  return a * y2[klo] + b * y2[khi];
140 }
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
static PetscErrorCode Vec x
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const

◆ sample2ndDerivative() [2/2]

Real BicubicSplineInterpolation::sample2ndDerivative ( Real  x1,
Real  x2,
unsigned int  deriv_var,
Real  yp1 = _deriv_bound,
Real  ypn = _deriv_bound 
)

Samples second derivative at point (x1, x2)

Definition at line 202 of file BicubicSplineInterpolation.C.

Referenced by BicubicSplineFunction::secondDerivative().

207 {
208  // Take second derivative along x1 axis
209  if (deriv_var == 1)
210  {
212 
213  // Evaluate second derivative wrt x1 of newly constructed column spline
216  }
217 
218  // Take second derivative along x2 axis
219  else if (deriv_var == 2)
220  {
222 
223  // Evaluate second derivative wrt x2 of newly constructed row spline
226  }
227 
228  else
229  mooseError("deriv_var must be either 1 or 2 in BicubicSplineInterpolation");
230 }
std::vector< Real > _x1
Independent values in the x1 direction.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
Real sample2ndDerivative(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
std::vector< Real > _x2
Independent values in the x2 direction.
void constructColumnSpline(Real x2, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx21=_deriv_bound, Real yx2n=_deriv_bound)
Helper functions to evaluate row splines and construct column spline for the given point...
std::vector< Real > _row_spline_second_derivs
Vectors used during sampling.
void constructRowSpline(Real x1, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
Helper functions to evaluate column splines and construct row spline for the given point...
std::vector< Real > _column_spline_second_derivs

◆ sampleDerivative() [1/2]

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 112 of file SplineInterpolationBase.C.

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

116 {
117  unsigned int klo, khi;
118  findInterval(x, x_int, klo, khi);
119 
120  Real h, a, b;
121  computeCoeffs(x, klo, khi, x_int, h, a, b);
122 
123  return (y[khi] - y[klo]) / h -
124  (((3.0 * a * a - 1.0) * y2[klo] + (3.0 * b * b - 1.0) * -y2[khi]) * h / 6.0);
125 }
void computeCoeffs(const std::vector< Real > &x, unsigned int klo, unsigned int khi, Real x_int, Real &h, Real &a, Real &b) const
static PetscErrorCode Vec x
void findInterval(const std::vector< Real > &x, Real x_int, unsigned int &klo, unsigned int &khi) const

◆ sampleDerivative() [2/2]

Real BicubicSplineInterpolation::sampleDerivative ( Real  x1,
Real  x2,
unsigned int  deriv_var,
Real  yp1 = _deriv_bound,
Real  ypn = _deriv_bound 
)

Samples first derivative at point (x1, x2)

Definition at line 171 of file BicubicSplineInterpolation.C.

Referenced by BicubicSplineFunction::derivative().

176 {
177  // Take derivative along x1 axis
178  if (deriv_var == 1)
179  {
181 
182  // Evaluate derivative wrt x1 of newly constructed column spline
185  }
186 
187  // Take derivative along x2 axis
188  else if (deriv_var == 2)
189  {
191 
192  // Evaluate derivative wrt x2 of newly constructed row spline
195  }
196 
197  else
198  mooseError("deriv_var must be either 1 or 2 in BicubicSplineInterpolation");
199 }
std::vector< Real > _x1
Independent values in the x1 direction.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
Real sampleDerivative(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
std::vector< Real > _x2
Independent values in the x2 direction.
void constructColumnSpline(Real x2, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx21=_deriv_bound, Real yx2n=_deriv_bound)
Helper functions to evaluate row splines and construct column spline for the given point...
std::vector< Real > _row_spline_second_derivs
Vectors used during sampling.
void constructRowSpline(Real x1, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
Helper functions to evaluate column splines and construct row spline for the given point...
std::vector< Real > _column_spline_second_derivs

◆ sampleValueAndDerivatives()

void BicubicSplineInterpolation::sampleValueAndDerivatives ( Real  x1,
Real  x2,
Real &  y,
Real &  dy1,
Real &  dy2,
Real  yx11 = _deriv_bound,
Real  yx1n = _deriv_bound,
Real  yx21 = _deriv_bound,
Real  yx2n = _deriv_bound 
)

Samples value and first derivatives at point (x1, x2) Use this function for speed when computing both value and derivatives, as it minimizes the amount of spline evaluation.

Definition at line 233 of file BicubicSplineInterpolation.C.

242 {
247 
251 }
std::vector< Real > _x1
Independent values in the x1 direction.
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
std::vector< Real > _x2
Independent values in the x2 direction.
void constructColumnSpline(Real x2, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx21=_deriv_bound, Real yx2n=_deriv_bound)
Helper functions to evaluate row splines and construct column spline for the given point...
std::vector< Real > _row_spline_second_derivs
Vectors used during sampling.
void constructRowSpline(Real x1, std::vector< Real > &spline_eval, std::vector< Real > &spline_second_derivs, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
Helper functions to evaluate column splines and construct row spline for the given point...
std::vector< Real > _column_spline_second_derivs

◆ setData()

void BicubicSplineInterpolation::setData ( const std::vector< Real > &  x1,
const std::vector< Real > &  x2,
const std::vector< std::vector< Real >> &  y,
const std::vector< Real > &  yx11 = std::vector<Real>(),
const std::vector< Real > &  yx1n = std::vector<Real>(),
const std::vector< Real > &  yx21 = std::vector<Real>(),
const std::vector< Real > &  yx2n = std::vector<Real>() 
)

Set the x1, x2 and y values, and first derivatives at the edges.

Definition at line 46 of file BicubicSplineInterpolation.C.

Referenced by BicubicSplineFunction::BicubicSplineFunction().

53 {
54  _x1 = x1;
55  _x2 = x2;
56  _y = y;
57  _yx11 = yx11;
58  _yx1n = yx1n;
59  _yx21 = yx21;
60  _yx2n = yx2n;
61 
62  auto n = _x2.size();
64  _column_spline_eval.resize(n);
65 
66  auto m = _x1.size();
68  _row_spline_eval.resize(m);
69 
70  errorCheck();
71  solve();
72 }
std::vector< Real > _x1
Independent values in the x1 direction.
void errorCheck()
Sanity checks on input data.
std::vector< std::vector< Real > > _y
The dependent values at (x1, x2) points.
std::vector< Real > _x2
Independent values in the x2 direction.
std::vector< Real > _yx11
Boundary conditions.
PetscInt m
void solve()
Calculates the tables of second derivatives.
std::vector< Real > _row_spline_second_derivs
Vectors used during sampling.
PetscInt n
std::vector< Real > _column_spline_second_derivs

◆ solve()

void BicubicSplineInterpolation::solve ( )
protected

Calculates the tables of second derivatives.

Definition at line 152 of file BicubicSplineInterpolation.C.

Referenced by BicubicSplineInterpolation(), and setData().

153 {
156 }
void constructRowSplineSecondDerivativeTable()
Precompute tables of row (column) spline second derivatives and store them to reduce computational de...

◆ 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 
)
protectedinherited

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

Definition at line 19 of file SplineInterpolationBase.C.

Referenced by constructColumnSpline(), constructColumnSplineSecondDerivativeTable(), constructRowSpline(), 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:208
static PetscErrorCode Vec x
PetscInt n

Member Data Documentation

◆ _column_spline_eval

std::vector<Real> BicubicSplineInterpolation::_column_spline_eval
protected

◆ _column_spline_second_derivs

std::vector<Real> BicubicSplineInterpolation::_column_spline_second_derivs
protected

◆ _deriv_bound

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

Definition at line 75 of file SplineInterpolationBase.h.

Referenced by errorCheck().

◆ _file_number

int BicubicSplineInterpolation::_file_number = 0
staticprotected

File number for data dump.

Definition at line 154 of file BicubicSplineInterpolation.h.

◆ _row_spline_eval

std::vector<Real> BicubicSplineInterpolation::_row_spline_eval
protected

◆ _row_spline_second_derivs

std::vector<Real> BicubicSplineInterpolation::_row_spline_second_derivs
protected

◆ _x1

std::vector<Real> BicubicSplineInterpolation::_x1
protected

◆ _x2

std::vector<Real> BicubicSplineInterpolation::_x2
protected

◆ _y

std::vector<std::vector<Real> > BicubicSplineInterpolation::_y
protected

◆ _y2_columns

std::vector<std::vector<Real> > BicubicSplineInterpolation::_y2_columns
protected

◆ _y2_rows

std::vector<std::vector<Real> > BicubicSplineInterpolation::_y2_rows
protected

Second derivatives.

Definition at line 112 of file BicubicSplineInterpolation.h.

Referenced by constructColumnSpline(), and constructRowSplineSecondDerivativeTable().

◆ _y_trans

std::vector<std::vector<Real> > BicubicSplineInterpolation::_y_trans
protected

Transpose of _y.

Definition at line 98 of file BicubicSplineInterpolation.h.

Referenced by constructColumnSplineSecondDerivativeTable(), and constructRowSpline().

◆ _yx11

std::vector<Real> BicubicSplineInterpolation::_yx11
protected

Boundary conditions.

The first index indicates the coordinate the derivative is with respect to. The second index indicates the grid index, e.g. 1 is the lower bound, and n is the upper bound

Definition at line 106 of file BicubicSplineInterpolation.h.

Referenced by constructColumnSplineSecondDerivativeTable(), errorCheck(), and setData().

◆ _yx1n

std::vector<Real> BicubicSplineInterpolation::_yx1n
protected

◆ _yx21

std::vector<Real> BicubicSplineInterpolation::_yx21
protected

◆ _yx2n

std::vector<Real> BicubicSplineInterpolation::_yx2n
protected

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