Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
HelpersPHEMlight5.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2013-2023 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// Helper methods for PHEMlight-based emission computation
21/****************************************************************************/
22#include <config.h>
23
24#include <limits>
25#include <cmath>
30
31#include "EnergyParams.h"
32#include "HelpersPHEMlight5.h"
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
39 HelpersPHEMlight("PHEMlight5", PHEMLIGHT5_BASE, -1),
40 myIndex(PHEMLIGHT5_BASE) {
41}
42
43
45 for (const auto& cep : myCEPs) {
46 delete cep.second;
47 }
48}
49
50
52HelpersPHEMlight5::getClassByName(const std::string& eClass, const SUMOVehicleClass vc) {
53 if (eClass == "unknown" && !myEmissionClassStrings.hasString("unknown")) {
54 myEmissionClassStrings.addAlias("unknown", getClassByName("PC_EU4_G", vc));
55 }
56 if (eClass == "default" && !myEmissionClassStrings.hasString("default")) {
57 myEmissionClassStrings.addAlias("default", getClassByName("PC_EU4_G", vc));
58 }
60 return myEmissionClassStrings.get(eClass);
61 }
62 if (eClass.size() < 6) {
63 throw InvalidArgument("Unknown emission class '" + eClass + "'.");
64 }
66 myVolumetricFuel = oc.getBool("emissions.volumetric-fuel");
67 std::vector<std::string> phemPath;
68 phemPath.push_back(oc.getString("phemlight-path") + "/");
69 if (getenv("PHEMLIGHT_PATH") != nullptr) {
70 phemPath.push_back(std::string(getenv("PHEMLIGHT_PATH")) + "/");
71 }
72 if (getenv("SUMO_HOME") != nullptr) {
73 phemPath.push_back(std::string(getenv("SUMO_HOME")) + "/data/emissions/PHEMlight5/");
74 }
75 if (myCorrection == nullptr && (!oc.isDefault("phemlight-year") || !oc.isDefault("phemlight-temperature"))) {
77 if (!oc.isDefault("phemlight-year")) {
78 myCorrection->setYear(oc.getInt("phemlight-year"));
79 std::string err;
80 if (!myCorrection->ReadDet(err)) {
81 throw InvalidArgument("Error reading PHEMlight5 deterioration data.\n" + err);
82 }
84 }
85 if (!oc.isDefault("phemlight-temperature")) {
86 myCorrection->setAmbTemp(oc.getFloat("phemlight-temperature"));
87 std::string err;
88 if (!myCorrection->ReadTNOx(err)) {
89 throw InvalidArgument("Error reading PHEMlight5 deterioration data.\n" + err);
90 }
92 }
93 }
96 myHelper.setclass(eClass);
97 if (!myCEPHandler.GetCEP(phemPath, &myHelper, myCorrection)) {
98 throw InvalidArgument("File for PHEMlight5 emission class " + eClass + " not found.\n" + myHelper.getErrMsg());
99 }
100 PHEMlightdllV5::CEP* const currCep = myCEPHandler.getCEPS().find(myHelper.getgClass())->second;
101 int index = myIndex++;
102 if (currCep->getHeavyVehicle()) {
104 }
105 myEmissionClassStrings.insert(eClass, index);
106 myCEPs[index] = currCep;
108 return index;
109}
110
111
112double
113HelpersPHEMlight5::getEmission(PHEMlightdllV5::CEP* currCep, const std::string& e, const double p, const double v) const {
114 return currCep->GetEmission(e, p, v, &myHelper);
115}
116
117
118double
119HelpersPHEMlight5::getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope) const {
120 PHEMlightdllV5::CEP* currCep = myCEPs.count(c) == 0 ? nullptr : myCEPs.find(c)->second;
121 if (currCep != nullptr) {
123 return v == 0.0 ? 0.0 : MIN2(a, currCep->GetMaxAccel(v, slope, isHBEV));
124 }
125 return a;
126}
127
128
129double
130HelpersPHEMlight5::getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* /* param */) const {
131 return myCEPs.find(c)->second->GetDecelCoast(v, a, slope);
132}
133
134
135double
136HelpersPHEMlight5::compute(const SUMOEmissionClass c, const PollutantsInterface::EmissionType e, const double v, const double a, const double slope, const EnergyParams* param) const {
137 if (param != nullptr && param->isEngineOff()) {
138 return 0.;
139 }
140 const double corrSpeed = MAX2(0.0, v);
141 assert(myCEPs.count(c) == 1);
142 PHEMlightdllV5::CEP* const currCep = myCEPs.find(c)->second;
143 const double corrAcc = getModifiedAccel(c, corrSpeed, a, slope);
144 const bool isBEV = currCep->getFuelType() == PHEMlightdllV5::Constants::strBEV;
145 const bool isHybrid = currCep->getFuelType() == PHEMlightdllV5::Constants::strHybrid;
146 const double power_raw = currCep->CalcPower(corrSpeed, corrAcc, slope, isBEV || isHybrid);
147 const double power = isHybrid ? currCep->CalcWheelPower(corrSpeed, corrAcc, slope) : currCep->CalcEngPower(power_raw);
148
149 if (!isBEV && corrAcc < currCep->GetDecelCoast(corrSpeed, corrAcc, slope) &&
151 return 0.;
152 }
153 const std::string& fuelType = currCep->getFuelType();
154 switch (e) {
156 return getEmission(currCep, "CO", power, corrSpeed) / SECONDS_PER_HOUR * 1000.;
158 return currCep->GetCO2Emission(getEmission(currCep, "FC", power, corrSpeed),
159 getEmission(currCep, "CO", power, corrSpeed),
160 getEmission(currCep, "HC", power, corrSpeed), &myHelper) / SECONDS_PER_HOUR * 1000.;
162 return getEmission(currCep, "HC", power, corrSpeed) / SECONDS_PER_HOUR * 1000.;
164 return getEmission(currCep, "NOx", power, corrSpeed) / SECONDS_PER_HOUR * 1000.;
166 return getEmission(currCep, "PM", power, corrSpeed) / SECONDS_PER_HOUR * 1000.;
168 if (myVolumetricFuel && fuelType == PHEMlightdllV5::Constants::strDiesel) { // divide by average diesel density of 836 g/l
169 return getEmission(currCep, "FC", power, corrSpeed) / 836. / SECONDS_PER_HOUR * 1000.;
170 }
171 if (myVolumetricFuel && fuelType == PHEMlightdllV5::Constants::strGasoline) { // divide by average gasoline density of 742 g/l
172 return getEmission(currCep, "FC", power, corrSpeed) / 742. / SECONDS_PER_HOUR * 1000.;
173 }
174 if (fuelType == PHEMlightdllV5::Constants::strBEV) {
175 return 0.;
176 }
177 return getEmission(currCep, "FC", power, corrSpeed) / SECONDS_PER_HOUR * 1000.; // still in mg even if myVolumetricFuel is set!
178 }
180 if (fuelType == PHEMlightdllV5::Constants::strBEV) {
181 return (getEmission(currCep, "FC_el", power, corrSpeed) + currCep->getAuxPower()) / SECONDS_PER_HOUR * 1000.;
182 }
183 return 0;
184 }
185 // should never get here
186 return 0.;
187}
188
189
190/****************************************************************************/
const double SECONDS_PER_HOUR
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
int SUMOEmissionClass
T MIN2(T a, T b)
Definition StdDefs.h:76
T MAX2(T a, T b)
Definition StdDefs.h:82
An upper class for objects with additional parameters.
bool isEngineOff() const
Returns the state of the engine when the vehicle is not moving.
int myIndex
the index of the next class
double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope) const
Returns the adapted acceleration value, useful for comparing with external PHEMlight references.
HelpersPHEMlight5()
Constructor.
virtual double getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param) const
Returns the maximum deceleration value (as a negative number), which can still be considered as non-b...
double compute(const SUMOEmissionClass c, const PollutantsInterface::EmissionType e, const double v, const double a, const double slope, const EnergyParams *param) const
Returns the amount of emitted pollutant given the vehicle type and state (in mg/s or in ml/s for fuel...
PHEMlightdllV5::CEPHandler myCEPHandler
std::map< SUMOEmissionClass, PHEMlightdllV5::CEP * > myCEPs
PHEMlightdllV5::Correction * myCorrection
double getEmission(PHEMlightdllV5::CEP *currCep, const std::string &e, const double p, const double v) const
Returns the amount of emitted pollutant given the vehicle type and state (in mg/s or in ml/s for fuel...
SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc)
Checks whether the string describes a known vehicle class.
virtual ~HelpersPHEMlight5()
Destructor.
PHEMlightdllV5::Helpers myHelper
Helper methods for PHEMlight-based emission computation.
A storage for options typed value containers)
Definition OptionsCont.h:89
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
const std::map< std::string, CEP * > & getCEPS() const
bool GetCEP(std::vector< std::string > &DataPath, Helpers *Helper, Correction *DataCor)
double CalcPower(double speed, double acc, double gradient, bool HBEV)
double CalcWheelPower(double speed, double acc, double gradient)
const bool & getHeavyVehicle() const
double GetMaxAccel(double speed, double gradient, bool HBEV)
const std::string & getFuelType() const
double getAuxPower() const
Definition V5/cpp/CEP.h:77
double CalcEngPower(double power)
double GetCO2Emission(double _FC, double _CO, double _HC, Helpers *VehicleClass)
double GetEmission(const std::string &pollutant, double power, double speed, Helpers *VehicleClass)
static const std::string strBEV
static const double ZERO_SPEED_ACCURACY
static const std::string strHybrid
static const std::string strGasoline
static const std::string strDiesel
void setAmbTemp(const double &value)
bool ReadDet(std::string &ErrMSG)
bool ReadTNOx(std::string &ErrMSG)
void setYear(const int &value)
void setUseTNOx(const bool &value)
void setUseDet(const bool &value)
void setPHEMDataV(const std::string &value)
bool setclass(const std::string &VEH)
const std::string & getgClass() const
void setCommentPrefix(const std::string &value)
const std::string & getErrMsg() const
bool myVolumetricFuel
return fuel consumption in l instead of mg
StringBijection< SUMOEmissionClass > myEmissionClassStrings
Mapping between emission class names and integer representations.
EmissionType
Enumerating all emission types, including fuel.
static const int HEAVY_BIT
the bit to set for denoting heavy vehicles
bool hasString(const std::string &str) const
void addAlias(const std::string str, const T key)
T get(const std::string &str) const
void insert(const std::string str, const T key, bool checkDuplicates=true)
static std::string to_lower_case(const std::string &str)
Transfers the content to lower case.