1 #ifndef ERF_METGRIDUTIL_H_
2 #define ERF_METGRIDUTIL_H_
12 const amrex::Box& domain,
13 const std::string& fname,
14 std::string& NC_dateTime,
15 amrex::Real& NC_epochTime,
24 amrex::FArrayBox& NC_xvel_fab,
25 amrex::FArrayBox& NC_yvel_fab,
26 amrex::FArrayBox& NC_temp_fab,
27 amrex::FArrayBox& NC_rhum_fab,
28 amrex::FArrayBox& NC_pres_fab,
29 amrex::FArrayBox& NC_ght_fab,
30 amrex::FArrayBox& NC_hgt_fab,
31 amrex::FArrayBox& NC_psfc_fab,
32 amrex::FArrayBox& NC_msfu_fab,
33 amrex::FArrayBox& NC_msfv_fab,
34 amrex::FArrayBox& NC_msfm_fab,
35 amrex::FArrayBox& NC_sst_fab,
36 amrex::FArrayBox& NC_LAT_fab,
37 amrex::FArrayBox& NC_LON_fab,
38 amrex::IArrayBox& NC_lmask_iab,
39 amrex::Real& Latitude,
40 amrex::Real& Longitude,
41 amrex::Geometry& geom);
47 const amrex::Vector<amrex::FArrayBox>& NC_hgt_fab);
51 const bool interp_theta,
52 const bool metgrid_debug_quiescent,
53 const bool metgrid_debug_isothermal,
54 const bool metgrid_debug_dry,
55 const bool metgrid_basic_linear,
56 const bool metgrid_use_below_sfc,
57 const bool metgrid_use_sfc,
58 const bool metgrid_retain_sfc,
59 const amrex::Real metgrid_proximity,
60 const int metgrid_order,
61 const int metgrid_metgrid_force_sfc_k,
62 const amrex::Real l_rdOcp,
66 amrex::FArrayBox& state_fab,
67 amrex::FArrayBox& x_vel_fab,
68 amrex::FArrayBox& y_vel_fab,
69 amrex::FArrayBox& z_vel_fab,
70 amrex::FArrayBox& z_phys_nd_fab,
71 const amrex::Vector<amrex::FArrayBox>& NC_hgt_fab,
72 const amrex::Vector<amrex::FArrayBox>& NC_ght_fab,
73 const amrex::Vector<amrex::FArrayBox>& NC_xvel_fab,
74 const amrex::Vector<amrex::FArrayBox>& NC_yvel_fab,
75 const amrex::Vector<amrex::FArrayBox>& NC_temp_fab,
76 const amrex::Vector<amrex::FArrayBox>& NC_rhum_fab,
77 const amrex::Vector<amrex::FArrayBox>& NC_pres_fab,
78 amrex::FArrayBox& p_interp_fab,
79 amrex::FArrayBox& t_interp_fab,
80 amrex::FArrayBox& theta_fab,
81 amrex::FArrayBox& mxrat_fab,
82 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_xlo,
83 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_xhi,
84 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_ylo,
85 amrex::Vector<amrex::Vector<amrex::FArrayBox>>& fabs_for_bcs_yhi,
86 const amrex::Array4<const int>& mask_c_arr,
87 const amrex::Array4<const int>& mask_u_arr,
88 const amrex::Array4<const int>& mask_v_arr);
92 amrex::FArrayBox& msfu_fab,
93 amrex::FArrayBox& msfv_fab,
94 amrex::FArrayBox& msfm_fab,
96 const amrex::Vector<amrex::FArrayBox>& NC_MSFU_fab,
97 const amrex::Vector<amrex::FArrayBox>& NC_MSFV_fab,
98 const amrex::Vector<amrex::FArrayBox>& NC_MSFM_fab);
102 const bool metgrid_debug_psfc,
103 const amrex::Real l_rdOcp,
104 const amrex::Box& valid_bx,
105 const amrex::Vector<int>& flag_psfc,
106 amrex::FArrayBox& state,
107 amrex::FArrayBox& r_hse_fab,
108 amrex::FArrayBox& p_hse_fab,
109 amrex::FArrayBox& pi_hse_fab,
110 amrex::FArrayBox& th_hse_fab,
111 amrex::FArrayBox& z_phys_cc_fab,
112 const amrex::Vector<amrex::FArrayBox>& NC_psfc_fab);
128 amrex::Real Px = 0.0;
129 for (
int i=0; i <= order; i++) {
132 for (
int k=0; k <= order; k++) {
133 if (k == i)
continue;
148 const bool& exp_interp,
152 amrex::Real* orig_x_z,
153 amrex::Real* orig_x_p,
155 amrex::Real* new_x_z,
156 amrex::Real* new_x_p,
159 if (order < 1) amrex::Abort(
"metgrid initialization, we cannot go lower order than linear");
161 amrex::Real CRC_const1 = 11880.516;
162 amrex::Real CRC_const2 = 0.1902632;
163 amrex::Real CRC_const3 = 0.0065;
167 #ifndef AMREX_USE_GPU
171 for (
int new_k=0; new_k < new_n; new_k++) {
172 #ifndef AMREX_USE_GPU
173 if (debug) amrex::Print() <<
"new_k=" << new_k;
176 bool extrapolating =
true;
178 for (
int ko=0; ko < orig_n-1; ko++) {
179 amrex::Real a = new_x_z[new_k]-orig_x_z[ko];
180 amrex::Real b = new_x_z[new_k]-orig_x_z[ko+1];
184 extrapolating =
false;
190 if (var_type ==
'T') {
193 amrex::Real depth_of_extrap_in_p = new_x_p[new_k]-orig_x_p[1];
194 amrex::Real avg_of_extrap_p = 0.5*(new_x_p[new_k]+orig_x_p[1]);
195 amrex::Real temp_extrap_starting_point = orig_y[1]*std::pow(orig_x_p[1]/100000.0,
R_d/
Cp_d);
196 amrex::Real dZdP = CRC_const1*CRC_const2*std::pow(avg_of_extrap_p/100.0, CRC_const2-1.0);
197 amrex::Real dZ = dZdP*(depth_of_extrap_in_p/100.0);
198 amrex::Real dT = dZ*CRC_const3;
199 new_y[new_k] = (temp_extrap_starting_point+dT)*std::pow(100000.0/new_x_p[new_k],
R_d/
Cp_d);
203 new_y[new_k] = orig_y[1];
209 if ((kl-((order+1)/2-1) >= 0) && (kr+((order+1)/2-1) <= orig_n-1)) {
210 int ksta = kl-((order/2)-1);
211 int kend = ksta+order;
213 amrex::GpuArray<amrex::Real,2> orig_x_sub;
214 amrex::Real* orig_x_sub_p = orig_x_sub.data();
216 new_x = new_x_p[new_k];
217 orig_x_sub_p[0] = orig_x_p[ksta];
218 orig_x_sub_p[1] = orig_x_p[kend];
220 new_x = new_x_z[new_k];
221 orig_x_sub_p[0] = orig_x_z[ksta];
222 orig_x_sub_p[1] = orig_x_z[kend];
224 amrex::GpuArray<amrex::Real,2> orig_y_sub;
225 amrex::Real* orig_y_sub_p = orig_y_sub.data();
226 orig_y_sub_p[0] = orig_y[ksta];
227 orig_y_sub_p[1] = orig_y[kend];
228 lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
230 amrex::Abort(
"metgrid initialization, lost in lagrange_setup (odd order)");
232 }
else if ((order%2 == 0) && (new_k >= 1+vboundb) && (new_k < new_n-vboundt)) {
233 if ((kl-(order/2) >= 0) && (kr+order/2 <= orig_n-1)) {
234 amrex::Real new_y_l, new_y_r;
236 int ksta = kl-(order/2-1);
237 int kend = ksta+order;
238 #ifndef AMREX_USE_GPU
239 int ksize = kend-ksta;
240 if (debug) amrex::Print() <<
" (1a) 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;
243 amrex::GpuArray<amrex::Real,256> orig_x_sub;
244 amrex::GpuArray<amrex::Real,256> orig_y_sub;
245 amrex::Real* orig_x_sub_p = orig_x_sub.data();
246 amrex::Real* orig_y_sub_p = orig_y_sub.data();
248 new_x = new_x_p[new_k];
249 for (
int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
251 new_x = new_x_z[new_k];
252 for (
int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
254 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
255 #ifndef AMREX_USE_GPU
257 amrex::Print() <<
" orig_x_sub = [";
258 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_x_sub_p[k];
259 amrex::Print() <<
"]" << std::endl;
260 amrex::Print() <<
" orig_y_sub = [";
261 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_y_sub_p[k];
262 amrex::Print() <<
"]" << std::endl;
268 int ksta = kl-order/2;
269 int kend = ksta+order;
270 #ifndef AMREX_USE_GPU
271 int ksize = kend-ksta;
272 if (debug) amrex::Print() <<
"new_k=" << new_k <<
" (1b) 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;
275 amrex::GpuArray<amrex::Real,256> orig_x_sub;
276 amrex::GpuArray<amrex::Real,256> orig_y_sub;
277 amrex::Real* orig_x_sub_p = orig_x_sub.data();
278 amrex::Real* orig_y_sub_p = orig_y_sub.data();
280 new_x = new_x_p[new_k];
281 for (
int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
283 new_x = new_x_z[new_k];
284 for (
int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
286 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
287 #ifndef AMREX_USE_GPU
289 amrex::Print() <<
" orig_x_sub = [";
290 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_x_sub_p[k];
291 amrex::Print() <<
"]" << std::endl;
292 amrex::Print() <<
" orig_y_sub = [";
293 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_y_sub_p[k];
294 amrex::Print() <<
"]" << std::endl;
299 new_y[new_k] = 0.5*(new_y_l+new_y_r);
300 #ifndef AMREX_USE_GPU
301 if (debug) amrex::Print() <<
" new_y=" << new_y[new_k] << std::endl;
303 }
else if ((kl-(order/2-1) >= 0) && (kr+order/2 <= orig_n-1)) {
304 int ksta = kl-(order/2-1);
305 int kend = ksta+order;
306 #ifndef AMREX_USE_GPU
307 int ksize = kend-ksta;
308 if (debug) amrex::Print() <<
" (2) 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;
311 amrex::GpuArray<amrex::Real,256> orig_x_sub;
312 amrex::GpuArray<amrex::Real,256> orig_y_sub;
313 amrex::Real* orig_x_sub_p = orig_x_sub.data();
314 amrex::Real* orig_y_sub_p = orig_y_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 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
323 #ifndef AMREX_USE_GPU
325 amrex::Print() <<
" orig_x_sub = [";
326 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_x_sub_p[k];
327 amrex::Print() <<
"]" << std::endl;
328 amrex::Print() <<
" orig_y_sub = [";
329 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_y_sub_p[k];
330 amrex::Print() <<
"]" << std::endl;
333 lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
334 #ifndef AMREX_USE_GPU
335 if (debug) amrex::Print() <<
" new_y=" << new_y[new_k] << std::endl;
337 }
else if ((kl-order/2 >= 0) && (kr+(order/2-1) <= orig_n-1)) {
338 int ksta = kl-order/2;
339 int kend = ksta+order;
340 #ifndef AMREX_USE_GPU
341 int ksize = kend-ksta;
342 if (debug) amrex::Print() <<
" (3) 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;
345 amrex::GpuArray<amrex::Real,256> orig_x_sub;
346 amrex::GpuArray<amrex::Real,256> orig_y_sub;
347 amrex::Real* orig_x_sub_p = orig_x_sub.data();
348 amrex::Real* orig_y_sub_p = orig_y_sub.data();
350 new_x = new_x_p[new_k];
351 for (
int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
353 new_x = new_x_z[new_k];
354 for (
int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
356 for (
int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
357 #ifndef AMREX_USE_GPU
359 amrex::Print() <<
" orig_x_sub = [";
360 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_x_sub_p[k];
361 amrex::Print() <<
"]" << std::endl;
362 amrex::Print() <<
" orig_y_sub = [";
363 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_y_sub_p[k];
364 amrex::Print() <<
"]" << std::endl;
367 lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
368 #ifndef AMREX_USE_GPU
369 if (debug) amrex::Print() <<
" new_y=" << new_y[new_k] << std::endl;
372 amrex::Abort(
"metgrid initialization, lost in lagrange_setup (even order)");
378 #ifndef AMREX_USE_GPU
379 int ksize = kend-ksta;
380 if (debug) amrex::Print() <<
" (4) 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;
383 amrex::GpuArray<amrex::Real,256> orig_x_sub;
384 amrex::GpuArray<amrex::Real,256> 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 = [";
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 = [";
401 for (
int k=0; k < ksize; k++) amrex::Print() <<
" " << orig_y_sub_p[k];
402 amrex::Print() <<
"]" << std::endl;
406 #ifndef AMREX_USE_GPU
407 if (debug) amrex::Print() <<
" new_y=" << new_y[new_k] << std::endl;
426 const bool& metgrid_use_sfc,
427 const bool& exp_interp,
428 const bool& metgrid_retain_sfc,
429 const amrex::Real& metgrid_proximity,
430 const int& metgrid_order,
431 const int& metgrid_force_sfc_k,
438 const amrex::Array4<amrex::Real const>& orig_z_full,
439 const amrex::Array4<amrex::Real const>& orig_data,
440 const amrex::Array4<amrex::Real const>& new_z_full,
441 const amrex::Array4<amrex::Real>& new_data_full,
442 const bool& update_bc_data,
443 const amrex::Array4<amrex::Real>& bc_data_xlo,
444 const amrex::Array4<amrex::Real>& bc_data_xhi,
445 const amrex::Array4<amrex::Real>& bc_data_ylo,
446 const amrex::Array4<amrex::Real>& bc_data_yhi,
447 const amrex::Box& bx_xlo,
448 const amrex::Box& bx_xhi,
449 const amrex::Box& bx_ylo,
450 const amrex::Box& bx_yhi,
451 const amrex::Array4<const int>& mask)
457 int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
458 int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
459 int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z;
460 int kmax_new = amrex::ubound(amrex::Box(new_z_full)).z;
462 AMREX_ASSERT(kmax_orig < 256);
463 AMREX_ASSERT(kmax_new < 256);
465 amrex::GpuArray<amrex::Real,256> new_z;
466 amrex::GpuArray<amrex::Real,256> new_p;
467 amrex::GpuArray<amrex::Real,256> new_data;
468 amrex::Real* new_z_p = new_z.data();
469 amrex::Real* new_p_p = new_p.data();
470 amrex::Real* new_data_p = new_data.data();
471 for (
int k=0; k < kmax_new; k++) {
473 new_z_p[k] = 0.25*(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));
474 }
else if (stag ==
'Y') {
475 new_z_p[k] = 0.25*(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));
476 }
else if (stag ==
'M') {
477 new_z_p[k] = 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 )+
478 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));
483 amrex::GpuArray<amrex::Real,256> orig_z;
484 amrex::Real* orig_z_p = orig_z.data();
485 for (
int k=0; k < kmax_orig; k++) {
487 orig_z_p[k] = orig_z_full(i,j,k);
488 }
else if (stag ==
'X') {
490 orig_z_p[k] = orig_z_full(i,j,k);
491 }
else if (i == imax_orig) {
492 orig_z_p[k] = orig_z_full(imax_orig-1,j,k);
494 orig_z_p[k] = 0.5*(orig_z_full(i,j,k)+orig_z_full(i-1,j,k));
496 }
else if (stag ==
'Y') {
498 orig_z_p[k] = orig_z_full(i,j,k);
499 }
else if (j == jmax_orig) {
500 orig_z_p[k] = orig_z_full(i,jmax_orig-1,k);
502 orig_z_p[k] = 0.5*(orig_z_full(i,j,k)+orig_z_full(i,j-1,k));
508 bool flip_data_required =
false;
509 if (orig_z[1] > orig_z[kmax_orig-1]) flip_data_required =
true;
510 if (flip_data_required) amrex::Abort(
"metgrid initialization flip_data_required. Not yet implemented.");
516 for (
int k=1; k < kmax_orig; k++) {
517 if (orig_z_p[k] > orig_z_p[0]) {
527 amrex::ignore_unused(zap,zap_above);
528 amrex::GpuArray<amrex::Real,256> ordered_z;
529 amrex::GpuArray<amrex::Real,256> ordered_data;
530 amrex::Real* ordered_z_p = ordered_z.data();
531 amrex::Real* ordered_data_p = ordered_data.data();
532 if (k_above_sfc > 1) {
538 for (
int k=1; k < k_above_sfc; k++) {
539 ordered_z_p[count] = orig_z_p[k];
540 ordered_data_p[count] = orig_data(i,j,k);
554 if (Pl-Pu < metgrid_proximity) {
561 ordered_z_p[count] = orig_z_p[0];
562 ordered_data_p[count] = orig_data(i,j,0);
567 int knext = k_above_sfc;
573 if (metgrid_force_sfc_k > 0) {
574 for (
int k=k_above_sfc; k < kmax_orig; k++) {
575 if (orig_z_p[k] > new_z_p[metgrid_force_sfc_k-1]) {
589 if (Pl-Pu < metgrid_proximity) {
596 for (
int k=knext; k < kmax_orig; k++) {
597 ordered_z_p[count] = orig_z_p[k];
598 ordered_data_p[count] = orig_data(i,j,k);
607 ordered_z_p[0] = orig_z[0];
608 ordered_data_p[0] = orig_data(i,j,0);
614 if (metgrid_force_sfc_k > 0) {
615 for (
int k=knext; k < kmax_orig; k++) {
616 if (orig_z_p[k] > new_z_p[metgrid_force_sfc_k]) {
628 for (
int k=knext; k < kmax_orig; k++) {
632 if (Pl-Pu < metgrid_proximity) {
637 ordered_z_p[count] = orig_z_p[k];
638 ordered_data_p[count] = orig_data(i,j,k);
644 int ksta(0), kend(0);
645 if (metgrid_use_below_sfc && metgrid_use_sfc) {
648 kend = ksta+kend_order-1;
649 }
else if (metgrid_use_below_sfc && !metgrid_use_sfc) {
652 for (
int k=0; k < kmax_orig; k++) {
653 if (ordered_z_p[k] == orig_z_p[0]) {
658 for (
int k=ksfc; k < kmax_orig-1; k++) {
659 ordered_z_p[k] = ordered_z_p[k+1];
660 ordered_data_p[k] = ordered_data_p[k+1];
663 kend = ksta+kend_order-2;
664 }
else if (!metgrid_use_below_sfc && metgrid_use_sfc) {
666 int kcount = k_above_sfc-1-zap_below;
668 for (
int k=0; k < kmax_orig; k++) {
669 if (ordered_z_p[kcount] == orig_z_p[k]) {
674 ksta = k_above_sfc-1-zap_below;
678 amrex::Abort(
"metgrid initialization, !use_levels below_ground && !metgrid_use_sfc");
694 amrex::GpuArray<amrex::Real,256> ordered_p;
695 amrex::Real* ordered_p_p = ordered_p.data();
696 for (
int k=0; k < kend_order; k++) {
702 amrex::GpuArray<amrex::Real,256> final_z;
703 amrex::GpuArray<amrex::Real,256> final_p;
704 amrex::GpuArray<amrex::Real,256> final_data;
705 amrex::Real* final_z_p = final_z.data();
706 amrex::Real* final_p_p = final_p.data();
707 amrex::Real* final_data_p = final_data.data();
708 final_z_p[0] = ordered_z[ksta];
709 final_p_p[0] = ordered_p[ksta];
710 final_data_p[0] = ordered_data[ksta];
711 for (
int k=ksta+1; k <= kend; k++) {
712 if ((final_p_p[new_n]-ordered_p_p[k]) < metgrid_proximity) {
716 final_z_p[new_n] = ordered_z_p[k];
717 final_p_p[new_n] = ordered_p_p[k];
718 final_data_p[new_n] = ordered_data_p[k];
738 if (metgrid_retain_sfc) new_data[0] = ordered_data[0];
741 for (
int k=0; k < kmax_new; k++) {
742 if (mask(i,j,k) && update_bc_data && bx_xlo.contains(i,j,k)) bc_data_xlo(i,j,k,0) = new_data[k];
743 if (mask(i,j,k) && update_bc_data && bx_xhi.contains(i,j,k)) bc_data_xhi(i,j,k,0) = new_data[k];
744 if (mask(i,j,k) && update_bc_data && bx_ylo.contains(i,j,k)) bc_data_ylo(i,j,k,0) = new_data[k];
745 if (mask(i,j,k) && update_bc_data && bx_yhi.contains(i,j,k)) bc_data_yhi(i,j,k,0) = new_data[k];
746 if (itime == 0) new_data_full(i,j,k,src_comp) = new_data[k];
759 const amrex::Array4<amrex::Real const>& orig_z,
760 const amrex::Array4<amrex::Real const>& orig_data,
761 const amrex::Array4<amrex::Real const>& new_z)
764 int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
765 int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
766 int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z;
770 z = 0.25*(new_z(i,j,k)+new_z(i,j+1,k)+new_z(i,j,k+1)+new_z(i,j+1,k+1));
772 else if (stag ==
'Y') {
773 z = 0.25*(new_z(i,j,k)+new_z(i+1,j,k)+new_z(i,j,k+1)+new_z(i+1,j,k+1));
775 else if (stag ==
'M') {
776 z = 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 )+
777 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));
783 amrex::Real dzlow = 1.0e12;
784 amrex::Real dzhi0 = -1.0e12;
785 for (
int kk = 0; kk < kmax_orig; kk++) {
786 amrex::Real orig_z_stag = 0.0;
788 orig_z_stag = orig_z(i,j,kk);
792 orig_z_stag = orig_z(i,j,kk);
794 else if (i == imax_orig) {
795 orig_z_stag = orig_z(imax_orig-1,j,kk);
798 orig_z_stag = 0.5*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
801 else if (stag ==
'Y') {
803 orig_z_stag = orig_z(i,j,kk);
805 else if (j == jmax_orig) {
806 orig_z_stag = orig_z(i,jmax_orig-1,kk);
809 orig_z_stag = 0.5*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
813 amrex::Real dz =
z - orig_z_stag;
814 if ((dz < 0.0) && (dz > dzhi0)) {
819 if ((dz >= 0.0) && (dz < dzlow)) {
829 amrex::Real dzhi1 = -1.0e12;
830 for (
int kk = 0; kk < kmax_orig; kk++) {
831 amrex::Real orig_z_stag = 0.0;
833 orig_z_stag = orig_z(i,j,kk);
835 else if (stag ==
'X') {
837 orig_z_stag = orig_z(i,j,kk);
839 else if (i == imax_orig) {
840 orig_z_stag = orig_z(imax_orig-1,j,kk);
843 orig_z_stag = 0.5*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
846 else if (stag ==
'Y') {
848 orig_z_stag = orig_z(i,j,kk);
850 else if (j == jmax_orig) {
851 orig_z_stag = orig_z(i,jmax_orig-1,kk);
854 orig_z_stag = 0.5*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
857 amrex::Real dz =
z - orig_z_stag;
858 if ((dz < 0.0) && (dz > dzhi1) && (kk != khi0)) {
864 amrex::Real y0 = orig_data(i,j,khi0,src_comp);
865 amrex::Real y1 = orig_data(i,j,khi1,src_comp);
866 return ( y0-(y1-y0)/(z1-z0)*(z0-
z) );
869 }
else if (khi0 == -1) {
873 z0 = orig_z(i,j,khi0);
875 else if (stag ==
'X') {
877 z0 = orig_z(i,j,khi0);
879 else if (i == imax_orig) {
880 z0 = orig_z(imax_orig-1,j,khi0);
883 z0 = 0.5*(orig_z(i,j,khi0)+orig_z(i-1,j,khi0));
886 else if (stag ==
'Y') {
888 z0 = orig_z(i,j,khi0);
890 else if (j == jmax_orig) {
891 z0 = orig_z(i,jmax_orig-1,khi0);
894 z0 = 0.5*(orig_z(i,j,khi0)+orig_z(i,j-1,khi0));
897 amrex::Real y0 = orig_data(i,j,khi0,src_comp);
898 amrex::Real y1 = orig_data(i,j,khi1,src_comp);
899 return ( y0+(y1-y0)/(z1-z0)*(
z-z0) );
902 amrex::Real y0 = orig_data(i,j,klow,src_comp);
903 amrex::Real y1 = orig_data(i,j,khi0,src_comp);
904 return ( y0+(y1-y0)/(z1-z0)*(
z-z0) );
915 const amrex::Array4<amrex::Real const>& rhum,
916 const amrex::Array4<amrex::Real const>& temp,
917 const amrex::Array4<amrex::Real const>&
pres,
918 const amrex::Array4<amrex::Real>& mxrat)
920 amrex::Real qv_max_p_safe = 10000.0;
921 amrex::Real qv_max_flag = 1.0e-5;
922 amrex::Real qv_max_value = 3.0e-6;
923 amrex::Real qv_min_p_safe = 110000.0;
924 amrex::Real qv_min_flag = 1.0e-6;
925 amrex::Real qv_min_value = 1.0e-6;
926 amrex::Real eps = 0.622;
927 amrex::Real svp1 = 0.6112;
928 amrex::Real svp2 = 17.67;
929 amrex::Real svp3 = 29.65;
930 amrex::Real svpt0 = 273.15;
932 if (temp(i,j,k) != 0.0) {
933 amrex::Real es=0.01*rhum(i,j,k)*svp1*10.0*exp(svp2*(temp(i,j,k)-svpt0)/(temp(i,j,k)-svp3));
934 if (es >=
pres(i,j,k)/100.0) {
936 mxrat(i,j,k) = std::pow(10.0, -6);
939 mxrat(i,j,k) = amrex::max(eps*es/(
pres(i,j,k)/100.0-es), 1.0e-6);
945 mxrat(i,j,k) = 1.0e-6;
953 if (
pres(i,j,k) < qv_max_p_safe) {
954 if (mxrat(i,j,k) > qv_max_flag) {
955 mxrat(i,j,k) = qv_max_value;
958 if (
pres(i,j,k) < qv_min_p_safe) {
959 if (mxrat(i,j,k) < qv_min_flag) {
960 mxrat(i,j,k) = qv_min_value;
constexpr amrex::Real Cp_d
Definition: ERF_Constants.H:12
constexpr amrex::Real p_0
Definition: ERF_Constants.H:18
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:21
constexpr amrex::Real R_d
Definition: ERF_Constants.H:10
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 &src_comp, const int &itime, 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, const bool &update_bc_data, const amrex::Array4< amrex::Real > &bc_data_xlo, const amrex::Array4< amrex::Real > &bc_data_xhi, const amrex::Array4< amrex::Real > &bc_data_ylo, const amrex::Array4< amrex::Real > &bc_data_yhi, const amrex::Box &bx_xlo, const amrex::Box &bx_xhi, const amrex::Box &bx_ylo, const amrex::Box &bx_yhi, const amrex::Array4< const int > &mask)
Definition: ERF_MetgridUtils.H:425
void init_base_state_from_metgrid(const bool use_moisture, const bool metgrid_debug_psfc, const amrex::Real l_rdOcp, const amrex::Box &valid_bx, const amrex::Vector< int > &flag_psfc, amrex::FArrayBox &state, amrex::FArrayBox &r_hse_fab, amrex::FArrayBox &p_hse_fab, amrex::FArrayBox &pi_hse_fab, amrex::FArrayBox &th_hse_fab, amrex::FArrayBox &z_phys_cc_fab, const amrex::Vector< amrex::FArrayBox > &NC_psfc_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:117
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:754
void read_from_metgrid(int lev, const amrex::Box &domain, const std::string &fname, std::string &NC_dateTime, amrex::Real &NC_epochTime, int &flag_psfc, int &flag_msf, int &flag_sst, 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_LAT_fab, amrex::FArrayBox &NC_LON_fab, amrex::IArrayBox &NC_lmask_iab, amrex::Real &Latitude, amrex::Real &Longitude, amrex::Geometry &geom)
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void calc_p_isothermal(const amrex::Real &z, amrex::Real &p)
Definition: ERF_MetgridUtils.H:416
void init_state_from_metgrid(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::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::Vector< amrex::FArrayBox > &NC_hgt_fab, const amrex::Vector< amrex::FArrayBox > &NC_ght_fab, const amrex::Vector< amrex::FArrayBox > &NC_xvel_fab, const amrex::Vector< amrex::FArrayBox > &NC_yvel_fab, const amrex::Vector< amrex::FArrayBox > &NC_temp_fab, const amrex::Vector< amrex::FArrayBox > &NC_rhum_fab, const amrex::Vector< amrex::FArrayBox > &NC_pres_fab, amrex::FArrayBox &p_interp_fab, amrex::FArrayBox &t_interp_fab, amrex::FArrayBox &theta_fab, amrex::FArrayBox &mxrat_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, const amrex::Array4< amrex::Real > &mxrat)
Definition: ERF_MetgridUtils.H:912
void init_terrain_from_metgrid(amrex::FArrayBox &z_phys_nd_fab, const amrex::Vector< amrex::FArrayBox > &NC_hgt_fab)
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_setup(char var_type, const bool &exp_interp, const int &orig_n, const int &new_n, const int &order, 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:147
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, const amrex::Vector< amrex::FArrayBox > &NC_MSFU_fab, const amrex::Vector< amrex::FArrayBox > &NC_MSFV_fab, const amrex::Vector< amrex::FArrayBox > &NC_MSFM_fab)
@ pres
Definition: ERF_Kessler.H:33