Skip to content

Commit 0b4606f

Browse files
committed
outp/hdf5: add template argument for particle selector
This particle selector decides whether to include a given particle in the output. Other than the trivial ParticleSelectorAll, ParticleSelectorEveryNth<N> is provided ot select every nth particle.
1 parent 3a23a9a commit 0b4606f

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

src/libpsc/psc_output_particles/output_particles_hdf5_impl.hxx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,38 @@ private:
293293
Hdf5ParticleType prt_type_;
294294
};
295295

296+
template <int EVERY>
297+
class ParticleSelectorEveryNth
298+
{
299+
public:
300+
template <typename Particle>
301+
bool operator()(const Particle& prt)
302+
{
303+
// return true for every `every_`th particle
304+
return (cnt_++ % EVERY) == 0;
305+
}
306+
307+
private:
308+
int cnt_ = 0;
309+
};
310+
311+
class ParticleSelectorAll
312+
{
313+
public:
314+
template <typename Particle>
315+
bool operator()(const Particle& prt)
316+
{
317+
return true;
318+
}
319+
};
320+
296321
namespace detail
297322
{
298323

299324
// ======================================================================
300325
// OutputParticlesHdf5
301326

302-
template <typename Mparticles>
327+
template <typename Mparticles, typename ParticleSelector>
303328
struct OutputParticlesHdf5
304329
{
305330
using writer_type = OutputParticlesWriterHDF5;
@@ -353,17 +378,24 @@ struct OutputParticlesHdf5
353378
{
354379
int nr_kinds = mprts.grid().kinds.size();
355380

381+
ParticleSelector selector;
382+
356383
for (int p = 0; p < mprts.n_patches(); p++) {
357384
const int* ldims = mprts.grid().ldims;
358385
int nr_indices = ldims[0] * ldims[1] * ldims[2] * nr_kinds;
359386
off[p].resize(nr_indices + 1);
360387
auto&& prts = mprts[p];
361388
unsigned int n_prts = prts.size();
389+
std::vector<int> particle_indices;
390+
particle_indices.reserve(n_prts);
362391

363392
// counting sort to get map
364393
for (int n = 0; n < n_prts; n++) {
365-
int si = get_sort_index(prts, n);
366-
off[p][si]++;
394+
if (selector(prts[n])) {
395+
particle_indices.push_back(n);
396+
int si = get_sort_index(prts, n);
397+
off[p][si]++;
398+
}
367399
}
368400
// prefix sum to get offsets
369401
int o = 0;
@@ -377,7 +409,7 @@ struct OutputParticlesHdf5
377409

378410
// sort a map only, not the actual particles
379411
map[p].resize(n_prts);
380-
for (int n = 0; n < n_prts; n++) {
412+
for (auto n : particle_indices) {
381413
int si = get_sort_index(prts, n);
382414
map[p][off2[si]++] = n;
383415
}
@@ -609,6 +641,7 @@ private:
609641

610642
} // namespace detail
611643

644+
template <typename ParticleSelector = ParticleSelectorAll>
612645
class OutputParticlesHdf5 : OutputParticlesBase
613646
{
614647
static OutputParticlesParams adjust_params(
@@ -640,7 +673,8 @@ public:
640673
return;
641674
}
642675

643-
detail::OutputParticlesHdf5<Mparticles> impl{grid, params_};
676+
detail::OutputParticlesHdf5<Mparticles, ParticleSelector> impl{grid,
677+
params_};
644678
impl(mprts, writer_);
645679
}
646680

src/libpsc/tests/test_output_particles.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ struct OutputParticlesTest : ::testing::Test
6464
Int3 ibn = {2, 2, 2};
6565
};
6666

67-
using OutputParticlesTestTypes =
68-
::testing::Types<Config<dim_xyz, MparticlesSingle, OutputParticlesAscii>
67+
using OutputParticlesTestTypes = ::testing::Types<
68+
Config<dim_xyz, MparticlesSingle, OutputParticlesAscii>
6969
#ifdef H5_HAVE_PARALLEL
70-
,
71-
Config<dim_xyz, MparticlesSingle, OutputParticlesHdf5>,
72-
Config<dim_xyz, MparticlesDouble, OutputParticlesHdf5>
70+
,
71+
Config<dim_xyz, MparticlesSingle, OutputParticlesHdf5<ParticleSelectorAll>>,
72+
Config<dim_xyz, MparticlesDouble, OutputParticlesHdf5<ParticleSelectorAll>>
7373
#endif
74-
>;
74+
>;
7575

7676
TYPED_TEST_SUITE(OutputParticlesTest, OutputParticlesTestTypes);
7777

src/psc_config.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include "../libpsc/vpic/fields_item_vpic.hxx"
3333

34-
using OutputParticlesDefault = OutputParticlesHdf5;
34+
using OutputParticlesDefault = OutputParticlesHdf5<ParticleSelectorAll>;
3535

3636
struct SimulationNone
3737
{
@@ -252,7 +252,7 @@ struct PscConfigVpicPsc
252252
using BndParticles = BndParticlesVpic<Mparticles>;
253253
using Checks = ChecksVpic<Mparticles, MfieldsState>;
254254
using Marder = MarderVpic<Mparticles, MfieldsState>;
255-
using OutputParticles = OutputParticlesHdf5;
255+
using OutputParticles = OutputParticlesHdf5<ParticleSelectorAll>;
256256
using OutputHydro = OutputHydroQ<Mparticles, MfieldsHydro,
257257
typename VpicConfig::MfieldsInterpolator>;
258258
using Dim = dim_xz;

src/psc_flatfoil_yz.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ using Balance = PscConfig::Balance;
226226
using Collision = PscConfig::Collision;
227227
using Checks = PscConfig::Checks;
228228
using Marder = PscConfig::Marder;
229+
#if CASE == CASE_2D_SMALL
230+
using OutputParticles = OutputParticlesHdf5<ParticleSelectorEveryNth<10>>;
231+
#else
229232
using OutputParticles = PscConfig::OutputParticles;
233+
#endif
230234
using Moment_n = typename Moment_n_Selector<Mparticles, Dim>::type;
231235
using Heating = typename HeatingSelector<Mparticles>::Heating;
232236

0 commit comments

Comments
 (0)