1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2014-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 /****************************************************************************/
10 /// @file    MSAmitranTrajectories.cpp
11 /// @author  Michael Behrisch
12 /// @date    13.03.2014
13 /// @version $Id$
14 ///
15 // Realises dumping the complete network state
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <microsim/MSVehicleControl.h>
25 #include <microsim/MSEdge.h>
26 #include <microsim/MSLane.h>
27 #include <microsim/MSNet.h>
28 #include <microsim/MSVehicle.h>
29 #include <microsim/MSGlobals.h>
30 #include <utils/iodevices/OutputDevice.h>
31 #include <utils/emissions/PollutantsInterface.h>
32 #include "MSAmitranTrajectories.h"
33 
34 // ===========================================================================
35 // static member definitions
36 // ===========================================================================
37 std::set<std::string> MSAmitranTrajectories::myWrittenTypes;
38 std::map<std::string, int> MSAmitranTrajectories::myWrittenVehicles;
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 void
write(OutputDevice & of,const SUMOTime timestep)45 MSAmitranTrajectories::write(OutputDevice& of, const SUMOTime timestep) {
46     MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
47     for (MSVehicleControl::constVehIt v = vc.loadedVehBegin(); v != vc.loadedVehEnd(); ++v) {
48         writeVehicle(of, *v->second, timestep);
49     }
50 }
51 
52 
53 void
writeVehicle(OutputDevice & of,const SUMOVehicle & veh,const SUMOTime timestep)54 MSAmitranTrajectories::writeVehicle(OutputDevice& of, const SUMOVehicle& veh, const SUMOTime timestep) {
55     if (veh.isOnRoad()) {
56         const std::string& type = veh.getVehicleType().getID();
57         if (myWrittenTypes.count(type) == 0) {
58             of.openTag(SUMO_TAG_ACTORCONFIG).writeAttr(SUMO_ATTR_ID, veh.getVehicleType().getNumericalID());
59             const SUMOEmissionClass c = veh.getVehicleType().getEmissionClass();
60             if (c != 0) {
61                 of.writeAttr(SUMO_ATTR_VEHICLECLASS, PollutantsInterface::getAmitranVehicleClass(c));
62                 of.writeAttr("fuel", PollutantsInterface::getFuel(c));
63                 of.writeAttr(SUMO_ATTR_EMISSIONCLASS, "Euro" + toString(PollutantsInterface::getEuroClass(c)));
64                 const double weight = PollutantsInterface::getWeight(c);
65                 if (weight > 0.) {
66                     of.writeAttr(SUMO_ATTR_WEIGHT, int(weight / 10. + 0.5));
67                 }
68             }
69             of.writeAttr(SUMO_ATTR_REF, type).closeTag();
70             myWrittenTypes.insert(type);
71         }
72         if (myWrittenVehicles.count(veh.getID()) == 0) {
73             const int index = (int)myWrittenVehicles.size();
74             of.openTag(SUMO_TAG_VEHICLE).writeAttr(SUMO_ATTR_ID, index)
75             .writeAttr(SUMO_ATTR_ACTORCONFIG, veh.getVehicleType().getNumericalID())
76             .writeAttr(SUMO_ATTR_STARTTIME, STEPS2MS(veh.getDeparture()));
77             of.writeAttr(SUMO_ATTR_REF, veh.getID()).closeTag();
78             myWrittenVehicles[veh.getID()] = index;
79         }
80         of.openTag(SUMO_TAG_MOTIONSTATE).writeAttr(SUMO_ATTR_VEHICLE, myWrittenVehicles[veh.getID()])
81         .writeAttr(SUMO_ATTR_SPEED, int(100.*veh.getSpeed() + 0.5))
82         .writeAttr(SUMO_ATTR_TIME, STEPS2MS(timestep))
83         .writeAttr(SUMO_ATTR_ACCELERATION, int(1000.*veh.getAcceleration() + 0.5));
84         of.closeTag();
85     }
86 }
87 
88 
89 /****************************************************************************/
90