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.junit.Test;
16  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
17  import org.opentrafficsim.core.geometry.OTSGeometryException;
18  import org.opentrafficsim.core.geometry.OTSLine3D;
19  import org.opentrafficsim.core.geometry.OTSPoint3D;
20  import org.opentrafficsim.core.gtu.GTUException;
21  import org.opentrafficsim.core.gtu.GTUType;
22  import org.opentrafficsim.core.network.NetworkException;
23  import org.opentrafficsim.road.car.CarTest;
24  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
25  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
26  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.FixedLaneChangeModel;
27  import org.opentrafficsim.road.network.OTSRoadNetwork;
28  import org.opentrafficsim.road.network.factory.LaneFactory;
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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
37   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
38   * <p>
39   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 15, 2016 <br>
40   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
41   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
42   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
43   */
44  public class CurveTest
45  {
46  
47      /**
48       * Let GTUs drive through a curve and check (de-)registration times at each node.
49       * @throws OTSGeometryException on error
50       * @throws NamingException on error
51       * @throws SimRuntimeException on error
52       * @throws NetworkException on error
53       * @throws GTUException on error
54       */
55      @Test
56      public final void curveTest()
57              throws OTSGeometryException, SimRuntimeException, NamingException, NetworkException, GTUException
58      {
59          final int laneCount = 1;
60          OTSSimulatorInterface simulator = CarTest.makeSimulator();
61          OTSRoadNetwork network = new OTSRoadNetwork("curve test network", true, simulator);
62          GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
63          LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
64          Speed speedLimit = new Speed(50, SpeedUnit.KM_PER_HOUR);
65          OTSRoadNode origin = new OTSRoadNode(network, "origin", new OTSPoint3D(10, 10, 0), Direction.ZERO);
66          OTSRoadNode curveStart = new OTSRoadNode(network, "curveStart", new OTSPoint3D(100, 10, 0), Direction.ZERO);
67          OTSRoadNode curveEnd = new OTSRoadNode(network, "curveEnd", new OTSPoint3D(150, 60, 0), 
68                  new Direction(90, DirectionUnit.EAST_DEGREE));
69          OTSRoadNode destination = new OTSRoadNode(network, "destination", new OTSPoint3D(150, 150, 0),
70                  new Direction(90, DirectionUnit.EAST_DEGREE));
71          Lane[] straight1 = LaneFactory.makeMultiLane(network, "straight1", origin, curveStart, null, laneCount, laneType,
72                  speedLimit, simulator);
73          Lane[] straight2 = LaneFactory.makeMultiLane(network, "straight2", curveEnd, destination, null, laneCount, laneType,
74                  speedLimit, simulator);
75          OTSLine3D curveLine = LaneFactory.makeBezier(origin, curveStart, curveEnd, destination);
76          Lane[] curve = LaneFactory.makeMultiLane(network, "bezier", curveStart, curveEnd, curveLine.getPoints(), laneCount,
77                  laneType, speedLimit, simulator);
78          Lane[][] laneSets = new Lane[][] {straight1, curve, straight2};
79          Length initialPosition = new Length(5, LengthUnit.METER);
80          Speed speed = new Speed(10, SpeedUnit.SI);
81          for (int lane = 0; lane < laneCount; lane++)
82          {
83              // System.out.println("Lane is " + lane);
84              double cumulativeLength = 0;
85              for (Lane[] set : laneSets)
86              {
87                  cumulativeLength += set[lane].getLength().si;
88                  double timeAtEnd = simulator.getSimulatorTime().si + (cumulativeLength - initialPosition.si) / speed.si;
89                  System.out.println("lane " + set[lane] + " length is " + set[lane].getLength()
90                          + " time for reference to get to end " + timeAtEnd);
91              }
92              LaneBasedIndividualGTU car = CarTest.makeReferenceCar("car", gtuType, straight1[lane], initialPosition, speed,
93                      simulator,
94                      new FixedAccelerationModel(new Acceleration(0, AccelerationUnit.SI), new Duration(25, DurationUnit.SI)),
95                      new FixedLaneChangeModel(null), (OTSRoadNetwork) 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 }