OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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
30namespace interpolation {
31
35 f_m = new double**[coordinates_m->xSize()];
36 for (int i = 0; i < coordinates_m->xSize(); i++) {
37 f_m[i] = new double*[coordinates_m->ySize()];
38 for (int j = 0; j < coordinates_m->ySize(); j++) {
39 f_m[i][j] = new double[coordinates_m->zSize()];
40 for (int k = 0; k < coordinates_m->zSize(); k++)
41 f_m[i][j][k] = lhs.f_m[i][j][k];
42 }
43 }
44 coordinates_m->add(this);
45 }
46
47 void TriLinearInterpolator::function(const double Point[3], double Value[1]) const {
48 int i, j, k; // position indices
49 coordinates_m->lowerBound(Point[0], i, Point[1], j, Point[2], k);
50 // bound checking
51 if (i + 2 > coordinates_m->xSize() || j + 2 > coordinates_m->ySize()
52 || k + 2 > coordinates_m->zSize()) {
53 Value[0] = 0;
54 return;
55 }
56 if (i < 0 || j < 0 || k < 0) {
57 Value[0] = 0;
58 return;
59 }
60 // interpolation in x
61 double dx = (Point[0] - coordinates_m->x(i + 1))
62 / (coordinates_m->x(i + 2) - coordinates_m->x(i + 1));
63 double f_x[2][2];
64 f_x[0][0] = (f_m[i + 1][j][k] - f_m[i][j][k]) * dx + f_m[i][j][k];
65 f_x[1][0] = (f_m[i + 1][j + 1][k] - f_m[i][j + 1][k]) * dx + f_m[i][j + 1][k];
66 f_x[0][1] = (f_m[i + 1][j][k + 1] - f_m[i][j][k + 1]) * dx + f_m[i][j][k + 1];
67 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];
68 // interpolation in y
69 double f_xy[2];
70 double dy = (Point[1] - coordinates_m->y(j + 1))
71 / (coordinates_m->y(j + 2) - coordinates_m->y(j + 1));
72 f_xy[0] = (f_x[1][0] - f_x[0][0]) * dy + f_x[0][0];
73 f_xy[1] = (f_x[1][1] - f_x[0][1]) * dy + f_x[0][1];
74 // interpolation in z
75 Value[0] = (f_xy[1] - f_xy[0]) / (coordinates_m->z(k + 2) - coordinates_m->z(k + 1))
76 * (Point[2] - coordinates_m->z(k + 1))
77 + f_xy[0];
78 }
79} // namespace interpolation
Definition Value.h:23
double & x(const int &i)
Definition ThreeDGrid.h:119
double & z(const int &k)
Definition ThreeDGrid.h:131
double & y(const int &j)
Definition ThreeDGrid.h:125
void add(VectorMap *map)
void lowerBound(const double &x, int &xIndex, const double &y, int &yIndex, const double &z, int &zIndex) const
Definition ThreeDGrid.h:361
TriLinearInterpolator(ThreeDGrid *grid, double ***F)