Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TrajectoriesHandler.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2014-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// An XML-Handler for amitran and netstate trajectories
19/****************************************************************************/
20#include <config.h>
21
22#include <string>
23#include <utility>
24#include <iostream>
34#include "TrajectoriesHandler.h"
35
36
37// ===========================================================================
38// method definitions
39// ===========================================================================
40TrajectoriesHandler::TrajectoriesHandler(const bool computeA, const bool computeAForward,
41 const bool accelZeroCorrection, const SUMOEmissionClass defaultClass, EnergyParams* params,
42 long long int attributes, const double defaultSlope, std::ostream* stdOut, OutputDevice* xmlOut) :
44 myComputeA(computeA),
45 myComputeAForward(computeAForward),
46 myAccelZeroCorrection(accelZeroCorrection),
47 myDefaultClass(defaultClass),
48 myParams(params), myAttributes(attributes),
49 myDefaultSlope(defaultSlope), myStdOut(stdOut), myXMLOut(xmlOut), myCurrentTime(-1), myStepSize(TS) {}
50
51
53
54
55void
57 const SUMOSAXAttributes& attrs) {
58 bool ok = true;
59 switch (element) {
61 myStepSize = attrs.getFloat("timeStepSize") / 1000.;
62 break;
65 break;
67 if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
68 double v = attrs.getFloat(SUMO_ATTR_SPEED);
69 double a = INVALID_VALUE;
70 double s = INVALID_VALUE;
72 } else {
73 const std::string acId = attrs.getString(SUMO_ATTR_ACTORCONFIG);
74 const std::string id = attrs.getString(SUMO_ATTR_ID);
75 if (myEmissionClassByType.count(acId) == 0) {
76 WRITE_WARNINGF(TL("Unknown actor configuration '%' for vehicle '%'!"), acId, id);
77 } else {
79 }
80 }
81 break;
83 const std::string id = attrs.getString(SUMO_ATTR_ID);
84 const std::string vClass = attrs.getString(SUMO_ATTR_VEHICLECLASS);
85 const std::string fuel = attrs.getString(SUMO_ATTR_FUEL);
86 const std::string eClass = attrs.getString(SUMO_ATTR_EMISSIONCLASS);
87 const double weight = attrs.getOpt<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok, 0.) * 10.;
88 myEmissionClassByType[id] = PollutantsInterface::getClass(myDefaultClass, vClass, fuel, eClass, weight);
89 break;
90 }
92 const std::string id = attrs.getString(SUMO_ATTR_VEHICLE);
93 if (myEmissionClassByVehicle.count(id) == 0) {
94 WRITE_WARNINGF(TL("Motion state for unknown vehicle '%'!"), id);
96 }
98 double v = attrs.getFloat(SUMO_ATTR_SPEED) / 100.;
99 double a = attrs.hasAttribute(SUMO_ATTR_ACCELERATION) ? attrs.get<double>(SUMO_ATTR_ACCELERATION, id.c_str(), ok) / 1000. : INVALID_VALUE;
100 double s = attrs.hasAttribute(SUMO_ATTR_SLOPE) ? RAD2DEG(asin(attrs.get<double>(SUMO_ATTR_SLOPE, id.c_str(), ok) / 10000.)) : INVALID_VALUE;
101 const SUMOTime time = attrs.getOpt<int>(SUMO_ATTR_TIME, id.c_str(), ok, INVALID_VALUE);
102 if (myXMLOut != nullptr) {
103 writeXMLEmissions(id, c, nullptr, time, v, a, s);
104 }
105 if (myStdOut != nullptr) {
106 writeEmissions(*myStdOut, id, c, nullptr, myAttributes, STEPS2TIME(time), v, a, s);
107 }
108 break;
109 }
110 default:
111 break;
112 }
113}
114
115
118 EnergyParams* params,
119 double& v, double& a, double& s) {
120
121 if (myComputeA) {
122 if (myLastV.count(id) == 0) {
123 a = 0.;
124 } else {
125 a = v - myLastV[id];
126 }
127 myLastV[id] = v;
128 if (myComputeAForward) {
129 v -= a;
130 }
131 }
133 a = PollutantsInterface::getModifiedAccel(c, v, a, s, params);
134 }
135 if (a == INVALID_VALUE) {
136 throw ProcessError(TL("Acceleration information is missing; try running with --compute-a."));
137 }
138 if (s == INVALID_VALUE) {
139 s = myDefaultSlope;
140 }
141 const PollutantsInterface::Emissions result = PollutantsInterface::computeAll(c, v, a, s, params);
142 mySums[id].addScaled(result, myStepSize);
143 if (id != "") {
144 mySums[""].addScaled(result, myStepSize);
145 }
146 return result;
147}
148
149
150void
151TrajectoriesHandler::writeOptional(std::ostream& o, long long int attributes, const SumoXMLAttr attr, double v) {
152 if ((attributes & ((long long int)1 << attr)) != 0) {
153 o << ";" << v;
154 }
155}
156
157
158bool
159TrajectoriesHandler::writeEmissions(std::ostream& o, const std::string id,
160 const SUMOEmissionClass c,
161 EnergyParams* params,
162 long long int attributes,
163 double t, double& v,
164 double& a, double& s) {
165 if (myComputeA && myLastV.count(id) == 0) {
166 myLastV[id] = v;
167 myLastSlope[id] = s;
168 return false;
169 }
170 if (myComputeAForward) {
171 t -= TS;
172 const double nextS = s;
173 s = myLastSlope[id];
174 myLastSlope[id] = nextS;
175 }
176 const PollutantsInterface::Emissions e = computeEmissions(id, c, params, v, a, s);
177 o << t;
178 writeOptional(o, attributes, SUMO_ATTR_SPEED, v);
179 writeOptional(o, attributes, SUMO_ATTR_ACCELERATION, a);
180 writeOptional(o, attributes, SUMO_ATTR_SLOPE, s);
181 writeOptional(o, attributes, SUMO_ATTR_CO_ABS, e.CO);
182 writeOptional(o, attributes, SUMO_ATTR_CO2_ABS, e.CO2);
183 writeOptional(o, attributes, SUMO_ATTR_HC_ABS, e.HC);
184 writeOptional(o, attributes, SUMO_ATTR_PMX_ABS, e.PMx);
185 writeOptional(o, attributes, SUMO_ATTR_NOX_ABS, e.NOx);
186 writeOptional(o, attributes, SUMO_ATTR_FUEL_ABS, e.fuel);
188 writeOptional(o, attributes, SUMO_ATTR_AMOUNT, PollutantsInterface::getCoastingDecel(c, v, a, s, params));
189 o << std::endl;
190 return true;
191}
192
193
194bool
196 const SUMOEmissionClass c,
197 EnergyParams* params,
198 SUMOTime t, double& v,
199 double a, double s) {
200 if (myComputeA && myLastV.count(id) == 0) {
201 myLastV[id] = v;
202 return false;
203 }
204 if (myCurrentTime != t) {
205 if (myCurrentTime != -1) {
207 }
208 myCurrentTime = t;
210 }
211 const PollutantsInterface::Emissions e = computeEmissions(id, c, params, v, a, s);
212 myXMLOut->openTag("vehicle").writeAttr("id", id).writeAttr("eclass", PollutantsInterface::getName(c));
213 myXMLOut->writeAttr("CO2", e.CO2).writeAttr("CO", e.CO).writeAttr("HC", e.HC).writeAttr("NOx", e.NOx);
214 myXMLOut->writeAttr("PMx", e.PMx).writeAttr("fuel", e.fuel).writeAttr("electricity", e.electricity);
215 myXMLOut->writeAttr("speed", v).closeTag();
216 return true;
217}
218
219
220void
221TrajectoriesHandler::writeSums(std::ostream& o, const std::string id) {
222 o << "CO:" << mySums[id].CO << std::endl
223 << "CO2:" << mySums[id].CO2 << std::endl
224 << "HC:" << mySums[id].HC << std::endl
225 << "NOx:" << mySums[id].NOx << std::endl
226 << "PMx:" << mySums[id].PMx << std::endl
227 << "fuel:" << mySums[id].fuel << std::endl
228 << "electricity:" << mySums[id].electricity << std::endl;
229}
230
231
232void
233TrajectoriesHandler::writeNormedSums(std::ostream& o, const std::string id, const double factor) {
234 o << mySums[id].fuel / factor << ","
235 << mySums[id].electricity / factor << ","
236 << mySums[id].CO2 / factor << ","
237 << mySums[id].NOx / factor << ","
238 << mySums[id].CO / factor << ","
239 << mySums[id].HC / factor << ","
240 << mySums[id].PMx / factor << std::endl;
241}
242
243
244/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define RAD2DEG(x)
Definition GeomHelper.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define TL(string)
Definition MsgHandler.h:315
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define TS
Definition SUMOTime.h:42
int SUMOEmissionClass
@ SUMO_TAG_TIMESTEP
@ SUMO_TAG_VEHICLE
description of a vehicle
@ SUMO_TAG_TRAJECTORIES
@ SUMO_TAG_MOTIONSTATE
@ SUMO_TAG_ACTORCONFIG
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_NOX_ABS
@ SUMO_ATTR_EMISSIONCLASS
@ SUMO_ATTR_SPEED
@ SUMO_ATTR_ELECTRICITY_ABS
@ SUMO_ATTR_PMX_ABS
@ SUMO_ATTR_FUEL
@ SUMO_ATTR_WEIGHT
@ SUMO_ATTR_VEHICLECLASS
@ SUMO_ATTR_ACTORCONFIG
@ SUMO_ATTR_CO_ABS
MSMeanData_Emissions.
@ SUMO_ATTR_FUEL_ABS
@ SUMO_ATTR_SLOPE
@ SUMO_ATTR_ACCELERATION
@ SUMO_ATTR_VEHICLE
@ SUMO_ATTR_HC_ABS
@ SUMO_ATTR_ID
@ SUMO_ATTR_AMOUNT
MSMeanData_Amitran.
@ SUMO_ATTR_CO2_ABS
@ SUMO_ATTR_TIME
trigger: the time of the step
An upper class for objects with additional parameters.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static double getModifiedAccel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param)
Returns the adapted acceleration value, useful for comparing with external PHEMlight references.
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
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 double getCoastingDecel(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param)
Returns the coasting deceleration value, useful for comparing with external PHEMlight references.
static Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
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.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
SAX-handler base for SUMO-files.
std::map< std::string, double > myLastSlope
const SUMOEmissionClass myDefaultClass
void writeNormedSums(std::ostream &o, const std::string id, const double factor)
void writeOptional(std::ostream &o, long long int attributes, const SumoXMLAttr attr, double v)
std::map< std::string, PollutantsInterface::Emissions > mySums
void writeSums(std::ostream &o, const std::string id)
static const int INVALID_VALUE
bool writeXMLEmissions(const std::string id, const SUMOEmissionClass c, EnergyParams *params, SUMOTime t, double &v, double a=INVALID_VALUE, double s=INVALID_VALUE)
std::map< std::string, SUMOEmissionClass > myEmissionClassByVehicle
std::map< std::string, SUMOEmissionClass > myEmissionClassByType
std::map< std::string, double > myLastV
const PollutantsInterface::Emissions computeEmissions(const std::string id, const SUMOEmissionClass c, EnergyParams *params, double &v, double &a, double &s)
TrajectoriesHandler(const bool computeA, const bool computeAForward, const bool accelZeroCorrection, const SUMOEmissionClass defaultClass, EnergyParams *params, long long int attributes, const double defaultSlope, std::ostream *stdOut, OutputDevice *xmlOut)
Constructor.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called when an opening-tag occurs.
bool writeEmissions(std::ostream &o, const std::string id, const SUMOEmissionClass c, EnergyParams *params, long long int attributes, double t, double &v, double &a, double &s)
Storage for collected values of all emission types.