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-2016 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 bool computeAForward, const SUMOEmissionClass defaultClass,
53  const SUMOReal defaultSlope, std::ostream* stdOut, OutputDevice* xmlOut)
54  : SUMOSAXHandler(""), myComputeA(computeA), myComputeAForward(computeAForward), 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)) {
78  } else {
79  const std::string acId = attrs.getString(SUMO_ATTR_ACTORCONFIG);
80  const std::string id = attrs.getString(SUMO_ATTR_ID);
81  if (myEmissionClassByType.count(acId) == 0) {
82  WRITE_WARNING("Unknown actor configuration '" + acId + "' for vehicle '" + id + "'!");
83  } else {
85  }
86  }
87  break;
88  case SUMO_TAG_ACTORCONFIG: {
89  const std::string id = attrs.getString(SUMO_ATTR_ID);
90  const std::string vClass = attrs.getString(SUMO_ATTR_VEHICLECLASS);
91  const std::string fuel = attrs.getString(SUMO_ATTR_FUEL);
92  const std::string eClass = attrs.getString(SUMO_ATTR_EMISSIONCLASS);
93  const SUMOReal weight = attrs.getOpt<SUMOReal>(SUMO_ATTR_WEIGHT, id.c_str(), ok, 0.) * 10.;
94  myEmissionClassByType[id] = PollutantsInterface::getClass(myDefaultClass, vClass, fuel, eClass, weight);
95  break;
96  }
97  case SUMO_TAG_MOTIONSTATE: {
98  const std::string id = attrs.getString(SUMO_ATTR_VEHICLE);
99  if (myEmissionClassByVehicle.count(id) == 0) {
100  WRITE_WARNING("Motion state for unknown vehicle '" + id + "'!");
102  }
104  SUMOReal v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
105  SUMOReal a = attrs.hasAttribute(SUMO_ATTR_ACCELERATION) ? attrs.get<SUMOReal>(SUMO_ATTR_ACCELERATION, id.c_str(), ok) / 1000. : INVALID_VALUE;
106  SUMOReal s = attrs.hasAttribute(SUMO_ATTR_SLOPE) ? RAD2DEG(asin(attrs.get<SUMOReal>(SUMO_ATTR_SLOPE, id.c_str(), ok) / 10000.)) : INVALID_VALUE;
107  const SUMOTime time = attrs.getOpt<int>(SUMO_ATTR_TIME, id.c_str(), ok, INVALID_VALUE);
108  if (myXMLOut != 0) {
109  writeXMLEmissions(id, c, time, v, a, s);
110  }
111  if (myStdOut != 0) {
112  writeEmissions(*myStdOut, id, c, STEPS2TIME(time), v, a, s);
113  }
114  break;
115  }
116  default:
117  break;
118  }
119 }
120 
121 
124  SUMOReal& v, SUMOReal& a, SUMOReal& s) {
125 
126  if (myComputeA) {
127  if (myLastV.count(id) == 0) {
128  a = 0.;
129  } else {
130  a = v - myLastV[id];
131  }
132  myLastV[id] = v;
133  if (myComputeAForward) {
134  v -= a;
135  }
136  }
137  if (a == INVALID_VALUE) {
138  throw ProcessError("Acceleration information is missing; try running with --compute-a.");
139  }
140  if (s == INVALID_VALUE) {
141  s = myDefaultSlope;
142  }
144  mySums[id].addScaled(result, myStepSize);
145  if (id != "") {
146  mySums[""].addScaled(result, myStepSize);
147  }
148  return result;
149 }
150 
151 
152 bool
153 TrajectoriesHandler::writeEmissions(std::ostream& o, const std::string id,
154  const SUMOEmissionClass c,
155  SUMOReal t, SUMOReal& v,
156  SUMOReal& a, SUMOReal& s) {
157  if (myComputeA && myLastV.count(id) == 0) {
158  myLastV[id] = v;
159  return false;
160  }
161  if (myComputeAForward) {
162  t -= TS;
163  }
164  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
165  o << t << ";" << v << ";" << a << ";" << s << ";"
166  << e.CO << ";" << e.CO2 << ";" << e.HC << ";" << e.PMx << ";"
167  << e.NOx << ";" << e.fuel << ";" << e.electricity << std::endl;
168  return true;
169 }
170 
171 
172 bool
174  const SUMOEmissionClass c,
175  SUMOTime t, SUMOReal& v,
176  SUMOReal a, SUMOReal s) {
177  if (myComputeA && myLastV.count(id) == 0) {
178  myLastV[id] = v;
179  return false;
180  }
181  if (myCurrentTime != t) {
182  if (myCurrentTime != -1) {
183  myXMLOut->closeTag();
184  }
185  myCurrentTime = t;
187  }
188  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
189  myXMLOut->openTag("vehicle").writeAttr("id", id).writeAttr("eclass", PollutantsInterface::getName(c));
190  myXMLOut->writeAttr("CO2", e.CO2).writeAttr("CO", e.CO).writeAttr("HC", e.HC).writeAttr("NOx", e.NOx);
191  myXMLOut->writeAttr("PMx", e.PMx).writeAttr("fuel", e.fuel).writeAttr("electricity", e.electricity);
192  myXMLOut->writeAttr("speed", v).closeTag();
193  return true;
194 }
195 
196 
197 void
198 TrajectoriesHandler::writeSums(std::ostream& o, const std::string id) {
199  o << "CO:" << mySums[id].CO << std::endl
200  << "CO2:" << mySums[id].CO2 << std::endl
201  << "HC:" << mySums[id].HC << std::endl
202  << "NOx:" << mySums[id].NOx << std::endl
203  << "PMx:" << mySums[id].PMx << std::endl
204  << "fuel:" << mySums[id].fuel << std::endl
205  << "electricity:" << mySums[id].electricity << std::endl;
206 }
207 
208 
209 void
210 TrajectoriesHandler::writeNormedSums(std::ostream& o, const std::string id, const SUMOReal factor) {
211  o << mySums[id].fuel / factor << ","
212  << mySums[id].electricity / factor << ","
213  << mySums[id].CO2 / factor << ","
214  << mySums[id].NOx / factor << ","
215  << mySums[id].CO / factor << ","
216  << mySums[id].HC / factor << ","
217  << mySums[id].PMx / factor << std::endl;
218 }
219 
220 
221 /****************************************************************************/
bool writeXMLEmissions(const std::string id, const SUMOEmissionClass c, SUMOTime t, 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 PollutantsInterface::Emissions computeEmissions(const std::string id, const SUMOEmissionClass c, SUMOReal &v, SUMOReal &a, SUMOReal &s)
long long int SUMOTime
Definition: SUMOTime.h:43
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:59
Storage for collected values of all emission types.
the weight of a district&#39;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...
bool writeEmissions(std::ostream &o, const std::string id, const SUMOEmissionClass c, SUMOReal t, SUMOReal &v, SUMOReal &a, SUMOReal &s)
#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.
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.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
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.
~TrajectoriesHandler()
Destructor.
std::map< std::string, SUMOEmissionClass > myEmissionClassByVehicle
void writeSums(std::ostream &o, const std::string id)
void writeNormedSums(std::ostream &o, const std::string id, const SUMOReal factor)
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:213
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.
TrajectoriesHandler(const bool computeA, const bool computeAForward, const SUMOEmissionClass defaultClass, const SUMOReal defaultSlope, std::ostream *stdOut, OutputDevice *xmlOut)
Constructor.