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