SUMO - Simulation of Urban MObility
CEP.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 //
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2016-2017 DLR (http://www.dlr.de/) and contributors
12 // PHEMlight module
13 // Copyright 2016 Technische Universitaet Graz, https://www.tugraz.at/
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 #include "CEP.h"
26 #include "Constants.h"
27 #include "Helpers.h"
28 
29 
30 namespace PHEMlightdll {
31 
32  CEP::CEP(bool heavyVehicle, double vehicleMass, double vehicleLoading, double vehicleMassRot, double crossArea, double cWValue, double f0, double f1, double f2, double f3, double f4, double axleRatio, std::vector<double>& transmissionGearRatios, double auxPower, double ratedPower, double engineIdlingSpeed, double engineRatedSpeed, double effictiveWheelDiameter, double pNormV0, double pNormP0, double pNormV1, double pNormP1, const std::string& vehicelFuelType, std::vector<std::vector<double> >& matrixFC, std::vector<std::string>& headerLinePollutants, std::vector<std::vector<double> >& matrixPollutants, std::vector<std::vector<double> >& matrixSpeedRotational, std::vector<std::vector<double> >& normedDragTable, double idlingFC, std::vector<double>& idlingPollutants) {
33  transmissionGearRatios.size(); // just to make the compiler happy about the unused parameter
35  _resistanceF0 = f0;
36  _resistanceF1 = f1;
37  _resistanceF2 = f2;
38  _resistanceF3 = f3;
39  _resistanceF4 = f4;
40  _cWValue = cWValue;
41  _crossSectionalArea = crossArea;
42  _massVehicle = vehicleMass;
43  _vehicleLoading = vehicleLoading;
44  _vehicleMassRot = vehicleMassRot;
45  _ratedPower = ratedPower;
46  _engineIdlingSpeed = engineIdlingSpeed;
47  _engineRatedSpeed = engineRatedSpeed;
48  _effectiveWheelDiameter = effictiveWheelDiameter;
49  _heavyVehicle = heavyVehicle;
50  _fuelType = vehicelFuelType;
51  _axleRatio = axleRatio;
52  _auxPower = auxPower;
53 
54  _pNormV0 = pNormV0 / 3.6;
55  _pNormP0 = pNormP0;
56  _pNormV1 = pNormV1 / 3.6;
57  _pNormP1 = pNormP1;
58 
59  std::vector<std::string> pollutantIdentifier;
60  std::vector<std::vector<double> > pollutantMeasures;
61  std::vector<std::vector<double> > normalizedPollutantMeasures;
62 
63  // init pollutant identifiers
64  for (int i = 0; i < (int)headerLinePollutants.size(); i++) {
65  pollutantIdentifier.push_back(headerLinePollutants[i]);
66  }
67 
68  // initialize measures
69  for (int i = 0; i < (int)headerLinePollutants.size(); i++) {
70  pollutantMeasures.push_back(std::vector<double>());
71  normalizedPollutantMeasures.push_back(std::vector<double>());
72  }
73 
74  // looping through matrix and assigning values for speed rotational table
75  _speedCurveRotational = std::vector<double>();
76  _speedPatternRotational = std::vector<double>();
77  _gearTransmissionCurve = std::vector<double>();
78  for (int i = 0; i < (int)matrixSpeedRotational.size(); i++) {
79  if (matrixSpeedRotational[i].size() != 3) {
80  return;
81  }
82 
83  _speedPatternRotational.push_back(matrixSpeedRotational[i][0] / 3.6);
84  _gearTransmissionCurve.push_back(matrixSpeedRotational[i][1]);
85  _speedCurveRotational.push_back(matrixSpeedRotational[i][2]);
86  }
87 
88  // looping through matrix and assigning values for drag table
89  _nNormTable = std::vector<double>();
90  _dragNormTable = std::vector<double>();
91  for (int i = 0; i < (int)normedDragTable.size(); i++) {
92  if (normedDragTable[i].size() != 2) {
93  return;
94  }
95 
96  _nNormTable.push_back(normedDragTable[i][0]);
97  _dragNormTable.push_back(normedDragTable[i][1]);
98  }
99 
100  // looping through matrix and assigning values for Fuel consumption
101  _cepCurveFC = std::vector<double>();
102  _normedCepCurveFC = std::vector<double>();
103  _powerPatternFC = std::vector<double>();
104  _normalizedPowerPatternFC = std::vector<double>();
105  for (int i = 0; i < (int)matrixFC.size(); i++) {
106  if (matrixFC[i].size() != 2) {
107  return;
108  }
109 
110  _powerPatternFC.push_back(matrixFC[i][0] * _ratedPower);
111  _normalizedPowerPatternFC.push_back(matrixFC[i][0]);
112  _cepCurveFC.push_back(matrixFC[i][1] * _ratedPower);
113  _normedCepCurveFC.push_back(matrixFC[i][1]);
114 
115  }
116 
117  _powerPatternPollutants = std::vector<double>();
118 
119  double pollutantMultiplyer = 1;
120 
122 
123  // looping through matrix and assigning values for pollutants
124  if (heavyVehicle) {
127  pollutantMultiplyer = _ratedPower;
128  }
129  else {
132  }
133 
134  _normailzedPowerPatternPollutants = std::vector<double>();
135 
136  _cepNormalizedCurvePollutants = std::map<std::string, std::vector<double> >();
137 
138  int headerCount = (int)headerLinePollutants.size();
139  for (int i = 0; i < (int)matrixPollutants.size(); i++) {
140  for (int j = 0; j < (int)matrixPollutants[i].size(); j++) {
141  if ((int)matrixPollutants[i].size() != headerCount + 1) {
142  return;
143  }
144 
145  if (j == 0) {
146  _normailzedPowerPatternPollutants.push_back(matrixPollutants[i][j]);
147  _powerPatternPollutants.push_back(matrixPollutants[i][j] * getNormalizingPower());
148  }
149  else {
150  pollutantMeasures[j - 1].push_back(matrixPollutants[i][j] * pollutantMultiplyer);
151  normalizedPollutantMeasures[j - 1].push_back(matrixPollutants[i][j]);
152  }
153  }
154  }
155 
156  _cepCurvePollutants = std::map<std::string, std::vector<double> >();
157  _idlingValuesPollutants = std::map<std::string, double>();
158 
159  for (int i = 0; i < (int)headerLinePollutants.size(); i++) {
160  _cepCurvePollutants.insert(std::make_pair(pollutantIdentifier[i], pollutantMeasures[i]));
161  _cepNormalizedCurvePollutants.insert(std::make_pair(pollutantIdentifier[i], normalizedPollutantMeasures[i]));
162  _idlingValuesPollutants.insert(std::make_pair(pollutantIdentifier[i], idlingPollutants[i] * pollutantMultiplyer));
163  }
164 
165  _idlingValueFC = idlingFC * _ratedPower;
166  }
167 
168  const bool& CEP::getHeavyVehicle() const {
169  return _heavyVehicle;
170  }
171 
172  const std::string& CEP::getFuelType() const {
173  return _fuelType;
174  }
175 
177  return _normalizingType;
178  }
179 
180  const double& CEP::getRatedPower() const {
181  return _ratedPower;
182  }
183 
184  void CEP::setRatedPower(const double& value) {
185  _ratedPower = value;
186  }
187 
188  const double& CEP::getNormalizingPower() const {
189  return _normalizingPower;
190  }
191 
192  const double& CEP::getDrivingPower() const {
193  return _drivingPower;
194  }
195 
196  void CEP::setDrivingPower(const double& value) {
197  _drivingPower = value;
198  }
199 
200  double CEP::CalcPower(double speed, double acc, double gradient) {
201  //Declaration
202  double power = 0;
203  double rotFactor = GetRotationalCoeffecient(speed);
204  double powerAux = (_auxPower * _ratedPower);
205 
206  //Calculate the power
207  power += (_massVehicle + _vehicleLoading) * Constants::GRAVITY_CONST * (_resistanceF0 + _resistanceF1 * speed + _resistanceF4 * std::pow(speed, 4)) * speed;
208  power += (_crossSectionalArea * _cWValue * Constants::AIR_DENSITY_CONST / 2) * std::pow(speed, 3);
209  power += (_massVehicle * rotFactor + _vehicleMassRot + _vehicleLoading) * acc * speed;
210  power += (_massVehicle + _vehicleLoading) * Constants::GRAVITY_CONST * gradient * 0.01 * speed;
211  power /= 1000;
213  power += powerAux;
214 
215  //Return result
216  return power;
217  }
218 
219  double CEP::CalcEngPower(double power) {
220  if (power < _powerPatternFC.front()) {
221  return _powerPatternFC.front();
222  }
223  if (power > _powerPatternFC.back()) {
224  return _powerPatternFC.back();
225  }
226 
227  return power;
228  }
229 
230  double CEP::GetEmission(const std::string& pollutant, double power, double speed, Helpers* VehicleClass) {
231  //Declaration
232  std::vector<double> emissionCurve;
233  std::vector<double> powerPattern;
234 
235  // bisection search to find correct position in power pattern
236  int upperIndex;
237  int lowerIndex;
238 
239  if (_fuelType != Constants::strBEV) {
241  if (pollutant == "FC") {
242  return _idlingValueFC;
243  }
244  else {
245  if (_cepCurvePollutants.find(pollutant) == _cepCurvePollutants.end()) {
246  VehicleClass->setErrMsg(std::string("Emission pollutant ") + pollutant + std::string(" not found!"));
247  return 0;
248  }
249 
250  return _idlingValuesPollutants[pollutant];
251  }
252  }
253  }
254 
255  if (pollutant == "FC") {
256  emissionCurve = _cepCurveFC;
257  powerPattern = _powerPatternFC;
258  }
259  else {
260  if (_cepCurvePollutants.find(pollutant) == _cepCurvePollutants.end()) {
261  VehicleClass->setErrMsg(std::string("Emission pollutant ") + pollutant + std::string(" not found!"));
262  return 0;
263  }
264 
265  emissionCurve = _cepCurvePollutants[pollutant];
266  powerPattern = _powerPatternPollutants;
267  }
268 
269  if (emissionCurve.empty()) {
270  VehicleClass->setErrMsg(std::string("Empty emission curve for ") + pollutant + std::string(" found!"));
271  return 0;
272  }
273  if (emissionCurve.size() == 1) {
274  return emissionCurve[0];
275  }
276 
277  // in case that the demanded power is smaller than the first entry (smallest) in the power pattern the first is returned (should never happen)
278  if (power <= powerPattern.front()) {
279  return emissionCurve[0];
280  }
281 
282  // if power bigger than all entries in power pattern return the last (should never happen)
283  if (power >= powerPattern.back()) {
284  return emissionCurve.back();
285  }
286 
287  FindLowerUpperInPattern(lowerIndex, upperIndex, powerPattern, power);
288  return Interpolate(power, powerPattern[lowerIndex], powerPattern[upperIndex], emissionCurve[lowerIndex], emissionCurve[upperIndex]);
289  }
290 
291  double CEP::GetCO2Emission(double _FC, double _CO, double _HC, Helpers* VehicleClass) {
292  //Declaration
293  double fCBr;
294  double fCHC = 0.866;
295  double fCCO = 0.429;
296  double fCCO2 = 0.273;
297 
298 //C# TO C++ CONVERTER NOTE: The following 'switch' operated on a string variable and was converted to C++ 'if-else' logic:
299 // switch (_fuelType)
300 //ORIGINAL LINE: case Constants.strGasoline:
302  fCBr = 0.865;
303  }
304 //ORIGINAL LINE: case Constants.strDiesel:
305  else if (_fuelType == Constants::strDiesel) {
306  fCBr = 0.863;
307  }
308 //ORIGINAL LINE: case Constants.strCNG:
309  else if (_fuelType == Constants::strCNG) {
310  fCBr = 0.693;
311  fCHC = 0.803;
312  }
313  else {
314  VehicleClass->setErrMsg(std::string("The propolsion type is not known! (") + _fuelType + std::string(")"));
315  return 0;
316  }
317 
318  return (_FC * fCBr - _CO * fCCO - _HC * fCHC) / fCCO2;
319  }
320 
321  double CEP::GetDecelCoast(double speed, double acc, double gradient) {
322  //Declaration
323  int upperIndex;
324  int lowerIndex;
325 
326  if (speed < Constants::SPEED_DCEL_MIN) {
327  return speed / Constants::SPEED_DCEL_MIN * GetDecelCoast(Constants::SPEED_DCEL_MIN, acc, gradient);
328  }
329 
330  double rotCoeff = GetRotationalCoeffecient(speed);
331  FindLowerUpperInPattern(lowerIndex, upperIndex, _speedPatternRotational, speed);
332  double iGear = Interpolate(speed, _speedPatternRotational[lowerIndex], _speedPatternRotational[upperIndex], _gearTransmissionCurve[lowerIndex], _gearTransmissionCurve[upperIndex]);
333 
334  double iTot = iGear * _axleRatio;
335 
336  double n = (30 * speed * iTot) / ((_effectiveWheelDiameter / 2) * M_PI);
337  double nNorm = (n - _engineIdlingSpeed) / (_engineRatedSpeed - _engineIdlingSpeed);
338 
339  FindLowerUpperInPattern(lowerIndex, upperIndex, _nNormTable, nNorm);
340 
341  double fMot = 0;
342 
343  if (speed >= 10e-2) {
344  fMot = (-Interpolate(nNorm, _nNormTable[lowerIndex], _nNormTable[upperIndex], _dragNormTable[lowerIndex], _dragNormTable[upperIndex]) * _ratedPower * 1000 / speed) / 0.9;
345  }
346 
347  double fRoll = (_resistanceF0 + _resistanceF1 * speed + std::pow(_resistanceF2 * speed, 2) + std::pow(_resistanceF3 * speed, 3) + std::pow(_resistanceF4 * speed, 4)) * (_massVehicle + _vehicleLoading) * Constants::GRAVITY_CONST;
348 
349  double fAir = _cWValue * _crossSectionalArea * 1.2 * 0.5 * std::pow(speed, 2);
350 
351  double fGrad = (_massVehicle + _vehicleLoading) * Constants::GRAVITY_CONST * gradient / 100;
352 
353  return -(fMot + fRoll + fAir + fGrad) / ((_massVehicle + _vehicleLoading) * rotCoeff);
354  }
355 
356  double CEP::GetRotationalCoeffecient(double speed) {
357  //Declaration
358  int upperIndex;
359  int lowerIndex;
360 
361  FindLowerUpperInPattern(lowerIndex, upperIndex, _speedPatternRotational, speed);
362  return Interpolate(speed, _speedPatternRotational[lowerIndex], _speedPatternRotational[upperIndex], _speedCurveRotational[lowerIndex], _speedCurveRotational[upperIndex]);
363  }
364 
365  void CEP::FindLowerUpperInPattern(int& lowerIndex, int& upperIndex, std::vector<double>& pattern, double value) {
366  lowerIndex = 0;
367  upperIndex = 0;
368 
369  if (value <= pattern.front()) {
370  lowerIndex = 0;
371  upperIndex = 0;
372  return;
373  }
374 
375  if (value >= pattern.back()) {
376  lowerIndex = (int)pattern.size() - 1;
377  upperIndex = (int)pattern.size() - 1;
378  return;
379  }
380 
381  // bisection search to find correct position in power pattern
382  int middleIndex = ((int)pattern.size() - 1) / 2;
383  upperIndex = (int)pattern.size() - 1;
384  lowerIndex = 0;
385 
386  while (upperIndex - lowerIndex > 1) {
387  if (pattern[middleIndex] == value) {
388  lowerIndex = middleIndex;
389  upperIndex = middleIndex;
390  return;
391  }
392  else if (pattern[middleIndex] < value) {
393  lowerIndex = middleIndex;
394  middleIndex = (upperIndex - lowerIndex) / 2 + lowerIndex;
395  }
396  else {
397  upperIndex = middleIndex;
398  middleIndex = (upperIndex - lowerIndex) / 2 + lowerIndex;
399  }
400  }
401 
402  if (pattern[lowerIndex] <= value && value < pattern[upperIndex]) {
403  return;
404  }
405  }
406 
407  double CEP::Interpolate(double px, double p1, double p2, double e1, double e2) {
408  if (p2 == p1) {
409  return e1;
410  }
411 
412  return e1 + (px - p1) / (p2 - p1) * (e2 - e1);
413  }
414 
416  _heavyVehicle = false;
417  _normalizingType = static_cast<NormalizingType>(0);
418  _ratedPower = 0;
419  _normalizingPower = 0;
420  _drivingPower = 0;
421  _massVehicle = 0;
422  _vehicleLoading = 0;
423  _vehicleMassRot = 0;
425  _cWValue = 0;
426  _resistanceF0 = 0;
427  _resistanceF1 = 0;
428  _resistanceF2 = 0;
429  _resistanceF3 = 0;
430  _resistanceF4 = 0;
431  _axleRatio = 0;
432  _auxPower = 0;
433  _pNormV0 = 0;
434  _pNormP0 = 0;
435  _pNormV1 = 0;
436  _pNormP1 = 0;
437  _engineRatedSpeed = 0;
438  _engineIdlingSpeed = 0;
440  _idlingValueFC = 0;
441  }
442 }
const NormalizingType & getNormalizingTypeX() const
Definition: CEP.cpp:176
std::vector< double > _speedPatternRotational
Definition: CEP.h:114
double _resistanceF0
Definition: CEP.h:98
static const double SPEED_DCEL_MIN
Definition: Constants.h:40
static const std::string strCNG
Definition: Constants.h:63
double GetEmission(const std::string &pollutant, double power, double speed, Helpers *VehicleClass)
Definition: CEP.cpp:230
const bool & getHeavyVehicle() const
Definition: CEP.cpp:168
std::vector< double > _normailzedPowerPatternPollutants
Definition: CEP.h:117
std::vector< double > _dragNormTable
Definition: CEP.h:130
double _massVehicle
Definition: CEP.h:93
std::map< std::string, std::vector< double > > _cepCurvePollutants
Definition: CEP.h:124
void setDrivingPower(const double &value)
Definition: CEP.cpp:196
double _effectiveWheelDiameter
Definition: CEP.h:112
std::vector< double > _nNormTable
Definition: CEP.h:129
void setErrMsg(const std::string &value)
Definition: Helpers.cpp:75
double _pNormP0
Definition: CEP.h:106
double _engineRatedSpeed
Definition: CEP.h:110
void setRatedPower(const double &value)
Definition: CEP.cpp:184
void InitializeInstanceFields()
Definition: CEP.cpp:415
#define abs(a)
Definition: polyfonts.c:67
double _pNormV1
Definition: CEP.h:107
double _drivingPower
Definition: CEP.h:85
double _resistanceF1
Definition: CEP.h:99
double GetRotationalCoeffecient(double speed)
Definition: CEP.cpp:356
double _auxPower
Definition: CEP.h:104
double _engineIdlingSpeed
Definition: CEP.h:111
std::map< std::string, double > _idlingValuesPollutants
Definition: CEP.h:127
double _resistanceF2
Definition: CEP.h:100
std::vector< double > _normedCepCurveFC
Definition: CEP.h:121
static const double NORMALIZING_SPEED
Definition: Constants.h:38
NormalizingType _normalizingType
Definition: CEP.h:69
double _pNormP1
Definition: CEP.h:108
std::map< std::string, std::vector< double > > _cepNormalizedCurvePollutants
Definition: CEP.h:125
double _normalizingPower
Definition: CEP.h:80
const double & getNormalizingPower() const
Definition: CEP.cpp:188
std::vector< double > _normalizedPowerPatternFC
Definition: CEP.h:116
static const double NORMALIZING_ACCELARATION
Definition: Constants.h:39
double _cWValue
Definition: CEP.h:97
static const std::string strDiesel
Definition: Constants.h:62
double GetDecelCoast(double speed, double acc, double gradient)
Definition: CEP.cpp:321
double CalcEngPower(double power)
Definition: CEP.cpp:219
bool _heavyVehicle
Definition: CEP.h:54
double _resistanceF4
Definition: CEP.h:102
std::vector< double > _speedCurveRotational
Definition: CEP.h:123
std::vector< double > _gearTransmissionCurve
Definition: CEP.h:122
static const std::string strGasoline
Definition: Constants.h:61
double _pNormV0
Definition: CEP.h:105
double _resistanceF3
Definition: CEP.h:101
#define M_PI
Definition: odrSpiral.cpp:40
CEP(bool heavyVehicle, double vehicleMass, double vehicleLoading, double vehicleMassRot, double crossArea, double cWValue, double f0, double f1, double f2, double f3, double f4, double axleRatio, std::vector< double > &transmissionGearRatios, double auxPower, double ratedPower, double engineIdlingSpeed, double engineRatedSpeed, double effictiveWheelDiameter, double pNormV0, double pNormP0, double pNormV1, double pNormP1, const std::string &vehicelFuelType, std::vector< std::vector< double > > &matrixFC, std::vector< std::string > &headerLinePollutants, std::vector< std::vector< double > > &matrixPollutants, std::vector< std::vector< double > > &matrixSpeedRotational, std::vector< std::vector< double > > &normedDragTable, double idlingFC, std::vector< double > &idlingPollutants)
Definition: CEP.cpp:32
std::vector< double > _powerPatternFC
Definition: CEP.h:115
std::vector< double > _cepCurveFC
Definition: CEP.h:120
static const std::string strBEV
Definition: Constants.h:65
double Interpolate(double px, double p1, double p2, double e1, double e2)
Definition: CEP.cpp:407
static const double GRAVITY_CONST
Definition: Constants.h:36
const double & getDrivingPower() const
Definition: CEP.cpp:192
double _axleRatio
Definition: CEP.h:103
double _crossSectionalArea
Definition: CEP.h:96
std::vector< double > _powerPatternPollutants
Definition: CEP.h:118
static double _DRIVE_TRAIN_EFFICIENCY
Definition: Constants.h:77
const double & getRatedPower() const
Definition: CEP.cpp:180
double _idlingValueFC
Definition: CEP.h:126
static const double ZERO_SPEED_ACCURACY
Definition: Constants.h:41
double CalcPower(double speed, double acc, double gradient)
Definition: CEP.cpp:200
const std::string & getFuelType() const
Definition: CEP.cpp:172
double _vehicleLoading
Definition: CEP.h:94
static const double AIR_DENSITY_CONST
Definition: Constants.h:37
double _ratedPower
Definition: CEP.h:74
void FindLowerUpperInPattern(int &lowerIndex, int &upperIndex, std::vector< double > &pattern, double value)
Definition: CEP.cpp:365
double GetCO2Emission(double _FC, double _CO, double _HC, Helpers *VehicleClass)
Definition: CEP.cpp:291
std::string _fuelType
Definition: CEP.h:59
double _vehicleMassRot
Definition: CEP.h:95