WarpX
NuclearFusionFunc.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 NUCLEAR_FUSION_FUNC_H_
9 #define NUCLEAR_FUSION_FUNC_H_
10 
12 
18 #include "Utils/TextMsg.H"
19 #include "WarpX.H"
20 
21 #include <AMReX_Algorithm.H>
22 #include <AMReX_DenseBins.H>
23 #include <AMReX_ParmParse.H>
24 #include <AMReX_Random.H>
25 #include <AMReX_REAL.H>
26 #include <AMReX_Vector.H>
27 
37  // Define shortcuts for frequently-used type names
42 
43 public:
47  NuclearFusionFunc () = default;
48 
56  NuclearFusionFunc (const std::string collision_name, MultiParticleContainer const * const mypc,
57  const bool isSameSpecies) : m_isSameSpecies(isSameSpecies)
58  {
59  using namespace amrex::literals;
60 
61 #ifdef AMREX_SINGLE_PRECISION_PARTICLES
62  WARPX_ABORT_WITH_MESSAGE("Nuclear fusion module does not currently work with single precision");
63 #endif
64 
66 
67  const amrex::ParmParse pp_collision_name(collision_name);
68  // default fusion multiplier
69  m_fusion_multiplier = 1.0_prt;
71  pp_collision_name, "fusion_multiplier", m_fusion_multiplier);
72  // default fusion probability threshold
73  m_probability_threshold = 0.02_prt;
75  pp_collision_name, "fusion_probability_threshold", m_probability_threshold);
76  // default fusion probability target_value
77  m_probability_target_value = 0.002_prt;
79  pp_collision_name, "fusion_probability_target_value",
81  }
82 
121  index_type const I1s, index_type const I1e,
122  index_type const I2s, index_type const I2e,
123  index_type const* AMREX_RESTRICT I1,
124  index_type const* AMREX_RESTRICT I2,
125  SoaData_type soa_1, SoaData_type soa_2,
126  GetParticlePosition /*get_position_1*/, GetParticlePosition /*get_position_2*/,
127  amrex::ParticleReal const /*q1*/, amrex::ParticleReal const /*q2*/,
128  amrex::ParticleReal const m1, amrex::ParticleReal const m2,
129  amrex::Real const dt, amrex::Real const dV,
130  index_type const cell_start_pair, index_type* AMREX_RESTRICT p_mask,
131  index_type* AMREX_RESTRICT p_pair_indices_1, index_type* AMREX_RESTRICT p_pair_indices_2,
132  amrex::ParticleReal* AMREX_RESTRICT p_pair_reaction_weight,
133  amrex::RandomEngine const& engine) const
134  {
135 
136  amrex::ParticleReal * const AMREX_RESTRICT w1 = soa_1.m_rdata[PIdx::w];
137  amrex::ParticleReal * const AMREX_RESTRICT u1x = soa_1.m_rdata[PIdx::ux];
138  amrex::ParticleReal * const AMREX_RESTRICT u1y = soa_1.m_rdata[PIdx::uy];
139  amrex::ParticleReal * const AMREX_RESTRICT u1z = soa_1.m_rdata[PIdx::uz];
140 
141  amrex::ParticleReal * const AMREX_RESTRICT w2 = soa_2.m_rdata[PIdx::w];
142  amrex::ParticleReal * const AMREX_RESTRICT u2x = soa_2.m_rdata[PIdx::ux];
143  amrex::ParticleReal * const AMREX_RESTRICT u2y = soa_2.m_rdata[PIdx::uy];
144  amrex::ParticleReal * const AMREX_RESTRICT u2z = soa_2.m_rdata[PIdx::uz];
145 
146  // Number of macroparticles of each species
147  const int NI1 = I1e - I1s;
148  const int NI2 = I2e - I2s;
149  const int max_N = amrex::max(NI1,NI2);
150 
151  int i1 = I1s;
152  int i2 = I2s;
153  int pair_index = cell_start_pair;
154 
155  // Because the number of particles of each species is not always equal (NI1 != NI2
156  // in general), some macroparticles will be paired with multiple macroparticles of the
157  // other species and we need to decrease their weight accordingly.
158  // c1 corresponds to the minimum number of times a particle of species 1 will be paired
159  // with a particle of species 2. Same for c2.
160  const int c1 = amrex::max(NI2/NI1,1);
161  const int c2 = amrex::max(NI1/NI2,1);
162 
163  // multiplier ratio to take into account unsampled pairs
164  const int multiplier_ratio = (m_isSameSpecies)?(2*max_N - 1):(max_N);
165 
166 #if (defined WARPX_DIM_RZ)
167  amrex::ParticleReal * const AMREX_RESTRICT theta1 = soa_1.m_rdata[PIdx::theta];
168  amrex::ParticleReal * const AMREX_RESTRICT theta2 = soa_2.m_rdata[PIdx::theta];
169 #endif
170 
171  for (int k = 0; k < max_N; ++k)
172  {
173  // c1k : how many times the current particle of species 1 is paired with a particle
174  // of species 2. Same for c2k.
175  const int c1k = (k%NI1 < max_N%NI1) ? c1 + 1: c1;
176  const int c2k = (k%NI2 < max_N%NI2) ? c2 + 1: c2;
177 
178 #if (defined WARPX_DIM_RZ)
179  /* In RZ geometry, macroparticles can collide with other macroparticles
180  * in the same *cylindrical* cell. For this reason, collisions between macroparticles
181  * are actually not local in space. In this case, the underlying assumption is that
182  * particles within the same cylindrical cell represent a cylindrically-symmetry
183  * momentum distribution function. Therefore, here, we temporarily rotate the
184  * momentum of one of the macroparticles in agreement with this cylindrical symmetry.
185  * (This is technically only valid if we use only the m=0 azimuthal mode in the simulation;
186  * there is a corresponding assert statement at initialization.) */
187  amrex::ParticleReal const theta = theta2[I2[i2]]-theta1[I1[i1]];
188  amrex::ParticleReal const u1xbuf = u1x[I1[i1]];
189  u1x[I1[i1]] = u1xbuf*std::cos(theta) - u1y[I1[i1]]*std::sin(theta);
190  u1y[I1[i1]] = u1xbuf*std::sin(theta) + u1y[I1[i1]]*std::cos(theta);
191 #endif
192 
194  u1x[ I1[i1] ], u1y[ I1[i1] ], u1z[ I1[i1] ],
195  u2x[ I2[i2] ], u2y[ I2[i2] ], u2z[ I2[i2] ],
196  m1, m2, w1[ I1[i1] ]/c1k, w2[ I2[i2] ]/c2k,
197  dt, dV, pair_index, p_mask, p_pair_reaction_weight,
198  m_fusion_multiplier, multiplier_ratio,
201  m_fusion_type, engine);
202 
203 #if (defined WARPX_DIM_RZ)
204  amrex::ParticleReal const u1xbuf_new = u1x[I1[i1]];
205  u1x[I1[i1]] = u1xbuf_new*std::cos(-theta) - u1y[I1[i1]]*std::sin(-theta);
206  u1y[I1[i1]] = u1xbuf_new*std::sin(-theta) + u1y[I1[i1]]*std::cos(-theta);
207 #endif
208 
209  p_pair_indices_1[pair_index] = I1[i1];
210  p_pair_indices_2[pair_index] = I2[i2];
211  ++i1; if ( i1 == static_cast<int>(I1e) ) { i1 = I1s; }
212  ++i2; if ( i2 == static_cast<int>(I2e) ) { i2 = I2s; }
213  ++pair_index;
214  }
215 
216  }
217 
218 private:
219  // Factor used to increase the number of fusion reaction by decreasing the weight of the
220  // produced particles
221  amrex::ParticleReal m_fusion_multiplier;
222  // If the fusion multiplier is too high and results in a fusion probability that approaches
223  // 1, there is a risk of underestimating the total fusion yield. In these cases, we reduce
224  // the fusion multiplier used in a given collision. m_probability_threshold is the fusion
225  // probability threshold above which we reduce the fusion multiplier.
226  // m_probability_target_value is the target probability used to determine by how much
227  // the fusion multiplier should be reduced.
228  amrex::ParticleReal m_probability_threshold;
229  amrex::ParticleReal m_probability_target_value;
232 };
233 
234 #endif // NUCLEAR_FUSION_FUNC_H_
#define AMREX_RESTRICT
#define AMREX_INLINE
#define AMREX_GPU_HOST_DEVICE
NuclearFusionType
Definition: BinaryCollisionUtils.H:22
AMREX_GPU_HOST_DEVICE AMREX_INLINE void SingleNuclearFusionEvent(const amrex::ParticleReal &u1x, const amrex::ParticleReal &u1y, const amrex::ParticleReal &u1z, const amrex::ParticleReal &u2x, const amrex::ParticleReal &u2y, const amrex::ParticleReal &u2z, const amrex::ParticleReal &m1, const amrex::ParticleReal &m2, amrex::ParticleReal w1, amrex::ParticleReal w2, const amrex::Real &dt, const amrex::ParticleReal &dV, const int &pair_index, index_type *AMREX_RESTRICT p_mask, amrex::ParticleReal *AMREX_RESTRICT p_pair_reaction_weight, const amrex::ParticleReal &fusion_multiplier, const int &multiplier_ratio, const amrex::ParticleReal &probability_threshold, const amrex::ParticleReal &probability_target_value, const NuclearFusionType &fusion_type, const amrex::RandomEngine &engine)
This function computes whether the collision between two particles result in a nuclear fusion event,...
Definition: SingleNuclearFusionEvent.H:55
#define WARPX_ABORT_WITH_MESSAGE(MSG)
Definition: TextMsg.H:15
Definition: MultiParticleContainer.H:65
This functor does binary nuclear fusions on a single cell. Particles of the two reacting species are ...
Definition: NuclearFusionFunc.H:36
NuclearFusionFunc()=default
Default constructor of the NuclearFusionFunc class.
amrex::ParticleReal m_fusion_multiplier
Definition: NuclearFusionFunc.H:221
bool m_isSameSpecies
Definition: NuclearFusionFunc.H:231
WarpXParticleContainer::ParticleType ParticleType
Definition: NuclearFusionFunc.H:38
ParticleBins::index_type index_type
Definition: NuclearFusionFunc.H:40
amrex::ParticleReal m_probability_threshold
Definition: NuclearFusionFunc.H:228
AMREX_GPU_HOST_DEVICE AMREX_INLINE void operator()(index_type const I1s, index_type const I1e, index_type const I2s, index_type const I2e, index_type const *AMREX_RESTRICT I1, index_type const *AMREX_RESTRICT I2, SoaData_type soa_1, SoaData_type soa_2, GetParticlePosition, GetParticlePosition, amrex::ParticleReal const, amrex::ParticleReal const, amrex::ParticleReal const m1, amrex::ParticleReal const m2, amrex::Real const dt, amrex::Real const dV, index_type const cell_start_pair, index_type *AMREX_RESTRICT p_mask, index_type *AMREX_RESTRICT p_pair_indices_1, index_type *AMREX_RESTRICT p_pair_indices_2, amrex::ParticleReal *AMREX_RESTRICT p_pair_reaction_weight, amrex::RandomEngine const &engine) const
operator() of the NuclearFusionFunc class. Performs nuclear fusions at the cell level using the algor...
Definition: NuclearFusionFunc.H:120
amrex::ParticleReal m_probability_target_value
Definition: NuclearFusionFunc.H:229
NuclearFusionFunc(const std::string collision_name, MultiParticleContainer const *const mypc, const bool isSameSpecies)
Constructor of the NuclearFusionFunc class.
Definition: NuclearFusionFunc.H:56
NuclearFusionType m_fusion_type
Definition: NuclearFusionFunc.H:230
unsigned int index_type
NuclearFusionType get_nuclear_fusion_type(const std::string collision_name, MultiParticleContainer const *const mypc)
Definition: BinaryCollisionUtils.cpp:20
AMREX_GPU_HOST_DEVICE constexpr AMREX_FORCE_INLINE const T & max(const T &a, const T &b) noexcept
float dt
Definition: stencil.py:440
int queryWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition: ParserUtils.H:118
Functor that can be used to extract the positions of the macroparticles inside a ParallelFor kernel.
Definition: GetAndSetPosition.H:53
@ theta
RZ needs all three position components.
Definition: NamedComponentParticleContainer.H:28
@ uz
Definition: NamedComponentParticleContainer.H:26
@ w
weight
Definition: NamedComponentParticleContainer.H:25
@ uy
Definition: NamedComponentParticleContainer.H:26
@ ux
Definition: NamedComponentParticleContainer.H:26
GpuArray< ParticleReal *, NAR > m_rdata
ParticleTileData< StorageParticleType, NArrayReal, NArrayInt > ParticleTileDataType