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;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.hipparchus.CalculusFieldElement;
29 import org.hipparchus.exception.LocalizedCoreFormats;
30 import org.hipparchus.exception.MathIllegalArgumentException;
31 import org.hipparchus.exception.MathIllegalStateException;
32 import org.hipparchus.ode.sampling.FieldODEStepHandler;
33 import org.hipparchus.ode.sampling.FieldODEStateInterpolator;
34 import org.hipparchus.util.FastMath;
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public class FieldDenseOutputModel<T extends CalculusFieldElement<T>>
85 implements FieldODEStepHandler<T> {
86
87
88 private T initialTime;
89
90
91 private T finalTime;
92
93
94 private boolean forward;
95
96
97 private int index;
98
99
100 private List<FieldODEStateInterpolator<T>> steps;
101
102
103
104
105 public FieldDenseOutputModel() {
106 steps = new ArrayList<>();
107 initialTime = null;
108 finalTime = null;
109 forward = true;
110 index = 0;
111 }
112
113
114
115
116
117
118
119
120
121
122
123 public void append(final FieldDenseOutputModel<T> model)
124 throws MathIllegalArgumentException, MathIllegalStateException {
125
126 if (model.steps.isEmpty()) {
127 return;
128 }
129
130 if (steps.isEmpty()) {
131 initialTime = model.initialTime;
132 forward = model.forward;
133 } else {
134
135
136 final FieldODEStateAndDerivative<T> s1 = steps.get(0).getPreviousState();
137 final FieldODEStateAndDerivative<T> s2 = model.steps.get(0).getPreviousState();
138 checkDimensionsEquality(s1.getPrimaryStateDimension(), s2.getPrimaryStateDimension());
139 checkDimensionsEquality(s1.getNumberOfSecondaryStates(), s2.getNumberOfSecondaryStates());
140 for (int i = 0; i < s1.getNumberOfSecondaryStates(); ++i) {
141 checkDimensionsEquality(s1.getSecondaryStateDimension(i), s2.getSecondaryStateDimension(i));
142 }
143
144 if (forward ^ model.forward) {
145 throw new MathIllegalArgumentException(LocalizedODEFormats.PROPAGATION_DIRECTION_MISMATCH);
146 }
147
148 final FieldODEStateInterpolator<T> lastInterpolator = steps.get(index);
149 final T current = lastInterpolator.getCurrentState().getTime();
150 final T previous = lastInterpolator.getPreviousState().getTime();
151 final T step = current.subtract(previous);
152 final T gap = model.getInitialTime().subtract(current);
153 if (gap.abs().subtract(step.abs().multiply(1.0e-3)).getReal() > 0) {
154 throw new MathIllegalArgumentException(LocalizedODEFormats.HOLE_BETWEEN_MODELS_TIME_RANGES,
155 gap.norm());
156 }
157
158 }
159
160 steps.addAll(model.steps);
161
162 index = steps.size() - 1;
163 finalTime = (steps.get(index)).getCurrentState().getTime();
164
165 }
166
167
168
169
170
171
172 private void checkDimensionsEquality(final int d1, final int d2)
173 throws MathIllegalArgumentException {
174 if (d1 != d2) {
175 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
176 d2, d1);
177 }
178 }
179
180
181 @Override
182 public void init(final FieldODEStateAndDerivative<T> initialState, final T t) {
183 initialTime = initialState.getTime();
184 finalTime = t;
185 forward = true;
186 index = 0;
187 steps.clear();
188 }
189
190
191 @Override
192 public void handleStep(final FieldODEStateInterpolator<T> interpolator) {
193
194 if (steps.isEmpty()) {
195 initialTime = interpolator.getPreviousState().getTime();
196 forward = interpolator.isForward();
197 }
198
199 steps.add(interpolator);
200 }
201
202
203 @Override
204 public void finish(FieldODEStateAndDerivative<T> finalState) {
205 finalTime = finalState.getTime();
206 index = steps.size() - 1;
207 }
208
209
210
211
212
213 public T getInitialTime() {
214 return initialTime;
215 }
216
217
218
219
220
221 public T getFinalTime() {
222 return finalTime;
223 }
224
225
226
227
228
229
230 public FieldODEStateAndDerivative<T> getInterpolatedState(final T time) {
231
232
233 int iMin = 0;
234 final FieldODEStateInterpolator<T> sMin = steps.get(iMin);
235 T tMin = sMin.getPreviousState().getTime().add(sMin.getCurrentState().getTime()).multiply(0.5);
236
237 int iMax = steps.size() - 1;
238 final FieldODEStateInterpolator<T> sMax = steps.get(iMax);
239 T tMax = sMax.getPreviousState().getTime().add(sMax.getCurrentState().getTime()).multiply(0.5);
240
241
242
243 if (locatePoint(time, sMin) <= 0) {
244 index = iMin;
245 return sMin.getInterpolatedState(time);
246 }
247 if (locatePoint(time, sMax) >= 0) {
248 index = iMax;
249 return sMax.getInterpolatedState(time);
250 }
251
252
253 while (iMax - iMin > 5) {
254
255
256 final FieldODEStateInterpolator<T> si = steps.get(index);
257 final int location = locatePoint(time, si);
258 if (location < 0) {
259 iMax = index;
260 tMax = si.getPreviousState().getTime().add(si.getCurrentState().getTime()).multiply(0.5);
261 } else if (location > 0) {
262 iMin = index;
263 tMin = si.getPreviousState().getTime().add(si.getCurrentState().getTime()).multiply(0.5);
264 } else {
265
266 return si.getInterpolatedState(time);
267 }
268
269
270 final int iMed = (iMin + iMax) / 2;
271 final FieldODEStateInterpolator<T> sMed = steps.get(iMed);
272 final T tMed = sMed.getPreviousState().getTime().add(sMed.getCurrentState().getTime()).multiply(0.5);
273
274 if (tMed.subtract(tMin).abs().subtract(1.0e-6).getReal() < 0 ||
275 tMax.subtract(tMed).abs().subtract(1.0e-6).getReal() < 0) {
276
277 index = iMed;
278 } else {
279
280
281
282 final T d12 = tMax.subtract(tMed);
283 final T d23 = tMed.subtract(tMin);
284 final T d13 = tMax.subtract(tMin);
285 final T dt1 = time.subtract(tMax);
286 final T dt2 = time.subtract(tMed);
287 final T dt3 = time.subtract(tMin);
288 final T iLagrange = dt2.multiply(dt3).multiply(d23).multiply(iMax).
289 subtract(dt1.multiply(dt3).multiply(d13).multiply(iMed)).
290 add( dt1.multiply(dt2).multiply(d12).multiply(iMin)).
291 divide(d12.multiply(d23).multiply(d13));
292 index = (int) FastMath.rint(iLagrange.getReal());
293 }
294
295
296 final int low = FastMath.max(iMin + 1, (9 * iMin + iMax) / 10);
297 final int high = FastMath.min(iMax - 1, (iMin + 9 * iMax) / 10);
298 if (index < low) {
299 index = low;
300 } else if (index > high) {
301 index = high;
302 }
303
304 }
305
306
307 index = iMin;
308 while (index <= iMax && locatePoint(time, steps.get(index)) > 0) {
309 ++index;
310 }
311
312 return steps.get(index).getInterpolatedState(time);
313
314 }
315
316
317
318
319
320
321
322
323 private int locatePoint(final T time, final FieldODEStateInterpolator<T> interval) {
324 if (forward) {
325 if (time.subtract(interval.getPreviousState().getTime()).getReal() < 0) {
326 return -1;
327 } else if (time.subtract(interval.getCurrentState().getTime()).getReal() > 0) {
328 return +1;
329 } else {
330 return 0;
331 }
332 }
333 if (time.subtract(interval.getPreviousState().getTime()).getReal() > 0) {
334 return -1;
335 } else if (time.subtract(interval.getCurrentState().getTime()).getReal() < 0) {
336 return +1;
337 } else {
338 return 0;
339 }
340 }
341
342 }