Added new test class, reworked some code

Started Implementing Ionization
This commit is contained in:
Christian
2018-11-07 12:11:24 +01:00
parent cf7ac857d2
commit 15851a540a
14 changed files with 559 additions and 222 deletions
+157 -103
View File
@@ -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);
}