173 lines
5.8 KiB
C++
173 lines
5.8 KiB
C++
#include <sstream>
|
|
#include <random>
|
|
|
|
#include "IO/restart.h"
|
|
#include "simulation/random_numbers.h"
|
|
#include "simulation/mpi_communication.h"
|
|
#include "simulation/errors.h"
|
|
#include "simulation/species.h"
|
|
#include "simulation/density_field.h"
|
|
#include "poisson_solver/poisson_solver.h"
|
|
#include "simulation/timer.h"
|
|
#include "simulation/event_position_distribution.h"
|
|
|
|
// Note: error handling is a bit messy. Sometimes it's dealt with in the
|
|
// called reset function (e.g. in the density field) and sometimes it's
|
|
// handled here
|
|
|
|
// Restart::Restart(bool do_restart, std::string absolute_filename,
|
|
// std::string restart_identifier):
|
|
// do_restart_(do_restart),
|
|
// restart_identifier_(restart_identifier),
|
|
// reader_(Hdf5Reader(absolute_filename)) {}
|
|
|
|
Restart::Restart(bool do_restart, std::string absolute_filename_without_extension,
|
|
std::string restart_idenitfier, int particle_restart_at):
|
|
do_restart_(do_restart),
|
|
restart_identifier_(restart_idenitfier),
|
|
reader_(absolute_filename_without_extension + ".h5"),
|
|
particle_reader_(absolute_filename_without_extension,
|
|
particle_restart_at) {}
|
|
|
|
void Restart::restart(
|
|
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species,
|
|
PoissonSolver* poisson_solver,
|
|
RandomNumbers* random_numbers,
|
|
std::vector<particle::SerializedParticle>& storage_queue,
|
|
int& injectionGroupOffset) {
|
|
|
|
if(!do_restart_) {
|
|
return;
|
|
}
|
|
#ifdef debug_level1
|
|
DEBUG_LEVEL_1("Restarting rng");
|
|
#endif
|
|
restartRandomNumbers(random_numbers);
|
|
|
|
#ifdef debug_level1
|
|
DEBUG_LEVEL_1("Restarting poisson solver.");
|
|
#endif
|
|
restartPoissonSolver(poisson_solver);
|
|
|
|
#ifdef debug_level1
|
|
DEBUG_LEVEL_1("Restarting timer.");
|
|
#endif
|
|
restartTimer(species.begin()->second.get());
|
|
|
|
for(const auto& pair: species) {
|
|
std::string path = pair.second->name() + "/DensityField/SimulationData/" +
|
|
restart_identifier_;
|
|
restartDrawingDensityField(pair.second->dynamicDensityField(), path);
|
|
// pair.second->positionDistributionFromDensity()->setMaximumDensity();
|
|
}
|
|
|
|
{
|
|
std::string path = "/RestartInfo/" + restart_identifier_ + "/InjectionGroupOffset";
|
|
injectionGroupOffset = reader_.readInt(path);
|
|
}
|
|
|
|
#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
|
|
DEBUG_LEVEL_1("Reloading particles.");
|
|
#endif
|
|
particle_reader_.readParticles(storage_queue);
|
|
}
|
|
|
|
void Restart::restartRandomNumbers(RandomNumbers* random) {
|
|
std::string path = "/RestartInfo/" + restart_identifier_
|
|
+ "/CPU_" + std::to_string(MpiCommunication::mpiRank());
|
|
std::stringstream engine_state = reader_.readEngineString(path);
|
|
std::mt19937* engine = random->engine();
|
|
engine_state >> *engine;
|
|
|
|
std::stringstream check;
|
|
check << *engine;
|
|
|
|
if(check.str() != engine_state.str()) {
|
|
ERROR("Something went wrong in the initialization of the "
|
|
"random engine.");
|
|
}
|
|
}
|
|
|
|
// only restart the time from which the simulation is restarted.
|
|
void Restart::restartTimer(species::TrackedSpecie* reference_specie) {
|
|
double time;
|
|
double delta_t;
|
|
|
|
// physical time
|
|
{
|
|
std::string path = reference_specie->name() + "/DensityField/SimulationData/" +
|
|
restart_identifier_ + "/PhysicalTime";
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(path);
|
|
#endif
|
|
// probably add a function in the reader to read a single double value instead of
|
|
// an 1d array of doubles
|
|
blitz::Array<double, 1> restart_time = reader_.readD1Double(path);
|
|
if(restart_time.size() != 1) {
|
|
ERROR("Physical time should be a single double value but " +
|
|
std::to_string(restart_time.size()) +
|
|
" was read by the hdf5 reader.");
|
|
}
|
|
time = restart_time(0);
|
|
}
|
|
|
|
// delta t
|
|
{
|
|
std::string path = reference_specie->name() + "/CurrentCollector/SimulationData/" +
|
|
restart_identifier_ + "/PhysicalTime";
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(path);
|
|
#endif
|
|
// probably add a function in the reader to read a single double value instead of
|
|
// a 1d array of doubles
|
|
blitz::Array<double, 1> time_resolution = reader_.readD1Double(path);
|
|
if(time_resolution.size() != 1) {
|
|
ERROR("Physical time should be a single double value but " +
|
|
std::to_string(time_resolution.size()) +
|
|
" was read by the hdf5 reader.");
|
|
}
|
|
delta_t = time_resolution(0);
|
|
}
|
|
reference_specie->dynamicDensityField()->restartTimer(time, delta_t);
|
|
return;
|
|
}
|
|
|
|
void Restart::restartDrawingDensityField(DynamicDensityField* field, std::string path) {
|
|
// restart TimeInBin field
|
|
{
|
|
std::string path_to_data = path + "/TimeInBin";
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(path_to_data);
|
|
#endif
|
|
blitz::Array<double, 4> number_in_bin = reader_.readD4Double(path_to_data);
|
|
field->restartNumberInBin(number_in_bin);
|
|
field->recalculateDensityArray();
|
|
}
|
|
|
|
// // restart deleted particles
|
|
// {
|
|
// std::string path_to_data = path + "/DeletedParticles";
|
|
// DEBUG_LEVEL_3(path_to_data);
|
|
// blitz::Array<long int, 5> deleted_particles = reader_.readD5LongInt(path_to_data);
|
|
// field->restartDeletedParticlesArray(deleted_particles);
|
|
// }
|
|
|
|
return;
|
|
}
|
|
|
|
// could be removed, but then the potential has to be calculated
|
|
// as in the normal simulation cycle
|
|
void Restart::restartPoissonSolver(PoissonSolver* solver) {
|
|
std::string path = "PoissonSolver/SimulationData/" + restart_identifier_ +
|
|
"/Potential/";
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(path);
|
|
#endif
|
|
blitz::Array<double, 2> potential = reader_.readD2Double(path);
|
|
solver->setPotential(potential);
|
|
}
|
|
|
|
void Restart::restartParticles(
|
|
std::vector<particle::SerializedParticle>& storage_queue) {
|
|
particle_reader_.readParticles(storage_queue);
|
|
} |