ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TypedInterp.H File Reference
#include <AMReX_FillPatchUtil.H>
#include <AMReX_Interpolater.H>
#include <AMReX_MFInterp_C.H>
#include <AMReX_MultiFab.H>
#include <limits>
Include dependency graph for ERF_TypedInterp.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  TypedConsLinear
 

Functions

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

Function Documentation

◆ interp_by_type()

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

Interpolate the coarse fields src into the fine fields dst (same number of components) with TypedConsLinear. The type of each coarse cell is given by indicator, one value per type, on any BoxArray (it is copied onto src's cells, ghost cells included): with one component it applies to every field, with src.nComp() components component n applies to field n. Coarse cells it does not reach, and cells outside the domain, have no type and are never anyone's neighbor. Fine cells the interpolation does not fill (ghost cells outside the domain) keep their values in dst. Every field takes the boundary condition bcs[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 }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
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_ConsoleIO.cpp:17
Here is the call graph for this function:

◆ interp_by_type_packed()

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

TypedConsLinear on data already packed for it. Components 0 to nfield-1 of csrc are the coarse fields and the nind (1 or nfield) after them their type indicators, one value per type; ghost cells included, since the interpolation reads them. The indicators are set to NaN (no type) outside the domain here. fdst has the same layout: its fields are interpolated, and keep their values in the cells the interpolation does not fill (ghost cells outside the domain); its indicator components are scratch. Every field takes the boundary condition bc.

Packing many fields moves them in one ParallelCopy rather than one apiece; a caller that packs them anyway saves interp_by_type's copies by building csrc and fdst directly.

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 }
Definition: ERF_TypedInterp.H:31
real(c_double), private bc
Definition: ERF_module_mp_morr_two_moment.F90:182

Referenced by interp_by_type(), NOAHMP::interp_from_lev0(), SurfaceModel::interp_surface_fields_from_coarse(), and SLM::Lsm_Interpolate_From_Source().

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

◆ typed_mcslope()

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

Slopes of the field (component nu) of coarse cell (i,j,k), from neighbors of its own type only (indicator in component nm). Follows mf_cell_cons_lin_interp_mcslope term for term; the slope in direction d goes to component ns + d*nslope, the layout mf_cell_cons_lin_interp reads.

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 }
@ ns
Definition: ERF_Morrison.H:50
@ alpha
Definition: ERF_SLM.H:153

Referenced by TypedConsLinear::interp().

Here is the caller graph for this function: