Basic neccessities to draw from a density field;

does not yet work correctly since the neccessary
density field is reset after a time resolution
step
This commit is contained in:
2024-08-08 10:10:50 +02:00
parent 4301c4453b
commit b57f6ff201
7 changed files with 237 additions and 63 deletions
+73 -48
View File
@@ -69,9 +69,6 @@ void Simulation::init(Parameter& parameter) {
DEBUG_LEVEL_0("Init cpu synchronizer.");
cpu_synchronizer_ = std::make_shared<CpuSynchronizer>();
DEBUG_LEVEL_3("Init done.");
active_queue_ = 0;
non_active_queue_ = 1;
}
int eventsThisCpu(int events_all_cpus) {
@@ -91,11 +88,6 @@ int eventsOfCycle(int stride, int leftover_events) {
}
}
void switch_active(int& active, int& non_active) {
non_active = active;
active = (active + 1)%2;
}
void Simulation::finalize() {
SIM_INFO("Simulation ended.");
}
@@ -110,6 +102,8 @@ void Simulation::start() {
const int time_resolution_events =
parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}),
total_simulated_events);
const int density_events =
parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0);
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
@@ -121,27 +115,24 @@ void Simulation::start() {
const double delta_t = time_resolution_events*injector_->timePassed();
timer_->setDeltaT(delta_t);
injector_->initInjectionTimeDistribution(delta_t);
int num_cpu_syncs = 0;
bool reset = true;
queues_[0].reserve(events_this_cpu);
queues_[1].reserve(events_this_cpu);
// bring some order into the reset checks
queue_.reserve(100);
timer_->increaseTimestep(delta_t);
while (leftover_events > 0) {
std::stringstream out;
out << "Timeinterval: " << timer_->time() << "s.";
out << "\n size of particle: " << sizeof(particle::Particle);
// out << "\n size of particle: " << sizeof(particle::Particle);
DEBUG_LEVEL_3(out.str());
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, time_resolution_events);
DEBUG_LEVEL_3("Size of backup queue: " +
std::to_string(queues_[non_active_queue_].size()));
processEvents(events_of_cylcle, density_events);
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
leftover_events -= events_of_cylcle;
@@ -186,7 +177,6 @@ void Simulation::start() {
// pair.second->lastSyncDeletionLogger()->resetDeletionLogger();
}
leftover_events_time_resolution = time_resolution_this_cpu;
switch_active(active_queue_, non_active_queue_);
}
}
}
@@ -195,45 +185,57 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
return random->rollUniform(0, queue_length);
}
void Simulation::processEvents(int events, const int time_resolution_events) {
void Simulation::processEvents(int events, int density_events) {
DEBUG_LEVEL_0("Processing events.");
int current_event = 0;
while ((current_event < events) || (!queues_[active_queue_].empty())) {
int current_density_event = 0;
while ((current_event < events) || (!queue_.empty())) {
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
if (queues_[active_queue_].empty()) {
current_event += 1;
DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
std::vector<particle::Particle> new_particles =
determineInjectedParticles(random_.get(), time_resolution_events);
if (queue_.empty()) {
std::vector<particle::Particle> new_particles;
if(current_density_event == density_events) {
current_event += 1;
DEBUG_LEVEL_2("Inject particles to queue.");
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
new_particles = determineInjectedParticles(random_.get());
#if defined debug_level3
for(auto& particle : new_particles) {
std::stringstream output_ss;
output_ss << particle.specie()->name() << " with velocity (cart): ";
output_ss << particle.velocity().cartesianEntries();
output_ss << " and position (cart)";
output_ss << particle.position().cartesianEntries();
DEBUG_LEVEL_3(output_ss.str());
}
for(auto& particle : new_particles) {
std::stringstream output_ss;
output_ss << particle.specie()->name() << " with velocity (cart): ";
output_ss << particle.velocity().cartesianEntries();
output_ss << " and position (cart)";
output_ss << particle.position().cartesianEntries();
DEBUG_LEVEL_3(output_ss.str());
}
#endif
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
DEBUG_LEVEL_2("Injected particles to queue.");
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
DEBUG_LEVEL_2("Injected particles to queue.");
queues_[active_queue_].insert(queues_[active_queue_].end(), new_particles.begin(),
} 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);
SimulationTimer::eventStopPoint(std::string("DensityParticleInjection"));
}
if(new_particles.empty())
continue;
queue_.insert(queue_.end(), new_particles.begin(),
new_particles.end());
}
DEBUG_LEVEL_2("Particles in queue: " +
std::to_string(queues_[active_queue_].size()));
DEBUG_LEVEL_2("Particles in backup queue: " +
std::to_string(queues_[non_active_queue_].size()));
std::to_string(queue_.size()));
const int particle_number =
particleNumberFromQueue(queues_[active_queue_].size(), random_.get());
particleNumberFromQueue(queue_.size(), random_.get());
// extract particle here from queue, so that an extension of the queue does
// not mess up the reference
particle::Particle particle = queues_[active_queue_].at(particle_number);
queues_[active_queue_].erase(queues_[active_queue_].begin() + particle_number);
particle::Particle particle = queue_.at(particle_number);
queue_.erase(queue_.begin() + particle_number);
processUntilTermination(particle);
}
@@ -242,6 +244,7 @@ void Simulation::processEvents(int events, const int time_resolution_events) {
void Simulation::processUntilTermination(
particle::Particle& particle) {
DEBUG_LEVEL_3("Process particle until termination");
DEBUG_LEVEL_3("Weight of the particle: " + std::to_string(particle.weight()));
while (particle.specie()->terminator()->isAlive(&particle)) {
auto old_position = particle.position();
double old_energy = particle.kineticEnergy_eV();
@@ -267,9 +270,8 @@ void Simulation::processUntilTermination(
if (particle.specie()->terminator()->reachedMaxTimeAlive(
&particle)) {
DEBUG_LEVEL_3("Move particle " + particle.specie()->name()
+" to backup queue.");
queues_[non_active_queue_].push_back(particle);
DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() +
". Reached timeresolution.");
break;
} else if (particle.specie()->terminator()->leftSimulationDomain(
&particle)) {
@@ -308,7 +310,7 @@ void Simulation::processUntilTermination(
particle.increaseInteractionCounter();
for (auto& new_particle : output.newParticles()) {
queues_[active_queue_].push_back(new_particle);
queue_.push_back(new_particle);
}
if (output.deleteCurrentParticle()) {
@@ -377,10 +379,33 @@ interaction::InteractionOutput Simulation::interact(
}
std::vector<particle::Particle> Simulation::determineInjectedParticles(
RandomNumbers* random, const int time_resolution_events) {
RandomNumbers* random) {
// timer_->increaseTimestep(injector_->timePassed());
return injector_->rollNewParticles(random->engine(),
timer_->time(), time_resolution_events);
return injector_->rollNewParticles(random->engine(), timer_->time());
}
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
RandomNumbers* random, const int numberParticlesToDraw) {
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_) {
const double particleNumberDensity =
pair.second->dynamicDensityField()->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));
injector_->rollNewParticleFromDensityField(random->engine(),
pair.second,
new_particles,
numberParticlesToDraw/particleNumberDensity,
injection_time);
}
return new_particles;
}
//maybe put that into the poisson solver