GLONASSAnalyticalPropagator.java

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

  18. import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
  19. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.FieldSinCos;
  23. import org.hipparchus.util.MathArrays;
  24. import org.hipparchus.util.MathUtils;
  25. import org.hipparchus.util.Precision;
  26. import org.orekit.annotation.DefaultDataContext;
  27. import org.orekit.attitudes.AttitudeProvider;
  28. import org.orekit.data.DataContext;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.frames.Frame;
  32. import org.orekit.frames.Frames;
  33. import org.orekit.orbits.CartesianOrbit;
  34. import org.orekit.orbits.Orbit;
  35. import org.orekit.propagation.Propagator;
  36. import org.orekit.propagation.SpacecraftState;
  37. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  38. import org.orekit.time.AbsoluteDate;
  39. import org.orekit.time.GLONASSDate;
  40. import org.orekit.time.TimeScale;
  41. import org.orekit.utils.IERSConventions;
  42. import org.orekit.utils.PVCoordinates;

  43. /**
  44.  * This class aims at propagating a GLONASS orbit from {@link GLONASSOrbitalElements}.
  45.  *
  46.  * @see <a href="http://russianspacesystems.ru/wp-content/uploads/2016/08/ICD-GLONASS-CDMA-General.-Edition-1.0-2016.pdf">
  47.  *       GLONASS Interface Control Document</a>
  48.  *
  49.  * @author Bryan Cazabonne
  50.  * @since 10.0
  51.  *
  52.  */
  53. public class GLONASSAnalyticalPropagator extends AbstractAnalyticalPropagator {

  54.     // Constants
  55.     /** Constant 7.0 / 3.0. */
  56.     private static final double SEVEN_THIRD = 7.0 / 3.0;

  57.     /** Constant 7.0 / 6.0. */
  58.     private static final double SEVEN_SIXTH = 7.0 / 6.0;

  59.     /** Constant 7.0 / 24.0. */
  60.     private static final double SEVEN_24TH = 7.0 / 24.0;

  61.     /** Constant 49.0 / 72.0. */
  62.     private static final double FN_72TH = 49.0 / 72.0;

  63.     /** Value of the earth's rotation rate in rad/s. */
  64.     private static final double GLONASS_AV = 7.2921150e-5;

  65.     /** Mean value of inclination for Glonass orbit is equal to 63°. */
  66.     private static final double GLONASS_MEAN_INCLINATION = 64.8;

  67.     /** Mean value of Draconian period for Glonass orbit is equal to 40544s : 11 hours 15 minutes 44 seconds. */
  68.     private static final double GLONASS_MEAN_DRACONIAN_PERIOD = 40544;

  69.     /** Second degree zonal coefficient of normal potential. */
  70.     private static final double GLONASS_J20 = 1.08262575e-3;

  71.     /** Equatorial radius of Earth (m). */
  72.     private static final double GLONASS_EARTH_EQUATORIAL_RADIUS = 6378136;

  73.     // Data used to solve Kepler's equation
  74.     /** First coefficient to compute Kepler equation solver starter. */
  75.     private static final double A;

  76.     /** Second coefficient to compute Kepler equation solver starter. */
  77.     private static final double B;

  78.     static {
  79.         final double k1 = 3 * FastMath.PI + 2;
  80.         final double k2 = FastMath.PI - 1;
  81.         final double k3 = 6 * FastMath.PI - 1;
  82.         A  = 3 * k2 * k2 / k1;
  83.         B  = k3 * k3 / (6 * k1);
  84.     }

  85.     // Fields
  86.     /** The GLONASS orbital elements used. */
  87.     private final GLONASSOrbitalElements glonassOrbit;

  88.     /** The spacecraft mass (kg). */
  89.     private final double mass;

  90.     /** The ECI frame used for GLONASS propagation. */
  91.     private final Frame eci;

  92.     /** The ECEF frame used for GLONASS propagation. */
  93.     private final Frame ecef;

  94.     /** Data context for propagation. */
  95.     private final DataContext dataContext;

  96.     /**
  97.      * This nested class aims at building a GLONASSPropagator.
  98.      * <p>It implements the classical builder pattern.</p>
  99.      *
  100.      */
  101.     public static class Builder {

  102.         // Required parameter
  103.         /** The GLONASS orbital elements. */
  104.         private final GLONASSOrbitalElements orbit;

  105.         // Optional parameters
  106.         /** The attitude provider. */
  107.         private AttitudeProvider attitudeProvider;
  108.         /** The mass. */
  109.         private double mass = DEFAULT_MASS;
  110.         /** The ECI frame. */
  111.         private Frame eci  = null;
  112.         /** The ECEF frame. */
  113.         private Frame ecef = null;
  114.         /** Data context. */
  115.         private DataContext dataContext;

  116.         /** Initializes the builder.
  117.          * <p>The GLONASS orbital elements is the only requested parameter to build a GLONASSPropagator.</p>
  118.          * <p>The attitude provider is set by default to the
  119.          *  {@link org.orekit.propagation.Propagator#DEFAULT_LAW DEFAULT_LAW} in the
  120.          *  default data context.<br>
  121.          * The mass is set by default to the
  122.          *  {@link org.orekit.propagation.Propagator#DEFAULT_MASS DEFAULT_MASS}.<br>
  123.          * The data context is by default to the
  124.          *  {@link DataContext#getDefault() default data context}.<br>
  125.          * The ECI frame is set by default to the
  126.          *  {@link org.orekit.frames.Predefined#EME2000 EME2000 frame} in the default data
  127.          *  context.<br>
  128.          * The ECEF frame is set by default to the
  129.          *  {@link org.orekit.frames.Predefined#ITRF_CIO_CONV_2010_SIMPLE_EOP
  130.          *  CIO/2010-based ITRF simple EOP} in the default data context.
  131.          * </p>
  132.          *
  133.          * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  134.          * Another data context can be set using
  135.          * {@code Builder(final GLONASSOrbitalElements gpsOrbElt, final DataContext dataContext)}</p>
  136.          *
  137.          * @param glonassOrbElt the GLONASS orbital elements to be used by the GLONASS propagator.
  138.          * @see #attitudeProvider(AttitudeProvider provider)
  139.          * @see #mass(double mass)
  140.          * @see #eci(Frame inertial)
  141.          * @see #ecef(Frame bodyFixed)
  142.          */
  143.         @DefaultDataContext
  144.         public Builder(final GLONASSOrbitalElements glonassOrbElt) {
  145.             this(glonassOrbElt, DataContext.getDefault());
  146.         }

  147.         /** Initializes the builder.
  148.          * <p>The GLONASS orbital elements is the only requested parameter to build a GLONASSPropagator.</p>
  149.          * <p>The attitude provider is set by default to the
  150.          *  {@link org.orekit.propagation.Propagator#getDefaultLaw(Frames)}.<br>
  151.          * The mass is set by default to the
  152.          *  {@link org.orekit.propagation.Propagator#DEFAULT_MASS DEFAULT_MASS}.<br>
  153.          * The ECI frame is set by default to the
  154.          *  {@link Frames#getEME2000() EME2000 frame}.<br>
  155.          * The ECEF frame is set by default to the
  156.          *  {@link Frames#getITRF(IERSConventions, boolean) CIO/2010-based ITRF simple
  157.          *  EOP}.
  158.          * </p>
  159.          *
  160.          * @param glonassOrbElt the GLONASS orbital elements to be used by the GLONASS propagator.
  161.          * @param dataContext the data context to use for frames and time scales.
  162.          * @see #attitudeProvider(AttitudeProvider provider)
  163.          * @see #mass(double mass)
  164.          * @see #eci(Frame inertial)
  165.          * @see #ecef(Frame bodyFixed)
  166.          * @since 10.1
  167.          */
  168.         public Builder(final GLONASSOrbitalElements glonassOrbElt,
  169.                        final DataContext dataContext) {
  170.             this.orbit = glonassOrbElt;
  171.             this.dataContext = dataContext;
  172.             final Frames frames = dataContext.getFrames();
  173.             this.eci   = frames.getEME2000();
  174.             this.ecef  = frames.getITRF(IERSConventions.IERS_2010, true);
  175.             attitudeProvider = Propagator.getDefaultLaw(frames);
  176.         }

  177.         /** Sets the attitude provider.
  178.          *
  179.          * @param userProvider the attitude provider
  180.          * @return the updated builder
  181.          */
  182.         public Builder attitudeProvider(final AttitudeProvider userProvider) {
  183.             this.attitudeProvider = userProvider;
  184.             return this;
  185.         }

  186.         /** Sets the mass.
  187.          *
  188.          * @param userMass the mass (in kg)
  189.          * @return the updated builder
  190.          */
  191.         public Builder mass(final double userMass) {
  192.             this.mass = userMass;
  193.             return this;
  194.         }

  195.         /** Sets the Earth Centered Inertial frame used for propagation.
  196.          *
  197.          * @param inertial the ECI frame
  198.          * @return the updated builder
  199.          */
  200.         public Builder eci(final Frame inertial) {
  201.             this.eci = inertial;
  202.             return this;
  203.         }

  204.         /** Sets the Earth Centered Earth Fixed frame assimilated to the WGS84 ECEF.
  205.          *
  206.          * @param bodyFixed the ECEF frame
  207.          * @return the updated builder
  208.          */
  209.         public Builder ecef(final Frame bodyFixed) {
  210.             this.ecef = bodyFixed;
  211.             return this;
  212.         }

  213.         /**
  214.          * Sets the data context used by the propagator. Does not update the ECI or ECEF
  215.          * frames which must be done separately using {@link #eci(Frame)} and {@link
  216.          * #ecef(Frame)}.
  217.          *
  218.          * @param context used for propagation.
  219.          * @return the updated builder.
  220.          */
  221.         public Builder dataContext(final DataContext context) {
  222.             this.dataContext = context;
  223.             return this;
  224.         }

  225.         /** Finalizes the build.
  226.          *
  227.          * @return the built GLONASSPropagator
  228.          */
  229.         public GLONASSAnalyticalPropagator build() {
  230.             return new GLONASSAnalyticalPropagator(this);
  231.         }

  232.     }

  233.     /**
  234.      * Private constructor.
  235.      * @param builder the builder
  236.      */
  237.     private GLONASSAnalyticalPropagator(final Builder builder) {
  238.         super(builder.attitudeProvider);
  239.         this.dataContext = builder.dataContext;
  240.         // Stores the GLONASS orbital elements
  241.         this.glonassOrbit = builder.orbit;
  242.         // Sets the start date as the date of the orbital elements
  243.         setStartDate(glonassOrbit.getDate());
  244.         // Sets the mass
  245.         this.mass = builder.mass;
  246.         // Sets the Earth Centered Inertial frame
  247.         this.eci  = builder.eci;
  248.         // Sets the Earth Centered Earth Fixed frame
  249.         this.ecef = builder.ecef;
  250.     }

  251.     /**
  252.      * Gets the PVCoordinates of the GLONASS SV in {@link #getECEF() ECEF frame}.
  253.      *
  254.      * <p>The algorithm is defined at Appendix M.1 from GLONASS Interface Control Document,
  255.      * with automatic differentiation added to compute velocity and
  256.      * acceleration.</p>
  257.      *
  258.      * @param date the computation date
  259.      * @return the GLONASS SV PVCoordinates in {@link #getECEF() ECEF frame}
  260.      */
  261.     public PVCoordinates propagateInEcef(final AbsoluteDate date) {

  262.         // Interval of prediction dTpr
  263.         final UnivariateDerivative2 dTpr = getdTpr(date);

  264.         // Zero
  265.         final UnivariateDerivative2 zero = dTpr.getField().getZero();

  266.         // The number of whole orbits "w" on a prediction interval
  267.         final UnivariateDerivative2 w = FastMath.floor(dTpr.divide(GLONASS_MEAN_DRACONIAN_PERIOD + glonassOrbit.getDeltaT()));

  268.         // Current inclination
  269.         final UnivariateDerivative2 i = zero.add(GLONASS_MEAN_INCLINATION / 180 * GLONASSOrbitalElements.GLONASS_PI + glonassOrbit.getDeltaI());

  270.         // Eccentricity
  271.         final UnivariateDerivative2 e = zero.add(glonassOrbit.getE());

  272.         // Mean draconique period in orbite w+1 and mean motion
  273.         final UnivariateDerivative2 tDR = w.multiply(2.0).add(1.0).multiply(glonassOrbit.getDeltaTDot()).
  274.                                           add(glonassOrbit.getDeltaT()).
  275.                                           add(GLONASS_MEAN_DRACONIAN_PERIOD);
  276.         final UnivariateDerivative2 n = tDR.divide(2.0 * GLONASSOrbitalElements.GLONASS_PI).reciprocal();

  277.         // Semi-major axis : computed by successive approximation
  278.         final UnivariateDerivative2 sma = computeSma(tDR, i, e);

  279.         // (ae / p)^2 term
  280.         final UnivariateDerivative2 p     = sma.multiply(e.multiply(e).negate().add(1.0));
  281.         final UnivariateDerivative2 aeop  = p.divide(GLONASS_EARTH_EQUATORIAL_RADIUS).reciprocal();
  282.         final UnivariateDerivative2 aeop2 = aeop.multiply(aeop);

  283.         // Current longitude of the ascending node
  284.         final UnivariateDerivative2 lambda = computeLambda(dTpr, n, aeop2, i);

  285.         // Current argument of perigee
  286.         final UnivariateDerivative2 pa = computePA(dTpr, n, aeop2, i);

  287.         // Mean longitude at the instant the spacecraft passes the current ascending node
  288.         final UnivariateDerivative2 tanPAo2 = FastMath.tan(pa.divide(2.0));
  289.         final UnivariateDerivative2 coef    = tanPAo2.multiply(FastMath.sqrt(e.negate().add(1.0).divide(e.add(1.0))));
  290.         final UnivariateDerivative2 e0      = FastMath.atan(coef).multiply(2.0).negate();
  291.         final UnivariateDerivative2 m1      = pa.add(e0).subtract(FastMath.sin(e0).multiply(e));

  292.         // Current mean longitude
  293.         final UnivariateDerivative2 correction = dTpr.
  294.                                                  subtract(w.multiply(GLONASS_MEAN_DRACONIAN_PERIOD + glonassOrbit.getDeltaT())).
  295.                                                  subtract(w.multiply(w).multiply(glonassOrbit.getDeltaTDot()));
  296.         final UnivariateDerivative2 m = m1.add(n.multiply(correction));

  297.         // Take into consideration the periodic perturbations
  298.         final FieldSinCos<UnivariateDerivative2> scPa = FastMath.sinCos(pa);
  299.         final UnivariateDerivative2 h = e.multiply(scPa.sin());
  300.         final UnivariateDerivative2 l = e.multiply(scPa.cos());
  301.         // δa1
  302.         final UnivariateDerivative2[] d1 = getParameterDifferentials(sma, i, h, l, m1);
  303.         // δa2
  304.         final UnivariateDerivative2[] d2 = getParameterDifferentials(sma, i, h, l, m);
  305.         // Apply corrections
  306.         final UnivariateDerivative2 smaCorr    = sma.add(d2[0]).subtract(d1[0]);
  307.         final UnivariateDerivative2 hCorr      = h.add(d2[1]).subtract(d1[1]);
  308.         final UnivariateDerivative2 lCorr      = l.add(d2[2]).subtract(d1[2]);
  309.         final UnivariateDerivative2 lambdaCorr = lambda.add(d2[3]).subtract(d1[3]);
  310.         final UnivariateDerivative2 iCorr      = i.add(d2[4]).subtract(d1[4]);
  311.         final UnivariateDerivative2 mCorr      = m.add(d2[5]).subtract(d1[5]);
  312.         final UnivariateDerivative2 eCorr      = FastMath.sqrt(hCorr.multiply(hCorr).add(lCorr.multiply(lCorr)));
  313.         final UnivariateDerivative2 paCorr;
  314.         if (eCorr.getValue() == 0.) {
  315.             paCorr = zero;
  316.         } else {
  317.             if (lCorr.getValue() == eCorr.getValue()) {
  318.                 paCorr = zero.add(0.5 * GLONASSOrbitalElements.GLONASS_PI);
  319.             } else if (lCorr.getValue() == -eCorr.getValue()) {
  320.                 paCorr = zero.add(-0.5 * GLONASSOrbitalElements.GLONASS_PI);
  321.             } else {
  322.                 paCorr = FastMath.atan2(hCorr, lCorr);
  323.             }
  324.         }

  325.         // Eccentric Anomaly
  326.         final UnivariateDerivative2 mk = mCorr.subtract(paCorr);
  327.         final UnivariateDerivative2 ek = getEccentricAnomaly(mk, eCorr);

  328.         // True Anomaly
  329.         final UnivariateDerivative2 vk =  getTrueAnomaly(ek, eCorr);

  330.         // Argument of Latitude
  331.         final UnivariateDerivative2 phik = vk.add(paCorr);

  332.         // Corrected Radius
  333.         final UnivariateDerivative2 pCorr = smaCorr.multiply(eCorr.multiply(eCorr).negate().add(1.0));
  334.         final UnivariateDerivative2 rk    = pCorr.divide(eCorr.multiply(FastMath.cos(vk)).add(1.0));

  335.         // Positions in orbital plane
  336.         final FieldSinCos<UnivariateDerivative2> scPhik = FastMath.sinCos(phik);
  337.         final UnivariateDerivative2 xk = scPhik.cos().multiply(rk);
  338.         final UnivariateDerivative2 yk = scPhik.sin().multiply(rk);

  339.         // Coordinates of position
  340.         final FieldSinCos<UnivariateDerivative2> scL = FastMath.sinCos(lambdaCorr);
  341.         final FieldSinCos<UnivariateDerivative2> scI = FastMath.sinCos(iCorr);
  342.         final FieldVector3D<UnivariateDerivative2> positionwithDerivatives =
  343.                         new FieldVector3D<>(xk.multiply(scL.cos()).subtract(yk.multiply(scL.sin()).multiply(scI.cos())),
  344.                                             xk.multiply(scL.sin()).add(yk.multiply(scL.cos()).multiply(scI.cos())),
  345.                                             yk.multiply(scI.sin()));

  346.         return new PVCoordinates(new Vector3D(positionwithDerivatives.getX().getValue(),
  347.                                               positionwithDerivatives.getY().getValue(),
  348.                                               positionwithDerivatives.getZ().getValue()),
  349.                                  new Vector3D(positionwithDerivatives.getX().getFirstDerivative(),
  350.                                               positionwithDerivatives.getY().getFirstDerivative(),
  351.                                               positionwithDerivatives.getZ().getFirstDerivative()),
  352.                                  new Vector3D(positionwithDerivatives.getX().getSecondDerivative(),
  353.                                               positionwithDerivatives.getY().getSecondDerivative(),
  354.                                               positionwithDerivatives.getZ().getSecondDerivative()));
  355.     }

  356.     /**
  357.      * Gets eccentric anomaly from mean anomaly.
  358.      * <p>The algorithm used to solve the Kepler equation has been published in:
  359.      * "Procedures for  solving Kepler's Equation", A. W. Odell and R. H. Gooding,
  360.      * Celestial Mechanics 38 (1986) 307-334</p>
  361.      * <p>It has been copied from the OREKIT library (KeplerianOrbit class).</p>
  362.      *
  363.      * @param mk the mean anomaly (rad)
  364.      * @param e the eccentricity
  365.      * @return the eccentric anomaly (rad)
  366.      */
  367.     private UnivariateDerivative2 getEccentricAnomaly(final UnivariateDerivative2 mk, final UnivariateDerivative2 e) {

  368.         // reduce M to [-PI PI] interval
  369.         final UnivariateDerivative2 reducedM = new UnivariateDerivative2(MathUtils.normalizeAngle(mk.getValue(), 0.0),
  370.                                                                          mk.getFirstDerivative(),
  371.                                                                          mk.getSecondDerivative());

  372.         // compute start value according to A. W. Odell and R. H. Gooding S12 starter
  373.         UnivariateDerivative2 ek;
  374.         if (FastMath.abs(reducedM.getValue()) < 1.0 / 6.0) {
  375.             if (FastMath.abs(reducedM.getValue()) < Precision.SAFE_MIN) {
  376.                 // this is an Orekit change to the S12 starter.
  377.                 // If reducedM is 0.0, the derivative of cbrt is infinite which induces NaN appearing later in
  378.                 // the computation. As in this case E and M are almost equal, we initialize ek with reducedM
  379.                 ek = reducedM;
  380.             } else {
  381.                 // this is the standard S12 starter
  382.                 ek = reducedM.add(reducedM.multiply(6).cbrt().subtract(reducedM).multiply(e));
  383.             }
  384.         } else {
  385.             if (reducedM.getValue() < 0) {
  386.                 final UnivariateDerivative2 w = reducedM.add(FastMath.PI);
  387.                 ek = reducedM.add(w.multiply(-A).divide(w.subtract(B)).subtract(FastMath.PI).subtract(reducedM).multiply(e));
  388.             } else {
  389.                 final UnivariateDerivative2 minusW = reducedM.subtract(FastMath.PI);
  390.                 ek = reducedM.add(minusW.multiply(A).divide(minusW.add(B)).add(FastMath.PI).subtract(reducedM).multiply(e));
  391.             }
  392.         }

  393.         final UnivariateDerivative2 e1 = e.negate().add(1.0);
  394.         final boolean noCancellationRisk = (e1.getValue() + ek.getValue() * ek.getValue() / 6) >= 0.1;

  395.         // perform two iterations, each consisting of one Halley step and one Newton-Raphson step
  396.         for (int j = 0; j < 2; ++j) {
  397.             final UnivariateDerivative2 f;
  398.             UnivariateDerivative2 fd;
  399.             final UnivariateDerivative2 fdd  = ek.sin().multiply(e);
  400.             final UnivariateDerivative2 fddd = ek.cos().multiply(e);
  401.             if (noCancellationRisk) {
  402.                 f  = ek.subtract(fdd).subtract(reducedM);
  403.                 fd = fddd.subtract(1).negate();
  404.             } else {
  405.                 f  = eMeSinE(ek, e).subtract(reducedM);
  406.                 final UnivariateDerivative2 s = ek.multiply(0.5).sin();
  407.                 fd = s.multiply(s).multiply(e.multiply(2.0)).add(e1);
  408.             }
  409.             final UnivariateDerivative2 dee = f.multiply(fd).divide(f.multiply(0.5).multiply(fdd).subtract(fd.multiply(fd)));

  410.             // update eccentric anomaly, using expressions that limit underflow problems
  411.             final UnivariateDerivative2 w = fd.add(dee.multiply(0.5).multiply(fdd.add(dee.multiply(fdd).divide(3))));
  412.             fd = fd.add(dee.multiply(fdd.add(dee.multiply(0.5).multiply(fdd))));
  413.             ek = ek.subtract(f.subtract(dee.multiply(fd.subtract(w))).divide(fd));
  414.         }

  415.         // expand the result back to original range
  416.         ek = ek.add(mk.getValue() - reducedM.getValue());

  417.         // Returns the eccentric anomaly
  418.         return ek;
  419.     }

  420.     /**
  421.      * Accurate computation of E - e sin(E).
  422.      *
  423.      * @param E eccentric anomaly
  424.      * @param ecc the eccentricity
  425.      * @return E - e sin(E)
  426.      */
  427.     private UnivariateDerivative2 eMeSinE(final UnivariateDerivative2 E, final UnivariateDerivative2 ecc) {
  428.         UnivariateDerivative2 x = E.sin().multiply(ecc.negate().add(1.0));
  429.         final UnivariateDerivative2 mE2 = E.negate().multiply(E);
  430.         UnivariateDerivative2 term = E;
  431.         UnivariateDerivative2 d    = E.getField().getZero();
  432.         // the inequality test below IS intentional and should NOT be replaced by a check with a small tolerance
  433.         for (UnivariateDerivative2 x0 = d.add(Double.NaN); !Double.valueOf(x.getValue()).equals(Double.valueOf(x0.getValue()));) {
  434.             d = d.add(2);
  435.             term = term.multiply(mE2.divide(d.multiply(d.add(1))));
  436.             x0 = x;
  437.             x = x.subtract(term);
  438.         }
  439.         return x;
  440.     }

  441.     /** Gets true anomaly from eccentric anomaly.
  442.     *
  443.     * @param ek the eccentric anomaly (rad)
  444.     * @param ecc the eccentricity
  445.     * @return the true anomaly (rad)
  446.     */
  447.     private UnivariateDerivative2 getTrueAnomaly(final UnivariateDerivative2 ek, final UnivariateDerivative2 ecc) {
  448.         final UnivariateDerivative2 svk = ek.sin().multiply(FastMath.sqrt( ecc.multiply(ecc).negate().add(1.0)));
  449.         final UnivariateDerivative2 cvk = ek.cos().subtract(ecc);
  450.         return svk.atan2(cvk);
  451.     }

  452.     /**
  453.      * Get the interval of prediction.
  454.      *
  455.      * @param date the considered date
  456.      * @return the duration from GLONASS orbit Reference epoch (s)
  457.      */
  458.     private UnivariateDerivative2 getdTpr(final AbsoluteDate date) {
  459.         final TimeScale glonass = dataContext.getTimeScales().getGLONASS();
  460.         final GLONASSDate tEnd = new GLONASSDate(date, glonass);
  461.         final GLONASSDate tSta = new GLONASSDate(glonassOrbit.getDate(), glonass);
  462.         final int n  = tEnd.getDayNumber();
  463.         final int na = tSta.getDayNumber();
  464.         final int deltaN;
  465.         if (na == 27) {
  466.             deltaN = n - na - FastMath.round((float) (n - na) / 1460) * 1460;
  467.         } else {
  468.             deltaN = n - na - FastMath.round((float) (n - na) / 1461) * 1461;
  469.         }
  470.         final UnivariateDerivative2 ti = new UnivariateDerivative2(tEnd.getSecInDay(), 1.0, 0.0);

  471.         return ti.subtract(glonassOrbit.getTime()).add(86400 * deltaN);
  472.     }

  473.     /**
  474.      * Computes the semi-major axis of orbit using technique of successive approximations.
  475.      * @param tDR mean draconique period (s)
  476.      * @param i current inclination (rad)
  477.      * @param e eccentricity
  478.      * @return the semi-major axis (m).
  479.      */
  480.     private UnivariateDerivative2 computeSma(final UnivariateDerivative2 tDR,
  481.                                              final UnivariateDerivative2 i,
  482.                                              final UnivariateDerivative2 e) {

  483.         // Zero
  484.         final UnivariateDerivative2 zero = tDR.getField().getZero();

  485.         // If one of the input parameter is equal to Double.NaN, an infinite loop can occur.
  486.         // In that case, we do not compute the value of the semi major axis.
  487.         // We decided to return a Double.NaN value instead.
  488.         if (Double.isNaN(tDR.getValue()) || Double.isNaN(i.getValue()) || Double.isNaN(e.getValue())) {
  489.             return zero.add(Double.NaN);
  490.         }

  491.         // Common parameters
  492.         final UnivariateDerivative2 sinI         = FastMath.sin(i);
  493.         final UnivariateDerivative2 sin2I        = sinI.multiply(sinI);
  494.         final UnivariateDerivative2 ome2         = e.multiply(e).negate().add(1.0);
  495.         final UnivariateDerivative2 ome2Pow3o2   = FastMath.sqrt(ome2).multiply(ome2);
  496.         final UnivariateDerivative2 pa           = zero.add(glonassOrbit.getPa());
  497.         final UnivariateDerivative2 cosPA        = FastMath.cos(pa);
  498.         final UnivariateDerivative2 opecosPA     = e.multiply(cosPA).add(1.0);
  499.         final UnivariateDerivative2 opecosPAPow2 = opecosPA.multiply(opecosPA);
  500.         final UnivariateDerivative2 opecosPAPow3 = opecosPAPow2.multiply(opecosPA);

  501.         // Initial approximation
  502.         UnivariateDerivative2 tOCK = tDR;

  503.         // Successive approximations
  504.         // The process of approximation ends when fulfilling the following condition: |a(n+1) - a(n)| < 1cm
  505.         UnivariateDerivative2 an   = zero;
  506.         UnivariateDerivative2 anp1 = zero;
  507.         boolean isLastStep = false;
  508.         while (!isLastStep) {

  509.             // a(n+1) computation
  510.             final UnivariateDerivative2 tOCKo2p     = tOCK.divide(2.0 * GLONASSOrbitalElements.GLONASS_PI);
  511.             final UnivariateDerivative2 tOCKo2pPow2 = tOCKo2p.multiply(tOCKo2p);
  512.             anp1 = FastMath.cbrt(tOCKo2pPow2.multiply(GLONASSOrbitalElements.GLONASS_MU));

  513.             // p(n+1) computation
  514.             final UnivariateDerivative2 p = anp1.multiply(ome2);

  515.             // Tock(n+1) computation
  516.             final UnivariateDerivative2 aeop  = p.divide(GLONASS_EARTH_EQUATORIAL_RADIUS).reciprocal();
  517.             final UnivariateDerivative2 aeop2 = aeop.multiply(aeop);
  518.             final UnivariateDerivative2 term1 = aeop2.multiply(GLONASS_J20).multiply(1.5);
  519.             final UnivariateDerivative2 term2 = sin2I.multiply(2.5).negate().add(2.0);
  520.             final UnivariateDerivative2 term3 = ome2Pow3o2.divide(opecosPAPow2);
  521.             final UnivariateDerivative2 term4 = opecosPAPow3.divide(ome2);
  522.             tOCK = tDR.divide(term1.multiply(term2.multiply(term3).add(term4)).negate().add(1.0));

  523.             // Check convergence
  524.             if (FastMath.abs(anp1.subtract(an).getReal()) <= 0.01) {
  525.                 isLastStep = true;
  526.             }

  527.             an = anp1;
  528.         }

  529.         return an;

  530.     }

  531.     /**
  532.      * Computes the current longitude of the ascending node.
  533.      * @param dTpr interval of prediction (s)
  534.      * @param n mean motion (rad/s)
  535.      * @param aeop2 square of the ratio between the radius of the ellipsoid and p, with p = sma * (1 - ecc²)
  536.      * @param i inclination (rad)
  537.      * @return the current longitude of the ascending node (rad)
  538.      */
  539.     private UnivariateDerivative2 computeLambda(final UnivariateDerivative2 dTpr,
  540.                                                 final UnivariateDerivative2 n,
  541.                                                 final UnivariateDerivative2 aeop2,
  542.                                                 final UnivariateDerivative2 i) {
  543.         final UnivariateDerivative2 cosI = FastMath.cos(i);
  544.         final UnivariateDerivative2 precession = aeop2.multiply(n).multiply(cosI).multiply(1.5 * GLONASS_J20);
  545.         return dTpr.multiply(precession.add(GLONASS_AV)).negate().add(glonassOrbit.getLambda());
  546.     }

  547.     /**
  548.      * Computes the current argument of perigee.
  549.      * @param dTpr interval of prediction (s)
  550.      * @param n mean motion (rad/s)
  551.      * @param aeop2 square of the ratio between the radius of the ellipsoid and p, with p = sma * (1 - ecc²)
  552.      * @param i inclination (rad)
  553.      * @return the current argument of perigee (rad)
  554.      */
  555.     private UnivariateDerivative2 computePA(final UnivariateDerivative2 dTpr,
  556.                                             final UnivariateDerivative2 n,
  557.                                             final UnivariateDerivative2 aeop2,
  558.                                             final UnivariateDerivative2 i) {
  559.         final UnivariateDerivative2 cosI  = FastMath.cos(i);
  560.         final UnivariateDerivative2 cos2I = cosI.multiply(cosI);
  561.         final UnivariateDerivative2 precession = aeop2.multiply(n).multiply(cos2I.multiply(5.0).negate().add(1.0)).multiply(0.75 * GLONASS_J20);
  562.         return dTpr.multiply(precession).negate().add(glonassOrbit.getPa());
  563.     }

  564.     /**
  565.      * Computes the differentials δa<sub>i</sub>.
  566.      * <p>
  567.      * The value of i depends of the type of longitude (i = 2 for the current mean longitude;
  568.      * i = 1 for the mean longitude at the instant the spacecraft passes the current ascending node)
  569.      * </p>
  570.      * @param a semi-major axis (m)
  571.      * @param i inclination (rad)
  572.      * @param h x component of the eccentricity (rad)
  573.      * @param l y component of the eccentricity (rad)
  574.      * @param m longitude (current or at the ascending node instant)
  575.      * @return the differentials of the orbital parameters
  576.      */
  577.     private UnivariateDerivative2[] getParameterDifferentials(final UnivariateDerivative2 a, final UnivariateDerivative2 i,
  578.                                                               final UnivariateDerivative2 h, final UnivariateDerivative2 l,
  579.                                                               final UnivariateDerivative2 m) {

  580.         // B constant
  581.         final UnivariateDerivative2 aeoa  = a.divide(GLONASS_EARTH_EQUATORIAL_RADIUS).reciprocal();
  582.         final UnivariateDerivative2 aeoa2 = aeoa.multiply(aeoa);
  583.         final UnivariateDerivative2 b     = aeoa2.multiply(1.5 * GLONASS_J20);

  584.         // Commons Parameters
  585.         final FieldSinCos<UnivariateDerivative2> scI   = FastMath.sinCos(i);
  586.         final FieldSinCos<UnivariateDerivative2> scLk  = FastMath.sinCos(m);
  587.         final FieldSinCos<UnivariateDerivative2> sc2Lk = FieldSinCos.sum(scLk, scLk);
  588.         final FieldSinCos<UnivariateDerivative2> sc3Lk = FieldSinCos.sum(scLk, sc2Lk);
  589.         final FieldSinCos<UnivariateDerivative2> sc4Lk = FieldSinCos.sum(sc2Lk, sc2Lk);
  590.         final UnivariateDerivative2 cosI   = scI.cos();
  591.         final UnivariateDerivative2 sinI   = scI.sin();
  592.         final UnivariateDerivative2 cosI2  = cosI.multiply(cosI);
  593.         final UnivariateDerivative2 sinI2  = sinI.multiply(sinI);
  594.         final UnivariateDerivative2 cosLk  = scLk.cos();
  595.         final UnivariateDerivative2 sinLk  = scLk.sin();
  596.         final UnivariateDerivative2 cos2Lk = sc2Lk.cos();
  597.         final UnivariateDerivative2 sin2Lk = sc2Lk.sin();
  598.         final UnivariateDerivative2 cos3Lk = sc3Lk.cos();
  599.         final UnivariateDerivative2 sin3Lk = sc3Lk.sin();
  600.         final UnivariateDerivative2 cos4Lk = sc4Lk.cos();
  601.         final UnivariateDerivative2 sin4Lk = sc4Lk.sin();

  602.         // h*cos(nLk), l*cos(nLk), h*sin(nLk) and l*sin(nLk)
  603.         // n = 1
  604.         final UnivariateDerivative2 hCosLk = h.multiply(cosLk);
  605.         final UnivariateDerivative2 hSinLk = h.multiply(sinLk);
  606.         final UnivariateDerivative2 lCosLk = l.multiply(cosLk);
  607.         final UnivariateDerivative2 lSinLk = l.multiply(sinLk);
  608.         // n = 2
  609.         final UnivariateDerivative2 hCos2Lk = h.multiply(cos2Lk);
  610.         final UnivariateDerivative2 hSin2Lk = h.multiply(sin2Lk);
  611.         final UnivariateDerivative2 lCos2Lk = l.multiply(cos2Lk);
  612.         final UnivariateDerivative2 lSin2Lk = l.multiply(sin2Lk);
  613.         // n = 3
  614.         final UnivariateDerivative2 hCos3Lk = h.multiply(cos3Lk);
  615.         final UnivariateDerivative2 hSin3Lk = h.multiply(sin3Lk);
  616.         final UnivariateDerivative2 lCos3Lk = l.multiply(cos3Lk);
  617.         final UnivariateDerivative2 lSin3Lk = l.multiply(sin3Lk);
  618.         // n = 4
  619.         final UnivariateDerivative2 hCos4Lk = h.multiply(cos4Lk);
  620.         final UnivariateDerivative2 hSin4Lk = h.multiply(sin4Lk);
  621.         final UnivariateDerivative2 lCos4Lk = l.multiply(cos4Lk);
  622.         final UnivariateDerivative2 lSin4Lk = l.multiply(sin4Lk);

  623.         // 1 - (3 / 2)*sin²i
  624.         final UnivariateDerivative2 om3o2xSinI2 = sinI2.multiply(1.5).negate().add(1.0);

  625.         // Compute Differentials
  626.         // δa
  627.         final UnivariateDerivative2 dakT1 = b.multiply(2.0).multiply(om3o2xSinI2).multiply(lCosLk.add(hSinLk));
  628.         final UnivariateDerivative2 dakT2 = b.multiply(sinI2).multiply(hSinLk.multiply(0.5).subtract(lCosLk.multiply(0.5)).
  629.                                                                      add(cos2Lk).add(lCos3Lk.multiply(3.5)).add(hSin3Lk.multiply(3.5)));
  630.         final UnivariateDerivative2 dak = dakT1.add(dakT2);

  631.         // δh
  632.         final UnivariateDerivative2 dhkT1 = b.multiply(om3o2xSinI2).multiply(sinLk.add(lSin2Lk.multiply(1.5)).subtract(hCos2Lk.multiply(1.5)));
  633.         final UnivariateDerivative2 dhkT2 = b.multiply(sinI2).multiply(0.25).multiply(sinLk.subtract(sin3Lk.multiply(SEVEN_THIRD)).add(lSin2Lk.multiply(5.0)).
  634.                                                                                     subtract(lSin4Lk.multiply(8.5)).add(hCos4Lk.multiply(8.5)).add(hCos2Lk));
  635.         final UnivariateDerivative2 dhkT3 = lSin2Lk.multiply(cosI2).multiply(b).multiply(0.5).negate();
  636.         final UnivariateDerivative2 dhk = dhkT1.subtract(dhkT2).add(dhkT3);

  637.         // δl
  638.         final UnivariateDerivative2 dlkT1 = b.multiply(om3o2xSinI2).multiply(cosLk.add(lCos2Lk.multiply(1.5)).add(hSin2Lk.multiply(1.5)));
  639.         final UnivariateDerivative2 dlkT2 = b.multiply(sinI2).multiply(0.25).multiply(cosLk.negate().subtract(cos3Lk.multiply(SEVEN_THIRD)).subtract(hSin2Lk.multiply(5.0)).
  640.                                                                                     subtract(lCos4Lk.multiply(8.5)).subtract(hSin4Lk.multiply(8.5)).add(lCos2Lk));
  641.         final UnivariateDerivative2 dlkT3 = hSin2Lk.multiply(cosI2).multiply(b).multiply(0.5);
  642.         final UnivariateDerivative2 dlk = dlkT1.subtract(dlkT2).add(dlkT3);

  643.         // δλ
  644.         final UnivariateDerivative2 dokT1 = b.negate().multiply(cosI);
  645.         final UnivariateDerivative2 dokT2 = lSinLk.multiply(3.5).subtract(hCosLk.multiply(2.5)).subtract(sin2Lk.multiply(0.5)).
  646.                                           subtract(lSin3Lk.multiply(SEVEN_SIXTH)).add(hCos3Lk.multiply(SEVEN_SIXTH));
  647.         final UnivariateDerivative2 dok = dokT1.multiply(dokT2);

  648.         // δi
  649.         final UnivariateDerivative2 dik = b.multiply(sinI).multiply(cosI).multiply(0.5).
  650.                         multiply(lCosLk.negate().add(hSinLk).add(cos2Lk).add(lCos3Lk.multiply(SEVEN_THIRD)).add(hSin3Lk.multiply(SEVEN_THIRD)));

  651.         // δL
  652.         final UnivariateDerivative2 dLkT1 = b.multiply(2.0).multiply(om3o2xSinI2).multiply(lSinLk.multiply(1.75).subtract(hCosLk.multiply(1.75)));
  653.         final UnivariateDerivative2 dLkT2 = b.multiply(sinI2).multiply(3.0).multiply(hCosLk.multiply(SEVEN_24TH).negate().subtract(lSinLk.multiply(SEVEN_24TH)).
  654.                                                                                    subtract(hCos3Lk.multiply(FN_72TH)).add(lSin3Lk.multiply(FN_72TH)).add(sin2Lk.multiply(0.25)));
  655.         final UnivariateDerivative2 dLkT3 = b.multiply(cosI2).multiply(lSinLk.multiply(3.5).subtract(hCosLk.multiply(2.5)).subtract(sin2Lk.multiply(0.5)).
  656.                                                                      subtract(lSin3Lk.multiply(SEVEN_SIXTH)).add(hCos3Lk.multiply(SEVEN_SIXTH)));
  657.         final UnivariateDerivative2 dLk = dLkT1.add(dLkT2).add(dLkT3);

  658.         // Final array
  659.         final UnivariateDerivative2[] differentials = MathArrays.buildArray(a.getField(), 6);
  660.         differentials[0] = dak.multiply(a);
  661.         differentials[1] = dhk;
  662.         differentials[2] = dlk;
  663.         differentials[3] = dok;
  664.         differentials[4] = dik;
  665.         differentials[5] = dLk;

  666.         return differentials;
  667.     }

  668.     /** {@inheritDoc} */
  669.     protected double getMass(final AbsoluteDate date) {
  670.         return mass;
  671.     }

  672.     /**
  673.      * Get the Earth gravity coefficient used for GLONASS propagation.
  674.      * @return the Earth gravity coefficient.
  675.      */
  676.     public static double getMU() {
  677.         return GLONASSOrbitalElements.GLONASS_MU;
  678.     }

  679.     /**
  680.      * Gets the underlying GLONASS orbital elements.
  681.      *
  682.      * @return the underlying GLONASS orbital elements
  683.      */
  684.     public GLONASSOrbitalElements getGLONASSOrbitalElements() {
  685.         return glonassOrbit;
  686.     }

  687.     /**
  688.      * Gets the Earth Centered Inertial frame used to propagate the orbit.
  689.      * @return the ECI frame
  690.      */
  691.     public Frame getECI() {
  692.         return eci;
  693.     }

  694.     /**
  695.      * Gets the Earth Centered Earth Fixed frame used to propagate GLONASS orbits.
  696.      * @return the ECEF frame
  697.      */
  698.     public Frame getECEF() {
  699.         return ecef;
  700.     }

  701.     /** {@inheritDoc} */
  702.     public Frame getFrame() {
  703.         return eci;
  704.     }

  705.     /** {@inheritDoc} */
  706.     public void resetInitialState(final SpacecraftState state) {
  707.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  708.     }

  709.     /** {@inheritDoc} */
  710.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  711.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  712.     }

  713.     /** {@inheritDoc} */
  714.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  715.         // Gets the PVCoordinates in ECEF frame
  716.         final PVCoordinates pvaInECEF = propagateInEcef(date);
  717.         // Transforms the PVCoordinates to ECI frame
  718.         final PVCoordinates pvaInECI = ecef.getTransformTo(eci, date).transformPVCoordinates(pvaInECEF);
  719.         // Returns the Cartesian orbit
  720.         return new CartesianOrbit(pvaInECI, eci, date, GLONASSOrbitalElements.GLONASS_MU);
  721.     }

  722. }