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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  134.     }

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

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

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

  154.     }

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

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

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

  177.     /** Get the first date of the range.
  178.      * @return the first date of the range
  179.      */
  180.     @Override
  181.     public FieldAbsoluteDate<T> getMinDate() {
  182.         return minDate;
  183.     }

  184.     /** Get the last date of the range.
  185.      * @return the last date of the range
  186.      */
  187.     @Override
  188.     public FieldAbsoluteDate<T> getMaxDate() {
  189.         return maxDate;
  190.     }

  191.     @Override
  192.     public Frame getFrame() {
  193.         return this.mapper.getFrame();
  194.     }

  195.     /** {@inheritDoc} */
  196.     @Override
  197.     public void resetInitialState(final FieldSpacecraftState<T> state) {
  198.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  199.     }

  200.     /** {@inheritDoc} */
  201.     @Override
  202.     protected void resetIntermediateState(final FieldSpacecraftState<T> state, final boolean forward) {
  203.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  204.     }

  205.     /** {@inheritDoc} */
  206.     @Override
  207.     public FieldSpacecraftState<T> getInitialState() {
  208.         return updateAdditionalStates(basicPropagate(getMinDate()));
  209.     }

  210.     /** {@inheritDoc} */
  211.     @Override
  212.     protected FieldSpacecraftState<T> updateAdditionalStates(final FieldSpacecraftState<T> original) {

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

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

  228.         return updated;

  229.     }

  230.     /** {@inheritDoc} */
  231.     @Override
  232.     public List<ParameterDriver> getParametersDrivers() {
  233.         // Integrated Ephemeris propagation model does not have parameter drivers.
  234.         return Collections.emptyList();
  235.     }

  236. }