Basic changes for time resolution.

Input from json still suboptimal.
This commit is contained in:
2024-05-14 09:23:52 +02:00
parent cac5e6ff5d
commit 77fa8d175a
12 changed files with 84 additions and 36 deletions
+5 -2
View File
@@ -35,7 +35,9 @@ class Injector {
std::shared_ptr<EventPositionDistribution> position_distribution,
std::shared_ptr<DriftVelocity> mother_drift_velocity);
std::vector<particle::Particle> rollNewParticles(std::mt19937* engine);
std::vector<particle::Particle> rollNewParticles(std::mt19937* engine,
const double current_time,
const int time_resolution_events);
double timePassed() const;
private:
@@ -48,7 +50,8 @@ class Injector {
std::mt19937* engine,
const Coordinate& position,
std::shared_ptr<species::TrackedSpecie> created_specie,
std::shared_ptr<VelocityDistribution> creation_velocity_distribution);
std::shared_ptr<VelocityDistribution> creation_velocity_distribution,
const double time_birth);
};
#endif /* INJECTOR_H */
+2 -1
View File
@@ -14,7 +14,8 @@ class Particle {
// Vector* velocity);
Particle(std::shared_ptr<species::TrackedSpecie> specie,
const Coordinate& position,
const Vector& velocity);
const Vector& velocity,
const double time_birth);
void position(const Coordinate& new_position);
CartesianCoordinate position() const;
+5 -3
View File
@@ -34,13 +34,15 @@ class Simulation {
void init(Parameter& parameter);
void start();
void finalize();
void processEvents(int events);
void processEvents(int events, std::vector<particle::Particle>& backup_queue,
const int time_resolution_events);
void processUntilTermination(particle::Particle& particle,
std::vector<particle::Particle>& particle_queue);
std::vector<particle::Particle>& particle_queue,
std::vector<particle::Particle>& backup_queue);
private:
std::vector<particle::Particle> determineInjectedParticles(
RandomNumbers* random);
RandomNumbers* random, const int time_resolution_events);
double meanFreePath(particle::Particle& particle, RandomNumbers* random);
+1 -1
View File
@@ -4,7 +4,7 @@
#include "simulation/particle.h"
class Geometry;
class DynamicDensityField;
class Terminator {
public:
Terminator(long int max_interactions,
+2 -1
View File
@@ -30,7 +30,8 @@ InteractionOutput ClusterFormation::performInteraction(
const blitz::TinyVector<double, 3> v_cluster =
kinematic::velocity(energy_eV, product_->mass_kg()) *
v_target / kinematic::norm(v_target);
particle::Particle cluster(product_, x_impact, CartesianVector(v_cluster));
particle::Particle cluster(product_, x_impact, CartesianVector(v_cluster),
particle.timeAlive());
InteractionOutput output;
output.add(cluster);
+6 -2
View File
@@ -93,10 +93,14 @@ InteractionOutput ElectronIonization::performInteraction(
particle.velocity(v_impact_new);
const double time_birth = particle.timeAlive();
InteractionOutput output;
output.add(
particle::Particle(electron_, x_impact, CartesianVector(v_ejected)));
output.add(particle::Particle(ion_, x_impact, CartesianVector(v_target)));
particle::Particle(electron_, x_impact, CartesianVector(v_ejected),
time_birth));
output.add(particle::Particle(ion_, x_impact, CartesianVector(v_target),
time_birth));
return output;
}
+4 -3
View File
@@ -133,9 +133,10 @@ void CpuSynchronizer::synchronize(
tracked_species) {
MPI_Barrier(MPI_COMM_WORLD);
auto it = tracked_species.begin();
DEBUG_LEVEL_3("Timer Synchronization.");
synchronize(it->second->dynamicDensityField()->timer_.get(),
it->second->lastSyncDensityField()->timer_.get());
// drops due to time being a global variable now
//DEBUG_LEVEL_3("Timer Synchronization.");
//synchronize(it->second->dynamicDensityField()->timer_.get(),
// it->second->lastSyncDensityField()->timer_.get());
for (auto& pair : tracked_species) {
// DEBUG_LEVEL_3("dynden Synchronization.");
+1 -1
View File
@@ -206,7 +206,7 @@ double DynamicDensityField::physicalTime() const {
}
void DynamicDensityField::resetDynamicDensityField() {
timer_->resetTime(); // reductive since shared between species
//timer_->resetTime(); // time stays global
time_in_bin_s_ = 0.;
deleted_particles_ = 0.;
density_per_m3_ = 0.;
+15 -4
View File
@@ -7,6 +7,7 @@
#include "simulation/particle.h"
#include "simulation/species.h"
#include "simulation/velocity_distribution.h"
#include <sstream>
Injector::Injector(
std::vector<InjectorInfo>& injector_info,
@@ -26,7 +27,8 @@ particle::Particle Injector::rollNewParticle(
std::mt19937* engine,
const Coordinate& position,
std::shared_ptr<species::TrackedSpecie> created_specie,
std::shared_ptr<VelocityDistribution> creation_velocity_distribution) {
std::shared_ptr<VelocityDistribution> creation_velocity_distribution,
const double time_birth) {
blitz::TinyVector<double, 3> velocity =
creation_velocity_distribution->rollVelocity(position, engine)
.cartesianEntries();
@@ -35,20 +37,29 @@ particle::Particle Injector::rollNewParticle(
velocity,
mother_drift_velocity_->driftVelocity(position).cartesianEntries());
}
std::stringstream out;
out << created_specie->name() << " created at time: " << time_birth << "s.";
DEBUG_LEVEL_3(out.str());
return particle::Particle(created_specie, position,
CartesianVector(velocity));
CartesianVector(velocity), time_birth);
}
std::vector<particle::Particle> Injector::rollNewParticles(
std::mt19937* engine) {
std::mt19937* engine, const double current_time,
const int time_resolution_events) {
CylinderCoordinate position = position_distribution_->rollPosition(engine);
DEBUG_LEVEL_3("Injected z position: " +
std::to_string(position.cylinderEntries()(2)));
std::vector<particle::Particle> new_particles;
const double delta_t = time_resolution_events*timePassed();
std::uniform_real_distribution<> injection_time(current_time - delta_t,
current_time);
const double time_birth = injection_time(*engine);
for (auto& injector_info : injector_infos_) {
new_particles.push_back(
rollNewParticle(engine, position, injector_info.created_specie,
injector_info.creation_velocity_distribution));
injector_info.creation_velocity_distribution,
time_birth));
}
return new_particles;
}
+3 -2
View File
@@ -20,12 +20,13 @@ Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
const Coordinate& position,
const Vector& velocity)
const Vector& velocity,
const double time_birth)
: specie_(specie),
position_(position),
velocity_(velocity, position),
id_(this->last_id_),
time_alive_s_(0.),
time_alive_s_(time_birth),
time_in_bin_s_(0.),
interaction_counter_(0) {
last_id_++;
+37 -15
View File
@@ -99,19 +99,33 @@ void Simulation::start() {
parameter_.intValue(json_path({"Simulation", "EventStride"}), 1);
const int total_simulated_events =
parameter_.intValue(json_path({"Simulation", "SimulatedEvents"}), 1);
const int time_resolution_events =
parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}),
total_simulated_events);
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
const int events_this_cpu = eventsThisCpu(total_simulated_events);
// maybe better as class memeber...
const int time_resolution_this_cpu = eventsThisCpu(time_resolution_events);
SIM_INFO("Events per cpu (approx.): " + std::to_string(events_this_cpu));
int leftover_events = events_this_cpu;
int num_cpu_syncs = 0;
bool reset = false;
std::vector<particle::Particle> backup_queue;
backup_queue.reserve(100); // probably should be a more sensible value
while (leftover_events > 0) {
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events);
timer_->increaseTimestep(time_resolution_events*injector_->timePassed());
std::stringstream out;
out << "Timeinterval: " << timer_->time() << "s.";
out << "\n size of particle: " << sizeof(particle::Particle);
DEBUG_LEVEL_3(out.str());
int events_of_cylcle = eventsOfCycle(event_stride, time_resolution_this_cpu);
SIM_INFO("Leftover events on first cpu: " +
std::to_string(leftover_events));
processEvents(events_of_cylcle);
processEvents(events_of_cylcle, backup_queue, time_resolution_events);
DEBUG_LEVEL_3("Size of backup queue: " + std::to_string(backup_queue.size()));
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
leftover_events -= events_of_cylcle;
@@ -154,10 +168,19 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
return random->rollUniform(0, queue_length);
}
void Simulation::processEvents(int events) {
void Simulation::processEvents(int events,
std::vector<particle::Particle>& backup_queue,
const int time_resolution_events) {
DEBUG_LEVEL_0("Processing events.");
std::vector<particle::Particle> particle_queue;
particle_queue.reserve(100);
DEBUG_LEVEL_3("Move backup to processing queue.");
// highly inefficient in case that #cpu_syncs/time_res >> 1
particle_queue.insert(particle_queue.end(), backup_queue.begin(),
backup_queue.end());
// clean backup_queue after moving the elements to the particle queue
backup_queue.erase(backup_queue.begin(), backup_queue.end());
DEBUG_LEVEL_3("Size processing queue: " + std::to_string(particle_queue.size()));
int current_event = 0;
while ((current_event < events) || (!particle_queue.empty())) {
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
@@ -167,7 +190,7 @@ void Simulation::processEvents(int events) {
DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
std::vector<particle::Particle> new_particles =
determineInjectedParticles(random_.get());
determineInjectedParticles(random_.get(), time_resolution_events);
#if defined debug_level3
for(auto& particle : new_particles) {
std::stringstream output_ss;
@@ -193,13 +216,14 @@ void Simulation::processEvents(int events) {
particle::Particle particle = particle_queue.at(particle_number);
particle_queue.erase(particle_queue.begin() + particle_number);
processUntilTermination(particle, particle_queue);
processUntilTermination(particle, particle_queue, backup_queue);
}
}
void Simulation::processUntilTermination(
particle::Particle& particle,
std::vector<particle::Particle>& particle_queue) {
std::vector<particle::Particle>& particle_queue,
std::vector<particle::Particle>& backup_queue) {
DEBUG_LEVEL_3("Process particle until termination");
while (particle.specie()->terminator()->isAlive(&particle)) {
auto old_position = particle.position();
@@ -226,12 +250,9 @@ void Simulation::processUntilTermination(
if (particle.specie()->terminator()->reachedMaxTimeAlive(
&particle)) {
DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name()
+". Reached max time alive.");
if (particle.specie()->deletionLogger()) {
particle.specie()->deletionLogger()->logDeletion(
particle.position(), DeletionType::max_time_alive);
}
DEBUG_LEVEL_3("Move particle " + particle.specie()->name()
+" to backup queue.");
backup_queue.push_back(particle);
break;
} else if (particle.specie()->terminator()->leftSimulationDomain(
&particle)) {
@@ -339,9 +360,10 @@ interaction::InteractionOutput Simulation::interact(
}
std::vector<particle::Particle> Simulation::determineInjectedParticles(
RandomNumbers* random) {
timer_->increaseTimestep(injector_->timePassed());
return injector_->rollNewParticles(random->engine());
RandomNumbers* random, const int time_resolution_events) {
// timer_->increaseTimestep(injector_->timePassed());
return injector_->rollNewParticles(random->engine(),
timer_->time(), time_resolution_events);
}
//maybe put that into the poisson solver
+3 -1
View File
@@ -2,6 +2,7 @@
#include "simulation/errors.h"
#include "simulation/geometry.h"
#include "simulation/density_field.h"
Terminator::Terminator(long int max_interactions,
double max_time_alive,
@@ -31,7 +32,8 @@ bool Terminator::reachedMaxInteractions(
return particle->interactionCounter() > max_interactions_;
}
bool Terminator::reachedMaxTimeAlive(const particle::Particle* particle) const {
return particle->timeAlive() > max_time_alive_;
return particle->timeAlive() > particle->specie()->dynamicDensityField()
->physicalTime();
}
bool Terminator::leftSimulationDomain(