Version 2, position dependent E/B, kinetic ions, boris mover, terminator
This commit is contained in:
Executable
+302
@@ -0,0 +1,302 @@
|
||||
#include "simulation/simulation.h"
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include "constants/sim_constants.h"
|
||||
#include "simulation/cpu_synchronizer.h"
|
||||
#include "simulation/current_collector.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/electromagnetic_field.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/injector.h"
|
||||
#include "simulation/interaction.h"
|
||||
#include "simulation/logger.h"
|
||||
#include "simulation/mover.h"
|
||||
#include "simulation/mpi_communication.h"
|
||||
#include "simulation/particle.h"
|
||||
#include "simulation/random_numbers.h"
|
||||
#include "simulation/reflector.h"
|
||||
#include "simulation/terminator.h"
|
||||
|
||||
#include "IO/simulation_output.h"
|
||||
|
||||
#include "generators/electric_field_generator.h"
|
||||
#include "generators/injector_generator.h"
|
||||
#include "generators/magnetic_field_generator.h"
|
||||
#include "generators/random_numbers_generator.h"
|
||||
#include "generators/simulation_output_generator.h"
|
||||
#include "generators/species_generator.h"
|
||||
|
||||
#include "tools/simulation_time.h"
|
||||
|
||||
Simulation::Simulation(std::string parameter_filename)
|
||||
: parameter_(parameter_filename) {
|
||||
init(parameter_);
|
||||
}
|
||||
|
||||
void Simulation::init(Parameter& parameter) {
|
||||
magnetic_field_ = generator::magneticField(parameter);
|
||||
electric_field_ = generator::electricField(parameter);
|
||||
|
||||
DEBUG_LEVEL_0("Creating species.");
|
||||
generator::SpeciesBuilder builder(parameter);
|
||||
tracked_species_ = builder.trackedSpecies();
|
||||
background_species_ = builder.backgroundSpecies();
|
||||
timer_ = builder.timer();
|
||||
|
||||
DEBUG_LEVEL_0("Creating injector.");
|
||||
injector_ = generator::buildInjector(tracked_species_, background_species_,
|
||||
parameter);
|
||||
|
||||
DEBUG_LEVEL_0("Init random numbers.");
|
||||
random_ = generator::buildRandomNumbers(parameter);
|
||||
|
||||
DEBUG_LEVEL_0("Init output.");
|
||||
output_ = generator::buildSimulationOutput(
|
||||
parameter.jsonObjectFromBase({"Output"}), tracked_species_);
|
||||
|
||||
DEBUG_LEVEL_0("Output meta data.");
|
||||
output_->writeMetaData(tracked_species_);
|
||||
|
||||
DEBUG_LEVEL_0("Init cpu synchronizer.");
|
||||
cpu_synchronizer_ = std::make_shared<CpuSynchronizer>();
|
||||
}
|
||||
|
||||
int eventsThisCpu(int events_all_cpus) {
|
||||
int events = events_all_cpus / MpiCommunication::mpi_size;
|
||||
int overflow = events_all_cpus % MpiCommunication::mpi_size;
|
||||
if (MpiCommunication::mpi_rank < overflow) {
|
||||
events += 1;
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
int eventsOfCycle(int stride, int leftover_events) {
|
||||
if ((leftover_events - stride) >= 0) {
|
||||
return stride;
|
||||
} else {
|
||||
return leftover_events;
|
||||
}
|
||||
}
|
||||
|
||||
void Simulation::finalize() {
|
||||
SIM_INFO("Simulation ended.");
|
||||
}
|
||||
|
||||
void Simulation::start() {
|
||||
SIM_INFO("Starting Simulation.");
|
||||
|
||||
const int event_stride =
|
||||
parameter_.intValue(json_path({"Simulation", "EventStride"}), 1);
|
||||
const int total_simulated_events =
|
||||
parameter_.intValue(json_path({"Simulation", "SimulatedEvents"}), 1);
|
||||
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);
|
||||
SIM_INFO("Events per cpu (approx.): " + std::to_string(events_this_cpu));
|
||||
int leftover_events = events_this_cpu;
|
||||
|
||||
int num_cpu_syncs = 0;
|
||||
while (leftover_events > 0) {
|
||||
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events);
|
||||
SIM_INFO("Leftover events on first cpu: " +
|
||||
std::to_string(leftover_events));
|
||||
processEvents(events_of_cylcle);
|
||||
|
||||
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
|
||||
leftover_events -= events_of_cylcle;
|
||||
|
||||
SIM_INFO("Cpu sync.");
|
||||
SimulationTimer::eventStartPoint(std::string("CpuSync"));
|
||||
cpu_synchronizer_->synchronize(tracked_species_);
|
||||
num_cpu_syncs += 1;
|
||||
SimulationTimer::eventStopPoint(std::string("CpuSync"));
|
||||
|
||||
SIM_INFO("Poisson step.");
|
||||
SimulationTimer::eventStartPoint(std::string("PoissonStep"));
|
||||
SimulationTimer::eventStopPoint(std::string("PoissonStep"));
|
||||
|
||||
SIM_INFO("Perform output.");
|
||||
SimulationTimer::eventStartPoint(std::string("Output"));
|
||||
output_->writeSimulationData(num_cpu_syncs, tracked_species_);
|
||||
SimulationTimer::eventStopPoint(std::string("Output"));
|
||||
}
|
||||
}
|
||||
|
||||
int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
|
||||
return random->rollUniform(0, queue_length);
|
||||
}
|
||||
|
||||
void Simulation::processEvents(int events) {
|
||||
DEBUG_LEVEL_0("Processing events.");
|
||||
std::vector<particle::Particle> particle_queue;
|
||||
particle_queue.reserve(100);
|
||||
int current_event = 0;
|
||||
while ((current_event < events) || (!particle_queue.empty())) {
|
||||
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
||||
|
||||
if (particle_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());
|
||||
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
|
||||
DEBUG_LEVEL_2("Injected particles to queue.");
|
||||
|
||||
particle_queue.insert(particle_queue.end(), new_particles.begin(),
|
||||
new_particles.end());
|
||||
}
|
||||
DEBUG_LEVEL_2("Particles in queue: " + particle_queue.size());
|
||||
const int particle_number =
|
||||
particleNumberFromQueue(particle_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 = particle_queue.at(particle_number);
|
||||
particle_queue.erase(particle_queue.begin() + particle_number);
|
||||
|
||||
processUntilTermination(particle, particle_queue);
|
||||
}
|
||||
}
|
||||
|
||||
void Simulation::processUntilTermination(
|
||||
particle::Particle& particle,
|
||||
std::vector<particle::Particle>& particle_queue) {
|
||||
DEBUG_LEVEL_3("Process particle until termination");
|
||||
while (particle.specie()->terminator()->isAlive(&particle)) {
|
||||
auto old_position = particle.position();
|
||||
double old_energy = particle.kineticEnergy_eV();
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("MFP calculation"));
|
||||
const double mfp_m = meanFreePath(particle, random_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("MFP calculation"));
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("Mover"));
|
||||
particle.specie()->mover()->move(&particle, mfp_m, electric_field_.get(),
|
||||
magnetic_field_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("Mover"));
|
||||
|
||||
bool was_reflected = false;
|
||||
if (particle.specie()->reflector()->needsReflectionBackToDomain(
|
||||
&particle)) {
|
||||
DEBUG_LEVEL_3("Reflect particle.");
|
||||
old_position = particle.position();
|
||||
particle.specie()->reflector()->reflectBackToDomain(&particle);
|
||||
particle.specie()->currentCollector()->collect(&particle, old_position);
|
||||
was_reflected = true;
|
||||
}
|
||||
|
||||
if (particle.specie()->terminator()->reachedMaxInteractions(&particle)) {
|
||||
DEBUG_LEVEL_3("Terminate particle. Reached max interactions.");
|
||||
if (particle.specie()->deletionLogger()) {
|
||||
particle.specie()->deletionLogger()->logDeletion(
|
||||
particle.position(), DeletionType::max_interactions);
|
||||
}
|
||||
break;
|
||||
} else if (particle.specie()->terminator()->reachedMaxTimeAlive(
|
||||
&particle)) {
|
||||
DEBUG_LEVEL_3("Terminate particle. Reached max time alive.");
|
||||
if (particle.specie()->deletionLogger()) {
|
||||
particle.specie()->deletionLogger()->logDeletion(
|
||||
particle.position(), DeletionType::max_time_alive);
|
||||
}
|
||||
break;
|
||||
} else if (particle.specie()->terminator()->leftSimulationDomain(
|
||||
&particle)) {
|
||||
DEBUG_LEVEL_3("Terminate particle. Left simulation domain.");
|
||||
std::stringstream output_ss;
|
||||
output_ss << "Position (cyl): " << particle.position().cylinderEntries();
|
||||
output_ss << " (cart): " << particle.position().cartesianEntries();
|
||||
DEBUG_LEVEL_3(output_ss.str());
|
||||
if (particle.specie()->deletionLogger()) {
|
||||
particle.specie()->deletionLogger()->logDeletion(particle.position(),
|
||||
DeletionType::wall);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(particle.specie()->dynamicDensityField()->changedPositionBin(
|
||||
old_position, particle.position())) &&
|
||||
!(particle.specie()->dynamicDensityField()->changedEnergyBin(
|
||||
old_energy, particle.kineticEnergy_eV())) &&
|
||||
!was_reflected) {
|
||||
DEBUG_LEVEL_3("Perform interaction.");
|
||||
DEBUG_LEVEL_3("Number of Interactions: " +
|
||||
std::to_string(particle.interactionCounter()));
|
||||
DEBUG_LEVEL_3("Ekin before interact: " +
|
||||
std::to_string(particle.kineticEnergy_eV()));
|
||||
std::stringstream output_ss;
|
||||
output_ss << "Position (cyl): " << particle.position().cylinderEntries();
|
||||
// output_ss << "\n (cart): " << particle.position().cartesianEntries();
|
||||
DEBUG_LEVEL_3(output_ss.str());
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("Interaction"));
|
||||
interaction::InteractionOutput output = interact(particle, random_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("Interaction"));
|
||||
|
||||
particle.increaseInteractionCounter();
|
||||
|
||||
for (auto& new_particle : output.newParticles()) {
|
||||
particle_queue.push_back(new_particle);
|
||||
}
|
||||
|
||||
if (output.deleteCurrentParticle()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double Simulation::meanFreePath(particle::Particle& particle,
|
||||
RandomNumbers* random) {
|
||||
double total_macroscopic_cross_section_m = 0.;
|
||||
for (auto& interaction : particle.specie()->interactions()) {
|
||||
total_macroscopic_cross_section_m +=
|
||||
interaction->macroscopicCrossSection(particle);
|
||||
}
|
||||
|
||||
if (total_macroscopic_cross_section_m <= 0.) {
|
||||
return std::numeric_limits<double>::max();
|
||||
} else {
|
||||
const double rnd_number = random->rollUniform(0., 1.);
|
||||
if (rnd_number <= 0) {
|
||||
return std::numeric_limits<double>::max();
|
||||
} else {
|
||||
return 1. / total_macroscopic_cross_section_m * log(1. / rnd_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
const double total_macroscopic_cross_section_m =
|
||||
std::accumulate(macroscopic_cross_sections_m.begin(),
|
||||
macroscopic_cross_sections_m.end(), 0.0);
|
||||
|
||||
if (total_macroscopic_cross_section_m <= 0.) {
|
||||
// do nothing
|
||||
return interaction::InteractionOutput();
|
||||
} else {
|
||||
std::discrete_distribution<int> interaction_decider(
|
||||
macroscopic_cross_sections_m.begin(),
|
||||
macroscopic_cross_sections_m.end());
|
||||
const int interaction_index = interaction_decider(*(random->engine()));
|
||||
return particle.specie()
|
||||
->interactions()
|
||||
.at(interaction_index)
|
||||
->performInteraction(particle, random);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
||||
RandomNumbers* random) {
|
||||
timer_->increaseTimestep(injector_->timePassed());
|
||||
return injector_->rollNewParticles(random->engine());
|
||||
}
|
||||
Reference in New Issue
Block a user