AbstractPropagator.java

  1. /* Copyright 2002-2013 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.errors.PropagationException;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.propagation.events.EventDetector;
  29. import org.orekit.propagation.sampling.OrekitFixedStepHandler;
  30. import org.orekit.propagation.sampling.OrekitStepHandler;
  31. import org.orekit.propagation.sampling.OrekitStepNormalizer;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.PVCoordinates;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  126.     /** {@inheritDoc} */
  127.     public void addAdditionalStateProvider(final AdditionalStateProvider additionalStateProvider)
  128.         throws OrekitException {

  129.         // check if the name is already used
  130.         if (isAdditionalStateManaged(additionalStateProvider.getName())) {
  131.             // this additional state is already registered, complain
  132.             throw new OrekitException(OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE,
  133.                                       additionalStateProvider.getName());
  134.         }

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

  137.     }

  138.     /** {@inheritDoc} */
  139.     public List<AdditionalStateProvider> getAdditionalStateProviders() {
  140.         return Collections.unmodifiableList(additionalStateProviders);
  141.     }

  142.     /** Update state by adding all additional states.
  143.      * @param original original state
  144.      * @return updated state, with all additional states included
  145.      * @exception PropagationException if one of the providers throws one
  146.      * @see #addAdditionalStateProvider(AdditionalStateProvider)
  147.      */
  148.     protected SpacecraftState updateAdditionalStates(final SpacecraftState original)
  149.         throws PropagationException {

  150.         // start with original state,
  151.         // which may already contain additional states, for example in interpolated ephemerides
  152.         SpacecraftState updated = original;

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

  165.         // update the additional states managed by providers
  166.         for (final AdditionalStateProvider provider : additionalStateProviders) {
  167.             updated = updated.addAdditionalState(provider.getName(),
  168.                                                  provider.getAdditionalState(updated));
  169.         }

  170.         return updated;

  171.     }

  172.     /** {@inheritDoc} */
  173.     public boolean isAdditionalStateManaged(final String name) {
  174.         for (final AdditionalStateProvider provider : additionalStateProviders) {
  175.             if (provider.getName().equals(name)) {
  176.                 return true;
  177.             }
  178.         }
  179.         return false;
  180.     }

  181.     /** {@inheritDoc} */
  182.     public String[] getManagedAdditionalStates() {
  183.         final String[] managed = new String[additionalStateProviders.size()];
  184.         for (int i = 0; i < managed.length; ++i) {
  185.             managed[i] = additionalStateProviders.get(i).getName();
  186.         }
  187.         return managed;
  188.     }

  189.     /** Get the fixed step size.
  190.      * @return fixed step size (or NaN if there are no fixed step size).
  191.      */
  192.     protected double getFixedStepSize() {
  193.         return fixedStepSize;
  194.     }

  195.     /** Get the step handler.
  196.      * @return step handler
  197.      */
  198.     protected OrekitStepHandler getStepHandler() {
  199.         return stepHandler;
  200.     }

  201.     /** {@inheritDoc} */
  202.     public abstract BoundedPropagator getGeneratedEphemeris();

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

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

  207.     /** {@inheritDoc} */
  208.     public abstract void clearEventsDetectors();

  209.     /** {@inheritDoc} */
  210.     public SpacecraftState propagate(final AbsoluteDate target) throws PropagationException {
  211.         if (startDate == null) {
  212.             startDate = getInitialState().getDate();
  213.         }
  214.         return propagate(startDate, target);
  215.     }

  216.     /** {@inheritDoc} */
  217.     public PVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame)
  218.         throws OrekitException {
  219.         return propagate(date).getPVCoordinates(frame);
  220.     }

  221. }