1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.opentrafficsim.core.gtu.GTUType.CAR;
5   
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.UNITS;
14  import org.djunits.value.vdouble.scalar.Acceleration;
15  import org.djunits.value.vdouble.scalar.Duration;
16  import org.djunits.value.vdouble.scalar.Length;
17  import org.djunits.value.vdouble.scalar.Speed;
18  import org.djunits.value.vdouble.scalar.Time;
19  import org.junit.Test;
20  import org.opentrafficsim.base.parameters.Parameters;
21  import org.opentrafficsim.core.dsol.AbstractOTSModel;
22  import org.opentrafficsim.core.dsol.OTSSimulator;
23  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
24  import org.opentrafficsim.core.geometry.OTSGeometryException;
25  import org.opentrafficsim.core.geometry.OTSLine3D;
26  import org.opentrafficsim.core.geometry.OTSPoint3D;
27  import org.opentrafficsim.core.gtu.GTUDirectionality;
28  import org.opentrafficsim.core.gtu.GTUException;
29  import org.opentrafficsim.core.gtu.GTUType;
30  import org.opentrafficsim.core.network.LinkType;
31  import org.opentrafficsim.core.network.Network;
32  import org.opentrafficsim.core.network.NetworkException;
33  import org.opentrafficsim.core.network.OTSNetwork;
34  import org.opentrafficsim.core.network.OTSNode;
35  import org.opentrafficsim.road.DefaultTestParameters;
36  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
37  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
38  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
39  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
40  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
41  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
42  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
43  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
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.changing.LaneKeepingPolicy;
49  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
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          GTUType gtuType = CAR;
79          LaneType laneType = LaneType.TWO_WAY_LANE;
80          OTSNetwork network = new OTSNetwork("network");
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();
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 OTSNetwork 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.multiplyBy(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 Network network, final LaneType laneType, final OTSSimulatorInterface simulator)
165             throws NetworkException, OTSGeometryException
166     {
167         OTSNode n1 = new OTSNode(network, "n1", new OTSPoint3D(0, 0));
168         OTSNode n2 = new OTSNode(network, "n2", new OTSPoint3D(100000.0, 0.0));
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, LinkType.ROAD, new OTSLine3D(coordinates),
171                 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                 new OvertakingConditions.LeftAndRight());
176     }
177 
178     
179     protected static class Model extends AbstractOTSModel
180     {
181         
182         private static final long serialVersionUID = 20141027L;
183 
184         
185 
186 
187         public Model(final OTSSimulatorInterface simulator)
188         {
189             super(simulator);
190         }
191 
192         
193         @Override
194         public final void constructModel() throws SimRuntimeException
195         {
196             
197         }
198 
199         
200         @Override
201         public final OTSNetwork getNetwork()
202         {
203             return null;
204         }
205     }
206 }