PartialDerivativesEquations.java

  1. /* Copyright 2010-2011 Centre National d'Études Spatiales
  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.propagation.FieldSpacecraftState;
  26. import org.orekit.propagation.SpacecraftState;
  27. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  28. import org.orekit.propagation.integration.CombinedDerivatives;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.ParameterDriver;
  31. import org.orekit.utils.ParameterDriversList;

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

  70.     /** Propagator computing state evolution. */
  71.     private final NumericalPropagator propagator;

  72.     /** Selected parameters for Jacobian computation. */
  73.     private ParameterDriversList selected;

  74.     /** Parameters map. */
  75.     private Map<ParameterDriver, Integer> map;

  76.     /** Name. */
  77.     private final String name;

  78.     /** Flag for Jacobian matrices initialization. */
  79.     private boolean initialized;

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

  98.     /** {@inheritDoc} */
  99.     public String getName() {
  100.         return name;
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public int getDimension() {
  105.         freezeParametersSelection();
  106.         return 6 * (6 + selected.getNbParams());
  107.     }

  108.     /** Freeze the selected parameters from the force models.
  109.      */
  110.     private void freezeParametersSelection() {
  111.         if (selected == null) {

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

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

  122.             // third pass: sort parameters lexicographically
  123.             selected.sort();

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

  137.         }
  138.     }

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

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

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

  199.         freezeParametersSelection();

  200.         // Check dimensions
  201.         final int stateDim = dY1dY0.length;
  202.         if (stateDim != 6 || stateDim != dY1dY0[0].length) {
  203.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
  204.                                       stateDim, dY1dY0[0].length);
  205.         }
  206.         if (dY1dP != null && stateDim != dY1dP.length) {
  207.             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
  208.                                       stateDim, dY1dP.length);
  209.         }
  210.         if (dY1dP == null && selected.getNbParams() != 0 ||
  211.             dY1dP != null && selected.getNbParams() != dY1dP[0].length) {
  212.             throw new OrekitException(new OrekitException(OrekitMessages.INITIAL_MATRIX_AND_PARAMETERS_NUMBER_MISMATCH,
  213.                                                           dY1dP == null ? 0 : dY1dP[0].length, selected.getNbParams()));
  214.         }

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

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

  222.     }

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

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

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

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

  254.     /** {@inheritDoc} */
  255.     public CombinedDerivatives combinedDerivatives(final SpacecraftState s) {

  256.         // initialize acceleration Jacobians to zero
  257.         final int paramDim = selected.getNbParams();
  258.         final int dim = 3;
  259.         final double[][] dAccdParam = new double[dim][paramDim];
  260.         final double[][] dAccdPos   = new double[dim][dim];
  261.         final double[][] dAccdVel   = new double[dim][dim];

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

  264.         // compute acceleration Jacobians, finishing with the largest force: Newtonian attraction
  265.         for (final ForceModel forceModel : propagator.getAllForceModels()) {

  266.             final NumericalGradientConverter converter = forceModel.dependsOnPositionOnly() ? posOnlyConverter : fullConverter;
  267.             final FieldSpacecraftState<Gradient> dsState = converter.getState(forceModel);
  268.             final Gradient[] parameters = converter.getParameters(dsState, forceModel);

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

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

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

  287.         }

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

  289.         // [        |        ]   [                 |                  ]   [     |     ]
  290.         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
  291.         // [        |        ]   [                 |                  ]   [     |     ]
  292.         // ---------+---------   ------------------+------------------- * ------+------
  293.         // [        |        ]   [                 |                  ]   [     |     ]
  294.         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
  295.         // [        |        ]   [                 |                  ]   [     |     ]

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

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

  299.         //     [ Adot ] = [ C ]
  300.         //     [ Bdot ] = [ D ]
  301.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  302.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

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

  306.         // copy C and E into Adot and Bdot
  307.         final int stateDim = 6;
  308.         final double[] p = s.getAdditionalState(getName());
  309.         final double[] pDot = new double[p.length];
  310.         System.arraycopy(p, dim * stateDim, pDot, 0, dim * stateDim);

  311.         // compute Cdot and Ddot
  312.         for (int i = 0; i < dim; ++i) {
  313.             final double[] dAdPi = dAccdPos[i];
  314.             final double[] dAdVi = dAccdVel[i];
  315.             for (int j = 0; j < stateDim; ++j) {
  316.                 pDot[(dim + i) * stateDim + j] =
  317.                     dAdPi[0] * p[j]                + dAdPi[1] * p[j +     stateDim] + dAdPi[2] * p[j + 2 * stateDim] +
  318.                     dAdVi[0] * p[j + 3 * stateDim] + dAdVi[1] * p[j + 4 * stateDim] + dAdVi[2] * p[j + 5 * stateDim];
  319.             }
  320.         }

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

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

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

  334.             //     [ Edot ] = [ F ]
  335.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

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

  339.             // copy F into Edot
  340.             final int columnTop = stateDim * stateDim + k;
  341.             pDot[columnTop]                = p[columnTop + 3 * paramDim];
  342.             pDot[columnTop +     paramDim] = p[columnTop + 4 * paramDim];
  343.             pDot[columnTop + 2 * paramDim] = p[columnTop + 5 * paramDim];

  344.             // compute Fdot
  345.             for (int i = 0; i < dim; ++i) {
  346.                 final double[] dAdPi = dAccdPos[i];
  347.                 final double[] dAdVi = dAccdVel[i];
  348.                 pDot[columnTop + (dim + i) * paramDim] =
  349.                     dAccdParam[i][k] +
  350.                     dAdPi[0] * p[columnTop]                + dAdPi[1] * p[columnTop +     paramDim] + dAdPi[2] * p[columnTop + 2 * paramDim] +
  351.                     dAdVi[0] * p[columnTop + 3 * paramDim] + dAdVi[1] * p[columnTop + 4 * paramDim] + dAdVi[2] * p[columnTop + 5 * paramDim];
  352.             }

  353.         }

  354.         return new CombinedDerivatives(pDot, null);

  355.     }

  356.     /** Get the flag for the initialization of the state jacobian.
  357.      * @return true if the state jacobian is initialized
  358.      * @since 10.2
  359.      */
  360.     public boolean isInitialize() {
  361.         return initialized;
  362.     }

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

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

  381.     }

  382. }