XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
linear_1d_interpolation.hpp
Go to the documentation of this file.
1 #ifndef LINEAR_1D_INTERPOLATION_HPP
2 #define LINEAR_1D_INTERPOLATION_HPP
3 
4 #include "space_settings.hpp"
5 
6 /* Simple 1D linear interpolation
7  * It is assumed that x is sorted in ascending order
8  * @param(in) x View of input x values
9  * @param(in) y View of input y(x) values
10  * @param(in) x1 View of output x values
11  * @param(out) y1 View of output y(x1) values
12  */
13 template<typename T>
14 void linear_1d_interpolation(const T& x, const T& y, const T& x1, const T& y1){
15  int n = x.size();
16  int n1 = x1.size();
17 
18  Kokkos::parallel_for("interpol_1d", Kokkos::RangePolicy<typename T::execution_space>( 0,n1 ), KOKKOS_LAMBDA( const int i){
19  if (x1(i) >= x(0) && x1(i) <= x(n-1)){
20  // use bisection to find points in x closest to x1(i)
21  int is = 0;
22  int ie = n-1;
23 
24  while(ie-is > 1){
25  int ip = (is+ie)/2;
26  if (x1(i) > x(ip)){
27  is=ip;
28  }else{
29  ie=ip;
30  }
31  }
32 
33  double slope = (y(ie)-y(is))/(x(ie)-x(is));
34  y1(i) = y(is) + slope * (x1(i)-x(is));
35  }else{
36  // Extrapolate if outside range
37  if (x1(i) < x(0)){
38  double slope = (y(1)-y(0)) / (x(1)-x(0));
39  y1(i)=y(0) + slope * (x1(i)-x(0));
40  }else if (x1(i) > x(n-1)){
41  double slope = (y(n-1)-y(n-2)) / (x(n-1)-x(n-2));
42  y1(i)=y(n-1) + slope * (x1(i)-x(n-1));
43  }else{
44  // NaN?
45  }
46  }
47  });
48  Kokkos::fence();
49 }
50 
51 #endif
void linear_1d_interpolation(const T &x, const T &y, const T &x1, const T &y1)
Definition: linear_1d_interpolation.hpp:14
void parallel_for(const std::string name, int n_ptl, Function func, Option option, HostAoSoA aosoa_h, DeviceAoSoA aosoa_d)
Definition: streamed_parallel_for.hpp:252