ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfaceGrid.H File Reference

The 2D surface grid of the fire and flood couplings, and the maps between it and the atmospheric columns. More...

#include <ERF_SurfaceCouplingNames.H>
#include <AMReX_BoxArray.H>
#include <AMReX_DistributionMapping.H>
#include <AMReX_Geometry.H>
#include <AMReX_MultiFab.H>
#include <AMReX_MFIter.H>
Include dependency graph for ERF_SurfaceGrid.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  SurfaceGrid
 2D horizontal surface grid More...
 

Functions

SurfaceGrid create_surface_grid (const amrex::BoxArray &ba_atm, const amrex::DistributionMapping &dm_atm, const amrex::Geometry &geom_atm, int C, const SurfaceCouplingNames &names)
 Construct a refined 2D surface grid over one atmospheric level's boxes. More...
 
bool surface_same_columns (const amrex::BoxArray &a, const amrex::BoxArray &b)
 
bool surface_pairs_with_atm (const amrex::MultiFab &surf_mf, const amrex::MultiFab &atm_mf, const SurfaceGrid &sg)
 
void surface_fill_boundary (amrex::MultiFab &mf, const amrex::Geometry &geom, const SurfaceCouplingNames &names)
 
void surface_fill_from_atm_k0 (amrex::MultiFab &surf_mf, const amrex::MultiFab &atm_k0, const SurfaceGrid &sg, const SurfaceCouplingNames &names, int scomp=0, int acomp=0)
 
void surface_coarsen_to_atm (amrex::MultiFab &atm_out, const amrex::MultiFab &surf, const SurfaceGrid &sg, const SurfaceCouplingNames &names)
 

Detailed Description

The 2D surface grid of the fire and flood couplings, and the maps between it and the atmospheric columns.

A 2D grid on the horizontal plane, refined by C relative to one atmospheric AMR level (erf.fire.anchor_level or erf.flood.anchor_level, the finest level by default). The grid covers that level's boxes, keeps their x-y decomposition and their MPI owners, and its index space starts at zero at the lower corner of the region they cover. FireGrid (ERF_FireGrid.H) and FloodGrid (ERF_FloodGrid.H) are this grid under each coupling's name.

Function Documentation

◆ create_surface_grid()

SurfaceGrid create_surface_grid ( const amrex::BoxArray &  ba_atm,
const amrex::DistributionMapping &  dm_atm,
const amrex::Geometry &  geom_atm,
int  C,
const SurfaceCouplingNames &  names 
)

Construct a refined 2D surface grid over one atmospheric level's boxes.

Parameters
[in]ba_atmBoxArray of the atmospheric level
[in]dm_atmDistributionMapping of the atmospheric level
[in]geom_atmGeometry of the atmospheric level
[in]CRefinement factor (>= 1, aborts otherwise)
[in]namesThe coupling's names, for the messages
Returns
SurfaceGrid over the region the boxes cover; its lev is left at 0 for the caller to set.
  1. Take the k = 0 slab of each box in ba_atm, keeping the box order.
  2. The region is their bounding rectangle, which must be covered exactly (aborts otherwise); atm_lo is its lower corner.
  3. Shift the slabs by -atm_lo and refine by {C, C, 1}; box n keeps the owner of atmospheric box n, so dm_atm is reused.
  4. The Geometry spans the region: index space 0 to C * size - 1, the physical extent of the region (the domain's own bounds where the region spans the domain), periodic only in a direction the region spans and the atmosphere is periodic in.

◆ surface_coarsen_to_atm()

void surface_coarsen_to_atm ( amrex::MultiFab &  atm_out,
const amrex::MultiFab &  surf,
const SurfaceGrid &  sg,
const SurfaceCouplingNames &  names 
)
inline

Average a surface-grid field over each atmospheric column (the C x C surface cells under it) into the k = 0 slabs of the grid's level: surface cells (i - lo_x) C + r, (j - lo_y) C + q for r, q in [0, C) average into atmospheric cell (i, j). Box n of the output is the k = 0 slab of atmospheric box n; an output box taller than one cell gets the average at every k of its valid region. On level 0, with k = 0 slabs, this is amrex::average_down() with ratio (C, C, 1), summed in the same order.

203 {
205  surface_pairs_with_atm(surf, atm_out, sg),
206  (names.tag() + " Averaging the " + names.grid()
207  + " onto the atmospheric columns: the two must pair box for box").c_str());
208  AMREX_ALWAYS_ASSERT(atm_out.nComp() >= surf.nComp());
209  const int C = sg.C;
210  const int lo_x = sg.atm_lo[0];
211  const int lo_y = sg.atm_lo[1];
212  const int ncomp = surf.nComp();
213  const amrex::Real inv_cells = amrex::Real(1.0) / amrex::Real(C * C);
214  for (amrex::MFIter mfi(atm_out); mfi.isValid(); ++mfi) {
215  auto crse = atm_out.array(mfi);
216  auto fine = surf.const_array(mfi);
217  const int kf = surf.boxArray()[mfi.index()].smallEnd(2);
218  amrex::ParallelFor(mfi.validbox(), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
219  {
220  const int ii = (i - lo_x) * C;
221  const int jj = (j - lo_y) * C;
222  amrex::Real c = amrex::Real(0.0);
223  for (int q = 0; q < C; ++q) {
224  for (int r = 0; r < C; ++r) {
225  c += fine(ii + r, jj + q, kf, n);
226  }
227  }
228  crse(i, j, k, n) = inv_cells * c;
229  });
230  }
231 }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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);})
bool surface_pairs_with_atm(const amrex::MultiFab &surf_mf, const amrex::MultiFab &atm_mf, const SurfaceGrid &sg)
Definition: ERF_SurfaceGrid.H:98
@ q
Definition: ERF_WSM6.H:296
std::string tag() const
"[FIRE]"
Definition: ERF_SurfaceCouplingNames.H:24
std::string grid() const
"fire grid"
Definition: ERF_SurfaceCouplingNames.H:28
amrex::IntVect atm_lo
Definition: ERF_SurfaceGrid.H:43
int C
Definition: ERF_SurfaceGrid.H:38

Referenced by coarsen_fire_flux_to_atm(), and coarsen_flood_to_atm().

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

◆ surface_fill_boundary()

void surface_fill_boundary ( amrex::MultiFab &  mf,
const amrex::Geometry &  geom,
const SurfaceCouplingNames &  names 
)
inline

Exchange the ghost cells of a cell-centred MultiFab on a surface grid (or on the atmospheric columns it covers) and fill the ones outside a non-periodic face of the domain by copying the nearest interior value.

FillBoundary alone leaves those cells untouched, so with an inflow-outflow atmosphere (the surface grid inherits its periodicity) a stencil that reads past the region's edge would read whatever the allocator had left there.

132 {
133  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(mf.ixType().cellCentered(),
134  (names.tag() + " Filling the ghost cells of the " + names.grid()
135  + ": the MultiFab must be cell-centred").c_str());
136  mf.FillBoundary(geom.periodicity());
137  const auto ng = mf.nGrowVect();
138  const bool px = geom.isPeriodic(0) || ng[0] == 0;
139  const bool py = geom.isPeriodic(1) || ng[1] == 0;
140  const bool pz = geom.isPeriodic(2) || ng[2] == 0;
141  if (px && py && pz) { return; } // nothing outside a non-periodic face to fill
142  const amrex::Box& dom = geom.Domain();
143  const auto dlo = dom.smallEnd();
144  const auto dhi = dom.bigEnd();
145  const int ncomp = mf.nComp();
146  for (amrex::MFIter mfi(mf); mfi.isValid(); ++mfi) {
147  const amrex::Box& gbx = mfi.fabbox();
148  if (dom.contains(gbx)) { continue; }
149  auto a = mf.array(mfi);
150  amrex::ParallelFor(gbx, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
151  {
152  int ic = i, jc = j, kc = k;
153  if (!px) { ic = amrex::min(amrex::max(i, dlo[0]), dhi[0]); }
154  if (!py) { jc = amrex::min(amrex::max(j, dlo[1]), dhi[1]); }
155  if (!pz) { kc = amrex::min(amrex::max(k, dlo[2]), dhi[2]); }
156  if (ic != i || jc != j || kc != k) { a(i, j, k, n) = a(ic, jc, kc, n); }
157  });
158  }
159 }
GpuArray< int, AMREX_SPACEDIM > dhi
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dlo
Definition: ERF_ActuatorSpreading.cpp:132
@ ng
Definition: ERF_Morrison.H:51

Referenced by fire_fill_boundary(), and flood_fill_boundary().

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

◆ surface_fill_from_atm_k0()

void surface_fill_from_atm_k0 ( amrex::MultiFab &  surf_mf,
const amrex::MultiFab &  atm_k0,
const SurfaceGrid &  sg,
const SurfaceCouplingNames &  names,
int  scomp = 0,
int  acomp = 0 
)
inline

Copy component acomp of a k = 0 atmospheric field into component scomp of a surface-grid field: every surface cell takes the value of the atmospheric column it lies in, (i / C + atm_lo[0], j / C + atm_lo[1]). Box n of the surface grid refines box n of the atmospheric level and has the same owner, so one MFIter serves both MultiFabs. A per-area quantity (precipitation in kg/m2) keeps its column total: each of the C^2 cells under a column receives the column's value per unit area.

173 {
175  surface_pairs_with_atm(surf_mf, atm_k0, sg),
176  (names.tag() + " Copying the atmospheric columns to the " + names.grid()
177  + ": the two must pair box for box").c_str());
178  const int C = sg.C;
179  const int lo_x = sg.atm_lo[0];
180  const int lo_y = sg.atm_lo[1];
181  for (amrex::MFIter mfi(surf_mf, false); mfi.isValid(); ++mfi) {
182  auto f = surf_mf.array(mfi);
183  auto a = atm_k0.const_array(mfi);
184  const int ka = atm_k0.boxArray()[mfi.index()].smallEnd(2);
185  amrex::ParallelFor(mfi.validbox(), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
186  {
187  f(i, j, k, scomp) = a(i / C + lo_x, j / C + lo_y, ka, acomp);
188  });
189  }
190 }

Referenced by fill_fire_from_atm_k0(), and fill_flood_from_atm_k0().

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

◆ surface_pairs_with_atm()

bool surface_pairs_with_atm ( const amrex::MultiFab &  surf_mf,
const amrex::MultiFab &  atm_mf,
const SurfaceGrid &  sg 
)
inline

True when box n of the surface grid refines the columns of box n of the atmospheric level (shifted by -atm_lo, refined by {C, C, 1}) for every n, the two have the same owners, and both are cell-centred (the maps loop over the valid cells, so a nodal box would reach one column past its pair).

100 {
101  const amrex::BoxArray& sba = surf_mf.boxArray();
102  const amrex::BoxArray& aba = atm_mf.boxArray();
103  if (!sba.ixType().cellCentered() || !aba.ixType().cellCentered()) {
104  return false;
105  }
106  if (sba.size() != aba.size() || surf_mf.DistributionMap() != atm_mf.DistributionMap()) {
107  return false;
108  }
109  for (int n = 0; n < aba.size(); ++n) {
110  amrex::Box ab = aba[n];
111  amrex::Box sb = sba[n];
112  ab.setSmall(2, 0); ab.setBig(2, 0);
113  sb.setSmall(2, 0); sb.setBig(2, 0);
114  ab.shift(-sg.atm_lo);
115  ab.refine(amrex::IntVect(sg.C, sg.C, 1));
116  if (ab != sb) { return false; }
117  }
118  return true;
119 }

Referenced by flood_pairs_with_atm(), surface_coarsen_to_atm(), and surface_fill_from_atm_k0().

Here is the caller graph for this function:

◆ surface_same_columns()

bool surface_same_columns ( const amrex::BoxArray &  a,
const amrex::BoxArray &  b 
)
inline

True when box n of a and box n of b cover the same columns (the same x-y extent) for every n; their z extents may differ.

80 {
81  if (a.size() != b.size()) { return false; }
82  for (int n = 0; n < a.size(); ++n) {
83  amrex::Box ba = a[n];
84  amrex::Box bb = b[n];
85  ba.setSmall(2, 0); ba.setBig(2, 0);
86  bb.setSmall(2, 0); bb.setBig(2, 0);
87  if (ba != bb) { return false; }
88  }
89  return true;
90 }

Referenced by FireCoupler::fill_plot_var(), and flood_same_columns().

Here is the caller graph for this function: