AdapterPropagator.java

  1. /* Copyright 2002-2024 CS GROUP
  2.  * Licensed to CS GROUP (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 org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.orbits.Orbit;
  24. import org.orekit.propagation.Propagator;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.utils.DoubleArrayDictionary;

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

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

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

  63.     }

  64.     /** Underlying reference propagator. */
  65.     private Propagator reference;

  66.     /** Effects to add. */
  67.     private List<DifferentialEffect> effects;

  68.     /** Build a propagator from an underlying reference propagator.
  69.      * <p>The reference propagator can be almost anything, numerical,
  70.      * analytical, and even an ephemeris. It may already take some maneuvers
  71.      * into account.</p>
  72.      * @param reference reference propagator
  73.      */
  74.     public AdapterPropagator(final Propagator reference) {
  75.         super(reference.getAttitudeProvider());
  76.         this.reference = reference;
  77.         this.effects = new ArrayList<DifferentialEffect>();
  78.         // Sets initial state
  79.         super.resetInitialState(getInitialState());
  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() {
  101.         return reference.getInitialState();
  102.     }

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

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

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {

  119.         // compute reference state
  120.         SpacecraftState state = reference.propagate(date);
  121.         final DoubleArrayDictionary additionalBefore    = state.getAdditionalStatesValues();
  122.         final DoubleArrayDictionary additionalDotBefore = state.getAdditionalStatesDerivatives();

  123.         // add all the effects
  124.         for (final DifferentialEffect effect : effects) {
  125.             state = effect.apply(state);
  126.         }

  127.         // forward additional states and derivatives from the reference propagator
  128.         for (final DoubleArrayDictionary.Entry entry : additionalBefore.getData()) {
  129.             if (!state.hasAdditionalState(entry.getKey())) {
  130.                 state = state.addAdditionalState(entry.getKey(), entry.getValue());
  131.             }
  132.         }
  133.         for (final DoubleArrayDictionary.Entry entry : additionalDotBefore.getData()) {
  134.             if (!state.hasAdditionalState(entry.getKey())) {
  135.                 state = state.addAdditionalStateDerivative(entry.getKey(), entry.getValue());
  136.             }
  137.         }

  138.         return state;

  139.     }

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

  144.     /** {@inheritDoc}*/
  145.     protected double getMass(final AbsoluteDate date) {
  146.         return basicPropagate(date).getMass();
  147.     }

  148. }