REMORA
Energy Research and Forecasting: An Atmospheric Modeling Code
REMORA_Math.H File Reference
#include <AMReX_REAL.H>
Include dependency graph for REMORA_Math.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real interpolate_1d (const amrex::Real *alpha, const amrex::Real *beta, const amrex::Real alpha_interp, const int alpha_size)
 

Function Documentation

◆ interpolate_1d()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real interpolate_1d ( const amrex::Real alpha,
const amrex::Real beta,
const amrex::Real  alpha_interp,
const int  alpha_size 
)
8 {
9  /*
10  Interpolates 1D array beta at the location alpha_interp in the array alpha
11  requiring 1D arrays alpha and beta to be the same size alpha_size.
12  */
13 
14  amrex::Real beta_interp;
15 
16  //in case the interpolation point already exists in the array
17  //just return it
18  for (int i = 0; i < alpha_size; ++i) {
19  if (alpha[i] == alpha_interp) {
20  beta_interp = beta[i];
21  return beta_interp;
22  }
23  }
24 
25  // we didn't find an exact match for alpha_interp in alpha,
26  // so we need linear interpolation/extrapolation
27  const amrex::Real* alpha_begin = &alpha[0];
28  const amrex::Real* alpha_end = &alpha[alpha_size];
29  amrex::Real max = *std::max_element(alpha_begin, alpha_end);
30  amrex::Real min = *std::min_element(alpha_begin, alpha_end);
31  if (alpha_interp >= min && alpha_interp <= max) //interpolate
32  {
33  for (int i = 0; i < alpha_size; ++i)
34  {
35  if (alpha_interp >= alpha[i] && alpha_interp <= alpha[i + 1])
36  {
37  //y = y0 + (y1-y0)*(x-x0)/(x1-x0);
38  amrex::Real y0 = beta[i];
39  amrex::Real y1 = beta[i + 1];
40  amrex::Real x = alpha_interp;
41  amrex::Real x0 = alpha[i];
42  amrex::Real x1 = alpha[i + 1];
43  beta_interp = y0 + (y1 - y0)*(x - x0) / (x1 - x0);
44  }
45  }
46  }
47  else //extrapolate
48  {
49  //y = y0 + ((x - x0) / (x1 - x0)) * (y1 - y0)
50  int i;
51  if (alpha_interp >= *alpha_end)
52  {
53  i = alpha_end - alpha_begin - 1;
54  }
55  else
56  {
57  i = 0;
58  }
59  amrex::Real y0 = beta[i];
60  amrex::Real y1 = beta[i + 1];
61  amrex::Real x = alpha_interp;
62  amrex::Real x0 = alpha[i];
63  amrex::Real x1 = alpha[i + 1];
64  beta_interp = y0 + ((x - x0) / (x1 - x0)) * (y1 - y0);
65  }
66 
67  return beta_interp;
68 
69 using namespace amrex;
70 }