234 int terrain_smoothing = 0;
235 pp.query(
"terrain_smoothing", terrain_smoothing);
237 if (lev > 0 && terrain_smoothing != 0) {
238 Abort(
"Must use terrain_smoothing = 0 when doing multilevel");
242 int ngrow = z_phys_nd.nGrow();
243 IntVect ngrowVect = z_phys_nd.nGrowVect();
245 const Box& domain = geom.Domain();
246 int domlo_x = domain.smallEnd(0);
int domhi_x = domain.bigEnd(0) + 1;
247 int domlo_y = domain.smallEnd(1);
int domhi_y = domain.bigEnd(1) + 1;
248 int domlo_z = domain.smallEnd(2);
int domhi_z = domain.bigEnd(2) + 1;
256 int nz = z_levels_h.size();
257 Real z_top = z_levels_h[nz-1];
259 Gpu::DeviceVector<Real> z_levels_d;
260 z_levels_d.resize(nz);
261 Gpu::copy(Gpu::hostToDevice, z_levels_h.begin(), z_levels_h.end(), z_levels_d.begin());
263 switch(terrain_smoothing) {
266 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
269 const Box& bx = mfi.validbox();
271 int k0 = bx.smallEnd()[2];
274 Box gbx = mfi.growntilebox(ngrowVect);
276 if (bx.smallEnd(2) == domlo_z) {
277 gbx.setSmall(2,domlo_z);
286 if (bx.bigEnd(2) == domhi_z) {
287 gbx.setBig(2,domhi_z);
290 if (gbx.bigEnd(2) > domhi_z) {
291 gbx.setBig(2,domhi_z);
295 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
296 auto const& z_lev = z_levels_d.data();
302 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
304 int ii = amrex::max(amrex::min(i,imax),imin);
305 int jj = amrex::max(amrex::min(j,jmax),jmin);
313 Real z_sfc = z_arr(ii,jj,k0);
314 Real z_lev_sfc = z_lev[k0];
316 z_arr(i,j,k) = ( (z_sfc - z_lev_sfc) * z_top +
317 (z_top - z_sfc ) *
z ) / (z_top - z_lev_sfc);
321 z_phys_nd.FillBoundary(geom.periodicity());
323 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
326 const Box& bx = mfi.validbox();
327 Box gbx = mfi.growntilebox(ngrowVect);
329 int k0 = bx.smallEnd()[2];
333 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
336 ParallelFor(makeSlab(gbx,2,0), [=] AMREX_GPU_DEVICE (
int i,
int j,
int)
338 z_arr(i,j,-1) = 2.0*z_arr(i,j,0) - z_arr(i,j,1);
348 MultiFab h_mf( z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, ngrow+1);
349 MultiFab h_mf_old(z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, ngrow+1);
357 BoxList bl2d = h_mf.boxArray().boxList();
358 for (
auto& b : bl2d) {
361 BoxArray ba2d(std::move(bl2d));
362 mf2d = MultiFab(ba2d, h_mf.DistributionMap(), 1, ngrow, MFInfo().SetAlloc(
false));
366 MultiArray4<Real>
const& ma_h_s = h_mf.arrays();
367 MultiArray4<Real>
const& ma_h_s_old = h_mf_old.arrays();
368 MultiArray4<Real>
const& ma_z_phys = z_phys_nd.arrays();
374 h_m = ParReduce(TypeList<ReduceOpMax>{}, TypeList<Real>{}, mf2d, IntVect(ngrow,ngrow,0),
375 [=] AMREX_GPU_DEVICE (
int box_no,
int i,
int j,
int) noexcept
379 const auto & h = ma_h_s[box_no];
380 const auto & z_arr = ma_z_phys[box_no];
382 int ii = amrex::max(amrex::min(i,imax),imin);
383 int jj = amrex::max(amrex::min(j,jmax),jmin);
386 z_arr(i,j,k0) = z_arr(ii,jj,k0);
389 h(i,j,k0) = z_arr(i,j,k0);
392 return { z_arr(i,j,k0) };
395 if (h_m < std::numeric_limits<Real>::epsilon()) h_m = 1e-16;
398 h_mf.FillBoundary(geom.periodicity());
401 MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
405 pp.query(
"terrain_gamma_m", gamma_m);
406 Real z_H = 2.44*h_m/(1-gamma_m);
409 for (
int k = domlo_z+1; k <= domhi_z; k++)
411 auto const& z_lev_h = z_levels_h.data();
413 Real zz = z_lev_h[k];
414 Real zz_minus = z_lev_h[k-1];
418 Real foo = cos((
PI/2)*(zz/z_H));
419 if(zz < z_H) { A = foo*foo*foo*foo*foo*foo; }
421 Real foo_minus = cos((
PI/2)*(zz_minus/z_H));
423 if(zz_minus < z_H) { A_minus = foo_minus*foo_minus*foo_minus*foo_minus*foo_minus*foo_minus; }
424 else { A_minus = 0; }
426 unsigned maxIter = 50;
428 Real threshold = gamma_m;
430 while (iter < maxIter && diff > threshold)
433 diff = ParReduce(TypeList<ReduceOpMin>{}, TypeList<Real>{}, mf2d, IntVect(ngrow,ngrow,0),
434 [=] AMREX_GPU_DEVICE (
int box_no,
int i,
int j,
int) noexcept
437 const auto & h_s = ma_h_s[box_no];
438 const auto & h_s_old = ma_h_s_old[box_no];
440 Real beta_k = 0.2*std::min(zz/(2*h_m),1.0);
443 int ii = amrex::min(amrex::max(i,domlo_x),domhi_x);
444 int jj = amrex::min(amrex::max(j,domlo_y),domhi_y);
447 h_s(i,j,k) = h_s_old(i,j,k-1) + beta_k*(h_s_old(ii+1,jj ,k-1)
448 + h_s_old(ii-1,jj ,k-1)
449 + h_s_old(ii ,jj+1,k-1)
450 + h_s_old(ii ,jj-1,k-1) - 4*h_s_old(ii,jj,k-1));
453 h_s(i,j,k) = h_s_old(i,j,k ) + beta_k*(h_s_old(ii+1,jj ,k )
454 + h_s_old(ii-1,jj ,k )
455 + h_s_old(ii ,jj+1,k )
456 + h_s_old(ii ,jj-1,k ) - 4*h_s_old(ii,jj,k ));
460 return { (zz + A * h_s(i,j,k) - (zz_minus + A_minus * h_s(i,j,k-1))) / (zz - zz_minus) };
464 MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
466 ParallelDescriptor::ReduceRealMin(diff);
471 h_mf_old.FillBoundary(geom.periodicity());
475 auto const& z_lev_d = z_levels_d.data();
478 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
481 Box xybx = mfi.growntilebox(ngrow);
484 Array4<Real>
const& h_s = h_mf_old.array(mfi);
485 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
487 ParallelFor(xybx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int) {
493 z_arr(i,j,k) =
z + A*h_s(i,j,k);
497 z_arr(i,j,k0-1) = 2.0*z_arr(i,j,k0) - z_arr(i,j,k);
503 Gpu::streamSynchronize();
512 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
515 Box gbx = mfi.growntilebox(ngrow);
516 gbx.setRange(2,domlo_z,domhi_z+1);
518 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
519 auto const& z_lev = z_levels_d.data();
521 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
526 int ii = amrex::max(amrex::min(i,imax),imin);
527 int jj = amrex::max(amrex::min(j,jmax),jmin);
531 z_arr(i,j,k) =
z + (std::pow((1. - (z/z_top)),
omega) * z_arr(ii,jj,k0));
535 z_arr(i,j,k0 ) = z_arr(ii,jj,k0);
538 z_arr(i,j,k0-1) = 2.0*z_arr(ii,jj,k0) - z_arr(i,j,k);
549 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
552 Box gbx = mfi.growntilebox(ngrow);
553 gbx.setRange(2,domlo_z,domhi_z+1);
555 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
556 auto const& z_lev = z_levels_d.data();
558 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
563 int ii = amrex::max(amrex::min(i,imax),imin);
564 int jj = amrex::max(amrex::min(j,jmax),jmin);
568 z_arr(i,j,k) =
z + z_arr(ii,jj,k0);
572 z_arr(i,j,k) =
z + (std::pow((1. - (z/z_top)),
omega) * z_arr(ii,jj,k0));
576 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
579 z_arr(i,j,k-1) = -z_arr(i,j,k+1);
constexpr amrex::Real PI
Definition: ERF_Constants.H:6
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pp(amrex::Real y)
Definition: ERF_MicrophysicsUtils.H:219
@ omega
Definition: ERF_SAM.H:49