Eclipse SUMO - Simulation of Urban MObility
PollutantsInterface.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-2020 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 #pragma once
22 #include <config.h>
23 
24 #include <cctype> // defines std::tolower
25 #include <vector>
26 #include <limits>
27 #include <cmath>
28 #include <algorithm>
29 #include <utils/common/StdDefs.h>
31 #include "PHEMCEP.h"
32 
33 
34 // ===========================================================================
35 // class declarations
36 // ===========================================================================
37 class HelpersHBEFA;
38 class HelpersHBEFA3;
39 class HelpersPHEMlight;
40 class HelpersEnergy;
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
51 
52 public:
54  enum EmissionType { CO2, CO, HC, FUEL, NO_X, PM_X, ELEC };
55 
60  struct Emissions {
61  double CO2;
62  double CO;
63  double HC;
64  double fuel;
65  double NOx;
66  double PMx;
67  double electricity;
68 
78  Emissions(double co2 = 0, double co = 0, double hc = 0, double f = 0, double nox = 0, double pmx = 0, double elec = 0)
79  : CO2(co2), CO(co), HC(hc), fuel(f), NOx(nox), PMx(pmx), electricity(elec) {
80  }
81 
86  void addScaled(const Emissions& a, const double scale = 1.) {
87  CO2 += scale * a.CO2;
88  CO += scale * a.CO;
89  HC += scale * a.HC;
90  fuel += scale * a.fuel;
91  NOx += scale * a.NOx;
92  PMx += scale * a.PMx;
93  electricity += scale * a.electricity;
94  }
95  };
96 
101  class Helper {
102  public:
106  Helper(std::string name, const int defaultClass = -1) : myName(name) {
107  if (defaultClass != -1) {
108  myEmissionClassStrings.insert("default", defaultClass);
109  myEmissionClassStrings.addAlias("unknown", defaultClass);
110  }
111  }
112 
116  const std::string& getName() const {
117  return myName;
118  }
119 
129  virtual SUMOEmissionClass getClassByName(const std::string& eClass, const SUMOVehicleClass vc) {
130  UNUSED_PARAMETER(vc);
131  if (myEmissionClassStrings.hasString(eClass)) {
132  return myEmissionClassStrings.get(eClass);
133  }
134  std::string eclower = eClass;
135  /*
136  For some compilers, std::tolower cannot be resolved correctly, resulting in error messages
137  like "No matching function found ... unresolved overloaded function type.", see e.g.
138  https://stackoverflow.com/questions/5539249. The problem may be fixed by specifying ::tolower,
139  the global namespace version of the function that has no overloads.
140 
141  Similarly, https://en.cppreference.com/w/cpp/string/byte/tolower suggests that one should not
142  use any of the functions defined in <cctype> with standard algorithms (like `transform`) when
143  the iterator type is `char` or `signed char` -- we shall convert the value to `unsigned char`
144  first:
145 
146  std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
147 
148  This, however, still generates an ugly warning in VS2017. Go figure ...
149  */
150  std::transform(eclower.begin(), eclower.end(), eclower.begin(), [](unsigned char c) {
151  return std::tolower(c);
152  });
153  return myEmissionClassStrings.get(eclower);
154  }
155 
160  const std::string getClassName(const SUMOEmissionClass c) const {
161  return myName + "/" + myEmissionClassStrings.getString(c);
162  }
163 
169  virtual bool isSilent(const SUMOEmissionClass c) {
170  return (c & 0xffffffff & ~HEAVY_BIT) == 0;
171  }
172 
175 
186  virtual SUMOEmissionClass getClass(const SUMOEmissionClass base, const std::string& vClass,
187  const std::string& fuel, const std::string& eClass, const double weight) const {
188  UNUSED_PARAMETER(vClass);
189  UNUSED_PARAMETER(fuel);
190  UNUSED_PARAMETER(eClass);
191  UNUSED_PARAMETER(weight);
192  return base;
193  }
194 
200  virtual std::string getAmitranVehicleClass(const SUMOEmissionClass c) const {
201  UNUSED_PARAMETER(c);
202  return "Passenger";
203  }
204 
210  virtual std::string getFuel(const SUMOEmissionClass c) const {
211  UNUSED_PARAMETER(c);
212  return "Gasoline";
213  }
214 
220  virtual int getEuroClass(const SUMOEmissionClass c) const {
221  UNUSED_PARAMETER(c);
222  return 0;
223  }
224 
231  virtual double getWeight(const SUMOEmissionClass c) const {
232  UNUSED_PARAMETER(c);
233  return -1.;
234  }
236 
245  virtual double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const std::map<int, double>* param) const {
246  UNUSED_PARAMETER(c);
247  UNUSED_PARAMETER(e);
248  UNUSED_PARAMETER(v);
249  UNUSED_PARAMETER(a);
250  UNUSED_PARAMETER(slope);
251  UNUSED_PARAMETER(param);
252  return 0.;
253  }
254 
263  virtual double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope) const {
264  UNUSED_PARAMETER(c);
265  UNUSED_PARAMETER(v);
266  UNUSED_PARAMETER(slope);
267  return a;
268  }
269 
273  void addAllClassesInto(std::vector<SUMOEmissionClass>& list) const {
275  }
276 
277  protected:
279  const std::string myName;
280 
283 
284  private:
286  Helper& operator=(const Helper&) = delete;
287  };
288 
290  static const int ZERO_EMISSIONS = 0;
291 
293  static const int HEAVY_BIT = 1 << 15;
294 
299  static SUMOEmissionClass getClassByName(const std::string& eClass, const SUMOVehicleClass vc = SVC_IGNORING);
300 
305  static const std::vector<SUMOEmissionClass> getAllClasses();
306 
308  static const std::vector<std::string>& getAllClassesStr();
309 
314  static std::string getName(const SUMOEmissionClass c);
315 
317  static std::string getPollutantName(const EmissionType e);
318 
323  static bool isHeavy(const SUMOEmissionClass c);
324 
329  static bool isSilent(const SUMOEmissionClass c);
330 
339  static SUMOEmissionClass getClass(const SUMOEmissionClass base, const std::string& vClass, const std::string& fuel, const std::string& eClass, const double weight);
340 
345  static std::string getAmitranVehicleClass(const SUMOEmissionClass c);
346 
351  static std::string getFuel(const SUMOEmissionClass c);
352 
357  static int getEuroClass(const SUMOEmissionClass c);
358 
364  static double getWeight(const SUMOEmissionClass c);
365 
374  static double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const std::map<int, double>* param = 0);
375 
383  static Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const std::map<int, double>* param = 0);
384 
394  static double computeDefault(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const double tt, const std::map<int, double>* param = 0);
395 
403  static double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope);
404 
406  static const HelpersEnergy& getEnergyHelper() {
407  return myEnergyHelper;
408  }
409 
410 private:
413 
416 
419 
422 
425 
427  static Helper* myHelpers[];
428 
430  static std::vector<std::string> myAllClassesStr;
431 };
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_IGNORING
vehicles ignoring classes
int SUMOEmissionClass
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
Helper methods for energy-based electricity consumption computation based on the battery device.
Definition: HelpersEnergy.h:40
Helper methods for HBEFA3-based emission computation.
Definition: HelpersHBEFA3.h:44
Helper methods for HBEFA-based emission computation.
Definition: HelpersHBEFA.h:44
Helper methods for PHEMlight-based emission computation.
zero emission model, used as superclass for the other model helpers
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 bool isSilent(const SUMOEmissionClass c)
Returns whether the class denotes a silent vehicle for interfacing with the noise model....
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....
const std::string getClassName(const SUMOEmissionClass c) const
Returns the complete name of the emission class including the model.
void addAllClassesInto(std::vector< SUMOEmissionClass > &list) const
Add all known emission classes of this model to the given container.
const std::string myName
the name of the model
Helper(std::string name, const int defaultClass=-1)
Constructor, intializes the name.
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...
virtual std::string getFuel(const SUMOEmissionClass c) const
Returns the fuel type described by this emission class as described in the Amitran interface (Gasolin...
virtual int getEuroClass(const SUMOEmissionClass c) const
Returns the Euro emission class described by this emission class as described in the Amitran interfac...
Helper & operator=(const Helper &)=delete
invalidate copy constructor
virtual double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const std::map< int, double > *param) const
Returns the amount of the emitted pollutant given the vehicle type and state (in mg/s or ml/s for fue...
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...
const std::string & getName() const
Returns the name of the model.
virtual double getWeight(const SUMOEmissionClass c) const
Returns a reference weight in kg described by this emission class as described in the Amitran interfa...
StringBijection< SUMOEmissionClass > myEmissionClassStrings
Mapping between emission class names and integer representations.
Helper methods for PHEMlight-based emission computation.
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 double computeDefault(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const double tt, const std::map< int, double > *param=0)
Returns the amount of emitted pollutant given the vehicle type and default values for the state (in m...
static std::string getAmitranVehicleClass(const SUMOEmissionClass c)
Returns the vehicle class described by the given emission class.
static std::string getPollutantName(const EmissionType e)
return the name for the given emission type
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 Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const std::map< int, double > *param=0)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
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 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 HelpersEnergy & getEnergyHelper()
get energy helper
static const std::vector< std::string > & getAllClassesStr()
Get all SUMOEmissionClass in string format.
static Helper * myHelpers[]
the known model helpers
static double compute(const SUMOEmissionClass c, const EmissionType e, const double v, const double a, const double slope, const std::map< int, double > *param=0)
Returns the amount of the emitted pollutant given the vehicle type and state (in mg/s or ml/s for fue...
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
static Helper myZeroHelper
Instance of Helper which gets cleaned up automatically.
bool hasString(const std::string &str) const
const std::string & getString(const T key) const
void addAlias(const std::string str, const T key)
T get(const std::string &str) const
void addKeysInto(std::vector< T > &list) const
void insert(const std::string str, const T key, bool checkDuplicates=true)
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.