Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix to particle defragmentation #1185

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Current develop

### Added (new features/APIs/variables/...)
- [[PR 1185]](https://github.com/parthenon-hpc-lab/parthenon/pull/1185/files) Bugfix to particle defragmentation
- [[PR 1183]](https://github.com/parthenon-hpc-lab/parthenon/pull/1183) Fix particle leapfrog example initialization data
- [[PR 1179]](https://github.com/parthenon-hpc-lab/parthenon/pull/1179) Make a global variable for whether simulation is a restart
- [[PR 1171]](https://github.com/parthenon-hpc-lab/parthenon/pull/1171) Add PARTHENON_USE_SYSTEM_PACKAGES build option
Expand Down
1 change: 1 addition & 0 deletions example/particles/parthinput.particles
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ rng_seed = 23487
const_dt = 0.5
deposition_method = per_cell
destroy_particles_frac = 0.1
defrag_threshold = 0.9
11 changes: 10 additions & 1 deletion example/particles/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
destroy_particles_frac >= 0. && destroy_particles_frac <= 1.,
"Fraction of particles to destroy each timestep must be between 0 and 1");

Real defrag_threshold = pin->GetOrAddReal("Particles", "defrag_threshold", 0.9);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful to be able to control this at runtime.

pkg->AddParam<>("defrag_threshold", defrag_threshold);
PARTHENON_REQUIRE(defrag_threshold >= 0. && defrag_threshold <= 1.,
"Defragmentation threshold must be between 0 and 1 inclusive.");

std::string deposition_method =
pin->GetOrAddString("Particles", "deposition_method", "per_particle");
if (deposition_method == "per_particle") {
Expand Down Expand Up @@ -609,6 +614,9 @@ TaskCollection ParticleDriver::MakeFinalizationTaskCollection() const {
auto &sc1 = pmb->meshblock_data.Get();
auto &tl = async_region1[i];

auto pkg = pmb->packages.Get("particles_package");
const auto defrag_threshold = pkg->Param<Real>("defrag_threshold");

auto destroy_some_particles = tl.AddTask(none, DestroySomeParticles, pmb.get());

auto sort_particles = tl.AddTask(destroy_some_particles,
Expand All @@ -617,7 +625,8 @@ TaskCollection ParticleDriver::MakeFinalizationTaskCollection() const {
auto deposit_particles = tl.AddTask(sort_particles, DepositParticles, pmb.get());

// Defragment if swarm memory pool occupancy is 90%
auto defrag = tl.AddTask(deposit_particles, &SwarmContainer::Defrag, sc.get(), 0.9);
auto defrag = tl.AddTask(deposit_particles, &SwarmContainer::Defrag, sc.get(),
defrag_threshold);

// estimate next time step
auto new_dt = tl.AddTask(
Expand Down
11 changes: 7 additions & 4 deletions src/interface/swarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,18 @@ void Swarm::Defrag() {
auto &mask = mask_;

const int &num_active = num_active_;
auto empty_indices = empty_indices_;
parthenon::par_scan(
"Set empty indices prefix sum", 0, nmax_pool_ - num_active_ - 1,
KOKKOS_LAMBDA(const int nn, int &update, const bool &final) {
const int n = nn + num_active;
"Set empty indices prefix sum", num_active, nmax_pool_ - 1,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a change to behavior, this is just clearer IMO

KOKKOS_LAMBDA(const int n, int &update, const bool &final) {
const int val = mask(n);
if (val) {
update += 1;
}
if (final) scan_scratch_toread(n) = update;
if (final) {
scan_scratch_toread(n) = update;
empty_indices(n - num_active) = n;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bugfix

}
});

parthenon::par_for(
Expand Down
Loading