1 package org.opentrafficsim.road.car;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.util.LinkedHashSet;
6 import java.util.Set;
7
8 import javax.naming.NamingException;
9
10 import org.djunits.unit.DurationUnit;
11 import org.djunits.unit.TimeUnit;
12 import org.djunits.unit.UNITS;
13 import org.djunits.value.vdouble.scalar.Acceleration;
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Speed;
17 import org.djunits.value.vdouble.scalar.Time;
18 import org.junit.Test;
19 import org.opentrafficsim.base.parameters.Parameters;
20 import org.opentrafficsim.core.dsol.AbstractOTSModel;
21 import org.opentrafficsim.core.dsol.OTSSimulator;
22 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
23 import org.opentrafficsim.core.geometry.OTSGeometryException;
24 import org.opentrafficsim.core.geometry.OTSLine3D;
25 import org.opentrafficsim.core.geometry.OTSPoint3D;
26 import org.opentrafficsim.core.gtu.GTUDirectionality;
27 import org.opentrafficsim.core.gtu.GTUException;
28 import org.opentrafficsim.core.gtu.GTUType;
29 import org.opentrafficsim.core.network.LinkType;
30 import org.opentrafficsim.core.network.NetworkException;
31 import org.opentrafficsim.core.network.OTSNode;
32 import org.opentrafficsim.road.DefaultTestParameters;
33 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
34 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
35 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
36 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
37 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
38 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
39 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
40 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
41 import org.opentrafficsim.road.network.OTSRoadNetwork;
42 import org.opentrafficsim.road.network.RoadNetwork;
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.changing.LaneKeepingPolicy;
48
49 import nl.tudelft.simulation.dsol.SimRuntimeException;
50
51
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 OTSRoadNetwork network = new OTSRoadNetwork("network", true);
77 GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
78 LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
79 OTSSimulatorInterface simulator = makeSimulator();
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 LaneBasedIndividualGTU referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator,
87 gtuFollowingModel, laneChangeModel, network);
88 assertEquals("The car should store it's ID", "12345", referenceCar.getId());
89 assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(),
90 referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
91 assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
92 assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
93 0.0001);
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();
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
132
133 public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
134 final Length initialPosition, final Speed initialSpeed, final OTSSimulatorInterface simulator,
135 final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSRoadNetwork network)
136 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
137 {
138 Length length = new Length(5.0, METER);
139 Length width = new Length(2.0, METER);
140 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
141 initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
142 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
143 Parameters parameters = DefaultTestParameters.create();
144 LaneBasedIndividualGTU gtu =
145 new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, length.multiplyBy(0.5), simulator, network);
146 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
147 new LaneBasedCFLCTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
148 gtu.setParameters(parameters);
149 gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
150
151 return gtu;
152 }
153
154
155
156
157
158
159
160
161
162 public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OTSSimulatorInterface simulator)
163 throws NetworkException, OTSGeometryException
164 {
165 OTSNode n1 = new OTSNode(network, "n1", new OTSPoint3D(0, 0));
166 OTSNode n2 = new OTSNode(network, "n2", new OTSPoint3D(100000.0, 0.0));
167 OTSPoint3D[] coordinates = new OTSPoint3D[] {new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0)};
168 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, network.getLinkType(LinkType.DEFAULTS.ROAD),
169 new OTSLine3D(coordinates), simulator, LaneKeepingPolicy.KEEPRIGHT);
170 Length latPos = new Length(0.0, METER);
171 Length width = new Length(4.0, METER);
172 return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, new Speed(100, KM_PER_HOUR));
173 }
174
175
176 protected static class Model extends AbstractOTSModel
177 {
178
179 private static final long serialVersionUID = 20141027L;
180
181
182
183
184 public Model(final OTSSimulatorInterface simulator)
185 {
186 super(simulator);
187 }
188
189
190 @Override
191 public final void constructModel() throws SimRuntimeException
192 {
193
194 }
195
196
197 @Override
198 public final OTSRoadNetwork getNetwork()
199 {
200 return null;
201 }
202 }
203 }