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