Skip to content

Commit 2cdcb77

Browse files
DSMC: make the check of the number of processes more robust (#5515)
The previous code was brittle because it was not using `AMREX_ALWAYS_ASSERT_WITH_MESSAGE` (which indeed cannot be used inside a GPU kernel. In practice, a user could specify e.g. 10 scattering processes and not get an error. The new code checks the number of processes before calling the GPU kernel, by using `AMREX_ALWAYS_ASSERT_WITH_MESSAGE`. --------- Co-authored-by: Roelof Groenewald <[email protected]>
1 parent 2ea2dd8 commit 2cdcb77

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Source/Particles/Collision/BinaryCollision/DSMC/CollisionFilterFunc.H

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @param[in] scattering processes an array of scattering processes included for consideration.
3535
* @param[in] engine the random engine.
3636
*/
37-
template <typename index_type>
37+
template <int max_process_count, typename index_type>
3838
AMREX_GPU_HOST_DEVICE AMREX_INLINE
3939
void CollisionPairFilter (const amrex::ParticleReal u1x, const amrex::ParticleReal u1y,
4040
const amrex::ParticleReal u1z, const amrex::ParticleReal u2x,
@@ -65,11 +65,11 @@ void CollisionPairFilter (const amrex::ParticleReal u1x, const amrex::ParticleRe
6565

6666
// Evaluate the cross-section for each scattering process to determine
6767
// the total collision probability.
68-
AMREX_ASSERT_WITH_MESSAGE(
69-
(process_count < 4), "Too many scattering processes in DSMC routine."
70-
);
71-
int coll_type[4] = {0, 0, 0, 0};
72-
amrex::ParticleReal sigma_sums[4] = {0._prt, 0._prt, 0._prt, 0._prt};
68+
69+
// The size of the arrays below is a compile-time constant (template parameter)
70+
// for performance reasons: it avoids dynamic memory allocation on the GPU.
71+
int coll_type[max_process_count] = {0};
72+
amrex::ParticleReal sigma_sums[max_process_count] = {0._prt};
7373
for (int ii = 0; ii < process_count; ii++) {
7474
auto const& scattering_process = scattering_processes[ii];
7575
coll_type[ii] = int(scattering_process.m_type);

Source/Particles/Collision/BinaryCollision/DSMC/DSMCFunc.H

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ public:
163163
u1y[I1[i1]] = u1xbuf*std::sin(theta) + u1y[I1[i1]]*std::cos(theta);
164164
#endif
165165

166-
CollisionPairFilter(
166+
const int max_process_count = 4; // Pre-defined value, for performance reasons
167+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(
168+
(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."
169+
);
170+
CollisionPairFilter<max_process_count>(
167171
u1x[ I1[i1] ], u1y[ I1[i1] ], u1z[ I1[i1] ],
168172
u2x[ I2[i2] ], u2y[ I2[i2] ], u2z[ I2[i2] ],
169173
m1, m2, w1[ I1[i1] ], w2[ I2[i2] ],

0 commit comments

Comments
 (0)