diff --git a/include/generators/species_generator.h b/include/generators/species_generator.h index f2c1e4e..62d44b4 100755 --- a/include/generators/species_generator.h +++ b/include/generators/species_generator.h @@ -24,6 +24,7 @@ struct TrackedSpeciesBuilderInfo { std::shared_ptr velocity_distribution; std::shared_ptr drift_velocity; std::shared_ptr last_sync_density_field; + std::shared_ptr density_drawing_field; double mass_kg; double charge_C; diff --git a/include/simulation/cpu_synchronizer.h b/include/simulation/cpu_synchronizer.h index 9e39d37..0930d71 100755 --- a/include/simulation/cpu_synchronizer.h +++ b/include/simulation/cpu_synchronizer.h @@ -21,10 +21,12 @@ class CpuSynchronizer { void synchronize( std::map>& - tracked_species); + tracked_species, bool reset); void synchronize(DynamicDensityField* field, - DynamicDensityField* last_sync_field); + DynamicDensityField* last_sync_field, + DynamicDensityField* density_for_drawing, + bool reset); void synchronize(CurrentCollector* collector, CurrentCollector* last_sync_collector); void synchronize(DeletionLogger* logger, diff --git a/include/simulation/density_field.h b/include/simulation/density_field.h index 201dae1..5859366 100755 --- a/include/simulation/density_field.h +++ b/include/simulation/density_field.h @@ -56,6 +56,7 @@ class DynamicDensityField : public DensityField { const CylinderBinning* positionBinning() const; double physicalTime() const; + double timeResolution() const; friend class CpuSynchronizer; diff --git a/include/simulation/simulation.h b/include/simulation/simulation.h index c7b739a..420e6bc 100755 --- a/include/simulation/simulation.h +++ b/include/simulation/simulation.h @@ -34,14 +34,16 @@ class Simulation { void init(Parameter& parameter); void start(); void finalize(); - void processEvents(int events, int density_events); + // 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 processUntilTermination(particle::Particle& particle); private: std::vector determineInjectedParticles(RandomNumbers* random); std::vector determineInjectedDensityParticles(RandomNumbers* random, - const int numberParticlesToDraw); + const int numberParticlesToDraw, const int current); double meanFreePath(particle::Particle& particle, RandomNumbers* random); diff --git a/include/simulation/species.h b/include/simulation/species.h index c86a48c..b21c331 100755 --- a/include/simulation/species.h +++ b/include/simulation/species.h @@ -62,7 +62,8 @@ class TrackedSpecie : public Specie { std::shared_ptr density_field, std::shared_ptr velocity_distribution, std::shared_ptr drift_velocity, - std::shared_ptr last_sync_density_field); + std::shared_ptr last_sync_density_field, + std::shared_ptr density_for_drawing); DensityField* densityField(); VelocityDistribution* velocityDistibution(); @@ -80,6 +81,8 @@ class TrackedSpecie : public Specie { DeletionLogger* lastSyncDeletionLogger(); void lastSyncDeletionLogger(std::shared_ptr new_logger); + DynamicDensityField* densityForDrawing(); + CurrentCollector* currentCollector(); void currentCollector(std::shared_ptr new_collector); @@ -109,6 +112,8 @@ class TrackedSpecie : public Specie { std::shared_ptr last_sync_density_field_; std::shared_ptr last_sync_deletion_logger_; std::shared_ptr last_sync_current_collector_; + + std::shared_ptr density_for_drawing_; }; class BackgroundSpecie : public Specie { diff --git a/src/generators/species_generator.cpp b/src/generators/species_generator.cpp index e983c86..21ed5fc 100755 --- a/src/generators/species_generator.cpp +++ b/src/generators/species_generator.cpp @@ -259,6 +259,13 @@ void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) { {"DensityField", "DynamicDensityField"}), timer_); +// not the optimal solution! (has to be optimized) + b.second.density_drawing_field = buildDynamicDensityField( + parameter, + json_path_append(b.second.path_in_json, + {"DensityField", "DynamicDensityField"}), + timer_); + b.second.last_sync_density_field = buildDynamicDensityField( parameter, json_path_append(b.second.path_in_json, @@ -283,7 +290,8 @@ void SpeciesBuilder::buildSpecies() { b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C, b.second.mover, b.second.terminator, b.second.reflector, b.second.density_field, b.second.velocity_distribution, - b.second.drift_velocity, b.second.last_sync_density_field)); + b.second.drift_velocity, b.second.last_sync_density_field, + b.second.density_drawing_field)); } } diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index 98e9085..84f8613 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -65,7 +65,9 @@ void CpuSynchronizer::synchronize(Timer* timer, Timer* last_sync_timer) { } void CpuSynchronizer::synchronize(DynamicDensityField* field, - DynamicDensityField* last_sync_field) { + DynamicDensityField* last_sync_field, + DynamicDensityField* density_for_drawing, + bool reset) { if (field && last_sync_field) { if(field == last_sync_field) { ERROR("The way last_sync_field is created does not work as intended."); @@ -75,6 +77,11 @@ void CpuSynchronizer::synchronize(DynamicDensityField* field, sumToAll(field->deleted_particles_, last_sync_field->deleted_particles_); field->deleted_particles_ = last_sync_field->deleted_particles_; + if (reset) { + density_for_drawing->time_in_bin_s_ = last_sync_field->time_in_bin_s_; + density_for_drawing->deleted_particles_ = + last_sync_field->deleted_particles_; + } } } @@ -130,7 +137,7 @@ void CpuSynchronizer::synchronize(DeletionLogger* logger, void CpuSynchronizer::synchronize( std::map>& - tracked_species) { + tracked_species, bool reset) { MPI_Barrier(MPI_COMM_WORLD); auto it = tracked_species.begin(); // drops due to time being a global variable now @@ -141,7 +148,8 @@ void CpuSynchronizer::synchronize( for (auto& pair : tracked_species) { // DEBUG_LEVEL_3("dynden Synchronization."); synchronize(pair.second->dynamicDensityField(), - pair.second->lastSyncDensityField()); + pair.second->lastSyncDensityField(), + pair.second->densityForDrawing(), reset); // DEBUG_LEVEL_3("cc Synchronization."); synchronize(pair.second->currentCollector(), pair.second->lastSyncCurrentCollector()); diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index e26e5f1..23f3214 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -183,14 +183,17 @@ void DynamicDensityField::initPositionDistributions() { auto zbins = position_binning_.longitudinal_binning.binBorders(); auto phibins = position_binning_.azimuth_binning.binBorders(); - rmin = *(rbins.begin()); - rmax = *(rbins.end()); + rmin = rbins.front(); + rmax = rbins.back(); + DEBUG_LEVEL_3("rmax: " + std::to_string(rmax)); - zmin = *(zbins.begin()); - zmax = *(zbins.end()); + zmin = zbins.front(); + zmax = zbins.back(); + DEBUG_LEVEL_3("zmax: " + std::to_string(zmax)); - phimin = *(phibins.begin()); - phimax = *(phibins.end()); + phimin = phibins.front(); + phimax = phibins.back(); + DEBUG_LEVEL_3("phimax: " + std::to_string(phimax)); r_dist_ = std::uniform_real_distribution(rmin*rmin, rmax*rmax); phi_dist_ = std::uniform_real_distribution(phimin, phimax); @@ -206,6 +209,7 @@ blitz::TinyVector DynamicDensityField::drawPositionAndEnergy( std::mt19937* engine) { // if the density is empty, then return a TinyVector filled with 0s as // an indiction of that (probably not the best solution) + recalculateDensityArray(); if (isEqual(blitz::max(density_per_m3_), 0.)) { DEBUG_LEVEL_3("Density is empty"); return blitz::TinyVector (0,0,0,0); @@ -245,6 +249,7 @@ blitz::TinyVector DynamicDensityField::drawPositionAndEnergy( densityAtPosition.begin()); random_E = E_dist_(*engine); + DEBUG_LEVEL_3("energy: " + std::to_string(random_E)); return blitz::TinyVector (random_r, random_phi, random_z, random_E); } @@ -288,6 +293,10 @@ double DynamicDensityField::physicalTime() const { return timer_->time(); } +double DynamicDensityField::timeResolution() const { + return timer_->getDeltaT(); +} + void DynamicDensityField::resetDynamicDensityField() { //timer_->resetTime(); // time stays global time_in_bin_s_ = 0.; diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index 63d6464..4cc1107 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -79,7 +79,7 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine, const double weight, const double injection_time) { blitz::TinyVector particle_specs = - specie->dynamicDensityField()->drawPositionAndEnergy(engine); + specie->densityForDrawing()->drawPositionAndEnergy(engine); // blitz::TinyVector particle_specs(0.01, 0, 1, 1); // check if density was empty (i.e. Energy component is 0) @@ -89,6 +89,7 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine, } DEBUG_LEVEL_3("Injected z position: " + std::to_string(particle_specs(2))); + DEBUG_LEVEL_3("Injected r position: " + std::to_string(particle_specs(0))); const double v_abs = kinematic::velocity(particle_specs[3], specie->mass_kg()); diff --git a/src/simulation/mover.cpp b/src/simulation/mover.cpp index 55b290a..33cfb32 100755 --- a/src/simulation/mover.cpp +++ b/src/simulation/mover.cpp @@ -186,8 +186,9 @@ void DriftMover::move(particle::Particle* particle, bool left_simulation_domain = false; // if particle too slow -> timestep > maximum time the particle lives // -> add maximal timestep to time in bin and stop moving the particle - bool died_in_bin = false; - const double max_time_alive = + bool died_in_bin = false; +// DEBUG_LEVEL_3("Mover: timealive before moving: " + std::to_string(particle->timeAlive())); + double max_time_alive = particle->specie()->dynamicDensityField()->physicalTime() - particle->timeAlive(); @@ -308,6 +309,22 @@ void DriftMover::move(particle::Particle* particle, particle->increaseTimeAlive(timestep); particle->increaseTimeInBin(timestep); + // if instead of eps*deltaT 0.0 is used the integral would be conserved if no particles were lost + // however in that case the terminator does not return the correct value if the time the + // particle is alive is identical to the physical time... -> increases computation time alot + // The version below means that each timestep the density can grow by (at most) a factor + // of (1 + eps) (if no particles are lost) + // TODO: + // - check if it's not better to change the terminator such that is uses the + // compare function from /include/simulation/compare.h + max_time_alive = ((max_time_alive - timestep) <= 0) + ? particle->specie()->dynamicDensityField() + ->timeResolution()*SimConstants::compare_epsilon + : (max_time_alive - timestep); + if (max_time_alive == 0) { + DEBUG_LEVEL_3("Mover: max_time_alive is 0."); + DEBUG_LEVEL_3("Mover: particle time alive: " + std::to_string(particle->timeAlive())); + } const auto new_position = particle->position(); diff --git a/src/simulation/particle.cpp b/src/simulation/particle.cpp index ba77485..f9be705 100755 --- a/src/simulation/particle.cpp +++ b/src/simulation/particle.cpp @@ -102,7 +102,7 @@ double Particle::timeAlive() const { } void Particle::increaseTimeInBin(double time_s) { - time_in_bin_s_ += time_s*weight_; + time_in_bin_s_ += time_s; } void Particle::resetTimeInBin() { @@ -110,7 +110,7 @@ void Particle::resetTimeInBin() { } double Particle::timeInBin() const { - return time_in_bin_s_; + return time_in_bin_s_*weight_; } long int Particle::ID() const { diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index c0bf4d6..51b6f29 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -104,6 +104,7 @@ void Simulation::start() { total_simulated_events); const int density_events = parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0); + DEBUG_LEVEL_0("Density events per cpu: " + std::to_string(density_events)); SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events)); SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); @@ -132,7 +133,7 @@ void Simulation::start() { int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution); SIM_INFO("Leftover events on first cpu: " + std::to_string(leftover_events)); - processEvents(events_of_cylcle, density_events); + processEvents(events_of_cylcle, density_events, num_cpu_syncs); DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events)); leftover_events -= events_of_cylcle; @@ -142,7 +143,7 @@ void Simulation::start() { SIM_INFO("Cpu sync."); SimulationTimer::eventStartPoint(std::string("CpuSync")); - cpu_synchronizer_->synchronize(tracked_species_); + cpu_synchronizer_->synchronize(tracked_species_, reset); num_cpu_syncs += 1; SimulationTimer::eventStopPoint(std::string("CpuSync")); @@ -185,7 +186,7 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) { return random->rollUniform(0, queue_length); } -void Simulation::processEvents(int events, int density_events) { +void Simulation::processEvents(int events, int density_events, int cpu_syncs) { DEBUG_LEVEL_0("Processing events."); int current_event = 0; int current_density_event = 0; @@ -195,6 +196,10 @@ void Simulation::processEvents(int events, int density_events) { if (queue_.empty()) { std::vector new_particles; if(current_density_event == density_events) { + // if(cpu_syncs > 0) { + // DEBUG_LEVEL_3("Break while loop."); + // break; + // } current_event += 1; DEBUG_LEVEL_2("Inject particles to queue."); SimulationTimer::eventStartPoint(std::string("ParticleInjection")); @@ -213,13 +218,13 @@ void Simulation::processEvents(int events, int density_events) { DEBUG_LEVEL_2("Injected particles to queue."); } else { - current_density_event += 1; DEBUG_LEVEL_2("Current density event: " + std::to_string(current_density_event)); //maybe use a different event name? SimulationTimer::eventStartPoint(std::string("DensityParticleInjection")); - new_particles = determineInjectedDensityParticles(random_.get(), density_events); + new_particles = determineInjectedDensityParticles(random_.get(), density_events, current_density_event); SimulationTimer::eventStopPoint(std::string("DensityParticleInjection")); + current_density_event += 1; } if(new_particles.empty()) continue; @@ -385,24 +390,28 @@ std::vector Simulation::determineInjectedParticles( } std::vector Simulation::determineInjectedDensityParticles( - RandomNumbers* random, const int numberParticlesToDraw) { + RandomNumbers* random, const int numberParticlesToDraw, const int current) { std::vector new_particles{}; if(!new_particles.empty()) { ERROR("In drawing particles from the density: the new particles vector is not empty at initialization"); } const double injection_time = timer_->time() - timer_->getDeltaT(); for(auto& pair: tracked_species_) { + // particle number of the denstiy, i.e. the integral over the density const double particleNumberDensity = - pair.second->dynamicDensityField()->particleNumber(); + pair.second->densityForDrawing()->particleNumber(); if (particleNumberDensity == 0) { DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name()); continue; } - DEBUG_LEVEL_3("particle number " + pair.second->name() + ": " + std::to_string(particleNumberDensity)); + if (current == 0) { + DEBUG_LEVEL_0("particle number " + pair.second->name() + ": " + std::to_string(particleNumberDensity)); + // DEBUG_LEVEL_0("weight: " + std::to_string(particleNumberDensity/numberParticlesToDraw)); + } injector_->rollNewParticleFromDensityField(random->engine(), pair.second, new_particles, - numberParticlesToDraw/particleNumberDensity, + particleNumberDensity/numberParticlesToDraw/MpiCommunication::mpiSize(), injection_time); } return new_particles; diff --git a/src/simulation/species.cpp b/src/simulation/species.cpp index 33e42a6..c8dd8f4 100755 --- a/src/simulation/species.cpp +++ b/src/simulation/species.cpp @@ -42,7 +42,8 @@ TrackedSpecie::TrackedSpecie( std::shared_ptr density_field, std::shared_ptr velocity_distribution, std::shared_ptr drift_velocity, - std::shared_ptr last_sync_density_field) + std::shared_ptr last_sync_density_field, + std::shared_ptr density_for_drawing) : Specie(type, name, mass_kg, charge_C), mover_(mover), terminator_(terminator), @@ -54,7 +55,8 @@ TrackedSpecie::TrackedSpecie( drift_velocity_(drift_velocity), last_sync_density_field_(last_sync_density_field), last_sync_deletion_logger_(std::shared_ptr(nullptr)), - last_sync_current_collector_(std::shared_ptr(nullptr)) {} + last_sync_current_collector_(std::shared_ptr(nullptr)), + density_for_drawing_(density_for_drawing) {} DensityField* TrackedSpecie::densityField() { return density_field_.get(); @@ -75,6 +77,10 @@ DynamicDensityField* TrackedSpecie::lastSyncDensityField() { return last_sync_density_field_.get(); } +DynamicDensityField* TrackedSpecie::densityForDrawing() { + return density_for_drawing_.get(); +} + Mover* TrackedSpecie::mover() { return mover_.get(); }