JacobiansMapper.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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. import org.hipparchus.linear.Array2DRowRealMatrix;
  19. import org.hipparchus.linear.DecompositionSolver;
  20. import org.hipparchus.linear.QRDecomposition;
  21. import org.hipparchus.linear.RealMatrix;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.orbits.Orbit;
  24. import org.orekit.orbits.OrbitType;
  25. import org.orekit.orbits.PositionAngle;
  26. import org.orekit.propagation.SpacecraftState;
  27. import org.orekit.utils.ParameterDriversList;

  28. /** Mapper between two-dimensional Jacobian matrices and one-dimensional {@link
  29.  * SpacecraftState#getAdditionalState(String) additional state arrays}.
  30.  * <p>
  31.  * This class does not hold the states by itself. Instances of this class are guaranteed
  32.  * to be immutable.
  33.  * </p>
  34.  * @author Luc Maisonobe
  35.  * @see org.orekit.propagation.numerical.PartialDerivativesEquations
  36.  * @see org.orekit.propagation.numerical.NumericalPropagator
  37.  * @see SpacecraftState#getAdditionalState(String)
  38.  * @see org.orekit.propagation.AbstractPropagator
  39.  */
  40. public class JacobiansMapper {

  41.     /** State dimension, fixed to 6.
  42.      * @since 9.0
  43.      */
  44.     public static final int STATE_DIMENSION = 6;

  45.     /** Name. */
  46.     private String name;

  47.     /** Selected parameters for Jacobian computation. */
  48.     private final ParameterDriversList parameters;

  49.     /** Orbit type. */
  50.     private final OrbitType orbitType;

  51.     /** Position angle type. */
  52.     private final PositionAngle angleType;

  53.     /** Simple constructor.
  54.      * @param name name of the Jacobians
  55.      * @param parameters selected parameters for Jacobian computation
  56.      * @param orbitType orbit type
  57.      * @param angleType position angle type
  58.      */
  59.     JacobiansMapper(final String name, final ParameterDriversList parameters,
  60.                     final OrbitType orbitType, final PositionAngle angleType) {
  61.         this.name       = name;
  62.         this.parameters = parameters;
  63.         this.orbitType  = orbitType;
  64.         this.angleType  = angleType;
  65.     }

  66.     /** Get the name of the partial Jacobians.
  67.      * @return name of the Jacobians
  68.      */
  69.     public String getName() {
  70.         return name;
  71.     }

  72.     /** Compute the length of the one-dimensional additional state array needed.
  73.      * @return length of the one-dimensional additional state array
  74.      */
  75.     public int getAdditionalStateDimension() {
  76.         return STATE_DIMENSION * (STATE_DIMENSION + parameters.getNbParams());
  77.     }

  78.     /** Get the state vector dimension.
  79.      * @return state vector dimension
  80.      * @deprecated as of 9.0, replaced with {@link #STATE_DIMENSION}
  81.      */
  82.     @Deprecated
  83.     public int getStateDimension() {
  84.         return STATE_DIMENSION;
  85.     }

  86.     /** Get the number of parameters.
  87.      * @return number of parameters
  88.      */
  89.     public int getParameters() {
  90.         return parameters.getNbParams();
  91.     }

  92.     /** Get the conversion Jacobian between state parameters and Cartesian parameters.
  93.      * @param state spacecraft state
  94.      * @return conversion Jacobian
  95.      */
  96.     private double[][] getdYdC(final SpacecraftState state) {

  97.         final double[][] dYdC = new double[STATE_DIMENSION][STATE_DIMENSION];

  98.         // make sure the state is in the desired orbit type
  99.         final Orbit orbit = orbitType.convertType(state.getOrbit());

  100.         // compute the Jacobian, taking the position angle type into account
  101.         orbit.getJacobianWrtCartesian(angleType, dYdC);

  102.         return dYdC;

  103.     }

  104.     /** Set the Jacobian with respect to state into a one-dimensional additional state array.
  105.      * <p>
  106.      * This method converts the Jacobians to Cartesian parameters and put the converted data
  107.      * in the one-dimensional {@code p} array.
  108.      * </p>
  109.      * @param state spacecraft state
  110.      * @param dY1dY0 Jacobian of current state at time t₁
  111.      * with respect to state at some previous time t₀
  112.      * @param dY1dP Jacobian of current state at time t₁
  113.      * with respect to parameters (may be null if there are no parameters)
  114.      * @param p placeholder where to put the one-dimensional additional state
  115.      * @see #getStateJacobian(SpacecraftState, double[][])
  116.      */
  117.     void setInitialJacobians(final SpacecraftState state, final double[][] dY1dY0,
  118.                              final double[][] dY1dP, final double[] p) {

  119.         // set up a converter between state parameters and Cartesian parameters
  120.         final RealMatrix dY1dC1 = new Array2DRowRealMatrix(getdYdC(state), false);
  121.         final DecompositionSolver solver = new QRDecomposition(dY1dC1).getSolver();

  122.         // convert the provided state Jacobian to Cartesian parameters
  123.         final RealMatrix dC1dY0 = solver.solve(new Array2DRowRealMatrix(dY1dY0, false));

  124.         // map the converted state Jacobian to one-dimensional array
  125.         int index = 0;
  126.         for (int i = 0; i < STATE_DIMENSION; ++i) {
  127.             for (int j = 0; j < STATE_DIMENSION; ++j) {
  128.                 p[index++] = dC1dY0.getEntry(i, j);
  129.             }
  130.         }

  131.         if (parameters.getNbParams() != 0) {
  132.             // convert the provided state Jacobian to Cartesian parameters
  133.             final RealMatrix dC1dP = solver.solve(new Array2DRowRealMatrix(dY1dP, false));

  134.             // map the converted parameters Jacobian to one-dimensional array
  135.             for (int i = 0; i < STATE_DIMENSION; ++i) {
  136.                 for (int j = 0; j < parameters.getNbParams(); ++j) {
  137.                     p[index++] = dC1dP.getEntry(i, j);
  138.                 }
  139.             }
  140.         }

  141.     }

  142.     /** Get the Jacobian with respect to state from a one-dimensional additional state array.
  143.      * <p>
  144.      * This method extract the data from the {@code state} and put it in the
  145.      * {@code dYdY0} array.
  146.      * </p>
  147.      * @param state spacecraft state
  148.      * @param dYdY0 placeholder where to put the Jacobian with respect to state
  149.      * @exception OrekitException if state does not contain the Jacobian additional state
  150.      * @see #getParametersJacobian(SpacecraftState, double[][])
  151.      */
  152.     public void getStateJacobian(final SpacecraftState state,  final double[][] dYdY0)
  153.         throws OrekitException {

  154.         // get the conversion Jacobian between state parameters and Cartesian parameters
  155.         final double[][] dYdC = getdYdC(state);

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

  158.         // compute dYdY0 = dYdC * dCdY0, without allocating new arrays
  159.         for (int i = 0; i < STATE_DIMENSION; i++) {
  160.             final double[] rowC = dYdC[i];
  161.             final double[] rowD = dYdY0[i];
  162.             for (int j = 0; j < STATE_DIMENSION; ++j) {
  163.                 double sum = 0;
  164.                 int pIndex = j;
  165.                 for (int k = 0; k < STATE_DIMENSION; ++k) {
  166.                     sum += rowC[k] * p[pIndex];
  167.                     pIndex += STATE_DIMENSION;
  168.                 }
  169.                 rowD[j] = sum;
  170.             }
  171.         }

  172.     }

  173.     /** Get theJacobian with respect to parameters from a one-dimensional additional state array.
  174.      * <p>
  175.      * This method extract the data from the {@code state} and put it in the
  176.      * {@code dYdP} array.
  177.      * </p>
  178.      * <p>
  179.      * If no parameters have been set in the constructor, the method returns immediately and
  180.      * does not reference {@code dYdP} which can safely be null in this case.
  181.      * </p>
  182.      * @param state spacecraft state
  183.      * @param dYdP placeholder where to put the Jacobian with respect to parameters
  184.      * @exception OrekitException if state does not contain the Jacobian additional state
  185.      * @see #getStateJacobian(SpacecraftState, double[][])
  186.      */
  187.     public void getParametersJacobian(final SpacecraftState state, final double[][] dYdP)
  188.         throws OrekitException {

  189.         if (parameters.getNbParams() != 0) {

  190.             // get the conversion Jacobian between state parameters and Cartesian parameters
  191.             final double[][] dYdC = getdYdC(state);

  192.             // extract the additional state
  193.             final double[] p = state.getAdditionalState(name);

  194.             // compute dYdP = dYdC * dCdP, without allocating new arrays
  195.             for (int i = 0; i < STATE_DIMENSION; i++) {
  196.                 final double[] rowC = dYdC[i];
  197.                 final double[] rowD = dYdP[i];
  198.                 for (int j = 0; j < parameters.getNbParams(); ++j) {
  199.                     double sum = 0;
  200.                     int pIndex = j + STATE_DIMENSION * STATE_DIMENSION;
  201.                     for (int k = 0; k < STATE_DIMENSION; ++k) {
  202.                         sum += rowC[k] * p[pIndex];
  203.                         pIndex += parameters.getNbParams();
  204.                     }
  205.                     rowD[j] = sum;
  206.                 }
  207.             }

  208.         }

  209.     }

  210. }