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

The average-down of the two-stream force-restore balance's surface fields between two levels whose land masks hold water. More...

#include <AMReX_BoxArray.H>
#include <AMReX_GpuQualifiers.H>
#include <AMReX_MFIter.H>
#include <AMReX_MultiFab.H>
#include <AMReX_Vector.H>
#include <AMReX_iMultiFab.H>
#include "ERF_SEBLandMask.H"
Include dependency graph for ERF_SEBLandAverageDown.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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)
 
void seb_average_down_land (const amrex::MultiFab &fine, amrex::MultiFab &crse, const amrex::iMultiFab &fine_mask, const amrex::iMultiFab &crse_mask, const amrex::IntVect &ratio)
 One field: the form above with a single pair. More...
 

Detailed Description

The average-down of the two-stream force-restore balance's surface fields between two levels whose land masks hold water.

The balance runs on land columns only (TwoStreamRadiation::advance), and a water column keeps the values it had at the start, at the restart, or when its level was built (the snow layer's water is set to zero there). An average over every fine column would then mix those held values into a coarse land column on a shore, and would overwrite a coarse water column that lies under fine land. Here a coarse land column takes the mean of the fine land columns under it; a coarse water column, and a coarse land column with no fine land column under it, keep their values.

With no water column on either level this is not called: ERF then uses amrex::average_down, as before the mask.

All fields go in one call: the fine land columns are counted once and one ParallelCopy carries every field. A pair with a null pointer on either side is left out.

Parameters
finethe 2D surface fields of the fine level (any number of components each)
crsethe same fields of the coarse level; updated under the fine level
fine_maskthe fine level's land mask (0 water), on the grids of fine
crse_maskthe coarse level's land mask (0 water), on the grids of crse
ratiothe refinement ratio, 1 in z

Function Documentation

◆ seb_average_down_land() [1/2]

void seb_average_down_land ( const amrex::MultiFab &  fine,
amrex::MultiFab &  crse,
const amrex::iMultiFab &  fine_mask,
const amrex::iMultiFab &  crse_mask,
const amrex::IntVect &  ratio 
)
inline

One field: the form above with a single pair.

134 {
135  seb_average_down_land(amrex::Vector<const amrex::MultiFab*>{&fine},
136  amrex::Vector<amrex::MultiFab*>{&crse}, fine_mask, crse_mask, ratio);
137 }
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
Here is the call graph for this function:

◆ seb_average_down_land() [2/2]

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

Referenced by ERF::post_timestep(), and seb_average_down_land().

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