diff --git a/src/mc_electron/constants.cpp b/src/mc_electron/constants.cpp index 4f66d7d..cddafca 100644 --- a/src/mc_electron/constants.cpp +++ b/src/mc_electron/constants.cpp @@ -38,3 +38,52 @@ const double Constants::ionN = 2.4; const double Constants::ionG5 = 0.33; const double Constants::ionBeta = 0.6; const double Constants::ionGamma = 10; + + +// Conversation +double Constants::calcAbs(blitz::TinyVector vec) { + return( sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2)) ); +} + + +double Constants::calcV_msFromEkin_eV(double Ekin_eV, double m_eV) { + double gamma = calcGammaFromEkin_eV(Ekin_eV, m_eV); + double beta = calcBetaFromGamma(gamma); + return(beta * c_ms); +} + +double Constants::calcGammaFromEkin_eV(double Ekin_eV, double m_eV) { + return (1. + Ekin_eV/m_eV); +} + +double Constants::calcBetaFromGamma(double gamma) { + return( sqrt(1 - pow(gamma,-2)) ); +} + +double Constants::calcEkin_eVFromVAbs_ms(double vAbs_ms, double m_eV) { + double beta = calcBetaFromVAbs_ms(vAbs_ms); + double gamma = calcGammaFromBeta(beta); + return ( (gamma-1) * m_eV * pow(c_nat,2) ); +} + +double Constants::calcBetaFromVAbs_ms(double vAbs_ms) { + return(vAbs_ms/c_ms); +} + +double Constants::calcGammaFromBeta(double beta) { + return( pow(1 - pow(beta,2),-0.5) ) ; +} + +// Help Functions +blitz::TinyVector Constants::zeroVec() { + blitz::TinyVector zeroVec; + zeroVec = 0,0,0; + return(zeroVec); +} +blitz::TinyVector Constants::getRandDir(ranlib::Uniform *rngUni) { + blitz::TinyVector dir; + double Phi_rad = 2*pi*rngUni->random(); + double Theta_rad = pi*rngUni->random(); + dir = sin(Theta_rad)*cos(Phi_rad) , sin(Theta_rad)*sin(Phi_rad) , cos(Theta_rad); + return(dir); +} diff --git a/src/mc_electron/constants.h b/src/mc_electron/constants.h index 081644f..50ad9e9 100644 --- a/src/mc_electron/constants.h +++ b/src/mc_electron/constants.h @@ -9,7 +9,18 @@ #ifndef constants_h #define constants_h +#include #include +#include +#include +#include +#include +#include +#include +#include "blitz/array.h" +#include "blitz/tinyvec2.h" +#include +#include class Constants { @@ -46,18 +57,18 @@ public: static const double ionBeta; static const double ionGamma; - // Conversion Functions - double calcV_ms(double Ekin_eV) { - double gamma = 1. + Ekin_eV/me_eV; - double beta = sqrt(1 - pow(gamma,-2)); - return(beta * c_ms); - } - double calcEkin_eV(double speedAbs_ms) { - double beta = speedAbs_ms/c_ms; - double gamma = pow(1 - pow(beta,2),-0.5); - return ( (gamma-1) * me_eV * pow(c_nat,2) ); - } + double calcAbs(blitz::TinyVector vec); + double calcV_msFromEkin_eV(double Ekin_eV, double m_eV); + double calcGammaFromEkin_eV(double Ekin_eV, double m_eV); + double calcBetaFromGamma(double gamma); + double calcEkin_eVFromVAbs_ms(double vAbs_ms, double m_eV); + double calcBetaFromVAbs_ms(double vAbs_ms); + double calcGammaFromBeta(double beta); + + // Help Functions + blitz::TinyVector zeroVec(); + blitz::TinyVector getRandDir(ranlib::Uniform *rngUni); }; #endif diff --git a/src/mc_electron/interaction.cpp b/src/mc_electron/interaction.cpp new file mode 100644 index 0000000..bf30e85 --- /dev/null +++ b/src/mc_electron/interaction.cpp @@ -0,0 +1,149 @@ +// +// interaction.cpp +// mc_electron +// +// Created by Christian Reiling on 13.11.18. +// Copyright © 2018 Christian Reiling. All rights reserved. +// + +#include "interaction.h" + + +Interaction::Interaction(Particle *InitParticle, Particle *ScatParticle) { + // Check valid Interaction? e+ and T2 + // Check Particle will interact... v1-v2 != 0 + P1 = InitParticle; + P2 = ScatParticle; +} + + +double Interaction::getSigmaTot_m2() { + { return (getSigmaEla_m2()); } +} + +/* + Elastic Scattering: + Using Rutherford Model with screening parameter following + Cross-sections for water vapour for the Monte Carlo electron track structure code from 10eV to the MeV region + S Uehara et al 1993 Phys. Med. Biol. 38 1841 + */ +double Interaction::getSigmaEla_m2() { + double numerator = pi*Z2()*(Z2()+1)*re_m*re_m * (1-pow(betaLab(),2)); + double denominator = elaEta() * (elaEta()+1)*pow(betaLab(),4); + return(numerator/denominator); +} + +void Interaction::doElaScat (ranlib::Uniform *rngUni) { + // Here theta and phi are COM - Angles.... prob not correct :( For m1 << m2 fine. + + double theta = getRandomTheta(rngUni); + double phi = rngUni->random() * 2 * pi; + doKinElaScat(theta, phi); +} + +double Interaction::elaEta() { + double etac = (EkinLab_eV() < 50e3)? 1.198 : 1.13+3.76*pow(Z2()/(137*betaLab()),2); + double elaTau = EkinLab_eV()/m1_eV(); + return (etac * 1.7e-5 * pow(Z2(),2./3.) /(elaTau*(elaTau+2) )); +} + +double Interaction::getRandomTheta( ranlib::Uniform *rngUni) { + double const1 = 2*pi/getSigmaEla_m2()*Z2()*(Z2()+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); +} + + +void Interaction::doKinElaScat(double theta, double phi) { + // Following Landau Lifschitz I Chapter 4 Paragraph 17 + blitz::TinyVector n0, v1New_ms, v2New_ms; + n0 = getNewDir(theta,phi); //New direction in Lab Frame + v1New_ms = (m2_eV()*calcAbs(vLab_ms())*n0 + m1_eV()*vLab_ms())/(m1_eV()+m2_eV()) + v2_ms(); + v2New_ms = (-1*m1_eV()*calcAbs(vLab_ms())*n0 + m1_eV()*vLab_ms())/(m1_eV()+m2_eV()) + v2_ms(); + P1->setSpeed_ms(v1New_ms); + P2->setSpeed_ms(v2New_ms); + return; +} + +blitz::TinyVector Interaction::getNewDir(double theta,double phi) { + // Transformation of direction following Plante and Cucinotta + double theta0, phi0, A3, A4, A5, A11, A21, theta1, phi1; + double phi1c [4]; + theta0 = acos(vLab_ms()[2]/calcAbs(vLab_ms())); + phi0 = atan2(vLab_ms()[1],vLab_ms()[0]); + + A3 = sin(theta)*cos(phi); + A4 = A3*cos(theta0) + cos(theta)*sin(theta0); + A5 = sin(theta)*sin(phi); + A11 = A4*cos(phi0) - A5*sin(phi0); + A21 = A4*sin(phi0) + A5*cos(phi0); + theta1 = acos( cos(theta0)*cos(theta)-A3*sin(theta0) ); + + // Get right phi1, maybe more elegant way later. + phi1 = -1; + phi1c[0] = acos( A11/sin(theta1)); + phi1c[1] = 2*pi - phi1c[0]; + phi1c[2] = asin (A21/sin(theta1)); + phi1c[3] = pi - phi1c[2]; + phi1c[2] = (phi1c[2] > 0) ? phi1c[2] : 2*pi + phi1c[2]; + for (int i=0;i<3;i++) { + for (int j=i+1;j<4;j++) { + if (phi1c[i]-phi1c[j] < 0.0001) { + phi1 = phi1c[i]; + goto endloops; + } + } + } +endloops: + if (phi1 == -1) { + std::cerr<<"Interaction::getNewDir: phi1 not found."< newDir; + newDir = sin(theta1)*cos(phi1) , sin(theta1)*sin(phi1) , cos(theta1); + return(newDir); +} + + +/* + Ionization: + Following + Differential and total cross sections for ionization of helium and hydrogen by electrons + M. E. Rudd 1991 Physical Review A Volume 44 Number3 + */ +/* +double Interaction::getSigmaIon_m2() { + double N, t, S_m2,F,g1; + N = 1; + t = getKinEnergy_eV()/EIonizationT2_eV; + S_m2 = 4*pi*N*pow(rBohr_m*ERydberg_eV/EIonizationT2_eV,2); + F = (ionA1*log(t) + ionA2 + ionA3/t)/t; + g1 = (1-pow(t,1-ionN))/(ionN-1) - pow(2/(t+1),ionN/2)*(1-pow(t,1-ionN/2))/(ionN-2); + return(S_m2*F*g1); +} + + +void Interaction::doIon(ranlib::Uniform *rngUni) { + // Get Energy after Interaction + + // Get Scattering Angle + + // Do Elastic Scattering with a resting electron + // Energy Initial from First Part, Secondary Electron: + + +} + +double Interaction::dSigmaIondW_m2eV(double W) { + double N, w, t, S_m2,F,f1; + N = 1; + w = W/EIonizationT2_eV; + t = getKinEnergy_eV()/EIonizationT2_eV; + S_m2 = 4*pi*N*pow(rBohr_m*ERydberg_eV/EIonizationT2_eV,2); + F = (ionA1*log(t) + ionA2 + ionA3/t)/t; + f1 = pow(w+1,-ionN) + pow(t-w,-ionN)-pow((w+1)*(t-w),-ionN/2); + return(S_m2*F*f1/EIonizationT2_eV); + +} + +*/ diff --git a/src/mc_electron/interaction.h b/src/mc_electron/interaction.h new file mode 100644 index 0000000..4fd0a80 --- /dev/null +++ b/src/mc_electron/interaction.h @@ -0,0 +1,70 @@ +// +// interaction.h +// mc_electron +// +// Created by Christian Reiling on 13.11.18. +// Copyright © 2018 Christian Reiling. All rights reserved. +// + +#ifndef interaction_h +#define interaction_h + +#include "particle.h" + +class Interaction : protected Constants { +public: + virtual ~Interaction() {}; + Interaction() {}; + Interaction(Particle *InitParticle, Particle *ScatParticle); + + Particle *getP1(){return(P1);} + //Particle *getP2(){return(P2);} + +protected: + Particle *P1; + Particle *P2; + blitz::TinyVector v1_ms() {return(P1->getSpeed_ms()); } + blitz::TinyVector v2_ms() {return(P2->getSpeed_ms()); } + blitz::TinyVector vLab_ms() {return(v1_ms()-v2_ms()); } + + double m1_eV() {return(P1->getMass_eV()); } + double m2_eV() {return(P2->getMass_eV()); } + double Z1() {return(P1->getCharge_e()); } + double Z2() {return(P2->getCharge_e()); } + + double EkinLab_eV() { + return(calcEkin_eVFromVAbs_ms(calcAbs(vLab_ms()), m1_eV())); + } + double betaLab() {return(calcAbs(vLab_ms())/c_ms); } + +public: + double getSigmaTot_m2() ; + + // Elastic Scattering +public: + double getSigmaEla_m2(); + void doElaScat (ranlib::Uniform *rngUni); +protected: + double elaEta(); + double getRandomTheta(ranlib::Uniform *rngUni); + void doKinElaScat(double theta, double phi); + blitz::TinyVector getNewDir(double theta,double phi); + + // Ionization + /* +public: + double getSigmaIon_m2(); + void doIon (ranlib::Uniform *rngUni); +protected: + double dSigmaIondW_m2eV(double W); +protected: + + */ + +}; + + + + + +#endif /* interaction_h */ diff --git a/src/mc_electron/interactions.cpp b/src/mc_electron/interactions.cpp deleted file mode 100644 index 5d933b5..0000000 --- a/src/mc_electron/interactions.cpp +++ /dev/null @@ -1,133 +0,0 @@ -// -// interactions.cpp -// mc_electron -// -// Created by Christian Reiling on 22.10.18. -// Copyright © 2018 Christian Reiling. All rights reserved. -// - -#include -#include "particle.h" - -void Particle::elasticKinematicClassical(Particle *P2, double theta, double phi) { - // Following Landau Lifschitz I Chapter 4 Paragraph 17 - blitz::TinyVector n0, v_ms, v1I_ms, v2I_ms, v1O_ms, v2O_ms; - double m1_eV, m2_eV, vAbs_ms; - v1I_ms = getSpeed_ms(); - v2I_ms = P2->getSpeed_ms(); - m1_eV = getMass_eV(); - m2_eV = P2->getMass_eV(); - n0 = getNewDir(theta,phi,v1I_ms); - v_ms = v1I_ms-v2I_ms; - vAbs_ms = sqrt(dot(v_ms,v_ms)); - v1O_ms = ( m2_eV*vAbs_ms*n0 + (m1_eV*v1I_ms+m2_eV*v2I_ms) )/(m1_eV+m2_eV); - v2O_ms = ( -1*m1_eV*vAbs_ms*n0 + (m1_eV*v1I_ms+m2_eV*v2I_ms) )/(m1_eV+m2_eV); - setSpeed_ms(v1O_ms); - P2->setSpeed_ms(v2O_ms); - return; -} - -blitz::TinyVector Particle::getNewDir(double theta,double phi,blitz::TinyVector oldDir) { - // Change of direction following Plante and Cucinotta - double oldDirAbs, theta0, phi0, A3, A4, A5, A11, A21, theta1, phi1; - double phi1c [4]; - oldDirAbs = sqrt( dot(oldDir,oldDir) ); - theta0 = acos(oldDir[2]/oldDirAbs); - phi0 = atan2(oldDir[1],oldDir[0]); - - A3 = sin(theta)*cos(phi); - A4 = A3*cos(theta0) + cos(theta)*sin(theta0); - A5 = sin(theta)*sin(phi); - A11 = A4*cos(phi0) - A5*sin(phi0); - A21 = A4*sin(phi0) + A5*cos(phi0); - theta1 = acos( cos(theta0)*cos(theta)-A3*sin(theta0) ); - - // Get right phi1, maybe more elegant way later. - phi1 = -1; - phi1c[0] = acos( A11/sin(theta1)); - phi1c[1] = 2*pi - phi1c[0]; - phi1c[2] = asin (A21/sin(theta1)); - phi1c[3] = pi - phi1c[2]; - phi1c[2] = (phi1c[2] > 0) ? phi1c[2] : 2*pi + phi1c[2]; - for (int i=0;i<3;i++) { - for (int j=i+1;j<4;j++) { - if (phi1c[i]-phi1c[j] < 0.0001) { - phi1 = phi1c[i]; - goto endloops; - } - } - } -endloops: - if (phi1 == -1) { - std::cerr<<"Particle::doElaScat: phi1 not found."< newDir; - newDir = sin(theta1)*cos(phi1) , sin(theta1)*sin(phi1) , cos(theta1); - return(newDir); -} - -/* - Elastic Scattering: - Using Rutherford Model with screening parameter following - Cross-sections for water vapour for the Monte Carlo electron track structure code from 10eV to the MeV region - S Uehara et al 1993 Phys. Med. Biol. 38 1841 - */ -double Particle::getSigmaEla_m2() { - double numerator = pi*ZT2*(ZT2+1)*re_m*re_m * (1-pow(beta(),2)); - double denominator = elaEta() * (elaEta()+1)*pow(beta(),4); - return(numerator/denominator); -} - -void Particle::doElaScat (ranlib::Uniform *rngUni) { - // Compute Scattering Angle - double theta = getRandomTheta(rngUni); - double phi = rngUni->random() * 2 * pi; - - // Compute Interaction Partner - blitz::TinyVector speed; - speed = 0,0,0; - Particle P2 = Particle(mT2_eV,speed); - elasticKinematicClassical(&P2, theta, phi); -} - -double Particle::getRandomTheta(ranlib::Uniform *rngUni) { - double const1 = 2*pi/getSigmaEla_m2()*ZT2*(ZT2+1)*re_m*re_m*(1- pow(beta(),2) )* pow(beta(),-4); - double theta = acos( pow( rngUni->random()/const1-pow(2*elaEta(),-1) , -1 ) +1+2*elaEta()); - return(theta); -} - - -/* - Ionization: - Following - Differential and total cross sections for ionization of helium and hydrogen by electrons - M. E. Rudd 1991 Physical Review A Volume 44 Number3 - */ -double Particle::getSigmaIon_m2() { - double N, t, S_m2,F,g1; - N = 1; - t = getKinEnergy_eV()/EIonizationT2_eV; - S_m2 = 4*pi*N*pow(rBohr_m*ERydberg_eV/EIonizationT2_eV,2); - F = (ionA1*log(t) + ionA2 + ionA3/t)/t; - g1 = (1-pow(t,1-ionN))/(ionN-1) - pow(2/(t+1),ionN/2)*(1-pow(t,1-ionN/2))/(ionN-2); - return(S_m2*F*g1); -} - - -void Particle::doIon(ranlib::Uniform *rngUni) { - // Get Energy after Interaction - - // Get Scattering Angle - - // Do Elastic Scattering with a resting electron - // Energy Initial from First Part, Secondary Electron: - - -} - - - - - - diff --git a/src/mc_electron/katrin.cpp b/src/mc_electron/katrin.cpp index 556570e..a580ac4 100644 --- a/src/mc_electron/katrin.cpp +++ b/src/mc_electron/katrin.cpp @@ -24,6 +24,33 @@ blitz::TinyVector Katrin::getElField(blitz::TinyVector loc_m } double Katrin::getTritiumDensity(blitz::TinyVector loc_m) { - return(wgtsTritiumDensity_m3); + return(wgtsT2Density_m3); +} + +Particle Katrin::getT2AverScatPart(blitz::TinyVector loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal) { + blitz::TinyVector speed_ms; + // Get Linear Flow + double sign = (loc_m[2] < wgtsLength_m/2.) ? -1 : 1; + double flow_ms = sign*wgtsT2FlowSpeed_ms; + speed_ms = 0,0,flow_ms; + Particle ScatPart; + ScatPart = Particle(mT2_eV, ZT2, speed_ms); + ScatPart.setLoc_m(loc_m); + return (ScatPart); +} + +Particle Katrin::getT2ScatPart(blitz::TinyVector loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal) { + Particle ScatPart = getT2AverScatPart(loc_m, rngUni, rngNormal); + blitz::TinyVector speed_ms, flowOffset_ms, tempOffset_ms; + // Get Linear Flow + flowOffset_ms = ScatPart.getSpeed_ms(); + + // Get Temperature Motion + double tempOffsetABS_ms = rngNormal->random()*sqrt(kb_eVK*wgtsTemp_K/mT2_eV)*c_ms; + tempOffset_ms = getRandDir(rngUni)*tempOffsetABS_ms; + + speed_ms = flowOffset_ms + tempOffset_ms; + ScatPart.setSpeed_ms(speed_ms); + return (ScatPart); } diff --git a/src/mc_electron/katrin.h b/src/mc_electron/katrin.h index eb0a278..6144581 100644 --- a/src/mc_electron/katrin.h +++ b/src/mc_electron/katrin.h @@ -9,15 +9,8 @@ #ifndef katrin_h #define katrin_h -#include -#include -#include -#include -#include -#include "blitz/array.h" -#include "blitz/tinyvec2.h" - #include "parameter.h" +#include "particle.h" class Katrin: public Parameter { @@ -25,10 +18,14 @@ public: Katrin(){}; virtual ~Katrin(){}; - blitz::TinyVector getMagField(blitz::TinyVector loc_m); blitz::TinyVector getElField(blitz::TinyVector loc_m); double getTritiumDensity(blitz::TinyVector loc_m); + + Particle getT2AverScatPart(blitz::TinyVector loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal); + Particle getT2ScatPart(blitz::TinyVector loc_m, ranlib::Uniform *rngUni, ranlib::NormalUnit *rngNormal); + + }; #endif diff --git a/src/mc_electron/main.cpp b/src/mc_electron/main.cpp index 4e0dc58..ee00a01 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -19,8 +19,8 @@ int main(int argc, const char * argv[]) { Debug("Debug Mode is on.") - runTests(); - /* + //runTests(); + std::clock_t start; double duration; start = std::clock(); @@ -31,5 +31,5 @@ int main(int argc, const char * argv[]) { duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cout<<"Duration: "<< duration <<'\n'; - */ + } diff --git a/src/mc_electron/makefile2 b/src/mc_electron/makefile2 index 693e7bf..a785444 100644 --- a/src/mc_electron/makefile2 +++ b/src/mc_electron/makefile2 @@ -10,7 +10,7 @@ TARGETS = mcelectron all: $(TARGETS) -mcelectron: main.o katrin.o simulation.o particle.o interactions.o tests.o +mcelectron: main.o katrin.o simulation.o particle.o interaction.o tests.o $(CXX) -o $@ $^ $(LDFLAGS) $(LOADLIBES) %.o : %.cpp diff --git a/src/mc_electron/parameter.h b/src/mc_electron/parameter.h index 859bee4..594fdd2 100644 --- a/src/mc_electron/parameter.h +++ b/src/mc_electron/parameter.h @@ -9,9 +9,6 @@ #ifndef parameter_h #define parameter_h -#include -#include - #include "constants.h" class Parameter: protected Constants @@ -32,7 +29,9 @@ public: wgtsMagField_T = value("wgtsMagField_T"); wgtsLength_m = value("wgtsLength_m"); wgtsRadius_m = value("wgtsRadius_m"); - wgtsTritiumDensity_m3 = value("wgtsTritiumDensity_m3"); + wgtsT2Density_m3 = value("wgtsT2Density_m3"); + wgtsTemp_K = value("wgtsTemp_K"); + wgtsT2FlowSpeed_ms = value("wgtsT2FlowSpeed_ms"); // Output numberZBins = value ("numberZBins"); @@ -99,7 +98,9 @@ protected: double wgtsMagField_T; double wgtsLength_m; double wgtsRadius_m; - double wgtsTritiumDensity_m3; + double wgtsT2Density_m3; + double wgtsTemp_K; + double wgtsT2FlowSpeed_ms; // Output int numberZBins; diff --git a/src/mc_electron/particle.cpp b/src/mc_electron/particle.cpp index 754efd1..f75862e 100644 --- a/src/mc_electron/particle.cpp +++ b/src/mc_electron/particle.cpp @@ -52,7 +52,7 @@ std::ostream& operator<<(std::ostream& os, const Particle& partOut) { < speed_ms) { + m_eV = mass_eV; + Z = charge_e; + x_m = zeroVec(); + v_ms = speed_ms; + Id = NULL; + parentId = NULL; + s = undef; } -double Particle::beta() { - return ( vAbs_ms()/c_ms ); +Particle::Particle(double mass_eV, double charge_e,blitz::TinyVector loc_m, blitz::TinyVector speed_ms, double idnum, double parentIdnum, species spec) { + m_eV = mass_eV; + Z = charge_e; + x_m = loc_m; + v_ms = speed_ms; + Id = idnum; + parentId = parentIdnum; + s = spec; } -double Particle::gamma() { - return ( pow(1- pow(beta(),2),-0.5)); -} -double Particle::calcEkin_eV() { - return ( (gamma()-1) * m_eV); -} - -double Particle::elaTau() { - return (calcEkin_eV()/me_eV) ; -} - -double Particle::elaEta() { - double etac = (calcEkin_eV() < 50e3)? 1.198 : 1.13+3.76*pow(ZT2/(137*beta()),2); - return (etac * 1.7e-5 * pow(ZT2,2./3.) /( elaTau()*(elaTau()+2) )); -} double Particle::borisFly_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 * vAbs_ms(); + distance_m = dt_s/2 * getvAbs_ms(); this->changeSpeedBoris(E_Vm , B_T , dt_s); x_m += dt_s/2 * v_ms; - distance_m += dt_s/2 * vAbs_ms(); + distance_m += dt_s/2 * getvAbs_ms(); return(distance_m); } void Particle::changeSpeedBoris(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*q_e * e_C; + double q_C = -1*Z * e_C; double m_kg = m_eV*e_C/c_ms/c_ms; t = q_C*dt_s/(2*m_kg) * B_T; diff --git a/src/mc_electron/particle.h b/src/mc_electron/particle.h index 1366baf..595fdef 100644 --- a/src/mc_electron/particle.h +++ b/src/mc_electron/particle.h @@ -6,17 +6,8 @@ // Copyright © 2018 Christian Reiling. All rights reserved. // -#ifndef particle_hpp -#define particle_hpp - -#include -#include -#include -#include -#include -#include "blitz/array.h" -#include "blitz/tinyvec2.h" -#include +#ifndef particle_h +#define particle_h #include "constants.h" @@ -24,99 +15,58 @@ typedef enum species{undef, electron, positron, proton, tritium, tritium2, heliu std::ostream& operator<<(std::ostream& os, const species& SpecOut); -class Particle: protected Constants -{ +class Particle: protected Constants { + +public: + virtual ~Particle() {} + Particle(); + Particle(double mass_eV, double charge_e, blitz::TinyVector speed_ms); + Particle(double mass_eV, double charge_e,blitz::TinyVector loc_m, blitz::TinyVector speed_ms, double idnum, double parentIdnum, species spec); + protected: double m_eV; - double q_e; + double Z; blitz::TinyVector x_m; blitz::TinyVector v_ms; species s; unsigned long int Id; unsigned long int parentId; - - double r_m(); - double vAbs_ms(); - double beta(); - double gamma(); - double calcEkin_eV(); - double elaTau(); - double elaEta(); + public: - virtual ~Particle() {} - Particle() {} - - Particle(double mass_eV, double charge_e,blitz::TinyVector loc_m, blitz::TinyVector speed_ms, double idnum, double parentIdnum, species spec) { - m_eV = mass_eV; - q_e = charge_e; - x_m = loc_m; - v_ms = speed_ms; - Id = idnum; - parentId = parentIdnum; - s = spec; - } - - Particle(double mass_eV,blitz::TinyVector speed_ms) { //Resting dummy for elastic scattering - blitz::TinyVector zero; - zero = 0,0,0; - m_eV = mass_eV; - q_e = 0; - x_m = zero; - v_ms = speed_ms; - Id = 0; - parentId = 0; - s = undef; - } - - - // Get Properties Methods double getMass_eV() { return(m_eV); } - double getCharge_e() { return(q_e); } + double getCharge_e() { return(Z); } blitz::TinyVector getLoc_m() { return(x_m); } blitz::TinyVector getSpeed_ms() { return(v_ms); } double getId() { return(Id); } double getParentId() { return(parentId); } - double getRadius_m() { return ( r_m() ); } - double getSpeedAbs_ms() { return (vAbs_ms() ); } - double getKinEnergy_eV() { return (calcEkin_eV() ); } + + // Calc Properties + + double getRadius_m() { return ( sqrt( pow(x_m[0],2) + pow(x_m[1],2) ) ); } + double getvAbs_ms() { return ( calcAbs(v_ms) ); } + double getKinEnergy_eV() { return ( (getGamma()-1) * m_eV); } + double getBeta() {return ( getvAbs_ms()/c_ms ); } + double getGamma() {return ( pow(1- pow(getBeta(),2),-0.5)); } // 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; }; - // Boris Push + /* + 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); protected: void changeLocFly(double dt_s); void changeSpeedBoris(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s); - - // Interactions -public: - double getSigmaTot_m2() { return (getSigmaEla_m2()+getSigmaIon_m2()); } - -protected: - void elasticKinematicClassical(Particle *P2, double theta, double phi); - blitz::TinyVector getNewDir(double theta,double phi,blitz::TinyVector oldDir); - - // Elastic Scattering -public: - double getSigmaEla_m2(); - void doElaScat (ranlib::Uniform *rngUni); -protected: - double getRandomTheta(ranlib::Uniform *rngUni); - // Ionization -public: - double getSigmaIon_m2(); - void doIon (ranlib::Uniform *rngUni); - - + // Write Methods public: friend std::ostream& operator<<(std::ostream& os, const Particle& partOut); }; - #endif diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 3bf829b..e8ab802 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -10,8 +10,11 @@ void Simulation::doit() { + double MFP_m; blitz::TinyVector loc_m; - bool insideFlag; + + Particle ScatT2; + Interaction IntereT2; while ( (numInjectedParticles < numTotalParticles) || (particleQue.size() > 0) ) { if (particleQue.empty()) { @@ -21,37 +24,41 @@ void Simulation::doit() { std::cout< loc_m, speed_ms; double r_m,phi_rad,z_m; blitz::TinyVector speedDir; - double Ekin_eV,vAbs_ms,vTheta_rad,vPhi_rad; + double Ekin_eV; r_m = wgtsRadius_m*sqrt(rngUni.random()); phi_rad = rngUni.random()*2*pi; @@ -76,13 +83,7 @@ Particle Simulation::inject() { exit(1); break; } - - vPhi_rad = 2*pi*rngUni.random(); - vTheta_rad = pi*rngUni.random(); - vAbs_ms = calcV_ms(Ekin_eV); - speed_ms = sin(vTheta_rad)*cos(vPhi_rad) , sin(vTheta_rad)*sin(vPhi_rad) , cos(vTheta_rad); - speed_ms *= vAbs_ms; - + speed_ms = getRandDir(&rngUni) * calcV_msFromEkin_eV(Ekin_eV,me_eV); return (Particle(me_eV,qe_e,loc_m, speed_ms,numInjectedParticles,numInjectedParticles,electron)); } double Simulation::getBetaDecayEnergyRejection_eV() { @@ -98,7 +99,6 @@ double Simulation::getBetaDecayEnergyRejection_eV() { } while(eta2*pmax > EnergyDistributionBetaDecay(Ecan_eV)); return(Ecan_eV); } - double Simulation::EnergyDistributionBetaDecay(double Ekin_eV) { if (Ekin_eV>Etridecay_eV) { return (0.); @@ -107,52 +107,82 @@ double Simulation::EnergyDistributionBetaDecay(double Ekin_eV) { return( BetaDecayC * FermiFunction(ZHe3, Ekin_eV) * p_eV*(Ekin_eV+me_eV)*pow(Etridecay_eV-Ekin_eV,2) ); } } - double Simulation::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)) ); } - double Simulation::getEnergyBoltzmann_eV() { double N = rngNormal.random(); - double beta = N*sqrt(kb_eVK*MBTemp_K/me_eV)*N; + double beta = N*sqrt(kb_eVK*MBTemp_K/me_eV); double gamma = 1/sqrt(1-pow(beta,2)); return((gamma-1)*me_eV); } -bool Simulation::move(Particle *inputParticle) { - blitz::TinyVector oldLoc_m = inputParticle->getLoc_m(); - double density_m3 = Kat.getTritiumDensity(oldLoc_m); + +// Moving +void Simulation::moveParticle(Particle *movePart, double MFP_m) { + blitz::TinyVector oldLoc_m = movePart->getLoc_m(); blitz::TinyVector E_Vm = Kat.getElField(oldLoc_m); blitz::TinyVector B_T = Kat.getMagField(oldLoc_m); - double sigTot_m2 = inputParticle->getSigmaTot_m2(); - double MFP_m = 1/(density_m3 * sigTot_m2); - double distance = 0.; while (distance < MFP_m) { - distance += inputParticle->borisFly_m(E_Vm , B_T , dt_s); - if (movedOutside(inputParticle->getLoc_m())) { - logParticle(inputParticle,oldLoc_m); - return(false); + distance += movePart->borisFly_m(E_Vm , B_T , dt_s); + if (movedOutside(movePart->getLoc_m())) { + logParticle(movePart,oldLoc_m); + movePart->setSpeed_ms(zeroVec()); + return; } } - logParticle(inputParticle,oldLoc_m); - return(true); + logParticle(movePart,oldLoc_m); + return; } - - -int Simulation::getInteraction(Particle inputParticle) { - return(0); +double Simulation::getMFP_m(Interaction Interaction) { + //Rework for inserting vector of Interactions + blitz::TinyVector loc_m; + double sigmaTot_m2, density_m3, MFP_m; + loc_m = Interaction.getP1()->getLoc_m(); + + sigmaTot_m2 = Interaction.getSigmaTot_m2(); + density_m3 = Kat.getTritiumDensity(loc_m); + MFP_m = 1/(density_m3 * sigmaTot_m2); + return(MFP_m); } - - bool Simulation::movedOutside(blitz::TinyVector loc_m) { double r_m = sqrt( pow(loc_m(0),2) + pow(loc_m(1),2) ); return ( ! (loc_m[2]>0 && loc_m[2] oldLoc_m) { +void Simulation::logParticle(Particle *logPart,blitz::TinyVector oldLoc_m) { if (!logging) { return; } // Annahme: Ohne Interaction bliebt Bewegung in z-Richtung Konstant. double zold = oldLoc_m[2]; - double znew = inputParticle->getLoc_m()[2]; + double znew = logPart->getLoc_m()[2]; double zmin = std::min(zold,znew); double zmax = std::max(zold,znew); for (int i=0;i=zmin && zLogValues[i]<=zmax) { - outStream[i]<<*inputParticle< -#include -#include -#include -#include -#include -#include -#include -#include "blitz/array.h" -#include "blitz/tinyvec2.h" -#include -#include - #include "parameter.h" #include "katrin.h" #include "particle.h" - +#include "interaction.h" class Simulation: public Parameter @@ -49,23 +36,29 @@ protected: int numInjectedParticles; std::deque particleQue; + // Injection Particle inject(); double getBetaDecayEnergyRejection_eV(); double EnergyDistributionBetaDecay(double Ekin_eV); double FermiFunction(double Z, double Ekin_eV); double getEnergyBoltzmann_eV(); - bool move(Particle *inputParticle); + // Moving + void moveParticle(Particle *movePart, double MFP_m); + double getMFP_m(Interaction Interaction); bool movedOutside(blitz::TinyVector loc_m); - int getInteraction(Particle inPutParticle); + // Interaction + int getInteraction(Particle interPart); + // Termination + bool checkTermination(Particle termPart); // Logging bool logging; std::vector outStream; std::vector zLogValues; - void logParticle(Particle *inputParticle,blitz::TinyVector oldLoc_m); + void logParticle(Particle *logPart,blitz::TinyVector oldLoc_m); std::string getTimeString(); }; diff --git a/src/mc_electron/tests.cpp b/src/mc_electron/tests.cpp index ed52f27..70e14fb 100644 --- a/src/mc_electron/tests.cpp +++ b/src/mc_electron/tests.cpp @@ -14,15 +14,8 @@ void runTests() { TEST.decaySpectrum(100000, outputFilePath + "decaySpectrum.dat"); TEST.rejectionBetaDecay(100000, outputFilePath + "rejectionBetaDecay.dat"); TEST.maxwellBoltzmann(100000, outputFilePath + "maxwellBoltzmann.dat"); - - TestParticle TESTPART; - TESTPART.sigmaEla_m2 (0, 20e3, 100000, outputFilePath + "sigmaEla_m2.dat"); - TESTPART.randomTheta (18.6e3, 100000, outputFilePath + "randomTheta.dat"); } - - - void TestSimulation::injection (int numParticles, std::string outputFilePath) { std::ofstream outFile; outFile.open (outputFilePath); @@ -54,6 +47,7 @@ void TestSimulation::rejectionBetaDecay(int numDecays, std::string outputFilePat outFile.close(); } + void TestSimulation::maxwellBoltzmann(int numDecays, std::string outputFilePath) { std::ofstream outFile; outFile.open (outputFilePath); @@ -63,30 +57,3 @@ void TestSimulation::maxwellBoltzmann(int numDecays, std::string outputFilePath) outFile.close(); } -void TestParticle::sigmaEla_m2 (double Emin_eV, double Emax_eV, int numpoints, std::string outputFilePath) { - double E_eV,Estep_eV; - Estep_eV = Emax_eV/(numpoints-1); - m_eV = me_eV; - std::ofstream outFile; - outFile.open (outputFilePath); - - for (int i = 0;i rngUni; - rngUni.seed(2342395328); - outFile.open (outputFilePath); - for (int i = 0;i