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.Network;
30  import org.opentrafficsim.core.network.NetworkException;
31  import org.opentrafficsim.core.network.OTSNetwork;
32  import org.opentrafficsim.core.network.OTSNode;
33  import org.opentrafficsim.road.car.CarTest;
34  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
35  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
36  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.FixedLaneChangeModel;
37  import org.opentrafficsim.road.network.factory.LaneFactory;
38  
39  /**
40   * Verify that GTUs register and unregister at the correct times and locations when following a curve.
41   * <p>
42   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
43   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
44   * <p>
45   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 15, 2016 <br>
46   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
47   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
48   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
49   */
50  public class CurveTest
51  {
52  
53      /**
54       * Let GTUs drive through a curve and check (de-)registration times at each node.
55       * @throws OTSGeometryException
56       * @throws NamingException
57       * @throws SimRuntimeException
58       * @throws NetworkException
59       * @throws GTUException
60       */
61      @Test
62      public void curveTest() throws OTSGeometryException, SimRuntimeException, NamingException, NetworkException, 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          Network network = new OTSNetwork("curve test network");
72          OTSNode origin = new OTSNode(network, "origin", new OTSPoint3D(10, 10, 0));
73          OTSNode curveStart = new OTSNode(network, "curveStart", new OTSPoint3D(100, 10, 0));
74          OTSNode curveEnd = new OTSNode(network, "curveEnd", new OTSPoint3D(150, 60, 0));
75          OTSNode destination = new OTSNode(network, "destination", new OTSPoint3D(150, 150, 0));
76          Lane[] straight1 =
77                  LaneFactory.makeMultiLane(network, "straight1", origin, curveStart, null, laneCount, laneType, speedLimit,
78                          simulator, LongitudinalDirectionality.DIR_PLUS);
79          Lane[] straight2 =
80                  LaneFactory.makeMultiLane(network, "straight2", curveEnd, destination, null, laneCount, laneType, speedLimit,
81                          simulator, LongitudinalDirectionality.DIR_PLUS);
82          OTSLine3D curveLine = LaneFactory.makeBezier(origin, curveStart, curveEnd, destination);
83          Lane[] curve =
84                  LaneFactory.makeMultiLane(network, "bezier", curveStart, curveEnd, curveLine.getPoints(), laneCount,
85                          laneType, speedLimit, simulator, LongitudinalDirectionality.DIR_PLUS);
86          Lane[][] laneSets = new Lane[][] { straight1, curve, straight2 };
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 = simulator.getSimulatorTime().get().si + (cumulativeLength - initialPosition.si) / speed.si;
97                  System.out.println("lane " + set[lane] + " length is " + set[lane].getLength()
98                          + " time for reference to get to end " + timeAtEnd);
99              }
100             LaneBasedIndividualGTU car =
101                     CarTest.makeReferenceCar("car", gtuType, straight1[lane], initialPosition, speed,
102                             (OTSDEVSSimulator) simulator, new FixedAccelerationModel(new Acceleration(0, AccelerationUnit.SI),
103                                     new Duration(25, TimeUnit.SI)), new FixedLaneChangeModel(null), (OTSNetwork) network);
104             printEventList(simulator);
105             System.out.println("STEP");
106             simulator.step();
107             printEventList(simulator);
108             System.out.println("STEP");
109             simulator.step();
110             printEventList(simulator);
111             // TODO finish writing this test
112         }
113     }
114 
115     /**
116      * Print all scheduled events of an OTSDEVSSimulatorInterface.
117      * @param simulator OTSDEVSSimulatorInterface; the OTSDEVSSimulatorInterface
118      */
119     public void printEventList(final OTSDEVSSimulatorInterface simulator)
120     {
121         for (SimEventInterface<OTSSimTimeDouble> se : simulator.getEventList())
122         {
123             System.out.println("se: " + se);
124         }
125 
126     }
127 
128 }