ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SEBLandFill.H
Go to the documentation of this file.
1 #ifndef ERF_SEB_LAND_FILL_H_
2 #define ERF_SEB_LAND_FILL_H_
3 
4 #include <AMReX_BCRec.H>
5 #include <AMReX_FillPatchUtil.H>
6 #include <AMReX_Geometry.H>
7 #include <AMReX_GpuQualifiers.H>
8 #include <AMReX_Interpolater.H>
9 #include <AMReX_MFIter.H>
10 #include <AMReX_MultiFab.H>
11 #include <AMReX_Reduce.H>
12 #include <AMReX_Vector.H>
13 #include <AMReX_iMultiFab.H>
14 
15 #include "ERF_SEBLandMask.H"
16 
17 /**
18  * @file ERF_SEBLandFill.H
19  * @brief The fill of a new fine level's force-restore surface fields from its parent
20  * (ERF::fill_seb_from_coarse).
21  */
22 
23 /**
24  * @brief Fill the halo of a coarse 2D surface field before it is interpolated.
25  *
26  * FillBoundary sets the interior and periodic ghost columns. Across a non-periodic face a
27  * ghost column takes the nearest column inside the domain: the slope stencil of the
28  * interpolater reads it for every boundary type. A periodic ghost column is left as
29  * FillBoundary set it. InterpFromCoarseLevel reads it twice, from this halo and from the
30  * periodic image of the valid column, and the two must agree.
31  */
32 inline void
33 seb_extend_halo (amrex::MultiFab& mf, const amrex::Geometry& geom)
34 {
35  using amrex::Real;
36  mf.FillBoundary(geom.periodicity());
37  const bool clamp_x = !geom.isPeriodic(0);
38  const bool clamp_y = !geom.isPeriodic(1);
39  const auto dlo = amrex::lbound(geom.Domain());
40  const auto dhi = amrex::ubound(geom.Domain());
41  for (amrex::MFIter mfi(mf, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
42  const amrex::Box& gbx = mfi.growntilebox();
43  const amrex::Array4<Real>& a = mf.array(mfi);
44  amrex::ParallelFor(gbx, mf.nComp(), [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
45  {
46  const int ic = clamp_x ? amrex::max(dlo.x, amrex::min(dhi.x, i)) : i;
47  const int jc = clamp_y ? amrex::max(dlo.y, amrex::min(dhi.y, j)) : j;
48  if (ic != i || jc != j) { a(i,j,k,n) = a(ic,jc,k,n); }
49  });
50  }
51 }
52 
53 /**
54  * @brief Fill @p fine from @p crse when the coarse land mask holds water.
55  *
56  * A water column holds a value the balance never updated. The fill keeps it out of the
57  * land columns:
58  *
59  * - A fine column whose parent and the parent's eight neighbors are all land takes the
60  * conservative linear interpolation, as on a level with no water.
61  * - Elsewhere no slope is used: the slope of a coarse column next to water is formed with
62  * the held value. A fine land column over a coarse land column takes the parent's value.
63  * A fine land column over a coarse water column takes the mean of the land columns among
64  * the parent's eight neighbors.
65  * - A fine land column whose parent and all eight neighbors are water has no land source.
66  * It takes the parent's held value and is counted.
67  * - A fine water column in that region takes its parent's value.
68  *
69  * Neighbors beyond a non-periodic domain face are left out. RemakeLevel copies the old
70  * fine values on top afterwards, so only columns new to the level keep this fill.
71  *
72  * @param fine the fine field; valid and ghost columns inside the domain are set
73  * @param crse the coarse field, after seb_extend_halo()
74  * @param crse_mask the coarse land mask, ghost columns filled, at least one ghost column
75  * @param fine_mask the fine land mask with the ghost columns of @p fine, or nullptr (all land)
76  * @param bcs one boundary record per component of @p fine
77  * @return the number of fine land columns with no land source on this rank (not reduced)
78  */
79 inline amrex::Long
80 seb_fill_from_coarse_over_land (amrex::MultiFab& fine, const amrex::MultiFab& crse,
81  const amrex::iMultiFab& crse_mask, const amrex::iMultiFab* fine_mask,
82  const amrex::Geometry& cgeom, const amrex::Geometry& fgeom,
83  const amrex::IntVect& ratio, const amrex::Vector<amrex::BCRec>& bcs)
84 {
85  using amrex::Real;
86  const int nc = fine.nComp();
87  AMREX_ALWAYS_ASSERT(crse.nComp() == nc && bcs.size() == nc);
88  seb_assert_land_mask_layout(&crse_mask, crse, "seb_fill_from_coarse_over_land (coarse)");
89  seb_assert_land_mask_layout(fine_mask, fine, "seb_fill_from_coarse_over_land (fine)");
90  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(crse_mask.nGrow(0) >= 1 && crse_mask.nGrow(1) >= 1 &&
91  crse.nGrow(0) >= 1 && crse.nGrow(1) >= 1,
92  "seb_fill_from_coarse_over_land: the coarse field and mask need one ghost column");
93  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(fine_mask == nullptr || fine_mask->nGrowVect().allGE(fine.nGrowVect()),
94  "seb_fill_from_coarse_over_land: the fine mask has fewer ghost columns than the field");
95 
96  // The interpolation with slopes, as on a level with no water.
97  amrex::InterpFromCoarseLevel(fine, fine.nGrowVect(), amrex::IntVect(0,0,0),
98  crse, 0, 0, nc, cgeom, fgeom, ratio, &amrex::cell_cons_interp, bcs, 0);
99 
100  // Per coarse column: [0, nc) the value a fine land column takes, [nc, 2 nc) the column's
101  // own value, 2 nc whether the column or one of its eight neighbors is water, 2 nc + 1
102  // whether it is water with no land neighbor.
103  const int i_near = 2 * nc;
104  const int i_none = 2 * nc + 1;
105  amrex::MultiFab src(crse.boxArray(), crse.DistributionMap(), 2 * nc + 2, crse.nGrowVect());
106  // The ghost columns at a coarse-fine edge of the coarse level are not set below, and
107  // those of crse and crse_mask there are not refreshed from the level under it. With
108  // the proper nesting ERF runs with (amr.n_proper = 2) no fine column has its parent
109  // there. The zero flag is a stopgap for looser nesting: it leaves such a column with
110  // the interpolation above, which still reads the coarse ghost columns as they are.
111  // Filling them from the level under the coarse one first would be the full fix.
112  src.setBndry(Real(0.0));
113  const bool per_x = cgeom.isPeriodic(0);
114  const bool per_y = cgeom.isPeriodic(1);
115  const auto dlo = amrex::lbound(cgeom.Domain());
116  const auto dhi = amrex::ubound(cgeom.Domain());
117  for (amrex::MFIter mfi(src, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
118  const amrex::Box& bx = mfi.tilebox();
119  const auto s = src.array(mfi);
120  const auto c = crse.const_array(mfi);
121  const SEBLandMaskView cm = seb_land_mask_view(&crse_mask, mfi);
122  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
123  {
124  int n_water = 0;
125  int n_land_nb = 0;
126  for (int jj = j - 1; jj <= j + 1; ++jj) {
127  for (int ii = i - 1; ii <= i + 1; ++ii) {
128  const bool inside = (per_x || (ii >= dlo.x && ii <= dhi.x)) &&
129  (per_y || (jj >= dlo.y && jj <= dhi.y));
130  if (!inside) { continue; }
131  if (cm.water(ii, jj)) { ++n_water; }
132  else if (ii != i || jj != j) { ++n_land_nb; }
133  }
134  }
135  s(i, j, k, i_near) = (n_water > 0) ? Real(1.0) : Real(0.0);
136  s(i, j, k, i_none) = (cm.water(i, j) && n_land_nb == 0) ? Real(1.0) : Real(0.0);
137  });
138  amrex::ParallelFor(bx, nc, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
139  {
140  const Real own = c(i, j, k, n);
141  s(i, j, k, nc + n) = own;
142  if (!cm.water(i, j)) { s(i, j, k, n) = own; return; }
143  Real sum = Real(0.0);
144  int n_land_nb = 0;
145  for (int jj = j - 1; jj <= j + 1; ++jj) {
146  for (int ii = i - 1; ii <= i + 1; ++ii) {
147  const bool inside = (per_x || (ii >= dlo.x && ii <= dhi.x)) &&
148  (per_y || (jj >= dlo.y && jj <= dhi.y));
149  if (!inside || cm.water(ii, jj)) { continue; }
150  sum += c(ii, jj, k, n);
151  ++n_land_nb;
152  }
153  }
154  // The divisor is chosen first: no division by zero is formed.
155  const Real divisor = (n_land_nb > 0) ? static_cast<Real>(n_land_nb) : Real(1.0);
156  s(i, j, k, n) = (n_land_nb > 0) ? sum / divisor : own;
157  });
158  }
159  seb_extend_halo(src, cgeom);
160 
161  // Each fine column takes its parent's entries: no arithmetic, no slope.
162  amrex::MultiFab tmp(fine.boxArray(), fine.DistributionMap(), 2 * nc + 2, fine.nGrowVect());
163  tmp.setVal(Real(0.0));
164  const amrex::Vector<amrex::BCRec> bcs_src(2 * nc + 2, bcs[0]);
165  amrex::InterpFromCoarseLevel(tmp, tmp.nGrowVect(), amrex::IntVect(0,0,0),
166  src, 0, 0, 2 * nc + 2, cgeom, fgeom, ratio, &amrex::pc_interp, bcs_src, 0);
167 
168  amrex::ReduceOps<amrex::ReduceOpSum> reduce_op;
169  amrex::ReduceData<amrex::Long> reduce_data(reduce_op);
170  using ReduceTuple = typename decltype(reduce_data)::Type;
171  for (amrex::MFIter mfi(fine, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
172  const auto f = fine.array(mfi);
173  const auto t = tmp.const_array(mfi);
174  const SEBLandMaskView fm = seb_land_mask_view(fine_mask, mfi);
175  // Ghost columns outside the domain were not filled: their flag is zero.
176  amrex::ParallelFor(mfi.growntilebox(), nc, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
177  {
178  if (t(i, j, k, i_near) > Real(0.5)) {
179  f(i, j, k, n) = fm.water(i, j) ? t(i, j, k, nc + n) : t(i, j, k, n);
180  }
181  });
182  reduce_op.eval(mfi.tilebox(), reduce_data,
183  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept -> ReduceTuple
184  {
185  return { (!fm.water(i, j) && t(i, j, k, i_none) > Real(0.5)) ? amrex::Long(1) : amrex::Long(0) };
186  });
187  }
188  return amrex::get<0>(reduce_data.value(reduce_op));
189 }
190 
191 #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);})
amrex::Long seb_fill_from_coarse_over_land(amrex::MultiFab &fine, const amrex::MultiFab &crse, const amrex::iMultiFab &crse_mask, const amrex::iMultiFab *fine_mask, const amrex::Geometry &cgeom, const amrex::Geometry &fgeom, const amrex::IntVect &ratio, const amrex::Vector< amrex::BCRec > &bcs)
Fill fine from crse when the coarse land mask holds water.
Definition: ERF_SEBLandFill.H:80
void seb_extend_halo(amrex::MultiFab &mf, const amrex::Geometry &geom)
Fill the halo of a coarse 2D surface field before it is interpolated.
Definition: ERF_SEBLandFill.H:33
The one place that says how the two-stream force-restore balance reads the land mask (ERF::lmask_lev:...
SEBLandMaskView seb_land_mask_view(const amrex::iMultiFab *mask, const amrex::MFIter &mfi)
The view of mask on the box of mfi; off when mask is nullptr.
Definition: ERF_SEBLandMask.H:54
void seb_assert_land_mask_layout(const amrex::iMultiFab *mask, const amrex::FabArrayBase &field, const char *who)
Stop when mask is given and is not on the grids of field.
Definition: ERF_SEBLandMask.H:43
@ nc
Definition: ERF_Morrison.H:47
@ t
Definition: ERF_WSM6.H:295
@ tmp
Definition: ERF_AdvanceWSM6.cpp:116
The land mask of one box, for a device kernel. Captured by value.
Definition: ERF_SEBLandMask.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool water(int i, int j) const noexcept
True when the balance does not run on column (i, j).
Definition: ERF_SEBLandMask.H:35