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.SpacecraftState;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.TimeStampedPVCoordinates;

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

  42.     /** Constituent propagators. */
  43.     private final NavigableMap<AbsoluteDate, BoundedPropagator> propagators;

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

  68.     /**
  69.      * Helper function for the constructor.
  70.      * @param propagators to consider.
  71.      * @return attitude provider.
  72.      */
  73.     private static AttitudeProvider defaultAttitude(
  74.             final Collection<? extends BoundedPropagator> propagators) {
  75.         // this check is needed here because it can't be before the super() call in the
  76.         // constructor.
  77.         if (propagators.isEmpty()) {
  78.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_PROPAGATORS);
  79.         }
  80.         return new InertialProvider(propagators.iterator().next().getFrame());
  81.     }

  82.     @Override
  83.     protected SpacecraftState basicPropagate(final AbsoluteDate date) {
  84.         // #589 override this method for a performance benefit,
  85.         // getPropagator(date).propagate(date) is only called once

  86.         // do propagation
  87.         final SpacecraftState state = getPropagator(date).propagate(date);

  88.         // evaluate attitude
  89.         final Attitude attitude =
  90.                 getAttitudeProvider().getAttitude(this, date, state.getFrame());

  91.         // build raw state
  92.         if (state.isOrbitDefined()) {
  93.             return new SpacecraftState(
  94.                     state.getOrbit(), attitude, state.getMass(),
  95.                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
  96.         } else {
  97.             return new SpacecraftState(
  98.                     state.getAbsPVA(), attitude, state.getMass(),
  99.                     state.getAdditionalStatesValues(), state.getAdditionalStatesDerivatives());
  100.         }
  101.     }

  102.     @Override
  103.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date,
  104.                                                      final Frame frame) {
  105.         return getPropagator(date).getPVCoordinates(date, frame);
  106.     }

  107.     @Override
  108.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  109.         return getPropagator(date).propagate(date).getOrbit();
  110.     }

  111.     @Override
  112.     public AbsoluteDate getMinDate() {
  113.         return propagators.firstEntry().getValue().getMinDate();
  114.     }

  115.     @Override
  116.     public AbsoluteDate getMaxDate() {
  117.         return propagators.lastEntry().getValue().getMaxDate();
  118.     }

  119.     @Override
  120.     protected double getMass(final AbsoluteDate date) {
  121.         return getPropagator(date).propagate(date).getMass();
  122.     }

  123.     @Override
  124.     public SpacecraftState getInitialState() {
  125.         return propagators.firstEntry().getValue().getInitialState();
  126.     }

  127.     @Override
  128.     protected void resetIntermediateState(final SpacecraftState state,
  129.                                           final boolean forward) {
  130.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  131.     }

  132.     @Override
  133.     public void resetInitialState(final SpacecraftState state) {
  134.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  135.     }

  136.     /**
  137.      * Get the propagator to use for the given date.
  138.      *
  139.      * @param date of query
  140.      * @return propagator to use on date.
  141.      */
  142.     private BoundedPropagator getPropagator(final AbsoluteDate date) {
  143.         final Entry<AbsoluteDate, BoundedPropagator> entry = propagators.floorEntry(date);
  144.         if (entry != null) {
  145.             return entry.getValue();
  146.         } else {
  147.             // let the first propagator throw the exception
  148.             return propagators.firstEntry().getValue();
  149.         }
  150.     }

  151. }