IntegratedEphemeris.java

  1. /* Copyright 2002-2016 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.apache.commons.math3.ode.ContinuousOutputModel;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitExceptionWrapper;
  28. import org.orekit.errors.OrekitInternalError;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.errors.PropagationException;
  31. import org.orekit.frames.Frame;
  32. import org.orekit.orbits.Orbit;
  33. import org.orekit.propagation.AdditionalStateProvider;
  34. import org.orekit.propagation.BoundedPropagator;
  35. import org.orekit.propagation.SpacecraftState;
  36. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  37. import org.orekit.time.AbsoluteDate;
  38. import org.orekit.utils.TimeStampedPVCoordinates;

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

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

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

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

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

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

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

  92.     /** Underlying raw mathematical model. */
  93.     private ContinuousOutputModel model;

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

  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 meanOrbit output only the mean orbit
  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.      * @exception OrekitException if several providers have the same name
  107.      */
  108.     public IntegratedEphemeris(final AbsoluteDate startDate,
  109.                                final AbsoluteDate minDate, final AbsoluteDate maxDate,
  110.                                final StateMapper mapper, final boolean meanOrbit,
  111.                                final ContinuousOutputModel model,
  112.                                final Map<String, double[]> unmanaged,
  113.                                final List<AdditionalStateProvider> providers,
  114.                                final String[] equations)
  115.         throws OrekitException {

  116.         super(mapper.getAttitudeProvider());

  117.         this.startDate = startDate;
  118.         this.minDate   = minDate;
  119.         this.maxDate   = maxDate;
  120.         this.mapper    = mapper;
  121.         this.meanOrbit = meanOrbit;
  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.         // set up providers to map the final elements of the model array to additional states
  129.         for (int i = 0; i < equations.length; ++i) {
  130.             addAdditionalStateProvider(new LocalProvider(equations[i], i));
  131.         }

  132.     }

  133.     /** Set up the model at some interpolation date.
  134.      * @param date desired interpolation date
  135.      * @exception PropagationException if specified date is outside
  136.      * of supported range
  137.      */
  138.     private void setInterpolationDate(final AbsoluteDate date)
  139.         throws PropagationException {

  140.         if (date.equals(startDate.shiftedBy(model.getInterpolatedTime()))) {
  141.             // the current model date is already the desired one
  142.             return;
  143.         }

  144.         if ((date.compareTo(minDate) < 0) || (date.compareTo(maxDate) > 0)) {
  145.             // date is outside of supported range
  146.             throw new PropagationException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE,
  147.                                            date, minDate, maxDate);
  148.         }

  149.         // reset interpolation model to the desired date
  150.         model.setInterpolatedTime(date.durationFrom(startDate));

  151.     }

  152.     /** {@inheritDoc} */
  153.     @Override
  154.     protected SpacecraftState basicPropagate(final AbsoluteDate date)
  155.         throws PropagationException {
  156.         try {
  157.             setInterpolationDate(date);
  158.             SpacecraftState state = mapper.mapArrayToState(
  159.                     mapper.mapDoubleToDate(model.getInterpolatedTime(), date),
  160.                     model.getInterpolatedState(),
  161.                     meanOrbit);
  162.             for (Map.Entry<String, double[]> initial : unmanaged.entrySet()) {
  163.                 state = state.addAdditionalState(initial.getKey(), initial.getValue());
  164.             }
  165.             return state;
  166.         } catch (OrekitExceptionWrapper oew) {
  167.             if (oew.getException() instanceof PropagationException) {
  168.                 throw (PropagationException) oew.getException();
  169.             } else {
  170.                 throw new PropagationException(oew.getException());
  171.             }
  172.         } catch (OrekitException oe) {
  173.             if (oe instanceof PropagationException) {
  174.                 throw (PropagationException) oe;
  175.             } else {
  176.                 throw new PropagationException(oe);
  177.             }
  178.         }
  179.     }

  180.     /** {@inheritDoc} */
  181.     protected Orbit propagateOrbit(final AbsoluteDate date)
  182.         throws PropagationException {
  183.         return basicPropagate(date).getOrbit();
  184.     }

  185.     /** {@inheritDoc} */
  186.     protected double getMass(final AbsoluteDate date) throws PropagationException {
  187.         return basicPropagate(date).getMass();
  188.     }

  189.     /** {@inheritDoc} */
  190.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame)
  191.         throws OrekitException {
  192.         return propagate(date).getPVCoordinates(frame);
  193.     }

  194.     /** Get the first date of the range.
  195.      * @return the first date of the range
  196.      */
  197.     public AbsoluteDate getMinDate() {
  198.         return minDate;
  199.     }

  200.     /** Get the last date of the range.
  201.      * @return the last date of the range
  202.      */
  203.     public AbsoluteDate getMaxDate() {
  204.         return maxDate;
  205.     }

  206.     @Override
  207.     public Frame getFrame() {
  208.         return this.mapper.getFrame();
  209.     }

  210.     /** {@inheritDoc} */
  211.     public void resetInitialState(final SpacecraftState state)
  212.         throws PropagationException {
  213.         throw new PropagationException(OrekitMessages.NON_RESETABLE_STATE);
  214.     }

  215.     /** {@inheritDoc} */
  216.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward)
  217.         throws PropagationException {
  218.         throw new PropagationException(OrekitMessages.NON_RESETABLE_STATE);
  219.     }

  220.     /** {@inheritDoc} */
  221.     public SpacecraftState getInitialState() throws PropagationException {
  222.         return updateAdditionalStates(basicPropagate(getMinDate()));
  223.     }

  224.     /** Replace the instance with a data transfer object for serialization.
  225.      * @return data transfer object that will be serialized
  226.      * @exception NotSerializableException if the state mapper cannot be serialized (typically for DSST propagator)
  227.      */
  228.     private Object writeReplace() throws NotSerializableException {

  229.         // unmanaged additional states
  230.         final String[]   unmanagedNames  = new String[unmanaged.size()];
  231.         final double[][] unmanagedValues = new double[unmanaged.size()][];
  232.         int i = 0;
  233.         for (Map.Entry<String, double[]> entry : unmanaged.entrySet()) {
  234.             unmanagedNames[i]  = entry.getKey();
  235.             unmanagedValues[i] = entry.getValue();
  236.             ++i;
  237.         }

  238.         // managed states providers
  239.         final List<AdditionalStateProvider> serializableProviders = new ArrayList<AdditionalStateProvider>();
  240.         final List<String> equationNames = new ArrayList<String>();
  241.         for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  242.             if (provider instanceof LocalProvider) {
  243.                 equationNames.add(((LocalProvider) provider).getName());
  244.             } else if (provider instanceof Serializable) {
  245.                 serializableProviders.add(provider);
  246.             }
  247.         }

  248.         return new DataTransferObject(startDate, minDate, maxDate, mapper, meanOrbit, model,
  249.                                       unmanagedNames, unmanagedValues,
  250.                                       serializableProviders.toArray(new AdditionalStateProvider[serializableProviders.size()]),
  251.                                       equationNames.toArray(new String[equationNames.size()]));

  252.     }

  253.     /** Local provider for additional state data. */
  254.     private class LocalProvider implements AdditionalStateProvider {

  255.         /** Name of the additional state. */
  256.         private final String name;

  257.         /** Index of the additional state. */
  258.         private final int index;

  259.         /** Simple constructor.
  260.          * @param name name of the additional state
  261.          * @param index index of the additional state
  262.          */
  263.         LocalProvider(final String name, final int index) {
  264.             this.name  = name;
  265.             this.index = index;
  266.         }

  267.         /** {@inheritDoc} */
  268.         public String getName() {
  269.             return name;
  270.         }

  271.         /** {@inheritDoc} */
  272.         public double[] getAdditionalState(final SpacecraftState state)
  273.             throws PropagationException {

  274.             // set the model date
  275.             setInterpolationDate(state.getDate());

  276.             // extract the part of the interpolated array corresponding to the additional state
  277.             return model.getInterpolatedSecondaryState(index);

  278.         }

  279.     }

  280.     /** Internal class used only for serialization. */
  281.     private static class DataTransferObject implements Serializable {

  282.         /** Serializable UID. */
  283.         private static final long serialVersionUID = 20140213L;

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

  286.         /** Indicator for mean orbit output. */
  287.         private final boolean meanOrbit;

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

  290.         /** First date of the range. */
  291.         private final AbsoluteDate minDate;

  292.         /** Last date of the range. */
  293.         private final AbsoluteDate maxDate;

  294.         /** Underlying raw mathematical model. */
  295.         private final ContinuousOutputModel model;

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

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

  300.         /** Names of additional equations. */
  301.         private final String[] equations;

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

  304.         /** Simple constructor.
  305.          * @param startDate Start date of the integration (can be minDate or maxDate)
  306.          * @param minDate first date of the range
  307.          * @param maxDate last date of the range
  308.          * @param mapper mapper between raw double components and spacecraft state
  309.          * @param meanOrbit output only the mean orbit.
  310.          * @param model underlying raw mathematical model
  311.          * @param unmanagedNames names of unmanaged additional states that must be simply copied
  312.          * @param unmanagedValues values of unmanaged additional states that must be simply copied
  313.          * @param providers providers for pre-integrated states
  314.          * @param equations names of additional equations
  315.          */
  316.         DataTransferObject(final AbsoluteDate startDate,
  317.                                   final AbsoluteDate minDate, final AbsoluteDate maxDate,
  318.                                   final StateMapper mapper, final boolean meanOrbit,
  319.                                   final ContinuousOutputModel model,
  320.                                   final String[] unmanagedNames, final double[][] unmanagedValues,
  321.                                   final AdditionalStateProvider[] providers,
  322.                                   final String[] equations) {
  323.             this.startDate       = startDate;
  324.             this.minDate         = minDate;
  325.             this.maxDate         = maxDate;
  326.             this.mapper          = mapper;
  327.             this.meanOrbit       = meanOrbit;
  328.             this.model           = model;
  329.             this.unmanagedNames  = unmanagedNames;
  330.             this.unmanagedValues = unmanagedValues;
  331.             this.providers       = providers;
  332.             this.equations       = equations;
  333.         }

  334.         /** Replace the deserialized data transfer object with a {@link IntegratedEphemeris}.
  335.          * @return replacement {@link IntegratedEphemeris}
  336.          */
  337.         private Object readResolve() {
  338.             try {
  339.                 final Map<String, double[]> unmanaged = new HashMap<String, double[]>(unmanagedNames.length);
  340.                 for (int i = 0; i < unmanagedNames.length; ++i) {
  341.                     unmanaged.put(unmanagedNames[i], unmanagedValues[i]);
  342.                 }
  343.                 return new IntegratedEphemeris(startDate, minDate, maxDate, mapper, meanOrbit, model,
  344.                                                unmanaged, Arrays.asList(providers), equations);
  345.             } catch (OrekitException oe) {
  346.                 throw new OrekitInternalError(oe);
  347.             }
  348.         }

  349.     }

  350. }