diff --git a/include/simulation/injector.h b/include/simulation/injector.h index dc60beb..48364a6 100755 --- a/include/simulation/injector.h +++ b/include/simulation/injector.h @@ -35,7 +35,9 @@ class Injector { std::shared_ptr position_distribution, std::shared_ptr mother_drift_velocity); - std::vector rollNewParticles(std::mt19937* engine); + std::vector rollNewParticles(std::mt19937* engine, + const double current_time, + const int time_resolution_events); double timePassed() const; private: @@ -48,7 +50,8 @@ class Injector { std::mt19937* engine, const Coordinate& position, std::shared_ptr created_specie, - std::shared_ptr creation_velocity_distribution); + std::shared_ptr creation_velocity_distribution, + const double time_birth); }; #endif /* INJECTOR_H */ diff --git a/include/simulation/particle.h b/include/simulation/particle.h index fe99bdf..ca1aaf0 100755 --- a/include/simulation/particle.h +++ b/include/simulation/particle.h @@ -14,7 +14,8 @@ class Particle { // Vector* velocity); Particle(std::shared_ptr specie, const Coordinate& position, - const Vector& velocity); + const Vector& velocity, + const double time_birth); void position(const Coordinate& new_position); CartesianCoordinate position() const; diff --git a/include/simulation/simulation.h b/include/simulation/simulation.h index 0e37404..6a8436a 100755 --- a/include/simulation/simulation.h +++ b/include/simulation/simulation.h @@ -34,13 +34,15 @@ class Simulation { void init(Parameter& parameter); void start(); void finalize(); - void processEvents(int events); + void processEvents(int events, std::vector& backup_queue, + const int time_resolution_events); void processUntilTermination(particle::Particle& particle, - std::vector& particle_queue); + std::vector& particle_queue, + std::vector& backup_queue); private: std::vector determineInjectedParticles( - RandomNumbers* random); + RandomNumbers* random, const int time_resolution_events); double meanFreePath(particle::Particle& particle, RandomNumbers* random); diff --git a/include/simulation/terminator.h b/include/simulation/terminator.h index 4e1c980..5c5c375 100755 --- a/include/simulation/terminator.h +++ b/include/simulation/terminator.h @@ -4,7 +4,7 @@ #include "simulation/particle.h" class Geometry; - +class DynamicDensityField; class Terminator { public: Terminator(long int max_interactions, diff --git a/src/interactions/cluster_formation.cpp b/src/interactions/cluster_formation.cpp index b1dc0a9..7f0e8eb 100755 --- a/src/interactions/cluster_formation.cpp +++ b/src/interactions/cluster_formation.cpp @@ -30,7 +30,8 @@ InteractionOutput ClusterFormation::performInteraction( const blitz::TinyVector v_cluster = kinematic::velocity(energy_eV, product_->mass_kg()) * v_target / kinematic::norm(v_target); - particle::Particle cluster(product_, x_impact, CartesianVector(v_cluster)); + particle::Particle cluster(product_, x_impact, CartesianVector(v_cluster), + particle.timeAlive()); InteractionOutput output; output.add(cluster); diff --git a/src/interactions/ionization.cpp b/src/interactions/ionization.cpp index e190e49..171f0c2 100755 --- a/src/interactions/ionization.cpp +++ b/src/interactions/ionization.cpp @@ -93,10 +93,14 @@ InteractionOutput ElectronIonization::performInteraction( particle.velocity(v_impact_new); + + const double time_birth = particle.timeAlive(); InteractionOutput output; output.add( - particle::Particle(electron_, x_impact, CartesianVector(v_ejected))); - output.add(particle::Particle(ion_, x_impact, CartesianVector(v_target))); + particle::Particle(electron_, x_impact, CartesianVector(v_ejected), + time_birth)); + output.add(particle::Particle(ion_, x_impact, CartesianVector(v_target), + time_birth)); return output; } diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index 17bd45a..98e9085 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -133,9 +133,10 @@ void CpuSynchronizer::synchronize( tracked_species) { MPI_Barrier(MPI_COMM_WORLD); auto it = tracked_species.begin(); - DEBUG_LEVEL_3("Timer Synchronization."); - synchronize(it->second->dynamicDensityField()->timer_.get(), - it->second->lastSyncDensityField()->timer_.get()); + // drops due to time being a global variable now + //DEBUG_LEVEL_3("Timer Synchronization."); + //synchronize(it->second->dynamicDensityField()->timer_.get(), + // it->second->lastSyncDensityField()->timer_.get()); for (auto& pair : tracked_species) { // DEBUG_LEVEL_3("dynden Synchronization."); diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index 5b38728..dc1a6e6 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -206,7 +206,7 @@ double DynamicDensityField::physicalTime() const { } void DynamicDensityField::resetDynamicDensityField() { - timer_->resetTime(); // reductive since shared between species + //timer_->resetTime(); // time stays global time_in_bin_s_ = 0.; deleted_particles_ = 0.; density_per_m3_ = 0.; diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index 4cd99a3..f6cdecb 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -7,6 +7,7 @@ #include "simulation/particle.h" #include "simulation/species.h" #include "simulation/velocity_distribution.h" +#include Injector::Injector( std::vector& injector_info, @@ -26,7 +27,8 @@ particle::Particle Injector::rollNewParticle( std::mt19937* engine, const Coordinate& position, std::shared_ptr created_specie, - std::shared_ptr creation_velocity_distribution) { + std::shared_ptr creation_velocity_distribution, + const double time_birth) { blitz::TinyVector velocity = creation_velocity_distribution->rollVelocity(position, engine) .cartesianEntries(); @@ -35,20 +37,29 @@ particle::Particle Injector::rollNewParticle( velocity, mother_drift_velocity_->driftVelocity(position).cartesianEntries()); } + std::stringstream out; + out << created_specie->name() << " created at time: " << time_birth << "s."; + DEBUG_LEVEL_3(out.str()); return particle::Particle(created_specie, position, - CartesianVector(velocity)); + CartesianVector(velocity), time_birth); } std::vector Injector::rollNewParticles( - std::mt19937* engine) { + std::mt19937* engine, const double current_time, + const int time_resolution_events) { CylinderCoordinate position = position_distribution_->rollPosition(engine); DEBUG_LEVEL_3("Injected z position: " + std::to_string(position.cylinderEntries()(2))); std::vector new_particles; + const double delta_t = time_resolution_events*timePassed(); + std::uniform_real_distribution<> injection_time(current_time - delta_t, + current_time); + const double time_birth = injection_time(*engine); for (auto& injector_info : injector_infos_) { new_particles.push_back( rollNewParticle(engine, position, injector_info.created_specie, - injector_info.creation_velocity_distribution)); + injector_info.creation_velocity_distribution, + time_birth)); } return new_particles; } diff --git a/src/simulation/particle.cpp b/src/simulation/particle.cpp index ffec319..93029ef 100755 --- a/src/simulation/particle.cpp +++ b/src/simulation/particle.cpp @@ -20,12 +20,13 @@ Particle::Particle(std::shared_ptr specie, Particle::Particle(std::shared_ptr specie, const Coordinate& position, - const Vector& velocity) + const Vector& velocity, + const double time_birth) : specie_(specie), position_(position), velocity_(velocity, position), id_(this->last_id_), - time_alive_s_(0.), + time_alive_s_(time_birth), time_in_bin_s_(0.), interaction_counter_(0) { last_id_++; diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 73f0acb..4e6d91d 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -99,19 +99,33 @@ void Simulation::start() { parameter_.intValue(json_path({"Simulation", "EventStride"}), 1); const int total_simulated_events = parameter_.intValue(json_path({"Simulation", "SimulatedEvents"}), 1); + const int time_resolution_events = + parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}), + total_simulated_events); + SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events)); SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); const int events_this_cpu = eventsThisCpu(total_simulated_events); + // maybe better as class memeber... + const int time_resolution_this_cpu = eventsThisCpu(time_resolution_events); SIM_INFO("Events per cpu (approx.): " + std::to_string(events_this_cpu)); int leftover_events = events_this_cpu; int num_cpu_syncs = 0; bool reset = false; + std::vector backup_queue; + backup_queue.reserve(100); // probably should be a more sensible value while (leftover_events > 0) { - int events_of_cylcle = eventsOfCycle(event_stride, leftover_events); + timer_->increaseTimestep(time_resolution_events*injector_->timePassed()); + std::stringstream out; + out << "Timeinterval: " << timer_->time() << "s."; + out << "\n size of particle: " << sizeof(particle::Particle); + DEBUG_LEVEL_3(out.str()); + int events_of_cylcle = eventsOfCycle(event_stride, time_resolution_this_cpu); SIM_INFO("Leftover events on first cpu: " + std::to_string(leftover_events)); - processEvents(events_of_cylcle); + processEvents(events_of_cylcle, backup_queue, time_resolution_events); + DEBUG_LEVEL_3("Size of backup queue: " + std::to_string(backup_queue.size())); DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events)); leftover_events -= events_of_cylcle; @@ -154,10 +168,19 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) { return random->rollUniform(0, queue_length); } -void Simulation::processEvents(int events) { +void Simulation::processEvents(int events, + std::vector& backup_queue, + const int time_resolution_events) { DEBUG_LEVEL_0("Processing events."); std::vector particle_queue; particle_queue.reserve(100); + DEBUG_LEVEL_3("Move backup to processing queue."); + // highly inefficient in case that #cpu_syncs/time_res >> 1 + particle_queue.insert(particle_queue.end(), backup_queue.begin(), + backup_queue.end()); + // clean backup_queue after moving the elements to the particle queue + backup_queue.erase(backup_queue.begin(), backup_queue.end()); + DEBUG_LEVEL_3("Size processing queue: " + std::to_string(particle_queue.size())); int current_event = 0; while ((current_event < events) || (!particle_queue.empty())) { DEBUG_LEVEL_2("Current event: " + std::to_string(current_event)); @@ -167,7 +190,7 @@ void Simulation::processEvents(int events) { DEBUG_LEVEL_2("Inject particles to queue."); SimulationTimer::eventStartPoint(std::string("ParticleInjection")); std::vector new_particles = - determineInjectedParticles(random_.get()); + determineInjectedParticles(random_.get(), time_resolution_events); #if defined debug_level3 for(auto& particle : new_particles) { std::stringstream output_ss; @@ -193,13 +216,14 @@ void Simulation::processEvents(int events) { particle::Particle particle = particle_queue.at(particle_number); particle_queue.erase(particle_queue.begin() + particle_number); - processUntilTermination(particle, particle_queue); + processUntilTermination(particle, particle_queue, backup_queue); } } void Simulation::processUntilTermination( particle::Particle& particle, - std::vector& particle_queue) { + std::vector& particle_queue, + std::vector& backup_queue) { DEBUG_LEVEL_3("Process particle until termination"); while (particle.specie()->terminator()->isAlive(&particle)) { auto old_position = particle.position(); @@ -226,12 +250,9 @@ void Simulation::processUntilTermination( if (particle.specie()->terminator()->reachedMaxTimeAlive( &particle)) { - DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() - +". Reached max time alive."); - if (particle.specie()->deletionLogger()) { - particle.specie()->deletionLogger()->logDeletion( - particle.position(), DeletionType::max_time_alive); - } + DEBUG_LEVEL_3("Move particle " + particle.specie()->name() + +" to backup queue."); + backup_queue.push_back(particle); break; } else if (particle.specie()->terminator()->leftSimulationDomain( &particle)) { @@ -339,9 +360,10 @@ interaction::InteractionOutput Simulation::interact( } std::vector Simulation::determineInjectedParticles( - RandomNumbers* random) { - timer_->increaseTimestep(injector_->timePassed()); - return injector_->rollNewParticles(random->engine()); + RandomNumbers* random, const int time_resolution_events) { + // timer_->increaseTimestep(injector_->timePassed()); + return injector_->rollNewParticles(random->engine(), + timer_->time(), time_resolution_events); } //maybe put that into the poisson solver diff --git a/src/simulation/terminator.cpp b/src/simulation/terminator.cpp index 5d0b25c..2e9ec1e 100755 --- a/src/simulation/terminator.cpp +++ b/src/simulation/terminator.cpp @@ -2,6 +2,7 @@ #include "simulation/errors.h" #include "simulation/geometry.h" +#include "simulation/density_field.h" Terminator::Terminator(long int max_interactions, double max_time_alive, @@ -31,7 +32,8 @@ bool Terminator::reachedMaxInteractions( return particle->interactionCounter() > max_interactions_; } bool Terminator::reachedMaxTimeAlive(const particle::Particle* particle) const { - return particle->timeAlive() > max_time_alive_; + return particle->timeAlive() > particle->specie()->dynamicDensityField() + ->physicalTime(); } bool Terminator::leftSimulationDomain(