ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TerrainMetrics.cpp File Reference
#include <ERF_TerrainMetrics.H>
#include <ERF_Utils.H>
#include <AMReX_ParmParse.H>
#include <ERF_Constants.H>
#include <ERF_Interpolation_1D.H>
#include <algorithm>
#include <array>
#include <cmath>
#include <limits>
#include <map>
#include <numeric>
#include <utility>
#include <vector>
Include dependency graph for ERF_TerrainMetrics.cpp:

Functions

void validate_flat_terrain (int lev, const MultiFab &z_phys_nd, const Vector< Real > &z_levels_h)
 
void validate_flat_top (int lev, const Geometry &geom, const MultiFab &z_phys_nd)
 
void init_default_zphys (int, const Geometry &geom, MultiFab &z_phys_nd, MultiFab &z_phys_cc, Real z_offset)
 
BoxArray join_boxes_stacked_in_z (const BoxArray &ba)
 
void make_terrain_fitted_coords (int lev, const Geometry &geom, MultiFab &z_phys_nd, Vector< Real > const &z_levels_h, GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &phys_bc_type, FineTerrain fine_terrain, MultiFab const *z_phys_interp, bool moving_mesh)
 
FineTerrain which_fine_terrain ()
 
void init_fine_terrain_grid (int lev, Geometry const &geom, MultiFab &z_phys_nd, Vector< Real > const &z_levels_h, FineTerrain fine_terrain, MultiFab const *z_phys_interp)
 
void init_which_terrain_grid (int lev, Geometry const &geom, MultiFab &z_phys_nd, Vector< Real > const &z_levels_h, FineTerrain fine_terrain, MultiFab const *z_phys_interp)
 
void make_J (const Geometry &geom, MultiFab &z_phys_nd, MultiFab &detJ_cc)
 
void make_areas (const Geometry &geom, MultiFab &z_phys_nd, MultiFab &ax, MultiFab &ay, MultiFab &az)
 
void make_zcc (const Geometry &geom, MultiFab &z_phys_nd, MultiFab &z_phys_cc)
 
void make_zsfc_nd (const Geometry &geom, const MultiFab &z_phys_nd, MultiFab &z_sfc_nd, const MultiFab *z_sfc_crse, const IntVect &ref_ratio)
 
Real get_dzmin_terrain (MultiFab &z_phys_nd)
 

Function Documentation

◆ get_dzmin_terrain()

Real get_dzmin_terrain ( MultiFab &  z_phys_nd)

Computation min dz at cell-center

1413 {
1414  auto const& ma_z_nd_arr = z_phys_nd.const_arrays();
1415  GpuTuple<Real> min = ParReduce(TypeList<ReduceOpMin>{},
1416  TypeList<Real>{},
1417  z_phys_nd, IntVect(0),
1418  [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k) noexcept
1419  -> GpuTuple<Real>
1420  {
1421  amrex::Real dz = Compute_Z_AtWFace(i,j,k+1,ma_z_nd_arr[box_no]) -
1422  Compute_Z_AtWFace(i,j,k ,ma_z_nd_arr[box_no]);
1423  return { dz };
1424  });
1425  Real r = (get<0>(min) + std::numeric_limits<amrex::Real>::epsilon());
1426  ParallelDescriptor::ReduceRealMin(r);
1427  return r;
1428 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_Z_AtWFace(const int &i, const int &j, const int &k, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:741
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12

Referenced by ERF::init_zphys(), ERF::ReadCheckpointFile(), and ERF::remake_zphys().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_default_zphys()

void init_default_zphys ( int  ,
const Geometry &  geom,
MultiFab &  z_phys_nd,
MultiFab &  z_phys_cc,
Real  z_offset 
)

Define a default z_phys so we have it even if a completely regular grid This will be over-written if we use z_levels, or grid stretching, or terrain-fitted grids

118 {
119  const auto& dx = geom.CellSize();
120  Real dz = dx[2];
121  Real z_bot = geom.ProbLo(2) - z_offset;
122 
123  for (MFIter mfi(z_phys_nd,true); mfi.isValid(); ++mfi)
124  {
125  const Box& bx = mfi.growntilebox();
126  const Array4< Real> z_nd_arr = z_phys_nd.array(mfi);
127  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
128  {
129  z_nd_arr(i,j,k) = z_bot + k * dz;
130  });
131  }
132 
133  for (MFIter mfi(z_phys_cc,true); mfi.isValid(); ++mfi)
134  {
135  const Box& bx = mfi.growntilebox();
136  const Array4< Real> z_cc_arr = z_phys_cc.array(mfi);
137  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
138  {
139  z_cc_arr(i,j,k) = z_bot + (k + myhalf) * dz;
140  });
141  }
142 }
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41

Referenced by ERF::init_stuff().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_fine_terrain_grid()

void init_fine_terrain_grid ( int  lev,
Geometry const &  geom,
MultiFab &  z_phys_nd,
Vector< Real > const &  z_levels_h,
FineTerrain  fine_terrain,
MultiFab const *  z_phys_interp 
)

Finish the terrain-fitted mesh on a fine level built from the coarse level.

On entry z_phys_nd holds the mesh interpolated from the coarse level in its valid region, with – for Transform – the fine-resolution terrain already written into the k=0 slab. The nodes outside the domain are still bogus_large_value, because neither the interpolation nor make_terrain_fitted_coords()'s setDomainBndry() call gave them a usable value, so we fill them here the same way the BTF branch does.

579 {
580  AMREX_ALWAYS_ASSERT(lev > 0);
581  AMREX_ALWAYS_ASSERT(fine_terrain != FineTerrain::None);
582 
583  const Box& domain = geom.Domain();
584  int domlo_x = domain.smallEnd(0); int domhi_x = domain.bigEnd(0) + 1;
585  int domlo_y = domain.smallEnd(1); int domhi_y = domain.bigEnd(1) + 1;
586  int domlo_z = domain.smallEnd(2);
587 
588  IntVect ngrowVect = z_phys_nd.nGrowVect();
589 
590  if (fine_terrain == FineTerrain::Transform) {
591  AMREX_ALWAYS_ASSERT(z_phys_interp != nullptr);
592 
593  const BoxArray& ba = z_phys_nd.boxArray();
594 
595  //
596  // The correction is anchored at the surface, so a box that does not reach the
597  // surface would keep the uncorrected interpolated mesh and would not join the
598  // box below it. Say so rather than silently building a tangled mesh.
599  //
600  for (int i = 0; i < ba.size(); i++) {
601  if (ba[i].smallEnd(2) != domlo_z) {
602  Abort("erf.amr_terrain_refinement = transform requires that every box on a fine level reach the surface: increase amr.max_grid_size_z or use erf.amr_terrain_refinement = interpolate");
603  }
604  }
605 
606  //
607  // Decay over the height of the fine grids as a whole and never over the height
608  // of an individual box, so that the mesh does not depend on the decomposition.
609  //
610  int khi_lev = ba.minimalBox().bigEnd(2);
611  Real z_lev_top = z_levels_h[khi_lev];
612  Real z_lev_bot = z_levels_h[domlo_z];
613  AMREX_ALWAYS_ASSERT(z_lev_top > z_lev_bot);
614 
615  Gpu::DeviceVector<Real> z_levels_d(z_levels_h.size());
616  Gpu::copy(Gpu::hostToDevice, z_levels_h.begin(), z_levels_h.end(), z_levels_d.begin());
617  auto const& z_lev = z_levels_d.data();
618 
619  //
620  // Correct the ghost nodes along with the valid ones. On a fine level whose
621  // grids do not cover the domain, the lateral ghost nodes have no valid owner
622  // anywhere, so the FillBoundary below cannot bring them up to date: left
623  // uncorrected they would keep the interpolated mesh above the surface while
624  // their k=0 node already holds the fine terrain, and the first layer of such a
625  // column inverts wherever the fine terrain stands more than a layer thickness
626  // above the interpolated one. Both fields the correction reads are defined on
627  // those nodes -- but only inside the domain, so stop there and leave the nodes
628  // outside it to the clamp below, which is what gives them a mesh at all.
629  //
630  const Box domain_nd = convert(domain, IntVect(1,1,1));
631 
632  for (MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi)
633  {
634  const Box bx = mfi.growntilebox(ngrowVect) & domain_nd;
635 
636  Array4<Real > const& z_arr = z_phys_nd.array(mfi);
637  Array4<Real const> const& z_interp = z_phys_interp->const_array(mfi);
638 
639  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
640  {
641  if (k > domlo_z && k <= khi_lev) {
642  // How much taller the fine terrain is than the interpolated surface
643  Real delta_terrain = z_arr(i,j,domlo_z) - z_interp(i,j,domlo_z);
644 
645  Real decay = (z_lev_top - z_lev[k]) / (z_lev_top - z_lev_bot);
646  decay = amrex::max(Real(0.0), amrex::min(Real(1.0), decay));
647 
648  z_arr(i,j,k) = z_interp(i,j,k) + decay * delta_terrain;
649  }
650  });
651  } // mfi
652 
653  // Give every ghost node that does have a valid owner that owner's value, rather
654  // than the one this box computed for itself, before the fill below reads them
655  z_phys_nd.FillBoundary(geom.periodicity());
656  }
657 
658  //
659  // Extend the mesh to the nodes outside the lateral domain boundary by copying the
660  // nearest column inside it, which is the value the BTF branch gives those nodes.
661  // Like that branch we clamp in a periodic direction too, even though FillBoundary
662  // below will replace what we write there with the periodic image: that way every
663  // node we read here is one inside the domain, whose value we know is already set.
664  //
665  int imin = domlo_x; int imax = domhi_x;
666  int jmin = domlo_y; int jmax = domhi_y;
667 
668  for (MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi)
669  {
670  Box gbx = mfi.growntilebox(ngrowVect);
671  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
672 
673  // Note that we only write nodes outside the domain and only read nodes inside
674  // it, so no node is both read and written here.
675  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
676  {
677  int ii = amrex::max(amrex::min(i,imax),imin);
678  int jj = amrex::max(amrex::min(j,jmax),jmin);
679  if (ii != i || jj != j) {
680  z_arr(i,j,k) = z_arr(ii,jj,k);
681  }
682  });
683  } // mfi
684 
685  z_phys_nd.FillBoundary(geom.periodicity());
686 
687  for (MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi)
688  {
689  const Box& bx = mfi.validbox();
690  Box gbx = mfi.growntilebox(ngrowVect);
691 
692  if (bx.smallEnd(2) == domlo_z)
693  {
694  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
695 
696  // Fill lateral boundaries below the bottom surface
697  ParallelFor(makeSlab(gbx,2,domlo_z), [=] AMREX_GPU_DEVICE (int i, int j, int)
698  {
699  z_arr(i,j,domlo_z-1) = two*z_arr(i,j,domlo_z) - z_arr(i,j,domlo_z+1);
700  });
701  }
702  } // mfi
703 } // init_fine_terrain_grid
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
@ Transform
add the fine terrain as a correction that decays with height
@ None
caller has not prepared a fine mesh from the coarse level

Referenced by init_which_terrain_grid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_which_terrain_grid()

void init_which_terrain_grid ( int  lev,
Geometry const &  geom,
MultiFab &  z_phys_nd,
Vector< Real > const &  z_levels_h,
FineTerrain  fine_terrain,
MultiFab const *  z_phys_interp 
)

Initialize the terrain grid using the selected model (BTF, STF, or Sullivan TF).

Parameters
[in]levLevel in the AMR hierarchy.
[in]geomGeometry defining the domain.
[out]z_phys_ndMultiFab to be filled with nodal physical heights.
[in]z_levels_hReference z-levels for grid stretching.
720 {
721  // User-selected method from inputs file (BTF default)
722  ParmParse pp("erf");
723  int terrain_smoothing = 0;
724  pp.queryAdd("terrain_smoothing", terrain_smoothing);
725 
726  //
727  // The STF and Sullivan TF transformations need the full column from the surface to
728  // the domain top, so on a fine level we build the mesh from the one interpolated
729  // from the coarse level instead. Only a caller that has done that interpolation
730  // (and told us so) may take this path; everyone else still aborts.
731  //
732  if (lev > 0 && terrain_smoothing != 0) {
733  if (fine_terrain == FineTerrain::None) {
734  Abort("terrain_smoothing = " + std::to_string(terrain_smoothing) +
735  " on level > 0 requires setting erf.amr_terrain_refinement (transform or interpolate)");
736  }
737  init_fine_terrain_grid(lev, geom, z_phys_nd, z_levels_h, fine_terrain, z_phys_interp);
738  return;
739  }
740 
741  // Number of ghost cells
742  int ngrow = z_phys_nd.nGrow();
743  IntVect ngrowVect = z_phys_nd.nGrowVect();
744 
745  const Box& domain = geom.Domain();
746  int domlo_x = domain.smallEnd(0); int domhi_x = domain.bigEnd(0) + 1;
747  int domlo_y = domain.smallEnd(1); int domhi_y = domain.bigEnd(1) + 1;
748  int domlo_z = domain.smallEnd(2); int domhi_z = domain.bigEnd(2) + 1;
749 
750  int imin = domlo_x; // if (geom.isPeriodic(0)) imin -= z_phys_nd.nGrowVect()[0];
751  int jmin = domlo_y; // if (geom.isPeriodic(1)) jmin -= z_phys_nd.nGrowVect()[1];
752 
753  int imax = domhi_x; // if (geom.isPeriodic(0)) imax += z_phys_nd.nGrowVect()[0];
754  int jmax = domhi_y; // if (geom.isPeriodic(1)) jmax += z_phys_nd.nGrowVect()[1];
755 
756  int nz = static_cast<int>(z_levels_h.size());
757  Real z_top = z_levels_h[nz-1];
758 
759  // The STF and Sullivan transforms measure heights and terrain from the bottom of the
760  // domain, which is z_levels_h[0] = geometry.prob_lo[2]; the BTF one does so already
761  Real z_bot = z_levels_h[0];
762  Real depth = z_top - z_bot;
763 
764  Gpu::DeviceVector<Real> z_levels_d;
765  z_levels_d.resize(nz);
766  Gpu::copy(Gpu::hostToDevice, z_levels_h.begin(), z_levels_h.end(), z_levels_d.begin());
767 
768  switch(terrain_smoothing) {
769  case 0: // BTF Method
770  {
771  for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
772  {
773  // Note that this box is nodal because it is based on z_phys_nd
774  const Box& bx = mfi.validbox();
775 
776  int k0 = bx.smallEnd()[2];
777 
778  // Grown box with corrected ghost cells at top
779  Box gbx = mfi.growntilebox(ngrowVect);
780 
781  if (bx.smallEnd(2) == domlo_z) {
782  gbx.setSmall(2,domlo_z);
783  } else {
784  gbx.growLo(2,-1);
785  }
786 
787  // Note that we don't overwrite the values at the high end of the box
788  // regardless of whether the box reaches the top of the domain or not.
789  // In the case of lev > 0, this ensures that the fine nodes at the top
790  // of a fine box are those that are interpolated from the coarse grid
791  if (bx.bigEnd(2) == domhi_z) {
792  gbx.setBig(2,domhi_z);
793  } else {
794  gbx.growHi(2,-1);
795  if (gbx.bigEnd(2) > domhi_z) {
796  gbx.setBig(2,domhi_z);
797  }
798  }
799 
800  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
801  auto const& z_lev = z_levels_d.data();
802 
803  //
804  // Vertical grid stretching using BTF model from p2163 of Klemp2011
805  // z_levels are only defined from k = dom_lo to dom_hi (nodal)
806  //
807  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
808  {
809  int ii = amrex::max(amrex::min(i,imax),imin);
810  int jj = amrex::max(amrex::min(j,jmax),jmin);
811 
812  //
813  // Start with flat z_lev set either with uniform cell size or specified z_levels
814  // If k0 = 0 then z_arr at k0 has already been filled from the terrain data
815  // If k0 > 0 then z_arr at k0 has already been filled from interpolation
816  //
817  // A box that does not start at the bottom (e.g. a refined box starting at
818  // k0 = 1) has ghost nodes below the domain, where z_lev is not defined;
819  // extend the levels there by reflection about the bottom
820  Real z = (k >= domlo_z) ? z_lev[k] : two*z_lev[domlo_z] - z_lev[2*domlo_z-k];
821  Real z_sfc = z_arr(ii,jj,k0);
822  Real z_lev_sfc = z_lev[k0];
823 
824  // Written so that no product feeds a sum: a compiler that contracts to fused
825  // multiply-adds (gcc on arm64, or with FMA enabled) could otherwise round
826  // the two products differently in its vectorized and scalar loops, and then
827  // two boxes would give a node they share different heights (#5237)
828  z_arr(i,j,k) = z + (z_sfc - z_lev_sfc) * (z_top - z) / (z_top - z_lev_sfc);
829  });
830  } // mfi
831 
832  z_phys_nd.FillBoundary(geom.periodicity());
833 
834  for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
835  {
836  // Note that this box is nodal because it is based on z_phys_nd
837  const Box& bx = mfi.validbox();
838  Box gbx = mfi.growntilebox(ngrowVect);
839 
840  int k0 = bx.smallEnd()[2];
841 
842  if (k0 == 0)
843  {
844  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
845 
846  // Fill lateral boundaries below the bottom surface
847  ParallelFor(makeSlab(gbx,2,0), [=] AMREX_GPU_DEVICE (int i, int j, int)
848  {
849  z_arr(i,j,-1) = two*z_arr(i,j,0) - z_arr(i,j,1);
850  });
851  }
852  } // mfi
853  break;
854  } // case 0
855 
856  case 1: // STF Method
857  {
858  // Get MultiFab spanning domain with 1 level of ghost cells
859  MultiFab h_mf( z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, ngrow+1);
860  MultiFab h_mf_old(z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, ngrow+1);
861 
862  // Save max height for smoothing
863  Real h_m;
864 
865  // Create 2D MF without allocation
866  MultiFab mf2d;
867  {
868  BoxList bl2d = h_mf.boxArray().boxList();
869  for (auto& b : bl2d) { b.setRange(2,b.smallEnd(2)); }
870  BoxArray ba2d(std::move(bl2d));
871  mf2d = MultiFab(ba2d, h_mf.DistributionMap(), 1, ngrow, MFInfo().SetAlloc(false));
872  }
873 
874  // Get MultiArray4s from the multifabs
875  MultiArray4<Real> const& ma_h_s = h_mf.arrays();
876  MultiArray4<Real> const& ma_h_s_old = h_mf_old.arrays();
877  MultiArray4<Real> const& ma_z_phys = z_phys_nd.arrays();
878 
879  // Bottom boundary
880  int k0 = domlo_z;
881 
882  // Get max value
883  h_m = ParReduce(TypeList<ReduceOpMax>{}, TypeList<Real>{}, mf2d, IntVect(ngrow,ngrow,0),
884  [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int) noexcept
885  -> GpuTuple<Real>
886  {
887  // Get Array4s
888  const auto & h = ma_h_s[box_no];
889  const auto & z_arr = ma_z_phys[box_no];
890 
891  int ii = amrex::max(amrex::min(i,imax),imin);
892  int jj = amrex::max(amrex::min(j,jmax),jmin);
893 
894  // Fill the lateral boundaries
895  z_arr(i,j,k0) = z_arr(ii,jj,k0);
896 
897  // Populate h with the terrain height above the bottom of the domain
898  h(i,j,k0) = z_arr(i,j,k0) - z_bot;
899 
900  // Return height for max
901  return { h(i,j,k0) };
902  });
903  amrex::ParallelDescriptor::ReduceRealMax(h_m);
904 
905  if (h_m < std::numeric_limits<Real>::epsilon()) h_m = Real(1e-16);
906 
907  // Fill ghost cells (neglects domain boundary if not periodic)
908  h_mf.FillBoundary(geom.periodicity());
909 
910  // Make h_mf copy for old values
911  MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
912 
913  // Minimum allowed fractional grid spacing
914  Real gamma_m = myhalf;
915  pp.queryAdd("terrain_gamma_m", gamma_m);
916  Real z_H = Real(2.44)*h_m/(1-gamma_m); // Klemp2011 Eqn. 11
917 
918  // Levels at which the minimum-thickness criterion forced us to stop smoothing
919  // early, reported once below: a terrain that needs this at many levels is at the
920  // edge of what STF can represent on this vertical grid.
921  int n_capped = 0;
922 
923  // Populate h_mf at k>0 with h_s, solving in ordered 2D slices
924  for (int k = domlo_z+1; k <= domhi_z; k++) // skip terrain level
925  {
926  auto const& z_lev_h = z_levels_h.data();
927 
928  Real zz = z_lev_h[k] - z_bot;
929  Real zz_minus = z_lev_h[k-1] - z_bot;
930 
931  // Hybrid attenuation profile, Klemp2011 Eqn. 9
932  Real A;
933  Real foo = std::cos((PI/2)*(zz/z_H));
934  if(zz < z_H) { A = foo*foo*foo*foo*foo*foo; } // A controls rate of return to atm
935  else { A = 0; }
936  Real foo_minus = std::cos((PI/2)*(zz_minus/z_H));
937  Real A_minus;
938  if(zz_minus < z_H) { A_minus = foo_minus*foo_minus*foo_minus*foo_minus*foo_minus*foo_minus; } // A controls rate of return to atm
939  else { A_minus = 0; }
940 
941  unsigned maxIter = 50; // M_k in paper
942  unsigned iter = 0;
943  Real threshold = gamma_m;
944  Real diff = bogus_large_value;
945  while (iter < maxIter)
946  {
947 
948  diff = ParReduce(TypeList<ReduceOpMin>{}, TypeList<Real>{}, mf2d, IntVect(ngrow,ngrow,0),
949  [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int) noexcept
950  -> GpuTuple<Real>
951  {
952  const auto & h_s = ma_h_s[box_no];
953  const auto & h_s_old = ma_h_s_old[box_no];
954 
955  Real beta_k = Real(0.2)*std::min(zz/(2*h_m),Real(1.0)); //smoothing coefficient (Eqn. 8)
956 
957  // Clip indices for ghost-cells
958  int ii = amrex::min(amrex::max(i,domlo_x),domhi_x);
959  int jj = amrex::min(amrex::max(j,domlo_y),domhi_y);
960 
961  if (iter == 0) {
962  h_s(i,j,k) = h_s_old(i,j,k-1) + beta_k*(h_s_old(ii+1,jj ,k-1)
963  + h_s_old(ii-1,jj ,k-1)
964  + h_s_old(ii ,jj+1,k-1)
965  + h_s_old(ii ,jj-1,k-1) - 4*h_s_old(ii,jj,k-1));
966  }
967  else {
968  h_s(i,j,k) = h_s_old(i,j,k ) + beta_k*(h_s_old(ii+1,jj ,k )
969  + h_s_old(ii-1,jj ,k )
970  + h_s_old(ii ,jj+1,k )
971  + h_s_old(ii ,jj-1,k ) - 4*h_s_old(ii,jj,k ));
972  }
973 
974  // Minimum vertical grid spacing condition (Klemp2011 Eqn. 7)
975  return { (zz + A * h_s(i,j,k) - (zz_minus + A_minus * h_s(i,j,k-1))) / (zz - zz_minus) };
976 
977  }); //ParReduce
978 
979  ParallelDescriptor::ReduceRealMin(diff);
980 
981  //
982  // Each pass smooths h_s at THIS level while the level below is already
983  // fixed, so over a peak it always thins the layer: the passes have to
984  // stop before Eqn. 7 is violated, not after. Measuring only once a pass
985  // has been committed is not enough -- nothing then stops the pass that
986  // breaks the criterion from being the one we keep, which is how a column
987  // over steep terrain ended up at 0.21 of its nominal layer thickness
988  // against gamma_m = 0.5. Reject that pass and restore what preceded it.
989  //
990  // At iter 0 there is no earlier pass at this level to fall back to, so
991  // carry the level below up unsmoothed instead. That leaves only the
992  // A-decay term, whose worst case is exactly what z_H = 2.44 h_m/(1-gamma_m)
993  // was chosen to bound: max|dA/dz| = 2.439/z_H, so h_m max|dA/dz| dz equals
994  // (1-gamma_m) dz and the thickness ratio lands on gamma_m.
995  //
996  if (diff < threshold) {
997  ++n_capped;
998  if (iter == 0) {
999  for ( MFIter mfi(h_mf, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1000  {
1001  Box xybx = mfi.growntilebox(ngrow);
1002  xybx.setRange(2,0);
1003  Array4<Real> const& h_s = h_mf.array(mfi);
1004  ParallelFor(xybx, [=] AMREX_GPU_DEVICE (int i, int j, int) noexcept
1005  {
1006  h_s(i,j,k) = h_s(i,j,k-1);
1007  });
1008  }
1009  MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
1010  h_mf_old.FillBoundary(geom.periodicity());
1011  } else {
1012  // h_mf_old still holds the last pass that satisfied the criterion
1013  MultiFab::Copy(h_mf, h_mf_old,0,0,1,h_mf.nGrow());
1014  }
1015  break;
1016  }
1017 
1018  MultiFab::Copy(h_mf_old, h_mf,0,0,1,h_mf_old.nGrow());
1019 
1020  iter++;
1021 
1022  // Fill ghost points
1023  h_mf_old.FillBoundary(geom.periodicity());
1024 
1025  } // while
1026 
1027  auto const& z_lev_d = z_levels_d.data();
1028 
1029  // Populate z_phys_nd by solving z_arr(i,j,k) = z + A*h_s(i,j,k)
1030  for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1031  {
1032  // Grown box with no z range
1033  Box xybx = mfi.growntilebox(ngrow);
1034  xybx.setRange(2,0);
1035 
1036  Array4<Real> const& h_s = h_mf_old.array(mfi);
1037  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
1038 
1039  ParallelFor(xybx, [=] AMREX_GPU_DEVICE (int i, int j, int) {
1040 
1041  // Location of nodes
1042  Real z = z_lev_d[k];
1043 
1044  // STF model from p2164 of Klemp2011 (Eqn. 4)
1045  z_arr(i,j,k) = z + A*h_s(i,j,k);
1046 
1047  // Fill below the bottom surface
1048  if (k == 1) {
1049  z_arr(i,j,k0-1) = two*z_arr(i,j,k0) - z_arr(i,j,k);
1050  }
1051  });
1052  } // mfi
1053  } // k
1054 
1055  if (n_capped > 0) {
1056  amrex::Print() << "NOTE: the smoothed terrain-following grid at level " << lev
1057  << " hit its minimum layer-thickness floor (erf.terrain_gamma_m = "
1058  << gamma_m << ") at " << n_capped << " of "
1059  << (domhi_z - domlo_z) << " levels, so the terrain smoothing was "
1060  << "capped there. Raising erf.terrain_gamma_m, coarsening the "
1061  << "vertical grid or using erf.terrain_smoothing = 0 would give "
1062  << "this terrain more room.\n";
1063  }
1064 
1065  Gpu::streamSynchronize();
1066 
1067  break;
1068  } // case 1
1069 
1070  case 2: // Sullivan TF Method
1071  {
1072  int k0 = 0;
1073 
1074  for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1075  {
1076  // Grown box with corrected ghost cells at top
1077  Box gbx = mfi.growntilebox(ngrow);
1078  gbx.setRange(2,domlo_z,domhi_z+1);
1079 
1080  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
1081  auto const& z_lev = z_levels_d.data();
1082 
1083  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
1084  {
1085  // Vertical grid stretching
1086  Real z = z_lev[k];
1087 
1088  int ii = amrex::max(amrex::min(i,imax),imin);
1089  int jj = amrex::max(amrex::min(j,jmax),jmin);
1090 
1091  // Fill levels using model from Sullivan et. al. 2014
1092  int omega = 3; //Used to adjust how rapidly grid lines level out. omega=1 is BTF!
1093  z_arr(i,j,k) = z + static_cast<Real>(std::pow((one - (z - z_bot)/depth),omega) * (z_arr(ii,jj,k0) - z_bot));
1094 
1095  // Fill lateral boundaries and below the bottom surface
1096  if (k == k0) {
1097  z_arr(i,j,k0 ) = z_arr(ii,jj,k0);
1098  }
1099  if (k == 1) {
1100  z_arr(i,j,k0-1) = two*z_arr(ii,jj,k0) - z_arr(i,j,k);
1101  }
1102  });
1103  } // mfi
1104  break;
1105  } // case 2
1106 
1107  case 3: // Debugging Test Method -- applies Sullivan TF starting at k = 1 so that domain does not change size
1108  {
1109  int k0 = 0;
1110 
1111  for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1112  {
1113  // Grown box with corrected ghost cells at top
1114  Box gbx = mfi.growntilebox(ngrow);
1115  gbx.setRange(2,domlo_z,domhi_z+1);
1116 
1117  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
1118  auto const& z_lev = z_levels_d.data();
1119 
1120  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
1121  {
1122  // Vertical grid stretching
1123  Real z = z_lev[k];
1124 
1125  int ii = amrex::max(amrex::min(i,imax),imin);
1126  int jj = amrex::max(amrex::min(j,jmax),jmin);
1127 
1128  // Fill values outside the lateral boundaries and below the bottom surface (necessary if init_type = "WRFInput")
1129  if (k == k0+1) {
1130  z_arr(i,j,k) = z + (z_arr(ii,jj,k0) - z_bot);
1131  } else {
1132  // Fill levels using model from Sullivan et. al. 2014
1133  int omega = 3; //Used to adjust how rapidly grid lines level out. omega=1 is BTF!
1134  z_arr(i,j,k) = z + static_cast<Real>(std::pow((one - (z - z_bot)/depth),omega) * (z_arr(ii,jj,k0) - z_bot));
1135  }
1136  });
1137  gbx.setBig(2,0);
1138  ParallelFor(gbx, [=] AMREX_GPU_DEVICE (int i, int j, int k)
1139  {
1140  z_arr(i,j,k ) = z_bot;
1141  z_arr(i,j,k-1) = two*z_bot - z_arr(i,j,k+1);
1142  });
1143  } // mfi
1144  break;
1145  } // case 3
1146  } //switch
1147 }
constexpr amrex::Real bogus_large_value
Definition: ERF_Constants.H:19
ParmParse pp("prob")
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real PI
Definition: ERF_NumericalConstants.H:46
void init_fine_terrain_grid(int lev, Geometry const &geom, MultiFab &z_phys_nd, Vector< Real > const &z_levels_h, FineTerrain fine_terrain, MultiFab const *z_phys_interp)
Definition: ERF_TerrainMetrics.cpp:573
@ omega
Definition: ERF_Morrison.H:57
constexpr int A
Definition: ERF_TwoStreamColumn.H:653

Referenced by make_terrain_fitted_coords().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ join_boxes_stacked_in_z()

BoxArray join_boxes_stacked_in_z ( const BoxArray &  ba)

The cells of a (cell-centered) BoxArray as boxes that are never stacked on each other.

Wherever a box of ba sits on the top face of another box of ba, the run of cells in z that the two belong to becomes a single box. Boxes stacked with different lateral extents are first cut at each other's lateral edges; the joined pieces are then merged in x and y and chopped to no more than the largest lateral size of the boxes they came from. Returns ba itself when no two boxes of ba are stacked.

155 {
156  AMREX_ALWAYS_ASSERT(ba.ixType().cellCentered());
157 
158  const int nboxes = static_cast<int>(ba.size());
159 
160  // Gather the boxes into stacks: link every box to the boxes that sit on its top face
161  std::vector<int> stack_of(nboxes);
162  std::iota(stack_of.begin(), stack_of.end(), 0);
163  auto find_stack = [&stack_of] (int i) {
164  while (stack_of[i] != i) {
165  stack_of[i] = stack_of[stack_of[i]];
166  i = stack_of[i];
167  }
168  return i;
169  };
170 
171  bool any_stacked = false;
172  for (int i = 0; i < nboxes; ++i) {
173  Box above(ba[i]);
174  above.setRange(2, ba[i].bigEnd(2)+1);
175  for (const auto& isect : ba.intersections(above)) {
176  stack_of[find_stack(isect.first)] = find_stack(i);
177  any_stacked = true;
178  }
179  }
180  if (!any_stacked) { return ba; }
181 
182  // std::map keeps the order, and so the joined boxes, the same on every rank
183  std::map<int, std::vector<Box>> stacks;
184  for (int i = 0; i < nboxes; ++i) {
185  stacks[find_stack(i)].push_back(ba[i]);
186  }
187 
188  BoxList bl_joined;
189  for (const auto& stack : stacks)
190  {
191  const std::vector<Box>& boxes = stack.second;
192  if (boxes.size() == 1) {
193  bl_joined.push_back(boxes[0]);
194  continue;
195  }
196 
197  // Cut the stack at every lateral edge of its boxes
198  std::array<std::vector<int>,2> cuts;
199  IntVect max_size(1);
200  for (const auto& b : boxes) {
201  for (int idim = 0; idim < 2; ++idim) {
202  cuts[idim].push_back(b.smallEnd(idim));
203  cuts[idim].push_back(b.bigEnd(idim)+1);
204  max_size[idim] = std::max(max_size[idim], b.length(idim));
205  }
206  }
207  for (auto& c : cuts) {
208  std::sort(c.begin(), c.end());
209  c.erase(std::unique(c.begin(), c.end()), c.end());
210  }
211  auto cut_index = [&cuts] (int idim, int coord) {
212  return static_cast<int>(std::lower_bound(cuts[idim].begin(), cuts[idim].end(), coord)
213  - cuts[idim].begin());
214  };
215 
216  // The z extents of the boxes over each piece, keyed by the piece's cut indices
217  std::map<std::pair<int,int>, std::vector<std::pair<int,int>>> zruns_of_piece;
218  for (const auto& b : boxes) {
219  for (int ix = cut_index(0, b.smallEnd(0)); ix < cut_index(0, b.bigEnd(0)+1); ++ix) {
220  for (int iy = cut_index(1, b.smallEnd(1)); iy < cut_index(1, b.bigEnd(1)+1); ++iy) {
221  zruns_of_piece[std::make_pair(ix,iy)].emplace_back(b.smallEnd(2), b.bigEnd(2));
222  }
223  }
224  }
225 
226  // Over each piece, join the extents that touch into one box per run of cells
227  BoxList bl_stack;
228  for (auto& piece : zruns_of_piece) {
229  const int ix = piece.first.first;
230  const int iy = piece.first.second;
231  auto& zruns = piece.second;
232  std::sort(zruns.begin(), zruns.end());
233  std::size_t n_first = 0;
234  for (std::size_t n = 1; n <= zruns.size(); ++n) {
235  if (n == zruns.size() || zruns[n].first != zruns[n-1].second+1) {
236  bl_stack.push_back(Box(IntVect(cuts[0][ix], cuts[1][iy], zruns[n_first].first),
237  IntVect(cuts[0][ix+1]-1, cuts[1][iy+1]-1, zruns[n-1].second)));
238  n_first = n;
239  }
240  }
241  }
242 
243  // Merging in x and y joins pieces with the same z extent, so it cannot stack them again
244  bl_stack.simplify();
245  max_size[2] = bl_stack.minimalBox().length(2);
246  bl_stack.maxSize(max_size);
247  bl_joined.join(bl_stack);
248  }
249 
250  return BoxArray(std::move(bl_joined));
251 }

Referenced by SurfaceLayer::define_pblh_columns().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_areas()

void make_areas ( const Geometry &  geom,
MultiFab &  z_phys_nd,
MultiFab &  ax,
MultiFab &  ay,
MultiFab &  az 
)

Computation of area fractions on faces

1196 {
1197  const auto* dx = geom.CellSize();
1198  Real dzInv = one/dx[2];
1199 
1200  // Domain valid box (z_nd is nodal)
1201  const Box& domain = geom.Domain();
1202  int domlo_z = domain.smallEnd(2);
1203 
1204  // The z-faces are always full when using terrain-fitted coordinates
1205  az.setVal(1.0);
1206 
1207  //
1208  // x-areas
1209  //
1210 #ifdef _OPENMP
1211 #pragma omp parallel if (Gpu::notInLaunchRegion())
1212 #endif
1213  for ( MFIter mfi(ax, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1214  {
1215  Box gbx = mfi.growntilebox(ax.nGrow());
1216  if (gbx.smallEnd(2) < domlo_z) {
1217  gbx.setSmall(2,domlo_z);
1218  }
1219 
1220  Array4<Real const> z_nd = z_phys_nd.const_array(mfi);
1221  Array4<Real > ax_arr = ax.array(mfi);
1222  ParallelFor(gbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
1223  ax_arr(i, j, k) = Real(.5) * dzInv * (
1224  z_nd(i,j,k+1) + z_nd(i,j+1,k+1) - z_nd(i,j,k) - z_nd(i,j+1,k));
1225  });
1226  }
1227 
1228  //
1229  // y-areas
1230  //
1231 #ifdef _OPENMP
1232 #pragma omp parallel if (Gpu::notInLaunchRegion())
1233 #endif
1234  for ( MFIter mfi(ay, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1235  {
1236  Box gbx = mfi.growntilebox(ay.nGrow());
1237  if (gbx.smallEnd(2) < domlo_z) {
1238  gbx.setSmall(2,domlo_z);
1239  }
1240 
1241  Array4<Real const> z_nd = z_phys_nd.const_array(mfi);
1242  Array4<Real > ay_arr = ay.array(mfi);
1243  ParallelFor(gbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
1244  ay_arr(i, j, k) = Real(.5) * dzInv * (
1245  z_nd(i,j,k+1) + z_nd(i+1,j,k+1) - z_nd(i,j,k) - z_nd(i+1,j,k));
1246  });
1247  }
1248 
1249  ax.FillBoundary(geom.periodicity());
1250  ay.FillBoundary(geom.periodicity());
1251  az.FillBoundary(geom.periodicity());
1252 }

Referenced by ERF::make_metrics_from_finest(), and ERF::update_terrain_arrays().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_J()

void make_J ( const Geometry &  geom,
MultiFab &  z_phys_nd,
MultiFab &  detJ_cc 
)

Computation of detJ at cell-center

1156 {
1157  const auto *dx = geom.CellSize();
1158  Real dzInv = one/dx[2];
1159 
1160  // Domain valid box (z_nd is nodal)
1161  const Box& domain = geom.Domain();
1162  int domlo_z = domain.smallEnd(2);
1163 
1164  // Number of ghost cells
1165  int ngrow= detJ_cc.nGrow();
1166 
1167 #ifdef _OPENMP
1168 #pragma omp parallel if (Gpu::notInLaunchRegion())
1169 #endif
1170  for ( MFIter mfi(detJ_cc, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1171  {
1172  Box gbx = mfi.growntilebox(ngrow);
1173  if (gbx.smallEnd(2) < domlo_z) {
1174  gbx.setSmall(2,domlo_z);
1175  }
1176  Array4<Real const> z_nd = z_phys_nd.const_array(mfi);
1177  Array4<Real > detJ = detJ_cc.array(mfi);
1178  ParallelFor(gbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
1179  detJ(i, j, k) = Real(.25) * dzInv * (
1180  z_nd(i,j,k+1) + z_nd(i+1,j,k+1) + z_nd(i,j+1,k+1) + z_nd(i+1,j+1,k+1)
1181  -z_nd(i,j,k ) - z_nd(i+1,j,k ) - z_nd(i,j+1,k ) - z_nd(i+1,j+1,k ) );
1182  });
1183  }
1184  detJ_cc.FillBoundary(geom.periodicity());
1185 }

Referenced by ERF::build_nested_base_state(), ERF::make_metrics_from_finest(), and ERF::update_terrain_arrays().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_terrain_fitted_coords()

void make_terrain_fitted_coords ( int  lev,
const Geometry &  geom,
MultiFab &  z_phys_nd,
Vector< Real > const &  z_levels_h,
GpuArray< ERF_BC, AMREX_SPACEDIM *2 > &  phys_bc_type,
FineTerrain  fine_terrain,
MultiFab const *  z_phys_interp,
bool  moving_mesh 
)

Computation of the terrain grid from BTF, STF, or Sullivan TF model

332 {
333  const Box& domain = geom.Domain();
334 
335  int domlo_z = domain.smallEnd(2);
336  int domhi_z = domain.bigEnd(2) + 1;
337 
338  // Just in case ...
339  z_phys_nd.setDomainBndry(bogus_large_value,0,1,geom);
340 
341  // ****************************************************************************
342 
343  if (lev == 0) {
344  BoxArray ba(z_phys_nd.boxArray());
345  bool all_boxes_touch_bottom = true;
346  for (int i = 0; i < ba.size(); i++) {
347  if (ba[i].smallEnd(2) != domlo_z) {
348  all_boxes_touch_bottom = false;
349  }
350  }
351 
352  if (all_boxes_touch_bottom) {
353  init_which_terrain_grid(lev, geom, z_phys_nd, z_levels_h, fine_terrain, z_phys_interp);
354  } else {
355 
356  BoxArray ba_new(domain);
357  ChopGrids2D(ba_new, domain, ParallelDescriptor::NProcs());
358 
359  DistributionMapping dm_new(ba_new);
360  ba_new.surroundingNodes();
361 
362  MultiFab z_phys_nd_new(ba_new, dm_new, 1, z_phys_nd.nGrowVect());
363 
364  z_phys_nd_new.ParallelCopy(z_phys_nd,0,0,1,z_phys_nd.nGrowVect(),z_phys_nd.nGrowVect());
365 
366  init_which_terrain_grid(lev, geom, z_phys_nd_new, z_levels_h, fine_terrain, z_phys_interp);
367 
368  z_phys_nd.ParallelCopy(z_phys_nd_new,0,0,1,z_phys_nd.nGrowVect(),z_phys_nd.nGrowVect());
369  }
370  } else { // lev > 0
371  //
372  // BTF maps each column of a box between the box's lowest node and the domain top.
373  // A box that sits on another box of this level must continue the columns of the box
374  // below it: its own lowest node holds only the mesh interpolated from the coarse
375  // level, so starting from there would make the mesh above it follow the coarse
376  // terrain and depend on where the grids are split in z. As on level 0 we therefore
377  // build the mesh on boxes that hold whole columns and copy it back. The bottom box
378  // of a column, such as a refined patch aloft, still starts from its own lowest node.
379  //
380  ParmParse pp("erf");
381  int terrain_smoothing = 0;
382  pp.query("terrain_smoothing", terrain_smoothing);
383 
384  const ColumnGrids* cg = (terrain_smoothing == 0)
385  ? &column_grids(lev, geom, amrex::convert(z_phys_nd.boxArray(), IntVect::TheCellVector()))
386  : nullptr;
387 
388  if (cg == nullptr || !cg->stacked) {
389  init_which_terrain_grid(lev, geom, z_phys_nd, z_levels_h, fine_terrain, z_phys_interp);
390  } else {
391  const IntVect& ng = z_phys_nd.nGrowVect();
392  MultiFab z_phys_nd_col(cg->ba_col_nd, cg->dm_col, 1, ng);
393 
394  z_phys_nd_col.ParallelCopy(z_phys_nd,0,0,1,ng,ng);
395 
396  init_which_terrain_grid(lev, geom, z_phys_nd_col, z_levels_h, fine_terrain, z_phys_interp);
397 
398  // Copy the ghost nodes too, then give every node the value of a box that holds it
399  // as valid, rather than one a neighboring box computed for its ghost node
400  z_phys_nd.ParallelCopy(z_phys_nd_col,0,0,1,ng,ng);
401  z_phys_nd.ParallelCopy(z_phys_nd_col,0,0,1,IntVect(0),ng);
402  }
403  }
404 
405  //
406  // A node on the high side of a periodic direction is the same node as its image on the
407  // low side, and a box that holds one as valid is not touched by FillBoundary -- so give
408  // both copies one value, or the faces on either side of the node see different metrics
409  // and the mass flux through them differs. make_terrain_surface_periodic already does
410  // this for a surface built by init_terrain_surface (and that is what the immersed
411  // boundary reads), but not for one that comes from wrfinput, met_em, a restart or the
412  // interpolation from a coarser level; this covers all of them, and the whole column.
413  // The copy kept is that of the lower-numbered box, which for the usual grids (and
414  // always within one box) is the low side, as in make_terrain_surface_periodic.
415  //
416  // Only the lateral periodicity: in a domain that is also periodic in z, the top node of
417  // a column is the image of the bottom one, but its height is not -- syncing them would
418  // give the top node the bottom's height and the top cell a negative thickness.
419  //
420  if (geom.isPeriodic(0) || geom.isPeriodic(1)) {
421  const Box& dom = geom.Domain();
422  const Periodicity lateral_period(IntVect(AMREX_D_DECL(geom.isPeriodic(0) ? dom.length(0) : 0,
423  geom.isPeriodic(1) ? dom.length(1) : 0,
424  0)));
425  z_phys_nd.OverrideSync(lateral_period);
426  }
427 
428  //
429  // Fill ghost layers and corners (including periodic) -- no matter what level
430  //
431  z_phys_nd.FillBoundary(geom.periodicity());
432 
433  if (phys_bc_type[Orientation(0,Orientation::low )] == ERF_BC::symmetry ||
434  phys_bc_type[Orientation(0,Orientation::high)] == ERF_BC::symmetry ||
435  phys_bc_type[Orientation(1,Orientation::low )] == ERF_BC::symmetry ||
436  phys_bc_type[Orientation(1,Orientation::high)] == ERF_BC::symmetry) {
437 
438  const auto& dom_lo = lbound(convert(domain,IntVect(1,1,1)));
439  const auto& dom_hi = ubound(convert(domain,IntVect(1,1,1)));
440 
441  //
442  // Mirror the whole ghost region, not just its first layer: the base state carries
443  // ComputeGhostCells+1 layers and the state stencils reach several cells past the
444  // boundary, so mirroring only the first would leave the rest of the ghost region
445  // holding whatever the terrain construction extrapolated into it.
446  //
447  const IntVect& ngv = z_phys_nd.nGrowVect();
448 
449  for (MFIter mfi(z_phys_nd,true); mfi.isValid(); ++mfi) {
450  //
451  // Whether this tile reaches a domain boundary is a question about its valid
452  // nodes, so ask it of the tilebox. The grown tilebox begins a ghost width short
453  // of the boundary and so never equals it -- asking it of the grown box is what
454  // kept this mirroring from ever running. The grown box is still what we sweep,
455  // so that the ghost rows in the other two directions are covered as well.
456  //
457  const Box& vbx = mfi.tilebox();
458  const Box& bx = mfi.growntilebox();
459  const Array4< Real> z_nd_arr = z_phys_nd.array(mfi);
460  if (phys_bc_type[Orientation(0,Orientation::low)] == ERF_BC::symmetry && vbx.smallEnd(0) == dom_lo.x) {
461  const int ng = ngv[0];
462  ParallelFor(makeSlab(bx,0,1), [=] AMREX_GPU_DEVICE (int , int j, int k)
463  {
464  for (int n = 1; n <= ng; ++n) {
465  z_nd_arr(dom_lo.x-n,j,k) = z_nd_arr(dom_lo.x+n,j,k);
466  }
467  });
468  }
469  if (phys_bc_type[Orientation(0,Orientation::high)] == ERF_BC::symmetry && vbx.bigEnd(0) == dom_hi.x) {
470  const int ng = ngv[0];
471  ParallelFor(makeSlab(bx,0,1), [=] AMREX_GPU_DEVICE (int , int j, int k)
472  {
473  for (int n = 1; n <= ng; ++n) {
474  z_nd_arr(dom_hi.x+n,j,k) = z_nd_arr(dom_hi.x-n,j,k);
475  }
476  });
477  }
478  if (phys_bc_type[Orientation(1,Orientation::low)] == ERF_BC::symmetry && vbx.smallEnd(1) == dom_lo.y) {
479  const int ng = ngv[1];
480  ParallelFor(makeSlab(bx,1,1), [=] AMREX_GPU_DEVICE (int i, int , int k)
481  {
482  for (int n = 1; n <= ng; ++n) {
483  z_nd_arr(i,dom_lo.y-n,k) = z_nd_arr(i,dom_lo.y+n,k);
484  }
485  });
486  }
487  if (phys_bc_type[Orientation(1,Orientation::high)] == ERF_BC::symmetry && vbx.bigEnd(1) == dom_hi.y) {
488  const int ng = ngv[1];
489  ParallelFor(makeSlab(bx,1,1), [=] AMREX_GPU_DEVICE (int i, int , int k)
490  {
491  for (int n = 1; n <= ng; ++n) {
492  z_nd_arr(i,dom_hi.y+n,k) = z_nd_arr(i,dom_hi.y-n,k);
493  }
494  });
495  }
496  }
497  }
498 
499  //********************************************************************************
500  // Populate domain boundary cells in z-direction
501  //********************************************************************************
502  int ngrow = z_phys_nd.nGrow();
503 
504  for ( MFIter mfi(z_phys_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi )
505  {
506  // Only set values above top of domain if this box reaches that far
507  Box nd_bx = mfi.tilebox();
508 
509  // Note that domhi_z is already nodal in the z-direction
510  if (nd_bx.bigEnd(2) >= domhi_z) {
511  // Grown box with no z range
512  Box bx_zhi = mfi.growntilebox(ngrow);
513  bx_zhi.setSmall(2,domhi_z+1);
514  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
515 
516  // Extrapolate top layer
517  ParallelFor(bx_zhi, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
518  z_arr(i,j,k) = z_arr(i,j,domhi_z)
519  + (k-domhi_z) * (z_arr(i,j,domhi_z) - z_arr(i,j,domhi_z-1));
520  });
521  }
522 
523  // Moving mesh only: the ghost planes below the bottom by linear extrapolation plane by plane,
524  // recomputed here after the periodic fill, so that the lateral ghost columns of a periodic
525  // domain follow the periodic surface (the planes below the ground exist nowhere as valid
526  // data, so the periodic fill cannot repair them). The mesh is rebuilt every stage and
527  // sub-step on a moving mesh, which reads these planes; static meshes keep upstream's fill.
528  if (moving_mesh && nd_bx.smallEnd(2) <= domlo_z) {
529  Box bx_zlo = mfi.growntilebox(ngrow);
530  bx_zlo.setBig(2,domlo_z-1);
531  Array4<Real> const& z_arr = z_phys_nd.array(mfi);
532 
533  for (int kk = domlo_z-1; kk >= bx_zlo.smallEnd(2); --kk) {
534  ParallelFor(makeSlab(bx_zlo,2,kk), [=] AMREX_GPU_DEVICE (int i, int j, int k) {
535  z_arr(i,j,k) = two*z_arr(i,j,k+1) - z_arr(i,j,k+2);
536  });
537  }
538  }
539  }
540 } // make_terrain_fitted_coords
void ChopGrids2D(BoxArray &ba, const Box &domain, int target_size)
Definition: ERF_ChopGrids.cpp:35
void init_which_terrain_grid(int lev, Geometry const &geom, MultiFab &z_phys_nd, Vector< Real > const &z_levels_h, FineTerrain fine_terrain, MultiFab const *z_phys_interp)
Definition: ERF_TerrainMetrics.cpp:714
@ ng
Definition: ERF_Morrison.H:51
real(c_double), private cg
Definition: ERF_module_mp_morr_two_moment.F90:203

Referenced by ERF::build_zphys_nd().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_zcc()

void make_zcc ( const Geometry &  geom,
MultiFab &  z_phys_nd,
MultiFab &  z_phys_cc 
)

Computation of z_phys at cell-center

1261 {
1262 #ifdef _OPENMP
1263 #pragma omp parallel if (Gpu::notInLaunchRegion())
1264 #endif
1265  for ( MFIter mfi(z_phys_cc, TilingIfNotGPU()); mfi.isValid(); ++mfi )
1266  {
1267  Box gbx = mfi.growntilebox();
1268  Array4<Real const> z_nd = z_phys_nd.const_array(mfi);
1269  Array4<Real > z_cc = z_phys_cc.array(mfi);
1270  ParallelFor(gbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
1271  z_cc(i, j, k) = Real(.125) * ( z_nd(i,j,k ) + z_nd(i+1,j,k ) + z_nd(i,j+1,k ) + z_nd(i+1,j+1,k )
1272  +z_nd(i,j,k+1) + z_nd(i+1,j,k+1) + z_nd(i,j+1,k+1) + z_nd(i+1,j+1,k+1) );
1273  });
1274  }
1275  z_phys_cc.FillBoundary(geom.periodicity());
1276 }

Referenced by ERF::build_nested_base_state(), ERF::make_base_state_on_grown_boxes(), ERF::post_timestep(), and ERF::update_terrain_arrays().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_zsfc_nd()

void make_zsfc_nd ( const Geometry &  geom,
const MultiFab &  z_phys_nd,
MultiFab &  z_sfc_nd,
const MultiFab *  z_sfc_crse,
const IntVect &  ref_ratio 
)

Computation of the height of the bottom of the domain at each node, z_sfc_nd

A node of a box that reaches the bottom of the domain takes the height of its own z_phys_nd there. The nodes of any other box, which can occur only on a refined level, are interpolated bilinearly from z_sfc_crse, the coarser level's, which is what the fine z_phys_nd interpolated from the coarse level has at the bottom of the domain.

Parameters
[in]geomgeometry of this level
[in]z_phys_ndnodal heights of this level
[out]z_sfc_nddefined on the 2D projection of z_phys_nd's grids (nodal in x and y, z index 0), with the same DistributionMapping; its ghost cells, at most as many as z_phys_nd's in x and y, are filled too
[in]z_sfc_crsez_sfc_nd of the next coarser level; may be null only if every column of this level has a box reaching the bottom of the domain
[in]ref_ratiorefinement ratio between the coarser level and this one
1301 {
1302  AMREX_ALWAYS_ASSERT(z_sfc_nd.size() == z_phys_nd.size() &&
1303  z_sfc_nd.DistributionMap() == z_phys_nd.DistributionMap());
1304 
1305  // The ghost cells are taken from z_phys_nd's, so it must have at least as many
1306  const IntVect ng(z_sfc_nd.nGrowVect()[0], z_sfc_nd.nGrowVect()[1], 0);
1307  AMREX_ALWAYS_ASSERT(ng[0] <= z_phys_nd.nGrowVect()[0] && ng[1] <= z_phys_nd.nGrowVect()[1]);
1308 
1309  const int klo = geom.Domain().smallEnd(2);
1310  const BoxArray& ba_nd = z_phys_nd.boxArray();
1311 
1312  // The boxes that reach the bottom of the domain, gathered on their own so that their
1313  // values also reach any box stacked above them in the same columns
1314  BoxList bl_bot(z_sfc_nd.ixType());
1315  Vector<int> pmap_bot, idx_bot;
1316  for (int i = 0; i < ba_nd.size(); ++i) {
1317  if (ba_nd[i].smallEnd(2) == klo) {
1318  bl_bot.push_back(z_sfc_nd.boxArray()[i]);
1319  pmap_bot.push_back(z_phys_nd.DistributionMap()[i]);
1320  idx_bot.push_back(i);
1321  }
1322  }
1323 
1324  // In the usual case every box reaches the bottom, so each box of z_sfc_nd lies under its
1325  // own box of z_phys_nd and needs neither the coarser level nor any communication
1326  if (static_cast<int>(idx_bot.size()) == ba_nd.size()) {
1327 #ifdef _OPENMP
1328 #pragma omp parallel if (Gpu::notInLaunchRegion())
1329 #endif
1330  for (MFIter mfi(z_sfc_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi)
1331  {
1332  const Box& bx = mfi.growntilebox(ng);
1333  const Array4< Real>& zs_arr = z_sfc_nd.array(mfi);
1334  const Array4<const Real>& z_nd = z_phys_nd.const_array(mfi);
1335  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int) noexcept
1336  {
1337  zs_arr(i,j,0) = z_nd(i,j,klo);
1338  });
1339  }
1340  return;
1341  }
1342 
1343  z_sfc_nd.setVal(zero);
1344 
1345  if (z_sfc_crse != nullptr) {
1346  // The coarse nodes surrounding the coarse cells under this level's columns
1347  const IntVect rr(ref_ratio[0], ref_ratio[1], 1);
1348  BoxArray ba_crse(z_sfc_nd.boxArray());
1349  ba_crse.grow(ng);
1350  ba_crse.enclosedCells(0); ba_crse.enclosedCells(1);
1351  ba_crse.coarsen(rr);
1352  ba_crse.surroundingNodes(0); ba_crse.surroundingNodes(1);
1353  MultiFab tmp(ba_crse, z_sfc_nd.DistributionMap(), 1, 0);
1354  tmp.ParallelCopy(*z_sfc_crse, 0, 0, 1, z_sfc_crse->nGrowVect(), IntVect(0));
1355 
1356  const Real rx_inv = one / Real(rr[0]);
1357  const Real ry_inv = one / Real(rr[1]);
1358 
1359 #ifdef _OPENMP
1360 #pragma omp parallel if (Gpu::notInLaunchRegion())
1361 #endif
1362  for (MFIter mfi(z_sfc_nd, TilingIfNotGPU()); mfi.isValid(); ++mfi)
1363  {
1364  const Box& bx = mfi.growntilebox(ng);
1365  const Array4< Real>& zs_arr = z_sfc_nd.array(mfi);
1366  const Array4<const Real>& zsc_arr = tmp.const_array(mfi);
1367  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int) noexcept
1368  {
1369  const int ic = amrex::coarsen(i,rr[0]);
1370  const int jc = amrex::coarsen(j,rr[1]);
1371  const Real fx = Real(i - ic*rr[0]) * rx_inv;
1372  const Real fy = Real(j - jc*rr[1]) * ry_inv;
1373  // Coarse nodes not needed (weight zero) may lie outside tmp, so skip them
1374  Real z = (one-fx)*(one-fy) * zsc_arr(ic,jc,0);
1375  if (fx > zero) { z += fx *(one-fy) * zsc_arr(ic+1,jc ,0); }
1376  if (fy > zero) { z += (one-fx) *fy * zsc_arr(ic ,jc+1,0); }
1377  if (fx > zero && fy > zero) { z += fx *fy * zsc_arr(ic+1,jc+1,0); }
1378  zs_arr(i,j,0) = z;
1379  });
1380  }
1381  } else {
1382  // Without a coarser level every column must be covered by a box reaching the bottom
1383  BoxArray ba_bot(bl_bot);
1384  for (int i = 0; i < z_sfc_nd.boxArray().size(); ++i) {
1385  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ba_bot.contains(z_sfc_nd.boxArray()[i]),
1386  "make_zsfc_nd: a column whose box does not reach the bottom needs the coarser level's z_sfc_nd");
1387  }
1388  }
1389 
1390  if (bl_bot.isEmpty()) { return; }
1391 
1392  MultiFab z_sfc_bot(BoxArray(std::move(bl_bot)), DistributionMapping(std::move(pmap_bot)), 1, ng);
1393  for (MFIter mfi(z_sfc_bot); mfi.isValid(); ++mfi)
1394  {
1395  const Box& bx = mfi.fabbox();
1396  const Array4< Real>& zs_arr = z_sfc_bot.array(mfi);
1397  const Array4<const Real>& z_nd = z_phys_nd.const_array(idx_bot[mfi.index()]);
1398  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int) noexcept
1399  {
1400  zs_arr(i,j,0) = z_nd(i,j,klo);
1401  });
1402  }
1403  // The ghost cells of the boxes reaching the bottom reach ghost cells of the others too
1404  z_sfc_nd.ParallelCopy(z_sfc_bot, 0, 0, 1, ng, ng);
1405 }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
const int klo
Definition: ERF_InitCustomPert_KE.H:9
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
@ tmp
Definition: ERF_AdvanceWSM6.cpp:116
real(c_double), private rr
Definition: ERF_module_mp_morr_two_moment.F90:224

Referenced by ERF::update_zsfc_nd().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate_flat_terrain()

void validate_flat_terrain ( int  lev,
const MultiFab &  z_phys_nd,
const Vector< Real > &  z_levels_h 
)
21 {
22  // The comparison is against heights that have been round-tripped through z_phys_nd, so the
23  // threshold has to track the working precision: a hard-wired 1e-10 is far below single-
24  // precision round-off at domain-top magnitudes and would abort on a genuinely flat mesh.
25  // Keep the original (double-precision) threshold, floored at a few hundred ulps.
26  const Real scale = std::max({Real(1.0), std::abs(z_levels_h.front()), std::abs(z_levels_h.back())});
27  const Real rel_tol = std::max(Real(1.0e-10), Real(100.0) * std::numeric_limits<Real>::epsilon());
28  const Real tolerance = rel_tol * scale;
29 
30  MultiFab expected(z_phys_nd.boxArray(), z_phys_nd.DistributionMap(), 1, 0);
31  Gpu::DeviceVector<Real> z_levels_d(z_levels_h.size());
32  Gpu::copy(Gpu::hostToDevice, z_levels_h.begin(), z_levels_h.end(), z_levels_d.begin());
33  const Real* z_levels = z_levels_d.data();
34 
35  for (MFIter mfi(expected); mfi.isValid(); ++mfi) {
36  const Box& bx = mfi.validbox();
37  auto const& expected_arr = expected.array(mfi);
38  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
39  expected_arr(i,j,k) = z_levels[k];
40  });
41  }
42 
43  MultiFab error(expected.boxArray(), expected.DistributionMap(), 1, 0);
44  MultiFab::Copy(error, expected, 0, 0, 1, 0);
45  MultiFab::Subtract(error, z_phys_nd, 0, 0, 1, 0);
46  const Real max_error = error.norminf(0, 0, false);
47 
48  if (max_error > tolerance) {
49  Abort("erf.flat_terrain = true, but z_phys_nd is not horizontally "
50  "flat or does not match the configured vertical spacing at "
51  "level " + std::to_string(lev) + ".");
52  }
53 }

Referenced by ERF::init_zphys().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate_flat_top()

void validate_flat_top ( int  lev,
const Geometry &  geom,
const MultiFab &  z_phys_nd 
)

Abort unless the top of the domain is flat, i.e. z_phys_nd is the same at every node of the top slab. The top boundary is a horizontal plane: erf_slow_rhs_pre sets Omega = rho w on the top face, the anelastic or pseudo-incompressible projection converts it with OmegaFromW, and the two only agree when the terrain slopes vanish there. Every mesh builder pins the top node to z_top, so this guards against a mesh (custom, read from a file or a checkpoint) that does not.

Parameters
[in]levLevel index.
[in]geomGeometry information.
[in]z_phys_ndNodal physical height field.
70 {
71  const int k_top = geom.Domain().bigEnd(2) + 1;
72 
73  ReduceOps<ReduceOpMin, ReduceOpMax> reduce_op;
74  ReduceData<Real, Real> reduce_data(reduce_op);
75  using ReduceTuple = typename decltype(reduce_data)::Type;
76 
77  for (MFIter mfi(z_phys_nd); mfi.isValid(); ++mfi) {
78  const Box& bx = mfi.validbox();
79  if (bx.bigEnd(2) < k_top) { continue; }
80  auto const& z_arr = z_phys_nd.const_array(mfi);
81  reduce_op.eval(makeSlab(bx,2,k_top), reduce_data,
82  [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple {
83  return {z_arr(i,j,k), z_arr(i,j,k)};
84  });
85  }
86 
87  ReduceTuple hv = reduce_data.value(reduce_op);
88  Real z_top_min = amrex::get<0>(hv);
89  Real z_top_max = amrex::get<1>(hv);
90  ParallelDescriptor::ReduceRealMin(z_top_min);
91  ParallelDescriptor::ReduceRealMax(z_top_max);
92 
93  // No box at this level reaches the top of the domain
94  if (z_top_min > z_top_max) { return; }
95 
96  // Same precision-aware tolerance as validate_flat_terrain
97  const Real scale = std::max({Real(1.0), std::abs(z_top_min), std::abs(z_top_max)});
98  const Real rel_tol = std::max(Real(1.0e-10), Real(100.0) * std::numeric_limits<Real>::epsilon());
99 
100  if (z_top_max - z_top_min > rel_tol * scale) {
101  Print() << "Top of the domain at level " << lev << " ranges from "
102  << z_top_min << " to " << z_top_max << std::endl;
103  Abort("The top of the domain must be flat: z_phys_nd varies along the top boundary at level "
104  + std::to_string(lev) + ".");
105  }
106 }

Referenced by ERF::update_terrain_arrays().

Here is the caller graph for this function:

◆ which_fine_terrain()

FineTerrain which_fine_terrain ( )

Translate erf.amr_terrain_refinement into a FineTerrain.

547 {
548  ParmParse pp("erf");
549  std::string amr_terrain_refinement = "interpolate";
550  pp.query("amr_terrain_refinement", amr_terrain_refinement);
551 
552  if (amr_terrain_refinement == "interpolate") {
554  } else if (amr_terrain_refinement == "transform") {
555  return FineTerrain::Transform;
556  }
557 
558  Abort("erf.amr_terrain_refinement = " + amr_terrain_refinement +
559  " is not recognized; it must be \"interpolate\" or \"transform\"");
561 }
@ Interpolate
use the coordinates interpolated from the coarse level as-is

Referenced by ERF::build_zphys_nd().

Here is the call graph for this function:
Here is the caller graph for this function: