View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import javax.naming.NamingException;
4   
5   import org.djunits.unit.DirectionUnit;
6   import org.djunits.unit.LengthUnit;
7   import org.djunits.unit.SpeedUnit;
8   import org.djunits.value.vdouble.scalar.Direction;
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.djunits.value.vdouble.scalar.Speed;
12  import org.djutils.draw.point.Point2d;
13  import org.junit.jupiter.api.Test;
14  import org.opentrafficsim.base.geometry.OtsLine2d;
15  import org.opentrafficsim.core.definitions.DefaultsNl;
16  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
17  import org.opentrafficsim.core.gtu.GtuException;
18  import org.opentrafficsim.core.gtu.GtuType;
19  import org.opentrafficsim.core.network.NetworkException;
20  import org.opentrafficsim.core.network.Node;
21  import org.opentrafficsim.road.car.CarTest;
22  import org.opentrafficsim.road.definitions.DefaultsRoadNl;
23  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
24  import org.opentrafficsim.road.network.RoadNetwork;
25  import org.opentrafficsim.road.network.factory.LaneFactory;
26  
27  import nl.tudelft.simulation.dsol.SimRuntimeException;
28  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
29  
30  /**
31   * Verify that GTUs register and unregister at the correct times and locations when following a curve.
32   * <p>
33   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
34   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
35   * </p>
36   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
37   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
38   * @author <a href="https://www.citg.tudelft.nl">Guus Tamminga</a>
39   */
40  public final class CurveTest
41  {
42  
43      /** Verbose test. */
44      private static final boolean VERBOSE = false;
45  
46      /** */
47      private CurveTest()
48      {
49          // do not instantiate test class
50      }
51  
52      /**
53       * Let GTUs drive through a curve and check (de-)registration times at each node.
54       * @throws NamingException on error
55       * @throws SimRuntimeException on error
56       * @throws NetworkException on error
57       * @throws GtuException on error
58       */
59      @Test
60      public void curveTest() throws SimRuntimeException, NamingException, NetworkException, GtuException
61      {
62          final int laneCount = 1;
63          OtsSimulatorInterface simulator = CarTest.makeSimulator();
64          RoadNetwork network = new RoadNetwork("curve test network", simulator);
65          GtuType gtuType = DefaultsNl.CAR;
66          LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
67          Speed speedLimit = new Speed(50, SpeedUnit.KM_PER_HOUR);
68          Node origin = new Node(network, "origin", new Point2d(10, 10), Direction.ZERO);
69          Node curveStart = new Node(network, "curveStart", new Point2d(100, 10), Direction.ZERO);
70          Node curveEnd = new Node(network, "curveEnd", new Point2d(150, 60), new Direction(90, DirectionUnit.EAST_DEGREE));
71          Node destination =
72                  new Node(network, "destination", new Point2d(150, 150), new Direction(90, DirectionUnit.EAST_DEGREE));
73          Lane[] straight1 = LaneFactory.makeMultiLane(network, "straight1", origin, curveStart, null, laneCount, laneType,
74                  speedLimit, simulator, DefaultsNl.VEHICLE);
75          Lane[] straight2 = LaneFactory.makeMultiLane(network, "straight2", curveEnd, destination, null, laneCount, laneType,
76                  speedLimit, simulator, DefaultsNl.VEHICLE);
77          OtsLine2d curveLine = LaneFactory.makeBezier(origin, curveStart, curveEnd, destination);
78          Lane[] curve = LaneFactory.makeMultiLane(network, "bezier", curveStart, curveEnd,
79                  curveLine.getPointList().toArray(new Point2d[curveLine.size()]), laneCount, laneType, speedLimit, simulator,
80                  DefaultsNl.VEHICLE);
81          Lane[][] laneSets = new Lane[][] {straight1, curve, straight2};
82          Length initialPosition = new Length(5, LengthUnit.METER);
83          Speed speed = new Speed(10, SpeedUnit.SI);
84          for (int lane = 0; lane < laneCount; lane++)
85          {
86              // System.out.println("Lane is " + lane);
87              double cumulativeLength = 0;
88              for (Lane[] set : laneSets)
89              {
90                  cumulativeLength += set[lane].getLength().si;
91                  double timeAtEnd = simulator.getSimulatorTime().si + (cumulativeLength - initialPosition.si) / speed.si;
92                  if (VERBOSE)
93                  {
94                      System.out.println("lane " + set[lane] + " length is " + set[lane].getLength()
95                              + " time for reference to get to end " + timeAtEnd);
96                  }
97              }
98              LaneBasedGtu car =
99                      CarTest.makeReferenceCar("car", gtuType, straight1[lane], initialPosition, speed, (RoadNetwork) network);
100             if (VERBOSE)
101             {
102                 printEventList(simulator);
103                 System.out.println("STEP");
104             }
105             simulator.step();
106             if (VERBOSE)
107             {
108                 printEventList(simulator);
109                 System.out.println("STEP");
110             }
111             simulator.step();
112             if (VERBOSE)
113             {
114                 printEventList(simulator);
115             }
116             // TODO finish writing this test
117         }
118     }
119 
120     /**
121      * Print all scheduled events of an OtsSimulatorInterface.
122      * @param simulator the OtsSimulatorInterface
123      */
124     public void printEventList(final OtsSimulatorInterface simulator)
125     {
126         for (SimEventInterface<Duration> se : simulator.getEventList())
127         {
128             System.out.println("se: " + se);
129         }
130     }
131 
132 }