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

The fill of a new fine level's force-restore surface fields from its parent (ERF::fill_seb_from_coarse). More...

#include <AMReX_BCRec.H>
#include <AMReX_FillPatchUtil.H>
#include <AMReX_Geometry.H>
#include <AMReX_GpuQualifiers.H>
#include <AMReX_Interpolater.H>
#include <AMReX_MFIter.H>
#include <AMReX_MultiFab.H>
#include <AMReX_Reduce.H>
#include <AMReX_Vector.H>
#include <AMReX_iMultiFab.H>
#include "ERF_SEBLandMask.H"
Include dependency graph for ERF_SEBLandFill.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void seb_extend_halo (amrex::MultiFab &mf, const amrex::Geometry &geom)
 Fill the halo of a coarse 2D surface field before it is interpolated. More...
 
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. More...
 

Detailed Description

The fill of a new fine level's force-restore surface fields from its parent (ERF::fill_seb_from_coarse).

Function Documentation

◆ seb_extend_halo()

void seb_extend_halo ( amrex::MultiFab &  mf,
const amrex::Geometry &  geom 
)
inline

Fill the halo of a coarse 2D surface field before it is interpolated.

FillBoundary sets the interior and periodic ghost columns. Across a non-periodic face a ghost column takes the nearest column inside the domain: the slope stencil of the interpolater reads it for every boundary type. A periodic ghost column is left as FillBoundary set it. InterpFromCoarseLevel reads it twice, from this halo and from the periodic image of the valid column, and the two must agree.

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 }
GpuArray< int, AMREX_SPACEDIM > dhi
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dlo
Definition: ERF_ActuatorSpreading.cpp:132
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);})

Referenced by ERF::fill_seb_from_coarse(), ERF::interp_psfc_from_coarse(), and seb_fill_from_coarse_over_land().

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

◆ seb_fill_from_coarse_over_land()

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 
)
inline

Fill fine from crse when the coarse land mask holds water.

A water column holds a value the balance never updated. The fill keeps it out of the land columns:

  • A fine column whose parent and the parent's eight neighbors are all land takes the conservative linear interpolation, as on a level with no water.
  • Elsewhere no slope is used: the slope of a coarse column next to water is formed with the held value. A fine land column over a coarse land column takes the parent's value. A fine land column over a coarse water column takes the mean of the land columns among the parent's eight neighbors.
  • A fine land column whose parent and all eight neighbors are water has no land source. It takes the parent's held value and is counted.
  • A fine water column in that region takes its parent's value.

Neighbors beyond a non-periodic domain face are left out. RemakeLevel copies the old fine values on top afterwards, so only columns new to the level keep this fill.

Parameters
finethe fine field; valid and ghost columns inside the domain are set
crsethe coarse field, after seb_extend_halo()
crse_maskthe coarse land mask, ghost columns filled, at least one ghost column
fine_maskthe fine land mask with the ghost columns of fine, or nullptr (all land)
bcsone boundary record per component of fine
Returns
the number of fine land columns with no land source on this rank (not reduced)
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 }
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)
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
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

Referenced by ERF::fill_seb_from_coarse().

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