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