256 int terrain_smoothing = 0;
257 pp.query(
"terrain_smoothing", terrain_smoothing);
259 if (lev > 0 && terrain_smoothing != 0) {
260 Abort(
"Must use terrain_smoothing = 0 when doing multilevel");
264 int ngrow = z_phys_nd.nGrow();
265 IntVect ngrowVect = z_phys_nd.nGrowVect();
267 const Box& domain = geom.Domain();
268 int domlo_x = domain.smallEnd(0);
int domhi_x = domain.bigEnd(0) + 1;
269 int domlo_y = domain.smallEnd(1);
int domhi_y = domain.bigEnd(1) + 1;
270 int domlo_z = domain.smallEnd(2);
int domhi_z = domain.bigEnd(2) + 1;
278 int nz = z_levels_h.size();
279 Real z_top = z_levels_h[nz-1];
281 Gpu::DeviceVector<Real> z_levels_d;
282 z_levels_d.resize(nz);
283 Gpu::copy(Gpu::hostToDevice, z_levels_h.begin(), z_levels_h.end(), z_levels_d.begin());
285 switch(terrain_smoothing) {
288 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
291 const Box& bx = mfi.validbox();
293 int k0 = bx.smallEnd()[2];
296 Box gbx = mfi.growntilebox(ngrowVect);
298 if (bx.smallEnd(2) == domlo_z) {
299 gbx.setSmall(2,domlo_z);
308 if (bx.bigEnd(2) == domhi_z) {
309 gbx.setBig(2,domhi_z);
312 if (gbx.bigEnd(2) > domhi_z) {
313 gbx.setBig(2,domhi_z);
317 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
318 auto const& z_lev = z_levels_d.data();
324 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
326 int ii = amrex::max(amrex::min(i,imax),imin);
327 int jj = amrex::max(amrex::min(j,jmax),jmin);
335 Real z_sfc = z_arr(ii,jj,k0);
336 Real z_lev_sfc = z_lev[k0];
338 z_arr(i,j,k) = ( (z_sfc - z_lev_sfc) * z_top +
339 (z_top - z_sfc ) *
z ) / (z_top - z_lev_sfc);
343 z_phys_nd.FillBoundary(geom.periodicity());
345 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
348 const Box& bx = mfi.validbox();
349 Box gbx = mfi.growntilebox(ngrowVect);
351 int k0 = bx.smallEnd()[2];
355 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
358 ParallelFor(makeSlab(gbx,2,0), [=] AMREX_GPU_DEVICE (
int i,
int j,
int)
360 z_arr(i,j,-1) = 2.0*z_arr(i,j,0) - z_arr(i,j,1);
370 MultiFab h_mf( z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, ngrow+1);
371 MultiFab h_mf_old(z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, ngrow+1);
379 BoxList bl2d = h_mf.boxArray().boxList();
380 for (
auto& b : bl2d) {
383 BoxArray ba2d(std::move(bl2d));
384 mf2d = MultiFab(ba2d, h_mf.DistributionMap(), 1, ngrow, MFInfo().SetAlloc(
false));
388 MultiArray4<Real>
const& ma_h_s = h_mf.arrays();
389 MultiArray4<Real>
const& ma_h_s_old = h_mf_old.arrays();
390 MultiArray4<Real>
const& ma_z_phys = z_phys_nd.arrays();
396 h_m = ParReduce(TypeList<ReduceOpMax>{}, TypeList<Real>{}, mf2d, IntVect(ngrow,ngrow,0),
397 [=] AMREX_GPU_DEVICE (
int box_no,
int i,
int j,
int) noexcept
401 const auto & h = ma_h_s[box_no];
402 const auto & z_arr = ma_z_phys[box_no];
404 int ii = amrex::max(amrex::min(i,imax),imin);
405 int jj = amrex::max(amrex::min(j,jmax),jmin);
408 z_arr(i,j,k0) = z_arr(ii,jj,k0);
411 h(i,j,k0) = z_arr(i,j,k0);
414 return { z_arr(i,j,k0) };
417 if (h_m < std::numeric_limits<Real>::epsilon()) h_m = 1e-16;
420 h_mf.FillBoundary(geom.periodicity());
423 MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
427 pp.query(
"terrain_gamma_m", gamma_m);
428 Real z_H = 2.44*h_m/(1-gamma_m);
431 for (
int k = domlo_z+1; k <= domhi_z; k++)
433 auto const& z_lev_h = z_levels_h.data();
435 Real zz = z_lev_h[k];
436 Real zz_minus = z_lev_h[k-1];
440 Real foo = cos((
PI/2)*(zz/z_H));
441 if(zz < z_H) { A = foo*foo*foo*foo*foo*foo; }
443 Real foo_minus = cos((
PI/2)*(zz_minus/z_H));
445 if(zz_minus < z_H) { A_minus = foo_minus*foo_minus*foo_minus*foo_minus*foo_minus*foo_minus; }
446 else { A_minus = 0; }
448 unsigned maxIter = 50;
450 Real threshold = gamma_m;
452 while (iter < maxIter && diff > threshold)
455 diff = ParReduce(TypeList<ReduceOpMin>{}, TypeList<Real>{}, mf2d, IntVect(ngrow,ngrow,0),
456 [=] AMREX_GPU_DEVICE (
int box_no,
int i,
int j,
int) noexcept
459 const auto & h_s = ma_h_s[box_no];
460 const auto & h_s_old = ma_h_s_old[box_no];
462 Real beta_k = 0.2*std::min(zz/(2*h_m),1.0);
465 int ii = amrex::min(amrex::max(i,domlo_x),domhi_x);
466 int jj = amrex::min(amrex::max(j,domlo_y),domhi_y);
469 h_s(i,j,k) = h_s_old(i,j,k-1) + beta_k*(h_s_old(ii+1,jj ,k-1)
470 + h_s_old(ii-1,jj ,k-1)
471 + h_s_old(ii ,jj+1,k-1)
472 + h_s_old(ii ,jj-1,k-1) - 4*h_s_old(ii,jj,k-1));
475 h_s(i,j,k) = h_s_old(i,j,k ) + beta_k*(h_s_old(ii+1,jj ,k )
476 + h_s_old(ii-1,jj ,k )
477 + h_s_old(ii ,jj+1,k )
478 + h_s_old(ii ,jj-1,k ) - 4*h_s_old(ii,jj,k ));
482 return { (zz + A * h_s(i,j,k) - (zz_minus + A_minus * h_s(i,j,k-1))) / (zz - zz_minus) };
486 MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
488 ParallelDescriptor::ReduceRealMin(diff);
493 h_mf_old.FillBoundary(geom.periodicity());
497 auto const& z_lev_d = z_levels_d.data();
500 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
503 Box xybx = mfi.growntilebox(ngrow);
506 Array4<Real>
const& h_s = h_mf_old.array(mfi);
507 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
509 ParallelFor(xybx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int) {
515 z_arr(i,j,k) =
z + A*h_s(i,j,k);
519 z_arr(i,j,k0-1) = 2.0*z_arr(i,j,k0) - z_arr(i,j,k);
525 Gpu::streamSynchronize();
534 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
537 Box gbx = mfi.growntilebox(ngrow);
538 gbx.setRange(2,domlo_z,domhi_z+1);
540 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
541 auto const& z_lev = z_levels_d.data();
543 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
548 int ii = amrex::max(amrex::min(i,imax),imin);
549 int jj = amrex::max(amrex::min(j,jmax),jmin);
553 z_arr(i,j,k) =
z + (std::pow((1. - (z/z_top)),
omega) * z_arr(ii,jj,k0));
557 z_arr(i,j,k0 ) = z_arr(ii,jj,k0);
560 z_arr(i,j,k0-1) = 2.0*z_arr(ii,jj,k0) - z_arr(i,j,k);
571 for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
574 Box gbx = mfi.growntilebox(ngrow);
575 gbx.setRange(2,domlo_z,domhi_z+1);
577 Array4<Real>
const& z_arr = z_phys_nd.array(mfi);
578 auto const& z_lev = z_levels_d.data();
580 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
585 int ii = amrex::max(amrex::min(i,imax),imin);
586 int jj = amrex::max(amrex::min(j,jmax),jmin);
590 z_arr(i,j,k) =
z + z_arr(ii,jj,k0);
594 z_arr(i,j,k) =
z + (std::pow((1. - (z/z_top)),
omega) * z_arr(ii,jj,k0));
598 ParallelFor(gbx, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k)
601 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