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