SUMO - Simulation of Urban MObility
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-sim.org/
13 // Copyright (C) 2013-2014 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 <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> > matrixFC;
71  std::vector< std::vector<double> > matrixPollutants;
72  std::vector<std::string> headerFC;
73  std::vector<std::string> headerPollutants;
74 
75  double vehicleMass;
76  double vehicleLoading;
77  double vehicleMassRot;
78  double crosssectionalArea;
79  double cwValue;
80  double f0;
81  double f1;
82  double f2;
83  double f3;
84  double f4;
85  double ratedPower;
86  std::string vehicleMassType;
87  std::string vehicleFuelType;
88  double pNormV0;
89  double pNormP0;
90  double pNormV1;
91  double pNormP1;
92 
94  std::string phemPath = oc.getString("phemlight-path") + "/";
95  if (!ReadVehicleFile(phemPath, emissionClassIdentifier, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, ratedPower, vehicleMassType, vehicleFuelType,
96  pNormV0, pNormP0, pNormV1, pNormP1, matrixSpeedInertiaTable)) {
97  return false;
98  }
99 
100  if (!ReadEmissionData(true, phemPath, emissionClassIdentifier, headerFC, matrixFC)) {
101  return false;
102  }
103 
104  if (!ReadEmissionData(false, phemPath, emissionClassIdentifier, headerPollutants, matrixPollutants)) {
105  return false;
106  }
107 
108  _ceps[emissionClass] = new PHEMCEP(vehicleMassType == "HV",
109  emissionClass, emissionClassIdentifier,
110  vehicleMass, vehicleLoading, vehicleMassRot,
111  crosssectionalArea, cwValue,
112  f0, f1, f2, f3, f4,
113  ratedPower, pNormV0, pNormP0, pNormV1, pNormP1,
114  vehicleFuelType, matrixFC, headerPollutants, matrixPollutants, matrixSpeedInertiaTable);
115 
116  return true;
117 } // end of Load()
118 
119 
120 PHEMCEP*
122  // check if Cep has been loaded
123  if (_ceps.find(emissionClass) == _ceps.end()) {
124  return 0;
125  } // end if
126 
127  return _ceps[emissionClass];
128 } // end of GetCep
129 
130 
131 bool
132 PHEMCEPHandler::ReadVehicleFile(const std::string& path, const std::string& emissionClass,
133  double& vehicleMass, double& vehicleLoading, double& vehicleMassRot,
134  double& crossArea, double& cWValue,
135  double& f0, double& f1, double& f2, double& f3, double& f4, double& ratedPower, std::string& vehicleMassType, std::string& vehicleFuelType,
136  double& pNormV0, double& pNormP0, double& pNormV1, double& pNormP1, std::vector< std::vector<double> >& matrixRotFactor) {
137  std::ifstream fileVehicle(std::string(path + emissionClass + ".veh").c_str());
138 
139  if (!fileVehicle.good()) {
140  return false;
141  }
142 
143  std::string line;
144  std::string cell;
145  std::string commentPrefix = "c";
146  int dataCount = 0;
147 
148  // skip header
149  std::getline(fileVehicle, line);
150 
151  while (std::getline(fileVehicle, line) && dataCount <= 49) {
152  std::stringstream lineStream(line);
153 
154  if (line.substr(0, 1) == commentPrefix) {
155  continue;
156  } else {
157  dataCount++;
158  }
159 
160  std::getline(lineStream, cell, ',');
161 
162  // reading Mass
163  if (dataCount == 1) {
164  std::istringstream(cell) >> vehicleMass;
165  }
166 
167  // reading vehicle loading
168  if (dataCount == 2) {
169  std::istringstream(cell) >> vehicleLoading;
170  }
171 
172  // reading cWValue
173  if (dataCount == 3) {
174  std::istringstream(cell) >> cWValue;
175  }
176 
177  // reading crossectional area
178  if (dataCount == 4) {
179  std::istringstream(cell) >> crossArea;
180  }
181 
182  // reading vehicle mass rotational
183  if (dataCount == 7) {
184  std::istringstream(cell) >> vehicleMassRot;
185  }
186 
187  // reading rated power
188  if (dataCount == 10) {
189  std::istringstream(cell) >> ratedPower;
190  }
191 
192  // reading f0
193  if (dataCount == 14) {
194  std::istringstream(cell) >> f0;
195  }
196 
197  // reading f1
198  if (dataCount == 15) {
199  std::istringstream(cell) >> f1;
200  }
201 
202  // reading f2
203  if (dataCount == 16) {
204  std::istringstream(cell) >> f2;
205  }
206 
207  // reading f3
208  if (dataCount == 17) {
209  std::istringstream(cell) >> f3;
210  }
211 
212  // reading f4
213  if (dataCount == 18) {
214  std::istringstream(cell) >> f4;
215  }
216 
217  // reading vehicleMassType
218  if (dataCount == 45) {
219  vehicleMassType = cell;
220  }
221 
222  // reading vehicleFuelType
223  if (dataCount == 46) {
224  vehicleFuelType = cell;
225  }
226 
227  // reading pNormV0
228  if (dataCount == 47) {
229  std::istringstream(cell) >> pNormV0;
230  }
231 
232  // reading pNormP0
233  if (dataCount == 48) {
234  std::istringstream(cell) >> pNormP0;
235  }
236 
237  // reading pNormV1
238  if (dataCount == 49) {
239  std::istringstream(cell) >> pNormV1;
240  }
241 
242  // reading pNormP1
243  if (dataCount == 50) {
244  std::istringstream(cell) >> pNormP1;
245  }
246  } // end while
247 
248  while (std::getline(fileVehicle, line)) {
249  std::stringstream lineStream(line);
250  std::string cell;
251  std::vector <double> vi;
252  while (std::getline(lineStream, cell, ',')) {
253  double entry;
254  std::istringstream(cell) >> entry;
255  vi.push_back(entry);
256 
257  } // end while
258  matrixRotFactor.push_back(vi);
259  } // end while
260 
261 
262  fileVehicle.close();
263  return true;
264 } // end of ReadVehicleFile
265 
266 
267 bool
268 PHEMCEPHandler::ReadEmissionData(bool readFC, const std::string& path, const std::string& emissionClass,
269  std::vector<std::string>& header, std::vector<std::vector<double> >& matrix) {
270 
271 
272  std::string pollutantExtension = "";
273  if (readFC) {
274  pollutantExtension += "_FC";
275  }
276  // declare file stream
277  std::ifstream fileEmission(std::string(path + emissionClass + pollutantExtension + ".csv").c_str());
278 
279  if (!fileEmission.good()) {
280  return false;
281  }
282 
283  std::string line;
284  std::string cell;
285  // read header line for pollutant identifiers
286  if (std::getline(fileEmission, line)) {
287  std::stringstream lineStream(line);
288 
289  // skip first entry "Pe"
290  std::getline(lineStream, cell, ',');
291 
292  while (std::getline(lineStream, cell, ',')) {
293  header.push_back(cell);
294  } // end while
295 
296  } // end if
297 
298  // skip units
299  std::getline(fileEmission, line);
300 
301  while (std::getline(fileEmission, line)) {
302  std::stringstream lineStream(line);
303  std::string cell;
304  std::vector <double> vi;
305  while (std::getline(lineStream, cell, ',')) {
306  double entry;
307  std::istringstream(cell) >> entry;
308  vi.push_back(entry);
309 
310  } // end while
311  matrix.push_back(vi);
312  } // end while
313 
314  fileEmission.close();
315 
316  return true;
317 } // end of ReadEmissionData
318 
319 
320 /****************************************************************************/
Data Handler for a single CEP emission data set.
Definition: PHEMCEP.h:49
std::map< SUMOEmissionClass, PHEMCEP * > _ceps
bijection between PHEMEmissionClass and CEPs
bool ReadEmissionData(bool readFC, const std::string &path, const std::string &emissionClass, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix)
Helper method to read a CEP file from file system.
Data Handler for all CEP emission and vehicle Data.
bool ReadVehicleFile(const 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 &ratedPower, std::string &vehicleMassType, std::string &vehicleFuelType, double &pNormV0, double &pNormP0, double &pNormV1, double &pNormP1, std::vector< std::vector< double > > &matrixRotFactor)
Helper method to read a vehicle file from file system.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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)
~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:108
PHEMCEP * GetCep(SUMOEmissionClass emissionClass)
Returns the CEP data for a PHEM emission class.