AbstractPropagator.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Map;

  23. import org.orekit.attitudes.AttitudeProvider;
  24. import org.orekit.errors.OrekitException;
  25. import org.orekit.errors.OrekitMessages;
  26. import org.orekit.frames.Frame;
  27. import org.orekit.propagation.events.EventDetector;
  28. import org.orekit.propagation.sampling.OrekitFixedStepHandler;
  29. import org.orekit.propagation.sampling.OrekitStepHandler;
  30. import org.orekit.propagation.sampling.OrekitStepNormalizer;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.TimeStampedPVCoordinates;

  33. /** Common handling of {@link Propagator} methods for analytical propagators.
  34.  * <p>
  35.  * This abstract class allows to provide easily the full set of {@link Propagator}
  36.  * methods, including all propagation modes support and discrete events support for
  37.  * any simple propagation method.
  38.  * </p>
  39.  * @author Luc Maisonobe
  40.  */
  41. public abstract class AbstractPropagator implements Propagator {

  42.     /** Propagation mode. */
  43.     private int mode;

  44.     /** Fixed step size. */
  45.     private double fixedStepSize;

  46.     /** Step handler. */
  47.     private OrekitStepHandler stepHandler;

  48.     /** Start date. */
  49.     private AbsoluteDate startDate;

  50.     /** Attitude provider. */
  51.     private AttitudeProvider attitudeProvider;

  52.     /** Additional state providers. */
  53.     private final List<AdditionalStateProvider> additionalStateProviders;

  54.     /** Initial state. */
  55.     private SpacecraftState initialState;

  56.     /** Build a new instance.
  57.      */
  58.     protected AbstractPropagator() {
  59.         mode                     = SLAVE_MODE;
  60.         stepHandler              = null;
  61.         fixedStepSize            = Double.NaN;
  62.         additionalStateProviders = new ArrayList<AdditionalStateProvider>();
  63.     }

  64.     /** Set a start date.
  65.      * @param startDate start date
  66.      */
  67.     protected void setStartDate(final AbsoluteDate startDate) {
  68.         this.startDate = startDate;
  69.     }

  70.     /** Get the start date.
  71.      * @return start date
  72.      */
  73.     protected AbsoluteDate getStartDate() {
  74.         return startDate;
  75.     }

  76.     /**  {@inheritDoc} */
  77.     public AttitudeProvider getAttitudeProvider() {
  78.         return attitudeProvider;
  79.     }

  80.     /**  {@inheritDoc} */
  81.     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
  82.         this.attitudeProvider = attitudeProvider;
  83.     }

  84.     /** {@inheritDoc} */
  85.     public SpacecraftState getInitialState() throws OrekitException {
  86.         return initialState;
  87.     }

  88.     /** {@inheritDoc} */
  89.     public int getMode() {
  90.         return mode;
  91.     }

  92.     /** {@inheritDoc} */
  93.     public Frame getFrame() {
  94.         return initialState.getFrame();
  95.     }

  96.     /** {@inheritDoc} */
  97.     public void resetInitialState(final SpacecraftState state) throws OrekitException {
  98.         initialState = state;
  99.         setStartDate(state.getDate());
  100.     }

  101.     /** {@inheritDoc} */
  102.     public void setSlaveMode() {
  103.         mode          = SLAVE_MODE;
  104.         stepHandler   = null;
  105.         fixedStepSize = Double.NaN;
  106.     }

  107.     /** {@inheritDoc} */
  108.     public void setMasterMode(final double h,
  109.                               final OrekitFixedStepHandler handler) {
  110.         setMasterMode(new OrekitStepNormalizer(h, handler));
  111.         fixedStepSize = h;
  112.     }

  113.     /** {@inheritDoc} */
  114.     public void setMasterMode(final OrekitStepHandler handler) {
  115.         mode          = MASTER_MODE;
  116.         stepHandler   = handler;
  117.         fixedStepSize = Double.NaN;
  118.     }

  119.     /** {@inheritDoc} */
  120.     public void setEphemerisMode() {
  121.         mode          = EPHEMERIS_GENERATION_MODE;
  122.         stepHandler   = null;
  123.         fixedStepSize = Double.NaN;
  124.     }

  125.     /** {@inheritDoc} */
  126.     @Override
  127.     public void setEphemerisMode(final OrekitStepHandler handler) {
  128.         mode          = EPHEMERIS_GENERATION_MODE;
  129.         stepHandler   = handler;
  130.         fixedStepSize = Double.NaN;
  131.     }

  132.     /** {@inheritDoc} */
  133.     public void addAdditionalStateProvider(final AdditionalStateProvider additionalStateProvider)
  134.         throws OrekitException {

  135.         // check if the name is already used
  136.         if (isAdditionalStateManaged(additionalStateProvider.getName())) {
  137.             // this additional state is already registered, complain
  138.             throw new OrekitException(OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE,
  139.                                       additionalStateProvider.getName());
  140.         }

  141.         // this is really a new name, add it
  142.         additionalStateProviders.add(additionalStateProvider);

  143.     }

  144.     /** {@inheritDoc} */
  145.     public List<AdditionalStateProvider> getAdditionalStateProviders() {
  146.         return Collections.unmodifiableList(additionalStateProviders);
  147.     }

  148.     /** Update state by adding all additional states.
  149.      * @param original original state
  150.      * @return updated state, with all additional states included
  151.      * @exception OrekitException if one of the providers throws one
  152.      * @see #addAdditionalStateProvider(AdditionalStateProvider)
  153.      */
  154.     protected SpacecraftState updateAdditionalStates(final SpacecraftState original)
  155.         throws OrekitException {

  156.         // start with original state,
  157.         // which may already contain additional states, for example in interpolated ephemerides
  158.         SpacecraftState updated = original;

  159.         if (initialState != null) {
  160.             // there is an initial state
  161.             // (null initial states occur for example in interpolated ephemerides)
  162.             // copy the additional states present in initialState but otherwise not managed
  163.             for (final Map.Entry<String, double[]> initial : initialState.getAdditionalStates().entrySet()) {
  164.                 if (!isAdditionalStateManaged(initial.getKey())) {
  165.                     // this additional state was in the initial state, but is unknown to the propagator
  166.                     // we simply copy its initial value as is
  167.                     updated = updated.addAdditionalState(initial.getKey(), initial.getValue());
  168.                 }
  169.             }
  170.         }

  171.         // update the additional states managed by providers
  172.         for (final AdditionalStateProvider provider : additionalStateProviders) {
  173.             updated = updated.addAdditionalState(provider.getName(),
  174.                                                  provider.getAdditionalState(updated));
  175.         }

  176.         return updated;

  177.     }

  178.     /** {@inheritDoc} */
  179.     public boolean isAdditionalStateManaged(final String name) {
  180.         for (final AdditionalStateProvider provider : additionalStateProviders) {
  181.             if (provider.getName().equals(name)) {
  182.                 return true;
  183.             }
  184.         }
  185.         return false;
  186.     }

  187.     /** {@inheritDoc} */
  188.     public String[] getManagedAdditionalStates() {
  189.         final String[] managed = new String[additionalStateProviders.size()];
  190.         for (int i = 0; i < managed.length; ++i) {
  191.             managed[i] = additionalStateProviders.get(i).getName();
  192.         }
  193.         return managed;
  194.     }

  195.     /** Get the fixed step size.
  196.      * @return fixed step size (or NaN if there are no fixed step size).
  197.      */
  198.     protected double getFixedStepSize() {
  199.         return fixedStepSize;
  200.     }

  201.     /** Get the step handler.
  202.      * @return step handler
  203.      */
  204.     protected OrekitStepHandler getStepHandler() {
  205.         return stepHandler;
  206.     }

  207.     /** {@inheritDoc} */
  208.     public abstract BoundedPropagator getGeneratedEphemeris();

  209.     /** {@inheritDoc} */
  210.     public abstract <T extends EventDetector> void addEventDetector(T detector);

  211.     /** {@inheritDoc} */
  212.     public abstract Collection<EventDetector> getEventsDetectors();

  213.     /** {@inheritDoc} */
  214.     public abstract void clearEventsDetectors();

  215.     /** {@inheritDoc} */
  216.     public SpacecraftState propagate(final AbsoluteDate target) throws OrekitException {
  217.         if (startDate == null) {
  218.             startDate = getInitialState().getDate();
  219.         }
  220.         return propagate(startDate, target);
  221.     }

  222.     /** {@inheritDoc} */
  223.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame)
  224.         throws OrekitException {
  225.         return propagate(date).getPVCoordinates(frame);
  226.     }

  227. }