1 package org.opentrafficsim.road.car;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.Serializable;
6 import java.util.LinkedHashSet;
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.dsol.AbstractOTSModel;
23 import org.opentrafficsim.core.dsol.OTSSimulator;
24 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25 import org.opentrafficsim.core.geometry.OTSGeometryException;
26 import org.opentrafficsim.core.geometry.OTSLine3D;
27 import org.opentrafficsim.core.geometry.OTSPoint3D;
28 import org.opentrafficsim.core.gtu.GTUDirectionality;
29 import org.opentrafficsim.core.gtu.GTUException;
30 import org.opentrafficsim.core.gtu.GTUType;
31 import org.opentrafficsim.core.network.LinkType;
32 import org.opentrafficsim.core.network.NetworkException;
33 import org.opentrafficsim.road.DefaultTestParameters;
34 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
35 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
36 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
37 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
38 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
39 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
40 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
41 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
42 import org.opentrafficsim.road.network.OTSRoadNetwork;
43 import org.opentrafficsim.road.network.RoadNetwork;
44 import org.opentrafficsim.road.network.lane.CrossSectionLink;
45 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
46 import org.opentrafficsim.road.network.lane.Lane;
47 import org.opentrafficsim.road.network.lane.LaneType;
48 import org.opentrafficsim.road.network.lane.OTSRoadNode;
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
61
62 public class CarTest implements UNITS
63 {
64
65
66
67
68
69
70
71
72 @SuppressWarnings("static-method")
73 @Test
74 public final void carTest()
75 throws NetworkException, SimRuntimeException, NamingException, GTUException, OTSGeometryException
76 {
77 Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
78 OTSRoadNetwork network = new OTSRoadNetwork("network", true);
79 GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
80 LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
81 OTSSimulatorInterface simulator = makeSimulator();
82 Lane lane = makeLane(network, laneType, simulator);
83 Length initialPosition = new Length(12, METER);
84 Speed initialSpeed = new Speed(34, KM_PER_HOUR);
85 GTUFollowingModelOld gtuFollowingModel =
86 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
87 LaneChangeModel laneChangeModel = new Egoistic();
88 LaneBasedIndividualGTU referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator,
89 gtuFollowingModel, laneChangeModel, network);
90 assertEquals("The car should store it's ID", "12345", referenceCar.getId());
91 assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(),
92 referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
93 assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
94 assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
95 0.0001);
96
97
98
99
100 }
101
102
103
104
105
106
107
108 public static OTSSimulatorInterface makeSimulator() throws SimRuntimeException, NamingException
109 {
110 OTSSimulatorInterface simulator = new OTSSimulator("CarTest");
111 Model model = new Model(simulator);
112 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
113 return simulator;
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
136 final Length initialPosition, final Speed initialSpeed, final OTSSimulatorInterface simulator,
137 final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSRoadNetwork network)
138 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
139 {
140 Length length = new Length(5.0, METER);
141 Length width = new Length(2.0, METER);
142 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
143 initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
144 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
145 Parameters parameters = DefaultTestParameters.create();
146 LaneBasedIndividualGTU gtu =
147 new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, length.times(0.5), simulator, network);
148 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
149 new LaneBasedCFLCTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
150 gtu.setParameters(parameters);
151 gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
152
153 return gtu;
154 }
155
156
157
158
159
160
161
162
163
164 public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OTSSimulatorInterface simulator)
165 throws NetworkException, OTSGeometryException
166 {
167 OTSRoadNode n1 = new OTSRoadNode(network, "n1", new OTSPoint3D(0, 0), Direction.ZERO);
168 OTSRoadNode n2 = new OTSRoadNode(network, "n2", new OTSPoint3D(100000.0, 0.0), Direction.ZERO);
169 OTSPoint3D[] coordinates = new OTSPoint3D[] {new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0)};
170 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, network.getLinkType(LinkType.DEFAULTS.ROAD),
171 new OTSLine3D(coordinates), simulator, LaneKeepingPolicy.KEEPRIGHT);
172 Length latPos = new Length(0.0, METER);
173 Length width = new Length(4.0, METER);
174 return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, new Speed(100, KM_PER_HOUR));
175 }
176
177
178 protected static class Model extends AbstractOTSModel
179 {
180
181 private static final long serialVersionUID = 20141027L;
182
183
184
185
186 public Model(final OTSSimulatorInterface simulator)
187 {
188 super(simulator);
189 }
190
191
192 @Override
193 public final void constructModel() throws SimRuntimeException
194 {
195
196 }
197
198
199 @Override
200 public final OTSRoadNetwork getNetwork()
201 {
202 return null;
203 }
204
205
206 @Override
207 public Serializable getSourceId()
208 {
209 return "CarTestModel";
210 }
211 }
212 }