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