256 lines
8.5 KiB
C++
256 lines
8.5 KiB
C++
//
|
|
// population_load_saver.cpp
|
|
// mc_electron
|
|
//
|
|
// Created by Christian Reiling on 11.06.19.
|
|
// Copyright © 2019 Christian Reiling. All rights reserved.
|
|
//
|
|
|
|
#include "population_load_saver.hpp"
|
|
|
|
using namespace std;
|
|
|
|
|
|
void Population_Load_Saver::save() {
|
|
ofstream save_stream(get_save_path());
|
|
write_overhead(&save_stream);
|
|
|
|
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) {
|
|
write_species(&save_stream, *itr);
|
|
}
|
|
}
|
|
|
|
if (all_species.size()<1) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::save: No Populations to save. \n";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
for (vector<species>::iterator itr = all_species.begin(); itr!= all_species.end(); itr++) {
|
|
write_species(&save_stream, *itr);
|
|
}
|
|
save_stream.close();
|
|
}
|
|
|
|
string Population_Load_Saver::get_save_path() {
|
|
string parameter_name = get_parameter_filenamepath();
|
|
stringstream save_path;
|
|
save_path<<parameter_name<<"_save_populations.dat";
|
|
return save_path.str();
|
|
}
|
|
|
|
void Population_Load_Saver::write_overhead(ofstream *save_stream) {
|
|
*save_stream<<"# File is automatically created. File contains Populations Density. To be loaded as start offset.\n";
|
|
int num_species = calc_number_pops();
|
|
*save_stream<<num_species<<"\t # Number of Populations \n";
|
|
|
|
*save_stream<<timer->get_time_s()<<"\t # Offset Time in seconds \n";
|
|
|
|
*save_stream<<z_RW_m<<"\t";
|
|
*save_stream<<z_WGTS_start_m<<"\t";
|
|
*save_stream<<z_WGTS_end_m<<"\t";
|
|
*save_stream<<r_WGTS_m<<"\t";
|
|
*save_stream<<" # 1. rear wall pos 2. WGTS start pos 3. WGTS end pos 4. WGTS radius all pos in m \n";
|
|
|
|
*save_stream<<load_parameter("T2_MAXDENS")<<"\t";
|
|
*save_stream<<load_parameter("TEMP")<<"\t";
|
|
*save_stream<<" #1. wgtsT2MaxDensity_m3 2. wgtsTemp_K \n";
|
|
|
|
*save_stream<<"# Start of populations data \n";
|
|
*save_stream<<"# z_bin \t r_bin \t phi_bin \t E_bin \t cumulated_times_s \t number_annhilated \n";
|
|
}
|
|
|
|
void Population_Load_Saver::write_species(ofstream *save_stream, species this_species) {
|
|
Population_Container *cont = populations->find(this_species)->access_container();
|
|
|
|
*save_stream<<get_species_string(this_species)<<"\t #species name \n";
|
|
|
|
*save_stream<<cont->scales.z.get_number_bins()<<"\t";
|
|
*save_stream<<cont->scales.r.get_number_bins()<<"\t";
|
|
*save_stream<<cont->scales.phi.get_number_bins()<<"\t";
|
|
*save_stream<<cont->E_scale.get_number_bins()<<"\t";
|
|
*save_stream<<"#number_bins of 1. z_scale 2. r_scale 3. phi_scale 4. E_scale \n";
|
|
|
|
for (int i_z = 0; i_z<cont->scales.z.get_number_bins(); i_z++) {
|
|
for (int i_r = 0; i_z<cont->scales.r.get_number_bins(); i_z++) {
|
|
for (int i_phi = 0; i_z<cont->scales.phi.get_number_bins(); i_z++) {
|
|
for (int i_E = 0; i_z<cont->E_scale.get_number_bins(); i_z++) {
|
|
int bin = cont->calc_data_bin(Bins{i_z, i_r, i_phi}, i_E);
|
|
*save_stream<<i_z<<"\t"<<i_r<<"\t"<<i_phi<<"\t"<<i_E<<"\t";
|
|
*save_stream<<cont->cum_times_s[bin]<<"\t";
|
|
*save_stream<<cont->offsets[bin]<<"\n";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*save_stream<<"end_of_species \n";
|
|
}
|
|
|
|
int Population_Load_Saver::calc_number_pops() {
|
|
int num_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) {
|
|
num_species += 1 ;
|
|
}
|
|
}
|
|
return num_species;
|
|
}
|
|
|
|
void Population_Load_Saver::load() {
|
|
// Maybe change whole system to h5, therefore allowing sloppy style
|
|
string load_path = get_enviroment_variable("pop_load_path");
|
|
if (load_path.empty()) {
|
|
cout<<"No File specified for loading Population start values.\n";
|
|
return;
|
|
}
|
|
ifstream load_stream(load_path);
|
|
if (!load_stream.good()) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::load: Could not open file.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
goto_nextline(&load_stream); //ignore first line
|
|
int number_populations = get_num_populations_line(&load_stream);
|
|
if (number_populations != calc_number_pops()) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::load: Number Population missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
double offset_time_s = get_offset_time_line(&load_stream);
|
|
timer->set_offset_time_s(offset_time_s);
|
|
check_katrin_geo_line(&load_stream);
|
|
check_katrin_T2_line(&load_stream);
|
|
goto_nextline(&load_stream);
|
|
goto_nextline(&load_stream);
|
|
|
|
vector<species> check_vector;
|
|
species current_species;
|
|
for (int i = 0; i< number_populations; i++) {
|
|
current_species = get_species_line(&load_stream);
|
|
append_check_vector(&check_vector, current_species);
|
|
load_species(&load_stream, current_species);
|
|
}
|
|
load_stream.close();
|
|
cout<<"Loading Offset Values for Populations sucessful.\n";
|
|
}
|
|
|
|
void Population_Load_Saver::goto_nextline(ifstream *load_stream) {
|
|
load_stream->ignore(numeric_limits<streamsize>::max(), '\n');
|
|
}
|
|
|
|
int Population_Load_Saver::get_num_populations_line(ifstream *load_stream) {
|
|
int num_populations;
|
|
*load_stream>>num_populations;
|
|
goto_nextline(load_stream);
|
|
if (num_populations < 1) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::get_num_populations_line: num_populations < 1.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
return num_populations;
|
|
}
|
|
|
|
double Population_Load_Saver::get_offset_time_line(ifstream *load_stream) {
|
|
double offset_time_s;
|
|
*load_stream>>offset_time_s;
|
|
goto_nextline(load_stream);
|
|
return offset_time_s;
|
|
}
|
|
|
|
void Population_Load_Saver::check_katrin_geo_line(ifstream *load_stream) {
|
|
double z_RW_m, z_WGTSstart_m, z_WGTSend_m, radius_m;
|
|
*load_stream>>z_RW_m;
|
|
if ( z_RW_m != z_RW_m) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::check_katrin_geo_line: z_RW missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
*load_stream>>z_WGTSstart_m;
|
|
if ( z_WGTSstart_m != z_WGTS_start_m) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::check_katrin_geo_line: z_WGTSstart missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
*load_stream>>z_WGTSend_m;
|
|
if ( z_WGTSend_m != z_WGTS_end_m) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::check_katrin_geo_line: z_WGTSend missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
*load_stream>>radius_m;
|
|
if ( radius_m != r_WGTS_m) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::check_katrin_geo_line: radius missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
goto_nextline(load_stream);
|
|
}
|
|
|
|
void Population_Load_Saver::check_katrin_T2_line(ifstream *load_stream) {
|
|
double wgtsT2MaxDensity_m3, wgtsTemp_K;
|
|
*load_stream>>wgtsT2MaxDensity_m3;
|
|
if ( wgtsT2MaxDensity_m3 != load_parameter("T2_MAXDENS")) {
|
|
cout<<"Warning while loading populations: wgtsT2MaxDensity_m3 missmatch.\n";
|
|
}
|
|
*load_stream>>wgtsTemp_K;
|
|
if ( wgtsTemp_K != load_parameter("TEMP")) {
|
|
cout<<"Warning while loading populations: wgtsTemp_K missmatch.\n";
|
|
}
|
|
goto_nextline(load_stream);
|
|
}
|
|
|
|
species Population_Load_Saver::get_species_line(ifstream *load_stream) {
|
|
string species_string;
|
|
*load_stream>>species_string;
|
|
goto_nextline(load_stream);
|
|
return get_species_from_string(species_string);
|
|
}
|
|
|
|
void Population_Load_Saver::append_check_vector(vector<species> *check_vector, species add_species) {
|
|
for (vector<species>::iterator itr=check_vector->begin(); itr!=check_vector->end(); itr++) {
|
|
if (*itr == add_species) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::append_check_vector: species already loaded.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
}
|
|
check_vector->push_back(add_species);
|
|
}
|
|
|
|
void Population_Load_Saver::load_species(ifstream *load_stream, species add_species) {
|
|
Population_Container *cont = populations->find(add_species)->access_container();
|
|
|
|
int num_z_bins, num_r_bins, num_phi_bins, num_E_bins;
|
|
*load_stream>>num_z_bins>>num_r_bins>>num_phi_bins>>num_E_bins;
|
|
if ( (num_z_bins != cont->scales.z.get_number_bins())
|
|
|| (num_r_bins != cont->scales.r.get_number_bins())
|
|
|| (num_phi_bins != cont->scales.phi.get_number_bins())
|
|
|| (num_E_bins != cont->E_scale.get_number_bins()) ) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::load_species: Number bins in scales missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
int z_bin, r_bin, phi_bin, E_bin;
|
|
for (int i = 0; i< cont->data_size; i++) {
|
|
*load_stream>>z_bin>>r_bin>>phi_bin>>E_bin;
|
|
if (i != cont->calc_data_bin(Bins{}, E_bin)) {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::load_species: Input Data bin missmatch.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
*load_stream>>cont->cum_times_s[i]>>cont->offsets[i];
|
|
}
|
|
string end_of_species;
|
|
*load_stream>>end_of_species;
|
|
if (end_of_species!="end_of_species") {
|
|
stringstream ss;
|
|
ss<<"Population_Load_Saver::load_species: Missmatch in number of lines..\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
}
|