www.mooseframework.org
TrilinearInterpolation.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "TrilinearInterpolation.h"
16 #include "MooseError.h"
17 
19  const std::vector<Real> & y,
20  const std::vector<Real> & z,
21  const std::vector<Real> & data)
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 }
41 
42 void
44  const std::vector<Real> & v, Real x, int & lower, int & upper, Real & d) const
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 }
77 
78 Real
80 {
81  int nY = _y_axis.size();
82  int nZ = _z_axis.size();
83 
84  return _fxyz[x * nY * nZ + y * nZ + z];
85 }
86 
87 Real
88 TrilinearInterpolation::sample(Real x, Real y, Real z) const
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 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 ve...
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
PetscInt N
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)
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
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.
std::vector< Real > _z_axis
vector of z-values