Eclipse SUMO - Simulation of Urban MObility
MSDevice_Emissions.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
20 // A device which collects vehicular emissions
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <microsim/MSNet.h>
25 #include <microsim/MSLane.h>
30 #include "MSDevice_Emissions.h"
31 
32 
33 // ===========================================================================
34 // method definitions
35 // ===========================================================================
36 // ---------------------------------------------------------------------------
37 // static initialisation methods
38 // ---------------------------------------------------------------------------
39 void
41  insertDefaultAssignmentOptions("emissions", "Emissions", oc);
42 
43  oc.doRegister("device.emissions.period", new Option_String("0"));
44  oc.addDescription("device.emissions.period", "Emissions", "Recording period for emission-output");
45 }
46 
47 
48 void
49 MSDevice_Emissions::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
51  if (equippedByDefaultAssignmentOptions(oc, "emissions", v, oc.isSet("emission-output"))) {
52  // build the device
53  MSDevice_Emissions* device = new MSDevice_Emissions(v, "emissions_" + v.getID());
54  into.push_back(device);
55  }
56 }
57 
58 
59 // ---------------------------------------------------------------------------
60 // MSDevice_Emissions-methods
61 // ---------------------------------------------------------------------------
62 MSDevice_Emissions::MSDevice_Emissions(SUMOVehicle& holder, const std::string& id)
63  : MSVehicleDevice(holder, id), myEmissions() {
64 }
65 
66 
68 }
69 
70 
71 bool
72 MSDevice_Emissions::notifyMove(SUMOTrafficObject& veh, double /*oldPos*/, double /*newPos*/, double newSpeed) {
74  const double a = veh.getAcceleration();
75  const double slope = veh.getSlope();
77  static_cast<const SUMOVehicle&>(veh).getEmissionParameters()), TS);
78  return true;
79 }
80 
81 bool
85  static_cast<const SUMOVehicle&>(veh).getEmissionParameters()), TS);
86  return true;
87 }
88 
89 void
91  const double /* frontOnLane */,
92  const double timeOnLane,
93  const double /* meanSpeedFrontOnLane */,
94  const double meanSpeedVehicleOnLane,
95  const double /* travelledDistanceFrontOnLane */,
96  const double /* travelledDistanceVehicleOnLane */,
97  const double /* meanLengthOnLane */) {
98 
99  // called by meso (see MSMeanData_Emissions::MSLaneMeanDataValues::notifyMoveInternal)
100  const double a = veh.getAcceleration();
102  meanSpeedVehicleOnLane, a, veh.getSlope(),
103  static_cast<const SUMOVehicle&>(veh).getEmissionParameters()), timeOnLane);
104 }
105 
106 
107 
108 void
110  if (tripinfoOut != nullptr) {
111  const int precision = MAX2(6, gPrecision);
112  tripinfoOut->openTag("emissions");
113  tripinfoOut->writeAttr("CO_abs", OutputDevice::realString(myEmissions.CO, precision));
114  tripinfoOut->writeAttr("CO2_abs", OutputDevice::realString(myEmissions.CO2, precision));
115  tripinfoOut->writeAttr("HC_abs", OutputDevice::realString(myEmissions.HC, precision));
116  tripinfoOut->writeAttr("PMx_abs", OutputDevice::realString(myEmissions.PMx, precision));
117  tripinfoOut->writeAttr("NOx_abs", OutputDevice::realString(myEmissions.NOx, precision));
118  tripinfoOut->writeAttr("fuel_abs", OutputDevice::realString(myEmissions.fuel, precision));
119  tripinfoOut->writeAttr("electricity_abs", OutputDevice::realString(myEmissions.electricity, precision));
120  tripinfoOut->closeTag();
121  }
122 }
123 
124 
125 /****************************************************************************/
#define TS
Definition: SUMOTime.h:40
int SUMOEmissionClass
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
T MAX2(T a, T b)
Definition: StdDefs.h:80
A device which collects vehicular emissions.
void generateOutput(OutputDevice *tripinfoOut) const
Called on writing tripinfo output.
bool notifyIdle(SUMOTrafficObject &veh)
Computes idling emission values and adds them to the emission sums.
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Computes current emission values and adds them to their sums.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Emissions-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
MSDevice_Emissions(SUMOVehicle &holder, const std::string &id)
Constructor.
void notifyMoveInternal(const SUMOTrafficObject &veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane)
Internal notification about the vehicle moves, see MSMoveReminder::notifyMoveInternal()
PollutantsInterface::Emissions myEmissions
Internal storages for pollutant/fuel sum in mg or ml.
~MSDevice_Emissions()
Destructor.
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc, const bool isPerson=false)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:137
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.h:205
Abstract in-vehicle device.
SUMOEmissionClass getEmissionClass() const
Get this vehicle type's emission class.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
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...
Representation of a vehicle, person, or container.
virtual double getAcceleration() const =0
Returns the object's acceleration.
virtual double getSlope() const =0
Returns the slope of the road at object's position in degrees.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual const EnergyParams * getEmissionParameters() const =0
Returns the vehicle's emission model parameter.
void addScaled(const Emissions &a, const double scale=1.)
Add the values of the other struct to this one, scaling the values if needed.