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