1 package org.opentrafficsim.road.gtu.lane.tactical.following;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.djunits.unit.TimeUnit;
8 import org.djunits.value.vdouble.scalar.Acceleration;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.Speed;
11 import org.djunits.value.vdouble.scalar.Time;
12 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class SequentialFixedAccelerationModel extends AbstractGTUFollowingModelMobil
27 {
28
29 private final List<FixedAccelerationModel> steps = new ArrayList<FixedAccelerationModel>();
30
31
32 private final OTSDEVSSimulatorInterface simulator;
33
34
35 private final Acceleration maximumSafeDeceleration;
36
37
38
39
40
41
42 public SequentialFixedAccelerationModel(final OTSDEVSSimulatorInterface simulator,
43 final Acceleration maximumSafeDeceleration)
44 {
45 this.simulator = simulator;
46 this.maximumSafeDeceleration = maximumSafeDeceleration;
47 }
48
49
50
51
52
53
54
55 public SequentialFixedAccelerationModel(final OTSDEVSSimulatorInterface simulator,
56 final Acceleration maximumSafeDeceleration, final Set<FixedAccelerationModel> steps)
57 {
58 this(simulator, maximumSafeDeceleration);
59 this.steps.addAll(steps);
60 }
61
62
63
64
65
66
67 public final SequentialFixedAccelerationModel addStep(final FixedAccelerationModel step)
68 {
69 this.steps.add(step);
70 return this;
71 }
72
73
74
75
76
77 public final int size()
78 {
79 return this.steps.size();
80 }
81
82
83
84
85
86
87 public final FixedAccelerationModel get(final int index)
88 {
89 return this.steps.get(index);
90 }
91
92
93
94
95
96
97 public final Time.Abs timeAfterCompletionOfStep(final int index)
98 {
99 Time.Abs sum = new Time.Abs(0, TimeUnit.SI);
100 for (int i = 0; i <= index; i++)
101 {
102 sum = sum.plus(this.steps.get(i).getDuration());
103 }
104 return sum;
105 }
106
107
108 private static final double MAXIMUMTIMEERROR = 0.001;
109
110
111
112
113
114 private FixedAccelerationModel getAccelerationModel()
115 {
116 Time.Abs when = this.simulator.getSimulatorTime().getTime();
117 double remainingTime = when.getSI();
118 for (FixedAccelerationModel step : this.steps)
119 {
120 if (remainingTime < -MAXIMUMTIMEERROR)
121 {
122 throw new Error("FixedSequentialAcceleration does not have a result for " + when);
123 }
124 if (remainingTime < MAXIMUMTIMEERROR)
125 {
126 return step;
127 }
128 remainingTime -= step.getDuration().getSI();
129 }
130 throw new Error("FixedSequentialAcceleration does not have a result for " + when);
131 }
132
133
134 @Override
135 public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
136 final Speed leaderSpeed, final Length.Rel headway, final Speed speedLimit)
137 {
138 return getAccelerationModel().getAcceleration();
139 }
140
141
142 @Override
143 public final Acceleration computeAcceleration(final Speed followerSpeed, final Speed followerMaximumSpeed,
144 final Speed leaderSpeed, final Length.Rel headway, final Speed speedLimit, final Time.Rel stepSize)
145 {
146
147 return getAccelerationModel().getAcceleration();
148 }
149
150
151 @Override
152 public final Acceleration getMaximumSafeDeceleration()
153 {
154 return this.maximumSafeDeceleration;
155 }
156
157
158 @Override
159 public final Time.Rel getStepSize()
160 {
161 return getAccelerationModel().getStepSize();
162 }
163
164
165 @Override
166 public final String getName()
167 {
168 return "FSAM";
169 }
170
171
172 @Override
173 public final String getLongName()
174 {
175 return "Fixed sequential acceleration model";
176 }
177
178 }