ARTEMIS
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  amrex::Abort("Nuclear fusion module does not currently work with single precision");
63 #endif
64 
66 
67  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  int multiplier_ratio;
165  if (m_isSameSpecies)
166  {
167  multiplier_ratio = 2*max_N - 1;
168  }
169  else
170  {
171  multiplier_ratio = max_N;
172  }
173 
174 #if (defined WARPX_DIM_RZ)
175  amrex::ParticleReal * const AMREX_RESTRICT theta1 = soa_1.m_rdata[PIdx::theta];
176  amrex::ParticleReal * const AMREX_RESTRICT theta2 = soa_2.m_rdata[PIdx::theta];
177 #endif
178 
179  for (int k = 0; k < max_N; ++k)
180  {
181  // c1k : how many times the current particle of species 1 is paired with a particle
182  // of species 2. Same for c2k.
183  const int c1k = (k%NI1 < max_N%NI1) ? c1 + 1: c1;
184  const int c2k = (k%NI2 < max_N%NI2) ? c2 + 1: c2;
185 
186 #if (defined WARPX_DIM_RZ)
187  /* In RZ geometry, macroparticles can collide with other macroparticles
188  * in the same *cylindrical* cell. For this reason, collisions between macroparticles
189  * are actually not local in space. In this case, the underlying assumption is that
190  * particles within the same cylindrical cell represent a cylindrically-symmetry
191  * momentum distribution function. Therefore, here, we temporarily rotate the
192  * momentum of one of the macroparticles in agreement with this cylindrical symmetry.
193  * (This is technically only valid if we use only the m=0 azimuthal mode in the simulation;
194  * there is a corresponding assert statement at initialization.) */
195  amrex::ParticleReal const theta = theta2[I2[i2]]-theta1[I1[i1]];
196  amrex::ParticleReal const u1xbuf = u1x[I1[i1]];
197  u1x[I1[i1]] = u1xbuf*std::cos(theta) - u1y[I1[i1]]*std::sin(theta);
198  u1y[I1[i1]] = u1xbuf*std::sin(theta) + u1y[I1[i1]]*std::cos(theta);
199 #endif
200 
202  u1x[ I1[i1] ], u1y[ I1[i1] ], u1z[ I1[i1] ],
203  u2x[ I2[i2] ], u2y[ I2[i2] ], u2z[ I2[i2] ],
204  m1, m2, w1[ I1[i1] ]/c1k, w2[ I2[i2] ]/c2k,
205  dt, dV, pair_index, p_mask, p_pair_reaction_weight,
206  m_fusion_multiplier, multiplier_ratio,
209  m_fusion_type, engine);
210 
211 #if (defined WARPX_DIM_RZ)
212  amrex::ParticleReal const u1xbuf_new = u1x[I1[i1]];
213  u1x[I1[i1]] = u1xbuf_new*std::cos(-theta) - u1y[I1[i1]]*std::sin(-theta);
214  u1y[I1[i1]] = u1xbuf_new*std::sin(-theta) + u1y[I1[i1]]*std::cos(-theta);
215 #endif
216 
217  p_pair_indices_1[pair_index] = I1[i1];
218  p_pair_indices_2[pair_index] = I2[i2];
219  ++i1; if ( i1 == static_cast<int>(I1e) ) { i1 = I1s; }
220  ++i2; if ( i2 == static_cast<int>(I2e) ) { i2 = I2s; }
221  ++pair_index;
222  }
223 
224  }
225 
226 private:
227  // Factor used to increase the number of fusion reaction by decreasing the weight of the
228  // produced particles
229  amrex::ParticleReal m_fusion_multiplier;
230  // If the fusion multiplier is too high and results in a fusion probability that approaches
231  // 1, there is a risk of underestimating the total fusion yield. In these cases, we reduce
232  // the fusion multiplier used in a given collision. m_probability_threshold is the fusion
233  // probability threshold above which we reduce the fusion multiplier.
234  // m_probability_target_value is the target probability used to determine by how much
235  // the fusion multiplier should be reduced.
236  amrex::ParticleReal m_probability_threshold;
237  amrex::ParticleReal m_probability_target_value;
240 };
241 
242 #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
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:229
bool m_isSameSpecies
Definition: NuclearFusionFunc.H:239
WarpXParticleContainer::ParticleType ParticleType
Definition: NuclearFusionFunc.H:38
ParticleBins::index_type index_type
Definition: NuclearFusionFunc.H:40
amrex::ParticleReal m_probability_threshold
Definition: NuclearFusionFunc.H:236
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:237
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:238
NuclearFusionType get_nuclear_fusion_type(const std::string collision_name, MultiParticleContainer const *const mypc)
Definition: BinaryCollisionUtils.cpp:20
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr const T & max(const T &a, const T &b) noexcept
void Abort(const std::string &msg)
int 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:27
@ uz
Definition: NamedComponentParticleContainer.H:25
@ w
weight
Definition: NamedComponentParticleContainer.H:24
@ uy
Definition: NamedComponentParticleContainer.H:25
@ ux
Definition: NamedComponentParticleContainer.H:25
GpuArray< ParticleReal *, NAR > m_rdata
ParticleTileData< StorageParticleType, NArrayReal, NArrayInt > ParticleTileDataType