PartialDerivativesEquations.java

  1. /* Copyright 2010-2011 Centre National d'Études Spatiales
  2.  * Licensed to CS Systèmes d'Information (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.DerivativeStructure;
  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.AdditionalEquations;
  28. import org.orekit.utils.ParameterDriver;
  29. import org.orekit.utils.ParameterDriversList;

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

  62.     /** Propagator computing state evolution. */
  63.     private final NumericalPropagator propagator;

  64.     /** Selected parameters for Jacobian computation. */
  65.     private ParameterDriversList selected;

  66.     /** Parameters map. */
  67.     private Map<ParameterDriver, Integer> map;

  68.     /** Name. */
  69.     private final String name;

  70.     /** Flag for Jacobian matrices initialization. */
  71.     private boolean initialized;

  72.     /** Simple constructor.
  73.      * <p>
  74.      * Upon construction, this set of equations is <em>automatically</em> added to
  75.      * the propagator by calling its {@link
  76.      * NumericalPropagator#addAdditionalEquations(AdditionalEquations)} method. So
  77.      * there is no need to call this method explicitly for these equations.
  78.      * </p>
  79.      * @param name name of the partial derivatives equations
  80.      * @param propagator the propagator that will handle the orbit propagation
  81.      * @exception OrekitException if a set of equations with the same name is already present
  82.      */
  83.     public PartialDerivativesEquations(final String name, final NumericalPropagator propagator)
  84.         throws OrekitException {
  85.         this.name                   = name;
  86.         this.selected               = null;
  87.         this.map                    = null;
  88.         this.propagator             = propagator;
  89.         this.initialized            = false;
  90.         propagator.addAdditionalEquations(this);
  91.     }

  92.     /** {@inheritDoc} */
  93.     public String getName() {
  94.         return name;
  95.     }

  96.     /** Freeze the selected parameters from the force models.
  97.      * @exception OrekitException if an existing driver for a
  98.      * parameter throws one when its value is reset using the value
  99.      * from another driver managing the same parameter
  100.      */
  101.     private void freezeParametersSelection()
  102.         throws OrekitException {
  103.         if (selected == null) {

  104.             // first pass: gather all parameters, binding similar names together
  105.             selected = new ParameterDriversList();
  106.             for (final ForceModel provider : propagator.getAllForceModels()) {
  107.                 for (final ParameterDriver driver : provider.getParametersDrivers()) {
  108.                     selected.add(driver);
  109.                 }
  110.             }

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

  114.             // third pass: sort parameters lexicographically
  115.             selected.sort();

  116.             // fourth pass: set up a map between parameters drivers and matrices columns
  117.             map = new IdentityHashMap<ParameterDriver, Integer>();
  118.             int parameterIndex = 0;
  119.             for (final ParameterDriver selectedDriver : selected.getDrivers()) {
  120.                 for (final ForceModel provider : propagator.getAllForceModels()) {
  121.                     for (final ParameterDriver driver : provider.getParametersDrivers()) {
  122.                         if (driver.getName().equals(selectedDriver.getName())) {
  123.                             map.put(driver, parameterIndex);
  124.                         }
  125.                     }
  126.                 }
  127.                 ++parameterIndex;
  128.             }

  129.         }
  130.     }

  131.     /** Get the selected parameters, in Jacobian matrix column order.
  132.      * <p>
  133.      * The force models parameters for which partial derivatives are desired,
  134.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  135.      * before this method is called, so the proper list is returned.
  136.      * </p>
  137.      * @return selected parameters, in Jacobian matrix column order which
  138.      * is lexicographic order
  139.      * @exception OrekitException if an existing driver for a
  140.      * parameter throws one when its value is reset using the value
  141.      * from another driver managing the same parameter
  142.      */
  143.     public ParameterDriversList getSelectedParameters()
  144.         throws OrekitException {
  145.         freezeParametersSelection();
  146.         return selected;
  147.     }

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

  177.     /** Set the initial value of the Jacobian with respect to state and parameter.
  178.      * <p>
  179.      * This method is equivalent to call {@link #setInitialJacobians(SpacecraftState,
  180.      * double[][], double[][])} with dYdY0 set to the identity matrix and dYdP set
  181.      * to a zero matrix.
  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, so proper matrices dimensions are used.
  187.      * </p>
  188.      * @param s0 initial state
  189.      * @param stateDimension state dimension, must be either 6 for orbit only or 7 for orbit and mass
  190.      * @return state with initial Jacobians added
  191.      * @exception OrekitException if the partial equation has not been registered in
  192.      * the propagator or if matrices dimensions are incorrect
  193.      * @see #getSelectedParameters()
  194.      * @deprecated as of 9.0, replaced by {@link #setInitialJacobians(SpacecraftState)}
  195.      */
  196.     @Deprecated
  197.     public SpacecraftState setInitialJacobians(final SpacecraftState s0, final int stateDimension)
  198.         throws OrekitException {
  199.         freezeParametersSelection();
  200.         final double[][] dYdY0 = new double[stateDimension][stateDimension];
  201.         final double[][] dYdP  = new double[stateDimension][selected.getNbParams()];
  202.         for (int i = 0; i < stateDimension; ++i) {
  203.             dYdY0[i][i] = 1.0;
  204.         }
  205.         return setInitialJacobians(s0, dYdY0, dYdP);
  206.     }

  207.     /** Set the initial value of the Jacobian with respect to state and parameter.
  208.      * <p>
  209.      * The returned state must be added to the propagator (it is not done
  210.      * automatically, as the user may need to add more states to it).
  211.      * </p>
  212.      * <p>
  213.      * The force models parameters for which partial derivatives are desired,
  214.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  215.      * before this method is called, and the {@code dY1dP} matrix dimension <em>must</em>
  216.      * be consistent with the selection.
  217.      * </p>
  218.      * @param s1 current state
  219.      * @param dY1dY0 Jacobian of current state at time t₁ with respect
  220.      * to state at some previous time t₀ (must be 6x6)
  221.      * @param dY1dP Jacobian of current state at time t₁ with respect
  222.      * to parameters (may be null if no parameters are selected)
  223.      * @return state with initial Jacobians added
  224.      * @exception OrekitException if the partial equation has not been registered in
  225.      * the propagator or if matrices dimensions are incorrect
  226.      * @see #getSelectedParameters()
  227.      */
  228.     public SpacecraftState setInitialJacobians(final SpacecraftState s1,
  229.                                                final double[][] dY1dY0, final double[][] dY1dP)
  230.         throws OrekitException {

  231.         freezeParametersSelection();

  232.         // Check dimensions
  233.         final int stateDim = dY1dY0.length;
  234.         if (stateDim != 6 || stateDim != dY1dY0[0].length) {
  235.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
  236.                                       stateDim, dY1dY0[0].length);
  237.         }
  238.         if (dY1dP != null && stateDim != dY1dP.length) {
  239.             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
  240.                                       stateDim, dY1dP.length);
  241.         }
  242.         if ((dY1dP == null && selected.getNbParams() != 0) ||
  243.             (dY1dP != null && selected.getNbParams() != dY1dP[0].length)) {
  244.             throw new OrekitException(new OrekitException(OrekitMessages.INITIAL_MATRIX_AND_PARAMETERS_NUMBER_MISMATCH,
  245.                                                           dY1dP == null ? 0 : dY1dP[0].length, selected.getNbParams()));
  246.         }

  247.         // store the matrices as a single dimension array
  248.         initialized = true;
  249.         final JacobiansMapper mapper = getMapper();
  250.         final double[] p = new double[mapper.getAdditionalStateDimension()];
  251.         mapper.setInitialJacobians(s1, dY1dY0, dY1dP, p);

  252.         // set value in propagator
  253.         return s1.addAdditionalState(name, p);

  254.     }

  255.     /** Get a mapper between two-dimensional Jacobians and one-dimensional additional state.
  256.      * @return a mapper between two-dimensional Jacobians and one-dimensional additional state,
  257.      * with the same name as the instance
  258.      * @exception OrekitException if the initial Jacobians have not been initialized yet
  259.      * @see #setInitialJacobians(SpacecraftState, int)
  260.      * @see #setInitialJacobians(SpacecraftState, double[][], double[][])
  261.      */
  262.     public JacobiansMapper getMapper() throws OrekitException {
  263.         if (!initialized) {
  264.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_INITIALIZED);
  265.         }
  266.         return new JacobiansMapper(name, selected,
  267.                                    propagator.getOrbitType(),
  268.                                    propagator.getPositionAngleType());
  269.     }

  270.     /** {@inheritDoc} */
  271.     public double[] computeDerivatives(final SpacecraftState s, final double[] pDot)
  272.         throws OrekitException {

  273.         // initialize acceleration Jacobians to zero
  274.         final int paramDim = selected.getNbParams();
  275.         final int dim = 3;
  276.         final double[][] dAccdParam = new double[dim][paramDim];
  277.         final double[][] dAccdPos   = new double[dim][dim];
  278.         final double[][] dAccdVel   = new double[dim][dim];

  279.         final DSConverter fullConverter    = new DSConverter(s, 6, propagator.getAttitudeProvider());
  280.         final DSConverter posOnlyConverter = new DSConverter(s, 3, propagator.getAttitudeProvider());

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

  283.             final DSConverter converter = forceModel.dependsOnPositionOnly() ? posOnlyConverter : fullConverter;
  284.             final FieldSpacecraftState<DerivativeStructure> dsState = converter.getState(forceModel);
  285.             final DerivativeStructure[] parameters = converter.getParameters(dsState, forceModel);

  286.             final FieldVector3D<DerivativeStructure> acceleration = forceModel.acceleration(dsState, parameters);
  287.             final double[] derivativesX = acceleration.getX().getAllDerivatives();
  288.             final double[] derivativesY = acceleration.getY().getAllDerivatives();
  289.             final double[] derivativesZ = acceleration.getZ().getAllDerivatives();

  290.             // update Jacobians with respect to state
  291.             addToRow(derivativesX, 0, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
  292.             addToRow(derivativesY, 1, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
  293.             addToRow(derivativesZ, 2, converter.getFreeStateParameters(), dAccdPos, dAccdVel);

  294.             int index = converter.getFreeStateParameters();
  295.             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
  296.                 if (driver.isSelected()) {
  297.                     final int parameterIndex = map.get(driver);
  298.                     ++index;
  299.                     dAccdParam[0][parameterIndex] += derivativesX[index];
  300.                     dAccdParam[1][parameterIndex] += derivativesY[index];
  301.                     dAccdParam[2][parameterIndex] += derivativesZ[index];
  302.                 }
  303.             }

  304.         }

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

  306.         // [        |        ]   [                 |                  ]   [     |     ]
  307.         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
  308.         // [        |        ]   [                 |                  ]   [     |     ]
  309.         // ---------+---------   ------------------+------------------- * ------+------
  310.         // [        |        ]   [                 |                  ]   [     |     ]
  311.         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
  312.         // [        |        ]   [                 |                  ]   [     |     ]

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

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

  316.         //     [ Adot ] = [ C ]
  317.         //     [ Bdot ] = [ D ]
  318.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  319.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

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

  323.         // copy C and E into Adot and Bdot
  324.         final int stateDim = 6;
  325.         final double[] p = s.getAdditionalState(getName());
  326.         System.arraycopy(p, dim * stateDim, pDot, 0, dim * stateDim);

  327.         // compute Cdot and Ddot
  328.         for (int i = 0; i < dim; ++i) {
  329.             final double[] dAdPi = dAccdPos[i];
  330.             final double[] dAdVi = dAccdVel[i];
  331.             for (int j = 0; j < stateDim; ++j) {
  332.                 pDot[(dim + i) * stateDim + j] =
  333.                     dAdPi[0] * p[j]                + dAdPi[1] * p[j +     stateDim] + dAdPi[2] * p[j + 2 * stateDim] +
  334.                     dAdVi[0] * p[j + 3 * stateDim] + dAdVi[1] * p[j + 4 * stateDim] + dAdVi[2] * p[j + 5 * stateDim];
  335.             }
  336.         }

  337.         for (int k = 0; k < paramDim; ++k) {
  338.             // the variational equations of the parameters Jacobian matrix are computed
  339.             // one column at a time, they have the following form:
  340.             // [      ]   [                 |                  ]   [   ]   [                  ]
  341.             // [ Edot ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [ E ]   [  dVel/dParam = 0 ]
  342.             // [      ]   [                 |                  ]   [   ]   [                  ]
  343.             // --------   ------------------+------------------- * ----- + --------------------
  344.             // [      ]   [                 |                  ]   [   ]   [                  ]
  345.             // [ Fdot ] = [    dAcc/dPos    |     dAcc/dVel    ]   [ F ]   [    dAcc/dParam   ]
  346.             // [      ]   [                 |                  ]   [   ]   [                  ]

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

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

  350.             //     [ Edot ] = [ F ]
  351.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

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

  355.             // copy F into Edot
  356.             final int columnTop = stateDim * stateDim + k;
  357.             pDot[columnTop]                = p[columnTop + 3 * paramDim];
  358.             pDot[columnTop +     paramDim] = p[columnTop + 4 * paramDim];
  359.             pDot[columnTop + 2 * paramDim] = p[columnTop + 5 * paramDim];

  360.             // compute Fdot
  361.             for (int i = 0; i < dim; ++i) {
  362.                 final double[] dAdPi = dAccdPos[i];
  363.                 final double[] dAdVi = dAccdVel[i];
  364.                 pDot[columnTop + (dim + i) * paramDim] =
  365.                     dAccdParam[i][k] +
  366.                     dAdPi[0] * p[columnTop]                + dAdPi[1] * p[columnTop +     paramDim] + dAdPi[2] * p[columnTop + 2 * paramDim] +
  367.                     dAdVi[0] * p[columnTop + 3 * paramDim] + dAdVi[1] * p[columnTop + 4 * paramDim] + dAdVi[2] * p[columnTop + 5 * paramDim];
  368.             }

  369.         }

  370.         // these equations have no effect on the main state itself
  371.         return null;

  372.     }

  373.     /** Fill Jacobians rows.
  374.      * @param derivatives derivatives of a component of acceleration (along either x, y or z)
  375.      * @param index component index (0 for x, 1 for y, 2 for z)
  376.      * @param freeStateParameters number of free parameters, either 3 (position),
  377.      * 6 (position-velocity) or 7 (position-velocity-mass)
  378.      * @param dAccdPos Jacobian of acceleration with respect to spacecraft position
  379.      * @param dAccdVel Jacobian of acceleration with respect to spacecraft velocity
  380.      */
  381.     private void addToRow(final double[] derivatives, final int index, final int freeStateParameters,
  382.                           final double[][] dAccdPos, final double[][] dAccdVel) {

  383.         for (int i = 0; i < 3; ++i) {
  384.             dAccdPos[index][i] += derivatives[i + 1];
  385.         }
  386.         if (freeStateParameters > 3) {
  387.             for (int i = 0; i < 3; ++i) {
  388.                 dAccdVel[index][i] += derivatives[i + 4];
  389.             }
  390.         }

  391.     }

  392. }