Reworked Container structure and wrote template for n-dim Containers.
This commit is contained in:
@@ -8,194 +8,182 @@
|
||||
|
||||
#include "function_container.hpp"
|
||||
|
||||
#include "functions.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Func_Container::Func_Container(std::string loadFilePath) {
|
||||
readFromCSV(loadFilePath);
|
||||
inline void Base_FuncContainer::averageMap_from_edgeMap (dim1Map *averageMap, dim1Map &edgeMap) {
|
||||
averageMap->clear();
|
||||
dim1Map::iterator curItr, afterItr;
|
||||
afterItr = edgeMap.begin();
|
||||
curItr = edgeMap.begin();
|
||||
afterItr++;
|
||||
while (afterItr!=edgeMap.end()) {
|
||||
averageMap->insert(dim1Pair( curItr->first + (afterItr->first-curItr->first)/2, curItr->second ));
|
||||
curItr++;
|
||||
afterItr++;
|
||||
}
|
||||
averageMap->insert(dim1Pair( curItr->first , curItr->second ));
|
||||
}
|
||||
|
||||
double Func_Container::interpolate(double x) {
|
||||
if (Map.empty()) {
|
||||
throw MyException("Func_Container: Container is empty.");
|
||||
inline void Base_FuncContainer::stackedMap_from_averageMap (dim1Map *stackedMap, dim1Map &averageMap, double lowerEdge) {
|
||||
double tot = 0;
|
||||
dim1Map::iterator prevItr, currItr;
|
||||
prevItr = averageMap.begin();
|
||||
currItr = averageMap.begin();
|
||||
tot += (currItr->first - lowerEdge) * currItr->second;
|
||||
currItr ++;
|
||||
while (currItr!=averageMap.end()) {
|
||||
tot += (currItr->first - prevItr->first) * currItr->second;
|
||||
currItr++;
|
||||
prevItr++;
|
||||
}
|
||||
//tot = tot/(averageMap.rbegin()->first - lowerEdge);
|
||||
|
||||
if (Map.begin() == Map.lower_bound(x) ) {
|
||||
return (Map.begin()->second);
|
||||
} else if (Map.end() == Map.lower_bound(x)){
|
||||
return ( (--Map.end())->second );
|
||||
stackedMap->clear();
|
||||
prevItr = averageMap.begin();
|
||||
currItr = averageMap.begin();
|
||||
double curVal = (currItr->first - lowerEdge)* currItr->second/tot;
|
||||
container_base::add_To_Map(stackedMap, deque<double> {curVal,currItr->first});
|
||||
currItr ++;
|
||||
while (currItr!=averageMap.end()) {
|
||||
curVal += (currItr->first - prevItr->first)*currItr->second/tot;
|
||||
container_base::add_To_Map(stackedMap, deque<double> {curVal,currItr->first});
|
||||
currItr++;
|
||||
prevItr++;
|
||||
}
|
||||
|
||||
dim1Map::iterator lowAnker_Itr = --Map.lower_bound(x);
|
||||
dim1Map::iterator upAnker_Itr = Map.lower_bound(x);
|
||||
double lowAnker_x = lowAnker_Itr->first;
|
||||
double lowAnker_fx = lowAnker_Itr->second;
|
||||
double upAnker_x = upAnker_Itr->first;
|
||||
double upAnker_fx = upAnker_Itr->second;
|
||||
|
||||
double res = lowAnker_fx + (x-lowAnker_x)/(upAnker_x-lowAnker_x) * (upAnker_fx-lowAnker_fx);
|
||||
return (res);
|
||||
}
|
||||
|
||||
inline double Base_FuncContainer::sampleValue_from_stackedMap (dim1Map *stackedMap, ranlib::Uniform<double> *rngUni) {
|
||||
return (stackedMap->lower_bound(rngUni->random())->second);
|
||||
}
|
||||
|
||||
// Base1D_Container
|
||||
// AddData
|
||||
void Base1D_FuncContainer::fillFromFunction(double (*f)(double), double minKey, double maxKey, int numpointsPerOrder) {
|
||||
int interval = numpointsPerOrder -1;
|
||||
int minI = floor(log10(minKey)*interval);
|
||||
int maxI = ceil(log10(maxKey)*interval);
|
||||
double x, fx;
|
||||
try {
|
||||
x = pow(10,double (minI)/double(interval));
|
||||
fx = f(x);
|
||||
} catch (...) {
|
||||
fx = f(x);
|
||||
}
|
||||
add_ankerpoint(x, fx);
|
||||
try {
|
||||
x = pow(10,double (maxI)/double(interval));
|
||||
fx = f(x);
|
||||
} catch (...) {
|
||||
fx = f(x);
|
||||
}
|
||||
add_ankerpoint(x, fx);
|
||||
for (int i = minI + 1;i<maxI;i++) {
|
||||
x = pow(10,double (i)/double(interval));
|
||||
fx = f(x);
|
||||
add_ankerpoint(x, fx);
|
||||
}
|
||||
}
|
||||
|
||||
// average1D_Container
|
||||
void Average1D_FuncContainer::set_Map(dim1Map &edgeMap) {
|
||||
averageMap_from_edgeMap (&Map, edgeMap);
|
||||
}
|
||||
|
||||
void Average1D_FuncContainer::set_Map(Edge1D_FuncContainer &edgeContainer) {
|
||||
dim1Map edgeMap = edgeContainer.get_Map();
|
||||
set_Map(edgeMap);
|
||||
}
|
||||
|
||||
|
||||
void Func_Container::add_ankerpoint(double x, double fx) {
|
||||
if ( Map.find(x)!=Map.end() ) {
|
||||
stringstream ss;
|
||||
ss<<"Func_Container::add_ankerpoint: Key x="<<x<<" already exists.";
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
Map.insert(dim1Pair(x, fx));
|
||||
return;
|
||||
}
|
||||
|
||||
void Func_Container::readFromCSV(std::string loadFilePath) {
|
||||
ifstream fileStream(loadFilePath);
|
||||
|
||||
if (!fileStream.good()) {
|
||||
stringstream ss;
|
||||
ss<<"Func_Container::readFromCSV: Could not open file. \n loadFilePath= "<<loadFilePath<<endl;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
// edge2D_Container
|
||||
|
||||
string lineFromFile;
|
||||
stringstream ss;
|
||||
double xTemp, fxTemp;
|
||||
bool lineReadError = false;
|
||||
void Edge2D_FuncContainer::get_Ionization_fromFunction(double (*f)(double,double), double minX, double maxX, int numpointsPerXOrder, int numYpoints) {
|
||||
int Xinterval = numpointsPerXOrder -1;
|
||||
int minXI = floor(log10(minX)*Xinterval);
|
||||
int maxXI = ceil(log10(maxX)*Xinterval);
|
||||
|
||||
while (getline(fileStream, lineFromFile)) {
|
||||
removeLeadingWhites(lineFromFile);
|
||||
replaceDelimiter(lineFromFile);
|
||||
if ( checkIgnoreLine(lineFromFile) ) {
|
||||
int Yinterval = numYpoints-1;
|
||||
double minY, maxY;
|
||||
int minYI, maxYI;
|
||||
double x, y, fxy;
|
||||
|
||||
for (int XI = minXI; XI<=maxXI; XI++) {
|
||||
x = pow(10,double (XI)/double(Xinterval));
|
||||
minY = 1e-5;
|
||||
maxY = (x-EionT2_eV)/2;
|
||||
if (maxY<0) {
|
||||
add_ankerpoint(x,0,0);
|
||||
continue;
|
||||
}
|
||||
|
||||
ss.clear();
|
||||
ss.str(lineFromFile);
|
||||
|
||||
if (ss>>xTemp && ss>>fxTemp) {
|
||||
add_ankerpoint(xTemp, fxTemp);
|
||||
} else {
|
||||
if (!lineReadError) {
|
||||
cout<<"Func_Container::readFromCSV: Warning could not read line in inputFile. \nloadFilePath = "<<loadFilePath<<endl;
|
||||
lineReadError = true;
|
||||
}
|
||||
minYI = floor(log10(minY)*Yinterval);
|
||||
maxYI = ceil(log10(maxY)*Yinterval);
|
||||
for (int YI = minYI; YI<maxYI; YI++) {
|
||||
y = pow(10,double (YI)/double(Yinterval));
|
||||
fxy = f(x,y);
|
||||
add_ankerpoint(x, y,fxy);
|
||||
}
|
||||
add_ankerpoint(x,maxY,f(x,maxY));
|
||||
}
|
||||
}
|
||||
|
||||
void Edge2D_FuncContainer::get_Elastic_fromFunction(double (*f)(double,double), double minX, double maxX, int numpointsPerXOrder, int numpointsOverY) {
|
||||
int Xinterval = numpointsPerXOrder -1;
|
||||
int minXI = floor(log10(minX)*Xinterval);
|
||||
int maxXI = ceil(log10(maxX)*Xinterval);
|
||||
double x, y, fxy;
|
||||
|
||||
for (int XI = minXI; XI<=maxXI; XI++) {
|
||||
x = pow(10,double (XI)/double(Xinterval));
|
||||
for (int YI = 0; YI<numpointsOverY; YI++) {
|
||||
y = YI/(numpointsOverY-1)*pi;
|
||||
fxy = f(x,y);
|
||||
add_ankerpoint(x, y,fxy);
|
||||
}
|
||||
}
|
||||
fileStream.close();
|
||||
|
||||
}
|
||||
|
||||
void Func_Container::printAnkerpoints() {
|
||||
dim1Map::iterator AnkerItr;
|
||||
for (AnkerItr = Map.begin();AnkerItr!=Map.end();AnkerItr++) {
|
||||
cout<<AnkerItr->first<<"\t"<<AnkerItr->second<<"\n";
|
||||
// Average2D_Container
|
||||
void Average2D_FuncContainer::set_Map(dim2Map &edgeMap) {
|
||||
// should template averageMap_from_edgeMap function and reuse it for first iteration...
|
||||
Map.clear();
|
||||
dim2Map::iterator curItr, afterItr;
|
||||
afterItr = edgeMap.begin();
|
||||
curItr = edgeMap.begin();
|
||||
afterItr++;
|
||||
dim1Map tempMap;
|
||||
while (afterItr!=edgeMap.end()) {
|
||||
averageMap_from_edgeMap (&tempMap, curItr->second);
|
||||
Map.insert(dim2Pair( curItr->first + (afterItr->first-curItr->first)/2, tempMap ));
|
||||
curItr++;
|
||||
afterItr++;
|
||||
}
|
||||
averageMap_from_edgeMap (&tempMap, curItr->second);
|
||||
Map.insert(dim2Pair( curItr->first , tempMap ));
|
||||
}
|
||||
|
||||
void Average2D_FuncContainer::set_Map(Edge2D_FuncContainer &edgeContainer) {
|
||||
dim2Map edgeMap = edgeContainer.get_Map();
|
||||
set_Map(edgeMap);
|
||||
}
|
||||
|
||||
// Stacked2D_Container:
|
||||
void Stacked2D_FuncContainer::set_Map(dim2Map &averageMap) {
|
||||
Map.clear();
|
||||
dim1Map tempMap;
|
||||
for (dim2Map::iterator Itr=averageMap.begin(); Itr!=averageMap.end() ; Itr++) {
|
||||
stackedMap_from_averageMap (&tempMap, Itr->second, 0);
|
||||
Map.insert(dim2Pair( Itr->first , tempMap ));
|
||||
}
|
||||
}
|
||||
|
||||
void Func_Container::exportToFile(std::string exportFilePath) {
|
||||
ofstream outFile;
|
||||
outFile.open(exportFilePath);
|
||||
if (!outFile.good()) {
|
||||
stringstream ss;
|
||||
ss<<"Func_Container::exportToFile: Could not open file. \n exportFilePath= "<<exportFilePath<<endl;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
outFile<<"# Automatically generated Data of Function_Container class by exportToFile().\n";
|
||||
outFile<<"# Generated on "<<getTimeString()<<".\n";
|
||||
outFile<<"#key (usually Energy)\tparameter (usually TCS)\n";
|
||||
dim1Map::iterator AnkerItr;
|
||||
for (AnkerItr = Map.begin();AnkerItr!=Map.end();AnkerItr++) {
|
||||
outFile<<AnkerItr->first<<"\t"<<AnkerItr->second<<"\n";
|
||||
}
|
||||
outFile.close();
|
||||
void Stacked2D_FuncContainer::set_Map(Average2D_FuncContainer &averageContainer) {
|
||||
dim2Map averageMap = averageContainer.get_Map();
|
||||
set_Map(averageMap);
|
||||
}
|
||||
|
||||
// Func2D_Container
|
||||
Func2D_Container::Func2D_Container(std::string loadFilePath) {
|
||||
readFromCSV(loadFilePath);
|
||||
}
|
||||
|
||||
double Func2D_Container::interpolate(double x, double y) {
|
||||
// Method might be improved, since first check for x bins, and afterwards check for y bins, so theoretically not choosing closest neighbour in terms of y but giving priority to x-closeness.
|
||||
// Avoid Mistakes by having complete set of y-data and uniform grid or improve code later.
|
||||
|
||||
|
||||
if (Map.empty()) {
|
||||
throw MyException("Func2D_Container: Container is empty.");
|
||||
}
|
||||
|
||||
if (Map.begin() == Map.lower_bound(x) ) {
|
||||
return (Map.begin()->second.interpolate(y));
|
||||
} else if (Map.end() == Map.lower_bound(x)){
|
||||
return ( (--Map.end())->second.interpolate(y) );
|
||||
}
|
||||
|
||||
dim2Map::iterator lowAnker_Itr = --Map.lower_bound(x);
|
||||
dim2Map::iterator upAnker_Itr = Map.lower_bound(x);
|
||||
double lowAnker_x = lowAnker_Itr->first;
|
||||
double lowAnker_fxy = lowAnker_Itr->second.interpolate(y);
|
||||
double upAnker_x = upAnker_Itr->first;
|
||||
double upAnker_fxy = upAnker_Itr->second.interpolate(y);
|
||||
|
||||
double res = lowAnker_fxy + (x-lowAnker_x)/(upAnker_x-lowAnker_x) * (upAnker_fxy-lowAnker_fxy);
|
||||
return (res);
|
||||
|
||||
}
|
||||
|
||||
void Func2D_Container::add_ankerpoint(double x, double y, double fxy) {
|
||||
if ( Map.find(x)==Map.end() ) {
|
||||
Func_Container Temp_Container;
|
||||
Temp_Container.add_ankerpoint(y, fxy);
|
||||
Map.insert(dim2Pair (x, Temp_Container));
|
||||
double Stacked2D_FuncContainer::sample_Value(double x, ranlib::Uniform<double> *rngUni) {
|
||||
if (Map.rbegin()->first < x) {
|
||||
return (sampleValue_from_stackedMap (&Map.rbegin()->second, rngUni));
|
||||
} else {
|
||||
try {
|
||||
Map.find(x)->second.add_ankerpoint(y,fxy);
|
||||
} catch (MyException& caught) {
|
||||
stringstream ss;
|
||||
ss<<"Func2D_Container::add_ankerpoint: Error from "<<caught.what();
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
return (sampleValue_from_stackedMap (&Map.lower_bound(x)->second, rngUni));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void Func2D_Container::readFromCSV(std::string loadFilePath) {
|
||||
ifstream fileStream(loadFilePath);
|
||||
if (!fileStream.good()) {
|
||||
stringstream ss;
|
||||
ss<<"Func_Container::readFromCSV: Could not open file.\nloadFilePath= "<<loadFilePath<<endl;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
|
||||
string lineFromFile;
|
||||
stringstream ss;
|
||||
double xTemp,yTemp,fxyTemp;
|
||||
bool lineReadError = false;
|
||||
|
||||
while (getline(fileStream, lineFromFile)) {
|
||||
removeLeadingWhites(lineFromFile);
|
||||
replaceDelimiter(lineFromFile);
|
||||
if ( checkIgnoreLine(lineFromFile) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ss.clear();
|
||||
ss.str(lineFromFile);
|
||||
|
||||
if (ss>>xTemp && ss>>yTemp&& ss>>fxyTemp) {
|
||||
add_ankerpoint(xTemp, yTemp, fxyTemp);
|
||||
} else {
|
||||
if (!lineReadError) {
|
||||
cout<<"Func_Container::readFromCSV: Warning could not read line in inputFile. \nloadFilePath = "<<loadFilePath<<endl;
|
||||
lineReadError = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
fileStream.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user