SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSDevice_Emissions.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A device which collects vehicular emissions
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include "MSDevice_Emissions.h"
33 #include <microsim/MSNet.h>
34 #include <microsim/MSLane.h>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 // ---------------------------------------------------------------------------
49 // static initialisation methods
50 // ---------------------------------------------------------------------------
51 void
54 
55  oc.doRegister("device.emissions.probability", new Option_Float(0.));
56  oc.addDescription("device.emissions.probability", "Emissions", "The probability for a vehicle to have an emission logging device");
57 
58  oc.doRegister("device.emissions.explicit", new Option_String());
59  oc.addSynonyme("device.emissions.explicit", "device.emissions.knownveh", true);
60  oc.addDescription("device.emissions.explicit", "Emissions", "Assign a device to named vehicles");
61 
62  oc.doRegister("device.emissions.deterministic", new Option_Bool(false));
63  oc.addDescription("device.emissions.deterministic", "Emissions", "The devices are set deterministic using a fraction of 1000");
64 }
65 
66 
67 void
68 MSDevice_Emissions::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
70  if (oc.getFloat("device.emissions.probability") == 0 && !oc.isSet("device.emissions.explicit")) {
71  // no route computation is modelled
72  return;
73  }
74  // route computation is enabled
75  bool haveByNumber = false;
76  if (oc.getBool("device.emissions.deterministic")) {
77  haveByNumber = MSNet::getInstance()->getVehicleControl().isInQuota(oc.getFloat("device.emissions.probability"));
78  } else {
79  haveByNumber = RandHelper::rand() <= oc.getFloat("device.emissions.probability");
80  }
81  bool haveByName = oc.isSet("device.emissions.explicit") && OptionsCont::getOptions().isInStringVector("device.emissions.explicit", v.getID());
82  if (haveByNumber || haveByName) {
83  // build the device
84  MSDevice_Emissions* device = new MSDevice_Emissions(v, "emissions_" + v.getID());
85  into.push_back(device);
86  }
87 }
88 
89 
90 // ---------------------------------------------------------------------------
91 // MSDevice_Emissions-methods
92 // ---------------------------------------------------------------------------
93 MSDevice_Emissions::MSDevice_Emissions(SUMOVehicle& holder, const std::string& id)
94  : MSDevice(holder, id),
95  myCO2(0), myCO(0), myHC(0), myPMx(0), myNOx(0), myFuel(0) {
96 }
97 
98 
100 }
101 
102 
103 bool
104 MSDevice_Emissions::notifyMove(SUMOVehicle& veh, SUMOReal /*oldPos*/, SUMOReal /*newPos*/, SUMOReal newSpeed) {
106  const SUMOReal a = veh.getAcceleration();
107  const SUMOReal slope = veh.getSlope();
108  myCO2 += TS * PollutantsInterface::computeCO2(c, newSpeed, a, slope);
109  myCO += TS * PollutantsInterface::computeCO(c, newSpeed, a, slope);
110  myHC += TS * PollutantsInterface::computeHC(c, newSpeed, a, slope);
111  myPMx += TS * PollutantsInterface::computePMx(c, newSpeed, a, slope);
112  myNOx += TS * PollutantsInterface::computeNOx(c, newSpeed, a, slope);
113  myFuel += TS * PollutantsInterface::computeFuel(c, newSpeed, a, slope);
114  return true;
115 }
116 
117 
118 void
120  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
121  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
122  (os.openTag("emissions") <<
123  " CO_abs=\"" << OutputDevice::realString(myCO, 6) <<
124  "\" CO2_abs=\"" << OutputDevice::realString(myCO2, 6) <<
125  "\" HC_abs=\"" << OutputDevice::realString(myHC, 6) <<
126  "\" PMx_abs=\"" << OutputDevice::realString(myPMx, 6) <<
127  "\" NOx_abs=\"" << OutputDevice::realString(myNOx, 6) <<
128  "\" fuel_abs=\"" << OutputDevice::realString(myFuel, 6) <<
129  "\"").closeTag();
130  }
131 }
132 
133 
134 
135 /****************************************************************************/
136 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:84
bool notifyMove(SUMOVehicle &veh, SUMOReal oldPos, SUMOReal newPos, SUMOReal newSpeed)
Computes current emission values and adds them to their sums.
bool isInStringVector(const std::string &optionName, const std::string &itemName)
Returns the named option is a list of string values containing the specified item.
MSDevice_Emissions(SUMOVehicle &holder, const std::string &id)
Constructor.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:154
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
#define TS
Definition: SUMOTime.h:52
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
static SUMOReal computeFuel(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of consumed fuel given the vehicle type and state (in ml/s)
SUMOEmissionClass
Definition of vehicle emission classes.
static SUMOReal computeCO2(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted CO2 given the vehicle type and state (in mg/s)
~MSDevice_Emissions()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:63
static SUMOReal computeNOx(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted NOx given the vehicle type and state (in mg/s)
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:263
static SUMOReal computeHC(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted HC given the vehicle type and state (in mg/s)
Abstract in-vehicle device.
Definition: MSDevice.h:68
static SUMOReal computeCO(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted CO given the vehicle type and state (in mg/s)
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static void insertOptions()
Inserts MSDevice_Emissions-options.
A storage for options typed value containers)
Definition: OptionsCont.h:108
virtual SUMOReal getAcceleration() const =0
Returns the vehicle's acceleration.
static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY)
Helper method for string formatting.
void generateOutput() const
Called on writing tripinfo output.
bool isInQuota(SUMOReal frac=-1) const
Returns the information whether the currently vehicle number shall be emitted considering that only f...
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
#define SUMOReal
Definition: config.h:215
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
virtual SUMOReal getSlope() const =0
Returns the slope of the road at vehicle's position.
SUMOEmissionClass getEmissionClass() const
Get this vehicle type's emission class.
A device which collects vehicular emissions.
virtual const std::string & getID() const =0
Get the vehicle's ID.
static SUMOReal computePMx(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted PMx given the vehicle type and state (in mg/s)
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.