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 
14 template<typename ValueFunc>
15 void ERFPC::ERFPCParticleToMesh(amrex::MultiFab& a_mf,
16  const amrex::MultiFab& a_z_phys_nd,
17  int a_lev, int a_comp,
18  ValueFunc&& value_func) const
19 {
20  using namespace amrex;
21 
22  AMREX_ASSERT(OK());
23  AMREX_ASSERT(numParticlesOutOfRange(*this, 0) == 0);
24 
25  // CIC deposition writes one cell outside the particle's own cell
26  AMREX_ALWAYS_ASSERT(a_mf.nGrowVect().allGE(amrex::IntVect(1)));
27 
28  const auto& geom = Geom(a_lev);
29  const auto plo = geom.ProbLoArray();
30  const auto dxi = geom.InvCellSizeArray();
31  const Real dx = geom.CellSize(0);
32  const Real dy = geom.CellSize(1);
33  const Box domain = geom.Domain();
34  const int k_max = domain.bigEnd(AMREX_SPACEDIM-1);
35  const int i_lo = domain.smallEnd(0), i_hi = domain.bigEnd(0);
36  const int j_lo = domain.smallEnd(1), j_hi = domain.bigEnd(1);
37  const int k_lo = domain.smallEnd(AMREX_SPACEDIM-1);
38  const bool per_x = geom.isPeriodic(0);
39  const bool per_y = geom.isPeriodic(1);
40 
41  a_mf.setVal(0.0);
42 
43 #ifdef AMREX_USE_OMP
44 #pragma omp parallel if (Gpu::notInLaunchRegion())
45 #endif
46  for (ParConstIterType pti(*this, a_lev); pti.isValid(); ++pti)
47  {
48  const int grid = pti.index();
49  const auto& ptile = ParticlesAt(a_lev, pti);
50  const auto& aos = ptile.GetArrayOfStructs();
51  const int np = aos.numParticles();
52  if (np == 0) { continue; }
53 
54  auto rho = a_mf[grid].array();
55  auto zheight = a_z_phys_nd[grid].array();
56 
57  const auto ptd = ptile.getConstParticleTileData();
58 
59  ParallelFor(np, [=] AMREX_GPU_DEVICE (int i)
60  {
61  const auto& p = ptd.m_aos[i];
62  if (p.id() <= 0) { return; }
63 
64  const Real pval = static_cast<Real>(value_func(ptd, i));
65 
66  const Real lx = static_cast<Real>((p.pos(0) - plo[0]) * dxi[0]) + Real(0.5);
67  const int ix = static_cast<int>(Math::floor(lx)) - 1;
68  const Real wx1 = lx - static_cast<Real>(ix + 1);
69  const Real wx0 = Real(1.0) - wx1;
70 
71  const Real ly = static_cast<Real>((p.pos(1) - plo[1]) * dxi[1]) + Real(0.5);
72  const int iy = static_cast<int>(Math::floor(ly)) - 1;
73  const Real wy1 = ly - static_cast<Real>(iy + 1);
74  const Real wy0 = Real(1.0) - wy1;
75 
76  const int kz = static_cast<int>(amrex::Math::floor((p.pos(AMREX_SPACEDIM-1) - plo[AMREX_SPACEDIM-1])
77  * dxi[AMREX_SPACEDIM-1]));
78  const int ix_p = static_cast<int>(Math::floor((p.pos(0) - plo[0]) * dxi[0]));
79  const int iy_p = static_cast<int>(Math::floor((p.pos(1) - plo[1]) * dxi[1]));
80  const Real fx = static_cast<Real>((p.pos(0) - plo[0]) * dxi[0]) - static_cast<Real>(ix_p);
81  const Real fy = static_cast<Real>((p.pos(1) - plo[1]) * dxi[1]) - static_cast<Real>(iy_p);
82 
83  auto zface = [=] AMREX_GPU_DEVICE (int k_face) -> Real {
84  return zheight(ix_p , iy_p , k_face) * (Real(1.0)-fx) * (Real(1.0)-fy)
85  + zheight(ix_p+1, iy_p , k_face) * fx * (Real(1.0)-fy)
86  + zheight(ix_p , iy_p+1, k_face) * (Real(1.0)-fx) * fy
87  + zheight(ix_p+1, iy_p+1, k_face) * fx * fy;
88  };
89 
90  // Effective k bounds for safe zface access: must respect both the
91  // geometry domain (k_max) and the local fab's z extent. With AMR
92  // partial-z fabs (especially with many small boxes from large rank
93  // counts), a particle at the top/bottom of its fab can have kz
94  // such that kz+2 / kz-1 read outside the fab. Use the fallback
95  // (one-sided) span formula in those cases.
96  const int kz_hi_safe = amrex::min(k_max, zheight.end[2] - 2);
97  const int kz_lo_safe = amrex::max(0, zheight.begin[2] + 1);
98 
99  const Real z_lo_k = zface(kz);
100  const Real z_hi_k = zface(kz + 1);
101  const Real z_ctr_k = Real(0.5) * (z_lo_k + z_hi_k);
102 
103  // Particle pos(2) is computational zeta; convert to physical z so
104  // the cell-center comparison and weight span are in the same units
105  // as zface() (which is built from the height array).
106  const Real p_z = static_cast<Real>(ERF::ParticlePos::z_from_zeta(
107  p.pos(0), p.pos(1), p.pos(AMREX_SPACEDIM-1),
108  plo, dxi, zheight));
109 
110  int kz_lo, kz_hi;
111  Real wz_lo, wz_hi;
112  if (p_z >= z_ctr_k) {
113  kz_lo = kz;
114  kz_hi = kz + 1;
115  Real span;
116  if (kz < kz_hi_safe) {
117  const Real z_hi2 = zface(kz + 2);
118  const Real z_ctr_hi = Real(0.5) * (z_hi_k + z_hi2);
119  span = z_ctr_hi - z_ctr_k;
120  } else {
121  span = z_hi_k - z_lo_k;
122  }
123  wz_hi = (p_z - z_ctr_k) / span;
124  wz_lo = Real(1.0) - wz_hi;
125  } else {
126  kz_hi = kz;
127  kz_lo = kz - 1;
128  Real span;
129  if (kz > kz_lo_safe) {
130  const Real z_lo2 = zface(kz - 1);
131  const Real z_ctr_lo = Real(0.5) * (z_lo2 + z_lo_k);
132  span = z_ctr_k - z_ctr_lo;
133  } else {
134  span = z_hi_k - z_lo_k;
135  }
136  wz_lo = (z_ctr_k - p_z) / span;
137  wz_hi = Real(1.0) - wz_lo;
138  }
139 
140  // Per-corner node-averaged cell thickness for volume normalization.
141  auto dz_cell = [=] AMREX_GPU_DEVICE (int ic, int jc, int kc) -> Real {
142  const Real dz = Real(0.25) * (
143  zheight(ic , jc , kc+1) - zheight(ic , jc , kc)
144  + zheight(ic+1, jc , kc+1) - zheight(ic+1, jc , kc)
145  + zheight(ic , jc+1, kc+1) - zheight(ic , jc+1, kc)
146  + zheight(ic+1, jc+1, kc+1) - zheight(ic+1, jc+1, kc) );
147  return dz;
148  };
149 
150  const Real inv_dxdy = Real(1.0) / (dx * dy);
151 
152  auto deposit = [=] AMREX_GPU_DEVICE (int ic, int jc, int kc, Real w_horiz, Real w_z)
153  {
154  // A corner with zero CIC weight deposits nothing. Skip it before
155  // touching dz_cell: when a particle sits exactly on a cell-center
156  // plane the outermost corner lands in the ghost region, where the
157  // node-averaged thickness can come out exactly zero (z_phys_nd
158  // ghost nodes outside the domain are not filled), and 0/0 raises
159  // an invalid-operation FPE.
160  if (w_horiz == Real(0.0) || w_z == Real(0.0)) { return; }
161 
162  // Clamp only for the dz lookup; still write to the ghost rho cell. In a
163  // periodic direction the exterior nodes hold the wrapped image, so the
164  // unclamped stencil already gives the thickness of the cell SumBoundary
165  // folds into; only the non-periodic exterior nodes need clamping.
166  const int ic_dz = per_x ? ic : amrex::max(i_lo, amrex::min(i_hi, ic));
167  const int jc_dz = per_y ? jc : amrex::max(j_lo, amrex::min(j_hi, jc));
168  const int kc_dz = amrex::max(k_lo, amrex::min(k_max, kc));
169  const Real dz = dz_cell(ic_dz, jc_dz, kc_dz);
170  AMREX_ALWAYS_ASSERT(dz > Real(0.0));
171  const Real contrib = pval * w_horiz * w_z * inv_dxdy / dz;
172  Gpu::Atomic::AddNoRet(&rho(ic, jc, kc, a_comp), contrib);
173  };
174 
175  deposit(ix , iy , kz_lo, wx0*wy0, wz_lo);
176  deposit(ix+1, iy , kz_lo, wx1*wy0, wz_lo);
177  deposit(ix , iy+1, kz_lo, wx0*wy1, wz_lo);
178  deposit(ix+1, iy+1, kz_lo, wx1*wy1, wz_lo);
179  deposit(ix , iy , kz_hi, wx0*wy0, wz_hi);
180  deposit(ix+1, iy , kz_hi, wx1*wy0, wz_hi);
181  deposit(ix , iy+1, kz_hi, wx0*wy1, wz_hi);
182  deposit(ix+1, iy+1, kz_hi, wx1*wy1, wz_hi);
183  });
184  }
185 
186  a_mf.SumBoundary(geom.periodicity());
187 }
188 
189 #endif
@ zface
Definition: ERF_EBStruct.H:29
const Real dy
Definition: ERF_InitCustomPert_ABL.H:45
const Real dx
Definition: ERF_InitCustomPert_ABL.H:44
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::Real Real
Definition: ERF_ShocInterface.H:19
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:71
@ rho
Definition: ERF_Kessler.H:24
@ dz
Definition: ERF_AdvanceWDM6.cpp:270
@ p
Definition: ERF_WSM6.H:191
Definition: ERF_ConsoleIO.cpp:15