OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
ProbeReader.cpp
Go to the documentation of this file.
1//
2// Class ProbeReader
3// Implements a parser and value extractor for Probe loss files.
4//
5// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
6// All rights reserved
7//
8// Implemented as part of the PhD thesis
9// "Toward massively parallel multi-objective optimization with application to
10// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
11//
12// This file is part of OPAL.
13//
14// OPAL is free software: you can redistribute it and/or modify
15// it under the terms of the GNU General Public License as published by
16// the Free Software Foundation, either version 3 of the License, or
17// (at your option) any later version.
18//
19// You should have received a copy of the GNU General Public License
20// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
21//
22#include "ProbeReader.h"
23
25
26#include <cstring>
27#include <fstream>
28
29ProbeReader::ProbeReader(std::string filename) :
30 filename_m(filename),
31 nColumns_m(0),
32 nRows_m(0),
33 data_m(0)
34{ }
35
36
38
39
41
42 nColumns_m = 0;
43 nRows_m = 0;
44
45 std::ifstream probe;
46
47 probe.open(filename_m.c_str(), std::ios::in);
48 if ( !probe ) {
49 throw OptPilotException("ProbeReader::parseFile()",
50 "Error opening file " + filename_m);
51 }
52
53 std::string header;
54 std::getline(probe, header, '\n');
55
56 if( header.find("# Element\n") == std::string::npos ) {
57 throw OptPilotException("ProbeReader::parseFile()",
58 "Error parsing Probe header!");
59 }
60
61 char *token = std::strtok(&header[0], " ");
62
63 // parse header
64 while ( token != NULL ) {
65
66 if ( std::string(token) == "Element")
67 token = std::strtok(NULL, " "); // skip name
68 else if ( token[0] != ')' && token[0] != '(' && token[0] != '#') {
69 std::string varname = std::string(token);
70
71 if ( varname.back() == ',' )
72 varname.pop_back();
73
74 columnNamesToID_m[varname] = nColumns_m++;
75 }
76 token = std::strtok(NULL, " ");
77 }
78
79 // parse values
80 data_m.resize(nColumns_m);
81
82 std::string line;
83 while ( std::getline(probe, line) ) {
84
85 ++nRows_m;
86
87 token = std::strtok(&line[0], " ");
88
89 // skip first (probe name)
90 token = std::strtok(NULL, " ");
91
92 int i = 0;
93
94 while ( token != NULL ) {
95 data_m[i++].push_back( std::atof(token) );
96 token = std::strtok(NULL, " ");
97 }
98 }
99
100 probe.close();
101}
102
103
104void ProbeReader::getVariableValue(int id, std::string varname, double& sim_value) {
105
106
107 int varindex = 0;
108 if(columnNamesToID_m.count(varname) > 0) {
109 varindex = columnNamesToID_m[varname];
110 } else {
111 throw OptPilotException("ProbeReader::getVariableValue",
112 "variable name!");
113 }
114
115 int col = 0;
116 if(columnNamesToID_m.count("id") > 0) {
117 col = columnNamesToID_m["id"];
118 } else {
119 throw OptPilotException("ProbeReader::getVariableValue",
120 "ID variable not found!");
121 }
122
123 int row = -1;
124 for (unsigned int i = 0; i < data_m[col].size(); ++i) {
125 if ( data_m[col][i] == id ) {
126 row = i;
127 break;
128 }
129 }
130
131 if ( row < 0 )
132 throw OptPilotException("ProbeReader::getVariableValue",
133 "Appropriate value not found!");
134
135 sim_value = data_m[varindex][row];
136}
std::string filename_m
Probe loss filename.
Definition: ProbeReader.h:42
int nColumns_m
Number of variables.
Definition: ProbeReader.h:45
std::vector< std::vector< double > > data_m
Definition: ProbeReader.h:51
void getVariableValue(int id, std::string varname, double &sim_value)
int nRows_m
Number of values per variable.
Definition: ProbeReader.h:48
void parseFile()
Definition: ProbeReader.cpp:40
ProbeReader(std::string filename)
Definition: ProbeReader.cpp:29
std::map< std::string, int > columnNamesToID_m
Definition: ProbeReader.h:50