1   /* Copyright 2002-2026 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  
19  import org.hipparchus.linear.MatrixUtils;
20  import org.hipparchus.linear.RealMatrix;
21  import org.orekit.orbits.OrbitParamsType;
22  import org.orekit.orbits.PositionAngleType;
23  import org.orekit.utils.DoubleArrayDictionary;
24  
25  /** Base harvester between two-dimensional Jacobian matrices and one-dimensional {@link
26   * SpacecraftState#getAdditionalState(String) additional state arrays}.
27   * @author Luc Maisonobe
28   * @since 11.1
29   */
30  public abstract class AbstractMatricesHarvester implements MatricesHarvester {
31  
32      /** Default state dimension, equivalent to position and velocity vectors. */
33      public static final int DEFAULT_STATE_DIMENSION = 6;
34  
35      /** State Transition Matrix state name. */
36      private String stmName;
37  
38      /** Initial State Transition Matrix. */
39      private RealMatrix initialStm;
40  
41      /** Initial columns of the Jacobians matrix with respect to parameters. */
42      private DoubleArrayDictionary initialJacobianColumns;
43  
44      /** Empty constructor.
45       * <p>
46       * This constructor is not strictly necessary, but it prevents spurious
47       * javadoc warnings with JDK 18 and later.
48       * </p>
49       * @since 14.0
50       */
51      public AbstractMatricesHarvester() {
52          // nothing to do
53      }
54  
55      /** Set the initial State Transition Matrix.
56       * <p>
57       * The arguments for initial matrices <em>must</em> be compatible with the
58       * {@link OrbitParamsType orbit type} and
59       * {@link PositionAngleType position angle} that will be used by propagator,
60       * which may be different from input and output
61       * </p>
62       * @param name State Transition Matrix state name
63       * @param stm  initial State Transition Matrix āˆ‚Y/āˆ‚Iā‚€
64       *             if null (which is the most frequent case), input and output
65       *             orbit types are assumed to be identical and the matrix is
66       *             assumed to be the 6x6 identity
67       */
68      protected void setInitialStm(final String name, final RealMatrix stm) {
69          this.stmName    = name;
70          this.initialStm = stm == null ?
71                            MatrixUtils.createRealIdentityMatrix(DEFAULT_STATE_DIMENSION) :
72                            stm;
73      }
74  
75      /** Set the initial columns of the Jacobians matrix with respect to parameters.
76       * <p>
77       * The arguments for initial matrices <em>must</em> be compatible with the
78       * {@link OrbitParamsType orbit type} and
79       * {@link PositionAngleType position angle} that will be used by propagator
80       * </p>
81       * @param initialJacobianColumns initial columns of the Jacobians matrix with respect to parameters,
82       * if null or if some selected parameters are missing from the dictionary, the corresponding
83       * initial column is assumed to be 0
84       */
85      protected void setInitialJacobianColumns(final DoubleArrayDictionary initialJacobianColumns) {
86          this.initialJacobianColumns = initialJacobianColumns == null ?
87                                        new DoubleArrayDictionary() :
88                                        initialJacobianColumns;
89      }
90  
91      /**
92       * Getter for the state dimension.
93       * @return state dimension
94       * @since 13.1
95       */
96      public int getStateDimension() {
97          return initialStm.getColumnDimension();
98      }
99  
100     /** Get the State Transition Matrix state name.
101      * @return State Transition Matrix state name
102      */
103     public String getStmName() {
104         return stmName;
105     }
106 
107     /** Get the initial State Transition Matrix.
108      * @return initial State Transition Matrix
109      */
110     public RealMatrix getInitialStateTransitionMatrix() {
111         return initialStm;
112     }
113 
114     /** Get the initial column of Jacobian matrix with respect to named parameter.
115      * @param columnName name of the column
116      * @return initial column of the Jacobian matrix
117      */
118     public double[] getInitialJacobianColumn(final String columnName) {
119         final DoubleArrayDictionary.Entry entry = initialJacobianColumns.getEntry(columnName);
120         return entry == null ? new double[getStateDimension()] : entry.getValue();
121     }
122 
123     /** Convert a flattened array to a square matrix.
124      * @param array input array
125      * @return the corresponding matrix
126      * @since 13.1
127      */
128     public RealMatrix toSquareMatrix(final double[] array) {
129         final int stateDimension = getStateDimension();
130         final RealMatrix matrix = MatrixUtils.createRealMatrix(stateDimension, stateDimension);
131         int index = 0;
132         for (int i = 0; i < stateDimension; ++i) {
133             for (int j = 0; j < stateDimension; ++j) {
134                 matrix.setEntry(i, j, array[index++]);
135             }
136         }
137         return matrix;
138     }
139 
140     /** Set the STM data into an array.
141      * @param matrix STM matrix
142      * @return an array containing the STM data
143      * @since 13.1
144      */
145     public double[] toArray(final double[][] matrix) {
146         final int stateDimension = matrix.length;
147         final double[] array = new double[stateDimension * stateDimension];
148         int index = 0;
149         for (final double[] row : matrix) {
150             for (int j = 0; j < stateDimension; ++j) {
151                 array[index++] = row[j];
152             }
153         }
154         return array;
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     public void setReferenceState(final SpacecraftState reference) {
160         // nothing to do
161     }
162 
163     /** Freeze the names of the Jacobian columns.
164      * <p>
165      * This method is called when propagation starts, i.e. when configuration is completed
166      * </p>
167      */
168     public abstract void freezeColumnsNames();
169 
170 }