AbstractPropagator.java

  1. /* Copyright 2002-2019 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() {
  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) {
  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.         // check if the name is already used
  135.         if (isAdditionalStateManaged(additionalStateProvider.getName())) {
  136.             // this additional state is already registered, complain
  137.             throw new OrekitException(OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE,
  138.                                       additionalStateProvider.getName());
  139.         }

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

  142.     }

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

  147.     /** Update state by adding all additional states.
  148.      * @param original original state
  149.      * @return updated state, with all additional states included
  150.           * @see #addAdditionalStateProvider(AdditionalStateProvider)
  151.      */
  152.     protected SpacecraftState updateAdditionalStates(final SpacecraftState original) {

  153.         // start with original state,
  154.         // which may already contain additional states, for example in interpolated ephemerides
  155.         SpacecraftState updated = original;

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

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

  173.         return updated;

  174.     }

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

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

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

  198.     /** Get the step handler.
  199.      * @return step handler
  200.      */
  201.     protected OrekitStepHandler getStepHandler() {
  202.         return stepHandler;
  203.     }

  204.     /** {@inheritDoc} */
  205.     public abstract BoundedPropagator getGeneratedEphemeris();

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

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

  210.     /** {@inheritDoc} */
  211.     public abstract void clearEventsDetectors();

  212.     /** {@inheritDoc} */
  213.     public SpacecraftState propagate(final AbsoluteDate target) {
  214.         if (startDate == null) {
  215.             startDate = getInitialState().getDate();
  216.         }
  217.         return propagate(startDate, target);
  218.     }

  219.     /** {@inheritDoc} */
  220.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
  221.         return propagate(date).getPVCoordinates(frame);
  222.     }

  223. }