SmallManeuverAnalyticalModel.java

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

  18. import java.util.Arrays;

  19. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  20. import org.apache.commons.math3.util.FastMath;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.frames.Frame;
  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.propagation.analytical.AdapterPropagator;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.utils.Constants;

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

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

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

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

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

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

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

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

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

  99.     /** Build a maneuver defined in user-specified frame.
  100.      * @param state0 state at maneuver date, <em>before</em> the maneuver
  101.      * is performed
  102.      * @param frame frame in which velocity increment is defined
  103.      * @param dV velocity increment in specified frame
  104.      * @param isp engine specific impulse (s)
  105.      * @exception OrekitException if velocity increment frame cannot be transformed
  106.      */
  107.     public SmallManeuverAnalyticalModel(final SpacecraftState state0, final Frame frame,
  108.                                         final Vector3D dV, final double isp)
  109.         throws OrekitException {

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

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

  114.         // compute initial Jacobian
  115.         final double[][] fullJacobian = new double[6][6];
  116.         j0 = new double[6][3];
  117.         final Orbit orbit0 = type.convertType(state0.getOrbit());
  118.         orbit0.getJacobianWrtCartesian(PositionAngle.MEAN, fullJacobian);
  119.         for (int i = 0; i < j0.length; ++i) {
  120.             System.arraycopy(fullJacobian[i], 3, j0[i], 0, 3);
  121.         }

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

  124.         // compute maneuver effect on Keplerian (or equinoctial) elements
  125.         inertialDV = frame.getTransformTo(state0.getFrame(), state0.getDate()).transformVector(dV);

  126.         // compute mean anomaly change: dM(t1) = dM(t0) + ksi * da * (t1 - t0)
  127.         final double mu = state0.getMu();
  128.         final double a  = state0.getA();
  129.         ksi = -1.5 * FastMath.sqrt(mu / a) / (a * a);

  130.     }

  131.     /** Get the date of the maneuver.
  132.      * @return date of the maneuver
  133.      */
  134.     public AbsoluteDate getDate() {
  135.         return state0.getDate();
  136.     }

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

  144.     /** Get the inertial frame in which the velocity increment is defined.
  145.      * @return inertial frame in which the velocity increment is defined
  146.      * @see #getInertialDV()
  147.      */
  148.     public Frame getInertialFrame() {
  149.         return state0.getFrame();
  150.     }

  151.     /** Compute the effect of the maneuver on an orbit.
  152.      * @param orbit1 original orbit at t<sub>1</sub>, without maneuver
  153.      * @return orbit at t<sub>1</sub>, taking the maneuver
  154.      * into account if t<sub>1</sub> &gt; t<sub>0</sub>
  155.      * @see #apply(SpacecraftState)
  156.      * @see #getJacobian(Orbit, PositionAngle, double[][])
  157.      */
  158.     public Orbit apply(final Orbit orbit1) {

  159.         if (orbit1.getDate().compareTo(state0.getDate()) <= 0) {
  160.             // the maneuver has not occurred yet, don't change anything
  161.             return orbit1;
  162.         }

  163.         return updateOrbit(orbit1);

  164.     }

  165.     /** Compute the effect of the maneuver on a spacecraft state.
  166.      * @param state1 original spacecraft state at t<sub>1</sub>,
  167.      * without maneuver
  168.      * @return spacecraft state at t<sub>1</sub>, taking the maneuver
  169.      * into account if t<sub>1</sub> &gt; t<sub>0</sub>
  170.      * @see #apply(Orbit)
  171.      * @see #getJacobian(Orbit, PositionAngle, double[][])
  172.      */
  173.     public SpacecraftState apply(final SpacecraftState state1) {

  174.         if (state1.getDate().compareTo(state0.getDate()) <= 0) {
  175.             // the maneuver has not occurred yet, don't change anything
  176.             return state1;
  177.         }

  178.         return new SpacecraftState(updateOrbit(state1.getOrbit()),
  179.                                    state1.getAttitude(), updateMass(state1.getMass()));

  180.     }

  181.     /** Compute the effect of the maneuver on an orbit.
  182.      * @param orbit1 original orbit at t<sub>1</sub>, without maneuver
  183.      * @return orbit at t<sub>1</sub>, always taking the maneuver into account
  184.      */
  185.     private Orbit updateOrbit(final Orbit orbit1) {

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

  196.         // convert current orbital state to Keplerian or equinoctial elements
  197.         final double[] parameters = new double[6];
  198.         type.mapOrbitToArray(type.convertType(orbit1), PositionAngle.MEAN, parameters);
  199.         for (int i = 0; i < delta.length; ++i) {
  200.             parameters[i] += delta[i];
  201.         }

  202.         // build updated orbit as Keplerian or equinoctial elements
  203.         final Orbit o = type.mapArrayToOrbit(parameters, PositionAngle.MEAN,
  204.                                              orbit1.getDate(), orbit1.getMu(),
  205.                                              orbit1.getFrame());

  206.         // convert to required type
  207.         return orbit1.getType().convertType(o);

  208.     }

  209.     /** Compute the Jacobian of the orbit with respect to maneuver parameters.
  210.      * <p>
  211.      * The Jacobian matrix is a 6x4 matrix. Element jacobian[i][j] corresponds to
  212.      * the partial derivative of orbital parameter i with respect to maneuver
  213.      * parameter j. The rows order is the same order as used in {@link
  214.      * Orbit#getJacobianWrtCartesian(PositionAngle, double[][]) Orbit.getJacobianWrtCartesian}
  215.      * method. Columns (0, 1, 2) correspond to the velocity increment coordinates
  216.      * (&Delta;V<sub>x</sub>, &Delta;V<sub>y</sub>, &Delta;V<sub>z</sub>) in the
  217.      * inertial frame returned by {@link #getInertialFrame()}, and column 3
  218.      * corresponds to the maneuver date t<sub>0</sub>.
  219.      * </p>
  220.      * @param orbit1 original orbit at t<sub>1</sub>, without maneuver
  221.      * @param positionAngle type of the position angle to use
  222.      * @param jacobian placeholder 6x4 (or larger) matrix to be filled with the Jacobian, if matrix
  223.      * is larger than 6x4, only the 6x4 upper left corner will be modified
  224.      * @see #apply(Orbit)
  225.      * @exception OrekitException if time derivative of the initial Jacobian cannot be computed
  226.      */
  227.     public void getJacobian(final Orbit orbit1, final PositionAngle positionAngle,
  228.                             final double[][] jacobian)
  229.         throws OrekitException {

  230.         final double dt = orbit1.getDate().durationFrom(state0.getDate());
  231.         if (dt < 0) {
  232.             // the maneuver has not occurred yet, Jacobian is null
  233.             for (int i = 0; i < 6; ++i) {
  234.                 Arrays.fill(jacobian[i], 0, 4, 0.0);
  235.             }
  236.             return;
  237.         }

  238.         // derivatives of Keplerian/equinoctial elements with respect to velocity increment
  239.         final double x  = inertialDV.getX();
  240.         final double y  = inertialDV.getY();
  241.         final double z  = inertialDV.getZ();
  242.         for (int i = 0; i < 6; ++i) {
  243.             System.arraycopy(j0[i], 0, jacobian[i], 0, 3);
  244.         }
  245.         for (int j = 0; j < 3; ++j) {
  246.             jacobian[5][j] += ksi * dt * j0[0][j];
  247.         }

  248.         // derivatives of Keplerian/equinoctial elements with respect to date
  249.         evaluateJ0Dot();
  250.         for (int i = 0; i < 6; ++i) {
  251.             jacobian[i][3] = j0Dot[i][0] * x + j0Dot[i][1] * y + j0Dot[i][2] * z;
  252.         }
  253.         final double da = j0[0][0] * x + j0[0][1] * y + j0[0][2] * z;
  254.         jacobian[5][3] += ksi * (jacobian[0][3] * dt - da);

  255.         if (orbit1.getType() != type || positionAngle != PositionAngle.MEAN) {

  256.             // convert to derivatives of cartesian parameters
  257.             final double[][] j2         = new double[6][6];
  258.             final double[][] pvJacobian = new double[6][4];
  259.             final Orbit updated         = updateOrbit(orbit1);
  260.             type.convertType(updated).getJacobianWrtParameters(PositionAngle.MEAN, j2);
  261.             for (int i = 0; i < 6; ++i) {
  262.                 for (int j = 0; j < 4; ++j) {
  263.                     pvJacobian[i][j] = j2[i][0] * jacobian[0][j] + j2[i][1] * jacobian[1][j] +
  264.                                        j2[i][2] * jacobian[2][j] + j2[i][3] * jacobian[3][j] +
  265.                                        j2[i][4] * jacobian[4][j] + j2[i][5] * jacobian[5][j];
  266.                 }
  267.             }

  268.             // convert to derivatives of specified parameters
  269.             final double[][] j3 = new double[6][6];
  270.             updated.getJacobianWrtCartesian(positionAngle, j3);
  271.             for (int j = 0; j < 4; ++j) {
  272.                 for (int i = 0; i < 6; ++i) {
  273.                     jacobian[i][j] = j3[i][0] * pvJacobian[0][j] + j3[i][1] * pvJacobian[1][j] +
  274.                                      j3[i][2] * pvJacobian[2][j] + j3[i][3] * pvJacobian[3][j] +
  275.                                      j3[i][4] * pvJacobian[4][j] + j3[i][5] * pvJacobian[5][j];
  276.                 }
  277.             }

  278.         }

  279.     }

  280.     /** Lazy evaluation of the initial Jacobian time derivative.
  281.      * @exception OrekitException if initial orbit cannot be shifted
  282.      */
  283.     private void evaluateJ0Dot() throws OrekitException {

  284.         if (j0Dot == null) {

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

  288.             // compute shifted Jacobians
  289.             final double[][] j0m1 = new double[6][6];
  290.             orbit.shiftedBy(-1 * dt).getJacobianWrtCartesian(PositionAngle.MEAN, j0m1);
  291.             final double[][] j0p1 = new double[6][6];
  292.             orbit.shiftedBy(+1 * dt).getJacobianWrtCartesian(PositionAngle.MEAN, j0p1);

  293.             // evaluate derivative by finite differences
  294.             for (int i = 0; i < j0Dot.length; ++i) {
  295.                 final double[] m1Row    = j0m1[i];
  296.                 final double[] p1Row    = j0p1[i];
  297.                 final double[] j0DotRow = j0Dot[i];
  298.                 for (int j = 0; j < 3; ++j) {
  299.                     j0DotRow[j] = (p1Row[j + 3] - m1Row[j + 3]) / (2 * dt);
  300.                 }
  301.             }

  302.         }

  303.     }

  304.     /** Update a spacecraft mass due to maneuver.
  305.      * @param mass masse before maneuver
  306.      * @return mass after maneuver
  307.      */
  308.     public double updateMass(final double mass) {
  309.         return massRatio * mass;
  310.     }

  311. }