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):
58  m_fusion_multiplier{amrex::ParticleReal{1.0}}, // default fusion multiplier
59  m_probability_threshold{amrex::ParticleReal{0.02}}, // default fusion probability threshold
60  m_probability_target_value{amrex::ParticleReal{0.002}}, // default fusion probability target_value
61  m_isSameSpecies{isSameSpecies}
62  {
63 
64 #ifdef AMREX_SINGLE_PRECISION_PARTICLES
65  WARPX_ABORT_WITH_MESSAGE("Nuclear fusion module does not currently work with single precision");
66 #endif
67 
69 
70  const amrex::ParmParse pp_collision_name(collision_name);
72  pp_collision_name, "fusion_multiplier", m_fusion_multiplier);
74  pp_collision_name, "fusion_probability_threshold", m_probability_threshold);
76  pp_collision_name, "fusion_probability_target_value",
78  }
79 
118  index_type const I1s, index_type const I1e,
119  index_type const I2s, index_type const I2e,
120  index_type const* AMREX_RESTRICT I1,
121  index_type const* AMREX_RESTRICT I2,
122  SoaData_type soa_1, SoaData_type soa_2,
123  GetParticlePosition<PIdx> /*get_position_1*/, GetParticlePosition<PIdx> /*get_position_2*/,
124  amrex::ParticleReal const /*q1*/, amrex::ParticleReal const /*q2*/,
125  amrex::ParticleReal const m1, amrex::ParticleReal const m2,
126  amrex::Real const dt, amrex::Real const dV,
127  index_type const cell_start_pair, index_type* AMREX_RESTRICT p_mask,
128  index_type* AMREX_RESTRICT p_pair_indices_1, index_type* AMREX_RESTRICT p_pair_indices_2,
129  amrex::ParticleReal* AMREX_RESTRICT p_pair_reaction_weight,
130  amrex::RandomEngine const& engine) const
131  {
132 
133  amrex::ParticleReal * const AMREX_RESTRICT w1 = soa_1.m_rdata[PIdx::w];
134  amrex::ParticleReal * const AMREX_RESTRICT u1x = soa_1.m_rdata[PIdx::ux];
135  amrex::ParticleReal * const AMREX_RESTRICT u1y = soa_1.m_rdata[PIdx::uy];
136  amrex::ParticleReal * const AMREX_RESTRICT u1z = soa_1.m_rdata[PIdx::uz];
137 
138  amrex::ParticleReal * const AMREX_RESTRICT w2 = soa_2.m_rdata[PIdx::w];
139  amrex::ParticleReal * const AMREX_RESTRICT u2x = soa_2.m_rdata[PIdx::ux];
140  amrex::ParticleReal * const AMREX_RESTRICT u2y = soa_2.m_rdata[PIdx::uy];
141  amrex::ParticleReal * const AMREX_RESTRICT u2z = soa_2.m_rdata[PIdx::uz];
142 
143  // Number of macroparticles of each species
144  const index_type NI1 = I1e - I1s;
145  const index_type NI2 = I2e - I2s;
146  const index_type max_N = amrex::max(NI1,NI2);
147 
148  index_type i1 = I1s;
149  index_type i2 = I2s;
150  index_type pair_index = cell_start_pair;
151 
152  // Because the number of particles of each species is not always equal (NI1 != NI2
153  // in general), some macroparticles will be paired with multiple macroparticles of the
154  // other species and we need to decrease their weight accordingly.
155  // c1 corresponds to the minimum number of times a particle of species 1 will be paired
156  // with a particle of species 2. Same for c2.
157  const index_type c1 = amrex::max(NI2/NI1,1u);
158  const index_type c2 = amrex::max(NI1/NI2,1u);
159 
160  // multiplier ratio to take into account unsampled pairs
161  const auto multiplier_ratio = static_cast<int>(
162  (m_isSameSpecies)?(2u*max_N - 1):(max_N));
163 
164 #if (defined WARPX_DIM_RZ)
165  amrex::ParticleReal * const AMREX_RESTRICT theta1 = soa_1.m_rdata[PIdx::theta];
166  amrex::ParticleReal * const AMREX_RESTRICT theta2 = soa_2.m_rdata[PIdx::theta];
167 #endif
168 
169  for (index_type k = 0; k < max_N; ++k)
170  {
171  // c1k : how many times the current particle of species 1 is paired with a particle
172  // of species 2. Same for c2k.
173  const index_type c1k = (k%NI1 < max_N%NI1) ? c1 + 1: c1;
174  const index_type c2k = (k%NI2 < max_N%NI2) ? c2 + 1: c2;
175 
176 #if (defined WARPX_DIM_RZ)
177  /* In RZ geometry, macroparticles can collide with other macroparticles
178  * in the same *cylindrical* cell. For this reason, collisions between macroparticles
179  * are actually not local in space. In this case, the underlying assumption is that
180  * particles within the same cylindrical cell represent a cylindrically-symmetry
181  * momentum distribution function. Therefore, here, we temporarily rotate the
182  * momentum of one of the macroparticles in agreement with this cylindrical symmetry.
183  * (This is technically only valid if we use only the m=0 azimuthal mode in the simulation;
184  * there is a corresponding assert statement at initialization.) */
185  amrex::ParticleReal const theta = theta2[I2[i2]]-theta1[I1[i1]];
186  amrex::ParticleReal const u1xbuf = u1x[I1[i1]];
187  u1x[I1[i1]] = u1xbuf*std::cos(theta) - u1y[I1[i1]]*std::sin(theta);
188  u1y[I1[i1]] = u1xbuf*std::sin(theta) + u1y[I1[i1]]*std::cos(theta);
189 #endif
190 
192  u1x[ I1[i1] ], u1y[ I1[i1] ], u1z[ I1[i1] ],
193  u2x[ I2[i2] ], u2y[ I2[i2] ], u2z[ I2[i2] ],
194  m1, m2, w1[ I1[i1] ]/c1k, w2[ I2[i2] ]/c2k,
195  dt, dV, static_cast<int>(pair_index), p_mask, p_pair_reaction_weight,
196  m_fusion_multiplier, multiplier_ratio,
199  m_fusion_type, engine);
200 
201 #if (defined WARPX_DIM_RZ)
202  amrex::ParticleReal const u1xbuf_new = u1x[I1[i1]];
203  u1x[I1[i1]] = u1xbuf_new*std::cos(-theta) - u1y[I1[i1]]*std::sin(-theta);
204  u1y[I1[i1]] = u1xbuf_new*std::sin(-theta) + u1y[I1[i1]]*std::cos(-theta);
205 #endif
206 
207  p_pair_indices_1[pair_index] = I1[i1];
208  p_pair_indices_2[pair_index] = I2[i2];
209  ++i1; if ( i1 == I1e ) { i1 = I1s; }
210  ++i2; if ( i2 == I2e ) { i2 = I2s; }
211  ++pair_index;
212  }
213 
214  }
215 
216 private:
217  // Factor used to increase the number of fusion reaction by decreasing the weight of the
218  // produced particles
219  amrex::ParticleReal m_fusion_multiplier;
220  // If the fusion multiplier is too high and results in a fusion probability that approaches
221  // 1, there is a risk of underestimating the total fusion yield. In these cases, we reduce
222  // the fusion multiplier used in a given collision. m_probability_threshold is the fusion
223  // probability threshold above which we reduce the fusion multiplier.
224  // m_probability_target_value is the target probability used to determine by how much
225  // the fusion multiplier should be reduced.
226  amrex::ParticleReal m_probability_threshold;
227  amrex::ParticleReal m_probability_target_value;
230 };
231 
232 #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:219
bool m_isSameSpecies
Definition: NuclearFusionFunc.H:229
WarpXParticleContainer::ParticleType ParticleType
Definition: NuclearFusionFunc.H:38
ParticleBins::index_type index_type
Definition: NuclearFusionFunc.H:40
amrex::ParticleReal m_probability_threshold
Definition: NuclearFusionFunc.H:226
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< PIdx >, GetParticlePosition< PIdx >, 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:117
amrex::ParticleReal m_probability_target_value
Definition: NuclearFusionFunc.H:227
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:228
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:442
int queryWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition: ParserUtils.H:118
@ theta
RZ needs all three position components.
Definition: NamedComponentParticleContainer.H:29
@ uz
Definition: NamedComponentParticleContainer.H:27
@ w
weight
Definition: NamedComponentParticleContainer.H:26
@ uy
Definition: NamedComponentParticleContainer.H:27
@ ux
Definition: NamedComponentParticleContainer.H:27
GpuArray< ParticleReal *, NAR > m_rdata
ParticleTileData< StorageParticleType, NArrayReal, NArrayInt > ParticleTileDataType