SUMO - Simulation of Urban MObility
CEPHandler.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <sstream>
3 #include "CEPHandler.h"
4 #include "CEP.h"
5 #include "Helpers.h"
6 #include "Constants.h"
7 
8 
9 namespace PHEMlightdll {
10 
12  _ceps = std::map<std::string, CEP*>();
13  }
14 
15  const std::map<std::string, CEP*>& CEPHandler::getCEPS() const {
16  return _ceps;
17  }
18 
19  bool CEPHandler::GetCEP(const std::vector<std::string>& DataPath, Helpers* Helper) {
20  if (getCEPS().find(Helper->getgClass()) == getCEPS().end()) {
21  if (!Load(DataPath, Helper)) {
22  return false;
23  }
24  }
25  return true;
26  }
27 
28  bool CEPHandler::Load(const std::vector<std::string>& DataPath, Helpers* Helper) {
29  //Deklaration
30  // get string identifier for PHEM emission class
31 //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'ToString':
32  std::string emissionRep = Helper->getgClass();
33 
34  // to hold everything.
35  std::vector<std::vector<double> > matrixSpeedInertiaTable;
36  std::vector<std::vector<double> > normedTragTableSpeedInertiaTable;
37  std::vector<std::vector<double> > matrixFC;
38  std::vector<std::vector<double> > matrixPollutants;
39  std::vector<double> idlingValuesFC;
40  std::vector<double> idlingValuesPollutants;
41  std::vector<std::string> headerFC;
42  std::vector<std::string> headerPollutants;
43 
44  double vehicleMass;
45  double vehicleLoading;
46  double vehicleMassRot;
47  double crosssectionalArea;
48  double cwValue;
49  double f0;
50  double f1;
51  double f2;
52  double f3;
53  double f4;
54  double axleRatio;
55  std::vector<double> transmissionGearRatios;
56  double auxPower;
57  double ratedPower;
58  double engineIdlingSpeed;
59  double engineRatedSpeed;
60  double effectiveWhellDiameter;
61  std::string vehicleMassType;
62  std::string vehicleFuelType;
63  double pNormV0;
64  double pNormP0;
65  double pNormV1;
66  double pNormP1;
67 
68  if (!ReadVehicleFile(DataPath, emissionRep, Helper, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, axleRatio, auxPower, ratedPower, engineIdlingSpeed, engineRatedSpeed, effectiveWhellDiameter, transmissionGearRatios, vehicleMassType, vehicleFuelType, pNormV0, pNormP0, pNormV1, pNormP1, matrixSpeedInertiaTable, normedTragTableSpeedInertiaTable)) {
69  return false;
70  }
71 
72  if (!ReadEmissionData(true, DataPath, emissionRep, Helper, headerFC, matrixFC, idlingValuesFC)) {
73  return false;
74  }
75 
76  if (!ReadEmissionData(false, DataPath, emissionRep, Helper, headerPollutants, matrixPollutants, idlingValuesPollutants)) {
77  return false;
78  }
79 
80  _ceps.insert(std::make_pair(Helper->getgClass(), new CEP(vehicleMassType == Constants::HeavyVehicle, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, axleRatio, transmissionGearRatios, auxPower, ratedPower, engineIdlingSpeed, engineRatedSpeed, effectiveWhellDiameter, pNormV0, pNormP0, pNormV1, pNormP1, vehicleFuelType, matrixFC, headerPollutants, matrixPollutants, matrixSpeedInertiaTable, normedTragTableSpeedInertiaTable, idlingValuesFC.front(), idlingValuesPollutants)));
81 
82  return true;
83  }
84 
85  bool CEPHandler::ReadVehicleFile(const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, double& vehicleMass, double& vehicleLoading, double& vehicleMassRot, double& crossArea, double& cWValue, double& f0, double& f1, double& f2, double& f3, double& f4, double& axleRatio, double& auxPower, double& ratedPower, double& engineIdlingSpeed, double& engineRatedSpeed, double& effectiveWheelDiameter, std::vector<double>& transmissionGearRatios, std::string& vehicleMassType, std::string& vehicleFuelType, double& pNormV0, double& pNormP0, double& pNormV1, double& pNormP1, std::vector<std::vector<double> >& matrixSpeedInertiaTable, std::vector<std::vector<double> >& normedDragTable) {
86  vehicleMass = 0;
87  vehicleLoading = 0;
88  vehicleMassRot = 0;
89  crossArea = 0;
90  cWValue = 0;
91  f0 = 0;
92  f1 = 0;
93  f2 = 0;
94  f3 = 0;
95  f4 = 0;
96  axleRatio = 0;
97  ratedPower = 0;
98  auxPower = 0;
99  engineIdlingSpeed = 0;
100  engineRatedSpeed = 0;
101  effectiveWheelDiameter = 0;
102  vehicleMassType = "";
103  vehicleFuelType = "";
104  pNormV0 = 0;
105  pNormP0 = 0;
106  pNormV1 = 0;
107  pNormP1 = 0;
108  transmissionGearRatios = std::vector<double>();
109  matrixSpeedInertiaTable = std::vector<std::vector<double> >();
110  normedDragTable = std::vector<std::vector<double> >();
111  std::string line;
112  std::string cell;
113  int dataCount = 0;
114 
115  //Open file
116  std::ifstream vehicleReader;
117  for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {
118  vehicleReader.open(((*i) + emissionClass + ".PHEMLight.veh").c_str());
119  if (vehicleReader.good()) {
120  break;
121  }
122  }
123  if (!vehicleReader.good()) {
124  Helper->setErrMsg("File does not exist! (" + emissionClass + ".PHEMLight.veh)");
125  return false;
126  }
127 
128  // skip header
129  ReadLine(vehicleReader);
130 
131  while ((line = ReadLine(vehicleReader)) != "" && dataCount <= 49) {
132  if (line.substr(0, 1) == Helper->getCommentPrefix()) {
133  continue;
134  }
135  else {
136  dataCount++;
137  }
138 
139  cell = split(line, ',')[0];
140 
141  // reading Mass
142  if (dataCount == 1) {
143  vehicleMass = todouble(cell);
144  }
145 
146  // reading vehicle loading
147  if (dataCount == 2) {
148  vehicleLoading = todouble(cell);
149  }
150 
151  // reading cWValue
152  if (dataCount == 3) {
153  cWValue = todouble(cell);
154  }
155 
156  // reading crossectional area
157  if (dataCount == 4) {
158  crossArea = todouble(cell);
159  }
160 
161  // reading vehicle mass rotational
162  if (dataCount == 7) {
163  vehicleMassRot = todouble(cell);
164  }
165 
166  // reading rated power
167  if (dataCount == 9) {
168  auxPower = todouble(cell);
169  }
170 
171  // reading rated power
172  if (dataCount == 10) {
173  ratedPower = todouble(cell);
174  }
175 
176  // reading engine rated speed
177  if (dataCount == 11) {
178  engineRatedSpeed = todouble(cell);
179  }
180 
181  // reading engine idling speed
182  if (dataCount == 12) {
183  engineIdlingSpeed = todouble(cell);
184  }
185 
186  // reading f0
187  if (dataCount == 14) {
188  f0 = todouble(cell);
189  }
190 
191  // reading f1
192  if (dataCount == 15) {
193  f1 = todouble(cell);
194  }
195 
196  // reading f2
197  if (dataCount == 16) {
198  f2 = todouble(cell);
199  }
200 
201  // reading f3
202  if (dataCount == 17) {
203  f3 = todouble(cell);
204  }
205 
206  // reading f4
207  if (dataCount == 18) {
208  f4 = todouble(cell);
209  }
210 
211  // reading axleRatio
212  if (dataCount == 21) {
213  axleRatio = todouble(cell);
214  }
215 
216  // reading effective wheel diameter
217  if (dataCount == 22) {
218  effectiveWheelDiameter = todouble(cell);
219  }
220 
221  if (dataCount >= 23 && dataCount <= 40) {
222  transmissionGearRatios.push_back(todouble(cell));
223  }
224 
225  // reading vehicleMassType
226  if (dataCount == 45) {
227  vehicleMassType = cell;
228  }
229 
230  // reading vehicleFuelType
231  if (dataCount == 46) {
232  vehicleFuelType = cell;
233  }
234 
235  // reading pNormV0
236  if (dataCount == 47) {
237  pNormV0 = todouble(cell);
238  }
239 
240  // reading pNormP0
241  if (dataCount == 48) {
242  pNormP0 = todouble(cell);
243  }
244 
245  // reading pNormV1
246  if (dataCount == 49) {
247  pNormV1 = todouble(cell);
248  }
249 
250  // reading pNormP1
251  if (dataCount == 50) {
252  pNormP1 = todouble(cell);
253  }
254  }
255 
256  while ((line = ReadLine(vehicleReader)) != "" && line.substr(0, 1) != Helper->getCommentPrefix()) {
257  if (line.substr(0, 1) == Helper->getCommentPrefix()) {
258  continue;
259  }
260 
261  matrixSpeedInertiaTable.push_back(todoubleList(split(line, ',')));
262  }
263 
264  while ((line = ReadLine(vehicleReader)) != "") {
265  if (line.substr(0, 1) == Helper->getCommentPrefix()) {
266  continue;
267  }
268 
269  normedDragTable.push_back(todoubleList(split(line, ',')));
270  }
271 
272  return true;
273  }
274 
275  bool CEPHandler::ReadEmissionData(bool readFC, const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, std::vector<std::string>& header, std::vector<std::vector<double> >& matrix, std::vector<double>& idlingValues) {
276  // declare file stream
277  std::string line;
278  header = std::vector<std::string>();
279  matrix = std::vector<std::vector<double> >();
280  idlingValues = std::vector<double>();
281 
282  std::string pollutantExtension = "";
283  if (readFC) {
284  pollutantExtension += std::string("_FC");
285  }
286 
287  std::ifstream fileReader;
288  for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {
289  fileReader.open(((*i) + emissionClass + pollutantExtension + ".csv").c_str());
290  if (fileReader.good()) {
291  break;
292  }
293  }
294  if (!fileReader.good()) {
295  Helper->setErrMsg("File does not exist! (" + emissionClass + pollutantExtension + ".csv)");
296  return false;
297  }
298 
299  // read header line for pollutant identifiers
300  if ((line = ReadLine(fileReader)) != "") {
301  std::vector<std::string> entries = split(line, ',');
302  // skip first entry "Pe"
303  for (int i = 1; i < (int)entries.size(); i++) {
304  header.push_back(entries[i]);
305  }
306  }
307 
308  // skip units
309  ReadLine(fileReader);
310 
311  // skip comment
312  ReadLine(fileReader);
313 
314  //readIdlingValues
315  line = ReadLine(fileReader);
316 
317  std::vector<std::string> stringIdlings = split(line, ',');
318  stringIdlings.erase(stringIdlings.begin());
319 
320  idlingValues = todoubleList(stringIdlings);
321 
322  while ((line = ReadLine(fileReader)) != "") {
323  matrix.push_back(todoubleList(split(line, ',')));
324  }
325  return true;
326  }
327 
328  std::vector<std::string> CEPHandler::split(const std::string& s, char delim) {
329  std::vector<std::string> elems;
330  std::stringstream ss(s);
331  std::string item;
332  while (std::getline(ss, item, delim)) {
333  elems.push_back(item);
334  }
335  return elems;
336  }
337 
338  double CEPHandler::todouble(const std::string& s) {
339  std::stringstream ss(s);
340  double item;
341  ss >> item;
342  return item;
343  }
344 
345  std::vector<double> CEPHandler::todoubleList(const std::vector<std::string>& s) {
346  std::vector<double> result;
347  for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); ++i) {
348  result.push_back(todouble(*i));
349  }
350  return result;
351  }
352 
353  std::string CEPHandler::ReadLine(std::ifstream& s) {
354  std::string line;
355  std::getline(s, line);
356  line.erase(line.find_last_not_of(" \n\r\t") + 1);
357  return line;
358  }
359 }
std::vector< double > todoubleList(const std::vector< std::string > &s)
Definition: CEPHandler.cpp:345
std::map< std::string, CEP * > _ceps
Definition: CEPHandler.h:28
void setErrMsg(const std::string &value)
Definition: Helpers.cpp:51
bool Load(const std::vector< std::string > &DataPath, Helpers *Helper)
Definition: CEPHandler.cpp:28
static const std::string HeavyVehicle
Definition: Constants.h:23
std::string ReadLine(std::ifstream &s)
Definition: CEPHandler.cpp:353
bool GetCEP(const std::vector< std::string > &DataPath, Helpers *Helper)
Definition: CEPHandler.cpp:19
const std::string & getgClass() const
Definition: Helpers.cpp:39
double todouble(const std::string &s)
Definition: CEPHandler.cpp:338
std::vector< std::string > split(const std::string &s, char delim)
Definition: CEPHandler.cpp:328
const std::map< std::string, CEP * > & getCEPS() const
Definition: CEPHandler.cpp:15
const std::string & getCommentPrefix() const
Definition: Helpers.cpp:55
bool ReadEmissionData(bool readFC, const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix, std::vector< double > &idlingValues)
Definition: CEPHandler.cpp:275
bool ReadVehicleFile(const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, double &vehicleMass, double &vehicleLoading, double &vehicleMassRot, double &crossArea, double &cWValue, double &f0, double &f1, double &f2, double &f3, double &f4, double &axleRatio, double &auxPower, double &ratedPower, double &engineIdlingSpeed, double &engineRatedSpeed, double &effectiveWheelDiameter, std::vector< double > &transmissionGearRatios, std::string &vehicleMassType, std::string &vehicleFuelType, double &pNormV0, double &pNormP0, double &pNormV1, double &pNormP1, std::vector< std::vector< double > > &matrixSpeedInertiaTable, std::vector< std::vector< double > > &normedDragTable)
Definition: CEPHandler.cpp:85