322 lines
9.2 KiB
C++
322 lines
9.2 KiB
C++
//
|
|
// base_container.h
|
|
// mc_electron
|
|
//
|
|
// Created by Christian Reiling on 10.05.19.
|
|
// Copyright © 2019 Christian Reiling. All rights reserved.
|
|
//
|
|
|
|
#ifndef base_container_h
|
|
#define base_container_h
|
|
|
|
#include <string>
|
|
#include <map>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <deque>
|
|
#include "blitz/tinyvec2.h"
|
|
#include "blitz/array.h"
|
|
|
|
#include "error_types.h"
|
|
#include "read_system.h"
|
|
#include "constants.h"
|
|
|
|
typedef std::deque<double> ddeque;
|
|
typedef blitz::TinyVector<double,3> vec3;
|
|
typedef std::map<double,double> dim1Map;
|
|
typedef std::map<double,dim1Map> dim2Map;
|
|
typedef std::map<double,dim2Map> dim3Map;
|
|
typedef std::map<double,dim3Map> dim4Map;
|
|
|
|
enum searchStyle {Find, Lower_bound, Lower_bound_Over};
|
|
|
|
|
|
template <class T_Map>
|
|
class Base_Container {
|
|
protected:
|
|
T_Map Map;
|
|
int Dim;
|
|
|
|
public:
|
|
Base_Container() {
|
|
Dim = calc_Dim<T_Map>();
|
|
}
|
|
Base_Container(std::string load_path) {
|
|
Dim = calc_Dim<T_Map>();
|
|
importFromCSV(load_path);
|
|
}
|
|
|
|
protected:
|
|
template <typename T> double calc_Dim();
|
|
private:
|
|
void inc_Count(dim1Map *subMap, int *count) ;
|
|
template <typename T> void inc_Count(std::map<double,T> *subMap, int *count);
|
|
|
|
public:
|
|
void importFromCSV(std::string load_path);
|
|
private:
|
|
void addPointFromLine(const std::string &csv_line);
|
|
|
|
public:
|
|
void addPoint(std::deque<double> pointIn);
|
|
private:
|
|
void rec_addPoint(std::map<double,double> *subMap, std::deque<double> pointIn);
|
|
template <typename T> void rec_addPoint (std::map<double,T> *subMap, std::deque<double> pointIn);
|
|
|
|
public:
|
|
void exportToCSV(std::string export_path) ;
|
|
private:
|
|
std::string getExportFileHeader() ;
|
|
void rec_exportToCSV (const dim1Map &subMap, std::ofstream &exportStream, std::string preset = "") ;
|
|
template <typename T> void rec_exportToCSV(const T &subMap, std::ofstream &exportStream, std::string preset = "") ;
|
|
|
|
protected:
|
|
double get_value(std::deque<double> Points, searchStyle searchMode) ;
|
|
template <typename T_target> T_target* getSubMap(std::deque<double> Points, searchStyle searchMode) ;
|
|
private:
|
|
template <typename T_target> void rec_getSubMap(T_target *subMap, T_target **targetMapPtr, std::deque<double> Points, searchStyle searchMode) ;
|
|
template <typename T_target, typename T> void rec_getSubMap(T *subMap, T_target **targetMapPtr, std::deque<double> Points, searchStyle searchMode) ;
|
|
template <class T> typename T::iterator searchitrByStyle(T *subMap, double x, searchStyle searchMode) ;
|
|
|
|
protected:
|
|
void checkEmpty() ;
|
|
public:
|
|
T_Map getMap() const {return Map;}
|
|
|
|
};
|
|
|
|
|
|
template <class T_Map>
|
|
template <typename T>
|
|
double Base_Container<T_Map>::calc_Dim() {
|
|
T dummyMap;
|
|
int count = 0;
|
|
inc_Count(&dummyMap, &count);
|
|
return count;
|
|
}
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::inc_Count(dim1Map *subMap, int *count) {
|
|
++(*count);
|
|
subMap->insert(std::pair<double,double>(0,0));
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <typename T>
|
|
void Base_Container<T_Map>::inc_Count(std::map<double,T> *subMap, int *count) {
|
|
++(*count);
|
|
T tempMap;
|
|
inc_Count(&tempMap, count);
|
|
subMap->insert(std::pair<double,T>(0,tempMap));
|
|
}
|
|
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::importFromCSV(std::string load_path) {
|
|
std::ifstream importStream(load_path);
|
|
if (!importStream.good()) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::readFromCSV: Could not open file. \n loadFilePath= "<<load_path<<"\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
std::string csv_line;
|
|
while (getline(importStream, csv_line)) {
|
|
remove_leading_whites(csv_line);
|
|
replace_delimiter(csv_line);
|
|
if ( check_ignore_line(csv_line) ) {
|
|
continue;
|
|
}
|
|
addPointFromLine(csv_line);
|
|
}
|
|
importStream.close();
|
|
}
|
|
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::addPointFromLine(const std::string &csv_line) {
|
|
std::stringstream ss;
|
|
ss.str(csv_line);
|
|
std::deque<double> dequeTemp;
|
|
double tempVal;
|
|
for (int i = 0;i<=Dim;i++) {
|
|
if (ss>>tempVal) {
|
|
dequeTemp.push_back(tempVal);
|
|
} else {
|
|
std::cout<<"Base_Container::addPointFromFile: Warning could not read line in inputFile. \nloadFilePath\t"<<csv_line<<"\n";
|
|
}
|
|
}
|
|
addPoint(dequeTemp);
|
|
}
|
|
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::addPoint(std::deque<double> pointIn) {
|
|
if (pointIn.size() != Dim+1) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::addPoint: deque pointIn dimension missmatch.\n";
|
|
for (std::deque<double>::iterator itr = pointIn.begin(); itr!=pointIn.end(); itr++) {
|
|
std::cout<<*itr<<"\n";
|
|
}
|
|
throw MyException(ss.str());
|
|
}
|
|
rec_addPoint(&Map, pointIn);
|
|
}
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::rec_addPoint(dim1Map *subMap, std::deque<double> pointIn) { //Iteration End
|
|
if (pointIn.size()!=2) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::addPoint: Dimension missmatch of pointIn.\nDelta =\t"<<pointIn.size()-2<<"\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
double x = pointIn[0];
|
|
double f = pointIn[1];
|
|
if (subMap->find(x) != subMap->end()) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::addPoint: Dublicate insertion of key.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
subMap->insert(std::pair<double,double>(x, f));
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <typename T>
|
|
void Base_Container<T_Map>::rec_addPoint (std::map<double,T> *subMap, std::deque<double> pointIn) {
|
|
double x = *pointIn.begin();
|
|
pointIn.pop_front();
|
|
if (subMap->find(x) == subMap->end()) { // No such element -> generate new Map and insert
|
|
T tempMap;
|
|
rec_addPoint(&tempMap, pointIn);
|
|
subMap->insert(std::pair<double,T>(x,tempMap));
|
|
} else { // Found such element -> Insert to existing map using iteration
|
|
rec_addPoint(&subMap->find(x)->second, pointIn);
|
|
}
|
|
}
|
|
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::exportToCSV(std::string export_path) {
|
|
checkEmpty();
|
|
std::ofstream exportStream(export_path);
|
|
if (!exportStream.good()) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::exportToCSV: Could not open file.\nexportFilePath\t "<<export_path<<"\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
|
|
exportStream<<getExportFileHeader();
|
|
rec_exportToCSV(Map, exportStream, "");
|
|
exportStream.close();
|
|
}
|
|
|
|
|
|
template <class T_Map>
|
|
std::string Base_Container<T_Map>::getExportFileHeader() {
|
|
std::stringstream ss;
|
|
ss<<"# Automatically generated Data of Function2D_Container class by exportToFile().\n";
|
|
ss<<"# Generated on "<<get_time_string()<<".\n";
|
|
ss<<"#key1 (usually Energy)\tkey2 (angluar distribution or secondary energy)\tparameter (usually SDCS)\n";
|
|
return ss.str();
|
|
}
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::rec_exportToCSV(const dim1Map &subMap, std::ofstream &exportStream, std::string preset) {
|
|
for (dim1Map::const_iterator itr = subMap.cbegin();itr!=subMap.cend();itr++) {
|
|
exportStream<<preset<<itr->first<<"\t"<<itr->second<<"\n";
|
|
}
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <typename T>
|
|
void Base_Container<T_Map>::rec_exportToCSV(const T &subMap, std::ofstream &exportStream, std::string preset) {
|
|
std::stringstream ss;
|
|
for (typename T::const_iterator itr = subMap.cbegin();itr!=subMap.cend();itr++) {
|
|
ss.clear();
|
|
ss.str("");
|
|
ss<<preset<<itr->first<<"\t";
|
|
rec_exportToCSV(itr->second, exportStream, ss.str());
|
|
}
|
|
}
|
|
|
|
|
|
template <class T_Map>
|
|
double Base_Container<T_Map>::get_value(std::deque<double> Points, searchStyle searchMode) {
|
|
double x = Points.back();
|
|
Points.pop_back();
|
|
dim1Map *subMap = getSubMap<dim1Map>(Points, searchMode);
|
|
dim1Map::const_iterator itr = searchitrByStyle(subMap, x, searchMode);
|
|
return itr->second;
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <typename T_target>
|
|
T_target* Base_Container<T_Map>::getSubMap(std::deque<double> Points, searchStyle searchMode) {
|
|
checkEmpty();
|
|
double T_Dim = calc_Dim<T_target>();
|
|
if (Points.size() != Dim - T_Dim) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::getSubMap: Dimension Missmatch of Points\n Missmatch =\t<< "<<Points.size() - (Dim - T_Dim)<<"\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
T_target *targetMap;
|
|
rec_getSubMap(&Map, &targetMap, Points, searchMode);
|
|
return targetMap;
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <typename T_target>
|
|
void Base_Container<T_Map>::rec_getSubMap(T_target *subMap, T_target **targetMapPtr, std::deque<double> Points, searchStyle searchMode) {
|
|
if (Points.size() != 0) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::get_subitr: Dimension Missmatch of Points\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
*targetMapPtr = subMap;
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <typename T_target, typename T>
|
|
void Base_Container<T_Map>::rec_getSubMap(T *subMap, T_target **targetMapPtr, std::deque<double> Points, searchStyle searchMode) {
|
|
double x = Points.front();
|
|
Points.pop_front();
|
|
typename T::iterator itr = searchitrByStyle(subMap, x, searchMode);
|
|
rec_getSubMap(&(itr->second), targetMapPtr, Points, searchMode);
|
|
}
|
|
|
|
template <class T_Map>
|
|
template <class T>
|
|
typename T::iterator Base_Container<T_Map>::searchitrByStyle(T *subMap, double x, searchStyle searchMode) {
|
|
typename T::iterator itr;
|
|
if (searchMode == Find) {
|
|
itr = subMap->find(x);
|
|
} else if (searchMode == Lower_bound) {
|
|
itr = subMap->lower_bound(x);
|
|
} else if (searchMode == Lower_bound_Over) {
|
|
itr = subMap->lower_bound(x);
|
|
if (itr == subMap->end()) {
|
|
--itr;
|
|
}
|
|
}
|
|
if (itr == subMap->end()) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Container::calcitr: Point is out of bound.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
return itr;
|
|
}
|
|
|
|
|
|
|
|
template <class T_Map>
|
|
void Base_Container<T_Map>::checkEmpty() {
|
|
if (Map.empty()) {
|
|
std::stringstream ss;
|
|
ss<<"Base_Conainer::checkEmpty: Map is empty.\n";
|
|
throw MyException(ss.str());
|
|
}
|
|
}
|
|
|
|
|
|
#endif /* base_container_h */
|