Decoupled timestep from number of beta events

This commit is contained in:
2025-02-13 10:03:15 +01:00
parent 047cfa8491
commit 24b2c2f2ed
2 changed files with 138 additions and 119 deletions
+7 -5
View File
@@ -36,16 +36,18 @@ class Simulation {
void finalize(); void finalize();
// cpu_syncs only included for debug purposes, e.g. only drawing decay particles // cpu_syncs only included for debug purposes, e.g. only drawing decay particles
// at the 0th timestep // at the 0th timestep
void processEvents(int events, int density_events, int cpu_syncs, void processEvents(int events, const double beta_particle_weight);
const double beta_particle_weight);
void processDensityEvents(int events, const double weight_factor);
void processUntilTermination(particle::Particle& particle); void processUntilTermination(particle::Particle& particle);
private: private:
std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random, std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random,
const double beta_particle_weight); const double beta_particle_weight);
std::vector<particle::Particle> std::vector<particle::Particle> determineInjectedDensityParticles(
determineInjectedDensityParticles(RandomNumbers* random, RandomNumbers* random,
const int numberParticlesToDraw, const int current, const double weight_factor,
const int current,
std::map<std::string, double>& particleNumbers); std::map<std::string, double>& particleNumbers);
double meanFreePath(particle::Particle& particle, RandomNumbers* random, double meanFreePath(particle::Particle& particle, RandomNumbers* random,
+131 -114
View File
@@ -112,14 +112,12 @@ void Simulation::start() {
// stride wrt the number of beta decay events per CPU // stride wrt the number of beta decay events per CPU
const int event_stride = const int event_stride =
parameter_.intValue(json_path({"Simulation", "EventStride"}), 1); 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 // number of timesteps to be simulated
const int number_timesteps = parameter_.intValue(json_path({"Simulation", const int number_timesteps = parameter_.intValue(json_path({"Simulation",
"NumberTimesteps"}), 1); "NumberTimesteps"}), 1);
// number of physical beta events defining the timescale by // number of physical beta events defining the timescale through
// time_resolution_events/Activity // time_resolution_events/Activity
const int time_resolution_events = const int time_resolution_events =
parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}), parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}),
@@ -137,27 +135,24 @@ void Simulation::start() {
// number of density events per timestep and CPU // number of density events per timestep and CPU
const int density_events = const int density_events =
parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0); parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0);
// calculate a 'stride' for the density events s.t. the same number of syncs
// would be performed
const int density_stride = std::ceil(static_cast<double>(
density_events * event_stride)/beta_decay_events);
// calculate the scaling factor for the particle weight for particles
// drawn from the density
const double density_weight_factor =
1./(MpiCommunication::mpiSize()*density_events);
DEBUG_LEVEL_0("Density events per cpu: " + std::to_string(density_events)); DEBUG_LEVEL_0("Density events per cpu: " + std::to_string(density_events));
SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps)); SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps));
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
// 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.");
// }
// }
// 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 = beta_decay_events; int leftover_events_time_resolution = beta_decay_events;
int leftover_density_events = density_events;
const double delta_t = time_resolution_events*injector_->timePassed(); const double delta_t = time_resolution_events*injector_->timePassed();
@@ -176,15 +171,6 @@ void Simulation::start() {
timer_->setDeltaT(delta_t); timer_->setDeltaT(delta_t);
injector_->initInjectionTimeDistribution(delta_t); injector_->initInjectionTimeDistribution(delta_t);
// // debugging(
// for(auto& pair : tracked_species_) {
// std::stringstream out;
// out << pair.second->name() << ": ";
// out << pair.second->densityForDrawing()->particleNumber();
// DEBUG_LEVEL_0(out.str());
// }
// // )debugging
int num_cpu_syncs = 0; int num_cpu_syncs = 0;
bool reset = true; bool reset = true;
int timesteps = 0; int timesteps = 0;
@@ -192,51 +178,58 @@ void Simulation::start() {
queue_.reserve(100); queue_.reserve(100);
timer_->increaseTimestep(delta_t); timer_->increaseTimestep(delta_t);
while (timesteps < number_timesteps) { while (timesteps < number_timesteps) {
#if defined debug_level3
std::stringstream out; std::stringstream out;
out << "Timeinterval: " << timer_->time() << "s."; out << "Timeinterval: " << timer_->time() << "s.";
DEBUG_LEVEL_3(out.str()); DEBUG_LEVEL_3(out.str());
#endif
// calculate the number of beta events to be simulated before a
// cpu sync is done
int events_of_cycle =
eventsOfCycle(event_stride, leftover_events_time_resolution);
int density_events_of_cycle =
eventsOfCycle(density_stride, leftover_density_events);
int events_of_cycle = eventsOfCycle(event_stride, leftover_events_time_resolution); DEBUG_LEVEL_1("Events of cycle: " + std::to_string(events_of_cycle));
DEBUG_LEVEL_1("Density Events of cycle: " +
std::to_string(density_events_of_cycle));
SIM_INFO("Leftover timesteps this CPU: " + SIM_INFO("Processing timestep: " + std::to_string(timesteps));
std::to_string(number_timesteps - timesteps));
SIM_INFO("Leftover events this timestep this CPU: " + SIM_INFO("Leftover events this timestep this CPU: " +
std::to_string(leftover_events_time_resolution)); std::to_string(leftover_events_time_resolution));
SIM_INFO("Leftover density events this timestep this CPU: " +
std::to_string(leftover_density_events));
processEvents(events_of_cycle, density_events, num_cpu_syncs, // process both density and beta events before a cpu synchronization
beta_particle_weight); processDensityEvents(density_events_of_cycle, density_weight_factor);
processEvents(events_of_cycle, beta_particle_weight);
DEBUG_LEVEL_1("Leftover timesteps: " + std::to_string(number_timesteps - timesteps)); DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timesteps));
DEBUG_LEVEL_1("Leftover events this timestep: " + DEBUG_LEVEL_1("Leftover events this timestep: " +
std::to_string(leftover_events_time_resolution)); std::to_string(leftover_events_time_resolution));
// leftover_events -= events_of_cylcle;
leftover_events_time_resolution -= events_of_cycle;
// Problem: proc 0: simuliert erst 2 dann 1 leftover_events_time_resolution -= events_of_cycle;
// proc 1: simuliert nur 2 leftover_density_events -= density_events_of_cycle;
// timestep besteht aus (2+1) + 2, aber die jetzt wird es noch zu
// 2 + 2, 1 + 2, in den syncs, das führt dazu, dass jede sync von proc 1
// als timestep gezählt wird, während proc 0 nur jede 2. sync eine timestep ist
reset = !(leftover_events_time_resolution > 0); reset = !(leftover_events_time_resolution > 0);
SIM_INFO("Cpu sync."); SIM_INFO("Cpu sync.");
SimulationTimer::eventStartPoint(std::string("CpuSync")); SimulationTimer::eventStartPoint(std::string("CpuSync"));
// 'reset' necessary as parameter to set the density for drawing
// otherwise it would be more difficult to set it... (cpu synchronizer
// is a friend class of the density field, i.e. has access to the
// private members)
cpu_synchronizer_->synchronize(tracked_species_, reset); cpu_synchronizer_->synchronize(tracked_species_, reset);
num_cpu_syncs += 1; num_cpu_syncs += 1;
SimulationTimer::eventStopPoint(std::string("CpuSync")); SimulationTimer::eventStopPoint(std::string("CpuSync"));
// // debugging( // can probably be moved into the reset section and therefore
// for(auto& pair : tracked_species_) { // the parameter 'reset' could probably be removed
// std::stringstream out;
// out << pair.second->name() << ": ";
// out << pair.second->densityForDrawing()->particleNumber();
// DEBUG_LEVEL_0(out.str());
// }
// // )debugging
SIM_INFO("Poisson step."); SIM_INFO("Poisson step.");
SimulationTimer::eventStartPoint(std::string("PoissonStep")); SimulationTimer::eventStartPoint(std::string("PoissonStep"));
// only solves the potential anew if 'reset' is true
poisson_solver_->solvePotential(chargedensity(), reset); poisson_solver_->solvePotential(chargedensity(), reset);
electric_field_->updateDynamicField(poisson_solver_->staticElectricField()); electric_field_->updateDynamicField(poisson_solver_->staticElectricField());
SimulationTimer::eventStopPoint(std::string("PoissonStep")); SimulationTimer::eventStopPoint(std::string("PoissonStep"));
@@ -250,7 +243,6 @@ void Simulation::start() {
SimulationTimer::eventStartPoint("Reset"); SimulationTimer::eventStartPoint("Reset");
if(reset) { if(reset) {
SIM_INFO("Do resets and increase the timestep for timeresolution."); SIM_INFO("Do resets and increase the timestep for timeresolution.");
timer_->increaseTimestep(delta_t);
for(auto& pair : tracked_species_) { for(auto& pair : tracked_species_) {
pair.second->dynamicDensityField()->resetDynamicDensityField(); pair.second->dynamicDensityField()->resetDynamicDensityField();
@@ -261,7 +253,11 @@ void Simulation::start() {
// the density from which new particles are drawn is only updated after a timestep // the density from which new particles are drawn is only updated after a timestep
pair.second->positionDistributionFromDensity()->setMaximumDensity(); pair.second->positionDistributionFromDensity()->setMaximumDensity();
} }
leftover_events_time_resolution = beta_decay_events; leftover_events_time_resolution = beta_decay_events;
leftover_density_events = density_events;
timer_->increaseTimestep(delta_t);
timesteps++; timesteps++;
} }
SimulationTimer::eventStopPoint("Reset"); SimulationTimer::eventStopPoint("Reset");
@@ -272,16 +268,64 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
return random->rollUniform(0, queue_length); return random->rollUniform(0, queue_length);
} }
void Simulation::processEvents(int events, int density_events, int cpu_syncs, void Simulation::processEvents(int events, const double beta_particle_weight) {
const double beta_particle_weight) {
DEBUG_LEVEL_0("Processing events."); DEBUG_LEVEL_0("Processing events.");
int current_event = 0; int current_event = 0;
int current_density_event = 0;
// calculate the number of particles currently contained within the densities while ((current_event < events) || (!queue_.empty())) {
// of the different tracked species before injection in order to avoid DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
// the expensive recalculateDensityField funtion being calculated for every
// new injection event if (queue_.empty()) {
std::vector<particle::Particle> new_particles;
current_event += 1;
DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
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
#if defined debug_level3
for(auto& particle : new_particles) {
std::stringstream output_ss;
output_ss << particle.specie()->name() << " with velocity (cart): ";
output_ss << particle.velocity().cartesianEntries();
output_ss << " and position (cart)";
output_ss << particle.position().cartesianEntries();
DEBUG_LEVEL_3(output_ss.str());
}
#endif
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
DEBUG_LEVEL_2("Injected particles to queue.");
queue_.insert(queue_.end(), new_particles.begin(),
new_particles.end());
}
DEBUG_LEVEL_2("Particles in queue: " +
std::to_string(queue_.size()));
const int particle_number =
particleNumberFromQueue(queue_.size(), random_.get());
// extract particle from queue here, so that an extension of the queue does
// not mess up the reference
particle::Particle particle = queue_.at(particle_number);
queue_.erase(queue_.begin() + particle_number);
processUntilTermination(particle);
}
}
void Simulation::processDensityEvents(int events, const double weight_factor) {
DEBUG_LEVEL_0("Processing density events.");
int current_event = 0;
// calculate the number of particles currently contained within the densities
// of the different tracked species before injection in order to avoid
// the expensive recalculateDensityField funtion being calculated for every
// new injection event
std::map<std::string, double> particleNumbers; std::map<std::string, double> particleNumbers;
for(auto& pair: tracked_species_) { for(auto& pair: tracked_species_) {
particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber()); particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber());
@@ -292,67 +336,40 @@ void Simulation::processEvents(int events, int density_events, int cpu_syncs,
if (queue_.empty()) { if (queue_.empty()) {
std::vector<particle::Particle> new_particles; std::vector<particle::Particle> new_particles;
if(current_density_event == density_events) {
current_event += 1;
DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
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
#if defined debug_level3
for(auto& particle : new_particles) {
std::stringstream output_ss;
output_ss << particle.specie()->name() << " with velocity (cart): ";
output_ss << particle.velocity().cartesianEntries();
output_ss << " and position (cart)";
output_ss << particle.position().cartesianEntries();
DEBUG_LEVEL_3(output_ss.str());
}
#endif
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
DEBUG_LEVEL_2("Injected particles to queue.");
} else { DEBUG_LEVEL_2("Current density event: " + std::to_string(current_event));
DEBUG_LEVEL_2("Current density event: " +
std::to_string(current_density_event)); SimulationTimer::eventStartPoint(std::string("DensityInjection"));
//maybe use a different event name? new_particles = determineInjectedDensityParticles(random_.get(),
SimulationTimer::eventStartPoint(std::string("DensityInjection")); weight_factor, current_event, particleNumbers);
new_particles = determineInjectedDensityParticles(random_.get(), density_events, SimulationTimer::eventStopPoint(std::string("DensityInjection"));
current_density_event,
particleNumbers); #if defined debug_level3
SimulationTimer::eventStopPoint(std::string("DensityInjection")); for(auto& particle : new_particles) {
#if defined debug_level3 std::stringstream output_ss;
for(auto& particle : new_particles) { output_ss << particle.specie()->name() << " with velocity (cart): ";
std::stringstream output_ss; output_ss << particle.velocity().cartesianEntries();
output_ss << particle.specie()->name() << " with velocity (cart): "; output_ss << " and position (cart)";
output_ss << particle.velocity().cartesianEntries(); output_ss << particle.position().cartesianEntries();
output_ss << " and position (cart)"; DEBUG_LEVEL_3(output_ss.str());
output_ss << particle.position().cartesianEntries();
DEBUG_LEVEL_3(output_ss.str());
}
#endif
// if no new particles could be injected from the density, it is not possible
// that during another iteration cycle new particles will be injected
// (drawing field only changes after timesteps)
if(new_particles.empty()) {
current_density_event = density_events-1;
}
current_density_event += 1;
} }
if(new_particles.empty()) #endif
continue; // if no new particles could be injected from the density, it is not possible
// that during another iteration cycle new particles will be injected
// (drawing field only changes after timesteps)
if(new_particles.empty()) {
return;
}
current_event += 1;
queue_.insert(queue_.end(), new_particles.begin(), queue_.insert(queue_.end(), new_particles.begin(), new_particles.end());
new_particles.end());
} }
DEBUG_LEVEL_2("Particles in queue: " + DEBUG_LEVEL_2("Particles in queue: " +
std::to_string(queue_.size())); std::to_string(queue_.size()));
const int particle_number = const int particle_number =
particleNumberFromQueue(queue_.size(), random_.get()); particleNumberFromQueue(queue_.size(), random_.get());
// extract particle here from queue, so that an extension of the queue does // extract particle from queue here, so that an extension of the queue does
// not mess up the reference // not mess up the reference
particle::Particle particle = queue_.at(particle_number); particle::Particle particle = queue_.at(particle_number);
queue_.erase(queue_.begin() + particle_number); queue_.erase(queue_.begin() + particle_number);
@@ -515,8 +532,9 @@ std::vector<particle::Particle> Simulation::determineInjectedParticles(
beta_particle_weight); beta_particle_weight);
} }
// weight_factor has to be such that particleNumber * weight_factor = weight
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles( std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
RandomNumbers* random, const int numberParticlesToDraw, const int current, RandomNumbers* random, const double weight_factor, const int current,
std::map<std::string, double>& particleNumbers) { std::map<std::string, double>& particleNumbers) {
std::vector<particle::Particle> new_particles{}; std::vector<particle::Particle> new_particles{};
@@ -543,8 +561,7 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
} }
#endif #endif
double weight = particleNumbers[pair.first]/numberParticlesToDraw double weight = particleNumbers[pair.first]*weight_factor;
/MpiCommunication::mpiSize();
injector_->drawNewParticleFromDensityField(random->engine(), injector_->drawNewParticleFromDensityField(random->engine(),
pair.second, new_particles, magnetic_field_, pair.second, new_particles, magnetic_field_,