269 lines
8.9 KiB
C++
269 lines
8.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("DECAYS");
|
|
const long int Simulation::simulationStepWidth = load_parameter("STEP");
|
|
const long int Simulation::popCalcDecays = load_parameter("POP_DECAYS");
|
|
const long int Simulation::popCalcStep = load_parameter("POP_STEP");
|
|
|
|
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();
|
|
}
|
|
|
|
void Simulation::init_pop_arrays() { // Init C-arrays used for MPI-Allgather commands depending on Populations sizes
|
|
Population_Manager *populations = looper.get_populations();
|
|
|
|
number_pop_species = 0;
|
|
vector<species> all_species = get_all_species_vector();
|
|
for (vector<species>::iterator itr = all_species.begin(); itr!= all_species.end(); itr++) {
|
|
if (populations->find(*itr)->access_container() != nullptr) {
|
|
number_pop_species += 1 ;
|
|
}
|
|
}
|
|
|
|
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 int *[number_pop_species];
|
|
|
|
Population_Base *population;
|
|
|
|
int i = 0;
|
|
for (vector<species>::iterator itr = all_species.begin(); itr!= all_species.end(); itr++) {
|
|
if (populations->find(*itr)->access_container() != nullptr) {
|
|
pop_species[i] = *itr;
|
|
population = populations->find(*itr);
|
|
|
|
pop_array_lengths[i] = population->access_container()->data_size;
|
|
global_cum_time_s[i] = new double[pop_array_lengths[i]];
|
|
global_offset[i] = new int [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;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
long int Simulation::calc_ID_offset() { // Calc ID_offset for individual MPI process
|
|
return numberTotalDecays * 1000 * mpi_rank;
|
|
}
|
|
|
|
long int Simulation::calc_seed() { // Calc seed for individual MPI process
|
|
return seed + mpi_rank;
|
|
}
|
|
|
|
std::string Simulation::calc_outfile_path() { // Create .h5 file for individual MPI process
|
|
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() { // Calc directory _num for MPI World
|
|
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) { // Concatanate directory name for MPI World
|
|
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) { // Create directory
|
|
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() { // Calc h5 subindex for individual MPI process
|
|
int digits = to_string(mpi_size).length();
|
|
std::stringstream ss;
|
|
ss << std::setw(digits) << std::setfill('0') << mpi_rank;
|
|
return ss.str();
|
|
}
|
|
|
|
|
|
Simulation::~Simulation() {
|
|
deconstruct_pop_arrays();
|
|
}
|
|
|
|
void Simulation::deconstruct_pop_arrays() { // Deallocate memory properly
|
|
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;
|
|
}
|
|
|
|
|
|
void Simulation::doit() { // 2 Phase: 1. calc average densities, 2. actual simulate
|
|
do_cycle(popCalcDecays, popCalcStep, false);
|
|
if (mpi_rank == 0) {
|
|
cout<<"Population calculation done.\n";
|
|
}
|
|
do_cycle(numberTotalDecays, simulationStepWidth, true);
|
|
looper.flush();
|
|
print_term_stats();
|
|
};
|
|
|
|
// Split up total number_total to different processes, average densities after step_size, set particle logging on off
|
|
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);
|
|
}
|
|
}
|
|
|
|
// Run a number of decays and sync populations afterwards
|
|
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() { // Sync populations among MPI World
|
|
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() { // Sync new densities for each species and calculate new densities properly
|
|
Population_Manager *populations = looper.get_populations();
|
|
Population_Container *container;
|
|
double *global_delta_cumulated_time_s;
|
|
int *global_delta_offset;
|
|
double *local_delta_cumulated_time_s;
|
|
int *local_delta_offset;
|
|
int array_length;
|
|
for (int i = 0; i<number_pop_species; i++) {
|
|
container = populations->find(pop_species[i])->access_container();
|
|
array_length = pop_array_lengths[i];
|
|
if (array_length == 0) {
|
|
continue;
|
|
}
|
|
global_delta_cumulated_time_s = new double[array_length];
|
|
global_delta_offset = new int[array_length];
|
|
local_delta_cumulated_time_s = new double[array_length];
|
|
local_delta_offset = new int[array_length];
|
|
|
|
for (int j = 0; j<array_length; j++) {
|
|
local_delta_cumulated_time_s[j] = container->cum_times_s[j] - global_cum_time_s[i][j];
|
|
local_delta_offset[j] = container->offsets[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_INT, MPI_SUM, MPI_COMM_WORLD);
|
|
|
|
for (int j = 0; j<array_length; j++) {
|
|
global_cum_time_s[i][j] += global_delta_cumulated_time_s[j];
|
|
global_offset[i][j] += global_delta_offset[j];
|
|
container->cum_times_s[j] = global_cum_time_s[i][j];
|
|
container->offsets[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() { // Push current number of decays to std::cout
|
|
if (mpi_rank == 0) {
|
|
cout<<"Status: "<<number_global_decays<<"/"<<(numberTotalDecays+popCalcDecays)<<endl;
|
|
}
|
|
}
|
|
|
|
void Simulation::print_term_stats() {
|
|
Terminator_Manager *terminators = looper.get_terminators();
|
|
vector<species> all_species = get_all_species_vector();
|
|
Termination_Stats local_stats, global_stats;
|
|
int local_array[7], global_array[7];
|
|
for (vector<species>::iterator itr = all_species.begin(); itr!= all_species.end(); itr++) {
|
|
if (terminators->exists(*itr)) {
|
|
local_stats = terminators->find(*itr)->get_stats();
|
|
local_stats.to_array(local_array);
|
|
MPI_Allreduce(local_array, global_array, 7, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
|
|
global_stats.from_array(global_array);
|
|
if (mpi_rank == 0) {
|
|
std::cout<<"# "<<*itr<<"\n";
|
|
std::cout<<"n_total\t"<<global_stats.n_total<<"\n";
|
|
std::cout<<"n_rw\t"<<global_stats.n_rw<<"\n";
|
|
std::cout<<"n_dipole\t"<<global_stats.n_dipole<<"\n";
|
|
std::cout<<"n_radial\t"<<global_stats.n_radial<<"\n";
|
|
std::cout<<"n_max\t"<<global_stats.n_max<<"\n";
|
|
std::cout<<"n_energy\t"<<global_stats.n_energy<<"\n";
|
|
std::cout<<"n_reaction\t"<<global_stats.n_reaction<<"\n";
|
|
}
|
|
}
|
|
}
|
|
}
|