www.mooseframework.org
TrilinearInterpolation.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "TrilinearInterpolation.h"
11 #include "MooseError.h"
12 
14  const std::vector<Real> & y,
15  const std::vector<Real> & z,
16  const std::vector<Real> & data)
17  : _x_axis(x), _y_axis(y), _z_axis(z), _fxyz(data)
18 {
19  if (_x_axis.size() < 1)
20  mooseError("x vector has zero elements. At least one element is required.");
21  if (_y_axis.size() < 1)
22  mooseError("y vector has zero elements. At least one element is required.");
23  if (_z_axis.size() < 1)
24  mooseError("z vector has zero elements. At least one element is required.");
25  if (_x_axis.size() * _y_axis.size() * _z_axis.size() != data.size())
26  mooseError("The size of data (",
27  data.size(),
28  ") does not match the supplied dimensions (",
29  _x_axis.size(),
30  ", ",
31  _y_axis.size(),
32  ", ",
33  _z_axis.size(),
34  ")");
35 }
36 
37 void
39  const std::vector<Real> & v, Real x, int & lower, int & upper, Real & d) const
40 {
41  unsigned int N = v.size();
42  if (x < v[0])
43  {
44  lower = 0;
45  upper = 0;
46  }
47  else if (x >= v[N - 1])
48  {
49  lower = N - 1;
50  upper = N - 1;
51  }
52  else
53  {
54  for (unsigned int i = 0; i < N - 1; i++)
55  {
56  if (x > v[i] && x < v[i + 1])
57  {
58  lower = i;
59  upper = i + 1;
60  d = (x - v[lower]) / (v[upper] - v[lower]);
61  break;
62  }
63  else if (x == v[i])
64  {
65  lower = i;
66  upper = i;
67  break;
68  }
69  }
70  }
71 }
72 
73 Real
75 {
76  int nY = _y_axis.size();
77  int nZ = _z_axis.size();
78 
79  return _fxyz[x * nY * nZ + y * nZ + z];
80 }
81 
82 Real
83 TrilinearInterpolation::sample(Real x, Real y, Real z) const
84 {
85  int x0 = 0;
86  int y0 = 0;
87  int z0 = 0;
88  int x1 = 0;
89  int y1 = 0;
90  int z1 = 0;
91  Real Dx = 0;
92  Real Dy = 0;
93  Real Dz = 0;
94 
95  // find the the indices of the cube, which contains the point
96  getCornerIndices(_x_axis, x, x0, x1, Dx);
97  getCornerIndices(_y_axis, y, y0, y1, Dy);
98  getCornerIndices(_z_axis, z, z0, z1, Dz);
99 
100  // find the corresponding function values for the corner indices
101  Real f000 = getCornerValues(x0, y0, z0);
102  Real f001 = getCornerValues(x0, y0, z1);
103  Real f010 = getCornerValues(x0, y1, z0);
104  Real f011 = getCornerValues(x0, y1, z1);
105  Real f100 = getCornerValues(x1, y0, z0);
106  Real f101 = getCornerValues(x1, y0, z1);
107  Real f110 = getCornerValues(x1, y1, z0);
108  Real f111 = getCornerValues(x1, y1, z1);
109 
110  // interpolation
111  Real f00 = (f100 - f000) * Dx + f000;
112  Real f10 = (f110 - f010) * Dx + f010;
113  Real f01 = (f101 - f001) * Dx + f001;
114  Real f11 = (f111 - f011) * Dx + f011;
115  Real f0 = (f10 - f00) * Dy + f00;
116  Real f1 = (f11 - f01) * Dy + f01;
117 
118  return (f1 - f0) * Dz + f0;
119 }
TrilinearInterpolation.h
TrilinearInterpolation::getCornerIndices
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.
Definition: TrilinearInterpolation.C:38
TrilinearInterpolation::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.
Definition: TrilinearInterpolation.C:13
mooseError
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition: MooseError.h:210
TrilinearInterpolation::_z_axis
std::vector< Real > _z_axis
vector of z-values
Definition: TrilinearInterpolation.h:91
x
static PetscErrorCode Vec x
Definition: PetscDMMoose.C:1263
TrilinearInterpolation::sample
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...
Definition: TrilinearInterpolation.C:83
TrilinearInterpolation::getCornerValues
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...
Definition: TrilinearInterpolation.C:74
TrilinearInterpolation::_y_axis
std::vector< Real > _y_axis
vector of y-values
Definition: TrilinearInterpolation.h:88
TrilinearInterpolation::_fxyz
std::vector< Real > _fxyz
vector of function values, f(x,y,z)
Definition: TrilinearInterpolation.h:94
MooseError.h
N
PetscInt N
Definition: PetscDMMoose.C:1504
TrilinearInterpolation::_x_axis
std::vector< Real > _x_axis
vector of x-values
Definition: TrilinearInterpolation.h:85