LCOV - code coverage report
Current view: top level - src/foreign/PHEMlight/V5/cpp - Correction.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 125 168 74.4 %
Date: 2024-04-27 15:34:54 Functions: 25 27 92.6 %

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

Generated by: LCOV version 1.14