diff --git a/src/makeinclude/Darwin-amd64 b/src/makeinclude/Darwin-amd64 index 4838fce..aa97692 100644 --- a/src/makeinclude/Darwin-amd64 +++ b/src/makeinclude/Darwin-amd64 @@ -79,7 +79,7 @@ INSTALL = install ########################################################################### # 4. Weitere Variablen, die (evtl!) irgendwo zur Makefile-Steuerung verwendet # werden (teilweise redundant, teilweise kaputt) -CPU_TYPE := $(shell cat /proc/cpuinfo|grep Quad| wc -l ) + $(shell cat /proc/cpuinfo|grep i7| wc -l ) +# CPU_TYPE := $(shell cat /proc/cpuinfo|grep Quad| wc -l ) + $(shell cat /proc/cpuinfo|grep i7| wc -l ) # Mehrere kaputte svn-makros REVNUM = 0 diff --git a/src/mc_electron/bedionization.cpp b/src/mc_electron/bedionization.cpp index 4f109a0..a685b77 100644 --- a/src/mc_electron/bedionization.cpp +++ b/src/mc_electron/bedionization.cpp @@ -9,7 +9,7 @@ #include "bedionization.hpp" -#include "myexeption.h" +#include "functions.hpp" #include "constants.hpp" #include @@ -18,7 +18,7 @@ using namespace std; const double BED_Ionization::S_m2 = 5.4721e-20; -const double BED_Ionization::B_eV = Constants().EionT2_eV; +const double BED_Ionization::B_eV = EionT2_eV; const double BED_Ionization::u = 1.664; const double BED_Ionization::N = 2; const double BED_Ionization::Ni = 1.173; diff --git a/src/mc_electron/constants.cpp b/src/mc_electron/constants.cpp deleted file mode 100644 index 8fab7ab..0000000 --- a/src/mc_electron/constants.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// constants.cpp -// mc_electron -// -// Created by Christian Reiling on 09.02.19. -// Copyright © 2019 Christian Reiling. All rights reserved. -// - - -#include "constants.hpp" - - -// Natural Constants -constant Constants::pi = 3.1415; -constant Constants::c_ms = 299792458; -constant Constants::kb_eVK = 8.6173303e-5; -constant Constants::e_C = 1.602176462e-19; -constant Constants::rBohr_m = 0.52917721067e-10; -constant Constants::ERyd_eV = 13.605693009; - -// Particle Properties -constant Constants::me_eV = 0.511e6; -constant Constants::re_m = 2.817940322719e-15; -constant Constants::ZT2 = 2.; -constant Constants::mT2_eV = 5.61886404234e9; -constant Constants::EionT2_eV = 15.43; - -// Beta Decay -constant Constants::BetaDecayConst = 1.2072343416696292e-47; -constant Constants::TritiumDecayEndpoint_eV = 18.574e3; - diff --git a/src/mc_electron/constants.hpp b/src/mc_electron/constants.hpp index d6f246e..308a968 100644 --- a/src/mc_electron/constants.hpp +++ b/src/mc_electron/constants.hpp @@ -12,29 +12,24 @@ typedef const double constant; -class Constants { -public: - Constants() {}; - // Natural Constants - static constant pi; - static constant c_ms; - static constant kb_eVK; - static constant e_C; - static constant rBohr_m; - static constant ERyd_eV; - - // Particle Properties - static constant me_eV; - static constant re_m; - static constant ZT2; - static constant mT2_eV; - static constant EionT2_eV; - - // Beta Decay - static constant BetaDecayConst; - static constant TritiumDecayEndpoint_eV; +// Natural Constants +constant pi = 3.1415; +constant c_ms = 299792458; +constant kb_eVK = 8.6173303e-5; +constant e_C = 1.602176462e-19; +constant rBohr_m = 0.52917721067e-10; +constant ERyd_eV = 13.605693009; -}; +// Particle Properties +constant me_eV = 0.511e6; +constant re_m = 2.817940322719e-15; +constant ZT2 = 2.; +constant mT2_eV = 5.61886404234e9; +constant EionT2_eV = 15.43; + +// Beta Decay +constant BetaDecayConst = 1.2072343416696292e-47; +constant TritiumDecayEndpoint_eV = 18.574e3; #endif /* constants_hpp */ diff --git a/src/mc_electron/electron.cpp b/src/mc_electron/electron.cpp index 98e259c..698dcbe 100644 --- a/src/mc_electron/electron.cpp +++ b/src/mc_electron/electron.cpp @@ -8,10 +8,10 @@ #include "electron.hpp" -#include "myexeption.h" - using namespace std; +const int Electron::ionOn = getParameter("ionOn"); +const int Electron::elaOn = getParameter("elaOn"); double Electron::get_SigmaTotal_m2(vec3 speedT2_ms) { vector Sigmas = get_SigmaVector_m2(speedT2_ms); @@ -24,13 +24,13 @@ double Electron::get_SigmaTotal_m2(vec3 speedT2_ms) { vector Electron::get_SigmaVector_m2(vec3 speedT2_ms) { vector Sigmas; - if (Particle::elaOn == 1) { + if (elaOn == 1) { Sigmas.push_back(get_elaSigma_m2(speedT2_ms)); } else { Sigmas.push_back(0.); } - if (Particle::ionOn == 1) { + if (ionOn == 1) { Sigmas.push_back(get_ionSigma_m2(speedT2_ms)); } else { Sigmas.push_back(0.); @@ -94,7 +94,9 @@ double Electron::roll_elaTheta(vec3 speedT2_ms, ranlib::Uniform *rngUni) double betaLab = get_vecAbs(speedLab_ms)/c_ms; double elaEta = get_elaEta(speedLab_ms); double const1 = 2*pi/get_elaSigma_m2(speedT2_ms)*ZT2*(ZT2+1)*re_m*re_m*(1- pow(betaLab,2) )* pow(betaLab,-4); - double theta = acos( pow( rngUni->random()/const1-pow(2*elaEta,-1) , -1 ) +1+2*elaEta); + double fac = pow( rngUni->random()/const1-pow(2*elaEta,-1) , -1 ) +1+2*elaEta; + double theta = acos( fac); + return(theta); } @@ -208,3 +210,7 @@ double Electron::roll_IonTheta2(double T_eV, double W_eV, ranlib::Uniform get_SigmaVector_m2(vec3 speedT2_ms); int roll_Interaction(vec3 speedT2_ms, ranlib::Uniform *rngUni); +private: + static const int ionOn; + static const int elaOn; // Elastic Scattering public: @@ -31,19 +37,19 @@ protected: double roll_elaTheta(vec3 speedT2_ms, ranlib::Uniform *rngUni); void do_elaKinetics(vec3 speedT2_ms, double theta, double phi); - // Ionization - public: double get_ionSigma_m2(vec3 speedT2_ms); Electron do_Ionization(vec3 speedT2_ms, int newId, ranlib::Uniform *rngUni); protected: double roll_ionW_eV(double T_eV, ranlib::Uniform *rngUni); - double roll_IonTheta1(double T_eV, double W_eV, ranlib::Uniform *rngUni); double roll_IonTheta2(double T_eV, double W_eV, ranlib::Uniform *rngUni); + + // Testing Container + //static Func_Container elaTCS_container; }; diff --git a/src/mc_electron/function2D_container.cpp b/src/mc_electron/function2D_container.cpp deleted file mode 100644 index 019812a..0000000 --- a/src/mc_electron/function2D_container.cpp +++ /dev/null @@ -1,105 +0,0 @@ -// -// function2D_container.cpp -// mc_electron -// -// Created by Christian Reiling on 15.03.19. -// Copyright © 2019 Christian Reiling. All rights reserved. -// - -#include "function2D_container.hpp" - -#include "myexeption.h" - -#include -#include - -using namespace std; - - -Func2D_Container::Func2D_Container() { - -} - -Func2D_Container::Func2D_Container(std::string loadFilePath) { - readFromCSV(loadFilePath); -} - -double Func2D_Container::interpolate(double x, double y) { - // Method might be improved, since first check for x bins, and afterwards check for y bins, so theoretically not choosing closest neighbour in terms of y but giving priority to x-closeness. - // Avoid Mistakes by having complete set of y-data and uniform grid or improve code later. - - - if (Map.empty()) { - throw MyException("Func2D_Container: Container is empty."); - } - - if (Map.begin() == Map.lower_bound(x) ) { - return (Map.begin()->second.interpolate(y)); - } else if (Map.end() == Map.lower_bound(x)){ - return ( (--Map.end())->second.interpolate(y) ); - } - - dim2Map::iterator lowAnker_Itr = --Map.lower_bound(x); - dim2Map::iterator upAnker_Itr = Map.lower_bound(x); - double lowAnker_x = lowAnker_Itr->first; - double lowAnker_fxy = lowAnker_Itr->second.interpolate(y); - double upAnker_x = upAnker_Itr->first; - double upAnker_fxy = upAnker_Itr->second.interpolate(y); - - double res = lowAnker_fxy + (x-lowAnker_x)/(upAnker_x-lowAnker_x) * (upAnker_fxy-lowAnker_fxy); - return (res); - -} - -void Func2D_Container::add_ankerpoint(double x, double y, double fxy) { - if ( Map.find(x)==Map.end() ) { - Func_Container Temp_Container; - Temp_Container.add_ankerpoint(y, fxy); - Map.insert(dim2Pair (x, Temp_Container)); - } else { - try { - Map.find(x)->second.add_ankerpoint(y,fxy); - } catch (MyException& caught) { - stringstream ss; - ss<<"Func2D_Container::add_ankerpoint: Error from "< -#include -#include - - -typedef std::map dim2Map; -typedef std::pair dim2Pair; - -class Func2D_Container { -public: - Func2D_Container(); - Func2D_Container(std::string loadFilePath); - double interpolate(double x, double y); - - void add_ankerpoint(double x, double y, double fxy); -private: - - - dim2Map Map; - void readFromCSV(std::string loadFilePath); - -} ; - -#endif /* function2D_container_hpp */ diff --git a/src/mc_electron/function_container.cpp b/src/mc_electron/function_container.cpp index 3c29dc1..f20ffcd 100644 --- a/src/mc_electron/function_container.cpp +++ b/src/mc_electron/function_container.cpp @@ -8,16 +8,13 @@ #include "function_container.hpp" -#include "myexeption.h" +#include "functions.hpp" #include #include using namespace std; -Func_Container::Func_Container() { - -} Func_Container::Func_Container(std::string loadFilePath) { readFromCSV(loadFilePath); @@ -100,26 +97,87 @@ void Func_Container::printAnkerpoints() { } } +// Func2D_Container +Func2D_Container::Func2D_Container(std::string loadFilePath) { + readFromCSV(loadFilePath); +} -bool checkIgnoreLine(string lineFromFile) { - if (lineFromFile[0] =='#') { - return true; - } else if ( lineFromFile.empty() ) { - return true; - } else { - return false; +double Func2D_Container::interpolate(double x, double y) { + // Method might be improved, since first check for x bins, and afterwards check for y bins, so theoretically not choosing closest neighbour in terms of y but giving priority to x-closeness. + // Avoid Mistakes by having complete set of y-data and uniform grid or improve code later. + + + if (Map.empty()) { + throw MyException("Func2D_Container: Container is empty."); } + if (Map.begin() == Map.lower_bound(x) ) { + return (Map.begin()->second.interpolate(y)); + } else if (Map.end() == Map.lower_bound(x)){ + return ( (--Map.end())->second.interpolate(y) ); + } + + dim2Map::iterator lowAnker_Itr = --Map.lower_bound(x); + dim2Map::iterator upAnker_Itr = Map.lower_bound(x); + double lowAnker_x = lowAnker_Itr->first; + double lowAnker_fxy = lowAnker_Itr->second.interpolate(y); + double upAnker_x = upAnker_Itr->first; + double upAnker_fxy = upAnker_Itr->second.interpolate(y); + + double res = lowAnker_fxy + (x-lowAnker_x)/(upAnker_x-lowAnker_x) * (upAnker_fxy-lowAnker_fxy); + return (res); + } -void removeLeadingWhites(string &lineFromFile) { - static const string WHITESPACE = " \n\r\t\f\v"; - size_t start = lineFromFile.find_first_not_of(WHITESPACE); - lineFromFile = (start == std::string::npos) ? "" : lineFromFile.substr(start); + +void Func2D_Container::add_ankerpoint(double x, double y, double fxy) { + if ( Map.find(x)==Map.end() ) { + Func_Container Temp_Container; + Temp_Container.add_ankerpoint(y, fxy); + Map.insert(dim2Pair (x, Temp_Container)); + } else { + try { + Map.find(x)->second.add_ankerpoint(y,fxy); + } catch (MyException& caught) { + stringstream ss; + ss<<"Func2D_Container::add_ankerpoint: Error from "< +#include using namespace std; - -double get_vecAbs(vec3 vec) { - return( sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2)) ); +// Read Parameters from file +double getParameter(string parName) { + string parpath = getEnv("parpath"); + string parfile = getEnv("parfile"); + if (parpath == "") { + throw MyException("Parameter::getParameter: Environment variable parpath not defined."); + } + if (parfile == "") { + throw MyException("Parameter::getParameter: Environment variable parfile not defined."); + } + makePath(parpath); + string loadFilePath = parpath + parfile; + ifstream fileStream(loadFilePath); + + if (!fileStream.good()) { + stringstream ss; + ss<<"Parameter::getParameter: Could not open file.\n loadFilePath =\t"<>parNameTemp && ss>>parTemp) { + if (parNameTemp == parName) { + fileStream.close(); + return (parTemp); + } + } else { + if (!lineReadError) { + cout<<"Parameter::getParameter: Line in parameter file faulty. \n path =\t"<=1) { stringstream ss; ss<<"get_Ekin_eV_from_speed(): Input speed v="< 0) ? phi_new_candidates[2] : 2*Constants::pi + phi_new_candidates[2]; + phi_new_candidates[3] = pi - phi_new_candidates[2]; + phi_new_candidates[2] = (phi_new_candidates[2] > 0) ? phi_new_candidates[2] : 2*pi + phi_new_candidates[2]; for (int i=0;i<3;i++) { for (int j=i+1;j<4;j++) { if (phi_new_candidates[i]-phi_new_candidates[j] < 0.0001) { phi_new = phi_new_candidates[i]; - goto endloops; } } } -endloops: if (phi_new == -1) { - for (int j = 0;j<3;j++) { - cout<<"phi1c["< *rngUni) { vec3 dir; - double phi = 2*Constants::pi*rngUni->random(); - double theta = Constants::pi*rngUni->random(); + double phi = 2*pi*rngUni->random(); + double theta = pi*rngUni->random(); dir = sin(theta)*cos(phi) , sin(theta)*sin(phi) , cos(theta); return(dir); } double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit *rngNormal) { - double C = Constants::kb_eVK*Temp_K/mass_eV; + double C = kb_eVK*Temp_K/mass_eV; double beta = 0; double sum = 0; for (int i=0;i<3;i++) { @@ -141,7 +232,6 @@ double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit *rn return((get_gamma_from_beta(beta)-1)*mass_eV); } - double roll_Rejectionmethod(double (*pdf)(double), double xmin, double xmax, double pmax, ranlib::Uniform *rngUni) { // get Energy distribution using Rejection Method double xcan,eta1,eta2; diff --git a/src/mc_electron/functions.hpp b/src/mc_electron/functions.hpp index 423fa5b..94c12cc 100644 --- a/src/mc_electron/functions.hpp +++ b/src/mc_electron/functions.hpp @@ -10,7 +10,7 @@ #define functions_hpp -#include "constants.hpp" +#include #include #include @@ -20,16 +20,37 @@ typedef blitz::TinyVector vec3; -double get_vecAbs(vec3 vec); +// Custom Error Type +struct MyException : public std::exception +{ + std::string s; + MyException(std::string errorMessage) : s(errorMessage) {} + ~MyException() throw () {} + const char* what() const throw() { return s.c_str(); } +}; +// Read Parameters from file +double getParameter(std::string parName); + +//String Manipulations for Read in from files +std::string getEnv (std::string envName); // string version of C function getenv. +void makePath (std::string &path); // append '/' if necessary to path. +bool checkIgnoreLine(std::string lineFromFile); // true if line is empty or starts with # +void removeLeadingWhites(std::string &lineFromFile); +void replaceDelimiter(std::string &lineFromFile); // replaces delimiter (';'',''=') with whitespace + +// Calculate Kinematic Relations double get_Speed_from_Ekin_eV(double Ekin_eV, double mass_eV); double get_gamma_from_Ekin_eV(double Ekin_eV, double mass_eV); double get_beta_from_gamma(double gamma); double get_Ekin_eV_from_speed(double Speed, double mass_eV); double get_gamma_from_beta(double beta); -vec3 get_Newdirection(vec3 oldDir, double theta,double phi); +// Operations on 3 dimensional Vectors +double get_vecAbs(vec3 vec); +vec3 get_Newdirection(vec3 oldDir, double theta,double phi); // Calculates new Direction after Interaction with (theta, phi) +// Functions for Random Sampling vec3 roll_uniform_direction(ranlib::Uniform *rngUni); double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit *rngNormal); double doRejectionMethod(double pdf(double), double xmin, double xmax, double pmax, ranlib::Uniform *rngUni); diff --git a/src/mc_electron/katrin.cpp b/src/mc_electron/katrin.cpp index 7d34c5b..9bf2ff1 100644 --- a/src/mc_electron/katrin.cpp +++ b/src/mc_electron/katrin.cpp @@ -9,7 +9,6 @@ #include "katrin.hpp" -#include "myexeption.h" #include "functions.hpp" #include "sstream" @@ -17,9 +16,19 @@ using namespace std; -Katrin::Katrin() { - set_cutoffEnergy_eV(); -} +const double Katrin::wgtsMagField_T = getParameter("wgtsMagField_T"); +const double Katrin::wgtsLength_m = getParameter("wgtsLength_m"); +const double Katrin::wgtsRadius_m = getParameter("wgtsRadius_m"); +const double Katrin::wgtsT2MaxDensity_m3 = getParameter("wgtsT2MaxDensity_m3"); +const double Katrin::wgtsTemp_K = getParameter("wgtsTemp_K"); +const double Katrin::wgtsT2FlowSpeed_ms = getParameter("wgtsT2FlowSpeed_ms"); + +const int Katrin::injectionMethod = getParameter("injectionMethod"); +const double Katrin::injectionEnergy_eV = getParameter("injectionEnergy_eV"); + +const double Katrin::dt_s = getParameter("dt_s"); + +const double Katrin::cutoffEnergy_eV = getParameter("cutoffEnergy_eV"); vec3 Katrin::getMagField_T(vec3 loc_m) { vec3 MagField_T; @@ -35,7 +44,7 @@ double Katrin::getTritiumDensity_m3(vec3 loc_m) { double mid_m = wgtsLength_m/2; double zProjection_m = (loc_m[2] < mid_m) ? loc_m[2] : wgtsLength_m - loc_m[2] ; double reductget_ionFactor = zProjection_m/mid_m; - return(reductget_ionFactor * wgtsT2Density_m3); + return(reductget_ionFactor * wgtsT2MaxDensity_m3); } vec3 Katrin::getTotSpeedT2_ms(vec3 loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal) { @@ -57,7 +66,6 @@ vec3 Katrin::getTempSpeedT2_ms(vec3 loc_m, ranlib::Uniform *rngUni, ranl } Electron Katrin::injectElectron(int newId, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal) { - double r_m = wgtsRadius_m*sqrt(rngUni->random()); double phi = rngUni->random()*2*pi; @@ -71,10 +79,10 @@ Electron Katrin::injectElectron(int newId, ranlib::Uniform *rngUni, ranl Ekin_eV = roll_FermiEnergy_eV(rngUni); break; case 1: //Delta - Ekin_eV = deltaKinEnergy_eV; + Ekin_eV = injectionEnergy_eV; break; case 2: //Maxwell Boltzman - Ekin_eV = roll_MBEnergy(MBTemp_K,me_eV, rngNormal); + Ekin_eV = roll_MBEnergy(injectionEnergy_eV/kb_eVK,me_eV, rngNormal); break; default: throw MyException("Katrin::injectElectron: Invalid Injection Method."); @@ -115,7 +123,7 @@ double Katrin::get_FermiFunction(double Z, double Ekin_eV) { } // Move Electrons and Log -void Katrin::do_BorisPushMovement(Electron *moveElectron, double distance_m, Logger *Log) { +void Katrin::do_BorisPushMovement(Electron *moveElectron, double distance_m, H5_Logger *Log) { // Rework, with Logging and Reflection at wall vec3 loc_m; @@ -160,7 +168,7 @@ void Katrin::doBoris_WallInteraction(Electron *moveElectron) { } } -void Katrin::doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 *oldLoc_m, Logger *Log) { +void Katrin::doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 *oldLoc_m, H5_Logger *Log) { double zDir = moveElectron->get_Speed_ms()[2] < 0 ? -1 : 1; if (*oldzDir != zDir) { Log->log_Electron(moveElectron, *oldLoc_m); @@ -170,7 +178,7 @@ void Katrin::doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 return; } -void Katrin::do_DriftApproximationMovement(Electron *moveElectron, double distance_m, Logger *Log) { +void Katrin::do_DriftApproximationMovement(Electron *moveElectron, double distance_m, H5_Logger *Log) { // Drift Approximation: Assume constant B and only movement out in z=0, z=wgtsLength_m... vec3 oldLoc_m = moveElectron->get_Location_m(); vec3 newLoc_m = oldLoc_m; @@ -197,7 +205,7 @@ double Katrin::getDrift_znew_m(double distance_m, Electron *moveElectron) { return (znew_m); } -void Katrin::doDrift_WallInteraction(vec3 *oldLoc_m, double *znew_m, Electron *moveElectron, Logger *Log) { +void Katrin::doDrift_WallInteraction(vec3 *oldLoc_m, double *znew_m, Electron *moveElectron, H5_Logger *Log) { if (*znew_m >= wgtsLength_m) { // Moveing into Spectrometer and reversing Speed. vec3 reverseLoc_m = *oldLoc_m; reverseLoc_m[2] = wgtsLength_m; @@ -268,14 +276,3 @@ bool Katrin::check_WallTermination(Electron checkElectron) { } -void Katrin::set_cutoffEnergy_eV() { - if (cutoffMode==0) { - cutoffEnergy_eV = cutoffEnergySet_eV; - } else if (cutoffMode==1) { - cutoffEnergy_eV = kb_eVK*wgtsTemp_K*cutoffFactor; - } else { - stringstream ss; - ss<<"Katrin::set_cutoffEnergy_eV(): cutoffMode= "< -class Katrin: private Parameter, private Constants { +class Katrin { public: - Katrin(); + Katrin() {}; +// Katrin Properties +public: vec3 getMagField_T(vec3 loc_m); vec3 getElField_Vm(vec3 loc_m); double getTritiumDensity_m3(vec3 loc_m); @@ -31,8 +32,15 @@ public: vec3 getTotSpeedT2_ms(vec3 loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal); vec3 getFlowSpeedT2_ms(vec3 loc_m); vec3 getTempSpeedT2_ms(vec3 loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal); +private: + static const double wgtsMagField_T; + static const double wgtsLength_m; + static const double wgtsRadius_m; + static const double wgtsT2MaxDensity_m3; + static const double wgtsTemp_K; + static const double wgtsT2FlowSpeed_ms; - // Injection + // Electron Injection public: Electron injectElectron(int newId, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal); private: @@ -40,16 +48,22 @@ private: double get_FermiDecaySpectrumDensity(double Ekin_eV); double get_FermiFunction(double Z, double Ekin_eV); + static const int injectionMethod; + static const double injectionEnergy_eV; + // Move Electrons public: double get_MFP_m(Electron *moveElectron); - void do_BorisPushMovement(Electron *moveElectron, double distance_m, Logger *Log); - void do_DriftApproximationMovement(Electron *moveElectron, double distance_m, Logger *Log); + void do_BorisPushMovement(Electron *moveElectron, double distance_m, H5_Logger *Log); + void do_DriftApproximationMovement(Electron *moveElectron, double distance_m, H5_Logger *Log); private: double getDrift_znew_m(double MFP_m, Electron *moveElectron); - void doDrift_WallInteraction(vec3 *oldLoc_m, double *znew_m, Electron *moveElectron, Logger *Log); + void doDrift_WallInteraction(vec3 *oldLoc_m, double *znew_m, Electron *moveElectron, H5_Logger *Log); void doBoris_WallInteraction(Electron *moveElectron); - void doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 *oldLoc_m, Logger *Log); + void doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 *oldLoc_m, H5_Logger *Log); + + static const double dt_s; + // Terminate Electrons public: @@ -59,8 +73,8 @@ private: bool check_EnergyTermination(Electron checkElectron); bool check_WallTermination(vec3 loc_m); bool check_WallTermination(Electron checkElectron); - double cutoffEnergy_eV; - void set_cutoffEnergy_eV(); + + static const double cutoffEnergy_eV; }; #endif /* katrin_hpp */ diff --git a/src/mc_electron/logger.cpp b/src/mc_electron/logger.cpp index 14e6e4c..19abe9a 100644 --- a/src/mc_electron/logger.cpp +++ b/src/mc_electron/logger.cpp @@ -8,48 +8,30 @@ #include "logger.hpp" -#include "myexeption.h" +#include "hdf5.h" +#include "hdf5_hl.h" +#include #include #include using namespace std; -Logger::Logger() { + +const int Base_Logger::numberZBins = getParameter("numberZBins"); +const double Base_Logger::wgtsLength_m = getParameter("wgtsLength_m"); + +// Base_Logger +Base_Logger::Base_Logger() { if (numberZBins == 0) { cout<<"Logging turned off."< oldLoc_m) { - if (!logging) { - return; - } - - // Assumption: no change of sign in v_z - if (numberZBins == -1) { - outStream[0]<<*logElectron<get_Location_m()[2]; - double zmin = min(zold,znew); - double zmax = max(zold,znew); - - for (int i=0;i=zmin && zLogValues[i]<=zmax) { - outStream[i]<<*logElectron< oldLoc_m) { + if (!logging) { + return; + } + logElectron logElectron_buffer = Electron_to_logElectron(inElectron); + // Assumption: no change of sign in v_z + if (numberZBins == -1) { + H5TBappend_records(particle_zH5G[0], "electrons", 1, particle_electronH5TB_size, particle_electronH5TB_offset, particle_electronH5TB_sizes, &logElectron_buffer ); + } else { + double zold = oldLoc_m[2]; + double znew = inElectron->get_Location_m()[2]; + double zmin = min(zold,znew); + double zmax = max(zold,znew); + + for (int i=0;i=zmin && zLogValues[i]<=zmax) { + H5TBappend_records(particle_zH5G[i], "electrons", 1, particle_electronH5TB_size, particle_electronH5TB_offset, particle_electronH5TB_sizes, &logElectron_buffer ); // Location wrong, because it s the new Location. + } + } + } + + + + return; +} + +void H5_Logger::create_particle_electronH5TBs() { + if (numberZBins == -1) { + particle_electronH5T = new hid_t[1]; + particle_electronH5T[0] = H5TBmake_table( "Electron Table", particle_zH5G[0], "electrons", 8, 0, + particle_electronH5TB_size, particle_electronH5TB_fieldNames, particle_electronH5TB_offset, particle_electronH5TB_fieldTypes, + particle_electronH5TB_chunkSize, NULL, 0, NULL); + } else { + particle_electronH5T = new hid_t[numberZBins]; + for (int i = 0; i < numberZBins; i++) + { + particle_electronH5T[i] = H5TBmake_table( "Electron Table", particle_zH5G[i], "electrons",8,0, + particle_electronH5TB_size, particle_electronH5TB_fieldNames, particle_electronH5TB_offset, particle_electronH5TB_fieldTypes, + particle_electronH5TB_chunkSize, NULL, 0, NULL); + } + } +} + +logElectron H5_Logger::Electron_to_logElectron(Electron *inElectron) { + logElectron res; + res.Id = inElectron->get_Id(); + res.parentId = inElectron->get_ParentId(); + res.x_m = inElectron->get_Location_m()[0]; + res.y_m = inElectron->get_Location_m()[1]; + res.z_m = inElectron->get_Location_m()[2]; + res.vx_ms = inElectron->get_Speed_ms()[0]; + res.vy_ms = inElectron->get_Speed_ms()[1]; + res.vz_ms = inElectron->get_Speed_ms()[2]; + return (res); +} + +// ASCI_Logger +ASCI_Logger::ASCI_Logger() : Base_Logger() { + set_outdirName(); + create_outdir(); + set_outStream(); +} + +void ASCI_Logger::log_Electron(Electron *inElectron, blitz::TinyVector oldLoc_m) { + if (!logging) { + return; + } + + // Assumption: no change of sign in v_z + if (numberZBins == -1) { + outStream[0]<<*inElectron<get_Location_m()[2]; + double zmin = min(zold,znew); + double zmax = max(zold,znew); + + for (int i=0;i=zmin && zLogValues[i]<=zmax) { + outStream[i]<<*inElectron< oldLoc_m); -private: bool logging; - std::string outdirName; std::vector zLogValues; - std::ofstream *outStream; void set_zLogValues(); - void set_outStream(); - void create_outdirName(); + + std::string getparName(); std::string getTimeString(); + std::string remove_catEnding (std::string inString); + bool isFile(std::string fileName); + bool isDir(std::string dirPath); +}; + +// H5_Logger: Logging to HDF5 file format. +class H5_Logger : private Base_Logger { + + +public: + H5_Logger(); + + +private: + // File + std::string outH5FName; + hid_t outH5F; + + void set_outH5FName(); + void create_outH5F(); + + // Groups + hid_t particleH5G; + hid_t *particle_zH5G; + + void create_H5Gs(); + + // Electrons +public: + void test_log(); + void log_Electron(Electron *inElectron,blitz::TinyVector oldLoc_m); +private: + static const size_t particle_electronH5TB_size; + static const char *particle_electronH5TB_fieldNames[8]; + static const size_t particle_electronH5TB_offset[8]; + static const hid_t particle_electronH5TB_fieldTypes[8]; + static const hsize_t particle_electronH5TB_chunkSize; + static const size_t particle_electronH5TB_sizes[8]; + + hid_t *particle_electronH5T; + + void create_particle_electronH5TBs(); + + logElectron Electron_to_logElectron(Electron *inElectron); +}; + +// ASCI_Logger: Log to ASCI-File. Only Electron Logger. Not very efficient. +class ASCI_Logger : private Base_Logger { +public: + ASCI_Logger(); + + void log_Electron(Electron *inElectron,blitz::TinyVector oldLoc_m); +private: + std::string outdirName; + std::ofstream *outStream; + + void set_outdirName(); + void create_outdir(); + void set_outStream(); }; diff --git a/src/mc_electron/main.cpp b/src/mc_electron/main.cpp index 749e823..3b68f8a 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -8,7 +8,6 @@ #include "simulation.hpp" -#include "myexeption.h" using namespace std; diff --git a/src/mc_electron/makefile2 b/src/mc_electron/makefile2 index 310e491..8bded81 100644 --- a/src/mc_electron/makefile2 +++ b/src/mc_electron/makefile2 @@ -8,13 +8,15 @@ CXXFLAGS = $(X_OFLAGS) -I.. $(X_INC) LDFLAGS = $(X_LIB) -LOADLIBES = -lblitz -lm +LOADLIBES = -lblitz -lm -lhdf5 -lhdf5_hl TARGETS = mcelectron all: $(TARGETS) -mcelectron: main.o parameter.o functions.o constants.o katrin.o logger.o simulation.o \ +mcelectron: main.o simulation.o\ + functions.o function_container.o \ + katrin.o logger.o \ particle.o electron.o bedionization.o $(CXX) -o $@ $^ $(LDFLAGS) $(LOADLIBES) diff --git a/src/mc_electron/myexeption.h b/src/mc_electron/myexeption.h deleted file mode 100644 index a60d7ec..0000000 --- a/src/mc_electron/myexeption.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// myexeption.h -// mc_electron -// -// Created by Christian Reiling on 01.03.19. -// Copyright © 2019 Christian Reiling. All rights reserved. -// - -#ifndef myexeption_h -#define myexeption_h - -#include - -using namespace std; - -struct MyException : public exception -{ - string s; - MyException(string ss) : s(ss) {} - ~MyException() throw () {} // Updated - const char* what() const throw() { return s.c_str(); } -}; - -#endif /* myexeption_h */ diff --git a/src/mc_electron/parameter.cpp b/src/mc_electron/parameter.cpp deleted file mode 100644 index 334de9e..0000000 --- a/src/mc_electron/parameter.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// -// parameter.cpp -// mc_electron -// -// Created by Christian Reiling on 26.02.19. -// Copyright © 2019 Christian Reiling. All rights reserved. -// - - -#include "parameter.hpp" - -#include "myexeption.h" - -#include -#include -#include -#include -#include - -using namespace std; - - -Parameter::Parameter() { - // Simulation Specs - - seed = (int)getValue("seed"); - numberTotalDecays = (int)getValue("numberTotalDecays"); - dt_s = getValue("dt_s"); - injectionMethod = (int)getValue("injectionMethod"); - deltaKinEnergy_eV = getValue("deltaKinEnergy_eV"); - MBTemp_K = getValue("MBTemp_K"); - elaOn = (int)getValue("elaOn"); - ionOn = (int)getValue("ionOn"); - propagationMethod = (int)getValue("propagationMethod"); - - // Cutoff - cutoffMode = (int)getValue("cutoffMode"); - cutoffEnergySet_eV = getValue("cutoffEnergySet_eV"); - cutoffFactor = getValue("cutoffFactor"); - maxNumberInteractionsPerElectron = getValue("maxNumberInteractionsPerElectron"); - - // Katrin Specs - wgtsMagField_T = getValue("wgtsMagField_T"); - wgtsLength_m = getValue("wgtsLength_m"); - wgtsRadius_m = getValue("wgtsRadius_m"); - wgtsT2Density_m3 = getValue("wgtsT2Density_m3"); - wgtsTemp_K = getValue("wgtsTemp_K"); - wgtsT2FlowSpeed_ms = getValue("wgtsT2FlowSpeed_ms"); - - // Output - numberZBins = getValue ("numberZBins"); - -} - - -double Parameter::getValue(string varName) { - char* ext = (char*)("cat"); - char* str = strdup(varName.c_str()); - double temp; - char dummy[256]; - char fname[256],*parfile; - FILE *fcat; - - if (getenv("parpath") == NULL) - { - throw MyException("Parameter::value: Environment variable parpath not defined."); - } - strcpy(fname, getenv("parpath")); - parfile = getenv("parfile"); - if (parfile == NULL) - { - throw MyException("Parameter::value: Environment variable parfile not defined."); - } - if (!ext) - { - ext = (char*)("cat"); - } - sprintf(fname,"%s/%s.%s",getenv("parpath"),getenv("parfile"), ext); - if (!(fcat = fopen(fname, "r"))) - { - stringstream ss; - ss<<"Parameter::value: Can not open file " < - -class Parameter { -public: - Parameter(); - -protected: - //Simulation Specs - int seed ; - long int numberTotalDecays; - double dt_s; - int injectionMethod; // 0 = Beta Decay, 1 = Delta, 2 = Maxwell Boltzmann - double deltaKinEnergy_eV; - double MBTemp_K; - int elaOn; - int ionOn; - int propagationMethod; // 0 = Drift Approximation, 1 = Borispush - - // Cutoff - int cutoffMode; // 0 = Energy from Parameter, 1 = WGTSTemperature*cutoffFactor - double cutoffEnergySet_eV; - double cutoffFactor; - int maxNumberInteractionsPerElectron; - - //KATRIN Specs - double wgtsMagField_T; - double wgtsLength_m; - double wgtsRadius_m; - double wgtsT2Density_m3; - double wgtsTemp_K; - double wgtsT2FlowSpeed_ms; - - // Output - int numberZBins; - -private: - double getValue(std::string varName); -}; - - -#endif diff --git a/src/mc_electron/particle.hpp b/src/mc_electron/particle.hpp index 1fb5a86..c9db340 100644 --- a/src/mc_electron/particle.hpp +++ b/src/mc_electron/particle.hpp @@ -9,7 +9,6 @@ #ifndef particle_hpp #define particle_hpp -#include "parameter.hpp" #include "constants.hpp" #include "functions.hpp" @@ -22,7 +21,7 @@ typedef enum species{undef, electron, positron, proton, tritium, tritium2, heliu std::ostream& operator<<(std::ostream& os, const species& SpecOut); -class Particle: protected Parameter, protected Constants{ +class Particle { public: Particle(double mass_eV, double charge_e,vec3 loc_m, vec3 speed_ms, double idnum, double parentIdnum, species spec); @@ -32,8 +31,8 @@ protected: vec3 x_m; vec3 v_ms; species s; - unsigned long int Id; - unsigned long int parentId; + unsigned int Id; + unsigned int parentId; public: diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 0e98486..1516e55 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -9,13 +9,19 @@ #include "simulation.hpp" -#include "myexeption.h" +#include "constants.hpp" +#include "functions.hpp" #include "fstream" using namespace std; +const long int Simulation::seed = getParameter("seed"); +const long int Simulation::numberTotalDecays = getParameter("numberTotalDecays"); +const int Simulation::propagationMethod = getParameter("propagationMethod"); +const int Simulation::maxInteractions = getParameter("maxInteractions"); + Simulation::Simulation() { numberTotalElectrons = 0; numberDecays = 0; @@ -45,11 +51,11 @@ void Simulation::doit() { }; bool Simulation::check_NumberInteractions(int &numberInteractionsThisElectron) { - if (maxNumberInteractionsPerElectron == 0) { + if (maxInteractions == 0) { return(false); } - if (numberInteractionsThisElectron < maxNumberInteractionsPerElectron) { + if (numberInteractionsThisElectron < maxInteractions) { numberInteractionsThisElectron++; return(false); } else { @@ -82,7 +88,7 @@ void Simulation::do_statusUpdate() { } } -void Simulation::move_electron(Electron *moveElectron, Logger *Log) { +void Simulation::move_electron(Electron *moveElectron, H5_Logger *Log) { double MFP_m = Kat.get_MFP_m(&electronQue.front()); if (propagationMethod==0) { Kat.do_DriftApproximationMovement(moveElectron, MFP_m, Log); @@ -125,10 +131,10 @@ void Simulation::do_interaction(Electron *interactElectron) { void Simulation::parameterCheck_TC6() { if ( - injectionMethod == 1 && - elaOn == 0 && - ionOn == 1 && - numberZBins == -1 + getParameter("injectionMethod") == 1 && + getParameter("elaOn") == 0 && + getParameter("ionOn") == 1 && + getParameter("numberZBins") == -1 ) { return; }else { diff --git a/src/mc_electron/simulation.hpp b/src/mc_electron/simulation.hpp index e2ec23c..417e03e 100644 --- a/src/mc_electron/simulation.hpp +++ b/src/mc_electron/simulation.hpp @@ -10,8 +10,6 @@ #define simulation_hpp -#include "parameter.hpp" -#include "constants.hpp" #include "katrin.hpp" #include "electron.hpp" #include "logger.hpp" @@ -19,22 +17,26 @@ #include -class Simulation: private Parameter, private Constants { +class Simulation { public: Simulation(); - + void doit(); private: void do_statusUpdate(); - void move_electron(Electron *moveElectron, Logger *Log); + void move_electron(Electron *moveElectron, H5_Logger *Log); void do_interaction(Electron *interactElectron); bool check_NumberInteractions(int &numberInteractionsThisElectron); + Katrin Kat; - Logger Log; + H5_Logger Log; + ranlib::Uniform rngUni; ranlib::NormalUnit rngNormal; + int numberTotalElectrons; int numberDecays; + std::deque electronQue; Electron *actionElectronPtr; @@ -42,6 +44,11 @@ private: int updater_numberOfPrevious; int updater_fraction; + static const long int seed; + static const long int numberTotalDecays; + + static const int propagationMethod; + static const int maxInteractions; public: void doTC6(); private: diff --git a/src/test_enviroment/main.cpp b/src/test_enviroment/main.cpp index 9073b16..d39b7c5 100644 --- a/src/test_enviroment/main.cpp +++ b/src/test_enviroment/main.cpp @@ -7,20 +7,39 @@ // -#include -#include - #include -#include -#include -#include "function_container.hpp" -#include "function2D_container.hpp" +//#include +#include "hdf5.h" + using namespace std; -int main(int argc, const char * argv[]) { - string file = "/Users/ChristianReiling/Documents/Masterarbeit/katrin-plasma/src/test_enviroment/yoon_TCS_ionization.dat"; +int main() { - Func_Container Func(file); - cout<