Decoupled timestep from number of beta events
This commit is contained in:
@@ -36,16 +36,18 @@ 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,
|
||||
const double beta_particle_weight);
|
||||
void processEvents(int events, const double beta_particle_weight);
|
||||
|
||||
void processDensityEvents(int events, const double weight_factor);
|
||||
void processUntilTermination(particle::Particle& particle);
|
||||
|
||||
private:
|
||||
std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random,
|
||||
const double beta_particle_weight);
|
||||
std::vector<particle::Particle>
|
||||
determineInjectedDensityParticles(RandomNumbers* random,
|
||||
const int numberParticlesToDraw, const int current,
|
||||
std::vector<particle::Particle> determineInjectedDensityParticles(
|
||||
RandomNumbers* random,
|
||||
const double weight_factor,
|
||||
const int current,
|
||||
std::map<std::string, double>& particleNumbers);
|
||||
|
||||
double meanFreePath(particle::Particle& particle, RandomNumbers* random,
|
||||
|
||||
+131
-114
@@ -112,14 +112,12 @@ void Simulation::start() {
|
||||
// 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
|
||||
// number of physical beta events defining the timescale through
|
||||
// time_resolution_events/Activity
|
||||
const int time_resolution_events =
|
||||
parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}),
|
||||
@@ -137,27 +135,24 @@ void Simulation::start() {
|
||||
// number of density events per timestep and CPU
|
||||
const int density_events =
|
||||
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));
|
||||
|
||||
SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps));
|
||||
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_density_events = density_events;
|
||||
|
||||
const double delta_t = time_resolution_events*injector_->timePassed();
|
||||
|
||||
@@ -176,15 +171,6 @@ void Simulation::start() {
|
||||
timer_->setDeltaT(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;
|
||||
bool reset = true;
|
||||
int timesteps = 0;
|
||||
@@ -192,51 +178,58 @@ void Simulation::start() {
|
||||
queue_.reserve(100);
|
||||
|
||||
timer_->increaseTimestep(delta_t);
|
||||
|
||||
while (timesteps < number_timesteps) {
|
||||
#if defined debug_level3
|
||||
std::stringstream out;
|
||||
out << "Timeinterval: " << timer_->time() << "s.";
|
||||
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: " +
|
||||
std::to_string(number_timesteps - timesteps));
|
||||
SIM_INFO("Processing timestep: " + std::to_string(timesteps));
|
||||
SIM_INFO("Leftover events this timestep this CPU: " +
|
||||
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,
|
||||
beta_particle_weight);
|
||||
// process both density and beta events before a cpu synchronization
|
||||
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: " +
|
||||
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
|
||||
// proc 1: simuliert nur 2
|
||||
// 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
|
||||
leftover_events_time_resolution -= events_of_cycle;
|
||||
leftover_density_events -= density_events_of_cycle;
|
||||
|
||||
reset = !(leftover_events_time_resolution > 0);
|
||||
|
||||
SIM_INFO("Cpu sync.");
|
||||
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);
|
||||
num_cpu_syncs += 1;
|
||||
SimulationTimer::eventStopPoint(std::string("CpuSync"));
|
||||
|
||||
// // debugging(
|
||||
// for(auto& pair : tracked_species_) {
|
||||
// std::stringstream out;
|
||||
// out << pair.second->name() << ": ";
|
||||
// out << pair.second->densityForDrawing()->particleNumber();
|
||||
// DEBUG_LEVEL_0(out.str());
|
||||
// }
|
||||
// // )debugging
|
||||
|
||||
// can probably be moved into the reset section and therefore
|
||||
// the parameter 'reset' could probably be removed
|
||||
SIM_INFO("Poisson step.");
|
||||
SimulationTimer::eventStartPoint(std::string("PoissonStep"));
|
||||
// only solves the potential anew if 'reset' is true
|
||||
poisson_solver_->solvePotential(chargedensity(), reset);
|
||||
electric_field_->updateDynamicField(poisson_solver_->staticElectricField());
|
||||
SimulationTimer::eventStopPoint(std::string("PoissonStep"));
|
||||
@@ -250,7 +243,6 @@ void Simulation::start() {
|
||||
SimulationTimer::eventStartPoint("Reset");
|
||||
if(reset) {
|
||||
SIM_INFO("Do resets and increase the timestep for timeresolution.");
|
||||
timer_->increaseTimestep(delta_t);
|
||||
for(auto& pair : tracked_species_) {
|
||||
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
|
||||
pair.second->positionDistributionFromDensity()->setMaximumDensity();
|
||||
}
|
||||
|
||||
leftover_events_time_resolution = beta_decay_events;
|
||||
leftover_density_events = density_events;
|
||||
|
||||
timer_->increaseTimestep(delta_t);
|
||||
timesteps++;
|
||||
}
|
||||
SimulationTimer::eventStopPoint("Reset");
|
||||
@@ -272,16 +268,64 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
|
||||
return random->rollUniform(0, queue_length);
|
||||
}
|
||||
|
||||
void Simulation::processEvents(int events, int density_events, int cpu_syncs,
|
||||
const double beta_particle_weight) {
|
||||
void Simulation::processEvents(int events, const double beta_particle_weight) {
|
||||
DEBUG_LEVEL_0("Processing events.");
|
||||
int current_event = 0;
|
||||
int current_density_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
|
||||
while ((current_event < events) || (!queue_.empty())) {
|
||||
DEBUG_LEVEL_2("Current event: " + std::to_string(current_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;
|
||||
for(auto& pair: tracked_species_) {
|
||||
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()) {
|
||||
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_density_event));
|
||||
//maybe use a different event name?
|
||||
SimulationTimer::eventStartPoint(std::string("DensityInjection"));
|
||||
new_particles = determineInjectedDensityParticles(random_.get(), density_events,
|
||||
current_density_event,
|
||||
particleNumbers);
|
||||
SimulationTimer::eventStopPoint(std::string("DensityInjection"));
|
||||
#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
|
||||
// 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;
|
||||
DEBUG_LEVEL_2("Current density event: " + std::to_string(current_event));
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("DensityInjection"));
|
||||
new_particles = determineInjectedDensityParticles(random_.get(),
|
||||
weight_factor, current_event, particleNumbers);
|
||||
SimulationTimer::eventStopPoint(std::string("DensityInjection"));
|
||||
|
||||
#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());
|
||||
}
|
||||
if(new_particles.empty())
|
||||
continue;
|
||||
#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()) {
|
||||
return;
|
||||
}
|
||||
current_event += 1;
|
||||
|
||||
queue_.insert(queue_.end(), new_particles.begin(),
|
||||
new_particles.end());
|
||||
queue_.insert(queue_.end(), new_particles.begin(), new_particles.end());
|
||||
}
|
||||
DEBUG_LEVEL_2("Particles in queue: " +
|
||||
std::to_string(queue_.size()));
|
||||
std::to_string(queue_.size()));
|
||||
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
|
||||
particle::Particle particle = queue_.at(particle_number);
|
||||
queue_.erase(queue_.begin() + particle_number);
|
||||
@@ -515,8 +532,9 @@ std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
||||
beta_particle_weight);
|
||||
}
|
||||
|
||||
// weight_factor has to be such that particleNumber * weight_factor = weight
|
||||
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::vector<particle::Particle> new_particles{};
|
||||
|
||||
@@ -543,8 +561,7 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
||||
}
|
||||
#endif
|
||||
|
||||
double weight = particleNumbers[pair.first]/numberParticlesToDraw
|
||||
/MpiCommunication::mpiSize();
|
||||
double weight = particleNumbers[pair.first]*weight_factor;
|
||||
|
||||
injector_->drawNewParticleFromDensityField(random->engine(),
|
||||
pair.second, new_particles, magnetic_field_,
|
||||
|
||||
Reference in New Issue
Block a user