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.Map.Entry;
  20. import java.util.NavigableMap;
  21. import java.util.TreeMap;

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

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

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

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

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

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

  114.     @Override
  115.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {
  116.         // #589 override this method for a performance benefit,
  117.         // getPropagator(date).propagate(date) is only called once

  118.         // do propagation
  119.         final SpacecraftState state = getPropagator(date).propagate(date);

  120.         // evaluate attitude
  121.         final Attitude attitude =
  122.                 getAttitudeProvider().getAttitude(this, date, state.getFrame());

  123.         // build raw state
  124.         if (state.isOrbitDefined()) {
  125.             return new SpacecraftState(
  126.                     state.getOrbit(), attitude, state.getMass(),
  127.                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
  128.         } else {
  129.             return new SpacecraftState(
  130.                     state.getAbsPVA(), attitude, state.getMass(),
  131.                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
  132.         }
  133.     }

  134.     @Override
  135.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date,
  136.                                                      final Frame frame) {
  137.         return getPropagator(date).getPVCoordinates(date, frame);
  138.     }

  139.     @Override
  140.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  141.         return getPropagator(date).propagate(date).getOrbit();
  142.     }

  143.     @Override
  144.     public AbsoluteDate getMinDate() {
  145.         return min;
  146.     }

  147.     @Override
  148.     public AbsoluteDate getMaxDate() {
  149.         return max;
  150.     }

  151.     @Override
  152.     protected double getMass(final AbsoluteDate date) {
  153.         return getPropagator(date).propagate(date).getMass();
  154.     }

  155.     @Override
  156.     public SpacecraftState getInitialState() {
  157.         return propagators.firstEntry().getValue().getInitialState();
  158.     }

  159.     @Override
  160.     protected void resetIntermediateState(final SpacecraftState state,
  161.                                           final boolean forward) {
  162.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  163.     }

  164.     @Override
  165.     public void resetInitialState(final SpacecraftState state) {
  166.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  167.     }

  168.     /**
  169.      * Get the propagator to use for the given date.
  170.      *
  171.      * @param date of query
  172.      * @return propagator to use on date.
  173.      */
  174.     private Propagator getPropagator(final AbsoluteDate date) {
  175.         final Entry<AbsoluteDate, ? extends Propagator> entry =
  176.                 propagators.floorEntry(date);
  177.         if (entry != null) {
  178.             return entry.getValue();
  179.         } else {
  180.             // let the first propagator throw the exception
  181.             return propagators.firstEntry().getValue();
  182.         }
  183.     }

  184. }