KeplerianPropagator.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.NotSerializableException;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.SortedSet;

  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.orbits.Orbit;
  30. import org.orekit.orbits.OrbitType;
  31. import org.orekit.orbits.PositionAngle;
  32. import org.orekit.propagation.AdditionalStateProvider;
  33. import org.orekit.propagation.SpacecraftState;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.utils.TimeSpanMap;

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

  41.     /** Serializable UID. */
  42.     private static final long serialVersionUID = 20151117L;

  43.     /** Initial state. */
  44.     private SpacecraftState initialState;

  45.     /** All states. */
  46.     private transient TimeSpanMap<SpacecraftState> states;

  47.     /** Build a propagator from orbit only.
  48.      * <p>The central attraction coefficient μ is set to the same value used
  49.      * for the initial orbit definition. Mass and attitude provider are set to
  50.      * unspecified non-null arbitrary values.</p>
  51.      * @param initialOrbit initial orbit
  52.      */
  53.     public KeplerianPropagator(final Orbit initialOrbit) {
  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.      */
  61.     public KeplerianPropagator(final Orbit initialOrbit, final double mu) {
  62.         this(initialOrbit, DEFAULT_LAW, mu, DEFAULT_MASS);
  63.     }

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

  75.     /** Build a propagator from orbit, attitude provider and central attraction
  76.      * coefficient μ.
  77.      * <p>Mass is set to an unspecified non-null arbitrary value.</p>
  78.      * @param initialOrbit initial orbit
  79.      * @param attitudeProv attitude provider
  80.      * @param mu central attraction coefficient (m³/s²)
  81.      */
  82.     public KeplerianPropagator(final Orbit initialOrbit,
  83.                                final AttitudeProvider attitudeProv,
  84.                                final double mu) {
  85.         this(initialOrbit, attitudeProv, mu, DEFAULT_MASS);
  86.     }

  87.     /** Build propagator from orbit, attitude provider, central attraction
  88.      * coefficient μ and mass.
  89.      * @param initialOrbit initial orbit
  90.      * @param attitudeProv attitude provider
  91.      * @param mu central attraction coefficient (m³/s²)
  92.      * @param mass spacecraft mass (kg)
  93.      */
  94.     public KeplerianPropagator(final Orbit initialOrbit, final AttitudeProvider attitudeProv,
  95.                                final double mu, final double mass) {

  96.         super(attitudeProv);

  97.         // ensure the orbit use the specified mu and has no non-Keplerian derivatives
  98.         initialState = fixState(initialOrbit,
  99.                                 getAttitudeProvider().getAttitude(initialOrbit,
  100.                                                                   initialOrbit.getDate(),
  101.                                                                   initialOrbit.getFrame()),
  102.                                 mass, mu, Collections.emptyMap());
  103.         states = new TimeSpanMap<SpacecraftState>(initialState);
  104.         super.resetInitialState(initialState);

  105.     }

  106.     /** Fix state to use a specified mu and remove derivatives.
  107.      * <p>
  108.      * This ensures the propagation model (which is based on calling
  109.      * {@link Orbit#shiftedBy(double)}) is Keplerian only and uses a specified mu.
  110.      * </p>
  111.      * @param orbit orbit to fix
  112.      * @param attitude current attitude
  113.      * @param mass current mass
  114.      * @param mu gravity coefficient to use
  115.      * @param additionalStates additional states
  116.      * @return fixed orbit
  117.      */
  118.     private SpacecraftState fixState(final Orbit orbit, final Attitude attitude, final double mass,
  119.                                      final double mu, final Map<String, double[]> additionalStates) {
  120.         final OrbitType type = orbit.getType();
  121.         final double[] stateVector = new double[6];
  122.         type.mapOrbitToArray(orbit, PositionAngle.TRUE, stateVector, null);
  123.         final Orbit fixedOrbit = type.mapArrayToOrbit(stateVector, null, PositionAngle.TRUE,
  124.                                                       orbit.getDate(), mu, orbit.getFrame());
  125.         SpacecraftState fixedState = new SpacecraftState(fixedOrbit, attitude, mass);
  126.         for (final Map.Entry<String, double[]> entry : additionalStates.entrySet()) {
  127.             fixedState = fixedState.addAdditionalState(entry.getKey(), entry.getValue());
  128.         }
  129.         return fixedState;
  130.     }

  131.     /** {@inheritDoc} */
  132.     public void resetInitialState(final SpacecraftState state) {

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

  140.         initialState = fixedState;
  141.         states       = new TimeSpanMap<SpacecraftState>(initialState);
  142.         super.resetInitialState(fixedState);

  143.     }

  144.     /** {@inheritDoc} */
  145.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  146.         if (forward) {
  147.             states.addValidAfter(state, state.getDate());
  148.         } else {
  149.             states.addValidBefore(state, state.getDate());
  150.         }
  151.     }

  152.     /** {@inheritDoc} */
  153.     protected Orbit propagateOrbit(final AbsoluteDate date) {

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

  161.         return orbit;

  162.     }

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

  167.     /** Replace the instance with a data transfer object for serialization.
  168.      * @return data transfer object that will be serialized
  169.      * @exception NotSerializableException if an additional state provider is not serializable
  170.      */
  171.     private Object writeReplace() throws NotSerializableException {
  172.         try {

  173.             // managed states providers
  174.             final List<AdditionalStateProvider> serializableProviders = new ArrayList<AdditionalStateProvider>();
  175.             for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  176.                 if (provider instanceof Serializable) {
  177.                     serializableProviders.add(provider);
  178.                 } else {
  179.                     throw new NotSerializableException(provider.getClass().getName());
  180.                 }
  181.             }

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

  204.             return new DataTransferObject(getInitialState().getOrbit(), getAttitudeProvider(),
  205.                                           getInitialState().getMu(), getInitialState().getMass(),
  206.                                           transitionDates, allStates,
  207.                                           serializableProviders.toArray(new AdditionalStateProvider[serializableProviders.size()]));
  208.         } catch (OrekitException orekitException) {
  209.             // this should never happen
  210.             throw new OrekitInternalError(null);
  211.         }

  212.     }

  213.     /** Internal class used only for serialization. */
  214.     private static class DataTransferObject implements Serializable {

  215.         /** Serializable UID. */
  216.         private static final long serialVersionUID = 20151202L;

  217.         /** Initial orbit. */
  218.         private final Orbit orbit;

  219.         /** Attitude provider. */
  220.         private final AttitudeProvider attitudeProvider;

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

  223.         /** Spacecraft mass (kg). */
  224.         private final double mass;

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

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

  229.         /** Providers for additional states. */
  230.         private final AdditionalStateProvider[] providers;

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

  254.         /** Replace the deserialized data transfer object with a {@link KeplerianPropagator}.
  255.          * @return replacement {@link KeplerianPropagator}
  256.          */
  257.         private Object readResolve() {
  258.             try {

  259.                 final KeplerianPropagator propagator =
  260.                                 new KeplerianPropagator(orbit, attitudeProvider, mu, mass);
  261.                 for (final AdditionalStateProvider provider : providers) {
  262.                     propagator.addAdditionalStateProvider(provider);
  263.                 }

  264.                 if (transitionDates != null) {
  265.                     // override the state transitions
  266.                     propagator.states = new TimeSpanMap<SpacecraftState>(allStates[0]);
  267.                     for (int i = 0; i < transitionDates.length; ++i) {
  268.                         propagator.states.addValidAfter(allStates[i + 1], transitionDates[i]);
  269.                     }
  270.                 }

  271.                 return propagator;

  272.             } catch (OrekitException oe) {
  273.                 throw new OrekitInternalError(oe);
  274.             }
  275.         }

  276.     }

  277. }