View Javadoc
1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import java.util.Map;
6   
7   import javax.naming.NamingException;
8   
9   import org.djunits.unit.DurationUnit;
10  import org.djunits.unit.util.UNITS;
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.djunits.value.vdouble.scalar.Time;
16  import org.djutils.draw.point.Point2d;
17  import org.junit.jupiter.api.Test;
18  import org.opentrafficsim.base.geometry.OtsLine2d;
19  import org.opentrafficsim.base.parameters.Parameters;
20  import org.opentrafficsim.core.definitions.DefaultsNl;
21  import org.opentrafficsim.core.dsol.AbstractOtsModel;
22  import org.opentrafficsim.core.dsol.OtsSimulator;
23  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
24  import org.opentrafficsim.core.gtu.GtuException;
25  import org.opentrafficsim.core.gtu.GtuType;
26  import org.opentrafficsim.core.network.NetworkException;
27  import org.opentrafficsim.core.network.Node;
28  import org.opentrafficsim.core.perception.HistoryManagerDevs;
29  import org.opentrafficsim.road.DefaultTestParameters;
30  import org.opentrafficsim.road.FixedCarFollowing;
31  import org.opentrafficsim.road.definitions.DefaultsRoadNl;
32  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
33  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.Lmrs;
34  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
35  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory.Setting;
36  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
37  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlanner;
38  import org.opentrafficsim.road.network.LaneKeepingPolicy;
39  import org.opentrafficsim.road.network.RoadNetwork;
40  import org.opentrafficsim.road.network.lane.CrossSectionLink;
41  import org.opentrafficsim.road.network.lane.Lane;
42  import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
43  import org.opentrafficsim.road.network.lane.LanePosition;
44  import org.opentrafficsim.road.network.lane.LaneType;
45  
46  import nl.tudelft.simulation.dsol.SimRuntimeException;
47  
48  /**
49   * <p>
50   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
51   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
52   * </p>
53   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
54   */
55  public final class CarTest implements UNITS
56  {
57  
58      /** */
59      private CarTest()
60      {
61          // do not instantiate test class
62      }
63  
64      /**
65       * Test some basics of the Car class.
66       * @throws NetworkException on ???
67       * @throws SimRuntimeException on ???
68       * @throws NamingException on ???
69       * @throws GtuException on ???
70       */
71      @SuppressWarnings("static-method")
72      @Test
73      public void carTest() throws NetworkException, SimRuntimeException, NamingException, GtuException
74      {
75          Duration initialTime = new Duration(0, DurationUnit.SI);
76          OtsSimulatorInterface simulator = makeSimulator();
77          RoadNetwork network = new RoadNetwork("network", simulator);
78          GtuType gtuType = DefaultsNl.CAR;
79          LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
80          Lane lane = makeLane(network, laneType, simulator);
81          Length initialPosition = new Length(12, METER);
82          Speed initialSpeed = new Speed(34, KM_PER_HOUR);
83          LaneBasedGtu referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, network);
84          assertEquals("12345", referenceCar.getId(), "The car should store it's ID");
85          assertEquals(initialPosition.getSI(), referenceCar.getPosition(lane, referenceCar.getReference(), initialTime).getSI(),
86                  0.0001, "At t=initialTime the car should be at it's initial position");
87          assertEquals(initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001, "The car should store it's initial speed");
88          assertEquals(0, referenceCar.getAcceleration().getSI(), 0.0001,
89                  "The car should have an initial acceleration equal to 0");
90          // TODO check with following model as part of tactical planner
91          // assertEquals("The gtu following model should be " + gtuFollowingModel, gtuFollowingModel, referenceCar
92          // .getBehavioralCharacteristics().getGtuFollowingModel());
93          // There is (currently) no way to retrieve the lane change model of a GTU.
94      }
95  
96      /**
97       * Create the simplest possible simulator.
98       * @return DevsSimulator.TimeDoubleUnit
99       * @throws SimRuntimeException on ???
100      * @throws NamingException on ???
101      */
102     public static OtsSimulatorInterface makeSimulator() throws SimRuntimeException, NamingException
103     {
104         OtsSimulatorInterface simulator = new OtsSimulator("CarTest");
105         Model model = new Model(simulator);
106         simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model,
107                 HistoryManagerDevs.noHistory(simulator));
108         return simulator;
109     }
110 
111     /**
112      * Create a new Car.
113      * @param id the name (number) of the Car
114      * @param gtuType the type of the new car
115      * @param lane the lane on which the new Car is positioned
116      * @param initialPosition the initial longitudinal position of the new Car
117      * @param initialSpeed the initial speed
118      * @param network the network
119      * @return the new Car
120      * @throws NamingException on network error when making the animation
121      * @throws NetworkException when the GTU cannot be placed on the given lane.
122      * @throws SimRuntimeException when the move method cannot be scheduled.
123      * @throws GtuException when construction of the GTU fails (probably due to an invalid parameter)
124      */
125     public static LaneBasedGtu makeReferenceCar(final String id, final GtuType gtuType, final Lane lane,
126             final Length initialPosition, final Speed initialSpeed, final RoadNetwork network)
127             throws NamingException, NetworkException, SimRuntimeException, GtuException
128     {
129         Length length = new Length(5.0, METER);
130         Length width = new Length(2.0, METER);
131         Speed maxSpeed = new Speed(120, KM_PER_HOUR);
132         Parameters parameters = DefaultTestParameters.create();
133         LaneBasedGtu gtu = new LaneBasedGtu(id, gtuType, length, width, maxSpeed, length.times(0.5), network);
134         gtu.setParameters(parameters);
135         LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(new LmrsFactory<>(Lmrs::new)
136                 .set(Setting.CAR_FOLLOWING_MODEL, (h, v) -> new FixedCarFollowing().get()).create(gtu), gtu);
137         gtu.init(strategicalPlanner, new LanePosition(lane, initialPosition).getLocation(), initialSpeed);
138 
139         return gtu;
140     }
141 
142     /**
143      * Makes lane.
144      * @param network the network
145      * @param laneType the type of the lane
146      * @param simulator simulator
147      * @return a lane of 1000 m long.
148      * @throws NetworkException on network error
149      */
150     public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OtsSimulatorInterface simulator)
151             throws NetworkException
152     {
153         Node n1 = new Node(network, "n1", new Point2d(0, 0), Direction.ZERO);
154         Node n2 = new Node(network, "n2", new Point2d(100000.0, 0.0), Direction.ZERO);
155         Point2d[] coordinates = new Point2d[] {new Point2d(0.0, 0.0), new Point2d(100000.0, 0.0)};
156         CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, DefaultsNl.ROAD, new OtsLine2d(coordinates),
157                 null, LaneKeepingPolicy.KEEPRIGHT);
158         Length latPos = new Length(0.0, METER);
159         Length width = new Length(4.0, METER);
160         return LaneGeometryUtil.createStraightLane(link12, "lane.1", latPos, latPos, width, width, laneType,
161                 Map.of(DefaultsNl.VEHICLE, new Speed(100, KM_PER_HOUR)));
162     }
163 
164     /** The helper model. */
165     protected static class Model extends AbstractOtsModel
166     {
167         /**
168          * Constructor.
169          * @param simulator the simulator to use
170          */
171         public Model(final OtsSimulatorInterface simulator)
172         {
173             super(simulator);
174         }
175 
176         @Override
177         public final void constructModel() throws SimRuntimeException
178         {
179             //
180         }
181 
182         @Override
183         public final RoadNetwork getNetwork()
184         {
185             return null;
186         }
187     }
188 }