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
+6 -3
View File
@@ -43,12 +43,15 @@ class Simulation {
std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random);
std::vector<particle::Particle>
determineInjectedDensityParticles(RandomNumbers* random,
const int numberParticlesToDraw, const int current);
const int numberParticlesToDraw, const int current,
std::map<std::string, double>& particleNumbers);
double meanFreePath(particle::Particle& particle, RandomNumbers* random);
double meanFreePath(particle::Particle& particle, RandomNumbers* random,
std::vector<double>& macroscopic_cross_sections_m);
interaction::InteractionOutput interact(particle::Particle& particle,
RandomNumbers* random);
RandomNumbers* random,
std::vector<double>& macroscopic_cross_sections_m);
blitz::Array<double,3> chargedensity();
+1 -1
View File
@@ -130,7 +130,7 @@ PitchAngleBinning buildPitchAngleBinning(json_t* object) {
return PitchAngleBinning(number_of_bins);
} else {
ERROR("This pitch angle binning is not implemented.");
ERROR("This pitch angle binning is not implemented: " + pitch_angle_type);
return PitchAngleBinning(0);
}
}
+9 -2
View File
@@ -11,8 +11,15 @@ std::shared_ptr<GravityField> gravityField(json_t* object) {
}
std::shared_ptr<GravityField> gravityField(Parameter& parameter) {
return gravityField(
json_object_by_path(parameter.jsonFile(), {"GravityField"}));
if(json_object_by_path(parameter.jsonFile(), {"GravityField"})) {
return gravityField(
json_object_by_path(parameter.jsonFile(), {"GravityField"}));
} else {
SIM_INFO("No \"GravityField\" object in the json file. Use default of (0,0,0).");
return std::shared_ptr<GravityField>( new GravityField(
std::shared_ptr<VectorField>( new VectorFieldConstant(
CylinderVector(blitz::TinyVector<double, 3> (0,0,0))))));
}
}
};
+22 -6
View File
@@ -6,6 +6,8 @@
#include "simulation/scalar_field.h"
#include "simulation/kinematic.h"
#include "tools/simulation_time.h"
template <class T>
blitz::Array<T, 4> initiate4DArray(blitz::Range radial,
blitz::Range phi,
@@ -238,8 +240,16 @@ blitz::Array<double, 3> DynamicDensityField::densityArray() {
DEBUG_LEVEL_3("recalc Density array");
recalculateDensityArray();
blitz::Array<double, 3> density(density_per_m3_(
blitz::Range::all(), blitz::Range::all(), blitz::Range::all(), 0));
// blitz::Array<double, 3> density(density_per_m3_(
// blitz::Range::all(), blitz::Range::all(), blitz::Range::all(), 0));
blitz::TinyVector<int,3> lowerbound(density_per_m3_.lbound(0),
density_per_m3_.lbound(1),
density_per_m3_.lbound(2));
blitz::TinyVector<int,3> extent(density_per_m3_.extent(0),
density_per_m3_.extent(1),
density_per_m3_.extent(2));
blitz::Array<double, 3> density(lowerbound, extent);
blitz::fourthIndex iE;
DEBUG_LEVEL_3("density sum");
density = blitz::sum(density_per_m3_, iE);
@@ -284,6 +294,7 @@ double DynamicDensityField::timeResolution() const {
}
void DynamicDensityField::recalculateDensityArray() {
// SimulationTimer::eventStartPoint("recalcDensityArray");
blitz::firstIndex ir;
blitz::secondIndex iphi;
blitz::thirdIndex iz;
@@ -300,6 +311,7 @@ void DynamicDensityField::recalculateDensityArray() {
density_per_m3_ = blitz::sum((time_in_bin_s_(ir, iphi, iz, iE, ipitch) / t -
deleted_particles_(ir, iphi, iz, iE, ipitch)), iE) /
bin_volume_m3_(ir, iphi, iz);
// SimulationTimer::eventStopPoint("recalcDensityArray");
}
void DynamicDensityField::setDensityDistribution() {
@@ -344,7 +356,7 @@ void DynamicDensityField::initPositionDistributions() {
// density distributions...
blitz::TinyVector<double, 5> DynamicDensityField::
draw(std::mt19937* engine) {
recalculateDensityArray();
// recalculateDensityArray();
if (isEqual(blitz::max(density_per_m3_), 0.)) {
DEBUG_LEVEL_3("Density is empty");
return blitz::TinyVector<double, 5> (0,0,0,0,0);
@@ -352,7 +364,7 @@ blitz::TinyVector<double, 5> DynamicDensityField::
// setDensityDistribution();
double random_r, random_z, random_phi, random_E, random_n;
double density_in_random_cell;
// SimulationTimer::eventStartPoint("DenDraw rejection");
do {
DEBUG_LEVEL_3("Missed.");
random_r = std::sqrt(r_dist_(*engine));
@@ -366,11 +378,14 @@ blitz::TinyVector<double, 5> DynamicDensityField::
DEBUG_LEVEL_3("drawn density: " + std::to_string(random_n));
} while (random_n >= density_in_random_cell);
// SimulationTimer::eventStopPoint("DenDraw rejection");
const blitz::TinyVector<int, 3> position_bin =
position_binning_.bin(CylinderCoordinate(
blitz::TinyVector<double, 3> (random_r, random_phi, random_z))
.cylinderEntries());
std::vector<double> energyBins = energy_binning_.binBorders();
energyBins.insert(energyBins.begin(), 0.);
blitz::Array<double, 1> densityAtPosition = density_per_m3_(position_bin(0),
@@ -381,8 +396,9 @@ blitz::TinyVector<double, 5> DynamicDensityField::
std::piecewise_constant_distribution<double> E_dist_(energyBins.begin(),
energyBins.end(),
densityAtPosition.begin());
random_E = E_dist_(*engine);
random_E = E_dist_(*engine);
DEBUG_LEVEL_3("energy: " + std::to_string(random_E));
const int energy_bin = energy_binning_.bin(random_E);
+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;