SmallManeuverAnalyticalModel.java

  1. /* Copyright 2002-2024 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.forces.maneuvers;

  18. import java.util.Arrays;

  19. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.frames.Frame;
  22. import org.orekit.orbits.Orbit;
  23. import org.orekit.orbits.OrbitType;
  24. import org.orekit.orbits.PositionAngleType;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.analytical.AdapterPropagator;
  27. import org.orekit.time.AbsoluteDate;
  28. import org.orekit.utils.Constants;

  29. /** Analytical model for small maneuvers.
  30.  * <p>The aim of this model is to compute quickly the effect at date t₁
  31.  * of a small maneuver performed at an earlier date t₀. Both the
  32.  * direct effect of the maneuver and the Jacobian of this effect with respect to
  33.  * maneuver parameters are available.
  34.  * </p>
  35.  * <p>These effect are computed analytically using two Jacobian matrices:
  36.  * <ol>
  37.  *   <li>J₀: Jacobian of Keplerian or equinoctial elements with respect
  38.  *   to Cartesian parameters at date t₀ allows to compute
  39.  *   maneuver effect as a change in orbital elements at maneuver date t₀,</li>
  40.  *   <li>J<sub>1/0</sub>: Jacobian of Keplerian or equinoctial elements
  41.  *   at date t₁ with respect to Keplerian or equinoctial elements
  42.  *   at date t₀ allows to propagate the change in orbital elements
  43.  *   to final date t₁.</li>
  44.  * </ol>
  45.  *
  46.  * <p>
  47.  * The second Jacobian, J<sub>1/0</sub>, is computed using a simple Keplerian
  48.  * model, i.e. it is the identity except for the mean motion row which also includes
  49.  * an off-diagonal element due to semi-major axis change.
  50.  * </p>
  51.  * <p>
  52.  * The orbital elements change at date t₁ can be added to orbital elements
  53.  * extracted from state, and the final elements taking account the changes are then
  54.  * converted back to appropriate type, which may be different from Keplerian or
  55.  * equinoctial elements.
  56.  * </p>
  57.  * <p>
  58.  * Note that this model takes <em>only</em> Keplerian effects into account. This means
  59.  * that using only this class to compute an inclination maneuver in Low Earth Orbit will
  60.  * <em>not</em> change ascending node drift rate despite inclination has changed (the
  61.  * same would be true for a semi-major axis change of course). In order to take this
  62.  * drift into account, an instance of {@link
  63.  * org.orekit.propagation.analytical.J2DifferentialEffect J2DifferentialEffect}
  64.  * must be used together with an instance of this class.
  65.  * </p>
  66.  * @author Luc Maisonobe
  67.  */
  68. public class SmallManeuverAnalyticalModel
  69.     implements AdapterPropagator.DifferentialEffect {

  70.     /** State at maneuver date (before maneuver occurrence). */
  71.     private final SpacecraftState state0;

  72.     /** Inertial velocity increment. */
  73.     private final Vector3D inertialDV;

  74.     /** Mass change ratio. */
  75.     private final double massRatio;

  76.     /** Type of orbit used for internal Jacobians. */
  77.     private final OrbitType type;

  78.     /** Initial Keplerian (or equinoctial) Jacobian with respect to maneuver. */
  79.     private final double[][] j0;

  80.     /** Time derivative of the initial Keplerian (or equinoctial) Jacobian with respect to maneuver. */
  81.     private double[][] j0Dot;

  82.     /** Mean anomaly change factor. */
  83.     private final double ksi;

  84.     /** Build a maneuver defined in spacecraft frame.
  85.      * @param state0 state at maneuver date, <em>before</em> the maneuver
  86.      * is performed
  87.      * @param dV velocity increment in spacecraft frame
  88.      * @param isp engine specific impulse (s)
  89.      */
  90.     public SmallManeuverAnalyticalModel(final SpacecraftState state0,
  91.                                         final Vector3D dV, final double isp) {
  92.         this(state0, state0.getFrame(),
  93.              state0.getAttitude().getRotation().applyInverseTo(dV),
  94.              isp);
  95.     }

  96.     /** Build a maneuver defined in user-specified frame.
  97.      * @param state0 state at maneuver date, <em>before</em> the maneuver
  98.      * is performed
  99.      * @param frame frame in which velocity increment is defined
  100.      * @param dV velocity increment in specified frame
  101.      * @param isp engine specific impulse (s)
  102.      */
  103.     public SmallManeuverAnalyticalModel(final SpacecraftState state0, final Frame frame,
  104.                                         final Vector3D dV, final double isp) {

  105.         this.state0    = state0;
  106.         this.massRatio = FastMath.exp(-dV.getNorm() / (Constants.G0_STANDARD_GRAVITY * isp));

  107.         // use equinoctial orbit type if possible, Keplerian if nearly hyperbolic orbits
  108.         type = (state0.getE() < 0.9) ? OrbitType.EQUINOCTIAL : OrbitType.KEPLERIAN;

  109.         // compute initial Jacobian
  110.         final double[][] fullJacobian = new double[6][6];
  111.         j0 = new double[6][3];
  112.         final Orbit orbit0 = type.convertType(state0.getOrbit());
  113.         orbit0.getJacobianWrtCartesian(PositionAngleType.MEAN, fullJacobian);
  114.         for (int i = 0; i < j0.length; ++i) {
  115.             System.arraycopy(fullJacobian[i], 3, j0[i], 0, 3);
  116.         }

  117.         // use lazy evaluation for j0Dot, as it is used only when Jacobians are evaluated
  118.         j0Dot = null;

  119.         // compute maneuver effect on Keplerian (or equinoctial) elements
  120.         inertialDV = frame.getStaticTransformTo(state0.getFrame(), state0.getDate())
  121.                 .transformVector(dV);

  122.         // compute mean anomaly change: dM(t1) = dM(t0) + ksi * da * (t1 - t0)
  123.         final double mu = state0.getMu();
  124.         final double a  = state0.getA();
  125.         ksi = -1.5 * FastMath.sqrt(mu / a) / (a * a);

  126.     }

  127.     /** Get the date of the maneuver.
  128.      * @return date of the maneuver
  129.      */
  130.     public AbsoluteDate getDate() {
  131.         return state0.getDate();
  132.     }

  133.     /** Get the inertial velocity increment of the maneuver.
  134.      * @return velocity increment in a state-dependent inertial frame
  135.      * @see #getInertialFrame()
  136.      */
  137.     public Vector3D getInertialDV() {
  138.         return inertialDV;
  139.     }

  140.     /** Get the inertial frame in which the velocity increment is defined.
  141.      * @return inertial frame in which the velocity increment is defined
  142.      * @see #getInertialDV()
  143.      */
  144.     public Frame getInertialFrame() {
  145.         return state0.getFrame();
  146.     }

  147.     /** Compute the effect of the maneuver on an orbit.
  148.      * @param orbit1 original orbit at t₁, without maneuver
  149.      * @return orbit at t₁, taking the maneuver
  150.      * into account if t₁ &gt; t₀
  151.      * @see #apply(SpacecraftState)
  152.      * @see #getJacobian(Orbit, PositionAngleType, double[][])
  153.      */
  154.     public Orbit apply(final Orbit orbit1) {

  155.         if (orbit1.getDate().compareTo(state0.getDate()) <= 0) {
  156.             // the maneuver has not occurred yet, don't change anything
  157.             return orbit1;
  158.         }

  159.         return orbit1.getType().convertType(updateOrbit(orbit1));

  160.     }

  161.     /** Compute the effect of the maneuver on a spacecraft state.
  162.      * @param state1 original spacecraft state at t₁,
  163.      * without maneuver
  164.      * @return spacecraft state at t₁, taking the maneuver
  165.      * into account if t₁ &gt; t₀
  166.      * @see #apply(Orbit)
  167.      * @see #getJacobian(Orbit, PositionAngleType, double[][])
  168.      */
  169.     public SpacecraftState apply(final SpacecraftState state1) {

  170.         if (state1.getDate().compareTo(state0.getDate()) <= 0) {
  171.             // the maneuver has not occurred yet, don't change anything
  172.             return state1;
  173.         }

  174.         return new SpacecraftState(state1.getOrbit().getType().convertType(updateOrbit(state1.getOrbit())),
  175.                                    state1.getAttitude(), updateMass(state1.getMass()));

  176.     }

  177.     /** Compute the effect of the maneuver on an orbit.
  178.      * @param orbit1 original orbit at t₁, without maneuver
  179.      * @return orbit at t₁, always taking the maneuver into account, always in the internal type
  180.      */
  181.     private Orbit updateOrbit(final Orbit orbit1) {

  182.         // compute maneuver effect
  183.         final double dt = orbit1.getDate().durationFrom(state0.getDate());
  184.         final double x  = inertialDV.getX();
  185.         final double y  = inertialDV.getY();
  186.         final double z  = inertialDV.getZ();
  187.         final double[] delta = new double[6];
  188.         for (int i = 0; i < delta.length; ++i) {
  189.             delta[i] = j0[i][0] * x + j0[i][1] * y + j0[i][2] * z;
  190.         }
  191.         delta[5] += ksi * delta[0] * dt;

  192.         // convert current orbital state to Keplerian or equinoctial elements
  193.         final double[] parameters    = new double[6];
  194.         type.mapOrbitToArray(type.convertType(orbit1), PositionAngleType.MEAN, parameters, null);
  195.         for (int i = 0; i < delta.length; ++i) {
  196.             parameters[i] += delta[i];
  197.         }

  198.         // build updated orbit as Keplerian or equinoctial elements
  199.         return type.mapArrayToOrbit(parameters, null, PositionAngleType.MEAN,
  200.                                     orbit1.getDate(), orbit1.getMu(), orbit1.getFrame());

  201.     }

  202.     /** Compute the Jacobian of the orbit with respect to maneuver parameters.
  203.      * <p>
  204.      * The Jacobian matrix is a 6x4 matrix. Element jacobian[i][j] corresponds to
  205.      * the partial derivative of orbital parameter i with respect to maneuver
  206.      * parameter j. The rows order is the same order as used in {@link
  207.      * Orbit#getJacobianWrtCartesian(PositionAngleType, double[][]) Orbit.getJacobianWrtCartesian}
  208.      * method. Columns (0, 1, 2) correspond to the velocity increment coordinates
  209.      * (ΔV<sub>x</sub>, ΔV<sub>y</sub>, ΔV<sub>z</sub>) in the
  210.      * inertial frame returned by {@link #getInertialFrame()}, and column 3
  211.      * corresponds to the maneuver date t₀.
  212.      * </p>
  213.      * @param orbit1 original orbit at t₁, without maneuver
  214.      * @param positionAngleType type of the position angle to use
  215.      * @param jacobian placeholder 6x4 (or larger) matrix to be filled with the Jacobian, if matrix
  216.      * is larger than 6x4, only the 6x4 upper left corner will be modified
  217.      * @see #apply(Orbit)
  218.      */
  219.     public void getJacobian(final Orbit orbit1, final PositionAngleType positionAngleType,
  220.                             final double[][] jacobian) {

  221.         final double dt = orbit1.getDate().durationFrom(state0.getDate());
  222.         if (dt < 0) {
  223.             // the maneuver has not occurred yet, Jacobian is null
  224.             for (int i = 0; i < 6; ++i) {
  225.                 Arrays.fill(jacobian[i], 0, 4, 0.0);
  226.             }
  227.             return;
  228.         }

  229.         // derivatives of Keplerian/equinoctial elements with respect to velocity increment
  230.         final double x  = inertialDV.getX();
  231.         final double y  = inertialDV.getY();
  232.         final double z  = inertialDV.getZ();
  233.         for (int i = 0; i < 6; ++i) {
  234.             System.arraycopy(j0[i], 0, jacobian[i], 0, 3);
  235.         }
  236.         for (int j = 0; j < 3; ++j) {
  237.             jacobian[5][j] += ksi * dt * j0[0][j];
  238.         }

  239.         // derivatives of Keplerian/equinoctial elements with respect to date
  240.         evaluateJ0Dot();
  241.         for (int i = 0; i < 6; ++i) {
  242.             jacobian[i][3] = j0Dot[i][0] * x + j0Dot[i][1] * y + j0Dot[i][2] * z;
  243.         }
  244.         final double da = j0[0][0] * x + j0[0][1] * y + j0[0][2] * z;
  245.         jacobian[5][3] += ksi * (jacobian[0][3] * dt - da);

  246.         if (orbit1.getType() != type || positionAngleType != PositionAngleType.MEAN) {

  247.             // convert to derivatives of Cartesian parameters
  248.             final double[][] j2         = new double[6][6];
  249.             final double[][] pvJacobian = new double[6][4];
  250.             final Orbit updated         = updateOrbit(orbit1);
  251.             updated.getJacobianWrtParameters(PositionAngleType.MEAN, j2);
  252.             for (int i = 0; i < 6; ++i) {
  253.                 for (int j = 0; j < 4; ++j) {
  254.                     pvJacobian[i][j] = j2[i][0] * jacobian[0][j] + j2[i][1] * jacobian[1][j] +
  255.                                        j2[i][2] * jacobian[2][j] + j2[i][3] * jacobian[3][j] +
  256.                                        j2[i][4] * jacobian[4][j] + j2[i][5] * jacobian[5][j];
  257.                 }
  258.             }

  259.             // convert to derivatives of specified parameters
  260.             final double[][] j3 = new double[6][6];
  261.             orbit1.getType().convertType(updated).getJacobianWrtCartesian(positionAngleType, j3);
  262.             for (int j = 0; j < 4; ++j) {
  263.                 for (int i = 0; i < 6; ++i) {
  264.                     jacobian[i][j] = j3[i][0] * pvJacobian[0][j] + j3[i][1] * pvJacobian[1][j] +
  265.                                      j3[i][2] * pvJacobian[2][j] + j3[i][3] * pvJacobian[3][j] +
  266.                                      j3[i][4] * pvJacobian[4][j] + j3[i][5] * pvJacobian[5][j];
  267.                 }
  268.             }

  269.         }

  270.     }

  271.     /** Lazy evaluation of the initial Jacobian time derivative.
  272.      */
  273.     private void evaluateJ0Dot() {

  274.         if (j0Dot == null) {

  275.             j0Dot = new double[6][3];
  276.             final double dt = 1.0e-5 / state0.getOrbit().getKeplerianMeanMotion();
  277.             final Orbit orbit = type.convertType(state0.getOrbit());

  278.             // compute shifted Jacobians
  279.             final double[][] j0m1 = new double[6][6];
  280.             orbit.shiftedBy(-1 * dt).getJacobianWrtCartesian(PositionAngleType.MEAN, j0m1);
  281.             final double[][] j0p1 = new double[6][6];
  282.             orbit.shiftedBy(+1 * dt).getJacobianWrtCartesian(PositionAngleType.MEAN, j0p1);

  283.             // evaluate derivative by finite differences
  284.             for (int i = 0; i < j0Dot.length; ++i) {
  285.                 final double[] m1Row    = j0m1[i];
  286.                 final double[] p1Row    = j0p1[i];
  287.                 final double[] j0DotRow = j0Dot[i];
  288.                 for (int j = 0; j < 3; ++j) {
  289.                     j0DotRow[j] = (p1Row[j + 3] - m1Row[j + 3]) / (2 * dt);
  290.                 }
  291.             }

  292.         }

  293.     }

  294.     /** Update a spacecraft mass due to maneuver.
  295.      * @param mass masse before maneuver
  296.      * @return mass after maneuver
  297.      */
  298.     public double updateMass(final double mass) {
  299.         return massRatio * mass;
  300.     }

  301. }