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