www.mooseframework.org
BilinearInterpolation.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 "BilinearInterpolation.h"
16 
18 
20  const std::vector<Real> & y,
21  const ColumnMajorMatrix & z)
22  : _xAxis(x), _yAxis(y), _zSurface(z)
23 {
24 }
25 
26 void
27 BilinearInterpolation::getNeighborIndices(const std::vector<Real> & inArr,
28  Real x,
29  int & lowerX,
30  int & upperX)
31 {
32  int N = inArr.size();
33  if (x <= inArr[0])
34  {
35  lowerX = 0;
36  upperX = 0;
37  }
38  else if (x >= inArr[N - 1])
39  {
40  lowerX = N - 1;
41  upperX = N - 1;
42  }
43  else
44  {
45  for (int i = 1; i < N; ++i)
46  {
47  if (x < inArr[i])
48  {
49  lowerX = i - 1;
50  upperX = i;
51  break;
52  }
53  else if (x == inArr[i])
54  {
55  lowerX = i;
56  upperX = i;
57  break;
58  }
59  }
60  }
61 }
62 
63 Real
64 BilinearInterpolation::sample(Real xcoord, Real ycoord)
65 {
66  // first find 4 neighboring points
67  int lx = 0; // index of x coordinate of adjacent grid point to left of P
68  int ux = 0; // index of x coordinate of adjacent grid point to right of P
69  getNeighborIndices(_xAxis, xcoord, lx, ux);
70 
71  int ly = 0; // index of y coordinate of adjacent grid point below P
72  int uy = 0; // index of y coordinate of adjacent grid point above P
73  getNeighborIndices(_yAxis, ycoord, ly, uy);
74 
75  Real fQ11 = _zSurface(ly, lx);
76  Real fQ21 = _zSurface(ly, ux);
77  Real fQ12 = _zSurface(uy, lx);
78  Real fQ22 = _zSurface(uy, ux);
79 
80  // if point exactly found on a node do not interpolate
81  if ((lx == ux) && (ly == uy))
82  return fQ11;
83 
84  Real x = xcoord;
85  Real y = ycoord;
86  Real x1 = _xAxis[lx];
87  Real x2 = _xAxis[ux];
88  Real y1 = _yAxis[ly];
89  Real y2 = _yAxis[uy];
90 
91  // if xcoord lies exactly on an xAxis node do linear interpolation
92  if (lx == ux)
93  return fQ11 + (fQ12 - fQ11) * (y - y1) / (y2 - y1);
94 
95  // if ycoord lies exactly on an yAxis node do linear interpolation
96 
97  if (ly == uy)
98  return fQ11 + (fQ21 - fQ11) * (x - x1) / (x2 - x1);
99 
100  Real fxy = fQ11 * (x2 - x) * (y2 - y);
101  fxy += fQ21 * (x - x1) * (y2 - y);
102  fxy += fQ12 * (x2 - x) * (y - y1);
103  fxy += fQ22 * (x - x1) * (y - y1);
104  fxy /= ((x2 - x1) * (y2 - y1));
105 
106  return fxy;
107 }
void getNeighborIndices(const std::vector< Real > &inArr, Real x, int &lowerX, int &upperX)
std::vector< Real > _xAxis
PetscInt N
BilinearInterpolation(const std::vector< Real > &XAXIS, const std::vector< Real > &YAXIS, const ColumnMajorMatrix &ZSURFACE)
Constructor, Takes two vectors of points for which to apply the fit.
std::vector< Real > _yAxis
static PetscErrorCode Vec x
This class defines a Tensor that can change its shape.
Real sample(Real xcoord, Real ycoord)
This function will take an independent variable input and will return the dependent variable based on...
ColumnMajorMatrix _zSurface