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 92234 : MSEmissionExport::write(OutputDevice& of, SUMOTime timestep, int precision) { 44 92234 : const OptionsCont& oc = OptionsCont::getOptions(); 45 184468 : const SUMOTime period = string2time(oc.getString("device.emissions.period")); 46 184468 : const SUMOTime begin = string2time(oc.getString("device.emissions.begin")); 47 92234 : const bool scaled = oc.getBool("emission-output.step-scaled"); 48 92234 : const bool useGeo = oc.getBool("emission-output.geo"); 49 92234 : if ((period > 0 && (timestep - begin) % period != 0) || timestep < begin) { 50 : return; 51 : } 52 86196 : of.openTag("timestep").writeAttr("time", time2string(timestep)); 53 43098 : of.setPrecision(precision); 54 43098 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl(); 55 128666 : for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) { 56 85568 : const SUMOVehicle* veh = it->second; 57 85568 : MSDevice_Emissions* emissionsDevice = (MSDevice_Emissions*)veh->getDevice(typeid(MSDevice_Emissions)); 58 84236 : if (emissionsDevice != nullptr && (veh->isOnRoad() || veh->isIdling())) { 59 18162 : std::string fclass = veh->getVehicleType().getID(); 60 18162 : fclass = fclass.substr(0, fclass.find_first_of("@")); 61 18162 : PollutantsInterface::Emissions emiss = PollutantsInterface::computeAll( 62 18162 : veh->getVehicleType().getEmissionClass(), 63 18162 : veh->getSpeed(), veh->getAcceleration(), veh->getSlope(), 64 18162 : veh->getEmissionParameters()); 65 18162 : if (scaled) { 66 600 : PollutantsInterface::Emissions tmp; 67 600 : tmp.addScaled(emiss, TS); 68 600 : emiss = tmp; 69 : } 70 60938 : of.openTag("vehicle").writeAttr("id", veh->getID()).writeAttr("eclass", PollutantsInterface::getName(veh->getVehicleType().getEmissionClass())); 71 90810 : of.writeAttr("CO2", emiss.CO2).writeAttr("CO", emiss.CO).writeAttr("HC", emiss.HC).writeAttr("NOx", emiss.NOx); 72 72648 : of.writeAttr("PMx", emiss.PMx).writeAttr("fuel", emiss.fuel).writeAttr("electricity", emiss.electricity); 73 18162 : of.writeAttr("noise", HelpersHarmonoise::computeNoise(veh->getVehicleType().getEmissionClass(), veh->getSpeed(), veh->getAcceleration())); 74 54486 : of.writeAttr("route", veh->getRoute().getID()).writeAttr("type", fclass); 75 18162 : if (MSGlobals::gUseMesoSim) { 76 3998 : const MEVehicle* mesoVeh = dynamic_cast<const MEVehicle*>(veh); 77 3998 : of.writeAttr("waiting", mesoVeh->getWaitingSeconds()); 78 7996 : of.writeAttr("edge", veh->getEdge()->getID()); 79 : } else { 80 14164 : const MSVehicle* microVeh = dynamic_cast<const MSVehicle*>(veh); 81 14164 : of.writeAttr("waiting", microVeh->getWaitingSeconds()); 82 28328 : of.writeAttr("lane", microVeh->getLane()->getID()); 83 : } 84 18162 : of.writeAttr("pos", veh->getPositionOnLane()); 85 18162 : of.writeAttr("speed", veh->getSpeed()); 86 18162 : of.writeAttr("angle", GeomHelper::naviDegree(veh->getAngle())); 87 : 88 18162 : Position pos = veh->getPosition(); 89 18162 : if (useGeo) { 90 60 : of.setPrecision(MAX2(gPrecisionGeo, precision)); 91 60 : GeoConvHelper::getFinal().cartesian2geo(pos); 92 : } 93 36324 : of.writeAttr(SUMO_ATTR_X, pos.x()); 94 18162 : of.writeAttr(SUMO_ATTR_Y, pos.y()); 95 18162 : if (MSNet::getInstance()->hasElevation()) { 96 112 : of.writeAttr(SUMO_ATTR_Z, pos.z()); 97 : } 98 18162 : of.setPrecision(precision); 99 : 100 36324 : of.closeTag(); 101 : } 102 : } 103 43098 : of.setPrecision(gPrecision); 104 86196 : of.closeTag(); 105 : }