Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2016-2024 German Aerospace Center (DLR) and others.
4 : // PHEMlight module
5 : // Copyright (C) 2016-2023 Technische Universitaet Graz, https://www.tugraz.at/
6 : // This program and the accompanying materials are made available under the
7 : // terms of the Eclipse Public License 2.0 which is available at
8 : // https://www.eclipse.org/legal/epl-2.0/
9 : // This Source Code may also be made available under the following Secondary
10 : // Licenses when the conditions for such availability set forth in the Eclipse
11 : // Public License 2.0 are satisfied: GNU General Public License, version 2
12 : // or later which is available at
13 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
15 : /****************************************************************************/
16 : /// @file CEP.cpp
17 : /// @author Martin Dippold
18 : /// @author Michael Behrisch
19 : /// @date July 2016
20 : ///
21 : //
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include "CEP.h"
26 : #include "CEPHandler.h"
27 : #include "Constants.h"
28 : #include "Helpers.h"
29 :
30 :
31 : namespace PHEMlightdllV5 {
32 :
33 28 : CEP::CEP(VEHPHEMLightJSON::VEH* Vehicle, std::vector<std::string>& headerLineFCvalues, std::vector<std::vector<double> >& matrixFCvalues, std::vector<std::string>& headerLinePollutants, std::vector<std::vector<double> >& matrixPollutants, std::vector<double>& idlingFCvalues, std::vector<double>& idlingPollutants) {
34 28 : InitializeInstanceFields();
35 28 : _resistanceF0 = Vehicle->getRollingResData()->getFr0();
36 28 : _resistanceF1 = Vehicle->getRollingResData()->getFr1();
37 28 : _resistanceF2 = Vehicle->getRollingResData()->getFr2();
38 28 : _resistanceF3 = Vehicle->getRollingResData()->getFr3();
39 28 : _resistanceF4 = Vehicle->getRollingResData()->getFr4();
40 28 : _cWValue = Vehicle->getVehicleData()->getCw();
41 28 : _crossSectionalArea = Vehicle->getVehicleData()->getA();
42 28 : _massVehicle = Vehicle->getVehicleData()->getMass();
43 28 : _vehicleLoading = Vehicle->getVehicleData()->getLoading();
44 28 : _vehicleMassRot = Vehicle->getVehicleData()->getRedMassWheel();
45 28 : setCalcType(Vehicle->getVehicleData()->getCalcType());
46 : //C# TO C++ CONVERTER NOTE: The following 'switch' operated on a string variable and was converted to C++ 'if-else' logic:
47 : // switch (CalcType)
48 : //ORIGINAL LINE: case "Conv":
49 28 : if (getCalcType() == "Conv") {
50 28 : setRatedPower(Vehicle->getEngineData()->getICEData()->getPrated());
51 28 : _engineRatedSpeed = Vehicle->getEngineData()->getICEData()->getnrated();
52 28 : _engineIdlingSpeed = Vehicle->getEngineData()->getICEData()->getIdling();
53 : }
54 : //ORIGINAL LINE: case "HEV":
55 0 : else if (getCalcType() == "HEV") {
56 : // Power von beiden zusammen Rest bezogen auf ICE
57 0 : setRatedPower(Vehicle->getEngineData()->getICEData()->getPrated() + Vehicle->getEngineData()->getEMData()->getPrated());
58 0 : _engineRatedSpeed = Vehicle->getEngineData()->getICEData()->getnrated();
59 0 : _engineIdlingSpeed = Vehicle->getEngineData()->getICEData()->getIdling();
60 : }
61 : //ORIGINAL LINE: case "BEV":
62 0 : else if (getCalcType() == "BEV") {
63 0 : setRatedPower(Vehicle->getEngineData()->getEMData()->getPrated());
64 0 : _engineRatedSpeed = Vehicle->getEngineData()->getEMData()->getnrated();
65 0 : _engineIdlingSpeed = 0;
66 : }
67 :
68 28 : _effectiveWheelDiameter = Vehicle->getVehicleData()->getWheelDiameter();
69 28 : privateHeavyVehicle = Vehicle->getVehicleData()->getMassType() == Constants::HeavyVehicle;
70 28 : setFuelType(Vehicle->getVehicleData()->getFuelType());
71 28 : _axleRatio = Vehicle->getTransmissionData()->getAxelRatio();
72 28 : _auxPower = Vehicle->getAuxiliariesData()->getPauxnorm();
73 :
74 28 : _pNormV0 = Vehicle->getFLDData()->getP_n_max_v0() / 3.6;
75 28 : _pNormP0 = Vehicle->getFLDData()->getP_n_max_p0();
76 28 : _pNormV1 = Vehicle->getFLDData()->getP_n_max_v1() / 3.6;
77 28 : _pNormP1 = Vehicle->getFLDData()->getP_n_max_p1();
78 :
79 : // Init pollutant identifiers, unit and measures
80 : std::vector<std::string> FCvaluesIdentifier;
81 : std::vector<std::vector<double> > normalizedFCvaluesMeasures;
82 112 : for (int i = 0; i < (int)headerLineFCvalues.size(); i++) {
83 84 : FCvaluesIdentifier.push_back(headerLineFCvalues[i]);
84 84 : normalizedFCvaluesMeasures.push_back(std::vector<double>());
85 : }
86 :
87 : // Init pollutant identifiers, unit and measures
88 : std::vector<std::string> pollutantIdentifier;
89 : std::vector<std::vector<double> > normalizedPollutantMeasures;
90 168 : for (int i = 0; i < (int)headerLinePollutants.size(); i++) {
91 140 : pollutantIdentifier.push_back(headerLinePollutants[i]);
92 140 : normalizedPollutantMeasures.push_back(std::vector<double>());
93 : }
94 :
95 : // Assigning values for speed rotational table
96 28 : _speedPatternRotational = std::vector<double>();
97 235 : for (int i = 0; i < (int)Vehicle->getTransmissionData()->getTransm()["Speed"].size(); i++) {
98 414 : _speedPatternRotational.push_back(Vehicle->getTransmissionData()->getTransm()["Speed"][i] / 3.6);
99 : }
100 :
101 28 : _gearTransmissionCurve = Vehicle->getTransmissionData()->getTransm()["GearRatio"];
102 28 : _speedCurveRotational = Vehicle->getTransmissionData()->getTransm()["RotMassF"];
103 :
104 : // Assigning values for drag table
105 28 : _nNormTable = Vehicle->getFLDData()->getDragCurve()["n_norm"];
106 56 : _dragNormTable = Vehicle->getFLDData()->getDragCurve()["pe_drag_norm"];
107 :
108 : // Looping through matrix and assigning values for FC values
109 28 : _normalizedPowerPatternFCvalues = std::vector<double>();
110 :
111 28 : int headerFCCount = (int)headerLineFCvalues.size();
112 392 : for (int i = 0; i < (int)matrixFCvalues.size(); i++) {
113 1820 : for (int j = 0; j < (int)matrixFCvalues[i].size(); j++) {
114 1456 : if ((int)matrixFCvalues[i].size() != headerFCCount + 1) {
115 : return;
116 : }
117 :
118 1456 : if (j == 0) {
119 364 : _normalizedPowerPatternFCvalues.push_back(matrixFCvalues[i][j]);
120 : }
121 : else {
122 1092 : normalizedFCvaluesMeasures[j - 1].push_back(matrixFCvalues[i][j]);
123 : }
124 : }
125 : }
126 :
127 28 : _idlingValueFCvalues = std::map<std::string, double>();
128 28 : _normedCepCurveFCvalues = std::map<std::string, std::vector<double> >();
129 :
130 112 : for (int i = 0; i < (int)headerLineFCvalues.size(); i++) {
131 168 : _normedCepCurveFCvalues.insert(std::make_pair(FCvaluesIdentifier[i], normalizedFCvaluesMeasures[i]));
132 84 : _idlingValueFCvalues.insert(std::make_pair(FCvaluesIdentifier[i], idlingFCvalues[i]));
133 : }
134 :
135 28 : _normalizedPowerPatternPollutants = std::vector<double>();
136 28 : _cepNormalizedCurvePollutants = std::map<std::string, std::vector<double> >();
137 :
138 28 : int headerCount = (int)headerLinePollutants.size();
139 896 : for (int i = 0; i < (int)matrixPollutants.size(); i++) {
140 6076 : for (int j = 0; j < (int)matrixPollutants[i].size(); j++) {
141 5208 : if ((int)matrixPollutants[i].size() != headerCount + 1) {
142 : return;
143 : }
144 :
145 5208 : if (j == 0) {
146 868 : _normalizedPowerPatternPollutants.push_back(matrixPollutants[i][j]);
147 : }
148 : else {
149 4340 : normalizedPollutantMeasures[j - 1].push_back(matrixPollutants[i][j]);
150 : }
151 : }
152 : }
153 :
154 28 : _idlingValuesPollutants = std::map<std::string, double>();
155 :
156 168 : for (int i = 0; i < (int)headerLinePollutants.size(); i++) {
157 280 : _cepNormalizedCurvePollutants.insert(std::make_pair(pollutantIdentifier[i], normalizedPollutantMeasures[i]));
158 140 : _idlingValuesPollutants.insert(std::make_pair(pollutantIdentifier[i], idlingPollutants[i]));
159 : }
160 :
161 28 : _FleetMix = std::map<std::string, double>();
162 28 : _FleetMix.insert(std::make_pair(Constants::strGasoline, 0));
163 28 : _FleetMix.insert(std::make_pair(Constants::strDiesel, 0));
164 28 : _FleetMix.insert(std::make_pair(Constants::strCNG, 0));
165 28 : _FleetMix.insert(std::make_pair(Constants::strLPG, 0));
166 28 : }
167 :
168 118164 : const bool& CEP::getHeavyVehicle() const {
169 118164 : return privateHeavyVehicle;
170 : }
171 :
172 94822 : const std::string& CEP::getFuelType() const {
173 94822 : return privateFuelType;
174 : }
175 :
176 28 : void CEP::setFuelType(const std::string& value) {
177 28 : privateFuelType = value;
178 28 : }
179 :
180 480438 : const std::string& CEP::getCalcType() const {
181 480438 : return privateCalcType;
182 : }
183 :
184 28 : void CEP::setCalcType(const std::string& value) {
185 28 : privateCalcType = value;
186 28 : }
187 :
188 423496 : const double& CEP::getRatedPower() const {
189 423496 : return privateRatedPower;
190 : }
191 :
192 28 : void CEP::setRatedPower(const double& value) {
193 28 : privateRatedPower = value;
194 28 : }
195 :
196 75866 : double CEP::CalcEngPower(double power, const double ratedPower) {
197 75866 : if (power < _normalizedPowerPatternFCvalues.front() * ratedPower) {
198 : return _normalizedPowerPatternFCvalues.front() * ratedPower;
199 : }
200 74382 : if (power > _normalizedPowerPatternFCvalues.back() * ratedPower) {
201 924 : return _normalizedPowerPatternFCvalues.back() * ratedPower;
202 : }
203 :
204 : return power;
205 : }
206 :
207 75824 : double CEP::GetEmission(const std::string& pollutant, double power, double speed, Helpers* VehicleClass, const double drivingPower, const double ratedPower) {
208 : //Declaration
209 : std::vector<double>* emissionCurve = nullptr;
210 : std::vector<double>* powerPattern = nullptr;
211 :
212 : // bisection search to find correct position in power pattern
213 : int upperIndex;
214 : int lowerIndex;
215 :
216 75824 : double emissionMultiplier = getHeavyVehicle() ? ratedPower : 1.;
217 75824 : if (std::abs(speed) <= Constants::ZERO_SPEED_ACCURACY) {
218 19408 : if (_cepNormalizedCurvePollutants.find(pollutant) == _cepNormalizedCurvePollutants.end() && _normedCepCurveFCvalues.find(pollutant) == _normedCepCurveFCvalues.end()) {
219 0 : VehicleClass->setErrMsg(std::string("Emission pollutant or fuel value ") + pollutant + std::string(" not found!"));
220 0 : return 0;
221 : }
222 :
223 19408 : if (_normedCepCurveFCvalues.find(pollutant) != _normedCepCurveFCvalues.end()) {
224 4852 : return _idlingValueFCvalues[pollutant] * ratedPower;
225 : }
226 14556 : else if (_cepNormalizedCurvePollutants.find(pollutant) != _cepNormalizedCurvePollutants.end()) {
227 14556 : return _idlingValuesPollutants[pollutant] * emissionMultiplier;
228 : }
229 : }
230 :
231 :
232 56416 : if (_cepNormalizedCurvePollutants.find(pollutant) == _cepNormalizedCurvePollutants.end() && _normedCepCurveFCvalues.find(pollutant) == _normedCepCurveFCvalues.end()) {
233 0 : VehicleClass->setErrMsg(std::string("Emission pollutant or fuel value ") + pollutant + std::string(" not found!"));
234 0 : return 0;
235 : }
236 :
237 : double normalizingPower = ratedPower;
238 56416 : if (_normedCepCurveFCvalues.find(pollutant) != _normedCepCurveFCvalues.end()) {
239 14104 : emissionCurve = &_normedCepCurveFCvalues[pollutant];
240 14104 : powerPattern = &_normalizedPowerPatternFCvalues;
241 : emissionMultiplier = ratedPower;
242 : }
243 42312 : else if (_cepNormalizedCurvePollutants.find(pollutant) != _cepNormalizedCurvePollutants.end()) {
244 42312 : emissionCurve = &_cepNormalizedCurvePollutants[pollutant];
245 42312 : powerPattern = &_normalizedPowerPatternPollutants;
246 42312 : if (!getHeavyVehicle()) {
247 : normalizingPower = drivingPower;
248 : }
249 : }
250 :
251 56416 : if (emissionCurve == nullptr || emissionCurve->empty()) {
252 0 : VehicleClass->setErrMsg(std::string("Empty emission curve for ") + pollutant + std::string(" found!"));
253 0 : return 0;
254 : }
255 56416 : if (emissionCurve->size() == 1) {
256 0 : return emissionCurve->front() * emissionMultiplier;
257 : }
258 :
259 : // in case that the demanded power is smaller than the first entry (smallest) in the power pattern the first is returned (should never happen)
260 56416 : if (power <= powerPattern->front() * normalizingPower) {
261 0 : return emissionCurve->front() * emissionMultiplier;
262 : }
263 :
264 : // if power bigger than all entries in power pattern return the last (should never happen)
265 56416 : if (power >= powerPattern->back() * normalizingPower) {
266 264 : return emissionCurve->back() * emissionMultiplier;
267 : }
268 :
269 56152 : FindLowerUpperInPattern(lowerIndex, upperIndex, *powerPattern, power, normalizingPower);
270 56152 : return Interpolate(power, (*powerPattern)[lowerIndex] * normalizingPower, (*powerPattern)[upperIndex] * normalizingPower, (*emissionCurve)[lowerIndex], (*emissionCurve)[upperIndex]) * emissionMultiplier;
271 : }
272 :
273 9478 : double CEP::GetCO2Emission(double _FC, double _CO, double _HC, Helpers* VehicleClass) {
274 : //Declaration
275 : double fCBr, fCHC, fCCO, fCCO2;
276 :
277 9478 : fCBr = 0;
278 9478 : fCHC = 0;
279 9478 : fCCO = 0;
280 9478 : fCCO2 = 0;
281 :
282 9478 : if (getFuelType() != "Mix") {
283 9478 : if (!GetfcVals(getFuelType(), fCBr, fCHC, fCCO, fCCO2, VehicleClass)) {
284 : return 0;
285 : }
286 : }
287 : else {
288 0 : if (!CalcfCValMix(fCBr, fCHC, fCCO, fCCO2, VehicleClass)) {
289 : return 0;
290 : }
291 : }
292 :
293 9478 : return (_FC * fCBr - _CO * fCCO - _HC * fCHC) / fCCO2;
294 : }
295 :
296 0 : bool CEP::CalcfCValMix(double& _fCBr, double& _fCHC, double& _fCCO, double& _fCCO2, Helpers* VehicleClass) {
297 : //Declaration
298 : double Sum = 0;
299 : double sumfCBr, sumfCHC, sumfCCO, sumfCCO2;
300 :
301 : //Initialise
302 : sumfCBr = 0;
303 : sumfCHC = 0;
304 : sumfCCO = 0;
305 : sumfCCO2 = 0;
306 :
307 : //calculate the sum
308 0 : for (std::map<std::string, double>::const_iterator id = _FleetMix.begin(); id != _FleetMix.end(); ++id) {
309 0 : Sum += _FleetMix[id->first];
310 : }
311 :
312 : //Calculate the weighted fuel factors
313 0 : if (Sum <= 0) {
314 0 : VehicleClass->setErrMsg("All propolsion types in the fleetshares file are not known!");
315 0 : return false;
316 : }
317 : else {
318 0 : for (std::map<std::string, double>::const_iterator id = _FleetMix.begin(); id != _FleetMix.end(); ++id) {
319 0 : if (!GetfcVals(id->first, _fCBr, _fCHC, _fCCO, _fCCO2, VehicleClass)) {
320 : return false;
321 : }
322 : else {
323 0 : sumfCBr += _fCBr * _FleetMix[id->first] / Sum;
324 0 : sumfCHC += _fCHC * _FleetMix[id->first] / Sum;
325 0 : sumfCCO += _fCCO * _FleetMix[id->first] / Sum;
326 0 : sumfCCO2 += _fCCO2 * _FleetMix[id->first] / Sum;
327 : }
328 : }
329 : }
330 : //Result values
331 0 : _fCBr = sumfCBr;
332 0 : _fCHC = sumfCHC;
333 0 : _fCCO = sumfCCO;
334 0 : _fCCO2 = sumfCCO2;
335 0 : return true;
336 : }
337 :
338 9478 : bool CEP::GetfcVals(const std::string& _fuelTypex, double& _fCBr, double& _fCHC, double& _fCCO, double& _fCCO2, Helpers* VehicleClass) {
339 9478 : _fCHC = 0.866;
340 9478 : _fCCO = 0.429;
341 9478 : _fCCO2 = 0.273;
342 :
343 : //C# TO C++ CONVERTER NOTE: The following 'switch' operated on a string variable and was converted to C++ 'if-else' logic:
344 : // switch (_fuelTypex)
345 : //ORIGINAL LINE: case Constants.strGasoline:
346 9478 : if (_fuelTypex == Constants::strGasoline) {
347 5141 : _fCBr = 0.865;
348 : }
349 : //ORIGINAL LINE: case Constants.strDiesel:
350 4337 : else if (_fuelTypex == Constants::strDiesel) {
351 4337 : _fCBr = 0.863;
352 : }
353 : //ORIGINAL LINE: case Constants.strCNG:
354 0 : else if (_fuelTypex == Constants::strCNG) {
355 0 : _fCBr = 0.693;
356 0 : _fCHC = 0.803;
357 : }
358 : //ORIGINAL LINE: case Constants.strLPG:
359 0 : else if (_fuelTypex == Constants::strLPG) {
360 0 : _fCBr = 0.825;
361 0 : _fCHC = 0.825;
362 : }
363 : else {
364 0 : VehicleClass->setErrMsg(std::string("The propulsion type is not known! (") + getFuelType() + std::string(")"));
365 0 : return false;
366 : }
367 : return true;
368 : }
369 :
370 85298 : double CEP::getFMot(const double speed, const double ratedPower, const double wheelRadius) {
371 85298 : if (speed < 10e-2) {
372 : return 0.;
373 : }
374 : //Declaration
375 : int upperIndex;
376 : int lowerIndex;
377 :
378 85298 : FindLowerUpperInPattern(lowerIndex, upperIndex, _speedPatternRotational, speed);
379 85298 : double iGear = Interpolate(speed, _speedPatternRotational[lowerIndex], _speedPatternRotational[upperIndex], _gearTransmissionCurve[lowerIndex], _gearTransmissionCurve[upperIndex]);
380 :
381 85298 : double iTot = iGear * _axleRatio;
382 :
383 85298 : double n = (30 * speed * iTot) / (wheelRadius * M_PI);
384 85298 : double nNorm = (n - _engineIdlingSpeed) / (_engineRatedSpeed - _engineIdlingSpeed);
385 :
386 85298 : FindLowerUpperInPattern(lowerIndex, upperIndex, _nNormTable, nNorm);
387 85298 : return (-Interpolate(nNorm, _nNormTable[lowerIndex], _nNormTable[upperIndex], _dragNormTable[lowerIndex], _dragNormTable[upperIndex]) * ratedPower * 1000 / speed) / Constants::getDRIVE_TRAIN_EFFICIENCY();
388 : }
389 :
390 347630 : double CEP::GetRotationalCoeffecient(double speed) {
391 : //Declaration
392 : int upperIndex;
393 : int lowerIndex;
394 :
395 347630 : FindLowerUpperInPattern(lowerIndex, upperIndex, _speedPatternRotational, speed);
396 347630 : return Interpolate(speed, _speedPatternRotational[lowerIndex], _speedPatternRotational[upperIndex], _speedCurveRotational[lowerIndex], _speedCurveRotational[upperIndex]);
397 : }
398 :
399 574378 : void CEP::FindLowerUpperInPattern(int& lowerIndex, int& upperIndex, const std::vector<double>& pattern, double value, double scale) {
400 574378 : lowerIndex = 0;
401 574378 : upperIndex = 0;
402 :
403 574378 : if (value <= pattern.front() * scale) {
404 : lowerIndex = 0;
405 : upperIndex = 0;
406 : return;
407 : }
408 :
409 558572 : if (value >= pattern.back() * scale) {
410 0 : lowerIndex = (int)pattern.size() - 1;
411 0 : upperIndex = (int)pattern.size() - 1;
412 0 : return;
413 : }
414 :
415 : // bisection search to find correct position in power pattern
416 558572 : int middleIndex = ((int)pattern.size() - 1) / 2;
417 558572 : upperIndex = (int)pattern.size() - 1;
418 558572 : lowerIndex = 0;
419 :
420 2316203 : while (upperIndex - lowerIndex > 1) {
421 1757631 : if (pattern[middleIndex] * scale == value) {
422 0 : lowerIndex = middleIndex;
423 0 : upperIndex = middleIndex;
424 0 : return;
425 : }
426 1757631 : else if (pattern[middleIndex] * scale < value) {
427 846719 : lowerIndex = middleIndex;
428 846719 : middleIndex = (upperIndex - lowerIndex) / 2 + lowerIndex;
429 : }
430 : else {
431 910912 : upperIndex = middleIndex;
432 910912 : middleIndex = (upperIndex - lowerIndex) / 2 + lowerIndex;
433 : }
434 : }
435 : }
436 :
437 589260 : double CEP::Interpolate(double px, double p1, double p2, double e1, double e2) {
438 589260 : if (p2 == p1) {
439 : return e1;
440 : }
441 :
442 573454 : return e1 + (px - p1) / (p2 - p1) * (e2 - e1);
443 : }
444 :
445 60060 : double CEP::GetPMaxNorm(double speed) {
446 : // Linear function between v0 and v1, constant elsewhere
447 60060 : if (speed <= _pNormV0) {
448 2632 : return _pNormP0;
449 : }
450 57428 : else if (speed >= _pNormV1) {
451 42546 : return _pNormP1;
452 : }
453 : else {
454 14882 : return Interpolate(speed, _pNormV0, _pNormV1, _pNormP0, _pNormP1);
455 : }
456 : }
457 :
458 28 : void CEP::InitializeInstanceFields() {
459 28 : _massVehicle = 0;
460 28 : _vehicleLoading = 0;
461 28 : _vehicleMassRot = 0;
462 28 : _crossSectionalArea = 0;
463 28 : _cWValue = 0;
464 28 : _resistanceF0 = 0;
465 28 : _resistanceF1 = 0;
466 28 : _resistanceF2 = 0;
467 28 : _resistanceF3 = 0;
468 28 : _resistanceF4 = 0;
469 28 : _axleRatio = 0;
470 28 : _auxPower = 0;
471 28 : _pNormV0 = 0;
472 28 : _pNormP0 = 0;
473 28 : _pNormV1 = 0;
474 28 : _pNormP1 = 0;
475 28 : _engineRatedSpeed = 0;
476 28 : _engineIdlingSpeed = 0;
477 28 : _effectiveWheelDiameter = 0;
478 28 : }
479 : }
|