1 #ifndef ERF_SEB_LAND_FILL_H_
2 #define ERF_SEB_LAND_FILL_H_
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>
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
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); }
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)
86 const int nc = fine.nComp();
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");
94 "seb_fill_from_coarse_over_land: the fine mask has fewer ghost columns than the field");
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);
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());
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);
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; }
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);
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);
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);
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;
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);
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);
176 amrex::ParallelFor(mfi.growntilebox(),
nc, [=] AMREX_GPU_DEVICE (
int i,
int j,
int k,
int n) noexcept
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);
182 reduce_op.eval(mfi.tilebox(), reduce_data,
183 [=] AMREX_GPU_DEVICE (
int i,
int j,
int k) noexcept -> ReduceTuple
185 return { (!fm.
water(i, j) &&
t(i, j, k, i_none) > Real(0.5)) ? amrex::Long(1) : amrex::Long(0) };
188 return amrex::get<0>(reduce_data.value(reduce_op));
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