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