Eclipse SUMO - Simulation of Urban MObility
PollutantsInterface.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-2022 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>
27 #include <utils/common/ToString.h>
28 
29 #include "HelpersHBEFA.h"
30 #include "HelpersHBEFA3.h"
31 #include "HelpersPHEMlight.h"
32 #include "HelpersEnergy.h"
33 #include "HelpersMMPEVEM.h"
34 #include "PollutantsInterface.h"
35 
36 
37 // ===========================================================================
38 // static definitions
39 // ===========================================================================
40 
52 };
53 std::vector<std::string> PollutantsInterface::myAllClassesStr;
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 
59 // ---------------------------------------------------------------------------
60 // PollutantsInterface::Emissions - methods
61 // ---------------------------------------------------------------------------
62 
63 PollutantsInterface::Emissions::Emissions(double co2, double co, double hc, double f, double nox, double pmx, double elec) :
64  CO2(co2),
65  CO(co),
66  HC(hc),
67  fuel(f),
68  NOx(nox),
69  PMx(pmx),
70  electricity(elec) {
71 }
72 
73 
74 void PollutantsInterface::Emissions::addScaled(const Emissions& a, const double scale) {
75  CO2 += scale * a.CO2;
76  CO += scale * a.CO;
77  HC += scale * a.HC;
78  fuel += scale * a.fuel;
79  NOx += scale * a.NOx;
80  PMx += scale * a.PMx;
81  electricity += scale * a.electricity;
82 }
83 
84 // ---------------------------------------------------------------------------
85 // PollutantsInterface::Helper - methods
86 // ---------------------------------------------------------------------------
87 
88 PollutantsInterface::Helper::Helper(std::string name, const int baseIndex, const int defaultClass) :
89  myName(name),
90  myBaseIndex(baseIndex) {
91  if (defaultClass != -1) {
92  myEmissionClassStrings.insert("default", defaultClass);
93  myEmissionClassStrings.addAlias("unknown", defaultClass);
94  }
95 }
96 
97 
98 const
100  return myName;
101 }
102 
103 
105 PollutantsInterface::Helper::getClassByName(const std::string& eClass, const SUMOVehicleClass vc) {
106  UNUSED_PARAMETER(vc);
107  if (myEmissionClassStrings.hasString(eClass)) {
108  return myEmissionClassStrings.get(eClass);
109  }
110  return myEmissionClassStrings.get(StringUtils::to_lower_case(eClass));
111 }
112 
113 
114 const std::string
116  return myName + "/" + myEmissionClassStrings.getString(c);
117 }
118 
119 
120 bool
122  return (c & 0xffffffff & ~HEAVY_BIT) == 0;
123 }
124 
125 
127 PollutantsInterface::Helper::getClass(const SUMOEmissionClass base, const std::string& vClass, const std::string& fuel, const std::string& eClass, const double weight) const {
128  UNUSED_PARAMETER(vClass);
129  UNUSED_PARAMETER(fuel);
130  UNUSED_PARAMETER(eClass);
131  UNUSED_PARAMETER(weight);
132  return base;
133 }
134 
135 
136 std::string
138  UNUSED_PARAMETER(c);
139  return "Passenger";
140 }
141 
142 
143 std::string
145  UNUSED_PARAMETER(c);
146  return "Gasoline";
147 }
148 
149 
150 int
152  UNUSED_PARAMETER(c);
153  return 0;
154 }
155 
156 
157 double
159  UNUSED_PARAMETER(c);
160  return -1.;
161 }
162 
163 
164 double
165 PollutantsInterface::Helper::compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams* param) const {
166  UNUSED_PARAMETER(c);
167  UNUSED_PARAMETER(e);
168  UNUSED_PARAMETER(v);
169  UNUSED_PARAMETER(a);
170  UNUSED_PARAMETER(slope);
171  UNUSED_PARAMETER(param);
172  return 0.;
173 }
174 
175 
176 double
177 PollutantsInterface::Helper::getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope) const {
178  UNUSED_PARAMETER(c);
179  UNUSED_PARAMETER(v);
180  UNUSED_PARAMETER(slope);
181  return a;
182 }
183 
184 
185 void
186 PollutantsInterface::Helper::addAllClassesInto(std::vector<SUMOEmissionClass>& list) const {
187  myEmissionClassStrings.addKeysInto(list);
188 }
189 
190 
191 bool
193  return (c >> 16) == (myBaseIndex >> 16);
194 }
195 
196 // ---------------------------------------------------------------------------
197 // PollutantsInterface - methods
198 // ---------------------------------------------------------------------------
199 
201 PollutantsInterface::getClassByName(const std::string& eClass, const SUMOVehicleClass vc) {
202  const std::string::size_type sep = eClass.find("/");
203  const std::string model = eClass.substr(0, sep); // this includes the case of no separator
204  for (int i = 0; i < 6; i++) {
205  if (myHelpers[i]->getName() == model) {
206  if (sep != std::string::npos) {
207  const std::string subClass = eClass.substr(sep + 1);
208  if (subClass == "zero") {
209  return myZeroHelper.getClassByName("default", vc);
210  }
211  return myHelpers[i]->getClassByName(subClass, vc);
212  }
213  return myHelpers[i]->getClassByName("default", vc);
214  }
215  }
216  if (sep == std::string::npos) {
217  if (eClass == "zero") {
218  return myZeroHelper.getClassByName("default", vc);
219  }
220  // default HBEFA2
221  return myHBEFA2Helper.getClassByName(eClass, vc);
222  }
223  throw InvalidArgument("Unknown emission class '" + eClass + "'.");
224 }
225 
226 
227 const std::vector<SUMOEmissionClass>
229  std::vector<SUMOEmissionClass> result;
230  for (int i = 0; i < 6; i++) {
231  myHelpers[i]->addAllClassesInto(result);
232  }
233  return result;
234 }
235 
236 
237 const std::vector<std::string>&
239  // first check if myAllClassesStr has to be filled
240  if (myAllClassesStr.empty()) {
241  // first obtain all emissionClasses
242  std::vector<SUMOEmissionClass> emissionClasses;
243  for (int i = 0; i < 6; i++) {
244  myHelpers[i]->addAllClassesInto(emissionClasses);
245  }
246  // now write all emissionClasses in myAllClassesStr
247  for (const auto& i : emissionClasses) {
248  myAllClassesStr.push_back(getName(i));
249  }
250  }
251  return myAllClassesStr;
252 }
253 
254 std::string
256  return myHelpers[c >> 16]->getClassName(c);
257 }
258 
259 
260 std::string
262  switch (e) {
263  case CO2:
264  return "CO2";
265  case CO:
266  return "CO";
267  case HC:
268  return "HC";
269  case FUEL:
270  return "fuel";
271  case NO_X:
272  return "NOx";
273  case PM_X:
274  return "PMx";
275  case ELEC:
276  return "electricity";
277  default:
278  throw InvalidArgument("Unknown emission type '" + toString(e) + "'");
279  }
280 }
281 
282 bool
284  return (c & HEAVY_BIT) != 0;
285 }
286 
287 
288 bool
290  return myHelpers[c >> 16]->isSilent(c);
291 }
292 
293 
295 PollutantsInterface::getClass(const SUMOEmissionClass base, const std::string& vClass,
296  const std::string& fuel, const std::string& eClass, const double weight) {
297  return myHelpers[base >> 16]->getClass(base, vClass, fuel, eClass, weight);
298 }
299 
300 
301 std::string
303  return myHelpers[c >> 16]->getAmitranVehicleClass(c);
304 }
305 
306 
307 std::string
309  return myHelpers[c >> 16]->getFuel(c);
310 }
311 
312 
313 int
315  return myHelpers[c >> 16]->getEuroClass(c);
316 }
317 
318 
319 double
321  return myHelpers[c >> 16]->getWeight(c);
322 }
323 
324 
325 double
326 PollutantsInterface::compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams* param) {
327  return myHelpers[c >> 16]->compute(c, e, v, a, slope, param);
328 }
329 
330 
332 PollutantsInterface::computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams* param) {
333  const Helper* const h = myHelpers[c >> 16];
334  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),
335  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),
336  h->compute(c, ELEC, v, a, slope, param));
337 }
338 
339 
340 double
341 PollutantsInterface::computeDefault(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const double tt, const EnergyParams* param) {
342  const Helper* const h = myHelpers[c >> 16];
343  return (h->compute(c, e, v, 0, slope, param) + h->compute(c, e, v - a, a, slope, param)) * tt / 2.;
344 }
345 
346 
347 double
348 PollutantsInterface::getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope) {
349  return myHelpers[c >> 16]->getModifiedAccel(c, v, a, slope);
350 }
351 
352 
353 const HelpersEnergy&
355  return myEnergyHelper;
356 }
357 
358 /****************************************************************************/
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
int SUMOEmissionClass
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
An upper class for objects with additional parameters.
Definition: EnergyParams.h:41
Helper methods for energy-based electricity consumption computation based on the battery device.
Definition: HelpersEnergy.h:41
Helper methods for HBEFA3-based emission computation.
Definition: HelpersHBEFA3.h:44
Helper methods for HBEFA-based emission computation.
Definition: HelpersHBEFA.h:44
This helper class allows the PollutantsInterface to load and use different MMPEVEMs.
Helper methods for PHEMlight-based emission computation.
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 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....
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...
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 HelpersPHEMlight myPHEMlightHelper
Instance of PHEMlightHelper which gets cleaned up automatically.
static double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope)
Returns the adapted acceleration value, useful for comparing with external PHEMlight references.
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 double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const EnergyParams *param=0)
Returns the amount of the emitted pollutant given the vehicle type and state (in mg/s or ml/s for fue...
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=0)
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 double getWeight(const SUMOEmissionClass c)
Returns a representative weight for the given emission class see http://colombo-fp7....
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 Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param=0)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
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 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 const std::vector< std::string > & getAllClassesStr()
Get all SUMOEmissionClass in string format.
static Helper * myHelpers[]
the known model helpers
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(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:59
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.