1 package org.opentrafficsim.road.car;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import java.util.LinkedHashSet;
6 import java.util.Map;
7 import java.util.Set;
8
9 import javax.naming.NamingException;
10
11 import org.djunits.unit.DurationUnit;
12 import org.djunits.unit.TimeUnit;
13 import org.djunits.unit.util.UNITS;
14 import org.djunits.value.vdouble.scalar.Acceleration;
15 import org.djunits.value.vdouble.scalar.Direction;
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.djunits.value.vdouble.scalar.Time;
20 import org.djutils.draw.point.Point2d;
21 import org.junit.jupiter.api.Test;
22 import org.opentrafficsim.base.parameters.Parameters;
23 import org.opentrafficsim.core.definitions.DefaultsNl;
24 import org.opentrafficsim.core.dsol.AbstractOtsModel;
25 import org.opentrafficsim.core.dsol.OtsSimulator;
26 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
27 import org.opentrafficsim.core.geometry.OtsGeometryException;
28 import org.opentrafficsim.core.geometry.OtsLine2d;
29 import org.opentrafficsim.core.gtu.GtuException;
30 import org.opentrafficsim.core.gtu.GtuType;
31 import org.opentrafficsim.core.network.NetworkException;
32 import org.opentrafficsim.core.network.Node;
33 import org.opentrafficsim.road.DefaultTestParameters;
34 import org.opentrafficsim.road.definitions.DefaultsRoadNl;
35 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
36 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCfLcTacticalPlanner;
37 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
38 import org.opentrafficsim.road.gtu.lane.tactical.following.GtuFollowingModelOld;
39 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
40 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
41 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
42 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlanner;
43 import org.opentrafficsim.road.network.RoadNetwork;
44 import org.opentrafficsim.road.network.lane.CrossSectionLink;
45 import org.opentrafficsim.road.network.lane.Lane;
46 import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
47 import org.opentrafficsim.road.network.lane.LanePosition;
48 import org.opentrafficsim.road.network.lane.LaneType;
49 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
50
51 import nl.tudelft.simulation.dsol.SimRuntimeException;
52
53
54
55
56
57
58
59
60 public class CarTest implements UNITS
61 {
62
63
64
65
66
67
68
69
70 @SuppressWarnings("static-method")
71 @Test
72 public final void carTest()
73 throws NetworkException, SimRuntimeException, NamingException, GtuException, OtsGeometryException
74 {
75 Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
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 GtuFollowingModelOld gtuFollowingModel =
84 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
85 LaneChangeModel laneChangeModel = new Egoistic();
86 LaneBasedGtu referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, gtuFollowingModel,
87 laneChangeModel, network);
88 assertEquals("12345", referenceCar.getId(), "The car should store it's ID");
89 assertEquals(initialPosition.getSI(), referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(),
90 0.0001, "At t=initialTime the car should be at it's initial position");
91 assertEquals(initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001, "The car should store it's initial speed");
92 assertEquals(0, referenceCar.getAcceleration().getSI(), 0.0001,
93 "The car should have an initial acceleration equal to 0");
94
95
96
97
98 }
99
100
101
102
103
104
105
106 public static OtsSimulatorInterface makeSimulator() throws SimRuntimeException, NamingException
107 {
108 OtsSimulatorInterface simulator = new OtsSimulator("CarTest");
109 Model model = new Model(simulator);
110 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
111 return simulator;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public static LaneBasedGtu makeReferenceCar(final String id, final GtuType gtuType, final Lane lane,
132 final Length initialPosition, final Speed initialSpeed, final GtuFollowingModelOld gtuFollowingModel,
133 final LaneChangeModel laneChangeModel, final RoadNetwork network)
134 throws NamingException, NetworkException, SimRuntimeException, GtuException, OtsGeometryException
135 {
136 Length length = new Length(5.0, METER);
137 Length width = new Length(2.0, METER);
138 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
139 Parameters parameters = DefaultTestParameters.create();
140 LaneBasedGtu gtu = new LaneBasedGtu(id, gtuType, length, width, maxSpeed, length.times(0.5), network);
141 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
142 new LaneBasedCfLcTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
143 gtu.setParameters(parameters);
144 gtu.init(strategicalPlanner, new LanePosition(lane, initialPosition), initialSpeed);
145
146 return gtu;
147 }
148
149
150
151
152
153
154
155
156
157 public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OtsSimulatorInterface simulator)
158 throws NetworkException, OtsGeometryException
159 {
160 Node n1 = new Node(network, "n1", new Point2d(0, 0), Direction.ZERO);
161 Node n2 = new Node(network, "n2", new Point2d(100000.0, 0.0), Direction.ZERO);
162 Point2d[] coordinates = new Point2d[] {new Point2d(0.0, 0.0), new Point2d(100000.0, 0.0)};
163 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, DefaultsNl.ROAD, new OtsLine2d(coordinates),
164 null, LaneKeepingPolicy.KEEPRIGHT);
165 Length latPos = new Length(0.0, METER);
166 Length width = new Length(4.0, METER);
167 return LaneGeometryUtil.createStraightLane(link12, "lane.1", latPos, latPos, width, width, laneType,
168 Map.of(DefaultsNl.VEHICLE, new Speed(100, KM_PER_HOUR)));
169 }
170
171
172 protected static class Model extends AbstractOtsModel
173 {
174
175 private static final long serialVersionUID = 20141027L;
176
177
178
179
180 public Model(final OtsSimulatorInterface simulator)
181 {
182 super(simulator);
183 }
184
185
186 @Override
187 public final void constructModel() throws SimRuntimeException
188 {
189
190 }
191
192
193 @Override
194 public final RoadNetwork getNetwork()
195 {
196 return null;
197 }
198 }
199 }