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-2017 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 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 TrajectoriesHandler::TrajectoriesHandler(const bool computeA, const bool computeAForward,
49  const bool accelZeroCorrection, const SUMOEmissionClass defaultClass,
50  const double defaultSlope, std::ostream* stdOut, OutputDevice* xmlOut)
51  : SUMOSAXHandler(""), myComputeA(computeA), myComputeAForward(computeAForward), myAccelZeroCorrection(accelZeroCorrection), myDefaultClass(defaultClass),
52  myDefaultSlope(defaultSlope), myStdOut(stdOut), myXMLOut(xmlOut), myCurrentTime(-1), myStepSize(TS) {}
53 
54 
56 
57 
58 void
60  const SUMOSAXAttributes& attrs) {
61  bool ok = true;
62  switch (element) {
64  myStepSize = attrs.getFloat("timeStepSize") / 1000.;
65  break;
66  case SUMO_TAG_TIMESTEP:
68  break;
69  case SUMO_TAG_VEHICLE:
70  if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
71  double v = attrs.getFloat(SUMO_ATTR_SPEED);
72  double a = INVALID_VALUE;
73  double s = INVALID_VALUE;
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 double weight = attrs.getOpt<double>(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  double v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
102  double a = attrs.hasAttribute(SUMO_ATTR_ACCELERATION) ? attrs.get<double>(SUMO_ATTR_ACCELERATION, id.c_str(), ok) / 1000. : INVALID_VALUE;
103  double s = attrs.hasAttribute(SUMO_ATTR_SLOPE) ? RAD2DEG(asin(attrs.get<double>(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, STEPS2TIME(time), v, a, s);
110  }
111  break;
112  }
113  default:
114  break;
115  }
116 }
117 
118 
121  double& v, double& a, double& s) {
122 
123  if (myComputeA) {
124  if (myLastV.count(id) == 0) {
125  a = 0.;
126  } else {
127  a = v - myLastV[id];
128  }
129  myLastV[id] = v;
130  if (myComputeAForward) {
131  v -= a;
132  }
133  }
134  if (myAccelZeroCorrection && v == 0.) {
135  a = 0.;
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  double t, double& v,
156  double& a, double& 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, double& v,
176  double a, double 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 double 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 /****************************************************************************/
void writeNormedSums(std::ostream &o, const std::string id, const double factor)
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
static Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const std::map< int, double > *param=0)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
TrajectoriesHandler(const bool computeA, const bool computeAForward, const bool accelZeroCorrection, const SUMOEmissionClass defaultClass, const double defaultSlope, std::ostream *stdOut, OutputDevice *xmlOut)
Constructor.
const SUMOEmissionClass myDefaultClass
std::map< std::string, SUMOEmissionClass > myEmissionClassByType
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
Storage for collected values of all emission types.
#define RAD2DEG(x)
Definition: GeomHelper.h:46
#define TS
Definition: SUMOTime.h:52
const PollutantsInterface::Emissions computeEmissions(const std::string id, const SUMOEmissionClass c, double &v, double &a, double &s)
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.
#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
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.
bool writeXMLEmissions(const std::string id, const SUMOEmissionClass c, SUMOTime t, double &v, double a=INVALID_VALUE, double s=INVALID_VALUE)
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
~TrajectoriesHandler()
Destructor.
std::map< std::string, double > myLastV
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
trigger: the time of the step
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)
description of a vehicle
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:52
bool writeEmissions(std::ostream &o, const std::string id, const SUMOEmissionClass c, double t, double &v, double &a, double &s)
std::map< std::string, PollutantsInterface::Emissions > mySums
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.