1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.ode.events;
24
25 import java.util.Arrays;
26
27 import org.hipparchus.CalculusFieldElement;
28 import org.hipparchus.Field;
29 import org.hipparchus.analysis.solvers.BracketedRealFieldUnivariateSolver;
30 import org.hipparchus.ode.FieldODEState;
31 import org.hipparchus.ode.FieldODEStateAndDerivative;
32 import org.hipparchus.util.MathArrays;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class FieldEventSlopeFilter<T extends FieldODEEventDetector<E>, E extends CalculusFieldElement<E>>
70 extends AbstractFieldODEDetector<FieldEventSlopeFilter<T, E>, E> {
71
72
73 private static final int HISTORY_SIZE = 100;
74
75
76
77
78 private final T rawDetector;
79
80
81 private final FilterType filter;
82
83
84 private final Transformer[] transformers;
85
86
87 private final E[] updates;
88
89
90 private boolean forward;
91
92
93 private E extremeT;
94
95
96
97
98
99
100 public FieldEventSlopeFilter(final Field<E> field, final T rawDetector, final FilterType filter) {
101 this(field,
102 rawDetector.getMaxCheckInterval(), rawDetector.getMaxIterationCount(),
103 rawDetector.getSolver(), new LocalHandler<>(rawDetector.getHandler()),
104 rawDetector, filter);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 private FieldEventSlopeFilter(final Field<E> field,
122 final FieldAdaptableInterval<E> maxCheck, final int maxIter,
123 final BracketedRealFieldUnivariateSolver<E> solver,
124 final FieldODEEventHandler<E> handler,
125 final T rawDetector, final FilterType filter) {
126 super(maxCheck, maxIter, solver, handler);
127 this.rawDetector = rawDetector;
128 this.filter = filter;
129 this.transformers = new Transformer[HISTORY_SIZE];
130 this.updates = MathArrays.buildArray(field, HISTORY_SIZE);
131 }
132
133
134 @Override
135 protected FieldEventSlopeFilter<T, E> create(final FieldAdaptableInterval<E> newMaxCheck, final int newMaxIter,
136 final BracketedRealFieldUnivariateSolver<E> newSolver,
137 final FieldODEEventHandler<E> newHandler) {
138 return new FieldEventSlopeFilter<T, E>(newSolver.getAbsoluteAccuracy().getField(), newMaxCheck, newMaxIter,
139 newSolver, newHandler, rawDetector, filter);
140 }
141
142
143
144
145
146 public T getDetector() {
147 return rawDetector;
148 }
149
150
151 @Override
152 public void init(final FieldODEStateAndDerivative<E> initialState, E finalTime) {
153
154
155 rawDetector.init(initialState, finalTime);
156
157
158 forward = finalTime.subtract(initialState.getTime()).getReal() >= 0;
159 extremeT = finalTime.getField().getZero().newInstance(forward ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY);
160 Arrays.fill(transformers, Transformer.UNINITIALIZED);
161 Arrays.fill(updates, extremeT);
162
163 }
164
165
166 @Override
167 public E g(final FieldODEStateAndDerivative<E> state) {
168
169 final E rawG = rawDetector.g(state);
170
171
172 if (forward) {
173 final int last = transformers.length - 1;
174 if (extremeT.subtract(state.getTime()).getReal() < 0) {
175
176
177
178 final Transformer previous = transformers[last];
179 final Transformer next = filter.selectTransformer(previous, rawG.getReal(), forward);
180 if (next != previous) {
181
182
183
184
185
186
187 System.arraycopy(updates, 1, updates, 0, last);
188 System.arraycopy(transformers, 1, transformers, 0, last);
189 updates[last] = extremeT;
190 transformers[last] = next;
191 }
192
193 extremeT = state.getTime();
194
195
196 return next.transformed(rawG);
197
198 } else {
199
200
201
202 for (int i = last; i > 0; --i) {
203 if (updates[i].subtract(state.getTime()).getReal() <= 0) {
204
205 return transformers[i].transformed(rawG);
206 }
207 }
208
209 return transformers[0].transformed(rawG);
210
211 }
212 } else {
213 if (state.getTime().subtract(extremeT).getReal() < 0) {
214
215
216
217 final Transformer previous = transformers[0];
218 final Transformer next = filter.selectTransformer(previous, rawG.getReal(), forward);
219 if (next != previous) {
220
221
222
223
224
225
226 System.arraycopy(updates, 0, updates, 1, updates.length - 1);
227 System.arraycopy(transformers, 0, transformers, 1, transformers.length - 1);
228 updates[0] = extremeT;
229 transformers[0] = next;
230 }
231
232 extremeT = state.getTime();
233
234
235 return next.transformed(rawG);
236
237 } else {
238
239
240
241 for (int i = 0; i < updates.length - 1; ++i) {
242 if (state.getTime().subtract(updates[i]).getReal() <= 0) {
243
244 return transformers[i].transformed(rawG);
245 }
246 }
247
248 return transformers[updates.length - 1].transformed(rawG);
249
250 }
251 }
252
253 }
254
255
256
257
258
259 private static class LocalHandler<T extends FieldODEEventDetector<E>, E extends CalculusFieldElement<E>>
260 implements FieldODEEventHandler<E> {
261
262
263 private final FieldODEEventHandler<E> rawHandler;
264
265
266
267
268 LocalHandler(final FieldODEEventHandler<E> rawHandler) {
269 this.rawHandler = rawHandler;
270 }
271
272
273 @Override
274 public Action eventOccurred(final FieldODEStateAndDerivative<E> state,
275 final FieldODEEventDetector<E> detector,
276 final boolean increasing) {
277
278 @SuppressWarnings("unchecked")
279 final FieldEventSlopeFilter<T, E> esf = (FieldEventSlopeFilter<T, E>) detector;
280 return rawHandler.eventOccurred(state, esf, esf.filter.isTriggeredOnIncreasing());
281 }
282
283
284 @Override
285 public FieldODEState<E> resetState(final FieldODEEventDetector<E> detector,
286 final FieldODEStateAndDerivative<E> state) {
287
288 @SuppressWarnings("unchecked")
289 final FieldEventSlopeFilter<T, E> esf = (FieldEventSlopeFilter<T, E>) detector;
290 return rawHandler.resetState(esf, state);
291 }
292
293 }
294
295 }