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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Emission data collector for edges/lanes that
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <microsim/MSNet.h>
25 #include <microsim/MSLane.h>
26 #include <microsim/MSVehicle.h>
28 #include <utils/common/SUMOTime.h>
29 #include <utils/common/ToString.h>
31 #include "MSMeanData_Emissions.h"
33 #include <limits>
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 // ---------------------------------------------------------------------------
40 // MSMeanData_Emissions::MSLaneMeanDataValues - methods
41 // ---------------------------------------------------------------------------
43  const double length, const bool doAdd,
44  const MSMeanData_Emissions* parent)
45  : MSMeanData::MeanDataValues(lane, length, doAdd, parent),
46  myEmissions() {}
47 
48 
50 }
51 
52 
53 void
55  sampleSeconds = 0.;
56  travelledDistance = 0.;
57  myEmissions = PollutantsInterface::Emissions();
58 }
59 
60 
61 void
64  v.sampleSeconds += sampleSeconds;
65  v.travelledDistance += travelledDistance;
66  v.myEmissions.addScaled(myEmissions);
67 }
68 
69 
70 void
71 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 */) {
72  if (veh.isVehicle()) {
73  sampleSeconds += timeOnLane;
74  travelledDistance += travelledDistanceVehicleOnLane;
75  const double a = veh.getAcceleration();
77  // XXX: recheck, which value to use here for the speed. (Leo) Refs. #2579
78  meanSpeedVehicleOnLane, a, veh.getSlope(),
79  static_cast<const SUMOVehicle&>(veh).getEmissionParameters()), timeOnLane);
80  }
81 }
82 
83 
84 void
86  const double /*numLanes*/, const double defaultTravelTime, const int /*numVehicles*/) const {
87  const double normFactor = double(3600. / STEPS2TIME(period) / myLaneLength);
88  dev << " CO_abs=\"" << OutputDevice::realString(myEmissions.CO, 6) <<
89  "\" CO2_abs=\"" << OutputDevice::realString(myEmissions.CO2, 6) <<
90  "\" HC_abs=\"" << OutputDevice::realString(myEmissions.HC, 6) <<
91  "\" PMx_abs=\"" << OutputDevice::realString(myEmissions.PMx, 6) <<
92  "\" NOx_abs=\"" << OutputDevice::realString(myEmissions.NOx, 6) <<
93  "\" fuel_abs=\"" << OutputDevice::realString(myEmissions.fuel, 6) <<
94  "\" electricity_abs=\"" << OutputDevice::realString(myEmissions.electricity, 6) <<
95  "\"\n CO_normed=\"" << OutputDevice::realString(normFactor * myEmissions.CO, 6) <<
96  "\" CO2_normed=\"" << OutputDevice::realString(normFactor * myEmissions.CO2, 6) <<
97  "\" HC_normed=\"" << OutputDevice::realString(normFactor * myEmissions.HC, 6) <<
98  "\" PMx_normed=\"" << OutputDevice::realString(normFactor * myEmissions.PMx, 6) <<
99  "\" NOx_normed=\"" << OutputDevice::realString(normFactor * myEmissions.NOx, 6) <<
100  "\" fuel_normed=\"" << OutputDevice::realString(normFactor * myEmissions.fuel, 6) <<
101  "\" electricity_normed=\"" << OutputDevice::realString(normFactor * myEmissions.electricity, 6);
102  if (sampleSeconds > myParent->getMinSamples()) {
103  double vehFactor = myParent->getMaxTravelTime() / sampleSeconds;
104  double traveltime = myParent->getMaxTravelTime();
105  if (travelledDistance > 0.f) {
106  vehFactor = MIN2(vehFactor, myLaneLength / travelledDistance);
107  traveltime = MIN2(traveltime, myLaneLength * sampleSeconds / travelledDistance);
108  }
109  dev << "\"\n traveltime=\"" << OutputDevice::realString(traveltime) <<
110  "\" CO_perVeh=\"" << OutputDevice::realString(myEmissions.CO * vehFactor, 6) <<
111  "\" CO2_perVeh=\"" << OutputDevice::realString(myEmissions.CO2 * vehFactor, 6) <<
112  "\" HC_perVeh=\"" << OutputDevice::realString(myEmissions.HC * vehFactor, 6) <<
113  "\" PMx_perVeh=\"" << OutputDevice::realString(myEmissions.PMx * vehFactor, 6) <<
114  "\" NOx_perVeh=\"" << OutputDevice::realString(myEmissions.NOx * vehFactor, 6) <<
115  "\" fuel_perVeh=\"" << OutputDevice::realString(myEmissions.fuel * vehFactor, 6) <<
116  "\" electricity_perVeh=\"" << OutputDevice::realString(myEmissions.electricity * vehFactor, 6);
117  } else if (defaultTravelTime >= 0.) {
119  const double speed = MIN2(myLaneLength / defaultTravelTime, t->getMaxSpeed());
120  dev << "\"\n traveltime=\"" << OutputDevice::realString(defaultTravelTime) <<
121  "\" CO_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::CO, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6) << // @todo: give correct slope
122  "\" CO2_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::CO2, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6) << // @todo: give correct slope
123  "\" HC_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::HC, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6) << // @todo: give correct slope
124  "\" PMx_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::PM_X, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6) << // @todo: give correct slope
125  "\" NOx_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::NO_X, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6) << // @todo: give correct slope
126  "\" fuel_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::FUEL, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6) << // @todo: give correct slope
127  "\" electricity_perVeh=\"" << OutputDevice::realString(PollutantsInterface::computeDefault(t->getEmissionClass(), PollutantsInterface::ELEC, speed, t->getCarFollowModel().getMaxAccel(), 0, defaultTravelTime), 6); // @todo: give correct slope
128  }
129  dev << "\"";
130  dev.closeTag();
131 }
132 
133 
134 
135 // ---------------------------------------------------------------------------
136 // MSMeanData_Emissions - methods
137 // ---------------------------------------------------------------------------
139  const SUMOTime dumpBegin,
140  const SUMOTime dumpEnd,
141  const bool useLanes, const bool withEmpty,
142  const bool printDefaults,
143  const bool withInternal,
144  const bool trackVehicles,
145  const double maxTravelTime,
146  const double minSamples,
147  const std::string& vTypes)
148  : MSMeanData(id, dumpBegin, dumpEnd, useLanes, withEmpty, printDefaults,
149  withInternal, trackVehicles, 0, maxTravelTime, minSamples, vTypes) {
150 }
151 
152 
154 
155 
157 MSMeanData_Emissions::createValues(MSLane* const lane, const double length, const bool doAdd) const {
158  return new MSLaneMeanDataValues(lane, length, doAdd, this);
159 }
160 
161 
162 /****************************************************************************/
MSVehicleType
The car-following model and parameter.
Definition: MSVehicleType.h:65
MSVehicleType::getEmissionClass
SUMOEmissionClass getEmissionClass() const
Get this vehicle type's emission class.
Definition: MSVehicleType.h:193
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
ToString.h
MSCFModel::getMaxAccel
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
Definition: MSCFModel.h:209
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
SUMOTime.h
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
MSVehicleControl::getVType
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.
Definition: MSVehicleControl.cpp:353
SUMOTrafficObject::getAcceleration
virtual double getAcceleration() const =0
Returns the vehicle's acceleration.
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MSMeanData_Emissions.h
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
SUMOTrafficObject::isVehicle
virtual bool isVehicle() const =0
Get the vehicle's ID.
PollutantsInterface::FUEL
@ FUEL
Definition: PollutantsInterface.h:55
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
MSMeanData_Emissions::MSLaneMeanDataValues::write
void write(OutputDevice &dev, const SUMOTime period, const double numLanes, const double defaultTravelTime, const int numVehicles=-1) const
Writes output values into the given stream.
Definition: MSMeanData_Emissions.cpp:85
SUMOVehicle::getEmissionParameters
virtual const std::map< int, double > * getEmissionParameters() const =0
Returns the vehicle's emission model parameter.
MSMeanData_Emissions::MSLaneMeanDataValues::~MSLaneMeanDataValues
virtual ~MSLaneMeanDataValues()
Destructor.
Definition: MSMeanData_Emissions.cpp:49
MSMeanData_Emissions::MSLaneMeanDataValues::myEmissions
PollutantsInterface::Emissions myEmissions
Collected values.
Definition: MSMeanData_Emissions.h:108
MSVehicle.h
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
PollutantsInterface::CO
@ CO
Definition: PollutantsInterface.h:55
MSMeanData
Data collector for edges/lanes.
Definition: MSMeanData.h:59
MSMeanData_Emissions
Emission data collector for edges/lanes.
Definition: MSMeanData_Emissions.h:55
MSVehicleType::getCarFollowModel
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
Definition: MSVehicleType.h:140
PollutantsInterface.h
PollutantsInterface::PM_X
@ PM_X
Definition: PollutantsInterface.h:55
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:56
MSMeanData::MeanDataValues::travelledDistance
double travelledDistance
The sum of the distances the vehicles travelled.
Definition: MSMeanData.h:175
PollutantsInterface::HC
@ HC
Definition: PollutantsInterface.h:55
OutputDevice.h
SUMOTrafficObject::getSlope
virtual double getSlope() const =0
Returns the slope of the road at vehicle's position.
PollutantsInterface::computeAll
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...
Definition: PollutantsInterface.cpp:154
PollutantsInterface::NO_X
@ NO_X
Definition: PollutantsInterface.h:55
PollutantsInterface::Emissions::addScaled
void addScaled(const Emissions &a, const double scale=1.)
Add the values of the other struct to this one, scaling the values if needed.
Definition: PollutantsInterface.h:87
MSMeanData_Emissions::MSLaneMeanDataValues::notifyMoveInternal
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.
Definition: MSMeanData_Emissions.cpp:71
PollutantsInterface::ELEC
@ ELEC
Definition: PollutantsInterface.h:55
PollutantsInterface::CO2
@ CO2
Definition: PollutantsInterface.h:55
PollutantsInterface::computeDefault
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...
Definition: PollutantsInterface.cpp:163
MSMeanData::MeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData.h:68
MSMeanData_Emissions::~MSMeanData_Emissions
virtual ~MSMeanData_Emissions()
Destructor.
Definition: MSMeanData_Emissions.cpp:153
MSMeanData_Emissions::MSLaneMeanDataValues::MSLaneMeanDataValues
MSLaneMeanDataValues(MSLane *const lane, const double length, const bool doAdd, const MSMeanData_Emissions *parent)
Constructor.
Definition: MSMeanData_Emissions.cpp:42
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSMeanData_Emissions::MSLaneMeanDataValues::addTo
void addTo(MSMeanData::MeanDataValues &val) const
Add the values of this to the given one and store them there.
Definition: MSMeanData_Emissions.cpp:62
PollutantsInterface::Emissions
Storage for collected values of all emission types.
Definition: PollutantsInterface.h:61
MSMeanData::MeanDataValues::sampleSeconds
double sampleSeconds
Definition: MSMeanData.h:172
MSVehicleType::getMaxSpeed
double getMaxSpeed() const
Get vehicle's maximum speed [m/s].
Definition: MSVehicleType.h:161
config.h
OutputDevice::realString
static std::string realString(const double v, const int precision=gPrecision)
Helper method for string formatting.
Definition: OutputDevice.cpp:159
MSMeanData_Emissions::createValues
MSMeanData::MeanDataValues * createValues(MSLane *const lane, const double length, const bool doAdd) const
Create an instance of MeanDataValues.
Definition: MSMeanData_Emissions.cpp:157
MSLane.h
MSVehicleControl.h
MSMeanData_Emissions::MSLaneMeanDataValues
Data structure for mean (aggregated) edge/lane values.
Definition: MSMeanData_Emissions.h:64
MSMeanData_Emissions::MSMeanData_Emissions
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)
Constructor.
Definition: MSMeanData_Emissions.cpp:138
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:336
MSMeanData_Emissions::MSLaneMeanDataValues::reset
void reset(bool afterWrite=false)
Resets values so they may be used for the next interval.
Definition: MSMeanData_Emissions.cpp:54