Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-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 : /****************************************************************************/
14 : /// @file MSEmissionExport.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Mario Krumnow
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @date 2012-04-26
20 : ///
21 : // Realises dumping Emission Data
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <utils/iodevices/OutputDevice.h>
26 : #include <utils/emissions/PollutantsInterface.h>
27 : #include <utils/emissions/HelpersHarmonoise.h>
28 : #include <utils/geom/GeomHelper.h>
29 : #include <utils/geom/GeoConvHelper.h>
30 : #include <microsim/MSLane.h>
31 : #include <microsim/MSNet.h>
32 : #include <microsim/MSVehicle.h>
33 : #include <microsim/devices/MSDevice_Emissions.h>
34 : #include <mesosim/MEVehicle.h>
35 : #include <microsim/MSVehicleControl.h>
36 : #include "MSEmissionExport.h"
37 :
38 :
39 : // ===========================================================================
40 : // method definitions
41 : // ===========================================================================
42 : void
43 93124 : MSEmissionExport::write(OutputDevice& of, SUMOTime timestep, int precision) {
44 93124 : const OptionsCont& oc = OptionsCont::getOptions();
45 93124 : const SUMOTime period = string2time(oc.getString("device.emissions.period"));
46 93124 : const SUMOTime begin = string2time(oc.getString("device.emissions.begin"));
47 93124 : const bool scaled = oc.getBool("emission-output.step-scaled");
48 93124 : const bool useGeo = oc.getBool("emission-output.geo");
49 93124 : if ((period > 0 && (timestep - begin) % period != 0) || timestep < begin) {
50 49136 : return;
51 : }
52 : const SumoXMLAttrMask mask = MSDevice_Emissions::getWrittenAttributes();
53 :
54 87976 : of.openTag("timestep").writeAttr("time", time2string(timestep));
55 43988 : of.setPrecision(precision);
56 43988 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
57 131406 : for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
58 87418 : const SUMOVehicle* veh = it->second;
59 87418 : MSDevice_Emissions* emissionsDevice = (MSDevice_Emissions*)veh->getDevice(typeid(MSDevice_Emissions));
60 86086 : if (emissionsDevice != nullptr && (veh->isOnRoad() || veh->isIdling())) {
61 19417 : std::string fclass = veh->getVehicleType().getID();
62 19417 : fclass = fclass.substr(0, fclass.find_first_of("@"));
63 19417 : PollutantsInterface::Emissions emiss = PollutantsInterface::computeAll(
64 19417 : veh->getVehicleType().getEmissionClass(),
65 19417 : veh->getSpeed(), veh->getAcceleration(), veh->getSlope(),
66 19417 : veh->getEmissionParameters());
67 19417 : if (scaled) {
68 600 : PollutantsInterface::Emissions tmp;
69 600 : tmp.addScaled(emiss, TS);
70 600 : emiss = tmp;
71 : }
72 38834 : of.openTag("vehicle");
73 : of.writeAttr(SUMO_ATTR_ID, veh->getID());
74 19417 : of.writeOptionalAttr(SUMO_ATTR_ECLASS, PollutantsInterface::getName(veh->getVehicleType().getEmissionClass()), mask);
75 19417 : of.writeOptionalAttr(SUMO_ATTR_CO2, emiss.CO2, mask);
76 19417 : of.writeOptionalAttr(SUMO_ATTR_CO, emiss.CO, mask);
77 19417 : of.writeOptionalAttr(SUMO_ATTR_HC, emiss.HC, mask);
78 19417 : of.writeOptionalAttr(SUMO_ATTR_NOX, emiss.NOx, mask);
79 19417 : of.writeOptionalAttr(SUMO_ATTR_PMX, emiss.PMx, mask);
80 19417 : of.writeOptionalAttr(SUMO_ATTR_FUEL, emiss.fuel, mask);
81 19417 : of.writeOptionalAttr(SUMO_ATTR_ELECTRICITY, emiss.electricity, mask);
82 19417 : of.writeOptionalAttr(SUMO_ATTR_NOISE, HelpersHarmonoise::computeNoise(veh->getVehicleType().getEmissionClass(), veh->getSpeed(), veh->getAcceleration()), mask);
83 19417 : of.writeOptionalAttr(SUMO_ATTR_ROUTE, veh->getRoute().getID(), mask);
84 19417 : of.writeOptionalAttr(SUMO_ATTR_TYPE, fclass, mask);
85 19417 : if (MSGlobals::gUseMesoSim) {
86 4458 : const MEVehicle* mesoVeh = dynamic_cast<const MEVehicle*>(veh);
87 4458 : of.writeOptionalAttr(SUMO_ATTR_WAITING, mesoVeh->getWaitingSeconds(), mask);
88 4458 : of.writeOptionalAttr(SUMO_ATTR_EDGE, veh->getEdge()->getID(), mask);
89 : } else {
90 14959 : const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh);
91 14959 : of.writeOptionalAttr(SUMO_ATTR_WAITING, microVeh->getWaitingSeconds(), mask);
92 14959 : of.writeOptionalAttr(SUMO_ATTR_LANE, microVeh->getLane()->getID(), mask);
93 : }
94 19417 : of.writeOptionalAttr(SUMO_ATTR_POSITION, veh->getPositionOnLane(), mask);
95 19417 : of.writeOptionalAttr(SUMO_ATTR_SPEED, veh->getSpeed(), mask);
96 19417 : of.writeOptionalAttr(SUMO_ATTR_ANGLE, GeomHelper::naviDegree(veh->getAngle()), mask);
97 :
98 19417 : Position pos = veh->getPosition();
99 19417 : if (useGeo) {
100 60 : of.setPrecision(MAX2(gPrecisionGeo, precision));
101 60 : GeoConvHelper::getFinal().cartesian2geo(pos);
102 : }
103 19417 : of.writeOptionalAttr(SUMO_ATTR_X, pos.x(), mask);
104 19417 : of.writeOptionalAttr(SUMO_ATTR_Y, pos.y(), mask);
105 19417 : if (MSNet::getInstance()->hasElevation()) {
106 56 : of.writeOptionalAttr(SUMO_ATTR_Z, pos.z(), mask);
107 : }
108 19417 : of.setPrecision(precision);
109 :
110 38834 : of.closeTag();
111 : }
112 : }
113 43988 : of.setPrecision(gPrecision);
114 87976 : of.closeTag();
115 : }
|