WarpX
DepositCharge.H
Go to the documentation of this file.
1 /* Copyright 2019-2021 Axel Huebl, Andrew Myers
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef ABLASTR_DEPOSIT_CHARGE_H_
8 #define ABLASTR_DEPOSIT_CHARGE_H_
9 
13 #include "ablastr/utils/TextMsg.H"
14 
15 #include <AMReX.H>
16 
17 #include <optional>
18 
19 
21 {
22 
52 template< typename T_PC >
53 static void
54 deposit_charge (typename T_PC::ParIterType& pti,
55  typename T_PC::RealVector const& wp,
56  amrex::Real const charge,
57  int const * const ion_lev,
58  amrex::MultiFab* rho,
59  amrex::FArrayBox& local_rho,
60  int const particle_shape,
61  std::array<amrex::Real, 3> const & dx,
62  std::array<amrex::Real, 3> const & xyzmin,
63  int const n_rz_azimuthal_modes = 0,
64  std::optional<amrex::IntVect> num_rho_deposition_guards = std::nullopt,
65  std::optional<int> depos_lev = std::nullopt,
66  std::optional<amrex::IntVect> rel_ref_ratio = std::nullopt,
67  long const offset = 0,
68  std::optional<long> np_to_deposit = std::nullopt,
69  int const icomp = 0, int const nc = 1,
70  amrex::Real * const AMREX_RESTRICT cost = nullptr,
71  long const load_balance_costs_update_algo = 0,
72  bool const do_device_synchronize = true)
73 {
74  // deposition guards
75  amrex::IntVect ng_rho = rho->nGrowVect();
76  if (num_rho_deposition_guards.has_value()) {
77  ng_rho = num_rho_deposition_guards.value();
78  }
79  ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE(ng_rho <= rho->nGrowVect(),
80  "num_rho_deposition_guards are larger than allocated!");
81  // particle shape
82  auto const[nox, noy, noz] = std::array<int, 3>{particle_shape, particle_shape, particle_shape};
83 
84  // used for MR when we want to deposit for a subset of the particles on the level in the
85  // current box; with offset, we start at a later particle index
86  if (!np_to_deposit.has_value()) {
87  np_to_deposit = pti.numParticles();
88  }
89  ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE(np_to_deposit.value() + offset <= pti.numParticles(),
90  "np_to_deposit + offset are out-of-bounds for particle iterator");
91 
92  int const lev = pti.GetLevel();
93  if (!depos_lev.has_value()) {
94  depos_lev = lev;
95  }
96  ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE((depos_lev.value() == (lev-1)) ||
97  (depos_lev.value() == (lev )),
98  "Deposition buffers only work for lev or lev-1");
99  if (!rel_ref_ratio.has_value()) {
100  ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE(lev == depos_lev,
101  "rel_ref_ratio must be set if lev != depos_lev");
102  rel_ref_ratio = amrex::IntVect(AMREX_D_DECL(1, 1, 1));
103  }
104 
105  // If no particles, do not do anything
106  if (np_to_deposit == 0) { return; }
107 
108  // Extract deposition order and check that particles shape fits within the guard cells.
109  // NOTE: In specific situations where the staggering of rho and the charge deposition algorithm
110  // are not trivial, this check might be too strict and we might need to relax it, as currently
111  // done for the current deposition.
112 
113 #if defined(WARPX_DIM_1D_Z)
116  const amrex::IntVect shape_extent = amrex::IntVect(static_cast<int>(noz/2+1));
117 #elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
119  const amrex::IntVect shape_extent = amrex::IntVect(static_cast<int>(nox/2+1),
120  static_cast<int>(noz/2+1));
121 #elif defined(WARPX_DIM_3D)
122  const amrex::IntVect shape_extent = amrex::IntVect(static_cast<int>(nox/2+1),
123  static_cast<int>(noy/2+1),
124  static_cast<int>(noz/2+1));
125 #endif
126 
127  // On CPU: particles deposit on tile arrays, which have a small number of guard cells ng_rho
128  // On GPU: particles deposit directly on the rho array, which usually have a larger number of guard cells
129 #ifndef AMREX_USE_GPU
130  const amrex::IntVect range = ng_rho - shape_extent;
131 #else
132  const amrex::IntVect range = rho->nGrowVect() - shape_extent;
133 #endif
134  amrex::ignore_unused(range); // for release builds
136  amrex::numParticlesOutOfRange(pti, range) == 0,
137  "Particles shape does not fit within tile (CPU) or guard cells (GPU) used for charge deposition");
138 
139  ABLASTR_PROFILE_VAR_NS("ablastr::particles::deposit_charge::ChargeDeposition", blp_ppc_chd, do_device_synchronize);
140  ABLASTR_PROFILE_VAR_NS("ablastr::particles::deposit_charge::Accumulate", blp_accumulate, do_device_synchronize);
141 
142  // Get tile box where charge is deposited.
143  // The tile box is different when depositing in the buffers (depos_lev<lev)
144  // or when depositing inside the level (depos_lev=lev)
145  amrex::Box tilebox;
146  if (lev == depos_lev) {
147  tilebox = pti.tilebox();
148  } else {
149  tilebox = amrex::coarsen(pti.tilebox(), rel_ref_ratio.value());
150  }
151 
152 #ifndef AMREX_USE_GPU
153  // Staggered tile box
154  amrex::Box tb = amrex::convert( tilebox, rho->ixType().toIntVect() );
155 #endif
156 
157  tilebox.grow(ng_rho);
158 
159 #ifdef AMREX_USE_GPU
160  amrex::ignore_unused(local_rho);
161  // GPU, no tiling: rho_fab points to the full rho array
162  amrex::MultiFab rhoi(*rho, amrex::make_alias, icomp*nc, nc);
163  auto & rho_fab = rhoi.get(pti);
164 #else
165  tb.grow(ng_rho);
166 
167  // CPU, tiling: rho_fab points to local_rho
168  local_rho.resize(tb, nc);
169 
170  // local_rho is set to zero
171  local_rho.setVal(0.0);
172 
173  auto & rho_fab = local_rho;
174 #endif
175 
176  const auto GetPosition = GetParticlePosition<PIdx>(pti, offset);
177 
178  // Indices of the lower bound
179  const amrex::Dim3 lo = lbound(tilebox);
180 
181  ABLASTR_PROFILE_VAR_START(blp_ppc_chd, do_device_synchronize);
182 
183  if (nox == 1){
184  doChargeDepositionShapeN<1>(GetPosition, wp.dataPtr()+offset, ion_lev,
185  rho_fab, np_to_deposit.value(), dx, xyzmin, lo, charge,
186  n_rz_azimuthal_modes, cost,
187  load_balance_costs_update_algo);
188  } else if (nox == 2){
189  doChargeDepositionShapeN<2>(GetPosition, wp.dataPtr()+offset, ion_lev,
190  rho_fab, np_to_deposit.value(), dx, xyzmin, lo, charge,
191  n_rz_azimuthal_modes, cost,
192  load_balance_costs_update_algo);
193  } else if (nox == 3){
194  doChargeDepositionShapeN<3>(GetPosition, wp.dataPtr()+offset, ion_lev,
195  rho_fab, np_to_deposit.value(), dx, xyzmin, lo, charge,
196  n_rz_azimuthal_modes, cost,
197  load_balance_costs_update_algo);
198  }
199  ABLASTR_PROFILE_VAR_STOP(blp_ppc_chd, do_device_synchronize);
200 
201 #ifndef AMREX_USE_GPU
202  // CPU, tiling: atomicAdd local_rho into rho
203  ABLASTR_PROFILE_VAR_START(blp_accumulate, do_device_synchronize);
204  (*rho)[pti].atomicAdd(local_rho, tb, tb, 0, icomp*nc, nc);
205  ABLASTR_PROFILE_VAR_STOP(blp_accumulate, do_device_synchronize);
206 #endif
207 }
208 
209 } // namespace ablastr::particles
210 
211 #endif // ABLASTR_DEPOSIT_CHARGE_H_
#define AMREX_D_DECL(a, b, c)
#define ABLASTR_PROFILE_VAR_START(vname, sync)
Definition: ProfilerWrapper.H:58
#define ABLASTR_PROFILE_VAR_NS(fname, vname, sync)
Definition: ProfilerWrapper.H:57
#define ABLASTR_PROFILE_VAR_STOP(vname, sync)
Definition: ProfilerWrapper.H:59
#define ABLASTR_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition: TextMsg.H:75
void setVal(Real const &x, const Box &bx, int dcomp, int ncomp) noexcept
AMREX_GPU_HOST_DEVICE Box & grow(int i) noexcept
void resize(const Box &b, int N=1, Arena *ar=nullptr)
IntVect nGrowVect() const noexcept
IndexType ixType() const noexcept
const FArrayBox & get(const MFIter &mfi) const noexcept
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE IntVect toIntVect() const noexcept
Definition: DepositCharge.H:21
static void deposit_charge(typename T_PC::ParIterType &pti, typename T_PC::RealVector const &wp, amrex::Real const charge, int const *const ion_lev, amrex::MultiFab *rho, amrex::FArrayBox &local_rho, int const particle_shape, std::array< amrex::Real, 3 > const &dx, std::array< amrex::Real, 3 > const &xyzmin, int const n_rz_azimuthal_modes=0, std::optional< amrex::IntVect > num_rho_deposition_guards=std::nullopt, std::optional< int > depos_lev=std::nullopt, std::optional< amrex::IntVect > rel_ref_ratio=std::nullopt, long const offset=0, std::optional< long > np_to_deposit=std::nullopt, int const icomp=0, int const nc=1, amrex::Real *const AMREX_RESTRICT cost=nullptr, long const load_balance_costs_update_algo=0, bool const do_device_synchronize=true)
Definition: DepositCharge.H:54
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Box convert(const Box &b, const IntVect &typ) noexcept
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 lbound(Array4< T > const &a) noexcept
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Box coarsen(const Box &b, int ref_ratio) noexcept
int numParticlesOutOfRange(Iterator const &pti, int nGrow)
int nc
Definition: check_interp_points_and_weights.py:130
tuple dx
lab frame
Definition: stencil.py:429