diff --git a/src/mc_electron/constants.h b/src/mc_electron/constants.h new file mode 100644 index 0000000..18f8d67 --- /dev/null +++ b/src/mc_electron/constants.h @@ -0,0 +1,46 @@ +// +// constants.h +// mc_electron +// +// Created by Christian Reiling on 08.10.18. +// Copyright © 2018 Christian Reiling. All rights reserved. +// + +#ifndef constants_h +#define constants_h + +class Constants +{ +public: + virtual ~Constants(){}; + Constants() {}; + +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 gamma = 1. + Ekin_eV/me_eV; + double beta = sqrt(1 - pow(gamma,-2)); + return(beta * c_ms); + + } + double Ekin_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/main.cpp b/src/mc_electron/main.cpp index c41bcc7..5975980 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -8,7 +8,15 @@ #include "simulation.h" +#ifdef DEBUG +#define Debug( x ) std::cout << x << std::endl; +#else +#define Debug( x ) +#endif + + int main(int argc, const char * argv[]) { + Debug("Debug Mode is on.") Simulation Sim; Sim.init(); Sim.doit(); diff --git a/src/mc_electron/parameter.h b/src/mc_electron/parameter.h index 20f44b6..76c5a36 100644 --- a/src/mc_electron/parameter.h +++ b/src/mc_electron/parameter.h @@ -12,8 +12,9 @@ #include #include +#include "constants.h" -class Parameter +class Parameter: protected Constants { public: virtual ~Parameter(){}; @@ -33,14 +34,6 @@ public: wgtsRadius_m = value("wgtsRadius_m"); wgtsTritiumDensity_m3 = value("wgtsTritiumDensity_m3"); } - // Natural Constants - 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; - private: double value(std::string strin, char* ext = (char*)("cat")) { @@ -105,19 +98,7 @@ protected: double wgtsLength_m; double wgtsRadius_m; double wgtsTritiumDensity_m3; - - // Help Functions - double v_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 beta = speedAbs_ms/c_ms; - double gamma = pow(1 - pow(beta,2),-0.5); - return ( (gamma-1) * me_eV * pow(c_nat,2) ); - } - + }; diff --git a/src/mc_electron/parameters.cat b/src/mc_electron/parameters.cat index d85d3ff..29c4e64 100644 --- a/src/mc_electron/parameters.cat +++ b/src/mc_electron/parameters.cat @@ -1,7 +1,7 @@ seed = 2342395328 -numTotalParticles = 100 -dt_s = 0.00000001 -injectionMethod = 2 +numTotalParticles = 1 +dt_s = 0.0000000001 +injectionMethod = 1 deltaKinEnergy_eV = 100 MBKinEnergy_eV = 100 diff --git a/src/mc_electron/particle.cpp b/src/mc_electron/particle.cpp index 7fda9f9..201fdb9 100644 --- a/src/mc_electron/particle.cpp +++ b/src/mc_electron/particle.cpp @@ -9,6 +9,9 @@ #include "particle.h" + + +// Overload << std::ostream& operator<<(std::ostream& os, const species& SpecOut) { switch (SpecOut) { case electron: @@ -41,7 +44,6 @@ std::ostream& operator<<(std::ostream& os, const species& SpecOut) { } return (os); } - std::ostream& operator<<(std::ostream& os, const Particle& partOut) { os << partOut.Id << ";" @@ -57,3 +59,122 @@ std::ostream& operator<<(std::ostream& os, const Particle& partOut) { << partOut.v_ms[2] << ";"; return os; } + + + + +// Private Functions +double Particle::r_m() { + return ( sqrt( pow(x_m[0],2) + pow(x_m[1],2) ) ); +} + +double Particle::vAbs_ms() { + return ( sqrt( pow(v_ms[0],2) + pow(v_ms[1],2) + pow(v_ms[2],2)) ); +} + +double Particle::beta() { + return ( vAbs_ms()/c_ms ); +} + +double Particle::gamma() { + return ( pow(1- pow(beta(),2),-0.5)); +} + +double Particle::Ekin_eV() { + return ( (gamma()-1) * m_eV); +} + +double Particle::tau() { + return (Ekin_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::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(); + this->changeSpeedBoris(E_Vm , B_T , dt_s); + x_m += dt_s/2 * v_ms; + distance_m += dt_s/2 * vAbs_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 m_kg = m_eV*e_C/c_ms/c_ms; + + t = q_C*dt_s/(2*m_kg) * B_T; + s = (2*t)/(1+dot(t,t)); + vMinus_ms = v_ms + (q_C*E_Vm*dt_s)/(2*m_kg); + vPrime_ms = vMinus_ms + cross(vMinus_ms,t); + vPlus_ms = vMinus_ms + cross(vPrime_ms,s); + 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 b46019c..996ec67 100644 --- a/src/mc_electron/particle.h +++ b/src/mc_electron/particle.h @@ -16,11 +16,15 @@ #include #include "blitz/array.h" #include "blitz/tinyvec2.h" +#include + +#include "constants.h" typedef enum species{electron, positron, proton, tritium, tritium2, helium, deuterium, DT}species; std::ostream& operator<<(std::ostream& os, const species& SpecOut); -class Particle + +class Particle: private Constants { private: double m_eV; @@ -30,6 +34,15 @@ private: species s; unsigned long int Id; unsigned long int parentId; + + double r_m(); + double vAbs_ms(); + double beta(); + double gamma(); + double Ekin_eV(); + double tau(); + double eta(); + public: virtual ~Particle() { } @@ -43,125 +56,35 @@ public: s = spec; } - //Particle(Array props); //Create from Array - // Get Properties Methods - double getMass_eV() { - return(m_eV); - } - - double getCharge_e() { - return(q_e); - } - - 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 (sqrt( pow(x_m[0],2) + pow(x_m[1],2) )); - } - - double getSpeedAbs_ms() { - return (sqrt( pow(v_ms[0],2) + pow(v_ms[1],2) + pow(v_ms[2],2))); - } - - double getKinEnergy_eV() { - // Implement Constants here, else Particle::Parameters - const double c_ms = 299792458; - const double me_eV = 0.511e6; - double SpeedAbs_ms = sqrt( pow(v_ms[0],2) + pow(v_ms[1],2) + pow(v_ms[2],2) ); - double beta = SpeedAbs_ms/c_ms; - double gamma = pow(1- pow(beta,2),-0.5); - return ( (gamma-1) * me_eV); - } + double getMass_eV() { return(m_eV); } + double getCharge_e() { return(q_e); } + 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 (Ekin_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 + double borisFly_m(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s); + 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); + + // Write Methods friend std::ostream& operator<<(std::ostream& os, const Particle& partOut); }; - - -// Some Code, maybe required later - /* - blitz::TinyVector locChange(blitz::TinyVector dx, int i, blitz::TinyVector xmin, blitz::TinyVector nx, blitz::TinyVector dvdt); - blitz::TinyVector speedChange(blitz::TinyVector dv, int i); - - void copyol(particle*); - Array prop(Array); - virtual Array array(); // ********************** warum virtual???? ****************************** - //void copyol(particle); - */ -/* - class particle_info : public particle { - - private: - TinyVector x0,p0; - double id; - double mue0; - - public: - particle_info(TinyVector anfangsort, TinyVector anfangsimpuls, double nummer, double anfangswinkel ):particle() - { - x0 = anfangsort; - p0 = anfangsimpuls; - mue0 = anfangswinkel; - id = nummer; - } - - particle_info() { } - ~particle_info() { } - - - TinyVector anfangsort() { return(x0); } - TinyVector anfangsimpuls() { return(p0); } - double nummer() { return(id); } - double anfangswinkel() { return(mue0); } - // virtual void copyol(particle_info*); - - Array array () // *********************** auslagern in .cpp ************************** - { - Array teilchenprop; - TinyVector temp; - temp=8; - teilchenprop.resize(temp); - - x0 = this->anfangsort(); - p0 = this->anfangsimpuls(); - id = this->nummer(); - mue0 = this->anfangswinkel(); - - teilchenprop(0) = x0(0); - teilchenprop(1) = x0(1); - teilchenprop(2) = x0(2); - teilchenprop(3) = p0(0); - teilchenprop(4) = p0(1); - teilchenprop(5) = p0(2); - teilchenprop(6) = id; - teilchenprop(7) = mue0; - - return(teilchenprop); - } - }; - */ - -// Other Functions - #endif diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 351de6a..8fbd03a 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -13,64 +13,34 @@ void Simulation::init() { } void Simulation::doit() { - double energy_eV,sigTot_m2, density_m3,MFP_m; - blitz::TinyVector oldLoc_m, newLoc_m; + blitz::TinyVector loc_m; + bool insideFlag; - while (numInjectedParticles < numTotalParticles) { - if (particleQueue.size() < 1) { - particleQueue.assign(1,inject()); + while ( (numInjectedParticles < numTotalParticles) || (particleQue.size() > 0) ) { + if (particleQue.size() < 1) { + particleQue.push_back(inject()); numInjectedParticles++; } - - particleQueueIt = particleQueue.begin(); - while (particleQueueIt != particleQueue.end()) { - step = 0; - while (true) { - energy_eV = particleQueueIt->getKinEnergy_eV(); - sigTot_m2 = sigmaTot_m2(energy_eV); - density_m3 = Kat.getTritiumDensity(particleQueueIt->getLoc_m()); - MFP_m = 1/ ( density_m3 * sigTot_m2); - oldLoc_m = particleQueueIt->getLoc_m(); - newLoc_m = move(*particleQueueIt ,MFP_m); - if (movedOutside(newLoc_m)) { - // Reflexion oder Absorbtion? Erstmal Reflexion - newLoc_m = moveToLocOfDeath(*particleQueueIt); - particleQueueIt->setLoc_m(newLoc_m); - - writeToLogger(*particleQueueIt); - goto particleDied; - } - - particleQueueIt->setLoc_m(newLoc_m); - writeToLogger(*particleQueueIt); - - - /* - switch (getprocess()) { - case "ElasticScattering": - break; - case "Ionization": - break; - case "Recombination"; - - partQueue.erase(partQueue.begin()); - continue; - break; - default: - cerr<<"getprocess() returned unknown event"< Simulation::move(Particle inputParticle,double distance_m) { - //Pseudocode, lineare bewegung - blitz::TinyVector oldLoc_m = inputParticle.getLoc_m(); - blitz::TinyVector v_ms =inputParticle.getSpeed_ms(); - blitz::TinyVector SpeedDir = pow(dot(v_ms,v_ms),-0.5)*v_ms; - return( oldLoc_m + SpeedDir*distance_m); +bool Simulation::move(Particle *inputParticle) { + blitz::TinyVector loc_m = inputParticle->getLoc_m(); + double density_m3 = Kat.getTritiumDensity(loc_m); + blitz::TinyVector E_Vm = Kat.getElField(loc_m); + blitz::TinyVector B_T = Kat.getMagField(loc_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); + // Log Movment here!!! + //std::cout<<*inputParticle<getLoc_m())) { + return(false); + } + } + 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] < wgtsLength_m && r_m < wgtsRadius_m ) ); + return ( ! (loc_m[2]>0 && loc_m[2] Simulation::moveToLocOfDeath(Particle inputParticle) { - - // Führe methode 1 step für move ein, die einen einzelnen Schritt in move ausführt und benutze diese, bis an Rand - double stepsize = inputParticle.getSpeedAbs_ms()*dt_s; - blitz::TinyVector loc_m = inputParticle.getLoc_m(); - blitz::TinyVector rVec_m; - double r_m; - - // Include saftey calc for too small stepsize - - int countfordebug = 0; - while (!movedOutside(loc_m) ) { - loc_m = move (inputParticle,stepsize); - inputParticle.setLoc_m(loc_m); - countfordebug++; - } - - if (loc_m(2) < 0) { - loc_m(2) = 0; - } else if (loc_m(2) > wgtsLength_m) { - loc_m(2) = wgtsLength_m; - } - - r_m = sqrt(pow(loc_m(0),2)+pow(loc_m(1),2)); - if (r_m > wgtsRadius_m) { - loc_m(0) = wgtsRadius_m/r_m * loc_m(0); - loc_m(1) = wgtsRadius_m/r_m * (loc_m(1)); - } - return (loc_m); +int Simulation::getInteraction(Particle inputParticle) { + return(0); } + + +/* // Logging Methods void Simulation::writeToLogger(Particle inputParticle) { std::string textLine; - + particleLogger.push_back(textLine); } @@ -180,35 +137,10 @@ void Simulation::writeToFile(std::vector *particleLogger) { } outFile.close(); } +*/ + + -// Cross Sections -double Simulation::sigmaTot_m2(double Ekin_eV) { - return(sigmaEla_m2(Ekin_eV)); -} -double Simulation::sigmaEla_m2(double Ekin_eV) { - // From Plante and S Uehara et al 1998 - double Z, tau, beta, etac, eta, nenner, zaehler; - Z = 1.; - tau = Ekin_eV/me_eV; - beta = v_ms(Ekin_eV)/c_ms; - etac = (Ekin_eV < 50e3)? 1.198 : 1.13+3.76*pow(Z/(137*beta),2); //beta im nenner oder zähler? - eta = etac * 1.7e-5 * pow(Z,2./3.) /( tau*(tau+2) ); - nenner = pi*Z*(Z+1)*re_m*re_m * (1-pow(beta,2)); - zaehler = eta * (eta+1)*pow(beta,4); - return(nenner/zaehler); -} -double Simulation::dSigmaEladOmega_m2 (double Ekin_eV, double Theta_rad, double Phi_rad) { - // From Plante and S Uehara et al 1998 - double Z, tau, beta, etac, eta, nenner, zaehler; - Z = 1.; - tau = Ekin_eV/me_eV; - beta = v_ms(Ekin_eV)/c_ms; - etac = (Ekin_eV < 50e3)? 1.198 : 1.13+3.76*pow(Z/(137*beta),2); //beta im nenner oder zähler? - eta = etac * 1.7e-5 * pow(Z,2./3.) /( tau*(tau+2) ); - nenner = Z*(Z+1)*re_m*re_m * (1-pow(beta,2)); - zaehler = pow(1.-cos(Theta_rad)+2*eta,2) * pow(beta,4); - return(nenner/zaehler); -} diff --git a/src/mc_electron/simulation.h b/src/mc_electron/simulation.h index ff66941..2717edb 100644 --- a/src/mc_electron/simulation.h +++ b/src/mc_electron/simulation.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include #include "blitz/array.h" @@ -30,8 +30,6 @@ public: Simulation() { numInjectedParticles = 0; rngUni.seed(seed); - particleLogger.clear(); - outputName = "TestRun"; // Check OutFile already existing? -no-> Create Outfile_1 @@ -49,9 +47,17 @@ protected: Katrin Kat; ranlib::Uniform rngUni; int numInjectedParticles; - std::vector particleQueue, newParticles; - std::vector::iterator particleQueueIt; + std::deque particleQue; + // Simulation Methods + Particle inject(); + bool move(Particle *inputParticle); + bool movedOutside(blitz::TinyVector loc_m); + + int getInteraction(Particle inPutParticle); + + + /* // Logging Objects int step; std::string outputName; @@ -59,22 +65,13 @@ protected: std::vector::iterator particleLoggerIt; std::ofstream outFile; - // Simulation Methods - Particle inject(); - blitz::TinyVector move(Particle inputParticle,double distance_m); - bool movedOutside(blitz::TinyVector loc_m); - blitz::TinyVector moveToLocOfDeath(Particle inputParticle); // Logging Methods void writeToLogger(Particle inputParticle); void writeToFile(std::vector *particleLogger); - - // Cross Sections - double sigmaTot_m2(double Ekin_eV); - double sigmaEla_m2(double Ekin_eV); - double dSigmaEladOmega_m2 (double Ekin_eV, double Theta_rad, double Phi_rad); - + */ + }; #endif