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.numerical;
18  
19  import java.util.List;
20  
21  import org.hipparchus.linear.MatrixUtils;
22  import org.hipparchus.linear.RealMatrix;
23  import org.orekit.orbits.Orbit;
24  import org.orekit.orbits.OrbitType;
25  import org.orekit.orbits.PositionAngleType;
26  import org.orekit.propagation.AbstractMatricesHarvester;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.utils.DoubleArrayDictionary;
29  
30  /** Harvester between two-dimensional Jacobian matrices and one-dimensional {@link
31   * SpacecraftState#getAdditionalState(String) additional state arrays}.
32   * @author Luc Maisonobe
33   * @since 11.1
34   */
35  class NumericalPropagationHarvester extends AbstractMatricesHarvester {
36  
37      /** Identity conversion matrix for initial STM. */
38      private static final double[][] IDENTITY6 = {
39          { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
40          { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
41          { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
42          { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
43          { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
44          { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
45      };
46  
47      /** Propagator bound to this harvester. */
48      private final NumericalPropagator propagator;
49  
50      /** Columns names for parameters. */
51      private List<String> columnsNames;
52  
53      /** Simple constructor.
54       * <p>
55       * The arguments for initial matrices <em>must</em> be compatible with the {@link org.orekit.orbits.OrbitType orbit type}
56       * and {@link PositionAngleType position angle} that will be used by propagator
57       * </p>
58       * <p>
59       * If the initial matrix is 7x7, it means that the mass is considered as being a state variable.
60       * </p>
61       * @param propagator propagator bound to this harvester
62       * @param stmName State Transition Matrix state name
63       * @param initialStm initial State Transition Matrix ∂Y/∂Y₀,
64       * if null (which is the most frequent case), assumed to be 6x6 identity
65       * @param initialJacobianColumns initial columns of the Jacobians matrix with respect to parameters,
66       * if null or if some selected parameters are missing from the dictionary, the corresponding
67       * initial column is assumed to be 0
68       */
69      NumericalPropagationHarvester(final NumericalPropagator propagator, final String stmName,
70                                    final RealMatrix initialStm, final DoubleArrayDictionary initialJacobianColumns) {
71          setInitialStm(stmName, initialStm);
72          setInitialJacobianColumns(initialJacobianColumns);
73          this.propagator   = propagator;
74          this.columnsNames = null;
75      }
76  
77      /** Get the conversion Jacobian between state parameters and parameters used for derivatives.
78       * @param state spacecraft state
79       * @return conversion Jacobian ∂Y/∂C
80       */
81      private double[][] getConversionJacobian(final SpacecraftState state) {
82  
83          if (state.isOrbitDefined() && state.getOrbit().getType() != OrbitType.CARTESIAN) {
84              // make sure the state is in the desired orbit type
85              final Orbit orbit = propagator.getOrbitType().convertType(state.getOrbit());
86  
87              // compute the Jacobian, taking the position angle type into account
88              final double[][] dYdC = new double[IDENTITY6.length][IDENTITY6[0].length];
89              orbit.getJacobianWrtCartesian(propagator.getPositionAngleType(), dYdC);
90              return dYdC;
91          } else {
92              return IDENTITY6;
93          }
94  
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public void freezeColumnsNames() {
100         columnsNames = getJacobiansColumnsNames();
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public List<String> getJacobiansColumnsNames() {
106         return columnsNames == null ? propagator.getJacobiansColumnsNames() : columnsNames;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public OrbitType getOrbitType() {
112         return propagator.getOrbitType();
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public PositionAngleType getPositionAngleType() {
118         return propagator.getPositionAngleType();
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public RealMatrix getStateTransitionMatrix(final SpacecraftState state) {
124 
125         if (!state.hasAdditionalData(getStmName())) {
126             return null;
127         }
128 
129         // extract the additional state
130         final double[] p = state.getAdditionalState(getStmName());
131         final RealMatrix  dCdY0 = toSquareMatrix(p);
132 
133         final RealMatrix  dYdY0;
134         if (!state.isOrbitDefined() || state.getOrbit().getType() == OrbitType.CARTESIAN) {
135             dYdY0 = dCdY0;
136         } else {
137             // get the conversion Jacobian
138             final RealMatrix dYdC = MatrixUtils.createRealIdentityMatrix(getStateDimension());
139             dYdC.setSubMatrix(getConversionJacobian(state), 0, 0);
140 
141             // compute dYdC * dCdY0
142             dYdY0 = dYdC.multiply(dCdY0);
143         }
144 
145         return dYdY0;
146 
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     public RealMatrix getParametersJacobian(final SpacecraftState state) {
152 
153         final List<String> names = getJacobiansColumnsNames();
154 
155         if (names == null || names.isEmpty()) {
156             return null;
157         }
158 
159         // get the conversion Jacobian
160         final RealMatrix dYdC = MatrixUtils.createRealIdentityMatrix(getStateDimension());
161         dYdC.setSubMatrix(getConversionJacobian(state), 0, 0);
162 
163         // compute dYdP = dYdC * dCdP
164         final RealMatrix dYdP = MatrixUtils.createRealMatrix(getStateDimension(), names.size());
165         for (int j = 0; j < names.size(); j++) {
166             final double[] p = state.getAdditionalState(names.get(j));
167             for (int i = 0; i < getStateDimension(); ++i) {
168                 final double[] dYdCi = dYdC.getRow(i);
169                 double sum = 0;
170                 for (int k = 0; k < getStateDimension(); ++k) {
171                     sum += dYdCi[k] * p[k];
172                 }
173                 dYdP.setEntry(i, j, sum);
174             }
175         }
176 
177         return dYdP;
178 
179     }
180 
181 }