Skip to content

Commit 1be74e0

Browse files
Add print of SN explosion counts (quokka-astro#1546)
### Description Print the number of stars that went SN explosion in each step (if >0) when `verbose = true`. Also change the format of some print logs in the particle module to make them more consistent and avoid excessive logs. ### Related issues Are there any GitHub issues that are fixed by this pull request? Add a link to them here. ### 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:_ - [ ] I have added a description (see above). - [ ] I have added a link to any related issues (if applicable; see above). - [ ] I have read the [Contributing Guide](https://github.com/quokka-astro/quokka/blob/development/CONTRIBUTING.md). - [ ] 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>
1 parent 81159e2 commit 1be74e0

4 files changed

Lines changed: 49 additions & 21 deletions

File tree

src/particles/PhysicsParticles.hpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ class PhysicsParticleDescriptorBase
145145
//----- Methods that are implemented for some but not all particle types, so they cannot be pure virtual -----
146146

147147
virtual auto depositSN(amrex::MultiFab & /*state*/, std::array<amrex::MultiFab, AMREX_SPACEDIM> const * /*state_fc*/, int /*lev*/, amrex::Real /*time*/,
148-
amrex::Real /*dt*/) -> amrex::Real
148+
amrex::Real /*dt*/) -> std::pair<int, amrex::Real>
149149
{
150-
return 0.0_rt;
150+
return {0, 0.0_rt};
151151
}
152152

153153
virtual void computeSinkAccretion(amrex::MultiFab &state, amrex::MultiFab &state_accretion_rate,
@@ -610,8 +610,9 @@ template <typename ContainerType, typename problem_t, ParticleType particleType>
610610

611611
// Implementation of supernova energy and momentum deposition from particles to grid
612612
auto depositSN(amrex::MultiFab &state, std::array<amrex::MultiFab, AMREX_SPACEDIM> const *state_fc, int lev, amrex::Real time, amrex::Real dt)
613-
-> amrex::Real override
613+
-> std::pair<int, amrex::Real> override
614614
{
615+
int num_sn_explosions = 0;
615616
amrex::Real max_velocity = 0.0;
616617

617618
if (this->container_ != nullptr && this->getEvolutionStageIndex() >= 0) {
@@ -621,16 +622,19 @@ template <typename ContainerType, typename problem_t, ParticleType particleType>
621622
"UnitSystem must be CGS for particleMeshInteraction");
622623

623624
// Deposit supernova energy and momentum from all particles. This also updates the evolution stage of the particles.
624-
max_velocity = SNDeposition<ContainerType, problem_t>(this->container_, state, state_fc, lev, time, dt, this->getMassIndex(),
625-
this->getEvolutionStageIndex(), this->getBirthTimeIndex());
625+
auto [sn_count, vel] =
626+
SNDeposition<ContainerType, problem_t>(this->container_, state, state_fc, lev, time, dt, this->getMassIndex(),
627+
this->getEvolutionStageIndex(), this->getBirthTimeIndex());
628+
num_sn_explosions = sn_count;
629+
max_velocity = vel;
626630
} else {
627631
// Only update evolution stage but not deposit energy/momentum
628632
SNFeedbackUtils::updateEvolutionStage(this->container_, lev, time + dt, this->getBirthTimeIndex(),
629633
this->getEvolutionStageIndex());
630634
}
631635
}
632636

633-
return max_velocity;
637+
return {num_sn_explosions, max_velocity};
634638
}
635639

636640
// compute accretion rate
@@ -807,16 +811,18 @@ template <typename problem_t> class PhysicsParticleRegister
807811

808812
// Deposit supernova energy and momentum from all particles
809813
auto depositSN(amrex::MultiFab &state, std::array<amrex::MultiFab, AMREX_SPACEDIM> const *state_fc, int lev, amrex::Real time, amrex::Real dt)
810-
-> amrex::Real
814+
-> std::pair<int, amrex::Real>
811815
{
812816
const BL_PROFILE("PhysicsParticleRegister::depositSN()");
817+
int total_sn_explosions = 0;
813818
amrex::Real max_velocity = 0.0;
814819
// Each particle type handles its own buffer creation and roundoff independently
815820
for (const auto &[type, descriptor] : particleRegistry_) {
816-
const amrex::Real max_velocity_ = descriptor->depositSN(state, state_fc, lev, time, dt);
817-
max_velocity = std::max(max_velocity, max_velocity_);
821+
auto [sn_count, velocity] = descriptor->depositSN(state, state_fc, lev, time, dt);
822+
total_sn_explosions += sn_count;
823+
max_velocity = std::max(max_velocity, velocity);
818824
}
819-
return max_velocity;
825+
return {total_sn_explosions, max_velocity};
820826
}
821827

822828
// Implementation of computeSinkAccretion
@@ -898,7 +904,7 @@ template <typename problem_t> class PhysicsParticleRegister
898904
}
899905
}
900906
if (!found) {
901-
amrex::Print() << "Warning: Requested particle type '" << requestedName << "' is not registered.\n";
907+
amrex::Print() << "[WARNING] Requested particle type '" << requestedName << "' is not registered.\n";
902908
}
903909
}
904910
}
@@ -1020,7 +1026,7 @@ template <typename problem_t> class PhysicsParticleRegister
10201026
void printParticleStatistics() const
10211027
{
10221028
const BL_PROFILE("PhysicsParticleRegister::printParticleStatistics()");
1023-
amrex::Print() << ">>> Particle statistics:\n";
1029+
amrex::Print() << "[PARTICLES] Statistics:\n";
10241030
amrex::Print() << fmt::format("{:<20}{:>15}\n", "Particle type", "Number of particles");
10251031

10261032
for (const auto &[type, descriptor] : particleRegistry_) {

src/particles/particle_deposition.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ AMREX_GPU_DEVICE AMREX_FORCE_INLINE void depositThermalKineticMomentumSNR(
272272

273273
template <typename ContainerType, typename problem_t>
274274
void depositToBuffer(ContainerType *container, amrex::MultiFab &state, amrex::MultiFab &state_buffer, int lev, amrex::Real time, amrex::Real dt, int mass_index,
275-
int evolutionStageIndex, int birthTimeIndex, const SNScheme SN_scheme_d)
275+
int evolutionStageIndex, int birthTimeIndex, const SNScheme SN_scheme_d, int *p_sn_count = nullptr)
276276
{
277277
const BL_PROFILE("SNFeedbackUtils::depositToBuffer()");
278278
constexpr amrex::Real stencil_volume = 4.0 / 3.0 * M_PI * SN_stencil_size * SN_stencil_size * SN_stencil_size;
@@ -330,6 +330,11 @@ void depositToBuffer(ContainerType *container, amrex::MultiFab &state, amrex::Mu
330330
const bool is_sn_progenitor = (p.idata(evolutionStageIndex) == static_cast<int>(StellarEvolutionStage::SNProgenitor));
331331

332332
if (is_sn_progenitor && step_end_time > p.rdata(birthTimeIndex + 1)) {
333+
// Count this SN explosion
334+
if (p_sn_count != nullptr) {
335+
amrex::Gpu::Atomic::AddNoRet(p_sn_count, 1);
336+
}
337+
333338
// Update the particle's evolution stage to SNRemnant
334339
p.idata(evolutionStageIndex) = static_cast<int>(StellarEvolutionStage::SNRemnant);
335340

@@ -621,7 +626,7 @@ void updateEvolutionStage(ContainerType *container, int lev_min, amrex::Real ste
621626

622627
template <typename ContainerType, typename problem_t>
623628
auto SNDeposition(ContainerType *container, amrex::MultiFab &state, std::array<amrex::MultiFab, AMREX_SPACEDIM> const *state_fc, int lev, amrex::Real time,
624-
amrex::Real dt, int mass_index, int evolutionStageIndex, int birthTimeIndex) -> Real
629+
amrex::Real dt, int mass_index, int evolutionStageIndex, int birthTimeIndex) -> std::pair<int, Real>
625630
{
626631
const BL_PROFILE("[particle_deposition] SNDeposition()");
627632
static_assert(SN_stencil_size <= 3,
@@ -638,9 +643,13 @@ auto SNDeposition(ContainerType *container, amrex::MultiFab &state, std::array<a
638643
amrex::Gpu::Buffer<amrex::Real> max_velocity_buffer({0.0});
639644
amrex::Real *p_max_velocity = max_velocity_buffer.data();
640645

646+
// Initialize SN explosion count tracking
647+
amrex::Gpu::Buffer<int> sn_count_buffer({0});
648+
int *p_sn_count = sn_count_buffer.data();
649+
641650
// Step 1: Local deposition within each box
642651
SNFeedbackUtils::depositToBuffer<ContainerType, problem_t>(container, state, state_buffer, lev, time, dt, mass_index, evolutionStageIndex,
643-
birthTimeIndex, SN_scheme_d);
652+
birthTimeIndex, SN_scheme_d, p_sn_count);
644653

645654
// Step 2: Sum boundary values
646655
state_buffer.SumBoundary(container->Geom(lev).periodicity());
@@ -651,12 +660,16 @@ auto SNDeposition(ContainerType *container, amrex::MultiFab &state, std::array<a
651660
// Step 3: Add the buffer to the state
652661
SNFeedbackUtils::addBufferToState<problem_t>(state, state_fc, state_buffer, SN_scheme_d, p_max_velocity);
653662

654-
// Step 4: Check maximum velocity and print warning if needed
663+
// Step 4: Check maximum velocity and SN count, then reduce across ranks
655664
auto *h_max_velocity = max_velocity_buffer.copyToHost();
656665
Real max_velocity = h_max_velocity[0];
657666
amrex::ParallelDescriptor::ReduceRealMax(max_velocity);
658667

659-
return max_velocity;
668+
auto *h_sn_count = sn_count_buffer.copyToHost();
669+
int sn_count = h_sn_count[0];
670+
amrex::ParallelDescriptor::ReduceIntSum(sn_count);
671+
672+
return {sn_count, max_velocity};
660673
}
661674

662675
#endif // AMREX_SPACEDIM == 3

src/particles/particle_destruction.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "AMReX_BLProfiler.H"
55
#include "particle_types.hpp"
6+
#include <fmt/format.h>
67

78
namespace quokka
89
{
@@ -79,8 +80,11 @@ static void destroyParticlesImpl(ContainerType *container, int mass_idx, int lev
7980

8081
// Print the total number of particles destroyed at this time step
8182
if (amrex::ParallelDescriptor::IOProcessor()) {
82-
amrex::Print() << ">>>Particle destruction:\n\tTime: " << current_time << " - Destroyed " << global_total_destroyed
83-
<< " particles (from level " << lev_min << " and above)\n\n";
83+
if (particle_verbose > 0) {
84+
amrex::Print()
85+
<< fmt::format("[PARTICLES] Particle destruction: Time: {} - Destroyed {} particles (from level {} and above)\n",
86+
current_time, global_total_destroyed, lev_min);
87+
}
8488
}
8589
}
8690
}

src/simulation.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,12 +1922,17 @@ template <typename problem_t> void AMRSimulation<problem_t>::particleMeshInterac
19221922
particleRegister_.createParticlesFromState(state_new_cc_[lev], accretion_rate_at_level, lev, time, dt, state_fc_ptr, verbose);
19231923

19241924
// Deposit the SN particles into the MultiFab
1925-
const amrex::Real max_velocity = particleRegister_.depositSN(state_new_cc_[lev], state_fc_ptr, lev, time, dt);
1925+
const auto [num_sn_explosions, max_velocity] = particleRegister_.depositSN(state_new_cc_[lev], state_fc_ptr, lev, time, dt);
1926+
1927+
// Print SN explosion count if verbose and non-zero
1928+
if (verbose && num_sn_explosions > 0) {
1929+
amrex::Print() << fmt::format("[PARTICLES] SN explosions: Time: {} - {} stars went supernova at level {}\n", time, num_sn_explosions, lev);
1930+
}
19261931

19271932
// Check if the maximum velocity is greater than the threshold
19281933
constexpr amrex::Real v_over_c_threshold = 0.03;
19291934
if (max_velocity > v_over_c_threshold * C::c_light) {
1930-
amrex::Print() << "WARNING: SN remnant net velocity (" << max_velocity / C::c_light << " c) greater than " << v_over_c_threshold
1935+
amrex::Print() << "[WARNING] SN remnant net velocity (" << max_velocity / C::c_light << " c) greater than " << v_over_c_threshold
19311936
<< " c threshold!" << "\n";
19321937
}
19331938
}

0 commit comments

Comments
 (0)