ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TypedInterp.H
Go to the documentation of this file.
1 #ifndef ERF_TYPED_INTERP_H_
2 #define ERF_TYPED_INTERP_H_
3 
4 #include <AMReX_FillPatchUtil.H>
5 #include <AMReX_Interpolater.H>
6 #include <AMReX_MFInterp_C.H>
7 #include <AMReX_MultiFab.H>
8 
9 #include <limits>
10 
11 /**
12  * Conservative linear interpolation that does not mix cells of different types, in the spirit
13  * of WRF's land/water-masked nest interpolation (erf-model/ERF#4390). Every cell is
14  * interpolated; a cell's type decides only which neighbors its slopes and limiter may see.
15  * The types are whatever the caller encodes: land and water (SLM's state), a valid value and
16  * the lsm_undefined sentinel (Noah-MP), or both at once (SLM's fluxes).
17  *
18  * The coarse data carries nfield fields followed by nind type indicators (one value per type;
19  * NaN, no type, outside the domain). With nind == 1 every field shares the one indicator; with
20  * nind == nfield field n has indicator n. In each direction the slope of a coarse cell is taken
21  * only if both of its neighbors are of the same type as the cell; otherwise it is zero, as it
22  * is at a domain boundary. The min/max limiter likewise looks only at neighbors of the same
23  * type. Where every neighbor is of the same type this is exactly AMReX's cell_cons_interp
24  * (CellConservativeLinear without linear limiting), so the result changes only next to a type
25  * boundary. Each coarse cell's mean is kept, and the indicator is copied (piecewise constant),
26  * so a cell whose neighbors are all of other types (an isolated sentinel, say) comes through
27  * exactly.
28  */
30  : public amrex::Interpolater
31 {
32 public:
33  explicit TypedConsLinear (int nind = 1) : m_nind(nind) {}
34 
35  amrex::Box CoarseBox (const amrex::Box& fine, const amrex::IntVect& ratio) override
36  {
37  amrex::Box crse = amrex::coarsen(fine, ratio);
38  for (int dim = 0; dim < AMREX_SPACEDIM; ++dim) {
39  if (ratio[dim] > 1) { crse.grow(dim, 1); }
40  }
41  return crse;
42  }
43 
44  amrex::Box CoarseBox (const amrex::Box& fine, int ratio) override
45  {
46  return CoarseBox(fine, amrex::IntVect(ratio));
47  }
48 
49  void interp (const amrex::FArrayBox& crse, int crse_comp,
50  amrex::FArrayBox& fine, int fine_comp, int ncomp,
51  const amrex::Box& fine_region, const amrex::IntVect& ratio,
52  const amrex::Geometry& crse_geom, const amrex::Geometry& /*fine_geom*/,
53  amrex::Vector<amrex::BCRec> const& bcr,
54  int /*actual_comp*/, int /*actual_state*/, amrex::RunOn runon) override;
55 
56 private:
57  int m_nind;
58 };
59 
60 /**
61  * Slopes of the field (component nu) of coarse cell (i,j,k), from neighbors of its own type only
62  * (indicator in component nm). Follows mf_cell_cons_lin_interp_mcslope term for term; the slope
63  * in direction d goes to component ns + d*nslope, the layout mf_cell_cons_lin_interp reads.
64  */
65 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
66 void typed_mcslope (int i, int j, int k, amrex::Array4<amrex::Real> const& slope,
67  int ns, int nslope,
68  amrex::Array4<amrex::Real const> const& u, int nu, int nm,
69  amrex::Box const& domain, amrex::IntVect const& ratio,
70  amrex::BCRec const& bc) noexcept
71 {
72  using amrex::Real;
73  // NaN (outside the domain) is never of the same type
74  const Real m0 = u(i,j,k,nm);
75  auto same = [&] (int ii, int jj, int kk) { return u(ii,jj,kk,nm) == m0; };
76 
77  Real sx = Real(0.);
78  Real sy = Real(0.);
79  Real sz = Real(0.);
80 
81  if (ratio[0] > 1 && same(i-1,j,k) && same(i+1,j,k)) {
82  Real dc = amrex::mf_compute_slopes_x(i, j, k, u, nu, domain, bc);
83  Real df = Real(2.0) * (u(i+1,j,k,nu) - u(i ,j,k,nu));
84  Real db = Real(2.0) * (u(i ,j,k,nu) - u(i-1,j,k,nu));
85  sx = (df*db >= Real(0.0)) ? amrex::min(std::abs(df),std::abs(db)) : Real(0.);
86  sx = std::copysign(Real(1.),dc)*amrex::min(sx,std::abs(dc));
87  }
88  if (ratio[1] > 1 && same(i,j-1,k) && same(i,j+1,k)) {
89  Real dc = amrex::mf_compute_slopes_y(i, j, k, u, nu, domain, bc);
90  Real df = Real(2.0) * (u(i,j+1,k,nu) - u(i,j ,k,nu));
91  Real db = Real(2.0) * (u(i,j ,k,nu) - u(i,j-1,k,nu));
92  sy = (df*db >= Real(0.0)) ? amrex::min(std::abs(df),std::abs(db)) : Real(0.);
93  sy = std::copysign(Real(1.),dc)*amrex::min(sy,std::abs(dc));
94  }
95  if (ratio[2] > 1 && same(i,j,k-1) && same(i,j,k+1)) {
96  Real dc = amrex::mf_compute_slopes_z(i, j, k, u, nu, domain, bc);
97  Real df = Real(2.0) * (u(i,j,k+1,nu) - u(i,j,k ,nu));
98  Real db = Real(2.0) * (u(i,j,k ,nu) - u(i,j,k-1,nu));
99  sz = (df*db >= Real(0.0)) ? amrex::min(std::abs(df),std::abs(db)) : Real(0.);
100  sz = std::copysign(Real(1.),dc)*amrex::min(sz,std::abs(dc));
101  }
102 
103  Real alpha = Real(1.0);
104  if (sx != Real(0.0) || sy != Real(0.0) || sz != Real(0.0)) {
105  Real dumax = std::abs(sx) * Real(ratio[0]-1)/Real(2*ratio[0])
106  + std::abs(sy) * Real(ratio[1]-1)/Real(2*ratio[1])
107  + std::abs(sz) * Real(ratio[2]-1)/Real(2*ratio[2]);
108  Real umax = u(i,j,k,nu);
109  Real umin = u(i,j,k,nu);
110  const int ilim = ratio[0] > 1 ? 1 : 0;
111  const int jlim = ratio[1] > 1 ? 1 : 0;
112  const int klim = ratio[2] > 1 ? 1 : 0;
113  for (int koff = -klim; koff <= klim; ++koff) {
114  for (int joff = -jlim; joff <= jlim; ++joff) {
115  for (int ioff = -ilim; ioff <= ilim; ++ioff) {
116  if (same(i+ioff,j+joff,k+koff)) {
117  umin = amrex::min(umin, u(i+ioff,j+joff,k+koff,nu));
118  umax = amrex::max(umax, u(i+ioff,j+joff,k+koff,nu));
119  }
120  }}}
121  // dumax > 0 here; this only keeps an if-converted divide below from dividing by zero
122  dumax = dumax == Real(0.) ? Real(1.) : dumax;
123  if (dumax * alpha > (umax - u(i,j,k,nu))) {
124  alpha = (umax - u(i,j,k,nu)) / dumax;
125  }
126  if (dumax * alpha > (u(i,j,k,nu) - umin)) {
127  alpha = (u(i,j,k,nu) - umin) / dumax;
128  }
129  }
130 
131  slope(i,j,k,ns ) = sx * alpha;
132  slope(i,j,k,ns+ nslope) = sy * alpha;
133  slope(i,j,k,ns+2*nslope) = sz * alpha;
134 }
135 
136 inline void
137 TypedConsLinear::interp (const amrex::FArrayBox& crse, int crse_comp,
138  amrex::FArrayBox& fine, int fine_comp, int ncomp,
139  const amrex::Box& fine_region, const amrex::IntVect& ratio,
140  const amrex::Geometry& crse_geom, const amrex::Geometry& /*fine_geom*/,
141  amrex::Vector<amrex::BCRec> const& bcr,
142  int /*actual_comp*/, int /*actual_state*/, amrex::RunOn runon)
143 {
144  using namespace amrex;
145  const int nind = m_nind;
146  const int nfield = ncomp - nind;
147  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nfield >= 1 && (nind == 1 || nind == nfield),
148  "TypedConsLinear interpolates nfield fields followed by 1 or nfield type indicators");
149  AMREX_ASSERT(fine.box().contains(fine_region));
150  AMREX_ASSERT(!bcr.empty());
151 
152  const Box& cdomain = crse_geom.Domain();
153  Array4<Real const> const& crsearr = crse.const_array();
154  Array4<Real> const& finearr = fine.array();
155 
156  Box cslope_bx = CoarseBox(fine_region, ratio);
157  for (int dim = 0; dim < AMREX_SPACEDIM; ++dim) {
158  if (ratio[dim] > 1) { cslope_bx.grow(dim, -1); }
159  }
160  const bool run_on_gpu = (runon == RunOn::Gpu && Gpu::inLaunchRegion());
161  FArrayBox ccfab(cslope_bx, AMREX_SPACEDIM*nfield, run_on_gpu ? The_Async_Arena() : The_Cpu_Arena());
162  Array4<Real> const& tmp = ccfab.array();
163  Array4<Real const> const& ctmp = ccfab.const_array();
164 
165  // Every field shares the boundary condition of the first
166  const BCRec bc = bcr[0];
167  const int ind_comp = crse_comp + nfield;
168 
169  AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(runon, cslope_bx, nfield, i, j, k, n,
170  {
171  const int nm = ind_comp + (nind == 1 ? 0 : n);
172  typed_mcslope(i, j, k, tmp, n, nfield, crsearr, crse_comp+n, nm, cdomain, ratio, bc);
173  });
174 
175  AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(runon, fine_region, ncomp, i, j, k, n,
176  {
177  if (n < nfield) {
178  mf_cell_cons_lin_interp(i, j, k, n, finearr, fine_comp, ctmp, crsearr, crse_comp,
179  nfield, ratio);
180  } else {
181  finearr(i,j,k,fine_comp+n) = crsearr(amrex::coarsen(i,ratio[0]),
182  amrex::coarsen(j,ratio[1]),
183  amrex::coarsen(k,ratio[2]), crse_comp+n);
184  }
185  });
186 }
187 
188 /**
189  * TypedConsLinear on data already packed for it. Components 0 to nfield-1 of csrc are the
190  * coarse fields and the nind (1 or nfield) after them their type indicators, one value per
191  * type; ghost cells included, since the interpolation reads them. The indicators are set to NaN
192  * (no type) outside the domain here. fdst has the same layout: its fields are interpolated, and
193  * keep their values in the cells the interpolation does not fill (ghost cells outside the
194  * domain); its indicator components are scratch. Every field takes the boundary condition bc.
195  *
196  * Packing many fields moves them in one ParallelCopy rather than one apiece; a caller that packs
197  * them anyway saves interp_by_type's copies by building csrc and fdst directly.
198  */
199 inline void
200 interp_by_type_packed (amrex::MultiFab& fdst, amrex::MultiFab& csrc, int nind,
201  const amrex::Geometry& cgeom, const amrex::Geometry& fgeom,
202  const amrex::IntVect& ratio, const amrex::BCRec& bc)
203 {
204  using namespace amrex;
205  const int ntot = csrc.nComp();
206  const int nfield = ntot - nind;
207  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(fdst.nComp() == ntot,
208  "interp_by_type_packed: csrc and fdst have different numbers of components");
209  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nfield >= 1 && (nind == 1 || nind == nfield),
210  "interp_by_type_packed: the indicators need 1 component or one per field");
211  TypedConsLinear typed_interp(nind);
212 
213  csrc.setDomainBndry(std::numeric_limits<Real>::quiet_NaN(), nfield, nind, cgeom);
214 
215  // InterpFromCoarseLevel reads a BCRec for every component it interpolates
216  const Vector<BCRec> bcs(ntot, bc);
217  InterpFromCoarseLevel(fdst, fdst.nGrowVect(), IntVect(0), csrc, 0, 0, ntot,
218  cgeom, fgeom, ratio, &typed_interp, bcs, 0);
219 }
220 
221 /**
222  * Interpolate the coarse fields src into the fine fields dst (same number of components) with
223  * TypedConsLinear. The type of each coarse cell is given by indicator, one value
224  * per type, on any BoxArray (it is copied onto src's cells, ghost cells included): with one
225  * component it applies to every field, with src.nComp() components component n applies to field
226  * n. Coarse cells it does not reach, and cells outside the domain, have no type and are never
227  * anyone's neighbor. Fine cells the interpolation does not fill (ghost cells outside the domain)
228  * keep their values in dst. Every field takes the boundary condition bcs[bcscomp].
229  */
230 inline void
231 interp_by_type (amrex::MultiFab& dst, const amrex::MultiFab& src,
232  const amrex::MultiFab& indicator,
233  const amrex::Geometry& cgeom, const amrex::Geometry& fgeom,
234  const amrex::IntVect& ratio,
235  const amrex::Vector<amrex::BCRec>& bcs, int bcscomp)
236 {
237  using namespace amrex;
238  const int nfield = src.nComp();
239  const int nind = indicator.nComp();
240  const int ntot = nfield + nind;
241  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(dst.nComp() == nfield,
242  "interp_by_type: src and dst have different numbers of components");
243 
244  // The fields and then their indicators, interpolated together
245  MultiFab csrc(src.boxArray(), src.DistributionMap(), ntot, src.nGrowVect());
246  MultiFab::Copy(csrc, src, 0, 0, nfield, src.nGrowVect());
247  csrc.setVal(std::numeric_limits<Real>::quiet_NaN(), nfield, nind, src.nGrowVect());
248  csrc.ParallelCopy(indicator, 0, nfield, nind, indicator.nGrowVect(), src.nGrowVect(),
249  cgeom.periodicity());
250 
251  MultiFab fdst(dst.boxArray(), dst.DistributionMap(), ntot, dst.nGrowVect());
252  MultiFab::Copy(fdst, dst, 0, 0, nfield, dst.nGrowVect());
253  fdst.setVal(Real(0.0), nfield, nind, dst.nGrowVect());
254 
255  interp_by_type_packed(fdst, csrc, nind, cgeom, fgeom, ratio, bcs[bcscomp]);
256  MultiFab::Copy(dst, fdst, 0, 0, nfield, dst.nGrowVect());
257 }
258 
259 #endif
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void typed_mcslope(int i, int j, int k, amrex::Array4< amrex::Real > const &slope, int ns, int nslope, amrex::Array4< amrex::Real const > const &u, int nu, int nm, amrex::Box const &domain, amrex::IntVect const &ratio, amrex::BCRec const &bc) noexcept
Definition: ERF_TypedInterp.H:66
void interp_by_type(amrex::MultiFab &dst, const amrex::MultiFab &src, const amrex::MultiFab &indicator, const amrex::Geometry &cgeom, const amrex::Geometry &fgeom, const amrex::IntVect &ratio, const amrex::Vector< amrex::BCRec > &bcs, int bcscomp)
Definition: ERF_TypedInterp.H:231
void interp_by_type_packed(amrex::MultiFab &fdst, amrex::MultiFab &csrc, int nind, const amrex::Geometry &cgeom, const amrex::Geometry &fgeom, const amrex::IntVect &ratio, const amrex::BCRec &bc)
Definition: ERF_TypedInterp.H:200
Definition: ERF_TypedInterp.H:31
void interp(const amrex::FArrayBox &crse, int crse_comp, amrex::FArrayBox &fine, int fine_comp, int ncomp, const amrex::Box &fine_region, const amrex::IntVect &ratio, const amrex::Geometry &crse_geom, const amrex::Geometry &, amrex::Vector< amrex::BCRec > const &bcr, int, int, amrex::RunOn runon) override
Definition: ERF_TypedInterp.H:137
int m_nind
Definition: ERF_TypedInterp.H:57
amrex::Box CoarseBox(const amrex::Box &fine, const amrex::IntVect &ratio) override
Definition: ERF_TypedInterp.H:35
amrex::Box CoarseBox(const amrex::Box &fine, int ratio) override
Definition: ERF_TypedInterp.H:44
TypedConsLinear(int nind=1)
Definition: ERF_TypedInterp.H:33
@ ns
Definition: ERF_Morrison.H:50
@ alpha
Definition: ERF_SLM.H:153
@ tmp
Definition: ERF_AdvanceWSM6.cpp:116
Definition: ERF_ConsoleIO.cpp:17
real(c_double), private bc
Definition: ERF_module_mp_morr_two_moment.F90:182