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

  18. import java.io.Serializable;
  19. import java.util.stream.Stream;

  20. import org.hipparchus.analysis.differentiation.DSFactory;
  21. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  25. import org.hipparchus.geometry.euclidean.threed.Rotation;
  26. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  27. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  28. import org.hipparchus.util.FastMath;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.frames.Frame;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.CartesianDerivativesFilter;
  33. import org.orekit.utils.PVCoordinates;
  34. import org.orekit.utils.TimeStampedPVCoordinates;


  35. /** This class holds Cartesian orbital parameters.

  36.  * <p>
  37.  * The parameters used internally are the Cartesian coordinates:
  38.  *   <ul>
  39.  *     <li>x</li>
  40.  *     <li>y</li>
  41.  *     <li>z</li>
  42.  *     <li>xDot</li>
  43.  *     <li>yDot</li>
  44.  *     <li>zDot</li>
  45.  *   </ul>
  46.  * contained in {@link PVCoordinates}.
  47.  *

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

  68.     /** Serializable UID. */
  69.     private static final long serialVersionUID = 20170414L;

  70.     /** Factory for first time derivatives. */
  71.     private static final DSFactory FACTORY = new DSFactory(1, 1);

  72.     /** Indicator for non-Keplerian derivatives. */
  73.     private final transient boolean hasNonKeplerianAcceleration;

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

  76.     /** Constructor from Cartesian parameters.
  77.      *
  78.      * <p> The acceleration provided in {@code pvCoordinates} is accessible using
  79.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  80.      * use {@code mu} and the position to compute the acceleration, including
  81.      * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
  82.      *
  83.      * @param pvaCoordinates the position, velocity and acceleration of the satellite.
  84.      * @param frame the frame in which the {@link PVCoordinates} are defined
  85.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  86.      * @param mu central attraction coefficient (m³/s²)
  87.      * @exception IllegalArgumentException if frame is not a {@link
  88.      * Frame#isPseudoInertial pseudo-inertial frame}
  89.      */
  90.     public CartesianOrbit(final TimeStampedPVCoordinates pvaCoordinates,
  91.                           final Frame frame, final double mu)
  92.         throws IllegalArgumentException {
  93.         super(pvaCoordinates, frame, mu);
  94.         hasNonKeplerianAcceleration = hasNonKeplerianAcceleration(pvaCoordinates, mu);
  95.         equinoctial = null;
  96.     }

  97.     /** Constructor from Cartesian parameters.
  98.      *
  99.      * <p> The acceleration provided in {@code pvCoordinates} is accessible using
  100.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  101.      * use {@code mu} and the position to compute the acceleration, including
  102.      * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
  103.      *
  104.      * @param pvaCoordinates the position and velocity of the satellite.
  105.      * @param frame the frame in which the {@link PVCoordinates} are defined
  106.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  107.      * @param date date of the orbital parameters
  108.      * @param mu central attraction coefficient (m³/s²)
  109.      * @exception IllegalArgumentException if frame is not a {@link
  110.      * Frame#isPseudoInertial pseudo-inertial frame}
  111.      */
  112.     public CartesianOrbit(final PVCoordinates pvaCoordinates, final Frame frame,
  113.                           final AbsoluteDate date, final double mu)
  114.         throws IllegalArgumentException {
  115.         this(new TimeStampedPVCoordinates(date, pvaCoordinates), frame, mu);
  116.     }

  117.     /** Constructor from any kind of orbital parameters.
  118.      * @param op orbital parameters to copy
  119.      */
  120.     public CartesianOrbit(final Orbit op) {
  121.         super(op.getPVCoordinates(), op.getFrame(), op.getMu());
  122.         hasNonKeplerianAcceleration = op.hasDerivatives();
  123.         if (op instanceof EquinoctialOrbit) {
  124.             equinoctial = (EquinoctialOrbit) op;
  125.         } else if (op instanceof CartesianOrbit) {
  126.             equinoctial = ((CartesianOrbit) op).equinoctial;
  127.         } else {
  128.             equinoctial = null;
  129.         }
  130.     }

  131.     /** {@inheritDoc} */
  132.     public OrbitType getType() {
  133.         return OrbitType.CARTESIAN;
  134.     }

  135.     /** Lazy evaluation of equinoctial parameters. */
  136.     private void initEquinoctial() {
  137.         if (equinoctial == null) {
  138.             if (hasDerivatives()) {
  139.                 // getPVCoordinates includes accelerations that will be interpreted as derivatives
  140.                 equinoctial = new EquinoctialOrbit(getPVCoordinates(), getFrame(), getDate(), getMu());
  141.             } else {
  142.                 // get rid of Keplerian acceleration so we don't assume
  143.                 // we have derivatives when in fact we don't have them
  144.                 equinoctial = new EquinoctialOrbit(new PVCoordinates(getPVCoordinates().getPosition(),
  145.                                                                      getPVCoordinates().getVelocity()),
  146.                                                    getFrame(), getDate(), getMu());
  147.             }
  148.         }
  149.     }

  150.     /** Get position with derivatives.
  151.      * @return position with derivatives
  152.      */
  153.     private FieldVector3D<DerivativeStructure> getPositionDS() {
  154.         final Vector3D p = getPVCoordinates().getPosition();
  155.         final Vector3D v = getPVCoordinates().getVelocity();
  156.         return new FieldVector3D<>(FACTORY.build(p.getX(), v.getX()),
  157.                                    FACTORY.build(p.getY(), v.getY()),
  158.                                    FACTORY.build(p.getZ(), v.getZ()));
  159.     }

  160.     /** Get velocity with derivatives.
  161.      * @return velocity with derivatives
  162.      */
  163.     private FieldVector3D<DerivativeStructure> getVelocityDS() {
  164.         final Vector3D v = getPVCoordinates().getVelocity();
  165.         final Vector3D a = getPVCoordinates().getAcceleration();
  166.         return new FieldVector3D<>(FACTORY.build(v.getX(), a.getX()),
  167.                                    FACTORY.build(v.getY(), a.getY()),
  168.                                    FACTORY.build(v.getZ(), a.getZ()));
  169.     }

  170.     /** {@inheritDoc} */
  171.     public double getA() {
  172.         final double r  = getPVCoordinates().getPosition().getNorm();
  173.         final double V2 = getPVCoordinates().getVelocity().getNormSq();
  174.         return r / (2 - r * V2 / getMu());
  175.     }

  176.     /** {@inheritDoc} */
  177.     public double getADot() {
  178.         if (hasDerivatives()) {
  179.             final DerivativeStructure r  = getPositionDS().getNorm();
  180.             final DerivativeStructure V2 = getVelocityDS().getNormSq();
  181.             final DerivativeStructure a  = r.divide(r.multiply(V2).divide(getMu()).subtract(2).negate());
  182.             return a.getPartialDerivative(1);
  183.         } else {
  184.             return Double.NaN;
  185.         }
  186.     }

  187.     /** {@inheritDoc} */
  188.     public double getE() {
  189.         final double muA = getMu() * getA();
  190.         if (muA > 0) {
  191.             // elliptic or circular orbit
  192.             final Vector3D pvP   = getPVCoordinates().getPosition();
  193.             final Vector3D pvV   = getPVCoordinates().getVelocity();
  194.             final double rV2OnMu = pvP.getNorm() * pvV.getNormSq() / getMu();
  195.             final double eSE     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(muA);
  196.             final double eCE     = rV2OnMu - 1;
  197.             return FastMath.sqrt(eCE * eCE + eSE * eSE);
  198.         } else {
  199.             // hyperbolic orbit
  200.             final Vector3D pvM = getPVCoordinates().getMomentum();
  201.             return FastMath.sqrt(1 - pvM.getNormSq() / muA);
  202.         }
  203.     }

  204.     /** {@inheritDoc} */
  205.     public double getEDot() {
  206.         if (hasDerivatives()) {
  207.             final FieldVector3D<DerivativeStructure> pvP   = getPositionDS();
  208.             final FieldVector3D<DerivativeStructure> pvV   = getVelocityDS();
  209.             final DerivativeStructure r       = getPositionDS().getNorm();
  210.             final DerivativeStructure V2      = getVelocityDS().getNormSq();
  211.             final DerivativeStructure rV2OnMu = r.multiply(V2).divide(getMu());
  212.             final DerivativeStructure a       = r.divide(rV2OnMu.negate().add(2));
  213.             final DerivativeStructure eSE     = FieldVector3D.dotProduct(pvP, pvV).divide(a.multiply(getMu()).sqrt());
  214.             final DerivativeStructure eCE     = rV2OnMu.subtract(1);
  215.             final DerivativeStructure e       = eCE.multiply(eCE).add(eSE.multiply(eSE)).sqrt();
  216.             return e.getPartialDerivative(1);
  217.         } else {
  218.             return Double.NaN;
  219.         }
  220.     }

  221.     /** {@inheritDoc} */
  222.     public double getI() {
  223.         return Vector3D.angle(Vector3D.PLUS_K, getPVCoordinates().getMomentum());
  224.     }

  225.     /** {@inheritDoc} */
  226.     public double getIDot() {
  227.         if (hasDerivatives()) {
  228.             final FieldVector3D<DerivativeStructure> momentum =
  229.                             FieldVector3D.crossProduct(getPositionDS(), getVelocityDS());
  230.             final DerivativeStructure i = FieldVector3D.angle(Vector3D.PLUS_K, momentum);
  231.             return i.getPartialDerivative(1);
  232.         } else {
  233.             return Double.NaN;
  234.         }
  235.     }

  236.     /** {@inheritDoc} */
  237.     public double getEquinoctialEx() {
  238.         initEquinoctial();
  239.         return equinoctial.getEquinoctialEx();
  240.     }

  241.     /** {@inheritDoc} */
  242.     public double getEquinoctialExDot() {
  243.         initEquinoctial();
  244.         return equinoctial.getEquinoctialExDot();
  245.     }

  246.     /** {@inheritDoc} */
  247.     public double getEquinoctialEy() {
  248.         initEquinoctial();
  249.         return equinoctial.getEquinoctialEy();
  250.     }

  251.     /** {@inheritDoc} */
  252.     public double getEquinoctialEyDot() {
  253.         initEquinoctial();
  254.         return equinoctial.getEquinoctialEyDot();
  255.     }

  256.     /** {@inheritDoc} */
  257.     public double getHx() {
  258.         final Vector3D w = getPVCoordinates().getMomentum().normalize();
  259.         // Check for equatorial retrograde orbit
  260.         if (((w.getX() * w.getX() + w.getY() * w.getY()) == 0) && w.getZ() < 0) {
  261.             return Double.NaN;
  262.         }
  263.         return -w.getY() / (1 + w.getZ());
  264.     }

  265.     /** {@inheritDoc} */
  266.     public double getHxDot() {
  267.         if (hasDerivatives()) {
  268.             final FieldVector3D<DerivativeStructure> w =
  269.                             FieldVector3D.crossProduct(getPositionDS(), getVelocityDS()).normalize();
  270.             // Check for equatorial retrograde orbit
  271.             final double x = w.getX().getValue();
  272.             final double y = w.getY().getValue();
  273.             final double z = w.getZ().getValue();
  274.             if (((x * x + y * y) == 0) && z < 0) {
  275.                 return Double.NaN;
  276.             }
  277.             final DerivativeStructure hx = w.getY().negate().divide(w.getZ().add(1));
  278.             return hx.getPartialDerivative(1);
  279.         } else {
  280.             return Double.NaN;
  281.         }
  282.     }

  283.     /** {@inheritDoc} */
  284.     public double getHy() {
  285.         final Vector3D w = getPVCoordinates().getMomentum().normalize();
  286.         // Check for equatorial retrograde orbit
  287.         if (((w.getX() * w.getX() + w.getY() * w.getY()) == 0) && w.getZ() < 0) {
  288.             return Double.NaN;
  289.         }
  290.         return  w.getX() / (1 + w.getZ());
  291.     }

  292.     /** {@inheritDoc} */
  293.     public double getHyDot() {
  294.         if (hasDerivatives()) {
  295.             final FieldVector3D<DerivativeStructure> w =
  296.                             FieldVector3D.crossProduct(getPositionDS(), getVelocityDS()).normalize();
  297.             // Check for equatorial retrograde orbit
  298.             final double x = w.getX().getValue();
  299.             final double y = w.getY().getValue();
  300.             final double z = w.getZ().getValue();
  301.             if (((x * x + y * y) == 0) && z < 0) {
  302.                 return Double.NaN;
  303.             }
  304.             final DerivativeStructure hy = w.getX().divide(w.getZ().add(1));
  305.             return hy.getPartialDerivative(1);
  306.         } else {
  307.             return Double.NaN;
  308.         }
  309.     }

  310.     /** {@inheritDoc} */
  311.     public double getLv() {
  312.         initEquinoctial();
  313.         return equinoctial.getLv();
  314.     }

  315.     /** {@inheritDoc} */
  316.     public double getLvDot() {
  317.         initEquinoctial();
  318.         return equinoctial.getLvDot();
  319.     }

  320.     /** {@inheritDoc} */
  321.     public double getLE() {
  322.         initEquinoctial();
  323.         return equinoctial.getLE();
  324.     }

  325.     /** {@inheritDoc} */
  326.     public double getLEDot() {
  327.         initEquinoctial();
  328.         return equinoctial.getLEDot();
  329.     }

  330.     /** {@inheritDoc} */
  331.     public double getLM() {
  332.         initEquinoctial();
  333.         return equinoctial.getLM();
  334.     }

  335.     /** {@inheritDoc} */
  336.     public double getLMDot() {
  337.         initEquinoctial();
  338.         return equinoctial.getLMDot();
  339.     }

  340.     /** {@inheritDoc} */
  341.     public boolean hasDerivatives() {
  342.         return hasNonKeplerianAcceleration;
  343.     }

  344.     /** {@inheritDoc} */
  345.     protected TimeStampedPVCoordinates initPVCoordinates() {
  346.         // nothing to do here, as the canonical elements are already the Cartesian ones
  347.         return getPVCoordinates();
  348.     }

  349.     /** {@inheritDoc} */
  350.     public CartesianOrbit shiftedBy(final double dt) {
  351.         final PVCoordinates shiftedPV = (getA() < 0) ? shiftPVHyperbolic(dt) : shiftPVElliptic(dt);
  352.         return new CartesianOrbit(shiftedPV, getFrame(), getDate().shiftedBy(dt), getMu());
  353.     }

  354.     /** {@inheritDoc}
  355.      * <p>
  356.      * The interpolated instance is created by polynomial Hermite interpolation
  357.      * ensuring velocity remains the exact derivative of position.
  358.      * </p>
  359.      * <p>
  360.      * As this implementation of interpolation is polynomial, it should be used only
  361.      * with small samples (about 10-20 points) in order to avoid <a
  362.      * href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's phenomenon</a>
  363.      * and numerical problems (including NaN appearing).
  364.      * </p>
  365.      * <p>
  366.      * If orbit interpolation on large samples is needed, using the {@link
  367.      * org.orekit.propagation.analytical.Ephemeris} class is a better way than using this
  368.      * low-level interpolation. The Ephemeris class automatically handles selection of
  369.      * a neighboring sub-sample with a predefined number of point from a large global sample
  370.      * in a thread-safe way.
  371.      * </p>
  372.      */
  373.     public CartesianOrbit interpolate(final AbsoluteDate date, final Stream<Orbit> sample) {
  374.         final TimeStampedPVCoordinates interpolated =
  375.                 TimeStampedPVCoordinates.interpolate(date, CartesianDerivativesFilter.USE_PVA,
  376.                                                      sample.map(orbit -> orbit.getPVCoordinates()));
  377.         return new CartesianOrbit(interpolated, getFrame(), date, getMu());
  378.     }

  379.     /** Compute shifted position and velocity in elliptic case.
  380.      * @param dt time shift
  381.      * @return shifted position and velocity
  382.      */
  383.     private PVCoordinates shiftPVElliptic(final double dt) {

  384.         // preliminary computation
  385.         final Vector3D pvP   = getPVCoordinates().getPosition();
  386.         final Vector3D pvV   = getPVCoordinates().getVelocity();
  387.         final double r2      = pvP.getNormSq();
  388.         final double r       = FastMath.sqrt(r2);
  389.         final double rV2OnMu = r * pvV.getNormSq() / getMu();
  390.         final double a       = getA();
  391.         final double eSE     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(getMu() * a);
  392.         final double eCE     = rV2OnMu - 1;
  393.         final double e2      = eCE * eCE + eSE * eSE;

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

  398.         // the following equations rely on the specific choice of u explained above,
  399.         // some coefficients that vanish to 0 in this case have already been removed here
  400.         final double ex      = (eCE - e2) * a / r;
  401.         final double ey      = -FastMath.sqrt(1 - e2) * eSE * a / r;
  402.         final double beta    = 1 / (1 + FastMath.sqrt(1 - e2));
  403.         final double thetaE0 = FastMath.atan2(ey + eSE * beta * ex, r / a + ex - eSE * beta * ey);
  404.         final double thetaM0 = thetaE0 - ex * FastMath.sin(thetaE0) + ey * FastMath.cos(thetaE0);

  405.         // compute in-plane shifted eccentric argument
  406.         final double thetaM1 = thetaM0 + getKeplerianMeanMotion() * dt;
  407.         final double thetaE1 = meanToEccentric(thetaM1, ex, ey);
  408.         final double cTE     = FastMath.cos(thetaE1);
  409.         final double sTE     = FastMath.sin(thetaE1);

  410.         // compute shifted in-plane Cartesian coordinates
  411.         final double exey   = ex * ey;
  412.         final double exCeyS = ex * cTE + ey * sTE;
  413.         final double x      = a * ((1 - beta * ey * ey) * cTE + beta * exey * sTE - ex);
  414.         final double y      = a * ((1 - beta * ex * ex) * sTE + beta * exey * cTE - ey);
  415.         final double factor = FastMath.sqrt(getMu() / a) / (1 - exCeyS);
  416.         final double xDot   = factor * (-sTE + beta * ey * exCeyS);
  417.         final double yDot   = factor * ( cTE - beta * ex * exCeyS);

  418.         final Vector3D shiftedP = new Vector3D(x, u, y, v);
  419.         final Vector3D shiftedV = new Vector3D(xDot, u, yDot, v);
  420.         if (hasNonKeplerianAcceleration) {

  421.             // extract non-Keplerian part of the initial acceleration
  422.             final Vector3D nonKeplerianAcceleration = new Vector3D(1, getPVCoordinates().getAcceleration(),
  423.                                                                    getMu() / (r2 * r), pvP);

  424.             // add the quadratic motion due to the non-Keplerian acceleration to the Keplerian motion
  425.             final Vector3D fixedP   = new Vector3D(1, shiftedP,
  426.                                                    0.5 * dt * dt, nonKeplerianAcceleration);
  427.             final double   fixedR2 = fixedP.getNormSq();
  428.             final double   fixedR  = FastMath.sqrt(fixedR2);
  429.             final Vector3D fixedV  = new Vector3D(1, shiftedV,
  430.                                                   dt, nonKeplerianAcceleration);
  431.             final Vector3D fixedA  = new Vector3D(-getMu() / (fixedR2 * fixedR), shiftedP,
  432.                                                   1, nonKeplerianAcceleration);

  433.             return new PVCoordinates(fixedP, fixedV, fixedA);

  434.         } else {
  435.             // don't include acceleration,
  436.             // so the shifted orbit is not considered to have derivatives
  437.             return new PVCoordinates(shiftedP, shiftedV);
  438.         }

  439.     }

  440.     /** Compute shifted position and velocity in hyperbolic case.
  441.      * @param dt time shift
  442.      * @return shifted position and velocity
  443.      */
  444.     private PVCoordinates shiftPVHyperbolic(final double dt) {

  445.         final PVCoordinates pv = getPVCoordinates();
  446.         final Vector3D pvP   = pv.getPosition();
  447.         final Vector3D pvV   = pv.getVelocity();
  448.         final Vector3D pvM   = pv.getMomentum();
  449.         final double r2      = pvP.getNormSq();
  450.         final double r       = FastMath.sqrt(r2);
  451.         final double rV2OnMu = r * pvV.getNormSq() / getMu();
  452.         final double a       = getA();
  453.         final double muA     = getMu() * a;
  454.         final double e       = FastMath.sqrt(1 - Vector3D.dotProduct(pvM, pvM) / muA);
  455.         final double sqrt    = FastMath.sqrt((e + 1) / (e - 1));

  456.         // compute mean anomaly
  457.         final double eSH     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(-muA);
  458.         final double eCH     = rV2OnMu - 1;
  459.         final double H0      = FastMath.log((eCH + eSH) / (eCH - eSH)) / 2;
  460.         final double M0      = e * FastMath.sinh(H0) - H0;

  461.         // find canonical 2D frame with p pointing to perigee
  462.         final double v0      = 2 * FastMath.atan(sqrt * FastMath.tanh(H0 / 2));
  463.         final Vector3D p     = new Rotation(pvM, v0, RotationConvention.FRAME_TRANSFORM).applyTo(pvP).normalize();
  464.         final Vector3D q     = Vector3D.crossProduct(pvM, p).normalize();

  465.         // compute shifted eccentric anomaly
  466.         final double M1      = M0 + getKeplerianMeanMotion() * dt;
  467.         final double H1      = meanToHyperbolicEccentric(M1, e);

  468.         // compute shifted in-plane Cartesian coordinates
  469.         final double cH     = FastMath.cosh(H1);
  470.         final double sH     = FastMath.sinh(H1);
  471.         final double sE2m1  = FastMath.sqrt((e - 1) * (e + 1));

  472.         // coordinates of position and velocity in the orbital plane
  473.         final double x      = a * (cH - e);
  474.         final double y      = -a * sE2m1 * sH;
  475.         final double factor = FastMath.sqrt(getMu() / -a) / (e * cH - 1);
  476.         final double xDot   = -factor * sH;
  477.         final double yDot   =  factor * sE2m1 * cH;

  478.         final Vector3D shiftedP = new Vector3D(x, p, y, q);
  479.         final Vector3D shiftedV = new Vector3D(xDot, p, yDot, q);
  480.         if (hasNonKeplerianAcceleration) {

  481.             // extract non-Keplerian part of the initial acceleration
  482.             final Vector3D nonKeplerianAcceleration = new Vector3D(1, getPVCoordinates().getAcceleration(),
  483.                                                                    getMu() / (r2 * r), pvP);

  484.             // add the quadratic motion due to the non-Keplerian acceleration to the Keplerian motion
  485.             final Vector3D fixedP   = new Vector3D(1, shiftedP,
  486.                                                    0.5 * dt * dt, nonKeplerianAcceleration);
  487.             final double   fixedR2 = fixedP.getNormSq();
  488.             final double   fixedR  = FastMath.sqrt(fixedR2);
  489.             final Vector3D fixedV  = new Vector3D(1, shiftedV,
  490.                                                   dt, nonKeplerianAcceleration);
  491.             final Vector3D fixedA  = new Vector3D(-getMu() / (fixedR2 * fixedR), shiftedP,
  492.                                                   1, nonKeplerianAcceleration);

  493.             return new PVCoordinates(fixedP, fixedV, fixedA);

  494.         } else {
  495.             // don't include acceleration,
  496.             // so the shifted orbit is not considered to have derivatives
  497.             return new PVCoordinates(shiftedP, shiftedV);
  498.         }

  499.     }

  500.     /** Computes the eccentric in-plane argument from the mean in-plane argument.
  501.      * @param thetaM = mean in-plane argument (rad)
  502.      * @param ex first component of eccentricity vector
  503.      * @param ey second component of eccentricity vector
  504.      * @return the eccentric in-plane argument.
  505.      */
  506.     private double meanToEccentric(final double thetaM, final double ex, final double ey) {
  507.         // Generalization of Kepler equation to in-plane parameters
  508.         // with thetaE = eta + E and
  509.         //      thetaM = eta + M = thetaE - ex.sin(thetaE) + ey.cos(thetaE)
  510.         // and eta being counted from an arbitrary reference in the orbital plane
  511.         double thetaE        = thetaM;
  512.         double thetaEMthetaM = 0.0;
  513.         int    iter          = 0;
  514.         do {
  515.             final double cosThetaE = FastMath.cos(thetaE);
  516.             final double sinThetaE = FastMath.sin(thetaE);

  517.             final double f2 = ex * sinThetaE - ey * cosThetaE;
  518.             final double f1 = 1.0 - ex * cosThetaE - ey * sinThetaE;
  519.             final double f0 = thetaEMthetaM - f2;

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

  522.             thetaEMthetaM -= shift;
  523.             thetaE         = thetaM + thetaEMthetaM;

  524.             if (FastMath.abs(shift) <= 1.0e-12) {
  525.                 return thetaE;
  526.             }

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

  528.         throw new MathIllegalStateException(LocalizedCoreFormats.CONVERGENCE_FAILED);

  529.     }

  530.     /** Computes the hyperbolic eccentric anomaly from the mean anomaly.
  531.      * <p>
  532.      * The algorithm used here for solving hyperbolic Kepler equation is
  533.      * Danby's iterative method (3rd order) with Vallado's initial guess.
  534.      * </p>
  535.      * @param M mean anomaly (rad)
  536.      * @param ecc eccentricity
  537.      * @return the hyperbolic eccentric anomaly
  538.      */
  539.     private double meanToHyperbolicEccentric(final double M, final double ecc) {

  540.         // Resolution of hyperbolic Kepler equation for Keplerian parameters

  541.         // Initial guess
  542.         double H;
  543.         if (ecc < 1.6) {
  544.             if ((-FastMath.PI < M && M < 0.) || M > FastMath.PI) {
  545.                 H = M - ecc;
  546.             } else {
  547.                 H = M + ecc;
  548.             }
  549.         } else {
  550.             if (ecc < 3.6 && FastMath.abs(M) > FastMath.PI) {
  551.                 H = M - FastMath.copySign(ecc, M);
  552.             } else {
  553.                 H = M / (ecc - 1.);
  554.             }
  555.         }

  556.         // Iterative computation
  557.         int iter = 0;
  558.         do {
  559.             final double f3  = ecc * FastMath.cosh(H);
  560.             final double f2  = ecc * FastMath.sinh(H);
  561.             final double f1  = f3 - 1.;
  562.             final double f0  = f2 - H - M;
  563.             final double f12 = 2. * f1;
  564.             final double d   = f0 / f12;
  565.             final double fdf = f1 - d * f2;
  566.             final double ds  = f0 / fdf;

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

  568.             H -= shift;

  569.             if (FastMath.abs(shift) <= 1.0e-12) {
  570.                 return H;
  571.             }

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

  573.         throw new MathIllegalStateException(OrekitMessages.UNABLE_TO_COMPUTE_HYPERBOLIC_ECCENTRIC_ANOMALY,
  574.                                             iter);
  575.     }

  576.     /** Create a 6x6 identity matrix.
  577.      * @return 6x6 identity matrix
  578.      */
  579.     private double[][] create6x6Identity() {
  580.         // this is the fastest way to set the 6x6 identity matrix
  581.         final double[][] identity = new double[6][6];
  582.         for (int i = 0; i < 6; i++) {
  583.             identity[i][i] = 1.0;
  584.         }
  585.         return identity;
  586.     }

  587.     @Override
  588.     protected double[][] computeJacobianMeanWrtCartesian() {
  589.         return create6x6Identity();
  590.     }

  591.     @Override
  592.     protected double[][] computeJacobianEccentricWrtCartesian() {
  593.         return create6x6Identity();
  594.     }

  595.     @Override
  596.     protected double[][] computeJacobianTrueWrtCartesian() {
  597.         return create6x6Identity();
  598.     }

  599.     /** {@inheritDoc} */
  600.     public void addKeplerContribution(final PositionAngle type, final double gm,
  601.                                       final double[] pDot) {

  602.         final PVCoordinates pv = getPVCoordinates();

  603.         // position derivative is velocity
  604.         final Vector3D velocity = pv.getVelocity();
  605.         pDot[0] += velocity.getX();
  606.         pDot[1] += velocity.getY();
  607.         pDot[2] += velocity.getZ();

  608.         // velocity derivative is Newtonian acceleration
  609.         final Vector3D position = pv.getPosition();
  610.         final double r2         = position.getNormSq();
  611.         final double coeff      = -gm / (r2 * FastMath.sqrt(r2));
  612.         pDot[3] += coeff * position.getX();
  613.         pDot[4] += coeff * position.getY();
  614.         pDot[5] += coeff * position.getZ();

  615.     }

  616.     /**  Returns a string representation of this Orbit object.
  617.      * @return a string representation of this object
  618.      */
  619.     public String toString() {
  620.         return "Cartesian parameters: " + getPVCoordinates().toString();
  621.     }

  622.     /** Replace the instance with a data transfer object for serialization.
  623.      * <p>
  624.      * This intermediate class serializes all needed parameters,
  625.      * including position-velocity which are <em>not</em> serialized by parent
  626.      * {@link Orbit} class.
  627.      * </p>
  628.      * @return data transfer object that will be serialized
  629.      */
  630.     private Object writeReplace() {
  631.         return new DTO(this);
  632.     }

  633.     /** Internal class used only for serialization. */
  634.     private static class DTO implements Serializable {

  635.         /** Serializable UID. */
  636.         private static final long serialVersionUID = 20170414L;

  637.         /** Double values. */
  638.         private double[] d;

  639.         /** Frame in which are defined the orbital parameters. */
  640.         private final Frame frame;

  641.         /** Simple constructor.
  642.          * @param orbit instance to serialize
  643.          */
  644.         private DTO(final CartesianOrbit orbit) {

  645.             final TimeStampedPVCoordinates pv = orbit.getPVCoordinates();

  646.             // decompose date
  647.             final double epoch  = FastMath.floor(pv.getDate().durationFrom(AbsoluteDate.J2000_EPOCH));
  648.             final double offset = pv.getDate().durationFrom(AbsoluteDate.J2000_EPOCH.shiftedBy(epoch));

  649.             if (orbit.hasDerivatives()) {
  650.                 this.d = new double[] {
  651.                     epoch, offset, orbit.getMu(),
  652.                     pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  653.                     pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  654.                     pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ()
  655.                 };
  656.             } else {
  657.                 this.d = new double[] {
  658.                     epoch, offset, orbit.getMu(),
  659.                     pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  660.                     pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ()
  661.                 };
  662.             }

  663.             this.frame = orbit.getFrame();

  664.         }

  665.         /** Replace the deserialized data transfer object with a {@link CartesianOrbit}.
  666.          * @return replacement {@link CartesianOrbit}
  667.          */
  668.         private Object readResolve() {
  669.             if (d.length >= 12) {
  670.                 // we have derivatives
  671.                 return new CartesianOrbit(new TimeStampedPVCoordinates(AbsoluteDate.J2000_EPOCH.shiftedBy(d[0]).shiftedBy(d[1]),
  672.                                                                        new Vector3D(d[3], d[ 4], d[ 5]),
  673.                                                                        new Vector3D(d[6], d[ 7], d[ 8]),
  674.                                                                        new Vector3D(d[9], d[10], d[11])),
  675.                                           frame, d[2]);
  676.             } else {
  677.                 // we don't have derivatives
  678.                 return new CartesianOrbit(new TimeStampedPVCoordinates(AbsoluteDate.J2000_EPOCH.shiftedBy(d[0]).shiftedBy(d[1]),
  679.                                                                        new Vector3D(d[3], d[ 4], d[ 5]),
  680.                                                                        new Vector3D(d[6], d[ 7], d[ 8])),
  681.                                           frame, d[2]);
  682.             }
  683.         }

  684.     }

  685. }