CartesianOrbit.java

  1. /* Copyright 2002-2020 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.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.annotation.DefaultDataContext;
  30. import org.orekit.data.DataContext;
  31. import org.orekit.errors.OrekitMessages;
  32. import org.orekit.frames.Frame;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.utils.CartesianDerivativesFilter;
  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.     /** Factory for first time derivatives. */
  73.     private static final DSFactory FACTORY = new DSFactory(1, 1);

  74.     /** Indicator for non-Keplerian derivatives. */
  75.     private final transient boolean hasNonKeplerianAcceleration;

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

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

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

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

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

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

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

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

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

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

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

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

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

  227.     /** {@inheritDoc} */
  228.     public double getIDot() {
  229.         if (hasDerivatives()) {
  230.             final FieldVector3D<DerivativeStructure> momentum =
  231.                             FieldVector3D.crossProduct(getPositionDS(), getVelocityDS());
  232.             final DerivativeStructure i = FieldVector3D.angle(Vector3D.PLUS_K, momentum);
  233.             return i.getPartialDerivative(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 FieldVector3D<DerivativeStructure> w =
  271.                             FieldVector3D.crossProduct(getPositionDS(), getVelocityDS()).normalize();
  272.             // Check for equatorial retrograde orbit
  273.             final double x = w.getX().getValue();
  274.             final double y = w.getY().getValue();
  275.             final double z = w.getZ().getValue();
  276.             if (((x * x + y * y) == 0) && z < 0) {
  277.                 return Double.NaN;
  278.             }
  279.             final DerivativeStructure hx = w.getY().negate().divide(w.getZ().add(1));
  280.             return hx.getPartialDerivative(1);
  281.         } else {
  282.             return Double.NaN;
  283.         }
  284.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  441.     }

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

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

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

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

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

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

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

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

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

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

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

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

  501.     }

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

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

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

  524.             thetaEMthetaM -= shift;
  525.             thetaE         = thetaM + thetaEMthetaM;

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

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

  530.         throw new MathIllegalStateException(LocalizedCoreFormats.CONVERGENCE_FAILED);

  531.     }

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

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

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

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

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

  570.             H -= shift;

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

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

  575.         throw new MathIllegalStateException(OrekitMessages.UNABLE_TO_COMPUTE_HYPERBOLIC_ECCENTRIC_ANOMALY,
  576.                                             iter);
  577.     }

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

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

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

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

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

  604.         final PVCoordinates pv = getPVCoordinates();

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

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

  617.     }

  618.     /**  Returns a string representation of this Orbit object.
  619.      * @return a string representation of this object
  620.      */
  621.     public String toString() {
  622.         // use only the six defining elements, like the other Orbit.toString() methods
  623.         final String comma = ", ";
  624.         final PVCoordinates pv = getPVCoordinates();
  625.         final Vector3D p = pv.getPosition();
  626.         final Vector3D v = pv.getVelocity();
  627.         return "Cartesian parameters: {P(" +
  628.                 p.getX() + comma +
  629.                 p.getY() + comma +
  630.                 p.getZ() + "), V(" +
  631.                 v.getX() + comma +
  632.                 v.getY() + comma +
  633.                 v.getZ() + ")}";
  634.     }

  635.     /** Replace the instance with a data transfer object for serialization.
  636.      * <p>
  637.      * This intermediate class serializes all needed parameters,
  638.      * including position-velocity which are <em>not</em> serialized by parent
  639.      * {@link Orbit} class.
  640.      * </p>
  641.      * @return data transfer object that will be serialized
  642.      */
  643.     @DefaultDataContext
  644.     private Object writeReplace() {
  645.         return new DTO(this);
  646.     }

  647.     /** Internal class used only for serialization. */
  648.     @DefaultDataContext
  649.     private static class DTO implements Serializable {

  650.         /** Serializable UID. */
  651.         private static final long serialVersionUID = 20170414L;

  652.         /** Double values. */
  653.         private double[] d;

  654.         /** Frame in which are defined the orbital parameters. */
  655.         private final Frame frame;

  656.         /** Simple constructor.
  657.          * @param orbit instance to serialize
  658.          */
  659.         private DTO(final CartesianOrbit orbit) {

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

  661.             // decompose date
  662.             final AbsoluteDate j2000Epoch =
  663.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  664.             final double epoch  = FastMath.floor(pv.getDate().durationFrom(j2000Epoch));
  665.             final double offset = pv.getDate().durationFrom(j2000Epoch.shiftedBy(epoch));

  666.             if (orbit.hasDerivatives()) {
  667.                 this.d = new double[] {
  668.                     epoch, offset, orbit.getMu(),
  669.                     pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  670.                     pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  671.                     pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ()
  672.                 };
  673.             } else {
  674.                 this.d = new double[] {
  675.                     epoch, offset, orbit.getMu(),
  676.                     pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  677.                     pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ()
  678.                 };
  679.             }

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

  681.         }

  682.         /** Replace the deserialized data transfer object with a {@link CartesianOrbit}.
  683.          * @return replacement {@link CartesianOrbit}
  684.          */
  685.         private Object readResolve() {
  686.             final AbsoluteDate j2000Epoch =
  687.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  688.             if (d.length >= 12) {
  689.                 // we have derivatives
  690.                 return new CartesianOrbit(new TimeStampedPVCoordinates(j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  691.                                                                        new Vector3D(d[3], d[ 4], d[ 5]),
  692.                                                                        new Vector3D(d[6], d[ 7], d[ 8]),
  693.                                                                        new Vector3D(d[9], d[10], d[11])),
  694.                                           frame, d[2]);
  695.             } else {
  696.                 // we don't have derivatives
  697.                 return new CartesianOrbit(new TimeStampedPVCoordinates(j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  698.                                                                        new Vector3D(d[3], d[ 4], d[ 5]),
  699.                                                                        new Vector3D(d[6], d[ 7], d[ 8])),
  700.                                           frame, d[2]);
  701.             }
  702.         }

  703.     }

  704. }