EquinoctialOrbit.java

  1. /* Copyright 2002-2023 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 org.hipparchus.analysis.differentiation.UnivariateDerivative1;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.SinCos;
  23. import org.orekit.annotation.DefaultDataContext;
  24. import org.orekit.data.DataContext;
  25. import org.orekit.errors.OrekitIllegalArgumentException;
  26. import org.orekit.errors.OrekitInternalError;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.PVCoordinates;
  31. import org.orekit.utils.TimeStampedPVCoordinates;


  32. /**
  33.  * This class handles equinoctial orbital parameters, which can support both
  34.  * circular and equatorial orbits.
  35.  * <p>
  36.  * The parameters used internally are the equinoctial elements which can be
  37.  * related to Keplerian elements as follows:
  38.  *   <pre>
  39.  *     a
  40.  *     ex = e cos(ω + Ω)
  41.  *     ey = e sin(ω + Ω)
  42.  *     hx = tan(i/2) cos(Ω)
  43.  *     hy = tan(i/2) sin(Ω)
  44.  *     lv = v + ω + Ω
  45.  *   </pre>
  46.  * where ω stands for the Perigee Argument and Ω stands for the
  47.  * Right Ascension of the Ascending Node.
  48.  *
  49.  * <p>
  50.  * The conversion equations from and to Keplerian elements given above hold only
  51.  * when both sides are unambiguously defined, i.e. when orbit is neither equatorial
  52.  * nor circular. When orbit is either equatorial or circular, the equinoctial
  53.  * parameters are still unambiguously defined whereas some Keplerian elements
  54.  * (more precisely ω and Ω) become ambiguous. For this reason, equinoctial
  55.  * parameters are the recommended way to represent orbits. Note however than
  56.  *  * the present implementation does not handle non-elliptical cases.
  57.  * </p>
  58.  * <p>
  59.  * The instance <code>EquinoctialOrbit</code> is guaranteed to be immutable.
  60.  * </p>
  61.  * @see    Orbit
  62.  * @see    KeplerianOrbit
  63.  * @see    CircularOrbit
  64.  * @see    CartesianOrbit
  65.  * @author Mathieu Rom&eacute;ro
  66.  * @author Luc Maisonobe
  67.  * @author Guylaine Prat
  68.  * @author Fabien Maussion
  69.  * @author V&eacute;ronique Pommier-Maurussane
  70.  */
  71. public class EquinoctialOrbit extends Orbit implements PositionAngleBased {

  72.     /** Serializable UID. */
  73.     private static final long serialVersionUID = 20170414L;

  74.     /** Semi-major axis (m). */
  75.     private final double a;

  76.     /** First component of the eccentricity vector. */
  77.     private final double ex;

  78.     /** Second component of the eccentricity vector. */
  79.     private final double ey;

  80.     /** First component of the inclination vector. */
  81.     private final double hx;

  82.     /** Second component of the inclination vector. */
  83.     private final double hy;

  84.     /** True longitude argument (rad). */
  85.     private final double lv;

  86.     /** Semi-major axis derivative (m/s). */
  87.     private final double aDot;

  88.     /** First component of the eccentricity vector derivative. */
  89.     private final double exDot;

  90.     /** Second component of the eccentricity vector derivative. */
  91.     private final double eyDot;

  92.     /** First component of the inclination vector derivative. */
  93.     private final double hxDot;

  94.     /** Second component of the inclination vector derivative. */
  95.     private final double hyDot;

  96.     /** True longitude argument derivative (rad/s). */
  97.     private final double lvDot;

  98.     /** Partial Cartesian coordinates (position and velocity are valid, acceleration may be missing). */
  99.     private transient PVCoordinates partialPV;

  100.     /** Creates a new instance.
  101.      * @param a  semi-major axis (m)
  102.      * @param ex e cos(ω + Ω), first component of eccentricity vector
  103.      * @param ey e sin(ω + Ω), second component of eccentricity vector
  104.      * @param hx tan(i/2) cos(Ω), first component of inclination vector
  105.      * @param hy tan(i/2) sin(Ω), second component of inclination vector
  106.      * @param l  (M or E or v) + ω + Ω, mean, eccentric or true longitude argument (rad)
  107.      * @param type type of longitude argument
  108.      * @param frame the frame in which the parameters are defined
  109.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  110.      * @param date date of the orbital parameters
  111.      * @param mu central attraction coefficient (m³/s²)
  112.      * @exception IllegalArgumentException if eccentricity is equal to 1 or larger or
  113.      * if frame is not a {@link Frame#isPseudoInertial pseudo-inertial frame}
  114.      */
  115.     public EquinoctialOrbit(final double a, final double ex, final double ey,
  116.                             final double hx, final double hy, final double l,
  117.                             final PositionAngleType type,
  118.                             final Frame frame, final AbsoluteDate date, final double mu)
  119.         throws IllegalArgumentException {
  120.         this(a, ex, ey, hx, hy, l,
  121.              Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN,
  122.              type, frame, date, mu);
  123.     }

  124.     /** Creates a new instance.
  125.      * @param a  semi-major axis (m)
  126.      * @param ex e cos(ω + Ω), first component of eccentricity vector
  127.      * @param ey e sin(ω + Ω), second component of eccentricity vector
  128.      * @param hx tan(i/2) cos(Ω), first component of inclination vector
  129.      * @param hy tan(i/2) sin(Ω), second component of inclination vector
  130.      * @param l  (M or E or v) + ω + Ω, mean, eccentric or true longitude argument (rad)
  131.      * @param aDot  semi-major axis derivative (m/s)
  132.      * @param exDot d(e cos(ω + Ω))/dt, first component of eccentricity vector derivative
  133.      * @param eyDot d(e sin(ω + Ω))/dt, second component of eccentricity vector derivative
  134.      * @param hxDot d(tan(i/2) cos(Ω))/dt, first component of inclination vector derivative
  135.      * @param hyDot d(tan(i/2) sin(Ω))/dt, second component of inclination vector derivative
  136.      * @param lDot  d(M or E or v) + ω + Ω)/dr, mean, eccentric or true longitude argument  derivative (rad/s)
  137.      * @param type type of longitude argument
  138.      * @param frame the frame in which the parameters are defined
  139.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  140.      * @param date date of the orbital parameters
  141.      * @param mu central attraction coefficient (m³/s²)
  142.      * @exception IllegalArgumentException if eccentricity is equal to 1 or larger or
  143.      * if frame is not a {@link Frame#isPseudoInertial pseudo-inertial frame}
  144.      */
  145.     public EquinoctialOrbit(final double a, final double ex, final double ey,
  146.                             final double hx, final double hy, final double l,
  147.                             final double aDot, final double exDot, final double eyDot,
  148.                             final double hxDot, final double hyDot, final double lDot,
  149.                             final PositionAngleType type,
  150.                             final Frame frame, final AbsoluteDate date, final double mu)
  151.         throws IllegalArgumentException {
  152.         super(frame, date, mu);
  153.         if (ex * ex + ey * ey >= 1.0) {
  154.             throw new OrekitIllegalArgumentException(OrekitMessages.HYPERBOLIC_ORBIT_NOT_HANDLED_AS,
  155.                                                      getClass().getName());
  156.         }
  157.         this.a     = a;
  158.         this.aDot  = aDot;
  159.         this.ex    = ex;
  160.         this.exDot = exDot;
  161.         this.ey    = ey;
  162.         this.eyDot = eyDot;
  163.         this.hx    = hx;
  164.         this.hxDot = hxDot;
  165.         this.hy    = hy;
  166.         this.hyDot = hyDot;

  167.         if (hasDerivatives()) {
  168.             final UnivariateDerivative1 exUD = new UnivariateDerivative1(ex, exDot);
  169.             final UnivariateDerivative1 eyUD = new UnivariateDerivative1(ey, eyDot);
  170.             final UnivariateDerivative1 lUD  = new UnivariateDerivative1(l,  lDot);
  171.             final UnivariateDerivative1 lvUD;
  172.             switch (type) {
  173.                 case MEAN :
  174.                     lvUD = FieldEquinoctialOrbit.eccentricToTrue(FieldEquinoctialOrbit.meanToEccentric(lUD, exUD, eyUD), exUD, eyUD);
  175.                     break;
  176.                 case ECCENTRIC :
  177.                     lvUD = FieldEquinoctialOrbit.eccentricToTrue(lUD, exUD, eyUD);
  178.                     break;
  179.                 case TRUE :
  180.                     lvUD = lUD;
  181.                     break;
  182.                 default : // this should never happen
  183.                     throw new OrekitInternalError(null);
  184.             }
  185.             this.lv    = lvUD.getValue();
  186.             this.lvDot = lvUD.getDerivative(1);
  187.         } else {
  188.             switch (type) {
  189.                 case MEAN :
  190.                     this.lv = eccentricToTrue(meanToEccentric(l, ex, ey), ex, ey);
  191.                     break;
  192.                 case ECCENTRIC :
  193.                     this.lv = eccentricToTrue(l, ex, ey);
  194.                     break;
  195.                 case TRUE :
  196.                     this.lv = l;
  197.                     break;
  198.                 default : // this should never happen
  199.                     throw new OrekitInternalError(null);
  200.             }
  201.             this.lvDot = Double.NaN;
  202.         }

  203.         this.partialPV = null;

  204.     }

  205.     /** Constructor from Cartesian parameters.
  206.      *
  207.      * <p> The acceleration provided in {@code pvCoordinates} is accessible using
  208.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  209.      * use {@code mu} and the position to compute the acceleration, including
  210.      * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
  211.      *
  212.      * @param pvCoordinates the position, velocity and acceleration
  213.      * @param frame the frame in which are defined the {@link PVCoordinates}
  214.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  215.      * @param mu central attraction coefficient (m³/s²)
  216.      * @exception IllegalArgumentException if eccentricity is equal to 1 or larger or
  217.      * if frame is not a {@link Frame#isPseudoInertial pseudo-inertial frame}
  218.      */
  219.     public EquinoctialOrbit(final TimeStampedPVCoordinates pvCoordinates,
  220.                             final Frame frame, final double mu)
  221.         throws IllegalArgumentException {
  222.         super(pvCoordinates, frame, mu);

  223.         //  compute semi-major axis
  224.         final Vector3D pvP   = pvCoordinates.getPosition();
  225.         final Vector3D pvV   = pvCoordinates.getVelocity();
  226.         final Vector3D pvA   = pvCoordinates.getAcceleration();
  227.         final double r2      = pvP.getNormSq();
  228.         final double r       = FastMath.sqrt(r2);
  229.         final double V2      = pvV.getNormSq();
  230.         final double rV2OnMu = r * V2 / mu;

  231.         // compute semi-major axis
  232.         a = r / (2 - rV2OnMu);

  233.         if (!isElliptical()) {
  234.             throw new OrekitIllegalArgumentException(OrekitMessages.HYPERBOLIC_ORBIT_NOT_HANDLED_AS,
  235.                                                      getClass().getName());
  236.         }

  237.         // compute inclination vector
  238.         final Vector3D w = pvCoordinates.getMomentum().normalize();
  239.         final double d = 1.0 / (1 + w.getZ());
  240.         hx = -d * w.getY();
  241.         hy =  d * w.getX();

  242.         // compute true longitude argument
  243.         final double cLv = (pvP.getX() - d * pvP.getZ() * w.getX()) / r;
  244.         final double sLv = (pvP.getY() - d * pvP.getZ() * w.getY()) / r;
  245.         lv = FastMath.atan2(sLv, cLv);

  246.         // compute eccentricity vector
  247.         final double eSE = Vector3D.dotProduct(pvP, pvV) / FastMath.sqrt(mu * a);
  248.         final double eCE = rV2OnMu - 1;
  249.         final double e2  = eCE * eCE + eSE * eSE;
  250.         final double f   = eCE - e2;
  251.         final double g   = FastMath.sqrt(1 - e2) * eSE;
  252.         ex = a * (f * cLv + g * sLv) / r;
  253.         ey = a * (f * sLv - g * cLv) / r;

  254.         partialPV = pvCoordinates;

  255.         if (hasNonKeplerianAcceleration(pvCoordinates, mu)) {
  256.             // we have a relevant acceleration, we can compute derivatives

  257.             final double[][] jacobian = new double[6][6];
  258.             getJacobianWrtCartesian(PositionAngleType.MEAN, jacobian);

  259.             final Vector3D keplerianAcceleration    = new Vector3D(-mu / (r * r2), pvP);
  260.             final Vector3D nonKeplerianAcceleration = pvA.subtract(keplerianAcceleration);
  261.             final double   aX                       = nonKeplerianAcceleration.getX();
  262.             final double   aY                       = nonKeplerianAcceleration.getY();
  263.             final double   aZ                       = nonKeplerianAcceleration.getZ();
  264.             aDot  = jacobian[0][3] * aX + jacobian[0][4] * aY + jacobian[0][5] * aZ;
  265.             exDot = jacobian[1][3] * aX + jacobian[1][4] * aY + jacobian[1][5] * aZ;
  266.             eyDot = jacobian[2][3] * aX + jacobian[2][4] * aY + jacobian[2][5] * aZ;
  267.             hxDot = jacobian[3][3] * aX + jacobian[3][4] * aY + jacobian[3][5] * aZ;
  268.             hyDot = jacobian[4][3] * aX + jacobian[4][4] * aY + jacobian[4][5] * aZ;

  269.             // in order to compute true anomaly derivative, we must compute
  270.             // mean anomaly derivative including Keplerian motion and convert to true anomaly
  271.             final double lMDot = getKeplerianMeanMotion() +
  272.                                  jacobian[5][3] * aX + jacobian[5][4] * aY + jacobian[5][5] * aZ;
  273.             final UnivariateDerivative1 exUD = new UnivariateDerivative1(ex, exDot);
  274.             final UnivariateDerivative1 eyUD = new UnivariateDerivative1(ey, eyDot);
  275.             final UnivariateDerivative1 lMUD = new UnivariateDerivative1(getLM(), lMDot);
  276.             final UnivariateDerivative1 lvUD = FieldEquinoctialOrbit.eccentricToTrue(FieldEquinoctialOrbit.meanToEccentric(lMUD, exUD, eyUD), exUD, eyUD);
  277.             lvDot = lvUD.getDerivative(1);

  278.         } else {
  279.             // acceleration is either almost zero or NaN,
  280.             // we assume acceleration was not known
  281.             // we don't set up derivatives
  282.             aDot  = Double.NaN;
  283.             exDot = Double.NaN;
  284.             eyDot = Double.NaN;
  285.             hxDot = Double.NaN;
  286.             hyDot = Double.NaN;
  287.             lvDot = Double.NaN;
  288.         }

  289.     }

  290.     /** Constructor from Cartesian parameters.
  291.      *
  292.      * <p> The acceleration provided in {@code pvCoordinates} is accessible using
  293.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  294.      * use {@code mu} and the position to compute the acceleration, including
  295.      * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
  296.      *
  297.      * @param pvCoordinates the position end velocity
  298.      * @param frame the frame in which are defined the {@link PVCoordinates}
  299.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  300.      * @param date date of the orbital parameters
  301.      * @param mu central attraction coefficient (m³/s²)
  302.      * @exception IllegalArgumentException if eccentricity is equal to 1 or larger or
  303.      * if frame is not a {@link Frame#isPseudoInertial pseudo-inertial frame}
  304.      */
  305.     public EquinoctialOrbit(final PVCoordinates pvCoordinates, final Frame frame,
  306.                             final AbsoluteDate date, final double mu)
  307.         throws IllegalArgumentException {
  308.         this(new TimeStampedPVCoordinates(date, pvCoordinates), frame, mu);
  309.     }

  310.     /** Constructor from any kind of orbital parameters.
  311.      * @param op orbital parameters to copy
  312.      */
  313.     public EquinoctialOrbit(final Orbit op) {
  314.         super(op.getFrame(), op.getDate(), op.getMu());
  315.         a         = op.getA();
  316.         aDot      = op.getADot();
  317.         ex        = op.getEquinoctialEx();
  318.         exDot     = op.getEquinoctialExDot();
  319.         ey        = op.getEquinoctialEy();
  320.         eyDot     = op.getEquinoctialEyDot();
  321.         hx        = op.getHx();
  322.         hxDot     = op.getHxDot();
  323.         hy        = op.getHy();
  324.         hyDot     = op.getHyDot();
  325.         lv        = op.getLv();
  326.         lvDot     = op.getLvDot();
  327.         partialPV = null;
  328.     }

  329.     /** {@inheritDoc} */
  330.     public OrbitType getType() {
  331.         return OrbitType.EQUINOCTIAL;
  332.     }

  333.     /** {@inheritDoc} */
  334.     public double getA() {
  335.         return a;
  336.     }

  337.     /** {@inheritDoc} */
  338.     public double getADot() {
  339.         return aDot;
  340.     }

  341.     /** {@inheritDoc} */
  342.     public double getEquinoctialEx() {
  343.         return ex;
  344.     }

  345.     /** {@inheritDoc} */
  346.     public double getEquinoctialExDot() {
  347.         return exDot;
  348.     }

  349.     /** {@inheritDoc} */
  350.     public double getEquinoctialEy() {
  351.         return ey;
  352.     }

  353.     /** {@inheritDoc} */
  354.     public double getEquinoctialEyDot() {
  355.         return eyDot;
  356.     }

  357.     /** {@inheritDoc} */
  358.     public double getHx() {
  359.         return hx;
  360.     }

  361.     /** {@inheritDoc} */
  362.     public double getHxDot() {
  363.         return hxDot;
  364.     }

  365.     /** {@inheritDoc} */
  366.     public double getHy() {
  367.         return hy;
  368.     }

  369.     /** {@inheritDoc} */
  370.     public double getHyDot() {
  371.         return hyDot;
  372.     }

  373.     /** {@inheritDoc} */
  374.     public double getLv() {
  375.         return lv;
  376.     }

  377.     /** {@inheritDoc} */
  378.     public double getLvDot() {
  379.         return lvDot;
  380.     }

  381.     /** {@inheritDoc} */
  382.     public double getLE() {
  383.         return trueToEccentric(lv, ex, ey);
  384.     }

  385.     /** {@inheritDoc} */
  386.     public double getLEDot() {
  387.         final UnivariateDerivative1 lVUD = new UnivariateDerivative1(lv, lvDot);
  388.         final UnivariateDerivative1 exUD = new UnivariateDerivative1(ex, exDot);
  389.         final UnivariateDerivative1 eyUD = new UnivariateDerivative1(ey, eyDot);
  390.         final UnivariateDerivative1 lEUD = FieldEquinoctialOrbit.trueToEccentric(lVUD, exUD, eyUD);
  391.         return lEUD.getDerivative(1);
  392.     }

  393.     /** {@inheritDoc} */
  394.     public double getLM() {
  395.         return eccentricToMean(trueToEccentric(lv, ex, ey), ex, ey);
  396.     }

  397.     /** {@inheritDoc} */
  398.     public double getLMDot() {
  399.         final UnivariateDerivative1 lVUD = new UnivariateDerivative1(lv, lvDot);
  400.         final UnivariateDerivative1 exUD = new UnivariateDerivative1(ex, exDot);
  401.         final UnivariateDerivative1 eyUD = new UnivariateDerivative1(ey, eyDot);
  402.         final UnivariateDerivative1 lMUD = FieldEquinoctialOrbit.eccentricToMean(FieldEquinoctialOrbit.trueToEccentric(lVUD, exUD, eyUD), exUD, eyUD);
  403.         return lMUD.getDerivative(1);
  404.     }

  405.     /** Get the longitude argument.
  406.      * @param type type of the angle
  407.      * @return longitude argument (rad)
  408.      */
  409.     public double getL(final PositionAngleType type) {
  410.         return (type == PositionAngleType.MEAN) ? getLM() :
  411.                                               ((type == PositionAngleType.ECCENTRIC) ? getLE() :
  412.                                                                                    getLv());
  413.     }

  414.     /** Get the longitude argument derivative.
  415.      * @param type type of the angle
  416.      * @return longitude argument derivative (rad/s)
  417.      */
  418.     public double getLDot(final PositionAngleType type) {
  419.         return (type == PositionAngleType.MEAN) ? getLMDot() :
  420.                                               ((type == PositionAngleType.ECCENTRIC) ? getLEDot() :
  421.                                                                                    getLvDot());
  422.     }

  423.     /** Computes the true longitude argument from the eccentric longitude argument.
  424.      * @param lE = E + ω + Ω eccentric longitude argument (rad)
  425.      * @param ex first component of the eccentricity vector
  426.      * @param ey second component of the eccentricity vector
  427.      * @return the true longitude argument
  428.      */
  429.     public static double eccentricToTrue(final double lE, final double ex, final double ey) {
  430.         final double epsilon = FastMath.sqrt(1 - ex * ex - ey * ey);
  431.         final SinCos scLE    = FastMath.sinCos(lE);
  432.         final double num     = ex * scLE.sin() - ey * scLE.cos();
  433.         final double den     = epsilon + 1 - ex * scLE.cos() - ey * scLE.sin();
  434.         return lE + 2 * FastMath.atan(num / den);
  435.     }

  436.     /** Computes the eccentric longitude argument from the true longitude argument.
  437.      * @param lv = v + ω + Ω true longitude argument (rad)
  438.      * @param ex first component of the eccentricity vector
  439.      * @param ey second component of the eccentricity vector
  440.      * @return the eccentric longitude argument
  441.      */
  442.     public static double trueToEccentric(final double lv, final double ex, final double ey) {
  443.         final double epsilon = FastMath.sqrt(1 - ex * ex - ey * ey);
  444.         final SinCos scLv    = FastMath.sinCos(lv);
  445.         final double num     = ey * scLv.cos() - ex * scLv.sin();
  446.         final double den     = epsilon + 1 + ex * scLv.cos() + ey * scLv.sin();
  447.         return lv + 2 * FastMath.atan(num / den);
  448.     }

  449.     /** Computes the eccentric longitude argument from the mean longitude argument.
  450.      * @param lM = M + ω + Ω mean longitude argument (rad)
  451.      * @param ex first component of the eccentricity vector
  452.      * @param ey second component of the eccentricity vector
  453.      * @return the eccentric longitude argument
  454.      */
  455.     public static double meanToEccentric(final double lM, final double ex, final double ey) {
  456.         // Generalization of Kepler equation to equinoctial parameters
  457.         // with lE = PA + RAAN + E and
  458.         //      lM = PA + RAAN + M = lE - ex.sin(lE) + ey.cos(lE)
  459.         double lE = lM;
  460.         double shift = 0.0;
  461.         double lEmlM = 0.0;
  462.         SinCos scLE  = FastMath.sinCos(lE);
  463.         int iter = 0;
  464.         do {
  465.             final double f2 = ex * scLE.sin() - ey * scLE.cos();
  466.             final double f1 = 1.0 - ex * scLE.cos() - ey * scLE.sin();
  467.             final double f0 = lEmlM - f2;

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

  470.             lEmlM -= shift;
  471.             lE     = lM + lEmlM;
  472.             scLE   = FastMath.sinCos(lE);

  473.         } while (++iter < 50 && FastMath.abs(shift) > 1.0e-12);

  474.         return lE;

  475.     }

  476.     /** Computes the mean longitude argument from the eccentric longitude argument.
  477.      * @param lE = E + ω + Ω mean longitude argument (rad)
  478.      * @param ex first component of the eccentricity vector
  479.      * @param ey second component of the eccentricity vector
  480.      * @return the mean longitude argument
  481.      */
  482.     public static double eccentricToMean(final double lE, final double ex, final double ey) {
  483.         final SinCos scLE = FastMath.sinCos(lE);
  484.         return lE - ex * scLE.sin() + ey * scLE.cos();
  485.     }

  486.     /** {@inheritDoc} */
  487.     public double getE() {
  488.         return FastMath.sqrt(ex * ex + ey * ey);
  489.     }

  490.     /** {@inheritDoc} */
  491.     public double getEDot() {
  492.         return (ex * exDot + ey * eyDot) / FastMath.sqrt(ex * ex + ey * ey);
  493.     }

  494.     /** {@inheritDoc} */
  495.     public double getI() {
  496.         return 2 * FastMath.atan(FastMath.sqrt(hx * hx + hy * hy));
  497.     }

  498.     /** {@inheritDoc} */
  499.     public double getIDot() {
  500.         final double h2 = hx * hx + hy * hy;
  501.         final double h  = FastMath.sqrt(h2);
  502.         return 2 * (hx * hxDot + hy * hyDot) / (h * (1 + h2));
  503.     }

  504.     /** Compute position and velocity but not acceleration.
  505.      */
  506.     private void computePVWithoutA() {

  507.         if (partialPV != null) {
  508.             // already computed
  509.             return;
  510.         }

  511.         // get equinoctial parameters
  512.         final double lE = getLE();

  513.         // inclination-related intermediate parameters
  514.         final double hx2   = hx * hx;
  515.         final double hy2   = hy * hy;
  516.         final double factH = 1. / (1 + hx2 + hy2);

  517.         // reference axes defining the orbital plane
  518.         final double ux = (1 + hx2 - hy2) * factH;
  519.         final double uy =  2 * hx * hy * factH;
  520.         final double uz = -2 * hy * factH;

  521.         final double vx = uy;
  522.         final double vy = (1 - hx2 + hy2) * factH;
  523.         final double vz =  2 * hx * factH;

  524.         // eccentricity-related intermediate parameters
  525.         final double exey = ex * ey;
  526.         final double ex2  = ex * ex;
  527.         final double ey2  = ey * ey;
  528.         final double e2   = ex2 + ey2;
  529.         final double eta  = 1 + FastMath.sqrt(1 - e2);
  530.         final double beta = 1. / eta;

  531.         // eccentric longitude argument
  532.         final SinCos scLe   = FastMath.sinCos(lE);
  533.         final double cLe    = scLe.cos();
  534.         final double sLe    = scLe.sin();
  535.         final double exCeyS = ex * cLe + ey * sLe;

  536.         // coordinates of position and velocity in the orbital plane
  537.         final double x      = a * ((1 - beta * ey2) * cLe + beta * exey * sLe - ex);
  538.         final double y      = a * ((1 - beta * ex2) * sLe + beta * exey * cLe - ey);

  539.         final double factor = FastMath.sqrt(getMu() / a) / (1 - exCeyS);
  540.         final double xdot   = factor * (-sLe + beta * ey * exCeyS);
  541.         final double ydot   = factor * ( cLe - beta * ex * exCeyS);

  542.         final Vector3D position =
  543.                         new Vector3D(x * ux + y * vx, x * uy + y * vy, x * uz + y * vz);
  544.         final Vector3D velocity =
  545.                         new Vector3D(xdot * ux + ydot * vx, xdot * uy + ydot * vy, xdot * uz + ydot * vz);
  546.         partialPV = new PVCoordinates(position, velocity);

  547.     }

  548.     /** Compute non-Keplerian part of the acceleration from first time derivatives.
  549.      * <p>
  550.      * This method should be called only when {@link #hasDerivatives()} returns true.
  551.      * </p>
  552.      * @return non-Keplerian part of the acceleration
  553.      */
  554.     private Vector3D nonKeplerianAcceleration() {

  555.         final double[][] dCdP = new double[6][6];
  556.         getJacobianWrtParameters(PositionAngleType.MEAN, dCdP);

  557.         final double nonKeplerianMeanMotion = getLMDot() - getKeplerianMeanMotion();
  558.         final double nonKeplerianAx = dCdP[3][0] * aDot  + dCdP[3][1] * exDot + dCdP[3][2] * eyDot +
  559.                                       dCdP[3][3] * hxDot + dCdP[3][4] * hyDot + dCdP[3][5] * nonKeplerianMeanMotion;
  560.         final double nonKeplerianAy = dCdP[4][0] * aDot  + dCdP[4][1] * exDot + dCdP[4][2] * eyDot +
  561.                                       dCdP[4][3] * hxDot + dCdP[4][4] * hyDot + dCdP[4][5] * nonKeplerianMeanMotion;
  562.         final double nonKeplerianAz = dCdP[5][0] * aDot  + dCdP[5][1] * exDot + dCdP[5][2] * eyDot +
  563.                                       dCdP[5][3] * hxDot + dCdP[5][4] * hyDot + dCdP[5][5] * nonKeplerianMeanMotion;

  564.         return new Vector3D(nonKeplerianAx, nonKeplerianAy, nonKeplerianAz);

  565.     }

  566.     /** {@inheritDoc} */
  567.     protected Vector3D initPosition() {

  568.         // get equinoctial parameters
  569.         final double lE = getLE();

  570.         // inclination-related intermediate parameters
  571.         final double hx2   = hx * hx;
  572.         final double hy2   = hy * hy;
  573.         final double factH = 1. / (1 + hx2 + hy2);

  574.         // reference axes defining the orbital plane
  575.         final double ux = (1 + hx2 - hy2) * factH;
  576.         final double uy =  2 * hx * hy * factH;
  577.         final double uz = -2 * hy * factH;

  578.         final double vx = uy;
  579.         final double vy = (1 - hx2 + hy2) * factH;
  580.         final double vz =  2 * hx * factH;

  581.         // eccentricity-related intermediate parameters
  582.         final double exey = ex * ey;
  583.         final double ex2  = ex * ex;
  584.         final double ey2  = ey * ey;
  585.         final double e2   = ex2 + ey2;
  586.         final double eta  = 1 + FastMath.sqrt(1 - e2);
  587.         final double beta = 1. / eta;

  588.         // eccentric longitude argument
  589.         final SinCos scLe   = FastMath.sinCos(lE);
  590.         final double cLe    = scLe.cos();
  591.         final double sLe    = scLe.sin();

  592.         // coordinates of position and velocity in the orbital plane
  593.         final double x      = a * ((1 - beta * ey2) * cLe + beta * exey * sLe - ex);
  594.         final double y      = a * ((1 - beta * ex2) * sLe + beta * exey * cLe - ey);

  595.         return new Vector3D(x * ux + y * vx, x * uy + y * vy, x * uz + y * vz);

  596.     }

  597.     /** {@inheritDoc} */
  598.     protected TimeStampedPVCoordinates initPVCoordinates() {

  599.         // position and velocity
  600.         computePVWithoutA();

  601.         // acceleration
  602.         final double r2 = partialPV.getPosition().getNormSq();
  603.         final Vector3D keplerianAcceleration = new Vector3D(-getMu() / (r2 * FastMath.sqrt(r2)), partialPV.getPosition());
  604.         final Vector3D acceleration = hasDerivatives() ?
  605.                                       keplerianAcceleration.add(nonKeplerianAcceleration()) :
  606.                                       keplerianAcceleration;

  607.         return new TimeStampedPVCoordinates(getDate(), partialPV.getPosition(), partialPV.getVelocity(), acceleration);

  608.     }

  609.     /** {@inheritDoc} */
  610.     public EquinoctialOrbit shiftedBy(final double dt) {

  611.         // use Keplerian-only motion
  612.         final EquinoctialOrbit keplerianShifted = new EquinoctialOrbit(a, ex, ey, hx, hy,
  613.                                                                        getLM() + getKeplerianMeanMotion() * dt,
  614.                                                                        PositionAngleType.MEAN, getFrame(),
  615.                                                                        getDate().shiftedBy(dt), getMu());

  616.         if (hasDerivatives()) {

  617.             // extract non-Keplerian acceleration from first time derivatives
  618.             final Vector3D nonKeplerianAcceleration = nonKeplerianAcceleration();

  619.             // add quadratic effect of non-Keplerian acceleration to Keplerian-only shift
  620.             keplerianShifted.computePVWithoutA();
  621.             final Vector3D fixedP   = new Vector3D(1, keplerianShifted.partialPV.getPosition(),
  622.                                                    0.5 * dt * dt, nonKeplerianAcceleration);
  623.             final double   fixedR2 = fixedP.getNormSq();
  624.             final double   fixedR  = FastMath.sqrt(fixedR2);
  625.             final Vector3D fixedV  = new Vector3D(1, keplerianShifted.partialPV.getVelocity(),
  626.                                                   dt, nonKeplerianAcceleration);
  627.             final Vector3D fixedA  = new Vector3D(-getMu() / (fixedR2 * fixedR), keplerianShifted.partialPV.getPosition(),
  628.                                                   1, nonKeplerianAcceleration);

  629.             // build a new orbit, taking non-Keplerian acceleration into account
  630.             return new EquinoctialOrbit(new TimeStampedPVCoordinates(keplerianShifted.getDate(),
  631.                                                                      fixedP, fixedV, fixedA),
  632.                                         keplerianShifted.getFrame(), keplerianShifted.getMu());

  633.         } else {
  634.             // Keplerian-only motion is all we can do
  635.             return keplerianShifted;
  636.         }

  637.     }

  638.     /** {@inheritDoc} */
  639.     protected double[][] computeJacobianMeanWrtCartesian() {

  640.         final double[][] jacobian = new double[6][6];

  641.         // compute various intermediate parameters
  642.         computePVWithoutA();
  643.         final Vector3D position = partialPV.getPosition();
  644.         final Vector3D velocity = partialPV.getVelocity();
  645.         final double r2         = position.getNormSq();
  646.         final double r          = FastMath.sqrt(r2);
  647.         final double r3         = r * r2;

  648.         final double mu         = getMu();
  649.         final double sqrtMuA    = FastMath.sqrt(a * mu);
  650.         final double a2         = a * a;

  651.         final double e2         = ex * ex + ey * ey;
  652.         final double oMe2       = 1 - e2;
  653.         final double epsilon    = FastMath.sqrt(oMe2);
  654.         final double beta       = 1 / (1 + epsilon);
  655.         final double ratio      = epsilon * beta;

  656.         final double hx2        = hx * hx;
  657.         final double hy2        = hy * hy;
  658.         final double hxhy       = hx * hy;

  659.         // precomputing equinoctial frame unit vectors (f, g, w)
  660.         final Vector3D f  = new Vector3D(1 - hy2 + hx2, 2 * hxhy, -2 * hy).normalize();
  661.         final Vector3D g  = new Vector3D(2 * hxhy, 1 + hy2 - hx2, 2 * hx).normalize();
  662.         final Vector3D w  = Vector3D.crossProduct(position, velocity).normalize();

  663.         // coordinates of the spacecraft in the equinoctial frame
  664.         final double x    = Vector3D.dotProduct(position, f);
  665.         final double y    = Vector3D.dotProduct(position, g);
  666.         final double xDot = Vector3D.dotProduct(velocity, f);
  667.         final double yDot = Vector3D.dotProduct(velocity, g);

  668.         // drDot / dEx = dXDot / dEx * f + dYDot / dEx * g
  669.         final double c1 = a / (sqrtMuA * epsilon);
  670.         final double c2 = a * sqrtMuA * beta / r3;
  671.         final double c3 = sqrtMuA / (r3 * epsilon);
  672.         final Vector3D drDotSdEx = new Vector3D( c1 * xDot * yDot - c2 * ey * x - c3 * x * y, f,
  673.                                                 -c1 * xDot * xDot - c2 * ey * y + c3 * x * x, g);

  674.         // drDot / dEy = dXDot / dEy * f + dYDot / dEy * g
  675.         final Vector3D drDotSdEy = new Vector3D( c1 * yDot * yDot + c2 * ex * x - c3 * y * y, f,
  676.                                                 -c1 * xDot * yDot + c2 * ex * y + c3 * x * y, g);

  677.         // da
  678.         final Vector3D vectorAR = new Vector3D(2 * a2 / r3, position);
  679.         final Vector3D vectorARDot = new Vector3D(2 * a2 / mu, velocity);
  680.         fillHalfRow(1, vectorAR,    jacobian[0], 0);
  681.         fillHalfRow(1, vectorARDot, jacobian[0], 3);

  682.         // dEx
  683.         final double d1 = -a * ratio / r3;
  684.         final double d2 = (hy * xDot - hx * yDot) / (sqrtMuA * epsilon);
  685.         final double d3 = (hx * y - hy * x) / sqrtMuA;
  686.         final Vector3D vectorExRDot =
  687.             new Vector3D((2 * x * yDot - xDot * y) / mu, g, -y * yDot / mu, f, -ey * d3 / epsilon, w);
  688.         fillHalfRow(ex * d1, position, -ey * d2, w, epsilon / sqrtMuA, drDotSdEy, jacobian[1], 0);
  689.         fillHalfRow(1, vectorExRDot, jacobian[1], 3);

  690.         // dEy
  691.         final Vector3D vectorEyRDot =
  692.             new Vector3D((2 * xDot * y - x * yDot) / mu, f, -x * xDot / mu, g, ex * d3 / epsilon, w);
  693.         fillHalfRow(ey * d1, position, ex * d2, w, -epsilon / sqrtMuA, drDotSdEx, jacobian[2], 0);
  694.         fillHalfRow(1, vectorEyRDot, jacobian[2], 3);

  695.         // dHx
  696.         final double h = (1 + hx2 + hy2) / (2 * sqrtMuA * epsilon);
  697.         fillHalfRow(-h * xDot, w, jacobian[3], 0);
  698.         fillHalfRow( h * x,    w, jacobian[3], 3);

  699.         // dHy
  700.         fillHalfRow(-h * yDot, w, jacobian[4], 0);
  701.         fillHalfRow( h * y,    w, jacobian[4], 3);

  702.         // dLambdaM
  703.         final double l = -ratio / sqrtMuA;
  704.         fillHalfRow(-1 / sqrtMuA, velocity, d2, w, l * ex, drDotSdEx, l * ey, drDotSdEy, jacobian[5], 0);
  705.         fillHalfRow(-2 / sqrtMuA, position, ex * beta, vectorEyRDot, -ey * beta, vectorExRDot, d3, w, jacobian[5], 3);

  706.         return jacobian;

  707.     }

  708.     /** {@inheritDoc} */
  709.     protected double[][] computeJacobianEccentricWrtCartesian() {

  710.         // start by computing the Jacobian with mean angle
  711.         final double[][] jacobian = computeJacobianMeanWrtCartesian();

  712.         // Differentiating the Kepler equation lM = lE - ex sin lE + ey cos lE leads to:
  713.         // dlM = (1 - ex cos lE - ey sin lE) dE - sin lE dex + cos lE dey
  714.         // which is inverted and rewritten as:
  715.         // dlE = a/r dlM + sin lE a/r dex - cos lE a/r dey
  716.         final SinCos scLe  = FastMath.sinCos(getLE());
  717.         final double cosLe = scLe.cos();
  718.         final double sinLe = scLe.sin();
  719.         final double aOr   = 1 / (1 - ex * cosLe - ey * sinLe);

  720.         // update longitude row
  721.         final double[] rowEx = jacobian[1];
  722.         final double[] rowEy = jacobian[2];
  723.         final double[] rowL  = jacobian[5];
  724.         for (int j = 0; j < 6; ++j) {
  725.             rowL[j] = aOr * (rowL[j] + sinLe * rowEx[j] - cosLe * rowEy[j]);
  726.         }

  727.         return jacobian;

  728.     }

  729.     /** {@inheritDoc} */
  730.     protected double[][] computeJacobianTrueWrtCartesian() {

  731.         // start by computing the Jacobian with eccentric angle
  732.         final double[][] jacobian = computeJacobianEccentricWrtCartesian();

  733.         // Differentiating the eccentric longitude equation
  734.         // tan((lV - lE)/2) = [ex sin lE - ey cos lE] / [sqrt(1-ex^2-ey^2) + 1 - ex cos lE - ey sin lE]
  735.         // leads to
  736.         // cT (dlV - dlE) = cE dlE + cX dex + cY dey
  737.         // with
  738.         // cT = [d^2 + (ex sin lE - ey cos lE)^2] / 2
  739.         // d  = 1 + sqrt(1-ex^2-ey^2) - ex cos lE - ey sin lE
  740.         // cE = (ex cos lE + ey sin lE) (sqrt(1-ex^2-ey^2) + 1) - ex^2 - ey^2
  741.         // cX =  sin lE (sqrt(1-ex^2-ey^2) + 1) - ey + ex (ex sin lE - ey cos lE) / sqrt(1-ex^2-ey^2)
  742.         // cY = -cos lE (sqrt(1-ex^2-ey^2) + 1) + ex + ey (ex sin lE - ey cos lE) / sqrt(1-ex^2-ey^2)
  743.         // which can be solved to find the differential of the true longitude
  744.         // dlV = (cT + cE) / cT dlE + cX / cT deX + cY / cT deX
  745.         final SinCos scLe      = FastMath.sinCos(getLE());
  746.         final double cosLe     = scLe.cos();
  747.         final double sinLe     = scLe.sin();
  748.         final double eSinE     = ex * sinLe - ey * cosLe;
  749.         final double ecosE     = ex * cosLe + ey * sinLe;
  750.         final double e2        = ex * ex + ey * ey;
  751.         final double epsilon   = FastMath.sqrt(1 - e2);
  752.         final double onePeps   = 1 + epsilon;
  753.         final double d         = onePeps - ecosE;
  754.         final double cT        = (d * d + eSinE * eSinE) / 2;
  755.         final double cE        = ecosE * onePeps - e2;
  756.         final double cX        = ex * eSinE / epsilon - ey + sinLe * onePeps;
  757.         final double cY        = ey * eSinE / epsilon + ex - cosLe * onePeps;
  758.         final double factorLe  = (cT + cE) / cT;
  759.         final double factorEx  = cX / cT;
  760.         final double factorEy  = cY / cT;

  761.         // update longitude row
  762.         final double[] rowEx = jacobian[1];
  763.         final double[] rowEy = jacobian[2];
  764.         final double[] rowL = jacobian[5];
  765.         for (int j = 0; j < 6; ++j) {
  766.             rowL[j] = factorLe * rowL[j] + factorEx * rowEx[j] + factorEy * rowEy[j];
  767.         }

  768.         return jacobian;

  769.     }

  770.     /** {@inheritDoc} */
  771.     public void addKeplerContribution(final PositionAngleType type, final double gm,
  772.                                       final double[] pDot) {
  773.         final double oMe2;
  774.         final double ksi;
  775.         final double n  = FastMath.sqrt(gm / a) / a;
  776.         final SinCos sc = FastMath.sinCos(lv);
  777.         switch (type) {
  778.             case MEAN :
  779.                 pDot[5] += n;
  780.                 break;
  781.             case ECCENTRIC :
  782.                 oMe2 = 1 - ex * ex - ey * ey;
  783.                 ksi  = 1 + ex * sc.cos() + ey * sc.sin();
  784.                 pDot[5] += n * ksi / oMe2;
  785.                 break;
  786.             case TRUE :
  787.                 oMe2 = 1 - ex * ex - ey * ey;
  788.                 ksi  = 1 + ex * sc.cos() + ey * sc.sin();
  789.                 pDot[5] += n * ksi * ksi / (oMe2 * FastMath.sqrt(oMe2));
  790.                 break;
  791.             default :
  792.                 throw new OrekitInternalError(null);
  793.         }
  794.     }

  795.     /**  Returns a string representation of this equinoctial parameters object.
  796.      * @return a string representation of this object
  797.      */
  798.     public String toString() {
  799.         return new StringBuilder().append("equinoctial parameters: ").append('{').
  800.                                   append("a: ").append(a).
  801.                                   append("; ex: ").append(ex).append("; ey: ").append(ey).
  802.                                   append("; hx: ").append(hx).append("; hy: ").append(hy).
  803.                                   append("; lv: ").append(FastMath.toDegrees(lv)).
  804.                                   append(";}").toString();
  805.     }

  806.     /** {@inheritDoc} */
  807.     @Override
  808.     public PositionAngleType getCachedPositionAngleType() {
  809.         return PositionAngleType.TRUE;
  810.     }

  811.     /** {@inheritDoc} */
  812.     @Override
  813.     public boolean hasRates() {
  814.         return hasDerivatives();
  815.     }

  816.     /** {@inheritDoc} */
  817.     @Override
  818.     public EquinoctialOrbit removeRates() {
  819.         final PositionAngleType positionAngleType = getCachedPositionAngleType();
  820.         return new EquinoctialOrbit(getA(), getEquinoctialEx(), getEquinoctialEy(), getHx(), getHy(),
  821.                 getL(positionAngleType), positionAngleType, getFrame(), getDate(), getMu());
  822.     }

  823.     /** Replace the instance with a data transfer object for serialization.
  824.      * @return data transfer object that will be serialized
  825.      */
  826.     @DefaultDataContext
  827.     private Object writeReplace() {
  828.         return new DTO(this);
  829.     }

  830.     /** Internal class used only for serialization. */
  831.     @DefaultDataContext
  832.     private static class DTO implements Serializable {

  833.         /** Serializable UID. */
  834.         private static final long serialVersionUID = 20170414L;

  835.         /** Double values. */
  836.         private double[] d;

  837.         /** Frame in which are defined the orbital parameters. */
  838.         private final Frame frame;

  839.         /** Simple constructor.
  840.          * @param orbit instance to serialize
  841.          */
  842.         private DTO(final EquinoctialOrbit orbit) {

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

  844.             // decompose date
  845.             final AbsoluteDate j2000Epoch =
  846.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  847.             final double epoch  = FastMath.floor(pv.getDate().durationFrom(j2000Epoch));
  848.             final double offset = pv.getDate().durationFrom(j2000Epoch.shiftedBy(epoch));

  849.             if (orbit.hasDerivatives()) {
  850.                 // we have derivatives
  851.                 this.d = new double[] {
  852.                     epoch, offset, orbit.getMu(),
  853.                     orbit.a, orbit.ex, orbit.ey,
  854.                     orbit.hx, orbit.hy, orbit.lv,
  855.                     orbit.aDot, orbit.exDot, orbit.eyDot,
  856.                     orbit.hxDot, orbit.hyDot, orbit.lvDot
  857.                 };
  858.             } else {
  859.                 // we don't have derivatives
  860.                 this.d = new double[] {
  861.                     epoch, offset, orbit.getMu(),
  862.                     orbit.a, orbit.ex, orbit.ey,
  863.                     orbit.hx, orbit.hy, orbit.lv
  864.                 };
  865.             }

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

  867.         }

  868.         /** Replace the deserialized data transfer object with a {@link EquinoctialOrbit}.
  869.          * @return replacement {@link EquinoctialOrbit}
  870.          */
  871.         private Object readResolve() {
  872.             final AbsoluteDate j2000Epoch =
  873.                     DataContext.getDefault().getTimeScales().getJ2000Epoch();
  874.             if (d.length >= 15) {
  875.                 // we have derivatives
  876.                 return new EquinoctialOrbit(d[ 3], d[ 4], d[ 5], d[ 6], d[ 7], d[ 8],
  877.                                             d[ 9], d[10], d[11], d[12], d[13], d[14],
  878.                                             PositionAngleType.TRUE,
  879.                                             frame, j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  880.                                             d[2]);
  881.             } else {
  882.                 // we don't have derivatives
  883.                 return new EquinoctialOrbit(d[ 3], d[ 4], d[ 5], d[ 6], d[ 7], d[ 8], PositionAngleType.TRUE,
  884.                                             frame, j2000Epoch.shiftedBy(d[0]).shiftedBy(d[1]),
  885.                                             d[2]);
  886.             }
  887.         }

  888.     }

  889. }