diff --git a/src/mc_electron/bedionization.cpp b/src/mc_electron/bedionization.cpp index 8e2848b..4f109a0 100644 --- a/src/mc_electron/bedionization.cpp +++ b/src/mc_electron/bedionization.cpp @@ -34,9 +34,9 @@ double BED_Ionization::get_TCS(double T_eV) { if (T_eV<=B_eV) { return (0); } else { - double t = calc_t(T_eV); + double t = get_t(T_eV); double fac = S_m2/(t+u+1); - double sum1 = calc_Dt(t)*log(t); + double sum1 = get_Dt(t)*log(t); double sum2 = (2-Ni/N)*( (t-1)/t - log(t)/(t+1) ); return(fac*(sum1 + sum2) ); } @@ -56,24 +56,24 @@ double BED_Ionization::get_SDCS (double T_eV, double W_eV) { if ( (T_eV<=B_eV) ||(W_eV >= (T_eV-B_eV) ) ) { return (0); } else { - double t = calc_t(T_eV); - double w = calc_w(W_eV); + double t = get_t(T_eV); + double w = get_w(W_eV); double fac = S_m2/B_eV/(t+u+1); double sum1 = (Ni/N - 2)/(t+1) * (1/(w+1) + 1/(t-w)); double sum2 = (2-Ni/N) * (pow(w+1,-2) + pow(t-w,-2)); - double sum3 = log(t)/(N*(w+1)) * calc_dfdw(w); + double sum3 = log(t)/(N*(w+1)) * get_dfdw(w); return(fac*(sum1 + sum2 + sum3)); } } -double BED_Ionization::calc_t(double T_eV) { +double BED_Ionization::get_t(double T_eV) { return(T_eV/B_eV); } -double BED_Ionization::calc_w(double W_eV) { +double BED_Ionization::get_w(double W_eV) { return(W_eV/B_eV); } -double BED_Ionization::calc_dfdw(double w) { +double BED_Ionization::get_dfdw(double w) { double y = 1/(w+1); double res = 0; for (int i=0;i<4;i++) { @@ -82,7 +82,7 @@ double BED_Ionization::calc_dfdw(double w) { return(res); } -double BED_Ionization::calc_Dt(double t) { +double BED_Ionization::get_Dt(double t) { double yh = 2/(t+1); double res = 0; for (int i=0;i<4;i++) { diff --git a/src/mc_electron/bedionization.hpp b/src/mc_electron/bedionization.hpp index c4d5b22..90d1c8c 100644 --- a/src/mc_electron/bedionization.hpp +++ b/src/mc_electron/bedionization.hpp @@ -25,11 +25,11 @@ private: static const double N,Ni; static const double c[4]; - double calc_t(double T); - double calc_w(double W); + double get_t(double T); + double get_w(double W); - double calc_dfdw(double w); - double calc_Dt(double t); + double get_dfdw(double w); + double get_Dt(double t); } ; diff --git a/src/mc_electron/electron.cpp b/src/mc_electron/electron.cpp index b1d7967..98e259c 100644 --- a/src/mc_electron/electron.cpp +++ b/src/mc_electron/electron.cpp @@ -13,8 +13,8 @@ using namespace std; -double Electron::calc_SigmaTotal_m2(vec3 speedT2_ms) { - vector Sigmas = calc_SigmaVector_m2(speedT2_ms); +double Electron::get_SigmaTotal_m2(vec3 speedT2_ms) { + vector Sigmas = get_SigmaVector_m2(speedT2_ms); double sigmaTot_m2 = 0; for (vector::iterator SigmasIt = Sigmas.begin() ; SigmasIt != Sigmas.end(); SigmasIt++) { sigmaTot_m2 += *SigmasIt; @@ -22,10 +22,10 @@ double Electron::calc_SigmaTotal_m2(vec3 speedT2_ms) { return(sigmaTot_m2); } -vector Electron::calc_SigmaVector_m2(vec3 speedT2_ms) { +vector Electron::get_SigmaVector_m2(vec3 speedT2_ms) { vector Sigmas; if (Particle::elaOn == 1) { - Sigmas.push_back(calc_elaSigma_m2(speedT2_ms)); + Sigmas.push_back(get_elaSigma_m2(speedT2_ms)); } else { Sigmas.push_back(0.); } @@ -39,12 +39,8 @@ vector Electron::calc_SigmaVector_m2(vec3 speedT2_ms) { } int Electron::roll_Interaction(vec3 speedT2_ms, ranlib::Uniform *rngUni) { - // Check Moved Outside - if (getKinEnergy_eV()==0) { - return(0); - } - vector Sigmas = calc_SigmaVector_m2(speedT2_ms); - double RandPoint = rngUni->random() * calc_SigmaTotal_m2(speedT2_ms); + vector Sigmas = get_SigmaVector_m2(speedT2_ms); + double RandPoint = rngUni->random() * get_SigmaTotal_m2(speedT2_ms); double Sum = 0; for (int i=0; i *rngUni) return(i+1); } } - - throw MyException("Interaction::roll_Interaction: Error in finding Interaction."); - + throw MyException("Electron::roll_Interaction(): Error in finding Interaction."); } @@ -74,12 +68,12 @@ void Electron::do_elaScattering (vec3 speedT2_ms, ranlib::Uniform *rngUn } -double Electron::calc_elaSigma_m2(vec3 speedT2_ms) { +double Electron::get_elaSigma_m2(vec3 speedT2_ms) { double SigmaEla_m2; vec3 speedLab_ms = v_ms - speedT2_ms; - double betaLab = calc_vecAbs(speedLab_ms)/c_ms; - //double EkinLab_eV = calc_Ekin_eV_from_speed(speedLab_ms, m_eV); - double elaEta = calc_elaEta(speedLab_ms); + double betaLab = get_vecAbs(speedLab_ms)/c_ms; + //double EkinLab_eV = get_Ekin_eV_from_speed(speedLab_ms, m_eV); + double elaEta = get_elaEta(speedLab_ms); double numerator = pi*ZT2*(ZT2+1)*re_m*re_m * (1-pow(betaLab,2)); double denominator = elaEta * (elaEta+1)*pow(betaLab,4); SigmaEla_m2 = numerator/denominator; @@ -87,9 +81,9 @@ double Electron::calc_elaSigma_m2(vec3 speedT2_ms) { } -double Electron::calc_elaEta(vec3 speedLab_ms) { - double Ekin_eVLab_eV = calc_Ekin_eV_from_speed(calc_vecAbs(speedLab_ms), m_eV); - double betaLab = calc_vecAbs(speedLab_ms)/c_ms; +double Electron::get_elaEta(vec3 speedLab_ms) { + double Ekin_eVLab_eV = get_Ekin_eV_from_speed(get_vecAbs(speedLab_ms), m_eV); + double betaLab = get_vecAbs(speedLab_ms)/c_ms; double etac = (Ekin_eVLab_eV < 50e3)? 1.198 : 1.13+3.76*pow(ZT2/(137*betaLab),2); double elaTau = Ekin_eVLab_eV/m_eV; return (etac * 1.7e-5 * pow(ZT2,2./3.) /(elaTau*(elaTau+2) )); @@ -97,9 +91,9 @@ double Electron::calc_elaEta(vec3 speedLab_ms) { double Electron::roll_elaTheta(vec3 speedT2_ms, ranlib::Uniform *rngUni) { vec3 speedLab_ms = v_ms - speedT2_ms; - double betaLab = calc_vecAbs(speedLab_ms)/c_ms; - double elaEta = calc_elaEta(speedLab_ms); - double const1 = 2*pi/calc_elaSigma_m2(speedT2_ms)*ZT2*(ZT2+1)*re_m*re_m*(1- pow(betaLab,2) )* pow(betaLab,-4); + 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); return(theta); } @@ -109,9 +103,9 @@ void Electron::do_elaKinetics(vec3 speedT2_ms, double thetaLab, double phiLab) { // Following Landau Lifschitz I Chapter 4 Paragraph 17 vec3 n0, speedLab_ms, v1New_ms, v2New_ms; speedLab_ms = v_ms - speedT2_ms; - n0 = calc_Newdirection(speedLab_ms, thetaLab, phiLab); - v1New_ms = (mT2_eV*calc_vecAbs(speedLab_ms)*n0 + m_eV*speedLab_ms)/(m_eV+mT2_eV) + speedT2_ms; - //v2New_ms = (-1*mass_eV*calc_vecAbs(speedLab_ms)*n0 + mass_eV*speedLab_ms)/(mass_eV+mT2_eV()) + speedT2_ms; + n0 = get_Newdirection(speedLab_ms, thetaLab, phiLab); + v1New_ms = (mT2_eV*get_vecAbs(speedLab_ms)*n0 + m_eV*speedLab_ms)/(m_eV+mT2_eV) + speedT2_ms; + //v2New_ms = (-1*mass_eV*get_vecAbs(speedLab_ms)*n0 + mass_eV*speedLab_ms)/(mass_eV+mT2_eV()) + speedT2_ms; v_ms = v1New_ms; // In "Real frame": both particles moving return; } @@ -123,19 +117,19 @@ Using BED-Model Kim and Rudd 1994 double Electron::get_ionSigma_m2(vec3 speedT2_ms) { vec3 speedLab_ms = v_ms - speedT2_ms; - double T_eV = calc_Ekin_eV_from_speed(calc_vecAbs(speedLab_ms), m_eV); + double T_eV = get_Ekin_eV_from_speed(get_vecAbs(speedLab_ms), m_eV); return (BED_Ionization::get_TCS(T_eV)); } Electron Electron::do_Ionization(vec3 speedT2_ms, int newId, ranlib::Uniform *rngUni) { vec3 speedOffset_ms = speedT2_ms; vec3 speedLab_ms = v_ms - speedOffset_ms; - double T_eV = calc_Ekin_eV_from_speed(calc_vecAbs(speedLab_ms), m_eV); + double T_eV = get_Ekin_eV_from_speed(get_vecAbs(speedLab_ms), m_eV); if (T_eV < EionT2_eV) { // Deviation, since Interaction-Process sample using v=0, but Interaction uses v!=0 speedOffset_ms = 0; speedLab_ms = v_ms - speedOffset_ms; - T_eV = calc_Ekin_eV_from_speed(calc_vecAbs(v_ms), m_eV); + T_eV = get_Ekin_eV_from_speed(get_SpeedAbs_ms(), m_eV); } if (T_eV < EionT2_eV) { @@ -151,18 +145,18 @@ Electron Electron::do_Ionization(vec3 speedT2_ms, int newId, ranlib::Uniform calc_SigmaVector_m2(vec3 speedT2_ms); + double get_SigmaTotal_m2(vec3 speedT2_ms); + std::vector get_SigmaVector_m2(vec3 speedT2_ms); int roll_Interaction(vec3 speedT2_ms, ranlib::Uniform *rngUni); // Elastic Scattering public: void do_elaScattering (vec3 speedT2_ms, ranlib::Uniform *rngUni); - double calc_elaSigma_m2(vec3 speedT2_ms); + double get_elaSigma_m2(vec3 speedT2_ms); protected: - double calc_elaEta(vec3 speedLab_ms); + double get_elaEta(vec3 speedLab_ms); double roll_elaTheta(vec3 speedT2_ms, ranlib::Uniform *rngUni); void do_elaKinetics(vec3 speedT2_ms, double theta, double phi); diff --git a/src/mc_electron/function2D_container.cpp b/src/mc_electron/function2D_container.cpp new file mode 100644 index 0000000..3b36f1b --- /dev/null +++ b/src/mc_electron/function2D_container.cpp @@ -0,0 +1,85 @@ +// +// 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 "<> xTemp) && (file >> yTemp) && (file >> fxyTemp)) { + add_ankerpoint(xTemp, yTemp, fxyTemp); + } + file.close(); + return; +} + diff --git a/src/mc_electron/function2D_container.hpp b/src/mc_electron/function2D_container.hpp new file mode 100644 index 0000000..a8a941b --- /dev/null +++ b/src/mc_electron/function2D_container.hpp @@ -0,0 +1,37 @@ +// +// function2D_container.hpp +// mc_electron +// +// Created by Christian Reiling on 15.03.19. +// Copyright © 2019 Christian Reiling. All rights reserved. +// + +#ifndef function2D_container_hpp +#define function2D_container_hpp + +#include "function_container.hpp" + +#include +#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 new file mode 100644 index 0000000..5f09f6d --- /dev/null +++ b/src/mc_electron/function_container.cpp @@ -0,0 +1,71 @@ +// +// function_container.cpp +// mc_electron +// +// Created by Christian Reiling on 15.03.19. +// Copyright © 2019 Christian Reiling. All rights reserved. +// + +#include "function_container.hpp" + +#include "myexeption.h" + +#include +#include + +using namespace std; + +Func_Container::Func_Container() { + +} + +Func_Container::Func_Container(std::string loadFilePath) { + readFromCSV(loadFilePath); +} + +double Func_Container::interpolate(double x) { + if (Map.empty()) { + throw MyException("Func_Container: Container is empty."); + } + + if (Map.begin() == Map.lower_bound(x) ) { + return (Map.begin()->second); + } else if (Map.end() == Map.lower_bound(x)){ + return ( (--Map.end())->second ); + } + + dim1Map::iterator lowAnker_Itr = --Map.lower_bound(x); + dim1Map::iterator upAnker_Itr = Map.lower_bound(x); + double lowAnker_x = lowAnker_Itr->first; + double lowAnker_fx = lowAnker_Itr->second; + double upAnker_x = upAnker_Itr->first; + double upAnker_fx = upAnker_Itr->second; + + double res = lowAnker_fx + (x-lowAnker_x)/(upAnker_x-lowAnker_x) * (upAnker_fx-lowAnker_fx); + return (res); +} + + +void Func_Container::add_ankerpoint(double x, double fx) { + if ( Map.find(x)!=Map.end() ) { + stringstream ss; + ss<<"Func_Container::add_ankerpoint: Key x="<> xTemp) && (file >> fxTemp)) { + add_ankerpoint(xTemp, fxTemp); + } + file.close(); + return; +} diff --git a/src/mc_electron/function_container.hpp b/src/mc_electron/function_container.hpp new file mode 100644 index 0000000..9795dda --- /dev/null +++ b/src/mc_electron/function_container.hpp @@ -0,0 +1,31 @@ +// +// function_container.hpp +// mc_electron +// +// Created by Christian Reiling on 15.03.19. +// Copyright © 2019 Christian Reiling. All rights reserved. +// + +#ifndef function_container_hpp +#define function_container_hpp + +#include +#include +#include + +typedef std::map dim1Map; +typedef std::pair dim1Pair; + +class Func_Container { +public: + Func_Container(); + Func_Container(std::string loadFilePath); + void add_ankerpoint(double x, double fx); + double interpolate(double x); +private: + dim1Map Map; + void readFromCSV(std::string loadFilePath); +} ; + + +#endif /* function_container_hpp */ diff --git a/src/mc_electron/functions.cpp b/src/mc_electron/functions.cpp index 2ee5455..13a4288 100644 --- a/src/mc_electron/functions.cpp +++ b/src/mc_electron/functions.cpp @@ -15,72 +15,74 @@ using namespace std; -double calc_vecAbs(vec3 vec) { +double get_vecAbs(vec3 vec) { return( sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2)) ); } -double calc_Ekin_eV_from_speed(double Speed_ms, double mass_eV) { + + +double get_Ekin_eV_from_speed(double Speed_ms, double mass_eV) { double beta = Speed_ms/Constants::c_ms; if (beta>=1) { stringstream ss; - ss<<"calc_Ekin_eV_from_speed(): Input speed v="<0."; + ss<<"get_Ekin_eV_from_speed(): Input speed v="<0."; throw MyException(ss.str()); } - double gamma = calc_gamma_from_beta(beta); + double gamma = get_gamma_from_beta(beta); return ( (gamma-1) * mass_eV ); } -double calc_gamma_from_beta(double beta) { +double get_gamma_from_beta(double beta) { if (beta>=1) { stringstream ss; - ss<<"calc_gamma_from_beta(): Input beta ="< *rn } beta = sqrt(C*(sum)); - return((calc_gamma_from_beta(beta)-1)*mass_eV); + return((get_gamma_from_beta(beta)-1)*mass_eV); } diff --git a/src/mc_electron/functions.hpp b/src/mc_electron/functions.hpp index 72512e3..423fa5b 100644 --- a/src/mc_electron/functions.hpp +++ b/src/mc_electron/functions.hpp @@ -12,22 +12,23 @@ #include "constants.hpp" -#include "blitz/array.h" -#include "blitz/tinyvec2.h" +#include +#include #include #include typedef blitz::TinyVector vec3; -double calc_vecAbs(blitz::TinyVector vec); -double calc_Speed_from_Ekin_eV(double Ekin_eV, double mass_eV); -double calc_gamma_from_Ekin_eV(double Ekin_eV, double mass_eV); -double calc_beta_from_gamma(double gamma); -double calc_Ekin_eV_from_speed(double Speed, double mass_eV); -double calc_gamma_from_beta(double beta); +double get_vecAbs(vec3 vec); -vec3 calc_Newdirection(vec3 oldDir, double theta,double phi); +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); vec3 roll_uniform_direction(ranlib::Uniform *rngUni); double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit *rngNormal); diff --git a/src/mc_electron/katrin.cpp b/src/mc_electron/katrin.cpp index ebd797c..7d34c5b 100644 --- a/src/mc_electron/katrin.cpp +++ b/src/mc_electron/katrin.cpp @@ -34,8 +34,8 @@ vec3 Katrin::getElField_Vm(vec3 loc_m) { 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 reductcalc_ionFactor = zProjection_m/mid_m; - return(reductcalc_ionFactor * wgtsT2Density_m3); + double reductget_ionFactor = zProjection_m/mid_m; + return(reductget_ionFactor * wgtsT2Density_m3); } vec3 Katrin::getTotSpeedT2_ms(vec3 loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal) { @@ -51,7 +51,7 @@ vec3 Katrin::getFlowSpeedT2_ms(vec3 loc_m) { } vec3 Katrin::getTempSpeedT2_ms(vec3 loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal) { - double tempOffsetABS_ms = calc_Speed_from_Ekin_eV(roll_MBEnergy(wgtsTemp_K,mT2_eV,rngNormal), mT2_eV); + double tempOffsetABS_ms = get_Speed_from_Ekin_eV(roll_MBEnergy(wgtsTemp_K,mT2_eV,rngNormal), mT2_eV); vec3 speed_ms = roll_uniform_direction(rngUni)*tempOffsetABS_ms; return(speed_ms); } @@ -77,10 +77,10 @@ Electron Katrin::injectElectron(int newId, ranlib::Uniform *rngUni, ranl Ekin_eV = roll_MBEnergy(MBTemp_K,me_eV, rngNormal); break; default: - throw MyException("Simulation::injectElectron: Invalid Injection Method."); + throw MyException("Katrin::injectElectron: Invalid Injection Method."); break; } - vec3 speed_ms = roll_uniform_direction(rngUni) * calc_Speed_from_Ekin_eV(Ekin_eV,me_eV); + vec3 speed_ms = roll_uniform_direction(rngUni) * get_Speed_from_Ekin_eV(Ekin_eV,me_eV); return (Electron(loc_m, speed_ms,newId,newId)); } @@ -94,115 +94,180 @@ double Katrin::roll_FermiEnergy_eV(ranlib::Uniform *rngUni) { eta1 = rngUni->random(); eta2 = rngUni->random(); Ecan_eV = Emin_eV + eta1*(Emax_eV - Emin_eV); - } while(eta2*pmax > calc_FermiDecaySpectrumDensity(Ecan_eV)); + } while(eta2*pmax > get_FermiDecaySpectrumDensity(Ecan_eV)); return(Ecan_eV); } -double Katrin::calc_FermiDecaySpectrumDensity(double Ekin_eV) { +double Katrin::get_FermiDecaySpectrumDensity(double Ekin_eV) { if (Ekin_eV>TritiumDecayEndpoint_eV) { return (0.); } else{ double p_eV = sqrt(Ekin_eV*Ekin_eV + 2 * Ekin_eV * me_eV); - return( BetaDecayConst * calc_FermiFunction(2, Ekin_eV) * p_eV*(Ekin_eV+me_eV)*pow(TritiumDecayEndpoint_eV-Ekin_eV,2) ); + return( BetaDecayConst * get_FermiFunction(2, Ekin_eV) * p_eV*(Ekin_eV+me_eV)*pow(TritiumDecayEndpoint_eV-Ekin_eV,2) ); } } -double Katrin::calc_FermiFunction(double Z, double Ekin_eV) { +double Katrin::get_FermiFunction(double Z, double Ekin_eV) { double gamma = 1+ Ekin_eV/me_eV; double beta = sqrt(1 - pow(gamma,-2)); double eta =Z/beta/137.; return( 2*pi*eta/(1-exp(-2*pi*eta)) ); } - // Move Electrons -void Katrin::do_BorisPush(Electron *moveElectron) { - double MFP_m = get_MFP_m(moveElectron); - vec3 oldLoc_m = moveElectron->getLoc_m(); - vec3 E_Vm, B_T; + // Move Electrons and Log +void Katrin::do_BorisPushMovement(Electron *moveElectron, double distance_m, Logger *Log) { + // Rework, with Logging and Reflection at wall + vec3 loc_m; - double distance = 0.; - while (distance < MFP_m) { - E_Vm = getElField_Vm(oldLoc_m); - B_T = getMagField_T(oldLoc_m); - distance += moveElectron->borisFly_m(E_Vm , B_T , dt_s); - if (movedOutside(moveElectron->getLoc_m())) { - moveElectron->setSpeed_ms(vec3(0,0,0)); + double oldzDir = moveElectron->get_Speed_ms()[2] < 0 ? -1 : 1; + vec3 oldLoc_m = moveElectron->get_Location_m(); + + double traveledDistance_m = 0; + + while (traveledDistance_m < distance_m) { + loc_m = moveElectron->get_Location_m(); + + traveledDistance_m += moveElectron->do_BorisStep_m(getElField_Vm(loc_m) , getMagField_T(loc_m) , dt_s); + + doBoris_WallInteraction(moveElectron); + + if (check_WallTermination(*moveElectron) == 0) { + Log->log_Electron(moveElectron, oldLoc_m); return; } + doBoris_LogzDirChange(moveElectron, &oldzDir, &oldLoc_m, Log); + + } + Log->log_Electron(moveElectron, oldLoc_m); + return; +} + +void Katrin::doBoris_WallInteraction(Electron *moveElectron) { + vec3 loc_m = moveElectron->get_Location_m(); + double r_m = moveElectron->get_Radius_m(); + if (loc_m[2]<=0) { // Rear Wall Termination + vec3 newLoc_m = moveElectron->get_Location_m(); + newLoc_m[2] = 0; + moveElectron->set_Location_m(newLoc_m); + } else if (r_m>=wgtsRadius_m) { // WGTS Wall Termination + vec3 newLoc_m = moveElectron->get_Location_m(); + double z_m = newLoc_m[2]; + newLoc_m[2] = 0; + newLoc_m = (wgtsRadius_m * 1.01)/get_vecAbs(newLoc_m) * newLoc_m; //1.01 to move Electron slightly out of WGTS + newLoc_m[2] = z_m; + } else if (loc_m[2]>=wgtsLength_m) { // Spectrometer Reflection + moveElectron->set_Speed_ms( -1*(moveElectron->get_Speed_ms()) ); + } +} + +void Katrin::doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 *oldLoc_m, Logger *Log) { + double zDir = moveElectron->get_Speed_ms()[2] < 0 ? -1 : 1; + if (*oldzDir != zDir) { + Log->log_Electron(moveElectron, *oldLoc_m); + *oldzDir = zDir; + *oldLoc_m = moveElectron->get_Location_m(); } return; } -void Katrin::do_DriftApproximation(Electron *moveElectron) { +void Katrin::do_DriftApproximationMovement(Electron *moveElectron, double distance_m, Logger *Log) { // Drift Approximation: Assume constant B and only movement out in z=0, z=wgtsLength_m... - double MFP_m = get_MFP_m(moveElectron); - vec3 oldLoc_m = moveElectron->getLoc_m(); + vec3 oldLoc_m = moveElectron->get_Location_m(); vec3 newLoc_m = oldLoc_m; - vec3 v_ms = moveElectron->getSpeed_ms(); - - double znew_m = getDrift_znew_m(MFP_m, oldLoc_m[2], v_ms); + double znew_m = getDrift_znew_m(distance_m, moveElectron); + doDrift_WallInteraction(&oldLoc_m, &znew_m, moveElectron, Log); newLoc_m[2] = znew_m; - moveElectron->setLoc_m(newLoc_m); - if (movedOutside(newLoc_m)) { - moveElectron->setSpeed_ms(vec3(0,0,0)); - } + + moveElectron->set_Location_m(newLoc_m); + Log->log_Electron(moveElectron, oldLoc_m); + return; } -double Katrin::getDrift_znew_m(double MFP_m, double zold_m, vec3 v_ms) { - double vAbs_ms = calc_vecAbs(v_ms); - double vDir = v_ms[2] < 0 ? -1 : 1; +double Katrin::getDrift_znew_m(double distance_m, Electron *moveElectron) { + double zold_m = moveElectron->get_Location_m()[2]; - vec3 vPar_ms = vec3(0,0,0); - vPar_ms[2] = v_ms[2]; - double vParAbs_ms = calc_vecAbs(vPar_ms); + vec3 v_ms = moveElectron->get_Speed_ms(); + double vTotAbs_ms = get_vecAbs(v_ms); + double vForwardAbs_ms = get_vecAbs(vec3(0,0,v_ms[2])); + double zDir = v_ms[2] < 0 ? -1 : 1; - double deltaZ_m = vDir*vParAbs_ms/vAbs_ms * MFP_m; - - double znew_m = zold_m + deltaZ_m; - - if (znew_m < 0) { - znew_m = 0; - } else if (znew_m > wgtsLength_m) { - znew_m = 2*wgtsLength_m - znew_m; - } + double znew_m = zold_m + zDir*vForwardAbs_ms/vTotAbs_ms * distance_m; return (znew_m); } +void Katrin::doDrift_WallInteraction(vec3 *oldLoc_m, double *znew_m, Electron *moveElectron, Logger *Log) { + if (*znew_m >= wgtsLength_m) { // Moveing into Spectrometer and reversing Speed. + vec3 reverseLoc_m = *oldLoc_m; + reverseLoc_m[2] = wgtsLength_m; + moveElectron->set_Location_m(reverseLoc_m); + + Log->log_Electron(moveElectron, *oldLoc_m); + + *oldLoc_m = reverseLoc_m; + *znew_m = 2*wgtsLength_m - *znew_m; + + moveElectron->set_Speed_ms( -1*(moveElectron->get_Speed_ms()) ); + } + if (*znew_m <= 0) { + *znew_m = 0; + } +} + double Katrin::get_MFP_m(Electron *moveElectron) { //Rework for inserting vector of Interactions vec3 loc_m, speedT2_ms; double sigmaTot_m2, density_m3, MFP_m; - loc_m = moveElectron->getLoc_m(); + loc_m = moveElectron->get_Location_m(); speedT2_ms = getFlowSpeedT2_ms(loc_m); - sigmaTot_m2 = moveElectron->calc_SigmaTotal_m2(speedT2_ms); + sigmaTot_m2 = moveElectron->get_SigmaTotal_m2(speedT2_ms); density_m3 = getTritiumDensity_m3(loc_m); MFP_m = 1/(density_m3 * sigmaTot_m2); return(MFP_m); } -bool Katrin::movedOutside(vec3 loc_m) { - double r_m = sqrt( pow(loc_m(0),2) + pow(loc_m(1),2) ); - return ( ! (loc_m[2]>0 && loc_m[2]<=wgtsLength_m && r_m= wgtsRadius_m) { + return (true); + } else { + return (false); + } +} + +bool Katrin::check_WallTermination(Electron checkElectron) { + vec3 loc_m = checkElectron.get_Location_m(); + return (check_WallTermination(loc_m)); +} + + void Katrin::set_cutoffEnergy_eV() { if (cutoffMode==0) { cutoffEnergy_eV = cutoffEnergySet_eV; @@ -210,7 +275,7 @@ void Katrin::set_cutoffEnergy_eV() { cutoffEnergy_eV = kb_eVK*wgtsTemp_K*cutoffFactor; } else { stringstream ss; - ss<<"Mover::Mover(): cutoffMode= "< *rngUni, ranlib::NormalUnit *rngNormal); private: double roll_FermiEnergy_eV(ranlib::Uniform *rngUni); - double calc_FermiDecaySpectrumDensity(double Ekin_eV); - double calc_FermiFunction(double Z, double Ekin_eV); + double get_FermiDecaySpectrumDensity(double Ekin_eV); + double get_FermiFunction(double Z, double Ekin_eV); // Move Electrons public: - void do_BorisPush(Electron *moveElectron); - void do_DriftApproximation(Electron *moveElectron); -private: - double getDrift_znew_m(double MFP_m, double zold_m, vec3 v_ms); - bool movedOutside(blitz::TinyVector loc_m); 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); +private: + double getDrift_znew_m(double MFP_m, Electron *moveElectron); + void doDrift_WallInteraction(vec3 *oldLoc_m, double *znew_m, Electron *moveElectron, Logger *Log); + void doBoris_WallInteraction(Electron *moveElectron); + void doBoris_LogzDirChange(Electron *moveElectron, double *oldzDir, vec3 *oldLoc_m, Logger *Log); // Terminate Electrons public: - bool get_Terminated(Electron termElectron); + bool check_Termination(Electron checkElectron); + bool check_EnergyTermination(double Ekin_eV); + bool check_EnergyTermination(Electron checkElectron); + bool check_WallTermination(vec3 loc_m); + bool check_WallTermination(Electron checkElectron); private: double cutoffEnergy_eV; void set_cutoffEnergy_eV(); diff --git a/src/mc_electron/logger.cpp b/src/mc_electron/logger.cpp index 4ea54c9..14e6e4c 100644 --- a/src/mc_electron/logger.cpp +++ b/src/mc_electron/logger.cpp @@ -37,7 +37,7 @@ void Logger::log_Electron(Electron *logElectron, blitz::TinyVector old outStream[0]<<*logElectron<getLoc_m()[2]; + double znew = logElectron->get_Location_m()[2]; double zmin = min(zold,znew); double zmax = max(zold,znew); diff --git a/src/mc_electron/main.cpp b/src/mc_electron/main.cpp index 2e263a6..749e823 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -15,7 +15,7 @@ using namespace std; int main(int argc, const char * argv[]) { clock_t start = clock(); - +/* try { Simulation Sim; Sim.doit(); @@ -23,6 +23,8 @@ int main(int argc, const char * argv[]) { cout< E_Vm , blitz::TinyVector B_T , double dt_s) { +double Particle::do_BorisStep_m(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s) { double distance_m; x_m += dt_s/2 * v_ms; - distance_m = dt_s/2 * getSpeedAbs_ms(); - this->changeSpeedBoris(E_Vm , B_T , dt_s); + distance_m = dt_s/2 * get_SpeedAbs_ms(); + this->doBoris_SpeedChange(E_Vm , B_T , dt_s); x_m += dt_s/2 * v_ms; - distance_m += dt_s/2 * getSpeedAbs_ms(); + distance_m += dt_s/2 * get_SpeedAbs_ms(); return(distance_m); } -void Particle::changeSpeedBoris(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s) { +void Particle::doBoris_SpeedChange(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s) { blitz::TinyVector vMinus_ms, vPlus_ms, vPrime_ms, t, s; double q_C = -1*Z * e_C; double m_kg = m_eV*e_C/c_ms/c_ms; diff --git a/src/mc_electron/particle.hpp b/src/mc_electron/particle.hpp index 39e8b4d..1fb5a86 100644 --- a/src/mc_electron/particle.hpp +++ b/src/mc_electron/particle.hpp @@ -38,28 +38,28 @@ protected: public: // Get Properties Methods - double getMass_eV() { return(m_eV); } - double getCharge_e() { return(Z); } - vec3 getLoc_m() { return(x_m); } - vec3 getSpeed_ms() { return(v_ms); } - species getSpecies() {return(s); } - double getId() { return(Id); } - double getParentId() { return(parentId); } + double get_Mass_eV() { return(m_eV); } + double get_Charge_e() { return(Z); } + vec3 get_Location_m() { return(x_m); } + vec3 get_Speed_ms() { return(v_ms); } + species get_Species() {return(s); } + double get_Id() { return(Id); } + double get_ParentId() { return(parentId); } // Get Properties - double getRadius_m() { return ( sqrt( pow(x_m[0],2) + pow(x_m[1],2) ) ); } - double getSpeedAbs_ms() { return ( calc_vecAbs(v_ms) ); } - double getKinEnergy_eV() { return ( calc_Ekin_eV_from_speed(getSpeedAbs_ms(), m_eV) ); } + double get_Radius_m() { return ( sqrt( pow(x_m[0],2) + pow(x_m[1],2) ) ); } + double get_SpeedAbs_ms() { return ( get_vecAbs(v_ms) ); } + double get_EKin_eV() { return ( get_Ekin_eV_from_speed(get_SpeedAbs_ms(), m_eV) ); } // Set Properties Methods - void setLoc_m(blitz::TinyVector xnew_m) {x_m = xnew_m; }; - void setSpeed_ms(blitz::TinyVector vnew_ms) {v_ms = vnew_ms; }; + void set_Location_m(blitz::TinyVector xnew_m) {x_m = xnew_m; }; + void set_Speed_ms(blitz::TinyVector vnew_ms) {v_ms = vnew_ms; }; //Boris Push: Propagate Flight for one timestep dt_s in E_Vm and B_T and returns distance. public: - double borisFly_m(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s); + double do_BorisStep_m(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s); protected: - void changeSpeedBoris(blitz::TinyVector E_Vm,blitz::TinyVector B_T , double dt_s); + void doBoris_SpeedChange(blitz::TinyVector E_Vm,blitz::TinyVector B_T , double dt_s); public: friend std::ostream& operator<<(std::ostream& os, const Particle& partOut); diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 02d63ad..9e377e0 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -32,12 +32,11 @@ void Simulation::doit() { } status_updater(); - oldLoc_m = electronQue.front().getLoc_m(); - move_electron(&electronQue.front()); - Log.log_Electron(&electronQue.front(), oldLoc_m); - do_interaction(&electronQue.front()); - - if (Kat.get_Terminated (electronQue.front())) { + move_electron(&electronQue.front(), &Log); + if (!Kat.check_EnergyTermination (electronQue.front()) ) { + do_interaction(&electronQue.front()); + } + if (Kat.check_EnergyTermination (electronQue.front())) { electronQue.pop_front(); } } @@ -69,11 +68,12 @@ void Simulation::status_updater() { } } -void Simulation::move_electron(Electron *moveElectron) { +void Simulation::move_electron(Electron *moveElectron, Logger *Log) { + double MFP_m = Kat.get_MFP_m(&electronQue.front()); if (propagationMethod==0) { - Kat.do_DriftApproximation(moveElectron); + Kat.do_DriftApproximationMovement(moveElectron, MFP_m, Log); } else if (propagationMethod==1) { - Kat.do_BorisPush(moveElectron); // If moved outside set v = 0,0,0; + Kat.do_BorisPushMovement(moveElectron, MFP_m, Log); // If moved outside set v = 0,0,0; } else { stringstream ss; ss<<"Simulation::move_electron: propagationMethod="<roll_Interaction(Kat.getFlowSpeedT2_ms(newLoc_m),&rngUni); - switch ( process ) { - case 0: //Moved Outside - break; + switch (process) { case 1: // Elastic Scattering interactElectron->do_elaScattering(Kat.getTotSpeedT2_ms(newLoc_m, &rngUni, &rngNormal) , &rngUni); break; diff --git a/src/mc_electron/simulation.hpp b/src/mc_electron/simulation.hpp index 6c633a0..fd71c66 100644 --- a/src/mc_electron/simulation.hpp +++ b/src/mc_electron/simulation.hpp @@ -27,7 +27,7 @@ public: private: void status_updater(); - void move_electron(Electron *moveElectron); + void move_electron(Electron *moveElectron, Logger *Log); void do_interaction(Electron *interactElectron); diff --git a/src/test_enviroment/main.cpp b/src/test_enviroment/main.cpp new file mode 100644 index 0000000..d3ffe56 --- /dev/null +++ b/src/test_enviroment/main.cpp @@ -0,0 +1,19 @@ +// +// main.cpp +// test_enviroment +// +// Created by Christian Reiling on 13.03.19. +// Copyright © 2019 Christian Reiling. All rights reserved. +// + +#include +#include +#include +#include "function_container.hpp" +#include "function2D_container.hpp" + +using namespace std; + +int main(int argc, const char * argv[]) { + +}