EpochDerivativesEquations.java

  1. /* Copyright 2002-2022 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.IdentityHashMap;
  19. import java.util.Map;

  20. import org.hipparchus.analysis.differentiation.Gradient;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.forces.ForceModel;
  25. import org.orekit.forces.gravity.ThirdBodyAttractionEpoch;
  26. import org.orekit.propagation.FieldSpacecraftState;
  27. import org.orekit.propagation.SpacecraftState;
  28. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  29. import org.orekit.propagation.integration.AdditionalEquations;
  30. import org.orekit.propagation.integration.CombinedDerivatives;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.ParameterDriver;
  33. import org.orekit.utils.ParameterDriversList;

  34. /** This class is a copy of {@link AbsolutePartialDerivativesEquations}
  35.  *  The computation of the derivatives of the acceleration due to a ThirdBodyAttraction
  36.  *  has been added.
  37.  *
  38.  * {@link AdditionalDerivativesProvider Provider} computing the partial derivatives
  39.  * of the state (orbit) with respect to initial state and force models parameters.
  40.  * <p>
  41.  * This set of equations are automatically added to a {@link NumericalPropagator numerical propagator}
  42.  * in order to compute partial derivatives of the orbit along with the orbit itself. This is
  43.  * useful for example in orbit determination applications.
  44.  * </p>
  45.  * <p>
  46.  * The partial derivatives with respect to initial state can be either dimension 6
  47.  * (orbit only) or 7 (orbit and mass).
  48.  * </p>
  49.  * <p>
  50.  * The partial derivatives with respect to force models parameters has a dimension
  51.  * equal to the number of selected parameters. Parameters selection is implemented at
  52.  * {@link ForceModel force models} level. Users must retrieve a {@link ParameterDriver
  53.  * parameter driver} using {@link ForceModel#getParameterDriver(String)} and then
  54.  * select it by calling {@link ParameterDriver#setSelected(boolean) setSelected(true)}.
  55.  * </p>
  56.  * <p>
  57.  * If several force models provide different {@link ParameterDriver drivers} for the
  58.  * same parameter name, selecting any of these drivers has the side effect of
  59.  * selecting all the drivers for this shared parameter. In this case, the partial
  60.  * derivatives will be the sum of the partial derivatives contributed by the
  61.  * corresponding force models. This case typically arises for central attraction
  62.  * coefficient, which has an influence on {@link org.orekit.forces.gravity.NewtonianAttraction
  63.  * Newtonian attraction}, {@link org.orekit.forces.gravity.HolmesFeatherstoneAttractionModel
  64.  * gravity field}, and {@link org.orekit.forces.gravity.Relativity relativity}.
  65.  * </p>
  66.  * @author V&eacute;ronique Pommier-Maurussane
  67.  * @author Luc Maisonobe
  68.  * @since 10.2
  69.  */
  70. @SuppressWarnings("deprecation")
  71. public class EpochDerivativesEquations
  72.     implements AdditionalDerivativesProvider,
  73.     org.orekit.propagation.integration.AdditionalEquations  {

  74.     /** Propagator computing state evolution. */
  75.     private final NumericalPropagator propagator;

  76.     /** Selected parameters for Jacobian computation. */
  77.     private ParameterDriversList selected;

  78.     /** Parameters map. */
  79.     private Map<ParameterDriver, Integer> map;

  80.     /** Name. */
  81.     private final String name;

  82.     /** Flag for Jacobian matrices initialization. */
  83.     private boolean initialized;

  84.     /** Simple constructor.
  85.      * <p>
  86.      * Upon construction, this set of equations is <em>automatically</em> added to
  87.      * the propagator by calling its {@link
  88.      * NumericalPropagator#addAdditionalEquations(AdditionalEquations)} method. So
  89.      * there is no need to call this method explicitly for these equations.
  90.      * </p>
  91.      * @param name name of the partial derivatives equations
  92.      * @param propagator the propagator that will handle the orbit propagation
  93.      */
  94.     public EpochDerivativesEquations(final String name, final NumericalPropagator propagator) {
  95.         this.name                   = name;
  96.         this.selected               = null;
  97.         this.map                    = null;
  98.         this.propagator             = propagator;
  99.         this.initialized            = false;
  100.         propagator.addAdditionalDerivativesProvider(this);
  101.     }

  102.     /** {@inheritDoc} */
  103.     public String getName() {
  104.         return name;
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     public int getDimension() {
  109.         freezeParametersSelection();
  110.         return 6 * (6 + selected.getNbParams() + 1);
  111.     }

  112.     /** Freeze the selected parameters from the force models.
  113.      */
  114.     private void freezeParametersSelection() {
  115.         if (selected == null) {

  116.             // first pass: gather all parameters, binding similar names together
  117.             selected = new ParameterDriversList();
  118.             for (final ForceModel provider : propagator.getAllForceModels()) {
  119.                 for (final ParameterDriver driver : provider.getParametersDrivers()) {
  120.                     selected.add(driver);
  121.                 }
  122.             }

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

  126.             // third pass: sort parameters lexicographically
  127.             selected.sort();

  128.             // fourth pass: set up a map between parameters drivers and matrices columns
  129.             map = new IdentityHashMap<>();
  130.             int parameterIndex = 0;
  131.             for (final ParameterDriver selectedDriver : selected.getDrivers()) {
  132.                 for (final ForceModel provider : propagator.getAllForceModels()) {
  133.                     for (final ParameterDriver driver : provider.getParametersDrivers()) {
  134.                         if (driver.getName().equals(selectedDriver.getName())) {
  135.                             map.put(driver, parameterIndex);
  136.                         }
  137.                     }
  138.                 }
  139.                 ++parameterIndex;
  140.             }

  141.         }
  142.     }

  143.     /** Get the selected parameters, in Jacobian matrix column order.
  144.      * <p>
  145.      * The force models parameters for which partial derivatives are desired,
  146.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  147.      * before this method is called, so the proper list is returned.
  148.      * </p>
  149.      * @return selected parameters, in Jacobian matrix column order which
  150.      * is lexicographic order
  151.      */
  152.     public ParameterDriversList getSelectedParameters() {
  153.         freezeParametersSelection();
  154.         return selected;
  155.     }

  156.     /** Set the initial value of the Jacobian with respect to state and parameter.
  157.      * <p>
  158.      * This method is equivalent to call {@link #setInitialJacobians(SpacecraftState,
  159.      * double[][], double[][])} with dYdY0 set to the identity matrix and dYdP set
  160.      * to a zero matrix.
  161.      * </p>
  162.      * <p>
  163.      * The force models parameters for which partial derivatives are desired,
  164.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  165.      * before this method is called, so proper matrices dimensions are used.
  166.      * </p>
  167.      * @param s0 initial state
  168.      * @return state with initial Jacobians added
  169.      * @see #getSelectedParameters()
  170.      * @since 9.0
  171.      */
  172.     public SpacecraftState setInitialJacobians(final SpacecraftState s0) {
  173.         freezeParametersSelection();
  174.         final int epochStateDimension = 6;
  175.         final double[][] dYdY0 = new double[epochStateDimension][epochStateDimension];
  176.         final double[][] dYdP  = new double[epochStateDimension][selected.getNbParams() + 6];
  177.         for (int i = 0; i < epochStateDimension; ++i) {
  178.             dYdY0[i][i] = 1.0;
  179.         }
  180.         return setInitialJacobians(s0, dYdY0, dYdP);
  181.     }

  182.     /** Set the initial value of the Jacobian with respect to state and parameter.
  183.      * <p>
  184.      * The returned state must be added to the propagator (it is not done
  185.      * automatically, as the user may need to add more states to it).
  186.      * </p>
  187.      * <p>
  188.      * The force models parameters for which partial derivatives are desired,
  189.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  190.      * before this method is called, and the {@code dY1dP} matrix dimension <em>must</em>
  191.      * be consistent with the selection.
  192.      * </p>
  193.      * @param s1 current state
  194.      * @param dY1dY0 Jacobian of current state at time t₁ with respect
  195.      * to state at some previous time t₀ (must be 6x6)
  196.      * @param dY1dP Jacobian of current state at time t₁ with respect
  197.      * to parameters (may be null if no parameters are selected)
  198.      * @return state with initial Jacobians added
  199.      * @see #getSelectedParameters()
  200.      */
  201.     public SpacecraftState setInitialJacobians(final SpacecraftState s1,
  202.                                                final double[][] dY1dY0, final double[][] dY1dP) {

  203.         freezeParametersSelection();

  204.         // Check dimensions
  205.         final int stateDimEpoch = dY1dY0.length;
  206.         if (stateDimEpoch != 6 || stateDimEpoch != dY1dY0[0].length) {
  207.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
  208.                                       stateDimEpoch, dY1dY0[0].length);
  209.         }
  210.         if (dY1dP != null && stateDimEpoch != dY1dP.length) {
  211.             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
  212.                                       stateDimEpoch, dY1dP.length);
  213.         }

  214.         // store the matrices as a single dimension array
  215.         initialized = true;
  216.         final AbsoluteJacobiansMapper absoluteMapper = getMapper();
  217.         final double[] p = new double[absoluteMapper.getAdditionalStateDimension() + 6];
  218.         absoluteMapper.setInitialJacobians(s1, dY1dY0, dY1dP, p);

  219.         // set value in propagator
  220.         return s1.addAdditionalState(name, p);

  221.     }

  222.     /** Get a mapper between two-dimensional Jacobians and one-dimensional additional state.
  223.      * @return a mapper between two-dimensional Jacobians and one-dimensional additional state,
  224.      * with the same name as the instance
  225.      * @see #setInitialJacobians(SpacecraftState)
  226.      * @see #setInitialJacobians(SpacecraftState, double[][], double[][])
  227.      */
  228.     public AbsoluteJacobiansMapper getMapper() {
  229.         if (!initialized) {
  230.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_INITIALIZED);
  231.         }
  232.         return new AbsoluteJacobiansMapper(name, selected);
  233.     }

  234.     /** {@inheritDoc} */
  235.     public void init(final SpacecraftState initialState, final AbsoluteDate target) {
  236.         // FIXME: remove in 12.0 when AdditionalEquations is removed
  237.         AdditionalDerivativesProvider.super.init(initialState, target);
  238.     }

  239.     /** {@inheritDoc} */
  240.     public double[] computeDerivatives(final SpacecraftState s, final double[] pDot) {
  241.         // FIXME: remove in 12.0 when AdditionalEquations is removed
  242.         System.arraycopy(derivatives(s), 0, pDot, 0, pDot.length);
  243.         return null;
  244.     }

  245.     /** {@inheritDoc} */
  246.     @Override
  247.     @Deprecated
  248.     public double[] derivatives(final SpacecraftState state) {
  249.         return combinedDerivatives(state).getAdditionalDerivatives();
  250.     }

  251.     /** {@inheritDoc} */
  252.     public CombinedDerivatives combinedDerivatives(final SpacecraftState s) {

  253.         // initialize acceleration Jacobians to zero
  254.         final int paramDimEpoch = selected.getNbParams() + 1; // added epoch
  255.         final int dimEpoch      = 3;
  256.         final double[][] dAccdParam = new double[dimEpoch][paramDimEpoch];
  257.         final double[][] dAccdPos   = new double[dimEpoch][dimEpoch];
  258.         final double[][] dAccdVel   = new double[dimEpoch][dimEpoch];

  259.         final NumericalGradientConverter fullConverter    = new NumericalGradientConverter(s, 6, propagator.getAttitudeProvider());
  260.         final NumericalGradientConverter posOnlyConverter = new NumericalGradientConverter(s, 3, propagator.getAttitudeProvider());

  261.         // compute acceleration Jacobians, finishing with the largest force: Newtonian attraction
  262.         for (final ForceModel forceModel : propagator.getAllForceModels()) {
  263.             final NumericalGradientConverter converter = forceModel.dependsOnPositionOnly() ? posOnlyConverter : fullConverter;
  264.             final FieldSpacecraftState<Gradient> dsState = converter.getState(forceModel);
  265.             final Gradient[] parameters = converter.getParameters(dsState, forceModel);

  266.             final FieldVector3D<Gradient> acceleration = forceModel.acceleration(dsState, parameters);
  267.             final double[] derivativesX = acceleration.getX().getGradient();
  268.             final double[] derivativesY = acceleration.getY().getGradient();
  269.             final double[] derivativesZ = acceleration.getZ().getGradient();

  270.             // update Jacobians with respect to state
  271.             addToRow(derivativesX, 0, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
  272.             addToRow(derivativesY, 1, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
  273.             addToRow(derivativesZ, 2, converter.getFreeStateParameters(), dAccdPos, dAccdVel);

  274.             int index = converter.getFreeStateParameters();
  275.             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
  276.                 if (driver.isSelected()) {
  277.                     final int parameterIndex = map.get(driver);
  278.                     dAccdParam[0][parameterIndex] += derivativesX[index];
  279.                     dAccdParam[1][parameterIndex] += derivativesY[index];
  280.                     dAccdParam[2][parameterIndex] += derivativesZ[index];
  281.                     ++index;
  282.                 }
  283.             }

  284.             // Add the derivatives of the acceleration w.r.t. the Epoch
  285.             if (forceModel instanceof ThirdBodyAttractionEpoch) {
  286.                 final double[] parametersValues = new double[] {parameters[0].getValue()};
  287.                 final double[] derivatives = ((ThirdBodyAttractionEpoch) forceModel).getDerivativesToEpoch(s, parametersValues);
  288.                 dAccdParam[0][paramDimEpoch - 1] += derivatives[0];
  289.                 dAccdParam[1][paramDimEpoch - 1] += derivatives[1];
  290.                 dAccdParam[2][paramDimEpoch - 1] += derivatives[2];
  291.             }

  292.         }

  293.         // the variational equations of the complete state Jacobian matrix have the following form:

  294.         // [        |        ]   [                 |                  ]   [     |     ]
  295.         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
  296.         // [        |        ]   [                 |                  ]   [     |     ]
  297.         // ---------+---------   ------------------+------------------- * ------+------
  298.         // [        |        ]   [                 |                  ]   [     |     ]
  299.         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
  300.         // [        |        ]   [                 |                  ]   [     |     ]

  301.         // The A, B, C and D sub-matrices and their derivatives (Adot ...) are 3x3 matrices

  302.         // The expanded multiplication above can be rewritten to take into account
  303.         // the fixed values found in the sub-matrices in the left factor. This leads to:

  304.         //     [ Adot ] = [ C ]
  305.         //     [ Bdot ] = [ D ]
  306.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  307.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

  308.         // The following loops compute these expressions taking care of the mapping of the
  309.         // (A, B, C, D) matrices into the single dimension array p and of the mapping of the
  310.         // (Adot, Bdot, Cdot, Ddot) matrices into the single dimension array pDot.

  311.         // copy C and E into Adot and Bdot
  312.         final int stateDim = 6;
  313.         final double[] p = s.getAdditionalState(getName());
  314.         final double[] pDot = new double[p.length];
  315.         System.arraycopy(p, dimEpoch * stateDim, pDot, 0, dimEpoch * stateDim);

  316.         // compute Cdot and Ddot
  317.         for (int i = 0; i < dimEpoch; ++i) {
  318.             final double[] dAdPi = dAccdPos[i];
  319.             final double[] dAdVi = dAccdVel[i];
  320.             for (int j = 0; j < stateDim; ++j) {
  321.                 pDot[(dimEpoch + i) * stateDim + j] =
  322.                     dAdPi[0] * p[j]                + dAdPi[1] * p[j +     stateDim] + dAdPi[2] * p[j + 2 * stateDim] +
  323.                     dAdVi[0] * p[j + 3 * stateDim] + dAdVi[1] * p[j + 4 * stateDim] + dAdVi[2] * p[j + 5 * stateDim];
  324.             }
  325.         }

  326.         for (int k = 0; k < paramDimEpoch; ++k) {
  327.             // the variational equations of the parameters Jacobian matrix are computed
  328.             // one column at a time, they have the following form:
  329.             // [      ]   [                 |                  ]   [   ]   [                  ]
  330.             // [ Edot ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [ E ]   [  dVel/dParam = 0 ]
  331.             // [      ]   [                 |                  ]   [   ]   [                  ]
  332.             // --------   ------------------+------------------- * ----- + --------------------
  333.             // [      ]   [                 |                  ]   [   ]   [                  ]
  334.             // [ Fdot ] = [    dAcc/dPos    |     dAcc/dVel    ]   [ F ]   [    dAcc/dParam   ]
  335.             // [      ]   [                 |                  ]   [   ]   [                  ]

  336.             // The E and F sub-columns and their derivatives (Edot, Fdot) are 3 elements columns.

  337.             // The expanded multiplication and addition above can be rewritten to take into
  338.             // account the fixed values found in the sub-matrices in the left factor. This leads to:

  339.             //     [ Edot ] = [ F ]
  340.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

  341.             // The following loops compute these expressions taking care of the mapping of the
  342.             // (E, F) columns into the single dimension array p and of the mapping of the
  343.             // (Edot, Fdot) columns into the single dimension array pDot.

  344.             // copy F into Edot
  345.             final int columnTop = stateDim * stateDim + k;
  346.             pDot[columnTop]                     = p[columnTop + 3 * paramDimEpoch];
  347.             pDot[columnTop +     paramDimEpoch] = p[columnTop + 4 * paramDimEpoch];
  348.             pDot[columnTop + 2 * paramDimEpoch] = p[columnTop + 5 * paramDimEpoch];

  349.             // compute Fdot
  350.             for (int i = 0; i < dimEpoch; ++i) {
  351.                 final double[] dAdP = dAccdPos[i];
  352.                 final double[] dAdV = dAccdVel[i];
  353.                 pDot[columnTop + (dimEpoch + i) * paramDimEpoch] =
  354.                     dAccdParam[i][k] +
  355.                     dAdP[0] * p[columnTop]                     + dAdP[1] * p[columnTop +     paramDimEpoch] + dAdP[2] * p[columnTop + 2 * paramDimEpoch] +
  356.                     dAdV[0] * p[columnTop + 3 * paramDimEpoch] + dAdV[1] * p[columnTop + 4 * paramDimEpoch] + dAdV[2] * p[columnTop + 5 * paramDimEpoch];
  357.             }

  358.         }

  359.         return new CombinedDerivatives(pDot, null);

  360.     }

  361.     /** Fill Jacobians rows.
  362.      * @param derivatives derivatives of a component of acceleration (along either x, y or z)
  363.      * @param index component index (0 for x, 1 for y, 2 for z)
  364.      * @param freeStateParameters number of free parameters, either 3 (position),
  365.      * 6 (position-velocity) or 7 (position-velocity-mass)
  366.      * @param dAccdPos Jacobian of acceleration with respect to spacecraft position
  367.      * @param dAccdVel Jacobian of acceleration with respect to spacecraft velocity
  368.      */
  369.     private void addToRow(final double[] derivatives, final int index, final int freeStateParameters,
  370.                           final double[][] dAccdPos, final double[][] dAccdVel) {

  371.         for (int i = 0; i < 3; ++i) {
  372.             dAccdPos[index][i] += derivatives[i];
  373.         }
  374.         if (freeStateParameters > 3) {
  375.             for (int i = 0; i < 3; ++i) {
  376.                 dAccdVel[index][i] += derivatives[i + 3];
  377.             }
  378.         }

  379.     }

  380. }