Eclipse SUMO - Simulation of Urban MObility
CEPHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2016-2019 German Aerospace Center (DLR) and others.
4 // PHEMlight module
5 // Copyright 2016 Technische Universitaet Graz, https://www.tugraz.at/
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 // SPDX-License-Identifier: EPL-2.0
11 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 
21 #include <fstream>
22 #include <sstream>
23 #include "CEPHandler.h"
24 #include "CEP.h"
25 #include "Helpers.h"
26 #include "Constants.h"
27 
28 
29 namespace PHEMlightdll {
30 
32  _ceps = std::map<std::string, CEP*>();
33  }
34 
35  const std::map<std::string, CEP*>& CEPHandler::getCEPS() const {
36  return _ceps;
37  }
38 
39  bool CEPHandler::GetCEP(const std::vector<std::string>& DataPath, Helpers* Helper) {
40  if (getCEPS().find(Helper->getgClass()) == getCEPS().end()) {
41  if (!Load(DataPath, Helper)) {
42  return false;
43  }
44  }
45  return true;
46  }
47 
48  bool CEPHandler::Load(const std::vector<std::string>& DataPath, Helpers* Helper) {
49  //Deklaration
50  // get string identifier for PHEM emission class
51 //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'ToString':
52  std::string emissionRep = Helper->getgClass();
53 
54  // to hold everything.
55  std::vector<std::vector<double> > matrixSpeedInertiaTable;
56  std::vector<std::vector<double> > normedTragTableSpeedInertiaTable;
57  std::vector<std::vector<double> > matrixFC;
58  std::vector<std::vector<double> > matrixPollutants;
59  std::vector<double> idlingValuesFC;
60  std::vector<double> idlingValuesPollutants;
61  std::vector<std::string> headerFC;
62  std::vector<std::string> headerPollutants;
63 
64  double vehicleMass;
65  double vehicleLoading;
66  double vehicleMassRot;
67  double crosssectionalArea;
68  double cwValue;
69  double f0;
70  double f1;
71  double f2;
72  double f3;
73  double f4;
74  double axleRatio;
75  std::vector<double> transmissionGearRatios;
76  double auxPower;
77  double ratedPower;
78  double engineIdlingSpeed;
79  double engineRatedSpeed;
80  double effectiveWhellDiameter;
81  std::string vehicleMassType;
82  std::string vehicleFuelType;
83  double pNormV0;
84  double pNormP0;
85  double pNormV1;
86  double pNormP1;
87 
88  if (!ReadVehicleFile(DataPath, emissionRep, Helper, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, axleRatio, auxPower, ratedPower, engineIdlingSpeed, engineRatedSpeed, effectiveWhellDiameter, transmissionGearRatios, vehicleMassType, vehicleFuelType, pNormV0, pNormP0, pNormV1, pNormP1, matrixSpeedInertiaTable, normedTragTableSpeedInertiaTable)) {
89  return false;
90  }
91 
92  if (!ReadEmissionData(true, DataPath, emissionRep, Helper, headerFC, matrixFC, idlingValuesFC)) {
93  return false;
94  }
95 
96  if (!ReadEmissionData(false, DataPath, emissionRep, Helper, headerPollutants, matrixPollutants, idlingValuesPollutants)) {
97  return false;
98  }
99 
100  _ceps.insert(std::make_pair(Helper->getgClass(), new CEP(vehicleMassType == Constants::HeavyVehicle, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, axleRatio, transmissionGearRatios, auxPower, ratedPower, engineIdlingSpeed, engineRatedSpeed, effectiveWhellDiameter, pNormV0, pNormP0, pNormV1, pNormP1, vehicleFuelType, matrixFC, headerPollutants, matrixPollutants, matrixSpeedInertiaTable, normedTragTableSpeedInertiaTable, idlingValuesFC.front(), idlingValuesPollutants)));
101 
102  return true;
103  }
104 
105  bool CEPHandler::ReadVehicleFile(const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, double& vehicleMass, double& vehicleLoading, double& vehicleMassRot, double& crossArea, double& cWValue, double& f0, double& f1, double& f2, double& f3, double& f4, double& axleRatio, double& auxPower, double& ratedPower, double& engineIdlingSpeed, double& engineRatedSpeed, double& effectiveWheelDiameter, std::vector<double>& transmissionGearRatios, 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) {
106  vehicleMass = 0;
107  vehicleLoading = 0;
108  vehicleMassRot = 0;
109  crossArea = 0;
110  cWValue = 0;
111  f0 = 0;
112  f1 = 0;
113  f2 = 0;
114  f3 = 0;
115  f4 = 0;
116  axleRatio = 0;
117  ratedPower = 0;
118  auxPower = 0;
119  engineIdlingSpeed = 0;
120  engineRatedSpeed = 0;
121  effectiveWheelDiameter = 0;
122  vehicleMassType = "";
123  vehicleFuelType = "";
124  pNormV0 = 0;
125  pNormP0 = 0;
126  pNormV1 = 0;
127  pNormP1 = 0;
128  transmissionGearRatios = std::vector<double>();
129  matrixSpeedInertiaTable = std::vector<std::vector<double> >();
130  normedDragTable = std::vector<std::vector<double> >();
131  std::string line;
132  std::string cell;
133  int dataCount = 0;
134 
135  //Open file
136  std::ifstream vehicleReader;
137  for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {
138  vehicleReader.open(((*i) + emissionClass + ".PHEMLight.veh").c_str());
139  if (vehicleReader.good()) {
140  break;
141  }
142  }
143  if (!vehicleReader.good()) {
144  Helper->setErrMsg("File does not exist! (" + emissionClass + ".PHEMLight.veh)");
145  return false;
146  }
147 
148  // skip header
149  ReadLine(vehicleReader);
150 
151  while ((line = ReadLine(vehicleReader)) != "" && dataCount <= 49) {
152  if (line.substr(0, 1) == Helper->getCommentPrefix()) {
153  continue;
154  }
155  else {
156  dataCount++;
157  }
158 
159  cell = split(line, ',')[0];
160 
161  // reading Mass
162  if (dataCount == 1) {
163  vehicleMass = todouble(cell);
164  }
165 
166  // reading vehicle loading
167  if (dataCount == 2) {
168  vehicleLoading = todouble(cell);
169  }
170 
171  // reading cWValue
172  if (dataCount == 3) {
173  cWValue = todouble(cell);
174  }
175 
176  // reading crossectional area
177  if (dataCount == 4) {
178  crossArea = todouble(cell);
179  }
180 
181  // reading vehicle mass rotational
182  if (dataCount == 7) {
183  vehicleMassRot = todouble(cell);
184  }
185 
186  // reading rated power
187  if (dataCount == 9) {
188  auxPower = todouble(cell);
189  }
190 
191  // reading rated power
192  if (dataCount == 10) {
193  ratedPower = todouble(cell);
194  }
195 
196  // reading engine rated speed
197  if (dataCount == 11) {
198  engineRatedSpeed = todouble(cell);
199  }
200 
201  // reading engine idling speed
202  if (dataCount == 12) {
203  engineIdlingSpeed = todouble(cell);
204  }
205 
206  // reading f0
207  if (dataCount == 14) {
208  f0 = todouble(cell);
209  }
210 
211  // reading f1
212  if (dataCount == 15) {
213  f1 = todouble(cell);
214  }
215 
216  // reading f2
217  if (dataCount == 16) {
218  f2 = todouble(cell);
219  }
220 
221  // reading f3
222  if (dataCount == 17) {
223  f3 = todouble(cell);
224  }
225 
226  // reading f4
227  if (dataCount == 18) {
228  f4 = todouble(cell);
229  }
230 
231  // reading axleRatio
232  if (dataCount == 21) {
233  axleRatio = todouble(cell);
234  }
235 
236  // reading effective wheel diameter
237  if (dataCount == 22) {
238  effectiveWheelDiameter = todouble(cell);
239  }
240 
241  if (dataCount >= 23 && dataCount <= 40) {
242  transmissionGearRatios.push_back(todouble(cell));
243  }
244 
245  // reading vehicleMassType
246  if (dataCount == 45) {
247  vehicleMassType = cell;
248  }
249 
250  // reading vehicleFuelType
251  if (dataCount == 46) {
252  vehicleFuelType = cell;
253  }
254 
255  // reading pNormV0
256  if (dataCount == 47) {
257  pNormV0 = todouble(cell);
258  }
259 
260  // reading pNormP0
261  if (dataCount == 48) {
262  pNormP0 = todouble(cell);
263  }
264 
265  // reading pNormV1
266  if (dataCount == 49) {
267  pNormV1 = todouble(cell);
268  }
269 
270  // reading pNormP1
271  if (dataCount == 50) {
272  pNormP1 = todouble(cell);
273  }
274  }
275 
276  while ((line = ReadLine(vehicleReader)) != "" && line.substr(0, 1) != Helper->getCommentPrefix()) {
277  if (line.substr(0, 1) == Helper->getCommentPrefix()) {
278  continue;
279  }
280 
281  matrixSpeedInertiaTable.push_back(todoubleList(split(line, ',')));
282  }
283 
284  while ((line = ReadLine(vehicleReader)) != "") {
285  if (line.substr(0, 1) == Helper->getCommentPrefix()) {
286  continue;
287  }
288 
289  normedDragTable.push_back(todoubleList(split(line, ',')));
290  }
291 
292  return true;
293  }
294 
295  bool CEPHandler::ReadEmissionData(bool readFC, const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, std::vector<std::string>& header, std::vector<std::vector<double> >& matrix, std::vector<double>& idlingValues) {
296  // declare file stream
297  std::string line;
298  header = std::vector<std::string>();
299  matrix = std::vector<std::vector<double> >();
300  idlingValues = std::vector<double>();
301 
302  std::string pollutantExtension = "";
303  if (readFC) {
304  pollutantExtension += std::string("_FC");
305  }
306 
307  std::ifstream fileReader;
308  for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {
309  fileReader.open(((*i) + emissionClass + pollutantExtension + ".csv").c_str());
310  if (fileReader.good()) {
311  break;
312  }
313  }
314  if (!fileReader.good()) {
315  Helper->setErrMsg("File does not exist! (" + emissionClass + pollutantExtension + ".csv)");
316  return false;
317  }
318 
319  // read header line for pollutant identifiers
320  if ((line = ReadLine(fileReader)) != "") {
321  std::vector<std::string> entries = split(line, ',');
322  // skip first entry "Pe"
323  for (int i = 1; i < (int)entries.size(); i++) {
324  header.push_back(entries[i]);
325  }
326  }
327 
328  // skip units
329  ReadLine(fileReader);
330 
331  // skip comment
332  ReadLine(fileReader);
333 
334  //readIdlingValues
335  line = ReadLine(fileReader);
336 
337  std::vector<std::string> stringIdlings = split(line, ',');
338  stringIdlings.erase(stringIdlings.begin());
339 
340  idlingValues = todoubleList(stringIdlings);
341 
342  while ((line = ReadLine(fileReader)) != "") {
343  matrix.push_back(todoubleList(split(line, ',')));
344  }
345  return true;
346  }
347 
348  std::vector<std::string> CEPHandler::split(const std::string& s, char delim) {
349  std::vector<std::string> elems;
350  std::stringstream ss(s);
351  std::string item;
352  while (std::getline(ss, item, delim)) {
353  elems.push_back(item);
354  }
355  return elems;
356  }
357 
358  double CEPHandler::todouble(const std::string& s) {
359  std::stringstream ss(s);
360  double item;
361  ss >> item;
362  return item;
363  }
364 
365  std::vector<double> CEPHandler::todoubleList(const std::vector<std::string>& s) {
366  std::vector<double> result;
367  for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); ++i) {
368  result.push_back(todouble(*i));
369  }
370  return result;
371  }
372 
373  std::string CEPHandler::ReadLine(std::ifstream& s) {
374  std::string line;
375  std::getline(s, line);
376  line.erase(line.find_last_not_of(" \n\r\t") + 1);
377  return line;
378  }
379 }
PHEMlightdll::CEPHandler::GetCEP
bool GetCEP(const std::vector< std::string > &DataPath, Helpers *Helper)
Definition: CEPHandler.cpp:39
PHEMlightdll::CEPHandler::getCEPS
const std::map< std::string, CEP * > & getCEPS() const
Definition: CEPHandler.cpp:35
PHEMlightdll::CEPHandler::Load
bool Load(const std::vector< std::string > &DataPath, Helpers *Helper)
Definition: CEPHandler.cpp:48
PHEMlightdll::CEPHandler::ReadLine
std::string ReadLine(std::ifstream &s)
Definition: CEPHandler.cpp:373
PHEMlightdll::CEPHandler::_ceps
std::map< std::string, CEP * > _ceps
Definition: CEPHandler.h:48
Helpers.h
PHEMlightdll::Helpers::setErrMsg
void setErrMsg(const std::string &value)
Definition: Helpers.cpp:71
PHEMlightdll::CEPHandler::CEPHandler
CEPHandler()
Definition: CEPHandler.cpp:31
PHEMlightdll
Definition: CEP.cpp:26
PHEMlightdll::CEPHandler::todoubleList
std::vector< double > todoubleList(const std::vector< std::string > &s)
Definition: CEPHandler.cpp:365
PHEMlightdll::Helpers::getCommentPrefix
const std::string & getCommentPrefix() const
Definition: Helpers.cpp:75
PHEMlightdll::CEPHandler::todouble
double todouble(const std::string &s)
Definition: CEPHandler.cpp:358
PHEMlightdll::Constants::HeavyVehicle
static const std::string HeavyVehicle
Definition: Constants.h:43
PHEMlightdll::Helpers
Definition: Helpers.h:28
PHEMlightdll::Helpers::getgClass
const std::string & getgClass() const
Definition: Helpers.cpp:59
PHEMlightdll::CEPHandler::ReadEmissionData
bool ReadEmissionData(bool readFC, const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix, std::vector< double > &idlingValues)
Definition: CEPHandler.cpp:295
CEPHandler.h
Constants.h
PHEMlightdll::CEP
Definition: CEP.h:36
PHEMlightdll::CEPHandler::split
std::vector< std::string > split(const std::string &s, char delim)
Definition: CEPHandler.cpp:348
CEP.h
PHEMlightdll::CEPHandler::ReadVehicleFile
bool ReadVehicleFile(const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, double &vehicleMass, double &vehicleLoading, double &vehicleMassRot, double &crossArea, double &cWValue, double &f0, double &f1, double &f2, double &f3, double &f4, double &axleRatio, double &auxPower, double &ratedPower, double &engineIdlingSpeed, double &engineRatedSpeed, double &effectiveWheelDiameter, std::vector< double > &transmissionGearRatios, 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)
Definition: CEPHandler.cpp:105