IntegratedEphemeris.java

  1. /* Copyright 2002-2020 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.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.frames.Frame;
  25. import org.orekit.orbits.Orbit;
  26. import org.orekit.propagation.AdditionalStateProvider;
  27. import org.orekit.propagation.BoundedPropagator;
  28. import org.orekit.propagation.PropagationType;
  29. import org.orekit.propagation.SpacecraftState;
  30. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.TimeStampedPVCoordinates;

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

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

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

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

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

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

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

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

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

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

  104.         super(mapper.getAttitudeProvider());

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

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

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

  120.     }

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

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

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

  135.     }

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

  148.     /** {@inheritDoc} */
  149.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  150.         return basicPropagate(date).getOrbit();
  151.     }

  152.     /** {@inheritDoc} */
  153.     protected double getMass(final AbsoluteDate date) {
  154.         return basicPropagate(date).getMass();
  155.     }

  156.     /** {@inheritDoc} */
  157.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
  158.         return propagate(date).getPVCoordinates(frame);
  159.     }

  160.     /** Get the first date of the range.
  161.      * @return the first date of the range
  162.      */
  163.     public AbsoluteDate getMinDate() {
  164.         return minDate;
  165.     }

  166.     /** Get the last date of the range.
  167.      * @return the last date of the range
  168.      */
  169.     public AbsoluteDate getMaxDate() {
  170.         return maxDate;
  171.     }

  172.     @Override
  173.     public Frame getFrame() {
  174.         return this.mapper.getFrame();
  175.     }

  176.     /** {@inheritDoc} */
  177.     public void resetInitialState(final SpacecraftState state) {
  178.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  179.     }

  180.     /** {@inheritDoc} */
  181.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  182.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  183.     }

  184.     /** {@inheritDoc} */
  185.     public SpacecraftState getInitialState() {
  186.         return updateAdditionalStates(basicPropagate(getMinDate()));
  187.     }

  188.     /** Local provider for additional state data. */
  189.     private class LocalProvider implements AdditionalStateProvider {

  190.         /** Name of the additional state. */
  191.         private final String name;

  192.         /** Index of the additional state. */
  193.         private final int index;

  194.         /** Simple constructor.
  195.          * @param name name of the additional state
  196.          * @param index index of the additional state
  197.          */
  198.         LocalProvider(final String name, final int index) {
  199.             this.name  = name;
  200.             this.index = index;
  201.         }

  202.         /** {@inheritDoc} */
  203.         public String getName() {
  204.             return name;
  205.         }

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

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

  210.         }

  211.     }

  212. }