Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
V5/cpp/CEPHandler.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2016-2026 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/****************************************************************************/
21//
22/****************************************************************************/
23#include <config.h>
24
25#include <fstream>
26#include <sstream>
27#define JSON_USE_IMPLICIT_CONVERSIONS 0
29#include "CEPHandler.h"
30#include "CEP.h"
31#include "Correction.h"
32#include "Helpers.h"
33
34
35namespace PHEMlightdllV5 {
36
39
40 const std::map<std::string, CEP*>& CEPHandler::getCEPS() const {
41 return _ceps;
42 }
43
44 bool CEPHandler::GetCEP(std::vector<std::string>& DataPath, Helpers* Helper, Correction* DataCor) {
45 if (getCEPS().find(Helper->getgClass()) == getCEPS().end()) {
46 if (!Load(DataPath, Helper, DataCor)) {
47 return false;
48 }
49 }
50 return true;
51 }
52
54 if (DataCor->getUseDet()) {
55 DataCor->setVehMileage(-1);
56 if (vehicle_Data->getMileage() > 0.) {
57 DataCor->setVehMileage(vehicle_Data->getMileage());
58 }
59
60 if (!DataCor->IniDETfactor(Helper)) {
61 return false;
62 }
63 }
64 if (DataCor->getUseTNOx()) {
65 if (!DataCor->IniTNOxfactor(Helper)) {
66 return false;
67 }
68 }
69
70 //Return value
71 return true;
72 }
73
74 bool CEPHandler::Load(std::vector<std::string>& DataPath, Helpers* Helper, Correction* DataCor, bool fleetMix) {
75 //Deklaration
76 // get string identifier for PHEM emission class
77 std::string emissionRep = Helper->getgClass();
78
79 // to hold everything.
80 std::vector<std::vector<double> > matrixFCvalues;
81 std::vector<std::vector<double> > matrixPollutants;
82 std::vector<double> idlingValuesFCvalues;
83 std::vector<double> idlingValuesPollutants;
84 std::vector<std::string> headerFCvalues;
85 std::vector<std::string> headerPollutants;
87
88 if (!ReadVehicleFile(DataPath, emissionRep, Helper, fleetMix, Vehicle)) {
89 delete Vehicle;
90 return false;
91 }
92
93 if (DataCor != nullptr) {
94 if (!CalcCorrection(DataCor, Helper, Vehicle->getVehicleData())) {
95 delete Vehicle;
96 return false;
97 }
98 }
99
100 if (!ReadEmissionData(true, DataPath, emissionRep, Helper, fleetMix, DataCor, headerFCvalues, matrixFCvalues, idlingValuesFCvalues)) {
101 delete Vehicle;
102 return false;
103 }
104 if (!ReadEmissionData(false, DataPath, emissionRep, Helper, fleetMix, DataCor, headerPollutants, matrixPollutants, idlingValuesPollutants)) {
105 delete Vehicle;
106 return false;
107 }
108
109 _ceps.insert(std::make_pair(Helper->getgClass(), new CEP(Vehicle, headerFCvalues, matrixFCvalues, headerPollutants, matrixPollutants, idlingValuesFCvalues, idlingValuesPollutants)));
110 delete Vehicle;
111 return true;
112 }
113
114 double json2double(const nlohmann::json& vd, const std::string& key) {
115 if (vd.contains(key)) {
116 return vd.at(key).get<double>();
117 }
118 return 0.;
119 }
120
121 bool CEPHandler::ReadVehicleFile(const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, bool /* fleetMix */, VEHPHEMLightJSON::VEH*& Vehicle) {
122 std::string path = "";
124
125 //Open file
126 std::ifstream vehicleReader;
127 for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {
128 vehicleReader.open(((*i) + emissionClass + ".PHEMLight.veh").c_str());
129 if (vehicleReader.good()) {
130 break;
131 }
132 }
133 if (!vehicleReader.good()) {
134 Helper->setErrMsg("File does not exist! (" + emissionClass + ".PHEMLight.veh)");
135 return false;
136 }
137
138 //**** VEH Datei einlesen ****
139 nlohmann::json json;
140 try {
141 vehicleReader >> json;
142 } catch (...) {
143 Helper->setErrMsg("Error during file read! (" + emissionClass + ".PHEMLight.veh)");
144 return false;
145 }
146
147 //*** Get the vehicle data
148 nlohmann::json::iterator vehDataIt = json.find("VehicleData");
149 if (vehDataIt == json.end()) {
150 Helper->setErrMsg("No VehicleData in " + emissionClass + ".PHEMLight.veh!");
151 return false;
152 }
153 const nlohmann::json& vd = *vehDataIt;
154 Vehicle->getVehicleData()->setMassType(vd.contains("MassType") ? vd.at("MassType").get<std::string>() : "LV");
155 Vehicle->getVehicleData()->setFuelType(vd.contains("FuelType") ? vd.at("FuelType").get<std::string>() : "D");
156 Vehicle->getVehicleData()->setCalcType(vd.contains("CalcType") ? vd.at("CalcType").get<std::string>() : "Conv");
157 Vehicle->getVehicleData()->setMass(json2double(vd, "Mass"));
158 Vehicle->getVehicleData()->setLoading(json2double(vd, "Loading"));
159 Vehicle->getVehicleData()->setRedMassWheel(json2double(vd, "RedMassWheel"));
160 Vehicle->getVehicleData()->setWheelDiameter(json2double(vd, "WheelDiameter"));
161 Vehicle->getVehicleData()->setCw(json2double(vd, "Cw"));
162 Vehicle->getVehicleData()->setA(json2double(vd, "A"));
163 Vehicle->getVehicleData()->setMileage(json2double(vd, "Mileage"));
164
165 // Auxiliaries
166 nlohmann::json::iterator auxDataIt = json.find("AuxiliariesData");
167 if (auxDataIt == json.end() || !auxDataIt->contains("Pauxnorm")) {
168 Vehicle->getAuxiliariesData()->setPauxnorm(0.);
169 } else {
170 Vehicle->getAuxiliariesData()->setPauxnorm(auxDataIt->at("Pauxnorm").get<double>());
171 }
172
173 // Engine Data
174 nlohmann::json::iterator engDataIt = json.find("EngineData");
175 if (engDataIt == json.end() || !engDataIt->contains("ICEData") || !engDataIt->contains("EMData")) {
176 Helper->setErrMsg("Incomplete EngineData in " + emissionClass + ".PHEMLight.veh!");
177 return false;
178 }
179 const nlohmann::json& iced = (*engDataIt)["ICEData"];
180 const nlohmann::json& emd = (*engDataIt)["EMData"];
181 Vehicle->getEngineData()->getICEData()->setPrated(json2double(iced, "Prated"));
182 Vehicle->getEngineData()->getICEData()->setnrated(json2double(iced, "nrated"));
183 Vehicle->getEngineData()->getICEData()->setIdling(json2double(iced, "Idling"));
184 Vehicle->getEngineData()->getEMData()->setPrated(json2double(emd, "Prated"));
185 Vehicle->getEngineData()->getEMData()->setnrated(json2double(emd, "nrated"));
186
187 // Rolling resistance
188 nlohmann::json::iterator rrDataIt = json.find("RollingResData");
189 if (rrDataIt == json.end()) {
190 Helper->setErrMsg("No RollingResData in " + emissionClass + ".PHEMLight.veh!");
191 return false;
192 }
193 const nlohmann::json& rrd = *rrDataIt;
194 Vehicle->getRollingResData()->setFr0(json2double(rrd, "Fr0"));
195 Vehicle->getRollingResData()->setFr1(json2double(rrd, "Fr1"));
196 Vehicle->getRollingResData()->setFr2(json2double(rrd, "Fr2"));
197 Vehicle->getRollingResData()->setFr3(json2double(rrd, "Fr3"));
198 Vehicle->getRollingResData()->setFr4(json2double(rrd, "Fr4"));
199
200 // Transmission
201 nlohmann::json::iterator trDataIt = json.find("TransmissionData");
202 if (trDataIt == json.end()) {
203 Helper->setErrMsg("No TransmissionData in " + emissionClass + ".PHEMLight.veh!");
204 return false;
205 }
206 Vehicle->getTransmissionData()->setAxelRatio(json2double(*trDataIt, "AxelRatio"));
207 nlohmann::json::iterator transmIt = trDataIt->find("Transm");
208 if (transmIt == trDataIt->end()) {
209 Helper->setErrMsg(std::string("Transmission ratios missing in vehicle file! Calculation stopped! (") + path + std::string(")"));
210 return false;
211 } else {
212 if (!transmIt->contains("Speed")) {
213 Helper->setErrMsg(std::string("No Speed signal in transmission data given! Calculation stopped! (") + path + std::string(")"));
214 return false;
215 }
216 if (!transmIt->contains("GearRatio")) {
217 Helper->setErrMsg(std::string("No GearRatio signal in transmission data given! Calculation stopped! (") + path + std::string(")"));
218 return false;
219 }
220 if (!transmIt->contains("RotMassF")) {
221 Helper->setErrMsg(std::string("No RotMassF signal in transmission data given! Calculation stopped! (") + path + std::string(")"));
222 return false;
223 }
224 }
225 Vehicle->getTransmissionData()->setTransm(transmIt->get<std::map<std::string, std::vector<double> > >());
226
227 // Full load and drag
228 nlohmann::json::iterator fldDataIt = json.find("FLDData");
229 if (fldDataIt == json.end()) {
230 Helper->setErrMsg("No FLDData in " + emissionClass + ".PHEMLight.veh!");
231 return false;
232 }
233 const nlohmann::json& fld = *fldDataIt;
234 Vehicle->getFLDData()->setP_n_max_v0(json2double(fld, "P_n_max_v0"));
235 Vehicle->getFLDData()->setP_n_max_p0(json2double(fld, "P_n_max_p0"));
236 Vehicle->getFLDData()->setP_n_max_v1(json2double(fld, "P_n_max_v1"));
237 Vehicle->getFLDData()->setP_n_max_p1(json2double(fld, "P_n_max_p1"));
238 nlohmann::json::iterator dragIt = fldDataIt->find("DragCurve");
239 if (dragIt == fldDataIt->end()) {
240 Helper->setErrMsg(std::string("Drag curve missing in vehicle file! Calculation stopped! (") + path + std::string(")"));
241 return false;
242 } else {
243 if (!dragIt->contains("n_norm")) {
244 Helper->setErrMsg(std::string("No n_norm signal in drag curve data given! Calculation stopped! (") + path + std::string(")"));
245 return false;
246 }
247 if (!dragIt->contains("pe_drag_norm")) {
248 Helper->setErrMsg(std::string("No pe_drag_norm signal in drag curve data given! Calculation stopped! (") + path + std::string(")"));
249 return false;
250 }
251 }
252 Vehicle->getFLDData()->setDragCurve(dragIt->get<std::map<std::string, std::vector<double> > >());
253
254 return true;
255 }
256
257 bool CEPHandler::ReadEmissionData(bool readFC, const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, bool /* fleetMix */, Correction* DataCor, std::vector<std::string>& header, std::vector<std::vector<double> >& matrix, std::vector<double>& idlingValues) {
258 // declare file stream
259 std::string line;
260 std::string path = "";
261 header = std::vector<std::string>();
262 matrix = std::vector<std::vector<double> >();
263 idlingValues = std::vector<double>();
264
265 std::string pollutantExtension = "";
266 if (readFC) {
267 pollutantExtension += std::string("_FC");
268 }
269
270 std::ifstream fileReader;
271 for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {
272 fileReader.open(((*i) + emissionClass + pollutantExtension + ".csv").c_str());
273 if (fileReader.good()) {
274 break;
275 }
276 }
277 if (!fileReader.good()) {
278 Helper->setErrMsg("File does not exist! (" + emissionClass + pollutantExtension + ".csv)");
279 return false;
280 }
281
282 // read header line for pollutant identifiers
283 if ((line = ReadLine(fileReader)) != "") {
284 const std::vector<std::string>& entries = split(line, ',');
285 // skip first entry "Pe"
286 for (int i = 1; i < (int)entries.size(); i++) {
287 header.push_back(entries[i]);
288 }
289 }
290
291 // skip units
292 ReadLine(fileReader);
293
294 // skip comment
295 ReadLine(fileReader);
296
297 //readIdlingValues
298 line = ReadLine(fileReader);
299
300 std::vector<std::string> stringIdlings = split(line, ',');
301 stringIdlings.erase(stringIdlings.begin());
302
303 idlingValues = todoubleList(stringIdlings);
304
305 while ((line = ReadLine(fileReader)) != "") {
306 matrix.push_back(todoubleList(split(line, ',')));
307 }
308
309 //Data correction (Det & TNOx)
310 if (!CorrectEmissionData(DataCor, header, matrix, idlingValues)) {
311 Helper->setErrMsg("Error in correction calculation");
312 return false;
313 }
314
315 //Return value
316 return true;
317 }
318
319 bool CEPHandler::CorrectEmissionData(Correction* DataCor, std::vector<std::string>& header, std::vector<std::vector<double> >& matrix, std::vector<double>& idlingValues) {
320 for (int i = 0; i < (int)header.size(); i++) {
321 double CorF = GetDetTempCor(DataCor, header[i]);
322 if (CorF != 1) {
323 for (int j = 0; j < (int)matrix.size(); j++) {
324 matrix[j][i + 1] *= CorF;
325 }
326 idlingValues[i] *= CorF;
327 }
328 }
329
330 //Return value
331 return true;
332 }
333
334 double CEPHandler::GetDetTempCor(Correction* DataCor, const std::string& Emi) {
335 //Initialisation
336 double CorF = 1;
337 std::string emi = Emi;
338 std::transform(emi.begin(), emi.end(), emi.begin(), [](char c) { return (char)::toupper(c); });
339
340 if (DataCor != 0) {
341 if (DataCor->getUseDet() && DataCor->DETFactors.count(emi) > 0) {
342 CorF += DataCor->DETFactors[emi] - 1;
343 }
344 if (DataCor->getUseTNOx()) {
345 if (emi.find("NOX") != std::string::npos) {
346 CorF += (DataCor->getTNOxFactor() - 1);
347 }
348 }
349 }
350
351 //Return value
352 return CorF;
353 }
354
355 const std::vector<std::string> CEPHandler::split(const std::string& s, char delim) {
356 std::vector<std::string> elems;
357 std::stringstream ss(s);
358 std::string item;
359 while (std::getline(ss, item, delim)) {
360 elems.push_back(item);
361 }
362 return elems;
363 }
364
365 double CEPHandler::todouble(const std::string& s) {
366 std::stringstream ss(s);
367 double item;
368 ss >> item;
369 return item;
370 }
371
372 std::vector<double> CEPHandler::todoubleList(const std::vector<std::string>& s) {
373 std::vector<double> result;
374 for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); ++i) {
375 result.push_back(todouble(*i));
376 }
377 return result;
378 }
379
380 std::string CEPHandler::ReadLine(std::ifstream& s) {
381 std::string line;
382 std::getline(s, line);
383 size_t lastNWChar = line.find_last_not_of(" \n\r\t");
384 if (lastNWChar != std::string::npos) {
385 line.erase(lastNWChar + 1);
386 }
387 return line;
388 }
389
390 const std::string& VEHPHEMLightJSON::VEH::getType() const {
391 return privateType;
392 }
393
394 void VEHPHEMLightJSON::VEH::setType(const std::string& value) {
395 privateType = value;
396 }
397
398 const std::string& VEHPHEMLightJSON::VEH::getVersion() const {
399 return privateVersion;
400 }
401
402 void VEHPHEMLightJSON::VEH::setVersion(const std::string& value) {
403 privateVersion = value;
404 }
405
409
411 return &privateAuxiliariesData;
412 }
413
417
421
425
429
431 return privateMassType;
432 }
433
434 void VEHPHEMLightJSON::Vehicle_Data::setMassType(const std::string& value) {
435 privateMassType = value;
436 }
437
439 return privateFuelType;
440 }
441
442 void VEHPHEMLightJSON::Vehicle_Data::setFuelType(const std::string& value) {
443 privateFuelType = value;
444 }
445
447 return privateCalcType;
448 }
449
450 void VEHPHEMLightJSON::Vehicle_Data::setCalcType(const std::string& value) {
451 privateCalcType = value;
452 }
453
455 return privateMass;
456 }
457
458 void VEHPHEMLightJSON::Vehicle_Data::setMass(const double& value) {
459 privateMass = value;
460 }
461
463 return privateLoading;
464 }
465
467 privateLoading = value;
468 }
469
471 return privateRedMassWheel;
472 }
473
475 privateRedMassWheel = value;
476 }
477
479 return privateWheelDiameter;
480 }
481
483 privateWheelDiameter = value;
484 }
485
487 return privateCw;
488 }
489
490 void VEHPHEMLightJSON::Vehicle_Data::setCw(const double& value) {
491 privateCw = value;
492 }
493
495 return privateA;
496 }
497
498 void VEHPHEMLightJSON::Vehicle_Data::setA(const double& value) {
499 privateA = value;
500 }
501
503 return privateMileage;
504 }
505
507 privateMileage = value;
508 }
509
511 return privateFr0;
512 }
513
514 void VEHPHEMLightJSON::Rollres_Data::setFr0(const double& value) {
515 privateFr0 = value;
516 }
517
519 return privateFr1;
520 }
521
522 void VEHPHEMLightJSON::Rollres_Data::setFr1(const double& value) {
523 privateFr1 = value;
524 }
525
527 return privateFr2;
528 }
529
530 void VEHPHEMLightJSON::Rollres_Data::setFr2(const double& value) {
531 privateFr2 = value;
532 }
533
535 return privateFr3;
536 }
537
538 void VEHPHEMLightJSON::Rollres_Data::setFr3(const double& value) {
539 privateFr3 = value;
540 }
541
543 return privateFr4;
544 }
545
546 void VEHPHEMLightJSON::Rollres_Data::setFr4(const double& value) {
547 privateFr4 = value;
548 }
549
553
557
559 return privatePrated;
560 }
561
562 void VEHPHEMLightJSON::ICE_Data::setPrated(const double& value) {
563 privatePrated = value;
564 }
565
567 return privatenrated;
568 }
569
570 void VEHPHEMLightJSON::ICE_Data::setnrated(const double& value) {
571 privatenrated = value;
572 }
573
575 return privateIdling;
576 }
577
578 void VEHPHEMLightJSON::ICE_Data::setIdling(const double& value) {
579 privateIdling = value;
580 }
581
583 return privatePrated;
584 }
585
586 void VEHPHEMLightJSON::EM_Data::setPrated(const double& value) {
587 privatePrated = value;
588 }
589
591 return privatenrated;
592 }
593
594 void VEHPHEMLightJSON::EM_Data::setnrated(const double& value) {
595 privatenrated = value;
596 }
597
599 return privatePauxnorm;
600 }
601
602 void VEHPHEMLightJSON::Aux_Data::setPauxnorm(const double& value) {
603 privatePauxnorm = value;
604 }
605
607 return privateP_n_max_v0;
608 }
609
611 privateP_n_max_v0 = value;
612 }
613
615 return privateP_n_max_p0;
616 }
617
619 privateP_n_max_p0 = value;
620 }
621
623 return privateP_n_max_v1;
624 }
625
627 privateP_n_max_v1 = value;
628 }
629
631 return privateP_n_max_p1;
632 }
633
635 privateP_n_max_p1 = value;
636 }
637
638 std::map<std::string, std::vector<double> >& VEHPHEMLightJSON::FullLoadDrag_Data::getDragCurve() {
639 return privateDragCurve;
640 }
641
642 void VEHPHEMLightJSON::FullLoadDrag_Data::setDragCurve(const std::map<std::string, std::vector<double> >& value) {
643 privateDragCurve = value;
644 }
645
647 setDragCurve(std::map<std::string, std::vector<double> >());
648 }
649
651 return privateAxelRatio;
652 }
653
655 privateAxelRatio = value;
656 }
657
658 std::map<std::string, std::vector<double> >& VEHPHEMLightJSON::Transmission_Data::getTransm() {
659 return privateTransm;
660 }
661
662 void VEHPHEMLightJSON::Transmission_Data::setTransm(const std::map<std::string, std::vector<double> >& value) {
663 privateTransm = value;
664 }
665
667 setTransm(std::map<std::string, std::vector<double> >());
668 }
669}
bool Load(std::vector< std::string > &DataPath, Helpers *Helper, Correction *DataCor, bool fleetMix=false)
const std::map< std::string, CEP * > & getCEPS() const
bool ReadVehicleFile(const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, bool fleetMix, VEHPHEMLightJSON::VEH *&Vehicle)
std::string ReadLine(std::ifstream &s)
std::vector< double > todoubleList(const std::vector< std::string > &s)
double todouble(const std::string &s)
bool CorrectEmissionData(Correction *DataCor, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix, std::vector< double > &idlingValues)
bool CalcCorrection(Correction *DataCor, Helpers *Helper, VEHPHEMLightJSON::Vehicle_Data *vehicle_Data)
std::map< std::string, CEP * > _ceps
bool GetCEP(std::vector< std::string > &DataPath, Helpers *Helper, Correction *DataCor)
const std::vector< std::string > split(const std::string &s, char delim)
double GetDetTempCor(Correction *DataCor, const std::string &Emi)
bool ReadEmissionData(bool readFC, const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, bool fleetMix, Correction *DataCor, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix, std::vector< double > &idlingValues)
bool IniDETfactor(Helpers *Helper)
const bool & getUseTNOx() const
bool IniTNOxfactor(Helpers *Helper)
std::map< std::string, double > DETFactors
Definition Correction.h:52
const double & getTNOxFactor() const
void setVehMileage(const double &value)
const bool & getUseDet() const
const std::string & getgClass() const
void setErrMsg(const std::string &value)
std::map< std::string, std::vector< double > > & getDragCurve()
void setDragCurve(const std::map< std::string, std::vector< double > > &value)
void setTransm(const std::map< std::string, std::vector< double > > &value)
std::map< std::string, std::vector< double > > & getTransm()
void setType(const std::string &value)
void setVersion(const std::string &value)
C++ TraCI client API implementation.
namespace for Niels Lohmann
Definition json.hpp:20114
iterator end() noexcept
returns an iterator to one past the last element
Definition json.hpp:22834
iterator find(const typename object_t::key_type &key)
find an element in a JSON object
Definition json.hpp:22692
iter_impl< basic_json > iterator
an iterator for a basic_json container
Definition json.hpp:20245
basic_json<> json
default specialization
Definition json.hpp:3549
double json2double(const nlohmann::json &vd, const std::string &key)