diff --git a/src/mc_electron/constants.cpp b/src/mc_electron/constants.cpp new file mode 100644 index 0000000..4f66d7d --- /dev/null +++ b/src/mc_electron/constants.cpp @@ -0,0 +1,40 @@ +// +// constants.cpp +// mc_electron +// +// Created by Christian Reiling on 06.11.18. +// Copyright © 2018 Christian Reiling. All rights reserved. +// + +#include "constants.h" + +const double Constants::pi = 3.1415; +const double Constants::c_nat = 1.; +const double Constants::c_ms = 299792458; +const double Constants::kb_eVK = 8.6173303e-5; +const double Constants::me_eV = 0.511e6; +const double Constants::qe_e = 1; +const double Constants::re_m = 2.817940322719e-15; +const double Constants::e_C = 1.602176462e-19; + + +// Beta Decay +const double Constants::BetaDecayC = 1.2072343416696292e-47; +const double Constants::ZHe3 = 2.; +const double Constants::Etridecay_eV = 18.574e3; + +// Elastic Scattering +const double Constants::ZT2 = 2.; +const double Constants::mT2_eV = 5.61886404234e9; + +// Ionization +const double Constants::rBohr_m = 0.52917721067e-10; +const double Constants::ERydberg_eV = 13.605693009; +const double Constants::EIonizationT2_eV = 13.603; +const double Constants::ionA1 = 0.74; +const double Constants::ionA2 = 0.87; +const double Constants::ionA3 = -0.6; +const double Constants::ionN = 2.4; +const double Constants::ionG5 = 0.33; +const double Constants::ionBeta = 0.6; +const double Constants::ionGamma = 10; diff --git a/src/mc_electron/constants.h b/src/mc_electron/constants.h index 18f8d67..081644f 100644 --- a/src/mc_electron/constants.h +++ b/src/mc_electron/constants.h @@ -9,38 +9,56 @@ #ifndef constants_h #define constants_h +#include + class Constants { public: - virtual ~Constants(){}; Constants() {}; + static const double pi; + static const double c_nat; + static const double c_ms; + static const double kb_eVK; + static const double me_eV; + static const double qe_e; + static const double re_m; + static const double e_C; + + + // Beta Decay + static const double BetaDecayC; + static const double ZHe3; + static const double Etridecay_eV; + + // Elastic Scattering + static const double ZT2; + static const double mT2_eV; + + // Ionization + static const double rBohr_m; + static const double ERydberg_eV; + static const double EIonizationT2_eV; + static const double ionA1; + static const double ionA2; + static const double ionA3; + static const double ionN; + static const double ionG5; + static const double ionBeta; + static const double ionGamma; -protected: - const double pi = 3.1415; - const double c_nat = 1.; - const double c_ms = 299792458; - const double me_eV = 0.511e6; - const double qe_e = 1; - const double re_m = 2.817940322719e-15; - const double e_C = 1.602176462e-19; - const double ZT2 = 2.; - const double mT2_eV = 5.61886404234e9; // Conversion Functions - double v_ms(double Ekin_eV) { + 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 Ekin_eV(double speedAbs_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) ); } }; - - #endif diff --git a/src/mc_electron/elastic.cpp b/src/mc_electron/elastic.cpp deleted file mode 100644 index 45a6753..0000000 --- a/src/mc_electron/elastic.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// -// elastic.cpp -// mc_electron -// -// Created by Christian Reiling on 22.10.18. -// Copyright © 2018 Christian Reiling. All rights reserved. -// - -#include diff --git a/src/mc_electron/interactions.cpp b/src/mc_electron/interactions.cpp new file mode 100644 index 0000000..5d933b5 --- /dev/null +++ b/src/mc_electron/interactions.cpp @@ -0,0 +1,133 @@ +// +// 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/main.cpp b/src/mc_electron/main.cpp index a0ad83c..4e0dc58 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -7,6 +7,7 @@ // #include "simulation.h" +#include "tests.h" #ifdef DEBUG #define Debug( x ) std::cout << x << std::endl; @@ -17,6 +18,18 @@ int main(int argc, const char * argv[]) { Debug("Debug Mode is on.") + + runTests(); + /* + std::clock_t start; + double duration; + start = std::clock(); + Simulation Sim; + Sim.initLogger(); Sim.doit(); + + duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; + std::cout<<"Duration: "<< duration <<'\n'; + */ } diff --git a/src/mc_electron/makefile b/src/mc_electron/makefile deleted file mode 100644 index 6e4d103..0000000 --- a/src/mc_electron/makefile +++ /dev/null @@ -1 +0,0 @@ -include $(HOMEBASE)/src/makeinclude/first.mak diff --git a/src/mc_electron/makefile2 b/src/mc_electron/makefile2 index 7e09f3f..693e7bf 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 +mcelectron: main.o katrin.o simulation.o particle.o interactions.o tests.o $(CXX) -o $@ $^ $(LDFLAGS) $(LOADLIBES) %.o : %.cpp diff --git a/src/mc_electron/parameter.h b/src/mc_electron/parameter.h index 7ec301b..859bee4 100644 --- a/src/mc_electron/parameter.h +++ b/src/mc_electron/parameter.h @@ -26,7 +26,7 @@ public: dt_s = value("dt_s"); injectionMethod = (int)value("injectionMethod"); deltaKinEnergy_eV = value("deltaKinEnergy_eV"); - MBKinEnergy_eV = value("MBKinEnergy_eV"); + MBTemp_K = value("MBTemp_K"); // Katrin Specs wgtsMagField_T = value("wgtsMagField_T"); @@ -93,7 +93,7 @@ protected: double dt_s; int injectionMethod; double deltaKinEnergy_eV; - double MBKinEnergy_eV; + double MBTemp_K; //KATRIN Specs double wgtsMagField_T; diff --git a/src/mc_electron/particle.cpp b/src/mc_electron/particle.cpp index 201fdb9..754efd1 100644 --- a/src/mc_electron/particle.cpp +++ b/src/mc_electron/particle.cpp @@ -10,10 +10,12 @@ #include "particle.h" - // Overload << std::ostream& operator<<(std::ostream& os, const species& SpecOut) { switch (SpecOut) { + case undef: + os<<"undef"; + break; case electron: os<<"e-"; break; @@ -80,22 +82,19 @@ double Particle::gamma() { return ( pow(1- pow(beta(),2),-0.5)); } -double Particle::Ekin_eV() { +double Particle::calcEkin_eV() { return ( (gamma()-1) * m_eV); } -double Particle::tau() { - return (Ekin_eV()/me_eV) ; +double Particle::elaTau() { + return (calcEkin_eV()/me_eV) ; } -double Particle::eta() { - double etac = (Ekin_eV() < 50e3)? 1.198 : 1.13+3.76*pow(ZT2/(137*beta()),2); - return (etac * 1.7e-5 * pow(ZT2,2./3.) /( tau()*(tau()+2) )); +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; @@ -120,61 +119,3 @@ void Particle::changeSpeedBoris(blitz::TinyVector E_Vm , blitz::TinyV v_ms = vPlus_ms + q_C/m_kg*E_Vm*0.5*dt_s; } } - -double Particle::getSigmaTot() { - return(getSigmaEla()); -} - -double Particle::getSigmaEla() { - double nenner = pi*ZT2*(ZT2+1)*re_m*re_m * (1-pow(beta(),2)); - double zaehler = eta() * (eta()+1)*pow(beta(),4); - return(nenner/zaehler); -} - -void Particle::doElaScat(ranlib::Uniform *rngUni) { - // Do Ela Scattering without Tritium Movement - double const1 = 2*pi/getSigmaEla()*ZT2*(ZT2+1)*re_m*re_m*(1- pow(beta(),2) )* pow(beta(),-4); - double theta = acos( pow( rngUni->random()/const1-pow(2*eta(),-1) , -1 ) +1+2*eta()); - - double phi = rngUni->random() * 2 * pi; - double vAbsNew_ms = sqrt( pow(vAbs_ms(),2)*(1-4*m_eV*mT2_eV*pow(m_eV+mT2_eV,-2)*cos(theta)*cos(theta)) ); - - // Change of direction following Plante and Cucinotta - double theta0 = acos(v_ms[2]/vAbs_ms()); - double phi0 = atan2(v_ms[1],v_ms[0]); - - double A3 = sin(theta)*cos(phi); - double A4 =A3*cos(theta0) + cos(theta)*sin(theta0); - double A5 = sin(theta)*sin(phi); - double A11 = A4*cos(phi0) - A5*sin(phi0); - double A21 = A4*sin(phi0) + A5*cos(phi0); - double theta1 = acos( cos(theta0)*cos(theta)-A3*sin(theta0) ); - - - // Get right phi1, maybe more elegant way later. - double phi1c [4]; - double 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."< vnew_ms; - vnew_ms = sin(theta1)*cos(phi1) , sin(theta1)*sin(phi1) , cos(theta1) ; - vnew_ms = vAbsNew_ms * vnew_ms; - setSpeed_ms(vnew_ms); -} diff --git a/src/mc_electron/particle.h b/src/mc_electron/particle.h index 996ec67..1366baf 100644 --- a/src/mc_electron/particle.h +++ b/src/mc_electron/particle.h @@ -20,13 +20,13 @@ #include "constants.h" -typedef enum species{electron, positron, proton, tritium, tritium2, helium, deuterium, DT}species; +typedef enum species{undef, electron, positron, proton, tritium, tritium2, helium, deuterium, DT}species; std::ostream& operator<<(std::ostream& os, const species& SpecOut); -class Particle: private Constants +class Particle: protected Constants { -private: +protected: double m_eV; double q_e; blitz::TinyVector x_m; @@ -39,13 +39,14 @@ private: double vAbs_ms(); double beta(); double gamma(); - double Ekin_eV(); - double tau(); - double eta(); + double calcEkin_eV(); + double elaTau(); + double elaEta(); public: + virtual ~Particle() {} + Particle() {} - virtual ~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; @@ -56,6 +57,20 @@ public: 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); } @@ -65,24 +80,41 @@ public: double getParentId() { return(parentId); } double getRadius_m() { return ( r_m() ); } double getSpeedAbs_ms() { return (vAbs_ms() ); } - double getKinEnergy_eV() { return (Ekin_eV() ); } + double getKinEnergy_eV() { return (calcEkin_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; }; // Boris Push +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); - // Cross Sections - double getSigmaTot(); - double getSigmaEla(); - void doElaScat(ranlib::Uniform *rngUni); + // 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); }; diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 3d0c2c2..3bf829b 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -9,11 +9,158 @@ #include "simulation.h" +void Simulation::doit() { + blitz::TinyVector loc_m; + bool insideFlag; + + while ( (numInjectedParticles < numTotalParticles) || (particleQue.size() > 0) ) { + if (particleQue.empty()) { + particleQue.push_back(inject()); + numInjectedParticles++; + if (numInjectedParticles%100 == 0){ + 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; + + r_m = wgtsRadius_m*sqrt(rngUni.random()); + phi_rad = rngUni.random()*2*pi; + z_m = rngUni.random()*wgtsLength_m; + loc_m = r_m*cos(phi_rad),r_m*sin(phi_rad),z_m; + // Add Code taking in account tritium density + + // Add Code for corregtion of direction by tritium direction und Boltzmann distribution + + switch(injectionMethod) { + case 0: // Fermi + Ekin_eV = getBetaDecayEnergyRejection_eV(); //Implement Later + break; + case 1: //Delta + Ekin_eV = deltaKinEnergy_eV; + break; + case 2: //Maxwell Boltzman + Ekin_eV = getEnergyBoltzmann_eV(); + break; + default: + std::cerr<<"Invalid Injection Method./n"; + 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; + + return (Particle(me_eV,qe_e,loc_m, speed_ms,numInjectedParticles,numInjectedParticles,electron)); +} +double Simulation::getBetaDecayEnergyRejection_eV() { + // get Energy distribution using Rejection Method + double pmax,Emin_eV, Emax_eV, Ecan_eV,eta1,eta2; + do { + pmax = 1.5e-28; // From Plotting EnergyDistributionBetaDecay + Emin_eV = 1e-15; + Emax_eV = Etridecay_eV; + eta1 = rngUni.random(); + eta2 = rngUni.random(); + Ecan_eV = Emin_eV + eta1*(Emax_eV - Emin_eV); + } while(eta2*pmax > EnergyDistributionBetaDecay(Ecan_eV)); + return(Ecan_eV); +} + +double Simulation::EnergyDistributionBetaDecay(double Ekin_eV) { + if (Ekin_eV>Etridecay_eV) { + return (0.); + } else{ + double p_eV = sqrt(Ekin_eV*Ekin_eV + 2 * Ekin_eV * me_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 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); + 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); + } + } + logParticle(inputParticle,oldLoc_m); + return(true); +} + + +int Simulation::getInteraction(Particle inputParticle) { + return(0); +} + + +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] loc_m; - bool insideFlag; - - while ( (numInjectedParticles < numTotalParticles) || (particleQue.size() > 0) ) { - if (particleQue.size() < 1) { - particleQue.push_back(inject()); - numInjectedParticles++; - } - insideFlag = move(&particleQue.front()); - if (insideFlag) { - switch (getInteraction(particleQue.front())) { - case 0: // Elastic Scattering - particleQue.front().doElaScat(&rngUni); - break; - case 1: // Ionization - //particleQue.push_back(); - break; - case 2: // Recombination - particleQue.pop_front(); - break; - default: - std::cerr<<"getprocess() returned unknown event"< loc_m, speed_ms; - double r_m,phi_rad,z_m; - blitz::TinyVector speedDir; - double Ekin_eV,absSpeed_ms; - - r_m = rngUni.random()*wgtsRadius_m; - phi_rad = rngUni.random()*2*pi; - z_m = rngUni.random()*wgtsLength_m; - loc_m = r_m*cos(phi_rad),r_m*sin(phi_rad),z_m; - // Add Code taking in account tritium density - - speedDir = rngUni.random(),rngUni.random(),rngUni.random(); - speedDir = 1./sqrt(dot(speedDir,speedDir))*speedDir; - // Add Code for corregtion of direction by tritium direction!!! - - switch(injectionMethod) { - case 0: // Fermi - Ekin_eV = 100.; //Implement Later - break; - case 1: //Delta - Ekin_eV = deltaKinEnergy_eV; - break; - case 2: //Maxwell Boltzman - Ekin_eV = 100.; //Implement Later - break; - default: - std::cerr<<"Invalid Injection Method./n"; - exit(1); - break; - } - - absSpeed_ms = v_ms(Ekin_eV); - speed_ms = absSpeed_ms * speedDir; - - return (Particle(me_eV,qe_e,loc_m, speed_ms,numInjectedParticles,NULL,electron)); -} - -bool Simulation::move(Particle *inputParticle) { - blitz::TinyVector oldLoc_m = inputParticle->getLoc_m(); - double density_m3 = Kat.getTritiumDensity(oldLoc_m); - blitz::TinyVector E_Vm = Kat.getElField(oldLoc_m); - blitz::TinyVector B_T = Kat.getMagField(oldLoc_m); - - double sigTot_m2 = inputParticle->getSigmaTot(); - 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); - } - } - logParticle(inputParticle,oldLoc_m); - return(true); -} - -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) { + if (!logging) { + return; + } // Annahme: Ohne Interaction bliebt Bewegung in z-Richtung Konstant. + double zold = oldLoc_m[2]; double znew = inputParticle->getLoc_m()[2]; + double zmin = std::min(zold,znew); + double zmax = std::max(zold,znew); + for (int i=0;i=zold && zLogValues[i]<=znew) { + if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) { outStream[i]<<*inputParticle< +#include #include "parameter.h" #include "katrin.h" @@ -34,10 +35,8 @@ public: Simulation() { numInjectedParticles = 0; rngUni.seed(seed); - initLogger(); - + logging = false; }; - void initLogger(void); void doit(void); @@ -46,10 +45,16 @@ protected: // Simulation Katrin Kat; ranlib::Uniform rngUni; + ranlib::NormalUnit rngNormal; int numInjectedParticles; std::deque particleQue; Particle inject(); + double getBetaDecayEnergyRejection_eV(); + double EnergyDistributionBetaDecay(double Ekin_eV); + double FermiFunction(double Z, double Ekin_eV); + double getEnergyBoltzmann_eV(); + bool move(Particle *inputParticle); bool movedOutside(blitz::TinyVector loc_m); int getInteraction(Particle inPutParticle); @@ -57,12 +62,11 @@ protected: // Logging + bool logging; std::vector outStream; std::vector zLogValues; void logParticle(Particle *inputParticle,blitz::TinyVector oldLoc_m); std::string getTimeString(); - - }; #endif diff --git a/src/mc_electron/tests.cpp b/src/mc_electron/tests.cpp index 085945d..ed52f27 100644 --- a/src/mc_electron/tests.cpp +++ b/src/mc_electron/tests.cpp @@ -6,4 +6,87 @@ // Copyright © 2018 Christian Reiling. All rights reserved. // -#include "tests.hpp" +#include "tests.h" +void runTests() { + std::string outputFilePath = "/Users/ChristianReiling/Documents/Masterarbeit/CodeTesting/Data/"; + TestSimulation TEST; + TEST.injection (100000, outputFilePath + "injection.dat"); + 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); + for (int i = 0;i rngUni; + rngUni.seed(2342395328); + outFile.open (outputFilePath); + for (int i = 0;i +#include "simulation.h" -#endif /* tests_hpp */ + +void runTests(); + + +class TestSimulation: public Simulation { +public: + + ~TestSimulation(){} + TestSimulation() : Simulation(){}; + + void injection (int numParticles, std::string outputFilePath); + void decaySpectrum (int numPoints, std::string outputFilePath); + void rejectionBetaDecay(int numDecays, std::string outputFilePath); + void maxwellBoltzmann(int numDecays, std::string outputFilePath); +}; + +class TestParticle: public Particle { +public: + ~TestParticle(){} + TestParticle(){}; + + void sigmaEla_m2 (double Emin, double Emax, int numpoints, std::string outputFilePath); + void randomTheta (double E_eV, int numCalculations, std::string outputFilePath); + + + +}; + +#endif