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,
53  const bool accelZeroCorrection, const SUMOEmissionClass defaultClass,
54  const SUMOReal defaultSlope, std::ostream* stdOut, OutputDevice* xmlOut)
55  : SUMOSAXHandler(""), myComputeA(computeA), myComputeAForward(computeAForward), myAccelZeroCorrection(accelZeroCorrection), myDefaultClass(defaultClass),
56  myDefaultSlope(defaultSlope), myStdOut(stdOut), myXMLOut(xmlOut), myCurrentTime(-1), myStepSize(TS) {}
57 
58 
60 
61 
62 void
64  const SUMOSAXAttributes& attrs) {
65  bool ok = true;
66  switch (element) {
68  myStepSize = attrs.getFloat("timeStepSize") / 1000.;
69  break;
70  case SUMO_TAG_TIMESTEP:
72  break;
73  case SUMO_TAG_VEHICLE:
74  if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
79  } else {
80  const std::string acId = attrs.getString(SUMO_ATTR_ACTORCONFIG);
81  const std::string id = attrs.getString(SUMO_ATTR_ID);
82  if (myEmissionClassByType.count(acId) == 0) {
83  WRITE_WARNING("Unknown actor configuration '" + acId + "' for vehicle '" + id + "'!");
84  } else {
86  }
87  }
88  break;
89  case SUMO_TAG_ACTORCONFIG: {
90  const std::string id = attrs.getString(SUMO_ATTR_ID);
91  const std::string vClass = attrs.getString(SUMO_ATTR_VEHICLECLASS);
92  const std::string fuel = attrs.getString(SUMO_ATTR_FUEL);
93  const std::string eClass = attrs.getString(SUMO_ATTR_EMISSIONCLASS);
94  const SUMOReal weight = attrs.getOpt<SUMOReal>(SUMO_ATTR_WEIGHT, id.c_str(), ok, 0.) * 10.;
95  myEmissionClassByType[id] = PollutantsInterface::getClass(myDefaultClass, vClass, fuel, eClass, weight);
96  break;
97  }
98  case SUMO_TAG_MOTIONSTATE: {
99  const std::string id = attrs.getString(SUMO_ATTR_VEHICLE);
100  if (myEmissionClassByVehicle.count(id) == 0) {
101  WRITE_WARNING("Motion state for unknown vehicle '" + id + "'!");
103  }
105  SUMOReal v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
106  SUMOReal a = attrs.hasAttribute(SUMO_ATTR_ACCELERATION) ? attrs.get<SUMOReal>(SUMO_ATTR_ACCELERATION, id.c_str(), ok) / 1000. : INVALID_VALUE;
107  SUMOReal s = attrs.hasAttribute(SUMO_ATTR_SLOPE) ? RAD2DEG(asin(attrs.get<SUMOReal>(SUMO_ATTR_SLOPE, id.c_str(), ok) / 10000.)) : INVALID_VALUE;
108  const SUMOTime time = attrs.getOpt<int>(SUMO_ATTR_TIME, id.c_str(), ok, INVALID_VALUE);
109  if (myXMLOut != 0) {
110  writeXMLEmissions(id, c, time, v, a, s);
111  }
112  if (myStdOut != 0) {
113  writeEmissions(*myStdOut, id, c, STEPS2TIME(time), v, a, s);
114  }
115  break;
116  }
117  default:
118  break;
119  }
120 }
121 
122 
125  SUMOReal& v, SUMOReal& a, SUMOReal& s) {
126 
127  if (myComputeA) {
128  if (myLastV.count(id) == 0) {
129  a = 0.;
130  } else {
131  a = v - myLastV[id];
132  }
133  myLastV[id] = v;
134  if (myComputeAForward) {
135  v -= a;
136  }
137  }
138  if (myAccelZeroCorrection && v == 0.) {
139  a = 0.;
140  }
141  if (a == INVALID_VALUE) {
142  throw ProcessError("Acceleration information is missing; try running with --compute-a.");
143  }
144  if (s == INVALID_VALUE) {
145  s = myDefaultSlope;
146  }
148  mySums[id].addScaled(result, myStepSize);
149  if (id != "") {
150  mySums[""].addScaled(result, myStepSize);
151  }
152  return result;
153 }
154 
155 
156 bool
157 TrajectoriesHandler::writeEmissions(std::ostream& o, const std::string id,
158  const SUMOEmissionClass c,
159  SUMOReal t, SUMOReal& v,
160  SUMOReal& a, SUMOReal& s) {
161  if (myComputeA && myLastV.count(id) == 0) {
162  myLastV[id] = v;
163  return false;
164  }
165  if (myComputeAForward) {
166  t -= TS;
167  }
168  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
169  o << t << ";" << v << ";" << a << ";" << s << ";"
170  << e.CO << ";" << e.CO2 << ";" << e.HC << ";" << e.PMx << ";"
171  << e.NOx << ";" << e.fuel << ";" << e.electricity << std::endl;
172  return true;
173 }
174 
175 
176 bool
178  const SUMOEmissionClass c,
179  SUMOTime t, SUMOReal& v,
180  SUMOReal a, SUMOReal s) {
181  if (myComputeA && myLastV.count(id) == 0) {
182  myLastV[id] = v;
183  return false;
184  }
185  if (myCurrentTime != t) {
186  if (myCurrentTime != -1) {
187  myXMLOut->closeTag();
188  }
189  myCurrentTime = t;
191  }
192  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
193  myXMLOut->openTag("vehicle").writeAttr("id", id).writeAttr("eclass", PollutantsInterface::getName(c));
194  myXMLOut->writeAttr("CO2", e.CO2).writeAttr("CO", e.CO).writeAttr("HC", e.HC).writeAttr("NOx", e.NOx);
195  myXMLOut->writeAttr("PMx", e.PMx).writeAttr("fuel", e.fuel).writeAttr("electricity", e.electricity);
196  myXMLOut->writeAttr("speed", v).closeTag();
197  return true;
198 }
199 
200 
201 void
202 TrajectoriesHandler::writeSums(std::ostream& o, const std::string id) {
203  o << "CO:" << mySums[id].CO << std::endl
204  << "CO2:" << mySums[id].CO2 << std::endl
205  << "HC:" << mySums[id].HC << std::endl
206  << "NOx:" << mySums[id].NOx << std::endl
207  << "PMx:" << mySums[id].PMx << std::endl
208  << "fuel:" << mySums[id].fuel << std::endl
209  << "electricity:" << mySums[id].electricity << std::endl;
210 }
211 
212 
213 void
214 TrajectoriesHandler::writeNormedSums(std::ostream& o, const std::string id, const SUMOReal factor) {
215  o << mySums[id].fuel / factor << ","
216  << mySums[id].electricity / factor << ","
217  << mySums[id].CO2 / factor << ","
218  << mySums[id].NOx / factor << ","
219  << mySums[id].CO / factor << ","
220  << mySums[id].HC / factor << ","
221  << mySums[id].PMx / factor << std::endl;
222 }
223 
224 
225 /****************************************************************************/
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
TrajectoriesHandler(const bool computeA, const bool computeAForward, const bool accelZeroCorrection, const SUMOEmissionClass defaultClass, const SUMOReal defaultSlope, std::ostream *stdOut, OutputDevice *xmlOut)
Constructor.
#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...
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
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.
~TrajectoriesHandler()
Destructor.
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.
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
std::map< std::string, SUMOReal > myLastV
std::map< std::string, PollutantsInterface::Emissions > mySums
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.