Restructuring Code. Removed some small classes which provided only 1 function, like parameter. Now each class calls it own parameters and stores them as static const.
Merged function containers. Added HDF5 output and reworked Logger class into Base class and 2 derived classes ASCI_Logger and H5_Logger
This commit is contained in:
+109
-19
@@ -9,20 +9,108 @@
|
||||
|
||||
#include "functions.hpp"
|
||||
|
||||
#include "myexeption.h"
|
||||
#include "constants.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
double get_vecAbs(vec3 vec) {
|
||||
return( sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2)) );
|
||||
// Read Parameters from file
|
||||
double getParameter(string parName) {
|
||||
string parpath = getEnv("parpath");
|
||||
string parfile = getEnv("parfile");
|
||||
if (parpath == "") {
|
||||
throw MyException("Parameter::getParameter: Environment variable parpath not defined.");
|
||||
}
|
||||
if (parfile == "") {
|
||||
throw MyException("Parameter::getParameter: Environment variable parfile not defined.");
|
||||
}
|
||||
makePath(parpath);
|
||||
string loadFilePath = parpath + parfile;
|
||||
ifstream fileStream(loadFilePath);
|
||||
|
||||
if (!fileStream.good()) {
|
||||
stringstream ss;
|
||||
ss<<"Parameter::getParameter: Could not open file.\n loadFilePath =\t"<<loadFilePath<<endl;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
|
||||
string lineFromFile;
|
||||
stringstream ss;
|
||||
string parNameTemp;
|
||||
double parTemp;
|
||||
bool lineReadError = false;
|
||||
|
||||
while (getline(fileStream, lineFromFile)) {
|
||||
removeLeadingWhites(lineFromFile);
|
||||
replaceDelimiter(lineFromFile);
|
||||
if ( checkIgnoreLine(lineFromFile) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ss.clear();
|
||||
ss.str(lineFromFile);
|
||||
|
||||
if (ss>>parNameTemp && ss>>parTemp) {
|
||||
if (parNameTemp == parName) {
|
||||
fileStream.close();
|
||||
return (parTemp);
|
||||
}
|
||||
} else {
|
||||
if (!lineReadError) {
|
||||
cout<<"Parameter::getParameter: Line in parameter file faulty. \n path =\t"<<loadFilePath<<endl;
|
||||
lineReadError = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ss.clear();
|
||||
ss<<"Parameter::getParameter: Parameter not found in file.\n parName =\t"<<parName<<"\n loadFilePath = \t"<<loadFilePath;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
|
||||
|
||||
//String Manipulations for Read in from files
|
||||
std::string getEnv (std::string envName) {
|
||||
char * val = getenv( envName.c_str() );
|
||||
return val == NULL ? std::string("") : std::string(val);
|
||||
}
|
||||
|
||||
void makePath (std::string &path) {
|
||||
if (path.back() != '/') {
|
||||
path = path + '/';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
bool checkIgnoreLine(std::string lineFromFile) {
|
||||
if (lineFromFile[0] =='#') {
|
||||
return true;
|
||||
} else if ( lineFromFile.empty() ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
void removeLeadingWhites(std::string &lineFromFile) {
|
||||
static const std::string WHITESPACE = " \n\r\t\f\v";
|
||||
size_t start = lineFromFile.find_first_not_of(WHITESPACE);
|
||||
lineFromFile = (start == std::string::npos) ? "" : lineFromFile.substr(start);
|
||||
return;
|
||||
}
|
||||
|
||||
void replaceDelimiter(std::string &lineFromFile) {
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), ';', ' ');
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), ',', ' ');
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), '=', ' ');
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate Kinematic Relations
|
||||
double get_Ekin_eV_from_speed(double Speed_ms, double mass_eV) {
|
||||
double beta = Speed_ms/Constants::c_ms;
|
||||
double beta = Speed_ms/c_ms;
|
||||
if (beta>=1) {
|
||||
stringstream ss;
|
||||
ss<<"get_Ekin_eV_from_speed(): Input speed v="<<Speed_ms<<" m/s equal or exeeds speed of light.";
|
||||
@@ -59,7 +147,7 @@ double get_Speed_from_Ekin_eV(double Ekin_eV, double mass_eV) {
|
||||
|
||||
double gamma = get_gamma_from_Ekin_eV(Ekin_eV, mass_eV);
|
||||
double beta = get_beta_from_gamma(gamma);
|
||||
return(beta * Constants::c_ms);
|
||||
return(beta * c_ms);
|
||||
}
|
||||
|
||||
double get_gamma_from_Ekin_eV(double Ekin_eV, double mass_eV) {
|
||||
@@ -80,6 +168,12 @@ double get_beta_from_gamma(double gamma) {
|
||||
return( sqrt(1 - pow(gamma,-2)) );
|
||||
}
|
||||
|
||||
|
||||
// Operations on 3 dimensional Vectors
|
||||
double get_vecAbs(vec3 vec) {
|
||||
return( sqrt( pow(vec[0],2) + pow(vec[1],2) + pow(vec[2],2)) );
|
||||
}
|
||||
|
||||
vec3 get_Newdirection(vec3 direction_old, double theta_change,double phi_change) {
|
||||
// Transformation of direction following Plante and Cucinotta
|
||||
double theta_old = acos(direction_old[2]/get_vecAbs(direction_old));
|
||||
@@ -92,45 +186,42 @@ vec3 get_Newdirection(vec3 direction_old, double theta_change,double phi_change)
|
||||
double A21 = A4*sin(phi_old) + A5*cos(phi_old);
|
||||
double theta_new = acos( cos(theta_old)*cos(theta_change)-A3*sin(theta_old) );
|
||||
|
||||
// Get right phi1, maybe more elegant way later.
|
||||
// Get right phi1, maybe more elegant way later. Problem comes from reverse functions only being on range of pi. So combine both statements to get proper phi over 2pi.
|
||||
int digits = 6;
|
||||
double fac = pow(10,digits);
|
||||
double phi_new = -1;
|
||||
double phi_new_candidates [4];
|
||||
phi_new_candidates[0] = acos( roundf ( A11/sin(theta_new) *fac)/fac );
|
||||
phi_new_candidates[1] = 2*Constants::pi - phi_new_candidates[0];
|
||||
phi_new_candidates[1] = 2*pi - phi_new_candidates[0];
|
||||
phi_new_candidates[2] = asin ( roundf ( A21/sin(theta_new) *fac)/fac );
|
||||
phi_new_candidates[3] = Constants::pi - phi_new_candidates[2];
|
||||
phi_new_candidates[2] = (phi_new_candidates[2] > 0) ? phi_new_candidates[2] : 2*Constants::pi + phi_new_candidates[2];
|
||||
phi_new_candidates[3] = pi - phi_new_candidates[2];
|
||||
phi_new_candidates[2] = (phi_new_candidates[2] > 0) ? phi_new_candidates[2] : 2*pi + phi_new_candidates[2];
|
||||
for (int i=0;i<3;i++) {
|
||||
for (int j=i+1;j<4;j++) {
|
||||
if (phi_new_candidates[i]-phi_new_candidates[j] < 0.0001) {
|
||||
phi_new = phi_new_candidates[i];
|
||||
goto endloops;
|
||||
}
|
||||
}
|
||||
}
|
||||
endloops:
|
||||
if (phi_new == -1) {
|
||||
for (int j = 0;j<3;j++) {
|
||||
cout<<"phi1c["<<j<<"]="<<phi_new_candidates[j]<<endl;
|
||||
}
|
||||
throw MyException("Interaction::getElaNewDir: phi1 not found.");
|
||||
}
|
||||
return(vec3(sin(theta_new)*cos(phi_new) , sin(theta_new)*sin(phi_new) , cos(theta_new)));
|
||||
}
|
||||
|
||||
|
||||
// Functions for Random Sampling
|
||||
vec3 roll_uniform_direction(ranlib::Uniform<double> *rngUni) {
|
||||
vec3 dir;
|
||||
double phi = 2*Constants::pi*rngUni->random();
|
||||
double theta = Constants::pi*rngUni->random();
|
||||
double phi = 2*pi*rngUni->random();
|
||||
double theta = pi*rngUni->random();
|
||||
dir = sin(theta)*cos(phi) , sin(theta)*sin(phi) , cos(theta);
|
||||
return(dir);
|
||||
}
|
||||
|
||||
double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit<double> *rngNormal) {
|
||||
|
||||
double C = Constants::kb_eVK*Temp_K/mass_eV;
|
||||
double C = kb_eVK*Temp_K/mass_eV;
|
||||
double beta = 0;
|
||||
double sum = 0;
|
||||
for (int i=0;i<3;i++) {
|
||||
@@ -141,7 +232,6 @@ double roll_MBEnergy(double Temp_K,double mass_eV,ranlib::NormalUnit<double> *rn
|
||||
return((get_gamma_from_beta(beta)-1)*mass_eV);
|
||||
}
|
||||
|
||||
|
||||
double roll_Rejectionmethod(double (*pdf)(double), double xmin, double xmax, double pmax, ranlib::Uniform<double> *rngUni) {
|
||||
// get Energy distribution using Rejection Method
|
||||
double xcan,eta1,eta2;
|
||||
|
||||
Reference in New Issue
Block a user