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.semianalytical.dsst.forces.DSSTForceModel;
  31. import org.orekit.propagation.semianalytical.dsst.utilities.FieldAuxiliaryElements;
  32. import org.orekit.utils.DoubleArrayDictionary;
  33. import org.orekit.utils.ParameterDriver;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  132.     }

  133.     /** {@inheritDoc} */
  134.     public double[] derivatives(final SpacecraftState state) {

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

  137.         // perform matrix multiplication with matrices flatten
  138.         final RealMatrix factor = computePartials(state);
  139.         int index = 0;
  140.         for (int i = 0; i < STATE_DIMENSION; ++i) {
  141.             for (int j = 0; j < STATE_DIMENSION; ++j) {
  142.                 double sum = 0;
  143.                 for (int k = 0; k < STATE_DIMENSION; ++k) {
  144.                     sum += factor.getEntry(i, k) * p[j + k * STATE_DIMENSION];
  145.                 }
  146.                 res[index++] = sum;
  147.             }
  148.         }

  149.         return res;

  150.     }

  151.     /** Compute the various partial derivatives.
  152.      * @param state current spacecraft state
  153.      * @return factor matrix
  154.      */
  155.     private RealMatrix computePartials(final SpacecraftState state) {

  156.         // set up containers for partial derivatives
  157.         final RealMatrix            factor               = MatrixUtils.createRealMatrix(STATE_DIMENSION, STATE_DIMENSION);
  158.         final DoubleArrayDictionary meanElementsPartials = new DoubleArrayDictionary();
  159.         final DSSTGradientConverter converter            = new DSSTGradientConverter(state, attitudeProvider);

  160.         // Compute Jacobian
  161.         for (final DSSTForceModel forceModel : forceModels) {

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

  165.             final Gradient[] meanElementRate = forceModel.getMeanElementRate(dsState, auxiliaryElements, parameters);
  166.             final double[] derivativesA  = meanElementRate[0].getGradient();
  167.             final double[] derivativesEx = meanElementRate[1].getGradient();
  168.             final double[] derivativesEy = meanElementRate[2].getGradient();
  169.             final double[] derivativesHx = meanElementRate[3].getGradient();
  170.             final double[] derivativesHy = meanElementRate[4].getGradient();
  171.             final double[] derivativesL  = meanElementRate[5].getGradient();

  172.             // update Jacobian with respect to state
  173.             addToRow(derivativesA,  0, factor);
  174.             addToRow(derivativesEx, 1, factor);
  175.             addToRow(derivativesEy, 2, factor);
  176.             addToRow(derivativesHx, 3, factor);
  177.             addToRow(derivativesHy, 4, factor);
  178.             addToRow(derivativesL,  5, factor);

  179.             // partials derivatives with respect to parameters
  180.             int paramsIndex = converter.getFreeStateParameters();
  181.             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
  182.                 if (driver.isSelected()) {

  183.                     // get the partials derivatives for this driver
  184.                     DoubleArrayDictionary.Entry entry = meanElementsPartials.getEntry(driver.getName());
  185.                     if (entry == null) {
  186.                         // create an entry filled with zeroes
  187.                         meanElementsPartials.put(driver.getName(), new double[STATE_DIMENSION]);
  188.                         entry = meanElementsPartials.getEntry(driver.getName());
  189.                     }

  190.                     // add the contribution of the current force model
  191.                     entry.increment(new double[] {
  192.                         derivativesA[paramsIndex], derivativesEx[paramsIndex], derivativesEy[paramsIndex],
  193.                         derivativesHx[paramsIndex], derivativesHy[paramsIndex], derivativesL[paramsIndex]
  194.                     });
  195.                     ++paramsIndex;

  196.                 }
  197.             }

  198.         }

  199.         // notify observers
  200.         for (Map.Entry<String, DSSTPartialsObserver> observersEntry : partialsObservers.entrySet()) {
  201.             final DoubleArrayDictionary.Entry entry = meanElementsPartials.getEntry(observersEntry.getKey());
  202.             observersEntry.getValue().partialsComputed(state, factor, entry == null ? new double[STATE_DIMENSION] : entry.getValue());
  203.         }

  204.         return factor;

  205.     }

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

  216.     /** Interface for observing partials derivatives. */
  217.     public interface DSSTPartialsObserver {

  218.         /** Callback called when partial derivatives have been computed.
  219.          * @param state current spacecraft state
  220.          * @param factor factor matrix
  221.          * @param meanElementsPartials partials derivatives of mean elements rates with respect to the parameter driver
  222.          * that was registered (zero if no parameters were not selected or parameter is unknown)
  223.          */
  224.         void partialsComputed(SpacecraftState state, RealMatrix factor, double[] meanElementsPartials);

  225.     }

  226. }