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