Screenshot with working ionization function.
Ionization will be changed to BED model.
This commit is contained in:
+156
-96
@@ -6,117 +6,174 @@
|
||||
// Copyright © 2018 Christian Reiling. All rights reserved.
|
||||
//
|
||||
|
||||
#include "simulation.h"
|
||||
#include "simulation.hpp"
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
while ( (numInjectedElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
|
||||
if (electronQue.empty()) {
|
||||
electronQue.push_back(injectElectron());
|
||||
numInjectedElectrons++;
|
||||
}
|
||||
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;
|
||||
|
||||
switch ( IntereT2.getInteraction(&rngUni) ) {
|
||||
if (numInjectedElectrons%100 == 0){
|
||||
cout<<numInjectedElectrons<<"/"<<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::doit(): propagationMethod= "<<propagationMethod<<" not valid.";
|
||||
throw (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
|
||||
ScatT2 = Kat.getT2ScatPart(particleQue.front().getLoc_m(), &rngUni, &rngNormal);
|
||||
IntereT2 = Interaction(&particleQue.front(),&ScatT2);
|
||||
IntereT2.doElaScat(&rngUni);
|
||||
electronQue.front().do_elaScattering(Kat.getTotSpeedT2_ms(loc_m, &rngUni, &rngNormal) , &rngUni);
|
||||
break;
|
||||
case 2: // Ionization
|
||||
particleQue.push_back( IntereT2.doIon(numInjectedParticles, &rngUni) );
|
||||
numInjectedParticles++;
|
||||
electronQue.push_back( electronQue.front().do_Ionization(Kat.getTotSpeedT2_ms(loc_m, &rngUni, &rngNormal), numInjectedElectrons, &rngUni) );
|
||||
numInjectedElectrons++;
|
||||
|
||||
// Update Ions...
|
||||
break;
|
||||
case 3: // Recombination
|
||||
break;
|
||||
default:
|
||||
cerr<<"getprocess() returned unknown event"<<endl;
|
||||
throw ("Simulation::doit(): getprocess() returned unknown event");
|
||||
break;
|
||||
}
|
||||
if (checkTermination (particleQue.front())) {
|
||||
particleQue.pop_front();
|
||||
if (checkTermination (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 << getDirName() << "/TC6all.dat";
|
||||
|
||||
ofstream outTC6;
|
||||
outTC6.open (fileName.str().c_str());
|
||||
|
||||
blitz::TinyVector <double,3> loc_m;
|
||||
while ( (numInjectedElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
|
||||
if (!electronQue.empty()) {
|
||||
throw ("Simulation::doTC6: electronQue should be empty");
|
||||
}
|
||||
|
||||
electronQue.push_back(injectElectron());
|
||||
numInjectedElectrons++;
|
||||
|
||||
|
||||
if (numInjectedElectrons%100 == 0){
|
||||
cout<<numInjectedElectrons<<"/"<<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 (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 ("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), numInjectedElectrons, &rngUni) );
|
||||
numInjectedElectrons++;
|
||||
electronQue.pop_front();
|
||||
break;
|
||||
default:
|
||||
throw ("Simulation::doit(): getprocess() returned unknown event");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
outTC6 << electronQue.front() <<endl;
|
||||
electronQue.pop_front();
|
||||
|
||||
}
|
||||
outTC6.close();
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Injection
|
||||
Electron Simulation::injectElectron() {
|
||||
|
||||
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;
|
||||
|
||||
blitz::TinyVector<double,3> loc_m = blitz::TinyVector<double,3> (r_m*cos(phi),r_m*sin(phi),z_m);
|
||||
double Ekin_eV;
|
||||
switch(injectionMethod) {
|
||||
case 0: // Fermi
|
||||
Ekin_eV = getBetaDecayEnergyRejection_eV(); //Implement Later
|
||||
Ekin_eV = getEkin_eVFermi_eV();
|
||||
break;
|
||||
case 1: //Delta
|
||||
Ekin_eV = deltaKinEnergy_eV;
|
||||
break;
|
||||
case 2: //Maxwell Boltzman
|
||||
Ekin_eV = getRandEnergyMaxwell(MBTemp_K,me_eV, &rngNormal);
|
||||
Ekin_eV = roll_MB_Energy(MBTemp_K,me_eV, &rngNormal);
|
||||
break;
|
||||
default:
|
||||
cerr<<"Invalid Injection Method./n";
|
||||
exit(1);
|
||||
throw ("Simulation::injectElectron: Invalid Injection Method.");
|
||||
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));
|
||||
blitz::TinyVector<double,3> speed_ms = roll_uniform_direction(&rngUni) * calc_Speed_from_Ekin_eV(Ekin_eV,me_eV);
|
||||
return (Electron(loc_m, speed_ms,numInjectedElectrons,numInjectedElectrons));
|
||||
}
|
||||
|
||||
/*
|
||||
double Simulation::getBetaDecayEnergyRejection_eV() {
|
||||
double (Simulation::*fptr) (double) = &Simulation::EnergyDistributionBetaDecay;
|
||||
return (doRejectionMethod(fptr , 1e-15, Etridecay_eV ,1.5e-28, &rngUni));
|
||||
return (doRejectionMethod(fptr , 1e-15, TritiumDecayEndpoint_eV ,1.5e-28, &rngUni));
|
||||
}
|
||||
*/
|
||||
|
||||
double Simulation::getBetaDecayEnergyRejection_eV() {
|
||||
double Simulation::getEkin_eVFermi_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;
|
||||
Emax_eV = TritiumDecayEndpoint_eV;
|
||||
do {
|
||||
eta1 = rngUni.random();
|
||||
eta2 = rngUni.random();
|
||||
Ecan_eV = Emin_eV + eta1*(Emax_eV - Emin_eV);
|
||||
} while(eta2*pmax > EnergyDistributionBetaDecay(Ecan_eV));
|
||||
} while(eta2*pmax > ESpectrumBetaDecay(Ecan_eV));
|
||||
return(Ecan_eV);
|
||||
}
|
||||
|
||||
double Simulation::EnergyDistributionBetaDecay(double Ekin_eV) {
|
||||
if (Ekin_eV>Etridecay_eV) {
|
||||
double Simulation::ESpectrumBetaDecay(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( BetaDecayC * FermiFunction(ZHe3, Ekin_eV) * p_eV*(Ekin_eV+me_eV)*pow(Etridecay_eV-Ekin_eV,2) );
|
||||
return( BetaDecayConst * FermiFunction(2, Ekin_eV) * p_eV*(Ekin_eV+me_eV)*pow(TritiumDecayEndpoint_eV-Ekin_eV,2) );
|
||||
}
|
||||
}
|
||||
double Simulation::FermiFunction(double Z, double Ekin_eV) {
|
||||
@@ -128,54 +185,58 @@ double Simulation::FermiFunction(double Z, double Ekin_eV) {
|
||||
|
||||
|
||||
// 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);
|
||||
void Simulation::moveElectronBorisPush(Electron *moveElectron, double MFP_m) {
|
||||
blitz::TinyVector<double,3> oldLoc_m = moveElectron->getLoc_m();
|
||||
blitz::TinyVector<double,3> E_Vm = Kat.getElField_Vm(oldLoc_m);
|
||||
blitz::TinyVector<double,3> B_T = Kat.getMagField_T(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());
|
||||
distance += moveElectron->borisFly_m(E_Vm , B_T , dt_s);
|
||||
if (movedOutside(moveElectron->getLoc_m())) {
|
||||
logElectron(moveElectron,oldLoc_m);
|
||||
moveElectron->setSpeed_ms(blitz::TinyVector<double,3>(0,0,0));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
logParticle(movePart,oldLoc_m);
|
||||
logElectron(moveElectron,oldLoc_m);
|
||||
return;
|
||||
}
|
||||
|
||||
void Simulation::moveParticleDriftApproximation(Particle *movePart, double MFP_m) {
|
||||
void Simulation::moveElectronDriftApproximation(Electron *moveElectron, 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> oldLoc_m = moveElectron->getLoc_m();
|
||||
blitz::TinyVector<double,3> newLoc_m = oldLoc_m;
|
||||
double z_m = oldLoc_m[2];
|
||||
double vz_ms = movePart->getSpeed_ms()[2];
|
||||
double vz_ms = moveElectron->getSpeed_ms()[2];
|
||||
double vdir = vz_ms < 0 ? -1 : 1;
|
||||
double vznew_m = z_m + vdir*MFP_m;
|
||||
double znew_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());
|
||||
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);
|
||||
logElectron(moveElectron,oldLoc_m);
|
||||
|
||||
if (znew_m==0 || znew_m==wgtsLength_m) {
|
||||
moveElectron->setSpeed_ms(blitz::TinyVector<double,3>(0,0,0));
|
||||
}
|
||||
newLoc_m[2] = vznew_m;
|
||||
movePart->setLoc_m(newLoc_m);
|
||||
logParticle(movePart,oldLoc_m);
|
||||
}
|
||||
|
||||
double Simulation::getMFP_m(Interaction Interaction) {
|
||||
double Simulation::getMFP_m(Electron moveElectron) {
|
||||
//Rework for inserting vector of Interactions
|
||||
blitz::TinyVector<double,3> loc_m;
|
||||
blitz::TinyVector<double,3> loc_m, speedT2_ms;
|
||||
double sigmaTot_m2, density_m3, MFP_m;
|
||||
loc_m = Interaction.getP1()->getLoc_m();
|
||||
loc_m = moveElectron.getLoc_m();
|
||||
speedT2_ms = Kat.getFlowSpeedT2_ms(loc_m);
|
||||
|
||||
sigmaTot_m2 = Interaction.getSigmaTot_m2();
|
||||
density_m3 = Kat.getTritiumDensity(loc_m);
|
||||
sigmaTot_m2 = moveElectron.calc_SigmaTotal_m2(speedT2_ms);
|
||||
density_m3 = Kat.getTritiumDensity_m3(loc_m);
|
||||
MFP_m = 1/(density_m3 * sigmaTot_m2);
|
||||
return(MFP_m);
|
||||
}
|
||||
@@ -186,16 +247,15 @@ bool Simulation::movedOutside(blitz::TinyVector<double,3> loc_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;
|
||||
bool Simulation::checkTermination(Electron termElectron) {
|
||||
double Ekin_eV, ETemp_eV;
|
||||
Ekin_eV = termElectron.getKinEnergy_eV();
|
||||
ETemp_eV = kb_eVK*wgtsTemp_K;
|
||||
if (ETemp_eV<0) {
|
||||
cerr<<"Simulation::checkTermination: ETemp_eV < 0." <<endl;
|
||||
exit(1);
|
||||
throw ("Simulation::checkTermination: ETemp_eV < 0.");
|
||||
}
|
||||
|
||||
if (EKin_eV<=0.001) {
|
||||
if (Ekin_eV<=cutoffEnergy_eV) {
|
||||
return(true);
|
||||
}
|
||||
return(false);
|
||||
@@ -227,8 +287,7 @@ void Simulation::initLogger(void) {
|
||||
string dirname = getDirName();
|
||||
|
||||
if (numberZBins == -1) {
|
||||
outStream = new ofstream [1
|
||||
];
|
||||
outStream = new ofstream [1];
|
||||
stringstream fileName;
|
||||
fileName << dirname << "/all" << ".dat";
|
||||
outStream[0].open(fileName.str().c_str());
|
||||
@@ -241,8 +300,8 @@ void Simulation::initLogger(void) {
|
||||
outStream[i].open(fileName.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
string Simulation::getDirName() {
|
||||
string poub = getenv("poub");
|
||||
string pname = getenv("pname");
|
||||
@@ -261,8 +320,9 @@ string Simulation::getDirName() {
|
||||
}
|
||||
} 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);
|
||||
stringstream ss;
|
||||
ss<<"Simulation::initLogger: Could not create folder "<<dirname.str()<<" for Logging.";
|
||||
throw (ss.str());
|
||||
}
|
||||
return(dirname.str());
|
||||
}
|
||||
@@ -277,22 +337,22 @@ string Simulation::getTimeString() {
|
||||
string str(buffer);
|
||||
return(str);
|
||||
}
|
||||
void Simulation::logParticle(Particle *logPart,blitz::TinyVector<double,3> oldLoc_m) {
|
||||
void Simulation::logElectron(Electron *logElectron, 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;
|
||||
outStream[0]<<*logElectron<<endl;
|
||||
} else {
|
||||
double zold = oldLoc_m[2];
|
||||
double znew = logPart->getLoc_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]<<*logPart<<endl; // Location wrong, because it s the new Location.
|
||||
outStream[i]<<*logElectron<<endl; // Location wrong, because it s the new Location.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user