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

132 lines
3.8 KiB
C++

//
// stacked_container.h
// mc_electron
//
// Created by Christian Reiling on 10.05.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef stacked_container_h
#define stacked_container_h
#include "average_container.h"
#include "random_operations.h"
template <class T_Map>
class Stacked_Container : public Base_Container<T_Map> {
double lowerEdge = 0;
public:
Stacked_Container() : Base_Container<T_Map>() {} ;
Stacked_Container(const double &lowerEdge, std::string load_path) : Base_Container<T_Map>(load_path) {
this->lowerEdge = lowerEdge;
}
Stacked_Container(const double &lowerEdge, const Average_Container<T_Map> &averageContainer) {
this->lowerEdge = lowerEdge;
calcMap(averageContainer);
}
public:
void calcMap(const Average_Container<T_Map> &averageContainer, const double &lowerEdge);
private:
void rec_calcMap(const dim1Map &subMap, std::deque<double> Points);
template <typename T> void rec_calcMap(const T &subMapm, std::deque<double> Points);
double calc_tot(const dim1Map &subMap);
public:
double sample(const std::deque<double> &Points, UniRNG *rngUni);
};
template <class T_Map>
void Stacked_Container<T_Map>::calcMap(const Average_Container<T_Map> &averageContainer, const double &lowerEdge) {
this->lowerEdge = lowerEdge;
T_Map averageMap = averageContainer.getMap();
if (averageMap.empty()) {
std::stringstream ss;
ss<<"Stacked_Container::calcMap: Input Average_Container empty.\n";
throw MyException(ss.str());
}
Base_Container<T_Map>::Map.clear();
std::deque<double> Points;
rec_calcMap(averageMap, Points);
}
template <class T_Map>
void Stacked_Container<T_Map>::rec_calcMap(const dim1Map &subMap, std::deque<double> Points) {
double tot = calc_tot(subMap);
double curVal, width, heigth;
// first value manually, because of lowerEdge
width = subMap.cbegin()->first - lowerEdge;
heigth = subMap.cbegin()->second/tot;
curVal = width * heigth;
Points.push_back(curVal);
Points.push_back(subMap.cbegin()->first);
Base_Container<T_Map>::addPoint(Points);
Points.pop_back();
Points.pop_back();
dim1Map::const_iterator preitr;
for (dim1Map::const_iterator curitr = (++subMap.cbegin()); curitr!=subMap.cend(); curitr++) {
preitr = curitr;
--preitr;
width = curitr->first - preitr->first;
heigth = curitr->second/tot;
curVal += width*heigth;
Points.push_back(curVal);
Points.push_back(curitr->first);
Base_Container<T_Map>::addPoint(Points);
Points.pop_back();
Points.pop_back();
}
}
template <class T_Map>
template <typename T>
void Stacked_Container<T_Map>::rec_calcMap(const T &subMap, std::deque<double> Points) {
for (typename T::const_iterator itr = subMap.cbegin(); itr!= subMap.cend(); itr++) {
Points.push_back(itr->first);
rec_calcMap(itr->second, Points);
Points.pop_back();
}
}
template <class T_Map>
double Stacked_Container<T_Map>::calc_tot(const dim1Map &subMap) {
double tot = 0;
// insert first value manually
double width, heigth;
width = subMap.begin()->first - lowerEdge;
heigth = subMap.begin()->second;
tot += width*heigth;
dim1Map::const_iterator preitr;
for (dim1Map::const_iterator curitr = (++subMap.cbegin()); curitr!=subMap.cend(); curitr++) {
preitr = curitr;
--preitr;
width = curitr->first - preitr->first;
heigth = curitr->second;
tot += width*heigth;
}
return tot;
}
template <class T_Map>
double Stacked_Container<T_Map>::sample(const std::deque<double> &Points, UniRNG *rngUni) {
Base_Container<T_Map>::checkEmpty();
if (Points.size() != Base_Container<T_Map>::Dim - 1) {
std::stringstream ss;
ss<<"Stacked_Container::sample: deque Point dimension missmatch.\n";
throw MyException(ss.str());
}
dim1Map *endMap = Base_Container<T_Map>::template getSubMap<dim1Map>(Points, Lower_bound_Over);
return endMap->lower_bound(rngUni->random())->second;
}
#endif /* stacked_container_h */