Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinearInterpolation.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file LinearInterpolation.h
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 
16 #ifndef LINEARINTERPOLATION_H
17 #define LINEARINTERPOLATION_H
18 
19 #include <cmath>
20 
21 #include "RealType.h"
22 
23 namespace Jetscape {
24 
26 template <class type>
27 type LinearInt(real x0, real x1, type y0, type y1, real x) {
28  type temp = ((x - x0) * y1 + (x1 - x) * y0) / (x1 - x0);
29  return temp;
30 }
31 
32 // inspired by numerical recipes
33 // x0,x1: grid points in x-direction
34 // y0,y1: grid points in y-direction
35 // f0-f3: function value starting at x0,y0, continue counterclockwise
36 // put differently: f0=f(x0,y0)
37 // f1=f(x1,y0)
38 // f2=f(x1,y1)
39 // f3=f(x0,y1)
40 template <class type>
42  type f3, real x, real y) {
43  type temp;
44  real t = (x - x0) / (x1 - x0);
45  real u = (y - y0) / (y1 - y0);
46  if ((std::isfinite(u) == 1) && (std::isfinite(t) == 1)) {
47  temp = (1 - t) * (1 - u) * f0 + t * (1 - u) * f1 + t * u * f2 +
48  (1 - t) * u * f3;
49  } else {
50  if (std::isfinite(u) == 0)
51  temp = LinearInt(x0, x1, f0, f2, x);
52  if (std::isfinite(t) == 0)
53  temp = LinearInt(y0, y1, f0, f2, y);
54  }
55  return temp;
56 }
57 
58 // 3D linear interpolation
59 template <class type>
60 type TrilinearInt(real x0, real x1, real y0, real y1, real z0, real z1,
61  type f000, type f001, type f010, type f011, type f100,
62  type f101, type f110, type f111, real x, real y, real z) {
63  type temp;
64  real t = (x - x0) / (x1 - x0);
65  real u = (y - y0) / (y1 - y0);
66  real v = (z - z0) / (z1 - z0);
67 
68  if ((std::isfinite(u) == 1) && (std::isfinite(t) == 1) &&
69  (std::isfinite(v) == 1)) {
70  temp = (1 - t) * (1 - u) * (1 - v) * f000;
71  temp = temp + (1 - t) * (1 - u) * v * f001;
72  temp = temp + (1 - t) * u * (1 - v) * f010;
73  temp = temp + (1 - t) * u * v * f011;
74  temp = temp + t * (1 - u) * (1 - v) * f100;
75  temp = temp + t * (1 - u) * v * f101;
76  temp = temp + t * u * (1 - v) * f110;
77  temp = temp + t * u * v * f111;
78  } else {
79  if (std::isfinite(t) == 0)
80  temp = BilinearInt(y0, y1, z0, z1, f000, f010, f011, f001, y, z);
81  if (std::isfinite(v) == 0)
82  temp = BilinearInt(x0, x1, y0, y1, f000, f100, f110, f010, x, y);
83  if (std::isfinite(u) == 0)
84  temp = BilinearInt(x0, x1, z0, z1, f000, f100, f101, f001, x, z);
85  }
86  return temp;
87 }
88 
89 } //end namespace Jetscape
90 
91 #endif // LINEARINTERPOLATION_H