This commit is contained in:
2019-03-11 14:05:01 +01:00
36 changed files with 16162 additions and 1069 deletions
+1 -2
View File
@@ -6,5 +6,4 @@ include $(HOMEBASE)/src/makeinclude/ostype.mak
@test -d $(X_OSTYPE) || mkdir $(X_OSTYPE)
@cd $(X_OSTYPE); $(MAKE) -f../makefile2 $@
all:
all:
+93
View File
@@ -0,0 +1,93 @@
//
// bedionization.cpp
// mc_electron
//
// Created by Christian Reiling on 22.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include "bedionization.hpp"
#include "myexeption.h"
#include "constants.hpp"
#include <sstream>
#include <cmath>
using namespace std;
const double BED_Ionization::S_m2 = 5.4721e-20;
const double BED_Ionization::B_eV = Constants().EionT2_eV;
const double BED_Ionization::u = 1.664;
const double BED_Ionization::N = 2;
const double BED_Ionization::Ni = 1.173;
const double BED_Ionization::c[4] = {1.1262 , 6.3982 , -7.8055, 2.1449} ;
double BED_Ionization::get_TCS(double T_eV) {
if (T_eV<=0) {
stringstream ss;
ss<<"get_TCS(double T_eV): Input energy T_eV="<<T_eV<<" eV < 0.";
throw MyException(ss.str());
}
if (T_eV<=B_eV) {
return (0);
} else {
double t = calc_t(T_eV);
double fac = S_m2/(t+u+1);
double sum1 = calc_Dt(t)*log(t);
double sum2 = (2-Ni/N)*( (t-1)/t - log(t)/(t+1) );
return(fac*(sum1 + sum2) );
}
}
double BED_Ionization::get_SDCS (double T_eV, double W_eV) {
if (T_eV<0) {
stringstream ss;
ss<<"getSDCS(double T_eV, double W_eV): Input energy T_eV="<<T_eV<<" eV < 0.";
throw MyException(ss.str());
} else if (W_eV<0) {
stringstream ss;
ss<<"getSDCS(double T_eV, double W_eV): Input energy W_eV="<<W_eV<<" eV < 0.";
throw MyException(ss.str());
}
if ( (T_eV<=B_eV) ||(W_eV >= (T_eV-B_eV) ) ) {
return (0);
} else {
double t = calc_t(T_eV);
double w = calc_w(W_eV);
double fac = S_m2/B_eV/(t+u+1);
double sum1 = (Ni/N - 2)/(t+1) * (1/(w+1) + 1/(t-w));
double sum2 = (2-Ni/N) * (pow(w+1,-2) + pow(t-w,-2));
double sum3 = log(t)/(N*(w+1)) * calc_dfdw(w);
return(fac*(sum1 + sum2 + sum3));
}
}
double BED_Ionization::calc_t(double T_eV) {
return(T_eV/B_eV);
}
double BED_Ionization::calc_w(double W_eV) {
return(W_eV/B_eV);
}
double BED_Ionization::calc_dfdw(double w) {
double y = 1/(w+1);
double res = 0;
for (int i=0;i<4;i++) {
res += c[i]*pow(y,i+3);
}
return(res);
}
double BED_Ionization::calc_Dt(double t) {
double yh = 2/(t+1);
double res = 0;
for (int i=0;i<4;i++) {
res += c[i]/(i+3)*(1-pow(yh,i+3));
}
res = res/N;
return(res);
}
+38
View File
@@ -0,0 +1,38 @@
//
// bedionization.hpp
// mc_electron
//
// Created by Christian Reiling on 22.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef bedionization_hpp
#define bedionization_hpp
// BED Ionization model from Kim, Y.-K. and Rudd, M. E. 1994
class BED_Ionization {
public:
BED_Ionization(){};
double get_TCS(double T_eV);
double get_SDCS(double T_eV, double W_eV);
protected:
static const double S_m2;
private:
static const double B_eV;
static const double u;
static const double N,Ni;
static const double c[4];
double calc_t(double T);
double calc_w(double W);
double calc_dfdw(double w);
double calc_Dt(double t);
} ;
#endif /* bedionization_hpp */
File diff suppressed because it is too large Load Diff
+21 -143
View File
@@ -2,152 +2,30 @@
// constants.cpp
// mc_electron
//
// Created by Christian Reiling on 06.11.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
// Created by Christian Reiling on 09.02.19.
// Copyright © 2019 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;
const double Constants::rBohr_m = 0.52917721067e-10;
const double Constants::ERyd_eV = 13.605693009;
#include "constants.hpp"
// Natural Constants
constant Constants::pi = 3.1415;
constant Constants::c_ms = 299792458;
constant Constants::kb_eVK = 8.6173303e-5;
constant Constants::e_C = 1.602176462e-19;
constant Constants::rBohr_m = 0.52917721067e-10;
constant Constants::ERyd_eV = 13.605693009;
// Particle Properties
constant Constants::me_eV = 0.511e6;
constant Constants::re_m = 2.817940322719e-15;
constant Constants::ZT2 = 2.;
constant Constants::mT2_eV = 5.61886404234e9;
constant Constants::EionT2_eV = 15.43;
// Beta Decay
const double Constants::BetaDecayC = 1.2072343416696292e-47;
const double Constants::ZHe3 = 2.;
const double Constants::Etridecay_eV = 18.574e3;
constant Constants::BetaDecayConst = 1.2072343416696292e-47;
constant Constants::TritiumDecayEndpoint_eV = 18.574e3;
// Tritium
const double Constants::ZT2 = 2.;
const double Constants::mT2_eV = 5.61886404234e9;
// Conversation
double Constants::calcAbs(blitz::TinyVector<double,3> 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<double,3> Constants::zeroVec() {
blitz::TinyVector<double,3> zeroVec;
zeroVec = 0,0,0;
return(zeroVec);
}
blitz::TinyVector<double,3> Constants::getRandDir(ranlib::Uniform<double> *rngUni) {
blitz::TinyVector<double,3> 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);
}
blitz::TinyVector<double,3> Constants::getNewDir(blitz::TinyVector<double,3> oldDir, 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(oldDir[2]/calcAbs(oldDir));
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.
int digits = 6;
double fac = pow(10,digits);
phi1 = -1;
phi1c[0] = acos( roundf ( A11/sin(theta1) *fac)/fac );
phi1c[1] = 2*pi - phi1c[0];
phi1c[2] = asin ( roundf ( A21/sin(theta1) *fac)/fac );
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) {
for (int j = 0;j<3;j++) {
cout<<"phi1c["<<j<<"]="<<phi1c[j]<<endl;
}
std::cerr<<"Interaction::getElaNewDir: 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);
}
// Random Generators
double Constants::getRandEnergyMaxwell(double Temp_K,double mass_eV,ranlib::NormalUnit<double> *rngNormal) {
double C, beta, N1,N2,N3;
C = kb_eVK*Temp_K/mass_eV;
beta = 0;
N1 = rngNormal->random();
N2 = rngNormal->random();
N3 = rngNormal->random();
beta = sqrt(C*(N1*N1 + N2*N2 + N3*N3));
return((calcGammaFromBeta(beta)-1)*mass_eV);
}
double Constants::doRejectionMethod(double (*pdf)(double), double xmin, double xmax, double pmax, ranlib::Uniform<double> *rngUni) {
// get Energy distribution using Rejection Method
double xcan,eta1,eta2;
do {
eta1 = rngUni->random();
eta2 = rngUni->random();
xcan = xmin + eta1*(xmax - xmin);
} while( eta2*pmax > pdf(xcan) );
return(xcan);
}
-73
View File
@@ -1,73 +0,0 @@
//
// 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
#include <stdio.h>
#include <math.h>
#include <deque>
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "blitz/array.h"
#include "blitz/tinyvec2.h"
#include <random/uniform.h>
#include <random/normal.h>
using namespace std;
class Constants
{
public:
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;
static const double rBohr_m;
static const double ERyd_eV;
// Beta Decay
static const double BetaDecayC;
static const double ZHe3;
static const double Etridecay_eV;
// Tritium
static const double ZT2;
static const double mT2_eV;
// Conversion Functions
double calcAbs(blitz::TinyVector<double,3> 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<double,3> zeroVec();
blitz::TinyVector<double,3> getRandDir(ranlib::Uniform<double> *rngUni);
blitz::TinyVector<double,3> getNewDir(blitz::TinyVector<double,3> oldDir, double theta,double phi);
// Random Generators
double getRandEnergyMaxwell(double Temp_K,double mass_eV,ranlib::NormalUnit<double> *rngNormal);
double doRejectionMethod(double pdf(double), double xmin, double xmax, double pmax, ranlib::Uniform<double> *rngUni);
};
#endif
+41
View File
@@ -0,0 +1,41 @@
//
// constants.hpp
// mc_electron
//
// Created by Christian Reiling on 08.10.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef constants_hpp
#define constants_hpp
typedef const double constant;
class Constants {
public:
Constants() {};
// Natural Constants
static constant pi;
static constant c_ms;
static constant kb_eVK;
static constant e_C;
static constant rBohr_m;
static constant ERyd_eV;
// Particle Properties
static constant me_eV;
static constant re_m;
static constant ZT2;
static constant mT2_eV;
static constant EionT2_eV;
// Beta Decay
static constant BetaDecayConst;
static constant TritiumDecayEndpoint_eV;
};
#endif /* constants_hpp */
-9
View File
@@ -1,9 +0,0 @@
//
// elastic.cpp
// mc_electron
//
// Created by Christian Reiling on 15.01.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include <stdio.h>
+209
View File
@@ -7,3 +7,212 @@
//
#include "electron.hpp"
#include "myexeption.h"
using namespace std;
double Electron::calc_SigmaTotal_m2(vec3 speedT2_ms) {
vector<double> Sigmas = calc_SigmaVector_m2(speedT2_ms);
double sigmaTot_m2 = 0;
for (vector<double>::iterator SigmasIt = Sigmas.begin() ; SigmasIt != Sigmas.end(); SigmasIt++) {
sigmaTot_m2 += *SigmasIt;
}
return(sigmaTot_m2);
}
vector<double> Electron::calc_SigmaVector_m2(vec3 speedT2_ms) {
vector<double> Sigmas;
if (Particle::elaOn == 1) {
Sigmas.push_back(calc_elaSigma_m2(speedT2_ms));
} else {
Sigmas.push_back(0.);
}
if (Particle::ionOn == 1) {
Sigmas.push_back(get_ionSigma_m2(speedT2_ms));
} else {
Sigmas.push_back(0.);
}
return(Sigmas);
}
int Electron::roll_Interaction(vec3 speedT2_ms, ranlib::Uniform<double> *rngUni) {
// Check Moved Outside
if (getKinEnergy_eV()==0) {
return(0);
}
vector<double> Sigmas = calc_SigmaVector_m2(speedT2_ms);
double RandPoint = rngUni->random() * calc_SigmaTotal_m2(speedT2_ms);
double Sum = 0;
for (int i=0; i<Sigmas.size();i++ ) {
Sum += Sigmas[i];
if (RandPoint<Sum) {
return(i+1);
}
}
throw MyException("Interaction::roll_Interaction: Error in finding Interaction.");
}
/*
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
*/
void Electron::do_elaScattering (vec3 speedT2_ms, ranlib::Uniform<double> *rngUni) {
// Here theta and phi are COM-Angles.... prob not correct. For m1 << m2 COM - Angle ~ Lab Angle
double thetaLab = roll_elaTheta(speedT2_ms, rngUni);
double phiLab = rngUni->random() * 2 * pi;
do_elaKinetics(speedT2_ms,thetaLab, phiLab);
}
double Electron::calc_elaSigma_m2(vec3 speedT2_ms) {
double SigmaEla_m2;
vec3 speedLab_ms = v_ms - speedT2_ms;
double betaLab = calc_vecAbs(speedLab_ms)/c_ms;
//double EkinLab_eV = calc_Ekin_eV_from_speed(speedLab_ms, m_eV);
double elaEta = calc_elaEta(speedLab_ms);
double numerator = pi*ZT2*(ZT2+1)*re_m*re_m * (1-pow(betaLab,2));
double denominator = elaEta * (elaEta+1)*pow(betaLab,4);
SigmaEla_m2 = numerator/denominator;
return ( SigmaEla_m2 );
}
double Electron::calc_elaEta(vec3 speedLab_ms) {
double Ekin_eVLab_eV = calc_Ekin_eV_from_speed(calc_vecAbs(speedLab_ms), m_eV);
double betaLab = calc_vecAbs(speedLab_ms)/c_ms;
double etac = (Ekin_eVLab_eV < 50e3)? 1.198 : 1.13+3.76*pow(ZT2/(137*betaLab),2);
double elaTau = Ekin_eVLab_eV/m_eV;
return (etac * 1.7e-5 * pow(ZT2,2./3.) /(elaTau*(elaTau+2) ));
}
double Electron::roll_elaTheta(vec3 speedT2_ms, ranlib::Uniform<double> *rngUni) {
vec3 speedLab_ms = v_ms - speedT2_ms;
double betaLab = calc_vecAbs(speedLab_ms)/c_ms;
double elaEta = calc_elaEta(speedLab_ms);
double const1 = 2*pi/calc_elaSigma_m2(speedT2_ms)*ZT2*(ZT2+1)*re_m*re_m*(1- pow(betaLab,2) )* pow(betaLab,-4);
double theta = acos( pow( rngUni->random()/const1-pow(2*elaEta,-1) , -1 ) +1+2*elaEta);
return(theta);
}
void Electron::do_elaKinetics(vec3 speedT2_ms, double thetaLab, double phiLab) {
// Following Landau Lifschitz I Chapter 4 Paragraph 17
vec3 n0, speedLab_ms, v1New_ms, v2New_ms;
speedLab_ms = v_ms - speedT2_ms;
n0 = calc_Newdirection(speedLab_ms, thetaLab, phiLab);
v1New_ms = (mT2_eV*calc_vecAbs(speedLab_ms)*n0 + m_eV*speedLab_ms)/(m_eV+mT2_eV) + speedT2_ms;
//v2New_ms = (-1*mass_eV*calc_vecAbs(speedLab_ms)*n0 + mass_eV*speedLab_ms)/(mass_eV+mT2_eV()) + speedT2_ms;
v_ms = v1New_ms; // In "Real frame": both particles moving
return;
}
/*
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 Electron::get_ionSigma_m2(vec3 speedT2_ms) {
vec3 speedLab_ms = v_ms - speedT2_ms;
double T_eV = calc_Ekin_eV_from_speed(calc_vecAbs(speedLab_ms), m_eV);
return (BED_Ionization::get_TCS(T_eV));
}
Electron Electron::do_Ionization(vec3 speedT2_ms, int newId, ranlib::Uniform<double> *rngUni) {
vec3 speedOffset_ms = speedT2_ms;
vec3 speedLab_ms = v_ms - speedOffset_ms;
double T_eV = calc_Ekin_eV_from_speed(calc_vecAbs(speedLab_ms), m_eV);
if (T_eV < EionT2_eV) { // Deviation, since Interaction-Process sample using v=0, but Interaction uses v!=0
speedOffset_ms = 0;
speedLab_ms = v_ms - speedOffset_ms;
T_eV = calc_Ekin_eV_from_speed(calc_vecAbs(v_ms), m_eV);
}
if (T_eV < EionT2_eV) {
throw MyException("Electron::do_Ionization: Ekin_eV < EIonT2_eV.");
}
double W_eV = roll_ionW_eV(T_eV, rngUni);
double E1_eV = T_eV-EionT2_eV - W_eV;
double phi1 = rngUni->random()*2*pi;
double phi2 = phi1-pi;
double theta1 = roll_IonTheta1(T_eV, W_eV, rngUni);
double theta2 = roll_IonTheta2(T_eV, W_eV, rngUni);
double v1Absnew_ms = calc_Speed_from_Ekin_eV(E1_eV, m_eV);
double v2Absnew_ms = calc_Speed_from_Ekin_eV(W_eV, m_eV);
vec3 P1newDir, P2newDir, v1New_ms, v2New_ms ;
P1newDir = calc_Newdirection(v_ms, theta1, phi1);
P2newDir = calc_Newdirection(v_ms, theta2, phi2);
v1New_ms = v1Absnew_ms*P1newDir + speedOffset_ms;
v2New_ms = v2Absnew_ms*P2newDir + speedOffset_ms;
setSpeed_ms(v1New_ms);
Electron eNew = Electron(x_m, v2New_ms, newId, getId());
// Update Ion matrix here
return(eNew);
}
double Electron::roll_ionW_eV(double T_eV, ranlib::Uniform<double> *rngUni) {
double pmax, Wmin_eV, Wmax_eV, Wcan_eV,eta1,eta2;
//pmax = get_ionSigma_m2(); //
pmax = BED_Ionization::get_TCS(T_eV);
Wmin_eV = 0;
Wmax_eV = T_eV-EionT2_eV;
do {
eta1 = rngUni->random();
eta2 = rngUni->random();
Wcan_eV = Wmin_eV + eta1*(Wmax_eV - Wmin_eV);
} while(eta2*pmax > BED_Ionization::get_SDCS(T_eV, Wcan_eV));
double W_eV = (Wcan_eV< (T_eV-EionT2_eV)/2 ) ? Wcan_eV : (T_eV-EionT2_eV) - Wcan_eV;
return(W_eV);
}
double Electron::roll_IonTheta1(double T_eV, double W_eV, ranlib::Uniform<double> *rngUni) {
double theta1;
if (W_eV > 100) {
double numerator = W_eV/T_eV;
double denominator = (1-W_eV / T_eV)*(T_eV/(2*m_eV)) + 1;
theta1 = asin(sqrt(numerator/denominator));
} else {
theta1 = rngUni->random()*pi/4;
}
return (theta1);
}
double Electron::roll_IonTheta2(double T_eV, double W_eV, ranlib::Uniform<double> *rngUni) {
double theta2;
if (W_eV > 200) {
double numerator = 1- W_eV/T_eV;
double denominator = 1 + W_eV/(2*m_eV);
theta2 = asin(sqrt(numerator/denominator));
} else if (W_eV >= 50) {
double decider = rngUni->random();
if (decider < 0.9) {
theta2 = 1 + rngUni->random() * pi/4 + pi/4;
} else {
theta2 = rngUni->random() * pi;
}
} else {
theta2 = rngUni->random() * pi;
}
return(theta2);
}
-14
View File
@@ -1,14 +0,0 @@
//
// electron.hpp
// mc_electron
//
// Created by Christian Reiling on 15.01.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef electron_hpp
#define electron_hpp
#include <stdio.h>
#endif /* electron_hpp */
+52
View File
@@ -0,0 +1,52 @@
//
// electron.hpp
// mc_electron
//
// Created by Christian Reiling on 15.01.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef electron_hpp
#define electron_hpp
#include "particle.hpp"
#include "bedionization.hpp"
class Electron : public Particle, private BED_Ionization {
public:
virtual ~Electron() {}
Electron(vec3 loc_m, vec3 speed_ms, double idnum, double parentIdnum) : Particle::Particle(me_eV, 1 , loc_m, speed_ms, idnum, parentIdnum, electron) {};
public:
double calc_SigmaTotal_m2(vec3 speedT2_ms);
std::vector<double> calc_SigmaVector_m2(vec3 speedT2_ms);
int roll_Interaction(vec3 speedT2_ms, ranlib::Uniform<double> *rngUni);
// Elastic Scattering
public:
void do_elaScattering (vec3 speedT2_ms, ranlib::Uniform<double> *rngUni);
double calc_elaSigma_m2(vec3 speedT2_ms);
protected:
double calc_elaEta(vec3 speedLab_ms);
double roll_elaTheta(vec3 speedT2_ms, ranlib::Uniform<double> *rngUni);
void do_elaKinetics(vec3 speedT2_ms, double theta, double phi);
// Ionization
public:
double get_ionSigma_m2(vec3 speedT2_ms);
Electron do_Ionization(vec3 speedT2_ms, int newId, ranlib::Uniform<double> *rngUni);
protected:
double roll_ionW_eV(double T_eV, ranlib::Uniform<double> *rngUni);
double roll_IonTheta1(double T_eV, double W_eV, ranlib::Uniform<double> *rngUni);
double roll_IonTheta2(double T_eV, double W_eV, ranlib::Uniform<double> *rngUni);
};
#endif /* electron_hpp */
+153
View File
@@ -0,0 +1,153 @@
//
// functions.cpp
// mc_electron
//
// Created by Christian Reiling on 07.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include "functions.hpp"
#include "myexeption.h"
#include <sstream>
using namespace std;
double calc_vecAbs(vec3 vec) {
return( sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2)) );
}
double calc_Ekin_eV_from_speed(double Speed_ms, double mass_eV) {
double beta = Speed_ms/Constants::c_ms;
if (beta>=1) {
stringstream ss;
ss<<"calc_Ekin_eV_from_speed(): Input speed v="<<Speed_ms<<" m/s equal or exeeds speed of light.";
throw MyException(ss.str());
} else if (beta<0) {
stringstream ss;
ss<<"calc_Ekin_eV_from_speed(): Input speed v="<<Speed_ms<<" m/s is expected to be absolute value therfore >0.";
throw MyException(ss.str());
}
double gamma = calc_gamma_from_beta(beta);
return ( (gamma-1) * mass_eV );
}
double calc_gamma_from_beta(double beta) {
if (beta>=1) {
stringstream ss;
ss<<"calc_gamma_from_beta(): Input beta ="<<beta<<" equal or exeeds 1.";
throw MyException(ss.str());
} else if (beta<0) {
stringstream ss;
ss<<"calc_gamma_from_beta(): Input beta ="<<beta<<" negative.";
throw MyException(ss.str());
}
return( pow(1 - pow(beta,2),-0.5) ) ;
}
double calc_Speed_from_Ekin_eV(double Ekin_eV, double mass_eV) {
if (Ekin_eV<0) {
stringstream ss;
ss<<"calc_Speed_from_Ekin_eV(): Input Ekin_eV ="<<Ekin_eV<<" negative.";
throw MyException(ss.str());
}
double gamma = calc_gamma_from_Ekin_eV(Ekin_eV, mass_eV);
double beta = calc_beta_from_gamma(gamma);
return(beta * Constants::c_ms);
}
double calc_gamma_from_Ekin_eV(double Ekin_eV, double mass_eV) {
if (Ekin_eV<0) {
stringstream ss;
ss<<"calc_gamma_from_Ekin_eV(): Input Ekin_eV ="<<Ekin_eV<<" negative.";
throw MyException(ss.str());
}
return (1. + Ekin_eV/mass_eV);
}
double calc_beta_from_gamma(double gamma) {
if (gamma<1) {
stringstream ss;
ss<<"calc_beta_from_gamma(): Input gamma ="<<gamma<<" smaller 1.";
throw MyException(ss.str());
}
return( sqrt(1 - pow(gamma,-2)) );
}
vec3 calc_Newdirection(vec3 direction_old, double theta_change,double phi_change) {
// Transformation of direction following Plante and Cucinotta
double theta_old = acos(direction_old[2]/calc_vecAbs(direction_old));
double phi_old = atan2(direction_old[1],direction_old[0]);
double A3 = sin(theta_change)*cos(phi_change);
double A4 = A3*cos(theta_old) + cos(theta_change)*sin(theta_old);
double A5 = sin(theta_change)*sin(phi_change);
double A11 = A4*cos(phi_old) - A5*sin(phi_old);
double A21 = A4*sin(phi_old) + A5*cos(phi_old);
double theta_new = acos( cos(theta_old)*cos(theta_change)-A3*sin(theta_old) );
// Get right phi1, maybe more elegant way later.
int digits = 6;
double fac = pow(10,digits);
double phi_new = -1;
double phi_new_candidates [4];
phi_new_candidates[0] = acos( roundf ( A11/sin(theta_new) *fac)/fac );
phi_new_candidates[1] = 2*Constants::pi - phi_new_candidates[0];
phi_new_candidates[2] = asin ( roundf ( A21/sin(theta_new) *fac)/fac );
phi_new_candidates[3] = Constants::pi - phi_new_candidates[2];
phi_new_candidates[2] = (phi_new_candidates[2] > 0) ? phi_new_candidates[2] : 2*Constants::pi + phi_new_candidates[2];
for (int i=0;i<3;i++) {
for (int j=i+1;j<4;j++) {
if (phi_new_candidates[i]-phi_new_candidates[j] < 0.0001) {
phi_new = phi_new_candidates[i];
goto endloops;
}
}
}
endloops:
if (phi_new == -1) {
for (int j = 0;j<3;j++) {
cout<<"phi1c["<<j<<"]="<<phi_new_candidates[j]<<endl;
}
throw MyException("Interaction::getElaNewDir: phi1 not found.");
}
return(vec3(sin(theta_new)*cos(phi_new) , sin(theta_new)*sin(phi_new) , cos(theta_new)));
}
vec3 roll_uniform_direction(ranlib::Uniform<double> *rngUni) {
vec3 dir;
double phi = 2*Constants::pi*rngUni->random();
double theta = Constants::pi*rngUni->random();
dir = sin(theta)*cos(phi) , sin(theta)*sin(phi) , cos(theta);
return(dir);
}
double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit<double> *rngNormal) {
double C = Constants::kb_eVK*Temp_K/mass_eV;
double beta = 0;
double sum = 0;
for (int i=0;i<3;i++) {
sum += pow(rngNormal->random(),2);
}
beta = sqrt(C*(sum));
return((calc_gamma_from_beta(beta)-1)*mass_eV);
}
double roll_Rejectionmethod(double (*pdf)(double), double xmin, double xmax, double pmax, ranlib::Uniform<double> *rngUni) {
// get Energy distribution using Rejection Method
double xcan,eta1,eta2;
do {
eta1 = rngUni->random();
eta2 = rngUni->random();
xcan = xmin + eta1*(xmax - xmin);
} while( eta2*pmax > pdf(xcan) );
return(xcan);
}
+37
View File
@@ -0,0 +1,37 @@
//
// functions.hpp
// mc_electron
//
// Created by Christian Reiling on 07.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef functions_hpp
#define functions_hpp
#include "constants.hpp"
#include "blitz/array.h"
#include "blitz/tinyvec2.h"
#include <random/uniform.h>
#include <random/normal.h>
typedef blitz::TinyVector<double,3> vec3;
double calc_vecAbs(blitz::TinyVector<double,3> vec);
double calc_Speed_from_Ekin_eV(double Ekin_eV, double mass_eV);
double calc_gamma_from_Ekin_eV(double Ekin_eV, double mass_eV);
double calc_beta_from_gamma(double gamma);
double calc_Ekin_eV_from_speed(double Speed, double mass_eV);
double calc_gamma_from_beta(double beta);
vec3 calc_Newdirection(vec3 oldDir, double theta,double phi);
vec3 roll_uniform_direction(ranlib::Uniform<double> *rngUni);
double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit<double> *rngNormal);
double doRejectionMethod(double pdf(double), double xmin, double xmax, double pmax, ranlib::Uniform<double> *rngUni);
#endif /* functions_hpp */
-9
View File
@@ -1,9 +0,0 @@
//
// ionization.cpp
// mc_electron
//
// Created by Christian Reiling on 15.01.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include <stdio.h>
+176 -28
View File
@@ -7,50 +7,198 @@
//
#include "katrin.h"
#include "katrin.hpp"
#include "myexeption.h"
#include "functions.hpp"
#include "sstream"
using namespace std;
Katrin::Katrin() {
set_cutoffEnergy_eV();
}
blitz::TinyVector<double,3> Katrin::getMagField(blitz::TinyVector<double,3> loc_m) {
blitz::TinyVector<double,3> MagField_T;
vec3 Katrin::getMagField_T(vec3 loc_m) {
vec3 MagField_T;
MagField_T = 0,0,wgtsMagField_T;
return(MagField_T);
}
blitz::TinyVector<double,3> Katrin::getElField(blitz::TinyVector<double,3> loc_m) {
blitz::TinyVector<double,3> ElField_Vm;
ElField_Vm = 0,0,0;
return (ElField_Vm);
vec3 Katrin::getElField_Vm(vec3 loc_m) {
return (vec3(0,0,0));
}
double Katrin::getTritiumDensity(blitz::TinyVector<double,3> loc_m) {
return(wgtsT2Density_m3);
double Katrin::getTritiumDensity_m3(vec3 loc_m) {
double mid_m = wgtsLength_m/2;
double zProjection_m = (loc_m[2] < mid_m) ? loc_m[2] : wgtsLength_m - loc_m[2] ;
double reductcalc_ionFactor = zProjection_m/mid_m;
return(reductcalc_ionFactor * wgtsT2Density_m3);
}
Particle Katrin::getT2AverScatPart(blitz::TinyVector<double,3> loc_m, ranlib::Uniform<double> *rngUni) {
blitz::TinyVector<double,3> speed_ms;
// Get Linear Flow
vec3 Katrin::getTotSpeedT2_ms(vec3 loc_m, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal) {
return ( getFlowSpeedT2_ms(loc_m) + getTempSpeedT2_ms(loc_m, rngUni, rngNormal) );
}
vec3 Katrin::getFlowSpeedT2_ms(vec3 loc_m) {
vec3 speed_ms;
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, tritium2);
ScatPart.setLoc_m(loc_m);
return (ScatPart);
return (speed_ms);
}
Particle Katrin::getT2ScatPart(blitz::TinyVector<double,3> loc_m, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal) {
Particle ScatPart = getT2AverScatPart(loc_m, rngUni);
blitz::TinyVector<double,3> speed_ms, flowOffset_ms, tempOffset_ms;
// Get Linear Flow
flowOffset_ms = ScatPart.getSpeed_ms();
vec3 Katrin::getTempSpeedT2_ms(vec3 loc_m, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal) {
double tempOffsetABS_ms = calc_Speed_from_Ekin_eV(roll_MBEnergy(wgtsTemp_K,mT2_eV,rngNormal), mT2_eV);
vec3 speed_ms = roll_uniform_direction(rngUni)*tempOffsetABS_ms;
return(speed_ms);
}
Electron Katrin::injectElectron(int newId, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal) {
// Get Temperature Motion
double tempOffsetABS_ms = calcV_msFromEkin_eV(getRandEnergyMaxwell(wgtsTemp_K,mT2_eV,rngNormal), mT2_eV);
tempOffset_ms = getRandDir(rngUni)*tempOffsetABS_ms;
speed_ms = flowOffset_ms + tempOffset_ms;
ScatPart.setSpeed_ms(speed_ms);
return (ScatPart);
double r_m = wgtsRadius_m*sqrt(rngUni->random());
double phi = rngUni->random()*2*pi;
double z_0tohalf_m = sqrt(rngUni->random())*wgtsLength_m/2;
double z_m = (rngUni->random()<0.5)? z_0tohalf_m : wgtsLength_m - z_0tohalf_m;
vec3 loc_m = vec3 (r_m*cos(phi),r_m*sin(phi),z_m);
double Ekin_eV;
switch(injectionMethod) {
case 0: // Fermi
Ekin_eV = roll_FermiEnergy_eV(rngUni);
break;
case 1: //Delta
Ekin_eV = deltaKinEnergy_eV;
break;
case 2: //Maxwell Boltzman
Ekin_eV = roll_MBEnergy(MBTemp_K,me_eV, rngNormal);
break;
default:
throw MyException("Simulation::injectElectron: Invalid Injection Method.");
break;
}
vec3 speed_ms = roll_uniform_direction(rngUni) * calc_Speed_from_Ekin_eV(Ekin_eV,me_eV);
return (Electron(loc_m, speed_ms,newId,newId));
}
double Katrin::roll_FermiEnergy_eV(ranlib::Uniform<double> *rngUni) {
// get Energy distribution using Rejection Method
double pmax,Emin_eV, Emax_eV, Ecan_eV,eta1,eta2;
pmax = 1.5e-28; // From Plotting EnergyDistributionBetaDecay
Emin_eV = 1e-15;
Emax_eV = TritiumDecayEndpoint_eV;
do {
eta1 = rngUni->random();
eta2 = rngUni->random();
Ecan_eV = Emin_eV + eta1*(Emax_eV - Emin_eV);
} while(eta2*pmax > calc_FermiDecaySpectrumDensity(Ecan_eV));
return(Ecan_eV);
}
double Katrin::calc_FermiDecaySpectrumDensity(double Ekin_eV) {
if (Ekin_eV>TritiumDecayEndpoint_eV) {
return (0.);
} else{
double p_eV = sqrt(Ekin_eV*Ekin_eV + 2 * Ekin_eV * me_eV);
return( BetaDecayConst * calc_FermiFunction(2, Ekin_eV) * p_eV*(Ekin_eV+me_eV)*pow(TritiumDecayEndpoint_eV-Ekin_eV,2) );
}
}
double Katrin::calc_FermiFunction(double Z, double Ekin_eV) {
double gamma = 1+ Ekin_eV/me_eV;
double beta = sqrt(1 - pow(gamma,-2));
double eta =Z/beta/137.;
return( 2*pi*eta/(1-exp(-2*pi*eta)) );
}
// Move
void Katrin::do_BorisPush(Electron *moveElectron) {
double MFP_m = get_MFP_m(moveElectron);
vec3 oldLoc_m = moveElectron->getLoc_m();
vec3 E_Vm, B_T;
double distance = 0.;
while (distance < MFP_m) {
E_Vm = getElField_Vm(oldLoc_m);
B_T = getMagField_T(oldLoc_m);
distance += moveElectron->borisFly_m(E_Vm , B_T , dt_s);
if (movedOutside(moveElectron->getLoc_m())) {
moveElectron->setSpeed_ms(vec3(0,0,0));
return;
}
}
return;
}
void Katrin::do_DriftApproximation(Electron *moveElectron) {
// Drift Approximation: Assume constant B and only movement out in z=0, z=wgtsLength_m...
double MFP_m= get_MFP_m(moveElectron);
vec3 oldLoc_m = moveElectron->getLoc_m();
vec3 newLoc_m = oldLoc_m;
double z_m = oldLoc_m[2];
double vz_ms = moveElectron->getSpeed_ms()[2];
double vdir = vz_ms < 0 ? -1 : 1;
double znew_m = z_m + vdir*MFP_m;
if (znew_m < 0) {
znew_m = 0;
} else if (znew_m > wgtsLength_m) {
znew_m = wgtsLength_m;
}
newLoc_m[2] = znew_m;
moveElectron->setLoc_m(newLoc_m);
if (movedOutside(newLoc_m)) {
moveElectron->setSpeed_ms(vec3(0,0,0));
return;
}
return;
}
double Katrin::get_MFP_m(Electron *moveElectron) {
//Rework for inserting vector of Interactions
vec3 loc_m, speedT2_ms;
double sigmaTot_m2, density_m3, MFP_m;
loc_m = moveElectron->getLoc_m();
speedT2_ms = getFlowSpeedT2_ms(loc_m);
sigmaTot_m2 = moveElectron->calc_SigmaTotal_m2(speedT2_ms);
density_m3 = getTritiumDensity_m3(loc_m);
MFP_m = 1/(density_m3 * sigmaTot_m2);
return(MFP_m);
}
bool Katrin::movedOutside(vec3 loc_m) {
double r_m = sqrt( pow(loc_m(0),2) + pow(loc_m(1),2) );
return ( ! (loc_m[2]>0 && loc_m[2]<wgtsLength_m && r_m<wgtsRadius_m ) );
}
// Termination
bool Katrin::get_Terminated(Electron termElectron) {
double Ekin_eV, ETemp_eV;
Ekin_eV = termElectron.getKinEnergy_eV();
ETemp_eV = kb_eVK*wgtsTemp_K;
if (ETemp_eV<0) {
throw MyException("Simulation::checkTermination: ETemp_eV < 0.");
}
if (Ekin_eV<=cutoffEnergy_eV) {
return(true);
}
return(false);
}
void Katrin::set_cutoffEnergy_eV() {
if (cutoffMode==0) {
cutoffEnergy_eV = cutoffEnergySet_eV;
} else if (cutoffMode==1) {
cutoffEnergy_eV = kb_eVK*wgtsTemp_K*cutoffFactor;
} else {
stringstream ss;
ss<<"Mover::Mover(): cutoffMode= "<<cutoffMode<<" not defined.";
throw MyException(ss.str());
}
}
-31
View File
@@ -1,31 +0,0 @@
//
// katrin.h
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef katrin_h
#define katrin_h
#include "../base/parameter.h"
#include "../particle/particle.h"
class Katrin: public Parameter
{
public:
Katrin(){};
virtual ~Katrin(){};
blitz::TinyVector<double,3> getMagField(blitz::TinyVector<double,3> loc_m);
blitz::TinyVector<double,3> getElField(blitz::TinyVector<double,3> loc_m);
double getTritiumDensity(blitz::TinyVector<double,3> loc_m);
Particle getT2AverScatPart(blitz::TinyVector<double,3> loc_m, ranlib::Uniform<double> *rngUni);
Particle getT2ScatPart(blitz::TinyVector<double,3> loc_m, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal);
};
#endif
+56
View File
@@ -0,0 +1,56 @@
//
// katrin.hpp
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef katrin_hpp
#define katrin_hpp
#include "parameter.hpp"
#include "constants.hpp"
#include "electron.hpp"
#include "blitz/tinyvec2.h"
#include "blitz/array.h"
#include <random/uniform.h>
#include <random/normal.h>
class Katrin: private Parameter, private Constants {
public:
Katrin();
vec3 getMagField_T(vec3 loc_m);
vec3 getElField_Vm(vec3 loc_m);
double getTritiumDensity_m3(vec3 loc_m);
vec3 getTotSpeedT2_ms(vec3 loc_m, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal);
vec3 getFlowSpeedT2_ms(vec3 loc_m);
vec3 getTempSpeedT2_ms(vec3 loc_m, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal);
// Injection
Electron injectElectron(int newId, ranlib::Uniform<double> *rngUni, ranlib::NormalUnit<double> *rngNormal);
private:
// Injection
double roll_FermiEnergy_eV(ranlib::Uniform<double> *rngUni);
double calc_FermiDecaySpectrumDensity(double Ekin_eV);
double calc_FermiFunction(double Z, double Ekin_eV);
// Move Electrons
public:
void do_BorisPush(Electron *moveElectron);
void do_DriftApproximation(Electron *moveElectron);
bool movedOutside(blitz::TinyVector<double,3> loc_m);
// Terminate Electrons
bool get_Terminated(Electron termElectron);
private:
double get_MFP_m(Electron *moveElectron);
double cutoffEnergy_eV;
void set_cutoffEnergy_eV();
};
#endif /* katrin_hpp */
+120
View File
@@ -0,0 +1,120 @@
//
// logger.cpp
// mc_electron
//
// Created by Christian Reiling on 27.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include "logger.hpp"
#include "myexeption.h"
#include <fstream>
#include <sys/stat.h>
using namespace std;
Logger::Logger() {
if (numberZBins == 0) {
cout<<"Logging turned off."<<endl;
logging = false;
return;
}
set_zLogValues();
set_outStream();
}
void Logger::logElectron(Electron *logElectron, blitz::TinyVector<double,3> oldLoc_m) {
if (!logging) {
return;
}
// Assumption: no change of sign in v_z
if (numberZBins == -1) {
outStream[0]<<*logElectron<<endl;
} else {
double zold = oldLoc_m[2];
double znew = logElectron->getLoc_m()[2];
double zmin = min(zold,znew);
double zmax = max(zold,znew);
for (int i=0;i<numberZBins;i++) {
if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) {
outStream[i]<<*logElectron<<endl; // Location wrong, because it s the new Location.
}
}
}
}
void Logger::set_zLogValues() {
zLogValues.clear();
logging = true;
if (numberZBins == -1) {
cout<<"Logging everything to one file."<<endl;
} else if (numberZBins == 1) {
cout<<"Logging 1 z-Value: "<<wgtsLength_m<<endl;
zLogValues.push_back(wgtsLength_m);
} else {
for (int i = 0;i<numberZBins;i++) {
zLogValues.push_back( i*wgtsLength_m/(numberZBins-1) );
}
cout<<"Logging "<<numberZBins<<" z-Values."<<endl;
}
return;
}
void Logger::set_outStream() {
string dirname = getDirName();
if (numberZBins == -1) {
outStream = new ofstream [1];
stringstream ss;
ss <<dirname << "/all" << ".dat";
outStream[0].open(ss.str().c_str());
} else {
outStream = new ofstream [numberZBins];
for (int i = 0; i < numberZBins; i++)
{
stringstream ss;
ss << dirname << "/z=" << zLogValues[i] << ".dat";
outStream[i].open(ss.str().c_str());
}
}
}
string Logger::getDirName() {
string poub = getenv("parpath");
string pname = getenv("parfile");
int i = 1;
stringstream dirname;
struct stat statbuf;
int isDir;
do {
dirname.str(std::string());
dirname << poub << "/" << pname << "_" << getTimeString() << "_" << i++;
isDir = 0;
if (stat(dirname.str().c_str(), &statbuf) != -1) {
if (S_ISDIR(statbuf.st_mode)) {
isDir = 1;
}
}
} while ( isDir == 1 );
if (mkdir(dirname.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
stringstream ss;
ss<<"Simulation::initLogger: Could not create folder "<<dirname.str()<<" for Logging.";
throw MyException(ss.str());
}
return(dirname.str());
}
string Logger::getTimeString() {
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
string str(buffer);
return(str);
}
+33
View File
@@ -0,0 +1,33 @@
//
// logger.hpp
// mc_electron
//
// Created by Christian Reiling on 27.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef logger_hpp
#define logger_hpp
#include "parameter.hpp"
#include "electron.hpp"
class Logger : private Parameter {
public:
Logger();
void logElectron(Electron *logElectron,blitz::TinyVector<double,3> oldLoc_m);
private:
bool logging;
std::vector<double> zLogValues;
std::ofstream *outStream;
void set_zLogValues();
void set_outStream();
std::string getDirName();
std::string getTimeString();
};
#endif /* logger_hpp */
+9 -14
View File
@@ -6,31 +6,26 @@
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#include "simulation.h"
#include "tests.h"
#ifdef DEBUG
#define Debug( x ) cout << x << endl;
#else
#define Debug( x )
#endif
#include "simulation.hpp"
#include "myexeption.h"
using namespace std;
int main(int argc, const char * argv[]) {
Debug("Debug Mode is on.")
int main(int argc, const char * argv[]) {
//runTests();
clock_t start;
double duration;
start = clock();
Simulation Sim;
Sim.initLogger();
Sim.doit();
try {
Simulation Sim;
Sim.doit();
} catch (MyException& caught) {
cout<<caught.what()<<endl;
}
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
cout<<"Duration: "<< duration <<'\n';
+1 -1
View File
@@ -1 +1 @@
include $(HOMEBASE)/src/makeinclude/first.mak
include $(HOMEBASE)/src/makeinclude/first.mak
+8 -3
View File
@@ -1,6 +1,10 @@
include $(HOMEBASE)/src/makeinclude/include.mak
CXXFLAGS = $(X_OFLAGS) -I.. $(X_INC)
SRCS := $(wildcard ../*.cpp)
OBJS := $(patsubst %.cpp,%.o,$(SRCS))
CXXFLAGS = $(X_OFLAGS) -I.. $(X_INC)
LDFLAGS = $(X_LIB)
@@ -10,11 +14,12 @@ TARGETS = mcelectron
all: $(TARGETS)
mcelectron: main.o katrin.o simulation.o particle.o interaction.o tests.o constants.o
mcelectron: main.o parameter.o functions.o constants.o katrin.o logger.o simulation.o \
particle.o electron.o elastic.o ionization.o bedionization.o
$(CXX) -o $@ $^ $(LDFLAGS) $(LOADLIBES)
%.o : %.cpp
$(CXX) -Wno-write-strings -c $< $(CXXFLAGS) -o $@
$(CXX) -c $< $(CXXFLAGS) -o $@
clean:
rm *.o mcelectron
+24
View File
@@ -0,0 +1,24 @@
//
// myexeption.h
// mc_electron
//
// Created by Christian Reiling on 01.03.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef myexeption_h
#define myexeption_h
#include <iostream>
using namespace std;
struct MyException : public exception
{
string s;
MyException(string ss) : s(ss) {}
~MyException() throw () {} // Updated
const char* what() const throw() { return s.c_str(); }
};
#endif /* myexeption_h */
+102
View File
@@ -0,0 +1,102 @@
//
// parameter.cpp
// mc_electron
//
// Created by Christian Reiling on 26.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include "parameter.hpp"
#include "myexeption.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
Parameter::Parameter() {
// Simulation Specs
seed = (int)getValue("seed");
numTotalElectrons = (long int)getValue("numTotalElectrons");
dt_s = getValue("dt_s");
injectionMethod = (int)getValue("injectionMethod");
deltaKinEnergy_eV = getValue("deltaKinEnergy_eV");
MBTemp_K = getValue("MBTemp_K");
elaOn = (int)getValue("elaOn");
ionOn = (int)getValue("ionOn");
propagationMethod = (int)getValue("propagationMethod");
// Cutoff
cutoffMode = (int)getValue("cutoffMode");
cutoffEnergySet_eV = getValue("cutoffEnergySet_eV");
cutoffFactor = getValue("cutoffFactor");
// Katrin Specs
wgtsMagField_T = getValue("wgtsMagField_T");
wgtsLength_m = getValue("wgtsLength_m");
wgtsRadius_m = getValue("wgtsRadius_m");
wgtsT2Density_m3 = getValue("wgtsT2Density_m3");
wgtsTemp_K = getValue("wgtsTemp_K");
wgtsT2FlowSpeed_ms = getValue("wgtsT2FlowSpeed_ms");
// Output
numberZBins = getValue ("numberZBins");
}
double Parameter::getValue(string varName) {
char* ext = (char*)("cat");
char* str = strdup(varName.c_str());
double temp;
char dummy[256];
char fname[256],*parfile;
FILE *fcat;
if (getenv("parpath") == NULL)
{
throw MyException("Parameter::value: Environment variable parpath not defined.");
}
strcpy(fname, getenv("parpath"));
parfile = getenv("parfile");
if (parfile == NULL)
{
throw MyException("Parameter::value: Environment variable parfile not defined.");
}
if (!ext)
{
ext = (char*)("cat");
}
sprintf(fname,"%s/%s.%s",getenv("parpath"),getenv("parfile"), ext);
if (!(fcat = fopen(fname, "r")))
{
stringstream ss;
ss<<"Parameter::value: Can not open file " <<fname<<".";
throw MyException(ss.str());
}
while (!feof(fcat))
{
// fscanf(fcat, "'%s%*[^']'%F%*[^']",dummy,&temp); 1
fscanf(fcat, "%s = %lf", dummy, &temp);
// if (strncmp(dummy, str,strlen(str)) == 0) {
if (strcmp(dummy, str) == 0)
{
fclose(fcat);
return(temp);
}
}
fclose(fcat);
stringstream ss;
ss<<"Parameter::value: Variable "<<str<<" not found.";
throw MyException(ss.str());
return 0;
}
-111
View File
@@ -1,111 +0,0 @@
//
// parameter.h
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef parameter_h
#define parameter_h
#include "constants.h"
class Parameter: protected Constants
{
public:
virtual ~Parameter(){};
Parameter() {
// Simulation Specs
seed = (int)value("seed");
numTotalParticles = (long int)value("numTotalParticles");
dt_s = value("dt_s");
injectionMethod = (int)value("injectionMethod");
deltaKinEnergy_eV = value("deltaKinEnergy_eV");
MBTemp_K = value("MBTemp_K");
// Katrin Specs
wgtsMagField_T = value("wgtsMagField_T");
wgtsLength_m = value("wgtsLength_m");
wgtsRadius_m = value("wgtsRadius_m");
wgtsT2Density_m3 = value("wgtsT2Density_m3");
wgtsTemp_K = value("wgtsTemp_K");
wgtsT2FlowSpeed_ms = value("wgtsT2FlowSpeed_ms");
// Output
numberZBins = value ("numberZBins");
}
private:
double value(string strin, char* ext = (char*)("cat")) {
char* str = strdup(strin.c_str());
double temp;
char dummy[256];
char fname[256],*pname;
FILE *fcat;
if (getenv("poub") == NULL)
{
cerr<<"Parameter::value: Environment variable poub not defined.\n";
exit(1);
}
strcpy(fname, getenv("poub"));
pname = getenv("pname");
if (pname == NULL)
{
cerr<<"Parameter::value: Environment variable pname not defined.\n";
exit(1);
}
if (!ext)
{
ext = (char*)("cat");
}
sprintf(fname,"%s/%s.%s",getenv("poub"),getenv("pname"), ext);
if (!(fcat = fopen(fname, "r")))
{
cerr<<"Parameter::value: Can not open file " <<fname<<".\n";
exit(1);
}
while (!feof(fcat))
{
// fscanf(fcat, "'%s%*[^']'%F%*[^']",dummy,&temp); 1
fscanf(fcat, "%s = %lf", dummy, &temp);
// if (strncmp(dummy, str,strlen(str)) == 0) {
if (strcmp(dummy, str) == 0)
{
fclose(fcat);
return(temp);
}
}
fclose(fcat);
cerr<<"Parameter::value: Variable "<<str<<" not found.\n";
exit(1);
return 0;
}
protected:
//Simulation Specs
int seed;
long int numTotalParticles;
double dt_s;
int injectionMethod;
double deltaKinEnergy_eV;
double MBTemp_K;
//KATRIN Specs
double wgtsMagField_T;
double wgtsLength_m;
double wgtsRadius_m;
double wgtsT2Density_m3;
double wgtsTemp_K;
double wgtsT2FlowSpeed_ms;
// Output
int numberZBins;
};
#endif
+51
View File
@@ -0,0 +1,51 @@
//
// parameter.hpp
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef parameter_hpp
#define parameter_hpp
#include <string>
class Parameter {
public:
Parameter();
protected:
//Simulation Specs
int seed;
long int numTotalElectrons;
double dt_s;
int injectionMethod; // 0 = Beta Decay, 1 = Delta, 2 = Maxwell Boltzmann
double deltaKinEnergy_eV;
double MBTemp_K;
int elaOn;
int ionOn;
int propagationMethod; // 0 = Drift Approximation, 1 = Borispush
// Cutoff
int cutoffMode; // 0 = Energy from Parameter, 1 = WGTSTemperature*cutoffFactor
double cutoffEnergySet_eV;
double cutoffFactor;
//KATRIN Specs
double wgtsMagField_T;
double wgtsLength_m;
double wgtsRadius_m;
double wgtsT2Density_m3;
double wgtsTemp_K;
double wgtsT2FlowSpeed_ms;
// Output
int numberZBins;
private:
double getValue(std::string varName);
};
#endif
-15
View File
@@ -1,15 +0,0 @@
seed = 2342395328
numTotalParticles = 1000
dt_s = 0.0000000001
injectionMethod = 1
deltaKinEnergy_eV = 1
MBTemp_K = 100
numberZBins = 51
wgtsMagField_T = 2.5
wgtsLength_m = 10
wgtsRadius_m = 0.045
wgtsT2Density_m3 = 5e21
wgtsTemp_K = 1.16e2
wgtsT2FlowSpeed_ms = 0
+9 -28
View File
@@ -7,7 +7,9 @@
//
#include "particle.h"
#include "particle.hpp"
using namespace std;
// Overload <<
@@ -41,11 +43,11 @@ ostream& operator<<(ostream& os, const species& SpecOut) {
os<<"DT";
break;
default:
cerr<<"unknown species";
exit(1);
os<<"";
}
return (os);
}
ostream& operator<<(ostream& os, const Particle& partOut) {
os
<< partOut.Id << ";"
@@ -58,26 +60,11 @@ ostream& operator<<(ostream& os, const Particle& partOut) {
<< partOut.x_m[2] << ";"
<< partOut.v_ms[0] << ";"
<< partOut.v_ms[1] << ";"
<< partOut.v_ms[2] << ";";
<< partOut.v_ms[2] ;
return os;
}
// Default Constructor
Particle::Particle() {
x_m = zeroVec();
v_ms = zeroVec();
s = undef;
}
Particle::Particle(double mass_eV, double charge_e, blitz::TinyVector<double,3> speed_ms, species spec) {
m_eV = mass_eV;
Z = charge_e;
x_m = zeroVec();
v_ms = speed_ms;
s = spec;
}
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) {
Particle::Particle(double mass_eV, double charge_e,vec3 loc_m, vec3 speed_ms, double idnum, double parentIdnum, species spec) {
m_eV = mass_eV;
Z = charge_e;
x_m = loc_m;
@@ -90,10 +77,10 @@ Particle::Particle(double mass_eV, double charge_e,blitz::TinyVector<double,3> l
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;
distance_m = dt_s/2 * getvAbs_ms();
distance_m = dt_s/2 * getSpeedAbs_ms();
this->changeSpeedBoris(E_Vm , B_T , dt_s);
x_m += dt_s/2 * v_ms;
distance_m += dt_s/2 * getvAbs_ms();
distance_m += dt_s/2 * getSpeedAbs_ms();
return(distance_m);
}
@@ -109,9 +96,3 @@ void Particle::changeSpeedBoris(blitz::TinyVector<double, 3> E_Vm , blitz::TinyV
vPlus_ms = vMinus_ms + cross(vPrime_ms,s);
v_ms = vPlus_ms + q_C/m_kg*E_Vm*0.5*dt_s;
}
void Particle::loseEkin_eV(double Eloss_eV) {
double eNew_eV = getKinEnergy_eV() - Eloss_eV;
double vAbsNew_ms = calcV_msFromEkin_eV(eNew_eV, m_eV);
v_ms = v_ms / getvAbs_ms() * vAbsNew_ms;
}
-80
View File
@@ -1,80 +0,0 @@
//
// particle.h
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef particle_h
#define particle_h
#include "constants.h"
typedef enum species{undef, electron, positron, proton, tritium, tritium2, helium, deuterium, DT}species;
ostream& operator<<(ostream& os, const species& SpecOut);
class Particle: protected Constants {
public:
virtual ~Particle() {}
Particle();
Particle(double mass_eV, double charge_e, blitz::TinyVector<double,3> speed_ms, species spec);
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);
protected:
double m_eV;
double Z;
blitz::TinyVector<double,3> x_m;
blitz::TinyVector<double,3> v_ms;
species s;
unsigned long int Id;
unsigned long int parentId;
public:
// Get Properties Methods
double getMass_eV() { return(m_eV); }
double getCharge_e() { return(Z); }
blitz::TinyVector<double,3> getLoc_m() { return(x_m); }
blitz::TinyVector<double,3> getSpeed_ms() { return(v_ms); }
species getSpecies() {return(s); }
double getId() { return(Id); }
double getParentId() { return(parentId); }
// 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<double, 3> xnew_m) {x_m = xnew_m; };
void setSpeed_ms(blitz::TinyVector<double, 3> vnew_ms) {v_ms = vnew_ms; };
/*
Boris Push:
Propagate Flight for one timestep dt_s in E_Vm and B_T and returns distance.
*/
public:
double borisFly_m(blitz::TinyVector<double, 3> E_Vm , blitz::TinyVector<double, 3> B_T , double dt_s);
protected:
void changeSpeedBoris(blitz::TinyVector<double, 3> E_Vm,blitz::TinyVector<double, 3> B_T , double dt_s);
/*
Interactions
*/
public:
void loseEkin_eV(double Eloss_eV);
// Write Methods
public:
friend ostream& operator<<(ostream& os, const Particle& partOut);
};
#endif
+69
View File
@@ -0,0 +1,69 @@
//
// particle.hpp
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef particle_hpp
#define particle_hpp
#include "parameter.hpp"
#include "constants.hpp"
#include "functions.hpp"
#include "iostream"
#include "blitz/tinyvec2.h"
#include "blitz/array.h"
typedef blitz::TinyVector<double,3> vec3;
typedef enum species{undef, electron, positron, proton, tritium, tritium2, helium, deuterium, DT}species;
std::ostream& operator<<(std::ostream& os, const species& SpecOut);
class Particle: protected Parameter, protected Constants{
public:
Particle(double mass_eV, double charge_e,vec3 loc_m, vec3 speed_ms, double idnum, double parentIdnum, species spec);
protected:
double m_eV;
double Z;
vec3 x_m;
vec3 v_ms;
species s;
unsigned long int Id;
unsigned long int parentId;
public:
// Get Properties Methods
double getMass_eV() { return(m_eV); }
double getCharge_e() { return(Z); }
vec3 getLoc_m() { return(x_m); }
vec3 getSpeed_ms() { return(v_ms); }
species getSpecies() {return(s); }
double getId() { return(Id); }
double getParentId() { return(parentId); }
// Get Properties
double getRadius_m() { return ( sqrt( pow(x_m[0],2) + pow(x_m[1],2) ) ); }
double getSpeedAbs_ms() { return ( calc_vecAbs(v_ms) ); }
double getKinEnergy_eV() { return ( calc_Ekin_eV_from_speed(getSpeedAbs_ms(), m_eV) ); }
// 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: Propagate Flight for one timestep dt_s in E_Vm and B_T and returns distance.
public:
double borisFly_m(blitz::TinyVector<double, 3> E_Vm , blitz::TinyVector<double, 3> B_T , double dt_s);
protected:
void changeSpeedBoris(blitz::TinyVector<double, 3> E_Vm,blitz::TinyVector<double, 3> B_T , double dt_s);
public:
friend std::ostream& operator<<(std::ostream& os, const Particle& partOut);
};
#endif /* particle_hpp */
+116 -273
View File
@@ -6,294 +6,137 @@
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#include "simulation.h"
#include "simulation.hpp"
#include "myexeption.h"
using namespace std;
Simulation::Simulation() {
numberElectrons = 0;
rngUni.seed(seed);
updater_numberOfPrevious = 0;
}
void Simulation::doit() {
double MFP_m;
blitz::TinyVector <double,3> loc_m;
Particle ScatT2;
Interaction IntereT2;
while ( (numInjectedParticles < numTotalParticles) || (particleQue.size() > 0) ) {
if (particleQue.empty()) {
particleQue.push_back(inject());
numInjectedParticles++;
if (numInjectedParticles%100 == 0){
cout<<numInjectedParticles<<"/"<<numTotalParticles<<endl;
}
vec3 oldLoc_m;
while ( (numberElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
if (electronQue.empty()) {
electronQue.push_back(Kat.injectElectron(numberElectrons++, &rngUni, &rngNormal));
}
ScatT2 = Kat.getT2AverScatPart(particleQue.front().getLoc_m(), &rngUni);
IntereT2 = Interaction(&particleQue.front(),&ScatT2);
//Replace with vector containing all Interactions and adapt functions and put particle pointers into Particle class
MFP_m = getMFP_m(IntereT2);
moveParticleDriftApproximation(&particleQue.front(),MFP_m);
//moveParticleBorisPush(&particleQue.front(),MFP_m); // If moved outside set v = 0,0,0;
status_updater();
oldLoc_m = electronQue.front().getLoc_m();
move_electron(&electronQue.front());
Log.logElectron(&electronQue.front(), oldLoc_m);
do_interaction(&electronQue.front());
switch ( IntereT2.getInteraction(&rngUni) ) {
case 0: //Moved Outside
break;
case 1: // Elastic Scattering
ScatT2 = Kat.getT2ScatPart(particleQue.front().getLoc_m(), &rngUni, &rngNormal);
IntereT2 = Interaction(&particleQue.front(),&ScatT2);
IntereT2.doElaScat(&rngUni);
break;
case 2: // Ionization
particleQue.push_back( IntereT2.doIon(numInjectedParticles, &rngUni) );
numInjectedParticles++;
// Update Ions...
break;
case 3: // Recombination
break;
default:
cerr<<"getprocess() returned unknown event"<<endl;
break;
}
if (checkTermination (particleQue.front())) {
particleQue.pop_front();
if (Kat.get_Terminated (electronQue.front())) {
electronQue.pop_front();
}
}
};
// Injection
Particle Simulation::inject() {
blitz::TinyVector<double,3> loc_m, speed_ms;
double r_m,phi_rad,z_m;
double Ekin_eV;
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
/*
void Simulation::doTC6() {
cout<<endl<<"Simulation::doTC6: Doing Testcase 6."<<endl<<endl;
stringstream fileName;
fileName << Log.getDirName() << "/TC6all.dat";
switch(injectionMethod) {
case 0: // Fermi
Ekin_eV = getBetaDecayEnergyRejection_eV(); //Implement Later
ofstream outTC6;
outTC6.open (fileName.str().c_str());
blitz::TinyVector <double,3> loc_m;
while ( (numberElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
if (!electronQue.empty()) {
throw MyException("Simulation::doTC6: electronQue should be empty");
}
electronQue.push_back(Kat.injectElectron(numberElectrons, &rngUni, &rngNormal));
numberElectrons++;
if (numberElectrons%100 == 0){
cout<<numberElectrons<<"/"<<numTotalElectrons<<endl;
}
if (propagationMethod==0) {
moveElectronDriftApproximation( &electronQue.front() , getMFP_m(electronQue.front()) );
} else if (propagationMethod==1) {
moveElectronBorisPush(&electronQue.front(),getMFP_m(electronQue.front())); // If moved outside set v = 0,0,0;
} else {
stringstream ss;
ss<<"Simulation::doTC6(): propagationMethod= "<<propagationMethod<<" not valid.";
throw MyException(ss.str());
}
loc_m = electronQue.front().getLoc_m();
switch ( electronQue.front().roll_Interaction(Kat.getFlowSpeedT2_ms(loc_m),&rngUni) ) {
case 0: //Moved Outside
break;
case 1: // Elastic Scattering
throw MyException("Simulation::doTC6(): Elastic Scattering should be turned off.");
break;
case 2: // Ionization
electronQue.push_back( electronQue.front().do_Ionization(Kat.getTotSpeedT2_ms(loc_m, &rngUni, &rngNormal), numberElectrons, &rngUni) );
numberElectrons++;
electronQue.pop_front();
break;
default:
throw MyException("Simulation::doit(): getprocess() returned unknown event");
break;
}
outTC6 << electronQue.front() <<endl;
electronQue.pop_front();
}
outTC6.close();
};
*/
void Simulation::status_updater() {
if (numberElectrons%100 == 0){
if (updater_numberOfPrevious!=numberElectrons) {
cout<<numberElectrons<<"/"<<numTotalElectrons<<endl;
}
updater_numberOfPrevious = numberElectrons;
}
}
void Simulation::move_electron(Electron *moveElectron) {
if (propagationMethod==0) {
Kat.do_DriftApproximation(moveElectron);
} else if (propagationMethod==1) {
Kat.do_BorisPush(moveElectron); // If moved outside set v = 0,0,0;
} else {
stringstream ss;
ss<<"Simulation::doit(): propagationMethod= "<<propagationMethod<<" not valid.";
throw MyException(ss.str());
}
}
void Simulation::do_interaction(Electron *interactElectron) {
vec3 newLoc_m = electronQue.front().getLoc_m();
switch ( interactElectron->roll_Interaction(Kat.getFlowSpeedT2_ms(newLoc_m),&rngUni) ) {
case 0: //Moved Outside
break;
case 1: //Delta
Ekin_eV = deltaKinEnergy_eV;
case 1: // Elastic Scattering
interactElectron->do_elaScattering(Kat.getTotSpeedT2_ms(newLoc_m, &rngUni, &rngNormal) , &rngUni);
break;
case 2: //Maxwell Boltzman
Ekin_eV = getRandEnergyMaxwell(MBTemp_K,me_eV, &rngNormal);
case 2: // Ionization
electronQue.push_back( interactElectron->do_Ionization(Kat.getTotSpeedT2_ms(newLoc_m, &rngUni, &rngNormal), numberElectrons, &rngUni) );
numberElectrons++;
// Update Ions...
break;
case 3: // Recombination
break;
default:
cerr<<"Invalid Injection Method./n";
exit(1);
throw MyException("Simulation::doit(): getprocess() returned unknown event");
break;
}
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() {
double (Simulation::*fptr) (double) = &Simulation::EnergyDistributionBetaDecay;
return (doRejectionMethod(fptr , 1e-15, Etridecay_eV ,1.5e-28, &rngUni));
}
*/
double Simulation::getBetaDecayEnergyRejection_eV() {
// get Energy distribution using Rejection Method
double pmax,Emin_eV, Emax_eV, Ecan_eV,eta1,eta2;
pmax = 1.5e-28; // From Plotting EnergyDistributionBetaDecay
Emin_eV = 1e-15;
Emax_eV = Etridecay_eV;
do {
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)) );
}
// Moving
void Simulation::moveParticleBorisPush(Particle *movePart, double MFP_m) {
blitz::TinyVector<double,3> oldLoc_m = movePart->getLoc_m();
blitz::TinyVector<double,3> E_Vm = Kat.getElField(oldLoc_m);
blitz::TinyVector<double,3> B_T = Kat.getMagField(oldLoc_m);
double distance = 0.;
while (distance < MFP_m) {
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(movePart,oldLoc_m);
return;
}
void Simulation::moveParticleDriftApproximation(Particle *movePart, double MFP_m) {
// Drift Approximation: Assume constant B and only movement out in z=0, z=wgtsLength_m...
blitz::TinyVector<double,3> oldLoc_m = movePart->getLoc_m();
blitz::TinyVector<double,3> newLoc_m = oldLoc_m;
double z_m = oldLoc_m[2];
double vz_ms = movePart->getSpeed_ms()[2];
double vdir = vz_ms < 0 ? -1 : 1;
double vznew_m = z_m + vdir*MFP_m;
if (vznew_m < 0) {
vznew_m = 0;
movePart->setSpeed_ms(zeroVec());
} else if (vznew_m > wgtsLength_m) {
vznew_m = wgtsLength_m;
movePart->setSpeed_ms(zeroVec());
}
newLoc_m[2] = vznew_m;
movePart->setLoc_m(newLoc_m);
logParticle(movePart,oldLoc_m);
}
double Simulation::getMFP_m(Interaction Interaction) {
//Rework for inserting vector of Interactions
blitz::TinyVector<double,3> 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<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 ) );
}
// Termination
bool Simulation::checkTermination(Particle termPart) {
double EKin_eV, ETemp_eV;
EKin_eV = termPart.getKinEnergy_eV();
ETemp_eV = 3./2.*kb_eVK*wgtsTemp_K;
if (ETemp_eV<0) {
cerr<<"Simulation::checkTermination: ETemp_eV < 0." <<endl;
exit(1);
}
if (EKin_eV<=0.001) {
return(true);
}
return(false);
}
// Logging
void Simulation::initLogger(void) {
// Set zLogValues;
zLogValues.clear();
logging = true;
if (numberZBins == 0) {
cout<<"Logging turned off."<<endl;
logging = false;
return;
} else if (numberZBins == -1) {
cout<<"Logging everything to one file."<<endl;
} else if (numberZBins == 1) {
cout<<"Logging 1 z-Value: "<<wgtsLength_m<<endl;
zLogValues.push_back(wgtsLength_m);
} else {
for (int i = 0;i<numberZBins;i++) {
zLogValues.push_back( i*wgtsLength_m/(numberZBins-1) );
}
cout<<"Logging "<<numberZBins<<" z-Values."<<endl;
}
string dirname = getDirName();
if (numberZBins == -1) {
outStream = new ofstream [1
];
stringstream fileName;
fileName << dirname << "/all" << ".dat";
outStream[0].open(fileName.str().c_str());
} else {
outStream = new ofstream [numberZBins];
for (int i = 0; i < numberZBins; i++)
{
stringstream fileName;
fileName << dirname << "/z=" << zLogValues[i] << ".dat";
outStream[i].open(fileName.str().c_str());
}
}
}
string Simulation::getDirName() {
string poub = getenv("poub");
string pname = getenv("pname");
int i = 1;
stringstream dirname;
struct stat statbuf;
int isDir;
do {
dirname.str(std::string());
dirname << poub << "/" << pname << "_" << getTimeString() << "_" << i++;
isDir = 0;
if (stat(dirname.str().c_str(), &statbuf) != -1) {
if (S_ISDIR(statbuf.st_mode)) {
isDir = 1;
}
}
} while ( isDir == 1 );
if (mkdir(dirname.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
cerr<<"Simulation::initLogger: Could not create folder "<<dirname.str()<<" for Logging."<<endl;
exit(1);
}
return(dirname.str());
}
string Simulation::getTimeString() {
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
string str(buffer);
return(str);
}
void Simulation::logParticle(Particle *logPart,blitz::TinyVector<double,3> oldLoc_m) {
if (!logging) {
return;
}
// Annahme: Ohne Interaction bliebt Bewegung in z-Richtung Konstant.
if (numberZBins == -1) {
outStream[0]<<*logPart<<endl;
} else {
double zold = oldLoc_m[2];
double znew = logPart->getLoc_m()[2];
double zmin = min(zold,znew);
double zmax = max(zold,znew);
for (int i=0;i<numberZBins;i++) {
if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) {
outStream[i]<<*logPart<<endl; // Location wrong, because it s the new Location.
}
}
}
}
-64
View File
@@ -1,64 +0,0 @@
//
// simulation.h
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef simulation_h
#define simulation_h
#include "parameter.h"
#include "katrin.h"
#include "particle.h"
#include "interaction.h"
class Simulation: public Parameter
{
public:
~Simulation(){};
Simulation() {
numInjectedParticles = 0;
rngUni.seed(seed);
logging = false;
};
void initLogger(void);
void doit(void);
protected:
// Simulation
Katrin Kat;
ranlib::Uniform<double> rngUni;
ranlib::NormalUnit<double> rngNormal;
int numInjectedParticles;
deque<Particle> particleQue;
// Injection
Particle inject();
double getBetaDecayEnergyRejection_eV();
double EnergyDistributionBetaDecay(double Ekin_eV);
double FermiFunction(double Z, double Ekin_eV);
// Moving
void moveParticleBorisPush(Particle *movePart, double MFP_m);
void moveParticleDriftApproximation(Particle *movePart, double MFP_m);
double getMFP_m(Interaction Interaction);
bool movedOutside(blitz::TinyVector<double,3> loc_m);
// Termination
bool checkTermination(Particle termPart);
// Logging
bool logging;
ofstream *outStream;
vector<double> zLogValues;
void logParticle(Particle *logPart,blitz::TinyVector<double,3> oldLoc_m);
string getDirName();
string getTimeString();
};
#endif
+44
View File
@@ -0,0 +1,44 @@
//
// simulation.hpp
// mc_electron
//
// Created by Christian Reiling on 20.09.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef simulation_hpp
#define simulation_hpp
#include "parameter.hpp"
#include "constants.hpp"
#include "katrin.hpp"
#include "electron.hpp"
#include "logger.hpp"
#include <deque>
class Simulation: private Parameter, private Constants {
public:
Simulation();
void doit();
// void doTC6(void);
private:
void status_updater();
void move_electron(Electron *moveElectron);
void do_interaction(Electron *interactElectron);
// Simulation
Katrin Kat;
Logger Log;
ranlib::Uniform<double> rngUni;
ranlib::NormalUnit<double> rngNormal;
int numberElectrons;
std::deque<Electron> electronQue;
int updater_numberOfPrevious;
};
#endif /* simulation_hpp */
-124
View File
@@ -1,124 +0,0 @@
//
// tests.cpp
// mc_electron
//
// Created by Christian Reiling on 17.10.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#include "tests.h"
void runTests() {
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");
TEST.SigmaTot_m2(10000, outputFilePath + "sigmaTot_m2.dat", -3, 9);
TEST.MFP_m(10000, outputFilePath + "MFP_m.dat", -3, 9);
}
void TestSimulation::injection (int numParticles, string outputFilePath) {
ofstream outFile;
outFile.open (outputFilePath.c_str());
for (int i = 0;i<numParticles;i++) {
outFile << inject() <<endl;
}
outFile.close();
}
void TestSimulation::decaySpectrum (int numPoints,string outputFilePath) {
ofstream outFile;
outFile.open (outputFilePath.c_str());
double x,y;
for (int i = 0;i<numPoints;i++) {
x = i * Etridecay_eV/(numPoints-1);
y = EnergyDistributionBetaDecay(x);
outFile << x <<";"<<y <<endl;
}
outFile.close();
}
void TestSimulation::rejectionBetaDecay(int numDecays, string outputFilePath) {
ofstream outFile;
outFile.open (outputFilePath.c_str());
for (int i = 0;i<numDecays;i++) {
outFile << getBetaDecayEnergyRejection_eV() <<endl;
}
outFile.close();
}
void TestSimulation::maxwellBoltzmann(int numDecays, string outputFilePath) {
ofstream outFile;
outFile.open (outputFilePath.c_str());
for (int i = 0;i<numDecays;i++) {
//outFile << getEnergyBoltzmann_eV() <<endl;
}
outFile.close();
}
void TestSimulation::SigmaTot_m2(int numPointsPerPower, string outputFilePath, int EminPower, int EmaxPower) {
if (EminPower >= EmaxPower) {
cerr<<"TestSimulation::getMFP_m: Power of lower energy bound \">=\" upper energy bound."<<endl;
exit(1);
}
ofstream outFile;
outFile.open (outputFilePath.c_str());
Particle P1 = Particle(me_eV, 1, zeroVec(), electron);
Particle P2 = Particle(mT2_eV, ZT2, zeroVec(), tritium2);
Interaction Inter = Interaction(&P1, &P2);
double distance = 1./double(numPointsPerPower);
int numPoints = (EmaxPower - EminPower) * numPointsPerPower;
double power;
double energy_eV, sigma_m2;
blitz::TinyVector<double,3> speed;
for (int i=0;i<=numPoints;i++) {
power = EminPower + i*distance;
energy_eV = pow(10,power);
speed = 0,0, calcV_msFromEkin_eV(energy_eV, me_eV);
P2.setSpeed_ms(speed);
sigma_m2 = Inter.getSigmaTot_m2();
outFile<<energy_eV<<";"<<sigma_m2<<endl;
}
outFile.close();
}
void TestSimulation::MFP_m(int numPointsPerPower, string outputFilePath, int EminPower, int EmaxPower) {
if (EminPower >= EmaxPower) {
cerr<<"TestSimulation::getMFP_m: Power of lower energy bound \">=\" upper energy bound."<<endl;
exit(1);
}
ofstream outFile;
outFile.open (outputFilePath.c_str());
Particle P1 = Particle(me_eV, 1, zeroVec(), electron);
Particle P2 = Particle(mT2_eV, ZT2, zeroVec(), tritium2);
Interaction Inter = Interaction(&P1, &P2);
double distance = 1./double(numPointsPerPower);
int numPoints = (EmaxPower - EminPower) * numPointsPerPower;
double power;
double energy_eV, MFP_m;
blitz::TinyVector<double,3> speed;
for (int i=0;i<=numPoints;i++) {
power = EminPower + i*distance;
energy_eV = pow(10,power);
speed = 0,0, calcV_msFromEkin_eV(energy_eV, me_eV);
P2.setSpeed_ms(speed);
MFP_m = getMFP_m(Inter);
outFile<<energy_eV<<";"<<MFP_m<<endl;
}
outFile.close();
}
-47
View File
@@ -1,47 +0,0 @@
//
// tests.h
// mc_electron
//
// Created by Christian Reiling on 17.10.18.
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#ifndef tests_h
#define tests_h
#include <stdio.h>
#include "simulation.h"
void runTests();
class TestSimulation: public Simulation {
public:
~TestSimulation(){}
TestSimulation() : Simulation(){};
void injection (int numParticles, string outputFilePath);
void decaySpectrum (int numPoints, string outputFilePath);
void rejectionBetaDecay(int numDecays, string outputFilePath);
void maxwellBoltzmann(int numDecays, string outputFilePath);
void SigmaTot_m2(int numPointsPerPower, string outputFilePath, int EminPower, int EmaxPower);
void MFP_m(int numPointsPerPower, string outputFilePath, int EminPower, int EmaxPower);
};
/*
class TestParticle: public Particle {
public:
~TestParticle(){}
TestParticle() : Particle(){};
TestParticle(double mass_eV, double charge_e, blitz::TinyVector<double,3> speed_ms, species spec) : Particle(mass_eV, charge_e, speed_ms, spec){};
TestParticle(double mass_eV, double charge_e,blitz::TinyVector<double,3> loc_m, blitz::TinyVector<double,3> speed_ms, double idnum, double parentIdnum, species spec) : Particle(mass_eV, charge_e, loc_m, speed_ms, idnum, parentIdnum, spec) {};
};
*/
#endif
+12
View File
@@ -0,0 +1,12 @@
//
// main.cpp
// test_mc_electron
//
// Created by Christian Reiling on 07.02.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#define CATCH_CONFIG_MAIN
#include "catch.hpp"