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 area = param->getDouble(SUMO_ATTR_WIDTH) * param->getDouble(SUMO_ATTR_HEIGHT) * M_PI / 4.;
207 const double incl = area / mass * -9.05337017 + -0.00017774;
208 const double grad = PHEMlightdllV5::Constants::GRAVITY_CONST * slope / 100.;
209 return MIN2(0., incl * v + 0.00001066 * mass + -0.38347107 - 20.0 * incl - grad);
210}
211
212
213void
214PollutantsInterface::Helper::addAllClassesInto(std::vector<SUMOEmissionClass>& list) const {
215 myEmissionClassStrings.addKeysInto(list);
216}
217
218
219bool
221 return (c >> 16) == (myBaseIndex >> 16);
222}
223
224// ---------------------------------------------------------------------------
225// PollutantsInterface - methods
226// ---------------------------------------------------------------------------
227
229PollutantsInterface::getClassByName(const std::string& eClass, const SUMOVehicleClass vc) {
230 const std::string::size_type sep = eClass.find("/");
231 const std::string model = eClass.substr(0, sep); // this includes the case of no separator
232 for (int i = 0; i < 8; i++) {
233 if (myHelpers[i]->getName() == model) {
234 if (sep != std::string::npos) {
235 const std::string subClass = eClass.substr(sep + 1);
236 if (subClass == "zero") {
237 return myZeroHelper.getClassByName("default", vc);
238 }
239 return myHelpers[i]->getClassByName(subClass, vc);
240 }
241 return myHelpers[i]->getClassByName("default", vc);
242 }
243 }
244 if (sep == std::string::npos) {
245 if (eClass == "zero") {
246 return myZeroHelper.getClassByName("default", vc);
247 }
248 // default HBEFA2
249 return myHBEFA2Helper.getClassByName(eClass, vc);
250 }
251 throw InvalidArgument("Unknown emission class '" + eClass + "'.");
252}
253
254
255const std::vector<SUMOEmissionClass>
257 std::vector<SUMOEmissionClass> result;
258 for (int i = 0; i < 8; i++) {
259 myHelpers[i]->addAllClassesInto(result);
260 }
261 return result;
262}
263
264
265const std::vector<std::string>&
267 // first check if myAllClassesStr has to be filled
268 if (myAllClassesStr.empty()) {
269 // first obtain all emissionClasses
270 std::vector<SUMOEmissionClass> emissionClasses;
271 for (int i = 0; i < 8; i++) {
272 myHelpers[i]->addAllClassesInto(emissionClasses);
273 }
274 // now write all emissionClasses in myAllClassesStr
275 for (const auto& i : emissionClasses) {
276 myAllClassesStr.push_back(getName(i));
277 }
278 }
279 return myAllClassesStr;
280}
281
282std::string
286
287
288std::string
290 switch (e) {
291 case CO2:
292 return "CO2";
293 case CO:
294 return "CO";
295 case HC:
296 return "HC";
297 case FUEL:
298 return "fuel";
299 case NO_X:
300 return "NOx";
301 case PM_X:
302 return "PMx";
303 case ELEC:
304 return "electricity";
305 default:
306 throw InvalidArgument("Unknown emission type '" + toString(e) + "'");
307 }
308}
309
310bool
312 return (c & HEAVY_BIT) != 0;
313}
314
315
316bool
318 return myHelpers[c >> 16]->isSilent(c);
319}
320
321
323PollutantsInterface::getClass(const SUMOEmissionClass base, const std::string& vClass,
324 const std::string& fuel, const std::string& eClass, const double weight) {
325 return myHelpers[base >> 16]->getClass(base, vClass, fuel, eClass, weight);
326}
327
328
329std::string
333
334
335std::string
337 return myHelpers[c >> 16]->getFuel(c);
338}
339
340
341int
345
346
347double
351
352
353double
354PollutantsInterface::compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams* param) {
355 return myHelpers[c >> 16]->compute(c, e, v, a, slope, param);
356}
357
358
360PollutantsInterface::computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
361 const Helper* const h = myHelpers[c >> 16];
362 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),
363 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),
364 h->compute(c, ELEC, v, a, slope, param));
365}
366
367
368double
369PollutantsInterface::computeDefault(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const double tt, const EnergyParams* param) {
370 const Helper* const h = myHelpers[c >> 16];
371 return (h->compute(c, e, v, 0, slope, param) + h->compute(c, e, v - a, a, slope, param)) * tt / 2.;
372}
373
374
375double
376PollutantsInterface::getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
377 return myHelpers[c >> 16]->getModifiedAccel(c, v, a, slope, param);
378}
379
380
381double
382PollutantsInterface::getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
383 return myHelpers[c >> 16]->getCoastingDecel(c, v, a, slope, param);
384}
385
386
387const HelpersEnergy&
391
392/****************************************************************************/
int SUMOEmissionClass
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SUMO_ATTR_MASS
@ SUMO_ATTR_HEIGHT
@ SUMO_ATTR_WIDTH
#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
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.
#define M_PI
Definition odrSpiral.cpp:45
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.