OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
TriLinearInterpolator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Chris Rogers
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 1. Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  * 3. Neither the name of STFC nor the names of its contributors may be used to
12  * endorse or promote products derived from this software without specific
13  * prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
29 
30 namespace interpolation {
31 
34 {
36  f_m = new double**[coordinates_m->xSize()];
37  for (int i = 0; i < coordinates_m->xSize(); i++) {
38  f_m[i] = new double*[coordinates_m->ySize()];
39  for (int j = 0; j < coordinates_m->ySize(); j++) {
40  f_m[i][j] = new double[coordinates_m->zSize()];
41  for (int k = 0; k < coordinates_m->zSize(); k++)
42  f_m[i][j][k] = lhs.f_m[i][j][k];
43  }
44  }
45  coordinates_m->add(this);
46 }
47 
49  (const double Point[3], double Value[1]) const {
50  int i, j, k; // position indices
51  coordinates_m->lowerBound(Point[0], i, Point[1], j, Point[2], k);
52  // bound checking
53  if (i + 2 > coordinates_m->xSize() ||
54  j + 2 > coordinates_m->ySize() ||
55  k + 2 > coordinates_m->zSize()) {
56  Value[0] = 0;
57  return;
58  }
59  if (i < 0 || j < 0 || k < 0) {
60  Value[0] = 0;
61  return;
62  }
63  // interpolation in x
64  double dx = (Point[0]-coordinates_m->x(i+1))/
65  (coordinates_m->x(i+2)-coordinates_m->x(i+1));
66  double f_x[2][2];
67  f_x[0][0] = (f_m[i+1][j] [k] - f_m[i][j] [k]) * dx + f_m[i][j] [k];
68  f_x[1][0] = (f_m[i+1][j+1][k] - f_m[i][j+1][k]) * dx + f_m[i][j+1][k];
69  f_x[0][1] = (f_m[i+1][j] [k+1] - f_m[i][j] [k+1]) * dx + f_m[i][j] [k+1];
70  f_x[1][1] = (f_m[i+1][j+1][k+1] - f_m[i][j+1][k+1]) * dx + f_m[i][j+1][k+1];
71  // interpolation in y
72  double f_xy[2];
73  double dy = (Point[1]-coordinates_m->y(j+1))/
74  (coordinates_m->y(j+2) - coordinates_m->y(j+1));
75  f_xy[0] = (f_x[1][0] - f_x[0][0])*dy + f_x[0][0];
76  f_xy[1] = (f_x[1][1] - f_x[0][1])*dy + f_x[0][1];
77  // interpolation in z
78  Value[0] = (f_xy[1] - f_xy[0])/
79  (coordinates_m->z(k+2) - coordinates_m->z(k+1))*
80  (Point[2] - coordinates_m->z(k+1)) + f_xy[0];
81 }
82 }
83 
Definition: Value.h:24
double & y(const int &j)
Definition: ThreeDGrid.h:128
void add(VectorMap *map)
Definition: ThreeDGrid.cpp:257
void lowerBound(const double &x, int &xIndex, const double &y, int &yIndex, const double &z, int &zIndex) const
Definition: ThreeDGrid.h:379
double & x(const int &i)
Definition: ThreeDGrid.h:122
double & z(const int &k)
Definition: ThreeDGrid.h:134
TriLinearInterpolator(ThreeDGrid *grid, double ***F)