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 /****************************************************************************/
7 // Helper class for PHEM Light, holds CEP data for emission computation
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <fstream>
31 #include <sstream>
32 #include <string>
33 #include <vector>
34 #include "PHEMCEPHandler.h"
35 #include "PHEMConstants.h"
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
43 }
44 
45 
47  std::map<SUMOEmissionClass, PHEMCEP*>::iterator iter = _ceps.begin();
48  while (iter != _ceps.end()) {
49  delete(iter->second);
50  iter++;
51  } // end while
52  _ceps.clear();
53 }
54 
55 
58  static PHEMCEPHandler instance;
59  return instance;
60 }
61 
62 
63 bool
65  // get string identifier for PHEM emission class
66  std::string emissionClassIdentifier = SumoEmissionClassStrings.getString(emissionClass);
67 
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,
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  if (!PHEMCEPHandler::Load(emissionClass)) {
125  throw InvalidArgument("File for PHEM emission class " + SumoEmissionClassStrings.getString(emissionClass) + " not found.");
126  }
127  } // end if
128 
129  return _ceps[emissionClass];
130 } // end of GetCep
131 
132 
133 bool
134 PHEMCEPHandler::ReadVehicleFile(const std::string& path, const std::string& emissionClass,
135  double& vehicleMass, double& vehicleLoading, double& vehicleMassRot,
136  double& crossArea, double& cWValue,
137  double& f0, double& f1, double& f2, double& f3, double& f4, double& ratedPower, std::string& vehicleMassType, std::string& vehicleFuelType,
138  double& pNormV0, double& pNormP0, double& pNormV1, double& pNormP1, std::vector< std::vector<double> >& matrixRotFactor) {
139  std::ifstream fileVehicle(std::string(path + emissionClass + ".veh").c_str());
140 
141  if (!fileVehicle.good()) {
142  return false;
143  }
144 
145  std::string line;
146  std::string cell;
147  std::string commentPrefix = "c";
148  int dataCount = 0;
149 
150  // skip header
151  std::getline(fileVehicle, line);
152 
153  while (std::getline(fileVehicle, line) && dataCount <= 49) {
154  std::stringstream lineStream(line);
155 
156  if (line.substr(0, 1) == commentPrefix) {
157  continue;
158  } else {
159  dataCount++;
160  }
161 
162  std::getline(lineStream, cell, ',');
163 
164  // reading Mass
165  if (dataCount == 1) {
166  std::istringstream(cell) >> vehicleMass;
167  }
168 
169  // reading vehicle loading
170  if (dataCount == 2) {
171  std::istringstream(cell) >> vehicleLoading;
172  }
173 
174  // reading cWValue
175  if (dataCount == 3) {
176  std::istringstream(cell) >> cWValue;
177  }
178 
179  // reading crossectional area
180  if (dataCount == 4) {
181  std::istringstream(cell) >> crossArea;
182  }
183 
184  // reading vehicle mass rotational
185  if (dataCount == 7) {
186  std::istringstream(cell) >> vehicleMassRot;
187  }
188 
189  // reading rated power
190  if (dataCount == 10) {
191  std::istringstream(cell) >> ratedPower;
192  }
193 
194  // reading f0
195  if (dataCount == 14) {
196  std::istringstream(cell) >> f0;
197  }
198 
199  // reading f1
200  if (dataCount == 15) {
201  std::istringstream(cell) >> f1;
202  }
203 
204  // reading f2
205  if (dataCount == 16) {
206  std::istringstream(cell) >> f2;
207  }
208 
209  // reading f3
210  if (dataCount == 17) {
211  std::istringstream(cell) >> f3;
212  }
213 
214  // reading f4
215  if (dataCount == 18) {
216  std::istringstream(cell) >> f4;
217  }
218 
219  // reading vehicleMassType
220  if (dataCount == 45) {
221  vehicleMassType = cell;
222  }
223 
224  // reading vehicleFuelType
225  if (dataCount == 46) {
226  vehicleFuelType = cell;
227  }
228 
229  // reading pNormV0
230  if (dataCount == 47) {
231  std::istringstream(cell) >> pNormV0;
232  }
233 
234  // reading pNormP0
235  if (dataCount == 48) {
236  std::istringstream(cell) >> pNormP0;
237  }
238 
239  // reading pNormV1
240  if (dataCount == 49) {
241  std::istringstream(cell) >> pNormV1;
242  }
243 
244  // reading pNormP1
245  if (dataCount == 50) {
246  std::istringstream(cell) >> pNormP1;
247  }
248  } // end while
249 
250  while (std::getline(fileVehicle, line)) {
251  std::stringstream lineStream(line);
252  std::string cell;
253  std::vector <double> vi;
254  while (std::getline(lineStream, cell, ',')) {
255  double entry;
256  std::istringstream(cell) >> entry;
257  vi.push_back(entry);
258 
259  } // end while
260  matrixRotFactor.push_back(vi);
261  } // end while
262 
263 
264  fileVehicle.close();
265  return true;
266 } // end of ReadVehicleFile
267 
268 
269 bool
270 PHEMCEPHandler::ReadEmissionData(bool readFC, const std::string& path, const std::string& emissionClass,
271  std::vector<std::string>& header, std::vector<std::vector<double> >& matrix) {
272 
273 
274  std::string pollutantExtension = "";
275  if (readFC) {
276  pollutantExtension += "_FC";
277  }
278  // declare file stream
279  std::ifstream fileEmission(std::string(path + emissionClass + pollutantExtension + ".csv").c_str());
280 
281  if (!fileEmission.good()) {
282  return false;
283  }
284 
285  std::string line;
286  std::string cell;
287  // read header line for pollutant identifiers
288  if (std::getline(fileEmission, line)) {
289  std::stringstream lineStream(line);
290 
291  // skip first entry "Pe"
292  std::getline(lineStream, cell, ',');
293 
294  while (std::getline(lineStream, cell, ',')) {
295  header.push_back(cell);
296  } // end while
297 
298  } // end if
299 
300  // skip units
301  std::getline(fileEmission, line);
302 
303  while (std::getline(fileEmission, line)) {
304  std::stringstream lineStream(line);
305  std::string cell;
306  std::vector <double> vi;
307  while (std::getline(lineStream, cell, ',')) {
308  double entry;
309  std::istringstream(cell) >> entry;
310  vi.push_back(entry);
311 
312  } // end while
313  matrix.push_back(vi);
314  } // end while
315 
316  fileEmission.close();
317 
318  return true;
319 } // end of ReadEmissionData
320 
321 
322 /****************************************************************************/
Data Handler for a single CEP emission data set.
Definition: PHEMCEP.h:46
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.
StringBijection< SUMOEmissionClass > SumoEmissionClassStrings(SumoEmissionClassStringInitializer, SVE_Solo_LKW_D_EU6_II)
bool Load(SUMOEmissionClass emissionClass)
Helper method to load CEP and vehicle files 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
static PHEMCEPHandler & getHandlerInstance()
Implementatio of Singelton pattern.
SUMOEmissionClass
Definition of vehicle emission classes.
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.