WarpX
Loading...
Searching...
No Matches
DSMCFunc.H
Go to the documentation of this file.
1/* Copyright 2024 The WarpX Community
2 *
3 * This file is part of WarpX.
4 *
5 * Authors: Roelof Groenewald (TAE Technologies)
6 *
7 * License: BSD-3-Clause-LBNL
8 */
9
10#ifndef WARPX_DSMC_FUNC_H_
11#define WARPX_DSMC_FUNC_H_
12
13#include "CollisionFilterFunc.H"
14
24#include "Utils/ParticleUtils.H"
26
27#include <AMReX_DenseBins.H>
28#include <AMReX_ParmParse.H>
29#include <AMReX_Random.H>
30
38{
39 // Define shortcuts for frequently-used type names
45 using SoaData_type = WarpXParticleContainer::ParticleTileType::ParticleTileDataType;
46
47public:
48
52 DSMCFunc () = default;
53
61 DSMCFunc ( const std::string& collision_name,
62 [[maybe_unused]] MultiParticleContainer const * mypc,
63 const bool isSameSpecies );
64
65 struct Executor {
93 index_type const I1s, index_type const I1e,
94 index_type const I2s, index_type const I2e,
95 index_type const* AMREX_RESTRICT I1,
96 index_type const* AMREX_RESTRICT I2,
97 const SoaData_type& soa_1, const SoaData_type& soa_2,
98 GetParticlePosition<PIdx> /*get_position_1*/, GetParticlePosition<PIdx> /*get_position_2*/,
99 amrex::ParticleReal const /*n1*/, amrex::ParticleReal const /*n2*/,
100 amrex::ParticleReal const /*T1*/, amrex::ParticleReal const /*T2*/,
101 amrex::Real const /*global_lamdb*/,
102 amrex::ParticleReal const /*q1*/, amrex::ParticleReal const /*q2*/,
103 amrex::ParticleReal const m1, amrex::ParticleReal const m2,
104 amrex::Real const dt, amrex::Real const dV, index_type coll_idx,
105 index_type const cell_start_pair, index_type* AMREX_RESTRICT p_mask,
106 index_type* AMREX_RESTRICT p_pair_indices_1, index_type* AMREX_RESTRICT p_pair_indices_2,
107 amrex::ParticleReal* AMREX_RESTRICT p_pair_reaction_weight,
108 amrex::ParticleReal* /*p_product_data*/,
109 amrex::RandomEngine const& engine) const
110 {
111 amrex::ParticleReal * const AMREX_RESTRICT w1 = soa_1.m_rdata[PIdx::w];
112 amrex::ParticleReal * const AMREX_RESTRICT u1x = soa_1.m_rdata[PIdx::ux];
113 amrex::ParticleReal * const AMREX_RESTRICT u1y = soa_1.m_rdata[PIdx::uy];
114 amrex::ParticleReal * const AMREX_RESTRICT u1z = soa_1.m_rdata[PIdx::uz];
115 uint64_t* AMREX_RESTRICT idcpu1 = soa_1.m_idcpu;
116
117 amrex::ParticleReal * const AMREX_RESTRICT w2 = soa_2.m_rdata[PIdx::w];
118 amrex::ParticleReal * const AMREX_RESTRICT u2x = soa_2.m_rdata[PIdx::ux];
119 amrex::ParticleReal * const AMREX_RESTRICT u2y = soa_2.m_rdata[PIdx::uy];
120 amrex::ParticleReal * const AMREX_RESTRICT u2z = soa_2.m_rdata[PIdx::uz];
121 uint64_t* AMREX_RESTRICT idcpu2 = soa_2.m_idcpu;
122
123 // Number of macroparticles of each species
124 const index_type NI1 = I1e - I1s;
125 const index_type NI2 = I2e - I2s;
126 const index_type max_N = amrex::max(NI1,NI2);
127 const index_type min_N = amrex::min(NI1,NI2);
128
129 index_type pair_index = cell_start_pair + coll_idx;
130
131 // multiplier ratio to take into account unsampled pairs
132 const auto multiplier_ratio = static_cast<int>(
133 m_isSameSpecies ? min_N + max_N - 1 : min_N);
134
135#if (defined WARPX_DIM_RZ)
136 amrex::ParticleReal * const AMREX_RESTRICT theta1 = soa_1.m_rdata[PIdx::theta];
137 amrex::ParticleReal * const AMREX_RESTRICT theta2 = soa_2.m_rdata[PIdx::theta];
138#endif
139 index_type i1 = I1s + coll_idx;
140 index_type i2 = I2s + coll_idx;
141 // we will start from collision number = coll_idx and then add
142 // stride (smaller set size) until we do all collisions (larger set size)
143 for (index_type k = coll_idx; k < max_N; k += min_N)
144 {
145 // do not check for collision if a particle's weight was
146 // reduced to zero from a previous collision
147 if (idcpu1[ I1[i1] ]==amrex::ParticleIdCpus::Invalid ||
148 idcpu2[ I2[i2] ]==amrex::ParticleIdCpus::Invalid ) {
149 p_mask[pair_index] = false;
150 } else {
151
152#if (defined WARPX_DIM_RZ)
153 /* In RZ geometry, macroparticles can collide with other macroparticles
154 * in the same *cylindrical* cell. For this reason, collisions between macroparticles
155 * are actually not local in space. In this case, the underlying assumption is that
156 * particles within the same cylindrical cell represent a cylindrically-symmetry
157 * momentum distribution function. Therefore, here, we temporarily rotate the
158 * momentum of one of the macroparticles in agreement with this cylindrical symmetry.
159 * (This is technically only valid if we use only the m=0 azimuthal mode in the simulation;
160 * there is a corresponding assert statement at initialization.)
161 */
162 amrex::ParticleReal const theta = theta2[I2[i2]]-theta1[I1[i1]];
163 amrex::ParticleReal const u1xbuf = u1x[I1[i1]];
164 u1x[I1[i1]] = u1xbuf*std::cos(theta) - u1y[I1[i1]]*std::sin(theta);
165 u1y[I1[i1]] = u1xbuf*std::sin(theta) + u1y[I1[i1]]*std::cos(theta);
166#endif
167
168 const int max_process_count = 4; // Pre-defined value, for performance reasons
170 (m_process_count < max_process_count), "Too many scattering processes in DSMC routine (hardcoded to only allow 4). Update the max_process_count value in source code to allow more scattering processes."
171 );
173 u1x[ I1[i1] ], u1y[ I1[i1] ], u1z[ I1[i1] ],
174 u2x[ I2[i2] ], u2y[ I2[i2] ], u2z[ I2[i2] ],
175 m1, m2, w1[ I1[i1] ], w2[ I2[i2] ],
176 dt, dV, static_cast<int>(pair_index), p_mask,
177 p_pair_reaction_weight, multiplier_ratio,
179
180#if (defined WARPX_DIM_RZ)
181 /* Undo the earlier velocity rotation. */
182 amrex::ParticleReal const u1xbuf_new = u1x[I1[i1]];
183 u1x[I1[i1]] = u1xbuf_new*std::cos(-theta) - u1y[I1[i1]]*std::sin(-theta);
184 u1y[I1[i1]] = u1xbuf_new*std::sin(-theta) + u1y[I1[i1]]*std::cos(-theta);
185#endif
186
187 // Remove pair reaction weight from the colliding particles' weights
188 if (p_mask[pair_index]) {
190 w1[ I1[i1] ], idcpu1[ I1[i1] ], p_pair_reaction_weight[pair_index]);
192 w2[ I2[i2] ], idcpu2[ I2[i2] ], p_pair_reaction_weight[pair_index]);
193 }
194 }
195
196 p_pair_indices_1[pair_index] = I1[i1];
197 p_pair_indices_2[pair_index] = I2[i2];
198 if (max_N == NI1) { i1 += min_N; }
199 if (max_N == NI2) { i2 += min_N; }
200 pair_index += min_N;
201 }
202 }
203
208 bool m_isSameSpecies = false;
210 };
211
212 [[nodiscard]] Executor const& executor () const { return m_exe; }
213
214 bool use_global_debye_length() { return false; }
215
216private:
219
221
223};
224
225#endif // WARPX_DSMC_FUNC_H_
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
#define AMREX_RESTRICT
#define AMREX_INLINE
#define AMREX_GPU_HOST_DEVICE
AMREX_GPU_HOST_DEVICE AMREX_INLINE void CollisionPairFilter(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, const amrex::ParticleReal w1, const 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 int multiplier, const int process_count, const ScatteringProcess::Executor *scattering_processes, const amrex::RandomEngine &engine)
This function determines whether a collision occurs for a given pair of particles.
Definition CollisionFilterFunc.H:39
bool use_global_debye_length()
Definition DSMCFunc.H:214
Executor const & executor() const
Definition DSMCFunc.H:212
amrex::Vector< ScatteringProcess > m_scattering_processes
Definition DSMCFunc.H:217
ParticleBins::index_type index_type
Definition DSMCFunc.H:44
Executor m_exe
Definition DSMCFunc.H:222
DSMCFunc()=default
Constructor of the DSMCFunc class.
WarpXParticleContainer::ParticleTileType ParticleTileType
Definition DSMCFunc.H:41
amrex::Gpu::DeviceVector< ScatteringProcess::Executor > m_scattering_processes_exe
Definition DSMCFunc.H:218
WarpXParticleContainer::ParticleType ParticleType
Definition DSMCFunc.H:40
amrex::DenseBins< ParticleTileDataType > ParticleBins
Definition DSMCFunc.H:43
bool m_isSameSpecies
Definition DSMCFunc.H:220
ParticleTileType::ParticleTileDataType ParticleTileDataType
Definition DSMCFunc.H:42
WarpXParticleContainer::ParticleTileType::ParticleTileDataType SoaData_type
Definition DSMCFunc.H:45
Definition MultiParticleContainer.H:68
AMREX_GPU_HOST_DEVICE AMREX_INLINE void remove_weight_from_colliding_particle(amrex::ParticleReal &weight, uint64_t &idcpu, const amrex::ParticleReal reaction_weight)
Subtract given weight from particle and set its ID to invalid if the weight reaches zero.
Definition BinaryCollisionUtils.H:148
PODVector< T, ArenaAllocator< T > > DeviceVector
constexpr std::uint64_t Invalid
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition DSMCFunc.H:65
bool m_need_product_data
Definition DSMCFunc.H:207
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, const SoaData_type &soa_1, const SoaData_type &soa_2, GetParticlePosition< PIdx >, GetParticlePosition< PIdx >, amrex::ParticleReal const, amrex::ParticleReal const, amrex::ParticleReal const, amrex::ParticleReal const, amrex::Real const, amrex::ParticleReal const, amrex::ParticleReal const, amrex::ParticleReal const m1, amrex::ParticleReal const m2, amrex::Real const dt, amrex::Real const dV, index_type coll_idx, 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::ParticleReal *, amrex::RandomEngine const &engine) const
Executor of the DSMCFunc class. Performs DSMC collisions at the cell level. Note that this function d...
Definition DSMCFunc.H:92
bool m_isSameSpecies
Definition DSMCFunc.H:208
bool m_computeSpeciesDensities
Definition DSMCFunc.H:205
bool m_computeSpeciesTemperatures
Definition DSMCFunc.H:206
int m_process_count
Definition DSMCFunc.H:204
ScatteringProcess::Executor * m_scattering_processes_data
Definition DSMCFunc.H:209
Functor that can be used to extract the positions of the macroparticles inside a ParallelFor kernel.
Definition GetAndSetPosition.H:75
@ theta
RZ needs all three position components.
Definition WarpXParticleContainer.H:72
@ uz
Definition WarpXParticleContainer.H:70
@ w
weight
Definition WarpXParticleContainer.H:69
@ uy
Definition WarpXParticleContainer.H:70
@ ux
Definition WarpXParticleContainer.H:70
Definition ScatteringProcess.H:75
ParticleTileData< StorageParticleType, NArrayReal, NArrayInt > ParticleTileDataType