AdapterPropagator.java

  1. /* Copyright 2002-2013 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.PropagationException;
  25. import org.orekit.orbits.Orbit;
  26. import org.orekit.propagation.Propagator;
  27. import org.orekit.propagation.SpacecraftState;
  28. import org.orekit.time.AbsoluteDate;

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

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

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

  65.     }

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

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

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

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

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

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

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

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

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     protected SpacecraftState basicPropagate(final AbsoluteDate date) throws PropagationException {

  112.         try {
  113.             // compute reference state
  114.             SpacecraftState state = reference.propagate(date);
  115.             final Map<String, double[]> before = state.getAdditionalStates();

  116.             // add all the effects
  117.             for (final DifferentialEffect effect : effects) {
  118.                 state = effect.apply(state);
  119.             }

  120.             // forward additional states from the reference propagator
  121.             for (final Map.Entry<String, double[]> entry : before.entrySet()) {
  122.                 if (!state.hasAdditionalState(entry.getKey())) {
  123.                     state = state.addAdditionalState(entry.getKey(), entry.getValue());
  124.                 }
  125.             }

  126.             return state;

  127.         } catch (OrekitExceptionWrapper oew) {
  128.             if (oew.getException() instanceof PropagationException) {
  129.                 throw (PropagationException) oew.getException();
  130.             } else {
  131.                 throw new PropagationException(oew.getException());
  132.             }
  133.         } catch (OrekitException oe) {
  134.             if (oe instanceof PropagationException) {
  135.                 throw (PropagationException) oe;
  136.             } else {
  137.                 throw new PropagationException(oe);
  138.             }
  139.         }

  140.     }

  141.     /** {@inheritDoc} */
  142.     protected Orbit propagateOrbit(final AbsoluteDate date)
  143.         throws PropagationException {
  144.         return basicPropagate(date).getOrbit();
  145.     }

  146.     /** {@inheritDoc}*/
  147.     protected double getMass(final AbsoluteDate date)
  148.         throws PropagationException {
  149.         return basicPropagate(date).getMass();
  150.     }

  151. }