diff --git a/include/simulation/simulation.h b/include/simulation/simulation.h index 268ec7b..317a308 100755 --- a/include/simulation/simulation.h +++ b/include/simulation/simulation.h @@ -90,6 +90,12 @@ class Simulation { std::shared_ptr cpu_synchronizer_; std::vector queue_; + std::vector storage_queue_[2]; + + int active_storage = 0; + int inactive_storage = 1; + + int timestep = 0; #ifdef debug double phyLossAtWallIon = 0; diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 58d3687..6ade5c9 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -38,12 +38,23 @@ #include "tools/simulation_time.h" +#include + Simulation::Simulation(std::string parameter_filename) : parameter_(parameter_filename) { init(parameter_); } void Simulation::init(Parameter& parameter) { + + if(MpiCommunication::mpiSize() == 1) { + std::ofstream file; + file.open("/home/christian/Documents/Heidelberg/karl/test_cases/thermalization/data.txt", + std::ios_base::out); + file << "#type(0:drawing,1:interaction)\tx\ty\tz\tvx\tvy\tvz\n"; + file.close(); + } + magnetic_field_ = generator::magneticField(parameter); electric_field_ = generator::electricField(parameter); gravity_field_ = generator::gravityField(parameter); @@ -196,13 +207,14 @@ void Simulation::start() { int num_cpu_syncs = 0; bool reset = true; - int timesteps = 0; queue_.reserve(100); + storage_queue_[0].reserve(1000); + storage_queue_[1].reserve(1000); timer_->increaseTimestep(delta_t); - while (timesteps < number_timesteps) { + while (timestep < number_timesteps) { #if defined debug_level3 std::stringstream out; out << "Timeinterval: " << timer_->time() << "s."; @@ -221,7 +233,7 @@ void Simulation::start() { std::to_string(density_events_of_cycle)); #endif - SIM_INFO("Processing timestep: " + std::to_string(timesteps)); + SIM_INFO("Processing timestep: " + std::to_string(timestep)); SIM_INFO("Leftover events this timestep this CPU: " + std::to_string(leftover_events_time_resolution)); SIM_INFO("Leftover density events this timestep this CPU: " + @@ -229,7 +241,8 @@ void Simulation::start() { // process both density and beta events before a cpu synchronization processDensityEvents(particles_per_bin, density_weight_factor); - processEvents(events_of_cycle, beta_particle_weight); + // if(timesteps == 0) + processEvents(events_of_cycle, beta_particle_weight); #ifdef debug_level1 DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timesteps)); @@ -270,6 +283,10 @@ void Simulation::start() { SimulationTimer::eventStartPoint("Reset"); if(reset) { SIM_INFO("Do resets and increase the timestep for timeresolution."); + // SIM_INFO("Number of particles in storage queue: " + std::to_string(storage_queue_[active_storage].size())); + // SIM_INFO("Number of particles in storage queue: " + std::to_string(storage_queue_[inactive_storage].size())); + active_storage = (active_storage + 1) % 2; + inactive_storage = (inactive_storage + 1) % 2; #ifdef debug std::stringstream out; out << "Ions (T+) lost at wall (physical value): " << phyLossAtWallIon; @@ -296,9 +313,9 @@ void Simulation::start() { } #ifdef debug out << "\n\ttotal number in tp: " << total_tp; - out << "\n\tdiff to expected: " << (timesteps+1)*0.02 - total_tp; + out << "\n\tdiff to expected: " << (timestep+1)*0.02 - total_tp; out << "\n\ttotal number created: " << createdDensity; - out << "\n\tdiff to expected: " << (timesteps)*0.02/MpiCommunication::mpiSize() + out << "\n\tdiff to expected: " << (timestep)*0.02/MpiCommunication::mpiSize() - createdDensity; out << "\n\ttotal number in density drawing field: " << total_tp_sync; out << "\n\tdiff to in density: " << total_tp_sync - total_tp; @@ -310,7 +327,7 @@ void Simulation::start() { leftover_density_events = particles_per_bin; timer_->increaseTimestep(delta_t); - timesteps++; + timestep++; } SimulationTimer::eventStopPoint("Reset"); } @@ -342,6 +359,19 @@ void Simulation::processEvents(int events, const double beta_particle_weight) { SimulationTimer::eventStartPoint(std::string("ParticleInjection")); new_particles = determineInjectedParticles(random_.get(), beta_particle_weight); + if(MpiCommunication::mpiSize() == 1) { + std::ofstream file; + file.open("/home/christian/Documents/Heidelberg/karl/test_cases/thermalization/data.txt", + std::ios_base::app); + for(auto part: new_particles) { + auto pos = part.cartesianPosition().cartesianEntries(); + auto vel = part.velocity().cartesianEntries(); + + file << 0 << "," << pos(0) << ',' << pos(1) << ',' << pos(2) << ','; + file << vel(0) << "," << vel(1) << ',' << vel(2) << '\n'; + } + file.close(); + } // 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 @@ -388,6 +418,8 @@ void Simulation::processDensityEvents(int particles_per_bin, const double weight int current_event = 0; // set to true if all grids of all species have been visited bool all_bins_done = false; + // SIM_INFO("Number of particles in storage queue: " + std::to_string(storage_queue_[active_storage].size())); + // SIM_INFO("Number of particles in storage queue: " + std::to_string(storage_queue_[inactive_storage].size())); // calculate the number of particles currently contained within the densities // of the different tracked species before injection in order to avoid @@ -416,6 +448,20 @@ void Simulation::processDensityEvents(int particles_per_bin, const double weight all_bins_done); SimulationTimer::eventStopPoint(std::string("DensityInjection")); + if(MpiCommunication::mpiSize() == 1) { + std::ofstream file; + file.open("/home/christian/Documents/Heidelberg/karl/test_cases/thermalization/data.txt", + std::ios_base::app); + for(auto part: new_particles) { + auto pos = part.cartesianPosition().cartesianEntries(); + auto vel = part.velocity().cartesianEntries(); + + file << 0 << "," << pos(0) << ',' << pos(1) << ',' << pos(2) << ','; + file << vel(0) << "," << vel(1) << ',' << vel(2) << '\n'; + } + file.close(); + } + #if defined debug_level3 for(auto& particle : new_particles) { std::stringstream output_ss; @@ -433,6 +479,9 @@ void Simulation::processDensityEvents(int particles_per_bin, const double weight if(all_bins_done) { current_event += 1; for(auto& element : tracked_species_) { + if(element.second->type() == species::ImplementedSpecies::electron) { + continue; + } element.second->randomIndex()->reshuffle(random_->engine()); } } @@ -462,6 +511,7 @@ void Simulation::processDensityEvents(int particles_per_bin, const double weight void Simulation::processUntilTermination( particle::Particle& particle) { + // SIM_INFO("size of a particle: " + std::to_string(sizeof(particle))); #ifdef debug_level3 DEBUG_LEVEL_3("Process particle until termination"); DEBUG_LEVEL_3("Weight of the particle: " + std::to_string(particle.weight())); @@ -503,6 +553,12 @@ void Simulation::processUntilTermination( DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() + ". Reached timeresolution."); #endif + // particle terminated due to reaching time resolution alone -> needs + // to be stored (if it's an electron) + if(particle.specie()->type() == species::ImplementedSpecies::electron) { + storage_queue_[inactive_storage].push_back(particle); + } + if(particle.specie()->deletionLogger()) { particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(), DeletionType::max_time_alive); @@ -567,6 +623,17 @@ void Simulation::processUntilTermination( SimulationTimer::eventStartPoint(std::string("Interaction")); interaction::InteractionOutput output = interact(particle, random_.get());//, // macroscopic_cross_sections_m); + if(MpiCommunication::mpiSize() == 1) { + std::ofstream file; + file.open("/home/christian/Documents/Heidelberg/karl/test_cases/thermalization/data.txt", + std::ios_base::app); + auto pos = particle.cartesianPosition().cartesianEntries(); + auto vel = particle.velocity().cartesianEntries(); + + file << 1 << "," << pos(0) << ',' << pos(1) << ',' << pos(2) << ','; + file << vel(0) << "," << vel(1) << ',' << vel(2) << '\n'; + file.close(); + } SimulationTimer::eventStopPoint(std::string("Interaction")); // particle has reached time resolution, but was not deposited due to @@ -584,7 +651,9 @@ void Simulation::processUntilTermination( // } // } - particle.increaseInteractionCounter(); + + // remove? + // particle.increaseInteractionCounter(); for (auto& new_particle : output.newParticles()) { queue_.push_back(new_particle); @@ -598,6 +667,8 @@ void Simulation::processUntilTermination( } if (particle.specie()->terminator()->reachedMaxInteractions(&particle)) { + // don't save particles that terminate due to the maximum interactions + // for now (might have to be done in the future though) #ifdef debug_level3 DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() + ". Reached max interactions."); @@ -721,6 +792,23 @@ std::vector Simulation::determineInjectedDensityParticles( for(auto& pair: tracked_species_) { + if(pair.second->type() == species::ImplementedSpecies::electron) { + // if the storage queue does not contain any more particles + // no new particles can be drawn for the electrons + if(storage_queue_[active_storage].empty()) { + all_bins_done = all_bins_done && true; + continue; + } else { + all_bins_done = false; + } + // take the first particle from the storage queue and extract it + particle::Particle new_particle = *(storage_queue_[active_storage].begin()); + storage_queue_[active_storage].erase(storage_queue_[active_storage].begin()); + // "draw" the actual particle + new_particles.push_back(new_particle); + continue; + } + tmp_done = pair.second->randomIndex()->visited_all(); if (particleNumbers[pair.first] == 0) {