SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-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  // EOL handling for Linux
153  if (line.size() > 0 && line.substr(line.size() - 1) == "\r") {
154  line = line.substr(0, line.size() - 1);
155  }
156 
157  std::stringstream lineStream(line);
158 
159  if (line.substr(0, 1) == commentPrefix) {
160  continue;
161  } else {
162  dataCount++;
163  }
164 
165  std::getline(lineStream, cell, ',');
166 
167  // reading Mass
168  if (dataCount == 1) {
169  std::istringstream(cell) >> vehicleMass;
170  }
171 
172  // reading vehicle loading
173  if (dataCount == 2) {
174  std::istringstream(cell) >> vehicleLoading;
175  }
176 
177  // reading cWValue
178  if (dataCount == 3) {
179  std::istringstream(cell) >> cWValue;
180  }
181 
182  // reading crossectional area
183  if (dataCount == 4) {
184  std::istringstream(cell) >> crossArea;
185  }
186 
187  // reading vehicle mass rotational
188  if (dataCount == 7) {
189  std::istringstream(cell) >> vehicleMassRot;
190  }
191 
192  // reading rated power
193  if (dataCount == 10) {
194  std::istringstream(cell) >> ratedPower;
195  }
196 
197  // reading f0
198  if (dataCount == 14) {
199  std::istringstream(cell) >> f0;
200  }
201 
202  // reading f1
203  if (dataCount == 15) {
204  std::istringstream(cell) >> f1;
205  }
206 
207  // reading f2
208  if (dataCount == 16) {
209  std::istringstream(cell) >> f2;
210  }
211 
212  // reading f3
213  if (dataCount == 17) {
214  std::istringstream(cell) >> f3;
215  }
216 
217  // reading f4
218  if (dataCount == 18) {
219  std::istringstream(cell) >> f4;
220  }
221 
222  // reading vehicleMassType
223  if (dataCount == 45) {
224  vehicleMassType = cell;
225  }
226 
227  // reading vehicleFuelType
228  if (dataCount == 46) {
229  vehicleFuelType = cell;
230  }
231 
232  // reading pNormV0
233  if (dataCount == 47) {
234  std::istringstream(cell) >> pNormV0;
235  }
236 
237  // reading pNormP0
238  if (dataCount == 48) {
239  std::istringstream(cell) >> pNormP0;
240  }
241 
242  // reading pNormV1
243  if (dataCount == 49) {
244  std::istringstream(cell) >> pNormV1;
245  }
246 
247  // reading pNormP1
248  if (dataCount == 50) {
249  std::istringstream(cell) >> pNormP1;
250  }
251  } // end while
252 
253  while (std::getline(fileVehicle, line)) {
254  std::stringstream lineStream(line);
255  std::string cell;
256  std::vector <double> vi;
257  while (std::getline(lineStream, cell, ',')) {
258  double entry;
259  std::istringstream(cell) >> entry;
260  vi.push_back(entry);
261 
262  } // end while
263  matrixRotFactor.push_back(vi);
264  } // end while
265 
266 
267  fileVehicle.close();
268  return true;
269 } // end of ReadVehicleFile
270 
271 
272 bool
273 PHEMCEPHandler::ReadEmissionData(bool readFC, const std::string& path, const std::string& emissionClass,
274  std::vector<std::string>& header, std::vector<std::vector<double> >& matrix) {
275 
276 
277  std::string pollutantExtension = "";
278  if (readFC) {
279  pollutantExtension += "_FC";
280  }
281  // declare file stream
282  std::ifstream fileEmission(std::string(path + emissionClass + pollutantExtension + ".csv").c_str());
283 
284  if (!fileEmission.good()) {
285  return false;
286  }
287 
288  std::string line;
289  std::string cell;
290  // read header line for pollutant identifiers
291  if (std::getline(fileEmission, line)) {
292  std::stringstream lineStream(line);
293 
294  // skip first entry "Pe"
295  std::getline(lineStream, cell, ',');
296 
297  while (std::getline(lineStream, cell, ',')) {
298  header.push_back(cell);
299  } // end while
300 
301  } // end if
302 
303  // skip units
304  std::getline(fileEmission, line);
305 
306  while (std::getline(fileEmission, line)) {
307  std::stringstream lineStream(line);
308  std::string cell;
309  std::vector <double> vi;
310  while (std::getline(lineStream, cell, ',')) {
311  double entry;
312  std::istringstream(cell) >> entry;
313  vi.push_back(entry);
314 
315  } // end while
316  matrix.push_back(vi);
317  } // end while
318 
319  fileEmission.close();
320 
321  return true;
322 } // end of ReadEmissionData
323 
324 
325 /****************************************************************************/
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)
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:108
PHEMCEP * GetCep(SUMOEmissionClass emissionClass)
Returns the CEP data for a PHEM emission class.