DSSTPartialDerivativesEquations.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.semianalytical.dsst;

  18. import java.util.IdentityHashMap;
  19. import java.util.Map;

  20. import org.hipparchus.analysis.differentiation.Gradient;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.propagation.FieldSpacecraftState;
  24. import org.orekit.propagation.PropagationType;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.integration.AdditionalEquations;
  27. import org.orekit.propagation.semianalytical.dsst.forces.DSSTForceModel;
  28. import org.orekit.propagation.semianalytical.dsst.utilities.FieldAuxiliaryElements;
  29. import org.orekit.utils.ParameterDriver;
  30. import org.orekit.utils.ParameterDriversList;

  31. /** Set of {@link AdditionalEquations additional equations} 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 DSSTPropagator DSST 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 are dimension 6 (orbit only).
  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 DSSTForceModel DSST force models} level. Users must retrieve a {@link ParameterDriver
  45.  * parameter driver} by looping on all drivers using {@link DSSTForceModel#getParametersDrivers()}
  46.  * and then select it by calling {@link ParameterDriver#setSelected(boolean) setSelected(true)}.
  47.  * </p>
  48.  * @author Bryan Cazabonne
  49.  * @since 10.0
  50.  */
  51. public class DSSTPartialDerivativesEquations implements AdditionalEquations {

  52.     /** Retrograde factor I.
  53.      *  <p>
  54.      *  DSST model needs equinoctial orbit as internal representation.
  55.      *  Classical equinoctial elements have discontinuities when inclination
  56.      *  is close to zero. In this representation, I = +1. <br>
  57.      *  To avoid this discontinuity, another representation exists and equinoctial
  58.      *  elements can be expressed in a different way, called "retrograde" orbit.
  59.      *  This implies I = -1. <br>
  60.      *  As Orekit doesn't implement the retrograde orbit, I is always set to +1.
  61.      *  But for the sake of consistency with the theory, the retrograde factor
  62.      *  has been kept in the formulas.
  63.      *  </p>
  64.      */
  65.     private static final int I = 1;

  66.     /** Propagator computing state evolution. */
  67.     private final DSSTPropagator propagator;

  68.     /** Selected parameters for Jacobian computation. */
  69.     private ParameterDriversList selected;

  70.     /** Parameters map. */
  71.     private Map<ParameterDriver, Integer> map;

  72.     /** Name. */
  73.     private final String name;

  74.     /** Flag for Jacobian matrices initialization. */
  75.     private boolean initialized;

  76.     /** Type of the orbit used for the propagation.*/
  77.     private PropagationType propagationType;

  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.      * DSSTPropagator#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.      * @param propagationType type of the orbit used for the propagation (mean or osculating)
  88.      */
  89.     public DSSTPartialDerivativesEquations(final String name,
  90.                                            final DSSTPropagator propagator,
  91.                                            final PropagationType propagationType) {
  92.         this.name                   = name;
  93.         this.selected               = null;
  94.         this.map                    = null;
  95.         this.propagator             = propagator;
  96.         this.initialized            = false;
  97.         this.propagationType        = propagationType;
  98.         propagator.addAdditionalEquations(this);
  99.     }

  100.     /** {@inheritDoc} */
  101.     public String getName() {
  102.         return name;
  103.     }

  104.     /** Freeze the selected parameters from the force models.
  105.      */
  106.     private void freezeParametersSelection() {
  107.         if (selected == null) {

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

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

  118.             // third pass: sort parameters lexicographically
  119.             selected.sort();

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

  133.         }
  134.     }

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

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

  181.         freezeParametersSelection();

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

  197.         // store the matrices as a single dimension array
  198.         initialized = true;
  199.         final DSSTJacobiansMapper mapper = getMapper();
  200.         final double[] p = new double[mapper.getAdditionalStateDimension()];
  201.         mapper.setInitialJacobians(s1, dY1dY0, dY1dP, p);

  202.         // set value in propagator
  203.         return s1.addAdditionalState(name, p);

  204.     }

  205.     /** Get a mapper between two-dimensional Jacobians and one-dimensional additional state.
  206.      * @return a mapper between two-dimensional Jacobians and one-dimensional additional state,
  207.      * with the same name as the instance
  208.      * @see #setInitialJacobians(SpacecraftState)
  209.      * @see #setInitialJacobians(SpacecraftState, double[][], double[][])
  210.      */
  211.     public DSSTJacobiansMapper getMapper() {
  212.         if (!initialized) {
  213.             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_INITIALIZED);
  214.         }
  215.         return new DSSTJacobiansMapper(name, selected, propagator, map, propagationType);
  216.     }

  217.     /** Get the selected parameters, in Jacobian matrix column order.
  218.      * <p>
  219.      * The force models parameters for which partial derivatives are desired,
  220.      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
  221.      * before this method is called, so the proper list is returned.
  222.      * </p>
  223.      * @return selected parameters, in Jacobian matrix column order which
  224.      * is lexicographic order
  225.      */
  226.     public ParameterDriversList getSelectedParameters() {
  227.         freezeParametersSelection();
  228.         return selected;
  229.     }

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

  232.         // initialize Jacobians to zero
  233.         final int paramDim = selected.getNbParams();
  234.         final int dim = 6;
  235.         final double[][] dMeanElementRatedParam   = new double[dim][paramDim];
  236.         final double[][] dMeanElementRatedElement = new double[dim][dim];
  237.         final DSSTGradientConverter converter = new DSSTGradientConverter(s, propagator.getAttitudeProvider());

  238.         // Compute Jacobian
  239.         for (final DSSTForceModel forceModel : propagator.getAllForceModels()) {

  240.             final FieldSpacecraftState<Gradient> dsState = converter.getState(forceModel);
  241.             final Gradient[] parameters = converter.getParameters(dsState, forceModel);
  242.             final FieldAuxiliaryElements<Gradient> auxiliaryElements = new FieldAuxiliaryElements<>(dsState.getOrbit(), I);

  243.             // "field" initialization of the force model if it was not done before
  244.             forceModel.initialize(auxiliaryElements, propagationType, parameters);
  245.             final Gradient[] meanElementRate = forceModel.getMeanElementRate(dsState, auxiliaryElements, parameters);
  246.             final double[] derivativesA  = meanElementRate[0].getGradient();
  247.             final double[] derivativesEx = meanElementRate[1].getGradient();
  248.             final double[] derivativesEy = meanElementRate[2].getGradient();
  249.             final double[] derivativesHx = meanElementRate[3].getGradient();
  250.             final double[] derivativesHy = meanElementRate[4].getGradient();
  251.             final double[] derivativesL  = meanElementRate[5].getGradient();

  252.             // update Jacobian with respect to state
  253.             addToRow(derivativesA,  0, dMeanElementRatedElement);
  254.             addToRow(derivativesEx, 1, dMeanElementRatedElement);
  255.             addToRow(derivativesEy, 2, dMeanElementRatedElement);
  256.             addToRow(derivativesHx, 3, dMeanElementRatedElement);
  257.             addToRow(derivativesHy, 4, dMeanElementRatedElement);
  258.             addToRow(derivativesL,  5, dMeanElementRatedElement);

  259.             int index = converter.getFreeStateParameters();
  260.             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
  261.                 if (driver.isSelected()) {
  262.                     final int parameterIndex = map.get(driver);
  263.                     dMeanElementRatedParam[0][parameterIndex] += derivativesA[index];
  264.                     dMeanElementRatedParam[1][parameterIndex] += derivativesEx[index];
  265.                     dMeanElementRatedParam[2][parameterIndex] += derivativesEy[index];
  266.                     dMeanElementRatedParam[3][parameterIndex] += derivativesHx[index];
  267.                     dMeanElementRatedParam[4][parameterIndex] += derivativesHy[index];
  268.                     dMeanElementRatedParam[5][parameterIndex] += derivativesL[index];
  269.                     ++index;
  270.                 }
  271.             }

  272.         }

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

  274.         //                     [ Adot ] = [ dMeanElementRatedElement ] * [ A ]

  275.         // The A matrix and its derivative (Adot) are 6 * 6 matrices

  276.         // The following loops compute these expression taking care of the mapping of the
  277.         // A matrix into the single dimension array p and of the mapping of the
  278.         // Adot matrix into the single dimension array pDot.

  279.         final double[] p = s.getAdditionalState(getName());

  280.         for (int i = 0; i < dim; i++) {
  281.             final double[] dMeanElementRatedElementi = dMeanElementRatedElement[i];
  282.             for (int j = 0; j < dim; j++) {
  283.                 pDot[j + dim * i] =
  284.                     dMeanElementRatedElementi[0] * p[j]           + dMeanElementRatedElementi[1] * p[j +     dim] + dMeanElementRatedElementi[2] * p[j + 2 * dim] +
  285.                     dMeanElementRatedElementi[3] * p[j + 3 * dim] + dMeanElementRatedElementi[4] * p[j + 4 * dim] + dMeanElementRatedElementi[5] * p[j + 5 * dim];
  286.             }
  287.         }

  288.         final int columnTop = dim * dim;
  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.             //             [ Bdot ] = [ dMeanElementRatedElement ] * [ B ] + [ dMeanElementRatedParam ]

  293.             // The B sub-columns and its derivative (Bdot) are 6 elements columns.

  294.             // The following loops compute this expression taking care of the mapping of the
  295.             // B columns into the single dimension array p and of the mapping of the
  296.             // Bdot columns into the single dimension array pDot.

  297.             for (int i = 0; i < dim; ++i) {
  298.                 final double[] dMeanElementRatedElementi = dMeanElementRatedElement[i];
  299.                 pDot[columnTop + (i + dim * k)] =
  300.                     dMeanElementRatedParam[i][k] +
  301.                     dMeanElementRatedElementi[0] * p[columnTop + k]                + dMeanElementRatedElementi[1] * p[columnTop + k +     paramDim] + dMeanElementRatedElementi[2] * p[columnTop + k + 2 * paramDim] +
  302.                     dMeanElementRatedElementi[3] * p[columnTop + k + 3 * paramDim] + dMeanElementRatedElementi[4] * p[columnTop + k + 4 * paramDim] + dMeanElementRatedElementi[5] * p[columnTop + k + 5 * paramDim];
  303.             }
  304.         }

  305.         // these equations have no effect on the main state itself
  306.         return null;

  307.     }

  308.     /** Fill Jacobians rows.
  309.      * @param derivatives derivatives of a component
  310.      * @param index component index (0 for a, 1 for ex, 2 for ey, 3 for hx, 4 for hy, 5 for l)
  311.      * @param dMeanElementRatedElement Jacobian of mean elements rate with respect to mean elements
  312.      */
  313.     private void addToRow(final double[] derivatives, final int index,
  314.                           final double[][] dMeanElementRatedElement) {

  315.         for (int i = 0; i < 6; i++) {
  316.             dMeanElementRatedElement[index][i] += derivatives[i];
  317.         }

  318.     }

  319. }