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

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitExceptionWrapper;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.errors.PropagationException;
  26. import org.orekit.orbits.Orbit;
  27. import org.orekit.propagation.Propagator;
  28. import org.orekit.propagation.SpacecraftState;
  29. import org.orekit.time.AbsoluteDate;

  30. /** Orbit propagator that adapts an underlying propagator, adding {@link
  31.  * DifferentialEffect differential effects}.
  32.  * <p>
  33.  * This propagator is used when a reference propagator does not handle
  34.  * some effects that we need. A typical example would be an ephemeris
  35.  * that was computed for a reference orbit, and we want to compute a
  36.  * station-keeping maneuver on top of this ephemeris, changing its
  37.  * final state. The principal is to add one or more {@link
  38.  * org.orekit.forces.maneuvers.SmallManeuverAnalyticalModel small maneuvers
  39.  * analytical models} to it and use it as a new propagator, which takes the
  40.  * maneuvers into account.
  41.  * </p>
  42.  * <p>
  43.  * From a space flight dynamics point of view, this is a differential
  44.  * correction approach. From a computer science point of view, this is
  45.  * a use of the decorator design pattern.
  46.  * </p>
  47.  * @see Propagator
  48.  * @see org.orekit.forces.maneuvers.SmallManeuverAnalyticalModel
  49.  * @author Luc Maisonobe
  50.  */
  51. public class AdapterPropagator extends AbstractAnalyticalPropagator {

  52.     /** Interface for orbit differential effects. */
  53.     public interface DifferentialEffect {

  54.         /** Apply the effect to a {@link SpacecraftState spacecraft state}.
  55.          * <p>
  56.          * Applying the effect may be a no-op in some cases. A typical example
  57.          * is maneuvers, for which the state is changed only for time <em>after</em>
  58.          * the maneuver occurrence.
  59.          * </p>
  60.          * @param original original state <em>without</em> the effect
  61.          * @return updated state at the same date, taking the effect
  62.          * into account if meaningful
  63.          * @exception OrekitException if effect cannot be computed
  64.          */
  65.         SpacecraftState apply(SpacecraftState original) throws OrekitException;

  66.     }

  67.     /** Underlying reference propagator. */
  68.     private Propagator reference;

  69.     /** Effects to add. */
  70.     private List<DifferentialEffect> effects;

  71.     /** Build a propagator from an underlying reference propagator.
  72.      * <p>The reference propagator can be almost anything, numerical,
  73.      * analytical, and even an ephemeris. It may already take some maneuvers
  74.      * into account.</p>
  75.      * @param reference reference propagator
  76.      */
  77.     public AdapterPropagator(final Propagator reference) {
  78.         super(reference.getAttitudeProvider());
  79.         this.reference = reference;
  80.         this.effects = new ArrayList<DifferentialEffect>();
  81.     }

  82.     /** Add a differential effect.
  83.      * @param effect differential effect
  84.      */
  85.     public void addEffect(final DifferentialEffect effect) {
  86.         effects.add(effect);
  87.     }

  88.     /** Get the reference propagator.
  89.      * @return reference propagator
  90.      */
  91.     public Propagator getPropagator() {
  92.         return reference;
  93.     }

  94.     /** Get the differential effects.
  95.      * @return differential effects models, as an unmodifiable list
  96.      */
  97.     public List<DifferentialEffect> getEffects() {
  98.         return Collections.unmodifiableList(effects);
  99.     }

  100.     /** {@inheritDoc} */
  101.     public SpacecraftState getInitialState() throws PropagationException {
  102.         return reference.getInitialState();
  103.     }

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public void resetInitialState(final SpacecraftState state)
  107.         throws PropagationException {
  108.         reference.resetInitialState(state);
  109.     }

  110.     /** {@inheritDoc} */
  111.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward)
  112.         throws PropagationException {
  113.         if (reference instanceof AbstractAnalyticalPropagator) {
  114.             ((AbstractAnalyticalPropagator) reference).resetIntermediateState(state, forward);
  115.         } else {
  116.             throw new PropagationException(OrekitMessages.NON_RESETABLE_STATE);
  117.         }
  118.     }

  119.     /** {@inheritDoc} */
  120.     @Override
  121.     protected SpacecraftState basicPropagate(final AbsoluteDate date) throws PropagationException {

  122.         try {
  123.             // compute reference state
  124.             SpacecraftState state = reference.propagate(date);
  125.             final Map<String, double[]> before = state.getAdditionalStates();

  126.             // add all the effects
  127.             for (final DifferentialEffect effect : effects) {
  128.                 state = effect.apply(state);
  129.             }

  130.             // forward additional states from the reference propagator
  131.             for (final Map.Entry<String, double[]> entry : before.entrySet()) {
  132.                 if (!state.hasAdditionalState(entry.getKey())) {
  133.                     state = state.addAdditionalState(entry.getKey(), entry.getValue());
  134.                 }
  135.             }

  136.             return state;

  137.         } catch (OrekitExceptionWrapper oew) {
  138.             if (oew.getException() instanceof PropagationException) {
  139.                 throw (PropagationException) oew.getException();
  140.             } else {
  141.                 throw new PropagationException(oew.getException());
  142.             }
  143.         } catch (OrekitException oe) {
  144.             if (oe instanceof PropagationException) {
  145.                 throw (PropagationException) oe;
  146.             } else {
  147.                 throw new PropagationException(oe);
  148.             }
  149.         }

  150.     }

  151.     /** {@inheritDoc} */
  152.     protected Orbit propagateOrbit(final AbsoluteDate date)
  153.         throws PropagationException {
  154.         return basicPropagate(date).getOrbit();
  155.     }

  156.     /** {@inheritDoc}*/
  157.     protected double getMass(final AbsoluteDate date)
  158.         throws PropagationException {
  159.         return basicPropagate(date).getMass();
  160.     }

  161. }