Eclipse SUMO - Simulation of Urban MObility
MSEmissionExport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-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 /****************************************************************************/
21 // Realises dumping Emission Data
22 /****************************************************************************/
23 #include <config.h>
24 
28 #include <utils/geom/GeomHelper.h>
29 #include <microsim/MSNet.h>
30 #include <microsim/MSVehicle.h>
32 #include <mesosim/MEVehicle.h>
34 #include "MSEmissionExport.h"
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
40 void
41 MSEmissionExport::write(OutputDevice& of, SUMOTime timestep, int precision) {
42  const SUMOTime period = string2time(OptionsCont::getOptions().getString("device.emissions.period"));
43  const SUMOTime begin = string2time(OptionsCont::getOptions().getString("begin"));
44  if (period > 0 && (timestep - begin) % period != 0) {
45  return;
46  }
47  of.openTag("timestep").writeAttr("time", time2string(timestep));
48  of.setPrecision(precision);
50  for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
51  const SUMOVehicle* veh = it->second;
52  MSDevice_Emissions* emissionsDevice = (MSDevice_Emissions*)veh->getDevice(typeid(MSDevice_Emissions));
53  if (emissionsDevice != nullptr && (veh->isOnRoad() || veh->isIdling())) {
54  std::string fclass = veh->getVehicleType().getID();
55  fclass = fclass.substr(0, fclass.find_first_of("@"));
58  veh->getSpeed(), veh->getAcceleration(), veh->getSlope(),
59  veh->getEmissionParameters());
60  of.openTag("vehicle").writeAttr("id", veh->getID()).writeAttr("eclass", PollutantsInterface::getName(veh->getVehicleType().getEmissionClass()));
61  of.writeAttr("CO2", emiss.CO2).writeAttr("CO", emiss.CO).writeAttr("HC", emiss.HC).writeAttr("NOx", emiss.NOx);
62  of.writeAttr("PMx", emiss.PMx).writeAttr("fuel", emiss.fuel).writeAttr("electricity", emiss.electricity);
64  of.writeAttr("route", veh->getRoute().getID()).writeAttr("type", fclass);
66  const MEVehicle* mesoVeh = dynamic_cast<const MEVehicle*>(veh);
67  of.writeAttr("waiting", mesoVeh->getWaitingSeconds());
68  of.writeAttr("edge", veh->getEdge()->getID());
69  } else {
70  const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
71  of.writeAttr("waiting", microVeh->getWaitingSeconds());
72  of.writeAttr("lane", microVeh->getLane()->getID());
73  }
74  of.writeAttr("pos", veh->getPositionOnLane()).writeAttr("speed", veh->getSpeed());
75  of.writeAttr("angle", GeomHelper::naviDegree(veh->getAngle())).writeAttr("x", veh->getPosition().x()).writeAttr("y", veh->getPosition().y());
76  of.closeTag();
77  }
78  }
80  of.closeTag();
81 }
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:31
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
static double naviDegree(const double angle)
Definition: GeomHelper.cpp:192
static double computeNoise(SUMOEmissionClass c, double v, double a)
Returns the noise produced by the a vehicle of the given type at the given speed.
A vehicle from the mesoscopic point of view.
Definition: MEVehicle.h:42
double getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
Definition: MEVehicle.h:305
A device which collects vehicular emissions.
static void write(OutputDevice &of, SUMOTime timestep, int precision)
Writes the complete network state of the given edges into the given device.
static bool gUseMesoSim
Definition: MSGlobals.h:88
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
The class responsible for building and deletion of vehicles.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
double getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
Definition: MSVehicle.h:669
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:550
SUMOEmissionClass getEmissionClass() const
Get this vehicle type's emission class.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:90
const std::string & getID() const
Returns the id.
Definition: Named.h:73
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
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:239
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
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...
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
double x() const
Returns the x-position.
Definition: Position.h:54
double y() const
Returns the y-position.
Definition: Position.h:59
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 double getSpeed() const =0
Returns the object's current speed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
Representation of a vehicle.
Definition: SUMOVehicle.h:58
virtual const std::map< int, double > * getEmissionParameters() const =0
Returns the vehicle's emission model parameter.
virtual const MSRoute & getRoute() const =0
Returns the current route.
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
virtual bool isOnRoad() const =0
Returns the information whether the vehicle is on a road (is simulated)
virtual bool isIdling() const =0
Returns whether the vehicle is idling (waiting to re-enter the net.
virtual double getAngle() const =0
Get the vehicle's angle.
Storage for collected values of all emission types.