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.      */
  82.     public PartialDerivativesEquations(final String name, final NumericalPropagator propagator) {
  83.         this.name                   = name;
  84.         this.selected               = null;
  85.         this.map                    = null;
  86.         this.propagator             = propagator;
  87.         this.initialized            = false;
  88.         propagator.addAdditionalEquations(this);
  89.     }

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

  94.     /** Freeze the selected parameters from the force models.
  95.      */
  96.     private void freezeParametersSelection() {
  97.         if (selected == null) {

  98.             // first pass: gather all parameters, binding similar names together
  99.             selected = new ParameterDriversList();
  100.             for (final ForceModel provider : propagator.getAllForceModels()) {
  101.                 for (final ParameterDriver driver : provider.getParametersDrivers()) {
  102.                     selected.add(driver);
  103.                 }
  104.             }

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

  108.             // third pass: sort parameters lexicographically
  109.             selected.sort();

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

  123.         }
  124.     }

  125.     /** Get the selected parameters, in Jacobian matrix column order.
  126.      * <p>
  127.      * The force models parameters for which partial derivatives are desired,
  128.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  129.      * before this method is called, so the proper list is returned.
  130.      * </p>
  131.      * @return selected parameters, in Jacobian matrix column order which
  132.      * is lexicographic order
  133.      */
  134.     public ParameterDriversList getSelectedParameters() {
  135.         freezeParametersSelection();
  136.         return selected;
  137.     }

  138.     /** Set the initial value of the Jacobian with respect to state and parameter.
  139.      * <p>
  140.      * This method is equivalent to call {@link #setInitialJacobians(SpacecraftState,
  141.      * double[][], double[][])} with dYdY0 set to the identity matrix and dYdP set
  142.      * to a zero matrix.
  143.      * </p>
  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 proper matrices dimensions are used.
  148.      * </p>
  149.      * @param s0 initial state
  150.      * @return state with initial Jacobians added
  151.      * @see #getSelectedParameters()
  152.      * @since 9.0
  153.      */
  154.     public SpacecraftState setInitialJacobians(final SpacecraftState s0) {
  155.         freezeParametersSelection();
  156.         final int stateDimension = 6;
  157.         final double[][] dYdY0 = new double[stateDimension][stateDimension];
  158.         final double[][] dYdP  = new double[stateDimension][selected.getNbParams()];
  159.         for (int i = 0; i < stateDimension; ++i) {
  160.             dYdY0[i][i] = 1.0;
  161.         }
  162.         return setInitialJacobians(s0, dYdY0, dYdP);
  163.     }

  164.     /** Set the initial value of the Jacobian with respect to state and parameter.
  165.      * <p>
  166.      * This method is equivalent to call {@link #setInitialJacobians(SpacecraftState,
  167.      * double[][], double[][])} with dYdY0 set to the identity matrix and dYdP set
  168.      * to a zero matrix.
  169.      * </p>
  170.      * <p>
  171.      * The force models parameters for which partial derivatives are desired,
  172.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  173.      * before this method is called, so proper matrices dimensions are used.
  174.      * </p>
  175.      * @param s0 initial state
  176.      * @param stateDimension state dimension, must be either 6 for orbit only or 7 for orbit and mass
  177.      * @return state with initial Jacobians added
  178.      * @see #getSelectedParameters()
  179.      * @deprecated as of 9.0, replaced by {@link #setInitialJacobians(SpacecraftState)}
  180.      */
  181.     @Deprecated
  182.     public SpacecraftState setInitialJacobians(final SpacecraftState s0, final int stateDimension) {
  183.         freezeParametersSelection();
  184.         final double[][] dYdY0 = new double[stateDimension][stateDimension];
  185.         final double[][] dYdP  = new double[stateDimension][selected.getNbParams()];
  186.         for (int i = 0; i < stateDimension; ++i) {
  187.             dYdY0[i][i] = 1.0;
  188.         }
  189.         return setInitialJacobians(s0, dYdY0, dYdP);
  190.     }

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

  212.         freezeParametersSelection();

  213.         // Check dimensions
  214.         final int stateDim = dY1dY0.length;
  215.         if (stateDim != 6 || stateDim != dY1dY0[0].length) {
  216.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
  217.                                       stateDim, dY1dY0[0].length);
  218.         }
  219.         if (dY1dP != null && stateDim != dY1dP.length) {
  220.             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
  221.                                       stateDim, dY1dP.length);
  222.         }
  223.         if ((dY1dP == null && selected.getNbParams() != 0) ||
  224.             (dY1dP != null && selected.getNbParams() != dY1dP[0].length)) {
  225.             throw new OrekitException(new OrekitException(OrekitMessages.INITIAL_MATRIX_AND_PARAMETERS_NUMBER_MISMATCH,
  226.                                                           dY1dP == null ? 0 : dY1dP[0].length, selected.getNbParams()));
  227.         }

  228.         // store the matrices as a single dimension array
  229.         initialized = true;
  230.         final JacobiansMapper mapper = getMapper();
  231.         final double[] p = new double[mapper.getAdditionalStateDimension()];
  232.         mapper.setInitialJacobians(s1, dY1dY0, dY1dP, p);

  233.         // set value in propagator
  234.         return s1.addAdditionalState(name, p);

  235.     }

  236.     /** Get a mapper between two-dimensional Jacobians and one-dimensional additional state.
  237.      * @return a mapper between two-dimensional Jacobians and one-dimensional additional state,
  238.      * with the same name as the instance
  239.           * @see #setInitialJacobians(SpacecraftState, int)
  240.      * @see #setInitialJacobians(SpacecraftState, double[][], double[][])
  241.      */
  242.     public JacobiansMapper getMapper() {
  243.         if (!initialized) {
  244.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_INITIALIZED);
  245.         }
  246.         return new JacobiansMapper(name, selected,
  247.                                    propagator.getOrbitType(),
  248.                                    propagator.getPositionAngleType());
  249.     }

  250.     /** {@inheritDoc} */
  251.     public double[] computeDerivatives(final SpacecraftState s, final double[] pDot) {

  252.         // initialize acceleration Jacobians to zero
  253.         final int paramDim = selected.getNbParams();
  254.         final int dim = 3;
  255.         final double[][] dAccdParam = new double[dim][paramDim];
  256.         final double[][] dAccdPos   = new double[dim][dim];
  257.         final double[][] dAccdVel   = new double[dim][dim];

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

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

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

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

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

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

  283.         }

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

  285.         // [        |        ]   [                 |                  ]   [     |     ]
  286.         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
  287.         // [        |        ]   [                 |                  ]   [     |     ]
  288.         // ---------+---------   ------------------+------------------- * ------+------
  289.         // [        |        ]   [                 |                  ]   [     |     ]
  290.         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
  291.         // [        |        ]   [                 |                  ]   [     |     ]

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

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

  295.         //     [ Adot ] = [ C ]
  296.         //     [ Bdot ] = [ D ]
  297.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  298.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

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

  302.         // copy C and E into Adot and Bdot
  303.         final int stateDim = 6;
  304.         final double[] p = s.getAdditionalState(getName());
  305.         System.arraycopy(p, dim * stateDim, pDot, 0, dim * stateDim);

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

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

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

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

  329.             //     [ Edot ] = [ F ]
  330.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

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

  334.             // copy F into Edot
  335.             final int columnTop = stateDim * stateDim + k;
  336.             pDot[columnTop]                = p[columnTop + 3 * paramDim];
  337.             pDot[columnTop +     paramDim] = p[columnTop + 4 * paramDim];
  338.             pDot[columnTop + 2 * paramDim] = p[columnTop + 5 * paramDim];

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

  348.         }

  349.         // these equations have no effect on the main state itself
  350.         return null;

  351.     }

  352.     /** Fill Jacobians rows.
  353.      * @param derivatives derivatives of a component of acceleration (along either x, y or z)
  354.      * @param index component index (0 for x, 1 for y, 2 for z)
  355.      * @param freeStateParameters number of free parameters, either 3 (position),
  356.      * 6 (position-velocity) or 7 (position-velocity-mass)
  357.      * @param dAccdPos Jacobian of acceleration with respect to spacecraft position
  358.      * @param dAccdVel Jacobian of acceleration with respect to spacecraft velocity
  359.      */
  360.     private void addToRow(final double[] derivatives, final int index, final int freeStateParameters,
  361.                           final double[][] dAccdPos, final double[][] dAccdVel) {

  362.         for (int i = 0; i < 3; ++i) {
  363.             dAccdPos[index][i] += derivatives[i + 1];
  364.         }
  365.         if (freeStateParameters > 3) {
  366.             for (int i = 0; i < 3; ++i) {
  367.                 dAccdVel[index][i] += derivatives[i + 4];
  368.             }
  369.         }

  370.     }

  371. }