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  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
33  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
34  
35  /**
36   * Verify that GTUs register and unregister at the correct times and locations when following a curve.
37   * <p>
38   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
39   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
40   * <p>
41   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 15, 2016 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
44   * @author <a href="http://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          OTSRoadNetwork network = new OTSRoadNetwork("curve test network", true, simulator);
64          GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
65          LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
66          Speed speedLimit = new Speed(50, SpeedUnit.KM_PER_HOUR);
67          OTSRoadNode origin = new OTSRoadNode(network, "origin", new OTSPoint3D(10, 10, 0), Direction.ZERO);
68          OTSRoadNode curveStart = new OTSRoadNode(network, "curveStart", new OTSPoint3D(100, 10, 0), Direction.ZERO);
69          OTSRoadNode curveEnd = new OTSRoadNode(network, "curveEnd", new OTSPoint3D(150, 60, 0), 
70                  new Direction(90, DirectionUnit.EAST_DEGREE));
71          OTSRoadNode destination = new OTSRoadNode(network, "destination", new OTSPoint3D(150, 150, 0),
72                  new Direction(90, DirectionUnit.EAST_DEGREE));
73          Lane[] straight1 = LaneFactory.makeMultiLane(network, "straight1", origin, curveStart, null, laneCount, laneType,
74                  speedLimit, simulator);
75          Lane[] straight2 = LaneFactory.makeMultiLane(network, "straight2", curveEnd, destination, null, laneCount, laneType,
76                  speedLimit, simulator);
77          OTSLine3D curveLine = LaneFactory.makeBezier(origin, curveStart, curveEnd, destination);
78          Lane[] curve = LaneFactory.makeMultiLane(network, "bezier", curveStart, curveEnd, curveLine.getPoints(), laneCount,
79                  laneType, speedLimit, simulator);
80          Lane[][] laneSets = new Lane[][] {straight1, curve, straight2};
81          Length initialPosition = new Length(5, LengthUnit.METER);
82          Speed speed = new Speed(10, SpeedUnit.SI);
83          for (int lane = 0; lane < laneCount; lane++)
84          {
85              // System.out.println("Lane is " + lane);
86              double cumulativeLength = 0;
87              for (Lane[] set : laneSets)
88              {
89                  cumulativeLength += set[lane].getLength().si;
90                  double timeAtEnd = simulator.getSimulatorTime().si + (cumulativeLength - initialPosition.si) / speed.si;
91                  System.out.println("lane " + set[lane] + " length is " + set[lane].getLength()
92                          + " time for reference to get to end " + timeAtEnd);
93              }
94              LaneBasedIndividualGTU car = CarTest.makeReferenceCar("car", gtuType, straight1[lane], initialPosition, speed,
95                      simulator,
96                      new FixedAccelerationModel(new Acceleration(0, AccelerationUnit.SI), new Duration(25, DurationUnit.SI)),
97                      new FixedLaneChangeModel(null), (OTSRoadNetwork) network);
98              printEventList(simulator);
99              System.out.println("STEP");
100             simulator.step();
101             printEventList(simulator);
102             System.out.println("STEP");
103             simulator.step();
104             printEventList(simulator);
105             // TODO finish writing this test
106         }
107     }
108 
109     /**
110      * Print all scheduled events of an DEVSSimulatorInterface.TimeDoubleUnit.
111      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the DEVSSimulatorInterface.TimeDoubleUnit
112      */
113     public final void printEventList(final DEVSSimulatorInterface.TimeDoubleUnit simulator)
114     {
115         for (SimEventInterface<SimTimeDoubleUnit> se : simulator.getEventList())
116         {
117             System.out.println("se: " + se);
118         }
119 
120     }
121 
122 }