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(), state.getAdditionalStates());
  95.         } else {
  96.             return new SpacecraftState(
  97.                     state.getAbsPVA(), attitude, state.getMass(), state.getAdditionalStates());
  98.         }
  99.     }

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

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

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

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

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

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

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

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

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

  149. }