453 lines
17 KiB
C++
Executable File
453 lines
17 KiB
C++
Executable File
#include "mpi.h"
|
|
#include "IO/simulation_output.h"
|
|
#include "IO/hdf5_writer.h"
|
|
|
|
#include "generators/species_generator.h"
|
|
#include "simulation/current_collector.h"
|
|
#include "simulation/density_field.h"
|
|
#include "simulation/logger.h"
|
|
#include "simulation/mpi_communication.h"
|
|
#include "simulation/species.h"
|
|
#include "poisson_solver/poisson_solver.h"
|
|
|
|
SimulationOutput::SimulationOutput(const std::string& output_folder,
|
|
const std::string& output_filename)
|
|
: writer(Hdf5Writer(output_folder, output_filename)) {}
|
|
|
|
void SimulationOutput::addDensityFieldOutput(OutputTask task) {
|
|
tasks_density_fields_.push_back(task);
|
|
}
|
|
|
|
void SimulationOutput::addCurrentBorderOutput(OutputTask task) {
|
|
tasks_current_collector_.push_back(task);
|
|
}
|
|
|
|
void SimulationOutput::addDeletionLoggerOutput(OutputTask task) {
|
|
tasks_deletion_logger_.push_back(task);
|
|
}
|
|
|
|
void SimulationOutput::setPotentialOutput(OutputTask task) {
|
|
task_potential_ = task;
|
|
}
|
|
|
|
void SimulationOutput::setRestartInfoOutput(OutputTask task) {
|
|
task_rng_ = task;
|
|
}
|
|
|
|
void SimulationOutput::writeMetaData(
|
|
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species,
|
|
const std::shared_ptr<PoissonSolver>& poisson_solver) {
|
|
if (MpiCommunication::mpi_rank == 0) {
|
|
for (auto& pair : species) {
|
|
writeMetaData(pair.second.get());
|
|
}
|
|
|
|
for (auto& task : tasks_density_fields_) {
|
|
std::string specie_path = "/" + task.specie_name;
|
|
writeMetaData(species.at(task.specie_name)->dynamicDensityField(),
|
|
specie_path);
|
|
}
|
|
|
|
for (auto& task : tasks_current_collector_) {
|
|
std::string specie_path = "/" + task.specie_name;
|
|
writeMetaData(species.at(task.specie_name)->currentCollector(),
|
|
specie_path);
|
|
}
|
|
|
|
{
|
|
std::string specie_path = "/" + task_potential_.specie_name;
|
|
writeMetaData(poisson_solver.get(), specie_path);
|
|
}
|
|
|
|
// write the number of mpi processes used in the current simulation
|
|
{
|
|
writer.write(MpiCommunication::mpiSize(), "/NumberProcesses");
|
|
}
|
|
}
|
|
}
|
|
|
|
void SimulationOutput::writeSimulationData(
|
|
int cpu_sync,
|
|
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species,
|
|
PoissonSolver* poisson_solver,
|
|
std::mt19937* engine) {
|
|
if (MpiCommunication::mpi_rank == 0) {
|
|
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Write density field data.");
|
|
#endif
|
|
|
|
for (auto& task : tasks_density_fields_) {
|
|
if (cpu_sync % task.intervall == 0) {
|
|
std::string specie_path = "/" + task.specie_name;
|
|
writeSimulationData(species.at(task.specie_name)->dynamicDensityField(),
|
|
specie_path, cpu_sync, task.overwrite_last);
|
|
}
|
|
}
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Write current collector data.");
|
|
#endif
|
|
for (auto& task : tasks_current_collector_) {
|
|
if (cpu_sync % task.intervall == 0) {
|
|
std::string specie_path = "/" + task.specie_name;
|
|
writeSimulationData(species.at(task.specie_name)->currentCollector(),
|
|
specie_path, cpu_sync, task.overwrite_last);
|
|
}
|
|
}
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Write deletion logger data.");
|
|
#endif
|
|
for (auto& task : tasks_deletion_logger_) {
|
|
if (cpu_sync % task.intervall == 0) {
|
|
std::string specie_path = "/" + task.specie_name;
|
|
writeSimulationData(species.at(task.specie_name)->deletionLogger(),
|
|
specie_path, cpu_sync, task.overwrite_last);
|
|
}
|
|
}
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Write PoissonSolver data.");
|
|
#endif
|
|
if (cpu_sync % task_potential_.intervall == 0) {
|
|
std::string poisson_path = "/" + task_potential_.specie_name;
|
|
writeSimulationData(poisson_solver, poisson_path, cpu_sync,
|
|
task_potential_.overwrite_last);
|
|
}
|
|
}
|
|
|
|
// if the rng intervall = 0 (no neg. intervalls allowed) -> no output,
|
|
// otherwise the intervall used the same as for the other outputs
|
|
if((task_rng_.intervall > 0) && (cpu_sync%task_rng_.intervall == 0)) {
|
|
writeRestartData(engine, cpu_sync, task_rng_.overwrite_last);
|
|
}
|
|
}
|
|
|
|
void SimulationOutput::writeSimulationData(DynamicDensityField* density_field,
|
|
std::string species_path,
|
|
int cpu_sync,
|
|
bool overwrite_last) {
|
|
std::string sync_path = "SyncPeriod_" + std::to_string(cpu_sync);
|
|
if (overwrite_last) {
|
|
sync_path = "LastSyncPeriod";
|
|
}
|
|
std::string path = species_path + "/DensityField/SimulationData/" + sync_path;
|
|
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write spectrum.");
|
|
#endif
|
|
writeSpectrum(density_field, path);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write density.");
|
|
#endif
|
|
writeDensity(density_field, path);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write time in bin.");
|
|
#endif
|
|
writeTimeInBin(density_field, path);
|
|
// DEBUG_LEVEL_3("Write deleted particles.");
|
|
// writeDeletedParticles(density_field, path);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write physical time.");
|
|
#endif
|
|
writePhysicalTime(density_field, path);
|
|
}
|
|
|
|
void SimulationOutput::writeSimulationData(CurrentCollector* current_collector,
|
|
std::string species_path,
|
|
int cpu_sync,
|
|
bool overwrite_last) {
|
|
std::string sync_path = "SyncPeriod_" + std::to_string(cpu_sync);
|
|
if (overwrite_last) {
|
|
sync_path = "LastSyncPeriod";
|
|
}
|
|
std::string path =
|
|
species_path + "/CurrentCollector/SimulationData/" + sync_path;
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write current crossings.");
|
|
#endif
|
|
writeCurrentCrossings(current_collector, path);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write current.");
|
|
#endif
|
|
writeCurrent(current_collector, path);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write physical time.");
|
|
#endif
|
|
writePhysicalTime(current_collector, path);
|
|
}
|
|
|
|
void SimulationOutput::writeSimulationData(DeletionLogger* logger,
|
|
std::string species_path,
|
|
int cpu_sync,
|
|
bool overwrite_last) {
|
|
std::string sync_path = "SyncPeriod_" + std::to_string(cpu_sync);
|
|
if (overwrite_last) {
|
|
sync_path = "LastSyncPeriod";
|
|
}
|
|
std::string path = species_path + "/DeletionLogger/" + sync_path;
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write number of deletions.");
|
|
#endif
|
|
writeNumDeletions(logger, path);
|
|
}
|
|
|
|
void SimulationOutput::writeSimulationData(PoissonSolver* poisson_solver,
|
|
std::string poisson_path,
|
|
int cpu_sync,
|
|
bool overwrite_last) {
|
|
std::string sync_path = "/SyncPeriod_" + std::to_string(cpu_sync);
|
|
if (overwrite_last) {
|
|
sync_path = "/LastSyncPeriod";
|
|
}
|
|
std::string path = poisson_path + "/SimulationData" + sync_path;
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Write potential.");
|
|
#endif
|
|
writePotential(poisson_solver, path);
|
|
}
|
|
|
|
void SimulationOutput::writeRestartData(std::mt19937* engine, int cpu_sync,
|
|
bool overwrite_last) {
|
|
std::string path_base = "/RestartInfo/SyncPeriod_" +
|
|
std::to_string(cpu_sync) +
|
|
"/CPU_";
|
|
if (overwrite_last) {
|
|
path_base = "/RestartInfo/LastSyncPeriod/CPU_";
|
|
}
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(path_base);
|
|
DEBUG_LEVEL_3("Write restart info.");
|
|
#endif
|
|
writeRandomEngineState(engine, path_base);
|
|
// DEBUG_LEVEL_3("Done");
|
|
}
|
|
|
|
void SimulationOutput::writeRandomEngineState(std::mt19937* engine, std::string path) {
|
|
std::ostringstream engine_state;
|
|
engine_state << (*engine);
|
|
writer.writeRNG(engine_state.str(), path);
|
|
}
|
|
|
|
void SimulationOutput::writeNumDeletions(DeletionLogger* logger,
|
|
std::string path) {
|
|
std::string path_to_deletions = path + "/NumDeletions";
|
|
auto deletions = logger->numDeletions();
|
|
for (auto& pair : deletions) {
|
|
if (pair.first == DeletionType::wall) {
|
|
writer.write(pair.second, path_to_deletions + "/Wall");
|
|
} else if (pair.first == DeletionType::recombination) {
|
|
writer.write(pair.second, path_to_deletions + "/Recombination");
|
|
} else if (pair.first == DeletionType::max_interactions) {
|
|
writer.write(pair.second, path_to_deletions + "/MaxInteractions");
|
|
} else if (pair.first == DeletionType::max_time_alive) {
|
|
writer.write(pair.second, path_to_deletions + "/MaxTimeAlive");
|
|
} else if (pair.first == DeletionType::cluster_formation) {
|
|
writer.write(pair.second, path_to_deletions + "/ClusterFormation");
|
|
}
|
|
}
|
|
|
|
auto physDeletions = logger->numPhysDeletions();
|
|
for (auto& pair : physDeletions) {
|
|
if (pair.first == PhysicalDeletionType::wall) {
|
|
writer.write(pair.second, path_to_deletions + "/Wall(physical)");
|
|
}
|
|
}
|
|
}
|
|
|
|
void SimulationOutput::writeCurrentCrossings(
|
|
CurrentCollector* current_collector,
|
|
std::string path) {
|
|
writer.write(current_collector->positiveCrossingsInR(),
|
|
path + "/PositiveCrossingsInR");
|
|
writer.write(current_collector->negativeCrossingsInR(),
|
|
path + "/NegativeCrossingsInR");
|
|
|
|
writer.write(current_collector->positiveCrossingsInPhi(),
|
|
path + "/PositiveCrossingsInPhi");
|
|
writer.write(current_collector->negativeCrossingsInPhi(),
|
|
path + "/NegativeCrossingsInPhi");
|
|
|
|
writer.write(current_collector->positiveCrossingsInZ(),
|
|
path + "/PositiveCrossingsInZ");
|
|
writer.write(current_collector->negativeCrossingsInZ(),
|
|
path + "/NegativeCrossingsInZ");
|
|
}
|
|
|
|
void SimulationOutput::writeCurrent(CurrentCollector* current_collector,
|
|
std::string path) {
|
|
writer.write(current_collector->currentInR(), path + "/CurrentInR");
|
|
writer.write(current_collector->currentInPhi(), path + "/CurrentInPhi");
|
|
writer.write(current_collector->currentInZ(), path + "/CurrentInZ");
|
|
}
|
|
|
|
void SimulationOutput::writePhysicalTime(CurrentCollector* current_collector,
|
|
std::string path) {
|
|
writer.write(current_collector->physicalTime(), path + "/PhysicalTime");
|
|
}
|
|
|
|
void SimulationOutput::writeSpectrum(DynamicDensityField* density_field,
|
|
std::string path) {
|
|
writer.write(density_field->spectrumArray(), path + "/Spectrum");
|
|
}
|
|
|
|
void SimulationOutput::writeDensity(DynamicDensityField* density_field,
|
|
std::string path) {
|
|
writer.write(density_field->densityArray(), path + "/Density");
|
|
}
|
|
|
|
void SimulationOutput::writeTimeInBin(DynamicDensityField* density_field,
|
|
std::string path) {
|
|
writer.write(density_field->timeInBinArray(), path + "/TimeInBin");
|
|
}
|
|
|
|
// void SimulationOutput::writeDeletedParticles(DynamicDensityField* density_field,
|
|
// std::string path) {
|
|
// writer.write(density_field->deletedParticlesArray(),
|
|
// path + "/DeletedParticles");
|
|
// }
|
|
|
|
void SimulationOutput::writePhysicalTime(DynamicDensityField* density_field,
|
|
std::string path) {
|
|
writer.write(density_field->physicalTime(), path + "/PhysicalTime");
|
|
}
|
|
|
|
void SimulationOutput::writePotential(PoissonSolver* poisson_solver, std::string path) {
|
|
writer.write(poisson_solver->potential(), path + "/Potential");
|
|
}
|
|
|
|
void SimulationOutput::writeMetaData(species::TrackedSpecie* specie) {
|
|
std::string specie_path = "/" + specie->name();
|
|
|
|
writer.write(specie->mass_kg(), specie_path + "/Mass");
|
|
writer.write(specie->charge_C(), specie_path + "/Charge");
|
|
}
|
|
|
|
/*
|
|
void SimulationOutput::writeMetaData(DynamicDensityField* density_field,
|
|
std::string path) {
|
|
std::string path_to_field = path + "/DensityField";
|
|
|
|
writeBinVolumes(density_field, path_to_field);
|
|
writeBinBorders(density_field, path_to_field);
|
|
writeNumBins(density_field, path_to_field);
|
|
}
|
|
*/
|
|
|
|
// Todo: use the functions again (as in the method thats uncommented above)
|
|
void SimulationOutput::writeMetaData(DynamicDensityField* density_field,
|
|
std::string path) {
|
|
std::string path_to_field = path + "/DensityField";
|
|
|
|
// write bin volumes
|
|
{
|
|
std::string path_to_volume = path_to_field + "/BinVolumes";
|
|
writer.write(density_field->binVolumeArray(), path_to_volume);
|
|
}
|
|
|
|
// write bin borders
|
|
{
|
|
std::string path_to_bin_borders = path_to_field + "/BinBorders";
|
|
|
|
std::vector<double> energy_bin_borders =
|
|
density_field->energyBinning()->binBorders();
|
|
writer.write(energy_bin_borders, path_to_bin_borders + "/energy");
|
|
|
|
std::vector<double> mu_bin_borders =
|
|
density_field->pitchAngleBinning()->binBorders();
|
|
writer.write(mu_bin_borders, path_to_bin_borders + "/pitchAngle");
|
|
|
|
std::vector<double> radial_bin_borders =
|
|
density_field->positionBinning()->radial_binning.binBorders();
|
|
writer.write(radial_bin_borders, path_to_bin_borders + "/radial");
|
|
|
|
std::vector<double> azimuth_bin_borders =
|
|
density_field->positionBinning()->azimuth_binning.binBorders();
|
|
writer.write(azimuth_bin_borders, path_to_bin_borders + "/azimuth");
|
|
|
|
std::vector<double> longitudinal_bin_borders =
|
|
density_field->positionBinning()->longitudinal_binning.binBorders();
|
|
writer.write(longitudinal_bin_borders, path_to_bin_borders + "/longitudinal");
|
|
}
|
|
|
|
// write num bins
|
|
{
|
|
std::string path_to_num_bins = path_to_field + "/NumBins";
|
|
blitz::TinyVector<int, 4> num_bins = density_field->spectrumArray().extent();
|
|
|
|
writer.write(num_bins(0), path_to_num_bins + "/energy");
|
|
writer.write(num_bins(1), path_to_num_bins + "/radial");
|
|
writer.write(num_bins(2), path_to_num_bins + "/azimuthal");
|
|
writer.write(num_bins(3), path_to_num_bins + "/longitudinal");
|
|
}
|
|
}
|
|
|
|
void SimulationOutput::writeMetaData(CurrentCollector* current_collector,
|
|
std::string path) {
|
|
std::string path_to_collector = path + "/CurrentCollector";
|
|
writeCollectorAreas(current_collector, path_to_collector);
|
|
}
|
|
|
|
void SimulationOutput::writeMetaData(PoissonSolver* poisson_solver,
|
|
std::string path) {
|
|
if(!poisson_solver) {
|
|
return;
|
|
}
|
|
std::string path_to_bin_borders = path + "/BinBorders";
|
|
|
|
std::vector<double> radial_bin_borders =
|
|
poisson_solver->densityBinning()->radial_binning.binBorders();
|
|
writer.write(radial_bin_borders, path_to_bin_borders + "/radial");
|
|
|
|
std::vector<double> azimuth_bin_borders =
|
|
poisson_solver->densityBinning()->azimuth_binning.binBorders();
|
|
writer.write(azimuth_bin_borders, path_to_bin_borders + "/azimuth");
|
|
|
|
std::vector<double> longitudinal_bin_borders =
|
|
poisson_solver->densityBinning()->longitudinal_binning.binBorders();
|
|
writer.write(longitudinal_bin_borders, path_to_bin_borders + "/longitudinal");
|
|
}
|
|
|
|
void SimulationOutput::writeCollectorAreas(CurrentCollector* current_collector,
|
|
std::string path) {
|
|
writer.write(current_collector->areaInR(), path + "/AreaInR");
|
|
writer.write(current_collector->areaInPhi(), path + "/AreaInPhi");
|
|
writer.write(current_collector->areaInZ(), path + "/AreaInZ");
|
|
}
|
|
|
|
void SimulationOutput::writeBinVolumes(DynamicDensityField* density_field,
|
|
std::string path_to_field) {
|
|
std::string path_to_volume = path_to_field + "/BinVolumes";
|
|
writer.write(density_field->binVolumeArray(), path_to_volume);
|
|
}
|
|
|
|
void SimulationOutput::writeNumBins(DynamicDensityField* density_field,
|
|
std::string path_to_field) {
|
|
std::string path_to_num_bins = path_to_field + "/NumBins";
|
|
blitz::TinyVector<int, 4> num_bins = density_field->spectrumArray().extent();
|
|
|
|
writer.write(num_bins(0), path_to_num_bins + "/energy");
|
|
writer.write(num_bins(1), path_to_num_bins + "/radial");
|
|
writer.write(num_bins(2), path_to_num_bins + "/azimuthal");
|
|
writer.write(num_bins(3), path_to_num_bins + "/longitudinal");
|
|
}
|
|
|
|
void SimulationOutput::writeBinBorders(DynamicDensityField* density_field,
|
|
std::string path_to_field) {
|
|
std::string path_to_bin_borders = path_to_field + "/BinBorders";
|
|
|
|
std::vector<double> energy_bin_borders =
|
|
density_field->energyBinning()->binBorders();
|
|
writer.write(energy_bin_borders, path_to_bin_borders + "/energy");
|
|
|
|
std::vector<double> radial_bin_borders =
|
|
density_field->positionBinning()->radial_binning.binBorders();
|
|
writer.write(radial_bin_borders, path_to_bin_borders + "/radial");
|
|
|
|
std::vector<double> azimuth_bin_borders =
|
|
density_field->positionBinning()->azimuth_binning.binBorders();
|
|
writer.write(azimuth_bin_borders, path_to_bin_borders + "/azimuth");
|
|
|
|
std::vector<double> longitudinal_bin_borders =
|
|
density_field->positionBinning()->longitudinal_binning.binBorders();
|
|
writer.write(longitudinal_bin_borders, path_to_bin_borders + "/longitudinal");
|
|
}
|