From 2439f4fca9831453e3c23c948bc11ac8e3a571b5 Mon Sep 17 00:00:00 2001 From: csendlinger Date: Wed, 4 Jun 2025 09:39:50 +0200 Subject: [PATCH] debug statements in preprocessor clause --- src/IO/simulation_output.cpp | 24 ++++++++++++++ src/interactions/charge_exchange.cpp | 2 ++ src/interactions/cluster_formation.cpp | 2 ++ src/interactions/elastic_scattering.cpp | 2 ++ src/interactions/excitation.cpp | 2 ++ src/interactions/free_movement.cpp | 2 ++ src/interactions/ionization.cpp | 2 ++ src/interactions/recombination.cpp | 2 ++ src/simulation/cpu_synchronizer.cpp | 2 +- src/simulation/injector.cpp | 2 ++ src/simulation/simulation.cpp | 44 +++++++++++++++++++++++-- 11 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/IO/simulation_output.cpp b/src/IO/simulation_output.cpp index dc62a04..9461933 100755 --- a/src/IO/simulation_output.cpp +++ b/src/IO/simulation_output.cpp @@ -51,7 +51,9 @@ void SimulationOutput::writeSimulationData( int cpu_sync, std::map>& species) { if (MpiCommunication::mpi_rank == 0) { +#ifdef debug_level2 DEBUG_LEVEL_2("Write density field data."); +#endif for (auto& task : tasks_density_fields_) { if (cpu_sync % task.intervall == 0) { std::string specie_path = "/" + task.specie_name; @@ -59,7 +61,9 @@ void SimulationOutput::writeSimulationData( specie_path, cpu_sync, task.overwrite_last); } } +#ifdef debug_level2 DEBUG_LEVEL_2("Write current collector data."); +#endif for (auto& task : tasks_current_collector_) { if (cpu_sync % task.intervall == 0) { std::string specie_path = "/" + task.specie_name; @@ -67,7 +71,9 @@ void SimulationOutput::writeSimulationData( specie_path, cpu_sync, task.overwrite_last); } } +#ifdef debug_level2 DEBUG_LEVEL_2("Write deletion logger data."); +#endif for (auto& task : tasks_deletion_logger_) { if (cpu_sync % task.intervall == 0) { std::string specie_path = "/" + task.specie_name; @@ -88,15 +94,25 @@ void SimulationOutput::writeSimulationData(DynamicDensityField* density_field, } std::string path = species_path + "/DensityField/SimulationData/" + sync_path; +#ifdef debug_level3 DEBUG_LEVEL_3("Write spectrum."); +#endif writeSpectrum(density_field, path); +#ifdef debug_level3 DEBUG_LEVEL_3("Write density."); +#endif writeDensity(density_field, path); +#ifdef debug_level3 DEBUG_LEVEL_3("Write time in bin."); +#endif writeTimeInBin(density_field, path); +#ifdef debug_level3 DEBUG_LEVEL_3("Write deleted particles."); +#endif writeDeletedParticles(density_field, path); +#ifdef debug_level3 DEBUG_LEVEL_3("Write physical time."); +#endif writePhysicalTime(density_field, path); } @@ -110,11 +126,17 @@ void SimulationOutput::writeSimulationData(CurrentCollector* current_collector, } std::string path = species_path + "/CurrentCollector/SimulationData/" + sync_path; +#ifdef debug_level3 DEBUG_LEVEL_3("Write current crossings."); +#endif writeCurrentCrossings(current_collector, path); +#ifdef debug_level3 DEBUG_LEVEL_3("Write current."); +#endif writeCurrent(current_collector, path); +#ifdef debug_level3 DEBUG_LEVEL_3("Write physical time."); +#endif writePhysicalTime(current_collector, path); } @@ -127,7 +149,9 @@ void SimulationOutput::writeSimulationData(DeletionLogger* logger, sync_path = "LastSyncPeriod"; } std::string path = species_path + "/DeletionLogger/" + sync_path; +#ifdef debug_level3 DEBUG_LEVEL_3("Write number of deletions."); +#endif writeNumDeletions(logger, path); } diff --git a/src/interactions/charge_exchange.cpp b/src/interactions/charge_exchange.cpp index 6e6a40a..d9d42d3 100755 --- a/src/interactions/charge_exchange.cpp +++ b/src/interactions/charge_exchange.cpp @@ -13,7 +13,9 @@ ChargeExchange::ChargeExchange(std::function cross_section, InteractionOutput ChargeExchange::performInteraction( particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform charge exchange."); +#endif const auto x_impact = particle.position(); // draw velocity of the target specie diff --git a/src/interactions/cluster_formation.cpp b/src/interactions/cluster_formation.cpp index c1b60a8..d5f682c 100755 --- a/src/interactions/cluster_formation.cpp +++ b/src/interactions/cluster_formation.cpp @@ -18,7 +18,9 @@ ClusterFormation::ClusterFormation( InteractionOutput ClusterFormation::performInteraction( particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform cluster formation."); +#endif const auto x_impact = particle.position(); const blitz::TinyVector v_target = target_->driftVelocity()->driftVelocity(x_impact).cartesianEntries() + diff --git a/src/interactions/elastic_scattering.cpp b/src/interactions/elastic_scattering.cpp index fa4b6ce..9899b82 100755 --- a/src/interactions/elastic_scattering.cpp +++ b/src/interactions/elastic_scattering.cpp @@ -18,7 +18,9 @@ ElasticScattering::ElasticScattering( InteractionOutput ElasticScattering::performInteraction( particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform elastic scattering."); +#endif // get the information of the incident particle const blitz::TinyVector v_impact = particle.velocity().cartesianEntries(); diff --git a/src/interactions/excitation.cpp b/src/interactions/excitation.cpp index b63dda0..526c227 100755 --- a/src/interactions/excitation.cpp +++ b/src/interactions/excitation.cpp @@ -18,7 +18,9 @@ Excitation::Excitation(std::function cross_section, InteractionOutput Excitation::performInteraction(particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform excitation."); +#endif const blitz::TinyVector v_impact = particle.velocity().cartesianEntries(); const blitz::TinyVector n_impact = diff --git a/src/interactions/free_movement.cpp b/src/interactions/free_movement.cpp index 4176b3b..37b476e 100755 --- a/src/interactions/free_movement.cpp +++ b/src/interactions/free_movement.cpp @@ -10,7 +10,9 @@ FreeMovement::FreeMovement() InteractionOutput FreeMovement::performInteraction(particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform free movement."); +#endif // do nothing return InteractionOutput(); } diff --git a/src/interactions/ionization.cpp b/src/interactions/ionization.cpp index ad779a3..0d40494 100755 --- a/src/interactions/ionization.cpp +++ b/src/interactions/ionization.cpp @@ -29,7 +29,9 @@ ElectronIonization::ElectronIonization( InteractionOutput ElectronIonization::performInteraction( particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform ionization."); +#endif const blitz::TinyVector v_impact = particle.velocity().cartesianEntries(); diff --git a/src/interactions/recombination.cpp b/src/interactions/recombination.cpp index 4553e16..fce8446 100755 --- a/src/interactions/recombination.cpp +++ b/src/interactions/recombination.cpp @@ -16,7 +16,9 @@ Recombination::Recombination(std::function cross_section, InteractionOutput Recombination::performInteraction( particle::Particle& particle, RandomNumbers* generator) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform recombination."); +#endif std::vector macroscopic_bin_cross_sections_m; const int number_energy_bins = recombination_target_->dynamicDensityField() diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index a00bdc5..3441d4d 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -133,7 +133,7 @@ void CpuSynchronizer::synchronize( tracked_species) { MPI_Barrier(MPI_COMM_WORLD); auto it = tracked_species.begin(); - DEBUG_LEVEL_3("Timer Synchronization."); + // DEBUG_LEVEL_3("Timer Synchronization."); synchronize(it->second->dynamicDensityField()->timer_.get(), it->second->lastSyncDensityField()->timer_.get()); diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index 8b6b087..b1f5070 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -45,8 +45,10 @@ std::vector Injector::rollNewParticles( // particles are assumed to be created by a decay process // therefore the resulting particles all start out at the same position CylinderCoordinate position = position_distribution_->rollPosition(engine); +#ifdef debug_level3 DEBUG_LEVEL_3("Injected z position: " + std::to_string(position.cylinderEntries()(2))); +#endif std::vector new_particles; // draw the velocity of all particles to be injected an append them to the new_particles vector for (auto& injector_info : injector_infos_) { diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 042dac7..cc1d833 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -38,29 +38,43 @@ void Simulation::init(Parameter& parameter) { magnetic_field_ = generator::magneticField(parameter); electric_field_ = generator::electricField(parameter); +#ifdef debug DEBUG_LEVEL_0("Creating species."); +#endif generator::SpeciesBuilder builder(parameter); tracked_species_ = builder.trackedSpecies(); background_species_ = builder.backgroundSpecies(); timer_ = builder.timer(); +#ifdef debug DEBUG_LEVEL_0("Creating injector."); +#endif injector_ = generator::buildInjector(tracked_species_, background_species_, parameter); +#ifdef debug DEBUG_LEVEL_0("Init random numbers."); +#endif random_ = generator::buildRandomNumbers(parameter); +#ifdef debug DEBUG_LEVEL_0("Init output."); +#endif output_ = generator::buildSimulationOutput( parameter.jsonObjectFromBase({"Output"}), tracked_species_); +#ifdef debug DEBUG_LEVEL_0("Output meta data."); +#endif output_->writeMetaData(tracked_species_); +#ifdef debug DEBUG_LEVEL_0("Init cpu synchronizer."); +#endif cpu_synchronizer_ = std::make_shared(); +#ifdef debug_level3 DEBUG_LEVEL_3("Init done."); +#endif } int eventsThisCpu(int events_all_cpus) { @@ -105,7 +119,7 @@ void Simulation::start() { if((events_this_cpu % event_stride == 0) && (total_simulated_events % MpiCommunication::mpiSize() <= MpiCommunication::mpiRank())) { - DEBUG_LEVEL_3("empty loop"); + // DEBUG_LEVEL_3("empty loop"); empty_loop = true; } } @@ -114,7 +128,7 @@ void Simulation::start() { // special case of unbalanced cpu load, with a part of the cpus having one cycle // that has no events, while the rest of the cpus have a single event (questionable if needed) if(leftover_events == 0 && empty_loop) { - DEBUG_LEVEL_3("empty loop looped"); + // DEBUG_LEVEL_3("empty loop looped"); empty_loop = false; } @@ -123,7 +137,9 @@ void Simulation::start() { std::to_string(leftover_events)); processEvents(evenst_of_cycle); +#ifdef debug_level1 DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events)); +#endif leftover_events -= evenst_of_cycle; SIM_INFO("Cpu sync."); @@ -148,27 +164,37 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) { } void Simulation::processEvents(int events) { +#ifdef debug DEBUG_LEVEL_0("Processing events."); +#endif std::vector particle_queue; particle_queue.reserve(100); int current_event = 0; while ((current_event < events) || (!particle_queue.empty())) { +#ifdef debug_level2 DEBUG_LEVEL_2("Current event: " + std::to_string(current_event)); +#endif // if the particle queue is empty a new beta decay event is started if (particle_queue.empty()) { current_event += 1; +#ifdef debug_level2 DEBUG_LEVEL_2("Inject particles to queue."); +#endif SimulationTimer::eventStartPoint(std::string("ParticleInjection")); // draw new beta decay particles std::vector new_particles = determineInjectedParticles(random_.get()); SimulationTimer::eventStopPoint(std::string("ParticleInjection")); +#ifdef debug_level2 DEBUG_LEVEL_2("Injected particles to queue."); +#endif particle_queue.insert(particle_queue.end(), new_particles.begin(), new_particles.end()); } +#ifdef debug_level2 DEBUG_LEVEL_2("Particles in queue: " + std::to_string(particle_queue.size())); +#endif // choose a random particle from the particle queue to be processed const int particle_number = particleNumberFromQueue(particle_queue.size(), random_.get()); @@ -185,7 +211,9 @@ void Simulation::processEvents(int events) { void Simulation::processUntilTermination( particle::Particle& particle, std::vector& particle_queue) { +#ifdef debug_level3 DEBUG_LEVEL_3("Process particle until termination"); +#endif while (particle.specie()->terminator()->isAlive(&particle)) { auto old_position = particle.position(); double old_energy = particle.kineticEnergy_eV(); @@ -205,7 +233,9 @@ void Simulation::processUntilTermination( bool was_reflected = false; if (particle.specie()->reflector()->needsReflectionBackToDomain( &particle)) { +#ifdef debug_level3 DEBUG_LEVEL_3("Reflect particle."); +#endif old_position = particle.position(); particle.specie()->reflector()->reflectBackToDomain(&particle); particle.specie()->currentCollector()->collect(&particle, old_position); @@ -214,7 +244,9 @@ void Simulation::processUntilTermination( if (particle.specie()->terminator()->reachedMaxTimeAlive( &particle)) { +#ifdef debug_level3 DEBUG_LEVEL_3("Terminate particle. Reached max time alive."); +#endif if (particle.specie()->deletionLogger()) { particle.specie()->deletionLogger()->logDeletion( particle.position(), DeletionType::max_time_alive); @@ -222,11 +254,13 @@ void Simulation::processUntilTermination( break; } else if (particle.specie()->terminator()->leftSimulationDomain( &particle)) { +#ifdef debug_level3 DEBUG_LEVEL_3("Terminate particle. Left simulation domain."); std::stringstream output_ss; output_ss << "Position (cyl): " << particle.position().cylinderEntries(); output_ss << " (cart): " << particle.position().cartesianEntries(); DEBUG_LEVEL_3(output_ss.str()); +#endif if (particle.specie()->deletionLogger()) { particle.specie()->deletionLogger()->logDeletion(particle.position(), DeletionType::wall); @@ -239,6 +273,7 @@ void Simulation::processUntilTermination( !(particle.specie()->dynamicDensityField()->changedEnergyBin( old_energy, particle.kineticEnergy_eV())) && !was_reflected) { +#ifdef debug_level3 DEBUG_LEVEL_3("Perform interaction."); DEBUG_LEVEL_3("Number of Interactions: " + std::to_string(particle.interactionCounter())); @@ -247,6 +282,7 @@ void Simulation::processUntilTermination( std::stringstream output_ss; output_ss << "Position (cyl): " << particle.position().cylinderEntries(); DEBUG_LEVEL_3(output_ss.str()); +#endif SimulationTimer::eventStartPoint(std::string("Interaction")); interaction::InteractionOutput output = interact(particle, random_.get(), @@ -260,13 +296,17 @@ void Simulation::processUntilTermination( } if (output.deleteCurrentParticle()) { +#ifdef debug_level3 DEBUG_LEVEL_3(particle.specie()->name() + " removed due to interaction."); +#endif break; } if (particle.specie()->terminator()->reachedMaxInteractions(&particle)) { +#ifdef debug_level3 DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() + ". Reached max interactions."); +#endif if (particle.specie()->deletionLogger()) { particle.specie()->deletionLogger()->logDeletion( particle.position(), DeletionType::max_interactions);