Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 : /****************************************************************************/
14 : /// @file MSMeanData.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Laura Bieker
19 : /// @author Leonhard Luecken
20 : /// @date Mon, 10.05.2004
21 : ///
22 : // Data collector for edges/lanes
23 : /****************************************************************************/
24 : #include <config.h>
25 :
26 : #include <limits>
27 : #ifdef HAVE_FOX
28 : #include <utils/common/ScopedLocker.h>
29 : #endif
30 : #include <utils/common/SUMOTime.h>
31 : #include <utils/common/ToString.h>
32 : #include <utils/common/StringTokenizer.h>
33 : #include <utils/iodevices/OutputDevice.h>
34 : #include <microsim/MSEdgeControl.h>
35 : #include <microsim/MSEdge.h>
36 : #include <microsim/MSLane.h>
37 : #include <microsim/MSVehicle.h>
38 : #include <microsim/cfmodels/MSCFModel.h>
39 : #include <microsim/MSNet.h>
40 : #include "MSMeanData_Amitran.h"
41 : #include "MSMeanData.h"
42 :
43 : #include <microsim/MSGlobals.h>
44 : #include <mesosim/MESegment.h>
45 : #include <mesosim/MELoop.h>
46 :
47 :
48 : // ===========================================================================
49 : // debug constants
50 : // ===========================================================================
51 : //#define DEBUG_NOTIFY_MOVE
52 : //#define DEBUG_NOTIFY_ENTER
53 :
54 : // ===========================================================================
55 : // method definitions
56 : // ===========================================================================
57 : // ---------------------------------------------------------------------------
58 : // MSMeanData::MeanDataValues - methods
59 : // ---------------------------------------------------------------------------
60 9135209 : MSMeanData::MeanDataValues::MeanDataValues(
61 : MSLane* const lane, const double length, const bool doAdd,
62 9135209 : const MSMeanData* const parent) :
63 18270418 : MSMoveReminder("meandata_" + (parent == nullptr ? "" : parent->getID() + "|") + (lane == nullptr ? "NULL" : lane->getID()), lane, doAdd),
64 9135209 : myParent(parent),
65 9135209 : myLaneLength(length),
66 9135209 : sampleSeconds(0),
67 27405627 : travelledDistance(0) { }
68 :
69 :
70 9063695 : MSMeanData::MeanDataValues::~MeanDataValues() {
71 9063695 : }
72 :
73 :
74 : bool
75 693115 : MSMeanData::MeanDataValues::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane) {
76 : #ifdef DEBUG_NOTIFY_ENTER
77 : std::cout << "\n" << SIMTIME << " MSMeanData_Net::MSLaneMeanDataValues: veh '" << veh.getID() << "' enters lane '" << enteredLane->getID() << "'" << std::endl;
78 : #else
79 : UNUSED_PARAMETER(enteredLane);
80 : #endif
81 : UNUSED_PARAMETER(reason);
82 693115 : return myParent == nullptr || myParent->vehicleApplies(veh);
83 : }
84 :
85 :
86 : bool
87 485295296 : MSMeanData::MeanDataValues::notifyMove(SUMOTrafficObject& veh, double oldPos, double newPos, double newSpeed) {
88 : // if the vehicle has arrived, the reminder must be kept so it can be
89 : // notified of the arrival subsequently
90 485295296 : const double oldSpeed = veh.getPreviousSpeed();
91 485295296 : double enterSpeed = MSGlobals::gSemiImplicitEulerUpdate ? newSpeed : oldSpeed; // NOTE: For the euler update, the vehicle is assumed to travel at constant speed for the whole time step
92 : double leaveSpeed = newSpeed, leaveSpeedFront = newSpeed;
93 :
94 : // These values will be further decreased below
95 485295296 : double timeOnLane = TS;
96 485295296 : double frontOnLane = oldPos > myLaneLength ? 0. : TS;
97 : bool ret = true;
98 :
99 : // entry and exit times (will be modified below)
100 : double timeBeforeEnter = 0.;
101 : double timeBeforeEnterBack = 0.;
102 485295296 : double timeBeforeLeaveFront = newPos <= myLaneLength ? TS : 0.;
103 : double timeBeforeLeave = TS;
104 :
105 : // Treat the case that the vehicle entered the lane in the last step
106 485295296 : if (oldPos < 0 && newPos >= 0) {
107 : // Vehicle was not on this lane in the last time step
108 12211906 : timeBeforeEnter = MSCFModel::passingTime(oldPos, 0, newPos, oldSpeed, newSpeed);
109 12211906 : timeOnLane = TS - timeBeforeEnter;
110 : frontOnLane = timeOnLane;
111 12211906 : enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
112 : }
113 :
114 485295296 : const double oldBackPos = oldPos - veh.getVehicleType().getLength();
115 485295296 : const double newBackPos = newPos - veh.getVehicleType().getLength();
116 :
117 : // Determine the time before the vehicle back enters
118 485295296 : if (oldBackPos < 0. && newBackPos > 0.) {
119 12506529 : timeBeforeEnterBack = MSCFModel::passingTime(oldBackPos, 0., newBackPos, oldSpeed, newSpeed);
120 472788767 : } else if (newBackPos <= 0) {
121 8902757 : timeBeforeEnterBack = TS;
122 : } else {
123 : timeBeforeEnterBack = 0.;
124 : }
125 :
126 : // Treat the case that the vehicle's back left the lane in the last step
127 485295296 : if (newBackPos > myLaneLength // vehicle's back has left the lane
128 14240268 : && oldBackPos <= myLaneLength) { // and hasn't left the lane before
129 : assert(!MSGlobals::gSemiImplicitEulerUpdate || newSpeed != 0); // how could it move across the lane boundary otherwise
130 : // (Leo) vehicle left this lane (it can also have skipped over it in one time step -> therefore we use "timeOnLane -= ..." and ( ... - timeOnLane) below)
131 14240268 : timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myLaneLength, newBackPos, oldSpeed, newSpeed);
132 14240268 : const double timeAfterLeave = TS - timeBeforeLeave;
133 14240268 : timeOnLane -= timeAfterLeave;
134 14240268 : leaveSpeed = MSCFModel::speedAfterTime(timeBeforeLeave, oldSpeed, newPos - oldPos);
135 : // XXX: Do we really need this? Why would this "reduce rounding errors"? (Leo) Refs. #2579
136 14240268 : if (fabs(timeOnLane) < NUMERICAL_EPS) { // reduce rounding errors
137 : timeOnLane = 0.;
138 : }
139 14240268 : ret = veh.hasArrived();
140 : }
141 :
142 : // Treat the case that the vehicle's front left the lane in the last step
143 485295296 : if (newPos > myLaneLength && oldPos <= myLaneLength) {
144 : // vehicle's front has left the lane and has not left before
145 : assert(!MSGlobals::gSemiImplicitEulerUpdate || newSpeed != 0);
146 15658714 : timeBeforeLeaveFront = MSCFModel::passingTime(oldPos, myLaneLength, newPos, oldSpeed, newSpeed);
147 15658714 : const double timeAfterLeave = TS - timeBeforeLeaveFront;
148 15658714 : frontOnLane -= timeAfterLeave;
149 : // XXX: Do we really need this? Why would this "reduce rounding errors"? (Leo) Refs. #2579
150 15658714 : if (fabs(frontOnLane) < NUMERICAL_EPS) { // reduce rounding errors
151 : frontOnLane = 0.;
152 : }
153 15658714 : leaveSpeedFront = MSCFModel::speedAfterTime(timeBeforeLeaveFront, oldSpeed, newPos - oldPos);
154 : }
155 :
156 : assert(frontOnLane <= TS);
157 : assert(timeOnLane <= TS);
158 :
159 485295296 : if (timeOnLane < 0) {
160 0 : WRITE_ERRORF(TL("Negative vehicle step fraction for '%' on lane '%'."), veh.getID(), getLane()->getID());
161 0 : return veh.hasArrived();
162 : }
163 485295296 : if (timeOnLane == 0) {
164 22474 : return veh.hasArrived();
165 : }
166 :
167 : #ifdef DEBUG_NOTIFY_MOVE
168 : std::stringstream ss;
169 : ss << "\n"
170 : << "lane length: " << myLaneLength
171 : << "\noldPos: " << oldPos
172 : << "\nnewPos: " << newPos
173 : << "\noldPosBack: " << oldBackPos
174 : << "\nnewPosBack: " << newBackPos
175 : << "\ntimeBeforeEnter: " << timeBeforeEnter
176 : << "\ntimeBeforeEnterBack: " << timeBeforeEnterBack
177 : << "\ntimeBeforeLeaveFront: " << timeBeforeLeaveFront
178 : << "\ntimeBeforeLeave: " << timeBeforeLeave;
179 : if (!(timeBeforeLeave >= MAX2(timeBeforeEnterBack, timeBeforeLeaveFront))
180 : || !(timeBeforeEnter <= MIN2(timeBeforeEnterBack, timeBeforeLeaveFront))) {
181 : WRITE_ERROR(ss.str());
182 : } else {
183 : std::cout << ss.str() << std::endl;
184 : }
185 :
186 : #endif
187 :
188 : assert(timeBeforeEnter <= MIN2(timeBeforeEnterBack, timeBeforeLeaveFront));
189 : assert(timeBeforeLeave >= MAX2(timeBeforeEnterBack, timeBeforeLeaveFront));
190 : // compute average vehicle length on lane in last step
191 485272822 : double vehLength = veh.getVehicleType().getLength();
192 : // occupied lane length at timeBeforeEnter (resp. stepStart if already on lane)
193 485272822 : double lengthOnLaneAtStepStart = MAX2(0., MIN4(myLaneLength, vehLength, vehLength - (oldPos - myLaneLength), oldPos));
194 : // occupied lane length at timeBeforeLeave (resp. stepEnd if still on lane)
195 485272822 : double lengthOnLaneAtStepEnd = MAX2(0., MIN4(myLaneLength, vehLength, vehLength - (newPos - myLaneLength), newPos));
196 : double integratedLengthOnLane = 0.;
197 485272822 : if (timeBeforeEnterBack < timeBeforeLeaveFront) {
198 : // => timeBeforeLeaveFront>0, myLaneLength>vehLength
199 : // vehicle length on detector at timeBeforeEnterBack
200 468085934 : double lengthOnLaneAtBackEnter = MIN2(veh.getVehicleType().getLength(), newPos);
201 : // linear quadrature of occupancy between timeBeforeEnter and timeBeforeEnterBack
202 468085934 : integratedLengthOnLane += (timeBeforeEnterBack - timeBeforeEnter) * (lengthOnLaneAtBackEnter + lengthOnLaneAtStepStart) * 0.5;
203 : // linear quadrature of occupancy between timeBeforeEnterBack and timeBeforeLeaveFront
204 : // (vehicle is completely on the edge in between)
205 468085934 : integratedLengthOnLane += (timeBeforeLeaveFront - timeBeforeEnterBack) * vehLength;
206 : // and until vehicle leaves/stepEnd
207 468085934 : integratedLengthOnLane += (timeBeforeLeave - timeBeforeLeaveFront) * (vehLength + lengthOnLaneAtStepEnd) * 0.5;
208 17186888 : } else if (timeBeforeEnterBack >= timeBeforeLeaveFront) {
209 : // => myLaneLength <= vehLength or (timeBeforeLeaveFront == timeBeforeEnterBack == 0)
210 : // vehicle length on detector at timeBeforeLeaveFront
211 : double lengthOnLaneAtLeaveFront;
212 17186888 : if (timeBeforeLeaveFront == timeBeforeEnter) {
213 : // for the case that front already left
214 : lengthOnLaneAtLeaveFront = lengthOnLaneAtStepStart;
215 8926665 : } else if (timeBeforeLeaveFront == timeBeforeLeave) {
216 : // for the case that front doesn't leave in this step
217 : lengthOnLaneAtLeaveFront = lengthOnLaneAtStepEnd;
218 : } else {
219 : lengthOnLaneAtLeaveFront = myLaneLength;
220 : }
221 : #ifdef DEBUG_NOTIFY_MOVE
222 : std::cout << "lengthOnLaneAtLeaveFront=" << lengthOnLaneAtLeaveFront << std::endl;
223 : #endif
224 : // linear quadrature of occupancy between timeBeforeEnter and timeBeforeLeaveFront
225 17186888 : integratedLengthOnLane += (timeBeforeLeaveFront - timeBeforeEnter) * (lengthOnLaneAtLeaveFront + lengthOnLaneAtStepStart) * 0.5;
226 : // linear quadrature of occupancy between timeBeforeLeaveFront and timeBeforeEnterBack
227 17186888 : integratedLengthOnLane += (timeBeforeEnterBack - timeBeforeLeaveFront) * lengthOnLaneAtLeaveFront;
228 : // and until vehicle leaves/stepEnd
229 17186888 : integratedLengthOnLane += (timeBeforeLeave - timeBeforeEnterBack) * (lengthOnLaneAtLeaveFront + lengthOnLaneAtStepEnd) * 0.5;
230 : }
231 :
232 485272822 : double meanLengthOnLane = integratedLengthOnLane / TS;
233 : #ifdef DEBUG_NOTIFY_MOVE
234 : std::cout << "Calculated mean length on lane '" << myLane->getID() << "' in last step as " << meanLengthOnLane
235 : << "\nlengthOnLaneAtStepStart=" << lengthOnLaneAtStepStart << ", lengthOnLaneAtStepEnd=" << lengthOnLaneAtStepEnd << ", integratedLengthOnLane=" << integratedLengthOnLane
236 : << std::endl;
237 : #endif
238 :
239 : // // XXX: use this, when #2556 is fixed! Refs. #2575
240 : // const double travelledDistanceFrontOnLane = MAX2(0., MIN2(newPos, myLaneLength) - MAX2(oldPos, 0.));
241 : // const double travelledDistanceVehicleOnLane = MIN2(newPos, myLaneLength) - MAX2(oldPos, 0.) + MIN2(MAX2(0., newPos - myLaneLength), veh.getVehicleType().getLength());
242 : // // XXX: #2556 fixed for ballistic update
243 485272822 : const double travelledDistanceFrontOnLane = MSGlobals::gSemiImplicitEulerUpdate ? frontOnLane * newSpeed
244 161834314 : : MAX2(0., MIN2(newPos, myLaneLength) - MAX2(oldPos, 0.));
245 566189979 : const double travelledDistanceVehicleOnLane = MSGlobals::gSemiImplicitEulerUpdate ? timeOnLane * newSpeed
246 161834314 : : MIN2(newPos, myLaneLength) - MAX2(oldPos, 0.) + MIN2(MAX2(0., newPos - myLaneLength), veh.getVehicleType().getLength());
247 : // // XXX: no fix
248 : // const double travelledDistanceFrontOnLane = frontOnLane*newSpeed;
249 : // const double travelledDistanceVehicleOnLane = timeOnLane*newSpeed;
250 :
251 : #ifdef HAVE_FOX
252 485272822 : ScopedLocker<> lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
253 : #endif
254 485272822 : notifyMoveInternal(veh, frontOnLane, timeOnLane, (enterSpeed + leaveSpeedFront) / 2., (enterSpeed + leaveSpeed) / 2., travelledDistanceFrontOnLane, travelledDistanceVehicleOnLane, meanLengthOnLane);
255 : return ret;
256 : }
257 :
258 :
259 : bool
260 1183961 : MSMeanData::MeanDataValues::notifyLeave(SUMOTrafficObject& /*veh*/, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
261 1183961 : if (MSGlobals::gUseMesoSim) {
262 : return false; // reminder is re-added on every segment (@recheck for performance)
263 : }
264 1029433 : return reason == MSMoveReminder::NOTIFICATION_JUNCTION;
265 : }
266 :
267 :
268 : bool
269 2951361 : MSMeanData::MeanDataValues::isEmpty() const {
270 2951361 : return sampleSeconds == 0;
271 : }
272 :
273 :
274 : void
275 0 : MSMeanData::MeanDataValues::update() {
276 0 : }
277 :
278 :
279 : double
280 1023797 : MSMeanData::MeanDataValues::getSamples() const {
281 1023797 : return sampleSeconds;
282 : }
283 :
284 :
285 : // ---------------------------------------------------------------------------
286 : // MSMeanData::MeanDataValueTracker - methods
287 : // ---------------------------------------------------------------------------
288 2744 : MSMeanData::MeanDataValueTracker::MeanDataValueTracker(MSLane* const lane,
289 : const double length,
290 2744 : const MSMeanData* const parent)
291 2744 : : MSMeanData::MeanDataValues(lane, length, true, parent) {
292 2744 : myCurrentData.push_back(new TrackerEntry(parent->createValues(lane, length, false)));
293 2744 : }
294 :
295 :
296 5488 : MSMeanData::MeanDataValueTracker::~MeanDataValueTracker() {
297 : std::list<TrackerEntry*>::iterator i;
298 29072 : for (i = myCurrentData.begin(); i != myCurrentData.end(); i++) {
299 26328 : delete *i;
300 : }
301 :
302 : // FIXME: myTrackedData may still hold some undeleted TrackerEntries. When to delete those? (Leo), refers to #2251
303 : // code below fails
304 :
305 : // std::map<SUMOTrafficObject*, TrackerEntry*>::iterator j;
306 : // for(j=myTrackedData.begin(); j!=myTrackedData.end();j++){
307 : // delete j->second;
308 : // }
309 5488 : }
310 :
311 :
312 : void
313 164304 : MSMeanData::MeanDataValueTracker::reset(bool afterWrite) {
314 164304 : if (afterWrite) {
315 70360 : if (myCurrentData.begin() != myCurrentData.end()) {
316 : // delete myCurrentData.front();
317 70360 : myCurrentData.pop_front();
318 : }
319 : } else {
320 93944 : myCurrentData.push_back(new TrackerEntry(myParent->createValues(myLane, myLaneLength, false)));
321 : }
322 164304 : }
323 :
324 :
325 : void
326 0 : MSMeanData::MeanDataValueTracker::addTo(MSMeanData::MeanDataValues& val) const {
327 0 : myCurrentData.front()->myValues->addTo(val);
328 0 : }
329 :
330 :
331 : void
332 15022 : MSMeanData::MeanDataValueTracker::notifyMoveInternal(const SUMOTrafficObject& veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double meanLengthOnLane) {
333 15022 : myTrackedData[&veh]->myValues->notifyMoveInternal(veh, frontOnLane, timeOnLane, meanSpeedFrontOnLane, meanSpeedVehicleOnLane, travelledDistanceFrontOnLane, travelledDistanceVehicleOnLane, meanLengthOnLane);
334 15022 : }
335 :
336 :
337 : bool
338 784 : MSMeanData::MeanDataValueTracker::notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
339 784 : if (myParent == nullptr || reason != MSMoveReminder::NOTIFICATION_SEGMENT) {
340 384 : myTrackedData[&veh]->myNumVehicleLeft++;
341 : }
342 784 : return myTrackedData[&veh]->myValues->notifyLeave(veh, lastPos, reason);
343 : }
344 :
345 :
346 : bool
347 3400 : MSMeanData::MeanDataValueTracker::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane) {
348 : #ifdef DEBUG_NOTIFY_ENTER
349 : std::cout << "\n" << SIMTIME << " MSMeanData::MeanDataValueTracker: veh '" << veh.getID() << "' enters lane '" << enteredLane->getID() << "'" << std::endl;
350 : #else
351 : UNUSED_PARAMETER(enteredLane);
352 : #endif
353 3400 : if (reason == MSMoveReminder::NOTIFICATION_SEGMENT) {
354 : return true;
355 : }
356 5768 : if (myParent->vehicleApplies(veh) && myTrackedData.find(&veh) == myTrackedData.end()) {
357 504 : myTrackedData[&veh] = myCurrentData.back();
358 504 : myTrackedData[&veh]->myNumVehicleEntered++;
359 504 : if (!myTrackedData[&veh]->myValues->notifyEnter(veh, reason)) {
360 0 : myTrackedData[&veh]->myNumVehicleLeft++;
361 0 : myTrackedData.erase(&veh);
362 0 : return false;
363 : }
364 : return true;
365 : }
366 : return false;
367 : }
368 :
369 :
370 : bool
371 91696 : MSMeanData::MeanDataValueTracker::isEmpty() const {
372 91696 : return myCurrentData.front()->myValues->isEmpty();
373 : }
374 :
375 :
376 : void
377 368 : MSMeanData::MeanDataValueTracker::write(OutputDevice& dev,
378 : const SumoXMLAttrMask& attributeMask,
379 : const SUMOTime period,
380 : const int numLanes,
381 : const double speedLimit,
382 : const double defaultTravelTime,
383 : const int /*numVehicles*/) const {
384 368 : myCurrentData.front()->myValues->write(dev, attributeMask, period, numLanes, speedLimit,
385 : defaultTravelTime,
386 : myCurrentData.front()->myNumVehicleEntered);
387 368 : }
388 :
389 :
390 : int
391 63116 : MSMeanData::MeanDataValueTracker::getNumReady() const {
392 : int result = 0;
393 1445056 : for (std::list<TrackerEntry*>::const_iterator it = myCurrentData.begin(); it != myCurrentData.end(); ++it) {
394 1384592 : if ((*it)->myNumVehicleEntered == (*it)->myNumVehicleLeft) {
395 1381940 : result++;
396 : } else {
397 : break;
398 : }
399 : }
400 63116 : return result;
401 : }
402 :
403 :
404 : double
405 368 : MSMeanData::MeanDataValueTracker::getSamples() const {
406 368 : return myCurrentData.front()->myValues->getSamples();
407 : }
408 :
409 :
410 : // ---------------------------------------------------------------------------
411 : // MSMeanData - methods
412 : // ---------------------------------------------------------------------------
413 24177 : MSMeanData::MSMeanData(const std::string& id,
414 : const SUMOTime dumpBegin, const SUMOTime dumpEnd,
415 : const bool useLanes, const std::string& excludeEmpty, const bool withInternal,
416 : const bool trackVehicles,
417 : const int detectPersons,
418 : const double maxTravelTime,
419 : const double minSamples,
420 : const std::string& vTypes,
421 : const std::string& writeAttributes,
422 : const std::vector<MSEdge*>& edges,
423 24177 : AggregateType aggregate) :
424 : MSDetectorFileOutput(id, vTypes, "", detectPersons),
425 24177 : myMinSamples(minSamples),
426 24177 : myMaxTravelTime(maxTravelTime),
427 24177 : myAmEdgeBased(!useLanes),
428 24177 : myDumpBegin(dumpBegin),
429 24177 : myDumpEnd(dumpEnd),
430 24177 : myInitTime(SUMOTime_MAX),
431 24177 : myEdges(edges),
432 156 : myDumpInternal(withInternal && MSGlobals::gUsingInternalLanes),
433 24177 : myTrackVehicles(trackVehicles),
434 96708 : myWrittenAttributes(OutputDevice::parseWrittenAttributes(StringTokenizer(writeAttributes).getVector(), "meandata '" + id + "'")),
435 72531 : myAggregate(aggregate) {
436 : try {
437 24177 : myDumpEmpty = !StringUtils::toBool(excludeEmpty);
438 84 : } catch (const BoolFormatException&) {
439 84 : if (excludeEmpty == "default" || excludeEmpty == "defaults") {
440 78 : myPrintDefaults = true;
441 6 : } else if (excludeEmpty == "modified") {
442 6 : myDumpEmpty = false;
443 6 : myPrintDefaults = true;
444 6 : myPrintModified = true;
445 : } else {
446 0 : throw;
447 : }
448 84 : }
449 24177 : }
450 :
451 :
452 : void
453 23250 : MSMeanData::init() {
454 23250 : myInitTime = MSNet::getInstance()->getCurrentTimeStep();
455 23250 : if (myEdges.empty()) {
456 : // use all edges by default
457 817966 : for (MSEdge* const edge : MSNet::getInstance()->getEdgeControl().getEdges()) {
458 794785 : if ((myDumpInternal || !edge->isInternal()) &&
459 528175 : ((detectsPersons() && myDumpInternal) || (!edge->isCrossing() && !edge->isWalkingArea()))) {
460 350150 : myEdges.push_back(edge);
461 : }
462 : }
463 : }
464 : int index = 0;
465 373513 : for (MSEdge* edge : myEdges) {
466 350263 : myMeasures.push_back(std::vector<MeanDataValues*>());
467 350263 : myEdgeIndex[edge] = index++;
468 350263 : const std::vector<MSLane*>& lanes = edge->getLanes();
469 350263 : if (MSGlobals::gUseMesoSim) {
470 : MeanDataValues* data;
471 29261 : if (!myAmEdgeBased) {
472 85 : for (MSLane* const lane : lanes) {
473 52 : data = createValues(lane, lanes[0]->getLength(), false);
474 52 : myMeasures.back().push_back(data);
475 52 : MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
476 310 : while (s != nullptr) {
477 258 : s->addDetector(data, lane->getIndex());
478 258 : s->prepareDetectorForWriting(*data, lane->getIndex());
479 : s = s->getNextSegment();
480 : }
481 52 : data->reset();
482 52 : data->reset(true);
483 : }
484 : } else {
485 29228 : if (myTrackVehicles) {
486 880 : data = new MeanDataValueTracker(nullptr, lanes[0]->getLength(), this);
487 : } else {
488 28348 : data = createValues(nullptr, lanes[0]->getLength(), false);
489 : }
490 58456 : data->setDescription("meandata_" + getID() + "|" + edge->getID());
491 29228 : myMeasures.back().push_back(data);
492 29228 : MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
493 148882 : while (s != nullptr) {
494 119654 : s->addDetector(data);
495 119654 : s->prepareDetectorForWriting(*data);
496 : s = s->getNextSegment();
497 : }
498 29228 : data->reset();
499 29228 : data->reset(true);
500 : }
501 : continue;
502 29261 : }
503 321002 : if (myAmEdgeBased && myTrackVehicles) {
504 936 : myMeasures.back().push_back(new MeanDataValueTracker(nullptr, lanes[0]->getLength(), this));
505 : }
506 848537 : for (MSLane* const lane : lanes) {
507 527535 : if (myTrackVehicles) {
508 2016 : if (myAmEdgeBased) {
509 1088 : lane->addMoveReminder(myMeasures.back().back());
510 : } else {
511 928 : myMeasures.back().push_back(new MeanDataValueTracker(lane, lane->getLength(), this));
512 : }
513 : } else {
514 525519 : myMeasures.back().push_back(createValues(lane, lane->getLength(), true));
515 : }
516 : }
517 : }
518 23250 : if (myAggregate == AggregateType::TAZ) {
519 450 : for (const MSEdge* e : MSEdge::getAllEdges()) {
520 444 : if (e->isTazConnector()) {
521 180 : myTAZ.push_back(e);
522 : }
523 : }
524 : }
525 23250 : }
526 :
527 :
528 24122 : MSMeanData::~MSMeanData() {
529 373776 : for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i) {
530 905164 : for (std::vector<MeanDataValues*>::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
531 555510 : delete *j;
532 : }
533 : }
534 24122 : }
535 :
536 :
537 : void
538 12649 : MSMeanData::resetOnly(SUMOTime stopTime) {
539 : UNUSED_PARAMETER(stopTime);
540 12649 : if (MSGlobals::gUseMesoSim) {
541 : MSEdgeVector::iterator edge = myEdges.begin();
542 117300 : for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i, ++edge) {
543 113716 : MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(**edge);
544 227432 : for (MeanDataValues* data : *i) {
545 602292 : while (s != nullptr) {
546 488576 : s->prepareDetectorForWriting(*data);
547 : s = s->getNextSegment();
548 : }
549 113716 : data->reset();
550 : }
551 : }
552 : return;
553 : }
554 257356 : for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i) {
555 530134 : for (std::vector<MeanDataValues*>::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
556 281843 : (*j)->reset();
557 : }
558 : }
559 : }
560 :
561 :
562 : std::string
563 576375 : MSMeanData::getEdgeID(const MSEdge* const edge) {
564 576375 : return edge->getID();
565 : }
566 :
567 :
568 : void
569 940 : MSMeanData::writeAggregated(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime) {
570 940 : if (myTrackVehicles) {
571 0 : throw ProcessError(TL("aggregated meanData output not yet implemented for trackVehicles"));
572 : }
573 :
574 : double edgeLengthSum = 0;
575 : int laneNumber = 0;
576 : double speedSum = 0;
577 : double totalTT = 0;
578 42172 : for (MSEdge* edge : myEdges) {
579 41232 : edgeLengthSum += edge->getLength();
580 41232 : laneNumber += edge->getNumDrivingLanes();
581 41232 : speedSum += edge->getSpeedLimit();
582 41232 : totalTT += edge->getLength() / edge->getSpeedLimit();
583 : }
584 940 : MeanDataValues* sumData = createValues(nullptr, edgeLengthSum, false);
585 42172 : for (const std::vector<MeanDataValues*>& edgeValues : myMeasures) {
586 82512 : for (MeanDataValues* meanData : edgeValues) {
587 41280 : meanData->addTo(*sumData);
588 41280 : if (!MSNet::getInstance()->skipFinalReset()) {
589 41280 : meanData->reset();
590 : }
591 : }
592 : }
593 940 : if (MSGlobals::gUseMesoSim) {
594 14040 : for (int i = 0; i < (int)myEdges.size(); i++) {
595 13728 : MSEdge* edge = myEdges[i];
596 : std::vector<MeanDataValues*>& edgeValues = myMeasures[i];
597 13728 : MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
598 68640 : while (s != nullptr) {
599 109824 : for (MeanDataValues* meanData : edgeValues) {
600 54912 : s->prepareDetectorForWriting(*meanData);
601 54912 : meanData->addTo(*sumData);
602 54912 : if (!MSNet::getInstance()->skipFinalReset()) {
603 54912 : meanData->reset();
604 : }
605 : }
606 : s = s->getNextSegment();
607 : }
608 : }
609 : }
610 :
611 940 : if (myDumpEmpty || !sumData->isEmpty()) {
612 1868 : writePrefix(dev, *sumData, SUMO_TAG_EDGE, "AGGREGATED");
613 934 : dev.writeAttr(SUMO_ATTR_NUMEDGES, myEdges.size());
614 934 : sumData->write(dev, myWrittenAttributes, stopTime - startTime, laneNumber, speedSum / (double)myEdges.size(),
615 934 : myPrintDefaults ? totalTT : -1.);
616 : }
617 940 : delete sumData;
618 940 : }
619 :
620 :
621 : void
622 18 : MSMeanData::writeAggregatedTAZ(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime) {
623 18 : if (myTrackVehicles) {
624 0 : throw ProcessError(TL("aggregated meanData output not yet implemented for trackVehicles"));
625 : }
626 :
627 558 : for (const MSEdge* taz : myTAZ) {
628 : double edgeLengthSum = 0;
629 : int laneNumber = 0;
630 : double speedSum = 0;
631 : double totalTT = 0;
632 : std::set<const MSEdge*> connected;
633 1332 : for (const MSEdge* edge : taz->getSuccessors()) {
634 : connected.insert(edge);
635 : }
636 1332 : for (const MSEdge* edge : taz->getPredecessors()) {
637 : connected.insert(edge);
638 : }
639 2124 : for (const MSEdge* edge : connected) {
640 1584 : edgeLengthSum += edge->getLength();
641 1584 : laneNumber += edge->getNumDrivingLanes();
642 1584 : speedSum += edge->getSpeedLimit();
643 1584 : totalTT += edge->getLength() / edge->getSpeedLimit();
644 : }
645 540 : MeanDataValues* sumData = createValues(nullptr, edgeLengthSum, false);
646 24300 : for (int i = 0; i < (int)myEdges.size(); i++) {
647 23760 : MSEdge* edge = myEdges[i];
648 : if (connected.count(edge) != 0) {
649 : std::vector<MeanDataValues*>& edgeValues = myMeasures[i];
650 1584 : if (MSGlobals::gUseMesoSim) {
651 528 : MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
652 2640 : while (s != nullptr) {
653 4224 : for (MeanDataValues* meanData : edgeValues) {
654 2112 : s->prepareDetectorForWriting(*meanData);
655 2112 : meanData->addTo(*sumData);
656 : }
657 : s = s->getNextSegment();
658 : }
659 : } else {
660 2112 : for (MeanDataValues* meanData : edgeValues) {
661 1056 : meanData->addTo(*sumData);
662 : }
663 : }
664 : }
665 : }
666 540 : if (myDumpEmpty || !sumData->isEmpty()) {
667 216 : writePrefix(dev, *sumData, SUMO_TAG_EDGE, taz->getID());
668 72 : dev.writeAttr(SUMO_ATTR_NUMEDGES, connected.size());
669 72 : sumData->write(dev, myWrittenAttributes, stopTime - startTime, laneNumber, speedSum / (double)connected.size(),
670 72 : myPrintDefaults ? totalTT : -1.);
671 : }
672 540 : delete sumData;
673 : }
674 :
675 18 : if (!MSNet::getInstance()->skipFinalReset()) {
676 810 : for (const std::vector<MeanDataValues*>& edgeValues : myMeasures) {
677 1584 : for (MeanDataValues* meanData : edgeValues) {
678 792 : meanData->reset();
679 : }
680 : }
681 : }
682 18 : }
683 :
684 :
685 : void
686 17886588 : MSMeanData::writeEdge(OutputDevice& dev,
687 : const std::vector<MeanDataValues*>& edgeValues,
688 : const MSEdge* const edge, SUMOTime startTime, SUMOTime stopTime) {
689 17886588 : if (MSGlobals::gUseMesoSim) {
690 : int idx = 0;
691 2721793 : for (MeanDataValues* const data : edgeValues) {
692 1360920 : MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
693 7001538 : while (s != nullptr) {
694 5640960 : s->prepareDetectorForWriting(*data, myAmEdgeBased ? -1 : idx);
695 : s = s->getNextSegment();
696 : }
697 1360920 : idx++;
698 : }
699 1360873 : if (myAmEdgeBased) {
700 1360826 : MeanDataValues* const data = edgeValues.front();
701 1360826 : if (myDumpEmpty || (myPrintModified && edge->getLanes()[0]->isSpeedModified()) || !data->isEmpty()) {
702 60462 : writePrefix(dev, *data, SUMO_TAG_EDGE, getEdgeID(edge));
703 60462 : data->write(dev, myWrittenAttributes, stopTime - startTime,
704 : edge->getNumDrivingLanes(),
705 : edge->getSpeedLimit(),
706 60462 : myPrintDefaults ? edge->getLength() / edge->getSpeedLimit() : -1.);
707 : }
708 1360826 : if (!MSNet::getInstance()->skipFinalReset()) {
709 1360826 : data->reset(true);
710 : }
711 1360826 : return;
712 : }
713 : }
714 16525762 : if (!myAmEdgeBased) {
715 8025194 : bool writeCheck = myDumpEmpty;
716 8025194 : if (!writeCheck) {
717 15611260 : for (const MeanDataValues* const laneData : edgeValues) {
718 7912847 : if (!laneData->isEmpty() || (myPrintModified && laneData->getLane()->isSpeedModified())) {
719 : writeCheck = true;
720 : break;
721 : }
722 : }
723 : }
724 7901499 : if (writeCheck) {
725 326781 : dev.openTag(SUMO_TAG_EDGE).writeAttr(SUMO_ATTR_ID, edge->getID());
726 : }
727 16170146 : for (MeanDataValues* const laneData : edgeValues) {
728 : const MSLane* const lane = laneData->getLane();
729 8144952 : if (myDumpEmpty || (myPrintModified && lane->isSpeedModified()) || !laneData->isEmpty()) {
730 870250 : writePrefix(dev, *laneData, SUMO_TAG_LANE, lane->getID());
731 435125 : laneData->write(dev, myWrittenAttributes, stopTime - startTime, 1, lane->getSpeedLimit(),
732 435125 : myPrintDefaults ? lane->getLength() / lane->getSpeedLimit() : -1.);
733 : }
734 8144952 : if (!MSNet::getInstance()->skipFinalReset()) {
735 8144952 : laneData->reset(true);
736 : }
737 : }
738 8025194 : if (writeCheck) {
739 653562 : dev.closeTag();
740 : }
741 : } else {
742 8500568 : if (myTrackVehicles) {
743 22440 : MeanDataValues& meanData = **edgeValues.begin();
744 22440 : if (myDumpEmpty || !meanData.isEmpty()) {
745 160 : writePrefix(dev, meanData, SUMO_TAG_EDGE, edge->getID());
746 160 : meanData.write(dev, myWrittenAttributes, stopTime - startTime, edge->getNumDrivingLanes(), edge->getSpeedLimit(),
747 160 : myPrintDefaults ? edge->getLength() / edge->getSpeedLimit() : -1.);
748 : }
749 22440 : if (!MSNet::getInstance()->skipFinalReset()) {
750 22440 : meanData.reset(true);
751 : }
752 : } else {
753 8478128 : MeanDataValues* sumData = createValues(nullptr, edge->getLength(), false);
754 8478128 : bool writeCheck = myDumpEmpty;
755 17123990 : for (MeanDataValues* const laneData : edgeValues) {
756 8645862 : laneData->addTo(*sumData);
757 8645862 : if (myPrintModified && laneData->getLane()->isSpeedModified()) {
758 : writeCheck = true;
759 : }
760 8645862 : if (!MSNet::getInstance()->skipFinalReset()) {
761 8645862 : laneData->reset();
762 : }
763 : }
764 8478128 : if (writeCheck || !sumData->isEmpty()) {
765 518645 : writePrefix(dev, *sumData, SUMO_TAG_EDGE, getEdgeID(edge));
766 518645 : sumData->write(dev, myWrittenAttributes, stopTime - startTime, edge->getNumDrivingLanes(), edge->getSpeedLimit(),
767 518645 : myPrintDefaults ? edge->getLength() / edge->getSpeedLimit() : -1.);
768 : }
769 8478127 : delete sumData;
770 : }
771 : }
772 : }
773 :
774 :
775 : void
776 437748 : MSMeanData::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
777 875496 : dev.openTag(SUMO_TAG_INTERVAL).writeAttr(SUMO_ATTR_BEGIN, time2string(startTime)).writeAttr(SUMO_ATTR_END, time2string(stopTime));
778 437748 : dev.writeAttr(SUMO_ATTR_ID, myID);
779 437748 : }
780 :
781 :
782 : void
783 1012642 : MSMeanData::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag tag, const std::string id) const {
784 1012642 : dev.openTag(tag);
785 1012642 : dev.writeAttr(SUMO_ATTR_ID, id);
786 1012642 : dev.writeOptionalAttr(SUMO_ATTR_SAMPLEDSECONDS, values.getSamples(), myWrittenAttributes);
787 1012642 : }
788 :
789 :
790 : void
791 449063 : MSMeanData::writeXMLOutput(OutputDevice& dev,
792 : SUMOTime startTime, SUMOTime stopTime) {
793 : // check whether this dump shall be written for the current time
794 449063 : int numReady = myDumpBegin < stopTime && myDumpEnd - DELTA_T >= startTime ? 1 : 0;
795 449063 : if (myTrackVehicles && myDumpBegin < stopTime) {
796 2124 : myPendingIntervals.push_back(std::make_pair(startTime, stopTime));
797 2124 : numReady = (int)myPendingIntervals.size();
798 63312 : for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i) {
799 124304 : for (std::vector<MeanDataValues*>::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
800 63116 : numReady = MIN2(numReady, ((MeanDataValueTracker*)*j)->getNumReady());
801 63116 : if (numReady == 0) {
802 : break;
803 : }
804 : }
805 63092 : if (numReady == 0) {
806 : break;
807 : }
808 : }
809 : }
810 449063 : const bool partialInterval = startTime < myInitTime;
811 449063 : if (numReady == 0 || myTrackVehicles || partialInterval) {
812 12649 : resetOnly(stopTime);
813 : }
814 449063 : if (partialInterval) {
815 : return;
816 : }
817 887054 : while (numReady-- > 0) {
818 438002 : if (!myPendingIntervals.empty()) {
819 1588 : startTime = myPendingIntervals.front().first;
820 1588 : stopTime = myPendingIntervals.front().second;
821 1588 : myPendingIntervals.pop_front();
822 : }
823 438002 : openInterval(dev, startTime, stopTime);
824 438002 : if (myAggregate == AggregateType::YES) {
825 940 : writeAggregated(dev, startTime, stopTime);
826 437062 : } else if (myAggregate == AggregateType::TAZ) {
827 18 : writeAggregatedTAZ(dev, startTime, stopTime);
828 : } else {
829 : MSEdgeVector::const_iterator edge = myEdges.begin();
830 18323631 : for (const std::vector<MeanDataValues*>& measures : myMeasures) {
831 17886588 : writeEdge(dev, measures, *edge, startTime, stopTime);
832 : ++edge;
833 : }
834 : }
835 876002 : dev.closeTag();
836 : }
837 : dev.flush();
838 : }
839 :
840 :
841 : void
842 23228 : MSMeanData::writeXMLDetectorProlog(OutputDevice& dev) const {
843 46456 : dev.writeXMLHeader("meandata", "meandata_file.xsd");
844 23228 : }
845 :
846 :
847 : void
848 28296125 : MSMeanData::detectorUpdate(const SUMOTime step) {
849 28296125 : if (step + DELTA_T == myDumpBegin) {
850 639 : init();
851 : }
852 28296125 : }
853 :
854 :
855 : const std::vector<MSMeanData::MeanDataValues*>*
856 0 : MSMeanData::getEdgeValues(const MSEdge* edge) const {
857 : auto it = myEdgeIndex.find(edge);
858 0 : if (it != myEdgeIndex.end()) {
859 0 : return &myMeasures[it->second];
860 : } else {
861 : return nullptr;
862 : }
863 : }
864 :
865 :
866 : const std::vector<MSMoveReminder*>
867 1 : MSMeanData::getReminders() const {
868 : std::vector<MSMoveReminder*> result;
869 45 : for (auto vec : myMeasures) {
870 44 : result.insert(result.end(), vec.begin(), vec.end());
871 44 : }
872 1 : return result;
873 0 : }
874 :
875 :
876 : /****************************************************************************/
|