1   /* Copyright 2002-2024 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.propagation.integration;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Queue;
28  
29  import org.hipparchus.analysis.UnivariateFunction;
30  import org.hipparchus.analysis.solvers.BracketedUnivariateSolver;
31  import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
32  import org.hipparchus.exception.MathRuntimeException;
33  import org.hipparchus.ode.DenseOutputModel;
34  import org.hipparchus.ode.ExpandableODE;
35  import org.hipparchus.ode.ODEIntegrator;
36  import org.hipparchus.ode.ODEState;
37  import org.hipparchus.ode.ODEStateAndDerivative;
38  import org.hipparchus.ode.OrdinaryDifferentialEquation;
39  import org.hipparchus.ode.SecondaryODE;
40  import org.hipparchus.ode.events.Action;
41  import org.hipparchus.ode.events.AdaptableInterval;
42  import org.hipparchus.ode.events.ODEEventDetector;
43  import org.hipparchus.ode.events.ODEEventHandler;
44  import org.hipparchus.ode.sampling.AbstractODEStateInterpolator;
45  import org.hipparchus.ode.sampling.ODEStateInterpolator;
46  import org.hipparchus.ode.sampling.ODEStepHandler;
47  import org.hipparchus.util.Precision;
48  import org.orekit.attitudes.AttitudeProvider;
49  import org.orekit.errors.OrekitException;
50  import org.orekit.errors.OrekitInternalError;
51  import org.orekit.errors.OrekitMessages;
52  import org.orekit.frames.Frame;
53  import org.orekit.orbits.OrbitType;
54  import org.orekit.orbits.PositionAngleType;
55  import org.orekit.propagation.AbstractPropagator;
56  import org.orekit.propagation.BoundedPropagator;
57  import org.orekit.propagation.EphemerisGenerator;
58  import org.orekit.propagation.PropagationType;
59  import org.orekit.propagation.SpacecraftState;
60  import org.orekit.propagation.events.EventDetector;
61  import org.orekit.propagation.events.handlers.EventHandler;
62  import org.orekit.propagation.sampling.OrekitStepHandler;
63  import org.orekit.propagation.sampling.OrekitStepInterpolator;
64  import org.orekit.time.AbsoluteDate;
65  import org.orekit.utils.DoubleArrayDictionary;
66  
67  
68  /** Common handling of {@link org.orekit.propagation.Propagator Propagator}
69   *  methods for both numerical and semi-analytical propagators.
70   *  @author Luc Maisonobe
71   */
72  public abstract class AbstractIntegratedPropagator extends AbstractPropagator {
73  
74      /** Internal name used for complete secondary state dimension.
75       * @since 11.1
76       */
77      private static final String SECONDARY_DIMENSION = "Orekit-secondary-dimension";
78  
79      /** Event detectors not related to force models. */
80      private final List<EventDetector> detectors;
81  
82      /** Step handlers dedicated to ephemeris generation. */
83      private final List<StoringStepHandler> ephemerisGenerators;
84  
85      /** Integrator selected by the user for the orbital extrapolation process. */
86      private final ODEIntegrator integrator;
87  
88      /** Offsets of secondary states managed by {@link AdditionalDerivativesProvider}.
89       * @since 11.1
90       */
91      private final Map<String, Integer> secondaryOffsets;
92  
93      /** Additional derivatives providers.
94       * @since 11.1
95       */
96      private final List<AdditionalDerivativesProvider> additionalDerivativesProviders;
97  
98      /** Map of secondary equation offset in main
99      /** Counter for differential equations calls. */
100     private int calls;
101 
102     /** Mapper between raw double components and space flight dynamics objects. */
103     private StateMapper stateMapper;
104 
105     /**
106      * Attitude provider when evaluating derivatives. Can be a frozen one for performance.
107      * @since 12.1
108      */
109     private AttitudeProvider attitudeProviderForDerivatives;
110 
111     /** Flag for resetting the state at end of propagation. */
112     private boolean resetAtEnd;
113 
114     /** Type of orbit to output (mean or osculating) <br/>
115      * <p>
116      * This is used only in the case of semi-analytical propagators where there is a clear separation between
117      * mean and short periodic elements. It is ignored by the Numerical propagator.
118      * </p>
119      */
120     private final PropagationType propagationType;
121 
122     /** Build a new instance.
123      * @param integrator numerical integrator to use for propagation.
124      * @param propagationType type of orbit to output (mean or osculating).
125      */
126     protected AbstractIntegratedPropagator(final ODEIntegrator integrator, final PropagationType propagationType) {
127         detectors                      = new ArrayList<>();
128         ephemerisGenerators            = new ArrayList<>();
129         additionalDerivativesProviders = new ArrayList<>();
130         this.secondaryOffsets          = new HashMap<>();
131         this.integrator                = integrator;
132         this.propagationType           = propagationType;
133         this.resetAtEnd                = true;
134     }
135 
136     /** Allow/disallow resetting the initial state at end of propagation.
137      * <p>
138      * By default, at the end of the propagation, the propagator resets the initial state
139      * to the final state, thus allowing a new propagation to be started from there without
140      * recomputing the part already performed. Calling this method with {@code resetAtEnd} set
141      * to false changes prevents such reset.
142      * </p>
143      * @param resetAtEnd if true, at end of each propagation, the {@link
144      * #getInitialState() initial state} will be reset to the final state of
145      * the propagation, otherwise the initial state will be preserved
146      * @since 9.0
147      */
148     public void setResetAtEnd(final boolean resetAtEnd) {
149         this.resetAtEnd = resetAtEnd;
150     }
151 
152     /** Getter for the resetting flag regarding initial state.
153      * @return resetting flag
154      * @since 12.0
155      */
156     public boolean getResetAtEnd() {
157         return this.resetAtEnd;
158     }
159 
160     /**
161      * Method called when initializing the attitude provider used when evaluating derivatives.
162      * @return attitude provider for derivatives
163      */
164     protected AttitudeProvider initializeAttitudeProviderForDerivatives() {
165         return getAttitudeProvider();
166     }
167 
168     /** Initialize the mapper. */
169     protected void initMapper() {
170         stateMapper = createMapper(null, Double.NaN, null, null, null, null);
171     }
172 
173     /** Get the integrator's name.
174      * @return name of underlying integrator
175      * @since 12.0
176      */
177     public String getIntegratorName() {
178         return integrator.getName();
179     }
180 
181     /**  {@inheritDoc} */
182     @Override
183     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
184         super.setAttitudeProvider(attitudeProvider);
185         stateMapper = createMapper(stateMapper.getReferenceDate(), stateMapper.getMu(),
186                                    stateMapper.getOrbitType(), stateMapper.getPositionAngleType(),
187                                    attitudeProvider, stateMapper.getFrame());
188     }
189 
190     /** Set propagation orbit type.
191      * @param orbitType orbit type to use for propagation, null for
192      * propagating using {@link org.orekit.utils.AbsolutePVCoordinates AbsolutePVCoordinates}
193      * rather than {@link org.orekit.orbits Orbit}
194      */
195     protected void setOrbitType(final OrbitType orbitType) {
196         stateMapper = createMapper(stateMapper.getReferenceDate(), stateMapper.getMu(),
197                                    orbitType, stateMapper.getPositionAngleType(),
198                                    stateMapper.getAttitudeProvider(), stateMapper.getFrame());
199     }
200 
201     /** Get propagation parameter type.
202      * @return orbit type used for propagation, null for
203      * propagating using {@link org.orekit.utils.AbsolutePVCoordinates AbsolutePVCoordinates}
204      * rather than {@link org.orekit.orbits Orbit}
205      */
206     protected OrbitType getOrbitType() {
207         return stateMapper.getOrbitType();
208     }
209 
210     /** Get the propagation type.
211      * @return propagation type.
212      * @since 11.1
213      */
214     public PropagationType getPropagationType() {
215         return propagationType;
216     }
217 
218     /** Set position angle type.
219      * <p>
220      * The position parameter type is meaningful only if {@link
221      * #getOrbitType() propagation orbit type}
222      * support it. As an example, it is not meaningful for propagation
223      * in {@link OrbitType#CARTESIAN Cartesian} parameters.
224      * </p>
225      * @param positionAngleType angle type to use for propagation
226      */
227     protected void setPositionAngleType(final PositionAngleType positionAngleType) {
228         stateMapper = createMapper(stateMapper.getReferenceDate(), stateMapper.getMu(),
229                                    stateMapper.getOrbitType(), positionAngleType,
230                                    stateMapper.getAttitudeProvider(), stateMapper.getFrame());
231     }
232 
233     /** Get propagation parameter type.
234      * @return angle type to use for propagation
235      */
236     protected PositionAngleType getPositionAngleType() {
237         return stateMapper.getPositionAngleType();
238     }
239 
240     /** Set the central attraction coefficient μ.
241      * @param mu central attraction coefficient (m³/s²)
242      */
243     public void setMu(final double mu) {
244         stateMapper = createMapper(stateMapper.getReferenceDate(), mu,
245                                    stateMapper.getOrbitType(), stateMapper.getPositionAngleType(),
246                                    stateMapper.getAttitudeProvider(), stateMapper.getFrame());
247     }
248 
249     /** Get the central attraction coefficient μ.
250      * @return mu central attraction coefficient (m³/s²)
251      * @see #setMu(double)
252      */
253     public double getMu() {
254         return stateMapper.getMu();
255     }
256 
257     /** Get the number of calls to the differential equations computation method.
258      * <p>The number of calls is reset each time the {@link #propagate(AbsoluteDate)}
259      * method is called.</p>
260      * @return number of calls to the differential equations computation method
261      */
262     public int getCalls() {
263         return calls;
264     }
265 
266     /** {@inheritDoc} */
267     @Override
268     public boolean isAdditionalStateManaged(final String name) {
269 
270         // first look at already integrated states
271         if (super.isAdditionalStateManaged(name)) {
272             return true;
273         }
274 
275         // then look at states we integrate ourselves
276         for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
277             if (provider.getName().equals(name)) {
278                 return true;
279             }
280         }
281 
282         return false;
283     }
284 
285     /** {@inheritDoc} */
286     @Override
287     public String[] getManagedAdditionalStates() {
288         final String[] alreadyIntegrated = super.getManagedAdditionalStates();
289         final String[] managed = new String[alreadyIntegrated.length + additionalDerivativesProviders.size()];
290         System.arraycopy(alreadyIntegrated, 0, managed, 0, alreadyIntegrated.length);
291         for (int i = 0; i < additionalDerivativesProviders.size(); ++i) {
292             managed[i + alreadyIntegrated.length] = additionalDerivativesProviders.get(i).getName();
293         }
294         return managed;
295     }
296 
297     /** Add a provider for user-specified state derivatives to be integrated along with the orbit propagation.
298      * @param provider provider for additional derivatives
299      * @see #addAdditionalStateProvider(org.orekit.propagation.AdditionalStateProvider)
300      * @since 11.1
301      */
302     public void addAdditionalDerivativesProvider(final AdditionalDerivativesProvider provider) {
303 
304         // check if the name is already used
305         if (isAdditionalStateManaged(provider.getName())) {
306             // these derivatives are already registered, complain
307             throw new OrekitException(OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE,
308                                       provider.getName());
309         }
310 
311         // this is really a new set of derivatives, add it
312         additionalDerivativesProviders.add(provider);
313 
314         secondaryOffsets.clear();
315 
316     }
317 
318     /** Get an unmodifiable list of providers for additional derivatives.
319      * @return providers for the additional derivatives
320      * @since 11.1
321      */
322     public List<AdditionalDerivativesProvider> getAdditionalDerivativesProviders() {
323         return Collections.unmodifiableList(additionalDerivativesProviders);
324     }
325 
326     /** {@inheritDoc} */
327     public void addEventDetector(final EventDetector detector) {
328         detectors.add(detector);
329     }
330 
331     /** {@inheritDoc} */
332     public Collection<EventDetector> getEventsDetectors() {
333         return Collections.unmodifiableCollection(detectors);
334     }
335 
336     /** {@inheritDoc} */
337     public void clearEventsDetectors() {
338         detectors.clear();
339     }
340 
341     /** Set up all user defined event detectors.
342      */
343     protected void setUpUserEventDetectors() {
344         for (final EventDetector detector : detectors) {
345             setUpEventDetector(integrator, detector);
346         }
347     }
348 
349     /** Wrap an Orekit event detector and register it to the integrator.
350      * @param integ integrator into which event detector should be registered
351      * @param detector event detector to wrap
352      */
353     protected void setUpEventDetector(final ODEIntegrator integ, final EventDetector detector) {
354         integ.addEventDetector(new AdaptedEventDetector(detector));
355     }
356 
357     /** {@inheritDoc} */
358     @Override
359     public EphemerisGenerator getEphemerisGenerator() {
360         final StoringStepHandler storingHandler = new StoringStepHandler();
361         ephemerisGenerators.add(storingHandler);
362         return storingHandler;
363     }
364 
365     /** Create a mapper between raw double components and spacecraft state.
366     /** Simple constructor.
367      * <p>
368      * The position parameter type is meaningful only if {@link
369      * #getOrbitType() propagation orbit type}
370      * support it. As an example, it is not meaningful for propagation
371      * in {@link OrbitType#CARTESIAN Cartesian} parameters.
372      * </p>
373      * @param referenceDate reference date
374      * @param mu central attraction coefficient (m³/s²)
375      * @param orbitType orbit type to use for mapping
376      * @param positionAngleType angle type to use for propagation
377      * @param attitudeProvider attitude provider
378      * @param frame inertial frame
379      * @return new mapper
380      */
381     protected abstract StateMapper createMapper(AbsoluteDate referenceDate, double mu,
382                                                 OrbitType orbitType, PositionAngleType positionAngleType,
383                                                 AttitudeProvider attitudeProvider, Frame frame);
384 
385     /** Get the differential equations to integrate (for main state only).
386      * @param integ numerical integrator to use for propagation.
387      * @return differential equations for main state
388      */
389     protected abstract MainStateEquations getMainStateEquations(ODEIntegrator integ);
390 
391     /** {@inheritDoc} */
392     @Override
393     public SpacecraftState propagate(final AbsoluteDate target) {
394         if (getStartDate() == null) {
395             if (getInitialState() == null) {
396                 throw new OrekitException(OrekitMessages.INITIAL_STATE_NOT_SPECIFIED_FOR_ORBIT_PROPAGATION);
397             }
398             setStartDate(getInitialState().getDate());
399         }
400         return propagate(getStartDate(), target);
401     }
402 
403     /** {@inheritDoc} */
404     public SpacecraftState propagate(final AbsoluteDate tStart, final AbsoluteDate tEnd) {
405 
406         if (getInitialState() == null) {
407             throw new OrekitException(OrekitMessages.INITIAL_STATE_NOT_SPECIFIED_FOR_ORBIT_PROPAGATION);
408         }
409 
410         // make sure the integrator will be reset properly even if we change its events handlers and step handlers
411         try (IntegratorResetter resetter = new IntegratorResetter(integrator)) {
412 
413             // prepare handling of STM and Jacobian matrices
414             setUpStmAndJacobianGenerators();
415 
416             // Initialize additional states
417             initializeAdditionalStates(tEnd);
418 
419             if (!tStart.equals(getInitialState().getDate())) {
420                 // if propagation start date is not initial date,
421                 // propagate from initial to start date without event detection
422                 try (IntegratorResetter startResetter = new IntegratorResetter(integrator)) {
423                     integrateDynamics(tStart, true);
424                 }
425             }
426 
427             // set up events added by user
428             setUpUserEventDetectors();
429 
430             // set up step handlers
431             for (final OrekitStepHandler handler : getMultiplexer().getHandlers()) {
432                 integrator.addStepHandler(new AdaptedStepHandler(handler));
433             }
434             for (final StoringStepHandler generator : ephemerisGenerators) {
435                 generator.setEndDate(tEnd);
436                 integrator.addStepHandler(generator);
437             }
438 
439             // propagate from start date to end date with event detection
440             final SpacecraftState finalState = integrateDynamics(tEnd, false);
441 
442             return finalState;
443 
444         }
445 
446     }
447 
448     /** Set up State Transition Matrix and Jacobian matrix handling.
449      * @since 11.1
450      */
451     protected void setUpStmAndJacobianGenerators() {
452         // nothing to do by default
453     }
454 
455     /** Propagation with or without event detection.
456      * @param tEnd target date to which orbit should be propagated
457      * @param forceResetAtEnd flag to force resetting state and date after integration
458      * @return state at end of propagation
459      */
460     private SpacecraftState integrateDynamics(final AbsoluteDate tEnd, final boolean forceResetAtEnd) {
461         try {
462 
463             initializePropagation();
464 
465             if (getInitialState().getDate().equals(tEnd)) {
466                 // don't extrapolate
467                 return getInitialState();
468             }
469 
470             // space dynamics view
471             stateMapper = createMapper(getInitialState().getDate(), stateMapper.getMu(),
472                                        stateMapper.getOrbitType(), stateMapper.getPositionAngleType(),
473                                        stateMapper.getAttitudeProvider(), getInitialState().getFrame());
474             attitudeProviderForDerivatives = initializeAttitudeProviderForDerivatives();
475 
476             if (Double.isNaN(getMu())) {
477                 setMu(getInitialState().getMu());
478             }
479 
480             if (getInitialState().getMass() <= 0.0) {
481                 throw new OrekitException(OrekitMessages.NOT_POSITIVE_SPACECRAFT_MASS,
482                                           getInitialState().getMass());
483             }
484 
485             // convert space flight dynamics API to math API
486             final SpacecraftState initialIntegrationState = getInitialIntegrationState();
487             final ODEState mathInitialState = createInitialState(initialIntegrationState);
488             final ExpandableODE mathODE = createODE(integrator);
489 
490             // mathematical integration
491             final ODEStateAndDerivative mathFinalState;
492             beforeIntegration(initialIntegrationState, tEnd);
493             mathFinalState = integrator.integrate(mathODE, mathInitialState,
494                                                   tEnd.durationFrom(getInitialState().getDate()));
495             afterIntegration();
496 
497             // get final state
498             SpacecraftState finalState =
499                             stateMapper.mapArrayToState(stateMapper.mapDoubleToDate(mathFinalState.getTime(),
500                                                                                     tEnd),
501                                                         mathFinalState.getPrimaryState(),
502                                                         mathFinalState.getPrimaryDerivative(),
503                                                         propagationType);
504 
505             finalState = updateAdditionalStatesAndDerivatives(finalState, mathFinalState);
506 
507             if (resetAtEnd || forceResetAtEnd) {
508                 resetInitialState(finalState);
509                 setStartDate(finalState.getDate());
510             }
511 
512             return finalState;
513 
514         } catch (MathRuntimeException mre) {
515             throw OrekitException.unwrap(mre);
516         }
517     }
518 
519     /**
520      * Returns an updated version of the inputted state with additional states, including
521      * from derivatives providers.
522      * @param originalState input state
523      * @param os ODE state and derivative
524      * @return new state
525      * @since 12.1
526      */
527     private SpacecraftState updateAdditionalStatesAndDerivatives(final SpacecraftState originalState,
528                                                                  final ODEStateAndDerivative os) {
529         SpacecraftState updatedState = originalState;
530         if (os.getNumberOfSecondaryStates() > 0) {
531             final double[] secondary           = os.getSecondaryState(1);
532             final double[] secondaryDerivative = os.getSecondaryDerivative(1);
533             for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
534                 final String name      = provider.getName();
535                 final int    offset    = secondaryOffsets.get(name);
536                 final int    dimension = provider.getDimension();
537                 updatedState = updatedState.addAdditionalState(name, Arrays.copyOfRange(secondary, offset, offset + dimension));
538                 updatedState = updatedState.addAdditionalStateDerivative(name, Arrays.copyOfRange(secondaryDerivative, offset, offset + dimension));
539             }
540         }
541         return updateAdditionalStates(updatedState);
542     }
543 
544     /** Get the initial state for integration.
545      * @return initial state for integration
546      */
547     protected SpacecraftState getInitialIntegrationState() {
548         return getInitialState();
549     }
550 
551     /** Create an initial state.
552      * @param initialState initial state in flight dynamics world
553      * @return initial state in mathematics world
554      */
555     private ODEState createInitialState(final SpacecraftState initialState) {
556 
557         // retrieve initial state
558         final double[] primary = new double[getBasicDimension()];
559         stateMapper.mapStateToArray(initialState, primary, null);
560 
561         if (secondaryOffsets.isEmpty()) {
562             // compute dimension of the secondary state
563             int offset = 0;
564             for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
565                 secondaryOffsets.put(provider.getName(), offset);
566                 offset += provider.getDimension();
567             }
568             secondaryOffsets.put(SECONDARY_DIMENSION, offset);
569         }
570 
571         return new ODEState(0.0, primary, secondary(initialState));
572 
573     }
574 
575     /** Create secondary state.
576      * @param state spacecraft state
577      * @return secondary state
578      * @since 11.1
579      */
580     private double[][] secondary(final SpacecraftState state) {
581 
582         if (secondaryOffsets.isEmpty()) {
583             return null;
584         }
585 
586         final double[][] secondary = new double[1][secondaryOffsets.get(SECONDARY_DIMENSION)];
587         for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
588             final String   name       = provider.getName();
589             final int      offset     = secondaryOffsets.get(name);
590             final double[] additional = state.getAdditionalState(name);
591             System.arraycopy(additional, 0, secondary[0], offset, additional.length);
592         }
593 
594         return secondary;
595 
596     }
597 
598     /** Create secondary state derivative.
599      * @param state spacecraft state
600      * @return secondary state derivative
601      * @since 11.1
602      */
603     private double[][] secondaryDerivative(final SpacecraftState state) {
604 
605         if (secondaryOffsets.isEmpty()) {
606             return null;
607         }
608 
609         final double[][] secondaryDerivative = new double[1][secondaryOffsets.get(SECONDARY_DIMENSION)];
610         for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
611             final String   name       = provider.getName();
612             final int      offset     = secondaryOffsets.get(name);
613             final double[] additionalDerivative = state.getAdditionalStateDerivative(name);
614             System.arraycopy(additionalDerivative, 0, secondaryDerivative[0], offset, additionalDerivative.length);
615         }
616 
617         return secondaryDerivative;
618 
619     }
620 
621     /** Create an ODE with all equations.
622      * @param integ numerical integrator to use for propagation.
623      * @return a new ode
624      */
625     private ExpandableODE createODE(final ODEIntegrator integ) {
626 
627         final ExpandableODE ode =
628                 new ExpandableODE(new ConvertedMainStateEquations(getMainStateEquations(integ)));
629 
630         // secondary part of the ODE
631         if (!additionalDerivativesProviders.isEmpty()) {
632             ode.addSecondaryEquations(new ConvertedSecondaryStateEquations());
633         }
634 
635         return ode;
636 
637     }
638 
639     /** Method called just before integration.
640      * <p>
641      * The default implementation does nothing, it may be specialized in subclasses.
642      * </p>
643      * @param initialState initial state
644      * @param tEnd target date at which state should be propagated
645      */
646     protected void beforeIntegration(final SpacecraftState initialState,
647                                      final AbsoluteDate tEnd) {
648         // do nothing by default
649     }
650 
651     /** Method called just after integration.
652      * <p>
653      * The default implementation does nothing, it may be specialized in subclasses.
654      * </p>
655      */
656     protected void afterIntegration() {
657         // do nothing by default
658     }
659 
660     /** Get state vector dimension without additional parameters.
661      * @return state vector dimension without additional parameters.
662      */
663     public int getBasicDimension() {
664         return 7;
665     }
666 
667     /** Get the integrator used by the propagator.
668      * @return the integrator.
669      */
670     protected ODEIntegrator getIntegrator() {
671         return integrator;
672     }
673 
674     /** Convert a state from mathematical world to space flight dynamics world.
675      * @param os mathematical state
676      * @return space flight dynamics state
677      */
678     private SpacecraftState convert(final ODEStateAndDerivative os) {
679 
680         final SpacecraftState s = stateMapper.mapArrayToState(os.getTime(), os.getPrimaryState(),
681             os.getPrimaryDerivative(), propagationType);
682         return updateAdditionalStatesAndDerivatives(s, os);
683     }
684 
685     /** Convert a state from space flight dynamics world to mathematical world.
686      * @param state space flight dynamics state
687      * @return mathematical state
688      */
689     private ODEStateAndDerivative convert(final SpacecraftState state) {
690 
691         // retrieve initial state
692         final double[] primary    = new double[getBasicDimension()];
693         final double[] primaryDot = new double[getBasicDimension()];
694         stateMapper.mapStateToArray(state, primary, primaryDot);
695 
696         // secondary part of the ODE
697         final double[][] secondary           = secondary(state);
698         final double[][] secondaryDerivative = secondaryDerivative(state);
699 
700         return new ODEStateAndDerivative(stateMapper.mapDateToDouble(state.getDate()),
701                                          primary, primaryDot,
702                                          secondary, secondaryDerivative);
703 
704     }
705 
706     /** Differential equations for the main state (orbit, attitude and mass). */
707     public interface MainStateEquations {
708 
709         /**
710          * Initialize the equations at the start of propagation. This method will be
711          * called before any calls to {@link #computeDerivatives(SpacecraftState)}.
712          *
713          * <p> The default implementation of this method does nothing.
714          *
715          * @param initialState initial state information at the start of propagation.
716          * @param target       date of propagation. Not equal to {@code
717          *                     initialState.getDate()}.
718          */
719         default void init(final SpacecraftState initialState, final AbsoluteDate target) {
720         }
721 
722         /** Compute differential equations for main state.
723          * @param state current state
724          * @return derivatives of main state
725          */
726         double[] computeDerivatives(SpacecraftState state);
727 
728     }
729 
730     /** Differential equations for the main state (orbit, attitude and mass), with converted API. */
731     private class ConvertedMainStateEquations implements OrdinaryDifferentialEquation {
732 
733         /** Main state equations. */
734         private final MainStateEquations main;
735 
736         /** Simple constructor.
737          * @param main main state equations
738          */
739         ConvertedMainStateEquations(final MainStateEquations main) {
740             this.main = main;
741             calls = 0;
742         }
743 
744         /** {@inheritDoc} */
745         public int getDimension() {
746             return getBasicDimension();
747         }
748 
749         @Override
750         public void init(final double t0, final double[] y0, final double finalTime) {
751             // update space dynamics view
752             SpacecraftState initialState = stateMapper.mapArrayToState(t0, y0, null, PropagationType.MEAN);
753             initialState = updateAdditionalStates(initialState);
754             initialState = updateStatesFromAdditionalDerivativesIfKnown(initialState);
755             final AbsoluteDate target = stateMapper.mapDoubleToDate(finalTime);
756             main.init(initialState, target);
757             attitudeProviderForDerivatives = initializeAttitudeProviderForDerivatives();
758         }
759 
760         /**
761          * Returns an updated version of the inputted state, with additional states from
762          * derivatives providers as given in the stored initial state.
763          * @param originalState input state
764          * @return new state
765          * @since 12.1
766          */
767         private SpacecraftState updateStatesFromAdditionalDerivativesIfKnown(final SpacecraftState originalState) {
768             SpacecraftState updatedState = originalState;
769             final SpacecraftState storedInitialState = getInitialState();
770             final double originalTime = stateMapper.mapDateToDouble(originalState.getDate());
771             if (storedInitialState != null && stateMapper.mapDateToDouble(storedInitialState.getDate()) == originalTime) {
772                 for (final AdditionalDerivativesProvider provider: additionalDerivativesProviders) {
773                     final String name = provider.getName();
774                     final double[] value = storedInitialState.getAdditionalState(name);
775                     updatedState = updatedState.addAdditionalState(name, value);
776                 }
777             }
778             return updatedState;
779         }
780 
781         /** {@inheritDoc} */
782         public double[] computeDerivatives(final double t, final double[] y) {
783 
784             // increment calls counter
785             ++calls;
786 
787             // update space dynamics view
788             stateMapper.setAttitudeProvider(attitudeProviderForDerivatives);
789             SpacecraftState currentState = stateMapper.mapArrayToState(t, y, null, PropagationType.MEAN);
790             stateMapper.setAttitudeProvider(getAttitudeProvider());
791 
792             currentState = updateAdditionalStates(currentState);
793             // compute main state differentials
794             return main.computeDerivatives(currentState);
795 
796         }
797 
798     }
799 
800     /** Differential equations for the secondary state (Jacobians, user variables ...), with converted API. */
801     private class ConvertedSecondaryStateEquations implements SecondaryODE {
802 
803         /** Dimension of the combined additional states. */
804         private final int combinedDimension;
805 
806         /** Simple constructor.
807           */
808         ConvertedSecondaryStateEquations() {
809             this.combinedDimension = secondaryOffsets.get(SECONDARY_DIMENSION);
810         }
811 
812         /** {@inheritDoc} */
813         @Override
814         public int getDimension() {
815             return combinedDimension;
816         }
817 
818         /** {@inheritDoc} */
819         @Override
820         public void init(final double t0, final double[] primary0,
821                          final double[] secondary0, final double finalTime) {
822             // update space dynamics view
823             final SpacecraftState initialState = convert(t0, primary0, null, secondary0);
824 
825             final AbsoluteDate target = stateMapper.mapDoubleToDate(finalTime);
826             for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
827                 provider.init(initialState, target);
828             }
829 
830         }
831 
832         /** {@inheritDoc} */
833         @Override
834         public double[] computeDerivatives(final double t, final double[] primary,
835                                            final double[] primaryDot, final double[] secondary) {
836 
837             // update space dynamics view
838             // the integrable generators generate method will be called here,
839             // according to the generators yield order
840             SpacecraftState updated = convert(t, primary, primaryDot, secondary);
841 
842             // set up queue for equations
843             final Queue<AdditionalDerivativesProvider> pending = new LinkedList<>(additionalDerivativesProviders);
844 
845             // gather the derivatives from all additional equations, taking care of dependencies
846             final double[] secondaryDot = new double[combinedDimension];
847             int yieldCount = 0;
848             while (!pending.isEmpty()) {
849                 final AdditionalDerivativesProvider provider = pending.remove();
850                 if (provider.yields(updated)) {
851                     // this provider has to wait for another one,
852                     // we put it again in the pending queue
853                     pending.add(provider);
854                     if (++yieldCount >= pending.size()) {
855                         // all pending providers yielded!, they probably need data not yet initialized
856                         // we let the propagation proceed, if these data are really needed right now
857                         // an appropriate exception will be triggered when caller tries to access them
858                         break;
859                     }
860                 } else {
861                     // we can use these equations right now
862                     final String              name           = provider.getName();
863                     final int                 offset         = secondaryOffsets.get(name);
864                     final int                 dimension      = provider.getDimension();
865                     final CombinedDerivatives derivatives    = provider.combinedDerivatives(updated);
866                     final double[]            additionalPart = derivatives.getAdditionalDerivatives();
867                     final double[]            mainPart       = derivatives.getMainStateDerivativesIncrements();
868                     System.arraycopy(additionalPart, 0, secondaryDot, offset, dimension);
869                     updated = updated.addAdditionalStateDerivative(name, additionalPart);
870                     if (mainPart != null) {
871                         // this equation does change the main state derivatives
872                         for (int i = 0; i < mainPart.length; ++i) {
873                             primaryDot[i] += mainPart[i];
874                         }
875                     }
876                     yieldCount = 0;
877                 }
878             }
879 
880             return secondaryDot;
881 
882         }
883 
884         /** Convert mathematical view to space view.
885          * @param t current value of the independent <I>time</I> variable
886          * @param primary array containing the current value of the primary state vector
887          * @param primaryDot array containing the derivative of the primary state vector
888          * @param secondary array containing the current value of the secondary state vector
889          * @return space view of the state
890          */
891         private SpacecraftState convert(final double t, final double[] primary,
892                                         final double[] primaryDot, final double[] secondary) {
893 
894             SpacecraftState initialState = stateMapper.mapArrayToState(t, primary, primaryDot, PropagationType.MEAN);
895 
896             for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
897                 final String name      = provider.getName();
898                 final int    offset    = secondaryOffsets.get(name);
899                 final int    dimension = provider.getDimension();
900                 initialState = initialState.addAdditionalState(name, Arrays.copyOfRange(secondary, offset, offset + dimension));
901             }
902 
903             return updateAdditionalStates(initialState);
904 
905         }
906 
907     }
908 
909     /** Adapt an {@link org.orekit.propagation.events.EventDetector}
910      * to Hipparchus {@link org.hipparchus.ode.events.ODEEventDetector} interface.
911      * @author Fabien Maussion
912      */
913     private class AdaptedEventDetector implements ODEEventDetector {
914 
915         /** Underlying event detector. */
916         private final EventDetector detector;
917 
918         /** Underlying event handler.
919          * @since 12.0
920          */
921         private final EventHandler handler;
922 
923         /** Time of the previous call to g. */
924         private double lastT;
925 
926         /** Value from the previous call to g. */
927         private double lastG;
928 
929         /** Build a wrapped event detector.
930          * @param detector event detector to wrap
931         */
932         AdaptedEventDetector(final EventDetector detector) {
933             this.detector = detector;
934             this.handler  = detector.getHandler();
935             this.lastT    = Double.NaN;
936             this.lastG    = Double.NaN;
937         }
938 
939         /** {@inheritDoc} */
940         @Override
941         public AdaptableInterval getMaxCheckInterval() {
942             return s -> detector.getMaxCheckInterval().currentInterval(convert(s));
943         }
944 
945         /** {@inheritDoc} */
946         @Override
947         public int getMaxIterationCount() {
948             return detector.getMaxIterationCount();
949         }
950 
951         /** {@inheritDoc} */
952         @Override
953         public BracketedUnivariateSolver<UnivariateFunction> getSolver() {
954             return new BracketingNthOrderBrentSolver(0, detector.getThreshold(), 0, 5);
955         }
956 
957         /** {@inheritDoc} */
958         @Override
959         public void init(final ODEStateAndDerivative s0, final double t) {
960             detector.init(convert(s0), stateMapper.mapDoubleToDate(t));
961             this.lastT = Double.NaN;
962             this.lastG = Double.NaN;
963         }
964 
965         /** {@inheritDoc} */
966         public double g(final ODEStateAndDerivative s) {
967             if (!Precision.equals(lastT, s.getTime(), 0)) {
968                 lastT = s.getTime();
969                 lastG = detector.g(convert(s));
970             }
971             return lastG;
972         }
973 
974         /** {@inheritDoc} */
975         public ODEEventHandler getHandler() {
976 
977             return new ODEEventHandler() {
978 
979                 /** {@inheritDoc} */
980                 public Action eventOccurred(final ODEStateAndDerivative s, final ODEEventDetector d, final boolean increasing) {
981                     return handler.eventOccurred(convert(s), detector, increasing);
982                 }
983 
984                 /** {@inheritDoc} */
985                 @Override
986                 public ODEState resetState(final ODEEventDetector d, final ODEStateAndDerivative s) {
987 
988                     final SpacecraftState oldState = convert(s);
989                     final SpacecraftState newState = handler.resetState(detector, oldState);
990                     stateChanged(newState);
991 
992                     // main part
993                     final double[] primary    = new double[s.getPrimaryStateDimension()];
994                     stateMapper.mapStateToArray(newState, primary, null);
995 
996                     // secondary part
997                     final double[][] secondary = new double[1][secondaryOffsets.get(SECONDARY_DIMENSION)];
998                     for (final AdditionalDerivativesProvider provider : additionalDerivativesProviders) {
999                         final String name      = provider.getName();
1000                         final int    offset    = secondaryOffsets.get(name);
1001                         final int    dimension = provider.getDimension();
1002                         System.arraycopy(newState.getAdditionalState(name), 0, secondary[0], offset, dimension);
1003                     }
1004 
1005                     return new ODEState(newState.getDate().durationFrom(getStartDate()),
1006                                         primary, secondary);
1007 
1008                 }
1009 
1010             };
1011         }
1012 
1013     }
1014 
1015     /** Adapt an {@link org.orekit.propagation.sampling.OrekitStepHandler}
1016      * to Hipparchus {@link ODEStepHandler} interface.
1017      * @author Luc Maisonobe
1018      */
1019     private class AdaptedStepHandler implements ODEStepHandler {
1020 
1021         /** Underlying handler. */
1022         private final OrekitStepHandler handler;
1023 
1024         /** Build an instance.
1025          * @param handler underlying handler to wrap
1026          */
1027         AdaptedStepHandler(final OrekitStepHandler handler) {
1028             this.handler = handler;
1029         }
1030 
1031         /** {@inheritDoc} */
1032         @Override
1033         public void init(final ODEStateAndDerivative s0, final double t) {
1034             handler.init(convert(s0), stateMapper.mapDoubleToDate(t));
1035         }
1036 
1037         /** {@inheritDoc} */
1038         @Override
1039         public void handleStep(final ODEStateInterpolator interpolator) {
1040             handler.handleStep(new AdaptedStepInterpolator(interpolator));
1041         }
1042 
1043         /** {@inheritDoc} */
1044         @Override
1045         public void finish(final ODEStateAndDerivative finalState) {
1046             handler.finish(convert(finalState));
1047         }
1048 
1049     }
1050 
1051     /** Adapt an Hipparchus {@link ODEStateInterpolator}
1052      * to an orekit {@link OrekitStepInterpolator} interface.
1053      * @author Luc Maisonobe
1054      */
1055     private class AdaptedStepInterpolator implements OrekitStepInterpolator {
1056 
1057         /** Underlying raw rawInterpolator. */
1058         private final ODEStateInterpolator mathInterpolator;
1059 
1060         /** Simple constructor.
1061          * @param mathInterpolator underlying raw interpolator
1062          */
1063         AdaptedStepInterpolator(final ODEStateInterpolator mathInterpolator) {
1064             this.mathInterpolator = mathInterpolator;
1065         }
1066 
1067         /** {@inheritDoc}} */
1068         @Override
1069         public SpacecraftState getPreviousState() {
1070             return convert(mathInterpolator.getPreviousState());
1071         }
1072 
1073         /** {@inheritDoc}} */
1074         @Override
1075         public boolean isPreviousStateInterpolated() {
1076             return mathInterpolator.isPreviousStateInterpolated();
1077         }
1078 
1079         /** {@inheritDoc}} */
1080         @Override
1081         public SpacecraftState getCurrentState() {
1082             return convert(mathInterpolator.getCurrentState());
1083         }
1084 
1085         /** {@inheritDoc}} */
1086         @Override
1087         public boolean isCurrentStateInterpolated() {
1088             return mathInterpolator.isCurrentStateInterpolated();
1089         }
1090 
1091         /** {@inheritDoc}} */
1092         @Override
1093         public SpacecraftState getInterpolatedState(final AbsoluteDate date) {
1094             return convert(mathInterpolator.getInterpolatedState(date.durationFrom(stateMapper.getReferenceDate())));
1095         }
1096 
1097         /** {@inheritDoc}} */
1098         @Override
1099         public boolean isForward() {
1100             return mathInterpolator.isForward();
1101         }
1102 
1103         /** {@inheritDoc}} */
1104         @Override
1105         public AdaptedStepInterpolator restrictStep(final SpacecraftState newPreviousState,
1106                                                     final SpacecraftState newCurrentState) {
1107             try {
1108                 final AbstractODEStateInterpolator aosi = (AbstractODEStateInterpolator) mathInterpolator;
1109                 return new AdaptedStepInterpolator(aosi.restrictStep(convert(newPreviousState),
1110                                                                      convert(newCurrentState)));
1111             } catch (ClassCastException cce) {
1112                 // this should never happen
1113                 throw new OrekitInternalError(cce);
1114             }
1115         }
1116 
1117     }
1118 
1119     /** Specialized step handler storing interpolators for ephemeris generation.
1120      * @since 11.0
1121      */
1122     private class StoringStepHandler implements ODEStepHandler, EphemerisGenerator {
1123 
1124         /** Underlying raw mathematical model. */
1125         private DenseOutputModel model;
1126 
1127         /** the user supplied end date. Propagation may not end on this date. */
1128         private AbsoluteDate endDate;
1129 
1130         /** Generated ephemeris. */
1131         private BoundedPropagator ephemeris;
1132 
1133         /** Last interpolator handled by the object.*/
1134         private  ODEStateInterpolator lastInterpolator;
1135 
1136         /** Set the end date.
1137          * @param endDate end date
1138          */
1139         public void setEndDate(final AbsoluteDate endDate) {
1140             this.endDate = endDate;
1141         }
1142 
1143         /** {@inheritDoc} */
1144         @Override
1145         public void init(final ODEStateAndDerivative s0, final double t) {
1146 
1147             this.model = new DenseOutputModel();
1148             model.init(s0, t);
1149 
1150             // ephemeris will be generated when last step is processed
1151             this.ephemeris = null;
1152 
1153             this.lastInterpolator = null;
1154 
1155         }
1156 
1157         /** {@inheritDoc} */
1158         @Override
1159         public BoundedPropagator getGeneratedEphemeris() {
1160             // Each time we try to get the ephemeris, rebuild it using the last data.
1161             buildEphemeris();
1162             return ephemeris;
1163         }
1164 
1165         /** {@inheritDoc} */
1166         @Override
1167         public void handleStep(final ODEStateInterpolator interpolator) {
1168             model.handleStep(interpolator);
1169             lastInterpolator = interpolator;
1170         }
1171 
1172         /** {@inheritDoc} */
1173         @Override
1174         public void finish(final ODEStateAndDerivative finalState) {
1175             buildEphemeris();
1176         }
1177 
1178         /** Method used to produce ephemeris at a given time.
1179          * Can be used at multiple times, updating the ephemeris to
1180          * its last state.
1181          */
1182         private void buildEphemeris() {
1183             // buildEphemeris was built in order to allow access to what was previously the finish method.
1184             // This now allows to call it through getGeneratedEphemeris, therefore through an external call,
1185             // which was not previously the case.
1186 
1187             // Update the model's finalTime with the last interpolator.
1188             model.finish(lastInterpolator.getCurrentState());
1189 
1190             // set up the boundary dates
1191             final double tI = model.getInitialTime();
1192             final double tF = model.getFinalTime();
1193             // tI is almost? always zero
1194             final AbsoluteDate startDate =
1195                             stateMapper.mapDoubleToDate(tI);
1196             final AbsoluteDate finalDate =
1197                             stateMapper.mapDoubleToDate(tF, this.endDate);
1198             final AbsoluteDate minDate;
1199             final AbsoluteDate maxDate;
1200             if (tF < tI) {
1201                 minDate = finalDate;
1202                 maxDate = startDate;
1203             } else {
1204                 minDate = startDate;
1205                 maxDate = finalDate;
1206             }
1207 
1208             // get the initial additional states that are not managed
1209             final DoubleArrayDictionary unmanaged = new DoubleArrayDictionary();
1210             for (final DoubleArrayDictionary.Entry initial : getInitialState().getAdditionalStatesValues().getData()) {
1211                 if (!isAdditionalStateManaged(initial.getKey())) {
1212                     // this additional state was in the initial state, but is unknown to the propagator
1213                     // we simply copy its initial value as is
1214                     unmanaged.put(initial.getKey(), initial.getValue());
1215                 }
1216             }
1217 
1218             // get the names of additional states managed by differential equations
1219             final String[] names      = new String[additionalDerivativesProviders.size()];
1220             final int[]    dimensions = new int[additionalDerivativesProviders.size()];
1221             for (int i = 0; i < names.length; ++i) {
1222                 names[i] = additionalDerivativesProviders.get(i).getName();
1223                 dimensions[i] = additionalDerivativesProviders.get(i).getDimension();
1224             }
1225 
1226             // create the ephemeris
1227             ephemeris = new IntegratedEphemeris(startDate, minDate, maxDate,
1228                                                 stateMapper, propagationType, model,
1229                                                 unmanaged, getAdditionalStateProviders(),
1230                                                 names, dimensions);
1231 
1232         }
1233 
1234     }
1235 
1236     /** Wrapper for resetting an integrator handlers.
1237      * <p>
1238      * This class is intended to be used in a try-with-resource statement.
1239      * If propagator-specific event handlers and step handlers are added to
1240      * the integrator in the try block, they will be removed automatically
1241      * when leaving the block, so the integrator only keeps its own handlers
1242      * between calls to {@link AbstractIntegratedPropagator#propagate(AbsoluteDate, AbsoluteDate).
1243      * </p>
1244      * @since 11.0
1245      */
1246     private static class IntegratorResetter implements AutoCloseable {
1247 
1248         /** Wrapped integrator. */
1249         private final ODEIntegrator integrator;
1250 
1251         /** Initial event detectors list. */
1252         private final List<ODEEventDetector> detectors;
1253 
1254         /** Initial step handlers list. */
1255         private final List<ODEStepHandler> stepHandlers;
1256 
1257         /** Simple constructor.
1258          * @param integrator wrapped integrator
1259          */
1260         IntegratorResetter(final ODEIntegrator integrator) {
1261             this.integrator   = integrator;
1262             this.detectors    = new ArrayList<>(integrator.getEventDetectors());
1263             this.stepHandlers = new ArrayList<>(integrator.getStepHandlers());
1264         }
1265 
1266         /** {@inheritDoc}
1267          * <p>
1268          * Reset event handlers and step handlers back to the initial list
1269          * </p>
1270          */
1271         @Override
1272         public void close() {
1273 
1274             // reset event handlers
1275             integrator.clearEventDetectors();
1276             detectors.forEach(integrator::addEventDetector);
1277 
1278             // reset step handlers
1279             integrator.clearStepHandlers();
1280             stepHandlers.forEach(integrator::addStepHandler);
1281 
1282         }
1283 
1284     }
1285 
1286 }