-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description Support particle destruction. Changes: - Define `ParticleDestructionTraits`. The user define their own particle destructor by specializing the `ParticleChecker` method in `ParticleDestructionTraits`, which return true or false to mark a particle for destruction. As a placeholder, `particles.param3` is used in particle destructor. - Define `ParticleDestructionImpl`, which loops over all particles at a level and mark particles for destruction. - Add a `getNumParticles` function to `PhysicsParticleDescriptor` to get the total number of *valid* particles of that type. - Add a new particle parameter `particles.verbose` to turn on printing particle logistics at each time step. Currently, it prints the number of particles of each type. The new particle destructor feature is tested in gravity_3d.cpp. Next, I'll add a `particleDestruction` method in `ParticleDestructionTraits`, which modifies the hydro state while removing a particle. ### Related issues None. ### Checklist _Before this pull request can be reviewed, all of these tasks should be completed. Denote completed tasks with an `x` inside the square brackets `[ ]` in the Markdown source below:_ - [x] I have added a description (see above). - [x] I have added a link to any related issues (if applicable; see above). - [x] I have read the [Contributing Guide](https://github.com/quokka-astro/quokka/blob/development/CONTRIBUTING.md). - [x] I have added tests for any new physics that this PR adds to the code. - [ ] *(For quokka-astro org members)* I have manually triggered the GPU tests with the magic comment `/azp run`. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
824c6c7
commit 306cf35
Showing
7 changed files
with
219 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#ifndef PARTICLE_DESTRUCTION_HPP_ | ||
#define PARTICLE_DESTRUCTION_HPP_ | ||
|
||
#include "particle_types.hpp" | ||
|
||
namespace quokka | ||
{ | ||
|
||
// Helper namespace with implementation details for particle destruction | ||
namespace ParticleDestructionImpl | ||
{ | ||
// Common implementation of particle destruction logic | ||
template <typename problem_t, typename ContainerType, template <typename> class CheckerType> | ||
static void destroyParticlesImpl(ContainerType *container, int mass_idx, int lev, amrex::Real current_time, amrex::Real dt) | ||
{ | ||
if (container != nullptr) { | ||
if (mass_idx >= 0) { | ||
// Use the provided ParticleChecker type to determine which particles to destroy | ||
CheckerType<problem_t> particle_checker; | ||
|
||
// Iterate through all particles at this level | ||
for (typename ContainerType::ParIterType pti(*container, lev); pti.isValid(); ++pti) { | ||
auto &particles = pti.GetArrayOfStructs(); | ||
const int np = particles.numParticles(); | ||
|
||
// Skip if no particles in this tile | ||
if (np == 0) { | ||
continue; | ||
} | ||
|
||
// Process particles on the device | ||
auto *parray = particles().data(); | ||
|
||
amrex::ParallelFor(np, [=] AMREX_GPU_DEVICE(int i) { | ||
auto &p = parray[i]; // NOLINT | ||
|
||
// Check if this particle should be destroyed | ||
if (particle_checker(p, mass_idx, current_time, dt)) { | ||
// Mark particle as invalid by negating its ID | ||
// This is the AMReX way to mark particles for removal | ||
// They will be removed during the next Redistribute call | ||
p.id() = -1; | ||
} | ||
}); | ||
} | ||
|
||
// Redistribute particles at this level to actually remove the invalid particles | ||
// TODO(cch): This won't work when AMR subcycling is enabled. When a particle moves into the ghost cells in the first step of the | ||
// subcycle, it may be moved from that level into a lower level. Then, in the second step of the subcycle, it will not be drifted. | ||
container->Redistribute(lev); | ||
} | ||
} | ||
} | ||
} // namespace ParticleDestructionImpl | ||
|
||
// Traits class for specializing particle destruction behavior | ||
template <ParticleType particleType> struct ParticleDestructionTraits { | ||
// Default nested ParticleChecker - determines if a particle should be destroyed | ||
template <typename problem_t> struct ParticleChecker { | ||
|
||
template <typename ParticleType> | ||
AMREX_GPU_DEVICE auto operator()(ParticleType &p, int mass_idx, amrex::Real current_time, amrex::Real dt) const -> bool | ||
{ | ||
// Default implementation: destroy particles with mass < 1.0 | ||
amrex::ignore_unused(p, mass_idx, current_time, dt); | ||
return false; | ||
} | ||
}; | ||
|
||
// Main method to destroy particles - uses the helper implementation | ||
template <typename problem_t, typename ContainerType> | ||
static void destroyParticles(ContainerType *container, int mass_idx, int lev, amrex::Real current_time, amrex::Real dt) | ||
{ | ||
// Use the common implementation with our checker type | ||
ParticleDestructionImpl::destroyParticlesImpl<problem_t, ContainerType, ParticleDestructionTraits<particleType>::template ParticleChecker>( | ||
container, mass_idx, lev, current_time, dt); | ||
} | ||
}; | ||
|
||
} // namespace quokka | ||
|
||
#endif // PARTICLE_DESTRUCTION_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.