NumericalPropagator.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.numerical;

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

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  24. import org.hipparchus.linear.MatrixUtils;
  25. import org.hipparchus.linear.QRDecomposition;
  26. import org.hipparchus.linear.RealMatrix;
  27. import org.hipparchus.ode.ODEIntegrator;
  28. import org.hipparchus.util.FastMath;
  29. import org.hipparchus.util.Precision;
  30. import org.orekit.annotation.DefaultDataContext;
  31. import org.orekit.attitudes.Attitude;
  32. import org.orekit.attitudes.AttitudeProvider;
  33. import org.orekit.attitudes.AttitudeProviderModifier;
  34. import org.orekit.data.DataContext;
  35. import org.orekit.errors.OrekitException;
  36. import org.orekit.errors.OrekitIllegalArgumentException;
  37. import org.orekit.errors.OrekitInternalError;
  38. import org.orekit.errors.OrekitMessages;
  39. import org.orekit.forces.ForceModel;
  40. import org.orekit.forces.gravity.NewtonianAttraction;
  41. import org.orekit.forces.inertia.InertialForces;
  42. import org.orekit.forces.maneuvers.Maneuver;
  43. import org.orekit.forces.maneuvers.jacobians.Duration;
  44. import org.orekit.forces.maneuvers.jacobians.MedianDate;
  45. import org.orekit.forces.maneuvers.jacobians.TriggerDate;
  46. import org.orekit.forces.maneuvers.trigger.ManeuverTriggers;
  47. import org.orekit.frames.Frame;
  48. import org.orekit.orbits.Orbit;
  49. import org.orekit.orbits.OrbitType;
  50. import org.orekit.orbits.PositionAngleType;
  51. import org.orekit.propagation.AbstractMatricesHarvester;
  52. import org.orekit.propagation.AdditionalStateProvider;
  53. import org.orekit.propagation.MatricesHarvester;
  54. import org.orekit.propagation.PropagationType;
  55. import org.orekit.propagation.Propagator;
  56. import org.orekit.propagation.SpacecraftState;
  57. import org.orekit.propagation.events.EventDetector;
  58. import org.orekit.propagation.events.ParameterDrivenDateIntervalDetector;
  59. import org.orekit.propagation.integration.AbstractIntegratedPropagator;
  60. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  61. import org.orekit.propagation.integration.StateMapper;
  62. import org.orekit.time.AbsoluteDate;
  63. import org.orekit.utils.AbsolutePVCoordinates;
  64. import org.orekit.utils.DoubleArrayDictionary;
  65. import org.orekit.utils.PVCoordinates;
  66. import org.orekit.utils.ParameterDriver;
  67. import org.orekit.utils.ParameterDriversList;
  68. import org.orekit.utils.ParameterDriversList.DelegatingDriver;
  69. import org.orekit.utils.TimeSpanMap.Span;
  70. import org.orekit.utils.ParameterObserver;
  71. import org.orekit.utils.TimeSpanMap;
  72. import org.orekit.utils.TimeStampedPVCoordinates;

  73. /** This class propagates {@link org.orekit.orbits.Orbit orbits} using
  74.  * numerical integration.
  75.  * <p>Numerical propagation is much more accurate than analytical propagation
  76.  * like for example {@link org.orekit.propagation.analytical.KeplerianPropagator
  77.  * Keplerian} or {@link org.orekit.propagation.analytical.EcksteinHechlerPropagator
  78.  * Eckstein-Hechler}, but requires a few more steps to set up to be used properly.
  79.  * Whereas analytical propagators are configured only thanks to their various
  80.  * constructors and can be used immediately after construction, numerical propagators
  81.  * configuration involve setting several parameters between construction time
  82.  * and propagation time.</p>
  83.  * <p>The configuration parameters that can be set are:</p>
  84.  * <ul>
  85.  *   <li>the initial spacecraft state ({@link #setInitialState(SpacecraftState)})</li>
  86.  *   <li>the central attraction coefficient ({@link #setMu(double)})</li>
  87.  *   <li>the various force models ({@link #addForceModel(ForceModel)},
  88.  *   {@link #removeForceModels()})</li>
  89.  *   <li>the {@link OrbitType type} of orbital parameters to be used for propagation
  90.  *   ({@link #setOrbitType(OrbitType)}),</li>
  91.  *   <li>the {@link PositionAngleType type} of position angle to be used in orbital parameters
  92.  *   to be used for propagation where it is relevant ({@link
  93.  *   #setPositionAngleType(PositionAngleType)}),</li>
  94.  *   <li>whether {@link MatricesHarvester state transition matrices and Jacobians matrices}
  95.  *   should be propagated along with orbital state ({@link
  96.  *   #setupMatricesComputation(String, RealMatrix, DoubleArrayDictionary)}),</li>
  97.  *   <li>whether {@link org.orekit.propagation.integration.AdditionalDerivativesProvider additional derivatives}
  98.  *   should be propagated along with orbital state ({@link
  99.  *   #addAdditionalDerivativesProvider(AdditionalDerivativesProvider)}),</li>
  100.  *   <li>the discrete events that should be triggered during propagation
  101.  *   ({@link #addEventDetector(EventDetector)},
  102.  *   {@link #clearEventsDetectors()})</li>
  103.  *   <li>the binding logic with the rest of the application ({@link #getMultiplexer()})</li>
  104.  * </ul>
  105.  * <p>From these configuration parameters, only the initial state is mandatory. The default
  106.  * propagation settings are in {@link OrbitType#EQUINOCTIAL equinoctial} parameters with
  107.  * {@link PositionAngleType#TRUE true} longitude argument. If the central attraction coefficient
  108.  * is not explicitly specified, the one used to define the initial orbit will be used.
  109.  * However, specifying only the initial state and perhaps the central attraction coefficient
  110.  * would mean the propagator would use only Keplerian forces. In this case, the simpler {@link
  111.  * org.orekit.propagation.analytical.KeplerianPropagator KeplerianPropagator} class would
  112.  * perhaps be more effective.</p>
  113.  * <p>The underlying numerical integrator set up in the constructor may also have its own
  114.  * configuration parameters. Typical configuration parameters for adaptive stepsize integrators
  115.  * are the min, max and perhaps start step size as well as the absolute and/or relative errors
  116.  * thresholds.</p>
  117.  * <p>The state that is seen by the integrator is a simple seven elements double array.
  118.  * The six first elements are either:
  119.  * <ul>
  120.  *   <li>the {@link org.orekit.orbits.EquinoctialOrbit equinoctial orbit parameters} (a, e<sub>x</sub>,
  121.  *   e<sub>y</sub>, h<sub>x</sub>, h<sub>y</sub>, λ<sub>M</sub> or λ<sub>E</sub>
  122.  *   or λ<sub>v</sub>) in meters and radians,</li>
  123.  *   <li>the {@link org.orekit.orbits.KeplerianOrbit Keplerian orbit parameters} (a, e, i, ω, Ω,
  124.  *   M or E or v) in meters and radians,</li>
  125.  *   <li>the {@link org.orekit.orbits.CircularOrbit circular orbit parameters} (a, e<sub>x</sub>, e<sub>y</sub>, i,
  126.  *   Ω, α<sub>M</sub> or α<sub>E</sub> or α<sub>v</sub>) in meters
  127.  *   and radians,</li>
  128.  *   <li>the {@link org.orekit.orbits.CartesianOrbit Cartesian orbit parameters} (x, y, z, v<sub>x</sub>,
  129.  *   v<sub>y</sub>, v<sub>z</sub>) in meters and meters per seconds.
  130.  * </ul>
  131.  * <p> The last element is the mass in kilograms and changes only during thrusters firings
  132.  *
  133.  * <p>The following code snippet shows a typical setting for Low Earth Orbit propagation in
  134.  * equinoctial parameters and true longitude argument:</p>
  135.  * <pre>
  136.  * final double dP       = 0.001;
  137.  * final double minStep  = 0.001;
  138.  * final double maxStep  = 500;
  139.  * final double initStep = 60;
  140.  * final double[][] tolerance = NumericalPropagator.tolerances(dP, orbit, OrbitType.EQUINOCTIAL);
  141.  * AdaptiveStepsizeIntegrator integrator = new DormandPrince853Integrator(minStep, maxStep, tolerance[0], tolerance[1]);
  142.  * integrator.setInitialStepSize(initStep);
  143.  * propagator = new NumericalPropagator(integrator);
  144.  * </pre>
  145.  * <p>By default, at the end of the propagation, the propagator resets the initial state to the final state,
  146.  * thus allowing a new propagation to be started from there without recomputing the part already performed.
  147.  * This behaviour can be changed by calling {@link #setResetAtEnd(boolean)}.
  148.  * </p>
  149.  * <p>Beware the same instance cannot be used simultaneously by different threads, the class is <em>not</em>
  150.  * thread-safe.</p>
  151.  *
  152.  * @see SpacecraftState
  153.  * @see ForceModel
  154.  * @see org.orekit.propagation.sampling.OrekitStepHandler
  155.  * @see org.orekit.propagation.sampling.OrekitFixedStepHandler
  156.  * @see org.orekit.propagation.integration.IntegratedEphemeris
  157.  * @see TimeDerivativesEquations
  158.  *
  159.  * @author Mathieu Rom&eacute;ro
  160.  * @author Luc Maisonobe
  161.  * @author Guylaine Prat
  162.  * @author Fabien Maussion
  163.  * @author V&eacute;ronique Pommier-Maurussane
  164.  */
  165. public class NumericalPropagator extends AbstractIntegratedPropagator {

  166.     /** Space dimension. */
  167.     private static final int SPACE_DIMENSION = 3;

  168.     /** State dimension. */
  169.     private static final int STATE_DIMENSION = 2 * SPACE_DIMENSION;

  170.     /** Threshold for matrix solving. */
  171.     private static final double THRESHOLD = Precision.SAFE_MIN;

  172.     /** Force models used during the extrapolation of the orbit. */
  173.     private final List<ForceModel> forceModels;

  174.     /** boolean to ignore or not the creation of a NewtonianAttraction. */
  175.     private boolean ignoreCentralAttraction;

  176.     /**
  177.      * boolean to know if a full attitude (with rates) is needed when computing derivatives for the ODE.
  178.      * since 12.1
  179.      */
  180.     private boolean needFullAttitudeForDerivatives = true;

  181.     /** Create a new instance of NumericalPropagator, based on orbit definition mu.
  182.      * After creation, the instance is empty, i.e. the attitude provider is set to an
  183.      * unspecified default law and there are no perturbing forces at all.
  184.      * This means that if {@link #addForceModel addForceModel} is not
  185.      * called after creation, the integrated orbit will follow a Keplerian
  186.      * evolution only. The defaults are {@link OrbitType#EQUINOCTIAL}
  187.      * for {@link #setOrbitType(OrbitType) propagation
  188.      * orbit type} and {@link PositionAngleType#TRUE} for {@link
  189.      * #setPositionAngleType(PositionAngleType) position angle type}.
  190.      *
  191.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  192.      *
  193.      * @param integrator numerical integrator to use for propagation.
  194.      * @see #NumericalPropagator(ODEIntegrator, AttitudeProvider)
  195.      */
  196.     @DefaultDataContext
  197.     public NumericalPropagator(final ODEIntegrator integrator) {
  198.         this(integrator,
  199.                 Propagator.getDefaultLaw(DataContext.getDefault().getFrames()));
  200.     }

  201.     /** Create a new instance of NumericalPropagator, based on orbit definition mu.
  202.      * After creation, the instance is empty, i.e. the attitude provider is set to an
  203.      * unspecified default law and there are no perturbing forces at all.
  204.      * This means that if {@link #addForceModel addForceModel} is not
  205.      * called after creation, the integrated orbit will follow a Keplerian
  206.      * evolution only. The defaults are {@link OrbitType#EQUINOCTIAL}
  207.      * for {@link #setOrbitType(OrbitType) propagation
  208.      * orbit type} and {@link PositionAngleType#TRUE} for {@link
  209.      * #setPositionAngleType(PositionAngleType) position angle type}.
  210.      * @param integrator numerical integrator to use for propagation.
  211.      * @param attitudeProvider the attitude law.
  212.      * @since 10.1
  213.      */
  214.     public NumericalPropagator(final ODEIntegrator integrator,
  215.                                final AttitudeProvider attitudeProvider) {
  216.         super(integrator, PropagationType.OSCULATING);
  217.         forceModels             = new ArrayList<>();
  218.         ignoreCentralAttraction = false;
  219.         initMapper();
  220.         setAttitudeProvider(attitudeProvider);
  221.         clearStepHandlers();
  222.         setOrbitType(OrbitType.EQUINOCTIAL);
  223.         setPositionAngleType(PositionAngleType.TRUE);
  224.     }

  225.     /** Set the flag to ignore or not the creation of a {@link NewtonianAttraction}.
  226.      * @param ignoreCentralAttraction if true, {@link NewtonianAttraction} is <em>not</em>
  227.      * added automatically if missing
  228.      */
  229.     public void setIgnoreCentralAttraction(final boolean ignoreCentralAttraction) {
  230.         this.ignoreCentralAttraction = ignoreCentralAttraction;
  231.     }

  232.      /** Set the central attraction coefficient μ.
  233.       * <p>
  234.       * Setting the central attraction coefficient is
  235.       * equivalent to {@link #addForceModel(ForceModel) add}
  236.       * a {@link NewtonianAttraction} force model.
  237.       * * </p>
  238.       * @param mu central attraction coefficient (m³/s²)
  239.       * @see #addForceModel(ForceModel)
  240.       * @see #getAllForceModels()
  241.       */
  242.     @Override
  243.     public void setMu(final double mu) {
  244.         if (ignoreCentralAttraction) {
  245.             superSetMu(mu);
  246.         } else {
  247.             addForceModel(new NewtonianAttraction(mu));
  248.             superSetMu(mu);
  249.         }
  250.     }

  251.     /** Set the central attraction coefficient μ only in upper class.
  252.      * @param mu central attraction coefficient (m³/s²)
  253.      */
  254.     private void superSetMu(final double mu) {
  255.         super.setMu(mu);
  256.     }

  257.     /** Check if Newtonian attraction force model is available.
  258.      * <p>
  259.      * Newtonian attraction is always the last force model in the list.
  260.      * </p>
  261.      * @return true if Newtonian attraction force model is available
  262.      */
  263.     private boolean hasNewtonianAttraction() {
  264.         final int last = forceModels.size() - 1;
  265.         return last >= 0 && forceModels.get(last) instanceof NewtonianAttraction;
  266.     }

  267.     /** Add a force model.
  268.      * <p>If this method is not called at all, the integrated orbit will follow
  269.      * a Keplerian evolution only.</p>
  270.      * @param model {@link ForceModel} to add (it can be either a perturbing force
  271.      * model or an instance of {@link NewtonianAttraction})
  272.      * @see #removeForceModels()
  273.      * @see #setMu(double)
  274.      */
  275.     public void addForceModel(final ForceModel model) {

  276.         if (model instanceof NewtonianAttraction) {
  277.             // we want to add the central attraction force model

  278.             try {
  279.                 // ensure we are notified of any mu change
  280.                 model.getParametersDrivers().get(0).addObserver(new ParameterObserver() {
  281.                     /** {@inheritDoc} */
  282.                     @Override
  283.                     public void valueSpanMapChanged(final TimeSpanMap<Double> previousValue, final ParameterDriver driver) {
  284.                         superSetMu(driver.getValue());
  285.                     }
  286.                     /** {@inheritDoc} */
  287.                     @Override
  288.                     public void valueChanged(final double previousValue, final ParameterDriver driver, final AbsoluteDate date) {
  289.                         superSetMu(driver.getValue());
  290.                     }
  291.                 });
  292.             } catch (OrekitException oe) {
  293.                 // this should never happen
  294.                 throw new OrekitInternalError(oe);
  295.             }

  296.             if (hasNewtonianAttraction()) {
  297.                 // there is already a central attraction model, replace it
  298.                 forceModels.set(forceModels.size() - 1, model);
  299.             } else {
  300.                 // there are no central attraction model yet, add it at the end of the list
  301.                 forceModels.add(model);
  302.             }
  303.         } else {
  304.             // we want to add a perturbing force model
  305.             if (hasNewtonianAttraction()) {
  306.                 // insert the new force model before Newtonian attraction,
  307.                 // which should always be the last one in the list
  308.                 forceModels.add(forceModels.size() - 1, model);
  309.             } else {
  310.                 // we only have perturbing force models up to now, just append at the end of the list
  311.                 forceModels.add(model);
  312.             }
  313.         }

  314.     }

  315.     /** Remove all force models (except central attraction).
  316.      * <p>Once all perturbing forces have been removed (and as long as no new force
  317.      * model is added), the integrated orbit will follow a Keplerian evolution
  318.      * only.</p>
  319.      * @see #addForceModel(ForceModel)
  320.      */
  321.     public void removeForceModels() {
  322.         final int last = forceModels.size() - 1;
  323.         if (hasNewtonianAttraction()) {
  324.             // preserve the Newtonian attraction model at the end
  325.             final ForceModel newton = forceModels.get(last);
  326.             forceModels.clear();
  327.             forceModels.add(newton);
  328.         } else {
  329.             forceModels.clear();
  330.         }
  331.     }

  332.     /** Get all the force models, perturbing forces and Newtonian attraction included.
  333.      * @return list of perturbing force models, with Newtonian attraction being the
  334.      * last one
  335.      * @see #addForceModel(ForceModel)
  336.      * @see #setMu(double)
  337.      */
  338.     public List<ForceModel> getAllForceModels() {
  339.         return Collections.unmodifiableList(forceModels);
  340.     }

  341.     /** Set propagation orbit type.
  342.      * @param orbitType orbit type to use for propagation, null for
  343.      * propagating using {@link org.orekit.utils.AbsolutePVCoordinates} rather than {@link Orbit}
  344.      */
  345.     @Override
  346.     public void setOrbitType(final OrbitType orbitType) {
  347.         super.setOrbitType(orbitType);
  348.     }

  349.     /** Get propagation parameter type.
  350.      * @return orbit type used for propagation, null for
  351.      * propagating using {@link org.orekit.utils.AbsolutePVCoordinates} rather than {@link Orbit}
  352.      */
  353.     @Override
  354.     public OrbitType getOrbitType() {
  355.         return super.getOrbitType();
  356.     }

  357.     /** Set position angle type.
  358.      * <p>
  359.      * The position parameter type is meaningful only if {@link
  360.      * #getOrbitType() propagation orbit type}
  361.      * support it. As an example, it is not meaningful for propagation
  362.      * in {@link OrbitType#CARTESIAN Cartesian} parameters.
  363.      * </p>
  364.      * @param positionAngleType angle type to use for propagation
  365.      */
  366.     @Override
  367.     public void setPositionAngleType(final PositionAngleType positionAngleType) {
  368.         super.setPositionAngleType(positionAngleType);
  369.     }

  370.     /** Get propagation parameter type.
  371.      * @return angle type to use for propagation
  372.      */
  373.     @Override
  374.     public PositionAngleType getPositionAngleType() {
  375.         return super.getPositionAngleType();
  376.     }

  377.     /** Set the initial state.
  378.      * @param initialState initial state
  379.      */
  380.     public void setInitialState(final SpacecraftState initialState) {
  381.         resetInitialState(initialState);
  382.     }

  383.     /** {@inheritDoc} */
  384.     @Override
  385.     public void resetInitialState(final SpacecraftState state) {
  386.         super.resetInitialState(state);
  387.         if (!hasNewtonianAttraction()) {
  388.             // use the state to define central attraction
  389.             setMu(state.getMu());
  390.         }
  391.         setStartDate(state.getDate());
  392.     }

  393.     /** Get the names of the parameters in the matrix returned by {@link MatricesHarvester#getParametersJacobian}.
  394.      * @return names of the parameters (i.e. columns) of the Jacobian matrix
  395.      */
  396.     List<String> getJacobiansColumnsNames() {
  397.         final List<String> columnsNames = new ArrayList<>();
  398.         for (final ForceModel forceModel : getAllForceModels()) {
  399.             for (final ParameterDriver driver : forceModel.getParametersDrivers()) {
  400.                 if (driver.isSelected() && !columnsNames.contains(driver.getNamesSpanMap().getFirstSpan().getData())) {
  401.                     // As driver with same name should have same NamesSpanMap we only check if the first span is present,
  402.                     // if not we add all span names to columnsNames
  403.                     for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  404.                         columnsNames.add(span.getData());
  405.                     }
  406.                 }
  407.             }
  408.         }
  409.         Collections.sort(columnsNames);
  410.         return columnsNames;
  411.     }

  412.     /** {@inheritDoc} */
  413.     @Override
  414.     protected AbstractMatricesHarvester createHarvester(final String stmName, final RealMatrix initialStm,
  415.                                                         final DoubleArrayDictionary initialJacobianColumns) {
  416.         return new NumericalPropagationHarvester(this, stmName, initialStm, initialJacobianColumns);
  417.     }

  418.     /** {@inheritDoc} */
  419.     @Override
  420.     protected void setUpStmAndJacobianGenerators() {

  421.         final AbstractMatricesHarvester harvester = getHarvester();
  422.         if (harvester != null) {

  423.             // set up the additional equations and additional state providers
  424.             final StateTransitionMatrixGenerator stmGenerator = setUpStmGenerator();
  425.             final List<String> triggersDates = setUpTriggerDatesJacobiansColumns(stmGenerator.getName());
  426.             setUpRegularParametersJacobiansColumns(stmGenerator, triggersDates);

  427.             // as we are now starting the propagation, everything is configured
  428.             // we can freeze the names in the harvester
  429.             harvester.freezeColumnsNames();

  430.         }

  431.     }

  432.     /** Set up the State Transition Matrix Generator.
  433.      * @return State Transition Matrix Generator
  434.      * @since 11.1
  435.      */
  436.     private StateTransitionMatrixGenerator setUpStmGenerator() {

  437.         final AbstractMatricesHarvester harvester = getHarvester();

  438.         // add the STM generator corresponding to the current settings, and setup state accordingly
  439.         StateTransitionMatrixGenerator stmGenerator = null;
  440.         for (final AdditionalDerivativesProvider equations : getAdditionalDerivativesProviders()) {
  441.             if (equations instanceof StateTransitionMatrixGenerator &&
  442.                 equations.getName().equals(harvester.getStmName())) {
  443.                 // the STM generator has already been set up in a previous propagation
  444.                 stmGenerator = (StateTransitionMatrixGenerator) equations;
  445.                 break;
  446.             }
  447.         }
  448.         if (stmGenerator == null) {
  449.             // this is the first time we need the STM generate, create it
  450.             stmGenerator = new StateTransitionMatrixGenerator(harvester.getStmName(), getAllForceModels(), getAttitudeProvider());
  451.             addAdditionalDerivativesProvider(stmGenerator);
  452.         }

  453.         if (!getInitialIntegrationState().hasAdditionalState(harvester.getStmName())) {
  454.             // add the initial State Transition Matrix if it is not already there
  455.             // (perhaps due to a previous propagation)
  456.             setInitialState(stmGenerator.setInitialStateTransitionMatrix(getInitialState(),
  457.                                                                          harvester.getInitialStateTransitionMatrix(),
  458.                                                                          getOrbitType(),
  459.                                                                          getPositionAngleType()));
  460.         }

  461.         return stmGenerator;

  462.     }

  463.     /** Set up the Jacobians columns generator dedicated to trigger dates.
  464.      * @param stmName name of the State Transition Matrix state
  465.      * @return names of the columns corresponding to trigger dates
  466.      * @since 11.1
  467.      */
  468.     private List<String> setUpTriggerDatesJacobiansColumns(final String stmName) {

  469.         final List<String> names = new ArrayList<>();
  470.         for (final ForceModel forceModel : getAllForceModels()) {
  471.             if (forceModel instanceof Maneuver) {
  472.                 final Maneuver maneuver = (Maneuver) forceModel;
  473.                 final ManeuverTriggers maneuverTriggers = maneuver.getManeuverTriggers();

  474.                 maneuverTriggers.getEventDetectors().
  475.                         filter(d -> d instanceof ParameterDrivenDateIntervalDetector).
  476.                         map(d -> (ParameterDrivenDateIntervalDetector) d).
  477.                         forEach(d -> {
  478.                             TriggerDate start;
  479.                             TriggerDate stop;

  480.                             if (d.getStartDriver().isSelected() || d.getMedianDriver().isSelected() || d.getDurationDriver().isSelected()) {
  481.                                 // normally datedriver should have only 1 span but just in case the user defines several span, there will
  482.                                 // be no problem here
  483.                                 for (Span<String> span = d.getStartDriver().getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  484.                                     start = manageTriggerDate(stmName, maneuver, maneuverTriggers, span.getData(), true, d.getThreshold());
  485.                                     names.add(start.getName());
  486.                                     start = null;
  487.                                 }
  488.                             }
  489.                             if (d.getStopDriver().isSelected() || d.getMedianDriver().isSelected() || d.getDurationDriver().isSelected()) {
  490.                                 // normally datedriver should have only 1 span but just in case the user defines several span, there will
  491.                                 // be no problem here
  492.                                 for (Span<String> span = d.getStopDriver().getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  493.                                     stop = manageTriggerDate(stmName, maneuver, maneuverTriggers, span.getData(), false, d.getThreshold());
  494.                                     names.add(stop.getName());
  495.                                     stop = null;
  496.                                 }
  497.                             }
  498.                             if (d.getMedianDriver().isSelected()) {
  499.                                 // for first span
  500.                                 Span<String> currentMedianNameSpan = d.getMedianDriver().getNamesSpanMap().getFirstSpan();
  501.                                 MedianDate median =
  502.                                         manageMedianDate(d.getStartDriver().getNamesSpanMap().getFirstSpan().getData(),
  503.                                                 d.getStopDriver().getNamesSpanMap().getFirstSpan().getData(), currentMedianNameSpan.getData());
  504.                                 names.add(median.getName());
  505.                                 // for all span
  506.                                 // normally datedriver should have only 1 span but just in case the user defines several span, there will
  507.                                 // be no problem here. /!\ medianDate driver, startDate driver and stopDate driver must have same span number
  508.                                 for (int spanNumber = 1; spanNumber < d.getMedianDriver().getNamesSpanMap().getSpansNumber(); ++spanNumber) {
  509.                                     currentMedianNameSpan = d.getMedianDriver().getNamesSpanMap().getSpan(currentMedianNameSpan.getEnd());
  510.                                     median =
  511.                                             manageMedianDate(d.getStartDriver().getNamesSpanMap().getSpan(currentMedianNameSpan.getStart()).getData(),
  512.                                                     d.getStopDriver().getNamesSpanMap().getSpan(currentMedianNameSpan.getStart()).getData(),
  513.                                                     currentMedianNameSpan.getData());
  514.                                     names.add(median.getName());

  515.                                 }

  516.                             }
  517.                             if (d.getDurationDriver().isSelected()) {
  518.                                 // for first span
  519.                                 Span<String> currentDurationNameSpan = d.getDurationDriver().getNamesSpanMap().getFirstSpan();
  520.                                 Duration duration =
  521.                                         manageManeuverDuration(d.getStartDriver().getNamesSpanMap().getFirstSpan().getData(),
  522.                                                 d.getStopDriver().getNamesSpanMap().getFirstSpan().getData(), currentDurationNameSpan.getData());
  523.                                 names.add(duration.getName());
  524.                                 // for all span
  525.                                 for (int spanNumber = 1; spanNumber < d.getDurationDriver().getNamesSpanMap().getSpansNumber(); ++spanNumber) {
  526.                                     currentDurationNameSpan = d.getDurationDriver().getNamesSpanMap().getSpan(currentDurationNameSpan.getEnd());
  527.                                     duration =
  528.                                             manageManeuverDuration(d.getStartDriver().getNamesSpanMap().getSpan(currentDurationNameSpan.getStart()).getData(),
  529.                                                     d.getStopDriver().getNamesSpanMap().getSpan(currentDurationNameSpan.getStart()).getData(),
  530.                                                     currentDurationNameSpan.getData());
  531.                                     names.add(duration.getName());

  532.                                 }
  533.                             }
  534.                         });
  535.             }
  536.         }

  537.         return names;

  538.     }

  539.     /** Manage a maneuver trigger date.
  540.      * @param stmName name of the State Transition Matrix state
  541.      * @param maneuver maneuver force model
  542.      * @param mt trigger to which the driver is bound
  543.      * @param driverName name of the date driver
  544.      * @param start if true, the driver is a maneuver start
  545.      * @param threshold event detector threshold
  546.      * @return generator for the date driver
  547.      * @since 11.1
  548.      */
  549.     private TriggerDate manageTriggerDate(final String stmName,
  550.                                           final Maneuver maneuver,
  551.                                           final ManeuverTriggers mt,
  552.                                           final String driverName,
  553.                                           final boolean start,
  554.                                           final double threshold) {

  555.         TriggerDate triggerGenerator = null;

  556.         // check if we already have set up the provider
  557.         for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  558.             if (provider instanceof TriggerDate &&
  559.                 provider.getName().equals(driverName)) {
  560.                 // the Jacobian column generator has already been set up in a previous propagation
  561.                 triggerGenerator = (TriggerDate) provider;
  562.                 break;
  563.             }
  564.         }

  565.         if (triggerGenerator == null) {
  566.             // this is the first time we need the Jacobian column generator, create it
  567.             triggerGenerator = new TriggerDate(stmName, driverName, start, maneuver, threshold);
  568.             mt.addResetter(triggerGenerator);
  569.             addAdditionalDerivativesProvider(triggerGenerator.getMassDepletionDelay());
  570.             addAdditionalStateProvider(triggerGenerator);
  571.         }

  572.         if (!getInitialIntegrationState().hasAdditionalState(driverName)) {
  573.             // add the initial Jacobian column if it is not already there
  574.             // (perhaps due to a previous propagation)
  575.             setInitialColumn(triggerGenerator.getMassDepletionDelay().getName(), new double[6]);
  576.             setInitialColumn(driverName, getHarvester().getInitialJacobianColumn(driverName));
  577.         }

  578.         return triggerGenerator;

  579.     }

  580.     /** Manage a maneuver median date.
  581.      * @param startName name of the start driver
  582.      * @param stopName name of the stop driver
  583.      * @param medianName name of the median driver
  584.      * @return generator for the median driver
  585.      * @since 11.1
  586.      */
  587.     private MedianDate manageMedianDate(final String startName, final String stopName, final String medianName) {

  588.         MedianDate medianGenerator = null;

  589.         // check if we already have set up the provider
  590.         for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  591.             if (provider instanceof MedianDate &&
  592.                 provider.getName().equals(medianName)) {
  593.                 // the Jacobian column generator has already been set up in a previous propagation
  594.                 medianGenerator = (MedianDate) provider;
  595.                 break;
  596.             }
  597.         }

  598.         if (medianGenerator == null) {
  599.             // this is the first time we need the Jacobian column generator, create it
  600.             medianGenerator = new MedianDate(startName, stopName, medianName);
  601.             addAdditionalStateProvider(medianGenerator);
  602.         }

  603.         if (!getInitialIntegrationState().hasAdditionalState(medianName)) {
  604.             // add the initial Jacobian column if it is not already there
  605.             // (perhaps due to a previous propagation)
  606.             setInitialColumn(medianName, getHarvester().getInitialJacobianColumn(medianName));
  607.         }

  608.         return medianGenerator;

  609.     }

  610.     /** Manage a maneuver duration.
  611.      * @param startName name of the start driver
  612.      * @param stopName name of the stop driver
  613.      * @param durationName name of the duration driver
  614.      * @return generator for the median driver
  615.      * @since 11.1
  616.      */
  617.     private Duration manageManeuverDuration(final String startName, final String stopName, final String durationName) {

  618.         Duration durationGenerator = null;

  619.         // check if we already have set up the provider
  620.         for (final AdditionalStateProvider provider : getAdditionalStateProviders()) {
  621.             if (provider instanceof Duration &&
  622.                 provider.getName().equals(durationName)) {
  623.                 // the Jacobian column generator has already been set up in a previous propagation
  624.                 durationGenerator = (Duration) provider;
  625.                 break;
  626.             }
  627.         }

  628.         if (durationGenerator == null) {
  629.             // this is the first time we need the Jacobian column generator, create it
  630.             durationGenerator = new Duration(startName, stopName, durationName);
  631.             addAdditionalStateProvider(durationGenerator);
  632.         }

  633.         if (!getInitialIntegrationState().hasAdditionalState(durationName)) {
  634.             // add the initial Jacobian column if it is not already there
  635.             // (perhaps due to a previous propagation)
  636.             setInitialColumn(durationName, getHarvester().getInitialJacobianColumn(durationName));
  637.         }

  638.         return durationGenerator;

  639.     }

  640.     /** Set up the Jacobians columns generator for regular parameters.
  641.      * @param stmGenerator generator for the State Transition Matrix
  642.      * @param triggerDates names of the columns already managed as trigger dates
  643.      * @since 11.1
  644.      */
  645.     private void setUpRegularParametersJacobiansColumns(final StateTransitionMatrixGenerator stmGenerator,
  646.                                                         final List<String> triggerDates) {

  647.         // first pass: gather all parameters (excluding trigger dates), binding similar names together
  648.         final ParameterDriversList selected = new ParameterDriversList();
  649.         for (final ForceModel forceModel : getAllForceModels()) {
  650.             for (final ParameterDriver driver : forceModel.getParametersDrivers()) {
  651.                 if (!triggerDates.contains(driver.getNamesSpanMap().getFirstSpan().getData())) {
  652.                     // if the first span is not in triggerdate means that the driver is not a trigger
  653.                     // date and can be selected here
  654.                     selected.add(driver);
  655.                 }
  656.             }
  657.         }

  658.         // second pass: now that shared parameter names are bound together,
  659.         // their selections status have been synchronized, we can filter them
  660.         selected.filter(true);

  661.         // third pass: sort parameters lexicographically
  662.         selected.sort();

  663.         // add the Jacobians column generators corresponding to parameters, and setup state accordingly
  664.         // a new column is needed for each value estimated so for each span of the parameterDriver
  665.         for (final DelegatingDriver driver : selected.getDrivers()) {

  666.             for (Span<String> currentNameSpan = driver.getNamesSpanMap().getFirstSpan(); currentNameSpan != null; currentNameSpan = currentNameSpan.next()) {

  667.                 IntegrableJacobianColumnGenerator generator = null;
  668.                 // check if we already have set up the providers
  669.                 for (final AdditionalDerivativesProvider provider : getAdditionalDerivativesProviders()) {
  670.                     if (provider instanceof IntegrableJacobianColumnGenerator &&
  671.                         provider.getName().equals(currentNameSpan.getData())) {
  672.                         // the Jacobian column generator has already been set up in a previous propagation
  673.                         generator = (IntegrableJacobianColumnGenerator) provider;
  674.                         break;
  675.                     }

  676.                 }

  677.                 if (generator == null) {
  678.                     // this is the first time we need the Jacobian column generator, create it
  679.                     generator = new IntegrableJacobianColumnGenerator(stmGenerator, currentNameSpan.getData());
  680.                     addAdditionalDerivativesProvider(generator);
  681.                 }

  682.                 if (!getInitialIntegrationState().hasAdditionalState(currentNameSpan.getData())) {
  683.                     // add the initial Jacobian column if it is not already there
  684.                     // (perhaps due to a previous propagation)
  685.                     setInitialColumn(currentNameSpan.getData(), getHarvester().getInitialJacobianColumn(currentNameSpan.getData()));
  686.                 }

  687.             }

  688.         }

  689.     }

  690.     /** Add the initial value of the column to the initial state.
  691.      * <p>
  692.      * The initial state must already contain the Cartesian State Transition Matrix.
  693.      * </p>
  694.      * @param columnName name of the column
  695.      * @param dYdQ column of the Jacobian ∂Y/∂qₘ with respect to propagation type,
  696.      * if null (which is the most frequent case), assumed to be 0
  697.      * @since 11.1
  698.      */
  699.     private void setInitialColumn(final String columnName, final double[] dYdQ) {

  700.         final SpacecraftState state = getInitialState();

  701.         if (dYdQ.length != STATE_DIMENSION) {
  702.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  703.                                       dYdQ.length, STATE_DIMENSION);
  704.         }

  705.         // convert to Cartesian Jacobian
  706.         final double[][] dYdC = new double[STATE_DIMENSION][STATE_DIMENSION];
  707.         getOrbitType().convertType(state.getOrbit()).getJacobianWrtCartesian(getPositionAngleType(), dYdC);
  708.         final double[] column = new QRDecomposition(MatrixUtils.createRealMatrix(dYdC), THRESHOLD).
  709.                         getSolver().
  710.                         solve(MatrixUtils.createRealVector(dYdQ)).
  711.                         toArray();

  712.         // set additional state
  713.         setInitialState(state.addAdditionalState(columnName, column));

  714.     }

  715.     /** {@inheritDoc} */
  716.     @Override
  717.     protected AttitudeProvider initializeAttitudeProviderForDerivatives() {
  718.         final AttitudeProvider attitudeProvider = getAttitudeProvider();
  719.         return needFullAttitudeForDerivatives ? attitudeProvider :
  720.                 AttitudeProviderModifier.getFrozenAttitudeProvider(attitudeProvider);
  721.     }

  722.     /** {@inheritDoc} */
  723.     @Override
  724.     protected StateMapper createMapper(final AbsoluteDate referenceDate, final double mu,
  725.                                        final OrbitType orbitType, final PositionAngleType positionAngleType,
  726.                                        final AttitudeProvider attitudeProvider, final Frame frame) {
  727.         return new OsculatingMapper(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  728.     }

  729.     /** Internal mapper using directly osculating parameters. */
  730.     private static class OsculatingMapper extends StateMapper {

  731.         /** Simple constructor.
  732.          * <p>
  733.          * The position parameter type is meaningful only if {@link
  734.          * #getOrbitType() propagation orbit type}
  735.          * support it. As an example, it is not meaningful for propagation
  736.          * in {@link OrbitType#CARTESIAN Cartesian} parameters.
  737.          * </p>
  738.          * @param referenceDate reference date
  739.          * @param mu central attraction coefficient (m³/s²)
  740.          * @param orbitType orbit type to use for mapping (can be null for {@link AbsolutePVCoordinates})
  741.          * @param positionAngleType angle type to use for propagation
  742.          * @param attitudeProvider attitude provider
  743.          * @param frame inertial frame
  744.          */
  745.         OsculatingMapper(final AbsoluteDate referenceDate, final double mu,
  746.                          final OrbitType orbitType, final PositionAngleType positionAngleType,
  747.                          final AttitudeProvider attitudeProvider, final Frame frame) {
  748.             super(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  749.         }

  750.         /** {@inheritDoc} */
  751.         public SpacecraftState mapArrayToState(final AbsoluteDate date, final double[] y, final double[] yDot,
  752.                                                final PropagationType type) {
  753.             // the parameter type is ignored for the Numerical Propagator

  754.             final double mass = y[6];
  755.             if (mass <= 0.0) {
  756.                 throw new OrekitException(OrekitMessages.NOT_POSITIVE_SPACECRAFT_MASS, mass);
  757.             }

  758.             if (getOrbitType() == null) {
  759.                 // propagation uses absolute position-velocity-acceleration
  760.                 final Vector3D p = new Vector3D(y[0],    y[1],    y[2]);
  761.                 final Vector3D v = new Vector3D(y[3],    y[4],    y[5]);
  762.                 final Vector3D a;
  763.                 final AbsolutePVCoordinates absPva;
  764.                 if (yDot == null) {
  765.                     absPva = new AbsolutePVCoordinates(getFrame(), new TimeStampedPVCoordinates(date, p, v));
  766.                 } else {
  767.                     a = new Vector3D(yDot[3], yDot[4], yDot[5]);
  768.                     absPva = new AbsolutePVCoordinates(getFrame(), new TimeStampedPVCoordinates(date, p, v, a));
  769.                 }

  770.                 final Attitude attitude = getAttitudeProvider().getAttitude(absPva, date, getFrame());
  771.                 return new SpacecraftState(absPva, attitude, mass);
  772.             } else {
  773.                 // propagation uses regular orbits
  774.                 final Orbit orbit       = getOrbitType().mapArrayToOrbit(y, yDot, getPositionAngleType(), date, getMu(), getFrame());
  775.                 final Attitude attitude = getAttitudeProvider().getAttitude(orbit, date, getFrame());

  776.                 return new SpacecraftState(orbit, attitude, mass);
  777.             }

  778.         }

  779.         /** {@inheritDoc} */
  780.         public void mapStateToArray(final SpacecraftState state, final double[] y, final double[] yDot) {
  781.             if (getOrbitType() == null) {
  782.                 // propagation uses absolute position-velocity-acceleration
  783.                 final Vector3D p = state.getAbsPVA().getPosition();
  784.                 final Vector3D v = state.getAbsPVA().getVelocity();
  785.                 y[0] = p.getX();
  786.                 y[1] = p.getY();
  787.                 y[2] = p.getZ();
  788.                 y[3] = v.getX();
  789.                 y[4] = v.getY();
  790.                 y[5] = v.getZ();
  791.                 y[6] = state.getMass();
  792.             }
  793.             else {
  794.                 getOrbitType().mapOrbitToArray(state.getOrbit(), getPositionAngleType(), y, yDot);
  795.                 y[6] = state.getMass();
  796.             }
  797.         }

  798.     }

  799.     /** {@inheritDoc} */
  800.     protected MainStateEquations getMainStateEquations(final ODEIntegrator integrator) {
  801.         return new Main(integrator);
  802.     }

  803.     /** Internal class for osculating parameters integration. */
  804.     private class Main implements MainStateEquations, TimeDerivativesEquations {

  805.         /** Derivatives array. */
  806.         private final double[] yDot;

  807.         /** Current state. */
  808.         private SpacecraftState currentState;

  809.         /** Jacobian of the orbital parameters with respect to the Cartesian parameters. */
  810.         private double[][] jacobian;

  811.         /** Flag keeping track whether Jacobian matrix needs to be recomputed or not. */
  812.         private boolean recomputingJacobian;

  813.         /** Simple constructor.
  814.          * @param integrator numerical integrator to use for propagation.
  815.          */
  816.         Main(final ODEIntegrator integrator) {

  817.             this.yDot     = new double[7];
  818.             this.jacobian = new double[6][6];
  819.             this.recomputingJacobian = true;

  820.             for (final ForceModel forceModel : forceModels) {
  821.                 forceModel.getEventDetectors().forEach(detector -> setUpEventDetector(integrator, detector));
  822.             }

  823.             // default value for Jacobian is identity
  824.             for (int i = 0; i < jacobian.length; ++i) {
  825.                 Arrays.fill(jacobian[i], 0.0);
  826.                 jacobian[i][i] = 1.0;
  827.             }

  828.         }

  829.         /** {@inheritDoc} */
  830.         @Override
  831.         public void init(final SpacecraftState initialState, final AbsoluteDate target) {
  832.             needFullAttitudeForDerivatives = forceModels.stream().anyMatch(ForceModel::dependsOnAttitudeRate);

  833.             forceModels.forEach(fm -> fm.init(initialState, target));

  834.             final int numberOfForces = forceModels.size();
  835.             final OrbitType orbitType = getOrbitType();
  836.             if (orbitType != null && orbitType != OrbitType.CARTESIAN && numberOfForces > 0) {
  837.                 if (numberOfForces > 1) {
  838.                     recomputingJacobian = true;
  839.                 } else {
  840.                     recomputingJacobian = !(forceModels.get(0) instanceof NewtonianAttraction);
  841.                 }
  842.             } else {
  843.                 recomputingJacobian = false;
  844.             }
  845.         }

  846.         /** {@inheritDoc} */
  847.         @Override
  848.         public double[] computeDerivatives(final SpacecraftState state) {

  849.             currentState = state;
  850.             Arrays.fill(yDot, 0.0);
  851.             if (recomputingJacobian) {
  852.                 // propagation uses Jacobian matrix of orbital parameters w.r.t. Cartesian ones
  853.                 currentState.getOrbit().getJacobianWrtCartesian(getPositionAngleType(), jacobian);
  854.             }

  855.             // compute the contributions of all perturbing forces,
  856.             // using the Kepler contribution at the end since
  857.             // NewtonianAttraction is always the last instance in the list
  858.             for (final ForceModel forceModel : forceModels) {
  859.                 forceModel.addContribution(state, this);
  860.             }

  861.             if (getOrbitType() == null) {
  862.                 // position derivative is velocity, and was not added above in the force models
  863.                 // (it is added when orbit type is non-null because NewtonianAttraction considers it)
  864.                 final Vector3D velocity = currentState.getPVCoordinates().getVelocity();
  865.                 yDot[0] += velocity.getX();
  866.                 yDot[1] += velocity.getY();
  867.                 yDot[2] += velocity.getZ();
  868.             }


  869.             return yDot.clone();

  870.         }

  871.         /** {@inheritDoc} */
  872.         @Override
  873.         public void addKeplerContribution(final double mu) {
  874.             if (getOrbitType() == null) {
  875.                 // if mu is neither 0 nor NaN, we want to include Newtonian acceleration
  876.                 if (mu > 0) {
  877.                     // velocity derivative is Newtonian acceleration
  878.                     final Vector3D position = currentState.getPosition();
  879.                     final double r2         = position.getNormSq();
  880.                     final double coeff      = -mu / (r2 * FastMath.sqrt(r2));
  881.                     yDot[3] += coeff * position.getX();
  882.                     yDot[4] += coeff * position.getY();
  883.                     yDot[5] += coeff * position.getZ();
  884.                 }

  885.             } else {
  886.                 // propagation uses regular orbits
  887.                 currentState.getOrbit().addKeplerContribution(getPositionAngleType(), mu, yDot);
  888.             }
  889.         }

  890.         /** {@inheritDoc} */
  891.         public void addNonKeplerianAcceleration(final Vector3D gamma) {
  892.             for (int i = 0; i < 6; ++i) {
  893.                 final double[] jRow = jacobian[i];
  894.                 yDot[i] += jRow[3] * gamma.getX() + jRow[4] * gamma.getY() + jRow[5] * gamma.getZ();
  895.             }
  896.         }

  897.         /** {@inheritDoc} */
  898.         @Override
  899.         public void addMassDerivative(final double q) {
  900.             if (q > 0) {
  901.                 throw new OrekitIllegalArgumentException(OrekitMessages.POSITIVE_FLOW_RATE, q);
  902.             }
  903.             yDot[6] += q;
  904.         }

  905.     }

  906.     /** Estimate tolerance vectors for integrators when propagating in absolute position-velocity-acceleration.
  907.      * @param dP user specified position error
  908.      * @param absPva reference absolute position-velocity-acceleration
  909.      * @return a two rows array, row 0 being the absolute tolerance error and row 1
  910.      * being the relative tolerance error
  911.      * @see NumericalPropagator#tolerances(double, Orbit, OrbitType)
  912.      */
  913.     public static double[][] tolerances(final double dP, final AbsolutePVCoordinates absPva) {

  914.         final double relative = dP / absPva.getPosition().getNorm();
  915.         final double dV = relative * absPva.getVelocity().getNorm();

  916.         final double[] absTol = new double[7];
  917.         final double[] relTol = new double[7];

  918.         absTol[0] = dP;
  919.         absTol[1] = dP;
  920.         absTol[2] = dP;
  921.         absTol[3] = dV;
  922.         absTol[4] = dV;
  923.         absTol[5] = dV;

  924.         // we set the mass tolerance arbitrarily to 1.0e-6 kg, as mass evolves linearly
  925.         // with trust, this often has no influence at all on propagation
  926.         absTol[6] = 1.0e-6;

  927.         Arrays.fill(relTol, relative);

  928.         return new double[][] {
  929.             absTol, relTol
  930.         };

  931.     }

  932.     /** Estimate tolerance vectors for integrators when propagating in orbits.
  933.      * <p>
  934.      * The errors are estimated from partial derivatives properties of orbits,
  935.      * starting from a scalar position error specified by the user.
  936.      * Considering the energy conservation equation V = sqrt(mu (2/r - 1/a)),
  937.      * we get at constant energy (i.e. on a Keplerian trajectory):
  938.      * <pre>
  939.      * V r² |dV| = mu |dr|
  940.      * </pre>
  941.      * <p> So we deduce a scalar velocity error consistent with the position error.
  942.      * From here, we apply orbits Jacobians matrices to get consistent errors
  943.      * on orbital parameters.
  944.      *
  945.      * <p>
  946.      * The tolerances are only <em>orders of magnitude</em>, and integrator tolerances
  947.      * are only local estimates, not global ones. So some care must be taken when using
  948.      * these tolerances. Setting 1mm as a position error does NOT mean the tolerances
  949.      * will guarantee a 1mm error position after several orbits integration.
  950.      * </p>
  951.      * @param dP user specified position error
  952.      * @param orbit reference orbit
  953.      * @param type propagation type for the meaning of the tolerance vectors elements
  954.      * (it may be different from {@code orbit.getType()})
  955.      * @return a two rows array, row 0 being the absolute tolerance error and row 1
  956.      * being the relative tolerance error
  957.      */
  958.     public static double[][] tolerances(final double dP, final Orbit orbit, final OrbitType type) {

  959.         // estimate the scalar velocity error
  960.         final PVCoordinates pv = orbit.getPVCoordinates();
  961.         final double r2 = pv.getPosition().getNormSq();
  962.         final double v  = pv.getVelocity().getNorm();
  963.         final double dV = orbit.getMu() * dP / (v * r2);

  964.         return tolerances(dP, dV, orbit, type);

  965.     }

  966.     /** Estimate tolerance vectors for integrators when propagating in orbits.
  967.      * <p>
  968.      * The errors are estimated from partial derivatives properties of orbits,
  969.      * starting from scalar position and velocity errors specified by the user.
  970.      * <p>
  971.      * The tolerances are only <em>orders of magnitude</em>, and integrator tolerances
  972.      * are only local estimates, not global ones. So some care must be taken when using
  973.      * these tolerances. Setting 1mm as a position error does NOT mean the tolerances
  974.      * will guarantee a 1mm error position after several orbits integration.
  975.      * </p>
  976.      * @param dP user specified position error
  977.      * @param dV user specified velocity error
  978.      * @param orbit reference orbit
  979.      * @param type propagation type for the meaning of the tolerance vectors elements
  980.      * (it may be different from {@code orbit.getType()})
  981.      * @return a two rows array, row 0 being the absolute tolerance error and row 1
  982.      * being the relative tolerance error
  983.      * @since 10.3
  984.      */
  985.     public static double[][] tolerances(final double dP, final double dV,
  986.                                         final Orbit orbit, final OrbitType type) {

  987.         final double[] absTol = new double[7];
  988.         final double[] relTol = new double[7];

  989.         // we set the mass tolerance arbitrarily to 1.0e-6 kg, as mass evolves linearly
  990.         // with trust, this often has no influence at all on propagation
  991.         absTol[6] = 1.0e-6;

  992.         if (type == OrbitType.CARTESIAN) {
  993.             absTol[0] = dP;
  994.             absTol[1] = dP;
  995.             absTol[2] = dP;
  996.             absTol[3] = dV;
  997.             absTol[4] = dV;
  998.             absTol[5] = dV;
  999.         } else {

  1000.             // convert the orbit to the desired type
  1001.             final double[][] jacobian = new double[6][6];
  1002.             final Orbit converted = type.convertType(orbit);
  1003.             converted.getJacobianWrtCartesian(PositionAngleType.TRUE, jacobian);

  1004.             for (int i = 0; i < 6; ++i) {
  1005.                 final double[] row = jacobian[i];
  1006.                 absTol[i] = FastMath.abs(row[0]) * dP +
  1007.                             FastMath.abs(row[1]) * dP +
  1008.                             FastMath.abs(row[2]) * dP +
  1009.                             FastMath.abs(row[3]) * dV +
  1010.                             FastMath.abs(row[4]) * dV +
  1011.                             FastMath.abs(row[5]) * dV;
  1012.                 if (Double.isNaN(absTol[i])) {
  1013.                     throw new OrekitException(OrekitMessages.SINGULAR_JACOBIAN_FOR_ORBIT_TYPE, type);
  1014.                 }
  1015.             }

  1016.         }

  1017.         Arrays.fill(relTol, dP / FastMath.sqrt(orbit.getPosition().getNormSq()));

  1018.         return new double[][] {
  1019.             absTol, relTol
  1020.         };

  1021.     }

  1022.     /** {@inheritDoc} */
  1023.     @Override
  1024.     protected void beforeIntegration(final SpacecraftState initialState, final AbsoluteDate tEnd) {

  1025.         if (!getFrame().isPseudoInertial()) {

  1026.             // inspect all force models to find InertialForces
  1027.             for (ForceModel force : forceModels) {
  1028.                 if (force instanceof InertialForces) {
  1029.                     return;
  1030.                 }
  1031.             }

  1032.             // throw exception if no inertial forces found
  1033.             throw new OrekitException(OrekitMessages.INERTIAL_FORCE_MODEL_MISSING, getFrame().getName());

  1034.         }

  1035.     }

  1036. }