193 lines
5.8 KiB
C++
193 lines
5.8 KiB
C++
//
|
|
// simulation.cpp
|
|
// mc_electron
|
|
//
|
|
// Created by Christian Reiling on 20.09.18.
|
|
// Copyright © 2018 Christian Reiling. All rights reserved.
|
|
//
|
|
|
|
#include "simulation.hpp"
|
|
|
|
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");
|
|
|
|
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_spieces_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_"<<mpi_rank<<".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());
|
|
}
|
|
}
|
|
|
|
void Simulation::doit() {
|
|
long int decays_this_cpu = numberTotalDecays/mpi_size;
|
|
int overflow = numberTotalDecays/mpi_size;
|
|
if (overflow < mpi_rank) { // Split overflows equaly to left over CPUs.
|
|
decays_this_cpu += 1;
|
|
}
|
|
int num_cycles = decays_this_cpu/simulationStepWidth;
|
|
int num_remaining = decays_this_cpu%simulationStepWidth;
|
|
|
|
for (int i = 0; i<(num_cycles-1); i++) {
|
|
do_decays(simulationStepWidth);
|
|
}
|
|
do_decays(simulationStepWidth + num_remaining);
|
|
};
|
|
|
|
void Simulation::do_decays(int number_decays) {
|
|
looper.do_loops(number_decays);
|
|
number_local_decays += number_decays;
|
|
sync_cpus();
|
|
do_statusUpdate();
|
|
}
|
|
|
|
void Simulation::sync_cpus() {
|
|
std::cout<<mpi_rank<<"\n";
|
|
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<<endl;
|
|
}
|
|
}
|
|
|