FieldDSSTPropagator.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.semianalytical.dsst;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.ode.FieldODEIntegrator;
  21. import org.hipparchus.ode.FieldODEStateAndDerivative;
  22. import org.hipparchus.ode.sampling.FieldODEStateInterpolator;
  23. import org.hipparchus.ode.sampling.FieldODEStepHandler;
  24. import org.hipparchus.util.FastMath;
  25. import org.hipparchus.util.MathArrays;
  26. import org.hipparchus.util.MathUtils;
  27. import org.orekit.annotation.DefaultDataContext;
  28. import org.orekit.attitudes.AttitudeProvider;
  29. import org.orekit.attitudes.FieldAttitude;
  30. import org.orekit.data.DataContext;
  31. import org.orekit.errors.OrekitException;
  32. import org.orekit.errors.OrekitInternalError;
  33. import org.orekit.errors.OrekitMessages;
  34. import org.orekit.frames.Frame;
  35. import org.orekit.orbits.FieldEquinoctialOrbit;
  36. import org.orekit.orbits.FieldOrbit;
  37. import org.orekit.orbits.OrbitType;
  38. import org.orekit.orbits.PositionAngleType;
  39. import org.orekit.propagation.FieldSpacecraftState;
  40. import org.orekit.propagation.PropagationType;
  41. import org.orekit.propagation.Propagator;
  42. import org.orekit.propagation.SpacecraftState;
  43. import org.orekit.propagation.integration.FieldAbstractIntegratedPropagator;
  44. import org.orekit.propagation.integration.FieldStateMapper;
  45. import org.orekit.propagation.numerical.FieldNumericalPropagator;
  46. import org.orekit.propagation.semianalytical.dsst.forces.DSSTForceModel;
  47. import org.orekit.propagation.semianalytical.dsst.forces.DSSTNewtonianAttraction;
  48. import org.orekit.propagation.semianalytical.dsst.forces.FieldShortPeriodTerms;
  49. import org.orekit.propagation.semianalytical.dsst.utilities.FieldAuxiliaryElements;
  50. import org.orekit.propagation.semianalytical.dsst.utilities.FieldFixedNumberInterpolationGrid;
  51. import org.orekit.propagation.semianalytical.dsst.utilities.FieldInterpolationGrid;
  52. import org.orekit.propagation.semianalytical.dsst.utilities.FieldMaxGapInterpolationGrid;
  53. import org.orekit.time.AbsoluteDate;
  54. import org.orekit.time.FieldAbsoluteDate;
  55. import org.orekit.utils.FieldArrayDictionary;
  56. import org.orekit.utils.ParameterDriver;
  57. import org.orekit.utils.ParameterObserver;
  58. import org.orekit.utils.TimeSpanMap;

  59. import java.util.ArrayList;
  60. import java.util.Arrays;
  61. import java.util.Collection;
  62. import java.util.Collections;
  63. import java.util.HashSet;
  64. import java.util.List;
  65. import java.util.Set;

  66. /**
  67.  * This class propagates {@link org.orekit.orbits.FieldOrbit orbits} using the DSST theory.
  68.  * <p>
  69.  * Whereas analytical propagators are configured only thanks to their various
  70.  * constructors and can be used immediately after construction, such a semianalytical
  71.  * propagator configuration involves setting several parameters between construction
  72.  * time and propagation time, just as numerical propagators.
  73.  * </p>
  74.  * <p>
  75.  * The configuration parameters that can be set are:
  76.  * </p>
  77.  * <ul>
  78.  * <li>the initial spacecraft state ({@link #setInitialState(FieldSpacecraftState)})</li>
  79.  * <li>the various force models ({@link #addForceModel(DSSTForceModel)},
  80.  * {@link #removeForceModels()})</li>
  81.  * <li>the discrete events that should be triggered during propagation (
  82.  * {@link #addEventDetector(org.orekit.propagation.events.FieldEventDetector)},
  83.  * {@link #clearEventsDetectors()})</li>
  84.  * <li>the binding logic with the rest of the application ({@link #getMultiplexer()})</li>
  85.  * </ul>
  86.  * <p>
  87.  * From these configuration parameters, only the initial state is mandatory.
  88.  * The default propagation settings are in {@link OrbitType#EQUINOCTIAL equinoctial}
  89.  * parameters with {@link PositionAngleType#TRUE true} longitude argument.
  90.  * The central attraction coefficient used to define the initial orbit will be used.
  91.  * However, specifying only the initial state would mean the propagator would use
  92.  * only Keplerian forces. In this case, the simpler
  93.  * {@link org.orekit.propagation.analytical.KeplerianPropagator KeplerianPropagator}
  94.  * class would be more effective.
  95.  * </p>
  96.  * <p>
  97.  * The underlying numerical integrator set up in the constructor may also have
  98.  * its own configuration parameters. Typical configuration parameters for adaptive
  99.  * stepsize integrators are the min, max and perhaps start step size as well as
  100.  * the absolute and/or relative errors thresholds.
  101.  * </p>
  102.  * <p>
  103.  * The state that is seen by the integrator is a simple six elements double array.
  104.  * These six elements are:
  105.  * <ul>
  106.  * <li>the {@link org.orekit.orbits.FieldEquinoctialOrbit equinoctial orbit parameters}
  107.  * (a, e<sub>x</sub>, e<sub>y</sub>, h<sub>x</sub>, h<sub>y</sub>, λ<sub>m</sub>)
  108.  * in meters and radians,</li>
  109.  * </ul>
  110.  *
  111.  * <p>By default, at the end of the propagation, the propagator resets the initial state to the final state,
  112.  * thus allowing a new propagation to be started from there without recomputing the part already performed.
  113.  * This behaviour can be chenged by calling {@link #setResetAtEnd(boolean)}.
  114.  * </p>
  115.  * <p>Beware the same instance cannot be used simultaneously by different threads, the class is <em>not</em>
  116.  * thread-safe.</p>
  117.  *
  118.  * @see FieldSpacecraftState
  119.  * @see DSSTForceModel
  120.  * @author Romain Di Costanzo
  121.  * @author Pascal Parraud
  122.  * @since 10.0
  123.  * @param <T> type of the field elements
  124.  */
  125. public class FieldDSSTPropagator<T extends CalculusFieldElement<T>> extends FieldAbstractIntegratedPropagator<T>  {

  126.     /** Retrograde factor I.
  127.      *  <p>
  128.      *  DSST model needs equinoctial orbit as internal representation.
  129.      *  Classical equinoctial elements have discontinuities when inclination
  130.      *  is close to zero. In this representation, I = +1. <br>
  131.      *  To avoid this discontinuity, another representation exists and equinoctial
  132.      *  elements can be expressed in a different way, called "retrograde" orbit.
  133.      *  This implies I = -1. <br>
  134.      *  As Orekit doesn't implement the retrograde orbit, I is always set to +1.
  135.      *  But for the sake of consistency with the theory, the retrograde factor
  136.      *  has been kept in the formulas.
  137.      *  </p>
  138.      */
  139.     private static final int I = 1;

  140.     /** Number of grid points per integration step to be used in interpolation of short periodics coefficients.*/
  141.     private static final int INTERPOLATION_POINTS_PER_STEP = 3;

  142.     /** Default value for epsilon. */
  143.     private static final double EPSILON_DEFAULT = 1.0e-13;

  144.     /** Default value for maxIterations. */
  145.     private static final int MAX_ITERATIONS_DEFAULT = 200;

  146.     /** Flag specifying whether the initial orbital state is given with osculating elements. */
  147.     private boolean initialIsOsculating;

  148.     /** Field used by this class.*/
  149.     private final Field<T> field;

  150.     /** Force models used to compute short periodic terms. */
  151.     private final transient List<DSSTForceModel> forceModels;

  152.     /** State mapper holding the force models. */
  153.     private FieldMeanPlusShortPeriodicMapper mapper;

  154.     /** Generator for the interpolation grid. */
  155.     private FieldInterpolationGrid<T> interpolationgrid;

  156.     /** Create a new instance of DSSTPropagator.
  157.      *  <p>
  158.      *  After creation, there are no perturbing forces at all.
  159.      *  This means that if {@link #addForceModel addForceModel}
  160.      *  is not called after creation, the integrated orbit will
  161.      *  follow a Keplerian evolution only.
  162.      *  </p>
  163.      *
  164.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  165.      *
  166.      *  @param field field used by default
  167.      *  @param integrator numerical integrator to use for propagation.
  168.      *  @param propagationType type of orbit to output (mean or osculating).
  169.      * @see #FieldDSSTPropagator(Field, FieldODEIntegrator, PropagationType,
  170.      * AttitudeProvider)
  171.      */
  172.     @DefaultDataContext
  173.     public FieldDSSTPropagator(final Field<T> field, final FieldODEIntegrator<T> integrator, final PropagationType propagationType) {
  174.         this(field, integrator, propagationType,
  175.                 Propagator.getDefaultLaw(DataContext.getDefault().getFrames()));
  176.     }

  177.     /** Create a new instance of DSSTPropagator.
  178.      *  <p>
  179.      *  After creation, there are no perturbing forces at all.
  180.      *  This means that if {@link #addForceModel addForceModel}
  181.      *  is not called after creation, the integrated orbit will
  182.      *  follow a Keplerian evolution only.
  183.      *  </p>
  184.      * @param field field used by default
  185.      *  @param integrator numerical integrator to use for propagation.
  186.      * @param propagationType type of orbit to output (mean or osculating).
  187.      * @param attitudeProvider attitude law to use.
  188.      * @since 10.1
  189.      */
  190.     public FieldDSSTPropagator(final Field<T> field,
  191.                                final FieldODEIntegrator<T> integrator,
  192.                                final PropagationType propagationType,
  193.                                final AttitudeProvider attitudeProvider) {
  194.         super(field, integrator, propagationType);
  195.         this.field  = field;
  196.         forceModels = new ArrayList<>();
  197.         initMapper(field);
  198.         // DSST uses only equinoctial orbits and mean longitude argument
  199.         setOrbitType(OrbitType.EQUINOCTIAL);
  200.         setPositionAngleType(PositionAngleType.MEAN);
  201.         setAttitudeProvider(attitudeProvider);
  202.         setInterpolationGridToFixedNumberOfPoints(INTERPOLATION_POINTS_PER_STEP);
  203.     }

  204.     /** Create a new instance of DSSTPropagator.
  205.      *  <p>
  206.      *  After creation, there are no perturbing forces at all.
  207.      *  This means that if {@link #addForceModel addForceModel}
  208.      *  is not called after creation, the integrated orbit will
  209.      *  follow a Keplerian evolution only. Only the mean orbits
  210.      *  will be generated.
  211.      *  </p>
  212.      *
  213.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  214.      *
  215.      *  @param field fied used by default
  216.      *  @param integrator numerical integrator to use for propagation.
  217.      * @see #FieldDSSTPropagator(Field, FieldODEIntegrator, AttitudeProvider)
  218.      */
  219.     @DefaultDataContext
  220.     public FieldDSSTPropagator(final Field<T> field, final FieldODEIntegrator<T> integrator) {
  221.         this(field, integrator,
  222.                 Propagator.getDefaultLaw(DataContext.getDefault().getFrames()));
  223.     }

  224.     /** Create a new instance of DSSTPropagator.
  225.      *  <p>
  226.      *  After creation, there are no perturbing forces at all.
  227.      *  This means that if {@link #addForceModel addForceModel}
  228.      *  is not called after creation, the integrated orbit will
  229.      *  follow a Keplerian evolution only. Only the mean orbits
  230.      *  will be generated.
  231.      *  </p>
  232.      * @param field fied used by default
  233.      *  @param integrator numerical integrator to use for propagation.
  234.      * @param attitudeProvider attitude law to use.
  235.      * @since 10.1
  236.      */
  237.     public FieldDSSTPropagator(final Field<T> field,
  238.                                final FieldODEIntegrator<T> integrator,
  239.                                final AttitudeProvider attitudeProvider) {
  240.         super(field, integrator, PropagationType.MEAN);
  241.         this.field  = field;
  242.         forceModels = new ArrayList<>();
  243.         initMapper(field);
  244.         // DSST uses only equinoctial orbits and mean longitude argument
  245.         setOrbitType(OrbitType.EQUINOCTIAL);
  246.         setPositionAngleType(PositionAngleType.MEAN);
  247.         setAttitudeProvider(attitudeProvider);
  248.         setInterpolationGridToFixedNumberOfPoints(INTERPOLATION_POINTS_PER_STEP);
  249.     }

  250.     /** Set the central attraction coefficient μ.
  251.      * <p>
  252.      * Setting the central attraction coefficient is
  253.      * equivalent to {@link #addForceModel(DSSTForceModel) add}
  254.      * a {@link DSSTNewtonianAttraction} force model.
  255.      * </p>
  256.     * @param mu central attraction coefficient (m³/s²)
  257.     * @see #addForceModel(DSSTForceModel)
  258.     * @see #getAllForceModels()
  259.     */
  260.     public void setMu(final T mu) {
  261.         addForceModel(new DSSTNewtonianAttraction(mu.getReal()));
  262.     }

  263.     /** Set the central attraction coefficient μ only in upper class.
  264.      * @param mu central attraction coefficient (m³/s²)
  265.      */
  266.     private void superSetMu(final T mu) {
  267.         super.setMu(mu);
  268.     }

  269.     /** Check if Newtonian attraction force model is available.
  270.      * <p>
  271.      * Newtonian attraction is always the last force model in the list.
  272.      * </p>
  273.      * @return true if Newtonian attraction force model is available
  274.      */
  275.     private boolean hasNewtonianAttraction() {
  276.         final int last = forceModels.size() - 1;
  277.         return last >= 0 && forceModels.get(last) instanceof DSSTNewtonianAttraction;
  278.     }

  279.     /** Set the initial state with osculating orbital elements.
  280.      *  @param initialState initial state (defined with osculating elements)
  281.      */
  282.     public void setInitialState(final FieldSpacecraftState<T> initialState) {
  283.         setInitialState(initialState, PropagationType.OSCULATING);
  284.     }

  285.     /** Set the initial state.
  286.      *  @param initialState initial state
  287.      *  @param stateType defined if the orbital state is defined with osculating or mean elements
  288.      */
  289.     public void setInitialState(final FieldSpacecraftState<T> initialState,
  290.                                 final PropagationType stateType) {
  291.         switch (stateType) {
  292.             case MEAN:
  293.                 initialIsOsculating = false;
  294.                 break;
  295.             case OSCULATING:
  296.                 initialIsOsculating = true;
  297.                 break;
  298.             default:
  299.                 throw new OrekitInternalError(null);
  300.         }
  301.         resetInitialState(initialState);
  302.     }

  303.     /** Reset the initial state.
  304.      *
  305.      *  @param state new initial state
  306.      */
  307.     @Override
  308.     public void resetInitialState(final FieldSpacecraftState<T> state) {
  309.         super.resetInitialState(state);
  310.         if (!hasNewtonianAttraction()) {
  311.             // use the state to define central attraction
  312.             setMu(state.getMu());
  313.         }
  314.         super.setStartDate(state.getDate());
  315.     }

  316.     /** Set the selected short periodic coefficients that must be stored as additional states.
  317.      * @param selectedCoefficients short periodic coefficients that must be stored as additional states
  318.      * (null means no coefficients are selected, empty set means all coefficients are selected)
  319.      */
  320.     public void setSelectedCoefficients(final Set<String> selectedCoefficients) {
  321.         mapper.setSelectedCoefficients(selectedCoefficients == null ?
  322.                                        null : new HashSet<String>(selectedCoefficients));
  323.     }

  324.     /** Get the selected short periodic coefficients that must be stored as additional states.
  325.      * @return short periodic coefficients that must be stored as additional states
  326.      * (null means no coefficients are selected, empty set means all coefficients are selected)
  327.      */
  328.     public Set<String> getSelectedCoefficients() {
  329.         final Set<String> set = mapper.getSelectedCoefficients();
  330.         return set == null ? null : Collections.unmodifiableSet(set);
  331.     }

  332.     /** Check if the initial state is provided in osculating elements.
  333.      * @return true if initial state is provided in osculating elements
  334.      */
  335.     public boolean initialIsOsculating() {
  336.         return initialIsOsculating;
  337.     }

  338.     /** Set the interpolation grid generator.
  339.      * <p>
  340.      * The generator will create an interpolation grid with a fixed
  341.      * number of points for each mean element integration step.
  342.      * </p>
  343.      * <p>
  344.      * If neither {@link #setInterpolationGridToFixedNumberOfPoints(int)}
  345.      * nor {@link #setInterpolationGridToMaxTimeGap(CalculusFieldElement)} has been called,
  346.      * by default the propagator is set as to 3 interpolations points per step.
  347.      * </p>
  348.      * @param interpolationPoints number of interpolation points at
  349.      * each integration step
  350.      * @see #setInterpolationGridToMaxTimeGap(CalculusFieldElement)
  351.      * @since 7.1
  352.      */
  353.     public void setInterpolationGridToFixedNumberOfPoints(final int interpolationPoints) {
  354.         interpolationgrid = new FieldFixedNumberInterpolationGrid<>(field, interpolationPoints);
  355.     }

  356.     /** Set the interpolation grid generator.
  357.      * <p>
  358.      * The generator will create an interpolation grid with a maximum
  359.      * time gap between interpolation points.
  360.      * </p>
  361.      * <p>
  362.      * If neither {@link #setInterpolationGridToFixedNumberOfPoints(int)}
  363.      * nor {@link #setInterpolationGridToMaxTimeGap(CalculusFieldElement)} has been called,
  364.      * by default the propagator is set as to 3 interpolations points per step.
  365.      * </p>
  366.      * @param maxGap maximum time gap between interpolation points (seconds)
  367.      * @see #setInterpolationGridToFixedNumberOfPoints(int)
  368.      * @since 7.1
  369.      */
  370.     public void setInterpolationGridToMaxTimeGap(final T maxGap) {
  371.         interpolationgrid = new FieldMaxGapInterpolationGrid<>(field, maxGap);
  372.     }

  373.     /** Add a force model to the global perturbation model.
  374.      *  <p>
  375.      *  If this method is not called at all,
  376.      *  the integrated orbit will follow a Keplerian evolution only.
  377.      *  </p>
  378.      *  @param force perturbing {@link DSSTForceModel force} to add
  379.      *  @see #removeForceModels()
  380.      *  @see #setMu(CalculusFieldElement)
  381.      */
  382.     public void addForceModel(final DSSTForceModel force) {

  383.         if (force instanceof DSSTNewtonianAttraction) {
  384.             // we want to add the central attraction force model

  385.             try {
  386.                 // ensure we are notified of any mu change
  387.                 force.getParametersDrivers().get(0).addObserver(new ParameterObserver() {
  388.                     /** {@inheritDoc} */
  389.                     @Override
  390.                     public void valueChanged(final double previousValue, final ParameterDriver driver, final AbsoluteDate date) {
  391.                         // mu PDriver should have only 1 span
  392.                         superSetMu(field.getZero().newInstance(driver.getValue()));
  393.                     }
  394.                     /** {@inheritDoc} */
  395.                     @Override
  396.                     public void valueSpanMapChanged(final TimeSpanMap<Double> previousValue, final ParameterDriver driver) {
  397.                         // mu PDriver should have only 1 span
  398.                         superSetMu(field.getZero().newInstance(driver.getValue()));
  399.                     }
  400.                 });
  401.             } catch (OrekitException oe) {
  402.                 // this should never happen
  403.                 throw new OrekitInternalError(oe);
  404.             }

  405.             if (hasNewtonianAttraction()) {
  406.                 // there is already a central attraction model, replace it
  407.                 forceModels.set(forceModels.size() - 1, force);
  408.             } else {
  409.                 // there are no central attraction model yet, add it at the end of the list
  410.                 forceModels.add(force);
  411.             }
  412.         } else {
  413.             // we want to add a perturbing force model
  414.             if (hasNewtonianAttraction()) {
  415.                 // insert the new force model before Newtonian attraction,
  416.                 // which should always be the last one in the list
  417.                 forceModels.add(forceModels.size() - 1, force);
  418.             } else {
  419.                 // we only have perturbing force models up to now, just append at the end of the list
  420.                 forceModels.add(force);
  421.             }
  422.         }

  423.         force.registerAttitudeProvider(getAttitudeProvider());

  424.     }

  425.     /** Remove all perturbing force models from the global perturbation model
  426.      *  (except central attraction).
  427.      *  <p>
  428.      *  Once all perturbing forces have been removed (and as long as no new force model is added),
  429.      *  the integrated orbit will follow a Keplerian evolution only.
  430.      *  </p>
  431.      *  @see #addForceModel(DSSTForceModel)
  432.      */
  433.     public void removeForceModels() {
  434.         final int last = forceModels.size() - 1;
  435.         if (hasNewtonianAttraction()) {
  436.             // preserve the Newtonian attraction model at the end
  437.             final DSSTForceModel newton = forceModels.get(last);
  438.             forceModels.clear();
  439.             forceModels.add(newton);
  440.         } else {
  441.             forceModels.clear();
  442.         }
  443.     }

  444.     /** Get all the force models, perturbing forces and Newtonian attraction included.
  445.      * @return list of perturbing force models, with Newtonian attraction being the
  446.      * last one
  447.      * @see #addForceModel(DSSTForceModel)
  448.      * @see #setMu(CalculusFieldElement)
  449.      */
  450.     public List<DSSTForceModel> getAllForceModels() {
  451.         return Collections.unmodifiableList(forceModels);
  452.     }

  453.     /** Get propagation parameter type.
  454.      * @return orbit type used for propagation
  455.      */
  456.     public OrbitType getOrbitType() {
  457.         return super.getOrbitType();
  458.     }

  459.     /** Get propagation parameter type.
  460.      * @return angle type to use for propagation
  461.      */
  462.     public PositionAngleType getPositionAngleType() {
  463.         return super.getPositionAngleType();
  464.     }

  465.     /** Conversion from mean to osculating orbit.
  466.      * <p>
  467.      * Compute osculating state <b>in a DSST sense</b>, corresponding to the
  468.      * mean SpacecraftState in input, and according to the Force models taken
  469.      * into account.
  470.      * </p><p>
  471.      * Since the osculating state is obtained by adding short-periodic variation
  472.      * of each force model, the resulting output will depend on the
  473.      * force models parameterized in input.
  474.      * </p>
  475.      * @param mean Mean state to convert
  476.      * @param forces Forces to take into account
  477.      * @param attitudeProvider attitude provider (may be null if there are no Gaussian force models
  478.      * like atmospheric drag, radiation pressure or specific user-defined models)
  479.      * @param <T> type of the elements
  480.      * @return osculating state in a DSST sense
  481.      */
  482.     @SuppressWarnings("unchecked")
  483.     public static <T extends CalculusFieldElement<T>> FieldSpacecraftState<T> computeOsculatingState(final FieldSpacecraftState<T> mean,
  484.                                                                                                  final AttitudeProvider attitudeProvider,
  485.                                                                                                  final Collection<DSSTForceModel> forces) {

  486.         //Create the auxiliary object
  487.         final FieldAuxiliaryElements<T> aux = new FieldAuxiliaryElements<>(mean.getOrbit(), I);

  488.         // Set the force models
  489.         final List<FieldShortPeriodTerms<T>> shortPeriodTerms = new ArrayList<>();
  490.         for (final DSSTForceModel force : forces) {
  491.             force.registerAttitudeProvider(attitudeProvider);
  492.             shortPeriodTerms.addAll(force.initializeShortPeriodTerms(aux, PropagationType.OSCULATING, force.getParameters(mean.getDate().getField(), mean.getDate())));
  493.             force.updateShortPeriodTerms(force.getParametersAllValues(mean.getDate().getField()), mean);
  494.         }

  495.         final FieldEquinoctialOrbit<T> osculatingOrbit = computeOsculatingOrbit(mean, shortPeriodTerms);

  496.         return new FieldSpacecraftState<>(osculatingOrbit, mean.getAttitude(), mean.getMass(),
  497.                                           mean.getAdditionalStatesValues());

  498.     }

  499.     /** Conversion from osculating to mean orbit.
  500.      * <p>
  501.      * Compute mean state <b>in a DSST sense</b>, corresponding to the
  502.      * osculating SpacecraftState in input, and according to the Force models
  503.      * taken into account.
  504.      * </p><p>
  505.      * Since the osculating state is obtained with the computation of
  506.      * short-periodic variation of each force model, the resulting output will
  507.      * depend on the force models parameterized in input.
  508.      * </p><p>
  509.      * The computation is done through a fixed-point iteration process.
  510.      * </p>
  511.      * @param osculating Osculating state to convert
  512.      * @param attitudeProvider attitude provider (may be null if there are no Gaussian force models
  513.      * like atmospheric drag, radiation pressure or specific user-defined models)
  514.      * @param forceModel Forces to take into account
  515.      * @param <T> type of the elements
  516.      * @return mean state in a DSST sense
  517.      */
  518.     public static <T extends CalculusFieldElement<T>> FieldSpacecraftState<T> computeMeanState(final FieldSpacecraftState<T> osculating,
  519.                                                                                            final AttitudeProvider attitudeProvider,
  520.                                                                                            final Collection<DSSTForceModel> forceModel) {
  521.         return computeMeanState(osculating, attitudeProvider, forceModel, EPSILON_DEFAULT, MAX_ITERATIONS_DEFAULT);
  522.     }

  523.     /** Conversion from osculating to mean orbit.
  524.      * <p>
  525.      * Compute mean state <b>in a DSST sense</b>, corresponding to the
  526.      * osculating SpacecraftState in input, and according to the Force models
  527.      * taken into account.
  528.      * </p><p>
  529.      * Since the osculating state is obtained with the computation of
  530.      * short-periodic variation of each force model, the resulting output will
  531.      * depend on the force models parameterized in input.
  532.      * </p><p>
  533.      * The computation is done through a fixed-point iteration process.
  534.      * </p>
  535.      * @param osculating Osculating state to convert
  536.      * @param attitudeProvider attitude provider (may be null if there are no Gaussian force models
  537.      * like atmospheric drag, radiation pressure or specific user-defined models)
  538.      * @param forceModel Forces to take into account
  539.      * @param epsilon convergence threshold for mean parameters conversion
  540.      * @param maxIterations maximum iterations for mean parameters conversion
  541.      * @return mean state in a DSST sense
  542.      * @param <T> type of the elements
  543.      * @since 10.1
  544.      */
  545.     public static <T extends CalculusFieldElement<T>> FieldSpacecraftState<T> computeMeanState(final FieldSpacecraftState<T> osculating,
  546.                                                                                            final AttitudeProvider attitudeProvider,
  547.                                                                                            final Collection<DSSTForceModel> forceModel,
  548.                                                                                            final double epsilon,
  549.                                                                                            final int maxIterations) {
  550.         final FieldOrbit<T> meanOrbit = computeMeanOrbit(osculating, attitudeProvider, forceModel, epsilon, maxIterations);
  551.         return new FieldSpacecraftState<>(meanOrbit, osculating.getAttitude(), osculating.getMass(),
  552.                                           osculating.getAdditionalStatesValues());
  553.     }

  554.     /** Override the default value of the parameter.
  555.     *  <p>
  556.     *  By default, if the initial orbit is defined as osculating,
  557.     *  it will be averaged over 2 satellite revolutions.
  558.     *  This can be changed by using this method.
  559.     *  </p>
  560.     *  @param satelliteRevolution number of satellite revolutions to use for converting osculating to mean
  561.     *                             elements
  562.     */
  563.     public void setSatelliteRevolution(final int satelliteRevolution) {
  564.         mapper.setSatelliteRevolution(satelliteRevolution);
  565.     }

  566.    /** Get the number of satellite revolutions to use for converting osculating to mean elements.
  567.     *  @return number of satellite revolutions to use for converting osculating to mean elements
  568.     */
  569.     public int getSatelliteRevolution() {
  570.         return mapper.getSatelliteRevolution();
  571.     }

  572.    /** {@inheritDoc} */
  573.     @Override
  574.     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
  575.         super.setAttitudeProvider(attitudeProvider);

  576.         //Register the attitude provider for each force model
  577.         for (final DSSTForceModel force : forceModels) {
  578.             force.registerAttitudeProvider(attitudeProvider);
  579.         }
  580.     }

  581.     /** Method called just before integration.
  582.      * <p>
  583.      * The default implementation does nothing, it may be specialized in subclasses.
  584.      * </p>
  585.      * @param initialState initial state
  586.      * @param tEnd target date at which state should be propagated
  587.      */
  588.     @SuppressWarnings("unchecked")
  589.     @Override
  590.     protected void beforeIntegration(final FieldSpacecraftState<T> initialState,
  591.                                      final FieldAbsoluteDate<T> tEnd) {

  592.         // check if only mean elements must be used
  593.         final PropagationType type = isMeanOrbit();

  594.         // compute common auxiliary elements
  595.         final FieldAuxiliaryElements<T> aux = new FieldAuxiliaryElements<>(initialState.getOrbit(), I);

  596.         // initialize all perturbing forces
  597.         final List<FieldShortPeriodTerms<T>> shortPeriodTerms = new ArrayList<>();
  598.         for (final DSSTForceModel force : forceModels) {
  599.             shortPeriodTerms.addAll(force.initializeShortPeriodTerms(aux, type, force.getParameters(field, initialState.getDate())));
  600.         }
  601.         mapper.setShortPeriodTerms(shortPeriodTerms);

  602.         // if required, insert the special short periodics step handler
  603.         if (type == PropagationType.OSCULATING) {
  604.             final FieldShortPeriodicsHandler spHandler = new FieldShortPeriodicsHandler(forceModels);
  605.             // Compute short periodic coefficients for this point
  606.             for (DSSTForceModel forceModel : forceModels) {
  607.                 forceModel.updateShortPeriodTerms(forceModel.getParametersAllValues(field), initialState);

  608.             }
  609.             final Collection<FieldODEStepHandler<T>> stepHandlers = new ArrayList<>();
  610.             stepHandlers.add(spHandler);
  611.             final FieldODEIntegrator<T> integrator = getIntegrator();
  612.             final Collection<FieldODEStepHandler<T>> existing = integrator.getStepHandlers();
  613.             stepHandlers.addAll(existing);

  614.             integrator.clearStepHandlers();

  615.             // add back the existing handlers after the short periodics one
  616.             for (final FieldODEStepHandler<T> sp : stepHandlers) {
  617.                 integrator.addStepHandler(sp);
  618.             }
  619.         }
  620.     }

  621.     /** {@inheritDoc} */
  622.     @Override
  623.     protected void afterIntegration() {
  624.         // remove the special short periodics step handler if added before
  625.         if (isMeanOrbit() ==  PropagationType.OSCULATING) {
  626.             final List<FieldODEStepHandler<T>> preserved = new ArrayList<>();
  627.             final FieldODEIntegrator<T> integrator = getIntegrator();

  628.             // clear the list
  629.             integrator.clearStepHandlers();

  630.             // add back the step handlers that were important for the user
  631.             for (final FieldODEStepHandler<T> sp : preserved) {
  632.                 integrator.addStepHandler(sp);
  633.             }
  634.         }
  635.     }

  636.     /** Compute mean state from osculating state.
  637.      * <p>
  638.      * Compute in a DSST sense the mean state corresponding to the input osculating state.
  639.      * </p><p>
  640.      * The computing is done through a fixed-point iteration process.
  641.      * </p>
  642.      * @param osculating initial osculating state
  643.      * @param attitudeProvider attitude provider (may be null if there are no Gaussian force models
  644.      * like atmospheric drag, radiation pressure or specific user-defined models)
  645.      * @param forceModel force models
  646.      * @param epsilon convergence threshold for mean parameters conversion
  647.      * @param maxIterations maximum iterations for mean parameters conversion
  648.      * @param <T> type of the elements
  649.      * @return mean state
  650.      * @since 10.1
  651.      */
  652.     @SuppressWarnings("unchecked")
  653.     private static <T extends CalculusFieldElement<T>> FieldOrbit<T> computeMeanOrbit(final FieldSpacecraftState<T> osculating, final AttitudeProvider attitudeProvider, final Collection<DSSTForceModel> forceModel,
  654.                                                                                   final double epsilon, final int maxIterations) {

  655.         // zero
  656.         final T zero = osculating.getDate().getField().getZero();

  657.         // rough initialization of the mean parameters
  658.         FieldEquinoctialOrbit<T> meanOrbit = (FieldEquinoctialOrbit<T>) OrbitType.EQUINOCTIAL.convertType(osculating.getOrbit());

  659.         // threshold for each parameter
  660.         final T epsilonT   = zero.newInstance(epsilon);
  661.         final T thresholdA = epsilonT.multiply(FastMath.abs(meanOrbit.getA()).add(1.));
  662.         final T thresholdE = epsilonT.multiply(meanOrbit.getE().add(1.));
  663.         final T thresholdI = epsilonT.multiply(meanOrbit.getI().add(1.));
  664.         final T thresholdL = epsilonT.multiply(zero.getPi());

  665.         // ensure all Gaussian force models can rely on attitude
  666.         for (final DSSTForceModel force : forceModel) {
  667.             force.registerAttitudeProvider(attitudeProvider);
  668.         }

  669.         int i = 0;
  670.         while (i++ < maxIterations) {

  671.             final FieldSpacecraftState<T> meanState = new FieldSpacecraftState<>(meanOrbit, osculating.getAttitude(), osculating.getMass());

  672.             //Create the auxiliary object
  673.             final FieldAuxiliaryElements<T> aux = new FieldAuxiliaryElements<>(meanOrbit, I);

  674.             // Set the force models
  675.             final List<FieldShortPeriodTerms<T>> shortPeriodTerms = new ArrayList<>();
  676.             for (final DSSTForceModel force : forceModel) {
  677.                 shortPeriodTerms.addAll(force.initializeShortPeriodTerms(aux, PropagationType.OSCULATING,
  678.                                  force.getParameters(osculating.getDate().getField(), osculating.getDate())));
  679.                 force.updateShortPeriodTerms(force.getParametersAllValues(osculating.getDate().getField()), meanState);
  680.             }

  681.             // recompute the osculating parameters from the current mean parameters
  682.             final FieldEquinoctialOrbit<T> rebuilt = computeOsculatingOrbit(meanState, shortPeriodTerms);

  683.             // adapted parameters residuals
  684.             final T deltaA  = osculating.getA().subtract(rebuilt.getA());
  685.             final T deltaEx = osculating.getEquinoctialEx().subtract(rebuilt.getEquinoctialEx());
  686.             final T deltaEy = osculating.getEquinoctialEy().subtract(rebuilt.getEquinoctialEy());
  687.             final T deltaHx = osculating.getHx().subtract(rebuilt.getHx());
  688.             final T deltaHy = osculating.getHy().subtract(rebuilt.getHy());
  689.             final T deltaLv = MathUtils.normalizeAngle(osculating.getLv().subtract(rebuilt.getLv()), zero);

  690.             // check convergence
  691.             if (FastMath.abs(deltaA).getReal()  < thresholdA.getReal() &&
  692.                 FastMath.abs(deltaEx).getReal() < thresholdE.getReal() &&
  693.                 FastMath.abs(deltaEy).getReal() < thresholdE.getReal() &&
  694.                 FastMath.abs(deltaHx).getReal() < thresholdI.getReal() &&
  695.                 FastMath.abs(deltaHy).getReal() < thresholdI.getReal() &&
  696.                 FastMath.abs(deltaLv).getReal() < thresholdL.getReal()) {
  697.                 return meanOrbit;
  698.             }

  699.             // update mean parameters
  700.             meanOrbit = new FieldEquinoctialOrbit<>(meanOrbit.getA().add(deltaA),
  701.                                                     meanOrbit.getEquinoctialEx().add(deltaEx),
  702.                                                     meanOrbit.getEquinoctialEy().add(deltaEy),
  703.                                                     meanOrbit.getHx().add(deltaHx),
  704.                                                     meanOrbit.getHy().add(deltaHy),
  705.                                                     meanOrbit.getLv().add(deltaLv),
  706.                                                     PositionAngleType.TRUE, meanOrbit.getFrame(),
  707.                                                     meanOrbit.getDate(), meanOrbit.getMu());
  708.         }

  709.         throw new OrekitException(OrekitMessages.UNABLE_TO_COMPUTE_DSST_MEAN_PARAMETERS, i);
  710.     }

  711.     /** Compute osculating state from mean state.
  712.      * <p>
  713.      * Compute and add the short periodic variation to the mean {@link SpacecraftState}.
  714.      * </p>
  715.      * @param meanState initial mean state
  716.      * @param shortPeriodTerms short period terms
  717.      * @param <T> type of the elements
  718.      * @return osculating state
  719.      */
  720.     private static <T extends CalculusFieldElement<T>> FieldEquinoctialOrbit<T> computeOsculatingOrbit(final FieldSpacecraftState<T> meanState,
  721.                                                                                                    final List<FieldShortPeriodTerms<T>> shortPeriodTerms) {

  722.         final T[] mean = MathArrays.buildArray(meanState.getDate().getField(), 6);
  723.         final T[] meanDot = MathArrays.buildArray(meanState.getDate().getField(), 6);
  724.         OrbitType.EQUINOCTIAL.mapOrbitToArray(meanState.getOrbit(), PositionAngleType.MEAN, mean, meanDot);
  725.         final T[] y = mean.clone();
  726.         for (final FieldShortPeriodTerms<T> spt : shortPeriodTerms) {
  727.             final T[] shortPeriodic = spt.value(meanState.getOrbit());
  728.             for (int i = 0; i < shortPeriodic.length; i++) {
  729.                 y[i] = y[i].add(shortPeriodic[i]);
  730.             }
  731.         }
  732.         return (FieldEquinoctialOrbit<T>) OrbitType.EQUINOCTIAL.mapArrayToOrbit(y, meanDot,
  733.                                                                                 PositionAngleType.MEAN, meanState.getDate(),
  734.                                                                                 meanState.getMu(), meanState.getFrame());
  735.     }

  736.     /** {@inheritDoc} */
  737.     @Override
  738.     protected FieldSpacecraftState<T> getInitialIntegrationState() {
  739.         if (initialIsOsculating) {
  740.             // the initial state is an osculating state,
  741.             // it must be converted to mean state
  742.             return computeMeanState(getInitialState(), getAttitudeProvider(), forceModels);
  743.         } else {
  744.             // the initial state is already a mean state
  745.             return getInitialState();
  746.         }
  747.     }

  748.     /** {@inheritDoc}
  749.      * <p>
  750.      * Note that for DSST, orbit type is hardcoded to {@link OrbitType#EQUINOCTIAL}
  751.      * and position angle type is hardcoded to {@link PositionAngleType#MEAN}, so
  752.      * the corresponding parameters are ignored.
  753.      * </p>
  754.      */
  755.     @Override
  756.     protected FieldStateMapper<T> createMapper(final FieldAbsoluteDate<T> referenceDate, final T mu,
  757.                                                final OrbitType ignoredOrbitType, final PositionAngleType ignoredPositionAngleType,
  758.                                                final AttitudeProvider attitudeProvider, final Frame frame) {

  759.         // create a mapper with the common settings provided as arguments
  760.         final FieldMeanPlusShortPeriodicMapper newMapper =
  761.                 new FieldMeanPlusShortPeriodicMapper(referenceDate, mu, attitudeProvider, frame);

  762.         // copy the specific settings from the existing mapper
  763.         if (mapper != null) {
  764.             newMapper.setSatelliteRevolution(mapper.getSatelliteRevolution());
  765.             newMapper.setSelectedCoefficients(mapper.getSelectedCoefficients());
  766.             newMapper.setShortPeriodTerms(mapper.getShortPeriodTerms());
  767.         }

  768.         mapper = newMapper;
  769.         return mapper;

  770.     }

  771.     /** Internal mapper using mean parameters plus short periodic terms. */
  772.     private class FieldMeanPlusShortPeriodicMapper extends FieldStateMapper<T> {

  773.         /** Short periodic coefficients that must be stored as additional states. */
  774.         private Set<String>                selectedCoefficients;

  775.         /** Number of satellite revolutions in the averaging interval. */
  776.         private int                        satelliteRevolution;

  777.         /** Short period terms. */
  778.         private List<FieldShortPeriodTerms<T>>     shortPeriodTerms;

  779.         /** Simple constructor.
  780.          * @param referenceDate reference date
  781.          * @param mu central attraction coefficient (m³/s²)
  782.          * @param attitudeProvider attitude provider
  783.          * @param frame inertial frame
  784.          */
  785.         FieldMeanPlusShortPeriodicMapper(final FieldAbsoluteDate<T> referenceDate, final T mu,
  786.                                          final AttitudeProvider attitudeProvider, final Frame frame) {

  787.             super(referenceDate, mu, OrbitType.EQUINOCTIAL, PositionAngleType.MEAN, attitudeProvider, frame);

  788.             this.selectedCoefficients = null;

  789.             // Default averaging period for conversion from osculating to mean elements
  790.             this.satelliteRevolution = 2;

  791.             this.shortPeriodTerms    = Collections.emptyList();

  792.         }

  793.         /** {@inheritDoc} */
  794.         @Override
  795.         public FieldSpacecraftState<T> mapArrayToState(final FieldAbsoluteDate<T> date,
  796.                                                        final T[] y, final T[] yDot,
  797.                                                        final PropagationType type) {

  798.             // add short periodic variations to mean elements to get osculating elements
  799.             // (the loop may not be performed if there are no force models and in the
  800.             //  case we want to remain in mean parameters only)
  801.             final T[] elements = y.clone();
  802.             final FieldArrayDictionary<T> coefficients;
  803.             switch (type) {
  804.                 case MEAN:
  805.                     coefficients = null;
  806.                     break;
  807.                 case OSCULATING:
  808.                     final FieldOrbit<T> meanOrbit = OrbitType.EQUINOCTIAL.mapArrayToOrbit(elements, yDot, PositionAngleType.MEAN, date, getMu(), getFrame());
  809.                     coefficients = selectedCoefficients == null ? null : new FieldArrayDictionary<>(date.getField());
  810.                     for (final FieldShortPeriodTerms<T> spt : shortPeriodTerms) {
  811.                         final T[] shortPeriodic = spt.value(meanOrbit);
  812.                         for (int i = 0; i < shortPeriodic.length; i++) {
  813.                             elements[i] = elements[i].add(shortPeriodic[i]);
  814.                         }
  815.                         if (selectedCoefficients != null) {
  816.                             coefficients.putAll(spt.getCoefficients(date, selectedCoefficients));
  817.                         }
  818.                     }
  819.                     break;
  820.                 default:
  821.                     throw new OrekitInternalError(null);
  822.             }

  823.             final T mass = elements[6];
  824.             if (mass.getReal() <= 0.0) {
  825.                 throw new OrekitException(OrekitMessages.NOT_POSITIVE_SPACECRAFT_MASS, mass);
  826.             }

  827.             final FieldOrbit<T> orbit       = OrbitType.EQUINOCTIAL.mapArrayToOrbit(elements, yDot, PositionAngleType.MEAN, date, getMu(), getFrame());
  828.             final FieldAttitude<T> attitude = getAttitudeProvider().getAttitude(orbit, date, getFrame());

  829.             if (coefficients == null) {
  830.                 return new FieldSpacecraftState<>(orbit, attitude, mass);
  831.             } else {
  832.                 return new FieldSpacecraftState<>(orbit, attitude, mass, coefficients);
  833.             }

  834.         }

  835.         /** {@inheritDoc} */
  836.         @Override
  837.         public void mapStateToArray(final FieldSpacecraftState<T> state, final T[] y, final T[] yDot) {

  838.             OrbitType.EQUINOCTIAL.mapOrbitToArray(state.getOrbit(), PositionAngleType.MEAN, y, yDot);
  839.             y[6] = state.getMass();

  840.         }

  841.         /** Set the number of satellite revolutions to use for converting osculating to mean elements.
  842.          *  <p>
  843.          *  By default, if the initial orbit is defined as osculating,
  844.          *  it will be averaged over 2 satellite revolutions.
  845.          *  This can be changed by using this method.
  846.          *  </p>
  847.          *  @param satelliteRevolution number of satellite revolutions to use for converting osculating to mean
  848.          *                             elements
  849.          */
  850.         public void setSatelliteRevolution(final int satelliteRevolution) {
  851.             this.satelliteRevolution = satelliteRevolution;
  852.         }

  853.         /** Get the number of satellite revolutions to use for converting osculating to mean elements.
  854.          *  @return number of satellite revolutions to use for converting osculating to mean elements
  855.          */
  856.         public int getSatelliteRevolution() {
  857.             return satelliteRevolution;
  858.         }

  859.         /** Set the selected short periodic coefficients that must be stored as additional states.
  860.          * @param selectedCoefficients short periodic coefficients that must be stored as additional states
  861.          * (null means no coefficients are selected, empty set means all coefficients are selected)
  862.          */
  863.         public void setSelectedCoefficients(final Set<String> selectedCoefficients) {
  864.             this.selectedCoefficients = selectedCoefficients;
  865.         }

  866.         /** Get the selected short periodic coefficients that must be stored as additional states.
  867.          * @return short periodic coefficients that must be stored as additional states
  868.          * (null means no coefficients are selected, empty set means all coefficients are selected)
  869.          */
  870.         public Set<String> getSelectedCoefficients() {
  871.             return selectedCoefficients;
  872.         }

  873.         /** Set the short period terms.
  874.          * @param shortPeriodTerms short period terms
  875.          * @since 7.1
  876.          */
  877.         public void setShortPeriodTerms(final List<FieldShortPeriodTerms<T>> shortPeriodTerms) {
  878.             this.shortPeriodTerms = shortPeriodTerms;
  879.         }

  880.         /** Get the short period terms.
  881.          * @return shortPeriodTerms short period terms
  882.          * @since 7.1
  883.          */
  884.         public List<FieldShortPeriodTerms<T>> getShortPeriodTerms() {
  885.             return shortPeriodTerms;
  886.         }

  887.     }

  888.     /** {@inheritDoc} */
  889.     @Override
  890.     protected MainStateEquations<T> getMainStateEquations(final FieldODEIntegrator<T> integrator) {
  891.         return new Main(integrator);
  892.     }

  893.     /** Internal class for mean parameters integration. */
  894.     private class Main implements MainStateEquations<T> {

  895.         /** Derivatives array. */
  896.         private final T[] yDot;

  897.         /** Simple constructor.
  898.          * @param integrator numerical integrator to use for propagation.
  899.          */
  900.         Main(final FieldODEIntegrator<T> integrator) {
  901.             yDot = MathArrays.buildArray(field, 7);

  902.             // Setup event detectors for each force model
  903.             forceModels.forEach(dsstForceModel -> dsstForceModel.getFieldEventDetectors(field).
  904.                                 forEach(eventDetector -> setUpEventDetector(integrator, eventDetector)));
  905.         }

  906.         /** {@inheritDoc} */
  907.         @Override
  908.         public void init(final FieldSpacecraftState<T> initialState, final FieldAbsoluteDate<T> target) {
  909.             forceModels.forEach(fm -> fm.init(initialState, target));
  910.         }

  911.         /** {@inheritDoc} */
  912.         @Override
  913.         public T[] computeDerivatives(final FieldSpacecraftState<T> state) {

  914.             final T zero = state.getDate().getField().getZero();
  915.             Arrays.fill(yDot, zero);

  916.             // compute common auxiliary elements
  917.             final FieldAuxiliaryElements<T> auxiliaryElements = new FieldAuxiliaryElements<>(state.getOrbit(), I);

  918.             // compute the contributions of all perturbing forces
  919.             for (final DSSTForceModel forceModel : forceModels) {
  920.                 final T[] daidt = elementRates(forceModel, state, auxiliaryElements, forceModel.getParametersAllValues(field));
  921.                 for (int i = 0; i < daidt.length; i++) {
  922.                     yDot[i] = yDot[i].add(daidt[i]);
  923.                 }
  924.             }

  925.             return yDot.clone();
  926.         }

  927.         /** This method allows to compute the mean equinoctial elements rates da<sub>i</sub> / dt
  928.          *  for a specific force model.
  929.          *  @param forceModel force to take into account
  930.          *  @param state current state
  931.          *  @param auxiliaryElements auxiliary elements related to the current orbit
  932.          *  @param parameters force model parameters (all span values for each parameters)
  933.          *  the extract parameter method {@link #extractParameters(double[], AbsoluteDate)} is called in
  934.          *  the method to select the right parameter.
  935.          *  @return the mean equinoctial elements rates da<sub>i</sub> / dt
  936.          */
  937.         private T[] elementRates(final DSSTForceModel forceModel,
  938.                                  final FieldSpacecraftState<T> state,
  939.                                  final FieldAuxiliaryElements<T> auxiliaryElements,
  940.                                  final T[] parameters) {
  941.             return forceModel.getMeanElementRate(state, auxiliaryElements, parameters);
  942.         }

  943.     }

  944.     /** Estimate tolerance vectors for an AdaptativeStepsizeIntegrator.
  945.      *  <p>
  946.      *  The errors are estimated from partial derivatives properties of orbits,
  947.      *  starting from a scalar position error specified by the user.
  948.      *  Considering the energy conservation equation V = sqrt(mu (2/r - 1/a)),
  949.      *  we get at constant energy (i.e. on a Keplerian trajectory):
  950.      *
  951.      *  <pre>
  952.      *  V r² |dV| = mu |dr|
  953.      *  </pre>
  954.      *
  955.      *  <p> So we deduce a scalar velocity error consistent with the position error. From here, we apply
  956.      *  orbits Jacobians matrices to get consistent errors on orbital parameters.
  957.      *
  958.      *  <p>
  959.      *  The tolerances are only <em>orders of magnitude</em>, and integrator tolerances are only
  960.      *  local estimates, not global ones. So some care must be taken when using these tolerances.
  961.      *  Setting 1mm as a position error does NOT mean the tolerances will guarantee a 1mm error
  962.      *  position after several orbits integration.
  963.      *  </p>
  964.      * @param <T> elements type
  965.      * @param dP user specified position error (m)
  966.      * @param orbit reference orbit
  967.      * @return a two rows array, row 0 being the absolute tolerance error
  968.      *                       and row 1 being the relative tolerance error
  969.      */
  970.     public static <T extends CalculusFieldElement<T>> double[][] tolerances(final T dP, final FieldOrbit<T> orbit) {
  971.         return FieldNumericalPropagator.tolerances(dP, orbit, OrbitType.EQUINOCTIAL);
  972.     }

  973.     /** Estimate tolerance vectors for an AdaptativeStepsizeIntegrator.
  974.      *  <p>
  975.      *  The errors are estimated from partial derivatives properties of orbits,
  976.      *  starting from scalar position and velocity errors specified by the user.
  977.      *  <p>
  978.      *  The tolerances are only <em>orders of magnitude</em>, and integrator tolerances are only
  979.      *  local estimates, not global ones. So some care must be taken when using these tolerances.
  980.      *  Setting 1mm as a position error does NOT mean the tolerances will guarantee a 1mm error
  981.      *  position after several orbits integration.
  982.      *  </p>
  983.      *
  984.      * @param <T> elements type
  985.      * @param dP user specified position error (m)
  986.      * @param dV user specified velocity error (m/s)
  987.      * @param orbit reference orbit
  988.      * @return a two rows array, row 0 being the absolute tolerance error
  989.      *                       and row 1 being the relative tolerance error
  990.      * @since 10.3
  991.      */
  992.     public static <T extends CalculusFieldElement<T>> double[][] tolerances(final T dP, final T dV,
  993.                                                                         final FieldOrbit<T> orbit) {
  994.         return FieldNumericalPropagator.tolerances(dP, dV, orbit, OrbitType.EQUINOCTIAL);
  995.     }

  996.     /** Step handler used to compute the parameters for the short periodic contributions.
  997.      * @author Lucian Barbulescu
  998.      */
  999.     private class FieldShortPeriodicsHandler implements FieldODEStepHandler<T> {

  1000.         /** Force models used to compute short periodic terms. */
  1001.         private final List<DSSTForceModel> forceModels;

  1002.         /** Constructor.
  1003.          * @param forceModels force models
  1004.          */
  1005.         FieldShortPeriodicsHandler(final List<DSSTForceModel> forceModels) {
  1006.             this.forceModels = forceModels;
  1007.         }

  1008.         /** {@inheritDoc} */
  1009.         @SuppressWarnings("unchecked")
  1010.         @Override
  1011.         public void handleStep(final FieldODEStateInterpolator<T> interpolator) {

  1012.             // Get the grid points to compute
  1013.             final T[] interpolationPoints =
  1014.                             interpolationgrid.getGridPoints(interpolator.getPreviousState().getTime(),
  1015.                                                             interpolator.getCurrentState().getTime());

  1016.             final FieldSpacecraftState<T>[] meanStates = new FieldSpacecraftState[interpolationPoints.length];
  1017.             for (int i = 0; i < interpolationPoints.length; ++i) {

  1018.                 // Build the mean state interpolated at grid point
  1019.                 final T time = interpolationPoints[i];
  1020.                 final FieldODEStateAndDerivative<T> sd = interpolator.getInterpolatedState(time);
  1021.                 meanStates[i] = mapper.mapArrayToState(time,
  1022.                                                        sd.getPrimaryState(),
  1023.                                                        sd.getPrimaryDerivative(),
  1024.                                                        PropagationType.MEAN);

  1025.             }

  1026.             // Compute short periodic coefficients for this step
  1027.             for (DSSTForceModel forceModel : forceModels) {
  1028.                 forceModel.updateShortPeriodTerms(forceModel.getParametersAllValues(field), meanStates);
  1029.             }

  1030.         }
  1031.     }

  1032. }