OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ProbeReader.cpp
Go to the documentation of this file.
1 #include "ProbeReader.h"
2 
4 
5 #include <cstring>
6 #include <fstream>
7 
8 ProbeReader::ProbeReader(std::string filename) :
9  filename_m(filename),
10  nColumns_m(0),
11  nRows_m(0),
12  data_m(0)
13 { }
14 
15 
17 
18 
20 
21  nColumns_m = 0;
22  nRows_m = 0;
23 
24  std::ifstream probe;
25 
26  probe.open(filename_m.c_str(), std::ios::in);
27  if ( !probe ) {
28  throw OptPilotException("ProbeReader::parseFile()",
29  "Error opening file " + filename_m);
30  }
31 
32  std::string header;
33  std::getline(probe, header, '\n');
34 
35  if( header.find("# Element\n") == std::string::npos ) {
36  throw OptPilotException("ProbeReader::parseFile()",
37  "Error parsing Probe header!");
38  }
39 
40  char *token = std::strtok(&header[0], " ");
41 
42  // parse header
43  while ( token != NULL ) {
44 
45  if ( std::string(token) == "Element")
46  token = std::strtok(NULL, " "); // skip name
47  else if ( token[0] != ')' && token[0] != '(' && token[0] != '#') {
48  std::string varname = std::string(token);
49 
50  if ( varname.back() == ',' )
51  varname.pop_back();
52 
53  columnNamesToID_m[varname] = nColumns_m++;
54  }
55  token = std::strtok(NULL, " ");
56  }
57 
58  // parse values
59  data_m.resize(nColumns_m);
60 
61  std::string line;
62  while ( std::getline(probe, line) ) {
63 
64  ++nRows_m;
65 
66  token = std::strtok(&line[0], " ");
67 
68  // skip first (probe name)
69  token = std::strtok(NULL, " ");
70 
71  int i = 0;
72 
73  while ( token != NULL ) {
74  data_m[i++].push_back( std::atof(token) );
75  token = std::strtok(NULL, " ");
76  }
77  }
78 
79  probe.close();
80 }
81 
82 
83 void ProbeReader::getVariableValue(int id, std::string varname, double& sim_value) {
84 
85 
86  int varindex = 0;
87  if(columnNamesToID_m.count(varname) > 0) {
88  varindex = columnNamesToID_m[varname];
89  } else {
90  throw OptPilotException("ProbeReader::getVariableValue",
91  "variable name!");
92  }
93 
94  int col = 0;
95  if(columnNamesToID_m.count("id") > 0) {
96  col = columnNamesToID_m["id"];
97  } else {
98  throw OptPilotException("ProbeReader::getVariableValue",
99  "ID variable not found!");
100  }
101 
102  int row = -1;
103  for (unsigned int i = 0; i < data_m[col].size(); ++i) {
104  if ( data_m[col][i] == id ) {
105  row = i;
106  break;
107  }
108  }
109 
110  if ( row < 0 )
111  throw OptPilotException("ProbeReader::getVariableValue",
112  "Appropriate value not found!");
113 
114  sim_value = data_m[varindex][row];
115 }
ProbeReader(std::string filename)
Definition: ProbeReader.cpp:8
std::map< std::string, int > columnNamesToID_m
Definition: ProbeReader.h:34
int nColumns_m
Number of variables.
Definition: ProbeReader.h:29
void getVariableValue(int id, std::string varname, double &sim_value)
Definition: ProbeReader.cpp:83
void parseFile()
Definition: ProbeReader.cpp:19
std::vector< std::vector< double > > data_m
Definition: ProbeReader.h:35
int nRows_m
Number of values per variable.
Definition: ProbeReader.h:32
std::string filename_m
Probe loss filename.
Definition: ProbeReader.h:26