Added myexeption to throw errors and show using MyExeption class.

This commit is contained in:
Christian
2019-03-01 11:12:44 +01:00
parent c0c6bf63a0
commit b19c03f524
10 changed files with 76 additions and 32 deletions
+4 -3
View File
@@ -9,6 +9,7 @@
#include "bedionization.hpp"
#include "myexeption.h"
#include "constants.hpp"
#include <sstream>
@@ -27,7 +28,7 @@ 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 (ss.str());
throw MyException(ss.str());
}
if (T_eV<=B_eV) {
@@ -45,11 +46,11 @@ 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 (ss.str());
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 (ss.str());
throw MyException(ss.str());
}
if ( (T_eV<=B_eV) ||(W_eV >= (T_eV-B_eV) ) ) {
+3 -1
View File
@@ -8,6 +8,8 @@
#include "electron.hpp"
#include "myexeption.h"
using namespace std;
@@ -52,6 +54,6 @@ int Electron::roll_Interaction(vec3 speedT2_ms, ranlib::Uniform<double> *rngUni)
}
}
throw ("Interaction::roll_Interaction: Error in finding Interaction.");
throw MyException("Interaction::roll_Interaction: Error in finding Interaction.");
}
+9 -8
View File
@@ -9,6 +9,7 @@
#include "functions.hpp"
#include "myexeption.h"
#include <sstream>
using namespace std;
@@ -23,11 +24,11 @@ double calc_Ekin_eV_from_speed(double Speed_ms, double mass_eV) {
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 (ss.str());
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 (ss.str());
throw MyException(ss.str());
}
double gamma = calc_gamma_from_beta(beta);
return ( (gamma-1) * mass_eV );
@@ -37,11 +38,11 @@ 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 (ss.str());
throw MyException(ss.str());
} else if (beta<0) {
stringstream ss;
ss<<"calc_gamma_from_beta(): Input beta ="<<beta<<" negative.";
throw (ss.str());
throw MyException(ss.str());
}
return( pow(1 - pow(beta,2),-0.5) ) ;
@@ -51,7 +52,7 @@ 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 (ss.str());
throw MyException(ss.str());
}
double gamma = calc_gamma_from_Ekin_eV(Ekin_eV, mass_eV);
@@ -63,7 +64,7 @@ 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 (ss.str());
throw MyException(ss.str());
}
return (1. + Ekin_eV/mass_eV);
}
@@ -72,7 +73,7 @@ double calc_beta_from_gamma(double gamma) {
if (gamma<1) {
stringstream ss;
ss<<"calc_beta_from_gamma(): Input gamma ="<<gamma<<" smaller 1.";
throw (ss.str());
throw MyException(ss.str());
}
return( sqrt(1 - pow(gamma,-2)) );
}
@@ -112,7 +113,7 @@ endloops:
for (int j = 0;j<3;j++) {
cout<<"phi1c["<<j<<"]="<<phi_new_candidates[j]<<endl;
}
throw ("Interaction::getElaNewDir: phi1 not found.");
throw MyException("Interaction::getElaNewDir: phi1 not found.");
}
return(vec3(sin(theta_new)*cos(phi_new) , sin(theta_new)*sin(phi_new) , cos(theta_new)));
}
+3 -1
View File
@@ -8,6 +8,8 @@
#include "electron.hpp"
#include "myexeption.h"
/*
Ionization:
@@ -34,7 +36,7 @@ Electron Electron::do_Ionization(vec3 speedT2_ms, int newId, ranlib::Uniform<dou
}
if (T_eV < EionT2_eV) {
throw ("Electron::do_Ionization: Ekin_eV < EIonT2_eV.");
throw MyException("Electron::do_Ionization: Ekin_eV < EIonT2_eV.");
}
double W_eV = roll_ionW_eV(T_eV, rngUni);
+5 -3
View File
@@ -9,7 +9,9 @@
#include "katrin.hpp"
#include "myexeption.h"
#include "functions.hpp"
#include "sstream"
using namespace std;
@@ -75,7 +77,7 @@ Electron Katrin::injectElectron(int newId, ranlib::Uniform<double> *rngUni, ranl
Ekin_eV = roll_MBEnergy(MBTemp_K,me_eV, rngNormal);
break;
default:
throw ("Simulation::injectElectron: Invalid Injection Method.");
throw MyException("Simulation::injectElectron: Invalid Injection Method.");
break;
}
vec3 speed_ms = roll_uniform_direction(rngUni) * calc_Speed_from_Ekin_eV(Ekin_eV,me_eV);
@@ -180,7 +182,7 @@ bool Katrin::get_Terminated(Electron termElectron) {
Ekin_eV = termElectron.getKinEnergy_eV();
ETemp_eV = kb_eVK*wgtsTemp_K;
if (ETemp_eV<0) {
throw ("Simulation::checkTermination: ETemp_eV < 0.");
throw MyException("Simulation::checkTermination: ETemp_eV < 0.");
}
if (Ekin_eV<=cutoffEnergy_eV) {
@@ -197,6 +199,6 @@ void Katrin::set_cutoffEnergy_eV() {
} else {
stringstream ss;
ss<<"Mover::Mover(): cutoffMode= "<<cutoffMode<<" not defined.";
throw (ss.str());
throw MyException(ss.str());
}
}
+3 -1
View File
@@ -8,6 +8,8 @@
#include "logger.hpp"
#include "myexeption.h"
#include <fstream>
#include <sys/stat.h>
@@ -101,7 +103,7 @@ string Logger::getDirName() {
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 (ss.str());
throw MyException(ss.str());
}
return(dirname.str());
}
+8 -3
View File
@@ -8,6 +8,8 @@
#include "simulation.hpp"
#include "myexeption.h"
using namespace std;
@@ -18,9 +20,12 @@ int main(int argc, const char * argv[]) {
double duration;
start = clock();
Simulation Sim;
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';
+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 */
+10 -6
View File
@@ -9,6 +9,8 @@
#include "parameter.hpp"
#include "myexeption.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>
@@ -20,6 +22,7 @@ using namespace std;
Parameter::Parameter() {
// Simulation Specs
seed = (int)getValue("seed");
numTotalElectrons = (long int)getValue("numTotalElectrons");
dt_s = getValue("dt_s");
@@ -45,10 +48,11 @@ Parameter::Parameter() {
// Output
numberZBins = getValue ("numberZBins");
}
}
double Parameter::getValue(std::string varName) {
double Parameter::getValue(string varName) {
char* ext = (char*)("cat");
char* str = strdup(varName.c_str());
double temp;
@@ -58,13 +62,13 @@ double Parameter::getValue(std::string varName) {
if (getenv("parpath") == NULL)
{
throw ("Parameter::value: Environment variable parpath not defined.");
throw MyException("Parameter::value: Environment variable parpath not defined.");
}
strcpy(fname, getenv("parpath"));
parfile = getenv("parfile");
if (parfile == NULL)
{
throw ("Parameter::value: Environment variable parfile not defined.");
throw MyException("Parameter::value: Environment variable parfile not defined.");
}
if (!ext)
{
@@ -75,7 +79,7 @@ double Parameter::getValue(std::string varName) {
{
stringstream ss;
ss<<"Parameter::value: Can not open file " <<fname<<".";
throw (ss.str());
throw MyException(ss.str());
}
while (!feof(fcat))
{
@@ -92,7 +96,7 @@ double Parameter::getValue(std::string varName) {
stringstream ss;
ss<<"Parameter::value: Variable "<<str<<" not found.";
throw (ss.str());
throw MyException(ss.str());
return 0;
}
+7 -6
View File
@@ -9,6 +9,7 @@
#include "simulation.hpp"
#include "myexeption.h"
using namespace std;
@@ -52,7 +53,7 @@ void Simulation::doTC6() {
blitz::TinyVector <double,3> loc_m;
while ( (numberElectrons < numTotalElectrons) || (electronQue.size() > 0) ) {
if (!electronQue.empty()) {
throw ("Simulation::doTC6: electronQue should be empty");
throw MyException("Simulation::doTC6: electronQue should be empty");
}
electronQue.push_back(Kat.injectElectron(numberElectrons, &rngUni, &rngNormal));
@@ -69,7 +70,7 @@ void Simulation::doTC6() {
} else {
stringstream ss;
ss<<"Simulation::doTC6(): propagationMethod= "<<propagationMethod<<" not valid.";
throw (ss.str());
throw MyException(ss.str());
}
@@ -78,7 +79,7 @@ void Simulation::doTC6() {
case 0: //Moved Outside
break;
case 1: // Elastic Scattering
throw ("Simulation::doTC6(): Elastic Scattering should be turned off.");
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) );
@@ -86,7 +87,7 @@ void Simulation::doTC6() {
electronQue.pop_front();
break;
default:
throw ("Simulation::doit(): getprocess() returned unknown event");
throw MyException("Simulation::doit(): getprocess() returned unknown event");
break;
}
@@ -115,7 +116,7 @@ void Simulation::move_electron(Electron *moveElectron) {
} else {
stringstream ss;
ss<<"Simulation::doit(): propagationMethod= "<<propagationMethod<<" not valid.";
throw (ss.str());
throw MyException(ss.str());
}
}
@@ -135,7 +136,7 @@ void Simulation::do_interaction(Electron *interactElectron) {
case 3: // Recombination
break;
default:
throw ("Simulation::doit(): getprocess() returned unknown event");
throw MyException("Simulation::doit(): getprocess() returned unknown event");
break;
}
}