From 641e26c10b2a4a0c0944628a5c2e83a1dd6b74db Mon Sep 17 00:00:00 2001 From: csendlinger Date: Tue, 9 Sep 2025 10:49:57 +0200 Subject: [PATCH] Added option whether particles should be stored explicitly --- include/generators/species_generator.h | 2 + include/simulation/simulation.h | 6 +-- include/simulation/species.h | 8 +++ src/generators/species_generator.cpp | 14 +++++ src/simulation/simulation.cpp | 75 +++++++++++++------------- src/simulation/species.cpp | 8 ++- 6 files changed, 70 insertions(+), 43 deletions(-) diff --git a/include/generators/species_generator.h b/include/generators/species_generator.h index 62d44b4..e2b534c 100755 --- a/include/generators/species_generator.h +++ b/include/generators/species_generator.h @@ -28,6 +28,7 @@ struct TrackedSpeciesBuilderInfo { double mass_kg; double charge_C; + bool explicitly_stored; species::ImplementedSpecies type; std::string name; json_path path_in_json; @@ -109,6 +110,7 @@ class SpeciesBuilder { void fillTerminator(Parameter& parameter); void fillReflector(Parameter& parameter); void fillTrackedDensity(Parameter& parameter); + void fillDensityDrawingType(Parameter& parameter); void initTimer(); void buildSpecies(); diff --git a/include/simulation/simulation.h b/include/simulation/simulation.h index 6ad2145..4239c5c 100755 --- a/include/simulation/simulation.h +++ b/include/simulation/simulation.h @@ -51,14 +51,14 @@ class Simulation { std::map& particleNumbers, bool& all_bins_done); - double meanFreePath(particle::Particle& particle, RandomNumbers* random, - std::vector& macroscopic_cross_sections_m); + // double meanFreePath(particle::Particle& particle, RandomNumbers* random, + // std::vector& macroscopic_cross_sections_m); // draw the individual collision times of the possible interactions and return the // minimum of them. Also sets the "nextInteractionIndex" in the particle to the // interaction of the chosen time. - double collisionTime(particle::Particle& particle, RandomNumbers* random); + void collisionTime(particle::Particle& particle, RandomNumbers* random); interaction::InteractionOutput interact(particle::Particle& particle, RandomNumbers* random); diff --git a/include/simulation/species.h b/include/simulation/species.h index 5636493..561eaac 100755 --- a/include/simulation/species.h +++ b/include/simulation/species.h @@ -59,6 +59,7 @@ class TrackedSpecie : public Specie { std::string name, double mass_kg, double charge_C, + bool explicitly_stored, std::shared_ptr mover, std::shared_ptr terminator, std::shared_ptr reflector, @@ -95,6 +96,8 @@ class TrackedSpecie : public Specie { // getter for the raw pointer to the RandomIndex object RandomIndex* randomIndex(); + bool explicitlyStored(); + private: std::shared_ptr density_field_; std::shared_ptr velocity_distribution_; @@ -115,6 +118,11 @@ class TrackedSpecie : public Specie { velocity_distribution_from_density_; std::shared_ptr random_index_; + + // should work if it changes from true -> false (after a restart) + // however, it should not work in the opposite case (in which the + // old density would be lost) + const bool explicitly_stored_; }; class BackgroundSpecie : public Specie { diff --git a/src/generators/species_generator.cpp b/src/generators/species_generator.cpp index 7b7b7f6..535433c 100755 --- a/src/generators/species_generator.cpp +++ b/src/generators/species_generator.cpp @@ -82,6 +82,7 @@ SpeciesBuilder::SpeciesBuilder(Parameter& parameter) { fillTerminator(parameter); fillReflector(parameter); fillTrackedDensity(parameter); + fillDensityDrawingType(parameter); buildSpecies(); fillInteractions(parameter); @@ -306,6 +307,13 @@ void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) { } } +void SpeciesBuilder::fillDensityDrawingType(Parameter& parameter) { + for(auto& b : tracked_species_info) { + b.second.explicitly_stored = parameter.boolValue( + json_path_append(b.second.path_in_json, {"StoreInQueue"}), false); + } +} + void SpeciesBuilder::buildSpecies() { #ifdef debug_level2 DEBUG_LEVEL_2("Build species from info."); @@ -319,9 +327,15 @@ void SpeciesBuilder::buildSpecies() { } for (auto& b : tracked_species_info) { +#ifdef debug + std::stringstream out; + out << b.second.name << ": stored in queue? " << (b.second.explicitly_stored ? "true": "false"); + DEBUG_LEVEL_1(out.str()); +#endif tracked_species[b.first] = std::shared_ptr(new species::TrackedSpecie( b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C, + b.second.explicitly_stored, b.second.mover, b.second.terminator, b.second.reflector, b.second.density_field, b.second.velocity_distribution, b.second.drift_velocity, b.second.density_drawing_field)); diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index d23fdee..b973be1 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -318,7 +318,7 @@ void Simulation::start() { SimulationTimer::eventStartPoint("Reset"); if(reset) { -#if defined(debug_level2) +#if defined(debug_level2) || defined(debug_level3) DEBUG_LEVEL_2("Number of particles in active storage queue: " + std::to_string(storage_queue_[active_storage].size())); DEBUG_LEVEL_2("Number of particles in inactive storage queue: " @@ -461,12 +461,11 @@ void Simulation::processDensityEvents(int particles_per_bin, const double weight #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) - + // (drawing field only changes after timesteps) if(all_bins_done) { current_event += 1; for(auto& element : tracked_species_) { - if(element.second->type() == species::ImplementedSpecies::electron) { + if(element.second->explicitlyStored()) { continue; } element.second->randomIndex()->reshuffle(random_->engine()); @@ -496,8 +495,7 @@ void Simulation::processDensityEvents(int particles_per_bin, const double weight } } -void Simulation::processUntilTermination( - particle::Particle& particle) { +void Simulation::processUntilTermination(particle::Particle& particle) { #ifdef debug_level3 DEBUG_LEVEL_3("Process particle until termination"); DEBUG_LEVEL_3("Weight of the particle: " + std::to_string(particle.weight())); @@ -507,13 +505,13 @@ void Simulation::processUntilTermination( double old_energy = particle.kineticEnergy_eV(); if(!particle.isNextInteractionValid()) { - DEBUG_LEVEL_1("Calculate time until collision"); - SimulationTimer::eventStartPoint(std::string("MFP calculation")); - const double collision_time = collisionTime(particle, random_.get()); - SimulationTimer::eventStopPoint(std::string("MFP calculation")); + // DEBUG_LEVEL_1("Calculate time until collision"); + SimulationTimer::eventStartPoint(std::string("CollTime calculation")); + collisionTime(particle, random_.get()); + SimulationTimer::eventStopPoint(std::string("CollTime calculation")); } -#ifdef debug +#ifdef debug_level3 std::stringstream out_; out_ << "Time until collision: " << particle.timeUntilCollision(); DEBUG_LEVEL_1(out_.str()); @@ -559,7 +557,7 @@ void Simulation::processUntilTermination( #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) { + if(particle.specie()->explicitlyStored()) { storage_queue_[inactive_storage].push_back(particle.serialize()); } @@ -660,38 +658,38 @@ void Simulation::processUntilTermination( particle.cylinderPosition(), DeletionType::max_interactions); } break; - } + } } particle.invalidateNextInteraction(); } } -double Simulation::meanFreePath(particle::Particle& particle, - RandomNumbers* random, - std::vector& macroscopic_cross_sections_m) { - double total_macroscopic_cross_section_m = 0.; - for (auto& interaction : particle.specie()->interactions()) { - double cross_section_tmp = interaction->macroscopicCrossSection(particle); - total_macroscopic_cross_section_m += cross_section_tmp; - macroscopic_cross_sections_m.push_back(cross_section_tmp); - } +// double Simulation::meanFreePath(particle::Particle& particle, +// RandomNumbers* random, +// std::vector& macroscopic_cross_sections_m) { +// double total_macroscopic_cross_section_m = 0.; +// for (auto& interaction : particle.specie()->interactions()) { +// double cross_section_tmp = interaction->macroscopicCrossSection(particle); +// total_macroscopic_cross_section_m += cross_section_tmp; +// macroscopic_cross_sections_m.push_back(cross_section_tmp); +// } - if (total_macroscopic_cross_section_m <= 0.) { - return std::numeric_limits::max(); - } else { - const double rnd_number = random->rollUniform(0., 1.); - if (rnd_number <= 0) { - return std::numeric_limits::max(); - } else { - return 1. / total_macroscopic_cross_section_m * log(1. / rnd_number); - } - } -} +// if (total_macroscopic_cross_section_m <= 0.) { +// return std::numeric_limits::max(); +// } else { +// const double rnd_number = random->rollUniform(0., 1.); +// if (rnd_number <= 0) { +// return std::numeric_limits::max(); +// } else { +// return 1. / total_macroscopic_cross_section_m * log(1. / rnd_number); +// } +// } +// } // maybe rewrite to s.t. the min_element part can be removed (together with the // algorithm lib) -double Simulation::collisionTime(particle::Particle& particle, RandomNumbers* random) { +void Simulation::collisionTime(particle::Particle& particle, RandomNumbers* random) { std::vector collision_times; double tmp; @@ -718,8 +716,6 @@ double Simulation::collisionTime(particle::Particle& particle, RandomNumbers* ra DEBUG_LEVEL_3(out.str()); #endif - - return *min_iterator; } interaction::InteractionOutput Simulation::interact( @@ -774,10 +770,11 @@ std::vector Simulation::determineInjectedDensityParticles( all_bins_done = true; for(auto& pair: tracked_species_) { - - if(pair.second->type() == species::ImplementedSpecies::electron) { +// Note: takes the next particle in the storage queue regardless if +// that particle is a member of the current tracked_specie + if(pair.second->explicitlyStored()) { // if the storage queue does not contain any more particles - // no new particles can be drawn for the electrons + // no new particles can be drawn if(storage_queue_[active_storage].empty()) { all_bins_done = all_bins_done && true; continue; diff --git a/src/simulation/species.cpp b/src/simulation/species.cpp index 8f17aac..449bdee 100755 --- a/src/simulation/species.cpp +++ b/src/simulation/species.cpp @@ -38,6 +38,7 @@ TrackedSpecie::TrackedSpecie( std::string name, double mass_kg, double charge_C, + bool explicitly_stored, std::shared_ptr mover, std::shared_ptr terminator, std::shared_ptr reflector, @@ -59,7 +60,8 @@ TrackedSpecie::TrackedSpecie( std::make_shared(density_for_drawing)), velocity_distribution_from_density_( std::make_shared( - density_for_drawing, mass_kg)) { + density_for_drawing, mass_kg)), + explicitly_stored_(explicitly_stored) { blitz::TinyVector num_bins; auto tmp = density_field->positionBinning()->numBins(); num_bins(0) = tmp(0); num_bins(1) = tmp(1); num_bins(2) = tmp(2); @@ -147,6 +149,10 @@ void TrackedSpecie::currentCollector( current_collector_ = new_collector; } +bool TrackedSpecie::explicitlyStored() { + return explicitly_stored_; +} + BackgroundSpecie::BackgroundSpecie( ImplementedSpecies type, std::string name,