IntegratedEphemeris.java

  1. /* Copyright 2002-2023 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. import java.util.Arrays;
  19. import java.util.List;

  20. import org.hipparchus.ode.DenseOutputModel;
  21. import org.hipparchus.ode.ODEStateAndDerivative;
  22. import org.orekit.attitudes.AttitudeProvider;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.orbits.Orbit;
  27. import org.orekit.propagation.AdditionalStateProvider;
  28. import org.orekit.propagation.BoundedPropagator;
  29. import org.orekit.propagation.PropagationType;
  30. import org.orekit.propagation.SpacecraftState;
  31. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.DoubleArrayDictionary;
  34. import org.orekit.utils.TimeStampedPVCoordinates;

  35. /** This class stores sequentially generated orbital parameters for
  36.  * later retrieval.
  37.  *
  38.  * <p>
  39.  * Instances of this class are built automatically when the {@link
  40.  * org.orekit.propagation.Propagator#getEphemerisGenerator()
  41.  * getEphemerisGenerator} method has been called. They are created when propagation is over.
  42.  * Random access to any intermediate state of the orbit throughout the propagation range is
  43.  * possible afterwards through this object.
  44.  * </p>
  45.  * <p>
  46.  * A typical use case is for numerically integrated orbits, which can be used by
  47.  * algorithms that need to wander around according to their own algorithm without
  48.  * cumbersome tight links with the integrator.
  49.  * </p>
  50.  * <p>
  51.  * As this class implements the {@link org.orekit.propagation.Propagator Propagator}
  52.  * interface, it can itself be used in batch mode to build another instance of the
  53.  * same type. This is however not recommended since it would be a waste of resources.
  54.  * </p>
  55.  * <p>
  56.  * Note that this class stores all intermediate states along with interpolation
  57.  * models, so it may be memory intensive.
  58.  * </p>
  59.  *
  60.  * @see org.orekit.propagation.numerical.NumericalPropagator
  61.  * @author Mathieu Rom&eacute;ro
  62.  * @author Luc Maisonobe
  63.  * @author V&eacute;ronique Pommier-Maurussane
  64.  */
  65. public class IntegratedEphemeris
  66.     extends AbstractAnalyticalPropagator implements BoundedPropagator {

  67.     /** Event detection requires evaluating the state slightly before / past an event. */
  68.     private static final double EXTRAPOLATION_TOLERANCE = 1.0;

  69.     /** Mapper between raw double components and spacecraft state. */
  70.     private final StateMapper mapper;

  71.     /** Type of orbit to output (mean or osculating).
  72.      * <p>
  73.      * This is used only in the case of semianalitical propagators where there is a clear separation between
  74.      * mean and short periodic elements. It is ignored by the Numerical propagator.
  75.      * </p>
  76.      */
  77.     private PropagationType type;

  78.     /** Start date of the integration (can be min or max). */
  79.     private final AbsoluteDate startDate;

  80.     /** First date of the range. */
  81.     private final AbsoluteDate minDate;

  82.     /** Last date of the range. */
  83.     private final AbsoluteDate maxDate;

  84.     /** Underlying raw mathematical model. */
  85.     private DenseOutputModel model;

  86.     /** Unmanaged additional states that must be simply copied. */
  87.     private final DoubleArrayDictionary unmanaged;

  88.     /** Names of additional equations.
  89.      * @since 11.2
  90.      */
  91.     private final String[] equations;

  92.     /** Dimensions of additional equations.
  93.      * @since 11.2
  94.      */
  95.     private final int[] dimensions;

  96.     /** Creates a new instance of IntegratedEphemeris.
  97.      * @param startDate Start date of the integration (can be minDate or maxDate)
  98.      * @param minDate first date of the range
  99.      * @param maxDate last date of the range
  100.      * @param mapper mapper between raw double components and spacecraft state
  101.      * @param type type of orbit to output (mean or osculating)
  102.      * @param model underlying raw mathematical model
  103.      * @param unmanaged unmanaged additional states that must be simply copied
  104.      * @param providers providers for pre-integrated states
  105.      * @param equations names of additional equations
  106.      * @param dimensions dimensions of additional equations
  107.      * @since 11.1.2
  108.      */
  109.     public IntegratedEphemeris(final AbsoluteDate startDate,
  110.                                final AbsoluteDate minDate, final AbsoluteDate maxDate,
  111.                                final StateMapper mapper, final PropagationType type,
  112.                                final DenseOutputModel model,
  113.                                final DoubleArrayDictionary unmanaged,
  114.                                final List<AdditionalStateProvider> providers,
  115.                                final String[] equations, final int[] dimensions) {

  116.         super(mapper.getAttitudeProvider());

  117.         this.startDate = startDate;
  118.         this.minDate   = minDate;
  119.         this.maxDate   = maxDate;
  120.         this.mapper    = mapper;
  121.         this.type      = type;
  122.         this.model     = model;
  123.         this.unmanaged = unmanaged;

  124.         // set up the pre-integrated providers
  125.         for (final AdditionalStateProvider provider : providers) {
  126.             addAdditionalStateProvider(provider);
  127.         }

  128.         this.equations  = equations.clone();
  129.         this.dimensions = dimensions.clone();

  130.         // set up initial state
  131.         super.resetInitialState(getInitialState());

  132.     }

  133.     /** Interpolate the model at some date.
  134.      * @param date desired interpolation date
  135.      * @return state interpolated at date
  136.      */
  137.     private ODEStateAndDerivative getInterpolatedState(final AbsoluteDate date) {

  138.         // compare using double precision instead of AbsoluteDate.compareTo(...)
  139.         // because time is expressed as a double when searching for events
  140.         if (date.compareTo(minDate.shiftedBy(-EXTRAPOLATION_TOLERANCE)) < 0) {
  141.             // date is outside of supported range
  142.             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_BEFORE,
  143.                     date, minDate, maxDate, minDate.durationFrom(date));
  144.         }
  145.         if (date.compareTo(maxDate.shiftedBy(EXTRAPOLATION_TOLERANCE)) > 0) {
  146.             // date is outside of supported range
  147.             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_AFTER,
  148.                     date, minDate, maxDate, date.durationFrom(maxDate));
  149.         }

  150.         return model.getInterpolatedState(date.durationFrom(startDate));

  151.     }

  152.     /** {@inheritDoc} */
  153.     @Override
  154.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {
  155.         final ODEStateAndDerivative os = getInterpolatedState(date);
  156.         SpacecraftState state = mapper.mapArrayToState(mapper.mapDoubleToDate(os.getTime(), date),
  157.                                                        os.getPrimaryState(), os.getPrimaryDerivative(),
  158.                                                        type);
  159.         for (DoubleArrayDictionary.Entry initial : unmanaged.getData()) {
  160.             state = state.addAdditionalState(initial.getKey(), initial.getValue());
  161.         }
  162.         return state;
  163.     }

  164.     /** {@inheritDoc} */
  165.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  166.         return basicPropagate(date).getOrbit();
  167.     }

  168.     /** {@inheritDoc} */
  169.     protected double getMass(final AbsoluteDate date) {
  170.         return basicPropagate(date).getMass();
  171.     }

  172.     /** {@inheritDoc} */
  173.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
  174.         return propagate(date).getPVCoordinates(frame);
  175.     }

  176.     /** Get the first date of the range.
  177.      * @return the first date of the range
  178.      */
  179.     public AbsoluteDate getMinDate() {
  180.         return minDate;
  181.     }

  182.     /** Get the last date of the range.
  183.      * @return the last date of the range
  184.      */
  185.     public AbsoluteDate getMaxDate() {
  186.         return maxDate;
  187.     }

  188.     @Override
  189.     public Frame getFrame() {
  190.         return this.mapper.getFrame();
  191.     }

  192.     /** {@inheritDoc} */
  193.     public void resetInitialState(final SpacecraftState state) {
  194.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  195.     }

  196.     /** {@inheritDoc} */
  197.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  198.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  199.     }

  200.     /** {@inheritDoc} */
  201.     @Override
  202.     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
  203.         super.setAttitudeProvider(attitudeProvider);
  204.         if (mapper != null) {
  205.             // At the construction, the mapper is not set yet
  206.             // However, if the attitude provider is changed afterwards, it must be changed in the mapper too
  207.             mapper.setAttitudeProvider(attitudeProvider);
  208.         }
  209.     }

  210.     /** {@inheritDoc} */
  211.     public SpacecraftState getInitialState() {
  212.         return updateAdditionalStates(basicPropagate(getMinDate()));
  213.     }

  214.     /** {@inheritDoc} */
  215.     @Override
  216.     protected SpacecraftState updateAdditionalStates(final SpacecraftState original) {

  217.         SpacecraftState updated = super.updateAdditionalStates(original);

  218.         if (equations.length > 0) {
  219.             final ODEStateAndDerivative osd                = getInterpolatedState(updated.getDate());
  220.             final double[]              combinedState      = osd.getSecondaryState(1);
  221.             final double[]              combinedDerivative = osd.getSecondaryDerivative(1);
  222.             int index = 0;
  223.             for (int i = 0; i < equations.length; ++i) {
  224.                 final double[] state      = Arrays.copyOfRange(combinedState,      index, index + dimensions[i]);
  225.                 final double[] derivative = Arrays.copyOfRange(combinedDerivative, index, index + dimensions[i]);
  226.                 updated = updated.
  227.                           addAdditionalState(equations[i], state).
  228.                           addAdditionalStateDerivative(equations[i], derivative);
  229.                 index += dimensions[i];
  230.             }
  231.         }

  232.         return updated;

  233.     }

  234. }