GLONASSNumericalPropagator.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.propagation.numerical;

  18. import java.util.Arrays;

  19. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  20. import org.hipparchus.ode.ODEIntegrator;
  21. import org.hipparchus.ode.nonstiff.ClassicalRungeKuttaIntegrator;
  22. import org.hipparchus.util.FastMath;
  23. import org.hipparchus.util.MathUtils;
  24. import org.hipparchus.util.SinCos;
  25. import org.orekit.attitudes.Attitude;
  26. import org.orekit.attitudes.AttitudeProvider;
  27. import org.orekit.data.DataContext;
  28. import org.orekit.errors.OrekitException;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.frames.Frame;
  31. import org.orekit.orbits.CartesianOrbit;
  32. import org.orekit.orbits.Orbit;
  33. import org.orekit.orbits.OrbitType;
  34. import org.orekit.orbits.PositionAngle;
  35. import org.orekit.propagation.PropagationType;
  36. import org.orekit.propagation.SpacecraftState;
  37. import org.orekit.propagation.analytical.gnss.data.GLONASSAlmanac;
  38. import org.orekit.propagation.analytical.gnss.data.GLONASSNavigationMessage;
  39. import org.orekit.propagation.analytical.gnss.data.GLONASSOrbitalElements;
  40. import org.orekit.propagation.analytical.gnss.data.GNSSConstants;
  41. import org.orekit.propagation.integration.AbstractIntegratedPropagator;
  42. import org.orekit.propagation.integration.StateMapper;
  43. import org.orekit.time.AbsoluteDate;
  44. import org.orekit.time.GLONASSDate;
  45. import org.orekit.utils.AbsolutePVCoordinates;
  46. import org.orekit.utils.Constants;
  47. import org.orekit.utils.IERSConventions;
  48. import org.orekit.utils.PVCoordinates;
  49. import org.orekit.utils.TimeStampedPVCoordinates;

  50. /**
  51.  * This class propagates GLONASS orbits using numerical integration.
  52.  * <p>
  53.  * As recommended by the GLONASS Interface Control Document (ICD),
  54.  * a {@link ClassicalRungeKuttaIntegrator  4th order Runge-Kutta technique}
  55.  * shall be used to integrate the equations.
  56.  * </p>
  57.  * <p>
  58.  * Classical used of this orbit propagator is to compute GLONASS satellite
  59.  * coordinates from the navigation message.
  60.  * </p>
  61.  * <p>
  62.  * If the projections of luni-solar accelerations to axes of
  63.  * Greenwich geocentric coordinates {@link GLONASSOrbitalElements#getXDotDot() X''(tb)},
  64.  * {@link GLONASSOrbitalElements#getYDotDot() Y''(tb)} and {@link GLONASSOrbitalElements#getZDotDot() Z''(tb)}
  65.  * are available in the navigation message; a transformation is performed to convert these
  66.  * accelerations into the correct coordinate system. In the case where they are not
  67.  * available into the navigation message, these accelerations are computed.
  68.  * </p>
  69.  * <p>
  70.  * <b>Caution:</b> The Glonass numerical propagator can only be used with {@link GLONASSNavigationMessage}.
  71.  * Using this propagator with a {@link GLONASSAlmanac} is prone to error.
  72.  * </p>
  73.  *
  74.  * @see <a href="http://russianspacesystems.ru/wp-content/uploads/2016/08/ICD-GLONASS-CDMA-General.-Edition-1.0-2016.pdf">
  75.  *       GLONASS Interface Control Document</a>
  76.  *
  77.  * @author Bryan Cazabonne
  78.  *
  79.  */
  80. public class GLONASSNumericalPropagator extends AbstractIntegratedPropagator {

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

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

  85.     /** Value of the Earth's rotation rate in rad/s (See Ref). */
  86.     private static final double GLONASS_AV = 7.2921151467e-5;

  87.     // Data used to solve Kepler's equation
  88.     /** First coefficient to compute Kepler equation solver starter. */
  89.     private static final double A;

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

  92.     static {
  93.         final double k1 = 3 * FastMath.PI + 2;
  94.         final double k2 = FastMath.PI - 1;
  95.         final double k3 = 6 * FastMath.PI - 1;
  96.         A  = 3 * k2 * k2 / k1;
  97.         B  = k3 * k3 / (6 * k1);
  98.     }

  99.     /** The GLONASS orbital elements used. */
  100.     private final GLONASSOrbitalElements glonassOrbit;

  101.     /** Initial date in GLONASS form. */
  102.     private final GLONASSDate initDate;

  103.     /** The spacecraft mass (kg). */
  104.     private final double mass;

  105.     /** The ECI frame used for GLONASS propagation. */
  106.     private final Frame eci;

  107.     /** Direction cosines and distance of perturbing body: Moon.
  108.      * <p>
  109.      * <ul>
  110.      * <li>double[0] = ξ<sub>m</sub></li>
  111.      * <li>double[1] = η<sub>m</sub></li>
  112.      * <li>double[2] = ψ<sub>m</sub></li>
  113.      * <li>double[3] = r<sub>m</sub></li>
  114.      * </ul>
  115.      * </p>
  116.      */
  117.     private double[] moonElements;

  118.     /** Direction cosines and distance of perturbing body: Sun.
  119.      * <p>
  120.      * <ul>
  121.      * <li>double[0] = ξ<sub>s</sub></li>
  122.      * <li>double[1] = η<sub>s</sub></li>
  123.      * <li>double[2] = ψ<sub>s</sub></li>
  124.      * <li>double[3] = r<sub>s</sub></li>
  125.      * </ul>
  126.      * </p>
  127.      */
  128.     private double[] sunElements;

  129.     /** Flag for availability of projections of acceleration transmitted within the navigation message. */
  130.     private final boolean isAccAvailable;

  131.     /** Data context used for propagation. */
  132.     private final DataContext dataContext;

  133.     /**
  134.      * Private constructor.
  135.      * @param integrator Runge-Kutta integrator
  136.      * @param glonassOrbit Glonass orbital elements
  137.      * @param eci Earth Centered Inertial frame
  138.      * @param provider Attitude provider
  139.      * @param mass Satellite mass (kg)
  140.      * @param context Data context
  141.      * @param isAccAvailable true if the acceleration  is transmitted within the navigation message
  142.      */
  143.     public GLONASSNumericalPropagator(final ClassicalRungeKuttaIntegrator integrator,
  144.                                       final GLONASSOrbitalElements glonassOrbit,
  145.                                       final Frame eci, final AttitudeProvider provider,
  146.                                       final double mass, final DataContext context,
  147.                                       final boolean isAccAvailable) {
  148.         super(integrator, PropagationType.OSCULATING);
  149.         this.dataContext = context;
  150.         this.isAccAvailable = isAccAvailable;
  151.         // Stores the GLONASS orbital elements
  152.         this.glonassOrbit = glonassOrbit;
  153.         // Sets the Earth Centered Inertial frame
  154.         this.eci = eci;
  155.         // Sets the mass
  156.         this.mass = mass;
  157.         this.initDate = new GLONASSDate(
  158.                 glonassOrbit.getDate(),
  159.                 dataContext.getTimeScales().getGLONASS());

  160.         // Initialize state mapper
  161.         initMapper();
  162.         setInitialState();
  163.         setAttitudeProvider(provider);
  164.         setOrbitType(OrbitType.CARTESIAN);
  165.         // It is not meaningful for propagation in Cartesian parameters
  166.         setPositionAngleType(PositionAngle.TRUE);
  167.         setMu(GNSSConstants.GLONASS_MU);

  168.         // As recommended by GLONASS ICD (2016), the direction cosines and distance
  169.         // of perturbing body are calculated one time (at tb).
  170.         if (!isAccAvailable) {
  171.             computeMoonElements(initDate);
  172.             computeSunElements(initDate);
  173.         }
  174.     }

  175.     /**
  176.      * Gets the underlying GLONASS orbital elements.
  177.      *
  178.      * @return the underlying GLONASS orbital elements
  179.      */
  180.     public GLONASSOrbitalElements getGLONASSOrbitalElements() {
  181.         return glonassOrbit;
  182.     }

  183.     /** {@inheritDoc} */
  184.     @Override
  185.     public SpacecraftState propagate(final AbsoluteDate date) {
  186.         // Spacecraft state in inertial frame
  187.         final SpacecraftState stateInInertial = super.propagate(date);

  188.         // Build the spacecraft state in inertial frame
  189.         final PVCoordinates pvInPZ90 = getPVInPZ90(stateInInertial);
  190.         final AbsolutePVCoordinates absPV = new AbsolutePVCoordinates(
  191.                 dataContext.getFrames().getPZ9011(IERSConventions.IERS_2010, true),
  192.                 stateInInertial.getDate(), pvInPZ90);
  193.         final TimeStampedPVCoordinates pvInInertial = absPV.getPVCoordinates(eci);
  194.         final SpacecraftState transformedState = new SpacecraftState(new CartesianOrbit(pvInInertial, eci, pvInInertial.getDate(), GNSSConstants.GLONASS_MU),
  195.                                                                      stateInInertial.getAttitude(),
  196.                                                                      stateInInertial.getMass(),
  197.                                                                      stateInInertial.getAdditionalStatesValues(),
  198.                                                                      stateInInertial.getAdditionalStatesDerivatives());

  199.         return transformedState;
  200.     }

  201.     /**
  202.      * Set the initial state.
  203.      * <p>
  204.      * The initial conditions on position and velocity are in the ECEF coordinate system PZ-90.
  205.      * Previous to orbit integration, they must be transformed to an absolute inertial coordinate system.
  206.      * </p>
  207.      */
  208.     private void setInitialState() {

  209.         // Transform initial PV coordinates to an absolute inertial coordinate system.
  210.         final PVCoordinates pvInInertial = getPVInInertial(initDate);

  211.         // Create a new orbit
  212.         final Orbit orbit = new CartesianOrbit(pvInInertial,
  213.                                                eci, initDate.getDate(),
  214.                                                GNSSConstants.GLONASS_MU);

  215.         // Reset the initial state to apply the transformation
  216.         resetInitialState(new SpacecraftState(orbit, mass));
  217.     }

  218.     /**
  219.      * This method computes the direction cosines and the distance used to
  220.      * compute the gravitational perturbations of the Moon.
  221.      *
  222.      * @param date the computation date in GLONASS scale
  223.      */
  224.     private void computeMoonElements(final GLONASSDate date) {

  225.         moonElements = new double[4];

  226.         // Constants
  227.         // Semi-major axis of the Moon's orbit (m)
  228.         final double am = 3.84385243e8;
  229.         // The Moon's orbit eccentricity
  230.         final double em = 0.054900489;
  231.         // Mean inclination of the Moon's orbit to the ecliptic (rad)
  232.         final double im = 0.0898041080;

  233.         // Computed parameters
  234.         // Time from epoch 2000 to the instant tb of GLONASS ephemeris broadcast
  235.         final double dtoJD = (glonassOrbit.getTime() - 10800.) / Constants.JULIAN_DAY;
  236.         final double t  = (date.getJD0() + dtoJD - 2451545.0) / 36525;
  237.         final double t2 = t * t;
  238.         // Mean inclination of Earth equator to ecliptic (rad)
  239.         final double eps = 0.4090926006 - 0.0002270711 * t;
  240.         // Mean longitude of the Moon's orbit perigee (rad)
  241.         final double gammaM = 1.4547885346 + 71.0176852437 * t - 0.0001801481 * t2;
  242.         // Mean longitude of the ascending node of the Moon (rad)
  243.         final double omegaM = 2.1824391966 - 33.7570459536 * t + 0.0000362262 * t2;
  244.         // Mean anomaly of the Moon (rad)
  245.         final double qm = 2.3555557435 + 8328.6914257190 * t + 0.0001545547 * t2;

  246.         // Commons parameters
  247.         final SinCos scOm  = FastMath.sinCos(omegaM);
  248.         final SinCos scIm  = FastMath.sinCos(im);
  249.         final SinCos scEs  = FastMath.sinCos(eps);
  250.         final SinCos scGm  = FastMath.sinCos(gammaM);
  251.         final double cosOm = scOm.cos();
  252.         final double sinOm = scOm.sin();
  253.         final double cosIm = scIm.cos();
  254.         final double sinIm = scIm.sin();
  255.         final double cosEs = scEs.cos();
  256.         final double sinEs = scEs.sin();
  257.         final double cosGm = scGm.cos();
  258.         final double sinGm = scGm.sin();

  259.         // Intermediate parameters
  260.         final double psiStar = cosOm * sinIm;
  261.         final double etaStar = sinOm * sinIm;
  262.         final double epsStar = 1. - cosOm * cosOm * (1. - cosIm);
  263.         final double eps11 = sinOm * cosOm * (1. - cosIm);
  264.         final double eps12 = 1. - sinOm * sinOm * (1. - cosIm);
  265.         final double eta11 = epsStar * cosEs - psiStar * sinEs;
  266.         final double eta12 = eps11 * cosEs + etaStar * sinEs;
  267.         final double psi11 = epsStar * sinEs + psiStar * cosEs;
  268.         final double psi12 = eps11 * sinEs - etaStar * cosEs;

  269.         // Eccentric Anomaly
  270.         final double ek = getEccentricAnomaly(qm, em);

  271.         // True Anomaly
  272.         final double vk    = getTrueAnomaly(ek, em);
  273.         final SinCos scVk  = FastMath.sinCos(vk);
  274.         final double sinVk = scVk.sin();
  275.         final double cosVk = scVk.cos();

  276.         // Direction cosine
  277.         final double epsM = eps11 * (sinVk * cosGm + cosVk * sinGm) + eps12 * (cosVk * cosGm - sinVk * sinGm);
  278.         final double etaM = eta11 * (sinVk * cosGm + cosVk * sinGm) + eta12 * (cosVk * cosGm - sinVk * sinGm);
  279.         final double psiM = psi11 * (sinVk * cosGm + cosVk * sinGm) + psi12 * (cosVk * cosGm - sinVk * sinGm);

  280.         // Distance
  281.         final double rm = am * (1. - em * FastMath.cos(ek));

  282.         moonElements[0] = epsM;
  283.         moonElements[1] = etaM;
  284.         moonElements[2] = psiM;
  285.         moonElements[3] = rm;

  286.     }

  287.     /**
  288.      * This method computes the direction cosines and the distance used to
  289.      * compute the gravitational perturbations of the Sun.
  290.      *
  291.      * @param date the computation date in GLONASS scale
  292.      */
  293.     private void computeSunElements(final GLONASSDate date) {

  294.         sunElements = new double[4];

  295.         // Constants
  296.         //  Major semi-axis of the Earth’s orbit around the Sun (m)
  297.         final double as = 1.49598e11;
  298.         // The eccentricity of the Earth’s orbit around the Sun
  299.         final double es = 0.016719;

  300.         // Computed parameters
  301.         // Time from epoch 2000 to the instant tb of GLONASS ephemeris broadcast
  302.         final double dtoJD = (glonassOrbit.getTime() - 10800.) / Constants.JULIAN_DAY;
  303.         final double t  = (date.getJD0() + dtoJD - 2451545.0) / 36525;
  304.         final double t2 = t * t;
  305.         // Mean inclination of Earth equator to ecliptic (rad)
  306.         final double eps = 0.4090926006 - 0.0002270711 * t;
  307.         // Mean tropic longitude of the Sun orbit perigee (rad)
  308.         final double ws = -7.6281824375 + 0.0300101976 * t + 0.0000079741 * t2;
  309.         // Mean anomaly of the Sun (rad)
  310.         final double qs = 6.2400601269 + 628.3019551714 * t - 0.0000026820 * t2;

  311.         // Eccentric Anomaly
  312.         final double ek = getEccentricAnomaly(qs, es);

  313.         // True Anomaly
  314.         final double vk    =  getTrueAnomaly(ek, es);
  315.         final SinCos scVk  = FastMath.sinCos(vk);
  316.         final double sinVk = scVk.sin();
  317.         final double cosVk = scVk.cos();

  318.         // Commons parameters
  319.         final SinCos scWs  = FastMath.sinCos(ws);
  320.         final SinCos scEs  = FastMath.sinCos(eps);
  321.         final double cosWs = scWs.cos();
  322.         final double sinWs = scWs.sin();
  323.         final double cosEs = scEs.cos();
  324.         final double sinEs = scEs.sin();

  325.         // Direction cosine
  326.         final double epsS = cosVk * cosWs - sinVk * sinWs;
  327.         final double etaS = cosEs * (sinVk * cosWs + cosVk * sinWs);
  328.         final double psiS = sinEs * (sinVk * cosWs + cosVk * sinWs);

  329.         // Distance
  330.         final double rs = as * (1. - es * FastMath.cos(ek));

  331.         sunElements[0] = epsS;
  332.         sunElements[1] = etaS;
  333.         sunElements[2] = psiS;
  334.         sunElements[3] = rs;

  335.     }

  336.     /**
  337.      * Computes the elliptic eccentric anomaly from the mean anomaly.
  338.      * <p>
  339.      * The algorithm used here for solving Kepler equation has been published
  340.      * in: "Procedures for  solving Kepler's Equation", A. W. Odell and
  341.      * R. H. Gooding, Celestial Mechanics 38 (1986) 307-334
  342.      * </p>
  343.      * <p>It has been copied from the OREKIT library (KeplerianOrbit class).</p>
  344.      *
  345.      * @param M mean anomaly (rad)
  346.      * @param e eccentricity
  347.      * @return E the eccentric anomaly
  348.      */
  349.     private double getEccentricAnomaly(final double M, final double e) {

  350.         // reduce M to [-PI PI) interval
  351.         final double reducedM = MathUtils.normalizeAngle(M, 0.0);

  352.         // compute start value according to A. W. Odell and R. H. Gooding S12 starter
  353.         double E;
  354.         if (FastMath.abs(reducedM) < 1.0 / 6.0) {
  355.             E = reducedM + e * (FastMath.cbrt(6 * reducedM) - reducedM);
  356.         } else {
  357.             if (reducedM < 0) {
  358.                 final double w = FastMath.PI + reducedM;
  359.                 E = reducedM + e * (A * w / (B - w) - FastMath.PI - reducedM);
  360.             } else {
  361.                 final double w = FastMath.PI - reducedM;
  362.                 E = reducedM + e * (FastMath.PI - A * w / (B - w) - reducedM);
  363.             }
  364.         }

  365.         final double e1 = 1 - e;
  366.         final boolean noCancellationRisk = (e1 + E * E / 6) >= 0.1;

  367.         // perform two iterations, each consisting of one Halley step and one Newton-Raphson step
  368.         for (int j = 0; j < 2; ++j) {
  369.             final double f;
  370.             double fd;
  371.             final SinCos scE  = FastMath.sinCos(E);
  372.             final double fdd  = e * scE.sin();
  373.             final double fddd = e * scE.cos();
  374.             if (noCancellationRisk) {
  375.                 f  = (E - fdd) - reducedM;
  376.                 fd = 1 - fddd;
  377.             } else {
  378.                 f  = eMeSinE(E, e) - reducedM;
  379.                 final double s = FastMath.sin(0.5 * E);
  380.                 fd = e1 + 2 * e * s * s;
  381.             }
  382.             final double dee = f * fd / (0.5 * f * fdd - fd * fd);

  383.             // update eccentric anomaly, using expressions that limit underflow problems
  384.             final double w = fd + 0.5 * dee * (fdd + dee * fddd / 3);
  385.             fd += dee * (fdd + 0.5 * dee * fddd);
  386.             E  -= (f - dee * (fd - w)) / fd;

  387.         }

  388.         // expand the result back to original range
  389.         E += M - reducedM;

  390.         return E;

  391.     }

  392.     /**
  393.      * Accurate computation of E - e sin(E).
  394.      *
  395.      * @param E eccentric anomaly
  396.      * @param e eccentricity
  397.      * @return E - e sin(E)
  398.      */
  399.     private static double eMeSinE(final double E, final double e) {
  400.         double x = (1 - e) * FastMath.sin(E);
  401.         final double mE2 = -E * E;
  402.         double term = E;
  403.         double d    = 0;
  404.         // the inequality test below IS intentional and should NOT be replaced by a check with a small tolerance
  405.         for (double x0 = Double.NaN; !Double.valueOf(x).equals(Double.valueOf(x0));) {
  406.             d += 2;
  407.             term *= mE2 / (d * (d + 1));
  408.             x0 = x;
  409.             x = x - term;
  410.         }
  411.         return x;
  412.     }

  413.     /**
  414.      * Get true anomaly from eccentric anomaly and eccentricity.
  415.      *
  416.      * @param ek the eccentric anomaly (rad)
  417.      * @param ecc the eccentricity
  418.      * @return the true anomaly (rad)
  419.      */
  420.     private double getTrueAnomaly(final double ek, final double ecc) {
  421.         final SinCos scek = FastMath.sinCos(ek);
  422.         final double svk  = FastMath.sqrt(1. - ecc * ecc) * scek.sin();
  423.         final double cvk  = scek.cos() - ecc;
  424.         return FastMath.atan2(svk, cvk);
  425.     }

  426.     /**
  427.      * This method transforms the PV coordinates obtained after the numerical
  428.      * integration in the ECEF PZ-90.
  429.      *
  430.      * @param state spacecraft state after integration
  431.      * @return the PV coordinates in the ECEF PZ-90.
  432.      */
  433.     private PVCoordinates getPVInPZ90(final SpacecraftState state) {

  434.         // Compute time difference between start date and end date
  435.         final double dt = state.getDate().durationFrom(initDate.getDate());

  436.         // Position and velocity vectors
  437.         final PVCoordinates pv = state.getPVCoordinates();
  438.         final Vector3D pos = pv.getPosition();
  439.         final Vector3D vel = pv.getVelocity();

  440.         // Components of position and velocity vectors
  441.         final double x0 = pos.getX();
  442.         final double y0 = pos.getY();
  443.         final double z0 = pos.getZ();
  444.         final double vx0 = vel.getX();
  445.         final double vy0 = vel.getY();
  446.         final double vz0 = vel.getZ();

  447.         // Greenwich Mean Sidereal Time (GMST)
  448.         final GLONASSDate gloDate = new GLONASSDate(
  449.                 state.getDate(),
  450.                 dataContext.getTimeScales().getGLONASS());
  451.         final double gmst = gloDate.getGMST();

  452.         final double ti = glonassOrbit.getTime() + dt;
  453.         // We use the GMST instead of the GMT as it is recommended into GLONASS ICD (2016)
  454.         final double s = gmst + GLONASS_AV * (ti - 10800.);

  455.         // Commons Parameters
  456.         final SinCos scS  = FastMath.sinCos(s);
  457.         final double cosS = scS.cos();
  458.         final double sinS = scS.sin();

  459.         // Transformed coordinates
  460.         final double x = x0 * cosS + y0 * sinS;
  461.         final double y = -x0 * sinS + y0 * cosS;
  462.         final double z = z0;
  463.         final double vx = vx0 * cosS + vy0 * sinS + GLONASS_AV * y;
  464.         final double vy = -vx0 * sinS + vy0 * cosS - GLONASS_AV * x;
  465.         final double vz = vz0;

  466.         // Transformed orbit
  467.         return new PVCoordinates(new Vector3D(x, y, z),
  468.                                  new Vector3D(vx, vy, vz));
  469.     }

  470.     /**
  471.      * This method computes the PV coordinates of the spacecraft center of mass.
  472.      * The returned PV are expressed in inertial coordinates system at the instant tb.
  473.      *
  474.      * @param date the computation date in GLONASS scale
  475.      * @return the PV Coordinates in inertial coordinates system
  476.      */
  477.     private PVCoordinates getPVInInertial(final GLONASSDate date) {

  478.         // Greenwich Mean Sidereal Time (GMST)
  479.         final double gmst = date.getGMST();

  480.         final double time = glonassOrbit.getTime();
  481.         final double dt   = time - 10800.;
  482.         // We use the GMST instead of the GMT as it is recommended into GLONASS ICD (2016)
  483.         final double s = gmst + GLONASS_AV * dt;

  484.         // Commons Parameters
  485.         final SinCos scS  = FastMath.sinCos(s);
  486.         final double cosS = scS.cos();
  487.         final double sinS = scS.sin();

  488.         // PV coordinates in inertial frame
  489.         final double x0  = glonassOrbit.getX() * cosS - glonassOrbit.getY() * sinS;
  490.         final double y0  = glonassOrbit.getX() * sinS + glonassOrbit.getY() * cosS;
  491.         final double z0  = glonassOrbit.getZ();
  492.         final double vx0 = glonassOrbit.getXDot() * cosS - glonassOrbit.getYDot() * sinS - GLONASS_AV * y0;
  493.         final double vy0 = glonassOrbit.getXDot() * sinS + glonassOrbit.getYDot() * cosS + GLONASS_AV * x0;
  494.         final double vz0 = glonassOrbit.getZDot();
  495.         return new PVCoordinates(new Vector3D(x0, y0, z0),
  496.                                  new Vector3D(vx0, vy0, vz0));
  497.     }

  498.     @Override
  499.     protected StateMapper createMapper(final AbsoluteDate referenceDate, final double mu,
  500.                                        final OrbitType orbitType, final PositionAngle positionAngleType,
  501.                                        final AttitudeProvider attitudeProvider, final Frame frame) {
  502.         return new Mapper(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  503.     }

  504.     /** Internal mapper. */
  505.     private static class Mapper extends StateMapper {

  506.         /**
  507.          * Simple constructor.
  508.          *
  509.          * @param referenceDate reference date
  510.          * @param mu central attraction coefficient (m³/s²)
  511.          * @param orbitType orbit type to use for mapping
  512.          * @param positionAngleType angle type to use for propagation
  513.          * @param attitudeProvider attitude provider
  514.          * @param frame inertial frame
  515.          */
  516.         Mapper(final AbsoluteDate referenceDate, final double mu,
  517.                final OrbitType orbitType, final PositionAngle positionAngleType,
  518.                final AttitudeProvider attitudeProvider, final Frame frame) {
  519.             super(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  520.         }

  521.         @Override
  522.         public SpacecraftState mapArrayToState(final AbsoluteDate date, final double[] y,
  523.                                                final double[] yDot, final PropagationType type) {
  524.             // The parameter meanOnly is ignored for the GLONASS Propagator
  525.             final double mass = y[6];
  526.             if (mass <= 0.0) {
  527.                 throw new OrekitException(OrekitMessages.SPACECRAFT_MASS_BECOMES_NEGATIVE, mass);
  528.             }

  529.             final Orbit orbit       = getOrbitType().mapArrayToOrbit(y, yDot, getPositionAngleType(), date, getMu(), getFrame());
  530.             final Attitude attitude = getAttitudeProvider().getAttitude(orbit, date, getFrame());

  531.             return new SpacecraftState(orbit, attitude, mass);
  532.         }

  533.         @Override
  534.         public void mapStateToArray(final SpacecraftState state, final double[] y,
  535.                                     final double[] yDot) {
  536.             getOrbitType().mapOrbitToArray(state.getOrbit(), getPositionAngleType(), y, yDot);
  537.             y[6] = state.getMass();
  538.         }

  539.     }

  540.     @Override
  541.     protected MainStateEquations getMainStateEquations(final ODEIntegrator integ) {
  542.         return new Main();
  543.     }

  544.     /** Internal class for orbital parameters integration. */
  545.     private class Main implements MainStateEquations {

  546.         /** Derivatives array. */
  547.         private final double[] yDot;

  548.         /**
  549.          * Simple constructor.
  550.          */
  551.         Main() {
  552.             yDot = new double[7];
  553.         }

  554.         @Override
  555.         public double[] computeDerivatives(final SpacecraftState state) {

  556.             // Date in Glonass form
  557.             final GLONASSDate gloDate = new GLONASSDate(
  558.                     state.getDate(),
  559.                     dataContext.getTimeScales().getGLONASS());

  560.             // Position and Velocity vectors
  561.             final Vector3D vel = state.getPVCoordinates().getVelocity();
  562.             final Vector3D pos = state.getPVCoordinates().getPosition();

  563.             Arrays.fill(yDot, 0.0);

  564.             // dPos/dt = Vel
  565.             yDot[0] += vel.getX();
  566.             yDot[1] += vel.getY();
  567.             yDot[2] += vel.getZ();

  568.             // Components of position and velocity vectors
  569.             final double x0 = pos.getX();
  570.             final double y0 = pos.getY();
  571.             final double z0 = pos.getZ();

  572.             // Normalized values
  573.             final double r  = pos.getNorm();
  574.             final double r2 = r * r;
  575.             final double oor = 1. / r;
  576.             final double oor2 = 1. / r2;
  577.             final double x = x0 * oor;
  578.             final double y = y0 * oor;
  579.             final double z = z0 * oor;
  580.             final double g = GNSSConstants.GLONASS_MU * oor2;
  581.             final double ro = GLONASS_EARTH_EQUATORIAL_RADIUS * oor;

  582.             yDot[3] += x * (-g + (-1.5 * GLONASS_J20 * g * ro * ro * (1. - 5. * z * z)));
  583.             yDot[4] += y * (-g + (-1.5 * GLONASS_J20 * g * ro * ro * (1. - 5. * z * z)));
  584.             yDot[5] += z * (-g + (-1.5 * GLONASS_J20 * g * ro * ro * (3. - 5. * z * z)));

  585.             // Luni-Solar contribution
  586.             final Vector3D acc;
  587.             if (isAccAvailable) {
  588.                 acc = getLuniSolarPerturbations(gloDate);
  589.             } else {
  590.                 final Vector3D accMoon = computeLuniSolarPerturbations(
  591.                         state, moonElements[0], moonElements[1], moonElements[2],
  592.                         moonElements[3],
  593.                         dataContext.getCelestialBodies().getMoon().getGM());
  594.                 final Vector3D accSun = computeLuniSolarPerturbations(
  595.                         state,
  596.                         sunElements[0], sunElements[1], sunElements[2],
  597.                         sunElements[3],
  598.                         dataContext.getCelestialBodies().getSun().getGM());
  599.                 acc = accMoon.add(accSun);
  600.             }

  601.             yDot[3] += acc.getX();
  602.             yDot[4] += acc.getY();
  603.             yDot[5] += acc.getZ();

  604.             return yDot.clone();
  605.         }

  606.         /**
  607.          * This method computes the accelerations induced by gravitational
  608.          * perturbations of the Sun and the Moon if they are not available into
  609.          * the navigation message data.
  610.          *
  611.          * @param state current state
  612.          * @param eps first direction cosine
  613.          * @param eta second direction cosine
  614.          * @param psi third direction cosine
  615.          * @param r distance of perturbing body
  616.          * @param g body gravitational field constant
  617.          * @return a vector containing the accelerations
  618.          */
  619.         private Vector3D computeLuniSolarPerturbations(final SpacecraftState state, final double eps,
  620.                                                        final double eta, final double psi,
  621.                                                        final double r, final double g) {

  622.             // Current pv coordinates
  623.             final PVCoordinates pv = state.getPVCoordinates();

  624.             final double oor = 1. / r;
  625.             final double oor2 = oor * oor;

  626.             // Normalized variable
  627.             final double x = pv.getPosition().getX() * oor;
  628.             final double y = pv.getPosition().getY() * oor;
  629.             final double z = pv.getPosition().getZ() * oor;
  630.             final double gm = g * oor2;

  631.             final double epsmX  = eps - x;
  632.             final double etamY  = eta - y;
  633.             final double psimZ  = psi - z;
  634.             final Vector3D vector = new Vector3D(epsmX, etamY, psimZ);
  635.             final double d2 = vector.getNormSq();
  636.             final double deltaM = FastMath.sqrt(d2) * d2;

  637.             // Accelerations
  638.             final double accX = gm * ((epsmX / deltaM) - eps);
  639.             final double accY = gm * ((etamY / deltaM) - eta);
  640.             final double accZ = gm * ((psimZ / deltaM) - psi);

  641.             return new Vector3D(accX, accY, accZ);
  642.         }

  643.         /**
  644.          * Get the accelerations induced by gravitational
  645.          * perturbations of the Sun and the Moon in a geocentric
  646.          * coordinate system.
  647.          * <p>
  648.          * The accelerations are obtained using projections of accelerations
  649.          * transmitted within navigation message data.
  650.          * </p>
  651.          *
  652.          * @param date the computation date in GLONASS scale
  653.          * @return a vector containing the sum of both accelerations
  654.          */
  655.         private Vector3D getLuniSolarPerturbations(final GLONASSDate date) {

  656.             // Greenwich Mean Sidereal Time (GMST)
  657.             final double gmst = date.getGMST();

  658.             final double time = glonassOrbit.getTime();
  659.             final double dt   = time - 10800.;
  660.             // We use the GMST instead of the GMT as it is recommended into GLONASS ICD (see Ref)
  661.             final double s = gmst + GLONASS_AV * dt;

  662.             // Commons Parameters
  663.             final SinCos scS  = FastMath.sinCos(s);
  664.             final double cosS = scS.cos();
  665.             final double sinS = scS.sin();

  666.             // Accelerations
  667.             final double accX = glonassOrbit.getXDotDot() * cosS - glonassOrbit.getYDotDot() * sinS;
  668.             final double accY = glonassOrbit.getXDotDot() * sinS + glonassOrbit.getYDotDot() * cosS;
  669.             final double accZ = glonassOrbit.getZDotDot();

  670.             return new Vector3D(accX, accY, accZ);
  671.         }

  672.     }

  673. }