ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_Interpolation_1D.H
Go to the documentation of this file.
1 #ifndef Initialization_1D_H
2 #define Initialization_1D_H
3 
4 #include <AMReX_BLassert.H>
5 #include <AMReX_REAL.H>
6 #include "ERF_Constants.H"
7 
8 /**
9  * Interpolates 1D array beta at the location alpha_interp in the array alpha
10  * requiring 1D arrays alpha and beta to be the same size alpha_size.
11  * This routine assumes the alpha data are monotonic from lowest to highest.
12  */
13 
14 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
15 amrex::Real interpolate_1d (const amrex::Real* alpha, const amrex::Real* beta,
16  const amrex::Real alpha_interp, const int alpha_size)
17 {
18  // We put this value here so beta_interp is initialized but the logic
19  // below is such that it will always be assigned some other value.
20  amrex::Real beta_interp = bogus_large_value;
21 
22  // An empty table has nothing to interpolate
23  AMREX_ASSERT(alpha_size >= 1);
24 
25  // A single point gives a constant: there is no second point to interpolate or
26  // extrapolate with
27  if (alpha_size == 1) {
28  return beta[0];
29  }
30 
31  // If the interpolation point already exists in the array,
32  // just return it
33  for (int i = 0; i < alpha_size; ++i) {
34  if (alpha[i] == alpha_interp) {
35  beta_interp = beta[i];
36  return beta_interp;
37  }
38  }
39 
40  // We didn't find an exact match for alpha_interp in alpha,
41  // so we need linear interpolation/extrapolation
42  amrex::Real alpha_min = alpha[0];
43  amrex::Real alpha_max = alpha[alpha_size-1];
44  if (alpha_interp >= alpha_min && alpha_interp <= alpha_max) //interpolate
45  {
46  for (int i = 0; i < alpha_size - 1; ++i)
47  {
48  if (alpha_interp >= alpha[i] && alpha_interp <= alpha[i + 1])
49  {
50  //y = y0 + (y1-y0)*(x-x0)/(x1-x0);
51  amrex::Real y0 = beta[i];
52  amrex::Real y1 = beta[i + 1];
53  amrex::Real x = alpha_interp;
54  amrex::Real x0 = alpha[i];
55  amrex::Real x1 = alpha[i + 1];
56  beta_interp = y0 + (y1 - y0)*(x - x0) / (x1 - x0);
57  return beta_interp;
58  }
59  }
60  }
61  else //extrapolate
62  {
63  // y = y0 + ((x - x0) / (x1 - x0)) * (y1 - y0)
64  int i = 0;
65  if (alpha_interp > alpha_max)
66  {
67  i = alpha_size-2;
68  }
69  else if (alpha_interp >= alpha_min)
70  {
71  amrex::Abort("interpolate_1d: we shouldn't be here!");
72  }
73 
74  amrex::Real y0 = beta[i];
75  amrex::Real y1 = beta[i + 1];
76  amrex::Real x = alpha_interp;
77  amrex::Real x0 = alpha[i];
78  amrex::Real x1 = alpha[i + 1];
79  beta_interp = y0 + ((x - x0) / (x1 - x0)) * (y1 - y0);
80  return beta_interp;
81  }
82 
83  return beta_interp;
84 }
85 
86 /*
87  * Interpolate between values from a vector of reals (e.g., zlevels_stag) to a
88  * new, expanded vector with refine_fac-1 new uniformly spaced points between
89  * the original values.
90  *
91  * Example:
92  * Vector<Real> tmp_orig = {0,1,2};
93  * Vector<Real> tmp(4);
94  * expand_and_interpolate_1d(tmp, tmp_orig, 2, false); // tmp == {0, myhalf, 1, amrex::Real(1.5), 2}
95  * expand_and_interpolate_1d(tmp, tmp_orig, 2, true); // tmp == {fourth, amrex::Real(0.75), amrex::Real(1.25), amrex::Real(1.75)}
96  */
97 AMREX_FORCE_INLINE
98 void
99 expand_and_interpolate_1d (amrex::Vector<amrex::Real>& znew,
100  const amrex::Vector<amrex::Real>& zorig,
101  int refine_fac,
102  bool destag=false)
103 {
104  const int Nz = zorig.size();
105  const int Nz_refined = refine_fac*(Nz-1) + 1;
106  if (destag) {
107  AMREX_ALWAYS_ASSERT(znew.size() == Nz_refined-1);
108  } else {
109  AMREX_ALWAYS_ASSERT(znew.size() == Nz_refined);
110  }
111  amrex::Vector<amrex::Real> znew_stag(Nz_refined);
112  for (int k=0; k < Nz; ++k) {
113  znew_stag[refine_fac*k] = zorig[k];
114  }
115  if (refine_fac > 1) {
116  for (int k=0; k < Nz-1; ++k) {
117  amrex::Real z1 = zorig[k];
118  amrex::Real dz = zorig[k+1] - z1;
119  for (int koff=1; koff < refine_fac; ++koff) {
120  znew_stag[refine_fac*k + koff] = amrex::Real(koff)/refine_fac * dz + z1;
121  }
122  }
123  }
124  if (destag) {
125  for (int k=0; k < Nz_refined-1; ++k) {
126  znew[k] = myhalf*(znew_stag[k] + znew_stag[k+1]);
127  }
128  } else {
129  znew = znew_stag;
130  }
131 }
132 #endif
constexpr amrex::Real bogus_large_value
Definition: ERF_Constants.H:19
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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)
Definition: ERF_Interpolation_1D.H:15
AMREX_FORCE_INLINE void expand_and_interpolate_1d(amrex::Vector< amrex::Real > &znew, const amrex::Vector< amrex::Real > &zorig, int refine_fac, bool destag=false)
Definition: ERF_Interpolation_1D.H:99
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
@ beta
Definition: ERF_SLM.H:154
@ alpha
Definition: ERF_SLM.H:153
@ dz
Definition: ERF_AdvanceWDM6.cpp:273