LCOV - code coverage report
Current view: top level - src/mesosim - MELoop.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 97.2 % 142 138
Test Date: 2026-03-02 16:00:03 Functions: 93.3 % 15 14

            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    MELoop.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @date    Tue, May 2005
      17              : ///
      18              : // The main mesocopic simulation loop
      19              : /****************************************************************************/
      20              : #include <config.h>
      21              : 
      22              : #include <queue>
      23              : #include <vector>
      24              : #include <map>
      25              : #include <cmath>
      26              : 
      27              : #include <microsim/MSNet.h>
      28              : #include <microsim/MSEdge.h>
      29              : #include <microsim/MSGlobals.h>
      30              : #include <microsim/MSLane.h>
      31              : #include <microsim/MSVehicle.h>
      32              : #include <microsim/MSVehicleControl.h>
      33              : #include <utils/options/OptionsCont.h>
      34              : #include <utils/common/ToString.h>
      35              : #include <utils/common/FileHelpers.h>
      36              : #include <utils/common/SUMOTime.h>
      37              : #include <utils/common/RandHelper.h>
      38              : #include "MELoop.h"
      39              : #include "MESegment.h"
      40              : #include "MEVehicle.h"
      41              : 
      42              : 
      43              : // ===========================================================================
      44              : // method definitions
      45              : // ===========================================================================
      46         5970 : MELoop::MELoop(const SUMOTime recheckInterval) : myFullRecheckInterval(recheckInterval), myLinkRecheckInterval(TIME2STEPS(1)) {
      47         5970 : }
      48              : 
      49         5872 : MELoop::~MELoop() {
      50       297650 :     for (std::vector<MESegment*>::const_iterator j = myEdges2FirstSegments.begin(); j != myEdges2FirstSegments.end(); ++j) {
      51       929607 :         for (MESegment* s = *j; s != nullptr;) {
      52              :             MESegment* n = s->getNextSegment();
      53       637829 :             delete s;
      54              :             s = n;
      55              :         }
      56              :     }
      57         5872 : }
      58              : 
      59              : 
      60              : void
      61     13132720 : MELoop::simulate(SUMOTime tMax) {
      62     45938017 :     while (!myLeaderCars.empty()) {
      63     44248347 :         const SUMOTime time = myLeaderCars.begin()->first;
      64     44248347 :         std::vector<MEVehicle*> vehs = myLeaderCars[time];
      65              :         assert(time > tMax - DELTA_T || vehs.size() == 0);
      66     44248347 :         if (time > tMax) {
      67              :             return;
      68              :         }
      69              :         myLeaderCars.erase(time);
      70     67827899 :         for (std::vector<MEVehicle*>::const_iterator i = vehs.begin(); i != vehs.end(); ++i) {
      71     35022602 :             checkCar(*i);
      72              :             assert(myLeaderCars.empty() || myLeaderCars.begin()->first >= time);
      73              :         }
      74     44248347 :     }
      75              : }
      76              : 
      77              : 
      78              : SUMOTime
      79     35037960 : MELoop::changeSegment(MEVehicle* veh, SUMOTime leaveTime, MESegment* const toSegment, MSMoveReminder::Notification reason, const bool ignoreLink) const {
      80     35037960 :     int qIdx = 0;
      81              :     MESegment* const onSegment = veh->getSegment();
      82              :     if (MESegment::isInvalid(toSegment)) {
      83       679302 :         if (veh->isStoppedTriggered()) {
      84         2198 :             return leaveTime + MAX2(SUMOTime(1), myLinkRecheckInterval);
      85              :         }
      86       677104 :         if (onSegment != nullptr) {
      87       675498 :             onSegment->send(veh, toSegment, qIdx, leaveTime, reason);
      88              :         } else {
      89         4818 :             WRITE_WARNINGF(TL("Vehicle '%' teleports beyond arrival edge '%', time=%."),
      90              :                            veh->getID(), veh->getEdge()->getID(), time2string(leaveTime));
      91              :         }
      92       677104 :         veh->setSegment(toSegment); // signal arrival
      93       677104 :         MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
      94       677104 :         return leaveTime;
      95     34427300 :     } else if (!MSGlobals::gCheckRoutes && !ignoreLink && !MESegment::isInvalid(onSegment) && &onSegment->getEdge() != &toSegment->getEdge() &&
      96        29761 :                veh->getEdge()->allowedLanes(*veh->succEdge(1), veh->getVClass()) == nullptr) {
      97        26468 :         if (veh->isStopped()) {
      98            2 :             veh->processStop();
      99              :         }
     100              :         return SUMOTime_MAX;
     101              :     }
     102     34332190 :     const SUMOTime entry = toSegment->hasSpaceFor(veh, leaveTime, qIdx);
     103     34332190 :     if (entry == leaveTime && (ignoreLink || veh->mayProceed())) {
     104     23461542 :         if (onSegment != nullptr) {
     105     23460889 :             if (veh->getQueIndex() == MESegment::PARKING_QUEUE) { // parking or just aborted parking
     106         5415 :                 if (veh->isParking()) {
     107         5153 :                     veh->processStop();
     108              :                 }
     109         5415 :                 veh->getEdge()->getLanes()[0]->removeParking(veh);  // TODO for GUI only
     110              :             } else {
     111     43993963 :                 onSegment->send(veh, toSegment, qIdx, leaveTime, onSegment->getNextSegment() == nullptr ? MSMoveReminder::NOTIFICATION_JUNCTION : MSMoveReminder::NOTIFICATION_SEGMENT);
     112              :             }
     113     23460889 :             toSegment->receive(veh, qIdx, leaveTime, false, ignoreLink, &onSegment->getEdge() != &toSegment->getEdge());
     114              :         } else {
     115         1959 :             WRITE_WARNINGF(TL("Vehicle '%' ends teleporting on edge '%':%, time=%."),
     116              :                            veh->getID(), toSegment->getEdge().getID(), toSegment->getIndex(), time2string(leaveTime));
     117              :             // this is not quite correct but suffices for interrogation by
     118              :             // subsequent methods (veh->getSpeed() needs segment != 0)
     119          653 :             veh->setSegment(myEdges2FirstSegments[veh->getEdge()->getNumericalID()]);
     120              :             // clean up detectors (do not add traffic data)
     121              :             // note: updateDatector is not called if leaveTime == getLastEntryTime()
     122          653 :             veh->updateDetectors(veh->getLastEntryTime(), veh->getEventTime(), true, MSMoveReminder::NOTIFICATION_TELEPORT);
     123          653 :             toSegment->receive(veh, qIdx, leaveTime, false, true, true);
     124              :         }
     125     23461542 :         return entry;
     126              :     }
     127     10870648 :     if (entry == leaveTime && !ignoreLink) { // this is a long way of saying !veh->mayProceed() (which is a costly call)
     128      9290445 :         return entry + MAX2(SUMOTime(1), myLinkRecheckInterval);
     129              :     }
     130              :     return entry;
     131              : }
     132              : 
     133              : 
     134              : void
     135     35022602 : MELoop::checkCar(MEVehicle* veh) {
     136              :     const SUMOTime leaveTime = veh->getEventTime();
     137              :     MESegment* const onSegment = veh->getSegment();
     138     35022602 :     MESegment* const toSegment = veh->getQueIndex() == MESegment::PARKING_QUEUE ? onSegment : nextSegment(onSegment, veh);
     139     35022602 :     const bool teleporting = (onSegment == nullptr); // is the vehicle currently teleporting?
     140              :     // @note reason is only evaluated if toSegment == nullptr
     141     35022602 :     const SUMOTime nextEntry = changeSegment(veh, leaveTime, toSegment, MSMoveReminder::NOTIFICATION_ARRIVED, teleporting);
     142     35022602 :     if (nextEntry == leaveTime) {
     143              :         return;
     144              :     }
     145     10894224 :     const bool r1 = MSGlobals::gTimeToGridlock > 0 && veh->getWaitingTime() > MSGlobals::gTimeToGridlock;
     146     10894224 :     const bool r3 = MSGlobals::gTimeToTeleportDisconnected >= 0 && veh->getWaitingTime() > MSGlobals::gTimeToTeleportDisconnected;
     147     10894224 :     if (!veh->isStopped() && (r1 || r3)) {
     148         5871 :         const bool disconnected = (MSGlobals::gTimeToTeleportDisconnected >= 0
     149         1818 :                                    && veh->succEdge(1) != nullptr
     150         7689 :                                    && veh->getEdge()->allowedLanes(*veh->succEdge(1), veh->getVClass()) == nullptr);
     151         5871 :         if ((r1 && !disconnected) || (r3 && disconnected)) {
     152         4069 :             teleportVehicle(veh, toSegment, disconnected);
     153         4069 :             return;
     154              :         }
     155              :     }
     156     10890155 :     if (veh->getBlockTime() == SUMOTime_MAX && !veh->isStopped()) {
     157              :         veh->setBlockTime(leaveTime);
     158              :     }
     159     10890155 :     if (nextEntry == SUMOTime_MAX) {
     160              :         // all usable queues on the next segment are full
     161      1315115 :         SUMOTime newEventTime = MAX3(toSegment->getEventTime() + 1, leaveTime + 1, leaveTime + myFullRecheckInterval);
     162      1315115 :         if (MSGlobals::gTimeToGridlock > 0) {
     163              :             // if teleporting is enabled, make sure we look at the vehicle when the gridlock-time is up
     164       843036 :             const SUMOTime recheck = MSGlobals::gTimeToTeleportDisconnected >= 0 ? MIN2(MSGlobals::gTimeToGridlock, MSGlobals::gTimeToTeleportDisconnected) : MSGlobals::gTimeToGridlock;
     165       843036 :             newEventTime = MAX2(MIN2(newEventTime, veh->getBlockTime() + recheck + 1), leaveTime + DELTA_T);
     166              :         }
     167              :         veh->setEventTime(newEventTime);
     168              :     } else {
     169              :         // receiving segment has recently received another vehicle or the junction is blocked
     170              :         veh->setEventTime(nextEntry);
     171              :     }
     172     10890155 :     addLeaderCar(veh, teleporting ? nullptr : onSegment->getLink(veh));
     173              : }
     174              : 
     175              : 
     176              : void
     177         4069 : MELoop::teleportVehicle(MEVehicle* veh, MESegment* const toSegment, bool disconnected) {
     178              :     const SUMOTime leaveTime = veh->getEventTime();
     179              :     MESegment* const onSegment = veh->getSegment();
     180         4069 :     if (MSGlobals::gRemoveGridlocked) {
     181           48 :         WRITE_WARNINGF(TL("Teleporting vehicle '%'; waited too long, from edge '%':%, time=%."),
     182              :                        veh->getID(), onSegment->getEdge().getID(), onSegment->getIndex(),
     183              :                        time2string(leaveTime));
     184           16 :         MSNet::getInstance()->getVehicleControl().registerTeleportJam();
     185              :         int qIdx = 0;
     186           16 :         onSegment->send(veh, nullptr, qIdx, leaveTime, MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED);
     187           16 :         veh->setSegment(nullptr);
     188           16 :         MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
     189           16 :         return;
     190              :     }
     191              :     const bool teleporting = (onSegment == nullptr); // is the vehicle already teleporting?
     192              :     // try to find a place on the current edge
     193         4053 :     MESegment* teleSegment = disconnected ? toSegment : toSegment->getNextSegment();
     194         9143 :     while (teleSegment != nullptr && changeSegment(veh, leaveTime, teleSegment, MSMoveReminder::NOTIFICATION_TELEPORT, true) != leaveTime) {
     195              :         // @caution the time to get to the next segment here is ignored XXX
     196              :         teleSegment = teleSegment->getNextSegment();
     197              :     }
     198         4053 :     if (teleSegment != nullptr) {
     199          428 :         if (!teleporting) {
     200              :             // we managed to teleport in a single jump
     201          838 :             const std::string reason = disconnected ? " (disconnected)" : "";
     202         1272 :             WRITE_WARNINGF(TL("Teleporting vehicle '%'; waited too long%, from edge '%':% to edge '%':%, time=%."),
     203              :                            veh->getID(), reason, onSegment->getEdge().getID(), onSegment->getIndex(),
     204              :                            teleSegment->getEdge().getID(), teleSegment->getIndex(), time2string(leaveTime));
     205          424 :             MSNet::getInstance()->getVehicleControl().registerTeleportJam();
     206              :         }
     207              :     } else {
     208              :         // teleport across the current edge and try insertion later
     209         3625 :         if (!teleporting) {
     210              :             int qIdx = 0;
     211              :             // announce start of multi-step teleport, arrival will be announced in changeSegment()
     212         6813 :             WRITE_WARNINGF(TL("Teleporting vehicle '%'; waited too long, from edge '%':%, time=%."),
     213              :                            veh->getID(), onSegment->getEdge().getID(), onSegment->getIndex(), time2string(leaveTime));
     214         2271 :             MSNet::getInstance()->getVehicleControl().registerTeleportJam();
     215              :             // remove from current segment
     216         2271 :             onSegment->send(veh, nullptr, qIdx, leaveTime, MSMoveReminder::NOTIFICATION_TELEPORT);
     217              :             // mark veh as teleporting
     218         2271 :             veh->setSegment(nullptr);
     219              :         }
     220              :         // @caution microsim uses current travel time teleport duration
     221        10875 :         const SUMOTime teleArrival = leaveTime + TIME2STEPS(veh->getEdge()->getLength() / MAX2(veh->getEdge()->getSpeedLimit(), NUMERICAL_EPS));
     222         3625 :         const bool atDest = veh->moveRoutePointer();
     223         3625 :         if (atDest) {
     224              :             // teleporting to end of route
     225         1606 :             changeSegment(veh, teleArrival, nullptr, MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED, true);
     226              :         } else {
     227              :             veh->setEventTime(teleArrival);
     228         2019 :             addLeaderCar(veh, nullptr);
     229              :             // teleporting vehicles must react to rerouters
     230         2019 :             getSegmentForEdge(*veh->getEdge())->addReminders(veh);
     231         2019 :             veh->activateReminders(MSMoveReminder::NOTIFICATION_JUNCTION);
     232              :         }
     233              :     }
     234              : }
     235              : 
     236              : 
     237              : void
     238     35030413 : MELoop::addLeaderCar(MEVehicle* veh, MSLink* link) {
     239     35030413 :     myLeaderCars[veh->getEventTime()].push_back(veh);
     240     35030413 :     veh->setApproaching(link);
     241     35030413 : }
     242              : 
     243              : 
     244              : void
     245            2 : MELoop::clearState() {
     246              :     myLeaderCars.clear();
     247            2 : }
     248              : 
     249              : 
     250              : bool
     251         8584 : MELoop::removeLeaderCar(MEVehicle* v) {
     252         8584 :     const auto candIt = myLeaderCars.find(v->getEventTime());
     253         8584 :     if (candIt != myLeaderCars.end()) {
     254         2427 :         std::vector<MEVehicle*>& cands = candIt->second;
     255         2427 :         auto it = find(cands.begin(), cands.end(), v);
     256         2427 :         if (it != cands.end()) {
     257              :             cands.erase(it);
     258              :             return true;
     259              :         }
     260              :     }
     261              :     return false;
     262              : }
     263              : 
     264              : 
     265              : void
     266            0 : MELoop::vaporizeCar(MEVehicle* v, MSMoveReminder::Notification reason) {
     267              :     int qIdx = 0;
     268            0 :     v->getSegment()->send(v, nullptr, qIdx, MSNet::getInstance()->getCurrentTimeStep(), reason);
     269            0 :     removeLeaderCar(v);
     270            0 : }
     271              : 
     272              : 
     273              : MESegment*
     274     34829029 : MELoop::nextSegment(MESegment* s, MEVehicle* v) {
     275     34829029 :     if (s != nullptr) { // vehicle is not teleporting
     276              :         MESegment* next = s->getNextSegment();
     277     34827020 :         if (next != nullptr) {
     278              :             // ok, the street continues
     279              :             return next;
     280              :         }
     281              :     }
     282              :     // we have to check the next edge in the vehicle's route
     283     13730354 :     const MSEdge* nextEdge = v->succEdge(1);
     284     13730354 :     if (nextEdge == nullptr) {
     285              :         // end of route
     286              :         return nullptr;
     287              :     }
     288     13060892 :     return myEdges2FirstSegments[nextEdge->getNumericalID()];
     289              : }
     290              : 
     291              : 
     292              : int
     293       434422 : MELoop::numSegmentsFor(const double length, const double sLength) {
     294       434422 :     int no = (int)floor(length / sLength + 0.5);
     295       434422 :     if (no == 0) { // assure there is at least one segment
     296              :         return 1;
     297              :     } else {
     298       180481 :         return no;
     299              :     }
     300              : }
     301              : 
     302              : 
     303              : void
     304       294201 : MELoop::buildSegmentsFor(const MSEdge& e, const OptionsCont& oc) {
     305       294201 :     const MESegment::MesoEdgeType& edgeType = MSNet::getInstance()->getMesoType(e.getEdgeType());
     306              :     const double length = e.getLength();
     307       294201 :     const int numSegments = numSegmentsFor(length, edgeType.edgeLength);
     308       294201 :     const double slength = length / (double)numSegments;
     309              :     MESegment* newSegment = nullptr;
     310              :     MESegment* nextSegment = nullptr;
     311       294201 :     const bool laneQueue = oc.getBool("meso-lane-queue");
     312       571384 :     bool multiQueue = laneQueue || (oc.getBool("meso-multi-queue") && e.getLanes().size() > 1 && e.getNumSuccessors() > 1);
     313       936631 :     for (int s = numSegments - 1; s >= 0; s--) {
     314      1284860 :         std::string id = e.getID() + ":" + toString(s);
     315       642430 :         newSegment = new MESegment(id, e, nextSegment, slength, e.getLanes()[0]->getSpeedLimit(), s, multiQueue, edgeType);
     316              :         multiQueue = laneQueue;
     317              :         nextSegment = newSegment;
     318              :     }
     319       588402 :     while (e.getNumericalID() >= static_cast<int>(myEdges2FirstSegments.size())) {
     320       294201 :         myEdges2FirstSegments.push_back(0);
     321              :     }
     322       294201 :     myEdges2FirstSegments[e.getNumericalID()] = newSegment;
     323       630201 :     for (MSLane* lane : e.getLanes()) {
     324       336000 :         lane->updateMesoGUISegments();
     325              :     }
     326       294201 : }
     327              : 
     328              : 
     329              : MESegment*
     330     56051846 : MELoop::getSegmentForEdge(const MSEdge& e, double pos) {
     331     56051846 :     if (e.getNumericalID() >= (int)myEdges2FirstSegments.size()) {
     332              :         return nullptr;
     333              :     }
     334     56051826 :     MESegment* s = myEdges2FirstSegments[e.getNumericalID()];
     335     56051826 :     if (pos > 0) {
     336              :         double cpos = 0;
     337       893125 :         while (s->getNextSegment() != nullptr && cpos + s->getLength() < pos) {
     338              :             cpos += s->getLength();
     339              :             s = s->getNextSegment();
     340              :         }
     341              :     }
     342              :     return s;
     343              : }
     344              : 
     345              : 
     346              : bool
     347       214426 : MELoop::isEnteringRoundabout(const MSEdge& e) {
     348       455190 :     for (const MSEdge* succ : e.getSuccessors()) {
     349       240776 :         if (succ->isRoundabout()) {
     350              :             return true;
     351              :         }
     352              :     }
     353              :     return false;
     354              : }
     355              : 
     356              : 
     357              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1