1   /* Copyright 2010-2011 Centre National d'Études Spatiales
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.IdentityHashMap;
20  import java.util.Map;
21  
22  import org.hipparchus.analysis.differentiation.Gradient;
23  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  import org.orekit.forces.ForceModel;
27  import org.orekit.propagation.FieldSpacecraftState;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.propagation.integration.AdditionalEquations;
30  import org.orekit.utils.ParameterDriver;
31  import org.orekit.utils.ParameterDriversList;
32  
33  /** Set of {@link AdditionalEquations additional equations} computing the partial derivatives
34   * of the state (orbit) with respect to initial state and force models parameters.
35   * <p>
36   * This set of equations are automatically added to a {@link NumericalPropagator numerical propagator}
37   * in order to compute partial derivatives of the orbit along with the orbit itself. This is
38   * useful for example in orbit determination applications.
39   * </p>
40   * <p>
41   * The partial derivatives with respect to initial state can be either dimension 6
42   * (orbit only) or 7 (orbit and mass).
43   * </p>
44   * <p>
45   * The partial derivatives with respect to force models parameters has a dimension
46   * equal to the number of selected parameters. Parameters selection is implemented at
47   * {@link ForceModel force models} level. Users must retrieve a {@link ParameterDriver
48   * parameter driver} using {@link ForceModel#getParameterDriver(String)} and then
49   * select it by calling {@link ParameterDriver#setSelected(boolean) setSelected(true)}.
50   * </p>
51   * <p>
52   * If several force models provide different {@link ParameterDriver drivers} for the
53   * same parameter name, selecting any of these drivers has the side effect of
54   * selecting all the drivers for this shared parameter. In this case, the partial
55   * derivatives will be the sum of the partial derivatives contributed by the
56   * corresponding force models. This case typically arises for central attraction
57   * coefficient, which has an influence on {@link org.orekit.forces.gravity.NewtonianAttraction
58   * Newtonian attraction}, {@link org.orekit.forces.gravity.HolmesFeatherstoneAttractionModel
59   * gravity field}, and {@link org.orekit.forces.gravity.Relativity relativity}.
60   * </p>
61   * @author V&eacute;ronique Pommier-Maurussane
62   * @author Luc Maisonobe
63   */
64  public class PartialDerivativesEquations implements AdditionalEquations {
65  
66      /** Propagator computing state evolution. */
67      private final NumericalPropagator propagator;
68  
69      /** Selected parameters for Jacobian computation. */
70      private ParameterDriversList selected;
71  
72      /** Parameters map. */
73      private Map<ParameterDriver, Integer> map;
74  
75      /** Name. */
76      private final String name;
77  
78      /** Flag for Jacobian matrices initialization. */
79      private boolean initialized;
80  
81      /** Simple constructor.
82       * <p>
83       * Upon construction, this set of equations is <em>automatically</em> added to
84       * the propagator by calling its {@link
85       * NumericalPropagator#addAdditionalEquations(AdditionalEquations)} method. So
86       * there is no need to call this method explicitly for these equations.
87       * </p>
88       * @param name name of the partial derivatives equations
89       * @param propagator the propagator that will handle the orbit propagation
90       */
91      public PartialDerivativesEquations(final String name, final NumericalPropagator propagator) {
92          this.name                   = name;
93          this.selected               = null;
94          this.map                    = null;
95          this.propagator             = propagator;
96          this.initialized            = false;
97          propagator.addAdditionalEquations(this);
98      }
99  
100     /** {@inheritDoc} */
101     public String getName() {
102         return name;
103     }
104 
105     /** Freeze the selected parameters from the force models.
106      */
107     private void freezeParametersSelection() {
108         if (selected == null) {
109 
110             // first pass: gather all parameters, binding similar names together
111             selected = new ParameterDriversList();
112             for (final ForceModel provider : propagator.getAllForceModels()) {
113                 for (final ParameterDriver driver : provider.getParametersDrivers()) {
114                     selected.add(driver);
115                 }
116             }
117 
118             // second pass: now that shared parameter names are bound together,
119             // their selections status have been synchronized, we can filter them
120             selected.filter(true);
121 
122             // third pass: sort parameters lexicographically
123             selected.sort();
124 
125             // fourth pass: set up a map between parameters drivers and matrices columns
126             map = new IdentityHashMap<ParameterDriver, Integer>();
127             int parameterIndex = 0;
128             for (final ParameterDriver selectedDriver : selected.getDrivers()) {
129                 for (final ForceModel provider : propagator.getAllForceModels()) {
130                     for (final ParameterDriver driver : provider.getParametersDrivers()) {
131                         if (driver.getName().equals(selectedDriver.getName())) {
132                             map.put(driver, parameterIndex);
133                         }
134                     }
135                 }
136                 ++parameterIndex;
137             }
138 
139         }
140     }
141 
142     /** Get the selected parameters, in Jacobian matrix column order.
143      * <p>
144      * The force models parameters for which partial derivatives are desired,
145      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
146      * before this method is called, so the proper list is returned.
147      * </p>
148      * @return selected parameters, in Jacobian matrix column order which
149      * is lexicographic order
150      */
151     public ParameterDriversList getSelectedParameters() {
152         freezeParametersSelection();
153         return selected;
154     }
155 
156     /** Set the initial value of the Jacobian with respect to state and parameter.
157      * <p>
158      * This method is equivalent to call {@link #setInitialJacobians(SpacecraftState,
159      * double[][], double[][])} with dYdY0 set to the identity matrix and dYdP set
160      * to a zero matrix.
161      * </p>
162      * <p>
163      * The force models parameters for which partial derivatives are desired,
164      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
165      * before this method is called, so proper matrices dimensions are used.
166      * </p>
167      * @param s0 initial state
168      * @return state with initial Jacobians added
169      * @see #getSelectedParameters()
170      * @since 9.0
171      */
172     public SpacecraftState setInitialJacobians(final SpacecraftState s0) {
173         freezeParametersSelection();
174         final int stateDimension = 6;
175         final double[][] dYdY0 = new double[stateDimension][stateDimension];
176         final double[][] dYdP  = new double[stateDimension][selected.getNbParams()];
177         for (int i = 0; i < stateDimension; ++i) {
178             dYdY0[i][i] = 1.0;
179         }
180         return setInitialJacobians(s0, dYdY0, dYdP);
181     }
182 
183     /** Set the initial value of the Jacobian with respect to state and parameter.
184      * <p>
185      * The returned state must be added to the propagator (it is not done
186      * automatically, as the user may need to add more states to it).
187      * </p>
188      * <p>
189      * The force models parameters for which partial derivatives are desired,
190      * <em>must</em> have been {@link ParameterDriver#setSelected(boolean) selected}
191      * before this method is called, and the {@code dY1dP} matrix dimension <em>must</em>
192      * be consistent with the selection.
193      * </p>
194      * @param s1 current state
195      * @param dY1dY0 Jacobian of current state at time t₁ with respect
196      * to state at some previous time t₀ (must be 6x6)
197      * @param dY1dP Jacobian of current state at time t₁ with respect
198      * to parameters (may be null if no parameters are selected)
199      * @return state with initial Jacobians added
200      * @see #getSelectedParameters()
201      */
202     public SpacecraftState setInitialJacobians(final SpacecraftState s1,
203                                                final double[][] dY1dY0, final double[][] dY1dP) {
204 
205         freezeParametersSelection();
206 
207         // Check dimensions
208         final int stateDim = dY1dY0.length;
209         if (stateDim != 6 || stateDim != dY1dY0[0].length) {
210             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_6X6,
211                                       stateDim, dY1dY0[0].length);
212         }
213         if (dY1dP != null && stateDim != dY1dP.length) {
214             throw new OrekitException(OrekitMessages.STATE_AND_PARAMETERS_JACOBIANS_ROWS_MISMATCH,
215                                       stateDim, dY1dP.length);
216         }
217         if (dY1dP == null && selected.getNbParams() != 0 ||
218             dY1dP != null && selected.getNbParams() != dY1dP[0].length) {
219             throw new OrekitException(new OrekitException(OrekitMessages.INITIAL_MATRIX_AND_PARAMETERS_NUMBER_MISMATCH,
220                                                           dY1dP == null ? 0 : dY1dP[0].length, selected.getNbParams()));
221         }
222 
223         // store the matrices as a single dimension array
224         initialized = true;
225         final JacobiansMapper mapper = getMapper();
226         final double[] p = new double[mapper.getAdditionalStateDimension()];
227         mapper.setInitialJacobians(s1, dY1dY0, dY1dP, p);
228 
229         // set value in propagator
230         return s1.addAdditionalState(name, p);
231 
232     }
233 
234     /** Get a mapper between two-dimensional Jacobians and one-dimensional additional state.
235      * @return a mapper between two-dimensional Jacobians and one-dimensional additional state,
236      * with the same name as the instance
237      * @see #setInitialJacobians(SpacecraftState)
238      * @see #setInitialJacobians(SpacecraftState, double[][], double[][])
239      */
240     public JacobiansMapper getMapper() {
241         if (!initialized) {
242             throw new OrekitException(OrekitMessages.STATE_JACOBIAN_NOT_INITIALIZED);
243         }
244         return new JacobiansMapper(name, selected,
245                                    propagator.getOrbitType(),
246                                    propagator.getPositionAngleType());
247     }
248 
249     /** {@inheritDoc} */
250     public double[] computeDerivatives(final SpacecraftState s, final double[] pDot) {
251 
252         // initialize acceleration Jacobians to zero
253         final int paramDim = selected.getNbParams();
254         final int dim = 3;
255         final double[][] dAccdParam = new double[dim][paramDim];
256         final double[][] dAccdPos   = new double[dim][dim];
257         final double[][] dAccdVel   = new double[dim][dim];
258 
259         final NumericalGradientConverter fullConverter    = new NumericalGradientConverter(s, 6, propagator.getAttitudeProvider());
260         final NumericalGradientConverter posOnlyConverter = new NumericalGradientConverter(s, 3, propagator.getAttitudeProvider());
261 
262         // compute acceleration Jacobians, finishing with the largest force: Newtonian attraction
263         for (final ForceModel forceModel : propagator.getAllForceModels()) {
264 
265             final NumericalGradientConverter converter = forceModel.dependsOnPositionOnly() ? posOnlyConverter : fullConverter;
266             final FieldSpacecraftState<Gradient> dsState = converter.getState(forceModel);
267             final Gradient[] parameters = converter.getParameters(dsState, forceModel);
268 
269             final FieldVector3D<Gradient> acceleration = forceModel.acceleration(dsState, parameters);
270             final double[] derivativesX = acceleration.getX().getGradient();
271             final double[] derivativesY = acceleration.getY().getGradient();
272             final double[] derivativesZ = acceleration.getZ().getGradient();
273 
274             // update Jacobians with respect to state
275             addToRow(derivativesX, 0, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
276             addToRow(derivativesY, 1, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
277             addToRow(derivativesZ, 2, converter.getFreeStateParameters(), dAccdPos, dAccdVel);
278 
279             int index = converter.getFreeStateParameters();
280             for (ParameterDriver driver : forceModel.getParametersDrivers()) {
281                 if (driver.isSelected()) {
282                     final int parameterIndex = map.get(driver);
283                     dAccdParam[0][parameterIndex] += derivativesX[index];
284                     dAccdParam[1][parameterIndex] += derivativesY[index];
285                     dAccdParam[2][parameterIndex] += derivativesZ[index];
286                     ++index;
287                 }
288             }
289 
290         }
291 
292         // the variational equations of the complete state Jacobian matrix have the following form:
293 
294         // [        |        ]   [                 |                  ]   [     |     ]
295         // [  Adot  |  Bdot  ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [  A  |  B  ]
296         // [        |        ]   [                 |                  ]   [     |     ]
297         // ---------+---------   ------------------+------------------- * ------+------
298         // [        |        ]   [                 |                  ]   [     |     ]
299         // [  Cdot  |  Ddot  ] = [    dAcc/dPos    |     dAcc/dVel    ]   [  C  |  D  ]
300         // [        |        ]   [                 |                  ]   [     |     ]
301 
302         // The A, B, C and D sub-matrices and their derivatives (Adot ...) are 3x3 matrices
303 
304         // The expanded multiplication above can be rewritten to take into account
305         // the fixed values found in the sub-matrices in the left factor. This leads to:
306 
307         //     [ Adot ] = [ C ]
308         //     [ Bdot ] = [ D ]
309         //     [ Cdot ] = [ dAcc/dPos ] * [ A ] + [ dAcc/dVel ] * [ C ]
310         //     [ Ddot ] = [ dAcc/dPos ] * [ B ] + [ dAcc/dVel ] * [ D ]
311 
312         // The following loops compute these expressions taking care of the mapping of the
313         // (A, B, C, D) matrices into the single dimension array p and of the mapping of the
314         // (Adot, Bdot, Cdot, Ddot) matrices into the single dimension array pDot.
315 
316         // copy C and E into Adot and Bdot
317         final int stateDim = 6;
318         final double[] p = s.getAdditionalState(getName());
319         System.arraycopy(p, dim * stateDim, pDot, 0, dim * stateDim);
320 
321         // compute Cdot and Ddot
322         for (int i = 0; i < dim; ++i) {
323             final double[] dAdPi = dAccdPos[i];
324             final double[] dAdVi = dAccdVel[i];
325             for (int j = 0; j < stateDim; ++j) {
326                 pDot[(dim + i) * stateDim + j] =
327                     dAdPi[0] * p[j]                + dAdPi[1] * p[j +     stateDim] + dAdPi[2] * p[j + 2 * stateDim] +
328                     dAdVi[0] * p[j + 3 * stateDim] + dAdVi[1] * p[j + 4 * stateDim] + dAdVi[2] * p[j + 5 * stateDim];
329             }
330         }
331 
332         for (int k = 0; k < paramDim; ++k) {
333             // the variational equations of the parameters Jacobian matrix are computed
334             // one column at a time, they have the following form:
335             // [      ]   [                 |                  ]   [   ]   [                  ]
336             // [ Edot ]   [  dVel/dPos = 0  |  dVel/dVel = Id  ]   [ E ]   [  dVel/dParam = 0 ]
337             // [      ]   [                 |                  ]   [   ]   [                  ]
338             // --------   ------------------+------------------- * ----- + --------------------
339             // [      ]   [                 |                  ]   [   ]   [                  ]
340             // [ Fdot ] = [    dAcc/dPos    |     dAcc/dVel    ]   [ F ]   [    dAcc/dParam   ]
341             // [      ]   [                 |                  ]   [   ]   [                  ]
342 
343             // The E and F sub-columns and their derivatives (Edot, Fdot) are 3 elements columns.
344 
345             // The expanded multiplication and addition above can be rewritten to take into
346             // account the fixed values found in the sub-matrices in the left factor. This leads to:
347 
348             //     [ Edot ] = [ F ]
349             //     [ Fdot ] = [ dAcc/dPos ] * [ E ] + [ dAcc/dVel ] * [ F ] + [ dAcc/dParam ]
350 
351             // The following loops compute these expressions taking care of the mapping of the
352             // (E, F) columns into the single dimension array p and of the mapping of the
353             // (Edot, Fdot) columns into the single dimension array pDot.
354 
355             // copy F into Edot
356             final int columnTop = stateDim * stateDim + k;
357             pDot[columnTop]                = p[columnTop + 3 * paramDim];
358             pDot[columnTop +     paramDim] = p[columnTop + 4 * paramDim];
359             pDot[columnTop + 2 * paramDim] = p[columnTop + 5 * paramDim];
360 
361             // compute Fdot
362             for (int i = 0; i < dim; ++i) {
363                 final double[] dAdPi = dAccdPos[i];
364                 final double[] dAdVi = dAccdVel[i];
365                 pDot[columnTop + (dim + i) * paramDim] =
366                     dAccdParam[i][k] +
367                     dAdPi[0] * p[columnTop]                + dAdPi[1] * p[columnTop +     paramDim] + dAdPi[2] * p[columnTop + 2 * paramDim] +
368                     dAdVi[0] * p[columnTop + 3 * paramDim] + dAdVi[1] * p[columnTop + 4 * paramDim] + dAdVi[2] * p[columnTop + 5 * paramDim];
369             }
370 
371         }
372 
373         // these equations have no effect on the main state itself
374         return null;
375 
376     }
377 
378     /** Get the flag for the initialization of the state jacobian.
379      * @return true if the state jacobian is initialized
380      * @since 10.2
381      */
382     public boolean isInitialize() {
383         return initialized;
384     }
385 
386     /** Fill Jacobians rows.
387      * @param derivatives derivatives of a component of acceleration (along either x, y or z)
388      * @param index component index (0 for x, 1 for y, 2 for z)
389      * @param freeStateParameters number of free parameters, either 3 (position),
390      * 6 (position-velocity) or 7 (position-velocity-mass)
391      * @param dAccdPos Jacobian of acceleration with respect to spacecraft position
392      * @param dAccdVel Jacobian of acceleration with respect to spacecraft velocity
393      */
394     private void addToRow(final double[] derivatives, final int index, final int freeStateParameters,
395                           final double[][] dAccdPos, final double[][] dAccdVel) {
396 
397         for (int i = 0; i < 3; ++i) {
398             dAccdPos[index][i] += derivatives[i];
399         }
400         if (freeStateParameters > 3) {
401             for (int i = 0; i < 3; ++i) {
402                 dAccdVel[index][i] += derivatives[i + 3];
403             }
404         }
405 
406     }
407 
408 }
409