SUMO - Simulation of Urban MObility
TrajectoriesHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2014-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // An XML-Handler for amitran and netstate trajectories
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <string>
31 #include <utility>
32 #include <iostream>
35 #include <utils/common/ToString.h>
37 #include <utils/geom/GeomHelper.h>
41 #include "TrajectoriesHandler.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 TrajectoriesHandler::TrajectoriesHandler(const bool computeA, const bool computeAForward,
48  const bool accelZeroCorrection, const SUMOEmissionClass defaultClass,
49  const double defaultSlope, std::ostream* stdOut, OutputDevice* xmlOut)
50  : SUMOSAXHandler(""), myComputeA(computeA), myComputeAForward(computeAForward), myAccelZeroCorrection(accelZeroCorrection), myDefaultClass(defaultClass),
51  myDefaultSlope(defaultSlope), myStdOut(stdOut), myXMLOut(xmlOut), myCurrentTime(-1), myStepSize(TS) {}
52 
53 
55 
56 
57 void
59  const SUMOSAXAttributes& attrs) {
60  bool ok = true;
61  switch (element) {
63  myStepSize = attrs.getFloat("timeStepSize") / 1000.;
64  break;
65  case SUMO_TAG_TIMESTEP:
67  break;
68  case SUMO_TAG_VEHICLE:
69  if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
70  double v = attrs.getFloat(SUMO_ATTR_SPEED);
71  double a = INVALID_VALUE;
72  double s = INVALID_VALUE;
74  } else {
75  const std::string acId = attrs.getString(SUMO_ATTR_ACTORCONFIG);
76  const std::string id = attrs.getString(SUMO_ATTR_ID);
77  if (myEmissionClassByType.count(acId) == 0) {
78  WRITE_WARNING("Unknown actor configuration '" + acId + "' for vehicle '" + id + "'!");
79  } else {
81  }
82  }
83  break;
84  case SUMO_TAG_ACTORCONFIG: {
85  const std::string id = attrs.getString(SUMO_ATTR_ID);
86  const std::string vClass = attrs.getString(SUMO_ATTR_VEHICLECLASS);
87  const std::string fuel = attrs.getString(SUMO_ATTR_FUEL);
88  const std::string eClass = attrs.getString(SUMO_ATTR_EMISSIONCLASS);
89  const double weight = attrs.getOpt<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok, 0.) * 10.;
90  myEmissionClassByType[id] = PollutantsInterface::getClass(myDefaultClass, vClass, fuel, eClass, weight);
91  break;
92  }
93  case SUMO_TAG_MOTIONSTATE: {
94  const std::string id = attrs.getString(SUMO_ATTR_VEHICLE);
95  if (myEmissionClassByVehicle.count(id) == 0) {
96  WRITE_WARNING("Motion state for unknown vehicle '" + id + "'!");
98  }
100  double v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
101  double a = attrs.hasAttribute(SUMO_ATTR_ACCELERATION) ? attrs.get<double>(SUMO_ATTR_ACCELERATION, id.c_str(), ok) / 1000. : INVALID_VALUE;
102  double s = attrs.hasAttribute(SUMO_ATTR_SLOPE) ? RAD2DEG(asin(attrs.get<double>(SUMO_ATTR_SLOPE, id.c_str(), ok) / 10000.)) : INVALID_VALUE;
103  const SUMOTime time = attrs.getOpt<int>(SUMO_ATTR_TIME, id.c_str(), ok, INVALID_VALUE);
104  if (myXMLOut != 0) {
105  writeXMLEmissions(id, c, time, v, a, s);
106  }
107  if (myStdOut != 0) {
108  writeEmissions(*myStdOut, id, c, STEPS2TIME(time), v, a, s);
109  }
110  break;
111  }
112  default:
113  break;
114  }
115 }
116 
117 
120  double& v, double& a, double& s) {
121 
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  if (myComputeAForward) {
130  v -= a;
131  }
132  }
133  if (myAccelZeroCorrection) {
135  }
136  if (a == INVALID_VALUE) {
137  throw ProcessError("Acceleration information is missing; try running with --compute-a.");
138  }
139  if (s == INVALID_VALUE) {
140  s = myDefaultSlope;
141  }
143  mySums[id].addScaled(result, myStepSize);
144  if (id != "") {
145  mySums[""].addScaled(result, myStepSize);
146  }
147  return result;
148 }
149 
150 
151 bool
152 TrajectoriesHandler::writeEmissions(std::ostream& o, const std::string id,
153  const SUMOEmissionClass c,
154  double t, double& v,
155  double& a, double& s) {
156  if (myComputeA && myLastV.count(id) == 0) {
157  myLastV[id] = v;
158  myLastSlope[id] = s;
159  return false;
160  }
161  if (myComputeAForward) {
162  t -= TS;
163  const double nextS = s;
164  s = myLastSlope[id];
165  myLastSlope[id] = nextS;
166  }
167  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
168  o << t << ";" << v << ";" << a << ";" << s << ";"
169  << e.CO << ";" << e.CO2 << ";" << e.HC << ";" << e.PMx << ";"
170  << e.NOx << ";" << e.fuel << ";" << e.electricity << std::endl;
171  return true;
172 }
173 
174 
175 bool
177  const SUMOEmissionClass c,
178  SUMOTime t, double& v,
179  double a, double s) {
180  if (myComputeA && myLastV.count(id) == 0) {
181  myLastV[id] = v;
182  return false;
183  }
184  if (myCurrentTime != t) {
185  if (myCurrentTime != -1) {
186  myXMLOut->closeTag();
187  }
188  myCurrentTime = t;
190  }
191  const PollutantsInterface::Emissions e = computeEmissions(id, c, v, a, s);
192  myXMLOut->openTag("vehicle").writeAttr("id", id).writeAttr("eclass", PollutantsInterface::getName(c));
193  myXMLOut->writeAttr("CO2", e.CO2).writeAttr("CO", e.CO).writeAttr("HC", e.HC).writeAttr("NOx", e.NOx);
194  myXMLOut->writeAttr("PMx", e.PMx).writeAttr("fuel", e.fuel).writeAttr("electricity", e.electricity);
195  myXMLOut->writeAttr("speed", v).closeTag();
196  return true;
197 }
198 
199 
200 void
201 TrajectoriesHandler::writeSums(std::ostream& o, const std::string id) {
202  o << "CO:" << mySums[id].CO << std::endl
203  << "CO2:" << mySums[id].CO2 << std::endl
204  << "HC:" << mySums[id].HC << std::endl
205  << "NOx:" << mySums[id].NOx << std::endl
206  << "PMx:" << mySums[id].PMx << std::endl
207  << "fuel:" << mySums[id].fuel << std::endl
208  << "electricity:" << mySums[id].electricity << std::endl;
209 }
210 
211 
212 void
213 TrajectoriesHandler::writeNormedSums(std::ostream& o, const std::string id, const double factor) {
214  o << mySums[id].fuel / factor << ","
215  << mySums[id].electricity / factor << ","
216  << mySums[id].CO2 / factor << ","
217  << mySums[id].NOx / factor << ","
218  << mySums[id].CO / factor << ","
219  << mySums[id].HC / factor << ","
220  << mySums[id].PMx / factor << std::endl;
221 }
222 
223 
224 /****************************************************************************/
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:260
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:59
Storage for collected values of all emission types.
#define RAD2DEG(x)
Definition: GeomHelper.h:45
static double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope)
Returns the adapted acceleration value, useful for comparing with external PHEMlight references...
#define TS
Definition: SUMOTime.h:51
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:199
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:64
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
std::map< std::string, double > myLastSlope
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:70
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:51
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.