4 #ifndef ERF_METGRIDUTIL_H_
5 #define ERF_METGRIDUTIL_H_
7 #include <AMReX_Math.H>
28 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
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)
39 pressure = std::numeric_limits<amrex::Real>::quiet_NaN();
41 pressure = amrex::Real(100000.0);
45 if (!amrex::Math::isfinite(file_psfc) || file_psfc <= amrex::Real(0.0)) {
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)) {
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)) {
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)) {
78 const amrex::Box& domain,
79 const std::string& fname,
80 std::string& NC_dateTime,
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);
114 amrex::Geometry& geom,
115 amrex::MultiFab* z_phys_nd,
116 amrex::FArrayBox& NC_hgt_fab);
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,
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);
167 amrex::FArrayBox& msfu_fab,
168 amrex::FArrayBox& msfv_fab,
169 amrex::FArrayBox& msfm_fab,
171 amrex::FArrayBox& NC_MSFU_fab,
172 amrex::FArrayBox& NC_MSFV_fab,
173 amrex::FArrayBox& NC_MSFM_fab);
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,
215 amrex::Real Px =
zero;
216 for (
int i=0; i <= order; i++) {
219 for (
int k=0; k <= order; k++) {
220 if (k == i)
continue;
242 const bool& exp_interp,
243 const int& orig_npts,
248 amrex::Real* orig_x_z,
249 amrex::Real* orig_x_p,
251 amrex::Real* new_x_z,
252 amrex::Real* new_x_p,
256 amrex::Real CRC_const1 = amrex::Real(11880.516);
257 amrex::Real CRC_const2 = amrex::Real(0.1902632);
258 amrex::Real CRC_const3 = amrex::Real(0.0065);
260 amrex::ignore_unused(i,j);
261 #ifndef AMREX_USE_GPU
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;
271 bool extrapolating =
true;
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];
279 extrapolating =
false;
285 if (var_type ==
'T') {
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);
298 new_y[new_k] = orig_y[0];
304 if ((kl-((order+1)/2-1) >= 0) && (kr+((order+1)/2-1) <= orig_npts-1)) {
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;
313 amrex::GpuArray<amrex::Real,9> orig_x_sub;
314 amrex::Real* orig_x_sub_p = orig_x_sub.data();
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]; }
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]; }
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
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;
335 lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
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;
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();
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]; }
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]; }
357 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
358 #ifndef AMREX_USE_GPU
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;
370 }
else if (order%2 == 0) {
371 if ((kl-(order/2) >= 0) && (kr+order/2 <= orig_npts-1)) {
374 amrex::Real new_y_l, new_y_r;
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;
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();
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]; }
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]; }
394 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
395 #ifndef AMREX_USE_GPU
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;
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;
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();
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]; }
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]; }
426 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
427 #ifndef AMREX_USE_GPU
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;
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;
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();
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]; }
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]; }
459 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
460 #ifndef AMREX_USE_GPU
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;
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;
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();
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]; }
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]; }
490 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
491 #ifndef AMREX_USE_GPU
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;
501 lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
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;
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();
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]; }
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]; }
522 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
523 #ifndef AMREX_USE_GPU
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;
536 #ifndef AMREX_USE_GPU
537 if (debug) amrex::Print() <<
" new_y[" << new_k <<
"]=" << new_y[new_k] << std::endl;
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,
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)
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;
589 int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z + 1;
590 int kmax_new = kmax + 1;
592 AMREX_ASSERT(kmax_orig <= 256);
593 AMREX_ASSERT(kmax_new < 256);
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++) {
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));
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++) {
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);
624 orig_z_p[k] =
myhalf*(orig_z_full(i,j,k)+orig_z_full(i-1,j,k));
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);
632 orig_z_p[k] =
myhalf*(orig_z_full(i,j,k)+orig_z_full(i,j-1,k));
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.");
646 for (
int k=1; k < kmax_orig; k++) {
647 if (orig_z_p[k] > orig_z_p[0]) {
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) {
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);
680 if (Pl-Pu < metgrid_proximity) {
685 ordered_z_p[
count] = orig_z_p[0];
686 ordered_data_p[
count] = orig_data(i,j,0,src_comp);
691 int knext = k_above_sfc;
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]) {
710 if (Pl-Pu < metgrid_proximity) {
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);
726 ordered_z_p[0] = orig_z_p[0];
727 ordered_data_p[0] = orig_data(i,j,0,src_comp);
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]) {
746 for (
int k=knext; k < kmax_orig; k++) {
750 if (Pl-Pu < metgrid_proximity) {
753 ordered_z_p[
count] = orig_z_p[k];
754 ordered_data_p[
count] = orig_data(i,j,k,src_comp);
760 int ksta(0), kend(0);
761 if (metgrid_use_below_sfc && metgrid_use_sfc) {
765 }
else if (metgrid_use_below_sfc && !metgrid_use_sfc) {
768 for (
int k=0; k < kmax_orig; k++) {
769 if (ordered_z_p[k] == orig_z_p[0]) {
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];
780 }
else if (!metgrid_use_below_sfc && metgrid_use_sfc) {
783 for (
int k=0; k < kend_order; k++) {
784 if (ordered_z_p[k] == orig_z_p[0]) {
793 amrex::Abort(
"metgrid initialization, !use_levels below_ground && !metgrid_use_sfc");
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++) {
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++) {
829 if ((final_p_p[kfinal]-ordered_p_p[k]) < metgrid_proximity)
continue;
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];
854 if (metgrid_retain_sfc) new_data[0] = orig_data(i,j,0,src_comp);
857 for (
int k=0; k < kmax_new; k++) {
858 new_data_full(i,j,k,dst_comp) = new_data[k];
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)
879 int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
880 int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
882 int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z + 1;
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));
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));
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));
898 amrex::Real
z0 = std::numeric_limits<amrex::Real>::quiet_NaN();
899 amrex::Real z1 = std::numeric_limits<amrex::Real>::quiet_NaN();
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;
907 orig_z_stag = orig_z(i,j,kk);
911 orig_z_stag = orig_z(i,j,kk);
913 else if (i == imax_orig) {
914 orig_z_stag = orig_z(imax_orig-1,j,kk);
917 orig_z_stag =
myhalf*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
920 else if (stag ==
'Y') {
922 orig_z_stag = orig_z(i,j,kk);
924 else if (j == jmax_orig) {
925 orig_z_stag = orig_z(i,jmax_orig-1,kk);
928 orig_z_stag =
myhalf*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
932 amrex::Real
dz =
z - orig_z_stag;
952 amrex::Real dzhi1 = -amrex::Real(1.0e12);
953 for (
int kk = 0; kk < kmax_orig; kk++) {
954 amrex::Real orig_z_stag =
zero;
956 orig_z_stag = orig_z(i,j,kk);
958 else if (stag ==
'X') {
960 orig_z_stag = orig_z(i,j,kk);
962 else if (i == imax_orig) {
963 orig_z_stag = orig_z(imax_orig-1,j,kk);
966 orig_z_stag =
myhalf*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
969 else if (stag ==
'Y') {
971 orig_z_stag = orig_z(i,j,kk);
973 else if (j == jmax_orig) {
974 orig_z_stag = orig_z(i,jmax_orig-1,kk);
977 orig_z_stag =
myhalf*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
980 amrex::Real
dz =
z - orig_z_stag;
981 if ((
dz <
zero) && (
dz > dzhi1) && (kk != khi0)) {
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) );
992 }
else if (khi0 == -1) {
1000 z0 = orig_z(i,j,khi0);
1002 else if (stag ==
'X') {
1004 z0 = orig_z(i,j,khi0);
1006 else if (i == imax_orig) {
1007 z0 = orig_z(imax_orig-1,j,khi0);
1010 z0 =
myhalf*(orig_z(i,j,khi0)+orig_z(i-1,j,khi0));
1013 else if (stag ==
'Y') {
1015 z0 = orig_z(i,j,khi0);
1017 else if (j == jmax_orig) {
1018 z0 = orig_z(i,jmax_orig-1,khi0);
1021 z0 =
myhalf*(orig_z(i,j,khi0)+orig_z(i,j-1,khi0));
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) );
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) );
1045 const amrex::Array4<amrex::Real const>& rhum,
1046 const amrex::Array4<amrex::Real const>& temp,
1047 const amrex::Array4<amrex::Real const>&
pres,
1049 const amrex::Array4<amrex::Real>& mxrat)
1051 amrex::Real qv_max_p_safe = amrex::Real(10000.0);
1052 amrex::Real qv_max_flag = amrex::Real(1.0e-5);
1053 amrex::Real qv_max_value = amrex::Real(3.0e-6);
1054 amrex::Real qv_min_p_safe = amrex::Real(110000.0);
1055 amrex::Real qv_min_flag = amrex::Real(1.0e-6);
1056 amrex::Real qv_min_value = amrex::Real(1.0e-6);
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);
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)) {
1067 mxrat(i,j,k,src_indx) = amrex::Math::powi<-6>(amrex::Real(10.0));
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));
1076 mxrat(i,j,k,src_indx) = amrex::Real(1.0e-6);
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;
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;
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