AdapterPropagator.java

  1. /* Copyright 2002-2025 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. import org.orekit.utils.DataDictionary;

  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.          */
  63.         SpacecraftState apply(SpacecraftState original);

  64.     }

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

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

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

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

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

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

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

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

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

  139.         return state;

  140.     }

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

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

  149. }