DSSTStateTransitionMatrixGenerator.java

  1. /* Copyright 2002-2022 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.HashMap;
  19. import java.util.List;
  20. import java.util.Map;

  21. import org.hipparchus.analysis.differentiation.Gradient;
  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.linear.MatrixUtils;
  24. import org.hipparchus.linear.RealMatrix;
  25. import org.orekit.attitudes.AttitudeProvider;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.propagation.FieldSpacecraftState;
  28. import org.orekit.propagation.SpacecraftState;
  29. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  30. import org.orekit.propagation.integration.CombinedDerivatives;
  31. import org.orekit.propagation.semianalytical.dsst.forces.DSSTForceModel;
  32. import org.orekit.propagation.semianalytical.dsst.utilities.FieldAuxiliaryElements;
  33. import org.orekit.utils.DoubleArrayDictionary;
  34. import org.orekit.utils.ParameterDriver;

  35. /** Generator for State Transition Matrix.
  36.  * @author Luc Maisonobe
  37.  * @since 11.1
  38.  */
  39. class DSSTStateTransitionMatrixGenerator implements AdditionalDerivativesProvider {

  40.     /** Space dimension. */
  41.     private static final int SPACE_DIMENSION = 3;

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

  56.     /** State dimension. */
  57.     public static final int STATE_DIMENSION = 2 * SPACE_DIMENSION;

  58.     /** Name of the Cartesian STM additional state. */
  59.     private final String stmName;

  60.     /** Force models used in propagation. */
  61.     private final List<DSSTForceModel> forceModels;

  62.     /** Attitude provider used in propagation. */
  63.     private final AttitudeProvider attitudeProvider;

  64.     /** Observers for partial derivatives. */
  65.     private final Map<String, DSSTPartialsObserver> partialsObservers;

  66.     /** Simple constructor.
  67.      * @param stmName name of the Cartesian STM additional state
  68.      * @param forceModels force models used in propagation
  69.      * @param attitudeProvider attitude provider used in propagation
  70.      */
  71.     DSSTStateTransitionMatrixGenerator(final String stmName, final List<DSSTForceModel> forceModels,
  72.                                        final AttitudeProvider attitudeProvider) {
  73.         this.stmName           = stmName;
  74.         this.forceModels       = forceModels;
  75.         this.attitudeProvider  = attitudeProvider;
  76.         this.partialsObservers = new HashMap<>();
  77.     }

  78.     /** Register an observer for partial derivatives.
  79.      * <p>
  80.      * The observer {@link DSSTPartialsObserver#partialsComputed(double[], double[]) partialsComputed}
  81.      * method will be called when partial derivatives are computed, as a side effect of
  82.      * calling {@link #generate(SpacecraftState)}
  83.      * </p>
  84.      * @param name name of the parameter driver this observer is interested in (may be null)
  85.      * @param observer observer to register
  86.      */
  87.     void addObserver(final String name, final DSSTPartialsObserver observer) {
  88.         partialsObservers.put(name, observer);
  89.     }

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

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public int getDimension() {
  98.         return STATE_DIMENSION * STATE_DIMENSION;
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public boolean yield(final SpacecraftState state) {
  103.         return !state.hasAdditionalState(getName());
  104.     }

  105.     /** Set the initial value of the State Transition Matrix.
  106.      * <p>
  107.      * The returned state must be added to the propagator.
  108.      * </p>
  109.      * @param state initial state
  110.      * @param dYdY0 initial State Transition Matrix ∂Y/∂Y₀,
  111.      * if null (which is the most frequent case), assumed to be 6x6 identity
  112.      * @return state with initial STM (converted to Cartesian ∂C/∂Y₀) added
  113.      */
  114.     SpacecraftState setInitialStateTransitionMatrix(final SpacecraftState state, final RealMatrix dYdY0) {

  115.         if (dYdY0 != null) {
  116.             if (dYdY0.getRowDimension() != STATE_DIMENSION ||
  117.                             dYdY0.getColumnDimension() != STATE_DIMENSION) {
  118.                 throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH_2x2,
  119.                                           dYdY0.getRowDimension(), dYdY0.getColumnDimension(),
  120.                                           STATE_DIMENSION, STATE_DIMENSION);
  121.             }
  122.         }

  123.         // flatten matrix
  124.         final double[] flat = new double[STATE_DIMENSION * STATE_DIMENSION];
  125.         int k = 0;
  126.         for (int i = 0; i < STATE_DIMENSION; ++i) {
  127.             for (int j = 0; j < STATE_DIMENSION; ++j) {
  128.                 flat[k++] = dYdY0.getEntry(i, j);
  129.             }
  130.         }

  131.         // set additional state
  132.         return state.addAdditionalState(stmName, flat);

  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     @Deprecated
  137.     public double[] derivatives(final SpacecraftState state) {
  138.         return combinedDerivatives(state).getAdditionalDerivatives();
  139.     }

  140.     /** {@inheritDoc} */
  141.     public CombinedDerivatives combinedDerivatives(final SpacecraftState state) {

  142.         final double[] p = state.getAdditionalState(getName());
  143.         final double[] res = new double[p.length];

  144.         // perform matrix multiplication with matrices flatten
  145.         final RealMatrix factor = computePartials(state);
  146.         int index = 0;
  147.         for (int i = 0; i < STATE_DIMENSION; ++i) {
  148.             for (int j = 0; j < STATE_DIMENSION; ++j) {
  149.                 double sum = 0;
  150.                 for (int k = 0; k < STATE_DIMENSION; ++k) {
  151.                     sum += factor.getEntry(i, k) * p[j + k * STATE_DIMENSION];
  152.                 }
  153.                 res[index++] = sum;
  154.             }
  155.         }

  156.         return new CombinedDerivatives(res, null);

  157.     }

  158.     /** Compute the various partial derivatives.
  159.      * @param state current spacecraft state
  160.      * @return factor matrix
  161.      */
  162.     private RealMatrix computePartials(final SpacecraftState state) {

  163.         // set up containers for partial derivatives
  164.         final RealMatrix            factor               = MatrixUtils.createRealMatrix(STATE_DIMENSION, STATE_DIMENSION);
  165.         final DoubleArrayDictionary meanElementsPartials = new DoubleArrayDictionary();
  166.         final DSSTGradientConverter converter            = new DSSTGradientConverter(state, attitudeProvider);

  167.         // Compute Jacobian
  168.         for (final DSSTForceModel forceModel : forceModels) {

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

  172.             final Gradient[] meanElementRate = forceModel.getMeanElementRate(dsState, auxiliaryElements, parameters);
  173.             final double[] derivativesA  = meanElementRate[0].getGradient();
  174.             final double[] derivativesEx = meanElementRate[1].getGradient();
  175.             final double[] derivativesEy = meanElementRate[2].getGradient();
  176.             final double[] derivativesHx = meanElementRate[3].getGradient();
  177.             final double[] derivativesHy = meanElementRate[4].getGradient();
  178.             final double[] derivativesL  = meanElementRate[5].getGradient();

  179.             // update Jacobian with respect to state
  180.             addToRow(derivativesA,  0, factor);
  181.             addToRow(derivativesEx, 1, factor);
  182.             addToRow(derivativesEy, 2, factor);
  183.             addToRow(derivativesHx, 3, factor);
  184.             addToRow(derivativesHy, 4, factor);
  185.             addToRow(derivativesL,  5, factor);

  186.             // partials derivatives with respect to parameters
  187.             int paramsIndex = converter.getFreeStateParameters();
  188.             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
  189.                 if (driver.isSelected()) {

  190.                     // get the partials derivatives for this driver
  191.                     DoubleArrayDictionary.Entry entry = meanElementsPartials.getEntry(driver.getName());
  192.                     if (entry == null) {
  193.                         // create an entry filled with zeroes
  194.                         meanElementsPartials.put(driver.getName(), new double[STATE_DIMENSION]);
  195.                         entry = meanElementsPartials.getEntry(driver.getName());
  196.                     }

  197.                     // add the contribution of the current force model
  198.                     entry.increment(new double[] {
  199.                         derivativesA[paramsIndex], derivativesEx[paramsIndex], derivativesEy[paramsIndex],
  200.                         derivativesHx[paramsIndex], derivativesHy[paramsIndex], derivativesL[paramsIndex]
  201.                     });
  202.                     ++paramsIndex;

  203.                 }
  204.             }

  205.         }

  206.         // notify observers
  207.         for (Map.Entry<String, DSSTPartialsObserver> observersEntry : partialsObservers.entrySet()) {
  208.             final DoubleArrayDictionary.Entry entry = meanElementsPartials.getEntry(observersEntry.getKey());
  209.             observersEntry.getValue().partialsComputed(state, factor, entry == null ? new double[STATE_DIMENSION] : entry.getValue());
  210.         }

  211.         return factor;

  212.     }

  213.     /** Fill Jacobians rows.
  214.      * @param derivatives derivatives of a component
  215.      * @param index component index (0 for a, 1 for ex, 2 for ey, 3 for hx, 4 for hy, 5 for l)
  216.      * @param factor Jacobian of mean elements rate with respect to mean elements
  217.      */
  218.     private void addToRow(final double[] derivatives, final int index, final RealMatrix factor) {
  219.         for (int i = 0; i < 6; i++) {
  220.             factor.addToEntry(index, i, derivatives[i]);
  221.         }
  222.     }

  223.     /** Interface for observing partials derivatives. */
  224.     public interface DSSTPartialsObserver {

  225.         /** Callback called when partial derivatives have been computed.
  226.          * @param state current spacecraft state
  227.          * @param factor factor matrix
  228.          * @param meanElementsPartials partials derivatives of mean elements rates with respect to the parameter driver
  229.          * that was registered (zero if no parameters were not selected or parameter is unknown)
  230.          */
  231.         void partialsComputed(SpacecraftState state, RealMatrix factor, double[] meanElementsPartials);

  232.     }

  233. }