Major changes:
- Moved to array operations for main loops. - Changed to setting pointers to operator only once before loop. - Changed Population Containers - Added Recombination - Added Inelastic Scattering Kinematics - Reworked Elastic Scattering Cross Section - Reworked Elastic Scattering Kinematics - Reworked organisation of different particle species and corresponding operators
This commit is contained in:
@@ -0,0 +1,328 @@
|
||||
//
|
||||
// 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:
|
||||
std::ifstream createImportStream(const std::string &load_path) ;
|
||||
void addPointFromLine(const std::string &lineFromFile);
|
||||
|
||||
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 exportPath) ;
|
||||
private:
|
||||
std::ofstream createExportStream(const std::string &exportPath) ;
|
||||
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 = createImportStream(load_path);
|
||||
std::string lineFromFile;
|
||||
while (getline(importStream, lineFromFile)) {
|
||||
removeLeadingWhites(lineFromFile);
|
||||
replaceDelimiter(lineFromFile);
|
||||
if ( checkIgnoreLine(lineFromFile) ) {
|
||||
continue;
|
||||
}
|
||||
addPointFromLine(lineFromFile);
|
||||
}
|
||||
importStream.close();
|
||||
}
|
||||
|
||||
template <class T_Map>
|
||||
std::ifstream Base_Container<T_Map>::createImportStream(const 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());
|
||||
}
|
||||
return(importStream);
|
||||
}
|
||||
|
||||
template <class T_Map>
|
||||
void Base_Container<T_Map>::addPointFromLine(const std::string &lineFromFile) {
|
||||
std::stringstream ss;
|
||||
ss.str(lineFromFile);
|
||||
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"<<lineFromFile<<"\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";
|
||||
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 exportPath) {
|
||||
checkEmpty();
|
||||
std::ofstream exportStream = createExportStream(exportPath);
|
||||
exportStream<<getExportFileHeader();
|
||||
rec_exportToCSV(Map, exportStream, "");
|
||||
exportStream.close();
|
||||
}
|
||||
|
||||
template <class T_Map>
|
||||
std::ofstream Base_Container<T_Map>::createExportStream(const std::string &exportPath) {
|
||||
std::ofstream exportStream(exportPath);
|
||||
if (!exportStream.good()) {
|
||||
std::stringstream ss;
|
||||
ss<<"Base_Container::exportToCSV: Could not open file.\nexportFilePath\t "<<exportPath<<"\n";
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
return(exportStream);
|
||||
}
|
||||
|
||||
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 "<<getTimeString()<<".\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 */
|
||||
Reference in New Issue
Block a user