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.dev/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
20 // A device which collects vehicular emissions
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <microsim/MSNet.h>
25 #include <microsim/MSLane.h>
26 #include <microsim/MSStop.h>
33 #include "MSDevice_Emissions.h"
34 
35 // ===========================================================================
36 // static members
37 // ===========================================================================
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 // ---------------------------------------------------------------------------
45 // static initialisation methods
46 // ---------------------------------------------------------------------------
47 void
49  insertDefaultAssignmentOptions("emissions", "Emissions", oc);
50 
51  oc.doRegister("device.emissions.begin", new Option_String("-1"));
52  oc.addDescription("device.emissions.begin", "Emissions", TL("Recording begin time for emission-data"));
53 
54  oc.doRegister("device.emissions.period", new Option_String("0"));
55  oc.addDescription("device.emissions.period", "Emissions", TL("Recording period for emission-output"));
56 }
57 
58 
59 void
60 MSDevice_Emissions::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
62  if (equippedByDefaultAssignmentOptions(oc, "emissions", v, oc.isSet("emission-output"))) {
63  into.push_back(new MSDevice_Emissions(v));
64  initOnce();
65  }
66 }
67 
68 
71  SumoXMLAttrMask mask;
72  // set all bits to 1
73  mask.set();
74  return mask;
75 }
76 
77 void
80  myAmInitialized = false;
81 }
82 
83 void
85  if (myAmInitialized) {
86  return;
87  }
88  myAmInitialized = true;
90  if (oc.isSet("emission-output.attributes")) {
91  myWrittenAttributes.reset();
92  for (std::string attrName : oc.getStringVector("emission-output.attributes")) {
93  if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
94  if (attrName == "all") {
95  myWrittenAttributes.set();
96  } else {
97  WRITE_ERRORF(TL("Unknown attribute '%' to write in emission output."), attrName);
98  }
99  continue;
100  }
101  int attr = SUMOXMLDefinitions::Attrs.get(attrName);
102  myWrittenAttributes.set(attr);
103  }
104  }
105  //std::cout << "mask=" << myWrittenAttributes << "\n";
106 }
107 
108 
109 
110 // ---------------------------------------------------------------------------
111 // MSDevice_Emissions-methods
112 // ---------------------------------------------------------------------------
114  : MSVehicleDevice(holder, "emissions_" + holder.getID()), myEmissions() {
115 }
116 
117 
119 }
120 
121 
122 bool
123 MSDevice_Emissions::notifyMove(SUMOTrafficObject& veh, double /*oldPos*/, double /*newPos*/, double newSpeed) {
126  return true;
127 }
128 
129 
130 bool
134  return true;
135 }
136 
137 
138 void
140  const double /* frontOnLane */,
141  const double timeOnLane,
142  const double /* meanSpeedFrontOnLane */,
143  const double meanSpeedVehicleOnLane,
144  const double /* travelledDistanceFrontOnLane */,
145  const double /* travelledDistanceVehicleOnLane */,
146  const double /* meanLengthOnLane */) {
147 
148  // called by meso (see MSMeanData_Emissions::MSLaneMeanDataValues::notifyMoveInternal)
150  myEmissions.addScaled(PollutantsInterface::computeAll(c, meanSpeedVehicleOnLane, veh.getAcceleration(), veh.getSlope(), myHolder.getEmissionParameters()), timeOnLane);
151 }
152 
153 
154 
155 void
157  if (tripinfoOut != nullptr) {
158  const OptionsCont& oc = OptionsCont::getOptions();
159  const int precision = MAX2(
160  oc.isDefault("emission-output.precision") ? 6 : oc.getInt("emission-output.precision"),
161  gPrecision);
162  tripinfoOut->openTag("emissions");
163  tripinfoOut->writeAttr("CO_abs", OutputDevice::realString(myEmissions.CO, precision));
164  tripinfoOut->writeAttr("CO2_abs", OutputDevice::realString(myEmissions.CO2, precision));
165  tripinfoOut->writeAttr("HC_abs", OutputDevice::realString(myEmissions.HC, precision));
166  tripinfoOut->writeAttr("PMx_abs", OutputDevice::realString(myEmissions.PMx, precision));
167  tripinfoOut->writeAttr("NOx_abs", OutputDevice::realString(myEmissions.NOx, precision));
168  tripinfoOut->writeAttr("fuel_abs", OutputDevice::realString(myEmissions.fuel, precision));
169  tripinfoOut->writeAttr("electricity_abs", OutputDevice::realString(myEmissions.electricity, precision));
170  tripinfoOut->closeTag();
171  }
172 }
173 
174 
175 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define TS
Definition: SUMOTime.h:42
int SUMOEmissionClass
std::bitset< 96 > SumoXMLAttrMask
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:26
T MAX2(T a, T b)
Definition: StdDefs.h:82
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.
static SumoXMLAttrMask myWrittenAttributes
bit mask for checking attributes to be written
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Computes current emission values and adds them to their sums.
static void cleanup()
resets the attribute mask
MSDevice_Emissions(SUMOVehicle &holder)
Constructor.
static SumoXMLAttrMask getDefaultMask()
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.
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 initOnce()
initialize attribute mask (once)
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:155
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:203
Abstract in-vehicle device.
SUMOVehicle & myHolder
The vehicle that stores the device.
SUMOEmissionClass getEmissionClass() const
Get this vehicle type's emission class.
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.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
Definition: OptionsCont.cpp:76
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
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:254
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)
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 EnergyParams * getEmissionParameters() const =0
Returns the vehicle's emission model parameter.
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.
int get(const std::string &str) const
void addScaled(const Emissions &a, const double scale=1.)
Add the values of the other struct to this one, scaling the values if needed.