STMEquations.java

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

  18. import java.util.Arrays;

  19. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  20. import org.hipparchus.linear.Array2DRowRealMatrix;
  21. import org.hipparchus.linear.RealMatrix;
  22. import org.orekit.bodies.CR3BPSystem;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  25. import org.orekit.propagation.integration.CombinedDerivatives;
  26. import org.orekit.time.AbsoluteDate;

  27. /** Class calculating the state transition matrix coefficient for CR3BP Computation.
  28.  * @see "Dynamical systems, the three-body problem, and space mission design, Koon, Lo, Marsden, Ross"
  29.  * @author Vincent Mouraux
  30.  * @since 10.2
  31.  */
  32. @SuppressWarnings("deprecation")
  33. public class STMEquations
  34.     implements AdditionalDerivativesProvider,
  35.                org.orekit.propagation.integration.AdditionalEquations {

  36.     /** Matrix Dimension. */
  37.     private static final int DIM = 6;

  38.     /** Mass ratio of the considered CR3BP System. */
  39.     private final CR3BPSystem syst;

  40.     /** Name of the equations. */
  41.     private final String name;

  42.     /** Potential Hessian Matrix. */
  43.     private final double[][] jacobian = new double[DIM][DIM];

  44.     /** Simple constructor.
  45.      * @param syst CR3BP System considered
  46.      */
  47.     public STMEquations(final CR3BPSystem syst) {
  48.         this.syst = syst;
  49.         this.name = "stmEquations";

  50.         // Jacobian constant values initialization
  51.         for (int j = 0; j < jacobian.length; ++j) {
  52.             Arrays.fill(jacobian[j], 0.0);
  53.         }

  54.         jacobian[0][3] = 1.0;
  55.         jacobian[1][4] = 1.0;
  56.         jacobian[2][5] = 1.0;
  57.         jacobian[3][4] = 2.0;
  58.         jacobian[4][3] = -2.0;
  59.     }

  60.     /** Method adding the standard initial values of the additional state to the initial spacecraft state.
  61.      * @param s Initial state of the system
  62.      * @return s Initial augmented (with the additional equations) state
  63.      */
  64.     public SpacecraftState setInitialPhi(final SpacecraftState s) {
  65.         final int stateDimension = 36;
  66.         final double[] phi = new double[stateDimension];
  67.         for (int i = 0; i < stateDimension; i = i + 7) {
  68.             phi[i] = 1.0;
  69.         }
  70.         return s.addAdditionalState(name, phi);
  71.     }

  72.     /** {@inheritDoc} */
  73.     public void init(final SpacecraftState initialState, final AbsoluteDate target) {
  74.         // FIXME: remove in 12.0 when AdditionalEquations is removed
  75.         AdditionalDerivativesProvider.super.init(initialState, target);
  76.     }

  77.     /** {@inheritDoc} */
  78.     public double[] computeDerivatives(final SpacecraftState s, final double[] pDot) {
  79.         // FIXME: remove in 12.0 when AdditionalEquations is removed
  80.         System.arraycopy(derivatives(s), 0, pDot, 0, pDot.length);
  81.         return null;
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     @Deprecated
  86.     public double[] derivatives(final SpacecraftState state) {
  87.         return combinedDerivatives(state).getAdditionalDerivatives();
  88.     }

  89.     /** {@inheritDoc} */
  90.     public CombinedDerivatives combinedDerivatives(final SpacecraftState s) {

  91.         // State Transition Matrix
  92.         final double[] phi = s.getAdditionalState(getName());
  93.         final double[] dPhi = new double[phi.length];

  94.         // Spacecraft Potential
  95.         final DerivativeStructure potential = new CR3BPForceModel(syst).getPotential(s);

  96.         // Potential derivatives
  97.         final double[] dU = potential.getAllDerivatives();

  98.         // second order derivatives index
  99.         final int idXX = potential.getFactory().getCompiler().getPartialDerivativeIndex(2, 0, 0);
  100.         final int idXY = potential.getFactory().getCompiler().getPartialDerivativeIndex(1, 1, 0);
  101.         final int idXZ = potential.getFactory().getCompiler().getPartialDerivativeIndex(1, 0, 1);
  102.         final int idYY = potential.getFactory().getCompiler().getPartialDerivativeIndex(0, 2, 0);
  103.         final int idYZ = potential.getFactory().getCompiler().getPartialDerivativeIndex(0, 1, 1);
  104.         final int idZZ = potential.getFactory().getCompiler().getPartialDerivativeIndex(0, 0, 2);

  105.         // New Jacobian values
  106.         jacobian[3][0] = dU[idXX];
  107.         jacobian[4][1] = dU[idYY];
  108.         jacobian[5][2] = dU[idZZ];
  109.         jacobian[3][1] = dU[idXY];
  110.         jacobian[4][0] = jacobian[3][1];
  111.         jacobian[3][2] = dU[idXZ];
  112.         jacobian[5][0] = jacobian[3][2];
  113.         jacobian[4][2] = dU[idYZ];
  114.         jacobian[5][1] = jacobian[4][2];

  115.         // STM derivatives computation : dPhi = Jacobian * Phi if both dPhi and Phi are defined as Matrix
  116.         for (int k = 0; k < DIM; k++) {
  117.             for (int l = 0; l < DIM; l++) {
  118.                 for (int i = 0; i < DIM; i++) {
  119.                     dPhi[DIM * k + l] =
  120.                         dPhi[DIM * k + l] + jacobian[k][i] * phi[DIM * i + l];
  121.                 }
  122.             }
  123.         }

  124.         return new CombinedDerivatives(dPhi, null);

  125.     }

  126.     /** {@inheritDoc} */
  127.     public String getName() {
  128.         return name;
  129.     }

  130.     /** {@inheritDoc} */
  131.     @Override
  132.     public int getDimension() {
  133.         return DIM * DIM;
  134.     }

  135.     /** Method returning the State Transition Matrix.
  136.      * @param s SpacecraftState of the system
  137.      * @return phiM State Transition Matrix
  138.      */
  139.     public RealMatrix getStateTransitionMatrix(final SpacecraftState s) {
  140.         final double[][] phi2dA = new double[DIM][DIM];
  141.         final double[] stm = s.getAdditionalState(getName());
  142.         for (int i = 0; i < DIM; i++) {
  143.             for (int j = 0; j < 6; j++) {
  144.                 phi2dA[i][j] = stm[DIM * i + j];
  145.             }
  146.         }
  147.         return new Array2DRowRealMatrix(phi2dA, false);
  148.     }
  149. }