Minor Changes

This commit is contained in:
Christian
2019-03-01 10:29:17 +01:00
parent 96e9ee4f2b
commit a20aa7a071
22 changed files with 733 additions and 705 deletions
+60 -278
View File
@@ -6,78 +6,60 @@
// Copyright © 2018 Christian Reiling. All rights reserved.
//
#include "simulation.hpp"
using namespace std;
Simulation::Simulation() {
numberElectrons = 0;
rngUni.seed(seed);
updater_numberOfPrevious = 0;
}
void Simulation::doit() {
blitz::TinyVector <double,3> loc_m;
while ( (numInjectedElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
vec3 oldLoc_m;
while ( (numberElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
if (electronQue.empty()) {
electronQue.push_back(injectElectron());
numInjectedElectrons++;
electronQue.push_back(Kat.injectElectron(numberElectrons++, &rngUni, &rngNormal));
}
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
electronQue.front().do_elaScattering(Kat.getTotSpeedT2_ms(loc_m, &rngUni, &rngNormal) , &rngUni);
break;
case 2: // Ionization
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:
throw ("Simulation::doit(): getprocess() returned unknown event");
break;
}
if (checkTermination (electronQue.front())) {
status_updater();
oldLoc_m = electronQue.front().getLoc_m();
move_electron(&electronQue.front());
Log.logElectron(&electronQue.front(), oldLoc_m);
do_interaction(&electronQue.front());
if (Kat.get_Terminated (electronQue.front())) {
electronQue.pop_front();
}
}
};
/*
void Simulation::doTC6() {
cout<<endl<<"Simulation::doTC6: Doing Testcase 6."<<endl<<endl;
stringstream fileName;
fileName << getDirName() << "/TC6all.dat";
fileName << Log.getDirName() << "/TC6all.dat";
ofstream outTC6;
outTC6.open (fileName.str().c_str());
blitz::TinyVector <double,3> loc_m;
while ( (numInjectedElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
while ( (numberElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
if (!electronQue.empty()) {
throw ("Simulation::doTC6: electronQue should be empty");
}
electronQue.push_back(injectElectron());
numInjectedElectrons++;
electronQue.push_back(Kat.injectElectron(numberElectrons, &rngUni, &rngNormal));
numberElectrons++;
if (numInjectedElectrons%100 == 0){
cout<<numInjectedElectrons<<"/"<<numTotalElectrons<<endl;
if (numberElectrons%100 == 0){
cout<<numberElectrons<<"/"<<numTotalElectrons<<endl;
}
if (propagationMethod==0) {
@@ -99,8 +81,8 @@ void Simulation::doTC6() {
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.push_back( electronQue.front().do_Ionization(Kat.getTotSpeedT2_ms(loc_m, &rngUni, &rngNormal), numberElectrons, &rngUni) );
numberElectrons++;
electronQue.pop_front();
break;
default:
@@ -115,245 +97,45 @@ void Simulation::doTC6() {
}
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 = getEkin_eVFermi_eV();
break;
case 1: //Delta
Ekin_eV = deltaKinEnergy_eV;
break;
case 2: //Maxwell Boltzman
Ekin_eV = roll_MB_Energy(MBTemp_K,me_eV, &rngNormal);
break;
default:
throw ("Simulation::injectElectron: Invalid Injection Method.");
break;
}
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, TritiumDecayEndpoint_eV ,1.5e-28, &rngUni));
}
*/
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 = TritiumDecayEndpoint_eV;
do {
eta1 = rngUni.random();
eta2 = rngUni.random();
Ecan_eV = Emin_eV + eta1*(Emax_eV - Emin_eV);
} while(eta2*pmax > ESpectrumBetaDecay(Ecan_eV));
return(Ecan_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( 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) {
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::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 += 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;
void Simulation::status_updater() {
if (numberElectrons%100 == 0){
if (updater_numberOfPrevious!=numberElectrons) {
cout<<numberElectrons<<"/"<<numTotalElectrons<<endl;
}
}
logElectron(moveElectron,oldLoc_m);
return;
}
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 = moveElectron->getLoc_m();
blitz::TinyVector<double,3> 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);
logElectron(moveElectron,oldLoc_m);
if (znew_m==0 || znew_m==wgtsLength_m) {
moveElectron->setSpeed_ms(blitz::TinyVector<double,3>(0,0,0));
updater_numberOfPrevious = numberElectrons;
}
}
double Simulation::getMFP_m(Electron moveElectron) {
//Rework for inserting vector of Interactions
blitz::TinyVector<double,3> loc_m, speedT2_ms;
double sigmaTot_m2, density_m3, MFP_m;
loc_m = moveElectron.getLoc_m();
speedT2_ms = Kat.getFlowSpeedT2_ms(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);
}
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(Electron termElectron) {
double Ekin_eV, ETemp_eV;
Ekin_eV = termElectron.getKinEnergy_eV();
ETemp_eV = kb_eVK*wgtsTemp_K;
if (ETemp_eV<0) {
throw ("Simulation::checkTermination: ETemp_eV < 0.");
}
if (Ekin_eV<=cutoffEnergy_eV) {
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);
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 {
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) {
stringstream ss;
ss<<"Simulation::initLogger: Could not create folder "<<dirname.str()<<" for Logging.";
ss<<"Simulation::doit(): propagationMethod= "<<propagationMethod<<" not valid.";
throw (ss.str());
}
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::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]<<*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 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: // Elastic Scattering
interactElectron->do_elaScattering(Kat.getTotSpeedT2_ms(newLoc_m, &rngUni, &rngNormal) , &rngUni);
break;
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:
throw ("Simulation::doit(): getprocess() returned unknown event");
break;
}
}