FieldKeplerianOrbit.java

  1. /* Copyright 2002-2024 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.lang.reflect.Array;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.Field;
  21. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
  22. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  23. import org.hipparchus.util.FastMath;
  24. import org.hipparchus.util.FieldSinCos;
  25. import org.hipparchus.util.MathArrays;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitIllegalArgumentException;
  28. import org.orekit.errors.OrekitInternalError;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.frames.Frame;
  31. import org.orekit.time.FieldAbsoluteDate;
  32. import org.orekit.utils.FieldPVCoordinates;
  33. import org.orekit.utils.TimeStampedFieldPVCoordinates;


  34. /**
  35.  * This class handles traditional Keplerian orbital parameters.

  36.  * <p>
  37.  * The parameters used internally are the classical Keplerian elements:
  38.  *   <pre>
  39.  *     a
  40.  *     e
  41.  *     i
  42.  *     ω
  43.  *     Ω
  44.  *     v
  45.  *   </pre>
  46.  * where ω stands for the Perigee Argument, Ω stands for the
  47.  * Right Ascension of the Ascending Node and v stands for the true anomaly.
  48.  * <p>
  49.  * This class supports hyperbolic orbits, using the convention that semi major
  50.  * axis is negative for such orbits (and of course eccentricity is greater than 1).
  51.  * </p>
  52.  * <p>
  53.  * When orbit is either equatorial or circular, some Keplerian elements
  54.  * (more precisely ω and Ω) become ambiguous so this class should not
  55.  * be used for such orbits. For this reason, {@link EquinoctialOrbit equinoctial
  56.  * orbits} is the recommended way to represent orbits.
  57.  * </p>

  58.  * <p>
  59.  * The instance <code>KeplerianOrbit</code> is guaranteed to be immutable.
  60.  * </p>
  61.  * @see     Orbit
  62.  * @see    CircularOrbit
  63.  * @see    CartesianOrbit
  64.  * @see    EquinoctialOrbit
  65.  * @author Luc Maisonobe
  66.  * @author Guylaine Prat
  67.  * @author Fabien Maussion
  68.  * @author V&eacute;ronique Pommier-Maurussane
  69.  * @author Andrea Antolino
  70.  * @since 9.0
  71.  * @param <T> type of the field elements
  72.  */
  73. public class FieldKeplerianOrbit<T extends CalculusFieldElement<T>> extends FieldOrbit<T>
  74.         implements PositionAngleBased {

  75.     /** Name of the eccentricity parameter. */
  76.     private static final String ECCENTRICITY = "eccentricity";

  77.     /** Semi-major axis (m). */
  78.     private final T a;

  79.     /** Eccentricity. */
  80.     private final T e;

  81.     /** Inclination (rad). */
  82.     private final T i;

  83.     /** Perigee Argument (rad). */
  84.     private final T pa;

  85.     /** Right Ascension of Ascending Node (rad). */
  86.     private final T raan;

  87.     /** Cached anomaly (rad). */
  88.     private final T cachedAnomaly;

  89.     /** Semi-major axis derivative (m/s). */
  90.     private final T aDot;

  91.     /** Eccentricity derivative. */
  92.     private final T eDot;

  93.     /** Inclination derivative (rad/s). */
  94.     private final T iDot;

  95.     /** Perigee Argument derivative (rad/s). */
  96.     private final T paDot;

  97.     /** Right Ascension of Ascending Node derivative (rad/s). */
  98.     private final T raanDot;

  99.     /** Derivative of cached anomaly (rad/s). */
  100.     private final T cachedAnomalyDot;

  101.     /** Cached type of position angle. */
  102.     private final PositionAngleType cachedPositionAngleType;

  103.     /** Partial Cartesian coordinates (position and velocity are valid, acceleration may be missing). */
  104.     private FieldPVCoordinates<T> partialPV;

  105.     /** PThird Canonical Vector. */
  106.     private final FieldVector3D<T> PLUS_K;

  107.     /** Creates a new instance.
  108.      * @param a  semi-major axis (m), negative for hyperbolic orbits
  109.      * @param e eccentricity (positive or equal to 0)
  110.      * @param i inclination (rad)
  111.      * @param pa perigee argument (ω, rad)
  112.      * @param raan right ascension of ascending node (Ω, rad)
  113.      * @param anomaly mean, eccentric or true anomaly (rad)
  114.      * @param type type of anomaly
  115.      * @param cachedPositionAngleType type of cached anomaly
  116.      * @param frame the frame in which the parameters are defined
  117.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  118.      * @param date date of the orbital parameters
  119.      * @param mu central attraction coefficient (m³/s²)
  120.      * @exception IllegalArgumentException if frame is not a {@link
  121.      * Frame#isPseudoInertial pseudo-inertial frame} or a and e don't match for hyperbolic orbits,
  122.      * or v is out of range for hyperbolic orbits
  123.      * @since 12.1
  124.      */
  125.     public FieldKeplerianOrbit(final T a, final T e, final T i,
  126.                                final T pa, final T raan,
  127.                                final T anomaly, final PositionAngleType type,
  128.                                final PositionAngleType cachedPositionAngleType,
  129.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  130.         throws IllegalArgumentException {
  131.         this(a, e, i, pa, raan, anomaly,
  132.              null, null, null, null, null, null,
  133.              type, cachedPositionAngleType, frame, date, mu);
  134.     }

  135.     /** Creates a new instance.
  136.      * @param a  semi-major axis (m), negative for hyperbolic orbits
  137.      * @param e eccentricity (positive or equal to 0)
  138.      * @param i inclination (rad)
  139.      * @param pa perigee argument (ω, rad)
  140.      * @param raan right ascension of ascending node (Ω, rad)
  141.      * @param anomaly mean, eccentric or true anomaly (rad)
  142.      * @param type type of anomaly
  143.      * @param frame the frame in which the parameters are defined
  144.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  145.      * @param date date of the orbital parameters
  146.      * @param mu central attraction coefficient (m³/s²)
  147.      * @exception IllegalArgumentException if frame is not a {@link
  148.      * Frame#isPseudoInertial pseudo-inertial frame} or a and e don't match for hyperbolic orbits,
  149.      * or v is out of range for hyperbolic orbits
  150.      * @since 12.1
  151.      */
  152.     public FieldKeplerianOrbit(final T a, final T e, final T i,
  153.                                final T pa, final T raan,
  154.                                final T anomaly, final PositionAngleType type,
  155.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  156.             throws IllegalArgumentException {
  157.         this(a, e, i, pa, raan, anomaly, type, type, frame, date, mu);
  158.     }

  159.     /** Creates a new instance.
  160.      * @param a  semi-major axis (m), negative for hyperbolic orbits
  161.      * @param e eccentricity (positive or equal to 0)
  162.      * @param i inclination (rad)
  163.      * @param pa perigee argument (ω, rad)
  164.      * @param raan right ascension of ascending node (Ω, rad)
  165.      * @param anomaly mean, eccentric or true anomaly (rad)
  166.      * @param aDot  semi-major axis derivative, null if unknown (m/s)
  167.      * @param eDot eccentricity derivative, null if unknown
  168.      * @param iDot inclination derivative, null if unknown (rad/s)
  169.      * @param paDot perigee argument derivative, null if unknown (rad/s)
  170.      * @param raanDot right ascension of ascending node derivative, null if unknown (rad/s)
  171.      * @param anomalyDot mean, eccentric or true anomaly derivative, null if unknown (rad/s)
  172.      * @param type type of anomaly
  173.      * @param cachedPositionAngleType type of cached anomaly
  174.      * @param frame the frame in which the parameters are defined
  175.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  176.      * @param date date of the orbital parameters
  177.      * @param mu central attraction coefficient (m³/s²)
  178.      * @exception IllegalArgumentException if frame is not a {@link
  179.      * Frame#isPseudoInertial pseudo-inertial frame} or a and e don't match for hyperbolic orbits,
  180.      * or v is out of range for hyperbolic orbits
  181.      * @since 12.1
  182.      */
  183.     public FieldKeplerianOrbit(final T a, final T e, final T i,
  184.                                final T pa, final T raan, final T anomaly,
  185.                                final T aDot, final T eDot, final T iDot,
  186.                                final T paDot, final T raanDot, final T anomalyDot,
  187.                                final PositionAngleType type, final PositionAngleType cachedPositionAngleType,
  188.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  189.         throws IllegalArgumentException {
  190.         super(frame, date, mu);
  191.         this.cachedPositionAngleType = cachedPositionAngleType;

  192.         if (a.multiply(e.negate().add(1)).getReal() < 0) {
  193.             throw new OrekitIllegalArgumentException(OrekitMessages.ORBIT_A_E_MISMATCH_WITH_CONIC_TYPE, a.getReal(), e.getReal());
  194.         }

  195.         // Checking eccentricity range
  196.         checkParameterRangeInclusive(ECCENTRICITY, e.getReal(), 0.0, Double.POSITIVE_INFINITY);

  197.         this.a       =    a;
  198.         this.aDot    =    aDot;
  199.         this.e       =    e;
  200.         this.eDot    =    eDot;
  201.         this.i       =    i;
  202.         this.iDot    =    iDot;
  203.         this.pa      =    pa;
  204.         this.paDot   =    paDot;
  205.         this.raan    =    raan;
  206.         this.raanDot =    raanDot;

  207.         this.PLUS_K = FieldVector3D.getPlusK(a.getField());  // third canonical vector

  208.         if (hasDerivatives()) {
  209.             final FieldUnivariateDerivative1<T> cachedAnomalyUD = initializeCachedAnomaly(anomaly, anomalyDot, type);
  210.             this.cachedAnomaly = cachedAnomalyUD.getValue();
  211.             this.cachedAnomalyDot = cachedAnomalyUD.getFirstDerivative();
  212.         } else {
  213.             this.cachedAnomaly = initializeCachedAnomaly(anomaly, type);
  214.             this.cachedAnomalyDot = null;
  215.         }

  216.         // check true anomaly range
  217.         if (!isElliptical()) {
  218.             final T trueAnomaly = getTrueAnomaly();
  219.             if (e.multiply(trueAnomaly.cos()).add(1).getReal() <= 0) {
  220.                 final double vMax = e.reciprocal().negate().acos().getReal();
  221.                 throw new OrekitIllegalArgumentException(OrekitMessages.ORBIT_ANOMALY_OUT_OF_HYPERBOLIC_RANGE,
  222.                         trueAnomaly.getReal(), e.getReal(), -vMax, vMax);
  223.             }
  224.         }

  225.         this.partialPV = null;

  226.     }

  227.     /** Creates a new instance.
  228.      * @param a  semi-major axis (m), negative for hyperbolic orbits
  229.      * @param e eccentricity (positive or equal to 0)
  230.      * @param i inclination (rad)
  231.      * @param pa perigee argument (ω, rad)
  232.      * @param raan right ascension of ascending node (Ω, rad)
  233.      * @param anomaly mean, eccentric or true anomaly (rad)
  234.      * @param aDot  semi-major axis derivative, null if unknown (m/s)
  235.      * @param eDot eccentricity derivative, null if unknown
  236.      * @param iDot inclination derivative, null if unknown (rad/s)
  237.      * @param paDot perigee argument derivative, null if unknown (rad/s)
  238.      * @param raanDot right ascension of ascending node derivative, null if unknown (rad/s)
  239.      * @param anomalyDot mean, eccentric or true anomaly derivative, null if unknown (rad/s)
  240.      * @param type type of anomaly
  241.      * @param frame the frame in which the parameters are defined
  242.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  243.      * @param date date of the orbital parameters
  244.      * @param mu central attraction coefficient (m³/s²)
  245.      * @exception IllegalArgumentException if frame is not a {@link
  246.      * Frame#isPseudoInertial pseudo-inertial frame} or a and e don't match for hyperbolic orbits,
  247.      * or v is out of range for hyperbolic orbits
  248.      */
  249.     public FieldKeplerianOrbit(final T a, final T e, final T i,
  250.                                final T pa, final T raan, final T anomaly,
  251.                                final T aDot, final T eDot, final T iDot,
  252.                                final T paDot, final T raanDot, final T anomalyDot,
  253.                                final PositionAngleType type,
  254.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  255.             throws IllegalArgumentException {
  256.         this(a, e, i, pa, raan, anomaly, aDot, eDot, iDot, paDot, raanDot, anomalyDot,
  257.                 type, type, frame, date, mu);
  258.     }

  259.     /** Constructor from Cartesian parameters.
  260.      *
  261.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  262.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  263.      * use {@code mu} and the position to compute the acceleration, including
  264.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  265.      *
  266.      * @param pvCoordinates the PVCoordinates of the satellite
  267.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  268.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  269.      * @param mu central attraction coefficient (m³/s²)
  270.      * @exception IllegalArgumentException if frame is not a {@link
  271.      * Frame#isPseudoInertial pseudo-inertial frame}
  272.      */
  273.     public FieldKeplerianOrbit(final TimeStampedFieldPVCoordinates<T> pvCoordinates,
  274.                                final Frame frame, final T mu)
  275.         throws IllegalArgumentException {
  276.         this(pvCoordinates, frame, mu, hasNonKeplerianAcceleration(pvCoordinates, mu));
  277.     }

  278.     /** Constructor from Cartesian parameters.
  279.      *
  280.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  281.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  282.      * use {@code mu} and the position to compute the acceleration, including
  283.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  284.      *
  285.      * @param pvCoordinates the PVCoordinates of the satellite
  286.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  287.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  288.      * @param mu central attraction coefficient (m³/s²)
  289.      * @param reliableAcceleration if true, the acceleration is considered to be reliable
  290.      * @exception IllegalArgumentException if frame is not a {@link
  291.      * Frame#isPseudoInertial pseudo-inertial frame}
  292.      */
  293.     private FieldKeplerianOrbit(final TimeStampedFieldPVCoordinates<T> pvCoordinates,
  294.                                 final Frame frame, final T mu,
  295.                                 final boolean reliableAcceleration)
  296.         throws IllegalArgumentException {

  297.         super(pvCoordinates, frame, mu);

  298.         // third canonical vector
  299.         this.PLUS_K = FieldVector3D.getPlusK(getOne().getField());

  300.         // compute inclination
  301.         final FieldVector3D<T> momentum = pvCoordinates.getMomentum();
  302.         final T m2 = momentum.getNormSq();

  303.         i = FieldVector3D.angle(momentum, PLUS_K);
  304.         // compute right ascension of ascending node
  305.         raan = FieldVector3D.crossProduct(PLUS_K, momentum).getAlpha();
  306.         // preliminary computations for parameters depending on orbit shape (elliptic or hyperbolic)
  307.         final FieldVector3D<T> pvP     = pvCoordinates.getPosition();
  308.         final FieldVector3D<T> pvV     = pvCoordinates.getVelocity();
  309.         final FieldVector3D<T> pvA     = pvCoordinates.getAcceleration();

  310.         final T   r2      = pvP.getNormSq();
  311.         final T   r       = r2.sqrt();
  312.         final T   V2      = pvV.getNormSq();
  313.         final T   rV2OnMu = r.multiply(V2).divide(mu);

  314.         // compute semi-major axis (will be negative for hyperbolic orbits)
  315.         a = r.divide(rV2OnMu.negate().add(2.0));
  316.         final T muA = a.multiply(mu);

  317.         // compute true anomaly
  318.         if (isElliptical()) {
  319.             // elliptic or circular orbit
  320.             final T eSE = FieldVector3D.dotProduct(pvP, pvV).divide(muA.sqrt());
  321.             final T eCE = rV2OnMu.subtract(1);
  322.             e = (eSE.multiply(eSE).add(eCE.multiply(eCE))).sqrt();
  323.             this.cachedPositionAngleType = PositionAngleType.ECCENTRIC;
  324.             cachedAnomaly = eSE.atan2(eCE);
  325.         } else {
  326.             // hyperbolic orbit
  327.             final T eSH = FieldVector3D.dotProduct(pvP, pvV).divide(muA.negate().sqrt());
  328.             final T eCH = rV2OnMu.subtract(1);
  329.             e = (m2.negate().divide(muA).add(1)).sqrt();
  330.             this.cachedPositionAngleType = PositionAngleType.TRUE;
  331.             cachedAnomaly = FieldKeplerianAnomalyUtility.hyperbolicEccentricToTrue(e, (eCH.add(eSH)).divide(eCH.subtract(eSH)).log().divide(2));
  332.         }

  333.         // Checking eccentricity range
  334.         checkParameterRangeInclusive(ECCENTRICITY, e.getReal(), 0.0, Double.POSITIVE_INFINITY);

  335.         // compute perigee argument
  336.         final FieldVector3D<T> node = new FieldVector3D<>(raan, getZero());
  337.         final T px = FieldVector3D.dotProduct(pvP, node);
  338.         final T py = FieldVector3D.dotProduct(pvP, FieldVector3D.crossProduct(momentum, node)).divide(m2.sqrt());
  339.         pa = py.atan2(px).subtract(getTrueAnomaly());

  340.         partialPV = pvCoordinates;

  341.         if (reliableAcceleration) {
  342.             // we have a relevant acceleration, we can compute derivatives

  343.             final T[][] jacobian = MathArrays.buildArray(a.getField(), 6, 6);
  344.             getJacobianWrtCartesian(PositionAngleType.MEAN, jacobian);

  345.             final FieldVector3D<T> keplerianAcceleration    = new FieldVector3D<>(r.multiply(r2).reciprocal().multiply(mu.negate()), pvP);
  346.             final FieldVector3D<T> nonKeplerianAcceleration = pvA.subtract(keplerianAcceleration);
  347.             final T   aX                       = nonKeplerianAcceleration.getX();
  348.             final T   aY                       = nonKeplerianAcceleration.getY();
  349.             final T   aZ                       = nonKeplerianAcceleration.getZ();
  350.             aDot    = jacobian[0][3].multiply(aX).add(jacobian[0][4].multiply(aY)).add(jacobian[0][5].multiply(aZ));
  351.             eDot    = jacobian[1][3].multiply(aX).add(jacobian[1][4].multiply(aY)).add(jacobian[1][5].multiply(aZ));
  352.             iDot    = jacobian[2][3].multiply(aX).add(jacobian[2][4].multiply(aY)).add(jacobian[2][5].multiply(aZ));
  353.             paDot   = jacobian[3][3].multiply(aX).add(jacobian[3][4].multiply(aY)).add(jacobian[3][5].multiply(aZ));
  354.             raanDot = jacobian[4][3].multiply(aX).add(jacobian[4][4].multiply(aY)).add(jacobian[4][5].multiply(aZ));

  355.             // in order to compute true anomaly derivative, we must compute
  356.             // mean anomaly derivative including Keplerian motion and convert to true anomaly
  357.             final T MDot = getKeplerianMeanMotion().
  358.                            add(jacobian[5][3].multiply(aX)).add(jacobian[5][4].multiply(aY)).add(jacobian[5][5].multiply(aZ));
  359.             final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  360.             final FieldUnivariateDerivative1<T> MUD = new FieldUnivariateDerivative1<>(getMeanAnomaly(), MDot);
  361.             if (cachedPositionAngleType == PositionAngleType.ECCENTRIC) {
  362.                 final FieldUnivariateDerivative1<T> EUD = (a.getReal() < 0) ?
  363.                          FieldKeplerianAnomalyUtility.hyperbolicMeanToEccentric(eUD, MUD) :
  364.                          FieldKeplerianAnomalyUtility.ellipticMeanToEccentric(eUD, MUD);
  365.                 cachedAnomalyDot = EUD.getFirstDerivative();
  366.             } else { // TRUE
  367.                 final FieldUnivariateDerivative1<T> vUD = (a.getReal() < 0) ?
  368.                          FieldKeplerianAnomalyUtility.hyperbolicMeanToTrue(eUD, MUD) :
  369.                          FieldKeplerianAnomalyUtility.ellipticMeanToTrue(eUD, MUD);
  370.                 cachedAnomalyDot = vUD.getFirstDerivative();
  371.             }

  372.         } else {
  373.             // acceleration is either almost zero or NaN,
  374.             // we assume acceleration was not known
  375.             // we don't set up derivatives
  376.             aDot    = null;
  377.             eDot    = null;
  378.             iDot    = null;
  379.             paDot   = null;
  380.             raanDot = null;
  381.             cachedAnomalyDot    = null;
  382.         }

  383.     }

  384.     /** Constructor from Cartesian parameters.
  385.      *
  386.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  387.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  388.      * use {@code mu} and the position to compute the acceleration, including
  389.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  390.      *
  391.      * @param FieldPVCoordinates the PVCoordinates of the satellite
  392.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  393.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  394.      * @param date date of the orbital parameters
  395.      * @param mu central attraction coefficient (m³/s²)
  396.      * @exception IllegalArgumentException if frame is not a {@link
  397.      * Frame#isPseudoInertial pseudo-inertial frame}
  398.      */
  399.     public FieldKeplerianOrbit(final FieldPVCoordinates<T> FieldPVCoordinates,
  400.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  401.         throws IllegalArgumentException {
  402.         this(new TimeStampedFieldPVCoordinates<>(date, FieldPVCoordinates), frame, mu);
  403.     }

  404.     /** Constructor from any kind of orbital parameters.
  405.      * @param op orbital parameters to copy
  406.      */
  407.     public FieldKeplerianOrbit(final FieldOrbit<T> op) {
  408.         this(op.getPVCoordinates(), op.getFrame(), op.getMu(), op.hasDerivatives());
  409.     }

  410.     /** Constructor from Field and KeplerianOrbit.
  411.      * <p>Build a FieldKeplerianOrbit from non-Field KeplerianOrbit.</p>
  412.      * @param field CalculusField to base object on
  413.      * @param op non-field orbit with only "constant" terms
  414.      * @since 12.0
  415.      */
  416.     public FieldKeplerianOrbit(final Field<T> field, final KeplerianOrbit op) {
  417.         this(field.getZero().newInstance(op.getA()), field.getZero().newInstance(op.getE()), field.getZero().newInstance(op.getI()),
  418.                 field.getZero().newInstance(op.getPerigeeArgument()), field.getZero().newInstance(op.getRightAscensionOfAscendingNode()),
  419.                 field.getZero().newInstance(op.getAnomaly(op.getCachedPositionAngleType())),
  420.                 (op.hasDerivatives()) ? field.getZero().newInstance(op.getADot()) : null,
  421.                 (op.hasDerivatives()) ? field.getZero().newInstance(op.getEDot()) : null,
  422.                 (op.hasDerivatives()) ? field.getZero().newInstance(op.getIDot()) : null,
  423.                 (op.hasDerivatives()) ? field.getZero().newInstance(op.getPerigeeArgumentDot()) : null,
  424.                 (op.hasDerivatives()) ? field.getZero().newInstance(op.getRightAscensionOfAscendingNodeDot()) : null,
  425.                 (op.hasDerivatives()) ? field.getZero().newInstance(op.getAnomalyDot(op.getCachedPositionAngleType())) : null,
  426.                 op.getCachedPositionAngleType(), op.getFrame(),
  427.                 new FieldAbsoluteDate<>(field, op.getDate()), field.getZero().newInstance(op.getMu()));
  428.     }

  429.     /** Constructor from Field and Orbit.
  430.      * <p>Build a FieldKeplerianOrbit from any non-Field Orbit.</p>
  431.      * @param field CalculusField to base object on
  432.      * @param op non-field orbit with only "constant" terms
  433.      * @since 12.0
  434.      */
  435.     public FieldKeplerianOrbit(final Field<T> field, final Orbit op) {
  436.         this(field, (KeplerianOrbit) OrbitType.KEPLERIAN.convertType(op));
  437.     }

  438.     /** {@inheritDoc} */
  439.     @Override
  440.     public OrbitType getType() {
  441.         return OrbitType.KEPLERIAN;
  442.     }

  443.     /** {@inheritDoc} */
  444.     @Override
  445.     public T getA() {
  446.         return a;
  447.     }

  448.     /** {@inheritDoc} */
  449.     @Override
  450.     public T getADot() {
  451.         return aDot;
  452.     }

  453.     /** {@inheritDoc} */
  454.     @Override
  455.     public T getE() {
  456.         return e;
  457.     }

  458.     /** {@inheritDoc} */
  459.     @Override
  460.     public T getEDot() {
  461.         return eDot;
  462.     }

  463.     /** {@inheritDoc} */
  464.     @Override
  465.     public T getI() {
  466.         return i;
  467.     }

  468.     /** {@inheritDoc} */
  469.     @Override
  470.     public T getIDot() {
  471.         return iDot;
  472.     }

  473.     /** Get the perigee argument.
  474.      * @return perigee argument (rad)
  475.      */
  476.     public T getPerigeeArgument() {
  477.         return pa;
  478.     }

  479.     /** Get the perigee argument derivative.
  480.      * <p>
  481.      * If the orbit was created without derivatives, the value returned is null.
  482.      * </p>
  483.      * @return perigee argument derivative (rad/s)
  484.      */
  485.     public T getPerigeeArgumentDot() {
  486.         return paDot;
  487.     }

  488.     /** Get the right ascension of the ascending node.
  489.      * @return right ascension of the ascending node (rad)
  490.      */
  491.     public T getRightAscensionOfAscendingNode() {
  492.         return raan;
  493.     }

  494.     /** Get the right ascension of the ascending node derivative.
  495.      * <p>
  496.      * If the orbit was created without derivatives, the value returned is null.
  497.      * </p>
  498.      * @return right ascension of the ascending node derivative (rad/s)
  499.      */
  500.     public T getRightAscensionOfAscendingNodeDot() {
  501.         return raanDot;
  502.     }

  503.     /** Get the true anomaly.
  504.      * @return true anomaly (rad)
  505.      */
  506.     public T getTrueAnomaly() {
  507.         switch (cachedPositionAngleType) {
  508.             case MEAN: return (a.getReal() < 0) ? FieldKeplerianAnomalyUtility.hyperbolicMeanToTrue(e, cachedAnomaly) :
  509.                     FieldKeplerianAnomalyUtility.ellipticMeanToTrue(e, cachedAnomaly);

  510.             case TRUE: return cachedAnomaly;

  511.             case ECCENTRIC: return (a.getReal() < 0) ? FieldKeplerianAnomalyUtility.hyperbolicEccentricToTrue(e, cachedAnomaly) :
  512.                     FieldKeplerianAnomalyUtility.ellipticEccentricToTrue(e, cachedAnomaly);

  513.             default: throw new OrekitInternalError(null);
  514.         }
  515.     }

  516.     /** Get the true anomaly derivative.
  517.      * <p>
  518.      * If the orbit was created without derivatives, the value returned is null.
  519.      * </p>
  520.      * @return true anomaly derivative (rad/s)
  521.      */
  522.     public T getTrueAnomalyDot() {
  523.         if (hasDerivatives()) {
  524.             switch (cachedPositionAngleType) {
  525.                 case MEAN:
  526.                     final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  527.                     final FieldUnivariateDerivative1<T> MUD = new FieldUnivariateDerivative1<>(cachedAnomaly, cachedAnomalyDot);
  528.                     final FieldUnivariateDerivative1<T> vUD = (a.getReal() < 0) ?
  529.                              FieldKeplerianAnomalyUtility.hyperbolicMeanToTrue(eUD, MUD) :
  530.                              FieldKeplerianAnomalyUtility.ellipticMeanToTrue(eUD, MUD);
  531.                     return vUD.getFirstDerivative();

  532.                 case TRUE:
  533.                     return cachedAnomalyDot;

  534.                 case ECCENTRIC:
  535.                     final FieldUnivariateDerivative1<T> eUD2 = new FieldUnivariateDerivative1<>(e, eDot);
  536.                     final FieldUnivariateDerivative1<T> EUD = new FieldUnivariateDerivative1<>(cachedAnomaly, cachedAnomalyDot);
  537.                     final FieldUnivariateDerivative1<T> vUD2 = (a.getReal() < 0) ?
  538.                              FieldKeplerianAnomalyUtility.hyperbolicEccentricToTrue(eUD2, EUD) :
  539.                              FieldKeplerianAnomalyUtility.ellipticEccentricToTrue(eUD2, EUD);
  540.                     return vUD2.getFirstDerivative();

  541.                 default:
  542.                     throw new OrekitInternalError(null);
  543.             }
  544.         } else {
  545.             return null;
  546.         }
  547.     }

  548.     /** Get the eccentric anomaly.
  549.      * @return eccentric anomaly (rad)
  550.      */
  551.     public T getEccentricAnomaly() {
  552.         switch (cachedPositionAngleType) {
  553.             case MEAN:
  554.                 return (a.getReal() < 0) ? FieldKeplerianAnomalyUtility.hyperbolicMeanToEccentric(e, cachedAnomaly) :
  555.                     FieldKeplerianAnomalyUtility.ellipticMeanToEccentric(e, cachedAnomaly);

  556.             case ECCENTRIC:
  557.                 return cachedAnomaly;

  558.             case TRUE:
  559.                 return (a.getReal() < 0) ? FieldKeplerianAnomalyUtility.hyperbolicTrueToEccentric(e, cachedAnomaly) :
  560.                     FieldKeplerianAnomalyUtility.ellipticTrueToEccentric(e, cachedAnomaly);

  561.             default:
  562.                 throw new OrekitInternalError(null);
  563.         }
  564.     }

  565.     /** Get the eccentric anomaly derivative.
  566.      * <p>
  567.      * If the orbit was created without derivatives, the value returned is null.
  568.      * </p>
  569.      * @return eccentric anomaly derivative (rad/s)
  570.      */
  571.     public T getEccentricAnomalyDot() {
  572.         if (hasDerivatives()) {
  573.             switch (cachedPositionAngleType) {
  574.                 case ECCENTRIC:
  575.                     return cachedAnomalyDot;

  576.                 case TRUE:
  577.                     final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  578.                     final FieldUnivariateDerivative1<T> vUD = new FieldUnivariateDerivative1<>(cachedAnomaly, cachedAnomalyDot);
  579.                     final FieldUnivariateDerivative1<T> EUD = (a.getReal() < 0) ?
  580.                              FieldKeplerianAnomalyUtility.hyperbolicTrueToEccentric(eUD, vUD) :
  581.                              FieldKeplerianAnomalyUtility.ellipticTrueToEccentric(eUD, vUD);
  582.                     return EUD.getFirstDerivative();

  583.                 case MEAN:
  584.                     final FieldUnivariateDerivative1<T> eUD2 = new FieldUnivariateDerivative1<>(e, eDot);
  585.                     final FieldUnivariateDerivative1<T> MUD = new FieldUnivariateDerivative1<>(cachedAnomaly, cachedAnomalyDot);
  586.                     final FieldUnivariateDerivative1<T> EUD2 = (a.getReal() < 0) ?
  587.                              FieldKeplerianAnomalyUtility.hyperbolicMeanToEccentric(eUD2, MUD) :
  588.                              FieldKeplerianAnomalyUtility.ellipticMeanToEccentric(eUD2, MUD);
  589.                     return EUD2.getFirstDerivative();

  590.                 default:
  591.                     throw new OrekitInternalError(null);
  592.             }
  593.         } else {
  594.             return null;
  595.         }
  596.     }

  597.     /** Get the mean anomaly.
  598.      * @return mean anomaly (rad)
  599.      */
  600.     public T getMeanAnomaly() {
  601.         switch (cachedPositionAngleType) {
  602.             case ECCENTRIC: return (a.getReal() < 0) ? FieldKeplerianAnomalyUtility.hyperbolicEccentricToMean(e, cachedAnomaly) :
  603.                     FieldKeplerianAnomalyUtility.ellipticEccentricToMean(e, cachedAnomaly);

  604.             case MEAN: return cachedAnomaly;

  605.             case TRUE: return (a.getReal() < 0) ? FieldKeplerianAnomalyUtility.hyperbolicTrueToMean(e, cachedAnomaly) :
  606.                     FieldKeplerianAnomalyUtility.ellipticTrueToMean(e, cachedAnomaly);

  607.             default: throw new OrekitInternalError(null);
  608.         }
  609.     }

  610.     /** Get the mean anomaly derivative.
  611.      * <p>
  612.      * If the orbit was created without derivatives, the value returned is null.
  613.      * </p>
  614.      * @return mean anomaly derivative (rad/s)
  615.      */
  616.     public T getMeanAnomalyDot() {
  617.         if (hasDerivatives()) {
  618.             switch (cachedPositionAngleType) {
  619.                 case MEAN:
  620.                     return cachedAnomalyDot;

  621.                 case ECCENTRIC:
  622.                     final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  623.                     final FieldUnivariateDerivative1<T> EUD = new FieldUnivariateDerivative1<>(cachedAnomaly, cachedAnomalyDot);
  624.                     final FieldUnivariateDerivative1<T> MUD = (a.getReal() < 0) ?
  625.                              FieldKeplerianAnomalyUtility.hyperbolicEccentricToMean(eUD, EUD) :
  626.                              FieldKeplerianAnomalyUtility.ellipticEccentricToMean(eUD, EUD);
  627.                     return MUD.getFirstDerivative();

  628.                 case TRUE:
  629.                     final FieldUnivariateDerivative1<T> eUD2 = new FieldUnivariateDerivative1<>(e, eDot);
  630.                     final FieldUnivariateDerivative1<T> vUD = new FieldUnivariateDerivative1<>(cachedAnomaly, cachedAnomalyDot);
  631.                     final FieldUnivariateDerivative1<T> MUD2 = (a.getReal() < 0) ?
  632.                              FieldKeplerianAnomalyUtility.hyperbolicTrueToMean(eUD2, vUD) :
  633.                              FieldKeplerianAnomalyUtility.ellipticTrueToMean(eUD2, vUD);
  634.                     return MUD2.getFirstDerivative();

  635.                 default:
  636.                     throw new OrekitInternalError(null);
  637.             }
  638.         } else {
  639.             return null;
  640.         }
  641.     }

  642.     /** Get the anomaly.
  643.      * @param type type of the angle
  644.      * @return anomaly (rad)
  645.      */
  646.     public T getAnomaly(final PositionAngleType type) {
  647.         return (type == PositionAngleType.MEAN) ? getMeanAnomaly() :
  648.                                               ((type == PositionAngleType.ECCENTRIC) ? getEccentricAnomaly() :
  649.                                                                                    getTrueAnomaly());
  650.     }

  651.     /** Get the anomaly derivative.
  652.      * <p>
  653.      * If the orbit was created without derivatives, the value returned is null.
  654.      * </p>
  655.      * @param type type of the angle
  656.      * @return anomaly derivative (rad/s)
  657.      */
  658.     public T getAnomalyDot(final PositionAngleType type) {
  659.         return (type == PositionAngleType.MEAN) ? getMeanAnomalyDot() :
  660.                                               ((type == PositionAngleType.ECCENTRIC) ? getEccentricAnomalyDot() :
  661.                                                                                    getTrueAnomalyDot());
  662.     }

  663.     /** {@inheritDoc} */
  664.     @Override
  665.     public boolean hasDerivatives() {
  666.         return aDot != null;
  667.     }

  668.     /** {@inheritDoc} */
  669.     @Override
  670.     public T getEquinoctialEx() {
  671.         return e.multiply(pa.add(raan).cos());
  672.     }

  673.     /** {@inheritDoc} */
  674.     @Override
  675.     public T getEquinoctialExDot() {

  676.         if (!hasDerivatives()) {
  677.             return null;
  678.         }

  679.         final FieldUnivariateDerivative1<T> eUD    = new FieldUnivariateDerivative1<>(e,    eDot);
  680.         final FieldUnivariateDerivative1<T> paUD   = new FieldUnivariateDerivative1<>(pa,   paDot);
  681.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  682.         return eUD.multiply(paUD.add(raanUD).cos()).getFirstDerivative();

  683.     }

  684.     /** {@inheritDoc} */
  685.     @Override
  686.     public T getEquinoctialEy() {
  687.         return e.multiply((pa.add(raan)).sin());
  688.     }

  689.     /** {@inheritDoc} */
  690.     @Override
  691.     public T getEquinoctialEyDot() {

  692.         if (!hasDerivatives()) {
  693.             return null;
  694.         }

  695.         final FieldUnivariateDerivative1<T> eUD    = new FieldUnivariateDerivative1<>(e,    eDot);
  696.         final FieldUnivariateDerivative1<T> paUD   = new FieldUnivariateDerivative1<>(pa,   paDot);
  697.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  698.         return eUD.multiply(paUD.add(raanUD).sin()).getFirstDerivative();

  699.     }

  700.     /** {@inheritDoc} */
  701.     @Override
  702.     public T getHx() {
  703.         // Check for equatorial retrograde orbit
  704.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  705.             return getZero().add(Double.NaN);
  706.         }
  707.         return raan.cos().multiply(i.divide(2).tan());
  708.     }

  709.     /** {@inheritDoc} */
  710.     @Override
  711.     public T getHxDot() {

  712.         if (!hasDerivatives()) {
  713.             return null;
  714.         }

  715.         // Check for equatorial retrograde orbit
  716.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  717.             return getZero().add(Double.NaN);
  718.         }

  719.         final FieldUnivariateDerivative1<T> iUD    = new FieldUnivariateDerivative1<>(i,    iDot);
  720.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  721.         return raanUD.cos().multiply(iUD.multiply(0.5).tan()).getFirstDerivative();

  722.     }

  723.     /** {@inheritDoc} */
  724.     @Override
  725.     public T getHy() {
  726.         // Check for equatorial retrograde orbit
  727.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  728.             return getZero().add(Double.NaN);
  729.         }
  730.         return raan.sin().multiply(i.divide(2).tan());
  731.     }

  732.     /** {@inheritDoc} */
  733.     @Override
  734.     public T getHyDot() {

  735.         if (!hasDerivatives()) {
  736.             return null;
  737.         }

  738.         // Check for equatorial retrograde orbit
  739.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  740.             return getZero().add(Double.NaN);
  741.         }

  742.         final FieldUnivariateDerivative1<T> iUD    = new FieldUnivariateDerivative1<>(i,    iDot);
  743.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  744.         return raanUD.sin().multiply(iUD.multiply(0.5).tan()).getFirstDerivative();

  745.     }

  746.     /** {@inheritDoc} */
  747.     @Override
  748.     public T getLv() {
  749.         return pa.add(raan).add(getTrueAnomaly());
  750.     }

  751.     /** {@inheritDoc} */
  752.     @Override
  753.     public T getLvDot() {
  754.         return hasDerivatives() ?
  755.                paDot.add(raanDot).add(getTrueAnomalyDot()) :
  756.                null;
  757.     }

  758.     /** {@inheritDoc} */
  759.     @Override
  760.     public T getLE() {
  761.         return pa.add(raan).add(getEccentricAnomaly());
  762.     }

  763.     /** {@inheritDoc} */
  764.     @Override
  765.     public T getLEDot() {
  766.         return hasDerivatives() ?
  767.                paDot.add(raanDot).add(getEccentricAnomalyDot()) :
  768.                null;
  769.     }

  770.     /** {@inheritDoc} */
  771.     @Override
  772.     public T getLM() {
  773.         return pa.add(raan).add(getMeanAnomaly());
  774.     }

  775.     /** {@inheritDoc} */
  776.     @Override
  777.     public T getLMDot() {
  778.         return hasDerivatives() ?
  779.                paDot.add(raanDot).add(getMeanAnomalyDot()) :
  780.                null;
  781.     }

  782.     /** Initialize cached anomaly with rate.
  783.      * @param anomaly input anomaly
  784.      * @param anomalyDot rate of input anomaly
  785.      * @param inputType position angle type passed as input
  786.      * @return anomaly to cache with rate
  787.      * @since 12.1
  788.      */
  789.     private FieldUnivariateDerivative1<T> initializeCachedAnomaly(final T anomaly, final T anomalyDot,
  790.                                                                   final PositionAngleType inputType) {
  791.         if (cachedPositionAngleType == inputType) {
  792.             return new FieldUnivariateDerivative1<>(anomaly, anomalyDot);

  793.         } else {
  794.             final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  795.             final FieldUnivariateDerivative1<T> anomalyUD = new FieldUnivariateDerivative1<>(anomaly, anomalyDot);

  796.             if (a.getReal() < 0) {
  797.                 switch (cachedPositionAngleType) {
  798.                     case MEAN:
  799.                         if (inputType == PositionAngleType.ECCENTRIC) {
  800.                             return FieldKeplerianAnomalyUtility.hyperbolicEccentricToMean(eUD, anomalyUD);
  801.                         } else {
  802.                             return FieldKeplerianAnomalyUtility.hyperbolicTrueToMean(eUD, anomalyUD);
  803.                         }

  804.                     case ECCENTRIC:
  805.                         if (inputType == PositionAngleType.MEAN) {
  806.                             return FieldKeplerianAnomalyUtility.hyperbolicMeanToEccentric(eUD, anomalyUD);
  807.                         } else {
  808.                             return FieldKeplerianAnomalyUtility.hyperbolicTrueToEccentric(eUD, anomalyUD);
  809.                         }

  810.                     case TRUE:
  811.                         if (inputType == PositionAngleType.MEAN) {
  812.                             return FieldKeplerianAnomalyUtility.hyperbolicMeanToTrue(eUD, anomalyUD);
  813.                         } else {
  814.                             return FieldKeplerianAnomalyUtility.hyperbolicEccentricToTrue(eUD, anomalyUD);
  815.                         }

  816.                     default:
  817.                         break;
  818.                 }

  819.             } else {
  820.                 switch (cachedPositionAngleType) {
  821.                     case MEAN:
  822.                         if (inputType == PositionAngleType.ECCENTRIC) {
  823.                             return FieldKeplerianAnomalyUtility.ellipticEccentricToMean(eUD, anomalyUD);
  824.                         } else {
  825.                             return FieldKeplerianAnomalyUtility.ellipticTrueToMean(eUD, anomalyUD);
  826.                         }

  827.                     case ECCENTRIC:
  828.                         if (inputType == PositionAngleType.MEAN) {
  829.                             return FieldKeplerianAnomalyUtility.ellipticMeanToEccentric(eUD, anomalyUD);
  830.                         } else {
  831.                             return FieldKeplerianAnomalyUtility.ellipticTrueToEccentric(eUD, anomalyUD);
  832.                         }

  833.                     case TRUE:
  834.                         if (inputType == PositionAngleType.MEAN) {
  835.                             return FieldKeplerianAnomalyUtility.ellipticMeanToTrue(eUD, anomalyUD);
  836.                         } else {
  837.                             return FieldKeplerianAnomalyUtility.ellipticEccentricToTrue(eUD, anomalyUD);
  838.                         }

  839.                     default:
  840.                         break;
  841.                 }

  842.             }
  843.             throw new OrekitInternalError(null);
  844.         }

  845.     }

  846.     /** Initialize cached anomaly.
  847.      * @param anomaly input anomaly
  848.      * @param inputType position angle type passed as input
  849.      * @return anomaly to cache
  850.      * @since 12.1
  851.      */
  852.     private T initializeCachedAnomaly(final T anomaly, final PositionAngleType inputType) {
  853.         return FieldKeplerianAnomalyUtility.convertAnomaly(inputType, anomaly, e, cachedPositionAngleType);
  854.     }

  855.     /** Compute reference axes.
  856.      * @return reference axes
  857.      * @since 12.0
  858.      */
  859.     private FieldVector3D<T>[] referenceAxes() {

  860.         // preliminary variables
  861.         final FieldSinCos<T> scRaan = FastMath.sinCos(raan);
  862.         final FieldSinCos<T> scPa   = FastMath.sinCos(pa);
  863.         final FieldSinCos<T> scI    = FastMath.sinCos(i);
  864.         final T cosRaan = scRaan.cos();
  865.         final T sinRaan = scRaan.sin();
  866.         final T cosPa   = scPa.cos();
  867.         final T sinPa   = scPa.sin();
  868.         final T cosI    = scI.cos();
  869.         final T sinI    = scI.sin();
  870.         final T crcp    = cosRaan.multiply(cosPa);
  871.         final T crsp    = cosRaan.multiply(sinPa);
  872.         final T srcp    = sinRaan.multiply(cosPa);
  873.         final T srsp    = sinRaan.multiply(sinPa);

  874.         // reference axes defining the orbital plane
  875.         @SuppressWarnings("unchecked")
  876.         final FieldVector3D<T>[] axes = (FieldVector3D<T>[]) Array.newInstance(FieldVector3D.class, 2);
  877.         axes[0] = new FieldVector3D<>(crcp.subtract(cosI.multiply(srsp)),  srcp.add(cosI.multiply(crsp)), sinI.multiply(sinPa));
  878.         axes[1] = new FieldVector3D<>(crsp.add(cosI.multiply(srcp)).negate(), cosI.multiply(crcp).subtract(srsp), sinI.multiply(cosPa));

  879.         return axes;

  880.     }

  881.     /** Compute position and velocity but not acceleration.
  882.      */
  883.     private void computePVWithoutA() {

  884.         if (partialPV != null) {
  885.             // already computed
  886.             return;
  887.         }

  888.         final FieldVector3D<T>[] axes = referenceAxes();

  889.         if (isElliptical()) {

  890.             // elliptical case

  891.             // elliptic eccentric anomaly
  892.             final T uME2             = e.negate().add(1).multiply(e.add(1));
  893.             final T s1Me2            = uME2.sqrt();
  894.             final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  895.             final T cosE             = scE.cos();
  896.             final T sinE             = scE.sin();

  897.             // coordinates of position and velocity in the orbital plane
  898.             final T x      = a.multiply(cosE.subtract(e));
  899.             final T y      = a.multiply(sinE).multiply(s1Me2);
  900.             final T factor = FastMath.sqrt(getMu().divide(a)).divide(e.negate().multiply(cosE).add(1));
  901.             final T xDot   = sinE.negate().multiply(factor);
  902.             final T yDot   = cosE.multiply(s1Me2).multiply(factor);

  903.             final FieldVector3D<T> position = new FieldVector3D<>(x, axes[0], y, axes[1]);
  904.             final FieldVector3D<T> velocity = new FieldVector3D<>(xDot, axes[0], yDot, axes[1]);
  905.             partialPV = new FieldPVCoordinates<>(position, velocity);

  906.         } else {

  907.             // hyperbolic case

  908.             // compute position and velocity factors
  909.             final FieldSinCos<T> scV = FastMath.sinCos(getTrueAnomaly());
  910.             final T sinV             = scV.sin();
  911.             final T cosV             = scV.cos();
  912.             final T f                = a.multiply(e.square().negate().add(1));
  913.             final T posFactor        = f.divide(e.multiply(cosV).add(1));
  914.             final T velFactor        = FastMath.sqrt(getMu().divide(f));

  915.             final FieldVector3D<T> position     = new FieldVector3D<>(posFactor.multiply(cosV), axes[0], posFactor.multiply(sinV), axes[1]);
  916.             final FieldVector3D<T> velocity     = new FieldVector3D<>(velFactor.multiply(sinV).negate(), axes[0], velFactor.multiply(e.add(cosV)), axes[1]);
  917.             partialPV = new FieldPVCoordinates<>(position, velocity);

  918.         }

  919.     }

  920.     /** Compute non-Keplerian part of the acceleration from first time derivatives.
  921.      * <p>
  922.      * This method should be called only when {@link #hasDerivatives()} returns true.
  923.      * </p>
  924.      * @return non-Keplerian part of the acceleration
  925.      */
  926.     private FieldVector3D<T> nonKeplerianAcceleration() {

  927.         final T[][] dCdP = MathArrays.buildArray(a.getField(), 6, 6);
  928.         getJacobianWrtParameters(PositionAngleType.MEAN, dCdP);

  929.         final T nonKeplerianMeanMotion = getMeanAnomalyDot().subtract(getKeplerianMeanMotion());
  930.         final T nonKeplerianAx =     dCdP[3][0].multiply(aDot).
  931.                                  add(dCdP[3][1].multiply(eDot)).
  932.                                  add(dCdP[3][2].multiply(iDot)).
  933.                                  add(dCdP[3][3].multiply(paDot)).
  934.                                  add(dCdP[3][4].multiply(raanDot)).
  935.                                  add(dCdP[3][5].multiply(nonKeplerianMeanMotion));
  936.         final T nonKeplerianAy =     dCdP[4][0].multiply(aDot).
  937.                                  add(dCdP[4][1].multiply(eDot)).
  938.                                  add(dCdP[4][2].multiply(iDot)).
  939.                                  add(dCdP[4][3].multiply(paDot)).
  940.                                  add(dCdP[4][4].multiply(raanDot)).
  941.                                  add(dCdP[4][5].multiply(nonKeplerianMeanMotion));
  942.         final T nonKeplerianAz =     dCdP[5][0].multiply(aDot).
  943.                                  add(dCdP[5][1].multiply(eDot)).
  944.                                  add(dCdP[5][2].multiply(iDot)).
  945.                                  add(dCdP[5][3].multiply(paDot)).
  946.                                  add(dCdP[5][4].multiply(raanDot)).
  947.                                  add(dCdP[5][5].multiply(nonKeplerianMeanMotion));

  948.         return new FieldVector3D<>(nonKeplerianAx, nonKeplerianAy, nonKeplerianAz);

  949.     }

  950.     /** {@inheritDoc} */
  951.     @Override
  952.     protected FieldVector3D<T> initPosition() {
  953.         final FieldVector3D<T>[] axes = referenceAxes();

  954.         if (isElliptical()) {

  955.             // elliptical case

  956.             // elliptic eccentric anomaly
  957.             final T uME2             = e.negate().add(1).multiply(e.add(1));
  958.             final T s1Me2            = uME2.sqrt();
  959.             final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  960.             final T cosE             = scE.cos();
  961.             final T sinE             = scE.sin();

  962.             return new FieldVector3D<>(a.multiply(cosE.subtract(e)), axes[0], a.multiply(sinE).multiply(s1Me2), axes[1]);

  963.         } else {

  964.             // hyperbolic case

  965.             // compute position and velocity factors
  966.             final FieldSinCos<T> scV = FastMath.sinCos(getTrueAnomaly());
  967.             final T sinV             = scV.sin();
  968.             final T cosV             = scV.cos();
  969.             final T f                = a.multiply(e.square().negate().add(1));
  970.             final T posFactor        = f.divide(e.multiply(cosV).add(1));

  971.             return new FieldVector3D<>(posFactor.multiply(cosV), axes[0], posFactor.multiply(sinV), axes[1]);

  972.         }

  973.     }

  974.     /** {@inheritDoc} */
  975.     @Override
  976.     protected TimeStampedFieldPVCoordinates<T> initPVCoordinates() {

  977.         // position and velocity
  978.         computePVWithoutA();

  979.         // acceleration
  980.         final T r2 = partialPV.getPosition().getNormSq();
  981.         final FieldVector3D<T> keplerianAcceleration = new FieldVector3D<>(r2.multiply(FastMath.sqrt(r2)).reciprocal().multiply(getMu().negate()),
  982.                                                                            partialPV.getPosition());
  983.         final FieldVector3D<T> acceleration = hasDerivatives() ?
  984.                                               keplerianAcceleration.add(nonKeplerianAcceleration()) :
  985.                                               keplerianAcceleration;

  986.         return new TimeStampedFieldPVCoordinates<>(getDate(), partialPV.getPosition(), partialPV.getVelocity(), acceleration);

  987.     }

  988.     /** {@inheritDoc} */
  989.     @Override
  990.     public FieldKeplerianOrbit<T> shiftedBy(final double dt) {
  991.         return shiftedBy(getZero().newInstance(dt));
  992.     }

  993.     /** {@inheritDoc} */
  994.     @Override
  995.     public FieldKeplerianOrbit<T> shiftedBy(final T dt) {

  996.         // use Keplerian-only motion
  997.         final FieldKeplerianOrbit<T> keplerianShifted = new FieldKeplerianOrbit<>(a, e, i, pa, raan,
  998.                                                                                   getKeplerianMeanMotion().multiply(dt).add(getMeanAnomaly()),
  999.                                                                                   PositionAngleType.MEAN, cachedPositionAngleType, getFrame(), getDate().shiftedBy(dt), getMu());

  1000.         if (hasDerivatives()) {

  1001.             // extract non-Keplerian acceleration from first time derivatives
  1002.             final FieldVector3D<T> nonKeplerianAcceleration = nonKeplerianAcceleration();

  1003.             // add quadratic effect of non-Keplerian acceleration to Keplerian-only shift
  1004.             keplerianShifted.computePVWithoutA();
  1005.             final FieldVector3D<T> fixedP   = new FieldVector3D<>(getOne(), keplerianShifted.partialPV.getPosition(),
  1006.                                                                   dt.square().multiply(0.5), nonKeplerianAcceleration);
  1007.             final T   fixedR2 = fixedP.getNormSq();
  1008.             final T   fixedR  = fixedR2.sqrt();
  1009.             final FieldVector3D<T> fixedV  = new FieldVector3D<>(getOne(), keplerianShifted.partialPV.getVelocity(),
  1010.                                                                  dt, nonKeplerianAcceleration);
  1011.             final FieldVector3D<T> fixedA  = new FieldVector3D<>(fixedR2.multiply(fixedR).reciprocal().multiply(getMu().negate()),
  1012.                                                                  keplerianShifted.partialPV.getPosition(),
  1013.                                                                  getOne(), nonKeplerianAcceleration);

  1014.             // build a new orbit, taking non-Keplerian acceleration into account
  1015.             return new FieldKeplerianOrbit<>(new TimeStampedFieldPVCoordinates<>(keplerianShifted.getDate(),
  1016.                                                                                  fixedP, fixedV, fixedA),
  1017.                                              keplerianShifted.getFrame(), keplerianShifted.getMu());

  1018.         } else {
  1019.             // Keplerian-only motion is all we can do
  1020.             return keplerianShifted;
  1021.         }

  1022.     }

  1023.     /** {@inheritDoc} */
  1024.     @Override
  1025.     protected T[][] computeJacobianMeanWrtCartesian() {
  1026.         if (isElliptical()) {
  1027.             return computeJacobianMeanWrtCartesianElliptical();
  1028.         } else {
  1029.             return computeJacobianMeanWrtCartesianHyperbolic();
  1030.         }
  1031.     }

  1032.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1033.      * <p>
  1034.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1035.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1036.      * yDot for j=4, zDot for j=5).
  1037.      * </p>
  1038.      * @return 6x6 Jacobian matrix
  1039.      */
  1040.     private T[][] computeJacobianMeanWrtCartesianElliptical() {

  1041.         final T[][] jacobian = MathArrays.buildArray(getA().getField(), 6, 6);

  1042.         // compute various intermediate parameters
  1043.         computePVWithoutA();
  1044.         final FieldVector3D<T> position = partialPV.getPosition();
  1045.         final FieldVector3D<T> velocity = partialPV.getVelocity();
  1046.         final FieldVector3D<T> momentum = partialPV.getMomentum();
  1047.         final T v2         = velocity.getNormSq();
  1048.         final T r2         = position.getNormSq();
  1049.         final T r          = r2.sqrt();
  1050.         final T r3         = r.multiply(r2);

  1051.         final T px         = position.getX();
  1052.         final T py         = position.getY();
  1053.         final T pz         = position.getZ();
  1054.         final T vx         = velocity.getX();
  1055.         final T vy         = velocity.getY();
  1056.         final T vz         = velocity.getZ();
  1057.         final T mx         = momentum.getX();
  1058.         final T my         = momentum.getY();
  1059.         final T mz         = momentum.getZ();

  1060.         final T mu         = getMu();
  1061.         final T sqrtMuA    = FastMath.sqrt(a.multiply(mu));
  1062.         final T sqrtAoMu   = FastMath.sqrt(a.divide(mu));
  1063.         final T a2         = a.square();
  1064.         final T twoA       = a.multiply(2);
  1065.         final T rOnA       = r.divide(a);

  1066.         final T oMe2       = e.square().negate().add(1);
  1067.         final T epsilon    = oMe2.sqrt();
  1068.         final T sqrtRec    = epsilon.reciprocal();

  1069.         final FieldSinCos<T> scI  = FastMath.sinCos(i);
  1070.         final FieldSinCos<T> scPA = FastMath.sinCos(pa);
  1071.         final T cosI       = scI.cos();
  1072.         final T sinI       = scI.sin();
  1073.         final T cosPA      = scPA.cos();
  1074.         final T sinPA      = scPA.sin();

  1075.         final T pv         = FieldVector3D.dotProduct(position, velocity);
  1076.         final T cosE       = a.subtract(r).divide(a.multiply(e));
  1077.         final T sinE       = pv.divide(e.multiply(sqrtMuA));

  1078.         // da
  1079.         final FieldVector3D<T> vectorAR = new FieldVector3D<>(a2.multiply(2).divide(r3), position);
  1080.         final FieldVector3D<T> vectorARDot = velocity.scalarMultiply(a2.multiply(mu.divide(2.).reciprocal()));
  1081.         fillHalfRow(getOne(), vectorAR,    jacobian[0], 0);
  1082.         fillHalfRow(getOne(), vectorARDot, jacobian[0], 3);

  1083.         // de
  1084.         final T factorER3 = pv.divide(twoA);
  1085.         final FieldVector3D<T> vectorER   = new FieldVector3D<>(cosE.multiply(v2).divide(r.multiply(mu)), position,
  1086.                                                                 sinE.divide(sqrtMuA), velocity,
  1087.                                                                 factorER3.negate().multiply(sinE).divide(sqrtMuA), vectorAR);
  1088.         final FieldVector3D<T> vectorERDot = new FieldVector3D<>(sinE.divide(sqrtMuA), position,
  1089.                                                                  cosE.multiply(mu.divide(2.).reciprocal()).multiply(r), velocity,
  1090.                                                                  factorER3.negate().multiply(sinE).divide(sqrtMuA), vectorARDot);
  1091.         fillHalfRow(getOne(), vectorER,    jacobian[1], 0);
  1092.         fillHalfRow(getOne(), vectorERDot, jacobian[1], 3);

  1093.         // dE / dr (Eccentric anomaly)
  1094.         final T coefE = cosE.divide(e.multiply(sqrtMuA));
  1095.         final FieldVector3D<T>  vectorEAnR =
  1096.             new FieldVector3D<>(sinE.negate().multiply(v2).divide(e.multiply(r).multiply(mu)), position, coefE, velocity,
  1097.                                 factorER3.negate().multiply(coefE), vectorAR);

  1098.         // dE / drDot
  1099.         final FieldVector3D<T>  vectorEAnRDot =
  1100.             new FieldVector3D<>(sinE.multiply(-2).multiply(r).divide(e.multiply(mu)), velocity, coefE, position,
  1101.                                 factorER3.negate().multiply(coefE), vectorARDot);

  1102.         // precomputing some more factors
  1103.         final T s1 = sinE.negate().multiply(pz).divide(r).subtract(cosE.multiply(vz).multiply(sqrtAoMu));
  1104.         final T s2 = cosE.negate().multiply(pz).divide(r3);
  1105.         final T s3 = sinE.multiply(vz).divide(sqrtMuA.multiply(-2));
  1106.         final T t1 = sqrtRec.multiply(cosE.multiply(pz).divide(r).subtract(sinE.multiply(vz).multiply(sqrtAoMu)));
  1107.         final T t2 = sqrtRec.multiply(sinE.negate().multiply(pz).divide(r3));
  1108.         final T t3 = sqrtRec.multiply(cosE.subtract(e)).multiply(vz).divide(sqrtMuA.multiply(2));
  1109.         final T t4 = sqrtRec.multiply(e.multiply(sinI).multiply(cosPA).multiply(sqrtRec).subtract(vz.multiply(sqrtAoMu)));
  1110.         final FieldVector3D<T> s = new FieldVector3D<>(cosE.divide(r), this.PLUS_K,
  1111.                                                        s1,       vectorEAnR,
  1112.                                                        s2,       position,
  1113.                                                        s3,       vectorAR);
  1114.         final FieldVector3D<T> sDot = new FieldVector3D<>(sinE.negate().multiply(sqrtAoMu), this.PLUS_K,
  1115.                                                           s1,               vectorEAnRDot,
  1116.                                                           s3,               vectorARDot);
  1117.         final FieldVector3D<T> t =
  1118.             new FieldVector3D<>(sqrtRec.multiply(sinE).divide(r), this.PLUS_K).add(new FieldVector3D<>(t1, vectorEAnR,
  1119.                                                                                                        t2, position,
  1120.                                                                                                        t3, vectorAR,
  1121.                                                                                                        t4, vectorER));
  1122.         final FieldVector3D<T> tDot = new FieldVector3D<>(sqrtRec.multiply(cosE.subtract(e)).multiply(sqrtAoMu), this.PLUS_K,
  1123.                                                           t1,                                                    vectorEAnRDot,
  1124.                                                           t3,                                                    vectorARDot,
  1125.                                                           t4,                                                    vectorERDot);

  1126.         // di
  1127.         final T factorI1 = sinI.negate().multiply(sqrtRec).divide(sqrtMuA);
  1128.         final T i1 =  factorI1;
  1129.         final T i2 =  factorI1.negate().multiply(mz).divide(twoA);
  1130.         final T i3 =  factorI1.multiply(mz).multiply(e).divide(oMe2);
  1131.         final T i4 = cosI.multiply(sinPA);
  1132.         final T i5 = cosI.multiply(cosPA);
  1133.         fillHalfRow(i1, new FieldVector3D<>(vy, vx.negate(), getZero()), i2, vectorAR, i3, vectorER, i4, s, i5, t,
  1134.                     jacobian[2], 0);
  1135.         fillHalfRow(i1, new FieldVector3D<>(py.negate(), px, getZero()), i2, vectorARDot, i3, vectorERDot, i4, sDot, i5, tDot,
  1136.                     jacobian[2], 3);

  1137.         // dpa
  1138.         fillHalfRow(cosPA.divide(sinI), s,    sinPA.negate().divide(sinI), t,    jacobian[3], 0);
  1139.         fillHalfRow(cosPA.divide(sinI), sDot, sinPA.negate().divide(sinI), tDot, jacobian[3], 3);

  1140.         // dRaan
  1141.         final T factorRaanR = (a.multiply(mu).multiply(oMe2).multiply(sinI).multiply(sinI)).reciprocal();
  1142.         fillHalfRow( factorRaanR.negate().multiply(my), new FieldVector3D<>(getZero(), vz, vy.negate()),
  1143.                      factorRaanR.multiply(mx), new FieldVector3D<>(vz.negate(), getZero(),  vx),
  1144.                      jacobian[4], 0);
  1145.         fillHalfRow(factorRaanR.negate().multiply(my), new FieldVector3D<>(getZero(), pz.negate(),  py),
  1146.                      factorRaanR.multiply(mx), new FieldVector3D<>(pz, getZero(), px.negate()),
  1147.                      jacobian[4], 3);

  1148.         // dM
  1149.         fillHalfRow(rOnA, vectorEAnR,    sinE.negate(), vectorER,    jacobian[5], 0);
  1150.         fillHalfRow(rOnA, vectorEAnRDot, sinE.negate(), vectorERDot, jacobian[5], 3);

  1151.         return jacobian;

  1152.     }

  1153.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1154.      * <p>
  1155.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1156.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1157.      * yDot for j=4, zDot for j=5).
  1158.      * </p>
  1159.      * @return 6x6 Jacobian matrix
  1160.      */
  1161.     private T[][] computeJacobianMeanWrtCartesianHyperbolic() {

  1162.         final T[][] jacobian = MathArrays.buildArray(getA().getField(), 6, 6);

  1163.         // compute various intermediate parameters
  1164.         computePVWithoutA();
  1165.         final FieldVector3D<T> position = partialPV.getPosition();
  1166.         final FieldVector3D<T> velocity = partialPV.getVelocity();
  1167.         final FieldVector3D<T> momentum = partialPV.getMomentum();
  1168.         final T r2         = position.getNormSq();
  1169.         final T r          = r2.sqrt();
  1170.         final T r3         = r.multiply(r2);

  1171.         final T x          = position.getX();
  1172.         final T y          = position.getY();
  1173.         final T z          = position.getZ();
  1174.         final T vx         = velocity.getX();
  1175.         final T vy         = velocity.getY();
  1176.         final T vz         = velocity.getZ();
  1177.         final T mx         = momentum.getX();
  1178.         final T my         = momentum.getY();
  1179.         final T mz         = momentum.getZ();

  1180.         final T mu         = getMu();
  1181.         final T absA       = a.negate();
  1182.         final T sqrtMuA    = absA.multiply(mu).sqrt();
  1183.         final T a2         = a.square();
  1184.         final T rOa        = r.divide(absA);

  1185.         final FieldSinCos<T> scI = FastMath.sinCos(i);
  1186.         final T cosI       = scI.cos();
  1187.         final T sinI       = scI.sin();

  1188.         final T pv         = FieldVector3D.dotProduct(position, velocity);

  1189.         // da
  1190.         final FieldVector3D<T> vectorAR = new FieldVector3D<>(a2.multiply(-2).divide(r3), position);
  1191.         final FieldVector3D<T> vectorARDot = velocity.scalarMultiply(a2.multiply(-2).divide(mu));
  1192.         fillHalfRow(getOne().negate(), vectorAR,    jacobian[0], 0);
  1193.         fillHalfRow(getOne().negate(), vectorARDot, jacobian[0], 3);

  1194.         // differentials of the momentum
  1195.         final T m      = momentum.getNorm();
  1196.         final T oOm    = m.reciprocal();
  1197.         final FieldVector3D<T> dcXP = new FieldVector3D<>(getZero(), vz, vy.negate());
  1198.         final FieldVector3D<T> dcYP = new FieldVector3D<>(vz.negate(),   getZero(),  vx);
  1199.         final FieldVector3D<T> dcZP = new FieldVector3D<>( vy, vx.negate(),   getZero());
  1200.         final FieldVector3D<T> dcXV = new FieldVector3D<>(  getZero(),  z.negate(),   y);
  1201.         final FieldVector3D<T> dcYV = new FieldVector3D<>(  z,   getZero(),  x.negate());
  1202.         final FieldVector3D<T> dcZV = new FieldVector3D<>( y.negate(),   x,   getZero());
  1203.         final FieldVector3D<T> dCP  = new FieldVector3D<>(mx.multiply(oOm), dcXP, my.multiply(oOm), dcYP, mz.multiply(oOm), dcZP);
  1204.         final FieldVector3D<T> dCV  = new FieldVector3D<>(mx.multiply(oOm), dcXV, my.multiply(oOm), dcYV, mz.multiply(oOm), dcZV);

  1205.         // dp
  1206.         final T mOMu   = m.divide(mu);
  1207.         final FieldVector3D<T> dpP  = new FieldVector3D<>(mOMu.multiply(2), dCP);
  1208.         final FieldVector3D<T> dpV  = new FieldVector3D<>(mOMu.multiply(2), dCV);

  1209.         // de
  1210.         final T p      = m.multiply(mOMu);
  1211.         final T moO2ae = absA.multiply(2).multiply(e).reciprocal();
  1212.         final T m2OaMu = p.negate().divide(absA);
  1213.         fillHalfRow(moO2ae, dpP, m2OaMu.multiply(moO2ae), vectorAR,    jacobian[1], 0);
  1214.         fillHalfRow(moO2ae, dpV, m2OaMu.multiply(moO2ae), vectorARDot, jacobian[1], 3);

  1215.         // di
  1216.         final T cI1 = m.multiply(sinI).reciprocal();
  1217.         final T cI2 = cosI.multiply(cI1);
  1218.         fillHalfRow(cI2, dCP, cI1.negate(), dcZP, jacobian[2], 0);
  1219.         fillHalfRow(cI2, dCV, cI1.negate(), dcZV, jacobian[2], 3);


  1220.         // dPA
  1221.         final T cP1     =  y.multiply(oOm);
  1222.         final T cP2     =  x.negate().multiply(oOm);
  1223.         final T cP3     =  mx.multiply(cP1).add(my.multiply(cP2)).negate();
  1224.         final T cP4     =  cP3.multiply(oOm);
  1225.         final T cP5     =  r2.multiply(sinI).multiply(sinI).negate().reciprocal();
  1226.         final T cP6     = z.multiply(cP5);
  1227.         final T cP7     = cP3.multiply(cP5);
  1228.         final FieldVector3D<T> dacP  = new FieldVector3D<>(cP1, dcXP, cP2, dcYP, cP4, dCP, oOm, new FieldVector3D<>(my.negate(), mx, getZero()));
  1229.         final FieldVector3D<T> dacV  = new FieldVector3D<>(cP1, dcXV, cP2, dcYV, cP4, dCV);
  1230.         final FieldVector3D<T> dpoP  = new FieldVector3D<>(cP6, dacP, cP7, this.PLUS_K);
  1231.         final FieldVector3D<T> dpoV  = new FieldVector3D<>(cP6, dacV);

  1232.         final T re2     = r2.multiply(e.square());
  1233.         final T recOre2 = p.subtract(r).divide(re2);
  1234.         final T resOre2 = pv.multiply(mOMu).divide(re2);
  1235.         final FieldVector3D<T> dreP  = new FieldVector3D<>(mOMu, velocity, pv.divide(mu), dCP);
  1236.         final FieldVector3D<T> dreV  = new FieldVector3D<>(mOMu, position, pv.divide(mu), dCV);
  1237.         final FieldVector3D<T> davP  = new FieldVector3D<>(resOre2.negate(), dpP, recOre2, dreP, resOre2.divide(r), position);
  1238.         final FieldVector3D<T> davV  = new FieldVector3D<>(resOre2.negate(), dpV, recOre2, dreV);
  1239.         fillHalfRow(getOne(), dpoP, getOne().negate(), davP, jacobian[3], 0);
  1240.         fillHalfRow(getOne(), dpoV, getOne().negate(), davV, jacobian[3], 3);

  1241.         // dRAAN
  1242.         final T cO0 = cI1.square();
  1243.         final T cO1 =  mx.multiply(cO0);
  1244.         final T cO2 =  my.negate().multiply(cO0);
  1245.         fillHalfRow(cO1, dcYP, cO2, dcXP, jacobian[4], 0);
  1246.         fillHalfRow(cO1, dcYV, cO2, dcXV, jacobian[4], 3);

  1247.         // dM
  1248.         final T s2a    = pv.divide(absA.multiply(2));
  1249.         final T oObux  = m.square().add(absA.multiply(mu)).sqrt().reciprocal();
  1250.         final T scasbu = pv.multiply(oObux);
  1251.         final FieldVector3D<T> dauP = new FieldVector3D<>(sqrtMuA.reciprocal(), velocity, s2a.negate().divide(sqrtMuA), vectorAR);
  1252.         final FieldVector3D<T> dauV = new FieldVector3D<>(sqrtMuA.reciprocal(), position, s2a.negate().divide(sqrtMuA), vectorARDot);
  1253.         final FieldVector3D<T> dbuP = new FieldVector3D<>(oObux.multiply(mu.divide(2.)), vectorAR,    m.multiply(oObux), dCP);
  1254.         final FieldVector3D<T> dbuV = new FieldVector3D<>(oObux.multiply(mu.divide(2.)), vectorARDot, m.multiply(oObux), dCV);
  1255.         final FieldVector3D<T> dcuP = new FieldVector3D<>(oObux, velocity, scasbu.negate().multiply(oObux), dbuP);
  1256.         final FieldVector3D<T> dcuV = new FieldVector3D<>(oObux, position, scasbu.negate().multiply(oObux), dbuV);
  1257.         fillHalfRow(getOne(), dauP, e.negate().divide(rOa.add(1)), dcuP, jacobian[5], 0);
  1258.         fillHalfRow(getOne(), dauV, e.negate().divide(rOa.add(1)), dcuV, jacobian[5], 3);

  1259.         return jacobian;

  1260.     }

  1261.     /** {@inheritDoc} */
  1262.     @Override
  1263.     protected T[][] computeJacobianEccentricWrtCartesian() {
  1264.         if (isElliptical()) {
  1265.             return computeJacobianEccentricWrtCartesianElliptical();
  1266.         } else {
  1267.             return computeJacobianEccentricWrtCartesianHyperbolic();
  1268.         }
  1269.     }

  1270.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1271.      * <p>
  1272.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1273.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1274.      * yDot for j=4, zDot for j=5).
  1275.      * </p>
  1276.      * @return 6x6 Jacobian matrix
  1277.      */
  1278.     private T[][] computeJacobianEccentricWrtCartesianElliptical() {

  1279.         // start by computing the Jacobian with mean angle
  1280.         final T[][] jacobian = computeJacobianMeanWrtCartesianElliptical();

  1281.         // Differentiating the Kepler equation M = E - e sin E leads to:
  1282.         // dM = (1 - e cos E) dE - sin E de
  1283.         // which is inverted and rewritten as:
  1284.         // dE = a/r dM + sin E a/r de
  1285.         final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  1286.         final T aOr              = e.negate().multiply(scE.cos()).add(1).reciprocal();

  1287.         // update anomaly row
  1288.         final T[] eRow           = jacobian[1];
  1289.         final T[] anomalyRow     = jacobian[5];
  1290.         for (int j = 0; j < anomalyRow.length; ++j) {
  1291.             anomalyRow[j] = aOr.multiply(anomalyRow[j].add(scE.sin().multiply(eRow[j])));
  1292.         }

  1293.         return jacobian;

  1294.     }

  1295.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1296.      * <p>
  1297.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1298.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1299.      * yDot for j=4, zDot for j=5).
  1300.      * </p>
  1301.      * @return 6x6 Jacobian matrix
  1302.      */
  1303.     private T[][] computeJacobianEccentricWrtCartesianHyperbolic() {

  1304.         // start by computing the Jacobian with mean angle
  1305.         final T[][] jacobian = computeJacobianMeanWrtCartesianHyperbolic();

  1306.         // Differentiating the Kepler equation M = e sinh H - H leads to:
  1307.         // dM = (e cosh H - 1) dH + sinh H de
  1308.         // which is inverted and rewritten as:
  1309.         // dH = 1 / (e cosh H - 1) dM - sinh H / (e cosh H - 1) de
  1310.         final T H      = getEccentricAnomaly();
  1311.         final T coshH  = H.cosh();
  1312.         final T sinhH  = H.sinh();
  1313.         final T absaOr = e.multiply(coshH).subtract(1).reciprocal();
  1314.         // update anomaly row
  1315.         final T[] eRow       = jacobian[1];
  1316.         final T[] anomalyRow = jacobian[5];

  1317.         for (int j = 0; j < anomalyRow.length; ++j) {
  1318.             anomalyRow[j] = absaOr.multiply(anomalyRow[j].subtract(sinhH.multiply(eRow[j])));

  1319.         }

  1320.         return jacobian;

  1321.     }

  1322.     /** {@inheritDoc} */
  1323.     @Override
  1324.     protected T[][] computeJacobianTrueWrtCartesian() {
  1325.         if (isElliptical()) {
  1326.             return computeJacobianTrueWrtCartesianElliptical();
  1327.         } else {
  1328.             return computeJacobianTrueWrtCartesianHyperbolic();
  1329.         }
  1330.     }

  1331.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1332.      * <p>
  1333.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1334.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1335.      * yDot for j=4, zDot for j=5).
  1336.      * </p>
  1337.      * @return 6x6 Jacobian matrix
  1338.      */
  1339.     private T[][] computeJacobianTrueWrtCartesianElliptical() {

  1340.         // start by computing the Jacobian with eccentric angle
  1341.         final T[][] jacobian = computeJacobianEccentricWrtCartesianElliptical();
  1342.         // Differentiating the eccentric anomaly equation sin E = sqrt(1-e^2) sin v / (1 + e cos v)
  1343.         // and using cos E = (e + cos v) / (1 + e cos v) to get rid of cos E leads to:
  1344.         // dE = [sqrt (1 - e^2) / (1 + e cos v)] dv - [sin E / (1 - e^2)] de
  1345.         // which is inverted and rewritten as:
  1346.         // dv = sqrt (1 - e^2) a/r dE + [sin E / sqrt (1 - e^2)] a/r de
  1347.         final T e2               = e.square();
  1348.         final T oMe2             = e2.negate().add(1);
  1349.         final T epsilon          = oMe2.sqrt();
  1350.         final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  1351.         final T aOr              = e.multiply(scE.cos()).negate().add(1).reciprocal();
  1352.         final T aFactor          = epsilon.multiply(aOr);
  1353.         final T eFactor          = scE.sin().multiply(aOr).divide(epsilon);

  1354.         // update anomaly row
  1355.         final T[] eRow           = jacobian[1];
  1356.         final T[] anomalyRow     = jacobian[5];
  1357.         for (int j = 0; j < anomalyRow.length; ++j) {
  1358.             anomalyRow[j] = aFactor.multiply(anomalyRow[j]).add(eFactor.multiply(eRow[j]));
  1359.         }
  1360.         return jacobian;

  1361.     }

  1362.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1363.      * <p>
  1364.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1365.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1366.      * yDot for j=4, zDot for j=5).
  1367.      * </p>
  1368.      * @return 6x6 Jacobian matrix
  1369.      */
  1370.     private T[][] computeJacobianTrueWrtCartesianHyperbolic() {

  1371.         // start by computing the Jacobian with eccentric angle
  1372.         final T[][] jacobian = computeJacobianEccentricWrtCartesianHyperbolic();

  1373.         // Differentiating the eccentric anomaly equation sinh H = sqrt(e^2-1) sin v / (1 + e cos v)
  1374.         // and using cosh H = (e + cos v) / (1 + e cos v) to get rid of cosh H leads to:
  1375.         // dH = [sqrt (e^2 - 1) / (1 + e cos v)] dv + [sinh H / (e^2 - 1)] de
  1376.         // which is inverted and rewritten as:
  1377.         // dv = sqrt (1 - e^2) a/r dH - [sinh H / sqrt (e^2 - 1)] a/r de
  1378.         final T e2       = e.square();
  1379.         final T e2Mo     = e2.subtract(1);
  1380.         final T epsilon  = e2Mo.sqrt();
  1381.         final T H        = getEccentricAnomaly();
  1382.         final T coshH    = H.cosh();
  1383.         final T sinhH    = H.sinh();
  1384.         final T aOr      = e.multiply(coshH).subtract(1).reciprocal();
  1385.         final T aFactor  = epsilon.multiply(aOr);
  1386.         final T eFactor  = sinhH.multiply(aOr).divide(epsilon);

  1387.         // update anomaly row
  1388.         final T[] eRow           = jacobian[1];
  1389.         final T[] anomalyRow     = jacobian[5];
  1390.         for (int j = 0; j < anomalyRow.length; ++j) {
  1391.             anomalyRow[j] = aFactor.multiply(anomalyRow[j]).subtract(eFactor.multiply(eRow[j]));
  1392.         }

  1393.         return jacobian;

  1394.     }

  1395.     /** {@inheritDoc} */
  1396.     @Override
  1397.     public void addKeplerContribution(final PositionAngleType type, final T gm,
  1398.                                       final T[] pDot) {
  1399.         pDot[5] = pDot[5].add(computeKeplerianAnomalyDot(type, a, e, gm, cachedAnomaly, cachedPositionAngleType));
  1400.     }

  1401.     /**
  1402.      * Compute rate of argument of latitude.
  1403.      * @param type position angle type of rate
  1404.      * @param a semi major axis
  1405.      * @param e eccentricity
  1406.      * @param mu mu
  1407.      * @param anomaly anomaly
  1408.      * @param cachedType position angle type of passed anomaly
  1409.      * @param <T> field type
  1410.      * @return first-order time derivative for anomaly
  1411.      * @since 12.2
  1412.      */
  1413.     private static <T extends CalculusFieldElement<T>> T computeKeplerianAnomalyDot(final PositionAngleType type, final T a, final T e,
  1414.                                                                                     final T mu, final T anomaly, final PositionAngleType cachedType) {
  1415.         final T absA = a.abs();
  1416.         final T n    = absA.reciprocal().multiply(mu).sqrt().divide(absA);
  1417.         if (type == PositionAngleType.MEAN) {
  1418.             return n;
  1419.         }
  1420.         final T ksi;
  1421.         final T oMe2;
  1422.         final T trueAnomaly = FieldKeplerianAnomalyUtility.convertAnomaly(cachedType, anomaly, e, PositionAngleType.TRUE);
  1423.         if (type == PositionAngleType.ECCENTRIC) {
  1424.             oMe2 = e.square().negate().add(1).abs();
  1425.             ksi = e.multiply(trueAnomaly.cos()).add(1);
  1426.             return n.multiply(ksi).divide(oMe2);
  1427.         } else {
  1428.             oMe2 = e.square().negate().add(1).abs();
  1429.             ksi  = e.multiply(trueAnomaly.cos()).add(1);
  1430.             return n.multiply(ksi).multiply(ksi).divide(oMe2.multiply(oMe2.sqrt()));
  1431.         }
  1432.     }

  1433.     /**  Returns a string representation of this Keplerian parameters object.
  1434.      * @return a string representation of this object
  1435.      */
  1436.     public String toString() {
  1437.         return new StringBuilder().append("Keplerian parameters: ").append('{').
  1438.                                   append("a: ").append(a.getReal()).
  1439.                                   append("; e: ").append(e.getReal()).
  1440.                                   append("; i: ").append(FastMath.toDegrees(i.getReal())).
  1441.                                   append("; pa: ").append(FastMath.toDegrees(pa.getReal())).
  1442.                                   append("; raan: ").append(FastMath.toDegrees(raan.getReal())).
  1443.                                   append("; v: ").append(FastMath.toDegrees(getTrueAnomaly().getReal())).
  1444.                                   append(";}").toString();
  1445.     }

  1446.     /** {@inheritDoc} */
  1447.     @Override
  1448.     public PositionAngleType getCachedPositionAngleType() {
  1449.         return cachedPositionAngleType;
  1450.     }

  1451.     /** {@inheritDoc} */
  1452.     @Override
  1453.     public boolean hasRates() {
  1454.         return hasDerivatives();
  1455.     }

  1456.     /** {@inheritDoc} */
  1457.     @Override
  1458.     public FieldKeplerianOrbit<T> removeRates() {
  1459.         return new FieldKeplerianOrbit<>(getA(), getE(), getI(), getPerigeeArgument(), getRightAscensionOfAscendingNode(),
  1460.                 cachedAnomaly, cachedPositionAngleType, getFrame(), getDate(), getMu());
  1461.     }

  1462.     /** Check if the given parameter is within an acceptable range.
  1463.      * The bounds are inclusive: an exception is raised when either of those conditions are met:
  1464.      * <ul>
  1465.      *     <li>The parameter is strictly greater than upperBound</li>
  1466.      *     <li>The parameter is strictly lower than lowerBound</li>
  1467.      * </ul>
  1468.      * <p>
  1469.      * In either of these cases, an OrekitException is raised.
  1470.      * </p>
  1471.      * @param parameterName name of the parameter
  1472.      * @param parameter value of the parameter
  1473.      * @param lowerBound lower bound of the acceptable range (inclusive)
  1474.      * @param upperBound upper bound of the acceptable range (inclusive)
  1475.      */
  1476.     private void checkParameterRangeInclusive(final String parameterName, final double parameter,
  1477.                                               final double lowerBound, final double upperBound) {
  1478.         if (parameter < lowerBound || parameter > upperBound) {
  1479.             throw new OrekitException(OrekitMessages.INVALID_PARAMETER_RANGE, parameterName,
  1480.                                       parameter, lowerBound, upperBound);
  1481.         }
  1482.     }

  1483.     /** {@inheritDoc} */
  1484.     @Override
  1485.     public KeplerianOrbit toOrbit() {
  1486.         final double cachedPositionAngle = cachedAnomaly.getReal();
  1487.         if (hasDerivatives()) {
  1488.             return new KeplerianOrbit(a.getReal(), e.getReal(), i.getReal(),
  1489.                                       pa.getReal(), raan.getReal(), cachedPositionAngle,
  1490.                                       aDot.getReal(), eDot.getReal(), iDot.getReal(),
  1491.                                       paDot.getReal(), raanDot.getReal(),
  1492.                                       cachedAnomalyDot.getReal(),
  1493.                                       cachedPositionAngleType, cachedPositionAngleType,
  1494.                                       getFrame(), getDate().toAbsoluteDate(), getMu().getReal());
  1495.         } else {
  1496.             return new KeplerianOrbit(a.getReal(), e.getReal(), i.getReal(),
  1497.                                       pa.getReal(), raan.getReal(), cachedPositionAngle,
  1498.                                       cachedPositionAngleType, cachedPositionAngleType,
  1499.                                       getFrame(), getDate().toAbsoluteDate(), getMu().getReal());
  1500.         }
  1501.     }


  1502. }