ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SEBLandAverageDown.H
Go to the documentation of this file.
1 #ifndef ERF_SEB_LAND_AVERAGE_DOWN_H_
2 #define ERF_SEB_LAND_AVERAGE_DOWN_H_
3 
4 #include <AMReX_BoxArray.H>
5 #include <AMReX_GpuQualifiers.H>
6 #include <AMReX_MFIter.H>
7 #include <AMReX_MultiFab.H>
8 #include <AMReX_Vector.H>
9 #include <AMReX_iMultiFab.H>
10 
11 #include "ERF_SEBLandMask.H"
12 
13 /**
14  * @file ERF_SEBLandAverageDown.H
15  * @brief The average-down of the two-stream force-restore balance's surface fields between
16  * two levels whose land masks hold water.
17  *
18  * The balance runs on land columns only (TwoStreamRadiation::advance), and a water column
19  * keeps the values it had at the start, at the restart, or when its level was built (the
20  * snow layer's water is set to zero there). An average over every fine column would then
21  * mix those held values into a coarse land column on a shore, and would overwrite a coarse
22  * water column that lies under fine land. Here a coarse land column takes the mean of the fine
23  * land columns under it; a coarse water column, and a coarse land column with no fine land
24  * column under it, keep their values.
25  *
26  * With no water column on either level this is not called: ERF then uses
27  * amrex::average_down, as before the mask.
28  *
29  * All fields go in one call: the fine land columns are counted once and one ParallelCopy
30  * carries every field. A pair with a null pointer on either side is left out.
31  *
32  * @param fine the 2D surface fields of the fine level (any number of components each)
33  * @param crse the same fields of the coarse level; updated under the fine level
34  * @param fine_mask the fine level's land mask (0 water), on the grids of @p fine
35  * @param crse_mask the coarse level's land mask (0 water), on the grids of @p crse
36  * @param ratio the refinement ratio, 1 in z
37  */
38 inline void
39 seb_average_down_land (const amrex::Vector<const amrex::MultiFab*>& fine,
40  const amrex::Vector<amrex::MultiFab*>& crse,
41  const amrex::iMultiFab& fine_mask, const amrex::iMultiFab& crse_mask,
42  const amrex::IntVect& ratio)
43 {
44  using amrex::Real;
45  AMREX_ALWAYS_ASSERT(fine.size() == crse.size() && ratio[2] == 1);
46 
47  // The fields in use, and the first component of each in the packed array.
48  amrex::Vector<int> used;
49  amrex::Vector<int> first;
50  int nc = 0;
51  for (int f = 0; f < fine.size(); ++f) {
52  if (fine[f] == nullptr || crse[f] == nullptr) { continue; }
53  AMREX_ALWAYS_ASSERT(fine[f]->nComp() == crse[f]->nComp());
54  seb_assert_land_mask_layout(&fine_mask, *fine[f], "seb_average_down_land (fine)");
55  seb_assert_land_mask_layout(&crse_mask, *crse[f], "seb_average_down_land (coarse)");
56  used.push_back(f);
57  first.push_back(nc);
58  nc += fine[f]->nComp();
59  }
60  if (used.empty()) { return; }
61  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(fine_mask.boxArray().coarsenable(ratio),
62  "seb_average_down_land: the fine grids are not aligned with the refinement ratio");
63 
64  const int rx = ratio[0];
65  const int ry = ratio[1];
66 
67  // The mean over the fine land columns, on the coarsened fine grids; the last component
68  // is the number of land columns it was taken over, counted once for all fields.
69  amrex::BoxArray cba = fine_mask.boxArray();
70  cba.coarsen(ratio);
71  amrex::MultiFab cfine(cba, fine_mask.DistributionMap(), nc + 1, 0);
72  for (amrex::MFIter mfi(cfine, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
73  const amrex::Box& bx = mfi.tilebox();
74  const auto c = cfine.array(mfi);
75  const SEBLandMaskView fm = seb_land_mask_view(&fine_mask, mfi);
76  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
77  {
78  int n_land = 0;
79  for (int jj = ry * j; jj < ry * (j + 1); ++jj) {
80  for (int ii = rx * i; ii < rx * (i + 1); ++ii) {
81  if (!fm.water(ii, jj)) { ++n_land; }
82  }
83  }
84  c(i, j, k, nc) = static_cast<Real>(n_land);
85  });
86  for (int u = 0; u < used.size(); ++u) {
87  const auto f = fine[used[u]]->const_array(mfi);
88  const int nf = fine[used[u]]->nComp();
89  const int c0 = first[u];
90  amrex::ParallelFor(bx, nf, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
91  {
92  // The divisor is chosen first: no division by zero is formed for a coarse
93  // column with no fine land column.
94  const Real n_land = c(i, j, k, nc);
95  const Real divisor = (n_land > Real(0.5)) ? n_land : Real(1.0);
96  Real sum = Real(0.0);
97  for (int jj = ry * j; jj < ry * (j + 1); ++jj) {
98  for (int ii = rx * i; ii < rx * (i + 1); ++ii) {
99  if (!fm.water(ii, jj)) { sum += f(ii, jj, k, n); }
100  }
101  }
102  c(i, j, k, c0 + n) = sum / divisor;
103  });
104  }
105  }
106 
107  // On the coarse grids: zero land columns where no fine level lies above.
108  const amrex::MultiFab& crse0 = *crse[used[0]];
109  amrex::MultiFab cover(crse0.boxArray(), crse0.DistributionMap(), nc + 1, 0);
110  cover.setVal(Real(0.0));
111  cover.ParallelCopy(cfine, 0, 0, nc + 1);
112  for (amrex::MFIter mfi(cover, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) {
113  const amrex::Box& bx = mfi.tilebox();
114  const auto v = cover.const_array(mfi);
115  const SEBLandMaskView cm = seb_land_mask_view(&crse_mask, mfi);
116  for (int u = 0; u < used.size(); ++u) {
117  const auto c = crse[used[u]]->array(mfi);
118  const int nf = crse[used[u]]->nComp();
119  const int c0 = first[u];
120  amrex::ParallelFor(bx, nf, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
121  {
122  if (cm.water(i, j) || v(i, j, k, nc) < Real(0.5)) { return; }
123  c(i, j, k, n) = v(i, j, k, c0 + n);
124  });
125  }
126  }
127 }
128 
129 /** @brief One field: the form above with a single pair. */
130 inline void
131 seb_average_down_land (const amrex::MultiFab& fine, amrex::MultiFab& crse,
132  const amrex::iMultiFab& fine_mask, const amrex::iMultiFab& crse_mask,
133  const amrex::IntVect& ratio)
134 {
135  seb_average_down_land(amrex::Vector<const amrex::MultiFab*>{&fine},
136  amrex::Vector<amrex::MultiFab*>{&crse}, fine_mask, crse_mask, ratio);
137 }
138 
139 #endif
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);})
void seb_average_down_land(const amrex::Vector< const amrex::MultiFab * > &fine, const amrex::Vector< amrex::MultiFab * > &crse, const amrex::iMultiFab &fine_mask, const amrex::iMultiFab &crse_mask, const amrex::IntVect &ratio)
Definition: ERF_SEBLandAverageDown.H:39
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
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