Files
katrin-plasma/src/mc_electron/simulation.cpp
T

228 lines
6.9 KiB
C++

//
// simulation.cpp
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#include "simulation.hpp"
#include <iomanip>
using namespace std;
const long int Simulation::seed = load_parameter("seed");
const long int Simulation::numberTotalDecays = load_parameter("numberTotalDecays");
const long int Simulation::simulationStepWidth = load_parameter("simulationStepWidth");
const long int Simulation::popCalcDecays = load_parameter("popCalcDecays");
const long int Simulation::popCalcStep = load_parameter("popCalcStep");
Simulation::Simulation() {
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
looper.init(calc_seed(), calc_ID_offset(), calc_outfile_path());
init_pop_arrays();
}
Simulation::~Simulation() {
deconstruct_pop_arrays();
}
void Simulation::init_pop_arrays() {
vector<species> all_species = get_all_species_vector();
number_pop_species = all_species.size();
pop_species = new species[number_pop_species];
pop_array_lengths = new int[number_pop_species];
global_cum_time_s = new double *[number_pop_species];
global_offset = new double *[number_pop_species];
Population_Manager *populations = looper.get_populations();
Population_Base *population;
for (int i = 0; i<number_pop_species; i++) {
pop_species[i] = all_species[i];
population = populations->find(pop_species[i]);
pop_array_lengths[i] = population->get_array_length();
global_cum_time_s[i] = new double[pop_array_lengths[i]];
global_offset[i] = new double[pop_array_lengths[i]];
for (int j = 0; j<pop_array_lengths[i]; j++) {
global_cum_time_s[i][j] = 0;
global_offset[i][j] = 0;
}
}
}
void Simulation::deconstruct_pop_arrays() {
delete[] pop_species;
delete[] pop_array_lengths;
for (int i = 0; i<number_pop_species; i++) {
delete[] global_cum_time_s[i];
delete[] global_offset[i];
}
delete[] global_cum_time_s;
delete[] global_offset;
}
long int Simulation::calc_ID_offset() {
return numberTotalDecays * 1000 * mpi_rank;
}
long int Simulation::calc_seed() {
return seed + mpi_rank;
}
std::string Simulation::calc_outfile_path() {
int dir_number;
std::string dirname;
if (mpi_rank == 0) {
dir_number = calc_dir_number();
dirname = build_dirname(dir_number);
create_out_dir(dirname);
MPI_Bcast(&dir_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
cout<<"Logging to folder "<<dirname<<"\n";
} else {
MPI_Bcast(&dir_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
dirname = build_dirname(dir_number);
}
MPI_Barrier(MPI_COMM_WORLD);
std::stringstream filename;
filename<<dirname<<"mc_electron_out_"<<create_number_extension()<<".h5";
return filename.str();
}
int Simulation::calc_dir_number() {
string parname = get_parameter_filenamepath();
stringstream dirname;
int i = 0;
do {
dirname.str(std::string());
dirname << parname << "_" << get_time_string() << "_" << ++i;
} while ( is_dir(dirname.str()) );
return i;
}
std::string Simulation::build_dirname(int dir_number) {
string parname = get_parameter_filenamepath();
stringstream dirname;
dirname.str(std::string());
dirname << parname << "_" << get_time_string() << "_" << dir_number<<"/";
return dirname.str();
}
void Simulation::create_out_dir(std::string dirname) {
if (mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
stringstream ss;
ss<<"Simulation::create_out_dir: Could not create output directiory.\n outdirName = "<<dirname<<endl;
throw MyException(ss.str());
}
}
std::string Simulation::create_number_extension() {
int digits = to_string(mpi_size).length();
std::stringstream ss;
ss << std::setw(digits) << std::setfill('0') << mpi_rank;
return ss.str();
}
void Simulation::doit() {
do_cycle(popCalcDecays, popCalcStep, false);
if (mpi_rank == 0) {
cout<<"Population calculation done.\n";
}
do_cycle(numberTotalDecays, simulationStepWidth, true);
};
void Simulation::do_cycle(long int number_total, long int step_size, bool logging) {
if (number_total == 0) {
return;
}
if (step_size <1) {
stringstream ss;
ss<<"Simulation::do_cycle: invalid step_size. \n";
throw MyException(ss.str());
}
long int decays_this_cpu = number_total/mpi_size;
int overflow = number_total%mpi_size;
int num_cycles = decays_this_cpu/step_size; // Must be same for all CPUs else deadlock!
int num_remaining = decays_this_cpu%step_size;
if (mpi_rank < overflow) { // Split overflows equaly to left over CPUs.
num_remaining += 1;
}
for (int i = 0; i<(num_cycles-1); i++) {
do_decays(step_size, logging);
}
if (num_cycles == 0) { // Rather unrealistic case.. maybe find a more elegant solution later.
do_decays(num_remaining, logging);
} else {
do_decays(step_size + num_remaining, logging);
}
}
void Simulation::do_decays(const long int &number_decays, const bool &logging) {
if (logging) {
looper.do_loops(number_decays);
} else {
looper.do_logging_off_loops(number_decays);
}
number_local_decays += number_decays;
sync_cpus();
do_statusUpdate();
}
void Simulation::sync_cpus() {
MPI_Allreduce(&number_local_decays, &number_global_decays, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
looper.set_global_decays(number_global_decays - number_local_decays);
sync_populations();
}
void Simulation::sync_populations() {
Population_Manager *populations = looper.get_populations();
Population_Base *population;
double *global_delta_cumulated_time_s;
double *global_delta_offset;
double *local_delta_cumulated_time_s;
double *local_delta_offset;
int array_length;
for (int i = 0; i<number_pop_species; i++) {
population = populations->find(pop_species[i]);
array_length = pop_array_lengths[i];
if (array_length == 0) {
continue;
}
global_delta_cumulated_time_s = new double[array_length];
global_delta_offset = new double[array_length];
local_delta_cumulated_time_s = new double[array_length];
local_delta_offset = new double[array_length];
for (int j = 0; j<array_length; j++) {
local_delta_cumulated_time_s[j] = population->get_cumulated_time_s_array()[j] - global_cum_time_s[i][j];
local_delta_offset[j] = population->get_offset_array()[j] - global_offset[i][j];
}
MPI_Allreduce(local_delta_cumulated_time_s, global_delta_cumulated_time_s, array_length, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(local_delta_offset, global_delta_offset, array_length, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
for (int j = 0; j<pop_array_lengths[i]; j++) {
global_cum_time_s[i][j] += global_delta_cumulated_time_s[j];
global_offset[i][j] += global_delta_offset[j];
population->get_cumulated_time_s_array()[j] = global_cum_time_s[i][j];
population->get_offset_array()[j] = global_offset[i][j];
}
delete[] global_delta_cumulated_time_s;
delete[] global_delta_offset;
delete[] local_delta_cumulated_time_s;
delete[] local_delta_offset;
}
}
void Simulation::do_statusUpdate() {
if (mpi_rank == 0) {
cout<<"Status: "<<number_global_decays<<"/"<<(numberTotalDecays+popCalcDecays)<<endl;
}
}