KeplerianPropagator.java

  1. /* Copyright 2002-2017 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.NotSerializableException;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.SortedSet;

  23. import org.orekit.attitudes.Attitude;
  24. import org.orekit.attitudes.AttitudeProvider;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitInternalError;
  27. import org.orekit.orbits.Orbit;
  28. import org.orekit.orbits.OrbitType;
  29. import org.orekit.orbits.PositionAngle;
  30. import org.orekit.propagation.AdditionalStateProvider;
  31. import org.orekit.propagation.SpacecraftState;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.TimeSpanMap;

  34. /** Simple Keplerian orbit propagator.
  35.  * @see Orbit
  36.  * @author Guylaine Prat
  37.  */
  38. public class KeplerianPropagator extends AbstractAnalyticalPropagator implements Serializable {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20151117L;

  41.     /** Initial state. */
  42.     private SpacecraftState initialState;

  43.     /** All states. */
  44.     private transient TimeSpanMap<SpacecraftState> states;

  45.     /** Build a propagator from orbit only.
  46.      * <p>The central attraction coefficient μ is set to the same value used
  47.      * for the initial orbit definition. Mass and attitude provider are set to
  48.      * unspecified non-null arbitrary values.</p>
  49.      * @param initialOrbit initial orbit
  50.      * @exception OrekitException if initial attitude cannot be computed
  51.      */
  52.     public KeplerianPropagator(final Orbit initialOrbit)
  53.         throws OrekitException {
  54.         this(initialOrbit, DEFAULT_LAW, initialOrbit.getMu(), DEFAULT_MASS);
  55.     }

  56.     /** Build a propagator from orbit and central attraction coefficient μ.
  57.      * <p>Mass and attitude provider are set to unspecified non-null arbitrary values.</p>
  58.      * @param initialOrbit initial orbit
  59.      * @param mu central attraction coefficient (m³/s²)
  60.      * @exception OrekitException if initial attitude cannot be computed
  61.      */
  62.     public KeplerianPropagator(final Orbit initialOrbit, final double mu)
  63.         throws OrekitException {
  64.         this(initialOrbit, DEFAULT_LAW, mu, DEFAULT_MASS);
  65.     }

  66.     /** Build a propagator from orbit and attitude provider.
  67.      * <p>The central attraction coefficient μ is set to the same value
  68.      * used for the initial orbit definition. Mass is set to an unspecified
  69.      * non-null arbitrary value.</p>
  70.      * @param initialOrbit initial orbit
  71.      * @param attitudeProv  attitude provider
  72.      * @exception OrekitException if initial attitude cannot be computed
  73.      */
  74.     public KeplerianPropagator(final Orbit initialOrbit,
  75.                                final AttitudeProvider attitudeProv)
  76.         throws OrekitException {
  77.         this(initialOrbit, attitudeProv, initialOrbit.getMu(), DEFAULT_MASS);
  78.     }

  79.     /** Build a propagator from orbit, attitude provider and central attraction
  80.      * coefficient μ.
  81.      * <p>Mass is set to an unspecified non-null arbitrary value.</p>
  82.      * @param initialOrbit initial orbit
  83.      * @param attitudeProv attitude provider
  84.      * @param mu central attraction coefficient (m³/s²)
  85.      * @exception OrekitException if initial attitude cannot be computed
  86.      */
  87.     public KeplerianPropagator(final Orbit initialOrbit,
  88.                                final AttitudeProvider attitudeProv,
  89.                                final double mu)
  90.         throws OrekitException {
  91.         this(initialOrbit, attitudeProv, mu, DEFAULT_MASS);
  92.     }

  93.     /** Build propagator from orbit, attitude provider, central attraction
  94.      * coefficient μ and mass.
  95.      * @param initialOrbit initial orbit
  96.      * @param attitudeProv attitude provider
  97.      * @param mu central attraction coefficient (m³/s²)
  98.      * @param mass spacecraft mass (kg)
  99.      * @exception OrekitException if initial attitude cannot be computed
  100.      */
  101.     public KeplerianPropagator(final Orbit initialOrbit, final AttitudeProvider attitudeProv,
  102.                                final double mu, final double mass)
  103.         throws OrekitException {

  104.         super(attitudeProv);

  105.         // ensure the orbit use the specified mu and has no non-Keplerian derivatives
  106.         initialState = fixState(initialOrbit,
  107.                                 getAttitudeProvider().getAttitude(initialOrbit,
  108.                                                                   initialOrbit.getDate(),
  109.                                                                   initialOrbit.getFrame()),
  110.                                 mass, mu);
  111.         states = new TimeSpanMap<SpacecraftState>(initialState);
  112.         super.resetInitialState(initialState);

  113.     }

  114.     /** Fix state to use a specified mu and remove derivatives.
  115.      * <p>
  116.      * This ensures the propagation model (which is based on calling
  117.      * {@link Orbit#shiftedBy(double)}) is Keplerian only and uses a specified mu.
  118.      * </p>
  119.      * @param orbit orbit to fix
  120.      * @param attitude current attitude
  121.      * @param mass current mass
  122.      * @param mu gravity coefficient to use
  123.      * @return fixed orbit
  124.      */
  125.     private SpacecraftState fixState(final Orbit orbit, final Attitude attitude, final double mass,
  126.                                      final double mu) {
  127.         final OrbitType type = orbit.getType();
  128.         final double[] stateVector = new double[6];
  129.         type.mapOrbitToArray(orbit, PositionAngle.TRUE, stateVector, null);
  130.         final Orbit fixedOrbit = type.mapArrayToOrbit(stateVector, null, PositionAngle.TRUE,
  131.                                                       orbit.getDate(), mu, orbit.getFrame());
  132.         return new SpacecraftState(fixedOrbit, attitude, mass);
  133.     }

  134.     /** {@inheritDoc} */
  135.     public void resetInitialState(final SpacecraftState state)
  136.         throws OrekitException {

  137.         // ensure the orbit use the specified mu and has no non-Keplerian derivatives
  138.         final double mu = initialState == null ? state.getMu() : initialState.getMu();
  139.         final SpacecraftState fixedState = fixState(state.getOrbit(),
  140.                                                     state.getAttitude(),
  141.                                                     state.getMass(),
  142.                                                     mu);

  143.         initialState = fixedState;
  144.         states       = new TimeSpanMap<SpacecraftState>(initialState);
  145.         super.resetInitialState(fixedState);

  146.     }

  147.     /** {@inheritDoc} */
  148.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward)
  149.         throws OrekitException {
  150.         if (forward) {
  151.             states.addValidAfter(state, state.getDate());
  152.         } else {
  153.             states.addValidBefore(state, state.getDate());
  154.         }
  155.     }

  156.     /** {@inheritDoc} */
  157.     protected Orbit propagateOrbit(final AbsoluteDate date)
  158.         throws OrekitException {

  159.         // propagate orbit
  160.         Orbit orbit = states.get(date).getOrbit();
  161.         do {
  162.             // we use a loop here to compensate for very small date shifts error
  163.             // that occur with long propagation time
  164.             orbit = orbit.shiftedBy(date.durationFrom(orbit.getDate()));
  165.         } while (!date.equals(orbit.getDate()));

  166.         return orbit;

  167.     }

  168.     /** {@inheritDoc}*/
  169.     protected double getMass(final AbsoluteDate date) {
  170.         return states.get(date).getMass();
  171.     }

  172.     /** Replace the instance with a data transfer object for serialization.
  173.      * @return data transfer object that will be serialized
  174.      * @exception NotSerializableException if an additional state provider is not serializable
  175.      */
  176.     private Object writeReplace() throws NotSerializableException {
  177.         try {

  178.             // managed states providers
  179.             final List<AdditionalStateProvider> serializableProviders = new ArrayList<AdditionalStateProvider>();
  180.             for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  181.                 if (provider instanceof Serializable) {
  182.                     serializableProviders.add(provider);
  183.                 } else {
  184.                     throw new NotSerializableException(provider.getClass().getName());
  185.                 }
  186.             }

  187.             // states transitions
  188.             final AbsoluteDate[]    transitionDates;
  189.             final SpacecraftState[] allStates;
  190.             final SortedSet<TimeSpanMap.Transition<SpacecraftState>> transitions = states.getTransitions();
  191.             if (transitions.size() == 1  && transitions.first().getBefore() == transitions.first().getAfter()) {
  192.                 // the single entry is a dummy one, without a real transition
  193.                 // we ignore it completely
  194.                 transitionDates = null;
  195.                 allStates       = null;
  196.             } else {
  197.                 transitionDates = new AbsoluteDate[transitions.size()];
  198.                 allStates       = new SpacecraftState[transitions.size() + 1];
  199.                 int i = 0;
  200.                 for (final TimeSpanMap.Transition<SpacecraftState> transition : transitions) {
  201.                     if (i == 0) {
  202.                         // state before the first transition
  203.                         allStates[i] = transition.getBefore();
  204.                     }
  205.                     transitionDates[i] = transition.getDate();
  206.                     allStates[++i]     = transition.getAfter();
  207.                 }
  208.             }

  209.             return new DataTransferObject(getInitialState().getOrbit(), getAttitudeProvider(),
  210.                                           getInitialState().getMu(), getInitialState().getMass(),
  211.                                           transitionDates, allStates,
  212.                                           serializableProviders.toArray(new AdditionalStateProvider[serializableProviders.size()]));
  213.         } catch (OrekitException orekitException) {
  214.             // this should never happen
  215.             throw new OrekitInternalError(null);
  216.         }

  217.     }

  218.     /** Internal class used only for serialization. */
  219.     private static class DataTransferObject implements Serializable {

  220.         /** Serializable UID. */
  221.         private static final long serialVersionUID = 20151202L;

  222.         /** Initial orbit. */
  223.         private final Orbit orbit;

  224.         /** Attitude provider. */
  225.         private final AttitudeProvider attitudeProvider;

  226.         /** Central attraction coefficient (m³/s²). */
  227.         private final double mu;

  228.         /** Spacecraft mass (kg). */
  229.         private final double mass;

  230.         /** Transition dates (may be null). */
  231.         private final AbsoluteDate[] transitionDates;

  232.         /** States before and after transitions (may be null). */
  233.         private final SpacecraftState[] allStates;

  234.         /** Providers for additional states. */
  235.         private final AdditionalStateProvider[] providers;

  236.         /** Simple constructor.
  237.          * @param orbit initial orbit
  238.          * @param attitudeProvider attitude provider
  239.          * @param mu central attraction coefficient (m³/s²)
  240.          * @param mass initial spacecraft mass (kg)
  241.          * @param transitionDates transition dates (may be null)
  242.          * @param allStates states before and after transitions (may be null)
  243.          * @param providers providers for additional states
  244.          */
  245.         DataTransferObject(final Orbit orbit,
  246.                            final AttitudeProvider attitudeProvider,
  247.                            final double mu, final double mass,
  248.                            final AbsoluteDate[] transitionDates,
  249.                            final SpacecraftState[] allStates,
  250.                            final AdditionalStateProvider[] providers) {
  251.             this.orbit            = orbit;
  252.             this.attitudeProvider = attitudeProvider;
  253.             this.mu               = mu;
  254.             this.mass             = mass;
  255.             this.transitionDates  = transitionDates;
  256.             this.allStates        = allStates;
  257.             this.providers        = providers;
  258.         }

  259.         /** Replace the deserialized data transfer object with a {@link KeplerianPropagator}.
  260.          * @return replacement {@link KeplerianPropagator}
  261.          */
  262.         private Object readResolve() {
  263.             try {

  264.                 final KeplerianPropagator propagator =
  265.                                 new KeplerianPropagator(orbit, attitudeProvider, mu, mass);
  266.                 for (final AdditionalStateProvider provider : providers) {
  267.                     propagator.addAdditionalStateProvider(provider);
  268.                 }

  269.                 if (transitionDates != null) {
  270.                     // override the state transitions
  271.                     propagator.states = new TimeSpanMap<SpacecraftState>(allStates[0]);
  272.                     for (int i = 0; i < transitionDates.length; ++i) {
  273.                         propagator.states.addValidAfter(allStates[i + 1], transitionDates[i]);
  274.                     }
  275.                 }

  276.                 return propagator;

  277.             } catch (OrekitException oe) {
  278.                 throw new OrekitInternalError(oe);
  279.             }
  280.         }

  281.     }

  282. }