SUMO - Simulation of Urban MObility
TrajectoriesHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // An XML-Handler for amitran and netstate trajectories
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2014-2015 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <utility>
33 #include <iostream>
36 #include <utils/common/ToString.h>
38 #include <utils/geom/GeomHelper.h>
42 #include "TrajectoriesHandler.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 TrajectoriesHandler::TrajectoriesHandler(const bool computeA, const SUMOEmissionClass defaultClass,
53  const SUMOReal defaultSlope, std::ostream* stdOut, OutputDevice* xmlOut)
54  : SUMOSAXHandler(""), myComputeA(computeA), myDefaultClass(defaultClass),
55  myDefaultSlope(defaultSlope), myStdOut(stdOut), myXMLOut(xmlOut), myCurrentTime(-1), myStepSize(TS) {}
56 
57 
59 
60 
61 void
63  const SUMOSAXAttributes& attrs) {
64  bool ok = true;
65  switch (element) {
67  myStepSize = attrs.getFloat("timeStepSize") / 1000.;
68  break;
69  case SUMO_TAG_TIMESTEP:
71  break;
72  case SUMO_TAG_VEHICLE:
73  if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
75  } else {
76  const std::string acId = attrs.getString(SUMO_ATTR_ACTORCONFIG);
77  const std::string id = attrs.getString(SUMO_ATTR_ID);
78  if (myEmissionClassByType.count(acId) == 0) {
79  WRITE_WARNING("Unknown actor configuration '" + acId + "' for vehicle '" + id + "'!");
80  } else {
82  }
83  }
84  break;
85  case SUMO_TAG_ACTORCONFIG: {
86  const std::string id = attrs.getString(SUMO_ATTR_ID);
87  const std::string vClass = attrs.getString(SUMO_ATTR_VEHICLECLASS);
88  const std::string fuel = attrs.getString(SUMO_ATTR_FUEL);
89  const std::string eClass = attrs.getString(SUMO_ATTR_EMISSIONCLASS);
90  const SUMOReal weight = attrs.getOpt<SUMOReal>(SUMO_ATTR_WEIGHT, id.c_str(), ok, 0.) * 10.;
91  myEmissionClassByType[id] = PollutantsInterface::getClass(myDefaultClass, vClass, fuel, eClass, weight);
92  break;
93  }
94  case SUMO_TAG_MOTIONSTATE: {
95  const std::string id = attrs.getString(SUMO_ATTR_VEHICLE);
96  if (myEmissionClassByVehicle.count(id) == 0) {
97  WRITE_WARNING("Motion state for unknown vehicle '" + id + "'!");
99  }
101  const SUMOReal v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
102  const SUMOReal a = attrs.hasAttribute(SUMO_ATTR_ACCELERATION) ? attrs.get<SUMOReal>(SUMO_ATTR_ACCELERATION, id.c_str(), ok) / 1000. : INVALID_VALUE;
103  const SUMOReal s = attrs.hasAttribute(SUMO_ATTR_SLOPE) ? RAD2DEG(asin(attrs.get<SUMOReal>(SUMO_ATTR_SLOPE, id.c_str(), ok) / 10000.)) : INVALID_VALUE;
104  const SUMOTime time = attrs.getOpt<int>(SUMO_ATTR_TIME, id.c_str(), ok, INVALID_VALUE);
105  if (myXMLOut != 0) {
106  writeXMLEmissions(id, c, time, v, a, s);
107  }
108  if (myStdOut != 0) {
109  writeEmissions(*myStdOut, id, c, time, v, a, s);
110  }
111  break;
112  }
113  default:
114  break;
115  }
116 }
117 
118 
121  const SUMOReal v, SUMOReal& a, SUMOReal& s) {
122  if (myComputeA) {
123  if (myLastV.count(id) == 0) {
124  a = 0.;
125  } else {
126  a = v - myLastV[id];
127  }
128  myLastV[id] = v;
129  }
130  if (a == INVALID_VALUE) {
131  throw ProcessError("Acceleration information is missing; try running with --compute-a.");
132  }
133  if (s == INVALID_VALUE) {
134  s = myDefaultSlope;
135  }
137  mySums[id].addScaled(result, myStepSize);
138  if (id != "") {
139  mySums[""].addScaled(result, myStepSize);
140  }
141  return result;
142 }
143 
144 
145 void
146 TrajectoriesHandler::writeEmissions(std::ostream& o, const std::string id,
147  const SUMOEmissionClass c,
148  const SUMOReal t, const SUMOReal v,
149  SUMOReal a, SUMOReal s) {
150  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
151  o << t << ";" << v << ";" << a << ";" << s
152  << ";" << e.CO << ";" << e.CO2 << ";" << e.HC << ";" << e.PMx << ";" << e.NOx << ";" << e.fuel << std::endl;
153 }
154 
155 
156 void
158  const SUMOEmissionClass c,
159  const SUMOTime t, const SUMOReal v,
160  SUMOReal a, SUMOReal s) {
161  if (myCurrentTime != t) {
162  if (myCurrentTime != -1) {
163  myXMLOut->closeTag();
164  }
165  myCurrentTime = t;
167  }
168  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
169  myXMLOut->openTag("vehicle").writeAttr("id", id).writeAttr("eclass", PollutantsInterface::getName(c));
170  myXMLOut->writeAttr("CO2", e.CO2).writeAttr("CO", e.CO).writeAttr("HC", e.HC).writeAttr("NOx", e.NOx);
171  myXMLOut->writeAttr("PMx", e.PMx).writeAttr("fuel", e.fuel);
172  myXMLOut->writeAttr("speed", v).closeTag();
173 }
174 
175 
176 void
177 TrajectoriesHandler::writeSums(std::ostream& o, const std::string id) {
178  o << "CO:" << mySums[id].CO << std::endl
179  << "CO2:" << mySums[id].CO2 << std::endl
180  << "HC:" << mySums[id].HC << std::endl
181  << "NOx:" << mySums[id].NOx << std::endl
182  << "PMx:" << mySums[id].PMx << std::endl
183  << "fuel:" << mySums[id].fuel << std::endl;
184 }
185 
186 
187 /****************************************************************************/
void writeEmissions(std::ostream &o, const std::string id, const SUMOEmissionClass c, const SUMOReal t, const SUMOReal v, SUMOReal a=INVALID_VALUE, SUMOReal s=INVALID_VALUE)
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
const SUMOEmissionClass myDefaultClass
std::map< std::string, SUMOEmissionClass > myEmissionClassByType
static Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
Storage for collected values of all emission types.
the weight of a district's source or sink
#define RAD2DEG(x)
Definition: GeomHelper.h:46
#define TS
Definition: SUMOTime.h:52
SAX-handler base for SUMO-files.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
const PollutantsInterface::Emissions computeEmissions(const std::string id, const SUMOEmissionClass c, const SUMOReal v, SUMOReal &a, SUMOReal &s)
static const int INVALID_VALUE
Encapsulated SAX-Attributes.
int SUMOEmissionClass
virtual SUMOReal getFloat(int id) const =0
Returns the SUMOReal-value of the named (by its enum-value) attribute.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called when an opening-tag occurs.
TrajectoriesHandler(const bool computeA, const SUMOEmissionClass defaultClass, const SUMOReal defaultSlope, std::ostream *stdOut, OutputDevice *xmlOut)
Constructor.
static SUMOEmissionClass getClass(const SUMOEmissionClass base, const std::string &vClass, const std::string &fuel, const std::string &eClass, const double weight)
Returns the emission class fittig the given parameters.
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
void writeXMLEmissions(const std::string id, const SUMOEmissionClass c, const SUMOTime t, const SUMOReal v, SUMOReal a=INVALID_VALUE, SUMOReal s=INVALID_VALUE)
~TrajectoriesHandler()
Destructor.
std::map< std::string, SUMOEmissionClass > myEmissionClassByVehicle
void writeSums(std::ostream &o, const std::string id)
int SUMOTime
Definition: SUMOTime.h:43
const SUMOReal myDefaultSlope
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
std::map< std::string, SUMOReal > myLastV
std::map< std::string, PollutantsInterface::Emissions > mySums
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.