CartesianOrbit.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.orbits;

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

  20. import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
  21. import org.hipparchus.exception.LocalizedCoreFormats;
  22. import org.hipparchus.exception.MathIllegalStateException;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.geometry.euclidean.threed.Rotation;
  25. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.SinCos;
  29. import org.orekit.annotation.DefaultDataContext;
  30. import org.orekit.data.DataContext;
  31. import org.orekit.frames.Frame;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.CartesianDerivativesFilter;
  34. import org.orekit.utils.FieldPVCoordinates;
  35. import org.orekit.utils.PVCoordinates;
  36. import org.orekit.utils.TimeStampedPVCoordinates;


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

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

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

  70.     /** Serializable UID. */
  71.     private static final long serialVersionUID = 20170414L;

  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 the position/velocity with derivatives.
  151.      * @return position/velocity with derivatives
  152.      * @since 10.2
  153.      */
  154.     private FieldPVCoordinates<UnivariateDerivative2> getPVDerivatives() {
  155.         // PVA coordinates
  156.         final PVCoordinates pva = getPVCoordinates();
  157.         final Vector3D      p   = pva.getPosition();
  158.         final Vector3D      v   = pva.getVelocity();
  159.         final Vector3D      a   = pva.getAcceleration();
  160.         // Field coordinates
  161.         final FieldVector3D<UnivariateDerivative2> pG = new FieldVector3D<>(new UnivariateDerivative2(p.getX(), v.getX(), a.getX()),
  162.                                                                new UnivariateDerivative2(p.getY(), v.getY(), a.getY()),
  163.                                                                new UnivariateDerivative2(p.getZ(), v.getZ(), a.getZ()));
  164.         final FieldVector3D<UnivariateDerivative2> vG = new FieldVector3D<>(new UnivariateDerivative2(v.getX(), a.getX(), 0.0),
  165.                                                                new UnivariateDerivative2(v.getY(), a.getY(), 0.0),
  166.                                                                new UnivariateDerivative2(v.getZ(), a.getZ(), 0.0));
  167.         return new FieldPVCoordinates<>(pG, vG);
  168.     }

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

  175.     /** {@inheritDoc} */
  176.     public double getADot() {
  177.         if (hasDerivatives()) {
  178.             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
  179.             final UnivariateDerivative2 r  = pv.getPosition().getNorm();
  180.             final UnivariateDerivative2 V2 = pv.getVelocity().getNormSq();
  181.             final UnivariateDerivative2 a  = r.divide(r.multiply(V2).divide(getMu()).subtract(2).negate());
  182.             return a.getDerivative(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 FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
  208.             final FieldVector3D<UnivariateDerivative2> pvP   = pv.getPosition();
  209.             final FieldVector3D<UnivariateDerivative2> pvV   = pv.getVelocity();
  210.             final UnivariateDerivative2 r       = pvP.getNorm();
  211.             final UnivariateDerivative2 V2      = pvV.getNormSq();
  212.             final UnivariateDerivative2 rV2OnMu = r.multiply(V2).divide(getMu());
  213.             final UnivariateDerivative2 a       = r.divide(rV2OnMu.negate().add(2));
  214.             final UnivariateDerivative2 eSE     = FieldVector3D.dotProduct(pvP, pvV).divide(a.multiply(getMu()).sqrt());
  215.             final UnivariateDerivative2 eCE     = rV2OnMu.subtract(1);
  216.             final UnivariateDerivative2 e       = eCE.multiply(eCE).add(eSE.multiply(eSE)).sqrt();
  217.             return e.getDerivative(1);
  218.         } else {
  219.             return Double.NaN;
  220.         }
  221.     }

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

  226.     /** {@inheritDoc} */
  227.     public double getIDot() {
  228.         if (hasDerivatives()) {
  229.             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
  230.             final FieldVector3D<UnivariateDerivative2> momentum =
  231.                             FieldVector3D.crossProduct(pv.getPosition(), pv.getVelocity());
  232.             final UnivariateDerivative2 i = FieldVector3D.angle(Vector3D.PLUS_K, momentum);
  233.             return i.getDerivative(1);
  234.         } else {
  235.             return Double.NaN;
  236.         }
  237.     }

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

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

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

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

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

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

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

  295.     /** {@inheritDoc} */
  296.     public double getHyDot() {
  297.         if (hasDerivatives()) {
  298.             final FieldPVCoordinates<UnivariateDerivative2> pv = getPVDerivatives();
  299.             final FieldVector3D<UnivariateDerivative2> w =
  300.                             FieldVector3D.crossProduct(pv.getPosition(), pv.getVelocity()).normalize();
  301.             // Check for equatorial retrograde orbit
  302.             final double x = w.getX().getValue();
  303.             final double y = w.getY().getValue();
  304.             final double z = w.getZ().getValue();
  305.             if ((x * x + y * y) == 0 && z < 0) {
  306.                 return Double.NaN;
  307.             }
  308.             final UnivariateDerivative2 hy = w.getX().divide(w.getZ().add(1));
  309.             return hy.getDerivative(1);
  310.         } else {
  311.             return Double.NaN;
  312.         }
  313.     }

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

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

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

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

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

  339.     /** {@inheritDoc} */
  340.     public double getLMDot() {
  341.         initEquinoctial();
  342.         return equinoctial.getLMDot();
  343.     }

  344.     /** {@inheritDoc} */
  345.     public boolean hasDerivatives() {
  346.         return hasNonKeplerianAcceleration;
  347.     }

  348.     /** {@inheritDoc} */
  349.     protected TimeStampedPVCoordinates initPVCoordinates() {
  350.         // nothing to do here, as the canonical elements are already the Cartesian ones
  351.         return getPVCoordinates();
  352.     }

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

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

  383.     /** Compute shifted position and velocity in elliptic case.
  384.      * @param dt time shift
  385.      * @return shifted position and velocity
  386.      */
  387.     private PVCoordinates shiftPVElliptic(final double dt) {

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

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

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

  410.         // compute in-plane shifted eccentric argument
  411.         final double thetaM1 = thetaM0 + getKeplerianMeanMotion() * dt;
  412.         final double thetaE1 = meanToEccentric(thetaM1, ex, ey);
  413.         final SinCos scTE    = FastMath.sinCos(thetaE1);
  414.         final double cTE     = scTE.cos();
  415.         final double sTE     = scTE.sin();

  416.         // compute shifted in-plane Cartesian coordinates
  417.         final double exey   = ex * ey;
  418.         final double exCeyS = ex * cTE + ey * sTE;
  419.         final double x      = a * ((1 - beta * ey * ey) * cTE + beta * exey * sTE - ex);
  420.         final double y      = a * ((1 - beta * ex * ex) * sTE + beta * exey * cTE - ey);
  421.         final double factor = FastMath.sqrt(getMu() / a) / (1 - exCeyS);
  422.         final double xDot   = factor * (-sTE + beta * ey * exCeyS);
  423.         final double yDot   = factor * ( cTE - beta * ex * exCeyS);

  424.         final Vector3D shiftedP = new Vector3D(x, u, y, v);
  425.         final Vector3D shiftedV = new Vector3D(xDot, u, yDot, v);
  426.         if (hasNonKeplerianAcceleration) {

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

  430.             // add the quadratic motion due to the non-Keplerian acceleration to the Keplerian motion
  431.             final Vector3D fixedP   = new Vector3D(1, shiftedP,
  432.                                                    0.5 * dt * dt, nonKeplerianAcceleration);
  433.             final double   fixedR2 = fixedP.getNormSq();
  434.             final double   fixedR  = FastMath.sqrt(fixedR2);
  435.             final Vector3D fixedV  = new Vector3D(1, shiftedV,
  436.                                                   dt, nonKeplerianAcceleration);
  437.             final Vector3D fixedA  = new Vector3D(-getMu() / (fixedR2 * fixedR), shiftedP,
  438.                                                   1, nonKeplerianAcceleration);

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

  440.         } else {
  441.             // don't include acceleration,
  442.             // so the shifted orbit is not considered to have derivatives
  443.             return new PVCoordinates(shiftedP, shiftedV);
  444.         }

  445.     }

  446.     /** Compute shifted position and velocity in hyperbolic case.
  447.      * @param dt time shift
  448.      * @return shifted position and velocity
  449.      */
  450.     private PVCoordinates shiftPVHyperbolic(final double dt) {

  451.         final PVCoordinates pv = getPVCoordinates();
  452.         final Vector3D pvP   = pv.getPosition();
  453.         final Vector3D pvV   = pv.getVelocity();
  454.         final Vector3D pvM   = pv.getMomentum();
  455.         final double r2      = pvP.getNormSq();
  456.         final double r       = FastMath.sqrt(r2);
  457.         final double rV2OnMu = r * pvV.getNormSq() / getMu();
  458.         final double a       = getA();
  459.         final double muA     = getMu() * a;
  460.         final double e       = FastMath.sqrt(1 - Vector3D.dotProduct(pvM, pvM) / muA);
  461.         final double sqrt    = FastMath.sqrt((e + 1) / (e - 1));

  462.         // compute mean anomaly
  463.         final double eSH     = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(-muA);
  464.         final double eCH     = rV2OnMu - 1;
  465.         final double H0      = FastMath.log((eCH + eSH) / (eCH - eSH)) / 2;
  466.         final double M0      = e * FastMath.sinh(H0) - H0;

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

  471.         // compute shifted eccentric anomaly
  472.         final double M1      = M0 + getKeplerianMeanMotion() * dt;
  473.         final double H1      = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e, M1);

  474.         // compute shifted in-plane Cartesian coordinates
  475.         final double cH     = FastMath.cosh(H1);
  476.         final double sH     = FastMath.sinh(H1);
  477.         final double sE2m1  = FastMath.sqrt((e - 1) * (e + 1));

  478.         // coordinates of position and velocity in the orbital plane
  479.         final double x      = a * (cH - e);
  480.         final double y      = -a * sE2m1 * sH;
  481.         final double factor = FastMath.sqrt(getMu() / -a) / (e * cH - 1);
  482.         final double xDot   = -factor * sH;
  483.         final double yDot   =  factor * sE2m1 * cH;

  484.         final Vector3D shiftedP = new Vector3D(x, p, y, q);
  485.         final Vector3D shiftedV = new Vector3D(xDot, p, yDot, q);
  486.         if (hasNonKeplerianAcceleration) {

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

  490.             // add the quadratic motion due to the non-Keplerian acceleration to the Keplerian motion
  491.             final Vector3D fixedP   = new Vector3D(1, shiftedP,
  492.                                                    0.5 * dt * dt, nonKeplerianAcceleration);
  493.             final double   fixedR2 = fixedP.getNormSq();
  494.             final double   fixedR  = FastMath.sqrt(fixedR2);
  495.             final Vector3D fixedV  = new Vector3D(1, shiftedV,
  496.                                                   dt, nonKeplerianAcceleration);
  497.             final Vector3D fixedA  = new Vector3D(-getMu() / (fixedR2 * fixedR), shiftedP,
  498.                                                   1, nonKeplerianAcceleration);

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

  500.         } else {
  501.             // don't include acceleration,
  502.             // so the shifted orbit is not considered to have derivatives
  503.             return new PVCoordinates(shiftedP, shiftedV);
  504.         }

  505.     }

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

  522.             final double f2 = ex * scThetaE.sin() - ey * scThetaE.cos();
  523.             final double f1 = 1.0 - ex * scThetaE.cos() - ey * scThetaE.sin();
  524.             final double f0 = thetaEMthetaM - f2;

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

  527.             thetaEMthetaM -= shift;
  528.             thetaE         = thetaM + thetaEMthetaM;

  529.             if (FastMath.abs(shift) <= 1.0e-12) {
  530.                 return thetaE;
  531.             }

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

  533.         throw new MathIllegalStateException(LocalizedCoreFormats.CONVERGENCE_FAILED);

  534.     }

  535.     /** Create a 6x6 identity matrix.
  536.      * @return 6x6 identity matrix
  537.      */
  538.     private double[][] create6x6Identity() {
  539.         // this is the fastest way to set the 6x6 identity matrix
  540.         final double[][] identity = new double[6][6];
  541.         for (int i = 0; i < 6; i++) {
  542.             identity[i][i] = 1.0;
  543.         }
  544.         return identity;
  545.     }

  546.     @Override
  547.     protected double[][] computeJacobianMeanWrtCartesian() {
  548.         return create6x6Identity();
  549.     }

  550.     @Override
  551.     protected double[][] computeJacobianEccentricWrtCartesian() {
  552.         return create6x6Identity();
  553.     }

  554.     @Override
  555.     protected double[][] computeJacobianTrueWrtCartesian() {
  556.         return create6x6Identity();
  557.     }

  558.     /** {@inheritDoc} */
  559.     public void addKeplerContribution(final PositionAngle type, final double gm,
  560.                                       final double[] pDot) {

  561.         final PVCoordinates pv = getPVCoordinates();

  562.         // position derivative is velocity
  563.         final Vector3D velocity = pv.getVelocity();
  564.         pDot[0] += velocity.getX();
  565.         pDot[1] += velocity.getY();
  566.         pDot[2] += velocity.getZ();

  567.         // velocity derivative is Newtonian acceleration
  568.         final Vector3D position = pv.getPosition();
  569.         final double r2         = position.getNormSq();
  570.         final double coeff      = -gm / (r2 * FastMath.sqrt(r2));
  571.         pDot[3] += coeff * position.getX();
  572.         pDot[4] += coeff * position.getY();
  573.         pDot[5] += coeff * position.getZ();

  574.     }

  575.     /**  Returns a string representation of this Orbit object.
  576.      * @return a string representation of this object
  577.      */
  578.     public String toString() {
  579.         // use only the six defining elements, like the other Orbit.toString() methods
  580.         final String comma = ", ";
  581.         final PVCoordinates pv = getPVCoordinates();
  582.         final Vector3D p = pv.getPosition();
  583.         final Vector3D v = pv.getVelocity();
  584.         return "Cartesian parameters: {P(" +
  585.                 p.getX() + comma +
  586.                 p.getY() + comma +
  587.                 p.getZ() + "), V(" +
  588.                 v.getX() + comma +
  589.                 v.getY() + comma +
  590.                 v.getZ() + ")}";
  591.     }

  592.     /** Replace the instance with a data transfer object for serialization.
  593.      * <p>
  594.      * This intermediate class serializes all needed parameters,
  595.      * including position-velocity which are <em>not</em> serialized by parent
  596.      * {@link Orbit} class.
  597.      * </p>
  598.      * @return data transfer object that will be serialized
  599.      */
  600.     @DefaultDataContext
  601.     private Object writeReplace() {
  602.         return new DTO(this);
  603.     }

  604.     /** Internal class used only for serialization. */
  605.     @DefaultDataContext
  606.     private static class DTO implements Serializable {

  607.         /** Serializable UID. */
  608.         private static final long serialVersionUID = 20170414L;

  609.         /** Double values. */
  610.         private double[] d;

  611.         /** Frame in which are defined the orbital parameters. */
  612.         private final Frame frame;

  613.         /** Simple constructor.
  614.          * @param orbit instance to serialize
  615.          */
  616.         private DTO(final CartesianOrbit orbit) {

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

  618.             // decompose date
  619.             final AbsoluteDate j2000Epoch =
  620.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  621.             final double epoch  = FastMath.floor(pv.getDate().durationFrom(j2000Epoch));
  622.             final double offset = pv.getDate().durationFrom(j2000Epoch.shiftedBy(epoch));

  623.             if (orbit.hasDerivatives()) {
  624.                 this.d = new double[] {
  625.                     epoch, offset, orbit.getMu(),
  626.                     pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  627.                     pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  628.                     pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ()
  629.                 };
  630.             } else {
  631.                 this.d = new double[] {
  632.                     epoch, offset, orbit.getMu(),
  633.                     pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  634.                     pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ()
  635.                 };
  636.             }

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

  638.         }

  639.         /** Replace the deserialized data transfer object with a {@link CartesianOrbit}.
  640.          * @return replacement {@link CartesianOrbit}
  641.          */
  642.         private Object readResolve() {
  643.             final AbsoluteDate j2000Epoch =
  644.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  645.             if (d.length >= 12) {
  646.                 // we have derivatives
  647.                 return new CartesianOrbit(new TimeStampedPVCoordinates(j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  648.                                                                        new Vector3D(d[3], d[ 4], d[ 5]),
  649.                                                                        new Vector3D(d[6], d[ 7], d[ 8]),
  650.                                                                        new Vector3D(d[9], d[10], d[11])),
  651.                                           frame, d[2]);
  652.             } else {
  653.                 // we don't have derivatives
  654.                 return new CartesianOrbit(new TimeStampedPVCoordinates(j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  655.                                                                        new Vector3D(d[3], d[ 4], d[ 5]),
  656.                                                                        new Vector3D(d[6], d[ 7], d[ 8])),
  657.                                           frame, d[2]);
  658.             }
  659.         }

  660.     }

  661. }