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:
@@ -43,12 +43,15 @@ class Simulation {
|
|||||||
std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random);
|
std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random);
|
||||||
std::vector<particle::Particle>
|
std::vector<particle::Particle>
|
||||||
determineInjectedDensityParticles(RandomNumbers* random,
|
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,
|
interaction::InteractionOutput interact(particle::Particle& particle,
|
||||||
RandomNumbers* random);
|
RandomNumbers* random,
|
||||||
|
std::vector<double>& macroscopic_cross_sections_m);
|
||||||
|
|
||||||
blitz::Array<double,3> chargedensity();
|
blitz::Array<double,3> chargedensity();
|
||||||
|
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ PitchAngleBinning buildPitchAngleBinning(json_t* object) {
|
|||||||
return PitchAngleBinning(number_of_bins);
|
return PitchAngleBinning(number_of_bins);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ERROR("This pitch angle binning is not implemented.");
|
ERROR("This pitch angle binning is not implemented: " + pitch_angle_type);
|
||||||
return PitchAngleBinning(0);
|
return PitchAngleBinning(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,15 @@ std::shared_ptr<GravityField> gravityField(json_t* object) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<GravityField> gravityField(Parameter& parameter) {
|
std::shared_ptr<GravityField> gravityField(Parameter& parameter) {
|
||||||
|
if(json_object_by_path(parameter.jsonFile(), {"GravityField"})) {
|
||||||
return gravityField(
|
return gravityField(
|
||||||
json_object_by_path(parameter.jsonFile(), {"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))))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
#include "simulation/scalar_field.h"
|
#include "simulation/scalar_field.h"
|
||||||
#include "simulation/kinematic.h"
|
#include "simulation/kinematic.h"
|
||||||
|
|
||||||
|
#include "tools/simulation_time.h"
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
blitz::Array<T, 4> initiate4DArray(blitz::Range radial,
|
blitz::Array<T, 4> initiate4DArray(blitz::Range radial,
|
||||||
blitz::Range phi,
|
blitz::Range phi,
|
||||||
@@ -238,8 +240,16 @@ blitz::Array<double, 3> DynamicDensityField::densityArray() {
|
|||||||
DEBUG_LEVEL_3("recalc Density array");
|
DEBUG_LEVEL_3("recalc Density array");
|
||||||
recalculateDensityArray();
|
recalculateDensityArray();
|
||||||
|
|
||||||
blitz::Array<double, 3> density(density_per_m3_(
|
// blitz::Array<double, 3> density(density_per_m3_(
|
||||||
blitz::Range::all(), blitz::Range::all(), blitz::Range::all(), 0));
|
// 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;
|
blitz::fourthIndex iE;
|
||||||
DEBUG_LEVEL_3("density sum");
|
DEBUG_LEVEL_3("density sum");
|
||||||
density = blitz::sum(density_per_m3_, iE);
|
density = blitz::sum(density_per_m3_, iE);
|
||||||
@@ -284,6 +294,7 @@ double DynamicDensityField::timeResolution() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DynamicDensityField::recalculateDensityArray() {
|
void DynamicDensityField::recalculateDensityArray() {
|
||||||
|
// SimulationTimer::eventStartPoint("recalcDensityArray");
|
||||||
blitz::firstIndex ir;
|
blitz::firstIndex ir;
|
||||||
blitz::secondIndex iphi;
|
blitz::secondIndex iphi;
|
||||||
blitz::thirdIndex iz;
|
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 -
|
density_per_m3_ = blitz::sum((time_in_bin_s_(ir, iphi, iz, iE, ipitch) / t -
|
||||||
deleted_particles_(ir, iphi, iz, iE, ipitch)), iE) /
|
deleted_particles_(ir, iphi, iz, iE, ipitch)), iE) /
|
||||||
bin_volume_m3_(ir, iphi, iz);
|
bin_volume_m3_(ir, iphi, iz);
|
||||||
|
// SimulationTimer::eventStopPoint("recalcDensityArray");
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicDensityField::setDensityDistribution() {
|
void DynamicDensityField::setDensityDistribution() {
|
||||||
@@ -344,7 +356,7 @@ void DynamicDensityField::initPositionDistributions() {
|
|||||||
// density distributions...
|
// density distributions...
|
||||||
blitz::TinyVector<double, 5> DynamicDensityField::
|
blitz::TinyVector<double, 5> DynamicDensityField::
|
||||||
draw(std::mt19937* engine) {
|
draw(std::mt19937* engine) {
|
||||||
recalculateDensityArray();
|
// recalculateDensityArray();
|
||||||
if (isEqual(blitz::max(density_per_m3_), 0.)) {
|
if (isEqual(blitz::max(density_per_m3_), 0.)) {
|
||||||
DEBUG_LEVEL_3("Density is empty");
|
DEBUG_LEVEL_3("Density is empty");
|
||||||
return blitz::TinyVector<double, 5> (0,0,0,0,0);
|
return blitz::TinyVector<double, 5> (0,0,0,0,0);
|
||||||
@@ -352,7 +364,7 @@ blitz::TinyVector<double, 5> DynamicDensityField::
|
|||||||
// setDensityDistribution();
|
// setDensityDistribution();
|
||||||
double random_r, random_z, random_phi, random_E, random_n;
|
double random_r, random_z, random_phi, random_E, random_n;
|
||||||
double density_in_random_cell;
|
double density_in_random_cell;
|
||||||
|
// SimulationTimer::eventStartPoint("DenDraw rejection");
|
||||||
do {
|
do {
|
||||||
DEBUG_LEVEL_3("Missed.");
|
DEBUG_LEVEL_3("Missed.");
|
||||||
random_r = std::sqrt(r_dist_(*engine));
|
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));
|
DEBUG_LEVEL_3("drawn density: " + std::to_string(random_n));
|
||||||
} while (random_n >= density_in_random_cell);
|
} while (random_n >= density_in_random_cell);
|
||||||
|
|
||||||
|
// SimulationTimer::eventStopPoint("DenDraw rejection");
|
||||||
|
|
||||||
const blitz::TinyVector<int, 3> position_bin =
|
const blitz::TinyVector<int, 3> position_bin =
|
||||||
position_binning_.bin(CylinderCoordinate(
|
position_binning_.bin(CylinderCoordinate(
|
||||||
blitz::TinyVector<double, 3> (random_r, random_phi, random_z))
|
blitz::TinyVector<double, 3> (random_r, random_phi, random_z))
|
||||||
.cylinderEntries());
|
.cylinderEntries());
|
||||||
|
|
||||||
|
|
||||||
std::vector<double> energyBins = energy_binning_.binBorders();
|
std::vector<double> energyBins = energy_binning_.binBorders();
|
||||||
energyBins.insert(energyBins.begin(), 0.);
|
energyBins.insert(energyBins.begin(), 0.);
|
||||||
blitz::Array<double, 1> densityAtPosition = density_per_m3_(position_bin(0),
|
blitz::Array<double, 1> densityAtPosition = density_per_m3_(position_bin(0),
|
||||||
@@ -383,6 +398,7 @@ blitz::TinyVector<double, 5> DynamicDensityField::
|
|||||||
densityAtPosition.begin());
|
densityAtPosition.begin());
|
||||||
|
|
||||||
random_E = E_dist_(*engine);
|
random_E = E_dist_(*engine);
|
||||||
|
|
||||||
DEBUG_LEVEL_3("energy: " + std::to_string(random_E));
|
DEBUG_LEVEL_3("energy: " + std::to_string(random_E));
|
||||||
|
|
||||||
const int energy_bin = energy_binning_.bin(random_E);
|
const int energy_bin = energy_binning_.bin(random_E);
|
||||||
|
|||||||
@@ -265,6 +265,16 @@ void Simulation::processEvents(int events, int density_events, int cpu_syncs) {
|
|||||||
DEBUG_LEVEL_0("Processing events.");
|
DEBUG_LEVEL_0("Processing events.");
|
||||||
int current_event = 0;
|
int current_event = 0;
|
||||||
int current_density_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())) {
|
while ((current_event < events) || (!queue_.empty())) {
|
||||||
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
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?
|
//maybe use a different event name?
|
||||||
SimulationTimer::eventStartPoint(std::string("DensityParticleInjection"));
|
SimulationTimer::eventStartPoint(std::string("DensityParticleInjection"));
|
||||||
new_particles = determineInjectedDensityParticles(random_.get(), density_events,
|
new_particles = determineInjectedDensityParticles(random_.get(), density_events,
|
||||||
current_density_event);
|
current_density_event,
|
||||||
|
particleNumbers);
|
||||||
SimulationTimer::eventStopPoint(std::string("DensityParticleInjection"));
|
SimulationTimer::eventStopPoint(std::string("DensityParticleInjection"));
|
||||||
#if defined debug_level3
|
#if defined debug_level3
|
||||||
for(auto& particle : new_particles) {
|
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());
|
DEBUG_LEVEL_3(output_ss.str());
|
||||||
}
|
}
|
||||||
#endif
|
#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;
|
current_density_event += 1;
|
||||||
}
|
}
|
||||||
if(new_particles.empty())
|
if(new_particles.empty())
|
||||||
@@ -340,8 +357,11 @@ void Simulation::processUntilTermination(
|
|||||||
auto old_position = particle.position();
|
auto old_position = particle.position();
|
||||||
double old_energy = particle.kineticEnergy_eV();
|
double old_energy = particle.kineticEnergy_eV();
|
||||||
|
|
||||||
|
std::vector<double> macroscopic_cross_sections_m;
|
||||||
|
|
||||||
SimulationTimer::eventStartPoint(std::string("MFP calculation"));
|
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::eventStopPoint(std::string("MFP calculation"));
|
||||||
|
|
||||||
SimulationTimer::eventStartPoint(std::string("Mover"));
|
SimulationTimer::eventStartPoint(std::string("Mover"));
|
||||||
@@ -399,7 +419,8 @@ void Simulation::processUntilTermination(
|
|||||||
DEBUG_LEVEL_3(output_ss.str());
|
DEBUG_LEVEL_3(output_ss.str());
|
||||||
|
|
||||||
SimulationTimer::eventStartPoint(std::string("Interaction"));
|
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"));
|
SimulationTimer::eventStopPoint(std::string("Interaction"));
|
||||||
|
|
||||||
particle.increaseInteractionCounter();
|
particle.increaseInteractionCounter();
|
||||||
@@ -427,11 +448,13 @@ void Simulation::processUntilTermination(
|
|||||||
}
|
}
|
||||||
|
|
||||||
double Simulation::meanFreePath(particle::Particle& particle,
|
double Simulation::meanFreePath(particle::Particle& particle,
|
||||||
RandomNumbers* random) {
|
RandomNumbers* random,
|
||||||
|
std::vector<double>& macroscopic_cross_sections_m) {
|
||||||
double total_macroscopic_cross_section_m = 0.;
|
double total_macroscopic_cross_section_m = 0.;
|
||||||
for (auto& interaction : particle.specie()->interactions()) {
|
for (auto& interaction : particle.specie()->interactions()) {
|
||||||
total_macroscopic_cross_section_m +=
|
double cross_section_tmp = interaction->macroscopicCrossSection(particle);
|
||||||
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.) {
|
if (total_macroscopic_cross_section_m <= 0.) {
|
||||||
@@ -448,12 +471,8 @@ double Simulation::meanFreePath(particle::Particle& particle,
|
|||||||
|
|
||||||
interaction::InteractionOutput Simulation::interact(
|
interaction::InteractionOutput Simulation::interact(
|
||||||
particle::Particle& particle,
|
particle::Particle& particle,
|
||||||
RandomNumbers* random) {
|
RandomNumbers* random,
|
||||||
std::vector<double> macroscopic_cross_sections_m;
|
std::vector<double>& macroscopic_cross_sections_m) {
|
||||||
for (auto& interaction : particle.specie()->interactions()) {
|
|
||||||
macroscopic_cross_sections_m.push_back(
|
|
||||||
interaction->macroscopicCrossSection(particle));
|
|
||||||
}
|
|
||||||
const double total_macroscopic_cross_section_m =
|
const double total_macroscopic_cross_section_m =
|
||||||
std::accumulate(macroscopic_cross_sections_m.begin(),
|
std::accumulate(macroscopic_cross_sections_m.begin(),
|
||||||
macroscopic_cross_sections_m.end(), 0.0);
|
macroscopic_cross_sections_m.end(), 0.0);
|
||||||
@@ -480,21 +499,15 @@ std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
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{};
|
std::vector<particle::Particle> new_particles{};
|
||||||
if(!new_particles.empty()) {
|
if(!new_particles.empty()) {
|
||||||
ERROR("In drawing particles from the density: the new particles vector is not empty at initialization");
|
ERROR("In drawing particles from the density: the new particles vector is not empty at initialization");
|
||||||
}
|
}
|
||||||
const double injection_time = timer_->time() - timer_->getDeltaT();
|
const double injection_time = timer_->time() - timer_->getDeltaT();
|
||||||
for(auto& pair: tracked_species_) {
|
for(auto& pair: tracked_species_) {
|
||||||
// not ideal, gets recalculated each time a new particle is drawn from the density,
|
if (particleNumbers[pair.first] == 0) {
|
||||||
// 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) {
|
|
||||||
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
|
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -502,14 +515,14 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
|||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
out << pair.second->name();
|
out << pair.second->name();
|
||||||
out << "particle number from density: ";
|
out << "particle number from density: ";
|
||||||
out << particleNumberFromDensity;
|
out << particleNumbers[pair.first];
|
||||||
DEBUG_LEVEL_3(out.str());
|
DEBUG_LEVEL_3(out.str());
|
||||||
}
|
}
|
||||||
injector_->rollNewParticleFromDensityField(random->engine(),
|
injector_->rollNewParticleFromDensityField(random->engine(),
|
||||||
pair.second,
|
pair.second,
|
||||||
new_particles,
|
new_particles,
|
||||||
magnetic_field_,
|
magnetic_field_,
|
||||||
particleNumberFromDensity/numberParticlesToDraw/MpiCommunication::mpiSize(),
|
particleNumbers[pair.first]/numberParticlesToDraw/MpiCommunication::mpiSize(),
|
||||||
injection_time);
|
injection_time);
|
||||||
}
|
}
|
||||||
return new_particles;
|
return new_particles;
|
||||||
|
|||||||
Reference in New Issue
Block a user