Electrons are explicitly stored between timesteps

and can be dumped using MPI-IO.
Restarts read particles from earlier output.
This commit is contained in:
2025-08-11 09:18:51 +02:00
parent 0dd65d38f3
commit 76a93f5aa4
16 changed files with 781 additions and 81 deletions
+59 -2
View File
@@ -18,6 +18,43 @@ Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
}
*/
// requires the name of a species "spec_name" to be smaller than 16 characters
// this is not! checked here and will lead to undefined behaviour if this
// requirement is not fulfilled
SerializedParticle::SerializedParticle(double time_birth, double weight,
blitz::TinyVector<double, 3> cartPos,
blitz::TinyVector<double, 3> cartVel,
std::string spec_name) :
time_birth_(time_birth), weight_(weight),
cartesian_position_(cartPos),
cartesian_velocity_(cartVel) {
std::copy(spec_name.begin(), spec_name.end(), specie_name_.data());
specie_name_[spec_name.length()] = '\0';
}
double SerializedParticle::timeBirth() {
return time_birth_;
}
double SerializedParticle::weight() {
return weight_;
}
blitz::TinyVector<double, 3> SerializedParticle::cartesianPosition() {
return cartesian_position_;
}
blitz::TinyVector<double, 3> SerializedParticle::cartesianVelocity() {
return cartesian_velocity_;
}
std::string SerializedParticle::specieName() {
return std::string (specie_name_.data());
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
const Coordinate& position,
const Vector& velocity,
@@ -34,6 +71,20 @@ Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
last_id_++;
}
Particle::Particle(particle::SerializedParticle sparticle, std::map<std::string,
std::shared_ptr<species::TrackedSpecie>>& tracked_species) :
specie_(tracked_species[sparticle.specieName()]),
position_cart_(sparticle.cartesianPosition()),
position_cyl_(blitz::TinyVector<double, 3>(0,0,0)),
velocity_(sparticle.cartesianVelocity()),
id_(this->last_id_),
time_alive_s_(sparticle.timeBirth()),
interaction_counter_(0),
weight_(sparticle.weight()) {
last_id_++;
position_cyl_ = CylinderCoordinate(position_cart_);
}
long int Particle::last_id_ = 0;
double Particle::mass_kg() const {
@@ -115,12 +166,18 @@ double Particle::weight() const {
return weight_;
}
void Particle::deposited() {
deposited_ = true;
void Particle::deposited(bool truthValue) {
deposited_ = truthValue;
}
bool Particle::wasDeposited() {
return deposited_;
}
particle::SerializedParticle Particle::serialize() {
return particle::SerializedParticle(time_alive_s_, weight_,
position_cart_.cartesianEntries(), velocity_.cartesianEntries(),
specie_->name());
}
} // namespace particle
+71 -58
View File
@@ -46,14 +46,25 @@ Simulation::Simulation(std::string parameter_filename)
}
void Simulation::init(Parameter& parameter) {
// reserve some size for the different particle queues
queue_.reserve(100);
storage_queue_[0].reserve(1000);
storage_queue_[1].reserve(1000);
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();
}
#ifdef debug
std::stringstream out;
out << "Size of a Particle: " << sizeof(particle::Particle);
out << "\nSize of a Serialized Particle: " << sizeof(particle::SerializedParticle);
DEBUG_LEVEL_0(out.str());
#endif
// 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);
@@ -97,7 +108,8 @@ void Simulation::init(Parameter& parameter) {
#endif
Restart restarter = generator::buildRestart(parameter);
restarter.restart(tracked_species_, poisson_solver_.get(), random_.get());
restarter.restart(tracked_species_, poisson_solver_.get(), random_.get(),
storage_queue_[active_storage]);
// poisson solver is restarted -> set the electric field from the potential
electric_field_->updateDynamicField(poisson_solver_->staticElectricField());
@@ -208,10 +220,6 @@ void Simulation::start() {
int num_cpu_syncs = 0;
bool reset = true;
queue_.reserve(100);
storage_queue_[0].reserve(1000);
storage_queue_[1].reserve(1000);
timer_->increaseTimestep(delta_t);
while (timestep < number_timesteps) {
@@ -245,7 +253,7 @@ void Simulation::start() {
processEvents(events_of_cycle, beta_particle_weight);
#ifdef debug_level1
DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timesteps));
DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timestep));
DEBUG_LEVEL_1("Leftover events this timestep: " +
std::to_string(leftover_events_time_resolution));
#endif
@@ -276,8 +284,9 @@ void Simulation::start() {
SIM_INFO("Perform output.");
SimulationTimer::eventStartPoint(std::string("Output"));
// only write inactive queue, obvi not correct if written between timesteps
output_->writeSimulationData(num_cpu_syncs, tracked_species_, poisson_solver_.get(),
random_->engine());
random_->engine(), storage_queue_[inactive_storage]);
SimulationTimer::eventStopPoint(std::string("Output"));
SimulationTimer::eventStartPoint("Reset");
@@ -359,19 +368,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();
// 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();
}
// 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
@@ -448,19 +457,19 @@ 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();
// 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();
}
// 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) {
@@ -553,15 +562,6 @@ 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);
#ifdef debug
if(!particle.wasDeposited()) {
std::stringstream out;
@@ -571,6 +571,15 @@ void Simulation::processUntilTermination(
ERROR(out.str());
}
#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.serialize());
}
if(particle.specie()->deletionLogger()) {
particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(),
DeletionType::max_time_alive);
}
break;
} else if (particle.specie()->terminator()->leftSimulationDomain(
@@ -597,6 +606,8 @@ void Simulation::processUntilTermination(
std::stringstream out;
out << "particle " << particle.specie()->name();
out << " left simulation domain but still contributes to the density";
out << "\nposition: " << particle.cylinderPosition().cylinderEntries();
out << "\nvelocity: " << particle.velocity().cartesianEntries();
ERROR(out.str());
}
#endif
@@ -623,17 +634,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();
// 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();
}
// 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
@@ -802,8 +813,10 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
all_bins_done = false;
}
// take the first particle from the storage queue and extract it
particle::Particle new_particle = *(storage_queue_[active_storage].begin());
particle::SerializedParticle new_sparticle =
*(storage_queue_[active_storage].begin());
storage_queue_[active_storage].erase(storage_queue_[active_storage].begin());
particle::Particle new_particle(new_sparticle, tracked_species_);
// "draw" the actual particle
new_particles.push_back(new_particle);
continue;