WarpX
ParticleCreationFunc.H
Go to the documentation of this file.
1 /* Copyright 2021 Neil Zaim
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 
8 #ifndef PARTICLE_CREATION_FUNC_H_
9 #define PARTICLE_CREATION_FUNC_H_
10 
11 #include "BinaryCollisionUtils.H"
12 
18 #include "WarpX.H"
19 
20 #include <AMReX_DenseBins.H>
21 #include <AMReX_GpuAtomic.H>
22 #include <AMReX_GpuDevice.H>
23 #include <AMReX_GpuContainers.H>
24 #include <AMReX_INT.H>
25 #include <AMReX_Random.H>
26 #include <AMReX_REAL.H>
27 #include <AMReX_Vector.H>
28 
34  // Define shortcuts for frequently-used type names
40 
41 public:
45  ParticleCreationFunc () = default;
46 
53  ParticleCreationFunc (std::string collision_name, MultiParticleContainer const * mypc);
54 
101  const index_type& n_total_pairs,
102  const SoaData_type& soa_1, const SoaData_type& soa_2,
103  ParticleTileType** AMREX_RESTRICT tile_products,
104  ParticleType* particle_ptr_1, ParticleType* particle_ptr_2,
105  const amrex::ParticleReal& m1, const amrex::ParticleReal& m2,
106  const amrex::Vector<amrex::ParticleReal>& products_mass,
107  const index_type* AMREX_RESTRICT p_mask,
108  const amrex::Vector<index_type>& products_np,
109  const SmartCopy* AMREX_RESTRICT copy_species1,
110  const SmartCopy* AMREX_RESTRICT copy_species2,
111  const index_type* AMREX_RESTRICT p_pair_indices_1,
112  const index_type* AMREX_RESTRICT p_pair_indices_2,
113  const amrex::ParticleReal* AMREX_RESTRICT p_pair_reaction_weight
114  ) const
115  {
116  using namespace amrex::literals;
117 
118  if (n_total_pairs == 0) return amrex::Vector<int>(m_num_product_species, 0);
119 
120  // Compute offset array and allocate memory for the produced species
121  amrex::Gpu::DeviceVector<index_type> offsets(n_total_pairs);
122  const auto total = amrex::Scan::ExclusiveSum(n_total_pairs, p_mask, offsets.data());
123  const index_type* AMREX_RESTRICT p_offsets = offsets.dataPtr();
125  for (int i = 0; i < m_num_product_species; i++)
126  {
127  // How many particles of product species i are created.
128  // Factor 2 is here because we currently create one product species at the position of
129  // each source particle of the binary collision. E.g., if a binary collision produces
130  // one electron, we create two electrons, one at the position of each particle that
131  // collided. This allows for exact charge conservation.
132  const index_type num_added = total * m_num_products_host[i] * 2;
133  num_added_vec[i] = static_cast<int>(num_added);
134  tile_products[i]->resize(products_np[i] + num_added);
135  }
136 
137  amrex::ParticleReal* AMREX_RESTRICT w1 = soa_1.m_rdata[PIdx::w];
138  amrex::ParticleReal* AMREX_RESTRICT w2 = soa_2.m_rdata[PIdx::w];
139 
140  // Create necessary GPU vectors, that will be used in the kernel below
141  amrex::Vector<SoaData_type> soa_products;
142  for (int i = 0; i < m_num_product_species; i++)
143  {
144  soa_products.push_back(tile_products[i]->getParticleTileData());
145  }
146 #ifdef AMREX_USE_GPU
150  amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, soa_products.begin(),
151  soa_products.end(),
152  device_soa_products.begin());
153  amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, products_np.begin(),
154  products_np.end(),
155  device_products_np.begin());
156  amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, products_mass.begin(),
157  products_mass.end(),
158  device_products_mass.begin());
160  SoaData_type* AMREX_RESTRICT soa_products_data = device_soa_products.data();
161  const index_type* AMREX_RESTRICT products_np_data = device_products_np.data();
162  const amrex::ParticleReal* AMREX_RESTRICT products_mass_data = device_products_mass.data();
163 #else
164  SoaData_type* AMREX_RESTRICT soa_products_data = soa_products.data();
165  const index_type* AMREX_RESTRICT products_np_data = products_np.data();
166  const amrex::ParticleReal* AMREX_RESTRICT products_mass_data = products_mass.data();
167 #endif
168 
169  const int t_num_product_species = m_num_product_species;
170  const int* AMREX_RESTRICT p_num_products_device = m_num_products_device.data();
171  const CollisionType t_collision_type = m_collision_type;
172 
173  amrex::ParallelForRNG(n_total_pairs,
174  [=] AMREX_GPU_DEVICE (int i, amrex::RandomEngine const& engine) noexcept
175  {
176  if (p_mask[i])
177  {
178  for (int j = 0; j < t_num_product_species; j++)
179  {
180  for (int k = 0; k < p_num_products_device[j]; k++)
181  {
182  // Factor 2 is here because we create one product species at the position
183  // of each source particle
184  const auto product_index = products_np_data[j] +
185  2*(p_offsets[i]*p_num_products_device[j] + k);
186  // Create product particle at position of particle 1
187  copy_species1[j](soa_products_data[j], soa_1, static_cast<int>(p_pair_indices_1[i]),
188  static_cast<int>(product_index), engine);
189  // Create another product particle at position of particle 2
190  copy_species2[j](soa_products_data[j], soa_2, static_cast<int>(p_pair_indices_2[i]),
191  static_cast<int>(product_index + 1), engine);
192 
193  // Set the weight of the new particles to p_pair_reaction_weight[i]/2
194  soa_products_data[j].m_rdata[PIdx::w][product_index] =
195  p_pair_reaction_weight[i]/amrex::ParticleReal(2.);
196  soa_products_data[j].m_rdata[PIdx::w][product_index + 1] =
197  p_pair_reaction_weight[i]/amrex::ParticleReal(2.);
198  }
199  }
200 
201  // Remove p_pair_reaction_weight[i] from the colliding particles' weights
202  amrex::Gpu::Atomic::AddNoRet(&w1[p_pair_indices_1[i]],
203  -p_pair_reaction_weight[i]);
204  amrex::Gpu::Atomic::AddNoRet(&w2[p_pair_indices_2[i]],
205  -p_pair_reaction_weight[i]);
206 
207  // If the colliding particle weight decreases to zero, remove particle by
208  // setting its id to -1
209  constexpr amrex::Long minus_one_long = -1;
210  if (w1[p_pair_indices_1[i]] <= amrex::ParticleReal(0.))
211  {
212  particle_ptr_1[p_pair_indices_1[i]].atomicSetID(minus_one_long);
213  }
214  if (w2[p_pair_indices_2[i]] <= amrex::ParticleReal(0.))
215  {
216  particle_ptr_2[p_pair_indices_2[i]].atomicSetID(minus_one_long);
217  }
218 
219  // Initialize the product particles' momentum, using a function depending on the
220  // specific collision type
221  if (t_collision_type == CollisionType::ProtonBoronToAlphasFusion)
222  {
223  const index_type product_start_index = products_np_data[0] + 2*p_offsets[i]*
224  p_num_products_device[0];
225  ProtonBoronFusionInitializeMomentum(soa_1, soa_2, soa_products_data[0],
226  p_pair_indices_1[i], p_pair_indices_2[i],
227  product_start_index, m1, m2, engine);
228  }
229  else if ((t_collision_type == CollisionType::DeuteriumTritiumToNeutronHeliumFusion)
232  {
233  amrex::ParticleReal fusion_energy = 0.0_prt;
235  fusion_energy = 17.5893e6_prt * PhysConst::q_e;
236  }
237  else if (t_collision_type == CollisionType::DeuteriumDeuteriumToProtonTritiumFusion) {
238  fusion_energy = 4.032667e6_prt * PhysConst::q_e;
239  }
240  else if (t_collision_type == CollisionType::DeuteriumDeuteriumToNeutronHeliumFusion) {
241  fusion_energy = 3.268911e6_prt * PhysConst::q_e;
242  }
243  TwoProductFusionInitializeMomentum(soa_1, soa_2,
244  soa_products_data[0], soa_products_data[1],
245  p_pair_indices_1[i], p_pair_indices_2[i],
246  products_np_data[0] + 2*p_offsets[i]*p_num_products_device[0],
247  products_np_data[1] + 2*p_offsets[i]*p_num_products_device[1],
248  m1, m2, products_mass_data[0], products_mass_data[1], fusion_energy, engine);
249  }
250 
251  }
252  });
253 
255 
256  return num_added_vec;
257  }
258 
259 private:
260  // How many different type of species the collision produces
262  // Vectors of size m_num_product_species storing how many particles of a given species are
263  // produced by a collision event. These vectors are duplicated (one version for host and one
264  // for device) which is necessary with GPUs but redundant on CPU.
268 };
269 
270 
281 
282 public:
284 
285  NoParticleCreationFunc (const std::string /*collision_name*/,
286  MultiParticleContainer const * const /*mypc*/) {}
287 
290  const index_type& /*n_total_pairs*/,
291  const SoaData_type& /*soa_1*/, const SoaData_type& /*soa_2*/,
292  ParticleTileType** /*tile_products*/,
293  ParticleType* /*particle_ptr_1*/, ParticleType* /*particle_ptr_2*/,
294  const amrex::ParticleReal& /*m1*/, const amrex::ParticleReal& /*m2*/,
295  const amrex::Vector<amrex::ParticleReal>& /*products_mass*/,
296  const index_type* /*p_mask*/, const amrex::Vector<index_type>& /*products_np*/,
297  const SmartCopy* /*copy_species1*/, const SmartCopy* /*copy_species2*/,
298  const index_type* /*p_pair_indices_1*/, const index_type* /*p_pair_indices_2*/,
299  const amrex::ParticleReal* /*p_pair_reaction_weight*/
300  ) const
301  {
302  return {};
303  }
304 };
305 
306 #endif // PARTICLE_CREATION_FUNC_H_
#define AMREX_RESTRICT
#define AMREX_INLINE
#define AMREX_GPU_DEVICE
CollisionType
Definition: BinaryCollisionUtils.H:15
@ ProtonBoronToAlphasFusion
@ DeuteriumDeuteriumToProtonTritiumFusion
@ DeuteriumDeuteriumToNeutronHeliumFusion
@ DeuteriumTritiumToNeutronHeliumFusion
Definition: MultiParticleContainer.H:65
This class does nothing and is used as second template parameter for binary collisions that do not cr...
Definition: ParticleCreationFunc.H:275
NoParticleCreationFunc(const std::string, MultiParticleContainer const *const)
Definition: ParticleCreationFunc.H:285
WarpXParticleContainer::ParticleType ParticleType
Definition: ParticleCreationFunc.H:276
NoParticleCreationFunc()=default
ParticleBins::index_type index_type
Definition: ParticleCreationFunc.H:279
This functor creates particles produced from a binary collision and sets their initial properties (po...
Definition: ParticleCreationFunc.H:33
WarpXParticleContainer::ParticleType ParticleType
Definition: ParticleCreationFunc.H:35
CollisionType m_collision_type
Definition: ParticleCreationFunc.H:267
AMREX_INLINE amrex::Vector< int > operator()(const index_type &n_total_pairs, const SoaData_type &soa_1, const SoaData_type &soa_2, ParticleTileType **AMREX_RESTRICT tile_products, ParticleType *particle_ptr_1, ParticleType *particle_ptr_2, const amrex::ParticleReal &m1, const amrex::ParticleReal &m2, const amrex::Vector< amrex::ParticleReal > &products_mass, const index_type *AMREX_RESTRICT p_mask, const amrex::Vector< index_type > &products_np, const SmartCopy *AMREX_RESTRICT copy_species1, const SmartCopy *AMREX_RESTRICT copy_species2, const index_type *AMREX_RESTRICT p_pair_indices_1, const index_type *AMREX_RESTRICT p_pair_indices_2, const amrex::ParticleReal *AMREX_RESTRICT p_pair_reaction_weight) const
operator() of the ParticleCreationFunc class. It creates new particles from binary collisions....
Definition: ParticleCreationFunc.H:100
ParticleBins::index_type index_type
Definition: ParticleCreationFunc.H:38
int m_num_product_species
Definition: ParticleCreationFunc.H:261
amrex::Gpu::DeviceVector< int > m_num_products_device
Definition: ParticleCreationFunc.H:265
ParticleCreationFunc()=default
Default constructor of the ParticleCreationFunc class.
amrex::Gpu::HostVector< int > m_num_products_host
Definition: ParticleCreationFunc.H:266
unsigned int index_type
T * data() noexcept
iterator begin() noexcept
T * dataPtr() noexcept
ParticleTile< ParticleType, NArrayReal, NArrayInt, Allocator > ParticleTileType
static constexpr auto q_e
elementary charge [C]
Definition: constant.H:50
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
void synchronize() noexcept
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
static constexpr HostToDevice hostToDevice
void streamSynchronize() noexcept
T ExclusiveSum(N n, T const *in, T *out, RetSum a_ret_sum=retSum)
void ParallelForRNG(T n, L &&f) noexcept
i
Definition: check_interp_points_and_weights.py:174
@ w
weight
Definition: NamedComponentParticleContainer.H:26
This is a functor for performing a "smart copy" that works in both host and device code.
Definition: SmartCopy.H:34
GpuArray< ParticleReal *, NAR > m_rdata
ParticleTileData< StorageParticleType, NArrayReal, NArrayInt > ParticleTileDataType