built restart utilities
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#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"
|
||||
|
||||
// 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)) {}
|
||||
|
||||
void Restart::restart(std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species,
|
||||
PoissonSolver* poisson_solver, RandomNumbers* random_numbers) {
|
||||
|
||||
if(!do_restart_) {
|
||||
return;
|
||||
}
|
||||
DEBUG_LEVEL_3("Restarting rng");
|
||||
restartRandomNumbers(random_numbers);
|
||||
DEBUG_LEVEL_3("Restarting poisson solver.");
|
||||
restartPoissonSolver(poisson_solver);
|
||||
|
||||
DEBUG_LEVEL_3("Restarting timer.");
|
||||
restartTimer(species.begin()->second.get());
|
||||
|
||||
for(const auto& pair: species) {
|
||||
std::string path = pair.second->name() + "/DensityField/SimulationData/" +
|
||||
restart_identifier_;
|
||||
restartDrawingDensityField(pair.second->densityForDrawing(), path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
// DEBUG_LEVEL_3("Restart of random engine worked");
|
||||
// std::uniform_real_distribution<double> dist(0,1);
|
||||
// DEBUG_LEVEL_3("first value after loading rng: " + std::to_string(dist(*engine)));
|
||||
}
|
||||
|
||||
// only restart the time from which the simulation is restarted.
|
||||
// The "timestep" is calculated from the json input data (same as no restart).
|
||||
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";
|
||||
DEBUG_LEVEL_3(path);
|
||||
// probably add a function in the reader to read a single double value instead of
|
||||
// a 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";
|
||||
DEBUG_LEVEL_3(path);
|
||||
// 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";
|
||||
DEBUG_LEVEL_3(path_to_data);
|
||||
blitz::Array<double, 4> time_in_bin = reader_.readD4Double(path_to_data);
|
||||
field->restartTimeInBin(time_in_bin);
|
||||
}
|
||||
|
||||
// restart deleted particles
|
||||
{
|
||||
std::string path_to_data = path + "/DeletedParticles";
|
||||
DEBUG_LEVEL_3(path_to_data);
|
||||
blitz::Array<long int, 4> deleted_particles = reader_.readD4LongInt(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/";
|
||||
DEBUG_LEVEL_3(path);
|
||||
blitz::Array<double, 3> potential = reader_.readD3Double(path);
|
||||
if(!blitz::all(potential.length() == solver->potential().length())) {
|
||||
DEBUG_LEVEL_3("Restart Poissonsolver: wrong sizes of the potential");
|
||||
}
|
||||
solver->setPotential(potential);
|
||||
}
|
||||
Reference in New Issue
Block a user