AbstractMatricesHarvester.java

  1. /* Copyright 2002-2025 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;

  18. import java.util.List;

  19. import org.hipparchus.linear.MatrixUtils;
  20. import org.hipparchus.linear.RealMatrix;
  21. import org.orekit.orbits.PositionAngleType;
  22. import org.orekit.utils.DoubleArrayDictionary;

  23. /** Base harvester between two-dimensional Jacobian matrices and one-dimensional {@link
  24.  * SpacecraftState#getAdditionalState(String) additional state arrays}.
  25.  * @author Luc Maisonobe
  26.  * @since 11.1
  27.  */
  28. public abstract class AbstractMatricesHarvester implements MatricesHarvester {

  29.     /** State dimension, fixed to 6.
  30.      * @deprecated as of 13.1, use DEFAULT_STATE_DIMENSION */
  31.     @Deprecated
  32.     public static final int STATE_DIMENSION = 6;

  33.     /** Default state dimension, equivalent to position and velocity vectors. */
  34.     public static final int DEFAULT_STATE_DIMENSION = 6;

  35.     /** Identity conversion matrix for Cartesian-like coordinates. */
  36.     private static final double[][] IDENTITY6 = {
  37.         { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
  38.         { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
  39.         { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
  40.         { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
  41.         { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
  42.         { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
  43.     };

  44.     /** Initial State Transition Matrix. */
  45.     private final RealMatrix initialStm;

  46.     /** Initial columns of the Jacobians matrix with respect to parameters. */
  47.     private final DoubleArrayDictionary initialJacobianColumns;

  48.     /** State Transition Matrix state name. */
  49.     private final String stmName;

  50.     /** Simple constructor.
  51.      * <p>
  52.      * The arguments for initial matrices <em>must</em> be compatible with the {@link org.orekit.orbits.OrbitType orbit type}
  53.      * and {@link PositionAngleType position angle} that will be used by propagator
  54.      * </p>
  55.      * @param stmName State Transition Matrix state name
  56.      * @param initialStm initial State Transition Matrix ∂Y/∂Y₀,
  57.      * if null (which is the most frequent case), assumed to be 6x6 identity
  58.      * @param initialJacobianColumns initial columns of the Jacobians matrix with respect to parameters,
  59.      * if null or if some selected parameters are missing from the dictionary, the corresponding
  60.      * initial column is assumed to be 0
  61.      */
  62.     protected AbstractMatricesHarvester(final String stmName, final RealMatrix initialStm, final DoubleArrayDictionary initialJacobianColumns) {
  63.         this.stmName                = stmName;
  64.         this.initialStm             = initialStm == null ? MatrixUtils.createRealIdentityMatrix(DEFAULT_STATE_DIMENSION) : initialStm;
  65.         this.initialJacobianColumns = initialJacobianColumns == null ? new DoubleArrayDictionary() : initialJacobianColumns;
  66.     }

  67.     /**
  68.      * Getter for the state dimension.
  69.      * @return state dimension
  70.      * @since 13.1
  71.      */
  72.     public int getStateDimension() {
  73.         return initialStm.getColumnDimension();
  74.     }

  75.     /** Get the State Transition Matrix state name.
  76.      * @return State Transition Matrix state name
  77.      */
  78.     public String getStmName() {
  79.         return stmName;
  80.     }

  81.     /** Get the initial State Transition Matrix.
  82.      * @return initial State Transition Matrix
  83.      */
  84.     public RealMatrix getInitialStateTransitionMatrix() {
  85.         return initialStm;
  86.     }

  87.     /** Get the initial column of Jacobian matrix with respect to named parameter.
  88.      * @param columnName name of the column
  89.      * @return initial column of the Jacobian matrix
  90.      */
  91.     public double[] getInitialJacobianColumn(final String columnName) {
  92.         final DoubleArrayDictionary.Entry entry = initialJacobianColumns.getEntry(columnName);
  93.         return entry == null ? new double[getStateDimension()] : entry.getValue();
  94.     }

  95.     /** Convert a flattened array to a square matrix.
  96.      * @param array input array
  97.      * @return the corresponding matrix
  98.      * @since 13.1
  99.      */
  100.     protected RealMatrix toSquareMatrix(final double[] array) {
  101.         final int stateDimension = getStateDimension();
  102.         final RealMatrix matrix = MatrixUtils.createRealMatrix(stateDimension, stateDimension);
  103.         int index = 0;
  104.         for (int i = 0; i < stateDimension; ++i) {
  105.             for (int j = 0; j < stateDimension; ++j) {
  106.                 matrix.setEntry(i, j, array[index++]);
  107.             }
  108.         }
  109.         return matrix;
  110.     }

  111.     /** Set the STM data into an array.
  112.      * @param matrix STM matrix
  113.      * @return an array containing the STM data
  114.      * @since 13.1
  115.      */
  116.     protected double[] toArray(final double[][] matrix) {
  117.         final int stateDimension = matrix.length;
  118.         final double[] array = new double[stateDimension * stateDimension];
  119.         int index = 0;
  120.         for (int i = 0; i < stateDimension; ++i) {
  121.             final double[] row = matrix[i];
  122.             for (int j = 0; j < stateDimension; ++j) {
  123.                 array[index++] = row[j];
  124.             }
  125.         }
  126.         return array;
  127.     }

  128.     /** Get the conversion Jacobian between state parameters and parameters used for derivatives.
  129.      * <p>
  130.      * The base implementation returns identity, which is suitable for DSST and TLE propagators,
  131.      * as state parameters and parameters used for derivatives are the same.
  132.      * </p>
  133.      * <p>
  134.      * For Numerical propagator, parameters used for derivatives are Cartesian
  135.      * and they can be different from state parameters because the numerical propagator can accept different type
  136.      * of orbits, so the method is overridden in derived classes.
  137.      * </p>
  138.      * @param state spacecraft state
  139.      * @return conversion Jacobian
  140.      */
  141.     protected double[][] getConversionJacobian(final SpacecraftState state) {
  142.         return IDENTITY6;
  143.     }

  144.     /** {@inheritDoc} */
  145.     @Override
  146.     public void setReferenceState(final SpacecraftState reference) {
  147.         // nothing to do
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public RealMatrix getStateTransitionMatrix(final SpacecraftState state) {

  152.         if (!state.hasAdditionalData(stmName)) {
  153.             return null;
  154.         }

  155.         // get the conversion Jacobian
  156.         final double[][] dYdC = getConversionJacobian(state);

  157.         // extract the additional state
  158.         final double[] p = state.getAdditionalState(stmName);

  159.         // compute dYdY0 = dYdC * dCdY0
  160.         final int stateDimension = getStateDimension();
  161.         final RealMatrix  dYdY0 = MatrixUtils.createRealMatrix(stateDimension, stateDimension);
  162.         for (int i = 0; i < stateDimension; i++) {
  163.             final double[] rowC = dYdC[i];
  164.             for (int j = 0; j < stateDimension; ++j) {
  165.                 double sum = 0;
  166.                 int pIndex = j;
  167.                 for (int k = 0; k < stateDimension; ++k) {
  168.                     sum += rowC[k] * p[pIndex];
  169.                     pIndex += stateDimension;
  170.                 }
  171.                 dYdY0.setEntry(i, j, sum);
  172.             }
  173.         }

  174.         return dYdY0;

  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public RealMatrix getParametersJacobian(final SpacecraftState state) {

  179.         final List<String> columnsNames = getJacobiansColumnsNames();

  180.         if (columnsNames == null || columnsNames.isEmpty()) {
  181.             return null;
  182.         }

  183.         // get the conversion Jacobian
  184.         final RealMatrix dYdC = MatrixUtils.createRealIdentityMatrix(getStateDimension());
  185.         dYdC.setSubMatrix(getConversionJacobian(state), 0, 0);

  186.         // compute dYdP = dYdC * dCdP
  187.         final RealMatrix dYdP = MatrixUtils.createRealMatrix(getStateDimension(), columnsNames.size());
  188.         for (int j = 0; j < columnsNames.size(); j++) {
  189.             final double[] p = state.getAdditionalState(columnsNames.get(j));
  190.             for (int i = 0; i < getStateDimension(); ++i) {
  191.                 final double[] dYdCi = dYdC.getRow(i);
  192.                 double sum = 0;
  193.                 for (int k = 0; k < getStateDimension(); ++k) {
  194.                     sum += dYdCi[k] * p[k];
  195.                 }
  196.                 dYdP.setEntry(i, j, sum);
  197.             }
  198.         }

  199.         return dYdP;

  200.     }

  201.     /** Freeze the names of the Jacobian columns.
  202.      * <p>
  203.      * This method is called when propagation starts, i.e. when configuration is completed
  204.      * </p>
  205.      */
  206.     public abstract void freezeColumnsNames();

  207. }