ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MetgridUtils.H
Go to the documentation of this file.
1 /**
2  * \file ERF_MetgridUtils.H
3  */
4 #ifndef ERF_METGRIDUTIL_H_
5 #define ERF_METGRIDUTIL_H_
6 
7 #include <AMReX_Math.H>
8 
9 #include <cmath>
10 #include "ERF_Constants.H"
11 #include <limits>
12 
13 #include <ERF.H>
14 #include <ERF_EOS.H>
15 #include <ERF_SurfaceTemperature.H>
16 #include <ERF_Utils.H>
17 #include <ERF_ProbCommon.H>
18 #include <ERF_HSEUtils.H>
19 #include <ERF_BaseStateParams.H>
20 
21 /**
22  * Return the physical surface pressure used by the Metgrid initialization.
23  *
24  * A debug pressure deliberately overrides file data. Otherwise a PSFC value
25  * from the current forcing file is preferred; when PSFC is absent, invert the
26  * standard-atmosphere temperature profile at the physical terrain height.
27  */
28 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
29 bool
30 metgrid_surface_pressure (const bool debug_psfc,
31  const int flag_psfc,
32  const amrex::Real file_psfc,
33  const amrex::Real z_sfc,
34  const amrex::Real P00,
35  const amrex::Real T00,
36  const amrex::Real TLP,
37  amrex::Real& pressure)
38 {
39  pressure = std::numeric_limits<amrex::Real>::quiet_NaN();
40  if (debug_psfc) {
41  pressure = amrex::Real(100000.0);
42  return true;
43  }
44  if (flag_psfc == 1) {
45  if (!amrex::Math::isfinite(file_psfc) || file_psfc <= amrex::Real(0.0)) {
46  return false;
47  }
48  pressure = file_psfc;
49  return true;
50  }
51 
52  if (!amrex::Math::isfinite(z_sfc) || !amrex::Math::isfinite(P00) || P00 <= amrex::Real(0.0) ||
53  !amrex::Math::isfinite(T00) || T00 <= amrex::Real(0.0) ||
54  !amrex::Math::isfinite(TLP) || TLP <= amrex::Real(0.0)) {
55  return false;
56  }
57  const amrex::Real ToA = T00 / TLP;
58  const amrex::Real radicand =
59  ToA*ToA - amrex::Real(2.0) * CONST_GRAV * z_sfc / (TLP * R_d);
60  if (!amrex::Math::isfinite(radicand) || radicand < amrex::Real(0.0)) {
61  return false;
62  }
63  const amrex::Real exponent = -ToA + std::sqrt(radicand);
64  const amrex::Real result = P00 * std::exp(exponent);
65  if (!amrex::Math::isfinite(result) || result <= amrex::Real(0.0)) {
66  return false;
67  }
68  pressure = result;
69  return true;
70 }
71 
72 /**
73  * Read metgrid NetCDF data and populate ERF initialization FABs.
74  */
75 void
77  int itime,
78  const amrex::Box& domain,
79  const std::string& fname,
80  std::string& NC_dateTime,
81  double& NC_epochTime,
82  int& flag_psfc,
83  int& flag_msf,
84  int& flag_sst,
85  int& flag_tsk,
86  int& flag_lmask,
87  int& NC_nx,
88  int& NC_ny,
89  amrex::Real& NC_dx,
90  amrex::Real& NC_dy,
91  amrex::FArrayBox& NC_xvel_fab,
92  amrex::FArrayBox& NC_yvel_fab,
93  amrex::FArrayBox& NC_temp_fab,
94  amrex::FArrayBox& NC_rhum_fab,
95  amrex::FArrayBox& NC_pres_fab,
96  amrex::FArrayBox& NC_ght_fab,
97  amrex::FArrayBox& NC_hgt_fab,
98  amrex::FArrayBox& NC_psfc_fab,
99  amrex::FArrayBox& NC_msfu_fab,
100  amrex::FArrayBox& NC_msfv_fab,
101  amrex::FArrayBox& NC_msfm_fab,
102  amrex::FArrayBox& NC_sst_fab,
103  amrex::FArrayBox& NC_tsk_fab,
104  amrex::FArrayBox& NC_LAT_fab,
105  amrex::FArrayBox& NC_LON_fab,
106  amrex::IArrayBox& NC_lmask_iab,
107  amrex::Geometry& geom);
108 
109 /**
110  * Initialize nodal terrain heights from metgrid surface-height data.
111  */
112 void
113 init_terrain_from_metgrid (const bool& avg_grid_faces_to_nodes,
114  amrex::Geometry& geom,
115  amrex::MultiFab* z_phys_nd,
116  amrex::FArrayBox& NC_hgt_fab);
117 
118 /**
119  * Initialize ERF state, velocity, and boundary data from metgrid fields.
120  */
121 void
122 init_state_from_metgrid (const int lev,
123  const int itime,
124  const bool use_moisture,
125  const bool interp_theta,
126  const bool metgrid_debug_quiescent,
127  const bool metgrid_debug_isothermal,
128  const bool metgrid_debug_dry,
129  const bool metgrid_basic_linear,
130  const bool metgrid_use_below_sfc,
131  const bool metgrid_use_sfc,
132  const bool metgrid_retain_sfc,
133  const amrex::Real metgrid_proximity,
134  const int metgrid_order,
135  const int metgrid_metgrid_force_sfc_k,
136  const amrex::Real l_rdOcp,
137  amrex::Box& tbxc,
138  amrex::Box& tbxu,
139  amrex::Box& tbxv,
140  amrex::Box& tbxw,
141  amrex::FArrayBox& state_fab,
142  amrex::FArrayBox& x_vel_fab,
143  amrex::FArrayBox& y_vel_fab,
144  amrex::FArrayBox& z_vel_fab,
145  amrex::FArrayBox& z_phys_nd_fab,
146  const amrex::FArrayBox& NC_ght_fab,
147  const amrex::FArrayBox& NC_xvel_fab,
148  const amrex::FArrayBox& NC_yvel_fab,
149  const amrex::FArrayBox& NC_temp_fab,
150  const amrex::FArrayBox& NC_rhum_fab,
151  const amrex::FArrayBox& NC_pres_fab,
152  amrex::FArrayBox& tmp_src_fab,
153  amrex::FArrayBox& tmp_dst_fab,
154  amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_xlo,
155  amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_xhi,
156  amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_ylo,
157  amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_yhi,
158  const amrex::Array4<const int>& mask_c_arr,
159  const amrex::Array4<const int>& mask_u_arr,
160  const amrex::Array4<const int>& mask_v_arr);
161 
162 /**
163  * Initialize map-scale factors from metgrid fields or defaults.
164  */
165 void
166 init_msfs_from_metgrid (const bool metgrid_debug_msf,
167  amrex::FArrayBox& msfu_fab,
168  amrex::FArrayBox& msfv_fab,
169  amrex::FArrayBox& msfm_fab,
170  const int& flag_msf,
171  amrex::FArrayBox& NC_MSFU_fab,
172  amrex::FArrayBox& NC_MSFV_fab,
173  amrex::FArrayBox& NC_MSFM_fab);
174 
175 
176 /**
177  * Initialize hydrostatic base-state quantities from metgrid data.
178  */
179 void
181  const bool metgrid_debug_psfc,
182  const amrex::Real l_rdOcp,
183  const amrex::Box& domain,
184  const amrex::Box& valid_bx,
185  const int& flag_psfc,
186  amrex::FArrayBox& state_fab,
187  amrex::FArrayBox& r_hse_fab,
188  amrex::FArrayBox& p_hse_fab,
189  amrex::FArrayBox& pi_hse_fab,
190  amrex::FArrayBox& th_hse_fab,
191  amrex::FArrayBox& qv_hse_fab,
192  amrex::FArrayBox& z_phys_nd_fab,
193  amrex::FArrayBox& z_phys_cc_fab,
194  const amrex::FArrayBox& NC_psfc_fab,
195  int* base_state_column_failed,
196  const BaseStateParams& bsp);
197 
198 /**
199  * Interpolate a value with a Lagrange polynomial stencil.
200  */
201 AMREX_FORCE_INLINE
202 AMREX_GPU_DEVICE
203 void
204 lagrange_interp (const int& order,
205  amrex::Real* x,
206  amrex::Real* y,
207  amrex::Real& new_x,
208  amrex::Real& new_y)
209 {
210  // Interpolation using Lagrange polynomials.
211  // P(x) = f(x0)Ln0(x) + ... + f(xn)Lnn(x)
212  // where Lnk(x) = (x -x0)(x -x1)...(x -xk-1)(x -xk+1)...(x -xn)
213  // ---------------------------------------------
214  // (xk-x0)(xk-x1)...(xk-xk-1)(xk-xk+1)...(xk-xn)
215  amrex::Real Px = zero;
216  for (int i=0; i <= order; i++) {
217  amrex::Real n = one;
218  amrex::Real d = one;
219  for (int k=0; k <= order; k++) {
220  if (k == i) continue;
221  n *= new_x-x[k];
222  d *= x[i]-x[k];
223  }
224  if (d != zero) {
225  Px += y[i]*n/d;
226  }
227  }
228  new_y = Px;
229 }
230 
231 /**
232  * Prepare interpolation stencils and apply Lagrange interpolation or extrapolation.
233  *
234  * Both orig_npts and new_npts are point counts, not last indices; valid indices
235  * into the orig_* arrays are 0 through orig_npts-1, and into the new_* arrays
236  * are 0 through new_npts-1.
237  */
238 AMREX_FORCE_INLINE
239 AMREX_GPU_DEVICE
240 void
241 lagrange_setup (char var_type,
242  const bool& exp_interp,
243  const int& orig_npts,
244  const int& new_npts,
245  const int& order,
246  const int& i,
247  const int& j,
248  amrex::Real* orig_x_z,
249  amrex::Real* orig_x_p,
250  amrex::Real* orig_y,
251  amrex::Real* new_x_z,
252  amrex::Real* new_x_p,
253  amrex::Real* new_y)
254 {
255 
256  amrex::Real CRC_const1 = amrex::Real(11880.516); // m
257  amrex::Real CRC_const2 = amrex::Real(0.1902632);
258  amrex::Real CRC_const3 = amrex::Real(0.0065); // K km-1
259 
260  amrex::ignore_unused(i,j);
261 #ifndef AMREX_USE_GPU
262  bool debug = false;
263 #endif
264 
265  for (int new_k=0; new_k < new_npts; new_k++) {
266 #ifndef AMREX_USE_GPU
267  if (debug) amrex::Print() << "new_k=" << new_k;
268 #endif
269  // Determine if interpolating or extrapolating.
270  // If interpolating, find bounding x values and store the indices.
271  bool extrapolating = true;
272  int kl, kr;
273  for (int ko=0; ko < orig_npts-1; ko++) {
274  amrex::Real a = new_x_z[new_k]-orig_x_z[ko];
275  amrex::Real b = new_x_z[new_k]-orig_x_z[ko+1];
276  if (a*b <= zero) {
277  kl = ko;
278  kr = ko+1;
279  extrapolating = false;
280  break;
281  }
282  }
283 
284  if (extrapolating) {
285  if (var_type == 'T') {
286  // Assume a standard atmosphere -amrex::Real(6.5) K km-1 lapse rate.
287  // Comparable to the WRF default, t_extrap_type=two
288  amrex::Real depth_of_extrap_in_p = new_x_p[new_k]-orig_x_p[0];
289  amrex::Real avg_of_extrap_p = myhalf*(new_x_p[new_k]+orig_x_p[0]);
290  amrex::Real temp_extrap_starting_point = orig_y[0]*std::pow(orig_x_p[0]/amrex::Real(100000.0), RdoCp);
291  amrex::Real dZdP = CRC_const1*CRC_const2*std::pow(avg_of_extrap_p/amrex::Real(100.0), CRC_const2-one);
292  amrex::Real dZ = dZdP*(depth_of_extrap_in_p/amrex::Real(100.0));
293  amrex::Real dT = dZ*CRC_const3;
294  new_y[new_k] = (temp_extrap_starting_point+dT)*std::pow(amrex::Real(100000.0)/new_x_p[new_k], RdoCp);
295  } else {
296  // Use a constant value below ground.
297  // Comparable to the WRF default, extrap_type=two
298  new_y[new_k] = orig_y[0];
299  }
300  continue;
301  }
302 
303  if (order%2 != 0) {
304  if ((kl-((order+1)/2-1) >= 0) && (kr+((order+1)/2-1) <= orig_npts-1)) {
305  // Odd order interpolation.
306  int ksta = kl-(((order+1)/2)-1);
307  int kend = ksta+order;
308 #ifndef AMREX_USE_GPU
309  int ksize = kend-ksta;
310  if (debug) amrex::Print() << " (1a) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
311 #endif
312  amrex::Real new_x;
313  amrex::GpuArray<amrex::Real,9> orig_x_sub;
314  amrex::Real* orig_x_sub_p = orig_x_sub.data();
315  if (exp_interp) {
316  new_x = new_x_p[new_k];
317  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
318  } else {
319  new_x = new_x_z[new_k];
320  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
321  }
322  amrex::GpuArray<amrex::Real,9> orig_y_sub;
323  amrex::Real* orig_y_sub_p = orig_y_sub.data();
324  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
325 #ifndef AMREX_USE_GPU
326  if (debug) {
327  amrex::Print() << " orig_x_sub_p = [";
328  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
329  amrex::Print() << "]" << std::endl;
330  amrex::Print() << " orig_y_sub_p = [";
331  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
332  amrex::Print() << "]" << std::endl;
333  }
334 #endif
335  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
336  } else {
337  // Interpolation stencil is too big due to proximity to the surface or model top.
338  // We have bounding points so resort to a simple linear interpolation.
339  int ksta = kl;
340  int kend = kr;
341 #ifndef AMREX_USE_GPU
342  int ksize = kend-ksta+1;
343  if (debug) amrex::Print() << " (1b) order=" << order << " new_x=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
344 #endif
345  amrex::Real new_x;
346  amrex::GpuArray<amrex::Real,2> orig_x_sub;
347  amrex::GpuArray<amrex::Real,2> orig_y_sub;
348  amrex::Real* orig_x_sub_p = orig_x_sub.data();
349  amrex::Real* orig_y_sub_p = orig_y_sub.data();
350  if (exp_interp) {
351  new_x = new_x_p[new_k];
352  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
353  } else {
354  new_x = new_x_z[new_k];
355  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
356  }
357  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
358 #ifndef AMREX_USE_GPU
359  if (debug) {
360  amrex::Print() << " orig_x_sub_p = [";
361  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
362  amrex::Print() << "]" << std::endl;
363  amrex::Print() << " orig_y_sub_p = [";
364  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
365  amrex::Print() << "]" << std::endl;
366  }
367 #endif
368  lagrange_interp(1, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
369  }
370  } else if (order%2 == 0) {
371  if ((kl-(order/2) >= 0) && (kr+order/2 <= orig_npts-1)) {
372  // Even order interpolation.
373  // Interpolate twice. Offset the range by 1 the 2nd time. Average the results.
374  amrex::Real new_y_l, new_y_r;
375  {
376  int ksta = kl-(order/2-1);
377  int kend = ksta+order;
378 #ifndef AMREX_USE_GPU
379  int ksize = kend-ksta;
380  if (debug) amrex::Print() << " (2a) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
381 #endif
382  amrex::Real new_x;
383  amrex::GpuArray<amrex::Real,10> orig_x_sub;
384  amrex::GpuArray<amrex::Real,10> orig_y_sub;
385  amrex::Real* orig_x_sub_p = orig_x_sub.data();
386  amrex::Real* orig_y_sub_p = orig_y_sub.data();
387  if (exp_interp) {
388  new_x = new_x_p[new_k];
389  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
390  } else {
391  new_x = new_x_z[new_k];
392  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
393  }
394  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
395 #ifndef AMREX_USE_GPU
396  if (debug) {
397  amrex::Print() << " orig_x_sub_p = [";
398  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
399  amrex::Print() << "]" << std::endl;
400  amrex::Print() << " orig_y_sub_p = [";
401  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
402  amrex::Print() << "]" << std::endl;
403  }
404 #endif
405  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y_l);
406  }
407  {
408  int ksta = kl-order/2;
409  int kend = ksta+order;
410 #ifndef AMREX_USE_GPU
411  int ksize = kend-ksta;
412  if (debug) amrex::Print() << "new_k=" << new_k << " (2b) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
413 #endif
414  amrex::Real new_x;
415  amrex::GpuArray<amrex::Real,10> orig_x_sub;
416  amrex::GpuArray<amrex::Real,10> orig_y_sub;
417  amrex::Real* orig_x_sub_p = orig_x_sub.data();
418  amrex::Real* orig_y_sub_p = orig_y_sub.data();
419  if (exp_interp) {
420  new_x = new_x_p[new_k];
421  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
422  } else {
423  new_x = new_x_z[new_k];
424  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
425  }
426  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
427 #ifndef AMREX_USE_GPU
428  if (debug) {
429  amrex::Print() << " orig_x_sub_p = [";
430  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
431  amrex::Print() << "]" << std::endl;
432  amrex::Print() << " orig_y_sub_p = [";
433  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
434  amrex::Print() << "]" << std::endl;
435  }
436 #endif
437  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y_r);
438  }
439  new_y[new_k] = myhalf*(new_y_l+new_y_r);
440  } else if ((kl-(order/2-1) >= 0) && (kr+order/2 <= orig_npts-1)) {
441  int ksta = kl-(order/2-1);
442  int kend = ksta+order;
443 #ifndef AMREX_USE_GPU
444  int ksize = kend-ksta;
445  if (debug) amrex::Print() << " (3) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
446 #endif
447  amrex::Real new_x;
448  amrex::GpuArray<amrex::Real,10> orig_x_sub;
449  amrex::GpuArray<amrex::Real,10> orig_y_sub;
450  amrex::Real* orig_x_sub_p = orig_x_sub.data();
451  amrex::Real* orig_y_sub_p = orig_y_sub.data();
452  if (exp_interp) {
453  new_x = new_x_p[new_k];
454  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
455  } else {
456  new_x = new_x_z[new_k];
457  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
458  }
459  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
460 #ifndef AMREX_USE_GPU
461  if (debug) {
462  amrex::Print() << " orig_x_sub_p = [";
463  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
464  amrex::Print() << "]" << std::endl;
465  amrex::Print() << " orig_y_sub_p = [";
466  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
467  amrex::Print() << "]" << std::endl;
468  }
469 #endif
470  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
471  } else if ((kl-order/2 >= 0) && (kr+order/2-1 <= orig_npts-1)) {
472  int ksta = kl-order/2;
473  int kend = ksta+order;
474 #ifndef AMREX_USE_GPU
475  int ksize = kend-ksta;
476  if (debug) amrex::Print() << " (4) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
477 #endif
478  amrex::Real new_x;
479  amrex::GpuArray<amrex::Real,10> orig_x_sub;
480  amrex::GpuArray<amrex::Real,10> orig_y_sub;
481  amrex::Real* orig_x_sub_p = orig_x_sub.data();
482  amrex::Real* orig_y_sub_p = orig_y_sub.data();
483  if (exp_interp) {
484  new_x = new_x_p[new_k];
485  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
486  } else {
487  new_x = new_x_z[new_k];
488  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
489  }
490  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
491 #ifndef AMREX_USE_GPU
492  if (debug) {
493  amrex::Print() << " orig_x_sub_p = [";
494  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
495  amrex::Print() << "]" << std::endl;
496  amrex::Print() << " orig_y_sub_p = [";
497  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
498  amrex::Print() << "]" << std::endl;
499  }
500 #endif
501  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
502  } else {
503  // Linear interpolation.
504  int ksta = kl;
505  int kend = kr;
506 #ifndef AMREX_USE_GPU
507  int ksize = kend-ksta+1;
508  if (debug) amrex::Print() << " (5) order=" << order << " new_x=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
509 #endif
510  amrex::Real new_x;
511  amrex::GpuArray<amrex::Real,2> orig_x_sub;
512  amrex::GpuArray<amrex::Real,2> orig_y_sub;
513  amrex::Real* orig_x_sub_p = orig_x_sub.data();
514  amrex::Real* orig_y_sub_p = orig_y_sub.data();
515  if (exp_interp) {
516  new_x = new_x_p[new_k];
517  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
518  } else {
519  new_x = new_x_z[new_k];
520  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
521  }
522  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
523 #ifndef AMREX_USE_GPU
524  if (debug) {
525  amrex::Print() << " orig_x_sub_p = [";
526  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
527  amrex::Print() << "]" << std::endl;
528  amrex::Print() << " orig_y_sub_p = [";
529  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
530  amrex::Print() << "]" << std::endl;
531  }
532 #endif
533  lagrange_interp(1, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
534  }
535  }
536 #ifndef AMREX_USE_GPU
537  if (debug) amrex::Print() << " new_y[" << new_k << "]=" << new_y[new_k] << std::endl;
538 #endif
539  }
540 }
541 
542 /**
543  * Compute isothermal pressure at height z.
544  */
545 AMREX_FORCE_INLINE
546 AMREX_GPU_DEVICE
547 void
548 calc_p_isothermal (const amrex::Real& z,
549  amrex::Real& p)
550 {
551  p = p_0*std::exp(-CONST_GRAV*z/(amrex::Real(290.0)*R_d));
552 }
553 
554 /**
555  * Interpolate one metgrid column onto ERF vertical levels with quality control.
556  */
557 AMREX_FORCE_INLINE
558 AMREX_GPU_DEVICE
559 void
560 interpolate_column_metgrid (const bool& metgrid_use_below_sfc,
561  const bool& metgrid_use_sfc,
562  const bool& exp_interp,
563  const bool& metgrid_retain_sfc,
564  const amrex::Real& metgrid_proximity,
565  const int& metgrid_order,
566  const int& metgrid_force_sfc_k,
567  const int& i,
568  const int& j,
569  const int& kmax,
570  const int& src_comp,
571  const int& dst_comp,
572  char var_type,
573  char stag,
574  const amrex::Array4<amrex::Real const>& orig_z_full,
575  const amrex::Array4<amrex::Real const>& orig_data,
576  const amrex::Array4<amrex::Real const>& new_z_full,
577  const amrex::Array4<amrex::Real>& new_data_full)
578 {
579  // Here we closely follow WRF's vert_interp from
580  // dyn_em/module_initialize_real.F, although changes have been
581  // made to accommodate interpolation relative to height instead of
582  // pressure.
583  int imin_orig = amrex::lbound(amrex::Box(orig_data)).x;
584  int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
585  int jmin_orig = amrex::lbound(amrex::Box(orig_data)).y;
586  int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
587  // The number of origin levels, 0 .. ubound: the loops below run k < kmax_orig, so taking
588  // ubound itself here would leave the top origin level out
589  int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z + 1;
590  int kmax_new = kmax + 1;
591 
592  AMREX_ASSERT(kmax_orig <= 256);
593  AMREX_ASSERT(kmax_new < 256);
594 
595  amrex::GpuArray<amrex::Real,256> new_z;
596  amrex::GpuArray<amrex::Real,256> new_p;
597  amrex::GpuArray<amrex::Real,256> new_data;
598  amrex::Real* new_z_p = new_z.data();
599  amrex::Real* new_p_p = new_p.data();
600  amrex::Real* new_data_p = new_data.data();
601  for (int k=0; k < kmax_new; k++) {
602  if (stag == 'X') {
603  new_z_p[k] = fourth*(new_z_full(i,j,k)+new_z_full(i,j+1,k)+new_z_full(i,j,k+1)+new_z_full(i,j+1,k+1));
604  } else if (stag == 'Y') {
605  new_z_p[k] = fourth*(new_z_full(i,j,k)+new_z_full(i+1,j,k)+new_z_full(i,j,k+1)+new_z_full(i+1,j,k+1));
606  } else if (stag == 'M') {
607  new_z_p[k] = amrex::Real(0.125)*(new_z_full(i,j,k )+new_z_full(i,j+1,k )+new_z_full(i+1,j,k )+new_z_full(i+1,j+1,k )+
608  new_z_full(i,j,k+1)+new_z_full(i,j+1,k+1)+new_z_full(i+1,j,k+1)+new_z_full(i+1,j+1,k+1));
609  }
610  calc_p_isothermal(new_z_p[k], new_p_p[k]);
611  }
612 
613  amrex::GpuArray<amrex::Real,256> orig_z;
614  amrex::Real* orig_z_p = orig_z.data();
615  for (int k=0; k < kmax_orig; k++) {
616  if (stag == 'M') {
617  orig_z_p[k] = orig_z_full(i,j,k);
618  } else if (stag == 'X') {
619  if (i <= imin_orig) {
620  orig_z_p[k] = orig_z_full(i,j,k);
621  } else if (i >= imax_orig) {
622  orig_z_p[k] = orig_z_full(imax_orig-1,j,k);
623  } else {
624  orig_z_p[k] = myhalf*(orig_z_full(i,j,k)+orig_z_full(i-1,j,k));
625  }
626  } else if (stag == 'Y') {
627  if (j <= jmin_orig) {
628  orig_z_p[k] = orig_z_full(i,j,k);
629  } else if (j >= jmax_orig) {
630  orig_z_p[k] = orig_z_full(i,jmax_orig-1,k);
631  } else {
632  orig_z_p[k] = myhalf*(orig_z_full(i,j,k)+orig_z_full(i,j-1,k));
633  }
634  }
635  }
636 
637  // Check if the data is top-down instead of bottom-up.
638  bool flip_data_required = false;
639  if (orig_z_p[1] > orig_z_p[kmax_orig-1]) flip_data_required = true;
640  if (flip_data_required) amrex::Abort("metgrid initialization flip_data_required. Not yet implemented.");
641 
642  // Search for the first level above the surface in the origin data.
643  // This is needed since the origin model topography will be
644  // different than the topography processed by WPS.
645  int k_above_sfc = 0;
646  for (int k=1; k < kmax_orig; k++) {
647  if (orig_z_p[k] > orig_z_p[0]) {
648  k_above_sfc = k;
649  break;
650  }
651  }
652 
653  int kend_order;
654  amrex::GpuArray<amrex::Real,256> ordered_z;
655  amrex::GpuArray<amrex::Real,256> ordered_data;
656  amrex::Real* ordered_z_p = ordered_z.data();
657  amrex::Real* ordered_data_p = ordered_data.data();
658  if (k_above_sfc > 1) {
659  // The levels are not monotonically increasing in height, so
660  // we sort and then make "artistic" quality control choices.
661  int count = 0;
662 
663  // Insert levels that are below the surface.
664  for (int k=1; k < k_above_sfc; k++) {
665  ordered_z_p[count] = orig_z_p[k];
666  ordered_data_p[count] = orig_data(i,j,k,src_comp);
667  count++;
668  }
669 
670  // Check if the level that is nearest to and below the surface
671  // is "too close". If so, we'll ignore the upper level and keep
672  // the lower. Origin data is likely to be on pressure levels
673  // with higher spatial resolution near-surface, which supports
674  // the choice of eliminating levels that are "too close" in
675  // pressure-space. For simplicity, calculate delta P assuming a
676  // baroclinic atmosphere.
677  amrex::Real Pu, Pl;
678  calc_p_isothermal(orig_z_p[0], Pu);
679  calc_p_isothermal(ordered_z_p[count-1], Pl);
680  if (Pl-Pu < metgrid_proximity) {
681  count--;
682  }
683 
684  // Insert the surface level.
685  ordered_z_p[count] = orig_z_p[0];
686  ordered_data_p[count] = orig_data(i,j,0,src_comp);
687  count++;
688 
689  // Quoting WRF's comments, the next level to use is at,
690  // "... ta da, the first level above the surface. I know, wow."
691  int knext = k_above_sfc;
692  // Conditionally more strongly use the surface data by removing
693  // levels between the surface and the height corresponding to a
694  // set number of ERF levels from the surface. This forces the
695  // interpolator to use the surface data up through a number of
696  // ERF levels from the surface.
697  if (metgrid_force_sfc_k > 0) {
698  for (int k=k_above_sfc; k < kmax_orig; k++) {
699  if (orig_z_p[k] > new_z_p[metgrid_force_sfc_k-1]) {
700  knext = k;
701  break;
702  }
703  }
704  }
705 
706  // Check if the level that is nearest to and above the surface
707  // is "too close". If so, we'll ignore that level.
708  calc_p_isothermal(orig_z_p[knext], Pu);
709  calc_p_isothermal(ordered_z_p[count-1], Pl);
710  if (Pl-Pu < metgrid_proximity) {
711  knext++;
712  }
713 
714  // Insert levels that are above the surface.
715  for (int k=knext; k < kmax_orig; k++) {
716  ordered_z_p[count] = orig_z_p[k];
717  ordered_data_p[count] = orig_data(i,j,k,src_comp);
718  count++;
719  }
720 
721  kend_order = count;
722  } else {
723  // The surface is the lowest level in the origin data.
724 
725  // Insert the surface.
726  ordered_z_p[0] = orig_z_p[0];
727  ordered_data_p[0] = orig_data(i,j,0,src_comp);
728 
729  // Similar to above, conditionally more strongly use the
730  // surface data.
731  int count = 1;
732  int knext = count;
733  // The same ERF level as in the branch above: the surface is used through
734  // metgrid_force_sfc_k ERF levels, k = 0 .. metgrid_force_sfc_k-1
735  if (metgrid_force_sfc_k > 0) {
736  for (int k=knext; k < kmax_orig; k++) {
737  if (orig_z_p[k] > new_z_p[metgrid_force_sfc_k-1]) {
738  knext = k;
739  break;
740  }
741  }
742  }
743 
744  // Insert the remaining levels, again ignoring levels that are
745  // "too close" to the prior valid level.
746  for (int k=knext; k < kmax_orig; k++) {
747  amrex::Real Pu, Pl;
748  calc_p_isothermal(orig_z_p[k], Pu);
749  calc_p_isothermal(ordered_z_p[count-1], Pl);
750  if (Pl-Pu < metgrid_proximity) {
751  continue;
752  }
753  ordered_z_p[count] = orig_z_p[k];
754  ordered_data_p[count] = orig_data(i,j,k,src_comp);
755  count++;
756  }
757  kend_order = count;
758  }
759 
760  int ksta(0), kend(0);
761  if (metgrid_use_below_sfc && metgrid_use_sfc) {
762  // Use all levels.
763  ksta = 0;
764  kend = kend_order-1;
765  } else if (metgrid_use_below_sfc && !metgrid_use_sfc) {
766  // Use all levels except for the surface.
767  int ksfc = 0;
768  for (int k=0; k < kmax_orig; k++) {
769  if (ordered_z_p[k] == orig_z_p[0]) {
770  ksfc = k;
771  break;
772  }
773  }
774  for (int k=ksfc; k < kmax_orig-1; k++) {
775  ordered_z_p[k] = ordered_z_p[k+1];
776  ordered_data_p[k] = ordered_data_p[k+1];
777  }
778  ksta = 0;
779  kend = kend_order-2;
780  } else if (!metgrid_use_below_sfc && metgrid_use_sfc) {
781  // Use all levels above and including the surface.
782  int ksfc = 0;
783  for (int k=0; k < kend_order; k++) {
784  if (ordered_z_p[k] == orig_z_p[0]) {
785  ksfc = k;
786  break;
787  }
788  }
789  ksta = ksfc;
790  kend = kend_order-1;
791  } else {
792  // We shouldn't be in here!
793  amrex::Abort("metgrid initialization, !use_levels below_ground && !metgrid_use_sfc");
794  }
795 
796  // Insert the level of maximum winds.
797 // amrex::Real maxw_above_this_level = amrex::Real(30000.0);
798 // amrex::Real maxw_horiz_pres_diff = amrex::Real(5000.0);
799 // if ((flag_maxw == 1) && (use_maxw)) {
800 // amrex::Abort("metgrid initialization, use_maxw not yet implemented");
801 // }
802 
803  // Insert the level of the tropopause.
804 // amrex::Real trop_horiz_pres_diff = amrex::Real(5000.0);
805 // if ((flag_trop == 1) && (use_trop)) {
806 // amrex::Abort("metgrid initialization, use_trop not yet implemented");
807 // }
808 
809  amrex::GpuArray<amrex::Real,256> ordered_p;
810  amrex::Real* ordered_p_p = ordered_p.data();
811  for (int k=0; k < kend_order; k++) {
812  calc_p_isothermal(ordered_z_p[k], ordered_p_p[k]);
813  }
814 
815  // kfinal is the index of the topmost level retained in the final_*
816  // arrays, so those arrays hold kfinal+1 points.
817  int kfinal = 0;
818  amrex::GpuArray<amrex::Real,256> final_z;
819  amrex::GpuArray<amrex::Real,256> final_p;
820  amrex::GpuArray<amrex::Real,256> final_data;
821  amrex::Real* final_z_p = final_z.data();
822  amrex::Real* final_p_p = final_p.data();
823  amrex::Real* final_data_p = final_data.data();
824  final_z_p[0] = ordered_z[ksta];
825  final_p_p[0] = ordered_p[ksta];
826  final_data_p[0] = ordered_data[ksta];
827  for (int k=ksta+1; k <= kend; k++) {
828  // Skip levels that are "too close" to the prior retained level.
829  if ((final_p_p[kfinal]-ordered_p_p[k]) < metgrid_proximity) continue;
830  kfinal++;
831  final_z_p[kfinal] = ordered_z_p[k];
832  final_p_p[kfinal] = ordered_p_p[k];
833  final_data_p[kfinal] = ordered_data_p[k];
834  }
835 
836  // Call the interpolator.
837  lagrange_setup(var_type,
838  exp_interp,
839  kfinal+1,
840  kmax_new,
841  metgrid_order,
842  i,
843  j,
844  final_z_p,
845  final_p_p,
846  final_data_p,
847  new_z_p,
848  new_p_p,
849  new_data_p);
850 
851  // Optionally replace the lowest level of data with the surface
852  // field from the origin data. That is origin level 0: ordered_data[0] is
853  // the surface only when no origin level lies below it.
854  if (metgrid_retain_sfc) new_data[0] = orig_data(i,j,0,src_comp);
855 
856  // Save the interpolated data.
857  for (int k=0; k < kmax_new; k++) {
858  new_data_full(i,j,k,dst_comp) = new_data[k];
859  }
860 
861 }
862 
863 /**
864  * Linearly interpolate one metgrid column value onto an ERF point.
865  */
866 AMREX_FORCE_INLINE
867 AMREX_GPU_DEVICE
868 amrex::Real
870  const int& j,
871  const int& k,
872  char stag,
873  int src_comp,
874  const amrex::Array4<amrex::Real const>& orig_z,
875  const amrex::Array4<amrex::Real const>& orig_data,
876  const amrex::Array4<amrex::Real const>& new_z)
877 {
878  // This subroutine is a bit ham-handed and can be cleaned up later.
879  int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
880  int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
881  // The number of origin levels, 0 .. ubound, so that the top one is searched too
882  int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z + 1;
883 
884  amrex::Real z;
885  if (stag == 'X') {
886  z = fourth*(new_z(i,j,k)+new_z(i,j+1,k)+new_z(i,j,k+1)+new_z(i,j+1,k+1));
887  }
888  else if (stag == 'Y') {
889  z = fourth*(new_z(i,j,k)+new_z(i+1,j,k)+new_z(i,j,k+1)+new_z(i+1,j,k+1));
890  }
891  else if (stag == 'M') {
892  z = amrex::Real(0.125)*(new_z(i,j,k )+new_z(i,j+1,k )+new_z(i+1,j,k )+new_z(i+1,j+1,k )+
893  new_z(i,j,k+1)+new_z(i,j+1,k+1)+new_z(i+1,j,k+1)+new_z(i+1,j+1,k+1));
894  }
895 
896  // Initialized to NaN so that any path which fails to set them fails loudly
897  // (with amrex.fpe_trap_invalid=1) rather than returning plausible garbage.
898  amrex::Real z0 = std::numeric_limits<amrex::Real>::quiet_NaN();
899  amrex::Real z1 = std::numeric_limits<amrex::Real>::quiet_NaN();
900  int klow = -1;
901  int khi0 = -1;
902  amrex::Real dzlow = amrex::Real(1.0e12);
903  amrex::Real dzhi0 = -amrex::Real(1.0e12);
904  for (int kk = 0; kk < kmax_orig; kk++) {
905  amrex::Real orig_z_stag = zero;
906  if (stag == 'M') {
907  orig_z_stag = orig_z(i,j,kk);
908  }
909  if (stag == 'X') {
910  if (i == 0) {
911  orig_z_stag = orig_z(i,j,kk);
912  }
913  else if (i == imax_orig) {
914  orig_z_stag = orig_z(imax_orig-1,j,kk);
915  }
916  else {
917  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
918  }
919  }
920  else if (stag == 'Y') {
921  if (j == 0) {
922  orig_z_stag = orig_z(i,j,kk);
923  }
924  else if (j == jmax_orig) {
925  orig_z_stag = orig_z(i,jmax_orig-1,kk);
926  }
927  else {
928  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
929  }
930  }
931 
932  amrex::Real dz = z - orig_z_stag;
933  if ((dz < zero) && (dz > dzhi0)) {
934  dzhi0 = dz;
935  khi0 = kk;
936  z1 = orig_z_stag;
937  }
938  if ((dz >= zero) && (dz < dzlow)) {
939  dzlow = dz;
940  klow = kk;
941  z0 = orig_z_stag;
942  }
943  } // kk
944 
945  // extrapolate below the bottom surface
946  if (klow == -1) {
947  // klow was never found, so z0 was never set above. The two levels used
948  // here are khi0 and khi1, both above z, so z0 is the height at khi0 --
949  // which is exactly what the search loop left in z1 before we clobber it.
950  z0 = z1;
951  int khi1 = -1;
952  amrex::Real dzhi1 = -amrex::Real(1.0e12);
953  for (int kk = 0; kk < kmax_orig; kk++) {
954  amrex::Real orig_z_stag = zero;
955  if (stag == 'M') {
956  orig_z_stag = orig_z(i,j,kk);
957  }
958  else if (stag == 'X') {
959  if (i == 0) {
960  orig_z_stag = orig_z(i,j,kk);
961  }
962  else if (i == imax_orig) {
963  orig_z_stag = orig_z(imax_orig-1,j,kk);
964  }
965  else {
966  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
967  }
968  }
969  else if (stag == 'Y') {
970  if (j == 0) {
971  orig_z_stag = orig_z(i,j,kk);
972  }
973  else if (j == jmax_orig) {
974  orig_z_stag = orig_z(i,jmax_orig-1,kk);
975  }
976  else {
977  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
978  }
979  }
980  amrex::Real dz = z - orig_z_stag;
981  if ((dz < zero) && (dz > dzhi1) && (kk != khi0)) {
982  dzhi1 = dz;
983  khi1 = kk;
984  z1 = orig_z_stag;
985  }
986  }
987  amrex::Real y0 = orig_data(i,j,khi0,src_comp);
988  amrex::Real y1 = orig_data(i,j,khi1,src_comp);
989  return ( y0-(y1-y0)/(z1-z0)*(z0-z) );
990 
991  // Extrapolate above the top surface
992  } else if (khi0 == -1) {
993  khi0 = klow - 1;
994  int khi1 = klow;
995  // khi0 was never found, so z1 was never set above. The upper level used
996  // here is khi1 = klow, whose height is currently held in z0 -- capture
997  // it before z0 is recomputed at the new (lower) khi0.
998  z1 = z0;
999  if (stag == 'M') {
1000  z0 = orig_z(i,j,khi0);
1001  }
1002  else if (stag == 'X') {
1003  if (i == 0) {
1004  z0 = orig_z(i,j,khi0);
1005  }
1006  else if (i == imax_orig) {
1007  z0 = orig_z(imax_orig-1,j,khi0);
1008  }
1009  else {
1010  z0 = myhalf*(orig_z(i,j,khi0)+orig_z(i-1,j,khi0));
1011  }
1012  }
1013  else if (stag == 'Y') {
1014  if (j == 0) {
1015  z0 = orig_z(i,j,khi0);
1016  }
1017  else if (j == jmax_orig) {
1018  z0 = orig_z(i,jmax_orig-1,khi0);
1019  }
1020  else {
1021  z0 = myhalf*(orig_z(i,j,khi0)+orig_z(i,j-1,khi0));
1022  }
1023  }
1024  amrex::Real y0 = orig_data(i,j,khi0,src_comp);
1025  amrex::Real y1 = orig_data(i,j,khi1,src_comp);
1026  return ( y0+(y1-y0)/(z1-z0)*(z-z0) );
1027  } else {
1028  // interpolate
1029  amrex::Real y0 = orig_data(i,j,klow,src_comp);
1030  amrex::Real y1 = orig_data(i,j,khi0,src_comp);
1031  return ( y0+(y1-y0)/(z1-z0)*(z-z0) );
1032 
1033  }
1034 }
1035 
1036 /**
1037  * Convert relative humidity to vapor mixing ratio using WRF-compatible bounds.
1038  */
1039 AMREX_FORCE_INLINE
1040 AMREX_GPU_DEVICE
1041 void
1043  int j,
1044  int k,
1045  const amrex::Array4<amrex::Real const>& rhum,
1046  const amrex::Array4<amrex::Real const>& temp,
1047  const amrex::Array4<amrex::Real const>& pres,
1048  int src_indx,
1049  const amrex::Array4<amrex::Real>& mxrat)
1050 {
1051  amrex::Real qv_max_p_safe = amrex::Real(10000.0); // WRF default value
1052  amrex::Real qv_max_flag = amrex::Real(1.0e-5); // WRF default value
1053  amrex::Real qv_max_value = amrex::Real(3.0e-6); // WRF default value
1054  amrex::Real qv_min_p_safe = amrex::Real(110000.0); // WRF default value
1055  amrex::Real qv_min_flag = amrex::Real(1.0e-6); // WRF default value
1056  amrex::Real qv_min_value = amrex::Real(1.0e-6); // WRF default value
1057  amrex::Real eps = RdoRv;
1058  amrex::Real svp1 = amrex::Real(0.6112);
1059  amrex::Real svp2 = amrex::Real(17.67);
1060  amrex::Real svp3 = amrex::Real(29.65);
1061  amrex::Real svpt0 = amrex::Real(273.15);
1062  // WRF's method when model_config_rec%rh2qv_wrt_liquid=.true. (default behavior)
1063  if (temp(i,j,k) != zero) {
1064  amrex::Real es=amrex::Real(0.01)*rhum(i,j,k)*svp1*amrex::Real(10.0)*std::exp(svp2*(temp(i,j,k)-svpt0)/(temp(i,j,k)-svp3));
1065  if (es >= pres(i,j,k)/amrex::Real(100.0)) {
1066  // vapor pressure exceeds total pressure
1067  mxrat(i,j,k,src_indx) = amrex::Math::powi<-6>(amrex::Real(10.0));
1068  }
1069  else {
1070  mxrat(i,j,k,src_indx) = amrex::max(eps*es/(pres(i,j,k)/amrex::Real(100.0)-es), amrex::Real(1.0e-6));
1071  }
1072  }
1073  else {
1074  // I don't know why there's a fringe case handled in WRF where T is absolute zero...
1075  // Let's just deal with it here in case we also end up needing it.
1076  mxrat(i,j,k,src_indx) = amrex::Real(1.0e-6);
1077  }
1078  // See the below comment from WRF dyn_em/module_initialize_real.F rh_to_mxrat1.
1079  // For pressures above a defined level, reasonable Qv values should be
1080  // a certain value or smaller. If they are larger than this, the input data
1081  // probably had "missing" RH, and we filled in some values. This is an
1082  // attempt to catch those. Also, set the minimum value for the entire
1083  // domain that is above the selected pressure level.
1084  if (pres(i,j,k) < qv_max_p_safe) {
1085  if (mxrat(i,j,k,src_indx) > qv_max_flag) {
1086  mxrat(i,j,k,src_indx) = qv_max_value;
1087  }
1088  }
1089  if (pres(i,j,k) < qv_min_p_safe) {
1090  if (mxrat(i,j,k,src_indx) < qv_min_flag) {
1091  mxrat(i,j,k,src_indx) = qv_min_value;
1092  }
1093  }
1094 }
1095 #endif
int count
Definition: ERF_ActuatorSpreading.cpp:264
constexpr amrex::Real p_0
Definition: ERF_Constants.H:69
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:72
constexpr amrex::Real R_d
Definition: ERF_Constants.H:49
constexpr amrex::Real RdoCp
Definition: ERF_Constants.H:57
constexpr amrex::Real RdoRv
Definition: ERF_Constants.H:60
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:12
const bool use_moisture
Definition: ERF_InitCustomPert_ABL.H:127
void init_terrain_from_metgrid(const bool &avg_grid_faces_to_nodes, amrex::Geometry &geom, amrex::MultiFab *z_phys_nd, amrex::FArrayBox &NC_hgt_fab)
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_interp(const int &order, amrex::Real *x, amrex::Real *y, amrex::Real &new_x, amrex::Real &new_y)
Definition: ERF_MetgridUtils.H:204
AMREX_FORCE_INLINE AMREX_GPU_DEVICE amrex::Real interpolate_column_metgrid_linear(const int &i, const int &j, const int &k, char stag, int src_comp, const amrex::Array4< amrex::Real const > &orig_z, const amrex::Array4< amrex::Real const > &orig_data, const amrex::Array4< amrex::Real const > &new_z)
Definition: ERF_MetgridUtils.H:869
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void interpolate_column_metgrid(const bool &metgrid_use_below_sfc, const bool &metgrid_use_sfc, const bool &exp_interp, const bool &metgrid_retain_sfc, const amrex::Real &metgrid_proximity, const int &metgrid_order, const int &metgrid_force_sfc_k, const int &i, const int &j, const int &kmax, const int &src_comp, const int &dst_comp, char var_type, char stag, const amrex::Array4< amrex::Real const > &orig_z_full, const amrex::Array4< amrex::Real const > &orig_data, const amrex::Array4< amrex::Real const > &new_z_full, const amrex::Array4< amrex::Real > &new_data_full)
Definition: ERF_MetgridUtils.H:560
void read_from_metgrid(int lev, int itime, const amrex::Box &domain, const std::string &fname, std::string &NC_dateTime, double &NC_epochTime, int &flag_psfc, int &flag_msf, int &flag_sst, int &flag_tsk, int &flag_lmask, int &NC_nx, int &NC_ny, amrex::Real &NC_dx, amrex::Real &NC_dy, amrex::FArrayBox &NC_xvel_fab, amrex::FArrayBox &NC_yvel_fab, amrex::FArrayBox &NC_temp_fab, amrex::FArrayBox &NC_rhum_fab, amrex::FArrayBox &NC_pres_fab, amrex::FArrayBox &NC_ght_fab, amrex::FArrayBox &NC_hgt_fab, amrex::FArrayBox &NC_psfc_fab, amrex::FArrayBox &NC_msfu_fab, amrex::FArrayBox &NC_msfv_fab, amrex::FArrayBox &NC_msfm_fab, amrex::FArrayBox &NC_sst_fab, amrex::FArrayBox &NC_tsk_fab, amrex::FArrayBox &NC_LAT_fab, amrex::FArrayBox &NC_LON_fab, amrex::IArrayBox &NC_lmask_iab, amrex::Geometry &geom)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool metgrid_surface_pressure(const bool debug_psfc, const int flag_psfc, const amrex::Real file_psfc, const amrex::Real z_sfc, const amrex::Real P00, const amrex::Real T00, const amrex::Real TLP, amrex::Real &pressure)
Definition: ERF_MetgridUtils.H:30
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void calc_p_isothermal(const amrex::Real &z, amrex::Real &p)
Definition: ERF_MetgridUtils.H:548
void init_msfs_from_metgrid(const bool metgrid_debug_msf, amrex::FArrayBox &msfu_fab, amrex::FArrayBox &msfv_fab, amrex::FArrayBox &msfm_fab, const int &flag_msf, amrex::FArrayBox &NC_MSFU_fab, amrex::FArrayBox &NC_MSFV_fab, amrex::FArrayBox &NC_MSFM_fab)
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_setup(char var_type, const bool &exp_interp, const int &orig_npts, const int &new_npts, const int &order, const int &i, const int &j, amrex::Real *orig_x_z, amrex::Real *orig_x_p, amrex::Real *orig_y, amrex::Real *new_x_z, amrex::Real *new_x_p, amrex::Real *new_y)
Definition: ERF_MetgridUtils.H:241
void init_base_state_from_metgrid(const bool use_moisture, const bool metgrid_debug_psfc, const amrex::Real l_rdOcp, const amrex::Box &domain, const amrex::Box &valid_bx, const int &flag_psfc, amrex::FArrayBox &state_fab, amrex::FArrayBox &r_hse_fab, amrex::FArrayBox &p_hse_fab, amrex::FArrayBox &pi_hse_fab, amrex::FArrayBox &th_hse_fab, amrex::FArrayBox &qv_hse_fab, amrex::FArrayBox &z_phys_nd_fab, amrex::FArrayBox &z_phys_cc_fab, const amrex::FArrayBox &NC_psfc_fab, int *base_state_column_failed, const BaseStateParams &bsp)
void init_state_from_metgrid(const int lev, const int itime, const bool use_moisture, const bool interp_theta, const bool metgrid_debug_quiescent, const bool metgrid_debug_isothermal, const bool metgrid_debug_dry, const bool metgrid_basic_linear, const bool metgrid_use_below_sfc, const bool metgrid_use_sfc, const bool metgrid_retain_sfc, const amrex::Real metgrid_proximity, const int metgrid_order, const int metgrid_metgrid_force_sfc_k, const amrex::Real l_rdOcp, amrex::Box &tbxc, amrex::Box &tbxu, amrex::Box &tbxv, amrex::Box &tbxw, amrex::FArrayBox &state_fab, amrex::FArrayBox &x_vel_fab, amrex::FArrayBox &y_vel_fab, amrex::FArrayBox &z_vel_fab, amrex::FArrayBox &z_phys_nd_fab, const amrex::FArrayBox &NC_ght_fab, const amrex::FArrayBox &NC_xvel_fab, const amrex::FArrayBox &NC_yvel_fab, const amrex::FArrayBox &NC_temp_fab, const amrex::FArrayBox &NC_rhum_fab, const amrex::FArrayBox &NC_pres_fab, amrex::FArrayBox &tmp_src_fab, amrex::FArrayBox &tmp_dst_fab, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_xlo, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_xhi, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_ylo, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_yhi, const amrex::Array4< const int > &mask_c_arr, const amrex::Array4< const int > &mask_u_arr, const amrex::Array4< const int > &mask_v_arr)
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void rh_to_mxrat(int i, int j, int k, const amrex::Array4< amrex::Real const > &rhum, const amrex::Array4< amrex::Real const > &temp, const amrex::Array4< amrex::Real const > &pres, int src_indx, const amrex::Array4< amrex::Real > &mxrat)
Definition: ERF_MetgridUtils.H:1042
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real fourth
Definition: ERF_NumericalConstants.H:42
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
@ pres
Definition: ERF_Kessler.H:28
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
@ p
Definition: ERF_WSM6.H:303
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real eps()
Definition: ERF_ShocMoments.cpp:19
real(c_double), parameter svp1
Definition: ERF_module_model_constants.F90:78
real(c_double), parameter svp3
Definition: ERF_module_model_constants.F90:80
real(c_double), parameter svp2
Definition: ERF_module_model_constants.F90:79
real(c_double), parameter svpt0
Definition: ERF_module_model_constants.F90:81
Definition: ERF_BaseStateParams.H:35