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