1 package org.opentrafficsim.draw.egtf;
2
3 import static org.junit.jupiter.api.Assertions.assertFalse;
4
5 import java.util.Locale;
6
7 import org.djutils.test.UnitTest;
8 import org.junit.jupiter.api.Test;
9 import org.opentrafficsim.animation.egtf.DataSource;
10 import org.opentrafficsim.animation.egtf.DataStream;
11 import org.opentrafficsim.animation.egtf.Egtf;
12 import org.opentrafficsim.animation.egtf.Filter;
13 import org.opentrafficsim.animation.egtf.typed.TypedQuantity;
14
15
16
17
18
19
20
21
22
23
24
25 public final class EgtfTest
26 {
27
28
29 private static final double MARGIN = 1e-6;
30
31
32 private EgtfTest()
33 {
34
35 }
36
37
38 @Test
39 public void combineSourcesTest()
40 {
41 Egtf egtf = new Egtf();
42 DataSource det = egtf.getDataSource("detectors");
43 DataSource fcd = egtf.getDataSource("fcd");
44 DataStream<?> detSpeed = det.addStreamSI(TypedQuantity.SPEED, 10.0, 5.0);
45 DataStream<?> detFlow = det.addStreamSI(TypedQuantity.FLOW, 1.0, 1.0);
46 DataStream<?> fcdSpeed = fcd.addStreamSI(TypedQuantity.SPEED, 2.0, 1.0);
47
48 Egtf egtf2 = new Egtf();
49 DataSource det2 = egtf2.getDataSource("detectors");
50 DataSource fcd2 = egtf2.getDataSource("fcd");
51 DataStream<?> detSpeed2 = det2.addStreamSI(TypedQuantity.SPEED, 10.0, 5.0);
52 DataStream<?> detFlow2 = det2.addStreamSI(TypedQuantity.FLOW, 1.0, 1.0);
53 DataStream<?> fcdSpeed2 = fcd2.addStreamSI(TypedQuantity.SPEED, 5.0, 2.5);
54
55 double dx = 500;
56 double dt = 60;
57 double xmax = 2000;
58 double tmax = 300;
59 double[] x = new double[1 + (int) (xmax / dx)];
60 double[] t = new double[1 + (int) (tmax / dt)];
61 for (int i = 0; i < x.length; i++)
62 {
63 x[i] = i * dx;
64 }
65 for (int j = 0; j < t.length; j++)
66 {
67 t[j] = j * dt;
68 }
69 double vFree = 33.0;
70 double qFree = 0.5;
71 double[][] vFreeGrid = new double[x.length][t.length];
72 for (int i = 0; i < x.length; i++)
73 {
74 double location = x[i];
75 double[] qFreeVector = new double[t.length];
76 double[] qFreeTime = new double[t.length];
77 double[] qFreeSpace = new double[t.length];
78 for (int j = 0; j < t.length; j++)
79 {
80 double time = t[j];
81 vFreeGrid[i][j] = vFree;
82 qFreeVector[j] = qFree;
83 qFreeTime[j] = time;
84 qFreeSpace[j] = location;
85 egtf2.addPointDataSI(detSpeed2, location, time, vFree);
86 egtf2.addPointDataSI(detFlow2, location, time, qFree);
87 }
88 egtf.addVectorDataSI(detFlow, qFreeSpace, qFreeTime, qFreeVector);
89 }
90 egtf.addGridDataSI(detSpeed, x, t, vFreeGrid);
91
92 Filter filterDet = egtf.filterFastSI(0.0, dx, xmax, 0.0, dt, tmax, TypedQuantity.SPEED, TypedQuantity.FLOW).get();
93
94 double location = 1200;
95 double time = 130;
96 double v = 10;
97 for (int i = 0; i < 20; i++)
98 {
99 egtf.addPointDataSI(fcdSpeed, location, time, v);
100 egtf2.addPointDataSI(fcdSpeed2, location, time, v);
101 location += v;
102 time += 1.0;
103 }
104 Filter filterCom1 = egtf.filterSI(x, t, TypedQuantity.SPEED, TypedQuantity.FLOW).get();
105 Filter filterCom12 = egtf2.filterSI(x, t, TypedQuantity.SPEED, TypedQuantity.FLOW).get();
106
107 location = 1100;
108 time = 140;
109 for (int i = 0; i < 20; i++)
110 {
111 egtf.addPointDataSI(fcdSpeed, location, time, v);
112 location += v;
113 time += 1.0;
114 }
115 Filter filterCom2 = egtf.filterSI(x, t, TypedQuantity.SPEED, TypedQuantity.FLOW).get();
116
117 double[][] vDet = filterDet.getSI(TypedQuantity.SPEED);
118 double[][] qDet = filterDet.getSI(TypedQuantity.FLOW);
119 double[][] vCom1 = filterCom1.getSI(TypedQuantity.SPEED);
120 double[][] qCom1 = filterCom1.getSI(TypedQuantity.FLOW);
121 double[][] vCom12 = filterCom12.getSI(TypedQuantity.SPEED);
122 double[][] qCom12 = filterCom12.getSI(TypedQuantity.FLOW);
123 double[][] vCom2 = filterCom2.getSI(TypedQuantity.SPEED);
124 double[][] qCom2 = filterCom2.getSI(TypedQuantity.FLOW);
125 for (int i = 0; i < x.length - 1; i++)
126 {
127 for (int j = 0; j < t.length - 1; j++)
128 {
129 assertFalse(Math.abs(vDet[i][j] - vFree) > MARGIN,
130 "Speed filtered with detector data is not equal to uniform input speed");
131 assertFalse(vCom1[i][j] > vDet[i][j],
132 "Speed filtered with FCD data is above speed filtered with detector data");
133 assertFalse(vCom1[i][j] < v, "Speed filtered with FCD data is below minimum speed data");
134 assertFalse(vCom12[i][j] < vCom1[i][j],
135 "Speed filtered with unreliable FCD data is below speed filtered FCD data");
136 assertFalse(vCom2[i][j] > vCom1[i][j],
137 "Speed filtered with additional FCD data is above speed filtered FCD data");
138 assertFalse(Math.abs(qDet[i][j] - 0.5) > MARGIN,
139 "Flow filtered with detector data is not equal to uniform input flow");
140 assertFalse(Math.abs(qCom1[i][j] - 0.5) > MARGIN,
141 "Flow filtered with FCD data is not equal to uniform input flow");
142 assertFalse(Math.abs(qCom12[i][j] - 0.5) > MARGIN,
143 "Flow filtered with unreliable FCD data is not equal to uniform input flow");
144 assertFalse(Math.abs(qCom2[i][j] - 0.5) > MARGIN,
145 "Flow filtered with additional FCD data is not equal to uniform input flow");
146 }
147 }
148
149
150
151
152
153
154
155
156 }
157
158
159
160
161
162
163 @SuppressWarnings("unused")
164 private void printMatrix(final double[][] matrix, final String name)
165 {
166 System.out.println(name + " = [");
167 for (double[] arr : matrix)
168 {
169 StringBuilder str = new StringBuilder();
170 String sep = "";
171 for (double d : arr)
172 {
173 str.append(sep).append(String.format(Locale.ROOT, "%.2f", d));
174 sep = ", ";
175 }
176 System.out.println(" " + str.append(";"));
177 }
178 System.out.println("];");
179 }
180
181
182 @Test
183 public void exceptionsTest()
184 {
185 Egtf egtf = new Egtf();
186 DataSource det = egtf.getDataSource("detectors");
187 DataStream<?> detSpeed = det.addStreamSI(TypedQuantity.SPEED, 10.0, 5.0);
188 UnitTest.testFail(() -> det.addStreamSI(TypedQuantity.SPEED, 5.0, 2.5),
189 "Double speed quantity for detectors should fail.");
190 UnitTest.testFail(() -> egtf.addVectorDataSI(detSpeed, new double[2], new double[3], new double[3]),
191 "Unequal lengths should result in a fail.", IllegalArgumentException.class);
192 UnitTest.testFail(() -> egtf.addVectorDataSI(detSpeed, new double[3], new double[2], new double[3]),
193 "Unequal lengths should result in a fail.", IllegalArgumentException.class);
194 UnitTest.testFail(() -> egtf.addVectorDataSI(detSpeed, new double[3], new double[3], new double[2]),
195 "Unequal lengths should result in a fail.", IllegalArgumentException.class);
196 }
197
198 }