From 047cfa849154c303d4e54303a846275677287ca1 Mon Sep 17 00:00:00 2001 From: csendlinger Date: Wed, 12 Feb 2025 14:07:43 +0100 Subject: [PATCH] Decoupled the number of events simulated from the timestep; Bug with density drawing present (if CPU syncs don't line up with the timestep) --- include/simulation/injector.h | 5 +- include/simulation/simulation.h | 6 +- src/generators/injector_generator.cpp | 6 +- src/simulation/injector.cpp | 22 ++++--- src/simulation/simulation.cpp | 84 ++++++++++++++++----------- 5 files changed, 73 insertions(+), 50 deletions(-) diff --git a/include/simulation/injector.h b/include/simulation/injector.h index 2251da7..6159c3c 100755 --- a/include/simulation/injector.h +++ b/include/simulation/injector.h @@ -28,7 +28,7 @@ struct InjectorInfo { public: std::shared_ptr created_specie; std::shared_ptr creation_velocity_distribution; - int multiplicity; + // int multiplicity; }; class Injector { @@ -39,7 +39,8 @@ class Injector { std::shared_ptr mother_drift_velocity); std::vector rollNewParticles(std::mt19937* engine, - const double current_time); + const double current_time, + const double beta_particle_weight); // draw new particles from the DynamicDensityField of the provided specie // the new particles are then appened to the vector queue diff --git a/include/simulation/simulation.h b/include/simulation/simulation.h index 593cf0a..55667ae 100755 --- a/include/simulation/simulation.h +++ b/include/simulation/simulation.h @@ -36,11 +36,13 @@ class Simulation { void finalize(); // cpu_syncs only included for debug purposes, e.g. only drawing decay particles // at the 0th timestep - void processEvents(int events, int density_events, int cpu_syncs); + void processEvents(int events, int density_events, int cpu_syncs, + const double beta_particle_weight); void processUntilTermination(particle::Particle& particle); private: - std::vector determineInjectedParticles(RandomNumbers* random); + std::vector determineInjectedParticles(RandomNumbers* random, + const double beta_particle_weight); std::vector determineInjectedDensityParticles(RandomNumbers* random, const int numberParticlesToDraw, const int current, diff --git a/src/generators/injector_generator.cpp b/src/generators/injector_generator.cpp index d9ee868..c957368 100755 --- a/src/generators/injector_generator.cpp +++ b/src/generators/injector_generator.cpp @@ -34,9 +34,9 @@ std::shared_ptr buildInjector( json_path_append(path_to_injector, {"CreatedSpecies", std::string(key), "CreationVelocityDistribution"})); - info.multiplicity = json_int(json_object_by_path(value, {"Multiplicity"}), 1); - ASSERT_POSITIVE(&info.multiplicity, "The multiplicity has to be positive. " + - specie_name + " multiplicity: " + std::to_string(info.multiplicity)); + // info.multiplicity = json_int(json_object_by_path(value, {"Multiplicity"}), 1); + // ASSERT_POSITIVE(&info.multiplicity, "The multiplicity has to be positive. " + + // specie_name + " multiplicity: " + std::to_string(info.multiplicity)); injector_infos.push_back(info); } diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index 45f2fee..68f4267 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -40,9 +40,13 @@ particle::Particle Injector::rollNewParticle( velocity, mother_drift_velocity_->driftVelocity(position).cartesianEntries()); } + +#if defined debug_level3 std::stringstream out; out << created_specie->name() << " created at time: " << time_birth << "s."; DEBUG_LEVEL_3(out.str()); +#endif + return particle::Particle(created_specie, position, CartesianVector(velocity), time_birth, weight); } @@ -53,26 +57,28 @@ void Injector::initInjectionTimeDistribution(double delta_t) { } std::vector Injector::rollNewParticles( - std::mt19937* engine, const double current_time) { + std::mt19937* engine, const double current_time, + const double beta_particle_weight) { // pull drawing from the position distribution into the "multiplicity" loop? // (would improve resolution stuff in space as well as just in velocity direction // especially for slow particles, i.e. ions) - CylinderCoordinate position = position_distribution_->rollPosition(engine); - DEBUG_LEVEL_3("Injected z position: " + - std::to_string(position.cylinderEntries()(2))); + std::vector new_particles; const double time_birth = current_time - injection_time_distribution_(*engine); for (auto& injector_info : injector_infos_) { - const double weight = 1./injector_info.multiplicity; + + CylinderCoordinate position = position_distribution_->rollPosition(engine); + DEBUG_LEVEL_3("Injected z position: " + + std::to_string(position.cylinderEntries()(2))); - for (int i = 0; i < injector_info.multiplicity; i++) { + // for (int i = 0; i < injector_info.multiplicity; i++) { new_particles.push_back( rollNewParticle(engine, position, injector_info.created_specie, injector_info.creation_velocity_distribution, - time_birth, weight)); - } + time_birth, beta_particle_weight)); + // } } return new_particles; } diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index e1065b0..fdbc307 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -109,47 +109,55 @@ void Simulation::finalize() { void Simulation::start() { SIM_INFO("Starting Simulation."); +// stride wrt the number of beta decay events per CPU const int event_stride = parameter_.intValue(json_path({"Simulation", "EventStride"}), 1); // const int total_simulated_events = // parameter_.intValue(json_path({"Simulation", "SimulatedEvents"}), 1); + +// number of timesteps to be simulated const int number_timesteps = parameter_.intValue(json_path({"Simulation", "NumberTimesteps"}), 1); + +// number of physical beta events defining the timescale by +// time_resolution_events/Activity const int time_resolution_events = parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}), MpiCommunication::mpiSize()); + +// number of beta decay events per timestep and CPU (will get normalized s.t. +// the physical number of particles is correct) + const int beta_decay_events = + parameter_.intValue(json_path{"Simulation", "BetaDecayEventsPerCPU"}, 1); + +// weight of a particle created through beta decay wrt a physical particle + const double beta_particle_weight = time_resolution_events/ + static_cast(beta_decay_events*MpiCommunication::mpiSize()); + +// number of density events per timestep and CPU const int density_events = parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0); DEBUG_LEVEL_0("Density events per cpu: " + std::to_string(density_events)); - // if(total_simulated_events % MpiCommunication::mpiSize() != 0) { - // ERROR("total simulated events are not divisible by the number of mpi procs"); - // } - // if(time_resolution_events % MpiCommunication::mpiSize() != 0) { - // ERROR("time resolution events are not divisible by the number of mpi procs"); - // } - - // SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events)); SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps)); SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); - // const int events_this_cpu = eventsThisCpu(total_simulated_events); - const int time_resolution_this_cpu = eventsThisCpu(time_resolution_events); + // const int time_resolution_this_cpu = eventsThisCpu(time_resolution_events); - if(time_resolution_events % MpiCommunication::mpiSize() != 0) { - if(time_resolution_events % MpiCommunication::mpiSize() <= MpiCommunication::mpiRank() && - time_resolution_this_cpu % event_stride == 0) { - ERROR("Code will not terminate! The event stride: " + std::to_string(event_stride) + - ", the number of CPUs: " + std::to_string(MpiCommunication::mpiSize()) + - " and the number of events per timestep: " + std::to_string(time_resolution_events) + - " create at least one CPU for which the number of CPU syncs for one timestep " + - " differs from the rest of the CPUs."); - } - } + // if(time_resolution_events % MpiCommunication::mpiSize() != 0) { + // if(time_resolution_events % MpiCommunication::mpiSize() <= MpiCommunication::mpiRank() && + // time_resolution_this_cpu % event_stride == 0) { + // ERROR("Code will not terminate! The event stride: " + std::to_string(event_stride) + + // ", the number of CPUs: " + std::to_string(MpiCommunication::mpiSize()) + + // " and the number of events per timestep: " + std::to_string(time_resolution_events) + + // " create at least one CPU for which the number of CPU syncs for one timestep " + + // " differs from the rest of the CPUs."); + // } + // } - SIM_INFO("Events per cpu (approx.) per timestep: " + - std::to_string(time_resolution_this_cpu)); + // SIM_INFO("Events per cpu (approx.) per timestep: " + + // std::to_string(time_resolution_this_cpu)); // int leftover_events = events_this_cpu; - int leftover_events_time_resolution = time_resolution_this_cpu; + int leftover_events_time_resolution = beta_decay_events; const double delta_t = time_resolution_events*injector_->timePassed(); @@ -189,17 +197,21 @@ void Simulation::start() { out << "Timeinterval: " << timer_->time() << "s."; DEBUG_LEVEL_3(out.str()); - int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution); - SIM_INFO("Leftover timesteps this CPU: " + std::to_string(number_timesteps - timesteps)); + int events_of_cycle = eventsOfCycle(event_stride, leftover_events_time_resolution); + + SIM_INFO("Leftover timesteps this CPU: " + + std::to_string(number_timesteps - timesteps)); SIM_INFO("Leftover events this timestep this CPU: " + std::to_string(leftover_events_time_resolution)); - processEvents(events_of_cylcle, density_events, num_cpu_syncs); + + processEvents(events_of_cycle, density_events, num_cpu_syncs, + beta_particle_weight); DEBUG_LEVEL_1("Leftover timesteps: " + std::to_string(number_timesteps - timesteps)); DEBUG_LEVEL_1("Leftover events this timestep: " + std::to_string(leftover_events_time_resolution)); // leftover_events -= events_of_cylcle; - leftover_events_time_resolution -= events_of_cylcle; + leftover_events_time_resolution -= events_of_cycle; // Problem: proc 0: simuliert erst 2 dann 1 // proc 1: simuliert nur 2 @@ -227,7 +239,6 @@ void Simulation::start() { SimulationTimer::eventStartPoint(std::string("PoissonStep")); poisson_solver_->solvePotential(chargedensity(), reset); electric_field_->updateDynamicField(poisson_solver_->staticElectricField()); - SimulationTimer::eventStopPoint(std::string("PoissonStep")); SIM_INFO("Perform output."); @@ -250,7 +261,7 @@ void Simulation::start() { // the density from which new particles are drawn is only updated after a timestep pair.second->positionDistributionFromDensity()->setMaximumDensity(); } - leftover_events_time_resolution = time_resolution_this_cpu; + leftover_events_time_resolution = beta_decay_events; timesteps++; } SimulationTimer::eventStopPoint("Reset"); @@ -261,7 +272,8 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) { return random->rollUniform(0, queue_length); } -void Simulation::processEvents(int events, int density_events, int cpu_syncs) { +void Simulation::processEvents(int events, int density_events, int cpu_syncs, + const double beta_particle_weight) { DEBUG_LEVEL_0("Processing events."); int current_event = 0; int current_density_event = 0; @@ -284,7 +296,8 @@ void Simulation::processEvents(int events, int density_events, int cpu_syncs) { current_event += 1; DEBUG_LEVEL_2("Inject particles to queue."); SimulationTimer::eventStartPoint(std::string("ParticleInjection")); - new_particles = determineInjectedParticles(random_.get()); + new_particles = determineInjectedParticles(random_.get(), + beta_particle_weight); // construction of stringstream can take up a lot of computation time // even if the debug output performs nothing therefore use the if defined // preprocessor directive @@ -305,11 +318,11 @@ void Simulation::processEvents(int events, int density_events, int cpu_syncs) { DEBUG_LEVEL_2("Current density event: " + std::to_string(current_density_event)); //maybe use a different event name? - SimulationTimer::eventStartPoint(std::string("DensityParticleInjection")); + SimulationTimer::eventStartPoint(std::string("DensityInjection")); new_particles = determineInjectedDensityParticles(random_.get(), density_events, current_density_event, particleNumbers); - SimulationTimer::eventStopPoint(std::string("DensityParticleInjection")); + SimulationTimer::eventStopPoint(std::string("DensityInjection")); #if defined debug_level3 for(auto& particle : new_particles) { std::stringstream output_ss; @@ -497,8 +510,9 @@ interaction::InteractionOutput Simulation::interact( } std::vector Simulation::determineInjectedParticles( - RandomNumbers* random) { - return injector_->rollNewParticles(random->engine(), timer_->time()); + RandomNumbers* random, const double beta_particle_weight) { + return injector_->rollNewParticles(random->engine(), timer_->time(), + beta_particle_weight); } std::vector Simulation::determineInjectedDensityParticles(