IntegratedEphemeris.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.io.NotSerializableException;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.hipparchus.ode.DenseOutputModel;
  26. import org.hipparchus.ode.ODEStateAndDerivative;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitInternalError;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.frames.Frame;
  31. import org.orekit.orbits.Orbit;
  32. import org.orekit.propagation.AdditionalStateProvider;
  33. import org.orekit.propagation.BoundedPropagator;
  34. import org.orekit.propagation.SpacecraftState;
  35. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  36. import org.orekit.time.AbsoluteDate;
  37. import org.orekit.utils.TimeStampedPVCoordinates;

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

  74.     /** Serializable UID. */
  75.     private static final long serialVersionUID = 20140213L;

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

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

  80.     /** Output only the mean orbit.
  81.      * <p>
  82.      * This is used only in the case of semianalitical propagators where there is a clear separation between
  83.      * mean and short periodic elements. It is ignored by the Numerical propagator.
  84.      * </p>
  85.      */
  86.     private boolean meanOrbit;

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

  89.     /** First date of the range. */
  90.     private final AbsoluteDate minDate;

  91.     /** Last date of the range. */
  92.     private final AbsoluteDate maxDate;

  93.     /** Underlying raw mathematical model. */
  94.     private DenseOutputModel model;

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

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

  115.         super(mapper.getAttitudeProvider());

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

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

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

  131.     }

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

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

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

  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {
  150.         final ODEStateAndDerivative os = getInterpolatedState(date);
  151.         SpacecraftState state = mapper.mapArrayToState(mapper.mapDoubleToDate(os.getTime(), date),
  152.                                                        os.getPrimaryState(), os.getPrimaryDerivative(),
  153.                                                        meanOrbit);
  154.         for (Map.Entry<String, double[]> initial : unmanaged.entrySet()) {
  155.             state = state.addAdditionalState(initial.getKey(), initial.getValue());
  156.         }
  157.         return state;
  158.     }

  159.     /** {@inheritDoc} */
  160.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  161.         return basicPropagate(date).getOrbit();
  162.     }

  163.     /** {@inheritDoc} */
  164.     protected double getMass(final AbsoluteDate date) {
  165.         return basicPropagate(date).getMass();
  166.     }

  167.     /** {@inheritDoc} */
  168.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
  169.         return propagate(date).getPVCoordinates(frame);
  170.     }

  171.     /** Get the first date of the range.
  172.      * @return the first date of the range
  173.      */
  174.     public AbsoluteDate getMinDate() {
  175.         return minDate;
  176.     }

  177.     /** Get the last date of the range.
  178.      * @return the last date of the range
  179.      */
  180.     public AbsoluteDate getMaxDate() {
  181.         return maxDate;
  182.     }

  183.     @Override
  184.     public Frame getFrame() {
  185.         return this.mapper.getFrame();
  186.     }

  187.     /** {@inheritDoc} */
  188.     public void resetInitialState(final SpacecraftState state) {
  189.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  190.     }

  191.     /** {@inheritDoc} */
  192.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  193.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  194.     }

  195.     /** {@inheritDoc} */
  196.     public SpacecraftState getInitialState() {
  197.         return updateAdditionalStates(basicPropagate(getMinDate()));
  198.     }

  199.     /** Replace the instance with a data transfer object for serialization.
  200.      * @return data transfer object that will be serialized
  201.      * @exception NotSerializableException if the state mapper cannot be serialized (typically for DSST propagator)
  202.      */
  203.     private Object writeReplace() throws NotSerializableException {

  204.         // unmanaged additional states
  205.         final String[]   unmanagedNames  = new String[unmanaged.size()];
  206.         final double[][] unmanagedValues = new double[unmanaged.size()][];
  207.         int i = 0;
  208.         for (Map.Entry<String, double[]> entry : unmanaged.entrySet()) {
  209.             unmanagedNames[i]  = entry.getKey();
  210.             unmanagedValues[i] = entry.getValue();
  211.             ++i;
  212.         }

  213.         // managed states providers
  214.         final List<AdditionalStateProvider> serializableProviders = new ArrayList<AdditionalStateProvider>();
  215.         final List<String> equationNames = new ArrayList<String>();
  216.         for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  217.             if (provider instanceof LocalProvider) {
  218.                 equationNames.add(((LocalProvider) provider).getName());
  219.             } else if (provider instanceof Serializable) {
  220.                 serializableProviders.add(provider);
  221.             }
  222.         }

  223.         return new DataTransferObject(startDate, minDate, maxDate, mapper, meanOrbit, model,
  224.                                       unmanagedNames, unmanagedValues,
  225.                                       serializableProviders.toArray(new AdditionalStateProvider[serializableProviders.size()]),
  226.                                       equationNames.toArray(new String[equationNames.size()]));

  227.     }

  228.     /** Local provider for additional state data. */
  229.     private class LocalProvider implements AdditionalStateProvider {

  230.         /** Name of the additional state. */
  231.         private final String name;

  232.         /** Index of the additional state. */
  233.         private final int index;

  234.         /** Simple constructor.
  235.          * @param name name of the additional state
  236.          * @param index index of the additional state
  237.          */
  238.         LocalProvider(final String name, final int index) {
  239.             this.name  = name;
  240.             this.index = index;
  241.         }

  242.         /** {@inheritDoc} */
  243.         public String getName() {
  244.             return name;
  245.         }

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

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

  250.         }

  251.     }

  252.     /** Internal class used only for serialization. */
  253.     private static class DataTransferObject implements Serializable {

  254.         /** Serializable UID. */
  255.         private static final long serialVersionUID = 20140213L;

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

  258.         /** Indicator for mean orbit output. */
  259.         private final boolean meanOrbit;

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

  262.         /** First date of the range. */
  263.         private final AbsoluteDate minDate;

  264.         /** Last date of the range. */
  265.         private final AbsoluteDate maxDate;

  266.         /** Underlying raw mathematical model. */
  267.         private final DenseOutputModel model;

  268.         /** Names of unmanaged additional states that must be simply copied. */
  269.         private final String[] unmanagedNames;

  270.         /** Values of unmanaged additional states that must be simply copied. */
  271.         private final double[][] unmanagedValues;

  272.         /** Names of additional equations. */
  273.         private final String[] equations;

  274.         /** Providers for pre-integrated states. */
  275.         private final AdditionalStateProvider[] providers;

  276.         /** Simple constructor.
  277.          * @param startDate Start date of the integration (can be minDate or maxDate)
  278.          * @param minDate first date of the range
  279.          * @param maxDate last date of the range
  280.          * @param mapper mapper between raw double components and spacecraft state
  281.          * @param meanOrbit output only the mean orbit.
  282.          * @param model underlying raw mathematical model
  283.          * @param unmanagedNames names of unmanaged additional states that must be simply copied
  284.          * @param unmanagedValues values of unmanaged additional states that must be simply copied
  285.          * @param providers providers for pre-integrated states
  286.          * @param equations names of additional equations
  287.          */
  288.         DataTransferObject(final AbsoluteDate startDate,
  289.                                   final AbsoluteDate minDate, final AbsoluteDate maxDate,
  290.                                   final StateMapper mapper, final boolean meanOrbit,
  291.                                   final DenseOutputModel model,
  292.                                   final String[] unmanagedNames, final double[][] unmanagedValues,
  293.                                   final AdditionalStateProvider[] providers,
  294.                                   final String[] equations) {
  295.             this.startDate       = startDate;
  296.             this.minDate         = minDate;
  297.             this.maxDate         = maxDate;
  298.             this.mapper          = mapper;
  299.             this.meanOrbit       = meanOrbit;
  300.             this.model           = model;
  301.             this.unmanagedNames  = unmanagedNames;
  302.             this.unmanagedValues = unmanagedValues;
  303.             this.providers       = providers;
  304.             this.equations       = equations;
  305.         }

  306.         /** Replace the deserialized data transfer object with a {@link IntegratedEphemeris}.
  307.          * @return replacement {@link IntegratedEphemeris}
  308.          */
  309.         private Object readResolve() {
  310.             try {
  311.                 final Map<String, double[]> unmanaged = new HashMap<String, double[]>(unmanagedNames.length);
  312.                 for (int i = 0; i < unmanagedNames.length; ++i) {
  313.                     unmanaged.put(unmanagedNames[i], unmanagedValues[i]);
  314.                 }
  315.                 return new IntegratedEphemeris(startDate, minDate, maxDate, mapper, meanOrbit, model,
  316.                                                unmanaged, Arrays.asList(providers), equations);
  317.             } catch (OrekitException oe) {
  318.                 throw new OrekitInternalError(oe);
  319.             }
  320.         }

  321.     }

  322. }