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