Code indexing in gitaly is broken and leads to code not being visible to the user. We work on the issue with highest priority.

Skip to content
Snippets Groups Projects
Commit 6c5c7f53 authored by Christof Metzger-Kraus's avatar Christof Metzger-Kraus
Browse files

use 'native' SDDS parser

parent b0eb1f8e
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,8 @@ SET (_UTIL_SRCS
MPIHelper.cpp
PeakReader.cpp
SDDSReader.cpp
ProbeReader.cpp
CommentAnnotatedInputFileParser.cpp
)
add_sources(${_UTIL_SRCS})
add_sources(${_UTIL_SRCS})
\ No newline at end of file
#include <fstream>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "Util/SDDSReader.h"
#include "Util/OptPilotException.h"
SDDSReader::SDDSReader(std::string filename) {
filename_m = filename;
numParams_m = 0;
numColumns_m = 0;
}
SDDSReader::~SDDSReader()
{}
void SDDSReader::parseFile() {
std::ifstream sdds;
sdds.open(filename_m.c_str(), std::ios::in);
if(!sdds) {
throw OptPilotException("SDDSReader::parseFile()",
"Error opening file " + filename_m);
}
std::string header;
std::getline(sdds, header, '\n');
if( !header.compare("SDDS1\n") ) {
throw OptPilotException("SDDSReader::parseFile()",
"Error parsing SDDS header!");
}
// parse header
while(sdds) {
std::string linestart;
std::getline(sdds, linestart, ' ');
if( linestart.compare(0, 12, "&description") == 0) {
std::getline(sdds, linestart, '&');
std::getline(sdds, linestart, '\n');
} else if( linestart.compare(0, 10, "&parameter") == 0) {
std::getline(sdds, linestart, '&');
std::vector<std::string> tmp = split(linestart, ",");
std::string ts = tmp[0];
tmp.clear();
tmp = split(ts, "=");
addParameter(tmp[1]);
std::getline(sdds, linestart, '\n');
} else if( linestart.compare(0, 7, "&column") == 0 ) {
std::getline(sdds, linestart, '&');
std::vector<std::string> tmp = split(linestart, ",");
std::string ts = tmp[0];
tmp.clear();
tmp = split(ts, "=");
addColumn(tmp[1]);
std::getline(sdds, linestart, '\n');
} else if( linestart.compare(0, 5, "&data") == 0 ) {
std::getline(sdds, linestart, '&');
std::getline(sdds, linestart, '\n');
break;
} else
throw OptPilotException("SDDSReader::parseFile()",
"Header line starts with unrecognized field: '" + linestart + "'!");
}
// read params
for(int i=0; i<numParams_m; i++) {
std::string param;
std::getline(sdds, param, '\n');
params_m.push_back(param);
}
// read all dumps
while(sdds) {
std::string line;
std::vector<std::string> tmp;
for(int i=0; i<numColumns_m-1; i++) {
std::getline(sdds, line, '\t');
tmp.push_back(line);
}
std::getline(sdds, line, '\n');
tmp.push_back(line);
if (tmp[0].empty()) continue; // skip empty lines
columns_m.push_back(tmp);
}
}
std::vector<std::string> SDDSReader::split(
std::string str, std::string delimiter) {
std::string tmp;
std::vector<std::string> res;
size_t fpos = str.find(delimiter);
while(fpos != std::string::npos) {
tmp = str.substr(0,fpos);
str = str.substr(fpos+1, std::string::npos);
res.push_back(tmp);
fpos = str.find(delimiter);
}
res.push_back(str);
return res;
}
//XXX use either all upper, or all lower case chars
void SDDSReader::fixCaseSensitivity(std::string &for_string) {
boost::to_lower(for_string);
}
void SDDSReader::addParameter(std::string param_name) {
fixCaseSensitivity(param_name);
paramNameToID_m.insert(std::pair<std::string, int>(param_name, numParams_m));
numParams_m++;
}
void SDDSReader::addColumn(std::string column_name) {
fixCaseSensitivity(column_name);
columnNameToID_m.insert(std::pair<std::string, int>(column_name, numColumns_m));
numColumns_m++;
}
\ No newline at end of file
#ifndef __SDDSREADER_H__
#define __SDDSREADER_H__
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <locale>
#include "Util/OptPilotException.h"
/**
* \class SDDSReader
* \brief Implements a parser and value extractor for SDDS files
*
* Statistical Opal simulation results are stored in SDDS files. This class
* grants the optimizer access to results of the forward solve to evaluate
* expressions (objectives, constraints).
*/
class SDDSReader {
public:
SDDSReader(std::string filename);
~SDDSReader();
void parseFile();
/**
* Converts the string value of a parameter at timestep t to a value of
* type T.
*
* @param t timestep (beginning at 1, -1 means last)
* @param param parameter name
* @param nval store result of type T in nval
*/
template <typename T>
void getValue(int t, std::string param_name, T& nval) {
fixCaseSensitivity(param_name);
// round timestep to last if not in range
size_t col_idx = 0;
if(t <= 0 || static_cast<size_t>(t) > columns_m.size())
col_idx = columns_m.size() - 1;
else
col_idx = static_cast<size_t>(t) - 1;
if(columnNameToID_m.count(param_name) > 0) {
int index = columnNameToID_m[param_name];
std::string val = (columns_m[col_idx])[index];
nval = stringToNum<T>(val);
} else {
throw OptPilotException("SDDSReader::getValue",
"unknown column name: '" + param_name + "'!");
}
#include "Structure/SDDSParser.h"
class SDDSReader: public SDDS::SDDSParser
{
public:
SDDSReader(const std::string &fname):
SDDSParser(fname)
{ }
inline void parseFile()
{
run();
}
/**
* Converts the string value of a parameter at a position spos to a value
* of type T.
*
* @param spos interpolate value at spos
* @param param parameter name
* @param nval store result of type T in nval
*/
template <typename T>
void getInterpolatedValue(double spos, std::string param_name, T& nval) {
fixCaseSensitivity(param_name);
T value_before = 0;
T value_after = 0;
double value_before_spos = 0;
double value_after_spos = 0;
size_t index_spos = 1;
if(columnNameToID_m.count(param_name) > 0) {
int index = columnNameToID_m[param_name];
size_t this_col = 0;
for(this_col = 0; this_col < columns_m.size(); this_col++) {
value_after_spos = stringToNum<double>(
(columns_m[this_col])[index_spos]);
if(spos < value_after_spos) {
size_t prev_col = 0;
if(this_col > 0) prev_col = this_col - 1;
value_before = stringToNum<T>(
(columns_m[prev_col])[index]);
value_after = stringToNum<T>(
(columns_m[this_col])[index]);
value_before_spos = stringToNum<double>(
(columns_m[prev_col])[index_spos]);
value_after_spos = stringToNum<double>(
(columns_m[this_col])[index_spos]);
break;
}
}
if(this_col == columns_m.size())
throw OptPilotException("SDDSReader::getInterpolatedValue",
"all values < specified spos");
} else {
throw OptPilotException("SDDSReader::getInterpolatedValue",
"unknown column name: '" + param_name + "'!");
}
// simple linear interpolation
if(spos - value_before_spos < 1e-8)
nval = value_before;
else
nval = value_before + (spos - value_before_spos)
* (value_after - value_before)
/ (value_after_spos - value_before_spos);
}
private:
/// SDDS filename
std::string filename_m;
/// number of parameters found in SDDS header
int numParams_m;
/// number of columns in the SDDS file
int numColumns_m;
/// mapping from parameter name to offset in params_m
std::map<std::string, int> paramNameToID_m;
/// mapping from column name to ID in columns_m
std::map<std::string, int> columnNameToID_m;
/// vector holding all vectors of columns
std::vector< std::vector<std::string> > columns_m;
/// vector of all parameters
std::vector< std::string > params_m;
void fixCaseSensitivity(std::string &for_string);
void addParameter(std::string param_name);
void addColumn(std::string column_name);
/**
* Split a 'delimiter' separated string into list
* @param str string to split
* @param delimiter separating items in string
* @return vector containing all string items
*/
std::vector<std::string> split(std::string str, std::string delimiter);
template <typename T>
T stringToNum( const std::string& s ) {
std::istringstream i(s);
T x;
if (!(i >> x))
return 0;
return x;
}
};
#endif
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment