Some optimizations (mfp: vector instead of recalc,

reduced number of density recalculations);
Default gravity field of 0;
adjusted error output
This commit is contained in:
2025-01-29 16:59:10 +01:00
parent 5dffc0c102
commit b529021149
5 changed files with 74 additions and 35 deletions
+36 -23
View File
@@ -265,6 +265,16 @@ 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;
// calculate the number of particles currently contained within the densities
// of the different tracked species before injection in order to avoid
// the expensive recalculateDensityField funtion being calculated for every
// new injection event
std::map<std::string, double> particleNumbers;
for(auto& pair: tracked_species_) {
particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber());
}
while ((current_event < events) || (!queue_.empty())) {
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
@@ -298,7 +308,8 @@ void Simulation::processEvents(int events, int density_events, int cpu_syncs) {
//maybe use a different event name?
SimulationTimer::eventStartPoint(std::string("DensityParticleInjection"));
new_particles = determineInjectedDensityParticles(random_.get(), density_events,
current_density_event);
current_density_event,
particleNumbers);
SimulationTimer::eventStopPoint(std::string("DensityParticleInjection"));
#if defined debug_level3
for(auto& particle : new_particles) {
@@ -310,6 +321,12 @@ void Simulation::processEvents(int events, int density_events, int cpu_syncs) {
DEBUG_LEVEL_3(output_ss.str());
}
#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)
if(new_particles.empty()) {
current_density_event = density_events-1;
}
current_density_event += 1;
}
if(new_particles.empty())
@@ -340,8 +357,11 @@ void Simulation::processUntilTermination(
auto old_position = particle.position();
double old_energy = particle.kineticEnergy_eV();
std::vector<double> macroscopic_cross_sections_m;
SimulationTimer::eventStartPoint(std::string("MFP calculation"));
const double mfp_m = meanFreePath(particle, random_.get());
const double mfp_m = meanFreePath(particle, random_.get(),
macroscopic_cross_sections_m);
SimulationTimer::eventStopPoint(std::string("MFP calculation"));
SimulationTimer::eventStartPoint(std::string("Mover"));
@@ -399,7 +419,8 @@ void Simulation::processUntilTermination(
DEBUG_LEVEL_3(output_ss.str());
SimulationTimer::eventStartPoint(std::string("Interaction"));
interaction::InteractionOutput output = interact(particle, random_.get());
interaction::InteractionOutput output = interact(particle, random_.get(),
macroscopic_cross_sections_m);
SimulationTimer::eventStopPoint(std::string("Interaction"));
particle.increaseInteractionCounter();
@@ -427,11 +448,13 @@ void Simulation::processUntilTermination(
}
double Simulation::meanFreePath(particle::Particle& particle,
RandomNumbers* random) {
RandomNumbers* random,
std::vector<double>& macroscopic_cross_sections_m) {
double total_macroscopic_cross_section_m = 0.;
for (auto& interaction : particle.specie()->interactions()) {
total_macroscopic_cross_section_m +=
interaction->macroscopicCrossSection(particle);
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.) {
@@ -448,12 +471,8 @@ double Simulation::meanFreePath(particle::Particle& particle,
interaction::InteractionOutput Simulation::interact(
particle::Particle& particle,
RandomNumbers* random) {
std::vector<double> macroscopic_cross_sections_m;
for (auto& interaction : particle.specie()->interactions()) {
macroscopic_cross_sections_m.push_back(
interaction->macroscopicCrossSection(particle));
}
RandomNumbers* random,
std::vector<double>& macroscopic_cross_sections_m) {
const double total_macroscopic_cross_section_m =
std::accumulate(macroscopic_cross_sections_m.begin(),
macroscopic_cross_sections_m.end(), 0.0);
@@ -480,21 +499,15 @@ std::vector<particle::Particle> Simulation::determineInjectedParticles(
}
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
RandomNumbers* random, const int numberParticlesToDraw, const int current) {
RandomNumbers* random, const int numberParticlesToDraw, const int current,
std::map<std::string, double>& particleNumbers) {
std::vector<particle::Particle> 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_) {
// not ideal, gets recalculated each time a new particle is drawn from the density,
// which does not change except after a timestep, but think the rejection method is
// much more time consuming anyways... might optimize in the future
// particle number of the denstiy, i.e. the integral over the density
const double particleNumberFromDensity =
pair.second->densityForDrawing()->particleNumber();
if (particleNumberFromDensity == 0) {
if (particleNumbers[pair.first] == 0) {
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
continue;
}
@@ -502,14 +515,14 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
std::stringstream out;
out << pair.second->name();
out << "particle number from density: ";
out << particleNumberFromDensity;
out << particleNumbers[pair.first];
DEBUG_LEVEL_3(out.str());
}
injector_->rollNewParticleFromDensityField(random->engine(),
pair.second,
new_particles,
magnetic_field_,
particleNumberFromDensity/numberParticlesToDraw/MpiCommunication::mpiSize(),
particleNumbers[pair.first]/numberParticlesToDraw/MpiCommunication::mpiSize(),
injection_time);
}
return new_particles;