ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfaceGrid.H
Go to the documentation of this file.
1 #ifndef ERF_SURFACE_GRID_H
2 #define ERF_SURFACE_GRID_H
3 
5 
6 #include <AMReX_BoxArray.H>
7 #include <AMReX_DistributionMapping.H>
8 #include <AMReX_Geometry.H>
9 #include <AMReX_MultiFab.H>
10 #include <AMReX_MFIter.H>
11 
12 /**
13  * @file ERF_SurfaceGrid.H
14  * @brief The 2D surface grid of the fire and flood couplings, and the maps
15  * between it and the atmospheric columns
16  *
17  * A 2D grid on the horizontal plane, refined by C relative to one atmospheric
18  * AMR level (erf.fire.anchor_level or erf.flood.anchor_level, the finest level
19  * by default). The grid covers that level's boxes, keeps their x-y
20  * decomposition and their MPI owners, and its index space starts at zero at
21  * the lower corner of the region they cover. FireGrid (ERF_FireGrid.H) and
22  * FloodGrid (ERF_FloodGrid.H) are this grid under each coupling's name.
23  */
24 
25 /**
26  * @struct SurfaceGrid
27  * @brief 2D horizontal surface grid
28  *
29  * A slab one cell thick with C*nx x C*ny cells, where (nx, ny) is the size of
30  * the region the atmospheric level's boxes cover: the whole domain on level 0,
31  * the refined region on a finer level.
32  */
34 {
35  amrex::BoxArray ba; ///< 2D BoxArray (C*nx x C*ny x 1), box n refines atmospheric box n
36  amrex::DistributionMapping dm; ///< The atmospheric level's owners
37  amrex::Geometry geom; ///< 2D Geometry in physical coordinates [m]
38  int C = 1; ///< Refinement factor (erf.fire.grid_ratio, erf.flood.grid_ratio)
39  /// Index, on the atmospheric level the grid refines, of the lower corner of
40  /// the region the grid covers. The grid's own index space starts at zero at
41  /// that corner, as every AMReX Geometry does, so surface cell (i, j) lies in
42  /// atmospheric cell (i / C + atm_lo[0], j / C + atm_lo[1]). Zero on level 0.
43  amrex::IntVect atm_lo {0, 0, 0};
44  int lev = 0; ///< The atmospheric level (anchor_level, resolved)
45 };
46 
47 /**
48  * @brief Construct a refined 2D surface grid over one atmospheric level's boxes
49  *
50  * @param[in] ba_atm BoxArray of the atmospheric level
51  * @param[in] dm_atm DistributionMapping of the atmospheric level
52  * @param[in] geom_atm Geometry of the atmospheric level
53  * @param[in] C Refinement factor (>= 1, aborts otherwise)
54  * @param[in] names The coupling's names, for the messages
55  *
56  * @return SurfaceGrid over the region the boxes cover; its lev is left at 0
57  * for the caller to set.
58  *
59  * 1. Take the k = 0 slab of each box in ba_atm, keeping the box order.
60  * 2. The region is their bounding rectangle, which must be covered exactly
61  * (aborts otherwise); atm_lo is its lower corner.
62  * 3. Shift the slabs by -atm_lo and refine by {C, C, 1}; box n keeps the owner
63  * of atmospheric box n, so dm_atm is reused.
64  * 4. The Geometry spans the region: index space 0 to C * size - 1, the
65  * physical extent of the region (the domain's own bounds where the region
66  * spans the domain), periodic only in a direction the region spans and the
67  * atmosphere is periodic in.
68  */
69 SurfaceGrid create_surface_grid (const amrex::BoxArray& ba_atm,
70  const amrex::DistributionMapping& dm_atm,
71  const amrex::Geometry& geom_atm,
72  int C,
73  const SurfaceCouplingNames& names);
74 
75 /**
76  * True when box n of a and box n of b cover the same columns (the same x-y
77  * extent) for every n; their z extents may differ.
78  */
79 inline bool surface_same_columns (const amrex::BoxArray& a, const amrex::BoxArray& b)
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 }
91 
92 /**
93  * True when box n of the surface grid refines the columns of box n of the
94  * atmospheric level (shifted by -atm_lo, refined by {C, C, 1}) for every n,
95  * the two have the same owners, and both are cell-centred (the maps loop over
96  * the valid cells, so a nodal box would reach one column past its pair).
97  */
98 inline bool surface_pairs_with_atm (const amrex::MultiFab& surf_mf, const amrex::MultiFab& atm_mf,
99  const SurfaceGrid& sg)
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 }
120 
121 /**
122  * Exchange the ghost cells of a cell-centred MultiFab on a surface grid (or
123  * on the atmospheric columns it covers) and fill the ones outside a
124  * non-periodic face of the domain by copying the nearest interior value.
125  *
126  * FillBoundary alone leaves those cells untouched, so with an inflow-outflow
127  * atmosphere (the surface grid inherits its periodicity) a stencil that reads
128  * past the region's edge would read whatever the allocator had left there.
129  */
130 inline void surface_fill_boundary (amrex::MultiFab& mf, const amrex::Geometry& geom,
131  const SurfaceCouplingNames& names)
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 }
160 
161 /**
162  * Copy component acomp of a k = 0 atmospheric field into component scomp of a
163  * surface-grid field: every surface cell takes the value of the atmospheric
164  * column it lies in, (i / C + atm_lo[0], j / C + atm_lo[1]). Box n of the
165  * surface grid refines box n of the atmospheric level and has the same owner,
166  * so one MFIter serves both MultiFabs. A per-area quantity (precipitation in
167  * kg/m2) keeps its column total: each of the C^2 cells under a column receives
168  * the column's value per unit area.
169  */
170 inline void surface_fill_from_atm_k0 (amrex::MultiFab& surf_mf, const amrex::MultiFab& atm_k0,
171  const SurfaceGrid& sg, const SurfaceCouplingNames& names,
172  int scomp = 0, int acomp = 0)
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 }
191 
192 /**
193  * Average a surface-grid field over each atmospheric column (the C x C surface
194  * cells under it) into the k = 0 slabs of the grid's level: surface cells
195  * (i - lo_x) C + r, (j - lo_y) C + q for r, q in [0, C) average into
196  * atmospheric cell (i, j). Box n of the output is the k = 0 slab of
197  * atmospheric box n; an output box taller than one cell gets the average at
198  * every k of its valid region. On level 0, with k = 0 slabs, this is
199  * amrex::average_down() with ratio (C, C, 1), summed in the same order.
200  */
201 inline void surface_coarsen_to_atm (amrex::MultiFab& atm_out, const amrex::MultiFab& surf,
202  const SurfaceGrid& sg, const SurfaceCouplingNames& names)
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 }
232 
233 #endif
GpuArray< int, AMREX_SPACEDIM > dhi
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dlo
Definition: ERF_ActuatorSpreading.cpp:132
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);})
The names a surface coupling uses in its inputs and messages.
bool surface_pairs_with_atm(const amrex::MultiFab &surf_mf, const amrex::MultiFab &atm_mf, const SurfaceGrid &sg)
Definition: ERF_SurfaceGrid.H:98
void surface_fill_boundary(amrex::MultiFab &mf, const amrex::Geometry &geom, const SurfaceCouplingNames &names)
Definition: ERF_SurfaceGrid.H:130
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)
Definition: ERF_SurfaceGrid.H:170
bool surface_same_columns(const amrex::BoxArray &a, const amrex::BoxArray &b)
Definition: ERF_SurfaceGrid.H:79
void surface_coarsen_to_atm(amrex::MultiFab &atm_out, const amrex::MultiFab &surf, const SurfaceGrid &sg, const SurfaceCouplingNames &names)
Definition: ERF_SurfaceGrid.H:201
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.
@ ng
Definition: ERF_Morrison.H:51
@ q
Definition: ERF_WSM6.H:296
Definition: ERF_SurfaceCouplingNames.H:18
std::string tag() const
"[FIRE]"
Definition: ERF_SurfaceCouplingNames.H:24
std::string grid() const
"fire grid"
Definition: ERF_SurfaceCouplingNames.H:28
2D horizontal surface grid
Definition: ERF_SurfaceGrid.H:34
amrex::Geometry geom
2D Geometry in physical coordinates [m]
Definition: ERF_SurfaceGrid.H:37
int lev
The atmospheric level (anchor_level, resolved)
Definition: ERF_SurfaceGrid.H:44
amrex::IntVect atm_lo
Definition: ERF_SurfaceGrid.H:43
amrex::BoxArray ba
2D BoxArray (C*nx x C*ny x 1), box n refines atmospheric box n
Definition: ERF_SurfaceGrid.H:35
amrex::DistributionMapping dm
The atmospheric level's owners.
Definition: ERF_SurfaceGrid.H:36
int C
Definition: ERF_SurfaceGrid.H:38