Path: blob/main/src/foreign/PHEMlight/cpp/CEPHandler.cpp
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.3// PHEMlight module4// Copyright 2016 Technische Universitaet Graz, https://www.tugraz.at/5// This program and the accompanying materials are made available under the6// terms of the Eclipse Public License 2.0 which is available at7// https://www.eclipse.org/legal/epl-2.0/8// This Source Code may also be made available under the following Secondary9// Licenses when the conditions for such availability set forth in the Eclipse10// Public License 2.0 are satisfied: GNU General Public License, version 211// or later which is available at12// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html13// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later14/****************************************************************************/15/// @file CEPHandler.cpp16/// @author Martin Dippold17/// @author Michael Behrisch18/// @date July 201619///20//21/****************************************************************************/22#include <config.h>2324#include <fstream>25#include <sstream>26#include "CEPHandler.h"27#include "CEP.h"28#include "Helpers.h"29#include "Constants.h"303132namespace PHEMlightdll {3334CEPHandler::CEPHandler() {35_ceps = std::map<std::string, CEP*>();36}3738const std::map<std::string, CEP*>& CEPHandler::getCEPS() const {39return _ceps;40}4142bool CEPHandler::GetCEP(const std::vector<std::string>& DataPath, Helpers* Helper) {43if (getCEPS().find(Helper->getgClass()) == getCEPS().end()) {44if (!Load(DataPath, Helper)) {45return false;46}47}48return true;49}5051bool CEPHandler::Load(const std::vector<std::string>& DataPath, Helpers* Helper) {52//Deklaration53// get string identifier for PHEM emission class54//C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'ToString':55std::string emissionRep = Helper->getgClass();5657// to hold everything.58std::vector<std::vector<double> > matrixSpeedInertiaTable;59std::vector<std::vector<double> > normedTragTableSpeedInertiaTable;60std::vector<std::vector<double> > matrixFC;61std::vector<std::vector<double> > matrixPollutants;62std::vector<double> idlingValuesFC;63std::vector<double> idlingValuesPollutants;64std::vector<std::string> headerFC;65std::vector<std::string> headerPollutants;6667double vehicleMass;68double vehicleLoading;69double vehicleMassRot;70double crosssectionalArea;71double cwValue;72double f0;73double f1;74double f2;75double f3;76double f4;77double axleRatio;78std::vector<double> transmissionGearRatios;79double auxPower;80double ratedPower;81double engineIdlingSpeed;82double engineRatedSpeed;83double effectiveWhellDiameter;84std::string vehicleMassType;85std::string vehicleFuelType;86double pNormV0;87double pNormP0;88double pNormV1;89double pNormP1;9091if (!ReadVehicleFile(DataPath, emissionRep, Helper, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, axleRatio, auxPower, ratedPower, engineIdlingSpeed, engineRatedSpeed, effectiveWhellDiameter, transmissionGearRatios, vehicleMassType, vehicleFuelType, pNormV0, pNormP0, pNormV1, pNormP1, matrixSpeedInertiaTable, normedTragTableSpeedInertiaTable)) {92return false;93}9495if (!ReadEmissionData(true, DataPath, emissionRep, Helper, headerFC, matrixFC, idlingValuesFC)) {96return false;97}9899if (!ReadEmissionData(false, DataPath, emissionRep, Helper, headerPollutants, matrixPollutants, idlingValuesPollutants)) {100return false;101}102103_ceps.insert(std::make_pair(Helper->getgClass(), new CEP(vehicleMassType == Constants::HeavyVehicle, vehicleMass, vehicleLoading, vehicleMassRot, crosssectionalArea, cwValue, f0, f1, f2, f3, f4, axleRatio, transmissionGearRatios, auxPower, ratedPower, engineIdlingSpeed, engineRatedSpeed, effectiveWhellDiameter, pNormV0, pNormP0, pNormV1, pNormP1, vehicleFuelType, matrixFC, headerPollutants, matrixPollutants, matrixSpeedInertiaTable, normedTragTableSpeedInertiaTable, idlingValuesFC.front(), idlingValuesPollutants)));104105return true;106}107108bool CEPHandler::ReadVehicleFile(const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, double& vehicleMass, double& vehicleLoading, double& vehicleMassRot, double& crossArea, double& cWValue, double& f0, double& f1, double& f2, double& f3, double& f4, double& axleRatio, double& auxPower, double& ratedPower, double& engineIdlingSpeed, double& engineRatedSpeed, double& effectiveWheelDiameter, std::vector<double>& transmissionGearRatios, std::string& vehicleMassType, std::string& vehicleFuelType, double& pNormV0, double& pNormP0, double& pNormV1, double& pNormP1, std::vector<std::vector<double> >& matrixSpeedInertiaTable, std::vector<std::vector<double> >& normedDragTable) {109vehicleMass = 0;110vehicleLoading = 0;111vehicleMassRot = 0;112crossArea = 0;113cWValue = 0;114f0 = 0;115f1 = 0;116f2 = 0;117f3 = 0;118f4 = 0;119axleRatio = 0;120ratedPower = 0;121auxPower = 0;122engineIdlingSpeed = 0;123engineRatedSpeed = 0;124effectiveWheelDiameter = 0;125vehicleMassType = "";126vehicleFuelType = "";127pNormV0 = 0;128pNormP0 = 0;129pNormV1 = 0;130pNormP1 = 0;131transmissionGearRatios = std::vector<double>();132matrixSpeedInertiaTable = std::vector<std::vector<double> >();133normedDragTable = std::vector<std::vector<double> >();134std::string line;135std::string cell;136int dataCount = 0;137138//Open file139std::ifstream vehicleReader;140for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {141vehicleReader.open(((*i) + emissionClass + ".PHEMLight.veh").c_str());142if (vehicleReader.good()) {143break;144}145}146if (!vehicleReader.good()) {147Helper->setErrMsg("File does not exist! (" + emissionClass + ".PHEMLight.veh)");148return false;149}150151// skip header152ReadLine(vehicleReader);153154while ((line = ReadLine(vehicleReader)) != "" && dataCount <= 49) {155if (line.substr(0, 1) == Helper->getCommentPrefix()) {156continue;157}158else {159dataCount++;160}161162cell = split(line, ',')[0];163164// reading Mass165if (dataCount == 1) {166vehicleMass = todouble(cell);167}168169// reading vehicle loading170if (dataCount == 2) {171vehicleLoading = todouble(cell);172}173174// reading cWValue175if (dataCount == 3) {176cWValue = todouble(cell);177}178179// reading crossectional area180if (dataCount == 4) {181crossArea = todouble(cell);182}183184// reading vehicle mass rotational185if (dataCount == 7) {186vehicleMassRot = todouble(cell);187}188189// reading rated power190if (dataCount == 9) {191auxPower = todouble(cell);192}193194// reading rated power195if (dataCount == 10) {196ratedPower = todouble(cell);197}198199// reading engine rated speed200if (dataCount == 11) {201engineRatedSpeed = todouble(cell);202}203204// reading engine idling speed205if (dataCount == 12) {206engineIdlingSpeed = todouble(cell);207}208209// reading f0210if (dataCount == 14) {211f0 = todouble(cell);212}213214// reading f1215if (dataCount == 15) {216f1 = todouble(cell);217}218219// reading f2220if (dataCount == 16) {221f2 = todouble(cell);222}223224// reading f3225if (dataCount == 17) {226f3 = todouble(cell);227}228229// reading f4230if (dataCount == 18) {231f4 = todouble(cell);232}233234// reading axleRatio235if (dataCount == 21) {236axleRatio = todouble(cell);237}238239// reading effective wheel diameter240if (dataCount == 22) {241effectiveWheelDiameter = todouble(cell);242}243244if (dataCount >= 23 && dataCount <= 40) {245transmissionGearRatios.push_back(todouble(cell));246}247248// reading vehicleMassType249if (dataCount == 45) {250vehicleMassType = cell;251}252253// reading vehicleFuelType254if (dataCount == 46) {255vehicleFuelType = cell;256}257258// reading pNormV0259if (dataCount == 47) {260pNormV0 = todouble(cell);261}262263// reading pNormP0264if (dataCount == 48) {265pNormP0 = todouble(cell);266}267268// reading pNormV1269if (dataCount == 49) {270pNormV1 = todouble(cell);271}272273// reading pNormP1274if (dataCount == 50) {275pNormP1 = todouble(cell);276}277}278279while ((line = ReadLine(vehicleReader)) != "" && line.substr(0, 1) != Helper->getCommentPrefix()) {280if (line.substr(0, 1) == Helper->getCommentPrefix()) {281continue;282}283284matrixSpeedInertiaTable.push_back(todoubleList(split(line, ',')));285}286287while ((line = ReadLine(vehicleReader)) != "") {288if (line.substr(0, 1) == Helper->getCommentPrefix()) {289continue;290}291292normedDragTable.push_back(todoubleList(split(line, ',')));293}294295return true;296}297298bool CEPHandler::ReadEmissionData(bool readFC, const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, std::vector<std::string>& header, std::vector<std::vector<double> >& matrix, std::vector<double>& idlingValues) {299// declare file stream300std::string line;301header = std::vector<std::string>();302matrix = std::vector<std::vector<double> >();303idlingValues = std::vector<double>();304305std::string pollutantExtension = "";306if (readFC) {307pollutantExtension += std::string("_FC");308}309310std::ifstream fileReader;311for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) {312fileReader.open(((*i) + emissionClass + pollutantExtension + ".csv").c_str());313if (fileReader.good()) {314break;315}316}317if (!fileReader.good()) {318Helper->setErrMsg("File does not exist! (" + emissionClass + pollutantExtension + ".csv)");319return false;320}321322// read header line for pollutant identifiers323if ((line = ReadLine(fileReader)) != "") {324std::vector<std::string> entries = split(line, ',');325// skip first entry "Pe"326for (int i = 1; i < (int)entries.size(); i++) {327header.push_back(entries[i]);328}329}330331// skip units332ReadLine(fileReader);333334// skip comment335ReadLine(fileReader);336337//readIdlingValues338line = ReadLine(fileReader);339340std::vector<std::string> stringIdlings = split(line, ',');341stringIdlings.erase(stringIdlings.begin());342343idlingValues = todoubleList(stringIdlings);344345while ((line = ReadLine(fileReader)) != "") {346matrix.push_back(todoubleList(split(line, ',')));347}348return true;349}350351std::vector<std::string> CEPHandler::split(const std::string& s, char delim) {352std::vector<std::string> elems;353std::stringstream ss(s);354std::string item;355while (std::getline(ss, item, delim)) {356elems.push_back(item);357}358return elems;359}360361double CEPHandler::todouble(const std::string& s) {362std::stringstream ss(s);363double item;364ss >> item;365return item;366}367368std::vector<double> CEPHandler::todoubleList(const std::vector<std::string>& s) {369std::vector<double> result;370for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); ++i) {371result.push_back(todouble(*i));372}373return result;374}375376std::string CEPHandler::ReadLine(std::ifstream& s) {377std::string line;378std::getline(s, line);379size_t lastNWChar = line.find_last_not_of(" \n\r\t");380if (lastNWChar != std::string::npos) {381line.erase(lastNWChar + 1);382}383return line;384}385}386387388