Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Correction.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 "Correction.h"
27#include "Helpers.h"
28#include "Constants.h"
29
30
31namespace PHEMlightdllV5 {
32
33 static std::string euroClass(std::string eClass) {
34 for (size_t pos = eClass.find("EU"); pos != std::string::npos; pos = eClass.find("EU", pos + 5)) {
35 eClass.replace(pos, 2, "EURO ");
36 }
37 return eClass;
38 }
39
40 Correction::Correction(const std::vector<std::string>& dataPath)
41 : privateDataPath(dataPath) {
42 setUseDet(false);
43 setUseTNOx(false);
44 setAmbTemp(20);
45 setYear(2022);
46 setVehMileage(-1);
47 setDETFilePath("Deterioration.det");
48 setVMAFilePath("Mileage.vma");
49 setTNOxFilePath("NOxCor.tno");
50 }
51
52 const bool& Correction::getUseDet() const {
53 return privateUseDet;
54 }
55
56 void Correction::setUseDet(const bool& value) {
57 privateUseDet = value;
58 }
59
60 const std::string& Correction::getDETFilePath() const {
61 return privateDETFilePath;
62 }
63
64 void Correction::setDETFilePath(const std::string& value) {
65 privateDETFilePath = value;
66 }
67
68 const std::string& Correction::getVMAFilePath() const {
69 return privateVMAFilePath;
70 }
71
72 void Correction::setVMAFilePath(const std::string& value) {
73 privateVMAFilePath = value;
74 }
75
76 const int& Correction::getYear() const {
77 return privateYear;
78 }
79
80 void Correction::setYear(const int& value) {
81 privateYear = value;
82 }
83
84 const double& Correction::getVehMileage() const {
85 return privateVehMileage;
86 }
87
88 void Correction::setVehMileage(const double& value) {
89 privateVehMileage = value;
90 }
91
92 bool Correction::ReadDet(std::string& ErrMSG) {
93 //Read Detoriation file
94 if (!ReadDETFile(ErrMSG)) {
95 return false;
96 }
97
98 if (!ReadVMAFile(ErrMSG)) {
99 return false;
100 }
101
102 // Return value
103 return true;
104 }
105
106 bool Correction::ReadDETFile(std::string& ErrMSG) {
107 //Open file
108 std::ifstream detReader;
109 for (const std::string& p : privateDataPath) {
110 detReader.open((p + getDETFilePath()).c_str());
111 if (detReader.good()) {
112 break;
113 }
114 }
115 if (!detReader.good()) {
116 ErrMSG = "File does not exist! (" + getDETFilePath() + ")";
117 return false;
118 }
119
120 //**** DET Datei einlesen ****
121 try {
122 detReader >> DETdata;
123 } catch (...) {
124 ErrMSG = "Error during file read! (" + getDETFilePath() + ")";
125 return false;
126 }
127
128 // Return value
129 return true;
130 }
131
132 bool Correction::ReadVMAFile(std::string& ErrMSG) {
133 //Open file
134 std::ifstream vmaReader;
135 for (const std::string& p : privateDataPath) {
136 vmaReader.open((p + getVMAFilePath()).c_str());
137 if (vmaReader.good()) {
138 break;
139 }
140 }
141 if (!vmaReader.good()) {
142 ErrMSG = "File does not exist! (" + getVMAFilePath() + ")";
143 return false;
144 }
145
146 //**** VMA Datei einlesen ****
147 try {
148 vmaReader >> VMAdata;
149 } catch (...) {
150 ErrMSG = "Error during file read! (" + getVMAFilePath() + ")";
151 return false;
152 }
153
154 // Return value
155 return true;
156 }
157
159 //Initialise
160 DETFactors = std::map<std::string, double>();
161
162 if (DETdata.at("Vehicle").contains(Helper->getvClass())) {
163 if (DETdata.at("Vehicle").at(Helper->getvClass()).at("PropulsionClass").contains(Helper->getpClass())) {
164 for (const auto& it : DETdata.at("Vehicle").at(Helper->getvClass()).at("PropulsionClass").at(Helper->getpClass()).at("Emission").items()) {
165 std::string EUclass = euroClass(Helper->geteClass());
166
167 //PC special classes check (ab, c, d, d-Temp). If available use otherwise use "EURO 6" if available
168 if ((Helper->getvClass() == Constants::strPKW || Helper->getvClass() == Constants::strLNF) && EUclass.length() > 6) {
169 std::string EUclassShort = EUclass.substr(0, 6);
170
171 if (!it.value().at("EUClass").contains(EUclass) && it.value().at("EUClass").contains(EUclassShort)) {
172 EUclass = EUclassShort;
173 }
174 }
175
176 //Store in upper case because retrieval will be upper case as well
177 std::string key = it.key();
178 std::transform(key.begin(), key.end(), key.begin(), [](char c) { return (char)::toupper(c); });
179 //Get the factor
180 if (it.value().at("EUClass").contains(EUclass)) {
181 const std::vector<double>& Mileage = it.value().at("Mileage");
182 const std::vector<double>& Factor = it.value().at("EUClass").at(EUclass);
183 if (getVehMileage() < 0) {
184 setVehMileage(GetMileage(Helper));
185 }
186
187 for (int i = 1; i < (int)Mileage.size(); i++) {
188 if (i == 1 && Mileage[i] > getVehMileage()) {
189 DETFactors.insert(std::make_pair(key, Factor[0]));
190 break;
191 }
192 else if (i == (int)Mileage.size() - 1 && getVehMileage() > Mileage[i]) {
193 DETFactors.insert(std::make_pair(key, Factor[i]));
194 break;
195 }
196 else if (getVehMileage() < Mileage[i]) {
197 DETFactors.insert(std::make_pair(key, Interpolate(getVehMileage(), Mileage[i - 1], Mileage[i], Factor[i - 1], Factor[i])));
198 break;
199 }
200 }
201 }
202 else {
203 DETFactors.insert(std::make_pair(key, 1));
204 }
205 }
206 }
207 }
208
209 //Return value
210 return true;
211 }
212
214 // Initialise
215 double Mileage = 0;
216
217 if (VMAdata.at("Vehicle").contains(Helper->getvClass())) {
218 if (VMAdata.at("Vehicle").at(Helper->getvClass()).at("PropulsionClass").contains(Helper->getpClass())) {
219 std::string Sclass = "0";
220//C# TO C++ CONVERTER NOTE: The following 'switch' operated on a string variable and was converted to C++ 'if-else' logic:
221// switch (Helper.sClass)
222//ORIGINAL LINE: case "":
223 if (Helper->getsClass() == "") {
224 Sclass = "0";
225 }
226//ORIGINAL LINE: case "I":
227 else if (Helper->getsClass() == "I") {
228 Sclass = "1";
229 }
230//ORIGINAL LINE: case "II":
231 else if (Helper->getsClass() == "II") {
232 Sclass = "2";
233 }
234//ORIGINAL LINE: case "III":
235 else if (Helper->getsClass() == "III") {
236 Sclass = "3";
237 }
238
239 if (VMAdata.at("Vehicle").at(Helper->getvClass()).at("PropulsionClass").at(Helper->getpClass()).at("SizeClass").contains(Sclass)) {
240 const nlohmann::json& sclassJson = VMAdata.at("Vehicle").at(Helper->getvClass()).at("PropulsionClass").at(Helper->getpClass()).at("SizeClass").at(Sclass);
241 std::string EUclass = euroClass(Helper->geteClass());
242
243 //PC special classes check (ab, c, d, d-Temp). If available use otherwise use "EURO 6" if available
244 if ((Helper->getvClass() == Constants::strPKW || Helper->getvClass() == Constants::strLNF) && EUclass.length() > 6) {
245 std::string EUclassShort = EUclass.substr(0, 6);
246
247 if (!sclassJson.at("EUClass").contains(EUclass) && sclassJson.at("EUClass").contains(EUclassShort)) {
248 EUclass = EUclassShort;
249 }
250 }
251
252 if (sclassJson.contains(EUclass)) {
253 //Calculate Mileage
254 const std::vector<double>& Factor = sclassJson.at(EUclass);
255 int AnzYear = getYear() - 2020 + 1;
256 Mileage = Factor[0] * std::pow(AnzYear, 3) + Factor[1] * std::pow(AnzYear, 2) + Factor[2] * AnzYear + Factor[3];
257
258 //Check calculated mileage
259 if (Mileage < 0) {
260 Mileage = 0;
261 }
262 }
263 }
264 }
265 }
266
267
268 //Return value
269 return Mileage;
270 }
271
272 const bool& Correction::getUseTNOx() const {
273 return privateUseTNOx;
274 }
275
276 void Correction::setUseTNOx(const bool& value) {
277 privateUseTNOx = value;
278 }
279
280 const std::string& Correction::getTNOxFilePath() const {
281 return privateTNOxFilePath;
282 }
283
284 void Correction::setTNOxFilePath(const std::string& value) {
285 privateTNOxFilePath = value;
286 }
287
288 const double& Correction::getAmbTemp() const {
289 return privateAmbTemp;
290 }
291
292 void Correction::setAmbTemp(const double& value) {
293 privateAmbTemp = value;
294 }
295
296 const double& Correction::getTNOxFactor() const {
297 return privateTNOxFactor;
298 }
299
300 void Correction::setTNOxFactor(const double& value) {
301 privateTNOxFactor = value;
302 }
303
304 bool Correction::ReadTNOx(std::string& ErrMSG) {
305 //Open file
306 std::ifstream tnoxReader;
307 for (const std::string& p : privateDataPath) {
308 tnoxReader.open((p + getTNOxFilePath()).c_str());
309 if (tnoxReader.good()) {
310 break;
311 }
312 }
313 if (!tnoxReader.good()) {
314 ErrMSG = "File does not exist! (" + getTNOxFilePath() + ")";
315 return false;
316 }
317
318 //**** VMA Datei einlesen ****
319 try {
320 tnoxReader >> TNOxdata;
321 } catch (...) {
322 ErrMSG = "Error during file read! (" + getTNOxFilePath() + ")";
323 return false;
324 }
325
326 // Return value
327 return true;
328 }
329
331 //Initialise
332 setTNOxFactor(1);
333
334 //Calculation only for diesel vehicles
335 if (Helper->getpClass() != Constants::strDiesel) {
336 return true;
337 }
338
339 if (TNOxdata.at("Vehicle").contains(Helper->getvClass())) {
340 std::string EUclass = euroClass(Helper->geteClass());
341
342 //PC special classes check (ab, c, d, d-Temp). If available use otherwise use "EURO 6" if available
343 if ((Helper->getvClass() == Constants::strPKW || Helper->getvClass() == Constants::strLNF) && EUclass.length() > 6) {
344 std::string EUclassShort = EUclass.substr(0, 6);
345
346 if (!TNOxdata.at("Vehicle").at(Helper->getvClass()).at("EUClass").contains(EUclass) && TNOxdata.at("Vehicle").at(Helper->getvClass()).at("EUClass").contains(EUclassShort)) {
347 EUclass = EUclassShort;
348 }
349 }
350
351
352 if (TNOxdata.at("Vehicle").at(Helper->getvClass()).at("EUClass").contains(EUclass)) {
353 //Check/set temperature borders, because calculation is a straight function
354 const nlohmann::json& EUclassJson = TNOxdata.at("Vehicle").at(Helper->getvClass()).at("EUClass").at(EUclass);
355 const double m = EUclassJson.at("m");
356 const double c = EUclassJson.at("c");
357 const double tb0 = EUclassJson.at("TB")[0];
358 if (getAmbTemp() < tb0) {
359 setTNOxFactor(m + c * tb0);
360 }
361 else if (getAmbTemp() > EUclassJson.at("TB")[1]) {
362 setTNOxFactor(1);
363 }
364 else {
365 setTNOxFactor(m + c * getAmbTemp());
366 }
367 }
368 }
369
370 //Return value
371 return true;
372 }
373
374 double Correction::Interpolate(double px, double p1, double p2, double e1, double e2) {
375 if (p2 == p1) {
376 return e1;
377 }
378
379 return e1 + (px - p1) / (p2 - p1) * (e2 - e1);
380 }
381
382}
static const std::string strDiesel
void setAmbTemp(const double &value)
const std::vector< std::string > privateDataPath
Definition Correction.h:38
bool IniDETfactor(Helpers *Helper)
const bool & getUseTNOx() const
void setTNOxFilePath(const std::string &value)
const int & getYear() const
bool IniTNOxfactor(Helpers *Helper)
std::map< std::string, double > DETFactors
Definition Correction.h:52
std::string privateDETFilePath
Definition Correction.h:39
void setDETFilePath(const std::string &value)
bool ReadVMAFile(std::string &ErrMSG)
bool ReadDet(std::string &ErrMSG)
const std::string & getTNOxFilePath() const
double Interpolate(double px, double p1, double p2, double e1, double e2)
const double & getVehMileage() const
void setTNOxFactor(const double &value)
const std::string & getDETFilePath() const
void setVMAFilePath(const std::string &value)
bool ReadTNOx(std::string &ErrMSG)
const double & getTNOxFactor() const
Correction(const std::vector< std::string > &dataPath)
std::string privateVMAFilePath
Definition Correction.h:40
std::string privateTNOxFilePath
Definition Correction.h:44
const double & getAmbTemp() const
double GetMileage(Helpers *Helper)
bool ReadDETFile(std::string &ErrMSG)
void setYear(const int &value)
void setUseTNOx(const bool &value)
void setVehMileage(const double &value)
const bool & getUseDet() const
void setUseDet(const bool &value)
const std::string & getVMAFilePath() const
const std::string & getpClass() const
const std::string & getvClass() const
const std::string & getsClass() const
const std::string & geteClass() const
static std::string euroClass(std::string eClass)