EpochDerivativesEquations.java

  1. /* Copyright 2002-2020 CS GROUP
  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.forces.gravity.ThirdBodyAttractionEpoch;
  26. import org.orekit.propagation.FieldSpacecraftState;
  27. import org.orekit.propagation.SpacecraftState;
  28. import org.orekit.propagation.integration.AdditionalEquations;
  29. import org.orekit.utils.ParameterDriver;
  30. import org.orekit.utils.ParameterDriversList;

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

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

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

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

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

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

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

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

  100.     /** Freeze the selected parameters from the force models.
  101.      */
  102.     private void freezeParametersSelection() {
  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<>();
  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.      */
  140.     public ParameterDriversList getSelectedParameters() {
  141.         freezeParametersSelection();
  142.         return selected;
  143.     }

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

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

  191.         freezeParametersSelection();

  192.         // Check dimensions
  193.         final int stateDimEpoch = dY1dY0.length;
  194.         if (stateDimEpoch != 6 || stateDimEpoch != dY1dY0[0].length) {
  195.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
  196.                                       stateDimEpoch, dY1dY0[0].length);
  197.         }
  198.         if (dY1dP != null && stateDimEpoch != dY1dP.length) {
  199.             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
  200.                                       stateDimEpoch, dY1dP.length);
  201.         }

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

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

  209.     }

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

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

  224.         // initialize acceleration Jacobians to zero
  225.         final int paramDimEpoch = selected.getNbParams() + 1; // added epoch
  226.         final int dimEpoch      = 3;
  227.         final double[][] dAccdParam = new double[dimEpoch][paramDimEpoch];
  228.         final double[][] dAccdPos   = new double[dimEpoch][dimEpoch];
  229.         final double[][] dAccdVel   = new double[dimEpoch][dimEpoch];

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

  232.         // compute acceleration Jacobians, finishing with the largest force: Newtonian attraction
  233.         for (final ForceModel forceModel : propagator.getAllForceModels()) {
  234.             final NumericalGradientConverter converter = forceModel.dependsOnPositionOnly() ? posOnlyConverter : fullConverter;
  235.             final FieldSpacecraftState<Gradient> dsState = converter.getState(forceModel);
  236.             final Gradient[] parameters = converter.getParameters(dsState, forceModel);

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

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

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

  255.             // Add the derivatives of the acceleration w.r.t. the Epoch
  256.             if (forceModel instanceof ThirdBodyAttractionEpoch) {
  257.                 final double[] parametersValues = new double[] {parameters[0].getValue()};
  258.                 final double[] derivatives = ((ThirdBodyAttractionEpoch) forceModel).getDerivativesToEpoch(s, parametersValues);
  259.                 dAccdParam[0][paramDimEpoch - 1] += derivatives[0];
  260.                 dAccdParam[1][paramDimEpoch - 1] += derivatives[1];
  261.                 dAccdParam[2][paramDimEpoch - 1] += derivatives[2];
  262.             }

  263.         }

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

  265.         // [        |        ]   [                 |                  ]   [     |     ]
  266.         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
  267.         // [        |        ]   [                 |                  ]   [     |     ]
  268.         // ---------+---------   ------------------+------------------- * ------+------
  269.         // [        |        ]   [                 |                  ]   [     |     ]
  270.         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
  271.         // [        |        ]   [                 |                  ]   [     |     ]

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

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

  275.         //     [ Adot ] = [ C ]
  276.         //     [ Bdot ] = [ D ]
  277.         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
  278.         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]

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

  282.         // copy C and E into Adot and Bdot
  283.         final int stateDim = 6;
  284.         final double[] p = s.getAdditionalState(getName());
  285.         System.arraycopy(p, dimEpoch * stateDim, pDot, 0, dimEpoch * stateDim);

  286.         // compute Cdot and Ddot
  287.         for (int i = 0; i < dimEpoch; ++i) {
  288.             final double[] dAdPi = dAccdPos[i];
  289.             final double[] dAdVi = dAccdVel[i];
  290.             for (int j = 0; j < stateDim; ++j) {
  291.                 pDot[(dimEpoch + i) * stateDim + j] =
  292.                     dAdPi[0] * p[j]                + dAdPi[1] * p[j +     stateDim] + dAdPi[2] * p[j + 2 * stateDim] +
  293.                     dAdVi[0] * p[j + 3 * stateDim] + dAdVi[1] * p[j + 4 * stateDim] + dAdVi[2] * p[j + 5 * stateDim];
  294.             }
  295.         }

  296.         for (int k = 0; k < paramDimEpoch; ++k) {
  297.             // the variational equations of the parameters Jacobian matrix are computed
  298.             // one column at a time, they have the following form:
  299.             // [      ]   [                 |                  ]   [   ]   [                  ]
  300.             // [ Edot ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [ E ]   [  dVel/dParam = 0 ]
  301.             // [      ]   [                 |                  ]   [   ]   [                  ]
  302.             // --------   ------------------+------------------- * ----- + --------------------
  303.             // [      ]   [                 |                  ]   [   ]   [                  ]
  304.             // [ Fdot ] = [    dAcc/dPos    |     dAcc/dVel    ]   [ F ]   [    dAcc/dParam   ]
  305.             // [      ]   [                 |                  ]   [   ]   [                  ]

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

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

  309.             //     [ Edot ] = [ F ]
  310.             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]

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

  314.             // copy F into Edot
  315.             final int columnTop = stateDim * stateDim + k;
  316.             pDot[columnTop]                     = p[columnTop + 3 * paramDimEpoch];
  317.             pDot[columnTop +     paramDimEpoch] = p[columnTop + 4 * paramDimEpoch];
  318.             pDot[columnTop + 2 * paramDimEpoch] = p[columnTop + 5 * paramDimEpoch];

  319.             // compute Fdot
  320.             for (int i = 0; i < dimEpoch; ++i) {
  321.                 final double[] dAdP = dAccdPos[i];
  322.                 final double[] dAdV = dAccdVel[i];
  323.                 pDot[columnTop + (dimEpoch + i) * paramDimEpoch] =
  324.                     dAccdParam[i][k] +
  325.                     dAdP[0] * p[columnTop]                     + dAdP[1] * p[columnTop +     paramDimEpoch] + dAdP[2] * p[columnTop + 2 * paramDimEpoch] +
  326.                     dAdV[0] * p[columnTop + 3 * paramDimEpoch] + dAdV[1] * p[columnTop + 4 * paramDimEpoch] + dAdV[2] * p[columnTop + 5 * paramDimEpoch];
  327.             }

  328.         }

  329.         // these equations have no effect on the main state itself
  330.         return null;

  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. }