Merged function containers. Added HDF5 output and reworked Logger class into Base class and 2 derived classes ASCI_Logger and H5_Logger
246 lines
6.9 KiB
C++
246 lines
6.9 KiB
C++
//
|
|
// functions.cpp
|
|
// mc_electron
|
|
//
|
|
// Created by Christian Reiling on 07.02.19.
|
|
// Copyright © 2019 Christian Reiling. All rights reserved.
|
|
//
|
|
|
|
|
|
#include "functions.hpp"
|
|
|
|
#include "constants.hpp"
|
|
|
|
#include <sstream>
|
|
#include <fstream>
|
|
|
|
using namespace std;
|
|
|
|
// 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/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.";
|
|
throw MyException(ss.str());
|
|
} else if (beta<0) {
|
|
stringstream ss;
|
|
ss<<"get_Ekin_eV_from_speed(): Input speed v="<<Speed_ms<<" m/s is expected to be absolute value therfore >0.";
|
|
throw MyException(ss.str());
|
|
}
|
|
double gamma = get_gamma_from_beta(beta);
|
|
return ( (gamma-1) * mass_eV );
|
|
}
|
|
|
|
double get_gamma_from_beta(double beta) {
|
|
if (beta>=1) {
|
|
stringstream ss;
|
|
ss<<"get_gamma_from_beta(): Input beta ="<<beta<<" equal or exeeds 1.";
|
|
throw MyException(ss.str());
|
|
} else if (beta<0) {
|
|
stringstream ss;
|
|
ss<<"get_gamma_from_beta(): Input beta ="<<beta<<" negative.";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
return( pow(1 - pow(beta,2),-0.5) ) ;
|
|
}
|
|
|
|
double get_Speed_from_Ekin_eV(double Ekin_eV, double mass_eV) {
|
|
if (Ekin_eV<0) {
|
|
stringstream ss;
|
|
ss<<"get_Speed_from_Ekin_eV(): Input Ekin_eV ="<<Ekin_eV<<" negative.";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
double gamma = get_gamma_from_Ekin_eV(Ekin_eV, mass_eV);
|
|
double beta = get_beta_from_gamma(gamma);
|
|
return(beta * c_ms);
|
|
}
|
|
|
|
double get_gamma_from_Ekin_eV(double Ekin_eV, double mass_eV) {
|
|
if (Ekin_eV<0) {
|
|
stringstream ss;
|
|
ss<<"get_gamma_from_Ekin_eV(): Input Ekin_eV ="<<Ekin_eV<<" negative.";
|
|
throw MyException(ss.str());
|
|
}
|
|
return (1. + Ekin_eV/mass_eV);
|
|
}
|
|
|
|
double get_beta_from_gamma(double gamma) {
|
|
if (gamma<1) {
|
|
stringstream ss;
|
|
ss<<"get_beta_from_gamma(): Input gamma ="<<gamma<<" smaller 1.";
|
|
throw MyException(ss.str());
|
|
}
|
|
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));
|
|
double phi_old = atan2(direction_old[1],direction_old[0]);
|
|
|
|
double A3 = sin(theta_change)*cos(phi_change);
|
|
double A4 = A3*cos(theta_old) + cos(theta_change)*sin(theta_old);
|
|
double A5 = sin(theta_change)*sin(phi_change);
|
|
double A11 = A4*cos(phi_old) - A5*sin(phi_old);
|
|
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. 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*pi - phi_new_candidates[0];
|
|
phi_new_candidates[2] = asin ( roundf ( A21/sin(theta_new) *fac)/fac );
|
|
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];
|
|
}
|
|
}
|
|
}
|
|
if (phi_new == -1) {
|
|
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*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 = kb_eVK*Temp_K/mass_eV;
|
|
double beta = 0;
|
|
double sum = 0;
|
|
for (int i=0;i<3;i++) {
|
|
sum += pow(rngNormal->random(),2);
|
|
}
|
|
beta = sqrt(C*(sum));
|
|
|
|
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;
|
|
do {
|
|
eta1 = rngUni->random();
|
|
eta2 = rngUni->random();
|
|
xcan = xmin + eta1*(xmax - xmin);
|
|
} while( eta2*pmax > pdf(xcan) );
|
|
return(xcan);
|
|
}
|
|
|