FieldCircularOrbit.java

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

  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. import java.util.stream.Stream;

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


  36. /**
  37.  * This class handles circular orbital parameters.

  38.  * <p>
  39.  * The parameters used internally are the circular elements which can be
  40.  * related to Keplerian elements as follows:
  41.  *   <ul>
  42.  *     <li>a</li>
  43.  *     <li>e<sub>x</sub> = e cos(ω)</li>
  44.  *     <li>e<sub>y</sub> = e sin(ω)</li>
  45.  *     <li>i</li>
  46.  *     <li>Ω</li>
  47.  *     <li>α<sub>v</sub> = v + ω</li>
  48.  *   </ul>
  49.  * where Ω stands for the Right Ascension of the Ascending Node and
  50.  * α<sub>v</sub> stands for the true latitude argument
  51.  * <p>
  52.  * The conversion equations from and to Keplerian elements given above hold only
  53.  * when both sides are unambiguously defined, i.e. when orbit is neither equatorial
  54.  * nor circular. When orbit is circular (but not equatorial), the circular
  55.  * parameters are still unambiguously defined whereas some Keplerian elements
  56.  * (more precisely ω and Ω) become ambiguous. When orbit is equatorial,
  57.  * neither the Keplerian nor the circular parameters can be defined unambiguously.
  58.  * {@link EquinoctialOrbit equinoctial orbits} is the recommended way to represent
  59.  * orbits.
  60.  * </p>
  61.  * <p>
  62.  * The instance <code>CircularOrbit</code> is guaranteed to be immutable.
  63.  * </p>
  64.  * @see    Orbit
  65.  * @see    KeplerianOrbit
  66.  * @see    CartesianOrbit
  67.  * @see    EquinoctialOrbit
  68.  * @author Luc Maisonobe
  69.  * @author Fabien Maussion
  70.  * @author V&eacute;ronique Pommier-Maurussane
  71.  * @since 9.0
  72.  */

  73. public  class FieldCircularOrbit<T extends CalculusFieldElement<T>>
  74.     extends FieldOrbit<T> {

  75.     /** Semi-major axis (m). */
  76.     private final T a;

  77.     /** First component of the circular eccentricity vector. */
  78.     private final T ex;

  79.     /** Second component of the circular eccentricity vector. */
  80.     private final T ey;

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

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

  85.     /** True latitude argument (rad). */
  86.     private final T alphaV;

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

  89.     /** First component of the circular eccentricity vector derivative. */
  90.     private final T exDot;

  91.     /** Second component of the circular eccentricity vector derivative. */
  92.     private final T eyDot;

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

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

  97.     /** True latitude argument derivative (rad/s). */
  98.     private final T alphaVDot;

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

  101.     /** one. */
  102.     private final T one;

  103.     /** zero. */
  104.     private final T zero;

  105.     /** Creates a new instance.
  106.      * @param a  semi-major axis (m)
  107.      * @param ex e cos(ω), first component of circular eccentricity vector
  108.      * @param ey e sin(ω), second component of circular eccentricity vector
  109.      * @param i inclination (rad)
  110.      * @param raan right ascension of ascending node (Ω, rad)
  111.      * @param alpha  an + ω, mean, eccentric or true latitude argument (rad)
  112.      * @param type type of latitude argument
  113.      * @param frame the frame in which are defined the parameters
  114.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  115.      * @param date date of the orbital parameters
  116.      * @param mu central attraction coefficient (m³/s²)
  117.      * @exception IllegalArgumentException if eccentricity is equal to 1 or larger or
  118.      * if frame is not a {@link Frame#isPseudoInertial pseudo-inertial frame}
  119.      */
  120.     public FieldCircularOrbit(final T a, final T ex, final T ey,
  121.                               final T i, final T raan,
  122.                               final T alpha, final PositionAngle type,
  123.                               final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  124.         throws IllegalArgumentException {
  125.         this(a, ex, ey, i, raan, alpha,
  126.              null, null, null, null, null, null,
  127.              type, frame, date, mu);
  128.     }

  129.     /** Creates a new instance.
  130.      * @param a  semi-major axis (m)
  131.      * @param ex e cos(ω), first component of circular eccentricity vector
  132.      * @param ey e sin(ω), second component of circular eccentricity vector
  133.      * @param i inclination (rad)
  134.      * @param raan right ascension of ascending node (Ω, rad)
  135.      * @param alpha  an + ω, mean, eccentric or true latitude argument (rad)
  136.      * @param aDot  semi-major axis derivative (m/s)
  137.      * @param exDot d(e cos(ω))/dt, first component of circular eccentricity vector derivative
  138.      * @param eyDot d(e sin(ω))/dt, second component of circular eccentricity vector derivative
  139.      * @param iDot inclination  derivative(rad/s)
  140.      * @param raanDot right ascension of ascending node derivative (rad/s)
  141.      * @param alphaDot  d(an + ω), mean, eccentric or true latitude argument derivative (rad/s)
  142.      * @param type type of latitude argument
  143.      * @param frame the frame in which are defined the parameters
  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 eccentricity is equal to 1 or larger or
  148.      * if frame is not a {@link Frame#isPseudoInertial pseudo-inertial frame}
  149.      */
  150.     public FieldCircularOrbit(final T a, final T ex, final T ey,
  151.                               final T i, final T raan, final T alpha,
  152.                               final T aDot, final T exDot, final T eyDot,
  153.                               final T iDot, final T raanDot, final T alphaDot,
  154.                               final PositionAngle type,
  155.                               final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  156.         throws IllegalArgumentException {
  157.         super(frame, date, mu);
  158.         if (ex.getReal() * ex.getReal() + ey.getReal() * ey.getReal() >= 1.0) {
  159.             throw new OrekitIllegalArgumentException(OrekitMessages.HYPERBOLIC_ORBIT_NOT_HANDLED_AS,
  160.                                                      getClass().getName());
  161.         }

  162.         this.a       =  a;
  163.         this.aDot    =  aDot;
  164.         this.ex      = ex;
  165.         this.exDot   = exDot;
  166.         this.ey      = ey;
  167.         this.eyDot   = eyDot;
  168.         this.i       = i;
  169.         this.iDot    = iDot;
  170.         this.raan    = raan;
  171.         this.raanDot = raanDot;

  172.         one = a.getField().getOne();
  173.         zero = a.getField().getZero();

  174.         if (hasDerivatives()) {
  175.             final FieldUnivariateDerivative1<T> exUD    = new FieldUnivariateDerivative1<>(ex,    exDot);
  176.             final FieldUnivariateDerivative1<T> eyUD    = new FieldUnivariateDerivative1<>(ey,    eyDot);
  177.             final FieldUnivariateDerivative1<T> alphaUD = new FieldUnivariateDerivative1<>(alpha, alphaDot);
  178.             final FieldUnivariateDerivative1<T> alphavUD;
  179.             switch (type) {
  180.                 case MEAN :
  181.                     alphavUD = eccentricToTrue(meanToEccentric(alphaUD, exUD, eyUD), exUD, eyUD);
  182.                     break;
  183.                 case ECCENTRIC :
  184.                     alphavUD = eccentricToTrue(alphaUD, exUD, eyUD);
  185.                     break;
  186.                 case TRUE :
  187.                     alphavUD = alphaUD;
  188.                     break;
  189.                 default :
  190.                     throw new OrekitInternalError(null);
  191.             }
  192.             this.alphaV    = alphavUD.getValue();
  193.             this.alphaVDot = alphavUD.getDerivative(1);
  194.         } else {
  195.             switch (type) {
  196.                 case MEAN :
  197.                     this.alphaV = eccentricToTrue(meanToEccentric(alpha, ex, ey), ex, ey);
  198.                     break;
  199.                 case ECCENTRIC :
  200.                     this.alphaV = eccentricToTrue(alpha, ex, ey);
  201.                     break;
  202.                 case TRUE :
  203.                     this.alphaV = alpha;
  204.                     break;
  205.                 default :
  206.                     throw new OrekitInternalError(null);
  207.             }
  208.             this.alphaVDot = null;
  209.         }

  210.         this.partialPV = null;

  211.     }

  212.     /** Constructor from Cartesian parameters.
  213.      *
  214.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  215.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  216.      * use {@code mu} and the position to compute the acceleration, including
  217.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  218.      *
  219.      * @param pvCoordinates the {@link FieldPVCoordinates} in inertial frame
  220.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  221.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  222.      * @param mu central attraction coefficient (m³/s²)
  223.      * @exception IllegalArgumentException if frame is not a {@link
  224.      * Frame#isPseudoInertial pseudo-inertial frame}
  225.      */
  226.     public FieldCircularOrbit(final TimeStampedFieldPVCoordinates<T> pvCoordinates,
  227.                               final Frame frame, final T mu)
  228.         throws IllegalArgumentException {
  229.         super(pvCoordinates, frame, mu);

  230.         // compute semi-major axis
  231.         final FieldVector3D<T> pvP = pvCoordinates.getPosition();
  232.         final FieldVector3D<T> pvV = pvCoordinates.getVelocity();
  233.         final FieldVector3D<T> pvA = pvCoordinates.getAcceleration();
  234.         final T r2 = pvP.getNormSq();
  235.         final T r  = r2.sqrt();
  236.         final T V2 = pvV.getNormSq();
  237.         final T rV2OnMu = r.multiply(V2).divide(mu);

  238.         zero = r.getField().getZero();
  239.         one = r.getField().getOne();

  240.         if (rV2OnMu.getReal() > 2) {
  241.             throw new OrekitIllegalArgumentException(OrekitMessages.HYPERBOLIC_ORBIT_NOT_HANDLED_AS,
  242.                                                      getClass().getName());
  243.         }

  244.         a = r.divide(rV2OnMu.negate().add(2));

  245.         // compute inclination
  246.         final FieldVector3D<T> momentum = pvCoordinates.getMomentum();
  247.         final FieldVector3D<T> plusK = FieldVector3D.getPlusK(r.getField());
  248.         i = FieldVector3D.angle(momentum, plusK);

  249.         // compute right ascension of ascending node
  250.         final FieldVector3D<T> node  = FieldVector3D.crossProduct(plusK, momentum);
  251.         raan = node.getY().atan2(node.getX());

  252.         // 2D-coordinates in the canonical frame
  253.         final FieldSinCos<T> scRaan = FastMath.sinCos(raan);
  254.         final FieldSinCos<T> scI    = FastMath.sinCos(i);
  255.         final T xP      = pvP.getX();
  256.         final T yP      = pvP.getY();
  257.         final T zP      = pvP.getZ();
  258.         final T x2      = (xP.multiply(scRaan.cos()).add(yP .multiply(scRaan.sin()))).divide(a);
  259.         final T y2      = (yP.multiply(scRaan.cos()).subtract(xP.multiply(scRaan.sin()))).multiply(scI.cos()).add(zP.multiply(scI.sin())).divide(a);

  260.         // compute eccentricity vector
  261.         final T eSE    = FieldVector3D.dotProduct(pvP, pvV).divide(a.multiply(mu).sqrt());
  262.         final T eCE    = rV2OnMu.subtract(1);
  263.         final T e2     = eCE.multiply(eCE).add(eSE.multiply(eSE));
  264.         final T f      = eCE.subtract(e2);
  265.         final T g      = eSE.multiply(e2.negate().add(1).sqrt());
  266.         final T aOnR   = a.divide(r);
  267.         final T a2OnR2 = aOnR.multiply(aOnR);
  268.         ex = a2OnR2.multiply(f.multiply(x2).add(g.multiply(y2)));
  269.         ey = a2OnR2.multiply(f.multiply(y2).subtract(g.multiply(x2)));

  270.         // compute latitude argument
  271.         final T beta = (ex.multiply(ex).add(ey.multiply(ey)).negate().add(1)).sqrt().add(1).reciprocal();
  272.         alphaV = eccentricToTrue(y2.add(ey).add(eSE.multiply(beta).multiply(ex)).atan2(x2.add(ex).subtract(eSE.multiply(beta).multiply(ey))),
  273.                                  ex, ey);

  274.         partialPV = pvCoordinates;

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

  277.             final T[][] jacobian = MathArrays.buildArray(a.getField(), 6, 6);
  278.             getJacobianWrtCartesian(PositionAngle.MEAN, jacobian);

  279.             final FieldVector3D<T> keplerianAcceleration    = new FieldVector3D<>(r.multiply(r2).reciprocal().multiply(mu.negate()), pvP);
  280.             final FieldVector3D<T> nonKeplerianAcceleration = pvA.subtract(keplerianAcceleration);
  281.             final T   aX                       = nonKeplerianAcceleration.getX();
  282.             final T   aY                       = nonKeplerianAcceleration.getY();
  283.             final T   aZ                       = nonKeplerianAcceleration.getZ();
  284.             aDot    = jacobian[0][3].multiply(aX).add(jacobian[0][4].multiply(aY)).add(jacobian[0][5].multiply(aZ));
  285.             exDot   = jacobian[1][3].multiply(aX).add(jacobian[1][4].multiply(aY)).add(jacobian[1][5].multiply(aZ));
  286.             eyDot   = jacobian[2][3].multiply(aX).add(jacobian[2][4].multiply(aY)).add(jacobian[2][5].multiply(aZ));
  287.             iDot    = jacobian[3][3].multiply(aX).add(jacobian[3][4].multiply(aY)).add(jacobian[3][5].multiply(aZ));
  288.             raanDot = jacobian[4][3].multiply(aX).add(jacobian[4][4].multiply(aY)).add(jacobian[4][5].multiply(aZ));

  289.             // in order to compute true anomaly derivative, we must compute
  290.             // mean anomaly derivative including Keplerian motion and convert to true anomaly
  291.             final T alphaMDot = getKeplerianMeanMotion().
  292.                                 add(jacobian[5][3].multiply(aX)).add(jacobian[5][4].multiply(aY)).add(jacobian[5][5].multiply(aZ));
  293.             final FieldUnivariateDerivative1<T> exUD     = new FieldUnivariateDerivative1<>(ex, exDot);
  294.             final FieldUnivariateDerivative1<T> eyUD     = new FieldUnivariateDerivative1<>(ey, eyDot);
  295.             final FieldUnivariateDerivative1<T> alphaMUD = new FieldUnivariateDerivative1<>(getAlphaM(), alphaMDot);
  296.             final FieldUnivariateDerivative1<T> alphavUD = eccentricToTrue(meanToEccentric(alphaMUD, exUD, eyUD), exUD, eyUD);
  297.             alphaVDot = alphavUD.getDerivative(1);

  298.         } else {
  299.             // acceleration is either almost zero or NaN,
  300.             // we assume acceleration was not known
  301.             // we don't set up derivatives
  302.             aDot      = null;
  303.             exDot     = null;
  304.             eyDot     = null;
  305.             iDot      = null;
  306.             raanDot   = null;
  307.             alphaVDot = null;
  308.         }

  309.     }

  310.     /** Constructor from Cartesian parameters.
  311.      *
  312.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  313.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  314.      * use {@code mu} and the position to compute the acceleration, including
  315.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  316.      *
  317.      * @param PVCoordinates the {@link FieldPVCoordinates} in inertial frame
  318.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  319.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  320.      * @param date date of the orbital parameters
  321.      * @param mu central attraction coefficient (m³/s²)
  322.      * @exception IllegalArgumentException if frame is not a {@link
  323.      * Frame#isPseudoInertial pseudo-inertial frame}
  324.      */
  325.     public FieldCircularOrbit(final FieldPVCoordinates<T> PVCoordinates, final Frame frame,
  326.                               final FieldAbsoluteDate<T> date, final T mu)
  327.         throws IllegalArgumentException {
  328.         this(new TimeStampedFieldPVCoordinates<>(date, PVCoordinates), frame, mu);
  329.     }

  330.     /** Constructor from any kind of orbital parameters.
  331.      * @param op orbital parameters to copy
  332.      */
  333.     public FieldCircularOrbit(final FieldOrbit<T> op) {
  334.         super(op.getFrame(), op.getDate(), op.getMu());
  335.         a    = op.getA();
  336.         i    = op.getI();
  337.         final T hx = op.getHx();
  338.         final T hy = op.getHy();
  339.         final T h2 = hx.multiply(hx).add(hy.multiply(hy));
  340.         final T h  = h2.sqrt();
  341.         raan = hy.atan2(hx);
  342.         final FieldSinCos<T> scRaan = FastMath.sinCos(raan);
  343.         final T cosRaan = h.getReal() == 0 ? scRaan.cos() : hx.divide(h);
  344.         final T sinRaan = h.getReal() == 0 ? scRaan.sin() : hy.divide(h);
  345.         final T equiEx = op.getEquinoctialEx();
  346.         final T equiEy = op.getEquinoctialEy();
  347.         ex   = equiEx.multiply(cosRaan).add(equiEy.multiply(sinRaan));
  348.         ey   = equiEy.multiply(cosRaan).subtract(equiEx.multiply(sinRaan));
  349.         this.alphaV = op.getLv().subtract(raan);

  350.         if (op.hasDerivatives()) {
  351.             aDot      = op.getADot();
  352.             final T      hxDot = op.getHxDot();
  353.             final T      hyDot = op.getHyDot();
  354.             iDot      = cosRaan.multiply(hxDot).add(sinRaan.multiply(hyDot)).multiply(2).divide(h2.add(1));
  355.             raanDot   = hx.multiply(hyDot).subtract(hy.multiply(hxDot)).divide(h2);
  356.             final T equiExDot = op.getEquinoctialExDot();
  357.             final T equiEyDot = op.getEquinoctialEyDot();
  358.             exDot     = equiExDot.add(equiEy.multiply(raanDot)).multiply(cosRaan).
  359.                         add(equiEyDot.subtract(equiEx.multiply(raanDot)).multiply(sinRaan));
  360.             eyDot     = equiEyDot.subtract(equiEx.multiply(raanDot)).multiply(cosRaan).
  361.                         subtract(equiExDot.add(equiEy.multiply(raanDot)).multiply(sinRaan));
  362.             alphaVDot = op.getLvDot().subtract(raanDot);
  363.         } else {
  364.             aDot      = null;
  365.             exDot     = null;
  366.             eyDot     = null;
  367.             iDot      = null;
  368.             raanDot   = null;
  369.             alphaVDot = null;
  370.         }

  371.         partialPV = null;

  372.         one = a.getField().getOne();
  373.         zero = a.getField().getZero();

  374.     }

  375.     /** {@inheritDoc} */
  376.     public OrbitType getType() {
  377.         return OrbitType.CIRCULAR;
  378.     }

  379.     /** {@inheritDoc} */
  380.     public T getA() {
  381.         return a;
  382.     }

  383.     /** {@inheritDoc} */
  384.     public T getADot() {
  385.         return aDot;
  386.     }

  387.     /** {@inheritDoc} */
  388.     public T getEquinoctialEx() {
  389.         final FieldSinCos<T> sc = FastMath.sinCos(raan);
  390.         return ex.multiply(sc.cos()).subtract(ey.multiply(sc.sin()));
  391.     }

  392.     /** {@inheritDoc} */
  393.     public T getEquinoctialExDot() {

  394.         if (!hasDerivatives()) {
  395.             return null;
  396.         }

  397.         final FieldSinCos<T> sc = FastMath.sinCos(raan);
  398.         return exDot.subtract(ey.multiply(raanDot)).multiply(sc.cos()).
  399.                subtract(eyDot.add(ex.multiply(raanDot)).multiply(sc.sin()));

  400.     }

  401.     /** {@inheritDoc} */
  402.     public T getEquinoctialEy() {
  403.         final FieldSinCos<T> sc = FastMath.sinCos(raan);
  404.         return ey.multiply(sc.cos()).add(ex.multiply(sc.sin()));
  405.     }

  406.     /** {@inheritDoc} */
  407.     public T getEquinoctialEyDot() {

  408.         if (!hasDerivatives()) {
  409.             return null;
  410.         }

  411.         final FieldSinCos<T> sc = FastMath.sinCos(raan);
  412.         return eyDot.add(ex.multiply(raanDot)).multiply(sc.cos()).
  413.                add(exDot.subtract(ey.multiply(raanDot)).multiply(sc.sin()));

  414.     }

  415.     /** Get the first component of the circular eccentricity vector.
  416.      * @return ex = e cos(ω), first component of the circular eccentricity vector
  417.      */
  418.     public T getCircularEx() {
  419.         return ex;
  420.     }

  421.     /** Get the first component of the circular eccentricity vector derivative.
  422.      * @return d(ex)/dt = d(e cos(ω))/dt, first component of the circular eccentricity vector derivative
  423.      */
  424.     public T getCircularExDot() {
  425.         return exDot;
  426.     }

  427.     /** Get the second component of the circular eccentricity vector.
  428.      * @return ey = e sin(ω), second component of the circular eccentricity vector
  429.      */
  430.     public T getCircularEy() {
  431.         return ey;
  432.     }

  433.     /** Get the second component of the circular eccentricity vector derivative.
  434.      * @return d(ey)/dt = d(e sin(ω))/dt, second component of the circular eccentricity vector derivative
  435.      */
  436.     public T getCircularEyDot() {
  437.         return eyDot;
  438.     }

  439.     /** {@inheritDoc} */
  440.     public T getHx() {
  441.         // Check for equatorial retrograde orbit
  442.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  443.             return zero.add(Double.NaN);
  444.         }
  445.         return raan.cos().multiply(i.divide(2).tan());
  446.     }

  447.     /** {@inheritDoc} */
  448.     public T getHxDot() {

  449.         if (!hasDerivatives()) {
  450.             return null;
  451.         }

  452.         // Check for equatorial retrograde orbit
  453.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  454.             return zero.add(Double.NaN);
  455.         }

  456.         final FieldSinCos<T> sc = FastMath.sinCos(raan);
  457.         final T tan             = i.multiply(0.5).tan();
  458.         return sc.cos().multiply(0.5).multiply(tan.multiply(tan).add(1)).multiply(iDot).
  459.                subtract(sc.sin().multiply(tan).multiply(raanDot));

  460.     }

  461.     /** {@inheritDoc} */
  462.     public T getHy() {
  463.         // Check for equatorial retrograde orbit
  464.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  465.             return zero.add(Double.NaN);
  466.         }
  467.         return raan.sin().multiply(i.divide(2).tan());
  468.     }

  469.     /** {@inheritDoc} */
  470.     public T getHyDot() {

  471.         if (!hasDerivatives()) {
  472.             return null;
  473.         }

  474.         // Check for equatorial retrograde orbit
  475.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  476.             return zero.add(Double.NaN);
  477.         }

  478.         final FieldSinCos<T> sc = FastMath.sinCos(raan);
  479.         final T tan     = i.multiply(0.5).tan();
  480.         return sc.sin().multiply(0.5).multiply(tan.multiply(tan).add(1)).multiply(iDot).
  481.                add(sc.cos().multiply(tan).multiply(raanDot));

  482.     }

  483.     /** Get the true latitude argument.
  484.      * @return v + ω true latitude argument (rad)
  485.      */
  486.     public T getAlphaV() {
  487.         return alphaV;
  488.     }

  489.     /** Get the true latitude argument derivative.
  490.      * @return d(v + ω)/dt true latitude argument derivative (rad/s)
  491.      */
  492.     public T getAlphaVDot() {
  493.         return alphaVDot;
  494.     }

  495.     /** Get the eccentric latitude argument.
  496.      * @return E + ω eccentric latitude argument (rad)
  497.      */
  498.     public T getAlphaE() {
  499.         return trueToEccentric(alphaV, ex, ey);
  500.     }

  501.     /** Get the eccentric latitude argument derivative.
  502.      * @return d(E + ω)/dt eccentric latitude argument derivative (rad/s)
  503.      */
  504.     public T getAlphaEDot() {

  505.         if (!hasDerivatives()) {
  506.             return null;
  507.         }

  508.         final FieldUnivariateDerivative1<T> alphaVUD = new FieldUnivariateDerivative1<>(alphaV, alphaVDot);
  509.         final FieldUnivariateDerivative1<T> exUD     = new FieldUnivariateDerivative1<>(ex, exDot);
  510.         final FieldUnivariateDerivative1<T> eyUD     = new FieldUnivariateDerivative1<>(ey, eyDot);
  511.         final FieldUnivariateDerivative1<T> alphaEUD = trueToEccentric(alphaVUD, exUD, eyUD);
  512.         return alphaEUD.getDerivative(1);

  513.     }

  514.     /** Get the mean latitude argument.
  515.      * @return M + ω mean latitude argument (rad)
  516.      */
  517.     public T getAlphaM() {
  518.         return eccentricToMean(trueToEccentric(alphaV, ex, ey), ex, ey);
  519.     }

  520.     /** Get the mean latitude argument derivative.
  521.      * @return d(M + ω)/dt mean latitude argument derivative (rad/s)
  522.      */
  523.     public T getAlphaMDot() {

  524.         if (!hasDerivatives()) {
  525.             return null;
  526.         }

  527.         final FieldUnivariateDerivative1<T> alphaVUD = new FieldUnivariateDerivative1<>(alphaV, alphaVDot);
  528.         final FieldUnivariateDerivative1<T> exUD     = new FieldUnivariateDerivative1<>(ex, exDot);
  529.         final FieldUnivariateDerivative1<T> eyUD     = new FieldUnivariateDerivative1<>(ey, eyDot);
  530.         final FieldUnivariateDerivative1<T> alphaMUD = eccentricToMean(trueToEccentric(alphaVUD, exUD, eyUD), exUD, eyUD);
  531.         return alphaMUD.getDerivative(1);

  532.     }

  533.     /** Get the latitude argument.
  534.      * @param type type of the angle
  535.      * @return latitude argument (rad)
  536.      */
  537.     public T getAlpha(final PositionAngle type) {
  538.         return (type == PositionAngle.MEAN) ? getAlphaM() :
  539.                                               ((type == PositionAngle.ECCENTRIC) ? getAlphaE() :
  540.                                                                                    getAlphaV());
  541.     }

  542.     /** Get the latitude argument derivative.
  543.      * @param type type of the angle
  544.      * @return latitude argument derivative (rad/s)
  545.      */
  546.     public T getAlphaDot(final PositionAngle type) {
  547.         return (type == PositionAngle.MEAN) ? getAlphaMDot() :
  548.                                               ((type == PositionAngle.ECCENTRIC) ? getAlphaEDot() :
  549.                                                                                    getAlphaVDot());
  550.     }

  551.     /** Computes the true latitude argument from the eccentric latitude argument.
  552.      * @param alphaE = E + ω eccentric latitude argument (rad)
  553.      * @param ex e cos(ω), first component of circular eccentricity vector
  554.      * @param ey e sin(ω), second component of circular eccentricity vector
  555.      * @param <T> Type of the field elements
  556.      * @return the true latitude argument.
  557.      */
  558.     public static <T extends CalculusFieldElement<T>> T eccentricToTrue(final T alphaE, final T ex, final T ey) {
  559.         final T epsilon               = ex.multiply(ex).add(ey.multiply(ey)).negate().add(1).sqrt();
  560.         final FieldSinCos<T> scAlphaE = FastMath.sinCos(alphaE);
  561.         return alphaE.add(ex.multiply(scAlphaE.sin()).subtract(ey.multiply(scAlphaE.cos())).divide(
  562.                                       epsilon.add(1).subtract(ex.multiply(scAlphaE.cos())).subtract(
  563.                                       ey.multiply(scAlphaE.sin()))).atan().multiply(2));
  564.     }

  565.     /** Computes the eccentric latitude argument from the true latitude argument.
  566.      * @param alphaV = v + ω true latitude argument (rad)
  567.      * @param ex e cos(ω), first component of circular eccentricity vector
  568.      * @param ey e sin(ω), second component of circular eccentricity vector
  569.      * @param <T> Type of the field elements
  570.      * @return the eccentric latitude argument.
  571.      */
  572.     public static <T extends CalculusFieldElement<T>> T trueToEccentric(final T alphaV, final T ex, final T ey) {
  573.         final T epsilon               = ex.multiply(ex).add(ey.multiply(ey)).negate().add(1).sqrt();
  574.         final FieldSinCos<T> scAlphaV = FastMath.sinCos(alphaV);
  575.         return alphaV.add(ey.multiply(scAlphaV.cos()).subtract(ex.multiply(scAlphaV.sin())).divide
  576.                                       (epsilon.add(1).add(ex.multiply(scAlphaV.cos()).add(ey.multiply(scAlphaV.sin())))).atan().multiply(2));
  577.     }

  578.     /** Computes the eccentric latitude argument from the mean latitude argument.
  579.      * @param alphaM = M + ω  mean latitude argument (rad)
  580.      * @param ex e cos(ω), first component of circular eccentricity vector
  581.      * @param ey e sin(ω), second component of circular eccentricity vector
  582.      * @param <T> Type of the field elements
  583.      * @return the eccentric latitude argument.
  584.      */
  585.     public static <T extends CalculusFieldElement<T>> T meanToEccentric(final T alphaM, final T ex, final T ey) {
  586.         // Generalization of Kepler equation to circular parameters
  587.         // with alphaE = PA + E and
  588.         //      alphaM = PA + M = alphaE - ex.sin(alphaE) + ey.cos(alphaE)

  589.         T alphaE                = alphaM;
  590.         T shift                 = alphaM.getField().getZero();
  591.         T alphaEMalphaM         = alphaM.getField().getZero();
  592.         FieldSinCos<T> scAlphaE = FastMath.sinCos(alphaE);
  593.         int    iter     = 0;
  594.         do {
  595.             final T f2 = ex.multiply(scAlphaE.sin()).subtract(ey.multiply(scAlphaE.cos()));
  596.             final T f1 = ex.negate().multiply(scAlphaE.cos()).subtract(ey.multiply(scAlphaE.sin())).add(1);
  597.             final T f0 = alphaEMalphaM.subtract(f2);

  598.             final T f12 = f1.multiply(2);
  599.             shift = f0.multiply(f12).divide(f1.multiply(f12).subtract(f0.multiply(f2)));

  600.             alphaEMalphaM  = alphaEMalphaM.subtract(shift);
  601.             alphaE         = alphaM.add(alphaEMalphaM);
  602.             scAlphaE       = FastMath.sinCos(alphaE);
  603.         } while (++iter < 50 && FastMath.abs(shift.getReal()) > 1.0e-12);
  604.         return alphaE;

  605.     }

  606.     /** Computes the mean latitude argument from the eccentric latitude argument.
  607.      * @param alphaE = E + ω  eccentric latitude argument (rad)
  608.      * @param ex e cos(ω), first component of circular eccentricity vector
  609.      * @param ey e sin(ω), second component of circular eccentricity vector
  610.      * @param <T> Type of the field elements
  611.      * @return the mean latitude argument.
  612.      */
  613.     public static <T extends CalculusFieldElement<T>> T eccentricToMean(final T alphaE, final T ex, final T ey) {
  614.         final FieldSinCos<T> scAlphaE = FastMath.sinCos(alphaE);
  615.         return alphaE.subtract(ex.multiply(scAlphaE.sin()).subtract(ey.multiply(scAlphaE.cos())));
  616.     }

  617.     /** {@inheritDoc} */
  618.     public T getE() {
  619.         return ex.multiply(ex).add(ey.multiply(ey)).sqrt();
  620.     }

  621.     /** {@inheritDoc} */
  622.     public T getEDot() {

  623.         if (!hasDerivatives()) {
  624.             return null;
  625.         }

  626.         return ex.multiply(exDot).add(ey.multiply(eyDot)).divide(getE());

  627.     }

  628.     /** {@inheritDoc} */
  629.     public T getI() {
  630.         return i;
  631.     }

  632.     /** {@inheritDoc} */
  633.     public T getIDot() {
  634.         return iDot;
  635.     }

  636.     /** Get the right ascension of the ascending node.
  637.      * @return right ascension of the ascending node (rad)
  638.      */
  639.     public T getRightAscensionOfAscendingNode() {
  640.         return raan;
  641.     }

  642.     /** Get the right ascension of the ascending node derivative.
  643.      * @return right ascension of the ascending node derivative (rad/s)
  644.      */
  645.     public T getRightAscensionOfAscendingNodeDot() {
  646.         return raanDot;
  647.     }

  648.     /** {@inheritDoc} */
  649.     public T getLv() {
  650.         return alphaV.add(raan);
  651.     }

  652.     /** {@inheritDoc} */
  653.     public T getLvDot() {
  654.         return hasDerivatives() ? alphaVDot.add(raanDot) : null;
  655.     }

  656.     /** {@inheritDoc} */
  657.     public T getLE() {
  658.         return getAlphaE().add(raan);
  659.     }

  660.     /** {@inheritDoc} */
  661.     public T getLEDot() {
  662.         return hasDerivatives() ? getAlphaEDot().add(raanDot) : null;
  663.     }

  664.     /** {@inheritDoc} */
  665.     public T getLM() {
  666.         return getAlphaM().add(raan);
  667.     }

  668.     /** {@inheritDoc} */
  669.     public T getLMDot() {
  670.         return hasDerivatives() ? getAlphaMDot().add(raanDot) : null;
  671.     }

  672.     /** {@inheritDoc} */
  673.     @Override
  674.     public boolean hasDerivatives() {
  675.         return aDot != null;
  676.     }

  677.     /** Compute position and velocity but not acceleration.
  678.      */
  679.     private void computePVWithoutA() {

  680.         if (partialPV != null) {
  681.             // already computed
  682.             return;
  683.         }

  684.         // get equinoctial parameters
  685.         final T equEx = getEquinoctialEx();
  686.         final T equEy = getEquinoctialEy();
  687.         final T hx = getHx();
  688.         final T hy = getHy();
  689.         final T lE = getLE();
  690.         // inclination-related intermediate parameters
  691.         final T hx2   = hx.multiply(hx);
  692.         final T hy2   = hy.multiply(hy);
  693.         final T factH = (hx2.add(1).add(hy2)).reciprocal();

  694.         // reference axes defining the orbital plane
  695.         final T ux = (hx2.add(1).subtract(hy2)).multiply(factH);
  696.         final T uy =  hx.multiply(2).multiply(hy).multiply(factH);
  697.         final T uz = hy.multiply(-2).multiply(factH);

  698.         final T vx = uy;
  699.         final T vy = (hy2.subtract(hx2).add(1)).multiply(factH);
  700.         final T vz =  hx.multiply(factH).multiply(2);

  701.         // eccentricity-related intermediate parameters
  702.         final T exey = equEx.multiply(equEy);
  703.         final T ex2  = equEx.multiply(equEx);
  704.         final T ey2  = equEy.multiply(equEy);
  705.         final T e2   = ex2.add(ey2);
  706.         final T eta  = e2.negate().add(1).sqrt().add(1);
  707.         final T beta = eta.reciprocal();

  708.         // eccentric latitude argument
  709.         final FieldSinCos<T> scLe = FastMath.sinCos(lE);
  710.         final T cLe    = scLe.cos();
  711.         final T sLe    = scLe.sin();
  712.         final T exCeyS = equEx.multiply(cLe).add(equEy.multiply(sLe));
  713.         // coordinates of position and velocity in the orbital plane
  714.         final T x      = a.multiply(beta.negate().multiply(ey2).add(1).multiply(cLe).add(beta.multiply(exey).multiply(sLe)).subtract(equEx));
  715.         final T y      = a.multiply(beta.negate().multiply(ex2).add(1).multiply(sLe).add(beta.multiply(exey).multiply(cLe)).subtract(equEy));

  716.         final T factor = one.add(getMu()).divide(a).sqrt().divide(exCeyS.negate().add(1));
  717.         final T xdot   = factor.multiply( beta.multiply(equEy).multiply(exCeyS).subtract(sLe ));
  718.         final T ydot   = factor.multiply( cLe.subtract(beta.multiply(equEx).multiply(exCeyS)));

  719.         final FieldVector3D<T> position = new FieldVector3D<>(x.multiply(ux).add(y.multiply(vx)),
  720.                                                               x.multiply(uy).add(y.multiply(vy)),
  721.                                                               x.multiply(uz).add(y.multiply(vz)));
  722.         final FieldVector3D<T> velocity = new FieldVector3D<>(xdot.multiply(ux).add(ydot.multiply(vx)),
  723.                                                               xdot.multiply(uy).add(ydot.multiply(vy)),
  724.                                                               xdot.multiply(uz).add(ydot.multiply(vz)));

  725.         partialPV = new FieldPVCoordinates<>(position, velocity);

  726.     }

  727.     /** Compute non-Keplerian part of the acceleration from first time derivatives.
  728.      * <p>
  729.      * This method should be called only when {@link #hasDerivatives()} returns true.
  730.      * </p>
  731.      * @return non-Keplerian part of the acceleration
  732.      */
  733.     private FieldVector3D<T> nonKeplerianAcceleration() {

  734.         final T[][] dCdP = MathArrays.buildArray(a.getField(), 6, 6);
  735.         getJacobianWrtParameters(PositionAngle.MEAN, dCdP);

  736.         final T nonKeplerianMeanMotion = getAlphaMDot().subtract(getKeplerianMeanMotion());
  737.         final T nonKeplerianAx =     dCdP[3][0].multiply(aDot).
  738.                                  add(dCdP[3][1].multiply(exDot)).
  739.                                  add(dCdP[3][2].multiply(eyDot)).
  740.                                  add(dCdP[3][3].multiply(iDot)).
  741.                                  add(dCdP[3][4].multiply(raanDot)).
  742.                                  add(dCdP[3][5].multiply(nonKeplerianMeanMotion));
  743.         final T nonKeplerianAy =     dCdP[4][0].multiply(aDot).
  744.                                  add(dCdP[4][1].multiply(exDot)).
  745.                                  add(dCdP[4][2].multiply(eyDot)).
  746.                                  add(dCdP[4][3].multiply(iDot)).
  747.                                  add(dCdP[4][4].multiply(raanDot)).
  748.                                  add(dCdP[4][5].multiply(nonKeplerianMeanMotion));
  749.         final T nonKeplerianAz =     dCdP[5][0].multiply(aDot).
  750.                                  add(dCdP[5][1].multiply(exDot)).
  751.                                  add(dCdP[5][2].multiply(eyDot)).
  752.                                  add(dCdP[5][3].multiply(iDot)).
  753.                                  add(dCdP[5][4].multiply(raanDot)).
  754.                                  add(dCdP[5][5].multiply(nonKeplerianMeanMotion));

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

  756.     }

  757.     /** {@inheritDoc} */
  758.     protected TimeStampedFieldPVCoordinates<T> initPVCoordinates() {

  759.         // position and velocity
  760.         computePVWithoutA();

  761.         // acceleration
  762.         final T r2 = partialPV.getPosition().getNormSq();
  763.         final FieldVector3D<T> keplerianAcceleration = new FieldVector3D<>(r2.multiply(r2.sqrt()).reciprocal().multiply(getMu().negate()),
  764.                                                                            partialPV.getPosition());
  765.         final FieldVector3D<T> acceleration = hasDerivatives() ?
  766.                                               keplerianAcceleration.add(nonKeplerianAcceleration()) :
  767.                                               keplerianAcceleration;

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

  769.     }

  770.     /** {@inheritDoc} */
  771.     public FieldCircularOrbit<T> shiftedBy(final double dt) {
  772.         return shiftedBy(getDate().getField().getZero().add(dt));
  773.     }

  774.     /** {@inheritDoc} */
  775.     public FieldCircularOrbit<T> shiftedBy(final T dt) {

  776.         // use Keplerian-only motion
  777.         final FieldCircularOrbit<T> keplerianShifted = new FieldCircularOrbit<>(a, ex, ey, i, raan,
  778.                                                                                 getAlphaM().add(getKeplerianMeanMotion().multiply(dt)),
  779.                                                                                 PositionAngle.MEAN, getFrame(),
  780.                                                                                 getDate().shiftedBy(dt), getMu());

  781.         if (hasDerivatives()) {

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

  784.             // add quadratic effect of non-Keplerian acceleration to Keplerian-only shift
  785.             keplerianShifted.computePVWithoutA();
  786.             final FieldVector3D<T> fixedP   = new FieldVector3D<>(one, keplerianShifted.partialPV.getPosition(),
  787.                                                                   dt.multiply(dt).multiply(0.5), nonKeplerianAcceleration);
  788.             final T   fixedR2 = fixedP.getNormSq();
  789.             final T   fixedR  = fixedR2.sqrt();
  790.             final FieldVector3D<T> fixedV  = new FieldVector3D<>(one, keplerianShifted.partialPV.getVelocity(),
  791.                                                                  dt, nonKeplerianAcceleration);
  792.             final FieldVector3D<T> fixedA  = new FieldVector3D<>(fixedR2.multiply(fixedR).reciprocal().multiply(getMu().negate()),
  793.                                                                  keplerianShifted.partialPV.getPosition(),
  794.                                                                  one, nonKeplerianAcceleration);

  795.             // build a new orbit, taking non-Keplerian acceleration into account
  796.             return new FieldCircularOrbit<>(new TimeStampedFieldPVCoordinates<>(keplerianShifted.getDate(),
  797.                                                                                 fixedP, fixedV, fixedA),
  798.                                             keplerianShifted.getFrame(), keplerianShifted.getMu());

  799.         } else {
  800.             // Keplerian-only motion is all we can do
  801.             return keplerianShifted;
  802.         }

  803.     }

  804.     /** {@inheritDoc}
  805.      * <p>
  806.      * The interpolated instance is created by polynomial Hermite interpolation
  807.      * on circular elements, without derivatives (which means the interpolation
  808.      * falls back to Lagrange interpolation only).
  809.      * </p>
  810.      * <p>
  811.      * As this implementation of interpolation is polynomial, it should be used only
  812.      * with small samples (about 10-20 points) in order to avoid <a
  813.      * href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's phenomenon</a>
  814.      * and numerical problems (including NaN appearing).
  815.      * </p>
  816.      * <p>
  817.      * If orbit interpolation on large samples is needed, using the {@link
  818.      * org.orekit.propagation.analytical.Ephemeris} class is a better way than using this
  819.      * low-level interpolation. The Ephemeris class automatically handles selection of
  820.      * a neighboring sub-sample with a predefined number of point from a large global sample
  821.      * in a thread-safe way.
  822.      * </p>
  823.      */
  824.     public FieldCircularOrbit<T> interpolate(final FieldAbsoluteDate<T> date, final Stream<FieldOrbit<T>> sample) {

  825.         // first pass to check if derivatives are available throughout the sample
  826.         final List<FieldOrbit<T>> list = sample.collect(Collectors.toList());
  827.         boolean useDerivatives = true;
  828.         for (final FieldOrbit<T> orbit : list) {
  829.             useDerivatives = useDerivatives && orbit.hasDerivatives();
  830.         }

  831.         // set up an interpolator
  832.         final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();

  833.         // second pass to feed interpolator
  834.         FieldAbsoluteDate<T> previousDate   = null;
  835.         T                    previousRAAN   = zero.add(Double.NaN);
  836.         T                    previousAlphaM = zero.add(Double.NaN);
  837.         for (final FieldOrbit<T> orbit : list) {
  838.             final FieldCircularOrbit<T> circ = (FieldCircularOrbit<T>) OrbitType.CIRCULAR.convertType(orbit);
  839.             final T continuousRAAN;
  840.             final T continuousAlphaM;
  841.             if (previousDate == null) {
  842.                 continuousRAAN   = circ.getRightAscensionOfAscendingNode();
  843.                 continuousAlphaM = circ.getAlphaM();
  844.             } else {
  845.                 final T dt       = circ.getDate().durationFrom(previousDate);
  846.                 final T keplerAM = previousAlphaM .add(circ.getKeplerianMeanMotion().multiply(dt));
  847.                 continuousRAAN   = MathUtils.normalizeAngle(circ.getRightAscensionOfAscendingNode(), previousRAAN);
  848.                 continuousAlphaM = MathUtils.normalizeAngle(circ.getAlphaM(), keplerAM);
  849.             }
  850.             previousDate   = circ.getDate();
  851.             previousRAAN   = continuousRAAN;
  852.             previousAlphaM = continuousAlphaM;
  853.             final T[] toAdd = MathArrays.buildArray(one.getField(), 6);
  854.             toAdd[0] = circ.getA();
  855.             toAdd[1] = circ.getCircularEx();
  856.             toAdd[2] = circ.getCircularEy();
  857.             toAdd[3] = circ.getI();
  858.             toAdd[4] = continuousRAAN;
  859.             toAdd[5] = continuousAlphaM;
  860.             if (useDerivatives) {
  861.                 final T[] toAddDot = MathArrays.buildArray(one.getField(), 6);
  862.                 toAddDot[0] = circ.getADot();
  863.                 toAddDot[1] = circ.getCircularExDot();
  864.                 toAddDot[2] = circ.getCircularEyDot();
  865.                 toAddDot[3] = circ.getIDot();
  866.                 toAddDot[4] = circ.getRightAscensionOfAscendingNodeDot();
  867.                 toAddDot[5] = circ.getAlphaMDot();
  868.                 interpolator.addSamplePoint(circ.getDate().durationFrom(date),
  869.                                             toAdd, toAddDot);
  870.             } else {
  871.                 interpolator.addSamplePoint(circ.getDate().durationFrom(date),
  872.                                             toAdd);
  873.             }
  874.         }

  875.         // interpolate
  876.         final T[][] interpolated = interpolator.derivatives(zero, 1);

  877.         // build a new interpolated instance
  878.         return new FieldCircularOrbit<>(interpolated[0][0], interpolated[0][1], interpolated[0][2],
  879.                                         interpolated[0][3], interpolated[0][4], interpolated[0][5],
  880.                                         interpolated[1][0], interpolated[1][1], interpolated[1][2],
  881.                                         interpolated[1][3], interpolated[1][4], interpolated[1][5],
  882.                                         PositionAngle.MEAN, getFrame(), date, getMu());

  883.     }

  884.     /** {@inheritDoc} */
  885.     protected T[][] computeJacobianMeanWrtCartesian() {

  886.         final T[][] jacobian = MathArrays.buildArray(one.getField(), 6, 6);

  887.         // compute various intermediate parameters
  888.         computePVWithoutA();
  889.         final FieldVector3D<T> position = partialPV.getPosition();
  890.         final FieldVector3D<T> velocity = partialPV.getVelocity();

  891.         final T x          = position.getX();
  892.         final T y          = position.getY();
  893.         final T z          = position.getZ();
  894.         final T vx         = velocity.getX();
  895.         final T vy         = velocity.getY();
  896.         final T vz         = velocity.getZ();
  897.         final T pv         = FieldVector3D.dotProduct(position, velocity);
  898.         final T r2         = position.getNormSq();
  899.         final T r          = r2.sqrt();
  900.         final T v2         = velocity.getNormSq();

  901.         final T mu         = getMu();
  902.         final T oOsqrtMuA  = one.divide(a.multiply(mu).sqrt());
  903.         final T rOa        = r.divide(a);
  904.         final T aOr        = a.divide(r);
  905.         final T aOr2       = a.divide(r2);
  906.         final T a2         = a.multiply(a);

  907.         final T ex2        = ex.multiply(ex);
  908.         final T ey2        = ey.multiply(ey);
  909.         final T e2         = ex2.add(ey2);
  910.         final T epsilon    = e2.negate().add(1.0).sqrt();
  911.         final T beta       = epsilon.add(1).reciprocal();

  912.         final T eCosE      = rOa.negate().add(1);
  913.         final T eSinE      = pv.multiply(oOsqrtMuA);

  914.         final FieldSinCos<T> scI    = FastMath.sinCos(i);
  915.         final FieldSinCos<T> scRaan = FastMath.sinCos(raan);
  916.         final T cosI       = scI.cos();
  917.         final T sinI       = scI.sin();
  918.         final T cosRaan    = scRaan.cos();
  919.         final T sinRaan    = scRaan.sin();

  920.         // da
  921.         fillHalfRow(aOr.multiply(2.0).multiply(aOr2), position, jacobian[0], 0);
  922.         fillHalfRow(a2.multiply(mu.divide(2.).reciprocal()), velocity, jacobian[0], 3);

  923.         // differentials of the normalized momentum
  924.         final FieldVector3D<T> danP = new FieldVector3D<>(v2, position, pv.negate(), velocity);
  925.         final FieldVector3D<T> danV = new FieldVector3D<>(r2, velocity, pv.negate(), position);
  926.         final T recip   = partialPV.getMomentum().getNorm().reciprocal();
  927.         final T recip2  = recip.multiply(recip);
  928.         final T recip2N = recip2.negate();
  929.         final FieldVector3D<T> dwXP = new FieldVector3D<>(recip,
  930.                                                           new FieldVector3D<>(zero, vz, vy.negate()),
  931.                                                           recip2N.multiply(sinRaan).multiply(sinI),
  932.                                                           danP);
  933.         final FieldVector3D<T> dwYP = new FieldVector3D<>(recip,
  934.                                                           new FieldVector3D<>(vz.negate(), zero, vx),
  935.                                                           recip2.multiply(cosRaan).multiply(sinI),
  936.                                                           danP);
  937.         final FieldVector3D<T> dwZP = new FieldVector3D<>(recip,
  938.                                                           new FieldVector3D<>(vy, vx.negate(), zero),
  939.                                                           recip2N.multiply(cosI),
  940.                                                           danP);
  941.         final FieldVector3D<T> dwXV = new FieldVector3D<>(recip,
  942.                                                           new FieldVector3D<>(zero, z.negate(), y),
  943.                                                           recip2N.multiply(sinRaan).multiply(sinI),
  944.                                                           danV);
  945.         final FieldVector3D<T> dwYV = new FieldVector3D<>(recip,
  946.                                                           new FieldVector3D<>(z, zero, x.negate()),
  947.                                                           recip2.multiply(cosRaan).multiply(sinI),
  948.                                                           danV);
  949.         final FieldVector3D<T> dwZV = new FieldVector3D<>(recip,
  950.                                                           new FieldVector3D<>(y.negate(), x, zero),
  951.                                                           recip2N.multiply(cosI),
  952.                                                           danV);

  953.         // di
  954.         fillHalfRow(sinRaan.multiply(cosI), dwXP, cosRaan.negate().multiply(cosI), dwYP, sinI.negate(), dwZP, jacobian[3], 0);
  955.         fillHalfRow(sinRaan.multiply(cosI), dwXV, cosRaan.negate().multiply(cosI), dwYV, sinI.negate(), dwZV, jacobian[3], 3);

  956.         // dRaan
  957.         fillHalfRow(sinRaan.divide(sinI), dwYP, cosRaan.divide(sinI), dwXP, jacobian[4], 0);
  958.         fillHalfRow(sinRaan.divide(sinI), dwYV, cosRaan.divide(sinI), dwXV, jacobian[4], 3);

  959.         // orbital frame: (p, q, w) p along ascending node, w along momentum
  960.         // the coordinates of the spacecraft in this frame are: (u, v, 0)
  961.         final T u     =  x.multiply(cosRaan).add(y.multiply(sinRaan));
  962.         final T cv    =  x.negate().multiply(sinRaan).add(y.multiply(cosRaan));
  963.         final T v     = cv.multiply(cosI).add(z.multiply(sinI));

  964.         // du
  965.         final FieldVector3D<T> duP = new FieldVector3D<>(cv.multiply(cosRaan).divide(sinI), dwXP,
  966.                                                          cv.multiply(sinRaan).divide(sinI), dwYP,
  967.                                                          one, new FieldVector3D<>(cosRaan, sinRaan, zero));
  968.         final FieldVector3D<T> duV = new FieldVector3D<>(cv.multiply(cosRaan).divide(sinI), dwXV,
  969.                                                          cv.multiply(sinRaan).divide(sinI), dwYV);

  970.         // dv
  971.         final FieldVector3D<T> dvP = new FieldVector3D<>(u.negate().multiply(cosRaan).multiply(cosI).divide(sinI).add(sinRaan.multiply(z)), dwXP,
  972.                                                          u.negate().multiply(sinRaan).multiply(cosI).divide(sinI).subtract(cosRaan.multiply(z)), dwYP,
  973.                                                          cv, dwZP,
  974.                                                          one, new FieldVector3D<>(sinRaan.negate().multiply(cosI), cosRaan.multiply(cosI), sinI));
  975.         final FieldVector3D<T> dvV = new FieldVector3D<>(u.negate().multiply(cosRaan).multiply(cosI).divide(sinI).add(sinRaan.multiply(z)), dwXV,
  976.                                                          u.negate().multiply(sinRaan).multiply(cosI).divide(sinI).subtract(cosRaan.multiply(z)), dwYV,
  977.                                                          cv, dwZV);

  978.         final FieldVector3D<T> dc1P = new FieldVector3D<>(aOr2.multiply(eSinE.multiply(eSinE).multiply(2).add(1).subtract(eCosE)).divide(r2), position,
  979.                                                           aOr2.multiply(-2).multiply(eSinE).multiply(oOsqrtMuA), velocity);
  980.         final FieldVector3D<T> dc1V = new FieldVector3D<>(aOr2.multiply(-2).multiply(eSinE).multiply(oOsqrtMuA), position,
  981.                                                           zero.add(2).divide(mu), velocity);
  982.         final FieldVector3D<T> dc2P = new FieldVector3D<>(aOr2.multiply(eSinE).multiply(eSinE.multiply(eSinE).subtract(e2.negate().add(1))).divide(r2.multiply(epsilon)), position,
  983.                                                           aOr2.multiply(e2.negate().add(1).subtract(eSinE.multiply(eSinE))).multiply(oOsqrtMuA).divide(epsilon), velocity);
  984.         final FieldVector3D<T> dc2V = new FieldVector3D<>(aOr2.multiply(e2.negate().add(1).subtract(eSinE.multiply(eSinE))).multiply(oOsqrtMuA).divide(epsilon), position,
  985.                                                           eSinE.divide(epsilon.multiply(mu)), velocity);

  986.         final T cof1   = aOr2.multiply(eCosE.subtract(e2));
  987.         final T cof2   = aOr2.multiply(epsilon).multiply(eSinE);
  988.         final FieldVector3D<T> dexP = new FieldVector3D<>(u, dc1P,  v, dc2P, cof1, duP,  cof2, dvP);
  989.         final FieldVector3D<T> dexV = new FieldVector3D<>(u, dc1V,  v, dc2V, cof1, duV,  cof2, dvV);
  990.         final FieldVector3D<T> deyP = new FieldVector3D<>(v, dc1P, u.negate(), dc2P, cof1, dvP, cof2.negate(), duP);
  991.         final FieldVector3D<T> deyV = new FieldVector3D<>(v, dc1V, u.negate(), dc2V, cof1, dvV, cof2.negate(), duV);
  992.         fillHalfRow(one, dexP, jacobian[1], 0);
  993.         fillHalfRow(one, dexV, jacobian[1], 3);
  994.         fillHalfRow(one, deyP, jacobian[2], 0);
  995.         fillHalfRow(one, deyV, jacobian[2], 3);

  996.         final T cle = u.divide(a).add(ex).subtract(eSinE.multiply(beta).multiply(ey));
  997.         final T sle = v.divide(a).add(ey).add(eSinE.multiply(beta).multiply(ex));
  998.         final T m1  = beta.multiply(eCosE);
  999.         final T m2  = m1.multiply(eCosE).negate().add(1);
  1000.         final T m3  = (u.multiply(ey).subtract(v.multiply(ex))).add(eSinE.multiply(beta).multiply(u.multiply(ex).add(v.multiply(ey))));
  1001.         final T m4  = sle.negate().add(cle.multiply(eSinE).multiply(beta));
  1002.         final T m5  = cle.add(sle.multiply(eSinE).multiply(beta));
  1003.         final T kk = m3.multiply(2).divide(r).add(aOr.multiply(eSinE)).add(m1.multiply(eSinE).multiply(m1.add(1).subtract(aOr.add(1).multiply(m2))).divide(epsilon)).divide(r2);
  1004.         final T jj = (m1.multiply(m2).divide(epsilon).subtract(1)).multiply(oOsqrtMuA);
  1005.         fillHalfRow(kk, position,
  1006.                     jj, velocity,
  1007.                     m4, dexP,
  1008.                     m5, deyP,
  1009.                     sle.negate().divide(a), duP,
  1010.                     cle.divide(a), dvP,
  1011.                     jacobian[5], 0);
  1012.         final T ll = (m1.multiply(m2).divide(epsilon ).subtract(1)).multiply(oOsqrtMuA);
  1013.         final T mm = m3.multiply(2).add(eSinE.multiply(a)).add(m1.multiply(eSinE).multiply(r).multiply(eCosE.multiply(beta).multiply(2).subtract(aOr.multiply(m2))).divide(epsilon)).divide(mu);

  1014.         fillHalfRow(ll, position,
  1015.                     mm, velocity,
  1016.                     m4, dexV,
  1017.                     m5, deyV,
  1018.                     sle.negate().divide(a), duV,
  1019.                     cle.divide(a), dvV,
  1020.                     jacobian[5], 3);
  1021.         return jacobian;

  1022.     }

  1023.     /** {@inheritDoc} */
  1024.     protected T[][] computeJacobianEccentricWrtCartesian() {

  1025.         // start by computing the Jacobian with mean angle
  1026.         final T[][] jacobian = computeJacobianMeanWrtCartesian();

  1027.         // Differentiating the Kepler equation aM = aE - ex sin aE + ey cos aE leads to:
  1028.         // daM = (1 - ex cos aE - ey sin aE) dE - sin aE dex + cos aE dey
  1029.         // which is inverted and rewritten as:
  1030.         // daE = a/r daM + sin aE a/r dex - cos aE a/r dey
  1031.         final T alphaE            = getAlphaE();
  1032.         final FieldSinCos<T> scAe = FastMath.sinCos(alphaE);
  1033.         final T cosAe             = scAe.cos();
  1034.         final T sinAe             = scAe.sin();
  1035.         final T aOr               = one.divide(one.subtract(ex.multiply(cosAe)).subtract(ey.multiply(sinAe)));

  1036.         // update longitude row
  1037.         final T[] rowEx = jacobian[1];
  1038.         final T[] rowEy = jacobian[2];
  1039.         final T[] rowL  = jacobian[5];
  1040.         for (int j = 0; j < 6; ++j) {
  1041.          // rowL[j] = aOr * (      rowL[j] +   sinAe *        rowEx[j]   -         cosAe *        rowEy[j]);
  1042.             rowL[j] = aOr.multiply(rowL[j].add(sinAe.multiply(rowEx[j])).subtract( cosAe.multiply(rowEy[j])));
  1043.         }
  1044.         jacobian[5] = rowL;
  1045.         return jacobian;

  1046.     }
  1047.     /** {@inheritDoc} */
  1048.     protected T[][] computeJacobianTrueWrtCartesian() {

  1049.         // start by computing the Jacobian with eccentric angle
  1050.         final T[][] jacobian = computeJacobianEccentricWrtCartesian();
  1051.         // Differentiating the eccentric latitude equation
  1052.         // tan((aV - aE)/2) = [ex sin aE - ey cos aE] / [sqrt(1-ex^2-ey^2) + 1 - ex cos aE - ey sin aE]
  1053.         // leads to
  1054.         // cT (daV - daE) = cE daE + cX dex + cY dey
  1055.         // with
  1056.         // cT = [d^2 + (ex sin aE - ey cos aE)^2] / 2
  1057.         // d  = 1 + sqrt(1-ex^2-ey^2) - ex cos aE - ey sin aE
  1058.         // cE = (ex cos aE + ey sin aE) (sqrt(1-ex^2-ey^2) + 1) - ex^2 - ey^2
  1059.         // cX =  sin aE (sqrt(1-ex^2-ey^2) + 1) - ey + ex (ex sin aE - ey cos aE) / sqrt(1-ex^2-ey^2)
  1060.         // cY = -cos aE (sqrt(1-ex^2-ey^2) + 1) + ex + ey (ex sin aE - ey cos aE) / sqrt(1-ex^2-ey^2)
  1061.         // which can be solved to find the differential of the true latitude
  1062.         // daV = (cT + cE) / cT daE + cX / cT deX + cY / cT deX
  1063.         final T alphaE            = getAlphaE();
  1064.         final FieldSinCos<T> scAe = FastMath.sinCos(alphaE);
  1065.         final T cosAe             = scAe.cos();
  1066.         final T sinAe             = scAe.sin();
  1067.         final T eSinE             = ex.multiply(sinAe).subtract(ey.multiply(cosAe));
  1068.         final T ecosE             = ex.multiply(cosAe).add(ey.multiply(sinAe));
  1069.         final T e2                = ex.multiply(ex).add(ey.multiply(ey));
  1070.         final T epsilon           = (one.subtract(e2)).sqrt();
  1071.         final T onePeps           = one.add(epsilon);
  1072.         final T d                 = onePeps.subtract(ecosE);
  1073.         final T cT                = (d.multiply(d).add(eSinE.multiply(eSinE))).divide(2);
  1074.         final T cE                = ecosE.multiply(onePeps).subtract(e2);
  1075.         final T cX                = ex.multiply(eSinE).divide(epsilon).subtract(ey).add(sinAe.multiply(onePeps));
  1076.         final T cY                = ey.multiply(eSinE).divide(epsilon).add(ex).subtract(cosAe.multiply(onePeps));
  1077.         final T factorLe          = (cT.add(cE)).divide(cT);
  1078.         final T factorEx          = cX.divide(cT);
  1079.         final T factorEy          = cY.divide(cT);

  1080.         // update latitude row
  1081.         final T[] rowEx = jacobian[1];
  1082.         final T[] rowEy = jacobian[2];
  1083.         final T[] rowA = jacobian[5];
  1084.         for (int j = 0; j < 6; ++j) {
  1085.             rowA[j] = factorLe.multiply(rowA[j]).add(factorEx.multiply(rowEx[j])).add(factorEy.multiply(rowEy[j]));
  1086.         }
  1087.         return jacobian;

  1088.     }

  1089.     /** {@inheritDoc} */
  1090.     public void addKeplerContribution(final PositionAngle type, final T gm,
  1091.                                       final T[] pDot) {
  1092.         final T oMe2;
  1093.         final T ksi;
  1094.         final T n = a.reciprocal().multiply(gm).sqrt().divide(a);
  1095.         final FieldSinCos<T> sc = FastMath.sinCos(alphaV);
  1096.         switch (type) {
  1097.             case MEAN :
  1098.                 pDot[5] = pDot[5].add(n);
  1099.                 break;
  1100.             case ECCENTRIC :
  1101.                 oMe2  = one.subtract(ex.multiply(ex)).subtract(ey.multiply(ey));
  1102.                 ksi   = one.add(ex.multiply(sc.cos())).add(ey.multiply(sc.sin()));
  1103.                 pDot[5] = pDot[5].add(n.multiply(ksi).divide(oMe2));
  1104.                 break;
  1105.             case TRUE :
  1106.                 oMe2  = one.subtract(ex.multiply(ex)).subtract(ey.multiply(ey));
  1107.                 ksi   = one.add(ex.multiply(sc.cos())).add(ey.multiply(sc.sin()));
  1108.                 pDot[5] = pDot[5].add(n.multiply(ksi).multiply(ksi).divide(oMe2.multiply(oMe2.sqrt())));
  1109.                 break;
  1110.             default :
  1111.                 throw new OrekitInternalError(null);
  1112.         }
  1113.     }

  1114.     /**  Returns a string representation of this Orbit object.
  1115.      * @return a string representation of this object
  1116.      */
  1117.     public String toString() {
  1118.         return new StringBuilder().append("circular parameters: ").append('{').
  1119.                                   append("a: ").append(a.getReal()).
  1120.                                   append(", ex: ").append(ex.getReal()).append(", ey: ").append(ey.getReal()).
  1121.                                   append(", i: ").append(FastMath.toDegrees(i.getReal())).
  1122.                                   append(", raan: ").append(FastMath.toDegrees(raan.getReal())).
  1123.                                   append(", alphaV: ").append(FastMath.toDegrees(alphaV.getReal())).
  1124.                                   append(";}").toString();
  1125.     }

  1126.     /** {@inheritDoc} */
  1127.     @Override
  1128.     public CircularOrbit toOrbit() {
  1129.         if (hasDerivatives()) {
  1130.             return new CircularOrbit(a.getReal(), ex.getReal(), ey.getReal(),
  1131.                                      i.getReal(), raan.getReal(), alphaV.getReal(),
  1132.                                      aDot.getReal(), exDot.getReal(), eyDot.getReal(),
  1133.                                      iDot.getReal(), raanDot.getReal(), alphaVDot.getReal(),
  1134.                                      PositionAngle.TRUE, getFrame(),
  1135.                                      getDate().toAbsoluteDate(), getMu().getReal());
  1136.         } else {
  1137.             return new CircularOrbit(a.getReal(), ex.getReal(), ey.getReal(),
  1138.                                      i.getReal(), raan.getReal(), alphaV.getReal(),
  1139.                                      PositionAngle.TRUE, getFrame(),
  1140.                                      getDate().toAbsoluteDate(), getMu().getReal());
  1141.         }
  1142.     }


  1143. }