Files
katrin-plasma/src/mc_electron/read_system.h
T
Christian 578d673b65 Moved serial mode to target mcelecron_serial.
Implemented parallelization with mpi.
2019-07-18 14:02:19 +02:00

208 lines
5.6 KiB
C++

//
// read_system.h
// mc_electron
//
// Created by Christian Reiling on 30.04.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef read_system_h
#define read_system_h
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <sys/stat.h>
#include "error_types.h"
inline double get_parameter_value(std::string csv_line) {
std::stringstream ss;
ss.str(csv_line);
std::string dummy;
double parameter_value = NAN;
if ( !(ss>>dummy && ss>>parameter_value) ) {
std::stringstream ss_err;
ss_err<<"get_parameter_value: Could not read line.\n";
throw MyException(ss_err.str());
}
return parameter_value;
}
inline bool is_parameter_line(std::string csv_line, const std::string &parameter_name) {
std::stringstream ss;
ss.str(csv_line);
std::string temp;
if ( !(ss>>temp) ) {
std::stringstream ss_err;
ss_err<<"is_parameter_line: Could not read line.\n";
throw MyException(ss_err.str());
}
if (temp == parameter_name) {
return true;
} else {
return false;
}
}
inline bool is_faulty(std::string csv_line) {
std::stringstream ss;
ss.str(csv_line);
std::string name_dummy;
double value_dummy;
if (ss>>name_dummy && ss>>value_dummy) {
return false;
} else {
return true;
}
}
inline bool check_ignore_line(const std::string &csv_line) { // true if line is empty or starts with #
if (csv_line[0] =='#') {
return true;
} else if ( csv_line.empty() ) {
return true;
} else {
return false;
}
}
inline void replace_delimiter(std::string &csv_line) { // replaces delimiter (';'',''=') with whitespace
replace(csv_line.begin(), csv_line.end(), ';', ' ');
replace(csv_line.begin(), csv_line.end(), ',', ' ');
replace(csv_line.begin(), csv_line.end(), '=', ' ');
}
inline void remove_leading_whites(std::string &csv_line) {
static const std::string whitespaces = " \n\r\t\f\v";
size_t start = csv_line.find_first_not_of(whitespaces);
csv_line = (start == std::string::npos) ? "" : csv_line.substr(start);
}
inline double find_parameter(std::ifstream *parameter_file_stream, const std::string &parameter_name) {
std::string csv_line;
double parameter_value = NAN;
while (getline(*parameter_file_stream, csv_line)) {
remove_leading_whites(csv_line);
replace_delimiter(csv_line);
if ( check_ignore_line(csv_line) ) {
continue;
}
if ( is_faulty(csv_line) ) {
std::cout<<"Line in parameter file faulty.\n\tline:\t"<<csv_line<<"\n";
} else if ( is_parameter_line(csv_line, parameter_name) ) {
parameter_value = get_parameter_value(csv_line);
break;
}
}
return parameter_value;
}
inline std::string make_path (const std::string &path) { // append '/' if necessary to path.
if (path.back() != '/') {
return path + '/';
} else {
return path;
}
}
inline std::string get_enviroment_variable (const std::string &envName) { // string version of C function getenv.
char * val = getenv( envName.c_str() );
return val == NULL ? std::string("") : std::string(val);
}
inline std::string get_parameter_file_path() {
std::string parameter_file_path = get_enviroment_variable("parpath");
if (parameter_file_path == "") {
throw MyException("Parameter::getParameter: Environment variable parpath not defined.");
}
std::string parameter_file_name = get_enviroment_variable("parfile");
if (parameter_file_name == "") {
throw MyException("Parameter::getParameter: Environment variable parfile not defined.");
}
parameter_file_path = make_path(parameter_file_path);
return parameter_file_path + parameter_file_name;
}
inline double load_parameter(const std::string &parameter_name) {
std::string parameter_file_path = get_parameter_file_path();
std::ifstream parameter_file_stream(parameter_file_path);
if (!parameter_file_stream.good()) {
std::stringstream ss;
ss<<"Parameter::getParameter: Could not open file.\n loadFilePath:\t"<<parameter_file_path<<"\n";
throw MyException(ss.str());
}
double parameter_value = find_parameter(&parameter_file_stream, parameter_name);
if ( isnan(parameter_value) ) {
std::stringstream ss;
ss<<"Parameter::getParameter: Parameter not found in file.\n parameter_name:\t"<<parameter_name<<"\n loadFilePath: \t"<<parameter_file_path;
throw MyException(ss.str());
} else {
return parameter_value;
}
}
inline std::string get_time_string() {
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
std::string str(buffer);
return str;
}
inline std::string get_homebase() {
std::string homebase = get_enviroment_variable("HOMEBASE");
if (homebase == "") {
std::stringstream ss;
ss<<"HOMEBASE not defined!\n.";
throw MyException (ss.str());
}
homebase = make_path(homebase);
return homebase;
}
inline std::string get_data_path() {
return get_homebase() + "src/mc_electron/data/";
}
inline std::string remove_cat (std::string cat_string) {
if ( cat_string.substr(cat_string.size()-4, 4) == ".cat" ){
return cat_string.substr(0, cat_string.size()-4);
} else {
return cat_string;
}
}
inline std::string get_parameter_filenamepath() {
std::string parameter_file_path = getenv("parpath");
std::string parameter_file_name = remove_cat(getenv("parfile"));
return parameter_file_path + "/" + parameter_file_name;
}
inline bool is_file(std::string file_path) {
struct stat buffer;
return stat (file_path.c_str(), &buffer) == 0;
}
inline bool is_dir(std::string dir_path) {
struct stat info;
if( stat( dir_path.c_str(), &info ) != 0 )
return false;
else if( info.st_mode & S_IFDIR ) // S_ISDIR() doesn't exist on my windows
return true;
else
return false;
}
#endif /* read_system_h */