LCOV - code coverage report
Current view: top level - src/microsim/output - MSMeanData.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 95.6 % 365 349
Test Date: 2025-12-06 15:35:27 Functions: 86.5 % 37 32

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2025 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      9043179 : MSMeanData::MeanDataValues::MeanDataValues(
      61              :     MSLane* const lane, const double length, const bool doAdd,
      62      9043179 :     const MSMeanData* const parent) :
      63     18086358 :     MSMoveReminder("meandata_" + (parent == nullptr ? "" : parent->getID() + "|") + (lane == nullptr ? "NULL" :  lane->getID()), lane, doAdd),
      64      9043179 :     myParent(parent),
      65      9043179 :     myLaneLength(length),
      66      9043179 :     sampleSeconds(0),
      67     27129537 :     travelledDistance(0) { }
      68              : 
      69              : 
      70      8971941 : MSMeanData::MeanDataValues::~MeanDataValues() {
      71      8971941 : }
      72              : 
      73              : 
      74              : bool
      75       690082 : 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       690082 :     return myParent == nullptr || myParent->vehicleApplies(veh);
      83              : }
      84              : 
      85              : 
      86              : bool
      87    458643388 : 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    458643388 :     const double oldSpeed = veh.getPreviousSpeed();
      91    458643388 :     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    458643388 :     double timeOnLane = TS;
      96    458643388 :     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    458643388 :     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    458643388 :     if (oldPos < 0 && newPos >= 0) {
     107              :         // Vehicle was not on this lane in the last time step
     108     10085788 :         timeBeforeEnter = MSCFModel::passingTime(oldPos, 0, newPos, oldSpeed, newSpeed);
     109     10085788 :         timeOnLane = TS - timeBeforeEnter;
     110              :         frontOnLane = timeOnLane;
     111     10085788 :         enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
     112              :     }
     113              : 
     114    458643388 :     const double oldBackPos = oldPos - veh.getVehicleType().getLength();
     115    458643388 :     const double newBackPos = newPos - veh.getVehicleType().getLength();
     116              : 
     117              :     // Determine the time before the vehicle back enters
     118    458643388 :     if (oldBackPos < 0. && newBackPos > 0.) {
     119     10336037 :         timeBeforeEnterBack = MSCFModel::passingTime(oldBackPos, 0., newBackPos, oldSpeed, newSpeed);
     120    448307351 :     } else if (newBackPos <= 0) {
     121      6740806 :         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    458643388 :     if (newBackPos > myLaneLength // vehicle's back has left the lane
     128     11552057 :             && 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     11552057 :         timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myLaneLength, newBackPos, oldSpeed, newSpeed);
     132     11552057 :         const double timeAfterLeave = TS - timeBeforeLeave;
     133     11552057 :         timeOnLane -= timeAfterLeave;
     134     11552057 :         leaveSpeed = MSCFModel::speedAfterTime(timeBeforeLeave, oldSpeed, newPos - oldPos);
     135              :         // XXX: Do we really need this? Why would this "reduce rounding errors"? (Leo) Refs. #2579
     136     11552057 :         if (fabs(timeOnLane) < NUMERICAL_EPS) { // reduce rounding errors
     137              :             timeOnLane = 0.;
     138              :         }
     139     11552057 :         ret = veh.hasArrived();
     140              :     }
     141              : 
     142              :     // Treat the case that the vehicle's front left the lane in the last step
     143    458643388 :     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     12566188 :         timeBeforeLeaveFront = MSCFModel::passingTime(oldPos, myLaneLength, newPos, oldSpeed, newSpeed);
     147     12566188 :         const double timeAfterLeave = TS - timeBeforeLeaveFront;
     148     12566188 :         frontOnLane -= timeAfterLeave;
     149              :         // XXX: Do we really need this? Why would this "reduce rounding errors"? (Leo) Refs. #2579
     150     12566188 :         if (fabs(frontOnLane) < NUMERICAL_EPS) { // reduce rounding errors
     151              :             frontOnLane = 0.;
     152              :         }
     153     12566188 :         leaveSpeedFront = MSCFModel::speedAfterTime(timeBeforeLeaveFront, oldSpeed, newPos - oldPos);
     154              :     }
     155              : 
     156              :     assert(frontOnLane <= TS);
     157              :     assert(timeOnLane <= TS);
     158              : 
     159    458643388 :     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    458643388 :     if (timeOnLane == 0) {
     164        20737 :         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    458622651 :     double vehLength = veh.getVehicleType().getLength();
     192              :     // occupied lane length at timeBeforeEnter (resp. stepStart if already on lane)
     193    458622651 :     double lengthOnLaneAtStepStart = MAX2(0., MIN4(myLaneLength, vehLength, vehLength - (oldPos - myLaneLength), oldPos));
     194              :     // occupied lane length at timeBeforeLeave (resp. stepEnd if still on lane)
     195    458622651 :     double lengthOnLaneAtStepEnd = MAX2(0., MIN4(myLaneLength, vehLength, vehLength - (newPos - myLaneLength), newPos));
     196              :     double integratedLengthOnLane = 0.;
     197    458622651 :     if (timeBeforeEnterBack < timeBeforeLeaveFront) {
     198              :         // => timeBeforeLeaveFront>0, myLaneLength>vehLength
     199              :         // vehicle length on detector at timeBeforeEnterBack
     200    445654833 :         double lengthOnLaneAtBackEnter = MIN2(veh.getVehicleType().getLength(), newPos);
     201              :         // linear quadrature of occupancy between timeBeforeEnter and timeBeforeEnterBack
     202    445654833 :         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    445654833 :         integratedLengthOnLane += (timeBeforeLeaveFront - timeBeforeEnterBack) * vehLength;
     206              :         // and until vehicle leaves/stepEnd
     207    445654833 :         integratedLengthOnLane += (timeBeforeLeave - timeBeforeLeaveFront) * (vehLength + lengthOnLaneAtStepEnd) * 0.5;
     208     12967818 :     } else if (timeBeforeEnterBack >= timeBeforeLeaveFront) {
     209              :         // => myLaneLength <= vehLength or (timeBeforeLeaveFront == timeBeforeEnterBack == 0)
     210              :         // vehicle length on detector at timeBeforeLeaveFront
     211              :         double lengthOnLaneAtLeaveFront;
     212     12967818 :         if (timeBeforeLeaveFront == timeBeforeEnter) {
     213              :             // for the case that front already left
     214              :             lengthOnLaneAtLeaveFront = lengthOnLaneAtStepStart;
     215      6764757 :         } 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     12967818 :         integratedLengthOnLane += (timeBeforeLeaveFront - timeBeforeEnter) * (lengthOnLaneAtLeaveFront + lengthOnLaneAtStepStart) * 0.5;
     226              :         // linear quadrature of occupancy between timeBeforeLeaveFront and timeBeforeEnterBack
     227     12967818 :         integratedLengthOnLane += (timeBeforeEnterBack - timeBeforeLeaveFront) * lengthOnLaneAtLeaveFront;
     228              :         // and until vehicle leaves/stepEnd
     229     12967818 :         integratedLengthOnLane += (timeBeforeLeave - timeBeforeEnterBack) * (lengthOnLaneAtLeaveFront + lengthOnLaneAtStepEnd) * 0.5;
     230              :     }
     231              : 
     232    458622651 :     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    458622651 :     const double travelledDistanceFrontOnLane = MSGlobals::gSemiImplicitEulerUpdate ? frontOnLane * newSpeed
     244    162224006 :             : MAX2(0., MIN2(newPos, myLaneLength) - MAX2(oldPos, 0.));
     245    539734654 :     const double travelledDistanceVehicleOnLane = MSGlobals::gSemiImplicitEulerUpdate ? timeOnLane * newSpeed
     246    162224006 :             : 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    458622651 :     ScopedLocker<> lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
     253              : #endif
     254    458622651 :     notifyMoveInternal(veh, frontOnLane, timeOnLane, (enterSpeed + leaveSpeedFront) / 2., (enterSpeed + leaveSpeed) / 2., travelledDistanceFrontOnLane, travelledDistanceVehicleOnLane, meanLengthOnLane);
     255              :     return ret;
     256              : }
     257              : 
     258              : 
     259              : bool
     260      1179410 : MSMeanData::MeanDataValues::notifyLeave(SUMOTrafficObject& /*veh*/, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
     261      1179410 :     if (MSGlobals::gUseMesoSim) {
     262              :         return false; // reminder is re-added on every segment (@recheck for performance)
     263              :     }
     264      1024882 :     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       967450 : MSMeanData::MeanDataValues::getSamples() const {
     281       967450 :     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        21376 : MSMeanData::MSMeanData(const std::string& id,
     414              :                        const SUMOTime dumpBegin, const SUMOTime dumpEnd,
     415              :                        const bool useLanes, const bool withEmpty,
     416              :                        const bool printDefaults, const bool withInternal,
     417              :                        const bool trackVehicles,
     418              :                        const int detectPersons,
     419              :                        const double maxTravelTime,
     420              :                        const double minSamples,
     421              :                        const std::string& vTypes,
     422              :                        const std::string& writeAttributes,
     423              :                        const std::vector<MSEdge*>& edges,
     424        21376 :                        AggregateType aggregate) :
     425              :     MSDetectorFileOutput(id, vTypes, "", detectPersons),
     426        21376 :     myMinSamples(minSamples),
     427        21376 :     myMaxTravelTime(maxTravelTime),
     428        21376 :     myDumpEmpty(withEmpty),
     429        21376 :     myAmEdgeBased(!useLanes),
     430        21376 :     myDumpBegin(dumpBegin),
     431        21376 :     myDumpEnd(dumpEnd),
     432        21376 :     myInitTime(SUMOTime_MAX),
     433        21376 :     myEdges(edges),
     434        21376 :     myPrintDefaults(printDefaults),
     435        21376 :     myDumpInternal(withInternal && MSGlobals::gUsingInternalLanes),
     436        21376 :     myTrackVehicles(trackVehicles),
     437        85504 :     myWrittenAttributes(OutputDevice::parseWrittenAttributes(StringTokenizer(writeAttributes).getVector(), "meandata '" + id + "'")),
     438        64128 :     myAggregate(aggregate)
     439        21376 : { }
     440              : 
     441              : 
     442              : void
     443        20235 : MSMeanData::init() {
     444        20235 :     myInitTime = MSNet::getInstance()->getCurrentTimeStep();
     445        20235 :     if (myEdges.empty()) {
     446              :         // use all edges by default
     447       706711 :         for (MSEdge* const edge : MSNet::getInstance()->getEdgeControl().getEdges()) {
     448       686541 :             if ((myDumpInternal || !edge->isInternal()) &&
     449       457956 :                     ((detectsPersons() && myDumpInternal) || (!edge->isCrossing() && !edge->isWalkingArea()))) {
     450       315042 :                 myEdges.push_back(edge);
     451              :             }
     452              :         }
     453              :     }
     454              :     int index = 0;
     455       335386 :     for (MSEdge* edge : myEdges) {
     456       315151 :         myMeasures.push_back(std::vector<MeanDataValues*>());
     457       315151 :         myEdgeIndex[edge] = index++;
     458       315151 :         const std::vector<MSLane*>& lanes = edge->getLanes();
     459       315151 :         if (MSGlobals::gUseMesoSim) {
     460              :             MeanDataValues* data;
     461        29181 :             if (!myAmEdgeBased) {
     462           85 :                 for (MSLane* const lane : lanes) {
     463           52 :                     data = createValues(lane, lanes[0]->getLength(), false);
     464           52 :                     myMeasures.back().push_back(data);
     465           52 :                     MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
     466          310 :                     while (s != nullptr) {
     467          258 :                         s->addDetector(data, lane->getIndex());
     468          258 :                         s->prepareDetectorForWriting(*data, lane->getIndex());
     469              :                         s = s->getNextSegment();
     470              :                     }
     471           52 :                     data->reset();
     472           52 :                     data->reset(true);
     473              :                 }
     474              :             } else {
     475        29148 :                 if (myTrackVehicles) {
     476          880 :                     data = new MeanDataValueTracker(nullptr, lanes[0]->getLength(), this);
     477              :                 } else {
     478        28268 :                     data = createValues(nullptr, lanes[0]->getLength(), false);
     479              :                 }
     480        58296 :                 data->setDescription("meandata_" + getID() + "|" + edge->getID());
     481        29148 :                 myMeasures.back().push_back(data);
     482        29148 :                 MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
     483       148456 :                 while (s != nullptr) {
     484       119308 :                     s->addDetector(data);
     485       119308 :                     s->prepareDetectorForWriting(*data);
     486              :                     s = s->getNextSegment();
     487              :                 }
     488        29148 :                 data->reset();
     489        29148 :                 data->reset(true);
     490              :             }
     491              :             continue;
     492        29181 :         }
     493       285970 :         if (myAmEdgeBased && myTrackVehicles) {
     494          936 :             myMeasures.back().push_back(new MeanDataValueTracker(nullptr, lanes[0]->getLength(), this));
     495              :         }
     496       741260 :         for (MSLane* const lane : lanes) {
     497       455290 :             if (myTrackVehicles) {
     498         2016 :                 if (myAmEdgeBased) {
     499         1088 :                     lane->addMoveReminder(myMeasures.back().back());
     500              :                 } else {
     501          928 :                     myMeasures.back().push_back(new MeanDataValueTracker(lane, lane->getLength(), this));
     502              :                 }
     503              :             } else {
     504       453274 :                 myMeasures.back().push_back(createValues(lane, lane->getLength(), true));
     505              :             }
     506              :         }
     507              :     }
     508        20235 :     if (myAggregate == AggregateType::TAZ) {
     509          450 :         for (const MSEdge* e : MSEdge::getAllEdges()) {
     510          444 :             if (e->isTazConnector()) {
     511          180 :                 myTAZ.push_back(e);
     512              :             }
     513              :         }
     514              :     }
     515        20235 : }
     516              : 
     517              : 
     518        21327 : MSMeanData::~MSMeanData() {
     519       336001 :     for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i) {
     520       798135 :         for (std::vector<MeanDataValues*>::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
     521       483461 :             delete *j;
     522              :         }
     523              :     }
     524        21327 : }
     525              : 
     526              : 
     527              : void
     528        12645 : MSMeanData::resetOnly(SUMOTime stopTime) {
     529              :     UNUSED_PARAMETER(stopTime);
     530        12645 :     if (MSGlobals::gUseMesoSim) {
     531              :         MSEdgeVector::iterator edge = myEdges.begin();
     532       117300 :         for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i, ++edge) {
     533       113716 :             MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(**edge);
     534       227432 :             for (MeanDataValues* data : *i) {
     535       602292 :                 while (s != nullptr) {
     536       488576 :                     s->prepareDetectorForWriting(*data);
     537              :                     s = s->getNextSegment();
     538              :                 }
     539       113716 :                 data->reset();
     540              :             }
     541              :         }
     542              :         return;
     543              :     }
     544       257304 :     for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i) {
     545       529990 :         for (std::vector<MeanDataValues*>::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
     546       281747 :             (*j)->reset();
     547              :         }
     548              :     }
     549              : }
     550              : 
     551              : 
     552              : std::string
     553      9814550 : MSMeanData::getEdgeID(const MSEdge* const edge) {
     554      9814550 :     return edge->getID();
     555              : }
     556              : 
     557              : 
     558              : void
     559          940 : MSMeanData::writeAggregated(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime) {
     560          940 :     if (myTrackVehicles) {
     561            0 :         throw ProcessError(TL("aggregated meanData output not yet implemented for trackVehicles"));
     562              :     }
     563              : 
     564              :     double edgeLengthSum = 0;
     565              :     int laneNumber = 0;
     566              :     double speedSum = 0;
     567              :     double totalTT = 0;
     568        42172 :     for (MSEdge* edge : myEdges) {
     569        41232 :         edgeLengthSum += edge->getLength();
     570        41232 :         laneNumber += edge->getNumDrivingLanes();
     571        41232 :         speedSum += edge->getSpeedLimit();
     572        41232 :         totalTT += edge->getLength() / edge->getSpeedLimit();
     573              :     }
     574          940 :     MeanDataValues* sumData = createValues(nullptr, edgeLengthSum, false);
     575        42172 :     for (const std::vector<MeanDataValues*>& edgeValues : myMeasures) {
     576        82512 :         for (MeanDataValues* meanData : edgeValues) {
     577        41280 :             meanData->addTo(*sumData);
     578        41280 :             if (!MSNet::getInstance()->skipFinalReset()) {
     579        41280 :                 meanData->reset();
     580              :             }
     581              :         }
     582              :     }
     583          940 :     if (MSGlobals::gUseMesoSim) {
     584        14040 :         for (int i = 0; i < (int)myEdges.size(); i++) {
     585        13728 :             MSEdge* edge = myEdges[i];
     586              :             std::vector<MeanDataValues*>& edgeValues = myMeasures[i];
     587        13728 :             MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
     588        68640 :             while (s != nullptr) {
     589       109824 :                 for (MeanDataValues* meanData : edgeValues) {
     590        54912 :                     s->prepareDetectorForWriting(*meanData);
     591        54912 :                     meanData->addTo(*sumData);
     592        54912 :                     if (!MSNet::getInstance()->skipFinalReset()) {
     593        54912 :                         meanData->reset();
     594              :                     }
     595              :                 }
     596              :                 s = s->getNextSegment();
     597              :             }
     598              :         }
     599              :     }
     600              : 
     601         1880 :     if (writePrefix(dev, *sumData, SUMO_TAG_EDGE, "AGGREGATED")) {
     602          934 :         dev.writeAttr(SUMO_ATTR_NUMEDGES, myEdges.size());
     603          934 :         sumData->write(dev, myWrittenAttributes, stopTime - startTime, laneNumber, speedSum / (double)myEdges.size(),
     604          934 :                        myPrintDefaults ? totalTT : -1.);
     605              :     }
     606          940 :     delete sumData;
     607          940 : }
     608              : 
     609              : 
     610              : void
     611           18 : MSMeanData::writeAggregatedTAZ(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime) {
     612           18 :     if (myTrackVehicles) {
     613            0 :         throw ProcessError(TL("aggregated meanData output not yet implemented for trackVehicles"));
     614              :     }
     615              : 
     616          558 :     for (const MSEdge* taz : myTAZ) {
     617              :         double edgeLengthSum = 0;
     618              :         int laneNumber = 0;
     619              :         double speedSum = 0;
     620              :         double totalTT = 0;
     621              :         std::set<const MSEdge*> connected;
     622         1332 :         for (const MSEdge* edge : taz->getSuccessors()) {
     623              :             connected.insert(edge);
     624              :         }
     625         1332 :         for (const MSEdge* edge : taz->getPredecessors()) {
     626              :             connected.insert(edge);
     627              :         }
     628         2124 :         for (const MSEdge* edge : connected) {
     629         1584 :             edgeLengthSum += edge->getLength();
     630         1584 :             laneNumber += edge->getNumDrivingLanes();
     631         1584 :             speedSum += edge->getSpeedLimit();
     632         1584 :             totalTT += edge->getLength() / edge->getSpeedLimit();
     633              :         }
     634          540 :         MeanDataValues* sumData = createValues(nullptr, edgeLengthSum, false);
     635        24300 :         for (int i = 0; i < (int)myEdges.size(); i++) {
     636        23760 :             MSEdge* edge = myEdges[i];
     637              :             if (connected.count(edge) != 0) {
     638              :                 std::vector<MeanDataValues*>& edgeValues = myMeasures[i];
     639         1584 :                 if (MSGlobals::gUseMesoSim) {
     640          528 :                     MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
     641         2640 :                     while (s != nullptr) {
     642         4224 :                         for (MeanDataValues* meanData : edgeValues) {
     643         2112 :                             s->prepareDetectorForWriting(*meanData);
     644         2112 :                             meanData->addTo(*sumData);
     645              :                         }
     646              :                         s = s->getNextSegment();
     647              :                     }
     648              :                 } else {
     649         2112 :                     for (MeanDataValues* meanData : edgeValues) {
     650         1056 :                         meanData->addTo(*sumData);
     651              :                     }
     652              :                 }
     653              :             }
     654              :         }
     655         1620 :         if (writePrefix(dev, *sumData, SUMO_TAG_EDGE, taz->getID())) {
     656           72 :             dev.writeAttr(SUMO_ATTR_NUMEDGES, connected.size());
     657           72 :             sumData->write(dev, myWrittenAttributes, stopTime - startTime, laneNumber, speedSum / (double)connected.size(),
     658           72 :                     myPrintDefaults ? totalTT : -1.);
     659              :         }
     660          540 :         delete sumData;
     661              :     }
     662              : 
     663           18 :     if (!MSNet::getInstance()->skipFinalReset()) {
     664          810 :         for (const std::vector<MeanDataValues*>& edgeValues : myMeasures) {
     665         1584 :             for (MeanDataValues* meanData : edgeValues) {
     666          792 :                 meanData->reset();
     667              :             }
     668              :         }
     669              :     }
     670           18 : }
     671              : 
     672              : 
     673              : void
     674     17847724 : MSMeanData::writeEdge(OutputDevice& dev,
     675              :                       const std::vector<MeanDataValues*>& edgeValues,
     676              :                       const MSEdge* const edge, SUMOTime startTime, SUMOTime stopTime) {
     677     17847724 :     if (MSGlobals::gUseMesoSim) {
     678              :         int idx = 0;
     679      2719305 :         for (MeanDataValues* const data : edgeValues) {
     680      1359676 :             MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*edge);
     681      6992732 :             while (s != nullptr) {
     682      5633398 :                 s->prepareDetectorForWriting(*data, myAmEdgeBased ? -1 : idx);
     683              :                 s = s->getNextSegment();
     684              :             }
     685      1359676 :             idx++;
     686              :         }
     687      1359629 :         if (myAmEdgeBased) {
     688      1359582 :             MeanDataValues* data = edgeValues.front();
     689      2719164 :             if (writePrefix(dev, *data, SUMO_TAG_EDGE, getEdgeID(edge))) {
     690        59584 :                 data->write(dev, myWrittenAttributes, stopTime - startTime,
     691              :                             edge->getNumDrivingLanes(),
     692              :                             edge->getSpeedLimit(),
     693        59584 :                             myPrintDefaults ? edge->getLength() / edge->getSpeedLimit() : -1.);
     694              :             }
     695      1359582 :             if (!MSNet::getInstance()->skipFinalReset()) {
     696      1359582 :                 data->reset(true);
     697              :             }
     698              :             return;
     699              :         }
     700              :     }
     701              :     std::vector<MeanDataValues*>::const_iterator lane;
     702     16488142 :     if (!myAmEdgeBased) {
     703      8007710 :         bool writeCheck = myDumpEmpty;
     704      8007710 :         if (!writeCheck) {
     705     15611260 :             for (lane = edgeValues.begin(); lane != edgeValues.end(); ++lane) {
     706      7912847 :                 if (!(*lane)->isEmpty()) {
     707              :                     writeCheck = true;
     708              :                     break;
     709              :                 }
     710              :             }
     711              :         }
     712      7901499 :         if (writeCheck) {
     713       309297 :             dev.openTag(SUMO_TAG_EDGE).writeAttr(SUMO_ATTR_ID, edge->getID());
     714              :         }
     715     16116569 :         for (lane = edgeValues.begin(); lane != edgeValues.end(); ++lane) {
     716      8108859 :             MeanDataValues& meanData = **lane;
     717     24326577 :             if (writePrefix(dev, meanData, SUMO_TAG_LANE, meanData.getLane()->getID())) {
     718       399032 :                 meanData.write(dev, myWrittenAttributes, stopTime - startTime, 1, meanData.getLane()->getSpeedLimit(),
     719       399032 :                                myPrintDefaults ? meanData.getLane()->getLength() / meanData.getLane()->getSpeedLimit() : -1.);
     720              :             }
     721      8108859 :             if (!MSNet::getInstance()->skipFinalReset()) {
     722      8108859 :                 meanData.reset(true);
     723              :             }
     724              :         }
     725      8007710 :         if (writeCheck) {
     726       618594 :             dev.closeTag();
     727              :         }
     728              :     } else {
     729      8480432 :         if (myTrackVehicles) {
     730        22440 :             MeanDataValues& meanData = **edgeValues.begin();
     731        67320 :             if (writePrefix(dev, meanData, SUMO_TAG_EDGE, edge->getID())) {
     732          160 :                 meanData.write(dev, myWrittenAttributes, stopTime - startTime, edge->getNumDrivingLanes(), edge->getSpeedLimit(),
     733          160 :                                myPrintDefaults ? edge->getLength() / edge->getSpeedLimit() : -1.);
     734              :             }
     735        22440 :             if (!MSNet::getInstance()->skipFinalReset()) {
     736        22440 :                 meanData.reset(true);
     737              :             }
     738              :         } else {
     739      8457992 :             MeanDataValues* sumData = createValues(nullptr, edge->getLength(), false);
     740     17064782 :             for (lane = edgeValues.begin(); lane != edgeValues.end(); ++lane) {
     741      8606790 :                 MeanDataValues& meanData = **lane;
     742      8606790 :                 meanData.addTo(*sumData);
     743      8606790 :                 if (!MSNet::getInstance()->skipFinalReset()) {
     744      8606790 :                     meanData.reset();
     745              :                 }
     746              :             }
     747     16915984 :             if (writePrefix(dev, *sumData, SUMO_TAG_EDGE, getEdgeID(edge))) {
     748       499234 :                 sumData->write(dev, myWrittenAttributes, stopTime - startTime, edge->getNumDrivingLanes(), edge->getSpeedLimit(),
     749       499234 :                                myPrintDefaults ? edge->getLength() / edge->getSpeedLimit() : -1.);
     750              :             }
     751      8457991 :             delete sumData;
     752              :         }
     753              :     }
     754              : }
     755              : 
     756              : 
     757              : void
     758       434202 : MSMeanData::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
     759       868404 :     dev.openTag(SUMO_TAG_INTERVAL).writeAttr(SUMO_ATTR_BEGIN, time2string(startTime)).writeAttr(SUMO_ATTR_END, time2string(stopTime));
     760       434202 :     dev.writeAttr(SUMO_ATTR_ID, myID);
     761       434202 : }
     762              : 
     763              : 
     764              : bool
     765     17947329 : MSMeanData::writePrefix(OutputDevice& dev, const MeanDataValues& values, const SumoXMLTag tag, const std::string id) const {
     766     17947329 :     if (myDumpEmpty || !values.isEmpty()) {
     767       956296 :         dev.openTag(tag);
     768       956296 :         dev.writeAttr(SUMO_ATTR_ID, id);
     769       956296 :         dev.writeOptionalAttr(SUMO_ATTR_SAMPLEDSECONDS, values.getSamples(), myWrittenAttributes);
     770       956296 :         return true;
     771              :     }
     772              :     return false;
     773              : }
     774              : 
     775              : 
     776              : void
     777       445511 : MSMeanData::writeXMLOutput(OutputDevice& dev,
     778              :                            SUMOTime startTime, SUMOTime stopTime) {
     779              :     // check whether this dump shall be written for the current time
     780       445511 :     int numReady = myDumpBegin < stopTime && myDumpEnd - DELTA_T >= startTime ? 1 : 0;
     781       445511 :     if (myTrackVehicles && myDumpBegin < stopTime) {
     782         2124 :         myPendingIntervals.push_back(std::make_pair(startTime, stopTime));
     783         2124 :         numReady = (int)myPendingIntervals.size();
     784        63312 :         for (std::vector<std::vector<MeanDataValues*> >::const_iterator i = myMeasures.begin(); i != myMeasures.end(); ++i) {
     785       124304 :             for (std::vector<MeanDataValues*>::const_iterator j = (*i).begin(); j != (*i).end(); ++j) {
     786        63116 :                 numReady = MIN2(numReady, ((MeanDataValueTracker*)*j)->getNumReady());
     787        63116 :                 if (numReady == 0) {
     788              :                     break;
     789              :                 }
     790              :             }
     791        63092 :             if (numReady == 0) {
     792              :                 break;
     793              :             }
     794              :         }
     795              :     }
     796       445511 :     const bool partialInterval = startTime < myInitTime;
     797       445511 :     if (numReady == 0 || myTrackVehicles || partialInterval) {
     798        12645 :         resetOnly(stopTime);
     799              :     }
     800       445511 :     if (partialInterval) {
     801              :         return;
     802              :     }
     803       879954 :     while (numReady-- > 0) {
     804       434454 :         if (!myPendingIntervals.empty()) {
     805         1588 :             startTime = myPendingIntervals.front().first;
     806         1588 :             stopTime = myPendingIntervals.front().second;
     807         1588 :             myPendingIntervals.pop_front();
     808              :         }
     809       434454 :         openInterval(dev, startTime, stopTime);
     810       434454 :         if (myAggregate == AggregateType::YES) {
     811          940 :             writeAggregated(dev, startTime, stopTime);
     812       433514 :         } else if (myAggregate == AggregateType::TAZ) {
     813           18 :             writeAggregatedTAZ(dev, startTime, stopTime);
     814              :         } else {
     815              :             MSEdgeVector::const_iterator edge = myEdges.begin();
     816     18281219 :             for (const std::vector<MeanDataValues*>& measures : myMeasures) {
     817     17847724 :                 writeEdge(dev, measures, *edge, startTime, stopTime);
     818              :                 ++edge;
     819              :             }
     820              :         }
     821       868906 :         dev.closeTag();
     822              :     }
     823              :     dev.flush();
     824              : }
     825              : 
     826              : 
     827              : void
     828        20215 : MSMeanData::writeXMLDetectorProlog(OutputDevice& dev) const {
     829        40430 :     dev.writeXMLHeader("meandata", "meandata_file.xsd");
     830        20215 : }
     831              : 
     832              : 
     833              : void
     834     63052005 : MSMeanData::detectorUpdate(const SUMOTime step) {
     835     63052005 :     if (step + DELTA_T == myDumpBegin) {
     836          639 :         init();
     837              :     }
     838     63052005 : }
     839              : 
     840              : 
     841              : const std::vector<MSMeanData::MeanDataValues*>*
     842            0 : MSMeanData::getEdgeValues(const MSEdge* edge) const {
     843              :     auto it = myEdgeIndex.find(edge);
     844            0 :     if (it != myEdgeIndex.end()) {
     845            0 :         return &myMeasures[it->second];
     846              :     } else {
     847              :         return nullptr;
     848              :     }
     849              : }
     850              : 
     851              : 
     852              : const std::vector<MSMoveReminder*>
     853            1 : MSMeanData::getReminders() const {
     854              :     std::vector<MSMoveReminder*> result;
     855           45 :     for (auto vec : myMeasures) {
     856           44 :         result.insert(result.end(), vec.begin(), vec.end());
     857           44 :     }
     858            1 :     return result;
     859            0 : }
     860              : 
     861              : 
     862              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1