LCOV - code coverage report
Current view: top level - src/mesosim - MELoop.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 97.2 % 141 137
Test Date: 2024-11-20 15:55:46 Functions: 93.8 % 16 15

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      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         5418 : MELoop::MELoop(const SUMOTime recheckInterval) : myFullRecheckInterval(recheckInterval), myLinkRecheckInterval(TIME2STEPS(1)) {
      47         5418 : }
      48              : 
      49         5321 : MELoop::~MELoop() {
      50       259029 :     for (std::vector<MESegment*>::const_iterator j = myEdges2FirstSegments.begin(); j != myEdges2FirstSegments.end(); ++j) {
      51       811476 :         for (MESegment* s = *j; s != nullptr;) {
      52              :             MESegment* n = s->getNextSegment();
      53       557768 :             delete s;
      54              :             s = n;
      55              :         }
      56              :     }
      57         5321 : }
      58              : 
      59              : 
      60              : void
      61     11687959 : MELoop::simulate(SUMOTime tMax) {
      62     43312048 :     while (!myLeaderCars.empty()) {
      63     42013860 :         const SUMOTime time = myLeaderCars.begin()->first;
      64     42013860 :         std::vector<MEVehicle*> vehs = myLeaderCars[time];
      65              :         assert(time > tMax - DELTA_T || vehs.size() == 0);
      66     42013860 :         if (time > tMax) {
      67              :             return;
      68              :         }
      69              :         myLeaderCars.erase(time);
      70     65473690 :         for (std::vector<MEVehicle*>::const_iterator i = vehs.begin(); i != vehs.end(); ++i) {
      71     33849601 :             checkCar(*i);
      72              :             assert(myLeaderCars.empty() || myLeaderCars.begin()->first >= time);
      73              :         }
      74     42013860 :     }
      75              : }
      76              : 
      77              : 
      78              : SUMOTime
      79     33864929 : MELoop::changeSegment(MEVehicle* veh, SUMOTime leaveTime, MESegment* const toSegment, MSMoveReminder::Notification reason, const bool ignoreLink) const {
      80     33864929 :     int qIdx = 0;
      81              :     MESegment* const onSegment = veh->getSegment();
      82              :     if (MESegment::isInvalid(toSegment)) {
      83       644291 :         if (veh->isStoppedTriggered()) {
      84         2198 :             return leaveTime + MAX2(SUMOTime(1), myLinkRecheckInterval);
      85              :         }
      86       642093 :         if (onSegment != nullptr) {
      87       640423 :             onSegment->send(veh, toSegment, qIdx, leaveTime, reason);
      88              :         } else {
      89         5010 :             WRITE_WARNINGF(TL("Vehicle '%' teleports beyond arrival edge '%', time=%."),
      90              :                            veh->getID(), veh->getEdge()->getID(), time2string(leaveTime));
      91              :         }
      92       642093 :         veh->setSegment(toSegment); // signal arrival
      93       642093 :         MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
      94       642093 :         return leaveTime;
      95              :     }
      96     33220638 :     const SUMOTime entry = toSegment->hasSpaceFor(veh, leaveTime, qIdx);
      97     33220638 :     if (entry == leaveTime && (ignoreLink || veh->mayProceed())) {
      98     23129248 :         if (onSegment != nullptr) {
      99     23128547 :             if (veh->getQueIndex() == MESegment::PARKING_QUEUE) { // parking or just aborted parking
     100         1057 :                 if (veh->isParking()) {
     101         1053 :                     veh->processStop();
     102              :                 }
     103         1057 :                 veh->getEdge()->getLanes()[0]->removeParking(veh);  // TODO for GUI only
     104              :             } else {
     105     43501137 :                 onSegment->send(veh, toSegment, qIdx, leaveTime, onSegment->getNextSegment() == nullptr ? MSMoveReminder::NOTIFICATION_JUNCTION : MSMoveReminder::NOTIFICATION_SEGMENT);
     106              :             }
     107     23128547 :             toSegment->receive(veh, qIdx, leaveTime, false, ignoreLink, &onSegment->getEdge() != &toSegment->getEdge());
     108              :         } else {
     109         2103 :             WRITE_WARNINGF(TL("Vehicle '%' ends teleporting on edge '%':%, time=%."),
     110              :                            veh->getID(), toSegment->getEdge().getID(), toSegment->getIndex(), time2string(leaveTime));
     111              :             // this is not quite correct but suffices for interrogation by
     112              :             // subsequent methods (veh->getSpeed() needs segment != 0)
     113          701 :             veh->setSegment(myEdges2FirstSegments[veh->getEdge()->getNumericalID()]);
     114              :             // clean up detectors (do not add traffic data)
     115              :             // note: updateDatector is not called if leaveTime == getLastEntryTime()
     116          701 :             veh->updateDetectors(veh->getLastEntryTime(), true, MSMoveReminder::NOTIFICATION_TELEPORT);
     117          701 :             toSegment->receive(veh, qIdx, leaveTime, false, true, true);
     118              :         }
     119     23129248 :         return entry;
     120              :     }
     121     10091390 :     if (entry == leaveTime && !ignoreLink) { // this is a long way of saying !veh->mayProceed() (which is a costly call)
     122      8502825 :         return entry + MAX2(SUMOTime(1), myLinkRecheckInterval);
     123              :     }
     124              :     return entry;
     125              : }
     126              : 
     127              : 
     128              : void
     129     33849601 : MELoop::checkCar(MEVehicle* veh) {
     130              :     const SUMOTime leaveTime = veh->getEventTime();
     131              :     MESegment* const onSegment = veh->getSegment();
     132     33849601 :     MESegment* const toSegment = veh->getQueIndex() == MESegment::PARKING_QUEUE ? onSegment : nextSegment(onSegment, veh);
     133     33849601 :     const bool teleporting = (onSegment == nullptr); // is the vehicle currently teleporting?
     134              :     // @note reason is only evaluated if toSegment == nullptr
     135     33849601 :     const SUMOTime nextEntry = changeSegment(veh, leaveTime, toSegment, MSMoveReminder::NOTIFICATION_ARRIVED, teleporting);
     136     33849601 :     if (nextEntry == leaveTime) {
     137              :         return;
     138              :     }
     139     10088520 :     const bool r1 = MSGlobals::gTimeToGridlock > 0 && veh->getWaitingTime() > MSGlobals::gTimeToGridlock;
     140     10088520 :     const bool r3 = MSGlobals::gTimeToTeleportDisconnected >= 0 && veh->getWaitingTime() > MSGlobals::gTimeToTeleportDisconnected;
     141     10088520 :     if (!veh->isStopped() && (r1 || r3)) {
     142         5250 :         const bool disconnected = (MSGlobals::gTimeToTeleportDisconnected >= 0
     143         1039 :                                    && veh->succEdge(1) != nullptr
     144         6289 :                                    && veh->getEdge()->allowedLanes(*veh->succEdge(1), veh->getVClass()) == nullptr);
     145         5250 :         if ((r1 && !disconnected) || (r3 && disconnected)) {
     146         4217 :             teleportVehicle(veh, toSegment);
     147         4217 :             return;
     148              :         }
     149              :     }
     150     10084303 :     if (veh->getBlockTime() == SUMOTime_MAX && !veh->isStopped()) {
     151              :         veh->setBlockTime(leaveTime);
     152              :     }
     153     10084303 :     if (nextEntry == SUMOTime_MAX) {
     154              :         // all usable queues on the next segment are full
     155      1309049 :         SUMOTime newEventTime = MAX3(toSegment->getEventTime() + 1, leaveTime + 1, leaveTime + myFullRecheckInterval);
     156      1309049 :         if (MSGlobals::gTimeToGridlock > 0) {
     157              :             // if teleporting is enabled, make sure we look at the vehicle when the gridlock-time is up
     158       850468 :             const SUMOTime recheck = MSGlobals::gTimeToTeleportDisconnected >= 0 ? MIN2(MSGlobals::gTimeToGridlock, MSGlobals::gTimeToTeleportDisconnected) : MSGlobals::gTimeToGridlock;
     159       850468 :             newEventTime = MAX2(MIN2(newEventTime, veh->getBlockTime() + recheck + 1), leaveTime + DELTA_T);
     160              :         }
     161              :         veh->setEventTime(newEventTime);
     162              :     } else {
     163              :         // receiving segment has recently received another vehicle or the junction is blocked
     164              :         veh->setEventTime(nextEntry);
     165              :     }
     166     10084303 :     addLeaderCar(veh, onSegment->getLink(veh));
     167              : }
     168              : 
     169              : 
     170              : void
     171         4217 : MELoop::teleportVehicle(MEVehicle* veh, MESegment* const toSegment) {
     172              :     const SUMOTime leaveTime = veh->getEventTime();
     173              :     MESegment* const onSegment = veh->getSegment();
     174         4217 :     if (MSGlobals::gRemoveGridlocked) {
     175           48 :         WRITE_WARNINGF(TL("Teleporting vehicle '%'; waited too long, from edge '%':%, time=%."),
     176              :                        veh->getID(), onSegment->getEdge().getID(), onSegment->getIndex(),
     177              :                        time2string(leaveTime));
     178           16 :         MSNet::getInstance()->getVehicleControl().registerTeleportJam();
     179              :         int qIdx = 0;
     180           16 :         onSegment->send(veh, nullptr, qIdx, leaveTime, MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED);
     181           16 :         veh->setSegment(nullptr);
     182           16 :         MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
     183           16 :         return;
     184              :     }
     185              :     const bool teleporting = (onSegment == nullptr); // is the vehicle already teleporting?
     186              :     // try to find a place on the current edge
     187              :     MESegment* teleSegment = toSegment->getNextSegment();
     188         9269 :     while (teleSegment != nullptr && changeSegment(veh, leaveTime, teleSegment, MSMoveReminder::NOTIFICATION_TELEPORT, true) != leaveTime) {
     189              :         // @caution the time to get to the next segment here is ignored XXX
     190              :         teleSegment = teleSegment->getNextSegment();
     191              :     }
     192         4201 :     if (teleSegment != nullptr) {
     193          440 :         if (!teleporting) {
     194              :             // we managed to teleport in a single jump
     195         1248 :             WRITE_WARNINGF(TL("Teleporting vehicle '%'; waited too long, from edge '%':% to edge '%':%, time=%."),
     196              :                            veh->getID(), onSegment->getEdge().getID(), onSegment->getIndex(),
     197              :                            teleSegment->getEdge().getID(), teleSegment->getIndex(), time2string(leaveTime));
     198          416 :             MSNet::getInstance()->getVehicleControl().registerTeleportJam();
     199              :         }
     200              :     } else {
     201              :         // teleport across the current edge and try insertion later
     202         3761 :         if (!teleporting) {
     203              :             int qIdx = 0;
     204              :             // announce start of multi-step teleport, arrival will be announced in changeSegment()
     205         7149 :             WRITE_WARNINGF(TL("Teleporting vehicle '%'; waited too long, from edge '%':%, time=%."),
     206              :                            veh->getID(), onSegment->getEdge().getID(), onSegment->getIndex(), time2string(leaveTime));
     207         2383 :             MSNet::getInstance()->getVehicleControl().registerTeleportJam();
     208              :             // remove from current segment
     209         2383 :             onSegment->send(veh, nullptr, qIdx, leaveTime, MSMoveReminder::NOTIFICATION_TELEPORT);
     210              :             // mark veh as teleporting
     211         2383 :             veh->setSegment(nullptr);
     212              :         }
     213              :         // @caution microsim uses current travel time teleport duration
     214        11283 :         const SUMOTime teleArrival = leaveTime + TIME2STEPS(veh->getEdge()->getLength() / MAX2(veh->getEdge()->getSpeedLimit(), NUMERICAL_EPS));
     215         3761 :         const bool atDest = veh->moveRoutePointer();
     216         3761 :         if (atDest) {
     217              :             // teleporting to end of route
     218         1670 :             changeSegment(veh, teleArrival, nullptr, MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED, true);
     219              :         } else {
     220              :             veh->setEventTime(teleArrival);
     221         2091 :             addLeaderCar(veh, nullptr);
     222              :             // teleporting vehicles must react to rerouters
     223         2091 :             getSegmentForEdge(*veh->getEdge())->addReminders(veh);
     224         2091 :             veh->activateReminders(MSMoveReminder::NOTIFICATION_JUNCTION);
     225              :         }
     226              :     }
     227              : }
     228              : 
     229              : 
     230              : void
     231     33857098 : MELoop::addLeaderCar(MEVehicle* veh, MSLink* link) {
     232     33857098 :     myLeaderCars[veh->getEventTime()].push_back(veh);
     233     33857098 :     veh->setApproaching(link);
     234     33857098 : }
     235              : 
     236              : 
     237              : void
     238            2 : MELoop::clearState() {
     239              :     myLeaderCars.clear();
     240            2 : }
     241              : 
     242              : 
     243              : bool
     244         8484 : MELoop::removeLeaderCar(MEVehicle* v) {
     245         8484 :     const auto candIt = myLeaderCars.find(v->getEventTime());
     246         8484 :     if (candIt != myLeaderCars.end()) {
     247         2335 :         std::vector<MEVehicle*>& cands = candIt->second;
     248         2335 :         auto it = find(cands.begin(), cands.end(), v);
     249         2335 :         if (it != cands.end()) {
     250              :             cands.erase(it);
     251              :             return true;
     252              :         }
     253              :     }
     254              :     return false;
     255              : }
     256              : 
     257              : 
     258              : void
     259            0 : MELoop::vaporizeCar(MEVehicle* v, MSMoveReminder::Notification reason) {
     260              :     int qIdx = 0;
     261            0 :     v->getSegment()->send(v, nullptr, qIdx, MSNet::getInstance()->getCurrentTimeStep(), reason);
     262            0 :     removeLeaderCar(v);
     263            0 : }
     264              : 
     265              : 
     266              : MESegment*
     267     33750090 : MELoop::nextSegment(MESegment* s, MEVehicle* v) {
     268     33750090 :     if (s != nullptr) { // vehicle is not teleporting
     269              :         MESegment* next = s->getNextSegment();
     270     33748011 :         if (next != nullptr) {
     271              :             // ok, the street continues
     272              :             return next;
     273              :         }
     274              :     }
     275              :     // we have to check the next edge in the vehicle's route
     276     12827370 :     const MSEdge* nextEdge = v->succEdge(1);
     277     12827370 :     if (nextEdge == nullptr) {
     278              :         // end of route
     279              :         return nullptr;
     280              :     }
     281     12192899 :     return myEdges2FirstSegments[nextEdge->getNumericalID()];
     282              : }
     283              : 
     284              : 
     285              : int
     286       377885 : MELoop::numSegmentsFor(const double length, const double sLength) {
     287       377885 :     int no = (int)floor(length / sLength + 0.5);
     288       377885 :     if (no == 0) { // assure there is at least one segment
     289              :         return 1;
     290              :     } else {
     291       161951 :         return no;
     292              :     }
     293              : }
     294              : 
     295              : 
     296              : void
     297       256629 : MELoop::buildSegmentsFor(const MSEdge& e, const OptionsCont& oc) {
     298       256629 :     const MESegment::MesoEdgeType& edgeType = MSNet::getInstance()->getMesoType(e.getEdgeType());
     299              :     const double length = e.getLength();
     300       256629 :     const int numSegments = numSegmentsFor(length, oc.getFloat("meso-edgelength"));
     301       256629 :     const double slength = length / (double)numSegments;
     302              :     MESegment* newSegment = nullptr;
     303              :     MESegment* nextSegment = nullptr;
     304       256629 :     const bool laneQueue = oc.getBool("meso-lane-queue");
     305       499122 :     bool multiQueue = laneQueue || (oc.getBool("meso-multi-queue") && e.getLanes().size() > 1 && e.getNumSuccessors() > 1);
     306       819483 :     for (int s = numSegments - 1; s >= 0; s--) {
     307      1125708 :         std::string id = e.getID() + ":" + toString(s);
     308       562854 :         newSegment = new MESegment(id, e, nextSegment, slength, e.getLanes()[0]->getSpeedLimit(), s, multiQueue, edgeType);
     309              :         multiQueue = laneQueue;
     310              :         nextSegment = newSegment;
     311              :     }
     312       513258 :     while (e.getNumericalID() >= static_cast<int>(myEdges2FirstSegments.size())) {
     313       256629 :         myEdges2FirstSegments.push_back(0);
     314              :     }
     315       256629 :     myEdges2FirstSegments[e.getNumericalID()] = newSegment;
     316       256629 : }
     317              : 
     318              : 
     319              : void
     320          160 : MELoop::updateSegmentsForEdge(const MSEdge& e) {
     321          160 :     if (e.getNumericalID() < (int)myEdges2FirstSegments.size()) {
     322          160 :         const MESegment::MesoEdgeType& edgeType = MSNet::getInstance()->getMesoType(e.getEdgeType());
     323          160 :         MESegment* s = myEdges2FirstSegments[e.getNumericalID()];
     324          400 :         while (s != nullptr) {
     325          240 :             s->initSegment(edgeType, e, s->getCapacity());
     326              :             s = s->getNextSegment();
     327              :         }
     328              :     }
     329          160 : }
     330              : 
     331              : 
     332              : MESegment*
     333     49068070 : MELoop::getSegmentForEdge(const MSEdge& e, double pos) {
     334     49068070 :     if (e.getNumericalID() >= (int)myEdges2FirstSegments.size()) {
     335              :         return nullptr;
     336              :     }
     337     49068050 :     MESegment* s = myEdges2FirstSegments[e.getNumericalID()];
     338     49068050 :     if (pos > 0) {
     339              :         double cpos = 0;
     340       784894 :         while (s->getNextSegment() != nullptr && cpos + s->getLength() < pos) {
     341              :             cpos += s->getLength();
     342              :             s = s->getNextSegment();
     343              :         }
     344              :     }
     345              :     return s;
     346              : }
     347              : 
     348              : 
     349              : bool
     350       186336 : MELoop::isEnteringRoundabout(const MSEdge& e) {
     351       398651 :     for (const MSEdge* succ : e.getSuccessors()) {
     352       212327 :         if (succ->isRoundabout()) {
     353              :             return true;
     354              :         }
     355              :     }
     356              :     return false;
     357              : }
     358              : 
     359              : 
     360              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1