ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
erf_detail_to_REMORA Namespace Reference

Functions

void PrintFluxLaneStats (const char *label, const amrex::MultiFab &mf, int comp=0)
 
void FillEastNorth (amrex::MultiFab &en, const amrex::MultiFab &fx, const amrex::MultiFab &fy, int k, const amrex::MultiFab *cos_alpha, const amrex::MultiFab *sin_alpha)
 
amrex::BoxArray StagedSourceBoxArray (const amrex::MultiFab &src, const amrex::MultiFab &dst, const amrex::iMultiFab &index_mf, int max_stencil_size)
 
void ApplyConservativeRemap (const amrex::MultiFab &src, amrex::MultiFab &dst, const amrex::MultiFab &weight_mf, const amrex::iMultiFab &index_mf, int max_stencil_size, const amrex::MultiFab *dst_mask=nullptr, amrex::Real fallback_val=amrex::Real(0.0))
 

Variables

constexpr int iUwind = 0
 
constexpr int iVwind = 1
 
constexpr int iPatm = 2
 
constexpr int iRH = 3
 
constexpr int iTair = 4
 
constexpr int iCloud = 5
 
constexpr int iRain = 6
 
constexpr int iSWrad = 7
 
constexpr int iLWrad = 8
 
constexpr int nStateLanes = 9
 
constexpr int iTauX = 0
 
constexpr int iTauY = 1
 
constexpr int iSHflux = 2
 
constexpr int iLHflux = 3
 
constexpr int iFluxSWrad = 4
 
constexpr int iFluxLWrad = 5
 
constexpr int iFluxRain = 6
 
constexpr int iFluxEvap = 7
 
constexpr int nFluxLanes = 8
 

Function Documentation

◆ ApplyConservativeRemap()

void erf_detail_to_REMORA::ApplyConservativeRemap ( const amrex::MultiFab &  src,
amrex::MultiFab &  dst,
const amrex::MultiFab &  weight_mf,
const amrex::iMultiFab &  index_mf,
int  max_stencil_size,
const amrex::MultiFab *  dst_mask = nullptr,
amrex::Real  fallback_val = amrex::Real(0.0) 
)
200 {
201  using namespace amrex;
202 
203  // 1. Data Routing: Get the ERF source data onto the REMORA destination layout,
204  // staging exactly the source region the stencils reference so every indexed
205  // read below lands in valid data.
206  MultiFab src_on_dst(StagedSourceBoxArray(src, dst, index_mf, max_stencil_size),
207  dst.DistributionMap(), src.nComp(), 0);
208  src_on_dst.setVal(0.0);
209  src_on_dst.ParallelCopy(src);
210 
211  // Ensure the destination is zeroed out before accumulating the weighted sum
212  dst.setVal(0.0);
213 
214  // 2. Stencil Application: Execute the local sparse dot product
215  for (MFIter mfi(dst, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
216  Box bx = mfi.tilebox();
217 
218  auto const& w_arr = weight_mf.const_array(mfi);
219  auto const& idx_arr = index_mf.const_array(mfi);
220  auto const& src_arr = src_on_dst.const_array(mfi);
221  auto dst_arr = dst.array(mfi);
222  const bool has_mask = (dst_mask != nullptr);
223  auto const& mask_arr = has_mask ? dst_mask->const_array(mfi) : Array4<const Real>{};
224 
225  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
226  // No stencil entry at all means no source cell overlapped this
227  // destination cell. Hand back the fallback rather than an accumulated
228  // zero, and do not apply the mask to it: a masked-out cell still gets
229  // evaluated by the receiving model's flux formulas before the mask is
230  // applied, so it too must hold an admissible value.
231  if (idx_arr(i, j, k, 0) < 0) {
232  dst_arr(i, j, k) = fallback_val;
233  return;
234  }
235 
236  Real sum = 0.0;
237  for (int m = 0; m < max_stencil_size; ++m) {
238  Real w = w_arr(i, j, k, m);
239  if (w > 0.0) {
240  int src_i = idx_arr(i, j, k, m * 3);
241  int src_j = idx_arr(i, j, k, m * 3 + 1);
242  int src_k = idx_arr(i, j, k, m * 3 + 2);
243 
244  sum += w * src_arr(src_i, src_j, src_k);
245  }
246  }
247  // Blend toward the fallback rather than multiplying by the mask.
248  //
249  // Multiplying drives masked cells to exactly zero, which is right for
250  // a flux lane (fallback_val = 0, so this reduces to sum *= mask and is
251  // bit-identical) but destructive for an intensive state lane: a land
252  // cell would hold 0 Pa and 0 K, and the receiving model evaluates its
253  // bulk formulas over land before applying its own mask, so those
254  // zeros become NaN and the NaN outlives the masking. Land cells still
255  // receive no coupled information either way; they just stay
256  // physically admissible.
257  if (has_mask) {
258  const Real mask = mask_arr(i, j, k);
259  sum = mask * sum + (Real(1.0) - mask) * fallback_val;
260  }
261  dst_arr(i, j, k) = sum;
262  });
263  }
264 }
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);})
Definition: ERF_ConsoleIO.cpp:17
amrex::BoxArray StagedSourceBoxArray(const amrex::MultiFab &src, const amrex::MultiFab &dst, const amrex::iMultiFab &index_mf, int max_stencil_size)
Definition: ERF_to_REMORA.cpp:104

Referenced by ERF::PackAtmosphericStates().

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

◆ FillEastNorth()

void erf_detail_to_REMORA::FillEastNorth ( amrex::MultiFab &  en,
const amrex::MultiFab &  fx,
const amrex::MultiFab &  fy,
int  k,
const amrex::MultiFab *  cos_alpha,
const amrex::MultiFab *  sin_alpha 
)
48 {
49  using namespace amrex;
50  const bool rotate = cos_alpha != nullptr && sin_alpha != nullptr;
51  if (rotate) {
53  cos_alpha->boxArray() == en.boxArray() &&
54  cos_alpha->DistributionMap() == en.DistributionMap(),
55  "The coupler's grid orientation is not on the level-0 2D layout");
56  }
57  for (MFIter mfi(en, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
58  // The 2D boxes index the 3D ones: each 3D box is the 2D one over the whole height,
59  // so it holds plane k. ERF doesn't split in z by default (amr.no_box_split_dir = 2);
60  // this says so when it is told to.
61  const Box& b3 = fx.box(mfi.index());
62  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(b3.smallEnd(2) <= k && b3.bigEnd(2) >= k,
63  "FillEastNorth needs each level-0 box to span the surface plane.");
64  const Box bx = mfi.tilebox();
65  auto const& ux = fx.const_array(mfi);
66  auto const& vy = fy.const_array(mfi);
67  auto const& e = en.array(mfi);
68  if (rotate) {
69  auto const& ca = cos_alpha->const_array(mfi);
70  auto const& sa = sin_alpha->const_array(mfi);
71  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int n) {
72  const Real u = Real(0.5) * (ux(i,j,k) + ux(i+1,j,k));
73  const Real v = Real(0.5) * (vy(i,j,k) + vy(i,j+1,k));
74  erf_earth_wind::grid_to_earth(u, v, ca(i,j,n), sa(i,j,n), e(i,j,n,0), e(i,j,n,1));
75  });
76  } else {
77  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int n) {
78  e(i,j,n,0) = Real(0.5) * (ux(i,j,k) + ux(i+1,j,k));
79  e(i,j,n,1) = Real(0.5) * (vy(i,j,k) + vy(i,j+1,k));
80  });
81  }
82  }
83 }
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 grid_to_earth(amrex::Real u, amrex::Real v, amrex::Real cos_alpha, amrex::Real sin_alpha, amrex::Real &u_east, amrex::Real &v_north) noexcept
Definition: ERF_EarthRelativeWind.H:29

Referenced by ERF::PackAtmosphericStates().

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

◆ PrintFluxLaneStats()

void erf_detail_to_REMORA::PrintFluxLaneStats ( const char *  label,
const amrex::MultiFab &  mf,
int  comp = 0 
)
34 {
35  amrex::Print() << "ERF flux-pack " << label
36  << ": min=" << mf.min(comp)
37  << " max=" << mf.max(comp) << "\n";
38 }

Referenced by ERF::PackAtmosphericStates().

Here is the caller graph for this function:

◆ StagedSourceBoxArray()

amrex::BoxArray erf_detail_to_REMORA::StagedSourceBoxArray ( const amrex::MultiFab &  src,
const amrex::MultiFab &  dst,
const amrex::iMultiFab &  index_mf,
int  max_stencil_size 
)
108 {
109  using namespace amrex;
110 
111  const Box src_domain = src.boxArray().minimalBox();
112  const int nboxes = static_cast<int>(dst.boxArray().size());
113  constexpr int int_big = std::numeric_limits<int>::max();
114 
115  Vector<int> lo(2 * nboxes, int_big);
116  Vector<int> hi(2 * nboxes, -int_big);
117 
118  for (MFIter mfi(dst); mfi.isValid(); ++mfi) {
119  const int b = mfi.index();
120  const Box bx = mfi.validbox();
121  auto const& idx = index_mf.const_array(mfi);
122 
123  ReduceOps<ReduceOpMin, ReduceOpMin, ReduceOpMax, ReduceOpMax> reduce_op;
124  ReduceData<int, int, int, int> reduce_data(reduce_op);
125  using ReduceTuple = typename decltype(reduce_data)::Type;
126 
127  reduce_op.eval(bx, reduce_data,
128  [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
129  {
130  int i_min = int_big, j_min = int_big, i_max = -int_big, j_max = -int_big;
131  for (int m = 0; m < max_stencil_size; ++m) {
132  const int si = idx(i, j, k, m * 3);
133  const int sj = idx(i, j, k, m * 3 + 1);
134  if (si < 0 || sj < 0) { continue; } // -1 marks an unused slot
135  i_min = amrex::min(i_min, si); i_max = amrex::max(i_max, si);
136  j_min = amrex::min(j_min, sj); j_max = amrex::max(j_max, sj);
137  }
138  return {i_min, j_min, i_max, j_max};
139  });
140 
141  auto const& hv = reduce_data.value(reduce_op);
142  lo[2*b ] = amrex::get<0>(hv); lo[2*b+1] = amrex::get<1>(hv);
143  hi[2*b ] = amrex::get<2>(hv); hi[2*b+1] = amrex::get<3>(hv);
144  }
145 
146  ParallelDescriptor::ReduceIntMin(lo.dataPtr(), static_cast<int>(lo.size()));
147  ParallelDescriptor::ReduceIntMax(hi.dataPtr(), static_cast<int>(hi.size()));
148 
149  // The staged boxes carry the source's index type, so that intersecting them with
150  // the source's boxes cannot trip Box::operator&='s sameType assertion. Every
151  // source is cell-centered today.
152  const IndexType src_ixtype = src.boxArray().ixType();
153 
154  BoxList bl(src_ixtype);
155  for (int b = 0; b < nboxes; ++b) {
156  Box need;
157  if (lo[2*b] > hi[2*b] || lo[2*b+1] > hi[2*b+1]) {
158  // No stencil anywhere in this destination box. A BoxArray cannot hold
159  // an empty box, so stage a single cell; nothing reads it.
160  need = Box(src_domain.smallEnd(), src_domain.smallEnd(), src_ixtype);
161  } else {
162  need = Box(IntVect(lo[2*b], lo[2*b+1], src_domain.smallEnd(2)),
163  IntVect(hi[2*b], hi[2*b+1], src_domain.bigEnd(2)),
164  src_ixtype);
165  // The intersection below silently drops any index outside the source
166  // domain, but the stencil kernel dereferences it regardless - so a
167  // weight generator that emitted an out-of-domain index would turn into
168  // an out-of-bounds read instead of a diagnosable failure. Assert first.
170  src_domain.contains(need),
171  "StagedSourceBoxArray: a remap stencil references a source index outside the source domain.");
172  need &= src_domain;
173  }
174  bl.push_back(need);
175  }
176  return BoxArray(std::move(bl));
177 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:404

Referenced by ApplyConservativeRemap().

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

Variable Documentation

◆ iCloud

constexpr int erf_detail_to_REMORA::iCloud = 5
constexpr

◆ iFluxEvap

constexpr int erf_detail_to_REMORA::iFluxEvap = 7

◆ iFluxLWrad

constexpr int erf_detail_to_REMORA::iFluxLWrad = 5

◆ iFluxRain

constexpr int erf_detail_to_REMORA::iFluxRain = 6

◆ iFluxSWrad

constexpr int erf_detail_to_REMORA::iFluxSWrad = 4
constexpr

◆ iLHflux

constexpr int erf_detail_to_REMORA::iLHflux = 3

◆ iLWrad

constexpr int erf_detail_to_REMORA::iLWrad = 8

◆ iPatm

constexpr int erf_detail_to_REMORA::iPatm = 2

◆ iRain

constexpr int erf_detail_to_REMORA::iRain = 6

◆ iRH

constexpr int erf_detail_to_REMORA::iRH = 3

◆ iSHflux

constexpr int erf_detail_to_REMORA::iSHflux = 2

◆ iSWrad

constexpr int erf_detail_to_REMORA::iSWrad = 7

◆ iTair

constexpr int erf_detail_to_REMORA::iTair = 4

◆ iTauX

constexpr int erf_detail_to_REMORA::iTauX = 0
constexpr

◆ iTauY

constexpr int erf_detail_to_REMORA::iTauY = 1

◆ iUwind

constexpr int erf_detail_to_REMORA::iUwind = 0
constexpr

◆ iVwind

constexpr int erf_detail_to_REMORA::iVwind = 1

◆ nFluxLanes

constexpr int erf_detail_to_REMORA::nFluxLanes = 8
constexpr

◆ nStateLanes

constexpr int erf_detail_to_REMORA::nStateLanes = 9
constexpr