// // 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 all_species = get_all_species_vector(); for (vector::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::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<get_time_s()<<"\t # Offset Time in seconds \n"; *save_stream<find(this_species)->access_container(); *save_stream<scales.z.get_number_bins()<<"\t"; *save_stream<scales.r.get_number_bins()<<"\t"; *save_stream<scales.phi.get_number_bins()<<"\t"; *save_stream<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_zscales.z.get_number_bins(); i_z++) { for (int i_r = 0; i_zscales.r.get_number_bins(); i_z++) { for (int i_phi = 0; i_zscales.phi.get_number_bins(); i_z++) { for (int i_E = 0; i_zE_scale.get_number_bins(); i_z++) { int bin = cont->calc_data_bin(Bins{i_z, i_r, i_phi}, i_E); *save_stream<cum_times_s[bin]<<"\t"; *save_stream<offsets[bin]<<"\n"; } } } } *save_stream<<"end_of_species \n"; } int Population_Load_Saver::calc_number_pops() { int num_species = 0; vector all_species = get_all_species_vector(); for (vector::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 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::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 *check_vector, species add_species) { for (vector::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()); } }