Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
PollutantsInterface.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-2024 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/****************************************************************************/
19// Interface to capsulate different emission models
20/****************************************************************************/
21#include <config.h>
22
23#include <limits>
24#include <cmath>
30
31#include "HelpersHBEFA.h"
32#include "HelpersHBEFA3.h"
33#include "HelpersHBEFA4.h"
34#include "HelpersPHEMlight.h"
35#include "HelpersEnergy.h"
36#include "HelpersMMPEVEM.h"
37#include "HelpersPHEMlight5.h"
38#include "PollutantsInterface.h"
39
40
41// ===========================================================================
42// static definitions
43// ===========================================================================
59};
60std::vector<std::string> PollutantsInterface::myAllClassesStr;
61
62
63// ===========================================================================
64// method definitions
65// ===========================================================================
66
67// ---------------------------------------------------------------------------
68// PollutantsInterface::Emissions - methods
69// ---------------------------------------------------------------------------
70
71PollutantsInterface::Emissions::Emissions(double co2, double co, double hc, double f, double nox, double pmx, double elec) :
72 CO2(co2),
73 CO(co),
74 HC(hc),
75 fuel(f),
76 NOx(nox),
77 PMx(pmx),
78 electricity(elec) {
79}
80
81
82void PollutantsInterface::Emissions::addScaled(const Emissions& a, const double scale) {
83 CO2 += scale * a.CO2;
84 CO += scale * a.CO;
85 HC += scale * a.HC;
86 fuel += scale * a.fuel;
87 NOx += scale * a.NOx;
88 PMx += scale * a.PMx;
89 electricity += scale * a.electricity;
90}
91
92// ---------------------------------------------------------------------------
93// PollutantsInterface::Helper - methods
94// ---------------------------------------------------------------------------
95
96PollutantsInterface::Helper::Helper(std::string name, const int baseIndex, const int defaultClass) :
97 myName(name),
98 myBaseIndex(baseIndex) {
99 if (defaultClass != -1) {
100 myEmissionClassStrings.insert("default", defaultClass);
101 myEmissionClassStrings.addAlias("unknown", defaultClass);
102 }
103}
104
105
106const
108 return myName;
109}
110
111
115 myVolumetricFuel = OptionsCont::getOptions().exists("emissions.volumetric-fuel") && OptionsCont::getOptions().getBool("emissions.volumetric-fuel");
116 if (myEmissionClassStrings.hasString(eClass)) {
117 return myEmissionClassStrings.get(eClass);
118 }
119 return myEmissionClassStrings.get(StringUtils::to_lower_case(eClass));
120}
121
122
123const std::string
125 return myName + "/" + myEmissionClassStrings.getString(c);
126}
127
128
129bool
131 return (c & (int)0xffffffff & ~HEAVY_BIT) == 0;
132}
133
134
136PollutantsInterface::Helper::getClass(const SUMOEmissionClass base, const std::string& vClass, const std::string& fuel, const std::string& eClass, const double weight) const {
137 UNUSED_PARAMETER(vClass);
138 UNUSED_PARAMETER(fuel);
139 UNUSED_PARAMETER(eClass);
140 UNUSED_PARAMETER(weight);
141 return base;
142}
143
144
145std::string
150
151
152std::string
155 return "Gasoline";
156}
157
158
159int
164
165
166double
171
172
173double
174PollutantsInterface::Helper::compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams* param) const {
179 UNUSED_PARAMETER(slope);
180 UNUSED_PARAMETER(param);
181 return 0.;
182}
183
184
185double
186PollutantsInterface::Helper::getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) const {
189 UNUSED_PARAMETER(slope);
190 UNUSED_PARAMETER(param);
191 return a;
192}
193
194
195double
196PollutantsInterface::Helper::getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) const {
197 // the interpolation for small v is basically the same as in PHEMlightdllV5::CEP::GetDecelCoast
200 }
201 if (param == nullptr) {
202 param = EnergyParams::getDefault();
203 }
204 // the magic numbers below come from a linear interpolation with http://ts-sim-service-ba/svn/simo/trunk/projects/sumo/data/emissions/linear.py
205 const double mass = param->getDouble(SUMO_ATTR_MASS);
206 const double incl = param->getDouble(SUMO_ATTR_FRONTSURFACEAREA) / mass * -9.05337017 + -0.00017774;
207 const double grad = PHEMlightdllV5::Constants::GRAVITY_CONST * slope / 100.;
208 return MIN2(0., incl * v + 0.00001066 * mass + -0.38347107 - 20.0 * incl - grad);
209}
210
211
212void
213PollutantsInterface::Helper::addAllClassesInto(std::vector<SUMOEmissionClass>& list) const {
214 myEmissionClassStrings.addKeysInto(list);
215}
216
217
218bool
220 return (c >> 16) == (myBaseIndex >> 16);
221}
222
223// ---------------------------------------------------------------------------
224// PollutantsInterface - methods
225// ---------------------------------------------------------------------------
226
228PollutantsInterface::getClassByName(const std::string& eClass, const SUMOVehicleClass vc) {
229 const std::string::size_type sep = eClass.find("/");
230 const std::string model = eClass.substr(0, sep); // this includes the case of no separator
231 for (int i = 0; i < 8; i++) {
232 if (myHelpers[i]->getName() == model) {
233 if (sep != std::string::npos) {
234 const std::string subClass = eClass.substr(sep + 1);
235 if (subClass == "zero") {
236 return myZeroHelper.getClassByName("default", vc);
237 }
238 return myHelpers[i]->getClassByName(subClass, vc);
239 }
240 return myHelpers[i]->getClassByName("default", vc);
241 }
242 }
243 if (sep == std::string::npos) {
244 if (eClass == "zero") {
245 return myZeroHelper.getClassByName("default", vc);
246 }
247 // default HBEFA2
248 return myHBEFA2Helper.getClassByName(eClass, vc);
249 }
250 throw InvalidArgument("Unknown emission class '" + eClass + "'.");
251}
252
253
254const std::vector<SUMOEmissionClass>
256 std::vector<SUMOEmissionClass> result;
257 for (int i = 0; i < 8; i++) {
258 myHelpers[i]->addAllClassesInto(result);
259 }
260 return result;
261}
262
263
264const std::vector<std::string>&
266 // first check if myAllClassesStr has to be filled
267 if (myAllClassesStr.empty()) {
268 // first obtain all emissionClasses
269 std::vector<SUMOEmissionClass> emissionClasses;
270 for (int i = 0; i < 8; i++) {
271 myHelpers[i]->addAllClassesInto(emissionClasses);
272 }
273 // now write all emissionClasses in myAllClassesStr
274 for (const auto& i : emissionClasses) {
275 myAllClassesStr.push_back(getName(i));
276 }
277 }
278 return myAllClassesStr;
279}
280
281std::string
285
286
287std::string
289 switch (e) {
290 case CO2:
291 return "CO2";
292 case CO:
293 return "CO";
294 case HC:
295 return "HC";
296 case FUEL:
297 return "fuel";
298 case NO_X:
299 return "NOx";
300 case PM_X:
301 return "PMx";
302 case ELEC:
303 return "electricity";
304 default:
305 throw InvalidArgument("Unknown emission type '" + toString(e) + "'");
306 }
307}
308
309bool
311 return (c & HEAVY_BIT) != 0;
312}
313
314
315bool
317 return myHelpers[c >> 16]->isSilent(c);
318}
319
320
322PollutantsInterface::getClass(const SUMOEmissionClass base, const std::string& vClass,
323 const std::string& fuel, const std::string& eClass, const double weight) {
324 return myHelpers[base >> 16]->getClass(base, vClass, fuel, eClass, weight);
325}
326
327
328std::string
332
333
334std::string
336 return myHelpers[c >> 16]->getFuel(c);
337}
338
339
340int
344
345
346double
350
351
352double
353PollutantsInterface::compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams* param) {
354 return myHelpers[c >> 16]->compute(c, e, v, a, slope, param);
355}
356
357
359PollutantsInterface::computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
360 const Helper* const h = myHelpers[c >> 16];
361 return Emissions(h->compute(c, CO2, v, a, slope, param), h->compute(c, CO, v, a, slope, param), h->compute(c, HC, v, a, slope, param),
362 h->compute(c, FUEL, v, a, slope, param), h->compute(c, NO_X, v, a, slope, param), h->compute(c, PM_X, v, a, slope, param),
363 h->compute(c, ELEC, v, a, slope, param));
364}
365
366
367double
368PollutantsInterface::computeDefault(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const double tt, const EnergyParams* param) {
369 const Helper* const h = myHelpers[c >> 16];
370 return (h->compute(c, e, v, 0, slope, param) + h->compute(c, e, v - a, a, slope, param)) * tt / 2.;
371}
372
373
374double
375PollutantsInterface::getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
376 return myHelpers[c >> 16]->getModifiedAccel(c, v, a, slope, param);
377}
378
379
380double
381PollutantsInterface::getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
382 return myHelpers[c >> 16]->getCoastingDecel(c, v, a, slope, param);
383}
384
385
386const HelpersEnergy&
390
391/****************************************************************************/
int SUMOEmissionClass
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SUMO_ATTR_MASS
@ SUMO_ATTR_FRONTSURFACEAREA
Front surface area.
#define UNUSED_PARAMETER(x)
Definition StdDefs.h:30
T MIN2(T a, T b)
Definition StdDefs.h:76
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
An upper class for objects with additional parameters.
double getDouble(SumoXMLAttr attr) const
Returns the value for a given key.
static const EnergyParams * getDefault()
Helper methods for energy-based electricity consumption computation based on the battery device.
Helper methods for HBEFA3-based emission computation.
Helper methods for HBEFA4-based emission computation.
Helper methods for HBEFA-based emission computation.
This helper class allows the PollutantsInterface to load and use different MMPEVEMs.
Helper methods for PHEMlight-based emission computation.
Helper methods for PHEMlight-based emission computation.
bool exists(const std::string &name) const
Returns the information whether the named option is known.
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.
static const double SPEED_DCEL_MIN
static const double GRAVITY_CONST
zero emission model, used as superclass for the other model helpers
virtual bool isSilent(const SUMOEmissionClass c)
Returns whether the class denotes a silent vehicle for interfacing with the noise model....
const std::string getClassName(const SUMOEmissionClass c) const
Returns the complete name of the emission class including the model.
virtual SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc)
Returns the emission class associated with the given name, aliases are possible If this method is ask...
const std::string & getName() const
Returns the name of the model.
bool includesClass(const SUMOEmissionClass c) const
virtual SUMOEmissionClass getClass(const SUMOEmissionClass base, const std::string &vClass, const std::string &fuel, const std::string &eClass, const double weight) const
Returns the emission class described by the given parameters. The base is used to determine the model...
void addAllClassesInto(std::vector< SUMOEmissionClass > &list) const
Add all known emission classes of this model to the given container.
virtual std::string getAmitranVehicleClass(const SUMOEmissionClass c) const
Returns the vehicle class described by this emission class as described in the Amitran interface (Pas...
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...
virtual double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param) const
Returns the adapted acceleration value, useful for comparing with external PHEMlight references....
virtual double getWeight(const SUMOEmissionClass c) const
Returns a reference weight in kg described by this emission class as described in the Amitran interfa...
Helper(std::string name, const int baseIndex, const int defaultClass)
Constructor, intializes the name.
virtual double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams *param) const
Returns the amount of the emitted pollutant given the vehicle type and state (in mg/s or ml/s for fue...
static const double ZERO_SPEED_ACCURACY
the lowest speed which allows reliable coasting calculations
virtual int getEuroClass(const SUMOEmissionClass c) const
Returns the Euro emission class described by this emission class as described in the Amitran interfac...
StringBijection< SUMOEmissionClass > myEmissionClassStrings
Mapping between emission class names and integer representations.
virtual std::string getFuel(const SUMOEmissionClass c) const
Returns the fuel type described by this emission class as described in the Amitran interface (Gasolin...
static bool isSilent(const SUMOEmissionClass c)
Checks whether the emission class describes an electric or similar silent vehicle.
static HelpersHBEFA3 myHBEFA3Helper
Instance of HBEFA3Helper which gets cleaned up automatically.
static HelpersPHEMlight5 myPHEMlight5Helper
Instance of PHEMlight5Helper which gets cleaned up automatically.
static HelpersPHEMlight myPHEMlightHelper
Instance of PHEMlightHelper which gets cleaned up automatically.
static std::string getAmitranVehicleClass(const SUMOEmissionClass c)
Returns the vehicle class described by the given emission class.
static const HelpersEnergy & getEnergyHelper()
get energy helper
static std::string getPollutantName(const EmissionType e)
return the name for the given emission type
static double computeDefault(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const double tt, const EnergyParams *param)
Returns the amount of emitted pollutant given the vehicle type and default values for the state (in m...
EmissionType
Enumerating all emission types, including fuel.
static std::string getFuel(const SUMOEmissionClass c)
Returns the fuel type of the given emission class.
static bool isHeavy(const SUMOEmissionClass c)
Checks whether the emission class describes a bus, truck or similar vehicle.
static HelpersHBEFA4 myHBEFA4Helper
Instance of HBEFA4Helper which gets cleaned up automatically.
static double getWeight(const SUMOEmissionClass c)
Returns a representative weight for the given emission class see http://colombo-fp7....
static double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param)
Returns the adapted acceleration value, useful for comparing with external PHEMlight references.
static std::vector< std::string > myAllClassesStr
get all emission classes in string format
static HelpersHBEFA myHBEFA2Helper
Instance of HBEFA2Helper which gets cleaned up automatically.
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
static const std::vector< SUMOEmissionClass > getAllClasses()
Checks whether the string describes a known vehicle class.
static const int ZERO_EMISSIONS
the first class in each model representing a zero emission vehicle
static SUMOEmissionClass getClass(const SUMOEmissionClass base, const std::string &vClass, const std::string &fuel, const std::string &eClass, const double weight)
Returns the emission class fittig the given parameters.
static const int HEAVY_BIT
the bit to set for denoting heavy vehicles
static double getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param)
Returns the coasting deceleration value, useful for comparing with external PHEMlight references.
static int getEuroClass(const SUMOEmissionClass c)
Returns the Euro norm described by the given emission class.
static HelpersEnergy myEnergyHelper
Instance of EnergyHelper which gets cleaned up automatically.
static double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams *param)
Returns the amount of the emitted pollutant given the vehicle type and state (in mg/s or ml/s for fue...
static const std::vector< std::string > & getAllClassesStr()
Get all SUMOEmissionClass in string format.
static Helper * myHelpers[]
the known model helpers
static Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
static HelpersMMPEVEM myMMPEVEMHelper
Instance of HelpersMMPEVEM which gets cleaned up automatically.
static Helper myZeroHelper
Instance of Helper which gets cleaned up automatically.
void addAlias(const std::string str, const T key)
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.
Storage for collected values of all emission types.
void addScaled(const Emissions &a, const double scale=1.)
Add the values of the other struct to this one, scaling the values if needed.
Emissions(double co2=0, double co=0, double hc=0, double f=0, double nox=0, double pmx=0, double elec=0)
Constructor, intializes all members.