Restructuring Code. Removed some small classes which provided only 1 function, like parameter. Now each class calls it own parameters and stores them as static const.
Merged function containers. Added HDF5 output and reworked Logger class into Base class and 2 derived classes ASCI_Logger and H5_Logger
This commit is contained in:
@@ -8,16 +8,13 @@
|
||||
|
||||
#include "function_container.hpp"
|
||||
|
||||
#include "myexeption.h"
|
||||
#include "functions.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
Func_Container::Func_Container() {
|
||||
|
||||
}
|
||||
|
||||
Func_Container::Func_Container(std::string loadFilePath) {
|
||||
readFromCSV(loadFilePath);
|
||||
@@ -100,26 +97,87 @@ void Func_Container::printAnkerpoints() {
|
||||
}
|
||||
}
|
||||
|
||||
// Func2D_Container
|
||||
Func2D_Container::Func2D_Container(std::string loadFilePath) {
|
||||
readFromCSV(loadFilePath);
|
||||
}
|
||||
|
||||
bool checkIgnoreLine(string lineFromFile) {
|
||||
if (lineFromFile[0] =='#') {
|
||||
return true;
|
||||
} else if ( lineFromFile.empty() ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
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 removeLeadingWhites(string &lineFromFile) {
|
||||
static const string WHITESPACE = " \n\r\t\f\v";
|
||||
size_t start = lineFromFile.find_first_not_of(WHITESPACE);
|
||||
lineFromFile = (start == std::string::npos) ? "" : lineFromFile.substr(start);
|
||||
|
||||
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));
|
||||
} 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;
|
||||
}
|
||||
|
||||
void replaceDelimiter(string &lineFromFile) {
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), ';', ' ');
|
||||
replace(lineFromFile.begin(), lineFromFile.end(), ',', ' ');
|
||||
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