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