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