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-sim.org/
10 // Copyright (C) 2014-2014 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>
41 #include "TrajectoriesHandler.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 TrajectoriesHandler::TrajectoriesHandler(const bool computeA, const SUMOEmissionClass defaultClass,
52  const SUMOReal defaultSlope, OutputDevice* xmlOut)
53  : SUMOSAXHandler(""), myComputeA(computeA), myDefaultClass(defaultClass),
54  myDefaultSlope(defaultSlope), myXMLOut(xmlOut), myCurrentTime(-1) {}
55 
56 
58 
59 
60 void
62  const SUMOSAXAttributes& attrs) {
63  bool ok = true;
64  switch (element) {
65  case SUMO_TAG_TIMESTEP:
67  break;
68  case SUMO_TAG_VEHICLE:
69  if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
71  } else {
73  }
74  break;
75  case SUMO_TAG_ACTORCONFIG: {
76  const std::string id = attrs.getString(SUMO_ATTR_ID);
77  const std::string vClass = attrs.getString(SUMO_ATTR_VEHICLECLASS);
78  const std::string fuel = attrs.getString(SUMO_ATTR_FUEL);
79  const std::string eClass = attrs.getString(SUMO_ATTR_EMISSIONCLASS);
80  const SUMOReal weight = attrs.getOpt<SUMOReal>(SUMO_ATTR_WEIGHT, id.c_str(), ok, 0.) * 10.;
81  myEmissionClassByType[id] = PollutantsInterface::getClass(myDefaultClass, vClass, fuel, eClass, weight);
82  break;
83  }
84  case SUMO_TAG_MOTIONSTATE: {
85  const std::string id = attrs.getString(SUMO_ATTR_VEHICLE);
87  const SUMOReal v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
88  const SUMOReal a = attrs.getOpt(SUMO_ATTR_ACCELERATION, id.c_str(), ok, INVALID_VALUE);
89  const SUMOReal s = attrs.getOpt(SUMO_ATTR_SLOPE, id.c_str(), ok, INVALID_VALUE);
90  const SUMOTime time = attrs.getOpt<int>(SUMO_ATTR_TIME, id.c_str(), ok, INVALID_VALUE);
91  if (myXMLOut != 0) {
92  writeXMLEmissions(id, c, time, v, a, s);
93  } else {
94  writeEmissions(std::cout, id, c, time, v, a, s);
95  }
96  break;
97  }
98  default:
99  break;
100  }
101 }
102 
103 
106  const SUMOReal v, SUMOReal& a, SUMOReal& s) {
107  if (myComputeA) {
108  if (myLastV.count(id) == 0) {
109  a = 0.;
110  } else {
111  a = v - myLastV[id];
112  }
113  myLastV[id] = v;
114  }
115  if (a == INVALID_VALUE) {
116  throw ProcessError("Acceleration information is missing; try running with --compute-a.");
117  }
118  if (s == INVALID_VALUE) {
119  s = myDefaultSlope;
120  }
122  mySums[id].addScaled(result);
123  return result;
124 }
125 
126 
127 void
128 TrajectoriesHandler::writeEmissions(std::ostream& o, const std::string id,
129  const SUMOEmissionClass c,
130  const SUMOReal t, const SUMOReal v,
131  SUMOReal a, SUMOReal s) {
132  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
133  o << t << ";" << v << ";" << a << ";" << s
134  << ";" << e.CO << ";" << e.CO2 << ";" << e.HC << ";" << e.PMx << ";" << e.NOx << ";" << e.fuel << std::endl;
135 }
136 
137 
138 void
140  const SUMOEmissionClass c,
141  const SUMOTime t, const SUMOReal v,
142  SUMOReal a, SUMOReal s) {
143  if (myCurrentTime != t) {
144  if (myCurrentTime != -1) {
145  myXMLOut->closeTag();
146  }
147  myCurrentTime = t;
149  }
150  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
151  myXMLOut->openTag("vehicle").writeAttr("id", id).writeAttr("eclass", PollutantsInterface::getName(c));
152  myXMLOut->writeAttr("CO2", e.CO2).writeAttr("CO", e.CO).writeAttr("HC", e.HC).writeAttr("NOx", e.NOx);
153  myXMLOut->writeAttr("PMx", e.PMx).writeAttr("fuel", e.fuel);
154  myXMLOut->writeAttr("speed", v).closeTag();
155 }
156 
157 
158 void
159 TrajectoriesHandler::writeSums(std::ostream& o, const std::string id) {
160  o << "CO:" << mySums[id].CO << std::endl
161  << "CO2:" << mySums[id].CO2 << std::endl
162  << "HC:" << mySums[id].HC << std::endl
163  << "NOx:" << mySums[id].NOx << std::endl
164  << "PMx:" << mySums[id].PMx << std::endl
165  << "fuel:" << mySums[id].fuel << std::endl;
166 }
167 
168 
169 /****************************************************************************/
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
TrajectoriesHandler(const bool computeA, const SUMOEmissionClass defaultClass, const SUMOReal defaultSlope, OutputDevice *xmlOut)
Constructor.
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
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...
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.
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.
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)
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:215
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, SUMOReal > myLastV
std::map< std::string, PollutantsInterface::Emissions > mySums
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.