FieldIntegratedEphemeris.java

  1. /* Copyright 2002-2024 CS GROUP
  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.integration;

  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.hipparchus.CalculusFieldElement;
  22. import org.hipparchus.ode.FieldDenseOutputModel;
  23. import org.hipparchus.ode.FieldODEStateAndDerivative;
  24. import org.orekit.errors.OrekitException;
  25. import org.orekit.errors.OrekitMessages;
  26. import org.orekit.frames.Frame;
  27. import org.orekit.orbits.FieldOrbit;
  28. import org.orekit.propagation.FieldAdditionalStateProvider;
  29. import org.orekit.propagation.FieldBoundedPropagator;
  30. import org.orekit.propagation.FieldSpacecraftState;
  31. import org.orekit.propagation.PropagationType;
  32. import org.orekit.propagation.analytical.FieldAbstractAnalyticalPropagator;
  33. import org.orekit.time.FieldAbsoluteDate;
  34. import org.orekit.utils.FieldArrayDictionary;
  35. import org.orekit.utils.ParameterDriver;
  36. import org.orekit.utils.TimeStampedFieldPVCoordinates;

  37. /** This class stores sequentially generated orbital parameters for
  38.  * later retrieval.
  39.  *
  40.  * <p>
  41.  * Instances of this class are built automatically when the {@link
  42.  * org.orekit.propagation.FieldPropagator#getEphemerisGenerator()
  43.  * getEphemerisGenerator} method has been called. They are created when propagation is over.
  44.  * Random access to any intermediate state of the orbit throughout the propagation range is
  45.  * possible afterwards through this object.
  46.  * </p>
  47.  * <p>
  48.  * A typical use case is for numerically integrated orbits, which can be used by
  49.  * algorithms that need to wander around according to their own algorithm without
  50.  * cumbersome tight links with the integrator.
  51.  * </p>
  52.  * <p>
  53.  * As this class implements the {@link org.orekit.propagation.Propagator Propagator}
  54.  * interface, it can itself be used in batch mode to build another instance of the
  55.  * same type. This is however not recommended since it would be a waste of resources.
  56.  * </p>
  57.  * <p>
  58.  * Note that this class stores all intermediate states along with interpolation
  59.  * models, so it may be memory intensive.
  60.  * </p>
  61.  *
  62.  * @see org.orekit.propagation.numerical.NumericalPropagator
  63.  * @author Mathieu Rom&eacute;ro
  64.  * @author Luc Maisonobe
  65.  * @author V&eacute;ronique Pommier-Maurussane
  66.  * @param <T> type of the field elements
  67.  */
  68. public class FieldIntegratedEphemeris <T extends CalculusFieldElement<T>>
  69.     extends FieldAbstractAnalyticalPropagator<T> implements FieldBoundedPropagator<T> {

  70.     /** Event detection requires evaluating the state slightly before / past an event. */
  71.     private static final double EXTRAPOLATION_TOLERANCE = 1.0;

  72.     /** Mapper between raw double components and spacecraft state. */
  73.     private final FieldStateMapper<T> mapper;

  74.     /** Type of orbit to output (mean or osculating).
  75.      * <p>
  76.      * This is used only in the case of semianalitical propagators where there is a clear separation between
  77.      * mean and short periodic elements. It is ignored by the Numerical propagator.
  78.      * </p>
  79.      */
  80.     private PropagationType type;

  81.     /** Start date of the integration (can be min or max). */
  82.     private final FieldAbsoluteDate<T> startDate;

  83.     /** First date of the range. */
  84.     private final FieldAbsoluteDate<T> minDate;

  85.     /** Last date of the range. */
  86.     private final FieldAbsoluteDate<T> maxDate;

  87.     /** Underlying raw mathematical model. */
  88.     private FieldDenseOutputModel<T> model;

  89.     /** Unmanaged additional states that must be simply copied. */
  90.     private final FieldArrayDictionary<T> unmanaged;

  91.     /** Names of additional equations.
  92.      * @since 11.2
  93.      */
  94.     private final String[] equations;

  95.     /** Dimensions of additional equations.
  96.      * @since 11.2
  97.      */
  98.     private final int[] dimensions;

  99.     /** Creates a new instance of IntegratedEphemeris.
  100.      * @param startDate Start date of the integration (can be minDate or maxDate)
  101.      * @param minDate first date of the range
  102.      * @param maxDate last date of the range
  103.      * @param mapper mapper between raw double components and spacecraft state
  104.      * @param type type of orbit to output (mean or osculating)
  105.      * @param model underlying raw mathematical model
  106.      * @param unmanaged unmanaged additional states that must be simply copied
  107.      * @param providers generators for pre-integrated states
  108.      * @param equations names of additional equations
  109.      * @param dimensions dimensions of additional equations
  110.      * @since 11.2
  111.      */
  112.     public FieldIntegratedEphemeris(final FieldAbsoluteDate<T> startDate,
  113.                                     final FieldAbsoluteDate<T> minDate, final FieldAbsoluteDate<T> maxDate,
  114.                                     final FieldStateMapper<T> mapper, final PropagationType type,
  115.                                     final FieldDenseOutputModel<T> model,
  116.                                     final FieldArrayDictionary<T> unmanaged,
  117.                                     final List<FieldAdditionalStateProvider<T>> providers,
  118.                                     final String[] equations, final int[] dimensions) {

  119.         super(startDate.getField(), mapper.getAttitudeProvider());

  120.         this.startDate = startDate;
  121.         this.minDate   = minDate;
  122.         this.maxDate   = maxDate;
  123.         this.mapper    = mapper;
  124.         this.type      = type;
  125.         this.model     = model;
  126.         this.unmanaged = unmanaged;

  127.         // set up the pre-integrated providers
  128.         for (final FieldAdditionalStateProvider<T> provider : providers) {
  129.             addAdditionalStateProvider(provider);
  130.         }

  131.         this.equations  = equations.clone();
  132.         this.dimensions = dimensions.clone();

  133.         // set up initial state
  134.         super.resetInitialState(getInitialState());

  135.     }

  136.     /** Interpolate the model at some date.
  137.      * @param date desired interpolation date
  138.      * @return state interpolated at date
  139.           * of supported range
  140.      */
  141.     private FieldODEStateAndDerivative<T> getInterpolatedState(final FieldAbsoluteDate<T> date) {

  142.         // compare using double precision instead of FieldAbsoluteDate<T>.compareTo(...)
  143.         // because time is expressed as a double when searching for events
  144.         if (date.compareTo(minDate.shiftedBy(-EXTRAPOLATION_TOLERANCE)) < 0) {
  145.             // date is outside of supported range
  146.             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_BEFORE,
  147.                     date, minDate, maxDate, minDate.durationFrom(date).getReal());
  148.         }
  149.         if (date.compareTo(maxDate.shiftedBy(EXTRAPOLATION_TOLERANCE)) > 0) {
  150.             // date is outside of supported range
  151.             throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_AFTER,
  152.                     date, minDate, maxDate, date.durationFrom(maxDate).getReal());
  153.         }

  154.         return model.getInterpolatedState(date.durationFrom(startDate));

  155.     }

  156.     /** {@inheritDoc} */
  157.     @Override
  158.     protected FieldSpacecraftState<T> basicPropagate(final FieldAbsoluteDate<T> date) {
  159.         final FieldODEStateAndDerivative<T> os = getInterpolatedState(date);
  160.         FieldSpacecraftState<T> state = mapper.mapArrayToState(mapper.mapDoubleToDate(os.getTime(), date),
  161.                                                                os.getPrimaryState(), os.getPrimaryDerivative(),
  162.                                                                type);
  163.         for (FieldArrayDictionary<T>.Entry initial : unmanaged.getData()) {
  164.             state = state.addAdditionalState(initial.getKey(), initial.getValue());
  165.         }
  166.         return state;
  167.     }

  168.     /** {@inheritDoc} */
  169.     @Override
  170.     protected FieldOrbit<T> propagateOrbit(final FieldAbsoluteDate<T> date, final T[] parameters) {
  171.         return basicPropagate(date).getOrbit();
  172.     }

  173.     /** {@inheritDoc} */
  174.     @Override
  175.     protected T getMass(final FieldAbsoluteDate<T> date) {
  176.         return basicPropagate(date).getMass();
  177.     }

  178.     /** {@inheritDoc} */
  179.     @Override
  180.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> date, final Frame frame) {
  181.         return propagate(date).getPVCoordinates(frame);
  182.     }

  183.     /** Get the first date of the range.
  184.      * @return the first date of the range
  185.      */
  186.     @Override
  187.     public FieldAbsoluteDate<T> getMinDate() {
  188.         return minDate;
  189.     }

  190.     /** Get the last date of the range.
  191.      * @return the last date of the range
  192.      */
  193.     @Override
  194.     public FieldAbsoluteDate<T> getMaxDate() {
  195.         return maxDate;
  196.     }

  197.     @Override
  198.     public Frame getFrame() {
  199.         return this.mapper.getFrame();
  200.     }

  201.     /** {@inheritDoc} */
  202.     @Override
  203.     public void resetInitialState(final FieldSpacecraftState<T> state) {
  204.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  205.     }

  206.     /** {@inheritDoc} */
  207.     @Override
  208.     protected void resetIntermediateState(final FieldSpacecraftState<T> state, final boolean forward) {
  209.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  210.     }

  211.     /** {@inheritDoc} */
  212.     @Override
  213.     public FieldSpacecraftState<T> getInitialState() {
  214.         return updateAdditionalStates(basicPropagate(getMinDate()));
  215.     }

  216.     /** {@inheritDoc} */
  217.     @Override
  218.     protected FieldSpacecraftState<T> updateAdditionalStates(final FieldSpacecraftState<T> original) {

  219.         FieldSpacecraftState<T> updated = super.updateAdditionalStates(original);

  220.         if (equations.length > 0) {
  221.             final FieldODEStateAndDerivative<T> osd                = getInterpolatedState(updated.getDate());
  222.             final T[]                           combinedState      = osd.getSecondaryState(1);
  223.             final T[]                           combinedDerivative = osd.getSecondaryDerivative(1);
  224.             int index = 0;
  225.             for (int i = 0; i < equations.length; ++i) {
  226.                 final T[] state      = Arrays.copyOfRange(combinedState,      index, index + dimensions[i]);
  227.                 final T[] derivative = Arrays.copyOfRange(combinedDerivative, index, index + dimensions[i]);
  228.                 updated = updated.
  229.                           addAdditionalState(equations[i], state).
  230.                           addAdditionalStateDerivative(equations[i], derivative);
  231.                 index += dimensions[i];
  232.             }
  233.         }

  234.         return updated;

  235.     }

  236.     /** {@inheritDoc} */
  237.     @Override
  238.     public List<ParameterDriver> getParametersDrivers() {
  239.         // Integrated Ephemeris propagation model does not have parameter drivers.
  240.         return Collections.emptyList();
  241.     }

  242. }