ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERFPCParticleToMesh.H
Go to the documentation of this file.
1 /*! \file ERFPCParticleToMesh.H
2  * \brief Template implementation of ERFPC::ERFPCParticleToMesh
3  *
4  * Separated from ERFPC.H to keep the header lightweight.
5  * Include this in .cpp files that call ERFPCParticleToMesh().
6  */
7 
8 #ifndef ERFPC_PARTICLE_TO_MESH_H_
9 #define ERFPC_PARTICLE_TO_MESH_H_
10 
11 #include <ERFPC.H>
12 #include <ERF_TerrainConversion.H>
13 #include <AMReX_MultiFabUtil.H>
14 
15 template<typename ValueFunc>
16 void ERFPC::ERFPCParticleToMesh (amrex::MultiFab& a_mf,
17  const amrex::MultiFab& a_z_phys_nd,
18  int a_lev, int a_comp,
19  ValueFunc&& value_func,
20  bool composite) const
21 {
22  using namespace amrex;
23 
24  AMREX_ASSERT(OK());
25  AMREX_ASSERT(numParticlesOutOfRange(*this, 0) == 0);
26 
27  // CIC deposition writes one cell outside the particle's own cell
28  AMREX_ALWAYS_ASSERT(a_mf.nGrowVect().allGE(amrex::IntVect(1)));
29 
30  const auto& geom = Geom(a_lev);
31  const auto plo = geom.ProbLoArray();
32  const auto dxi = geom.InvCellSizeArray();
33  const Real dx = geom.CellSize(0);
34  const Real dy = geom.CellSize(1);
35  const Box domain = geom.Domain();
36  const int k_max = domain.bigEnd(AMREX_SPACEDIM-1);
37  const int i_lo = domain.smallEnd(0), i_hi = domain.bigEnd(0);
38  const int j_lo = domain.smallEnd(1), j_hi = domain.bigEnd(1);
39  const int k_lo = domain.smallEnd(AMREX_SPACEDIM-1);
40  const bool per_x = geom.isPeriodic(0);
41  const bool per_y = geom.isPeriodic(1);
42  const bool per_z = geom.isPeriodic(AMREX_SPACEDIM-1);
43 
44  iMultiFab owned;
45  if (composite) {
46  owned.define(a_mf.boxArray(), a_mf.DistributionMap(), 1, IntVect(1));
47  owned.setVal(0);
48  if (a_lev < finestLevel()) {
49  iMultiFab exposed = makeFineMask(a_mf, ParticleBoxArray(a_lev+1),
50  GetParGDB()->refRatio(a_lev), 1, 0);
51  iMultiFab::Copy(owned, exposed, 0, 0, 1, 0);
52  } else {
53  owned.setVal(1, 0, 1, 0);
54  }
55  owned.FillBoundary(geom.periodicity());
56  }
57 
58  a_mf.setVal(0.0);
59 
60 #ifdef AMREX_USE_OMP
61 #pragma omp parallel if (Gpu::notInLaunchRegion())
62 #endif
63  for (ParConstIterType pti(*this, a_lev); pti.isValid(); ++pti)
64  {
65  const int grid = pti.index();
66  const auto& ptile = ParticlesAt(a_lev, pti);
67  const auto& aos = ptile.GetArrayOfStructs();
68  const int np = aos.numParticles();
69  if (np == 0) { continue; }
70 
71  auto rho = a_mf[grid].array();
72  auto zheight = a_z_phys_nd[grid].array();
73  const Array4<const int> valid = composite ? owned.const_array(grid) : Array4<const int>{};
74 
75  const auto ptd = ptile.getConstParticleTileData();
76 
77  ParallelFor(np, [=] AMREX_GPU_DEVICE (int i)
78  {
79  const auto& p = ptd.m_aos[i];
80  if (p.id() <= 0) { return; }
81 
82  const Real pval = static_cast<Real>(value_func(ptd, i));
83 
84  const Real lx = static_cast<Real>((p.pos(0) - plo[0]) * dxi[0]) + Real(0.5);
85  const int ix = static_cast<int>(Math::floor(lx)) - 1;
86  const Real wx1 = lx - static_cast<Real>(ix + 1);
87  const Real wx0 = Real(1.0) - wx1;
88 
89  const Real ly = static_cast<Real>((p.pos(1) - plo[1]) * dxi[1]) + Real(0.5);
90  const int iy = static_cast<int>(Math::floor(ly)) - 1;
91  const Real wy1 = ly - static_cast<Real>(iy + 1);
92  const Real wy0 = Real(1.0) - wy1;
93 
94  const int kz = static_cast<int>(amrex::Math::floor((p.pos(AMREX_SPACEDIM-1) - plo[AMREX_SPACEDIM-1])
95  * dxi[AMREX_SPACEDIM-1]));
96  const int ix_p = static_cast<int>(Math::floor((p.pos(0) - plo[0]) * dxi[0]));
97  const int iy_p = static_cast<int>(Math::floor((p.pos(1) - plo[1]) * dxi[1]));
98  const Real fx = static_cast<Real>((p.pos(0) - plo[0]) * dxi[0]) - static_cast<Real>(ix_p);
99  const Real fy = static_cast<Real>((p.pos(1) - plo[1]) * dxi[1]) - static_cast<Real>(iy_p);
100 
101  auto zface = [=] AMREX_GPU_DEVICE (int k_face) -> Real {
102  return zheight(ix_p , iy_p , k_face) * (Real(1.0)-fx) * (Real(1.0)-fy)
103  + zheight(ix_p+1, iy_p , k_face) * fx * (Real(1.0)-fy)
104  + zheight(ix_p , iy_p+1, k_face) * (Real(1.0)-fx) * fy
105  + zheight(ix_p+1, iy_p+1, k_face) * fx * fy;
106  };
107 
108  // Effective k bounds for safe zface access: must respect both the
109  // geometry domain (k_max) and the local fab's z extent. With AMR
110  // partial-z fabs (especially with many small boxes from large rank
111  // counts), a particle at the top/bottom of its fab can have kz
112  // such that kz+2 / kz-1 read outside the fab. Use the fallback
113  // (one-sided) span formula in those cases.
114  const int kz_hi_safe = amrex::min(k_max, zheight.end[2] - 2);
115  const int kz_lo_safe = amrex::max(0, zheight.begin[2] + 1);
116 
117  const Real z_lo_k = zface(kz);
118  const Real z_hi_k = zface(kz + 1);
119  const Real z_ctr_k = Real(0.5) * (z_lo_k + z_hi_k);
120 
121  // Particle pos(2) is computational zeta; convert to physical z so
122  // the cell-center comparison and weight span are in the same units
123  // as zface() (which is built from the height array).
124  const Real p_z = static_cast<Real>(ERF::ParticlePos::z_from_zeta(
125  p.pos(0), p.pos(1), p.pos(AMREX_SPACEDIM-1),
126  plo, dxi, zheight));
127 
128  int kz_lo, kz_hi;
129  Real wz_lo, wz_hi;
130  if (p_z >= z_ctr_k) {
131  kz_lo = kz;
132  kz_hi = kz + 1;
133  Real span;
134  if (kz < kz_hi_safe) {
135  const Real z_hi2 = zface(kz + 2);
136  const Real z_ctr_hi = Real(0.5) * (z_hi_k + z_hi2);
137  span = z_ctr_hi - z_ctr_k;
138  } else {
139  span = z_hi_k - z_lo_k;
140  }
141  wz_hi = (p_z - z_ctr_k) / span;
142  wz_lo = Real(1.0) - wz_hi;
143  } else {
144  kz_hi = kz;
145  kz_lo = kz - 1;
146  Real span;
147  if (kz > kz_lo_safe) {
148  const Real z_lo2 = zface(kz - 1);
149  const Real z_ctr_lo = Real(0.5) * (z_lo2 + z_lo_k);
150  span = z_ctr_k - z_ctr_lo;
151  } else {
152  span = z_hi_k - z_lo_k;
153  }
154  wz_lo = (z_ctr_k - p_z) / span;
155  wz_hi = Real(1.0) - wz_lo;
156  }
157 
158  // Per-corner node-averaged cell thickness for volume normalization.
159  auto dz_cell = [=] AMREX_GPU_DEVICE (int ic, int jc, int kc) -> Real {
160  return ERFParticleGeometry::cell_thickness(zheight, ic, jc, kc);
161  };
162 
163  const Real inv_dxdy = Real(1.0) / (dx * dy);
164 
165  // Restrict appended-species CIC to the owning level's composite
166  // cells. Renormalization preserves each particle's mass at walls
167  // and coarse/fine edges without creating another grid reservoir.
168  Real weight = Real(1);
169  if (composite) {
170  weight = Real(0);
171  for (int iz = 0; iz < 2; ++iz) {
172  const int kc = iz ? kz_hi : kz_lo;
173  const Real wz = iz ? wz_hi : wz_lo;
174  for (int jy = 0; jy < 2; ++jy) {
175  for (int jx = 0; jx < 2; ++jx) {
176  if (valid(ix+jx, iy+jy, kc)) {
177  weight += (jx ? wx1 : wx0) * (jy ? wy1 : wy0) * wz;
178  }
179  }
180  }
181  }
182  AMREX_ALWAYS_ASSERT(weight > Real(0));
183  }
184 
185  auto deposit = [=] AMREX_GPU_DEVICE (int ic, int jc, int kc, Real w_horiz, Real w_z)
186  {
187  // A corner with zero CIC weight deposits nothing. Skip it before
188  // touching dz_cell: when a particle sits exactly on a cell-center
189  // plane the outermost corner lands in the ghost region, where the
190  // node-averaged thickness can come out exactly zero (z_phys_nd
191  // ghost nodes outside the domain are not filled), and 0/0 raises
192  // an invalid-operation FPE.
193  if (w_horiz == Real(0.0) || w_z == Real(0.0)) { return; }
194  if (composite && !valid(ic,jc,kc)) { return; }
195 
196  // A corner outside a non-periodic boundary folds into the boundary cell; SumBoundary wraps periodic ones.
197  const int ic_dz = per_x ? ic : amrex::max(i_lo, amrex::min(i_hi, ic));
198  const int jc_dz = per_y ? jc : amrex::max(j_lo, amrex::min(j_hi, jc));
199  const int kc_dz = amrex::max(k_lo, amrex::min(k_max, kc));
200  const Real dz = dz_cell(ic_dz, jc_dz, kc_dz);
201  AMREX_ALWAYS_ASSERT(dz > Real(0.0));
202  const Real contrib = pval * w_horiz * w_z * inv_dxdy / (dz * weight);
203  Gpu::Atomic::AddNoRet(&rho(ic_dz, jc_dz, per_z ? kc : kc_dz, a_comp), contrib);
204  };
205 
206  deposit(ix , iy , kz_lo, wx0*wy0, wz_lo);
207  deposit(ix+1, iy , kz_lo, wx1*wy0, wz_lo);
208  deposit(ix , iy+1, kz_lo, wx0*wy1, wz_lo);
209  deposit(ix+1, iy+1, kz_lo, wx1*wy1, wz_lo);
210  deposit(ix , iy , kz_hi, wx0*wy0, wz_hi);
211  deposit(ix+1, iy , kz_hi, wx1*wy0, wz_hi);
212  deposit(ix , iy+1, kz_hi, wx0*wy1, wz_hi);
213  deposit(ix+1, iy+1, kz_hi, wx1*wy1, wz_hi);
214  });
215  }
216 
217  a_mf.SumBoundary(geom.periodicity());
218 }
219 
220 #endif
GpuArray< Real, AMREX_SPACEDIM > plo
Definition: ERF_ActuatorSpreading.cpp:131
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
@ zface
Definition: ERF_EBStruct.H:29
const Real lx
Definition: ERF_InitCustomPertVels_CloudChamber.H:10
const Real ly
Definition: ERF_InitCustomPertVels_CloudChamber.H:11
const Real dy
Definition: ERF_InitCustomPert_ABL.H:101
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_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real z_from_zeta(amrex::Real x, amrex::Real y, amrex::Real zeta, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &plo, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &dxi, amrex::Array4< amrex::Real const > const &height_arr) noexcept
Convert computational vertical coordinate zeta to physical z at column (x, y).
Definition: ERF_TerrainConversion.H:77
@ rho
Definition: ERF_Kessler.H:25
@ dz
Definition: ERF_AdvanceWDM6.cpp:273
@ p
Definition: ERF_WSM6.H:303
Definition: ERF_ConsoleIO.cpp:17