SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PHEMCEPHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Helper class for PHEM Light, holds CEP data for emission computation
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2013-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cstdlib>
34 #include <fstream>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 #include "PHEMCEPHandler.h"
39 #include "PHEMConstants.h"
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47 }
48 
49 
51  std::map<SUMOEmissionClass, PHEMCEP*>::iterator iter = _ceps.begin();
52  while (iter != _ceps.end()) {
53  delete(iter->second);
54  iter++;
55  } // end while
56  _ceps.clear();
57 }
58 
59 
62  static PHEMCEPHandler instance;
63  return instance;
64 }
65 
66 
67 bool
68 PHEMCEPHandler::Load(SUMOEmissionClass emissionClass, const std::string& emissionClassIdentifier) {
69  // to hold everything.
70  std::vector< std::vector<double> > matrixSpeedInertiaTable;
71  std::vector< std::vector<double> > normedDragTable;
72  std::vector< std::vector<double> > matrixFC;
73  std::vector< std::vector<double> > matrixPollutants;
74  std::vector<std::string> headerFC;
75  std::vector<std::string> headerPollutants;
76  std::vector<double> idlingValues;
77  std::vector<double> idlingValuesFC;
78 
79  double vehicleMass;
80  double vehicleLoading;
81  double vehicleMassRot;
82  double crosssectionalArea;
83  double cwValue;
84  double f0;
85  double f1;
86  double f2;
87  double f3;
88  double f4;
89  double axleRatio;
90  double ratedPower;
91  double engineIdlingSpeed;
92  double engineRatedSpeed;
93  double effectiveWheelDiameter;
94  std::string vehicleMassType;
95  std::string vehicleFuelType;
96  double pNormV0;
97  double pNormP0;
98  double pNormV1;
99  double pNormP1;
100 
102  //std::string phemPath = oc.getString("phemlight-path") + "/";
103  std::vector<std::string> phemPath;
104  phemPath.push_back(oc.getString("phemlight-path") + "/");
105  if (getenv("PHEMLIGHT_PATH") != 0) {
106  phemPath.push_back(std::string(getenv("PHEMLIGHT_PATH")) + "/");
107  }
108  if (getenv("SUMO_HOME") != 0) {
109  phemPath.push_back(std::string(getenv("SUMO_HOME")) + "/data/emissions/PHEMlight/");
110  }
111  if (!ReadVehicleFile(phemPath, emissionClassIdentifier,
112  vehicleMass,
113  vehicleLoading,
114  vehicleMassRot,
115  crosssectionalArea,
116  cwValue,
117  f0,
118  f1,
119  f2,
120  f3,
121  f4,
122  axleRatio,
123  ratedPower,
124  engineIdlingSpeed,
125  engineRatedSpeed,
126  effectiveWheelDiameter,
127  vehicleMassType,
128  vehicleFuelType,
129  pNormV0,
130  pNormP0,
131  pNormV1,
132  pNormP1,
133  matrixSpeedInertiaTable,
134  normedDragTable)) {
135  return false;
136  }
137 
138  if (!ReadEmissionData(true, phemPath, emissionClassIdentifier, headerFC, matrixFC, idlingValuesFC)) {
139  return false;
140  }
141 
142  if (!ReadEmissionData(false, phemPath, emissionClassIdentifier, headerPollutants, matrixPollutants, idlingValues)) {
143  return false;
144  }
145 
146  _ceps[emissionClass] = new PHEMCEP(vehicleMassType == "HV",
147  emissionClass, emissionClassIdentifier,
148  vehicleMass,
149  vehicleLoading,
150  vehicleMassRot,
151  crosssectionalArea,
152  cwValue,
153  f0,
154  f1,
155  f2,
156  f3,
157  f4,
158  ratedPower,
159  pNormV0,
160  pNormP0,
161  pNormV1,
162  pNormP1,
163  axleRatio,
164  engineIdlingSpeed,
165  engineRatedSpeed,
166  effectiveWheelDiameter,
167  idlingValuesFC.front(),
168  vehicleFuelType,
169  matrixFC,
170  headerPollutants,
171  matrixPollutants,
172  matrixSpeedInertiaTable,
173  normedDragTable,
174  idlingValues);
175 
176  return true;
177 } // end of Load()
178 
179 
180 PHEMCEP*
182  // check if Cep has been loaded
183  if (_ceps.find(emissionClass) == _ceps.end()) {
184  return 0;
185  } // end if
186 
187  return _ceps[emissionClass];
188 } // end of GetCep
189 
190 
191 bool
192 PHEMCEPHandler::ReadVehicleFile(const std::vector<std::string>& path, const std::string& emissionClass,
193  double& vehicleMass,
194  double& vehicleLoading,
195  double& vehicleMassRot,
196  double& crossArea,
197  double& cWValue,
198  double& f0,
199  double& f1,
200  double& f2,
201  double& f3,
202  double& f4,
203  double& axleRatio,
204  double& ratedPower,
205  double& engineIdlingSpeed,
206  double& engineRatedSpeed,
207  double& effectiveWheelDiameter,
208  std::string& vehicleMassType,
209  std::string& vehicleFuelType,
210  double& pNormV0,
211  double& pNormP0,
212  double& pNormV1,
213  double& pNormP1,
214  std::vector< std::vector<double> >& matrixSpeedInertiaTable,
215  std::vector< std::vector<double> >& normedDragTable)
216 
217 {
218  std::ifstream fileVehicle;
219  for (std::vector<std::string>::const_iterator i = path.begin(); i != path.end(); i++) {
220  fileVehicle.open(((*i) + emissionClass + ".PHEMLight.veh").c_str());
221  if (fileVehicle.good()) {
222  break;
223  }
224  }
225  if (!fileVehicle.good()) {
226  return false;
227  }
228 
229  std::string line;
230  std::string cell;
231  std::string commentPrefix = "c";
232  int dataCount = 0;
233 
234  // skip header
235  std::getline(fileVehicle, line);
236 
237  while (std::getline(fileVehicle, line) && dataCount <= 49) {
238  // EOL handling for Linux
239  if (line.size() > 0 && line.substr(line.size() - 1) == "\r") {
240  line = line.substr(0, line.size() - 1);
241  }
242 
243  std::stringstream lineStream(line);
244 
245  if (line.substr(0, 1) == commentPrefix) {
246  continue;
247  } else {
248  dataCount++;
249  }
250 
251  std::getline(lineStream, cell, ',');
252 
253  // reading Mass
254  if (dataCount == 1) {
255  std::istringstream(cell) >> vehicleMass;
256  }
257 
258  // reading vehicle loading
259  if (dataCount == 2) {
260  std::istringstream(cell) >> vehicleLoading;
261  }
262 
263  // reading cWValue
264  if (dataCount == 3) {
265  std::istringstream(cell) >> cWValue;
266  }
267 
268  // reading crossectional area
269  if (dataCount == 4) {
270  std::istringstream(cell) >> crossArea;
271  }
272 
273  // reading vehicle mass rotational
274  if (dataCount == 7) {
275  std::istringstream(cell) >> vehicleMassRot;
276  }
277 
278  // reading rated power
279  if (dataCount == 10) {
280  std::istringstream(cell) >> ratedPower;
281  }
282 
283  // reading engine rated speed
284  if (dataCount == 11) {
285  std::istringstream(cell) >> engineRatedSpeed;
286  }
287 
288  // reading engine idling speed
289  if (dataCount == 12) {
290  std::istringstream(cell) >> engineIdlingSpeed;
291  }
292 
293  // reading f0
294  if (dataCount == 14) {
295  std::istringstream(cell) >> f0;
296  }
297 
298  // reading f1
299  if (dataCount == 15) {
300  std::istringstream(cell) >> f1;
301  }
302 
303  // reading f2
304  if (dataCount == 16) {
305  std::istringstream(cell) >> f2;
306  }
307 
308  // reading f3
309  if (dataCount == 17) {
310  std::istringstream(cell) >> f3;
311  }
312 
313  // reading f4
314  if (dataCount == 18) {
315  std::istringstream(cell) >> f4;
316  }
317  // reading axleRatio
318  if (dataCount == 21) {
319  std::istringstream(cell) >> axleRatio;
320  }
321 
322  // reading effective wheel diameter
323  if (dataCount == 22) {
324  std::istringstream(cell) >> effectiveWheelDiameter;
325  }
326 
327  // reading vehicleMassType
328  if (dataCount == 45) {
329  vehicleMassType = cell;
330  }
331 
332  // reading vehicleFuelType
333  if (dataCount == 46) {
334  vehicleFuelType = cell;
335  }
336 
337  // reading pNormV0
338  if (dataCount == 47) {
339  std::istringstream(cell) >> pNormV0;
340  }
341 
342  // reading pNormP0
343  if (dataCount == 48) {
344  std::istringstream(cell) >> pNormP0;
345  }
346 
347  // reading pNormV1
348  if (dataCount == 49) {
349  std::istringstream(cell) >> pNormV1;
350  }
351 
352  // reading pNormP1
353  if (dataCount == 50) {
354  std::istringstream(cell) >> pNormP1;
355  }
356  } // end while
357 
358  while (std::getline(fileVehicle, line) && line.substr(0, 1) != commentPrefix) {
359  std::stringstream lineStream(line);
360  std::string cell;
361  std::vector <double> vi;
362  while (std::getline(lineStream, cell, ',')) {
363  double entry;
364  std::istringstream(cell) >> entry;
365  vi.push_back(entry);
366 
367  } // end while
368  matrixSpeedInertiaTable.push_back(vi);
369  } // end while
370 
371  while (std::getline(fileVehicle, line)) {
372  if (line.substr(0, 1) == commentPrefix) {
373  continue;
374  }
375 
376  std::stringstream lineStream(line);
377  std::string cell;
378  std::vector <double> vi;
379  while (std::getline(lineStream, cell, ',')) {
380  double entry;
381  std::istringstream(cell) >> entry;
382  vi.push_back(entry);
383 
384  } // end while
385  normedDragTable.push_back(vi);
386  } // end while
387 
388 
389  fileVehicle.close();
390  return true;
391 } // end of ReadVehicleFile
392 
393 
394 bool PHEMCEPHandler::ReadEmissionData(bool readFC, const std::vector<std::string>& path, const std::string& emissionClass,
395  std::vector<std::string>& header, std::vector<std::vector<double> >& matrix, std::vector<double>& idlingValues) {
396 
397  std::string pollutantExtension = "";
398  if (readFC) {
399  pollutantExtension += "_FC";
400  }
401  // declare file stream
402  std::ifstream fileEmission;
403  for (std::vector<std::string>::const_iterator i = path.begin(); i != path.end(); i++) {
404  fileEmission.open(((*i) + emissionClass + pollutantExtension + ".csv").c_str());
405  if (fileEmission.good()) {
406  break;
407  }
408  }
409 
410  if (!fileEmission.good()) {
411  return false;
412  }
413 
414  std::string line;
415  std::string cell;
416  // read header line for pollutant identifiers
417  if (std::getline(fileEmission, line)) {
418  std::stringstream lineStream(line);
419 
420  // skip first entry "Pe"
421  std::getline(lineStream, cell, ',');
422 
423  while (std::getline(lineStream, cell, ',')) {
424  header.push_back(cell);
425  } // end while
426 
427  } // end if
428 
429  // skip units
430  std::getline(fileEmission, line);
431 
432  // skip comments
433  std::getline(fileEmission, line);
434 
435  // reading idlingValues
436  std::getline(fileEmission, line);
437 
438  std::stringstream idlingStream(line);
439  std::string idlingCell;
440 
441  //skipping idle comment
442  std::getline(idlingStream, idlingCell, ',');
443 
444  while (std::getline(idlingStream, idlingCell, ',')) {
445  double entry;
446  std::istringstream(idlingCell) >> entry;
447  idlingValues.push_back(entry);
448  } // end while
449 
450  while (std::getline(fileEmission, line)) {
451  std::stringstream lineStream(line);
452  std::string cell;
453  std::vector <double> vi;
454  while (std::getline(lineStream, cell, ',')) {
455  double entry;
456  std::istringstream(cell) >> entry;
457  vi.push_back(entry);
458 
459  } // end while
460  matrix.push_back(vi);
461  } // end while
462 
463  fileEmission.close();
464 
465  return true;
466 } // end of ReadEmissionData
467 
468 
469 /****************************************************************************/
Data Handler for a single CEP emission data set.
Definition: PHEMCEP.h:59
std::map< SUMOEmissionClass, PHEMCEP * > _ceps
bijection between PHEMEmissionClass and CEPs
bool ReadVehicleFile(const std::vector< std::string > &path, const std::string &emissionClass, double &vehicleMass, double &vehicleLoading, double &vehicleMassRot, double &crossArea, double &cWValue, double &f0, double &f1, double &f2, double &f3, double &f4, double &axleRatio, double &ratedPower, double &engineIdlingSpeed, double &engineRatedSpeed, double &effectiveWheelDiameter, 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)
Helper method to read a vehicle file from file system.
Data Handler for all CEP emission and vehicle Data.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
bool Load(SUMOEmissionClass emissionClass, const std::string &emissionClassIdentifier)
Helper method to load CEP and vehicle files from file system.
static PHEMCEPHandler & getHandlerInstance()
Implementatio of Singelton pattern.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool ReadEmissionData(bool readFC, const std::vector< std::string > &path, const std::string &emissionClass, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix, std::vector< double > &idlingValues)
Helper method to read a CEP file from file system.
int SUMOEmissionClass
~PHEMCEPHandler()
Destructor.
PHEMCEPHandler()
Implementation of Singelton pattern private (copy) constructor and =operator to avoid more than one i...
A storage for options typed value containers)
Definition: OptionsCont.h:99
PHEMCEP * GetCep(SUMOEmissionClass emissionClass)
Returns the CEP data for a PHEM emission class.