Added new test class, reworked some code
Started Implementing Ionization
This commit is contained in:
@@ -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;
|
||||
+34
-16
@@ -9,38 +9,56 @@
|
||||
#ifndef constants_h
|
||||
#define constants_h
|
||||
|
||||
#include <math.h>
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
//
|
||||
// elastic.cpp
|
||||
// mc_electron
|
||||
//
|
||||
// Created by Christian Reiling on 22.10.18.
|
||||
// Copyright © 2018 Christian Reiling. All rights reserved.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// interactions.cpp
|
||||
// mc_electron
|
||||
//
|
||||
// Created by Christian Reiling on 22.10.18.
|
||||
// Copyright © 2018 Christian Reiling. All rights reserved.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "particle.h"
|
||||
|
||||
void Particle::elasticKinematicClassical(Particle *P2, double theta, double phi) {
|
||||
// Following Landau Lifschitz I Chapter 4 Paragraph 17
|
||||
blitz::TinyVector<double,3> 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<double,3> Particle::getNewDir(double theta,double phi,blitz::TinyVector<double,3> 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."<<std::endl;
|
||||
exit(1);
|
||||
}
|
||||
blitz::TinyVector<double,3> 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<double> *rngUni) {
|
||||
// Compute Scattering Angle
|
||||
double theta = getRandomTheta(rngUni);
|
||||
double phi = rngUni->random() * 2 * pi;
|
||||
|
||||
// Compute Interaction Partner
|
||||
blitz::TinyVector<double,3> speed;
|
||||
speed = 0,0,0;
|
||||
Particle P2 = Particle(mT2_eV,speed);
|
||||
elasticKinematicClassical(&P2, theta, phi);
|
||||
}
|
||||
|
||||
double Particle::getRandomTheta(ranlib::Uniform<double> *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<double> *rngUni) {
|
||||
// Get Energy after Interaction
|
||||
|
||||
// Get Scattering Angle
|
||||
|
||||
// Do Elastic Scattering with a resting electron
|
||||
// Energy Initial from First Part, Secondary Electron:
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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';
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
include $(HOMEBASE)/src/makeinclude/first.mak
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<double, 3> E_Vm , blitz::TinyVector<double, 3> B_T , double dt_s) {
|
||||
double distance_m;
|
||||
x_m += dt_s/2 * v_ms;
|
||||
@@ -120,61 +119,3 @@ void Particle::changeSpeedBoris(blitz::TinyVector<double, 3> 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<double> *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."<<std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> vnew_ms;
|
||||
vnew_ms = sin(theta1)*cos(phi1) , sin(theta1)*sin(phi1) , cos(theta1) ;
|
||||
vnew_ms = vAbsNew_ms * vnew_ms;
|
||||
setSpeed_ms(vnew_ms);
|
||||
}
|
||||
|
||||
+44
-12
@@ -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<double,3> 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<double,3> loc_m, blitz::TinyVector<double,3> 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<double,3> speed_ms) { //Resting dummy for elastic scattering
|
||||
blitz::TinyVector<double,3> 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<double, 3> xnew_m) {x_m = xnew_m; };
|
||||
void setSpeed_ms(blitz::TinyVector<double, 3> vnew_ms) {v_ms = vnew_ms; };
|
||||
|
||||
// Boris Push
|
||||
public:
|
||||
double borisFly_m(blitz::TinyVector<double, 3> E_Vm , blitz::TinyVector<double, 3> B_T , double dt_s);
|
||||
protected:
|
||||
void changeLocFly(double dt_s);
|
||||
void changeSpeedBoris(blitz::TinyVector<double, 3> E_Vm , blitz::TinyVector<double, 3> B_T , double dt_s);
|
||||
|
||||
// Cross Sections
|
||||
double getSigmaTot();
|
||||
double getSigmaEla();
|
||||
void doElaScat(ranlib::Uniform<double> *rngUni);
|
||||
// Interactions
|
||||
public:
|
||||
double getSigmaTot_m2() { return (getSigmaEla_m2()+getSigmaIon_m2()); }
|
||||
|
||||
protected:
|
||||
void elasticKinematicClassical(Particle *P2, double theta, double phi);
|
||||
blitz::TinyVector<double,3> getNewDir(double theta,double phi,blitz::TinyVector<double,3> oldDir);
|
||||
|
||||
// Elastic Scattering
|
||||
public:
|
||||
double getSigmaEla_m2();
|
||||
void doElaScat (ranlib::Uniform<double> *rngUni);
|
||||
protected:
|
||||
double getRandomTheta(ranlib::Uniform<double> *rngUni);
|
||||
// Ionization
|
||||
public:
|
||||
double getSigmaIon_m2();
|
||||
void doIon (ranlib::Uniform<double> *rngUni);
|
||||
|
||||
|
||||
// Write Methods
|
||||
public:
|
||||
friend std::ostream& operator<<(std::ostream& os, const Particle& partOut);
|
||||
};
|
||||
|
||||
|
||||
+157
-103
@@ -9,11 +9,158 @@
|
||||
#include "simulation.h"
|
||||
|
||||
|
||||
void Simulation::doit() {
|
||||
blitz::TinyVector <double,3> loc_m;
|
||||
bool insideFlag;
|
||||
|
||||
while ( (numInjectedParticles < numTotalParticles) || (particleQue.size() > 0) ) {
|
||||
if (particleQue.empty()) {
|
||||
particleQue.push_back(inject());
|
||||
numInjectedParticles++;
|
||||
if (numInjectedParticles%100 == 0){
|
||||
std::cout<<numInjectedParticles<<"/"<<numTotalParticles<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
insideFlag = move(&particleQue.front());
|
||||
if (insideFlag) {
|
||||
switch (getInteraction(particleQue.front())) {
|
||||
case 0: // Elastic Scattering
|
||||
particleQue.front().doElaScat(&rngUni);
|
||||
std::cout<<particleQue.front()<<particleQue.front().getKinEnergy_eV()<<std::endl;
|
||||
break;
|
||||
case 1: // Ionization
|
||||
//particleQue.push_back();
|
||||
break;
|
||||
case 2: // Recombination
|
||||
particleQue.pop_front();
|
||||
break;
|
||||
default:
|
||||
std::cerr<<"getprocess() returned unknown event"<<std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
}else {
|
||||
particleQue.pop_front();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Particle Simulation::inject() {
|
||||
blitz::TinyVector<double,3> loc_m, speed_ms;
|
||||
double r_m,phi_rad,z_m;
|
||||
blitz::TinyVector<double,3> 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<double,3> oldLoc_m = inputParticle->getLoc_m();
|
||||
double density_m3 = Kat.getTritiumDensity(oldLoc_m);
|
||||
blitz::TinyVector<double,3> E_Vm = Kat.getElField(oldLoc_m);
|
||||
blitz::TinyVector<double,3> 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<double,3> 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 ) );
|
||||
}
|
||||
|
||||
void Simulation::initLogger(void) {
|
||||
// Set zLogValues;
|
||||
zLogValues.clear();
|
||||
logging = true;
|
||||
if (numberZBins < 1) {
|
||||
std::cout<<"No Logging Selected"<<std::endl;
|
||||
std::cout<<"Logging turned off."<<std::endl;
|
||||
logging = false;
|
||||
return;
|
||||
} else if (numberZBins == 1) {
|
||||
std::cout<<"Logging 1 z-Value: "<<wgtsLength_m<<std::endl;
|
||||
zLogValues.push_back(wgtsLength_m);
|
||||
@@ -23,7 +170,7 @@ void Simulation::initLogger(void) {
|
||||
}
|
||||
std::cout<<"Logging "<<numberZBins<<" z-Values."<<std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::string poub = getenv("poub");
|
||||
std::string pname = getenv("pname");
|
||||
int i = 1;
|
||||
@@ -62,113 +209,20 @@ std::string Simulation::getTimeString() {
|
||||
return(str);
|
||||
}
|
||||
|
||||
void Simulation::doit() {
|
||||
blitz::TinyVector <double,3> 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"<<std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
}else {
|
||||
particleQue.pop_front();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Particle Simulation::inject() {
|
||||
blitz::TinyVector<double,3> loc_m, speed_ms;
|
||||
double r_m,phi_rad,z_m;
|
||||
blitz::TinyVector<double,3> 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<double,3> oldLoc_m = inputParticle->getLoc_m();
|
||||
double density_m3 = Kat.getTritiumDensity(oldLoc_m);
|
||||
blitz::TinyVector<double,3> E_Vm = Kat.getElField(oldLoc_m);
|
||||
blitz::TinyVector<double,3> 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<double,3> 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 ) );
|
||||
}
|
||||
|
||||
void Simulation::logParticle(Particle *inputParticle,blitz::TinyVector<double,3> 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<numberZBins;i++) {
|
||||
if (zLogValues[i]>=zold && zLogValues[i]<=znew) {
|
||||
if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) {
|
||||
outStream[i]<<*inputParticle<<std::endl; // Location wrong, because it s the new Location.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Simulation::getInteraction(Particle inputParticle) {
|
||||
return(0);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "blitz/array.h"
|
||||
#include "blitz/tinyvec2.h"
|
||||
#include <random/uniform.h>
|
||||
#include <random/normal.h>
|
||||
|
||||
#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<double> rngUni;
|
||||
ranlib::NormalUnit<double> rngNormal;
|
||||
int numInjectedParticles;
|
||||
std::deque<Particle> 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<double,3> loc_m);
|
||||
int getInteraction(Particle inPutParticle);
|
||||
@@ -57,12 +62,11 @@ protected:
|
||||
|
||||
|
||||
// Logging
|
||||
bool logging;
|
||||
std::vector<std::ofstream> outStream;
|
||||
std::vector<double> zLogValues;
|
||||
void logParticle(Particle *inputParticle,blitz::TinyVector<double,3> oldLoc_m);
|
||||
std::string getTimeString();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<numParticles;i++) {
|
||||
outFile << inject() <<std::endl;
|
||||
}
|
||||
outFile.close();
|
||||
|
||||
}
|
||||
|
||||
void TestSimulation::decaySpectrum (int numPoints,std::string outputFilePath) {
|
||||
std::ofstream outFile;
|
||||
outFile.open (outputFilePath);
|
||||
double x,y;
|
||||
for (int i = 0;i<numPoints;i++) {
|
||||
x = i * Etridecay_eV/(numPoints-1);
|
||||
y = EnergyDistributionBetaDecay(x);
|
||||
outFile << x <<";"<<y <<std::endl;
|
||||
}
|
||||
outFile.close();
|
||||
}
|
||||
|
||||
void TestSimulation::rejectionBetaDecay(int numDecays, std::string outputFilePath) {
|
||||
std::ofstream outFile;
|
||||
outFile.open (outputFilePath);
|
||||
for (int i = 0;i<numDecays;i++) {
|
||||
outFile << getBetaDecayEnergyRejection_eV() <<std::endl;
|
||||
}
|
||||
outFile.close();
|
||||
}
|
||||
|
||||
void TestSimulation::maxwellBoltzmann(int numDecays, std::string outputFilePath) {
|
||||
std::ofstream outFile;
|
||||
outFile.open (outputFilePath);
|
||||
for (int i = 0;i<numDecays;i++) {
|
||||
outFile << getEnergyBoltzmann_eV() <<std::endl;
|
||||
}
|
||||
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<numpoints;i++) {
|
||||
E_eV = i*Estep_eV;
|
||||
v_ms = calcV_ms(E_eV) , 0 , 0;
|
||||
outFile << E_eV<<";"<<getSigmaEla_m2() <<std::endl;
|
||||
}
|
||||
outFile.close();
|
||||
}
|
||||
|
||||
void TestParticle::randomTheta (double E_eV, int numCalculations, std::string outputFilePath) {
|
||||
std::ofstream outFile;
|
||||
m_eV = me_eV;
|
||||
v_ms = calcV_ms(E_eV) , 0 , 0;
|
||||
ranlib::Uniform<double> rngUni;
|
||||
rngUni.seed(2342395328);
|
||||
outFile.open (outputFilePath);
|
||||
for (int i = 0;i<numCalculations;i++) {
|
||||
outFile << getRandomTheta(&rngUni) <<std::endl;
|
||||
}
|
||||
outFile.close();
|
||||
}
|
||||
|
||||
+33
-4
@@ -1,14 +1,43 @@
|
||||
//
|
||||
// tests.hpp
|
||||
// tests.h
|
||||
// mc_electron
|
||||
//
|
||||
// Created by Christian Reiling on 17.10.18.
|
||||
// Copyright © 2018 Christian Reiling. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef tests_hpp
|
||||
#define tests_hpp
|
||||
#ifndef tests_h
|
||||
#define tests_h
|
||||
|
||||
#include <stdio.h>
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user