// // 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 #include 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"<>parNameTemp && ss>>parTemp) { if (parNameTemp == parName) { fileStream.close(); return (parTemp); } } else { if (!lineReadError) { cout<<"Parameter::getParameter: Line in parameter file faulty. \n path =\t"<=1) { stringstream ss; ss<<"get_Ekin_eV_from_speed(): Input speed v="<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 ="< 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 *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 *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 *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); }