AggregateBoundedPropagator.java

  1. /* Contributed in the public domain.
  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.Collection;
  19. import java.util.Collections;
  20. import java.util.Map.Entry;
  21. import java.util.NavigableMap;
  22. import java.util.TreeMap;

  23. import org.orekit.attitudes.Attitude;
  24. import org.orekit.attitudes.AttitudeProvider;
  25. import org.orekit.attitudes.FrameAlignedProvider;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.orbits.Orbit;
  30. import org.orekit.propagation.BoundedPropagator;
  31. import org.orekit.propagation.Propagator;
  32. import org.orekit.propagation.SpacecraftState;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.utils.TimeStampedPVCoordinates;

  35. /**
  36.  * A {@link BoundedPropagator} that covers a larger time span from several constituent
  37.  * propagators that cover shorter time spans.
  38.  *
  39.  * @author Evan Ward
  40.  * @see #AggregateBoundedPropagator(Collection)
  41.  * @since 9.0
  42.  */
  43. public class AggregateBoundedPropagator extends AbstractAnalyticalPropagator
  44.         implements BoundedPropagator {

  45.     /** Constituent propagators. */
  46.     private final NavigableMap<AbsoluteDate, ? extends BoundedPropagator> propagators;
  47.     /** Minimum date for {@link #getMinDate()}. */
  48.     private final AbsoluteDate min;
  49.     /** Maximum date for {@link #getMaxDate()}. */
  50.     private final AbsoluteDate max;

  51.     /**
  52.      * Create a propagator by concatenating several {@link BoundedPropagator}s.
  53.      *
  54.      * @param propagators that provide the backing data for this instance. There must be
  55.      *                    at least one propagator in the collection. If there are gaps
  56.      *                    between the {@link BoundedPropagator#getMaxDate()} of one
  57.      *                    propagator and the {@link BoundedPropagator#getMinDate()} of the
  58.      *                    next propagator an exception may be thrown by any method of this
  59.      *                    class at any time. If there are overlaps between the {@link
  60.      *                    BoundedPropagator#getMaxDate()} of one propagator and the {@link
  61.      *                    BoundedPropagator#getMinDate()} of the next propagator then the
  62.      *                    propagator with the latest {@link BoundedPropagator#getMinDate()}
  63.      *                    is used.
  64.      */
  65.     public AggregateBoundedPropagator(
  66.             final Collection<? extends BoundedPropagator> propagators) {
  67.         super(defaultAttitude(propagators));
  68.         final NavigableMap<AbsoluteDate, BoundedPropagator> map =
  69.                 new TreeMap<>();
  70.         for (final BoundedPropagator propagator : propagators) {
  71.             map.put(propagator.getMinDate(), propagator);
  72.         }
  73.         this.propagators = map;
  74.         this.min = map.firstEntry().getValue().getMinDate();
  75.         this.max = map.lastEntry().getValue().getMaxDate();
  76.         super.resetInitialState(
  77.                 this.propagators.firstEntry().getValue().getInitialState());
  78.     }

  79.     /**
  80.      * Create a propagator from several constituent propagators.
  81.      *
  82.      * @param propagators that provide the backing data for this instance. Each
  83.      *                    propagator is used from the date of it's key in the
  84.      *                    map until the date of the next key. The first
  85.      *                    propagator is also used before the first key and the
  86.      *                    last propagator after the last key.
  87.      * @param min         the value for {@link #getMinDate()}.
  88.      * @param max         the value for {@link #getMaxDate()}.
  89.      */
  90.     public AggregateBoundedPropagator(
  91.             final NavigableMap<AbsoluteDate, ? extends BoundedPropagator> propagators,
  92.             final AbsoluteDate min,
  93.             final AbsoluteDate max) {
  94.         super(defaultAttitude(propagators.values()));
  95.         this.propagators = propagators;
  96.         this.min = min;
  97.         this.max = max;
  98.         super.resetInitialState(
  99.                 this.propagators.firstEntry().getValue().getInitialState());
  100.     }

  101.     /**
  102.      * Helper function for the constructor.
  103.      * @param propagators to consider.
  104.      * @return attitude provider.
  105.      */
  106.     private static AttitudeProvider defaultAttitude(
  107.             final Collection<? extends Propagator> propagators) {
  108.         // this check is needed here because it can't be before the super() call in the
  109.         // constructor.
  110.         if (propagators.isEmpty()) {
  111.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_PROPAGATORS);
  112.         }
  113.         return new FrameAlignedProvider(propagators.iterator().next().getFrame());
  114.     }

  115.     /** Get an unmodifiable view of the propagators map.
  116.      * <p>
  117.      * The key of the map entries are the {@link BoundedPropagator#getMinDate() min dates}
  118.      * of each propagator.
  119.      * </p>
  120.      * @return unmodifiable view of the propagators map
  121.      * @since 12.0
  122.      */
  123.     public NavigableMap<AbsoluteDate, ? extends BoundedPropagator> getPropagators() {
  124.         return Collections.unmodifiableNavigableMap(propagators);
  125.     }

  126.     @Override
  127.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {
  128.         // #589 override this method for a performance benefit,
  129.         // getPropagator(date).propagate(date) is only called once

  130.         // do propagation
  131.         final SpacecraftState state = getPropagator(date).propagate(date);

  132.         // evaluate attitude
  133.         final Attitude attitude =
  134.                 getAttitudeProvider().getAttitude(this, date, state.getFrame());

  135.         // build raw state
  136.         if (state.isOrbitDefined()) {
  137.             return new SpacecraftState(
  138.                     state.getOrbit(), attitude, state.getMass(),
  139.                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
  140.         } else {
  141.             return new SpacecraftState(
  142.                     state.getAbsPVA(), attitude, state.getMass(),
  143.                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
  144.         }
  145.     }

  146.     @Override
  147.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date,
  148.                                                      final Frame frame) {
  149.         return getPropagator(date).getPVCoordinates(date, frame);
  150.     }

  151.     @Override
  152.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  153.         return getPropagator(date).propagate(date).getOrbit();
  154.     }

  155.     @Override
  156.     public AbsoluteDate getMinDate() {
  157.         return min;
  158.     }

  159.     @Override
  160.     public AbsoluteDate getMaxDate() {
  161.         return max;
  162.     }

  163.     @Override
  164.     protected double getMass(final AbsoluteDate date) {
  165.         return getPropagator(date).propagate(date).getMass();
  166.     }

  167.     @Override
  168.     public SpacecraftState getInitialState() {
  169.         return propagators.firstEntry().getValue().getInitialState();
  170.     }

  171.     @Override
  172.     protected void resetIntermediateState(final SpacecraftState state,
  173.                                           final boolean forward) {
  174.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  175.     }

  176.     @Override
  177.     public void resetInitialState(final SpacecraftState state) {
  178.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  179.     }

  180.     /**
  181.      * Get the propagator to use for the given date.
  182.      *
  183.      * @param date of query
  184.      * @return propagator to use on date.
  185.      */
  186.     private BoundedPropagator getPropagator(final AbsoluteDate date) {
  187.         final Entry<AbsoluteDate, ? extends BoundedPropagator> entry =
  188.                 propagators.floorEntry(date);
  189.         if (entry != null) {
  190.             return entry.getValue();
  191.         } else {
  192.             // let the first propagator throw the exception
  193.             return propagators.firstEntry().getValue();
  194.         }
  195.     }

  196. }