Ephemeris.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.analytical;

  18. import java.io.Serializable;
  19. import java.util.List;
  20. import java.util.Set;
  21. import java.util.stream.Collectors;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.FastMath;
  25. import org.orekit.attitudes.Attitude;
  26. import org.orekit.attitudes.AttitudeProvider;
  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.BoundedPropagator;
  33. import org.orekit.propagation.SpacecraftState;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.utils.ImmutableTimeStampedCache;
  36. import org.orekit.utils.PVCoordinatesProvider;
  37. import org.orekit.utils.TimeStampedPVCoordinates;

  38. /** This class is designed to accept and handle tabulated orbital entries.
  39.  * Tabulated entries are classified and then extrapolated in way to obtain
  40.  * continuous output, with accuracy and computation methods configured by the user.
  41.  *
  42.  * @author Fabien Maussion
  43.  * @author Véronique Pommier-Maurussane
  44.  * @author Luc Maisonobe
  45.  */
  46. public class Ephemeris extends AbstractAnalyticalPropagator implements BoundedPropagator, Serializable {

  47.     /** Default extrapolation time threshold: 1ms.
  48.      * @since 9.0
  49.      **/
  50.     public static final double DEFAULT_EXTRAPOLATION_THRESHOLD_SEC = 1e-3;

  51.     /** Serializable UID. */
  52.     private static final long serialVersionUID = 20170606L;

  53.      /** First date in range. */
  54.     private final AbsoluteDate minDate;

  55.     /** Last date in range. */
  56.     private final AbsoluteDate maxDate;

  57.     /** The extrapolation threshold beyond which the propagation will fail. **/
  58.     private final double extrapolationThreshold;

  59.     /** Reference frame. */
  60.     private final Frame frame;

  61.     /** Names of the additional states. */
  62.     private final String[] additional;

  63.     /** Local PV Provider used for computing attitude. **/
  64.     private LocalPVProvider pvProvider;

  65.     /** Thread-safe cache. */
  66.     private final transient ImmutableTimeStampedCache<SpacecraftState> cache;

  67.     /** Constructor with tabulated states.
  68.      * <p>
  69.      * This constructor allows extrapolating outside of the states time span
  70.      * by up to the 1ms {@link #DEFAULT_EXTRAPOLATION_THRESHOLD_SEC default
  71.      * extrapolation threshold}.
  72.      * </p>
  73.      * @param states tabulates states
  74.      * @param interpolationPoints number of points to use in interpolation
  75.           * @exception MathIllegalArgumentException if the number of states is smaller than
  76.      * the number of points to use in interpolation
  77.      * @see #Ephemeris(List, int, double)
  78.      */
  79.     public Ephemeris(final List<SpacecraftState> states, final int interpolationPoints)
  80.         throws MathIllegalArgumentException {
  81.         this(states, interpolationPoints, DEFAULT_EXTRAPOLATION_THRESHOLD_SEC);
  82.     }

  83.     /** Constructor with tabulated states.
  84.      * @param states tabulates states
  85.      * @param interpolationPoints number of points to use in interpolation
  86.      * @param extrapolationThreshold the largest time difference in seconds between
  87.      * the start or stop boundary of the ephemeris bounds to be doing extrapolation
  88.           * @exception MathIllegalArgumentException if the number of states is smaller than
  89.      * the number of points to use in interpolation
  90.      * @since 9.0
  91.      */
  92.     public Ephemeris(final List<SpacecraftState> states, final int interpolationPoints,
  93.                      final double extrapolationThreshold)
  94.         throws MathIllegalArgumentException {

  95.         super(DEFAULT_LAW);

  96.         if (states.size() < interpolationPoints) {
  97.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INSUFFICIENT_DIMENSION,
  98.                                                    states.size(), interpolationPoints);
  99.         }

  100.         final SpacecraftState s0 = states.get(0);
  101.         minDate = s0.getDate();
  102.         maxDate = states.get(states.size() - 1).getDate();
  103.         frame = s0.getFrame();

  104.         final Set<String> names0 = s0.getAdditionalStates().keySet();
  105.         additional = names0.toArray(new String[names0.size()]);

  106.         // check all states handle the same additional states
  107.         for (final SpacecraftState state : states) {
  108.             s0.ensureCompatibleAdditionalStates(state);
  109.         }

  110.         pvProvider = new LocalPVProvider();

  111.         // user needs to explicitly set attitude provider if they want to use one
  112.         setAttitudeProvider(null);

  113.         // set up cache
  114.         cache = new ImmutableTimeStampedCache<SpacecraftState>(interpolationPoints, states);

  115.         this.extrapolationThreshold = extrapolationThreshold;
  116.     }

  117.     /** Get the first date of the range.
  118.      * @return the first date of the range
  119.      */
  120.     public AbsoluteDate getMinDate() {
  121.         return minDate;
  122.     }

  123.     /** Get the last date of the range.
  124.      * @return the last date of the range
  125.      */
  126.     public AbsoluteDate getMaxDate() {
  127.         return maxDate;
  128.     }

  129.     /** Get the maximum timespan outside of the stored ephemeris that is allowed
  130.      * for extrapolation.
  131.      * @return the extrapolation threshold in seconds
  132.      */
  133.     public double getExtrapolationThreshold() {
  134.         return extrapolationThreshold;
  135.     }

  136.     @Override
  137.     public Frame getFrame() {
  138.         return frame;
  139.     }

  140.     @Override
  141.     /** {@inheritDoc} */
  142.     public SpacecraftState basicPropagate(final AbsoluteDate date) {
  143.         final SpacecraftState evaluatedState;

  144.         final AbsoluteDate central;
  145.         if (date.compareTo(minDate) < 0 && FastMath.abs(date.durationFrom(minDate)) <= extrapolationThreshold) {
  146.             // avoid TimeStampedCacheException as we are still within the tolerance before minDate
  147.             central = minDate;
  148.         } else if (date.compareTo(maxDate) > 0 && FastMath.abs(date.durationFrom(maxDate)) <= extrapolationThreshold) {
  149.             // avoid TimeStampedCacheException as we are still within the tolerance after maxDate
  150.             central = maxDate;
  151.         } else {
  152.             central = date;
  153.         }
  154.         final List<SpacecraftState> neighbors = cache.getNeighbors(central).collect(Collectors.toList());
  155.         evaluatedState = neighbors.get(0).interpolate(date, neighbors);

  156.         final AttitudeProvider attitudeProvider = getAttitudeProvider();

  157.         if (attitudeProvider == null) {
  158.             return evaluatedState;
  159.         } else {
  160.             pvProvider.setCurrentState(evaluatedState);
  161.             final Attitude calculatedAttitude = attitudeProvider.getAttitude(pvProvider, date,
  162.                                                                              evaluatedState.getFrame());
  163.             return new SpacecraftState(evaluatedState.getOrbit(), calculatedAttitude, evaluatedState.getMass());
  164.         }
  165.     }

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

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

  174.     /** {@inheritDoc} */
  175.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  176.         return propagate(date).getPVCoordinates(f);
  177.     }

  178.     /** Try (and fail) to reset the initial state.
  179.      * <p>
  180.      * This method always throws an exception, as ephemerides cannot be reset.
  181.      * </p>
  182.      * @param state new initial state to consider
  183.      */
  184.     public void resetInitialState(final SpacecraftState state) {
  185.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  186.     }

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

  191.     /** {@inheritDoc} */
  192.     public SpacecraftState getInitialState() {
  193.         return basicPropagate(getMinDate());
  194.     }

  195.     /** {@inheritDoc} */
  196.     @Override
  197.     public boolean isAdditionalStateManaged(final String name) {

  198.         // the additional state may be managed by a specific provider in the base class
  199.         if (super.isAdditionalStateManaged(name)) {
  200.             return true;
  201.         }

  202.         // the additional state may be managed in the states sample
  203.         for (final String a : additional) {
  204.             if (a.equals(name)) {
  205.                 return true;
  206.             }
  207.         }

  208.         return false;

  209.     }

  210.     /** {@inheritDoc} */
  211.     @Override
  212.     public String[] getManagedAdditionalStates() {
  213.         final String[] upperManaged = super.getManagedAdditionalStates();
  214.         final String[] managed = new String[upperManaged.length + additional.length];
  215.         System.arraycopy(upperManaged, 0, managed, 0, upperManaged.length);
  216.         System.arraycopy(additional, 0, managed, upperManaged.length, additional.length);
  217.         return managed;
  218.     }

  219.     /** Replace the instance with a data transfer object for serialization.
  220.      * <p>
  221.      * This intermediate class serializes only the data needed for generation,
  222.      * but does <em>not</em> serializes the cache itself (in fact the cache is
  223.      * not serializable).
  224.      * </p>
  225.      * @return data transfer object that will be serialized
  226.      */
  227.     private Object writeReplace() {
  228.         return new DataTransferObject(cache.getAll(), cache.getNeighborsSize(), extrapolationThreshold);
  229.     }

  230.     /** Internal class used only for serialization. */
  231.     private static class DataTransferObject implements Serializable {

  232.         /** Serializable UID. */
  233.         private static final long serialVersionUID = 20170606L;

  234.         /** Tabulates states. */
  235.         private final List<SpacecraftState> states;

  236.         /** Number of points to use in interpolation. */
  237.         private final int interpolationPoints;

  238.         /** The extrapolation threshold beyond which the propagation will fail. **/
  239.         private final double extrapolationThreshold;

  240.         /** Simple constructor.
  241.          * @param states tabulates states
  242.          * @param interpolationPoints number of points to use in interpolation
  243.          * @param extrapolationThreshold extrapolation threshold beyond which the propagation will fail
  244.          */
  245.         private DataTransferObject(final List<SpacecraftState> states, final int interpolationPoints,
  246.                                    final double extrapolationThreshold) {
  247.             this.states                 = states;
  248.             this.interpolationPoints    = interpolationPoints;
  249.             this.extrapolationThreshold = extrapolationThreshold;
  250.         }

  251.         /** Replace the deserialized data transfer object with a
  252.          * {@link Ephemeris}.
  253.          * @return replacement {@link Ephemeris}
  254.          */
  255.         private Object readResolve() {
  256.             try {
  257.                 // build a new provider, with an empty cache
  258.                 return new Ephemeris(states, interpolationPoints, extrapolationThreshold);
  259.             } catch (OrekitException oe) {
  260.                 // this should never happen
  261.                 throw new OrekitInternalError(oe);
  262.             }
  263.         }

  264.     }

  265.     /** Internal PVCoordinatesProvider for attitude computation. */
  266.     private static class LocalPVProvider implements PVCoordinatesProvider, Serializable {

  267.         /** Serializable UID. */
  268.         private static final long serialVersionUID = 20160115L;

  269.         /** Current state. */
  270.         private SpacecraftState currentState;

  271.         /** Get the current state.
  272.          * @return current state
  273.          */
  274.         public SpacecraftState getCurrentState() {
  275.             return currentState;
  276.         }

  277.         /** Set the current state.
  278.          * @param state state to set
  279.          */
  280.         public void setCurrentState(final SpacecraftState state) {
  281.             this.currentState = state;
  282.         }

  283.         /** {@inheritDoc} */
  284.         public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame f) {
  285.             final double dt = getCurrentState().getDate().durationFrom(date);
  286.             final double closeEnoughTimeInSec = 1e-9;

  287.             if (FastMath.abs(dt) > closeEnoughTimeInSec) {
  288.                 throw new OrekitException(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, FastMath.abs(dt), 0.0,
  289.                                           closeEnoughTimeInSec);
  290.             }

  291.             return getCurrentState().getPVCoordinates(f);

  292.         }

  293.     }

  294. }