TrilinearInterpolation Class Reference

This class interpolates a function of three values (f(x,y,z)). More...

#include <TrilinearInterpolation.h>

## Public Member Functions

TrilinearInterpolation (const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &z, const std::vector< Real > &data)
Constructor initializes data for interpolation. More...

virtual ~TrilinearInterpolation ()=default

Real sample (Real x, Real y, Real z) const
Interpolates for the desired (x,y,z) coordinate and returns the value based on the function values vector. More...

## Protected Member Functions

void getCornerIndices (const std::vector< Real > &v, Real x, int &lower, int &upper, Real &d) const
Finds the indices of the cube that point (x,y,z) is in. More...

Real getCornerValues (int x, int y, int z) const
Searches the function value vector for the value at a given corner coordinate from the getCornerIndices function. More...

## Protected Attributes

std::vector< Real > _x_axis
vector of x-values More...

std::vector< Real > _y_axis
vector of y-values More...

std::vector< Real > _z_axis
vector of z-values More...

std::vector< Real > _fxyz
vector of function values, f(x,y,z) More...

## Detailed Description

This class interpolates a function of three values (f(x,y,z)).

It takes 4 vectors for x, y, z, and function values, f(x,y,z). The vector of function values should be done in the following manner: Function values of constant x and y are written, corresponding with values in vector, z. Function values for the constant x, next y-value, corresponding with values in vector, z. After last y-value, function values for the next x, first y-value, corresponding with values in vector, z.

An example: f(1,4,7) = 10, f(1,4,8) = 11, f(1,4,9) = 12 f(1,5,7) = 13, f(1,5,8) = 14, f(1,5,9) = 15 f(1,6,7) = 16, f(1,6,8) = 17, f(1,6,9) = 18 f(2,4,7) = 20, f(2,4,8) = 21, f(2,4,9) = 22 f(2,5,7) = 23, f(2,5,8) = 24, f(2,5,9) = 25 f(2,6,7) = 26, f(2,6,8) = 27, f(2,6,9) = 28 f(3,4,7) = 30, f(3,4,8) = 31, f(3,4,9) = 32 f(3,5,7) = 33, f(3,5,8) = 34, f(3,5,9) = 35 f(3,6,7) = 36, f(3,6,8) = 37, f(3,6,9) = 38

x = {1, 2, 3}; y = {4, 5, 6}; z = {7, 8, 9}; fxyz = { // fxyz for x = 1 10, 11, 12, 13, 14, 15, 16, 17, 18, // fxyz for x = 2 20, 21, 22, 23, 24, 25, 26, 27, 28, // fxyz for x = 3 30, 31, 32, 33, 34, 35, 36, 37, 38 };

Definition at line 62 of file TrilinearInterpolation.h.

## Constructor & Destructor Documentation

 TrilinearInterpolation::TrilinearInterpolation ( const std::vector< Real > & x, const std::vector< Real > & y, const std::vector< Real > & z, const std::vector< Real > & data )

Constructor initializes data for interpolation.

Parameters
 x vector for x-coordinates y vector for y-coordinates z vector for z-coordinates data vector for function values formatted in the same manner as the example

Definition at line 18 of file TrilinearInterpolation.C.

22  : _x_axis(x), _y_axis(y), _z_axis(z), _fxyz(data)
23 {
24  if (_x_axis.size() < 1)
25  mooseError("x vector has zero elements. At least one element is required.");
26  if (_y_axis.size() < 1)
27  mooseError("y vector has zero elements. At least one element is required.");
28  if (_z_axis.size() < 1)
29  mooseError("z vector has zero elements. At least one element is required.");
30  if (_x_axis.size() * _y_axis.size() * _z_axis.size() != data.size())
31  mooseError("The size of data (",
32  data.size(),
33  ") does not match the supplied dimensions (",
34  _x_axis.size(),
35  ", ",
36  _y_axis.size(),
37  ", ",
38  _z_axis.size(),
39  ")");
40 }
std::vector< Real > _y_axis
vector of y-values
std::vector< Real > _x_axis
vector of x-values
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 > _fxyz
vector of function values, f(x,y,z)
static PetscErrorCode Vec x
std::vector< Real > _z_axis
vector of z-values
 virtual TrilinearInterpolation::~TrilinearInterpolation ( )
virtualdefault

## Member Function Documentation

 void TrilinearInterpolation::getCornerIndices ( const std::vector< Real > & v, Real x, int & lower, int & upper, Real & d ) const
protected

Finds the indices of the cube that point (x,y,z) is in.

Parameters
 [in] v vector to find lower and upper limits for the cube [in] x desired coordinate [out] lower lower limit for cube [out] upper upper limit for cube [out] d ratio of (x - lower) / (upper - lower)

Definition at line 43 of file TrilinearInterpolation.C.

Referenced by sample().

45 {
46  unsigned int N = v.size();
47  if (x < v[0])
48  {
49  lower = 0;
50  upper = 0;
51  }
52  else if (x >= v[N - 1])
53  {
54  lower = N - 1;
55  upper = N - 1;
56  }
57  else
58  {
59  for (unsigned int i = 0; i < N - 1; i++)
60  {
61  if (x > v[i] && x < v[i + 1])
62  {
63  lower = i;
64  upper = i + 1;
65  d = (x - v[lower]) / (v[upper] - v[lower]);
66  break;
67  }
68  else if (x == v[i])
69  {
70  lower = i;
71  upper = i;
72  break;
73  }
74  }
75  }
76 }
PetscInt N
static PetscErrorCode Vec x
 Real TrilinearInterpolation::getCornerValues ( int x, int y, int z ) const
protected

Searches the function value vector for the value at a given corner coordinate from the getCornerIndices function.

Parameters
 x index for x-coordinate of corner y index for y-coordinate of corner z index for z-coordinate of corner
Returns
function value for the (x,y,z) coordinate

Definition at line 79 of file TrilinearInterpolation.C.

Referenced by sample().

80 {
81  int nY = _y_axis.size();
82  int nZ = _z_axis.size();
83
84  return _fxyz[x * nY * nZ + y * nZ + z];
85 }
std::vector< Real > _y_axis
vector of y-values
std::vector< Real > _fxyz
vector of function values, f(x,y,z)
static PetscErrorCode Vec x
std::vector< Real > _z_axis
vector of z-values
 Real TrilinearInterpolation::sample ( Real x, Real y, Real z ) const

Interpolates for the desired (x,y,z) coordinate and returns the value based on the function values vector.

Parameters
 x desired x-coordinate y desired y-coordinate z desired z-coordinate
Returns
interpolated value at coordinate (x,y,z)

Definition at line 88 of file TrilinearInterpolation.C.

89 {
90  int x0 = 0;
91  int y0 = 0;
92  int z0 = 0;
93  int x1 = 0;
94  int y1 = 0;
95  int z1 = 0;
96  Real Dx = 0;
97  Real Dy = 0;
98  Real Dz = 0;
99
100  // find the the indices of the cube, which contains the point
101  getCornerIndices(_x_axis, x, x0, x1, Dx);
102  getCornerIndices(_y_axis, y, y0, y1, Dy);
103  getCornerIndices(_z_axis, z, z0, z1, Dz);
104
105  // find the corresponding function values for the corner indices
106  Real f000 = getCornerValues(x0, y0, z0);
107  Real f001 = getCornerValues(x0, y0, z1);
108  Real f010 = getCornerValues(x0, y1, z0);
109  Real f011 = getCornerValues(x0, y1, z1);
110  Real f100 = getCornerValues(x1, y0, z0);
111  Real f101 = getCornerValues(x1, y0, z1);
112  Real f110 = getCornerValues(x1, y1, z0);
113  Real f111 = getCornerValues(x1, y1, z1);
114
115  // interpolation
116  Real f00 = (f100 - f000) * Dx + f000;
117  Real f10 = (f110 - f010) * Dx + f010;
118  Real f01 = (f101 - f001) * Dx + f001;
119  Real f11 = (f111 - f011) * Dx + f011;
120  Real f0 = (f10 - f00) * Dy + f00;
121  Real f1 = (f11 - f01) * Dy + f01;
122
123  return (f1 - f0) * Dz + f0;
124 }
std::vector< Real > _y_axis
vector of y-values
Real getCornerValues(int x, int y, int z) const
Searches the function value vector for the value at a given corner coordinate from the getCornerIndic...
std::vector< Real > _x_axis
vector of x-values
void getCornerIndices(const std::vector< Real > &v, Real x, int &lower, int &upper, Real &d) const
Finds the indices of the cube that point (x,y,z) is in.
static PetscErrorCode Vec x
std::vector< Real > _z_axis
vector of z-values

## Member Data Documentation

 std::vector TrilinearInterpolation::_fxyz
protected

vector of function values, f(x,y,z)

Definition at line 100 of file TrilinearInterpolation.h.

Referenced by getCornerValues().

 std::vector TrilinearInterpolation::_x_axis
protected

vector of x-values

Definition at line 91 of file TrilinearInterpolation.h.

Referenced by sample(), and TrilinearInterpolation().

 std::vector TrilinearInterpolation::_y_axis
protected

vector of y-values

Definition at line 94 of file TrilinearInterpolation.h.

Referenced by getCornerValues(), sample(), and TrilinearInterpolation().

 std::vector TrilinearInterpolation::_z_axis
protected

vector of z-values

Definition at line 97 of file TrilinearInterpolation.h.

Referenced by getCornerValues(), sample(), and TrilinearInterpolation().

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