SUMO - Simulation of Urban MObility
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
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>
34
#include <
utils/common/UtilExceptions.h
>
35
#include <
utils/common/MsgHandler.h
>
36
#include <
utils/common/ToString.h
>
37
#include <
utils/emissions/PollutantsInterface.h
>
38
#include <
utils/iodevices/OutputDevice.h
>
39
#include <
utils/xml/SUMOSAXHandler.h
>
40
#include <
utils/xml/SUMOXMLDefinitions.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
57
TrajectoriesHandler::~TrajectoriesHandler
() {}
58
59
60
void
61
TrajectoriesHandler::myStartElement
(
int
element,
62
const
SUMOSAXAttributes
& attrs) {
63
bool
ok =
true
;
64
switch
(element) {
65
case
SUMO_TAG_TIMESTEP
:
66
myCurrentTime
= attrs.
getSUMOTimeReporting
(
SUMO_ATTR_TIME
, 0, ok);
67
break
;
68
case
SUMO_TAG_VEHICLE
:
69
if
(attrs.
hasAttribute
(
SUMO_ATTR_SPEED
)) {
70
writeEmissions
(std::cout, attrs.
getString
(
SUMO_ATTR_ID
),
myDefaultClass
,
myCurrentTime
, attrs.
getFloat
(
SUMO_ATTR_SPEED
));
71
}
else
{
72
myEmissionClassByVehicle
[attrs.
getString
(
SUMO_ATTR_ID
)] =
myEmissionClassByType
[attrs.
getString
(
SUMO_ATTR_ACTORCONFIG
)];
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
);
86
const
SUMOEmissionClass
c =
myEmissionClassByVehicle
[id];
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
104
const
PollutantsInterface::Emissions
105
TrajectoriesHandler::computeEmissions
(
const
std::string
id
,
const
SUMOEmissionClass
c,
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
}
121
const
PollutantsInterface::Emissions
result =
PollutantsInterface::computeAll
(c, v, a, s);
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
139
TrajectoriesHandler::writeXMLEmissions
(
const
std::string
id
,
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;
148
myXMLOut
->
openTag
(
SUMO_TAG_TIMESTEP
).
writeAttr
(
SUMO_ATTR_TIME
,
time2string
(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
/****************************************************************************/
tmp
buildd
sumo-0.21.0+dfsg
src
tools
TrajectoriesHandler.cpp
Generated on Thu Nov 20 2014 19:49:59 for SUMO - Simulation of Urban MObility by
1.8.1.2