1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.propagation.integration;
18
19 import java.io.NotSerializableException;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.math3.ode.ContinuousOutputModel;
28 import org.orekit.errors.OrekitException;
29 import org.orekit.errors.OrekitExceptionWrapper;
30 import org.orekit.errors.OrekitMessages;
31 import org.orekit.errors.PropagationException;
32 import org.orekit.frames.Frame;
33 import org.orekit.orbits.Orbit;
34 import org.orekit.propagation.AdditionalStateProvider;
35 import org.orekit.propagation.BoundedPropagator;
36 import org.orekit.propagation.SpacecraftState;
37 import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
38 import org.orekit.time.AbsoluteDate;
39 import org.orekit.utils.PVCoordinates;
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 public class IntegratedEphemeris
76 extends AbstractAnalyticalPropagator implements BoundedPropagator, Serializable {
77
78
79 private static final long serialVersionUID = 20130613L;
80
81
82 private final StateMapper mapper;
83
84
85 private final AbsoluteDate startDate;
86
87
88 private final AbsoluteDate minDate;
89
90
91 private final AbsoluteDate maxDate;
92
93
94 private ContinuousOutputModel model;
95
96
97 private final Map<String, double[]> unmanaged;
98
99
100
101
102
103
104
105
106
107
108
109
110 public IntegratedEphemeris(final AbsoluteDate startDate,
111 final AbsoluteDate minDate, final AbsoluteDate maxDate,
112 final StateMapper mapper, final ContinuousOutputModel model,
113 final Map<String, double[]> unmanaged,
114 final List<AdditionalStateProvider> providers,
115 final String[] equations)
116 throws OrekitException {
117
118 super(mapper.getAttitudeProvider());
119
120 this.startDate = startDate;
121 this.minDate = minDate;
122 this.maxDate = maxDate;
123 this.mapper = mapper;
124 this.model = model;
125 this.unmanaged = unmanaged;
126
127
128 for (final AdditionalStateProvider provider : providers) {
129 addAdditionalStateProvider(provider);
130 }
131
132
133 for (int i = 0; i < equations.length; ++i) {
134 addAdditionalStateProvider(new LocalProvider(equations[i], i));
135 }
136
137 }
138
139
140
141
142
143
144 private void setInterpolationDate(final AbsoluteDate date)
145 throws PropagationException {
146
147 if (date.equals(startDate.shiftedBy(model.getInterpolatedTime()))) {
148
149 return;
150 }
151
152 if ((date.compareTo(minDate) < 0) || (date.compareTo(maxDate) > 0)) {
153
154 throw new PropagationException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE,
155 date, minDate, maxDate);
156 }
157
158
159 model.setInterpolatedTime(date.durationFrom(startDate));
160
161 }
162
163
164 @Override
165 protected SpacecraftState basicPropagate(final AbsoluteDate date)
166 throws PropagationException {
167 try {
168 setInterpolationDate(date);
169 SpacecraftState state = mapper.mapArrayToState(model.getInterpolatedTime(),
170 model.getInterpolatedState());
171 for (Map.Entry<String, double[]> initial : unmanaged.entrySet()) {
172 state = state.addAdditionalState(initial.getKey(), initial.getValue());
173 }
174 return state;
175 } catch (OrekitExceptionWrapper oew) {
176 if (oew.getException() instanceof PropagationException) {
177 throw (PropagationException) oew.getException();
178 } else {
179 throw new PropagationException(oew.getException());
180 }
181 } catch (OrekitException oe) {
182 if (oe instanceof PropagationException) {
183 throw (PropagationException) oe;
184 } else {
185 throw new PropagationException(oe);
186 }
187 }
188 }
189
190
191 protected Orbit propagateOrbit(final AbsoluteDate date)
192 throws PropagationException {
193 return basicPropagate(date).getOrbit();
194 }
195
196
197 protected double getMass(final AbsoluteDate date) throws PropagationException {
198 return basicPropagate(date).getMass();
199 }
200
201
202 public PVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame)
203 throws OrekitException {
204 return propagate(date).getPVCoordinates(frame);
205 }
206
207
208
209
210 public AbsoluteDate getMinDate() {
211 return minDate;
212 }
213
214
215
216
217 public AbsoluteDate getMaxDate() {
218 return maxDate;
219 }
220
221 @Override
222 public Frame getFrame() {
223 return this.mapper.getFrame();
224 }
225
226
227 public void resetInitialState(final SpacecraftState state)
228 throws PropagationException {
229 throw new PropagationException(OrekitMessages.NON_RESETABLE_STATE);
230 }
231
232
233 public SpacecraftState getInitialState() throws PropagationException {
234 return updateAdditionalStates(basicPropagate(getMinDate()));
235 }
236
237
238
239
240
241 private Object writeReplace() throws NotSerializableException {
242
243
244 final String[] unmanagedNames = new String[unmanaged.size()];
245 final double[][] unmanagedValues = new double[unmanaged.size()][];
246 int i = 0;
247 for (Map.Entry<String, double[]> entry : unmanaged.entrySet()) {
248 unmanagedNames[i] = entry.getKey();
249 unmanagedValues[i] = entry.getValue();
250 ++i;
251 }
252
253
254 final List<AdditionalStateProvider> serializableProviders = new ArrayList<AdditionalStateProvider>();
255 final List<String> equationNames = new ArrayList<String>();
256 for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
257 if (provider instanceof LocalProvider) {
258 equationNames.add(((LocalProvider) provider).getName());
259 } else if (provider instanceof Serializable) {
260 serializableProviders.add(provider);
261 }
262 }
263
264 return new DataTransferObject(startDate, minDate, maxDate, mapper, model,
265 unmanagedNames, unmanagedValues,
266 serializableProviders.toArray(new AdditionalStateProvider[serializableProviders.size()]),
267 equationNames.toArray(new String[equationNames.size()]));
268
269 }
270
271
272 private class LocalProvider implements AdditionalStateProvider {
273
274
275 private final String name;
276
277
278 private final int index;
279
280
281
282
283
284 public LocalProvider(final String name, final int index) {
285 this.name = name;
286 this.index = index;
287 }
288
289
290 public String getName() {
291 return name;
292 }
293
294
295 public double[] getAdditionalState(final SpacecraftState state)
296 throws PropagationException {
297
298
299 setInterpolationDate(state.getDate());
300
301
302 return model.getInterpolatedSecondaryState(index);
303
304 }
305
306 }
307
308
309 private static class DataTransferObject implements Serializable {
310
311
312 private static final long serialVersionUID = 20130621L;
313
314
315 private final StateMapper mapper;
316
317
318 private final AbsoluteDate startDate;
319
320
321 private final AbsoluteDate minDate;
322
323
324 private final AbsoluteDate maxDate;
325
326
327 private final ContinuousOutputModel model;
328
329
330 private final String[] unmanagedNames;
331
332
333 private final double[][] unmanagedValues;
334
335
336 private final String[] equations;
337
338
339 private final AdditionalStateProvider[] providers;
340
341
342
343
344
345
346
347
348
349
350
351
352 public DataTransferObject(final AbsoluteDate startDate,
353 final AbsoluteDate minDate, final AbsoluteDate maxDate,
354 final StateMapper mapper, final ContinuousOutputModel model,
355 final String[] unmanagedNames, final double[][] unmanagedValues,
356 final AdditionalStateProvider[] providers,
357 final String[] equations) {
358 this.startDate = startDate;
359 this.minDate = minDate;
360 this.maxDate = maxDate;
361 this.mapper = mapper;
362 this.model = model;
363 this.unmanagedNames = unmanagedNames;
364 this.unmanagedValues = unmanagedValues;
365 this.providers = providers;
366 this.equations = equations;
367 }
368
369
370
371
372 private Object readResolve() {
373 try {
374 final Map<String, double[]> unmanaged = new HashMap<String, double[]>(unmanagedNames.length);
375 for (int i = 0; i < unmanagedNames.length; ++i) {
376 unmanaged.put(unmanagedNames[i], unmanagedValues[i]);
377 }
378 return new IntegratedEphemeris(startDate, minDate, maxDate, mapper, model,
379 unmanaged, Arrays.asList(providers), equations);
380 } catch (OrekitException oe) {
381 throw OrekitException.createInternalError(oe);
382 }
383 }
384
385 }
386
387 }