CartesianOrbit.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.orbits;

  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.List;

  22. import org.apache.commons.math3.exception.ConvergenceException;
  23. import org.apache.commons.math3.geometry.euclidean.threed.Rotation;
  24. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  25. import org.apache.commons.math3.util.FastMath;
  26. import org.apache.commons.math3.util.Pair;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.PVCoordinates;


  31. /** This class holds cartesian orbital parameters.

  32.  * <p>
  33.  * The parameters used internally are the cartesian coordinates:
  34.  *   <ul>
  35.  *     <li>x</li>
  36.  *     <li>y</li>
  37.  *     <li>z</li>
  38.  *     <li>xDot</li>
  39.  *     <li>yDot</li>
  40.  *     <li>zDot</li>
  41.  *   </ul>
  42.  * contained in {@link PVCoordinates}.
  43.  * </p>

  44.  * <p>
  45.  * Note that the implementation of this class delegates all non-cartesian related
  46.  * computations ({@link #getA()}, {@link #getEquinoctialEx()}, ...) to an underlying
  47.  * instance of the {@link EquinoctialOrbit} class. This implies that using this class
  48.  * only for analytical computations which are always based on non-cartesian
  49.  * parameters is perfectly possible but somewhat sub-optimal.
  50.  * </p>
  51.  * <p>
  52.  * The instance <code>CartesianOrbit</code> is guaranteed to be immutable.
  53.  * </p>
  54.  * @see    Orbit
  55.  * @see    KeplerianOrbit
  56.  * @see    CircularOrbit
  57.  * @see    EquinoctialOrbit
  58.  * @author Luc Maisonobe
  59.  * @author Guylaine Prat
  60.  * @author Fabien Maussion
  61.  * @author V&eacute;ronique Pommier-Maurussane
  62.  */
  63. public class CartesianOrbit extends Orbit {

  64.     /** Serializable UID. */
  65.     private static final long serialVersionUID = -5411308212620896302L;

  66.     /** Underlying equinoctial orbit to which high-level methods are delegated. */
  67.     private transient EquinoctialOrbit equinoctial;

  68.     /** Constructor from cartesian parameters.
  69.      * @param pvCoordinates the position and velocity of the satellite.
  70.      * @param frame the frame in which the {@link PVCoordinates} are defined
  71.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  72.      * @param date date of the orbital parameters
  73.      * @param mu central attraction coefficient (m<sup>3</sup>/s<sup>2</sup>)
  74.      * @exception IllegalArgumentException if frame is not a {@link
  75.      * Frame#isPseudoInertial pseudo-inertial frame}
  76.      */
  77.     public CartesianOrbit(final PVCoordinates pvCoordinates, final Frame frame,
  78.                           final AbsoluteDate date, final double mu)
  79.         throws IllegalArgumentException {
  80.         super(pvCoordinates, frame, date, mu);
  81.         equinoctial = null;
  82.     }

  83.     /** Constructor from any kind of orbital parameters.
  84.      * @param op orbital parameters to copy
  85.      */
  86.     public CartesianOrbit(final Orbit op) {
  87.         super(op.getPVCoordinates(), op.getFrame(), op.getDate(), op.getMu());
  88.         if (op instanceof EquinoctialOrbit) {
  89.             equinoctial = (EquinoctialOrbit) op;
  90.         } else if (op instanceof CartesianOrbit) {
  91.             equinoctial = ((CartesianOrbit) op).equinoctial;
  92.         } else {
  93.             equinoctial = null;
  94.         }
  95.     }

  96.     /** {@inheritDoc} */
  97.     public OrbitType getType() {
  98.         return OrbitType.CARTESIAN;
  99.     }

  100.     /** Lazy evaluation of equinoctial parameters. */
  101.     private void initEquinoctial() {
  102.         if (equinoctial == null) {
  103.             equinoctial = new EquinoctialOrbit(getPVCoordinates(), getFrame(), getDate(), getMu());
  104.         }
  105.     }

  106.     /** {@inheritDoc} */
  107.     public double getA() {
  108.         // lazy evaluation of semi-major axis
  109.         final double r  = getPVCoordinates().getPosition().getNorm();
  110.         final double V2 = getPVCoordinates().getVelocity().getNormSq();
  111.         return r / (2 - r * V2 / getMu());
  112.     }

  113.     /** {@inheritDoc} */
  114.     public double getE() {
  115.         final Vector3D pvP   = getPVCoordinates().getPosition();
  116.         final Vector3D pvV   = getPVCoordinates().getVelocity();
  117.         final double rV2OnMu = pvP.getNorm() * pvV.getNormSq() / getMu();
  118.         final double eSE     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(getMu() * getA());
  119.         final double eCE     = rV2OnMu - 1;
  120.         return FastMath.sqrt(eCE * eCE + eSE * eSE);
  121.     }

  122.     /** {@inheritDoc} */
  123.     public double getI() {
  124.         return Vector3D.angle(Vector3D.PLUS_K, getPVCoordinates().getMomentum());
  125.     }

  126.     /** {@inheritDoc} */
  127.     public double getEquinoctialEx() {
  128.         initEquinoctial();
  129.         return equinoctial.getEquinoctialEx();
  130.     }

  131.     /** {@inheritDoc} */
  132.     public double getEquinoctialEy() {
  133.         initEquinoctial();
  134.         return equinoctial.getEquinoctialEy();
  135.     }

  136.     /** {@inheritDoc} */
  137.     public double getHx() {
  138.         final Vector3D w = getPVCoordinates().getMomentum().normalize();
  139.         // Check for equatorial retrograde orbit
  140.         if (((w.getX() * w.getX() + w.getY() * w.getY()) == 0) && w.getZ() < 0) {
  141.             return Double.NaN;
  142.         }
  143.         return -w.getY() / (1 + w.getZ());
  144.     }

  145.     /** {@inheritDoc} */
  146.     public double getHy() {
  147.         final Vector3D w = getPVCoordinates().getMomentum().normalize();
  148.         // Check for equatorial retrograde orbit
  149.         if (((w.getX() * w.getX() + w.getY() * w.getY()) == 0) && w.getZ() < 0) {
  150.             return Double.NaN;
  151.         }
  152.         return  w.getX() / (1 + w.getZ());
  153.     }

  154.     /** {@inheritDoc} */
  155.     public double getLv() {
  156.         initEquinoctial();
  157.         return equinoctial.getLv();
  158.     }

  159.     /** {@inheritDoc} */
  160.     public double getLE() {
  161.         initEquinoctial();
  162.         return equinoctial.getLE();
  163.     }

  164.     /** {@inheritDoc} */
  165.     public double getLM() {
  166.         initEquinoctial();
  167.         return equinoctial.getLM();
  168.     }

  169.     /** {@inheritDoc} */
  170.     protected PVCoordinates initPVCoordinates() {
  171.         // nothing to do here, as the canonical elements are already the cartesian ones
  172.         return getPVCoordinates();
  173.     }

  174.     /** {@inheritDoc} */
  175.     public CartesianOrbit shiftedBy(final double dt) {
  176.         final PVCoordinates shiftedPV = (getA() < 0) ? shiftPVHyperbolic(dt) : shiftPVElliptic(dt);
  177.         return new CartesianOrbit(shiftedPV, getFrame(), getDate().shiftedBy(dt), getMu());
  178.     }

  179.     /** {@inheritDoc}
  180.      * <p>
  181.      * The interpolated instance is created by polynomial Hermite interpolation
  182.      * ensuring velocity remains the exact derivative of position.
  183.      * </p>
  184.      * <p>
  185.      * As this implementation of interpolation is polynomial, it should be used only
  186.      * with small samples (about 10-20 points) in order to avoid <a
  187.      * href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's phenomenon</a>
  188.      * and numerical problems (including NaN appearing).
  189.      * </p>
  190.      * <p>
  191.      * If orbit interpolation on large samples is needed, using the {@link
  192.      * org.orekit.propagation.analytical.Ephemeris} class is a better way than using this
  193.      * low-level interpolation. The Ephemeris class automatically handles selection of
  194.      * a neighboring sub-sample with a predefined number of point from a large global sample
  195.      * in a thread-safe way.
  196.      * </p>
  197.      */
  198.     public CartesianOrbit interpolate(final AbsoluteDate date, final Collection<Orbit> sample) {
  199.         final List<Pair<AbsoluteDate, PVCoordinates>> datedPV =
  200.                 new ArrayList<Pair<AbsoluteDate, PVCoordinates>>(sample.size());
  201.         for (final Orbit orbit : sample) {
  202.             datedPV.add(new Pair<AbsoluteDate, PVCoordinates>(orbit.getDate(), orbit.getPVCoordinates()));
  203.         }
  204.         final PVCoordinates interpolated = PVCoordinates.interpolate(date, true, datedPV);
  205.         return new CartesianOrbit(interpolated, getFrame(), date, getMu());
  206.     }

  207.     /** Compute shifted position and velocity in elliptic case.
  208.      * @param dt time shift
  209.      * @return shifted position and velocity
  210.      */
  211.     private PVCoordinates shiftPVElliptic(final double dt) {

  212.         // preliminary computation
  213.         final Vector3D pvP   = getPVCoordinates().getPosition();
  214.         final Vector3D pvV   = getPVCoordinates().getVelocity();
  215.         final double r       = pvP.getNorm();
  216.         final double rV2OnMu = r * pvV.getNormSq() / getMu();
  217.         final double a       = getA();
  218.         final double eSE     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(getMu() * a);
  219.         final double eCE     = rV2OnMu - 1;
  220.         final double e2      = eCE * eCE + eSE * eSE;

  221.         // we can use any arbitrary reference 2D frame in the orbital plane
  222.         // in order to simplify some equations below, we use the current position as the u axis
  223.         final Vector3D u     = pvP.normalize();
  224.         final Vector3D v     = Vector3D.crossProduct(getPVCoordinates().getMomentum(), u).normalize();

  225.         // the following equations rely on the specific choice of u explained above,
  226.         // some coefficients that vanish to 0 in this case have already been removed here
  227.         final double ex      = (eCE - e2) * a / r;
  228.         final double ey      = -FastMath.sqrt(1 - e2) * eSE * a / r;
  229.         final double beta    = 1 / (1 + FastMath.sqrt(1 - e2));
  230.         final double thetaE0 = FastMath.atan2(ey + eSE * beta * ex, r / a + ex - eSE * beta * ey);
  231.         final double thetaM0 = thetaE0 - ex * FastMath.sin(thetaE0) + ey * FastMath.cos(thetaE0);

  232.         // compute in-plane shifted eccentric argument
  233.         final double thetaM1 = thetaM0 + getKeplerianMeanMotion() * dt;
  234.         final double thetaE1 = meanToEccentric(thetaM1, ex, ey);
  235.         final double cTE     = FastMath.cos(thetaE1);
  236.         final double sTE     = FastMath.sin(thetaE1);

  237.         // compute shifted in-plane cartesian coordinates
  238.         final double exey   = ex * ey;
  239.         final double exCeyS = ex * cTE + ey * sTE;
  240.         final double x      = a * ((1 - beta * ey * ey) * cTE + beta * exey * sTE - ex);
  241.         final double y      = a * ((1 - beta * ex * ex) * sTE + beta * exey * cTE - ey);
  242.         final double factor = FastMath.sqrt(getMu() / a) / (1 - exCeyS);
  243.         final double xDot   = factor * (-sTE + beta * ey * exCeyS);
  244.         final double yDot   = factor * ( cTE - beta * ex * exCeyS);

  245.         return new PVCoordinates(new Vector3D(x, u, y, v), new Vector3D(xDot, u, yDot, v));

  246.     }

  247.     /** Compute shifted position and velocity in hyperbolic case.
  248.      * @param dt time shift
  249.      * @return shifted position and velocity
  250.      */
  251.     private PVCoordinates shiftPVHyperbolic(final double dt) {

  252.         final PVCoordinates pv = getPVCoordinates();
  253.         final Vector3D pvP   = pv.getPosition();
  254.         final Vector3D pvV   = pv.getVelocity();
  255.         final Vector3D pvM   = pv.getMomentum();
  256.         final double r       = pvP.getNorm();
  257.         final double rV2OnMu = r * pvV.getNormSq() / getMu();
  258.         final double a       = getA();
  259.         final double muA     = getMu() * a;
  260.         final double e       = FastMath.sqrt(1 - Vector3D.dotProduct(pvM, pvM) / muA);
  261.         final double sqrt    = FastMath.sqrt((e + 1) / (e - 1));

  262.         // compute mean anomaly
  263.         final double eSH     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(-muA);
  264.         final double eCH     = rV2OnMu - 1;
  265.         final double H0      = FastMath.log((eCH + eSH) / (eCH - eSH)) / 2;
  266.         final double M0      = e * FastMath.sinh(H0) - H0;

  267.         // find canonical 2D frame with p pointing to perigee
  268.         final double v0      = 2 * FastMath.atan(sqrt * FastMath.tanh(H0 / 2));
  269.         final Vector3D p     = new Rotation(pvM, -v0).applyTo(pvP).normalize();
  270.         final Vector3D q     = Vector3D.crossProduct(pvM, p).normalize();

  271.         // compute shifted eccentric anomaly
  272.         final double M1      = M0 + getKeplerianMeanMotion() * dt;
  273.         final double H1      = meanToHyperbolicEccentric(M1, e);

  274.         // compute shifted in-plane cartesian coordinates
  275.         final double cH     = FastMath.cosh(H1);
  276.         final double sH     = FastMath.sinh(H1);
  277.         final double sE2m1  = FastMath.sqrt((e - 1) * (e + 1));

  278.         // coordinates of position and velocity in the orbital plane
  279.         final double x      = a * (cH - e);
  280.         final double y      = -a * sE2m1 * sH;
  281.         final double factor = FastMath.sqrt(getMu() / -a) / (e * cH - 1);
  282.         final double xDot   = -factor * sH;
  283.         final double yDot   =  factor * sE2m1 * cH;

  284.         return new PVCoordinates(new Vector3D(x, p, y, q), new Vector3D(xDot, p, yDot, q));

  285.     }

  286.     /** Computes the eccentric in-plane argument from the mean in-plane argument.
  287.      * @param thetaM = mean in-plane argument (rad)
  288.      * @param ex first component of eccentricity vector
  289.      * @param ey second component of eccentricity vector
  290.      * @return the eccentric in-plane argument.
  291.      */
  292.     private double meanToEccentric(final double thetaM, final double ex, final double ey) {
  293.         // Generalization of Kepler equation to in-plane parameters
  294.         // with thetaE = eta + E and
  295.         //      thetaM = eta + M = thetaE - ex.sin(thetaE) + ey.cos(thetaE)
  296.         // and eta being counted from an arbitrary reference in the orbital plane
  297.         double thetaE        = thetaM;
  298.         double thetaEMthetaM = 0.0;
  299.         int    iter          = 0;
  300.         do {
  301.             final double cosThetaE = FastMath.cos(thetaE);
  302.             final double sinThetaE = FastMath.sin(thetaE);

  303.             final double f2 = ex * sinThetaE - ey * cosThetaE;
  304.             final double f1 = 1.0 - ex * cosThetaE - ey * sinThetaE;
  305.             final double f0 = thetaEMthetaM - f2;

  306.             final double f12 = 2.0 * f1;
  307.             final double shift = f0 * f12 / (f1 * f12 - f0 * f2);

  308.             thetaEMthetaM -= shift;
  309.             thetaE         = thetaM + thetaEMthetaM;

  310.             if (FastMath.abs(shift) <= 1.0e-12) {
  311.                 return thetaE;
  312.             }

  313.         } while (++iter < 50);

  314.         throw new ConvergenceException();

  315.     }

  316.     /** Computes the hyperbolic eccentric anomaly from the mean anomaly.
  317.      * <p>
  318.      * The algorithm used here for solving hyperbolic Kepler equation is
  319.      * Danby's iterative method (3rd order) with Vallado's initial guess.
  320.      * </p>
  321.      * @param M mean anomaly (rad)
  322.      * @param ecc eccentricity
  323.      * @return the hyperbolic eccentric anomaly
  324.      */
  325.     private double meanToHyperbolicEccentric(final double M, final double ecc) {

  326.         // Resolution of hyperbolic Kepler equation for keplerian parameters

  327.         // Initial guess
  328.         double H;
  329.         if (ecc < 1.6) {
  330.             if ((-FastMath.PI < M && M < 0.) || M > FastMath.PI) {
  331.                 H = M - ecc;
  332.             } else {
  333.                 H = M + ecc;
  334.             }
  335.         } else {
  336.             if (ecc < 3.6 && FastMath.abs(M) > FastMath.PI) {
  337.                 H = M - FastMath.copySign(ecc, M);
  338.             } else {
  339.                 H = M / (ecc - 1.);
  340.             }
  341.         }

  342.         // Iterative computation
  343.         int iter = 0;
  344.         do {
  345.             final double f3  = ecc * FastMath.cosh(H);
  346.             final double f2  = ecc * FastMath.sinh(H);
  347.             final double f1  = f3 - 1.;
  348.             final double f0  = f2 - H - M;
  349.             final double f12 = 2. * f1;
  350.             final double d   = f0 / f12;
  351.             final double fdf = f1 - d * f2;
  352.             final double ds  = f0 / fdf;

  353.             final double shift = f0 / (fdf + ds * ds * f3 / 6.);

  354.             H -= shift;

  355.             if (FastMath.abs(shift) <= 1.0e-12) {
  356.                 return H;
  357.             }

  358.         } while (++iter < 50);

  359.         throw new ConvergenceException(OrekitMessages.UNABLE_TO_COMPUTE_HYPERBOLIC_ECCENTRIC_ANOMALY,
  360.                                        iter);
  361.     }

  362.     @Override
  363.     public void getJacobianWrtCartesian(final PositionAngle type, final double[][] jacobian) {
  364.         // this is the fastest way to set the 6x6 identity matrix
  365.         for (int i = 0; i < 6; i++) {
  366.             for (int j = 0; j < 6; j++) {
  367.                 jacobian[i][j] = 0;
  368.             }
  369.             jacobian[i][i] = 1;
  370.         }
  371.     }

  372.     @Override
  373.     protected double[][] computeJacobianMeanWrtCartesian() {
  374.         // not used
  375.         return null;
  376.     }
  377.     @Override
  378.     protected double[][] computeJacobianEccentricWrtCartesian() {
  379.         // not used
  380.         return null;
  381.     }

  382.     @Override
  383.     protected double[][] computeJacobianTrueWrtCartesian() {
  384.         // not used
  385.         return null;
  386.     }

  387.     /** {@inheritDoc} */
  388.     public void addKeplerContribution(final PositionAngle type, final double gm,
  389.                                       final double[] pDot) {

  390.         final PVCoordinates pv = getPVCoordinates();

  391.         // position derivative is velocity
  392.         final Vector3D velocity = pv.getVelocity();
  393.         pDot[0] += velocity.getX();
  394.         pDot[1] += velocity.getY();
  395.         pDot[2] += velocity.getZ();

  396.         // velocity derivative is Newtonian acceleration
  397.         final Vector3D position = pv.getPosition();
  398.         final double r2         = position.getNormSq();
  399.         final double coeff      = -gm / (r2 * FastMath.sqrt(r2));
  400.         pDot[3] += coeff * position.getX();
  401.         pDot[4] += coeff * position.getY();
  402.         pDot[5] += coeff * position.getZ();

  403.     }

  404.     /**  Returns a string representation of this Orbit object.
  405.      * @return a string representation of this object
  406.      */
  407.     public String toString() {
  408.         return "cartesian parameters: " + getPVCoordinates().toString();
  409.     }

  410.     /** Replace the instance with a data transfer object for serialization.
  411.      * <p>
  412.      * This intermediate class serializes all needed parameters,
  413.      * including position-velocity which are <em>not</em> serialized by parent
  414.      * {@link Orbit} class.
  415.      * </p>
  416.      * @return data transfer object that will be serialized
  417.      */
  418.     private Object writeReplace() {
  419.         return new DataTransferObject(getPVCoordinates(), getFrame(), getDate(), getMu());
  420.     }

  421.     /** Internal class used only for serialization. */
  422.     private static class DataTransferObject implements Serializable {

  423.         /** Serializable UID. */
  424.         private static final long serialVersionUID = 4184412866917874790L;

  425.         /** Computed PVCoordinates. */
  426.         private PVCoordinates pvCoordinates;

  427.         /** Frame in which are defined the orbital parameters. */
  428.         private final Frame frame;

  429.         /** Date of the orbital parameters. */
  430.         private final AbsoluteDate date;

  431.         /** Value of mu used to compute position and velocity (m<sup>3</sup>/s<sup>2</sup>). */
  432.         private final double mu;

  433.         /** Simple constructor.
  434.          * @param pvCoordinates the position and velocity of the satellite.
  435.          * @param frame the frame in which the {@link PVCoordinates} are defined
  436.          * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  437.          * @param date date of the orbital parameters
  438.          * @param mu central attraction coefficient (m<sup>3</sup>/s<sup>2</sup>)
  439.          */
  440.         private DataTransferObject(final PVCoordinates pvCoordinates, final Frame frame,
  441.                                    final AbsoluteDate date, final double mu) {
  442.             this.pvCoordinates = pvCoordinates;
  443.             this.frame         = frame;
  444.             this.date          = date;
  445.             this.mu            = mu;
  446.         }

  447.         /** Replace the deserialized data transfer object with a {@link CartesianOrbit}.
  448.          * @return replacement {@link CartesianOrbit}
  449.          */
  450.         private Object readResolve() {
  451.             // build a new provider, with an empty cache
  452.             return new CartesianOrbit(pvCoordinates, frame, date, mu);
  453.         }

  454.     }

  455. }