// // 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() { if (all_species.size()<1) { stringstream ss; ss<<"Population_Load_Saver::save: No Populations to save. \n"; throw MyException(ss.str()); } ofstream save_stream(get_save_path()); write_overhead(&save_stream); 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_filename(); stringstream save_path; save_path<get_physTime_s()<<"\t # Offset Time in seconds \n"; *save_stream<find(this_species); bin_count4 number_bins = pop->get_bin_count4(); bin_content_vec bins = pop->get_bins_content(); *save_stream<z_bin<<"\t"; *save_stream<r_bin<<"\t"; *save_stream<phi_bin<<"\t"; *save_stream<E_bin<<"\t"; *save_stream<cumulated_time_s<<"\t"; *save_stream<offset<<"\n"; } *save_stream<<"end_of_species \n"; } void Population_Load_Saver::load() { string load_path = get_enviroment_variable("pop_load_path"); if (load_path.empty()) { cout<<"No File specified for Population default loading.\n"; return; } std::ifstream load_stream(load_path); if (!load_stream.good()) { std::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); 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(std::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) { std::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(std::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(std::ifstream *load_stream) { double z_RW_m, z_WGTSstart_m, z_WGTSend_m, radius_m; *load_stream>>z_RW_m; if ( z_RW_m != Katrin_Geo::get_z_RW_m()) { std::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 != Katrin_Geo::get_z_WGTSstart_m()) { std::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 != Katrin_Geo::get_z_WGTSend_m()) { std::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 != Katrin_Geo::get_radius_m()) { std::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(std::ifstream *load_stream) { double wgtsT2MaxDensity_m3, wgtsTemp_K, wgtsT2FlowSpeed_ms; *load_stream>>wgtsT2MaxDensity_m3; if ( wgtsT2MaxDensity_m3 != load_parameter("wgtsT2MaxDensity_m3")) { cout<<"Warning while loading populations: wgtsT2MaxDensity_m3 missmatch.\n"; } *load_stream>>wgtsTemp_K; if ( wgtsTemp_K != load_parameter("wgtsTemp_K")) { cout<<"Warning while loading populations: wgtsTemp_K missmatch.\n"; } *load_stream>>wgtsT2FlowSpeed_ms; if ( wgtsT2FlowSpeed_ms != load_parameter("wgtsT2FlowSpeed_ms")) { cout<<"Warning while loading populations: wgtsT2FlowSpeed_ms missmatch.\n"; } goto_nextline(load_stream); } species Population_Load_Saver::get_species_line(std::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(std::vector *check_vector, species add_species) { for (std::vector::iterator Itr=check_vector->begin(); Itr!=check_vector->end(); Itr++) { if (*Itr == add_species) { std::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(std::ifstream *load_stream, species add_species) { bin_count4 bin_counts; *load_stream>>bin_counts[0]; *load_stream>>bin_counts[1]; *load_stream>>bin_counts[2]; *load_stream>>bin_counts[3]; int number_content_lines = 1; for (int i = 0; i<4;i++) { number_content_lines *= (bin_counts[i]+1); } bin_content_vec bin_contents; pop_bin_content temp; for (int i = 0; i>temp.z_bin; *load_stream>>temp.r_bin; *load_stream>>temp.phi_bin; *load_stream>>temp.E_bin; *load_stream>>temp.cumulated_time_s; *load_stream>>temp.offset; goto_nextline(load_stream); bin_contents.push_back(temp); } string end_of_species; *load_stream>>end_of_species; if (end_of_species!="end_of_species") { std::stringstream ss; ss<<"Population_Load_Saver::load_species: Missmatch in number of lines..\n"; throw MyException(ss.str()); } populations->find(add_species)->set_bins(bin_counts, bin_contents); }