Eclipse SUMO - Simulation of Urban MObility
MSMeanData_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-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 // Emission data collector for edges/lanes that
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <microsim/MSNet.h>
24 #include <microsim/MSLane.h>
25 #include <microsim/MSVehicle.h>
27 #include <utils/common/SUMOTime.h>
28 #include <utils/common/ToString.h>
30 #include "MSMeanData_Emissions.h"
32 #include <limits>
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 // ---------------------------------------------------------------------------
39 // MSMeanData_Emissions::MSLaneMeanDataValues - methods
40 // ---------------------------------------------------------------------------
42  const double length, const bool doAdd,
43  const MSMeanData_Emissions* parent)
44  : MSMeanData::MeanDataValues(lane, length, doAdd, parent),
45  myEmissions() {}
46 
47 
49 }
50 
51 
52 void
54  sampleSeconds = 0.;
55  travelledDistance = 0.;
56  myEmissions = PollutantsInterface::Emissions();
57 }
58 
59 
60 void
63  v.sampleSeconds += sampleSeconds;
64  v.travelledDistance += travelledDistance;
65  v.myEmissions.addScaled(myEmissions);
66 }
67 
68 
69 void
70 MSMeanData_Emissions::MSLaneMeanDataValues::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 */) {
71  if (myParent != nullptr && !myParent->vehicleApplies(veh)) {
72  return;
73  }
74  if (veh.isVehicle()) {
75  sampleSeconds += timeOnLane;
76  travelledDistance += travelledDistanceVehicleOnLane;
77  const double a = veh.getAcceleration();
79  // XXX: recheck, which value to use here for the speed. (Leo) Refs. #2579
80  meanSpeedVehicleOnLane, a, veh.getSlope(),
81  static_cast<const SUMOVehicle&>(veh).getEmissionParameters()), timeOnLane);
82  }
83 }
84 
85 bool
87  if (veh.isVehicle()) {
89  0., 0., 0.,
90  static_cast<const SUMOVehicle&>(veh).getEmissionParameters()), TS);
91  }
92  return true;
93 }
94 
95 
96 void
97 MSMeanData_Emissions::MSLaneMeanDataValues::write(OutputDevice& dev, long long int attributeMask, const SUMOTime period,
98  const double /*numLanes*/, const double defaultTravelTime, const int /*numVehicles*/) const {
99  const double normFactor = double(3600. / STEPS2TIME(period) / myLaneLength);
100  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO_ABS, OutputDevice::realString(myEmissions.CO, 6));
101  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO2_ABS, OutputDevice::realString(myEmissions.CO2, 6));
102  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_HC_ABS, OutputDevice::realString(myEmissions.HC, 6));
103  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_PMX_ABS, OutputDevice::realString(myEmissions.PMx, 6));
104  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_NOX_ABS, OutputDevice::realString(myEmissions.NOx, 6));
105  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_FUEL_ABS, OutputDevice::realString(myEmissions.fuel, 6));
106  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_ELECTRICITY_ABS, OutputDevice::realString(myEmissions.electricity, 6));
107  if (attributeMask == 0) {
108  dev << "\n ";
109  }
110  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO_NORMED, OutputDevice::realString(normFactor * myEmissions.CO, 6));
111  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO2_NORMED, OutputDevice::realString(normFactor * myEmissions.CO2, 6));
112  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_HC_NORMED, OutputDevice::realString(normFactor * myEmissions.HC, 6));
113  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_PMX_NORMED, OutputDevice::realString(normFactor * myEmissions.PMx, 6));
114  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_NOX_NORMED, OutputDevice::realString(normFactor * myEmissions.NOx, 6));
115  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_FUEL_NORMED, OutputDevice::realString(normFactor * myEmissions.fuel, 6));
116  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_ELECTRICITY_NORMED, OutputDevice::realString(normFactor * myEmissions.electricity, 6));
117 
118  if (sampleSeconds > myParent->getMinSamples()) {
119  double vehFactor = myParent->getMaxTravelTime() / sampleSeconds;
120  double traveltime = myParent->getMaxTravelTime();
121  if (travelledDistance > 0.f) {
122  vehFactor = MIN2(vehFactor, myLaneLength / travelledDistance);
123  traveltime = MIN2(traveltime, myLaneLength * sampleSeconds / travelledDistance);
124  }
125  if (attributeMask == 0) {
126  dev << "\n ";
127  }
128  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_TRAVELTIME, OutputDevice::realString(traveltime));
129  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO_PERVEH, OutputDevice::realString(vehFactor * myEmissions.CO, 6));
130  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO2_PERVEH, OutputDevice::realString(vehFactor * myEmissions.CO2, 6));
131  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_HC_PERVEH, OutputDevice::realString(vehFactor * myEmissions.HC, 6));
132  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_PMX_PERVEH, OutputDevice::realString(vehFactor * myEmissions.PMx, 6));
133  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_NOX_PERVEH, OutputDevice::realString(vehFactor * myEmissions.NOx, 6));
134  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_FUEL_PERVEH, OutputDevice::realString(vehFactor * myEmissions.fuel, 6));
135  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_ELECTRICITY_PERVEH, OutputDevice::realString(vehFactor * myEmissions.electricity, 6));
136  } else if (defaultTravelTime >= 0.) {
138  const double speed = MIN2(myLaneLength / defaultTravelTime, t->getMaxSpeed());
139 
140  if (attributeMask == 0) {
141  dev << "\n ";
142  }
143  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_TRAVELTIME, OutputDevice::realString(defaultTravelTime));
144  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::CO, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
145  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_CO2_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::CO2, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
146  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_HC_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::HC, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
147  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_PMX_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::PM_X, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
148  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_NOX_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::NO_X, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
149  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_FUEL_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::FUEL, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
150  checkWriteAttribute(dev, attributeMask, SUMO_ATTR_ELECTRICITY_PERVEH, OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::ELEC, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6));
151  }
152  dev.closeTag();
153 }
154 
155 
156 
157 // ---------------------------------------------------------------------------
158 // MSMeanData_Emissions - methods
159 // ---------------------------------------------------------------------------
161  const SUMOTime dumpBegin,
162  const SUMOTime dumpEnd,
163  const bool useLanes, const bool withEmpty,
164  const bool printDefaults,
165  const bool withInternal,
166  const bool trackVehicles,
167  const double maxTravelTime,
168  const double minSamples,
169  const std::string& vTypes,
170  const std::string& writeAttributes) :
171  MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
172  withInternal, trackVehicles, 0, maxTravelTime, minSamples, vTypes, writeAttributes)
173 { }
174 
175 
177 
178 
180 MSMeanData_Emissions::createValues(MSLane* const lane, const double length, const bool doAdd) const {
181  return new MSLaneMeanDataValues(lane, length, doAdd, this);
182 }
183 
184 
185 /****************************************************************************/
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
#define TS
Definition: SUMOTime.h:40
long long int SUMOTime
Definition: SUMOTime.h:31
@ SUMO_ATTR_NOX_ABS
@ SUMO_ATTR_FUEL_PERVEH
@ SUMO_ATTR_HC_NORMED
@ SUMO_ATTR_FUEL_NORMED
@ SUMO_ATTR_CO_PERVEH
@ SUMO_ATTR_ELECTRICITY_ABS
@ SUMO_ATTR_PMX_PERVEH
@ SUMO_ATTR_HC_PERVEH
@ SUMO_ATTR_CO2_NORMED
@ SUMO_ATTR_NOX_NORMED
@ SUMO_ATTR_TRAVELTIME
@ SUMO_ATTR_PMX_ABS
@ SUMO_ATTR_ELECTRICITY_PERVEH
@ SUMO_ATTR_CO_ABS
MSMeanData_Emissions.
@ SUMO_ATTR_FUEL_ABS
@ SUMO_ATTR_CO_NORMED
@ SUMO_ATTR_NOX_PERVEH
@ SUMO_ATTR_HC_ABS
@ SUMO_ATTR_CO2_PERVEH
@ SUMO_ATTR_PMX_NORMED
@ SUMO_ATTR_ELECTRICITY_NORMED
@ SUMO_ATTR_CO2_ABS
T MIN2(T a, T b)
Definition: StdDefs.h:73
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
Definition: MSCFModel.h:208
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:66
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:181
Data structure for mean (aggregated) edge/lane values.
void write(OutputDevice &dev, long long int attributeMask, const SUMOTime period, const double numLanes, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Emissions *parent)
Constructor.
bool notifyIdle(SUMOTrafficObject &veh)
Computes idling emission values and adds them to the aggregate emission sums.
PollutantsInterface::Emissions myEmissions
Collected values.
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
void notifyMoveInternal(const SUMOTrafficObject &veh, const double, const double timeOnLane, const double, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double)
Internal notification about the vehicle moves.
Emission data collector for edges/lanes.
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
MSMeanData_Emissions(const std::string &id, const SUMOTime dumpBegin, const SUMOTime dumpEnd, const bool useLanes, const bool withEmpty, const bool printDefaults, const bool withInternal, const bool trackVehicles, const double minSamples, const double maxTravelTime, const std::string &vTypes, const std::string &writeAttributes)
Constructor.
virtual ~MSMeanData_Emissions()
Destructor.
Data collector for edges/lanes.
Definition: MSMeanData.h:57
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:371
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=nullptr)
Returns the named vehicle type or a sample from the named distribution.
The car-following model and parameter.
Definition: MSVehicleType.h:62
double getMaxSpeed() const
Get vehicle's maximum speed [m/s].
SUMOEmissionClass getEmissionClass() const
Get this vehicle type's emission class.
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
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 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...
Representation of a vehicle, person, or container.
virtual bool isVehicle() const
Whether it is a vehicle.
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:58
virtual const std::map< int, double > * getEmissionParameters() const =0
Returns the vehicle's emission model parameter.
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.