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.time.AbsoluteDate;
  29. import org.orekit.utils.ParameterDriver;
  30. import org.orekit.utils.ParameterDriversList;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  136.         }
  137.     }

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

  151.     /** Set the initial value of the Jacobian with respect to state and parameter.
  152.      * <p>
  153.      * This method is equivalent to call {@link #setInitialJacobians(SpacecraftState,
  154.      * double[][], double[][])} with dYdY0 set to the identity matrix and dYdP set
  155.      * to a zero matrix.
  156.      * </p>
  157.      * <p>
  158.      * The force models parameters for which partial derivatives are desired,
  159.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  160.      * before this method is called, so proper matrices dimensions are used.
  161.      * </p>
  162.      * @param s0 initial state
  163.      * @return state with initial Jacobians added
  164.      * @see #getSelectedParameters()
  165.      * @since 9.0
  166.      */
  167.     public SpacecraftState setInitialJacobians(final SpacecraftState s0) {
  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.      * The returned state must be added to the propagator (it is not done
  180.      * automatically, as the user may need to add more states to it).
  181.      * </p>
  182.      * <p>
  183.      * The force models parameters for which partial derivatives are desired,
  184.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  185.      * before this method is called, and the {@code dY1dP} matrix dimension <em>must</em>
  186.      * be consistent with the selection.
  187.      * </p>
  188.      * @param s1 current state
  189.      * @param dY1dY0 Jacobian of current state at time t₁ with respect
  190.      * to state at some previous time t₀ (must be 6x6)
  191.      * @param dY1dP Jacobian of current state at time t₁ with respect
  192.      * to parameters (may be null if no parameters are selected)
  193.      * @return state with initial Jacobians added
  194.      * @see #getSelectedParameters()
  195.      */
  196.     public SpacecraftState setInitialJacobians(final SpacecraftState s1,
  197.                                                final double[][] dY1dY0, final double[][] dY1dP) {

  198.         freezeParametersSelection();

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

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

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

  221.     }

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

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

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

  247.     /** {@inheritDoc} */
  248.     public double[] derivatives(final SpacecraftState s) {

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

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

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

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

  262.             final FieldVector3D<Gradient> acceleration = forceModel.acceleration(dsState, parameters);
  263.             final double[] derivativesX = acceleration.getX().getGradient();
  264.             final double[] derivativesY = acceleration.getY().getGradient();
  265.             final double[] derivativesZ = acceleration.getZ().getGradient();

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

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

  280.         }

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

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

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

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

  292.         //     [ Adot ] = [ C ]
  293.         //     [ Bdot ] = [ D ]
  294.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  295.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

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

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

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

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

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

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

  327.             //     [ Edot ] = [ F ]
  328.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

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

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

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

  346.         }

  347.         return pDot;

  348.     }

  349.     /** Get the flag for the initialization of the state jacobian.
  350.      * @return true if the state jacobian is initialized
  351.      * @since 10.2
  352.      */
  353.     public boolean isInitialize() {
  354.         return initialized;
  355.     }

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

  366.         for (int i = 0; i < 3; ++i) {
  367.             dAccdPos[index][i] += derivatives[i];
  368.         }
  369.         if (freeStateParameters > 3) {
  370.             for (int i = 0; i < 3; ++i) {
  371.                 dAccdVel[index][i] += derivatives[i + 3];
  372.             }
  373.         }

  374.     }

  375. }