IntegratedEphemeris.java

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

  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.TimeStampedPVCoordinates;

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

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

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

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

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

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

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

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

  85.     /** Unmanaged additional states that must be simply copied. */
  86.     private final Map<String, double[]> unmanaged;

  87.     /** Creates a new instance of IntegratedEphemeris.
  88.      * @param startDate Start date of the integration (can be minDate or maxDate)
  89.      * @param minDate first date of the range
  90.      * @param maxDate last date of the range
  91.      * @param mapper mapper between raw double components and spacecraft state
  92.      * @param type type of orbit to output (mean or osculating)
  93.      * @param model underlying raw mathematical model
  94.      * @param unmanaged unmanaged additional states that must be simply copied
  95.      * @param providers providers for pre-integrated states
  96.      * @param equations names of additional equations
  97.      */
  98.     public IntegratedEphemeris(final AbsoluteDate startDate,
  99.                                final AbsoluteDate minDate, final AbsoluteDate maxDate,
  100.                                final StateMapper mapper, final PropagationType type,
  101.                                final DenseOutputModel model,
  102.                                final Map<String, double[]> unmanaged,
  103.                                final List<AdditionalStateProvider> providers,
  104.                                final String[] equations) {

  105.         super(mapper.getAttitudeProvider());

  106.         this.startDate = startDate;
  107.         this.minDate   = minDate;
  108.         this.maxDate   = maxDate;
  109.         this.mapper    = mapper;
  110.         this.type      = type;
  111.         this.model     = model;
  112.         this.unmanaged = unmanaged;

  113.         // set up the pre-integrated providers
  114.         for (final AdditionalStateProvider provider : providers) {
  115.             addAdditionalStateProvider(provider);
  116.         }

  117.         // set up providers to map the final elements of the model array to additional states
  118.         for (int i = 0; i < equations.length; ++i) {
  119.             addAdditionalStateProvider(new LocalProvider(equations[i], i));
  120.         }

  121.     }

  122.     /** Interpolate the model at some date.
  123.      * @param date desired interpolation date
  124.      * @return state interpolated at date
  125.      */
  126.     private ODEStateAndDerivative getInterpolatedState(final AbsoluteDate date) {

  127.         // compare using double precision instead of AbsoluteDate.compareTo(...)
  128.         // because time is expressed as a double when searching for events
  129.         if (date.compareTo(minDate.shiftedBy(-EXTRAPOLATION_TOLERANCE)) < 0) {
  130.             // date is outside of supported range
  131.             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_BEFORE,
  132.                     date, minDate, maxDate, minDate.durationFrom(date));
  133.         }
  134.         if (date.compareTo(maxDate.shiftedBy(EXTRAPOLATION_TOLERANCE)) > 0) {
  135.             // date is outside of supported range
  136.             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_AFTER,
  137.                     date, minDate, maxDate, date.durationFrom(maxDate));
  138.         }

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

  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {
  144.         final ODEStateAndDerivative os = getInterpolatedState(date);
  145.         SpacecraftState state = mapper.mapArrayToState(mapper.mapDoubleToDate(os.getTime(), date),
  146.                                                        os.getPrimaryState(), os.getPrimaryDerivative(),
  147.                                                        type);
  148.         for (Map.Entry<String, double[]> initial : unmanaged.entrySet()) {
  149.             state = state.addAdditionalState(initial.getKey(), initial.getValue());
  150.         }
  151.         return state;
  152.     }

  153.     /** {@inheritDoc} */
  154.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  155.         return basicPropagate(date).getOrbit();
  156.     }

  157.     /** {@inheritDoc} */
  158.     protected double getMass(final AbsoluteDate date) {
  159.         return basicPropagate(date).getMass();
  160.     }

  161.     /** {@inheritDoc} */
  162.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
  163.         return propagate(date).getPVCoordinates(frame);
  164.     }

  165.     /** Get the first date of the range.
  166.      * @return the first date of the range
  167.      */
  168.     public AbsoluteDate getMinDate() {
  169.         return minDate;
  170.     }

  171.     /** Get the last date of the range.
  172.      * @return the last date of the range
  173.      */
  174.     public AbsoluteDate getMaxDate() {
  175.         return maxDate;
  176.     }

  177.     @Override
  178.     public Frame getFrame() {
  179.         return this.mapper.getFrame();
  180.     }

  181.     /** {@inheritDoc} */
  182.     public void resetInitialState(final SpacecraftState state) {
  183.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  184.     }

  185.     /** {@inheritDoc} */
  186.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  187.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  188.     }

  189.     /** {@inheritDoc} */
  190.     @Override
  191.     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
  192.         super.setAttitudeProvider(attitudeProvider);
  193.         if (mapper != null) {
  194.             // At the construction, the mapper is not set yet
  195.             // However, if the attitude provider is changed afterwards, it must be changed in the mapper too
  196.             mapper.setAttitudeProvider(attitudeProvider);
  197.         }
  198.     }

  199.     /** {@inheritDoc} */
  200.     public SpacecraftState getInitialState() {
  201.         return updateAdditionalStates(basicPropagate(getMinDate()));
  202.     }

  203.     /** Local provider for additional state data. */
  204.     private class LocalProvider implements AdditionalStateProvider {

  205.         /** Name of the additional state. */
  206.         private final String name;

  207.         /** Index of the additional state. */
  208.         private final int index;

  209.         /** Simple constructor.
  210.          * @param name name of the additional state
  211.          * @param index index of the additional state
  212.          */
  213.         LocalProvider(final String name, final int index) {
  214.             this.name  = name;
  215.             this.index = index;
  216.         }

  217.         /** {@inheritDoc} */
  218.         public String getName() {
  219.             return name;
  220.         }

  221.         /** {@inheritDoc} */
  222.         public double[] getAdditionalState(final SpacecraftState state) {

  223.             // extract the part of the interpolated array corresponding to the additional state
  224.             return getInterpolatedState(state.getDate()).getSecondaryState(index + 1);

  225.         }

  226.     }

  227. }