diff --git a/src/mc_electron/asci_logger.cpp b/src/mc_electron/asci_logger.cpp index 9064003..cc77c5e 100644 --- a/src/mc_electron/asci_logger.cpp +++ b/src/mc_electron/asci_logger.cpp @@ -95,7 +95,7 @@ void ASCI_Logger::log_Particle(Particle *logPart, blitz::TinyVector ol } void ASCI_Logger::set_outdirName() { - string parname = get_parameter_filename(); + string parname = get_parameter_filenamepath(); stringstream dirname; int i = 1; do { diff --git a/src/mc_electron/average_container.h b/src/mc_electron/average_container.h index 96d380c..8b8a47e 100644 --- a/src/mc_electron/average_container.h +++ b/src/mc_electron/average_container.h @@ -54,21 +54,21 @@ template template void Average_Container::rec_calcMap(const T &subMap, std::deque Points) { double curVal, nextVal, avgVal; - typename T::const_iterator curItr, nextItr; - for (nextItr = ++(subMap.cbegin()); nextItr!= subMap.cend(); nextItr++) { - curItr = nextItr; - --curItr; - curVal = curItr->first; - nextVal = nextItr->first; + typename T::const_iterator curitr, nextitr; + for (nextitr = ++(subMap.cbegin()); nextitr!= subMap.cend(); nextitr++) { + curitr = nextitr; + --curitr; + curVal = curitr->first; + nextVal = nextitr->first; avgVal = curVal + (nextVal-curVal)/2; Points.push_back(avgVal); - rec_calcMap(curItr->second, Points); + rec_calcMap(curitr->second, Points); Points.pop_back(); } // insert last point as overflow value - curItr = (--subMap.cend()); - Points.push_back(curItr->first); - rec_calcMap(curItr->second, Points); + curitr = (--subMap.cend()); + Points.push_back(curitr->first); + rec_calcMap(curitr->second, Points); } diff --git a/src/mc_electron/base_container.h b/src/mc_electron/base_container.h index 5702b9a..3358a22 100644 --- a/src/mc_electron/base_container.h +++ b/src/mc_electron/base_container.h @@ -76,7 +76,7 @@ protected: private: template void rec_getSubMap(T_target *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) ; template void rec_getSubMap(T *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) ; - template typename T::iterator searchItrByStyle(T *subMap, double x, searchStyle searchMode) ; + template typename T::iterator searchitrByStyle(T *subMap, double x, searchStyle searchMode) ; protected: void checkEmpty() ; @@ -155,8 +155,8 @@ void Base_Container::addPoint(std::deque pointIn) { if (pointIn.size() != Dim+1) { std::stringstream ss; ss<<"Base_Container::addPoint: deque pointIn dimension missmatch.\n"; - for (std::deque::iterator Itr = pointIn.begin(); Itr!=pointIn.end(); Itr++) { - std::cout<<*Itr<<"\n"; + for (std::deque::iterator itr = pointIn.begin(); itr!=pointIn.end(); itr++) { + std::cout<<*itr<<"\n"; } throw MyException(ss.str()); } @@ -222,8 +222,8 @@ std::string Base_Container::getExportFileHeader() { template void Base_Container::rec_exportToCSV(const dim1Map &subMap, std::ofstream &exportStream, std::string preset) { - for (dim1Map::const_iterator Itr = subMap.cbegin();Itr!=subMap.cend();Itr++) { - exportStream<first<<"\t"<second<<"\n"; + for (dim1Map::const_iterator itr = subMap.cbegin();itr!=subMap.cend();itr++) { + exportStream<first<<"\t"<second<<"\n"; } } @@ -231,11 +231,11 @@ template template void Base_Container::rec_exportToCSV(const T &subMap, std::ofstream &exportStream, std::string preset) { std::stringstream ss; - for (typename T::const_iterator Itr = subMap.cbegin();Itr!=subMap.cend();Itr++) { + for (typename T::const_iterator itr = subMap.cbegin();itr!=subMap.cend();itr++) { ss.clear(); ss.str(""); - ss<first<<"\t"; - rec_exportToCSV(Itr->second, exportStream, ss.str()); + ss<first<<"\t"; + rec_exportToCSV(itr->second, exportStream, ss.str()); } } @@ -245,8 +245,8 @@ double Base_Container::get_value(std::deque Points, searchStyle s double x = Points.back(); Points.pop_back(); dim1Map *subMap = getSubMap(Points, searchMode); - dim1Map::const_iterator Itr = searchItrByStyle(subMap, x, searchMode); - return Itr->second; + dim1Map::const_iterator itr = searchitrByStyle(subMap, x, searchMode); + return itr->second; } template @@ -269,7 +269,7 @@ template void Base_Container::rec_getSubMap(T_target *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) { if (Points.size() != 0) { std::stringstream ss; - ss<<"Base_Container::get_subItr: Dimension Missmatch of Points\n"; + ss<<"Base_Container::get_subitr: Dimension Missmatch of Points\n"; throw MyException(ss.str()); } *targetMapPtr = subMap; @@ -280,30 +280,30 @@ template void Base_Container::rec_getSubMap(T *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) { double x = Points.front(); Points.pop_front(); - typename T::iterator Itr = searchItrByStyle(subMap, x, searchMode); - rec_getSubMap(&(Itr->second), targetMapPtr, Points, searchMode); + typename T::iterator itr = searchitrByStyle(subMap, x, searchMode); + rec_getSubMap(&(itr->second), targetMapPtr, Points, searchMode); } template template -typename T::iterator Base_Container::searchItrByStyle(T *subMap, double x, searchStyle searchMode) { - typename T::iterator Itr; +typename T::iterator Base_Container::searchitrByStyle(T *subMap, double x, searchStyle searchMode) { + typename T::iterator itr; if (searchMode == Find) { - Itr = subMap->find(x); + itr = subMap->find(x); } else if (searchMode == Lower_bound) { - Itr = subMap->lower_bound(x); + itr = subMap->lower_bound(x); } else if (searchMode == Lower_bound_Over) { - Itr = subMap->lower_bound(x); - if (Itr == subMap->end()) { - --Itr; + itr = subMap->lower_bound(x); + if (itr == subMap->end()) { + --itr; } } - if (Itr == subMap->end()) { + if (itr == subMap->end()) { std::stringstream ss; - ss<<"Base_Container::calcItr: Point is out of bound.\n"; + ss<<"Base_Container::calcitr: Point is out of bound.\n"; throw MyException(ss.str()); } - return Itr; + return itr; } diff --git a/src/mc_electron/edge_container.h b/src/mc_electron/edge_container.h index 9ea84f9..b785ba8 100644 --- a/src/mc_electron/edge_container.h +++ b/src/mc_electron/edge_container.h @@ -58,20 +58,20 @@ double Edge_Container::rec_interpolate(const T &subMap, std::dequesecond, subPoint); + if (upitr == subMap.cbegin()) { + return rec_interpolate(upitr->second, subPoint); } else { - typename T::const_iterator lowItr = upItr; - --lowItr; - if (upItr == subMap.cend()) { - return rec_interpolate(lowItr->second, subPoint); + typename T::const_iterator lowitr = upitr; + --lowitr; + if (upitr == subMap.cend()) { + return rec_interpolate(lowitr->second, subPoint); } else { - double low_x = lowItr->first; - double low_f = rec_interpolate(lowItr->second, subPoint); - double up_x = upItr->first; - double up_f = rec_interpolate(upItr->second, subPoint); + double low_x = lowitr->first; + double low_f = rec_interpolate(lowitr->second, subPoint); + double up_x = upitr->first; + double up_f = rec_interpolate(upitr->second, subPoint); double res = low_f + (x-low_x)/(up_x-low_x) * (up_f-low_f); return res; diff --git a/src/mc_electron/energeticPop_container.cpp b/src/mc_electron/energeticPop_container.cpp index 42e6c12..a386a64 100644 --- a/src/mc_electron/energeticPop_container.cpp +++ b/src/mc_electron/energeticPop_container.cpp @@ -17,11 +17,11 @@ void EnergeticPop_Container::set_Map() { double Ex_max = ceil(log10(E_max_eV)); double step = (Ex_max-Ex_min)/Epoints; double x, E_eV; - for (vector::iterator Itr = bins.begin(); Itr!=bins.end(); Itr++) { + for (vector::iterator itr = bins.begin(); itr!=bins.end(); itr++) { for (int i = 1; i<=Epoints; i++) { x = Ex_min + step*i; E_eV = pow(10,x); - addPoint(deque{(*Itr)[0], (*Itr)[1], (*Itr)[2], E_eV, 0}); + addPoint(deque{(*itr)[0], (*itr)[1], (*itr)[2], E_eV, 0}); } } } @@ -37,8 +37,8 @@ double EnergeticPop_Container::getDensity_m3(const vec3 &locXYZ_m, double physTi double EnergeticPop_Container::get_cumulated_time_s(vec3 zrphiCords) { dim1Map EMap = *getSubMap(ddeque{zrphiCords[0], zrphiCords[1], zrphiCords[2]}, Lower_bound); double cumTime_s = 0; - for (dim1Map::iterator Itr = EMap.begin(); Itr != EMap.end(); Itr++) { - cumTime_s +=Itr->second; + for (dim1Map::iterator itr = EMap.begin(); itr != EMap.end(); itr++) { + cumTime_s +=itr->second; } return cumTime_s; } @@ -62,11 +62,11 @@ void EnergeticPop_Container::upDensity (const Particle &upParticle, const vec3 & double vAbs_ms = upParticle.get_vAbs_ms(); double E_eV = upParticle.get_E_eV(); - dim4Map::iterator zItr_min = Map.lower_bound(z_min_m); - dim4Map::iterator zItr_max = Map.lower_bound(z_max_m); + dim4Map::iterator zitr_min = Map.lower_bound(z_min_m); + dim4Map::iterator zitr_max = Map.lower_bound(z_max_m); - for (dim4Map::iterator Itr = zItr_min;Itr != zItr_max;Itr++) { - Itr->second.lower_bound(r_m)->second.lower_bound(phi)->second.lower_bound(E_eV)->second += l_m/vAbs_ms; + for (dim4Map::iterator itr = zitr_min;itr != zitr_max;itr++) { + itr->second.lower_bound(r_m)->second.lower_bound(phi)->second.lower_bound(E_eV)->second += l_m/vAbs_ms; } } @@ -79,17 +79,17 @@ double EnergeticPop_Container::roll_Energy_eV(const vec3 &locXYZ_m, UniRNG *rngU double targetTime_s = rngUni->random() * get_cumulated_time_s(zrphiCords); double summedTime_s = 0; - dim1Map::iterator Itr = EMap.begin(); + dim1Map::iterator itr = EMap.begin(); do { - summedTime_s += Itr->second; - Itr++; + summedTime_s += itr->second; + itr++; } while(summedTime_s < targetTime_s); - Itr--; + itr--; - if (Itr == EMap.begin() ) { - return Itr->first; + if (itr == EMap.begin() ) { + return itr->first; } else { - double E_eV = ( (Itr->first) + ((--Itr)->first) )/2; + double E_eV = ( (itr->first) + ((--itr)->first) )/2; return E_eV; } } diff --git a/src/mc_electron/h5_file.cpp b/src/mc_electron/h5_file.cpp index 1fb8715..3d9a06d 100644 --- a/src/mc_electron/h5_file.cpp +++ b/src/mc_electron/h5_file.cpp @@ -15,20 +15,22 @@ using namespace std; -H5_File::H5_File() { - create(); +H5_File::H5_File(std::string name_extension) { + create(name_extension); h5part = H5_Particles(&h5_file); h5pop = H5_Populations(&h5_file); } -void H5_File::create() { - const string constH5Name = get_file_name(); - h5_file = H5Fcreate(constH5Name.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT); +void H5_File::create(std::string output_path) { + //const string constH5Name = get_file_name(); + h5_file = H5Fcreate(output_path.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT); } +/* std::string H5_File::get_file_name() { - string parname = get_parameter_filename(); + string parname = get_parameter_filenamepath(); stringstream H5name; + int i = 1; do { H5name.str(std::string()); @@ -36,4 +38,4 @@ std::string H5_File::get_file_name() { } while ( is_file(H5name.str()) ); return H5name.str(); } - +*/ diff --git a/src/mc_electron/h5_file.hpp b/src/mc_electron/h5_file.hpp index af0158f..3b3803e 100644 --- a/src/mc_electron/h5_file.hpp +++ b/src/mc_electron/h5_file.hpp @@ -18,12 +18,13 @@ class H5_File { H5_Populations h5pop; public: - H5_File(); + H5_File() {}; + H5_File(std::string output_path); H5_Particles *get_h5part() {return &h5part;} H5_Populations *get_h5pop() {return &h5pop;} private: - void create(); - std::string get_file_name(); + void create(std::string output_path); + //std::string get_file_name(); }; diff --git a/src/mc_electron/h5_particles.h b/src/mc_electron/h5_particles.h index 11b7e90..9aa09b1 100644 --- a/src/mc_electron/h5_particles.h +++ b/src/mc_electron/h5_particles.h @@ -51,7 +51,7 @@ inline H5_Particles::H5_Particles(hid_t *h5_file) { numberZBins = load_parameter("numberZBins"); set_logging(); set_zValues(); - print_logInfo(); + //print_logInfo(); create_h5gs(); } diff --git a/src/mc_electron/kinetic_population.h b/src/mc_electron/kinetic_population.h index 815a199..36e7c16 100644 --- a/src/mc_electron/kinetic_population.h +++ b/src/mc_electron/kinetic_population.h @@ -57,6 +57,11 @@ private: Lin_Scale get_r_scale(); Lin_Scale calc_phi_scale(); Log_Scale get_E_scale(); + +public: + int get_array_length(); + double *get_cumulated_time_s_array(); + double *get_offset_array(); }; @@ -81,18 +86,12 @@ inline double Kinetic_Population::get_density_m3(const vec3 &loc_m) { ss<<"Kinetic_Population::get_density_m3: loc_m outside of Katrin.\n"; throw MyException(ss.str()); } + double total_cumulated_time_s = cumulated_times_s.get_value_sum(loc_m); + double total_annhilated = number_annhilated.get_value_sum(loc_m); + double physical_time_s = timer->get_physTime_s(); - int num_E_bins = cumulated_times_s.get_num_E_bins(); - double *cumulated_time_s = cumulated_times_s.get_E_bins(loc_m); - double *offset = number_annhilated.get_E_bins(loc_m); - double total_cumulated_time_s = 0; - double total_offset = 0; - for (int i=0; iget_physTime_s(); double bin_size_m3 = bin_sizes.get_cell_volume_m3(loc_m); + loc_bins loc_bin = cumulated_times_s.get_loc_bins(loc_m); + int z_bin = loc_bin.z_bin; + int r_bin = loc_bin.r_bin; + int phi_bin = loc_bin.phi_bin; int num_E_bins = cumulated_times_s.get_num_E_bins(); - double *cumulated_time_s = cumulated_times_s.get_E_bins(loc_m); - double *offset = number_annhilated.get_E_bins(loc_m); + + double cumulated_time_s; + double offset; + double delta_density_m3; double cum_density_m3 = 0; - for (int i=0; itarget) { - double lower = cumulated_times_s.get_corresponding_E_lower_eV(i); - double upper = cumulated_times_s.get_corresponding_E_upper_eV(i); + double lower = cumulated_times_s.get_lower_E_boarder_eV(E_bin); + double upper = cumulated_times_s.get_upper_E_boarder_eV(E_bin); return lower + rngUni->random() * (upper-lower); } } @@ -223,13 +232,13 @@ inline void Kinetic_Population::set_bins(bin_count4 bin_counts, bin_content_vec int z_bin, r_bin, phi_bin, E_bin; double cumulated_time_s, offset; - for(bin_content_vec::iterator Itr = bin_contents.begin(); Itr != bin_contents.end(); Itr++) { - z_bin = (*Itr).z_bin; - r_bin = (*Itr).r_bin; - phi_bin = (*Itr).phi_bin; - E_bin = (*Itr).E_bin; - cumulated_time_s = (*Itr).cumulated_time_s; - offset = (*Itr).offset; + for(bin_content_vec::iterator itr = bin_contents.begin(); itr != bin_contents.end(); itr++) { + z_bin = (*itr).z_bin; + r_bin = (*itr).r_bin; + phi_bin = (*itr).phi_bin; + E_bin = (*itr).E_bin; + cumulated_time_s = (*itr).cumulated_time_s; + offset = (*itr).offset; cumulated_times_s.set_value_by_bin(cumulated_time_s, z_bin, r_bin, phi_bin, E_bin); number_annhilated.set_value_by_bin(offset, z_bin, r_bin, phi_bin, E_bin); } @@ -255,4 +264,16 @@ inline Log_Scale Kinetic_Population::get_E_scale() { } +inline int Kinetic_Population::get_array_length() { + return cumulated_times_s.get_total_data_size(); +} + +inline double *Kinetic_Population::get_cumulated_time_s_array() { + return cumulated_times_s.get_array(); +} + +inline double *Kinetic_Population::get_offset_array() { + return number_annhilated.get_array(); +} + #endif /* energetic_base_h */ diff --git a/src/mc_electron/main.cpp b/src/mc_electron/main.cpp index e9b1411..f6ee002 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -8,13 +8,76 @@ #include "simulation.hpp" + using namespace std; - -int main(int argc, const char * argv[]) { - clock_t start = clock(); +int main(int argc, char *argv[]) { + MPI_Init(&argc, &argv); + + int size, rank; + MPI_Comm_size(MPI_COMM_WORLD,&size); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + std::cout <<"CPU " < + +#include "simulation_serial.hpp" + + +using namespace std; + +int main(int argc, char *argv[]) { + + Simulation Sim; + clock_t start_time = clock(); + Sim.doit(); + + cout<<"Total Time: "<<(double(clock() - start_time) / CLOCKS_PER_SEC)<<" s\n"; +} diff --git a/src/mc_electron/makefile2 b/src/mc_electron/makefile2 index 75cad57..035c6b3 100644 --- a/src/mc_electron/makefile2 +++ b/src/mc_electron/makefile2 @@ -1,7 +1,15 @@ include $(HOMEBASE)/src/makeinclude/include.mak -SRCS := $(wildcard ../*.cpp) -OBJS := $(patsubst %.cpp,%.o,$(SRCS)) +OBJS := particle_looper.o \ + tcs_container.o dcs_theta_container.o dcs_ionization_container.o population3_container.o population4_container.o size_container.o \ + thermalPop_container.o energeticPop_container.o \ + particle.o injector.o timer.o katrin_fields.o \ + h5_file.o logger_builder.o population_load_saver.o \ + h5tb_particle_interface.o h5tb_population_interface.o h5tb_population_meta_interface.o \ + population_builder.o \ + mover_builder.o \ + interaction_builder.o BED_eT2_ionization.o uehara_eT2_elastic.o rutherford_elastic.o \ + terminator_builder.o CXXFLAGS = $(X_OFLAGS) -I.. $(X_INC) @@ -14,21 +22,15 @@ TARGETS = mcelectron all: $(TARGETS) -mcelectron: main.o simulation.o\ - tcs_container.o dcs_theta_container.o dcs_ionization_container.o population3_container.o population4_container.o size_container.o \ - thermalPop_container.o energeticPop_container.o \ - particle.o injector.o timer.o katrin_fields.o \ - h5_file.o logger_builder.o population_load_saver.o \ - h5tb_particle_interface.o h5tb_population_interface.o h5tb_population_meta_interface.o \ - population_builder.o \ - mover_builder.o \ - interaction_builder.o BED_eT2_ionization.o uehara_eT2_elastic.o rutherford_elastic.o \ - terminator_builder.o +mcelectron: main.o simulation.o $(OBJS) + $(MPICXX) -o $@ $^ $(LDFLAGS) $(LOADLIBES) + +mcelectron_serial: main_serial.o simulation_serial.o $(OBJS) $(CXX) -o $@ $^ $(LDFLAGS) $(LOADLIBES) %.o : %.cpp $(CXX) -c $< $(CXXFLAGS) -o $@ clean: - rm *.o mcelectron + rm *.o mcelectron mcelectron_serial diff --git a/src/mc_electron/manager_base.h b/src/mc_electron/manager_base.h index 552cbad..0f2e66c 100644 --- a/src/mc_electron/manager_base.h +++ b/src/mc_electron/manager_base.h @@ -40,8 +40,8 @@ inline void Manager_Base::add(T* add) { template inline bool Manager_Base::exists(species this_species) { - for (typename std::vector::iterator Itr = processes.begin(); Itr!=processes.end(); Itr++) { - if ((*Itr)->get_species() == this_species) { + for (typename std::vector::iterator itr = processes.begin(); itr!=processes.end(); itr++) { + if ((*itr)->get_species() == this_species) { return true; } } @@ -50,9 +50,9 @@ inline bool Manager_Base::exists(species this_species) { template inline T* Manager_Base::find(species this_species) { - for (typename std::vector::iterator Itr = processes.begin(); Itr!=processes.end(); Itr++) { - if ((*Itr)->get_species() == this_species) { - return *Itr; + for (typename std::vector::iterator itr = processes.begin(); itr!=processes.end(); itr++) { + if ((*itr)->get_species() == this_species) { + return *itr; } } std::stringstream ss; diff --git a/src/mc_electron/particle.cpp b/src/mc_electron/particle.cpp index 494e761..ca39fb8 100644 --- a/src/mc_electron/particle.cpp +++ b/src/mc_electron/particle.cpp @@ -42,7 +42,7 @@ std::string get_species_string(const species &spec) { return res; } -species get_species_from_string(const std::string &species_string) { +std::vector get_all_spieces_vector() { std::vector all; all.push_back(electron); all.push_back(tritium); @@ -50,9 +50,14 @@ species get_species_from_string(const std::string &species_string) { all.push_back(tritium2I); all.push_back(tritium2); all.push_back(tritium3I); - for (std::vector::iterator Itr = all.begin(); Itr!= all.end(); Itr++) { - if (get_species_string(*Itr) == species_string) { - return *Itr; + return all; +}; + +species get_species_from_string(const std::string &species_string) { + std::vector all = get_all_spieces_vector(); + for (std::vector::iterator itr = all.begin(); itr!= all.end(); itr++) { + if (get_species_string(*itr) == species_string) { + return *itr; } } stringstream ss; @@ -60,6 +65,8 @@ species get_species_from_string(const std::string &species_string) { throw MyException(ss.str()); } + + double get_species_mass(const species &spec) { double res; switch (spec) { diff --git a/src/mc_electron/particle.hpp b/src/mc_electron/particle.hpp index 2923627..9e3b0f5 100644 --- a/src/mc_electron/particle.hpp +++ b/src/mc_electron/particle.hpp @@ -20,7 +20,7 @@ typedef blitz::TinyVector vec3; enum species{electron, tritium, tritiumI, tritium2I, tritium2, tritium3I}; - +std::vector get_all_spieces_vector(); std::string get_species_string(const species &spec); species get_species_from_string(const std::string &species_string); double get_species_mass(const species &spec); diff --git a/src/mc_electron/particle_looper.cpp b/src/mc_electron/particle_looper.cpp new file mode 100644 index 0000000..447d727 --- /dev/null +++ b/src/mc_electron/particle_looper.cpp @@ -0,0 +1,72 @@ +// +// particle_looper.cpp +// mc_electron +// +// Created by Christian Reiling on 16.07.19. +// Copyright © 2019 Christian Reiling. All rights reserved. +// + +#include "particle_looper.hpp" + + +void Particle_Looper::init_managers() { + timer = Timer(&local_decays, &global_decays); + part_loggers = Particle_Logger_Builder::build(&h5f); + populations = Population_Builder::build(&timer, &rngUni, &rngNormal); + + injector = Injector(&rngUni, &rngNormal); + movers = Mover_Builder::build(&part_loggers, &populations); + interactions = Interaction_Builder::build(&populations, &rngUni); + terminators = Terminator_Builder::build(); + + pop_loggers = Population_Logger_Builder::build(&h5f, &populations, &timer); + pop_load_saver = Population_Load_Saver(&populations, &timer); +} + +void Particle_Looper::do_loops(int target_decays) { + std::queue particle_queue; + int number_decays = 0; + while ( (number_decays < target_decays) || (!particle_queue.empty()) ) { + if (particle_queue.empty()) { + pop_loggers.log(); + particle_queue.push(injector.injectElectron(get_new_id())); + number_decays++; + local_decays++; + } + process_first(&particle_queue); + } + pop_loggers.log(); +} + +void Particle_Looper::process_first(std::queue *particle_queue) { + Particle curPart = particle_queue->front(); + Mover_Base *moverPtr = movers.find(curPart.get_species()); + Interaction_Base *interactionPtr = interactions.find(curPart.get_species()); + Terminator_Base *terminatorPtr = terminators.find(curPart.get_species()); + + InteractionOut IntOut; + double macroSigma_m2; + vec3 oldLoc_m; + while (!terminatorPtr->checkTermination(curPart)) { + oldLoc_m = curPart.get_x_m(); + macroSigma_m2 = interactionPtr->get_macroSigma_m2(curPart); + moverPtr->move(&curPart, macroSigma_m2); + if (!terminatorPtr->checkTermination(curPart)) { + IntOut = interactionPtr->do_Interaction(&curPart); + for (int i=0; ipush(IntOut.get_new_particles()[i]); + particle_queue->back().set_Id(get_new_id()); + } + if (IntOut.get_delete_current()) { + break; + } + } + } + particle_queue->pop(); +} + +long int Particle_Looper::get_new_id() { + long int res = ID_offset + ID_counter; + ID_counter++; + return res; +} diff --git a/src/mc_electron/particle_looper.hpp b/src/mc_electron/particle_looper.hpp new file mode 100644 index 0000000..6853bbb --- /dev/null +++ b/src/mc_electron/particle_looper.hpp @@ -0,0 +1,89 @@ +// +// particle_looper.hpp +// mc_electron +// +// Created by Christian Reiling on 16.07.19. +// Copyright © 2019 Christian Reiling. All rights reserved. +// + +#ifndef particle_looper_hpp +#define particle_looper_hpp + +#include + +#include "h5_file.hpp" +#include "timer.hpp" + +#include "logger_builder.hpp" +#include "population_builder.hpp" + +#include "injector.hpp" +#include "mover_builder.hpp" +#include "interaction_builder.hpp" +#include "terminator_builder.hpp" + +#include "population_load_saver.hpp" + + +class Particle_Looper { + UniRNG rngUni; + NormalRNG rngNormal; + + H5_File h5f; + Timer timer; + + Particle_Logger_Manager part_loggers; + Population_Manager populations; + + Injector injector; + Mover_Manager movers; + Interaction_Manager interactions; + Terminator_Manager terminators; + + Population_Logger_Manager pop_loggers; + Population_Load_Saver pop_load_saver; + + long int ID_offset; + long int ID_counter = 0; + long int local_decays = 0; + long int global_decays = 0; + +public: + Particle_Looper() {}; + void init(long int seed, long int ID_offset, std::string output_path) { + rngUni.seed(seed); + rngNormal.seed(seed); + this->ID_offset = ID_offset; + h5f = H5_File(output_path); + init_managers(); + //pop_load_saver.load(); + } + ~Particle_Looper() { + //pop_load_saver.save(); + } +private: + void init_managers(); + +public: + void do_loops(int target_decays); +private: + void process_first(std::queue *particle_queue); + long int get_new_id(); + +public: + long int get_number_total_particles() { + return ID_counter; + } + + void set_global_decays(long int global_decays) { + this->global_decays = global_decays; + } + + Population_Manager *get_populations() { + return &populations; + } +}; + + + +#endif /* particle_looper_hpp */ diff --git a/src/mc_electron/population3_container.cpp b/src/mc_electron/population3_container.cpp index 58254af..954241f 100644 --- a/src/mc_electron/population3_container.cpp +++ b/src/mc_electron/population3_container.cpp @@ -17,27 +17,13 @@ Pop3_Container::Pop3_Container(int num_z_bins, int num_r_bins, int num_phi_bins) r_scale = Lin_Scale(0,r_WGTS_m, num_r_bins); phi_scale = Lin_Scale(0, 2*pi, num_phi_bins); - data = new double **[num_z_bins]; - for (int i = 0; i < num_z_bins; i++) { - data[i] = new double *[num_r_bins]; - for(int j = 0; j < num_r_bins; j++) { - data[i][j] = new double[num_phi_bins]; - for (int k = 0; k < num_phi_bins; k++) { - data[i][j][k] = 0; - } - } - } + r_offset = num_phi_bins; + z_offset = r_offset*num_r_bins; + total_data_size = num_z_bins*z_offset; + data = new double[total_data_size]; } Pop3_Container::~Pop3_Container() { - int z_dimension = z_scale.get_number_bins(); - int r_dimension = r_scale.get_number_bins(); - for (int i = 0; i < z_dimension; i++) { - for(int j = 0; j < r_dimension; j++) { - delete[] data[i][j]; - } - delete[] data[i]; - } delete[] data; }; @@ -45,19 +31,12 @@ Pop3_Container::Pop3_Container(const Pop3_Container& other) { z_scale = other.z_scale; r_scale = other.r_scale; phi_scale = other.phi_scale; - int z_dimension = z_scale.get_number_bins(); - int r_dimension = r_scale.get_number_bins(); - int phi_dimension = phi_scale.get_number_bins(); + z_offset = other.z_offset; + r_offset = other.r_offset; + total_data_size = other.total_data_size; - data = new double **[z_dimension]; - for (int i = 0; i < z_dimension; i++) { - data[i] = new double *[r_dimension]; - for(int j = 0; j < r_dimension; j++) { - data[i][j] = new double[phi_dimension]; - for (int k = 0; k < phi_dimension; k++) { - data[i][j][k] = other.data[i][j][k]; - } - } + for (int i=0; i::iterator Itr = all_species.begin(); Itr!= all_species.end(); Itr++) { - write_species(&save_stream, *Itr); + 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(); + string parameter_name = get_parameter_filenamepath(); stringstream save_path; save_path<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"; + for (bin_content_vec::iterator itr = bins.begin(); itr != bins.end(); itr++) { + *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"; } @@ -184,8 +184,8 @@ species Population_Load_Saver::get_species_line(std::ifstream *load_stream) { } 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) { + 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()); diff --git a/src/mc_electron/population_logger_base.h b/src/mc_electron/population_logger_base.h index 96eca12..d03f8eb 100644 --- a/src/mc_electron/population_logger_base.h +++ b/src/mc_electron/population_logger_base.h @@ -46,8 +46,8 @@ inline void Population_Logger::create_h5tb_meta(hid_t *h5g) { ss<get_species())<<"_meta_data"; h5tb_meta = H5TB_Population_Meta_Interface(ss.str(), h5g); meta_vec bins_meta_info = pop->get_bins_meta_info(); - for (meta_vec::iterator Itr = bins_meta_info.begin(); Itr != bins_meta_info.end(); Itr++) { - h5tb_meta.write(*Itr); + for (meta_vec::iterator itr = bins_meta_info.begin(); itr != bins_meta_info.end(); itr++) { + h5tb_meta.write(*itr); } } @@ -59,8 +59,8 @@ inline void Population_Logger::create_h5tb(hid_t *h5g) { inline void Population_Logger::log() { bin_content_vec bins_content = pop->get_bins_content(); pop_log_bin temp; - for (bin_content_vec::iterator Itr = bins_content.begin(); Itr != bins_content.end(); Itr++) { - temp = transform(*Itr); + for (bin_content_vec::iterator itr = bins_content.begin(); itr != bins_content.end(); itr++) { + temp = transform(*itr); h5tb.write(temp); } } diff --git a/src/mc_electron/read_system.h b/src/mc_electron/read_system.h index b800fbc..0fcb4f7 100644 --- a/src/mc_electron/read_system.h +++ b/src/mc_electron/read_system.h @@ -161,6 +161,11 @@ inline std::string get_time_string() { inline std::string get_homebase() { std::string homebase = get_enviroment_variable("HOMEBASE"); + if (homebase == "") { + std::stringstream ss; + ss<<"HOMEBASE not defined!\n."; + throw MyException (ss.str()); + } homebase = make_path(homebase); return homebase; } @@ -177,7 +182,7 @@ inline std::string remove_cat (std::string cat_string) { } } -inline std::string get_parameter_filename() { +inline std::string get_parameter_filenamepath() { std::string parameter_file_path = getenv("parpath"); std::string parameter_file_name = remove_cat(getenv("parfile")); return parameter_file_path + "/" + parameter_file_name; diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 209515f..d008273 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -10,147 +10,182 @@ 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"); -void Simulation::init() { - set_RNGs(); - - timer = Timer(&numberDecays); - part_loggers = Particle_Logger_Builder::build(&h5f); - populations = Population_Builder::build(&timer, &rngUni, &rngNormal); - - injector = Injector(&rngUni, &rngNormal); - movers = Mover_Builder::build(&part_loggers, &populations); - interactions = Interaction_Builder::build(&populations, &rngUni); - terminators = Terminator_Builder::build(); - - pop_loggers = Population_Logger_Builder::build(&h5f, &populations, &timer); - pop_load_saver = Population_Load_Saver(&populations, &timer); - - set_statusUpdater(); +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::set_RNGs() { - rngUni.seed(seed); - rngNormal.seed(seed); +void Simulation::init_pop_arrays() { + vector 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; ifind(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 0) ) { - if (!electronQue.empty()) { - throw MyException("Simulation::doTC6: electronQue should be empty"); - } - electronQue.push_back(Kat.injectElectron(numberTotalElectrons++, &rngUni, &rngNormal)); - do_interaction(&electronQue.front()); //parameter should only allow for ionizations - electronQue.pop_front(); // delete initial electron - Log.log_Electron(&electronQue.front(), vec3(0,0,0)); //should be daugther electron - electronQue.pop_front(); //delete daugther electron - } -}; - -void Simulation::do_interaction(Electron *interactElectron) { - vec3 newLoc_m = electronQue.front().get_x_m(); - if (Kat.check_Termination(*interactElectron)) { - stringstream ss; - ss<<"Simulation::do_interaction: Particle already terminated."; - throw MyException(ss.str()); - } - int process = interactElectron->roll_Interaction(Kat.get_T2_flowSpeed_ms(newLoc_m),&rngUni); - switch (process) { - case 1: // Elastic Scattering - interactElectron->do_elaScattering(Kat.roll_T2_v_ms(newLoc_m, &rngUni, &rngNormal) , &rngUni); - break; - case 2: // Ionization - electronQue.push_back( interactElectron->do_Ionization(Kat.roll_T2_v_ms(newLoc_m, &rngUni, &rngNormal), numberTotalElectrons, &rngUni) ); - numberTotalElectrons++; - // Update Ions... - break; - case 3: // Recombination - break; - default: - stringstream ss; - ss<<"Simulation::do_interaction: process="<::rec_calcMap(const dim1Map &subMap, std::dequefirst - preItr->first; - heigth = curItr->second/tot; + dim1Map::const_iterator preitr; + for (dim1Map::const_iterator curitr = (++subMap.cbegin()); curitr!=subMap.cend(); curitr++) { + preitr = curitr; + --preitr; + width = curitr->first - preitr->first; + heigth = curitr->second/tot; curVal += width*heigth; Points.push_back(curVal); - Points.push_back(curItr->first); + Points.push_back(curitr->first); Base_Container::addPoint(Points); Points.pop_back(); Points.pop_back(); @@ -88,9 +88,9 @@ void Stacked_Container::rec_calcMap(const dim1Map &subMap, std::deque template void Stacked_Container::rec_calcMap(const T &subMap, std::deque Points) { - for (typename T::const_iterator Itr = subMap.cbegin(); Itr!= subMap.cend(); Itr++) { - Points.push_back(Itr->first); - rec_calcMap(Itr->second, Points); + for (typename T::const_iterator itr = subMap.cbegin(); itr!= subMap.cend(); itr++) { + Points.push_back(itr->first); + rec_calcMap(itr->second, Points); Points.pop_back(); } } @@ -103,12 +103,12 @@ double Stacked_Container::calc_tot(const dim1Map &subMap) { width = subMap.begin()->first - lowerEdge; heigth = subMap.begin()->second; tot += width*heigth; - dim1Map::const_iterator preItr; - for (dim1Map::const_iterator curItr = (++subMap.cbegin()); curItr!=subMap.cend(); curItr++) { - preItr = curItr; - --preItr; - width = curItr->first - preItr->first; - heigth = curItr->second; + dim1Map::const_iterator preitr; + for (dim1Map::const_iterator curitr = (++subMap.cbegin()); curitr!=subMap.cend(); curitr++) { + preitr = curitr; + --preitr; + width = curitr->first - preitr->first; + heigth = curitr->second; tot += width*heigth; } return tot; diff --git a/src/mc_electron/thermalPop_container.cpp b/src/mc_electron/thermalPop_container.cpp index c7486dc..70057e8 100644 --- a/src/mc_electron/thermalPop_container.cpp +++ b/src/mc_electron/thermalPop_container.cpp @@ -13,8 +13,8 @@ using namespace std; void ThermalPop_Container::set_Map() { vector bins = get_bins(); - for (vector::iterator Itr = bins.begin(); Itr!=bins.end(); Itr++) { - addPoint(ddeque{(*Itr)[0], (*Itr)[1], (*Itr)[2], 0}); + for (vector::iterator itr = bins.begin(); itr!=bins.end(); itr++) { + addPoint(ddeque{(*itr)[0], (*itr)[1], (*itr)[2], 0}); } } @@ -43,11 +43,11 @@ void ThermalPop_Container::upDensity (const Particle &upParticle, const vec3 &ol double l_m = get_l_m(); double vAbs_ms = upParticle.get_vAbs_ms(); - dim3Map::iterator zItr_min = Map.lower_bound(z_min_m); - dim3Map::iterator zItr_max = Map.lower_bound(z_max_m); + dim3Map::iterator zitr_min = Map.lower_bound(z_min_m); + dim3Map::iterator zitr_max = Map.lower_bound(z_max_m); - for (dim3Map::iterator Itr = zItr_min;Itr != zItr_max;Itr++) { - Itr->second.lower_bound(r_m)->second.lower_bound(phi)->second += l_m/vAbs_ms; + for (dim3Map::iterator itr = zitr_min;itr != zitr_max;itr++) { + itr->second.lower_bound(r_m)->second.lower_bound(phi)->second += l_m/vAbs_ms; } } diff --git a/src/mc_electron/thermal_population.h b/src/mc_electron/thermal_population.h index 43c0133..9f19cd3 100644 --- a/src/mc_electron/thermal_population.h +++ b/src/mc_electron/thermal_population.h @@ -59,6 +59,10 @@ private: Lin_Scale get_r_scale(); Lin_Scale calc_phi_scale(); +public: + int get_array_length(); + double *get_cumulated_time_s_array(); + double *get_offset_array(); }; @@ -212,12 +216,12 @@ inline void Thermal_Population::set_bins(bin_count4 bin_counts, bin_content_vec } int z_bin, r_bin, phi_bin; double cumulated_time_s, offset; - for(bin_content_vec::iterator Itr = bin_contents.begin(); Itr != bin_contents.end(); Itr++) { - z_bin = (*Itr).z_bin; - r_bin = (*Itr).r_bin; - phi_bin = (*Itr).phi_bin; - cumulated_time_s = (*Itr).cumulated_time_s; - offset = (*Itr).offset; + for(bin_content_vec::iterator itr = bin_contents.begin(); itr != bin_contents.end(); itr++) { + z_bin = (*itr).z_bin; + r_bin = (*itr).r_bin; + phi_bin = (*itr).phi_bin; + cumulated_time_s = (*itr).cumulated_time_s; + offset = (*itr).offset; cumulated_times_s.set_value_by_bin(cumulated_time_s, z_bin, r_bin, phi_bin); number_annhilated.set_value_by_bin(offset, z_bin, r_bin, phi_bin); } @@ -253,5 +257,15 @@ inline Lin_Scale Thermal_Population::calc_phi_scale() { } } +inline int Thermal_Population::get_array_length() { + return cumulated_times_s.get_total_data_size(); +} +inline double *Thermal_Population::get_cumulated_time_s_array() { + return cumulated_times_s.get_array(); +} + +inline double *Thermal_Population::get_offset_array() { + return number_annhilated.get_array(); +} #endif /* Thermal_Population_h */ diff --git a/src/mc_electron/timer.cpp b/src/mc_electron/timer.cpp index 0debc19..249cbea 100644 --- a/src/mc_electron/timer.cpp +++ b/src/mc_electron/timer.cpp @@ -6,16 +6,34 @@ // Copyright © 2019 Christian Reiling. All rights reserved. // +#include + #include "timer.hpp" #include "read_system.h" -const double Timer::wgtsT2MaxDensity_m3 = load_parameter("wgtsT2MaxDensity_m3"); -double Timer::get_physTime_s() { - return double(*numTotDecays)/T2_activity_s + offset_time_s; +const double Timer::wgtsT2MaxDensity_m3 = load_parameter("wgtsT2MaxDensity_m3"); +using namespace std; + +/* +Timer::Timer(const Timer& other) { + T2_activity_s = other.T2_activity_s; + offset_time_s = other.offset_time_s; + local_decays = &(*other.local_decays); + global_decays = &(*other.global_decays); } +Timer& Timer::operator=(const Timer& other) { + Timer tmp(other); + swap(T2_activity_s, tmp.T2_activity_s); + swap(offset_time_s, tmp.offset_time_s); + local_decays = &(*tmp.local_decays); + global_decays = &(*tmp.global_decays); + return *this; +} +*/ + void Timer::set_T2_activity_s() { double r_m = r_WGTS_m; double l_m = l_WGTS_m; @@ -24,4 +42,10 @@ void Timer::set_T2_activity_s() { T2_activity_s = NT2*decayConstT2_s; } +double Timer::get_physTime_s() { + return double(calc_total_decays())/T2_activity_s + offset_time_s; +} +long int Timer::calc_total_decays() { + return (*local_decays + *global_decays); +} diff --git a/src/mc_electron/timer.hpp b/src/mc_electron/timer.hpp index faac343..210c4aa 100644 --- a/src/mc_electron/timer.hpp +++ b/src/mc_electron/timer.hpp @@ -16,20 +16,30 @@ class Timer { double T2_activity_s; double offset_time_s; - int *numTotDecays; + long int *local_decays; + long int *global_decays; public: - Timer() {numTotDecays = NULL;} - Timer(int *numTotDecays) { - this->numTotDecays = numTotDecays; + Timer () {}; + Timer(long int *local_decays, long int *global_decays) { + this->local_decays = local_decays; + this->global_decays = global_decays; offset_time_s = 0; set_T2_activity_s(); } - double get_physTime_s(); + //Timer(const Timer& other); + //Timer& operator=(const Timer& other); + void set_offset_time_s(double new_offset_time_s) {offset_time_s = new_offset_time_s;} private: void set_T2_activity_s(); + +public: + double get_physTime_s(); +private: + long int calc_total_decays(); + }; diff --git a/src/mc_electron/tritium2_population.h b/src/mc_electron/tritium2_population.h index c43fefe..434e00f 100644 --- a/src/mc_electron/tritium2_population.h +++ b/src/mc_electron/tritium2_population.h @@ -51,6 +51,11 @@ public: private: double calc_DensityTriangle(const double &z_m); double interpolate_red(const double &z_m, const double &z_min_m, const double &z_max_m, const double &red_min, const double &red_max); + +public: + int get_array_length() {return 0;} + double *get_cumulated_time_s_array() {return NULL;} + double *get_offset_array() {return NULL;} }; const double T2_Population::wgtsT2MaxDensity_m3 = load_parameter("wgtsT2MaxDensity_m3");