From 6b7eeac0bbe5c080e4678768d249cd665ec1047b Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 27 Nov 2018 10:14:15 +0100 Subject: [PATCH 1/3] Removed c++11 only functions --- src/mc_electron/particle.cpp | 25 ++++++++--------- src/mc_electron/particle.h | 3 +- src/mc_electron/simulation.cpp | 50 +++++++++++++++++++++++++++------- src/mc_electron/simulation.h | 8 ++++-- 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/mc_electron/particle.cpp b/src/mc_electron/particle.cpp index eabb630..c65f6ae 100644 --- a/src/mc_electron/particle.cpp +++ b/src/mc_electron/particle.cpp @@ -94,7 +94,6 @@ Particle::Particle(double mass_eV, double charge_e,blitz::TinyVector l } - double Particle::borisFly_m(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s) { double distance_m; x_m += dt_s/2 * v_ms; @@ -106,16 +105,16 @@ double Particle::borisFly_m(blitz::TinyVector E_Vm , blitz::TinyVecto } void Particle::changeSpeedBoris(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s) { - { - blitz::TinyVector vMinus_ms, vPlus_ms, vPrime_ms, t, s; - double q_C = -1*Z * e_C; - double m_kg = m_eV*e_C/c_ms/c_ms; - - t = q_C*dt_s/(2*m_kg) * B_T; - s = (2*t)/(1+dot(t,t)); - vMinus_ms = v_ms + (q_C*E_Vm*dt_s)/(2*m_kg); - vPrime_ms = vMinus_ms + cross(vMinus_ms,t); - vPlus_ms = vMinus_ms + cross(vPrime_ms,s); - v_ms = vPlus_ms + q_C/m_kg*E_Vm*0.5*dt_s; - } + blitz::TinyVector vMinus_ms, vPlus_ms, vPrime_ms, t, s; + double q_C = -1*Z * e_C; + double m_kg = m_eV*e_C/c_ms/c_ms; + + t = q_C*dt_s/(2*m_kg) * B_T; + s = (2*t)/(1+dot(t,t)); + vMinus_ms = v_ms + (q_C*E_Vm*dt_s)/(2*m_kg); + vPrime_ms = vMinus_ms + cross(vMinus_ms,t); + vPlus_ms = vMinus_ms + cross(vPrime_ms,s); + v_ms = vPlus_ms + q_C/m_kg*E_Vm*0.5*dt_s; } + + diff --git a/src/mc_electron/particle.h b/src/mc_electron/particle.h index 595fdef..7e10708 100644 --- a/src/mc_electron/particle.h +++ b/src/mc_electron/particle.h @@ -61,8 +61,7 @@ public: public: double borisFly_m(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s); protected: - void changeLocFly(double dt_s); - void changeSpeedBoris(blitz::TinyVector E_Vm , blitz::TinyVector B_T , double dt_s); + void changeSpeedBoris(blitz::TinyVector E_Vm,blitz::TinyVector B_T , double dt_s); // Write Methods public: diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index 47846fb..8f4f2bb 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -29,7 +29,7 @@ void Simulation::doit() { //Replace with vector containing all Interactions and adapt functions and put particle pointers into Particle class MFP_m = getMFP_m(IntereT2); - moveParticle(&particleQue.front(),MFP_m); // If moved outside set v = 0,0,0; + moveParticleBorisPush(&particleQue.front(),MFP_m); // If moved outside set v = 0,0,0; switch (getInteraction(particleQue.front())) { case 0: //Moved Outside @@ -117,7 +117,7 @@ double Simulation::FermiFunction(double Z, double Ekin_eV) { // Moving -void Simulation::moveParticle(Particle *movePart, double MFP_m) { +void Simulation::moveParticleBorisPush(Particle *movePart, double MFP_m) { blitz::TinyVector oldLoc_m = movePart->getLoc_m(); blitz::TinyVector E_Vm = Kat.getElField(oldLoc_m); blitz::TinyVector B_T = Kat.getMagField(oldLoc_m); @@ -131,9 +131,32 @@ void Simulation::moveParticle(Particle *movePart, double MFP_m) { 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 oldLoc_m = movePart->getLoc_m(); + blitz::TinyVector 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 loc_m; @@ -156,7 +179,7 @@ int Simulation::getInteraction(Particle interPart) { if (interPart.getKinEnergy_eV()==0) { return(0); } - return(0); + return(1); } // Termination @@ -169,13 +192,13 @@ bool Simulation::checkTermination(Particle termPart) { exit(1); } - if (EKin_eV<=ETemp_eV) { + if (EKin_eV<=0.00001) { return(true); } - return(false); } + // Logging void Simulation::initLogger(void) { // Set zLogValues; @@ -195,6 +218,16 @@ void Simulation::initLogger(void) { std::cout<<"Logging "< loc_m); @@ -55,9 +56,12 @@ protected: // Logging bool logging; - std::vector outStream; + //std::vector outStream; + //std::ofstream outStream[]; + std::ofstream *outStream; std::vector zLogValues; void logParticle(Particle *logPart,blitz::TinyVector oldLoc_m); + std::string getDirName(); std::string getTimeString(); }; From 2cf9bc6c2390074288f3eee651bc19b83f12f9c4 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 27 Nov 2018 10:22:56 +0100 Subject: [PATCH 2/3] Removed std:: and using namespace std --- src/mc_electron/constants.h | 3 +++ src/mc_electron/interaction.cpp | 2 +- src/mc_electron/main.cpp | 12 ++++++---- src/mc_electron/parameter.h | 10 ++++---- src/mc_electron/particle.cpp | 6 ++--- src/mc_electron/particle.h | 4 ++-- src/mc_electron/simulation.cpp | 42 ++++++++++++++++----------------- src/mc_electron/simulation.h | 14 +++++------ src/mc_electron/tests.cpp | 26 ++++++++++---------- src/mc_electron/tests.h | 8 +++---- 10 files changed, 66 insertions(+), 61 deletions(-) diff --git a/src/mc_electron/constants.h b/src/mc_electron/constants.h index c1caaeb..96b62d3 100644 --- a/src/mc_electron/constants.h +++ b/src/mc_electron/constants.h @@ -22,6 +22,9 @@ #include #include +using namespace std; + + class Constants { public: diff --git a/src/mc_electron/interaction.cpp b/src/mc_electron/interaction.cpp index 52b2bb8..12a1398 100644 --- a/src/mc_electron/interaction.cpp +++ b/src/mc_electron/interaction.cpp @@ -95,7 +95,7 @@ blitz::TinyVector Interaction::getNewDir(double theta,double phi) { } endloops: if (phi1 == -1) { - std::cerr<<"Interaction::getNewDir: phi1 not found."< newDir; diff --git a/src/mc_electron/main.cpp b/src/mc_electron/main.cpp index ecc2157..9719572 100644 --- a/src/mc_electron/main.cpp +++ b/src/mc_electron/main.cpp @@ -10,27 +10,29 @@ #include "tests.h" #ifdef DEBUG -#define Debug( x ) std::cout << x << std::endl; +#define Debug( x ) cout << x << endl; #else #define Debug( x ) #endif +using namespace std; + int main(int argc, const char * argv[]) { Debug("Debug Mode is on.") //runTests(); /* - std::clock_t start; + clock_t start; double duration; - start = std::clock(); + start = clock(); */ Simulation Sim; Sim.initLogger(); Sim.doit(); /* - duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; - std::cout<<"Duration: "<< duration <<'\n'; + duration = ( clock() - start ) / (double) CLOCKS_PER_SEC; + cout<<"Duration: "<< duration <<'\n'; */ } diff --git a/src/mc_electron/parameter.h b/src/mc_electron/parameter.h index 594fdd2..a5bf7e0 100644 --- a/src/mc_electron/parameter.h +++ b/src/mc_electron/parameter.h @@ -38,7 +38,7 @@ public: } private: - double value(std::string strin, char* ext = (char*)("cat")) { + double value(string strin, char* ext = (char*)("cat")) { char* str = strdup(strin.c_str()); double temp; char dummy[256]; @@ -47,14 +47,14 @@ private: if (getenv("poub") == NULL) { - std::cerr<<"Parameter::value: Environment variable poub not defined.\n"; + cerr<<"Parameter::value: Environment variable poub not defined.\n"; exit(1); } strcpy(fname, getenv("poub")); pname = getenv("pname"); if (pname == NULL) { - std::cerr<<"Parameter::value: Environment variable pname not defined.\n"; + cerr<<"Parameter::value: Environment variable pname not defined.\n"; exit(1); } if (!ext) @@ -64,7 +64,7 @@ private: sprintf(fname,"%s/%s.%s",getenv("poub"),getenv("pname"), ext); if (!(fcat = fopen(fname, "r"))) { - std::cerr<<"Parameter::value: Can not open file " < oldLoc_m) { @@ -269,12 +269,12 @@ void Simulation::logParticle(Particle *logPart,blitz::TinyVector oldLo double zold = oldLoc_m[2]; double znew = logPart->getLoc_m()[2]; - double zmin = std::min(zold,znew); - double zmax = std::max(zold,znew); + double zmin = min(zold,znew); + double zmax = max(zold,znew); for (int i=0;i=zmin && zLogValues[i]<=zmax) { - outStream[i]<<*logPart< rngUni; ranlib::NormalUnit rngNormal; int numInjectedParticles; - std::deque particleQue; + deque particleQue; // Injection Particle inject(); @@ -56,13 +56,13 @@ protected: // Logging bool logging; - //std::vector outStream; - //std::ofstream outStream[]; - std::ofstream *outStream; - std::vector zLogValues; + //vector outStream; + //ofstream outStream[]; + ofstream *outStream; + vector zLogValues; void logParticle(Particle *logPart,blitz::TinyVector oldLoc_m); - std::string getDirName(); - std::string getTimeString(); + string getDirName(); + string getTimeString(); }; #endif diff --git a/src/mc_electron/tests.cpp b/src/mc_electron/tests.cpp index 435e3ed..88ab424 100644 --- a/src/mc_electron/tests.cpp +++ b/src/mc_electron/tests.cpp @@ -8,7 +8,7 @@ #include "tests.h" void runTests() { - std::string outputFilePath = "/Users/ChristianReiling/Documents/Masterarbeit/CodeTesting/Data/"; + string outputFilePath = "/Users/ChristianReiling/Documents/Masterarbeit/CodeTesting/Data/"; TestSimulation TEST; TEST.injection (100000, outputFilePath + "injection.dat"); TEST.decaySpectrum(100000, outputFilePath + "decaySpectrum.dat"); @@ -16,43 +16,43 @@ void runTests() { TEST.maxwellBoltzmann(100000, outputFilePath + "maxwellBoltzmann.dat"); } -void TestSimulation::injection (int numParticles, std::string outputFilePath) { - std::ofstream outFile; +void TestSimulation::injection (int numParticles, string outputFilePath) { + ofstream outFile; outFile.open (outputFilePath); for (int i = 0;i Date: Tue, 27 Nov 2018 11:11:19 +0100 Subject: [PATCH 3/3] Using stringstream and bugfixing using c_str(). Compatible with kalinka now --- src/mc_electron/constants.h | 1 + src/mc_electron/simulation.cpp | 18 ++++++++++-------- src/mc_electron/tests.cpp | 8 ++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/mc_electron/constants.h b/src/mc_electron/constants.h index 96b62d3..c2138ae 100644 --- a/src/mc_electron/constants.h +++ b/src/mc_electron/constants.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/src/mc_electron/simulation.cpp b/src/mc_electron/simulation.cpp index fbd318e..2d78f67 100644 --- a/src/mc_electron/simulation.cpp +++ b/src/mc_electron/simulation.cpp @@ -223,32 +223,34 @@ void Simulation::initLogger(void) { for (int i = 0; i < numberZBins; i++) { - string fileName = dirname + "/z=" + to_string(zLogValues[i]) + ".dat"; - outStream[i].open(fileName); + 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; - string dirname; + stringstream dirname; struct stat statbuf; int isDir; do { - dirname = poub + "/" + pname + "_" + getTimeString() + "_" + to_string(i++); + dirname.clear(); + dirname << poub << "/" << pname << "_" << getTimeString() << "_" << i++; isDir = 0; - if (stat(dirname.c_str(), &statbuf) != -1) { + if (stat(dirname.str().c_str(), &statbuf) != -1) { if (S_ISDIR(statbuf.st_mode)) { isDir = 1; } } } while ( isDir == 1 ); - if (mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { - cerr<<"Simulation::initLogger: Could not create folder "<