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