Fixed to state that drawing from density works.

Some debugging elements not removed.
This commit is contained in:
2024-08-12 14:20:45 +02:00
parent b57f6ff201
commit 9dda12b60a
13 changed files with 100 additions and 31 deletions
+11 -3
View File
@@ -65,7 +65,9 @@ void CpuSynchronizer::synchronize(Timer* timer, Timer* last_sync_timer) {
}
void CpuSynchronizer::synchronize(DynamicDensityField* field,
DynamicDensityField* last_sync_field) {
DynamicDensityField* last_sync_field,
DynamicDensityField* density_for_drawing,
bool reset) {
if (field && last_sync_field) {
if(field == last_sync_field) {
ERROR("The way last_sync_field is created does not work as intended.");
@@ -75,6 +77,11 @@ void CpuSynchronizer::synchronize(DynamicDensityField* field,
sumToAll(field->deleted_particles_, last_sync_field->deleted_particles_);
field->deleted_particles_ = last_sync_field->deleted_particles_;
if (reset) {
density_for_drawing->time_in_bin_s_ = last_sync_field->time_in_bin_s_;
density_for_drawing->deleted_particles_ =
last_sync_field->deleted_particles_;
}
}
}
@@ -130,7 +137,7 @@ void CpuSynchronizer::synchronize(DeletionLogger* logger,
void CpuSynchronizer::synchronize(
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>&
tracked_species) {
tracked_species, bool reset) {
MPI_Barrier(MPI_COMM_WORLD);
auto it = tracked_species.begin();
// drops due to time being a global variable now
@@ -141,7 +148,8 @@ void CpuSynchronizer::synchronize(
for (auto& pair : tracked_species) {
// DEBUG_LEVEL_3("dynden Synchronization.");
synchronize(pair.second->dynamicDensityField(),
pair.second->lastSyncDensityField());
pair.second->lastSyncDensityField(),
pair.second->densityForDrawing(), reset);
// DEBUG_LEVEL_3("cc Synchronization.");
synchronize(pair.second->currentCollector(),
pair.second->lastSyncCurrentCollector());
+15 -6
View File
@@ -183,14 +183,17 @@ void DynamicDensityField::initPositionDistributions() {
auto zbins = position_binning_.longitudinal_binning.binBorders();
auto phibins = position_binning_.azimuth_binning.binBorders();
rmin = *(rbins.begin());
rmax = *(rbins.end());
rmin = rbins.front();
rmax = rbins.back();
DEBUG_LEVEL_3("rmax: " + std::to_string(rmax));
zmin = *(zbins.begin());
zmax = *(zbins.end());
zmin = zbins.front();
zmax = zbins.back();
DEBUG_LEVEL_3("zmax: " + std::to_string(zmax));
phimin = *(phibins.begin());
phimax = *(phibins.end());
phimin = phibins.front();
phimax = phibins.back();
DEBUG_LEVEL_3("phimax: " + std::to_string(phimax));
r_dist_ = std::uniform_real_distribution<double>(rmin*rmin, rmax*rmax);
phi_dist_ = std::uniform_real_distribution<double>(phimin, phimax);
@@ -206,6 +209,7 @@ blitz::TinyVector<double, 4> DynamicDensityField::drawPositionAndEnergy(
std::mt19937* engine) {
// if the density is empty, then return a TinyVector filled with 0s as
// an indiction of that (probably not the best solution)
recalculateDensityArray();
if (isEqual(blitz::max(density_per_m3_), 0.)) {
DEBUG_LEVEL_3("Density is empty");
return blitz::TinyVector<double, 4> (0,0,0,0);
@@ -245,6 +249,7 @@ blitz::TinyVector<double, 4> DynamicDensityField::drawPositionAndEnergy(
densityAtPosition.begin());
random_E = E_dist_(*engine);
DEBUG_LEVEL_3("energy: " + std::to_string(random_E));
return blitz::TinyVector<double, 4> (random_r, random_phi, random_z, random_E);
}
@@ -288,6 +293,10 @@ double DynamicDensityField::physicalTime() const {
return timer_->time();
}
double DynamicDensityField::timeResolution() const {
return timer_->getDeltaT();
}
void DynamicDensityField::resetDynamicDensityField() {
//timer_->resetTime(); // time stays global
time_in_bin_s_ = 0.;
+2 -1
View File
@@ -79,7 +79,7 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine,
const double weight,
const double injection_time) {
blitz::TinyVector<double, 4> particle_specs =
specie->dynamicDensityField()->drawPositionAndEnergy(engine);
specie->densityForDrawing()->drawPositionAndEnergy(engine);
// blitz::TinyVector<double, 4> particle_specs(0.01, 0, 1, 1);
// check if density was empty (i.e. Energy component is 0)
@@ -89,6 +89,7 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine,
}
DEBUG_LEVEL_3("Injected z position: " + std::to_string(particle_specs(2)));
DEBUG_LEVEL_3("Injected r position: " + std::to_string(particle_specs(0)));
const double v_abs = kinematic::velocity(particle_specs[3], specie->mass_kg());
+19 -2
View File
@@ -186,8 +186,9 @@ void DriftMover::move(particle::Particle* particle,
bool left_simulation_domain = false;
// if particle too slow -> timestep > maximum time the particle lives
// -> add maximal timestep to time in bin and stop moving the particle
bool died_in_bin = false;
const double max_time_alive =
bool died_in_bin = false;
// DEBUG_LEVEL_3("Mover: timealive before moving: " + std::to_string(particle->timeAlive()));
double max_time_alive =
particle->specie()->dynamicDensityField()->physicalTime() -
particle->timeAlive();
@@ -308,6 +309,22 @@ void DriftMover::move(particle::Particle* particle,
particle->increaseTimeAlive(timestep);
particle->increaseTimeInBin(timestep);
// if instead of eps*deltaT 0.0 is used the integral would be conserved if no particles were lost
// however in that case the terminator does not return the correct value if the time the
// particle is alive is identical to the physical time... -> increases computation time alot
// The version below means that each timestep the density can grow by (at most) a factor
// of (1 + eps) (if no particles are lost)
// TODO:
// - check if it's not better to change the terminator such that is uses the
// compare function from /include/simulation/compare.h
max_time_alive = ((max_time_alive - timestep) <= 0)
? particle->specie()->dynamicDensityField()
->timeResolution()*SimConstants::compare_epsilon
: (max_time_alive - timestep);
if (max_time_alive == 0) {
DEBUG_LEVEL_3("Mover: max_time_alive is 0.");
DEBUG_LEVEL_3("Mover: particle time alive: " + std::to_string(particle->timeAlive()));
}
const auto new_position = particle->position();
+2 -2
View File
@@ -102,7 +102,7 @@ double Particle::timeAlive() const {
}
void Particle::increaseTimeInBin(double time_s) {
time_in_bin_s_ += time_s*weight_;
time_in_bin_s_ += time_s;
}
void Particle::resetTimeInBin() {
@@ -110,7 +110,7 @@ void Particle::resetTimeInBin() {
}
double Particle::timeInBin() const {
return time_in_bin_s_;
return time_in_bin_s_*weight_;
}
long int Particle::ID() const {
+18 -9
View File
@@ -104,6 +104,7 @@ void Simulation::start() {
total_simulated_events);
const int density_events =
parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0);
DEBUG_LEVEL_0("Density events per cpu: " + std::to_string(density_events));
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
@@ -132,7 +133,7 @@ void Simulation::start() {
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution);
SIM_INFO("Leftover events on first cpu: " +
std::to_string(leftover_events));
processEvents(events_of_cylcle, density_events);
processEvents(events_of_cylcle, density_events, num_cpu_syncs);
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
leftover_events -= events_of_cylcle;
@@ -142,7 +143,7 @@ void Simulation::start() {
SIM_INFO("Cpu sync.");
SimulationTimer::eventStartPoint(std::string("CpuSync"));
cpu_synchronizer_->synchronize(tracked_species_);
cpu_synchronizer_->synchronize(tracked_species_, reset);
num_cpu_syncs += 1;
SimulationTimer::eventStopPoint(std::string("CpuSync"));
@@ -185,7 +186,7 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
return random->rollUniform(0, queue_length);
}
void Simulation::processEvents(int events, int density_events) {
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;
@@ -195,6 +196,10 @@ void Simulation::processEvents(int events, int density_events) {
if (queue_.empty()) {
std::vector<particle::Particle> new_particles;
if(current_density_event == density_events) {
// if(cpu_syncs > 0) {
// DEBUG_LEVEL_3("Break while loop.");
// break;
// }
current_event += 1;
DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
@@ -213,13 +218,13 @@ void Simulation::processEvents(int events, int density_events) {
DEBUG_LEVEL_2("Injected particles to queue.");
} else {
current_density_event += 1;
DEBUG_LEVEL_2("Current density event: " +
std::to_string(current_density_event));
//maybe use a different event name?
SimulationTimer::eventStartPoint(std::string("DensityParticleInjection"));
new_particles = determineInjectedDensityParticles(random_.get(), density_events);
new_particles = determineInjectedDensityParticles(random_.get(), density_events, current_density_event);
SimulationTimer::eventStopPoint(std::string("DensityParticleInjection"));
current_density_event += 1;
}
if(new_particles.empty())
continue;
@@ -385,24 +390,28 @@ std::vector<particle::Particle> Simulation::determineInjectedParticles(
}
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
RandomNumbers* random, const int numberParticlesToDraw) {
RandomNumbers* random, const int numberParticlesToDraw, const int current) {
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_) {
// particle number of the denstiy, i.e. the integral over the density
const double particleNumberDensity =
pair.second->dynamicDensityField()->particleNumber();
pair.second->densityForDrawing()->particleNumber();
if (particleNumberDensity == 0) {
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
continue;
}
DEBUG_LEVEL_3("particle number " + pair.second->name() + ": " + std::to_string(particleNumberDensity));
if (current == 0) {
DEBUG_LEVEL_0("particle number " + pair.second->name() + ": " + std::to_string(particleNumberDensity));
// DEBUG_LEVEL_0("weight: " + std::to_string(particleNumberDensity/numberParticlesToDraw));
}
injector_->rollNewParticleFromDensityField(random->engine(),
pair.second,
new_particles,
numberParticlesToDraw/particleNumberDensity,
particleNumberDensity/numberParticlesToDraw/MpiCommunication::mpiSize(),
injection_time);
}
return new_particles;
+8 -2
View File
@@ -42,7 +42,8 @@ TrackedSpecie::TrackedSpecie(
std::shared_ptr<DynamicDensityField> density_field,
std::shared_ptr<VelocityDistribution> velocity_distribution,
std::shared_ptr<DriftVelocity> drift_velocity,
std::shared_ptr<DynamicDensityField> last_sync_density_field)
std::shared_ptr<DynamicDensityField> last_sync_density_field,
std::shared_ptr<DynamicDensityField> density_for_drawing)
: Specie(type, name, mass_kg, charge_C),
mover_(mover),
terminator_(terminator),
@@ -54,7 +55,8 @@ TrackedSpecie::TrackedSpecie(
drift_velocity_(drift_velocity),
last_sync_density_field_(last_sync_density_field),
last_sync_deletion_logger_(std::shared_ptr<DeletionLogger>(nullptr)),
last_sync_current_collector_(std::shared_ptr<CurrentCollector>(nullptr)) {}
last_sync_current_collector_(std::shared_ptr<CurrentCollector>(nullptr)),
density_for_drawing_(density_for_drawing) {}
DensityField* TrackedSpecie::densityField() {
return density_field_.get();
@@ -75,6 +77,10 @@ DynamicDensityField* TrackedSpecie::lastSyncDensityField() {
return last_sync_density_field_.get();
}
DynamicDensityField* TrackedSpecie::densityForDrawing() {
return density_for_drawing_.get();
}
Mover* TrackedSpecie::mover() {
return mover_.get();
}