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.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.      * The returned state must be added to the propagator (it is not done
  167.      * automatically, as the user may need to add more states to it).
  168.      * </p>
  169.      * <p>
  170.      * The force models parameters for which partial derivatives are desired,
  171.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  172.      * before this method is called, and the {@code dY1dP} matrix dimension <em>must</em>
  173.      * be consistent with the selection.
  174.      * </p>
  175.      * @param s1 current state
  176.      * @param dY1dY0 Jacobian of current state at time t₁ with respect
  177.      * to state at some previous time t₀ (must be 6x6)
  178.      * @param dY1dP Jacobian of current state at time t₁ with respect
  179.      * to parameters (may be null if no parameters are selected)
  180.      * @return state with initial Jacobians added
  181.      * @see #getSelectedParameters()
  182.      */
  183.     public SpacecraftState setInitialJacobians(final SpacecraftState s1,
  184.                                                final double[][] dY1dY0, final double[][] dY1dP) {

  185.         freezeParametersSelection();

  186.         // Check dimensions
  187.         final int stateDim = dY1dY0.length;
  188.         if (stateDim != 6 || stateDim != dY1dY0[0].length) {
  189.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
  190.                                       stateDim, dY1dY0[0].length);
  191.         }
  192.         if (dY1dP != null && stateDim != dY1dP.length) {
  193.             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
  194.                                       stateDim, dY1dP.length);
  195.         }
  196.         if ((dY1dP == null && selected.getNbParams() != 0) ||
  197.             (dY1dP != null && selected.getNbParams() != dY1dP[0].length)) {
  198.             throw new OrekitException(new OrekitException(OrekitMessages.INITIAL_MATRIX_AND_PARAMETERS_NUMBER_MISMATCH,
  199.                                                           dY1dP == null ? 0 : dY1dP[0].length, selected.getNbParams()));
  200.         }

  201.         // store the matrices as a single dimension array
  202.         initialized = true;
  203.         final JacobiansMapper mapper = getMapper();
  204.         final double[] p = new double[mapper.getAdditionalStateDimension()];
  205.         mapper.setInitialJacobians(s1, dY1dY0, dY1dP, p);

  206.         // set value in propagator
  207.         return s1.addAdditionalState(name, p);

  208.     }

  209.     /** Get a mapper between two-dimensional Jacobians and one-dimensional additional state.
  210.      * @return a mapper between two-dimensional Jacobians and one-dimensional additional state,
  211.      * with the same name as the instance
  212.      * @see #setInitialJacobians(SpacecraftState)
  213.      * @see #setInitialJacobians(SpacecraftState, double[][], double[][])
  214.      */
  215.     public JacobiansMapper getMapper() {
  216.         if (!initialized) {
  217.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_INITIALIZED);
  218.         }
  219.         return new JacobiansMapper(name, selected,
  220.                                    propagator.getOrbitType(),
  221.                                    propagator.getPositionAngleType());
  222.     }

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

  225.         // initialize acceleration Jacobians to zero
  226.         final int paramDim = selected.getNbParams();
  227.         final int dim = 3;
  228.         final double[][] dAccdParam = new double[dim][paramDim];
  229.         final double[][] dAccdPos   = new double[dim][dim];
  230.         final double[][] dAccdVel   = new double[dim][dim];

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

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

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

  238.             final FieldVector3D<Gradient> acceleration = forceModel.acceleration(dsState, parameters);
  239.             final double[] derivativesX = acceleration.getX().getGradient();
  240.             final double[] derivativesY = acceleration.getY().getGradient();
  241.             final double[] derivativesZ = acceleration.getZ().getGradient();

  242.             // update Jacobians with respect to state
  243.             addToRow(derivativesX, 0, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
  244.             addToRow(derivativesY, 1, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
  245.             addToRow(derivativesZ, 2, converter.getFreeStateParameters(), dAccdPos, dAccdVel);

  246.             int index = converter.getFreeStateParameters();
  247.             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
  248.                 if (driver.isSelected()) {
  249.                     final int parameterIndex = map.get(driver);
  250.                     dAccdParam[0][parameterIndex] += derivativesX[index];
  251.                     dAccdParam[1][parameterIndex] += derivativesY[index];
  252.                     dAccdParam[2][parameterIndex] += derivativesZ[index];
  253.                     ++index;
  254.                 }
  255.             }

  256.         }

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

  258.         // [        |        ]   [                 |                  ]   [     |     ]
  259.         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
  260.         // [        |        ]   [                 |                  ]   [     |     ]
  261.         // ---------+---------   ------------------+------------------- * ------+------
  262.         // [        |        ]   [                 |                  ]   [     |     ]
  263.         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
  264.         // [        |        ]   [                 |                  ]   [     |     ]

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

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

  268.         //     [ Adot ] = [ C ]
  269.         //     [ Bdot ] = [ D ]
  270.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  271.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

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

  275.         // copy C and E into Adot and Bdot
  276.         final int stateDim = 6;
  277.         final double[] p = s.getAdditionalState(getName());
  278.         System.arraycopy(p, dim * stateDim, pDot, 0, dim * stateDim);

  279.         // compute Cdot and Ddot
  280.         for (int i = 0; i < dim; ++i) {
  281.             final double[] dAdPi = dAccdPos[i];
  282.             final double[] dAdVi = dAccdVel[i];
  283.             for (int j = 0; j < stateDim; ++j) {
  284.                 pDot[(dim + i) * stateDim + j] =
  285.                     dAdPi[0] * p[j]                + dAdPi[1] * p[j +     stateDim] + dAdPi[2] * p[j + 2 * stateDim] +
  286.                     dAdVi[0] * p[j + 3 * stateDim] + dAdVi[1] * p[j + 4 * stateDim] + dAdVi[2] * p[j + 5 * stateDim];
  287.             }
  288.         }

  289.         for (int k = 0; k < paramDim; ++k) {
  290.             // the variational equations of the parameters Jacobian matrix are computed
  291.             // one column at a time, they have the following form:
  292.             // [      ]   [                 |                  ]   [   ]   [                  ]
  293.             // [ Edot ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [ E ]   [  dVel/dParam = 0 ]
  294.             // [      ]   [                 |                  ]   [   ]   [                  ]
  295.             // --------   ------------------+------------------- * ----- + --------------------
  296.             // [      ]   [                 |                  ]   [   ]   [                  ]
  297.             // [ Fdot ] = [    dAcc/dPos    |     dAcc/dVel    ]   [ F ]   [    dAcc/dParam   ]
  298.             // [      ]   [                 |                  ]   [   ]   [                  ]

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

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

  302.             //     [ Edot ] = [ F ]
  303.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

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

  307.             // copy F into Edot
  308.             final int columnTop = stateDim * stateDim + k;
  309.             pDot[columnTop]                = p[columnTop + 3 * paramDim];
  310.             pDot[columnTop +     paramDim] = p[columnTop + 4 * paramDim];
  311.             pDot[columnTop + 2 * paramDim] = p[columnTop + 5 * paramDim];

  312.             // compute Fdot
  313.             for (int i = 0; i < dim; ++i) {
  314.                 final double[] dAdPi = dAccdPos[i];
  315.                 final double[] dAdVi = dAccdVel[i];
  316.                 pDot[columnTop + (dim + i) * paramDim] =
  317.                     dAccdParam[i][k] +
  318.                     dAdPi[0] * p[columnTop]                + dAdPi[1] * p[columnTop +     paramDim] + dAdPi[2] * p[columnTop + 2 * paramDim] +
  319.                     dAdVi[0] * p[columnTop + 3 * paramDim] + dAdVi[1] * p[columnTop + 4 * paramDim] + dAdVi[2] * p[columnTop + 5 * paramDim];
  320.             }

  321.         }

  322.         // these equations have no effect on the main state itself
  323.         return null;

  324.     }

  325.     /** Get the flag for the initialization of the state jacobian.
  326.      * @return true if the state jacobian is initialized
  327.      * @since 10.2
  328.      */
  329.     public boolean isInitialize() {
  330.         return initialized;
  331.     }

  332.     /** Fill Jacobians rows.
  333.      * @param derivatives derivatives of a component of acceleration (along either x, y or z)
  334.      * @param index component index (0 for x, 1 for y, 2 for z)
  335.      * @param freeStateParameters number of free parameters, either 3 (position),
  336.      * 6 (position-velocity) or 7 (position-velocity-mass)
  337.      * @param dAccdPos Jacobian of acceleration with respect to spacecraft position
  338.      * @param dAccdVel Jacobian of acceleration with respect to spacecraft velocity
  339.      */
  340.     private void addToRow(final double[] derivatives, final int index, final int freeStateParameters,
  341.                           final double[][] dAccdPos, final double[][] dAccdVel) {

  342.         for (int i = 0; i < 3; ++i) {
  343.             dAccdPos[index][i] += derivatives[i];
  344.         }
  345.         if (freeStateParameters > 3) {
  346.             for (int i = 0; i < 3; ++i) {
  347.                 dAccdVel[index][i] += derivatives[i + 3];
  348.             }
  349.         }

  350.     }

  351. }