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.GLONASSOrbitalElements;
  38. import org.orekit.propagation.analytical.gnss.data.GNSSConstants;
  39. import org.orekit.propagation.integration.AbstractIntegratedPropagator;
  40. import org.orekit.propagation.integration.StateMapper;
  41. import org.orekit.time.AbsoluteDate;
  42. import org.orekit.time.GLONASSDate;
  43. import org.orekit.utils.AbsolutePVCoordinates;
  44. import org.orekit.utils.Constants;
  45. import org.orekit.utils.IERSConventions;
  46. import org.orekit.utils.PVCoordinates;
  47. import org.orekit.utils.TimeStampedPVCoordinates;

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

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

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

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

  81.     // Data used to solve Kepler's equation
  82.     /** First coefficient to compute Kepler equation solver starter. */
  83.     private static final double A;

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

  86.     static {
  87.         final double k1 = 3 * FastMath.PI + 2;
  88.         final double k2 = FastMath.PI - 1;
  89.         final double k3 = 6 * FastMath.PI - 1;
  90.         A  = 3 * k2 * k2 / k1;
  91.         B  = k3 * k3 / (6 * k1);
  92.     }

  93.     /** The GLONASS orbital elements used. */
  94.     private final GLONASSOrbitalElements glonassOrbit;

  95.     /** Initial date in GLONASS form. */
  96.     private final GLONASSDate initDate;

  97.     /** The spacecraft mass (kg). */
  98.     private final double mass;

  99.     /** The ECI frame used for GLONASS propagation. */
  100.     private final Frame eci;

  101.     /** Direction cosines and distance of perturbing body: Moon.
  102.      * <p>
  103.      * <ul>
  104.      * <li>double[0] = ξ<sub>m</sub></li>
  105.      * <li>double[1] = η<sub>m</sub></li>
  106.      * <li>double[2] = ψ<sub>m</sub></li>
  107.      * <li>double[3] = r<sub>m</sub></li>
  108.      * </ul>
  109.      * </p>
  110.      */
  111.     private double[] moonElements;

  112.     /** Direction cosines and distance of perturbing body: Sun.
  113.      * <p>
  114.      * <ul>
  115.      * <li>double[0] = ξ<sub>s</sub></li>
  116.      * <li>double[1] = η<sub>s</sub></li>
  117.      * <li>double[2] = ψ<sub>s</sub></li>
  118.      * <li>double[3] = r<sub>s</sub></li>
  119.      * </ul>
  120.      * </p>
  121.      */
  122.     private double[] sunElements;

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

  125.     /** Data context used for propagation. */
  126.     private final DataContext dataContext;

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

  154.         // Initialize state mapper
  155.         initMapper();
  156.         setInitialState();
  157.         setAttitudeProvider(provider);
  158.         setOrbitType(OrbitType.CARTESIAN);
  159.         // It is not meaningful for propagation in Cartesian parameters
  160.         setPositionAngleType(PositionAngle.TRUE);
  161.         setMu(GNSSConstants.GLONASS_MU);

  162.         // As recommended by GLONASS ICD (2016), the direction cosines and distance
  163.         // of perturbing body are calculated one time (at tb).
  164.         if (!isAccAvailable) {
  165.             computeMoonElements(initDate);
  166.             computeSunElements(initDate);
  167.         }
  168.     }

  169.     /**
  170.      * Gets the underlying GLONASS orbital elements.
  171.      *
  172.      * @return the underlying GLONASS orbital elements
  173.      */
  174.     public GLONASSOrbitalElements getGLONASSOrbitalElements() {
  175.         return glonassOrbit;
  176.     }

  177.     /** {@inheritDoc} */
  178.     @Override
  179.     public SpacecraftState propagate(final AbsoluteDate date) {
  180.         // Spacecraft state in inertial frame
  181.         final SpacecraftState stateInInertial = super.propagate(date);

  182.         // Build the spacecraft state in inertial frame
  183.         final PVCoordinates pvInPZ90 = getPVInPZ90(stateInInertial);
  184.         final AbsolutePVCoordinates absPV = new AbsolutePVCoordinates(
  185.                 dataContext.getFrames().getPZ9011(IERSConventions.IERS_2010, true),
  186.                 stateInInertial.getDate(), pvInPZ90);
  187.         final TimeStampedPVCoordinates pvInInertial = absPV.getPVCoordinates(eci);
  188.         final SpacecraftState transformedState = new SpacecraftState(new CartesianOrbit(pvInInertial, eci, pvInInertial.getDate(), GNSSConstants.GLONASS_MU),
  189.                                                                      stateInInertial.getAttitude(),
  190.                                                                      stateInInertial.getMass(),
  191.                                                                      stateInInertial.getAdditionalStatesValues(),
  192.                                                                      stateInInertial.getAdditionalStatesDerivatives());

  193.         return transformedState;
  194.     }

  195.     /**
  196.      * Set the initial state.
  197.      * <p>
  198.      * The initial conditions on position and velocity are in the ECEF coordinate system PZ-90.
  199.      * Previous to orbit integration, they must be transformed to an absolute inertial coordinate system.
  200.      * </p>
  201.      */
  202.     private void setInitialState() {

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

  205.         // Create a new orbit
  206.         final Orbit orbit = new CartesianOrbit(pvInInertial,
  207.                                                eci, initDate.getDate(),
  208.                                                GNSSConstants.GLONASS_MU);

  209.         // Reset the initial state to apply the transformation
  210.         resetInitialState(new SpacecraftState(orbit, mass));
  211.     }

  212.     /**
  213.      * This method computes the direction cosines and the distance used to
  214.      * compute the gravitational perturbations of the Moon.
  215.      *
  216.      * @param date the computation date in GLONASS scale
  217.      */
  218.     private void computeMoonElements(final GLONASSDate date) {

  219.         moonElements = new double[4];

  220.         // Constants
  221.         // Semi-major axis of the Moon's orbit (m)
  222.         final double am = 3.84385243e8;
  223.         // The Moon's orbit eccentricity
  224.         final double em = 0.054900489;
  225.         // Mean inclination of the Moon's orbit to the ecliptic (rad)
  226.         final double im = 0.0898041080;

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

  240.         // Commons parameters
  241.         final SinCos scOm  = FastMath.sinCos(omegaM);
  242.         final SinCos scIm  = FastMath.sinCos(im);
  243.         final SinCos scEs  = FastMath.sinCos(eps);
  244.         final SinCos scGm  = FastMath.sinCos(gammaM);
  245.         final double cosOm = scOm.cos();
  246.         final double sinOm = scOm.sin();
  247.         final double cosIm = scIm.cos();
  248.         final double sinIm = scIm.sin();
  249.         final double cosEs = scEs.cos();
  250.         final double sinEs = scEs.sin();
  251.         final double cosGm = scGm.cos();
  252.         final double sinGm = scGm.sin();

  253.         // Intermediate parameters
  254.         final double psiStar = cosOm * sinIm;
  255.         final double etaStar = sinOm * sinIm;
  256.         final double epsStar = 1. - cosOm * cosOm * (1. - cosIm);
  257.         final double eps11 = sinOm * cosOm * (1. - cosIm);
  258.         final double eps12 = 1. - sinOm * sinOm * (1. - cosIm);
  259.         final double eta11 = epsStar * cosEs - psiStar * sinEs;
  260.         final double eta12 = eps11 * cosEs + etaStar * sinEs;
  261.         final double psi11 = epsStar * sinEs + psiStar * cosEs;
  262.         final double psi12 = eps11 * sinEs - etaStar * cosEs;

  263.         // Eccentric Anomaly
  264.         final double ek = getEccentricAnomaly(qm, em);

  265.         // True Anomaly
  266.         final double vk    = getTrueAnomaly(ek, em);
  267.         final SinCos scVk  = FastMath.sinCos(vk);
  268.         final double sinVk = scVk.sin();
  269.         final double cosVk = scVk.cos();

  270.         // Direction cosine
  271.         final double epsM = eps11 * (sinVk * cosGm + cosVk * sinGm) + eps12 * (cosVk * cosGm - sinVk * sinGm);
  272.         final double etaM = eta11 * (sinVk * cosGm + cosVk * sinGm) + eta12 * (cosVk * cosGm - sinVk * sinGm);
  273.         final double psiM = psi11 * (sinVk * cosGm + cosVk * sinGm) + psi12 * (cosVk * cosGm - sinVk * sinGm);

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

  276.         moonElements[0] = epsM;
  277.         moonElements[1] = etaM;
  278.         moonElements[2] = psiM;
  279.         moonElements[3] = rm;

  280.     }

  281.     /**
  282.      * This method computes the direction cosines and the distance used to
  283.      * compute the gravitational perturbations of the Sun.
  284.      *
  285.      * @param date the computation date in GLONASS scale
  286.      */
  287.     private void computeSunElements(final GLONASSDate date) {

  288.         sunElements = new double[4];

  289.         // Constants
  290.         //  Major semi-axis of the Earth’s orbit around the Sun (m)
  291.         final double as = 1.49598e11;
  292.         // The eccentricity of the Earth’s orbit around the Sun
  293.         final double es = 0.016719;

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

  305.         // Eccentric Anomaly
  306.         final double ek = getEccentricAnomaly(qs, es);

  307.         // True Anomaly
  308.         final double vk    =  getTrueAnomaly(ek, es);
  309.         final SinCos scVk  = FastMath.sinCos(vk);
  310.         final double sinVk = scVk.sin();
  311.         final double cosVk = scVk.cos();

  312.         // Commons parameters
  313.         final SinCos scWs  = FastMath.sinCos(ws);
  314.         final SinCos scEs  = FastMath.sinCos(eps);
  315.         final double cosWs = scWs.cos();
  316.         final double sinWs = scWs.sin();
  317.         final double cosEs = scEs.cos();
  318.         final double sinEs = scEs.sin();

  319.         // Direction cosine
  320.         final double epsS = cosVk * cosWs - sinVk * sinWs;
  321.         final double etaS = cosEs * (sinVk * cosWs + cosVk * sinWs);
  322.         final double psiS = sinEs * (sinVk * cosWs + cosVk * sinWs);

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

  325.         sunElements[0] = epsS;
  326.         sunElements[1] = etaS;
  327.         sunElements[2] = psiS;
  328.         sunElements[3] = rs;

  329.     }

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

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

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

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

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

  377.             // update eccentric anomaly, using expressions that limit underflow problems
  378.             final double w = fd + 0.5 * dee * (fdd + dee * fddd / 3);
  379.             fd += dee * (fdd + 0.5 * dee * fddd);
  380.             E  -= (f - dee * (fd - w)) / fd;

  381.         }

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

  384.         return E;

  385.     }

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

  407.     /**
  408.      * Get true anomaly from eccentric anomaly and eccentricity.
  409.      *
  410.      * @param ek the eccentric anomaly (rad)
  411.      * @param ecc the eccentricity
  412.      * @return the true anomaly (rad)
  413.      */
  414.     private double getTrueAnomaly(final double ek, final double ecc) {
  415.         final SinCos scek = FastMath.sinCos(ek);
  416.         final double svk  = FastMath.sqrt(1. - ecc * ecc) * scek.sin();
  417.         final double cvk  = scek.cos() - ecc;
  418.         return FastMath.atan2(svk, cvk);
  419.     }

  420.     /**
  421.      * This method transforms the PV coordinates obtained after the numerical
  422.      * integration in the ECEF PZ-90.
  423.      *
  424.      * @param state spacecraft state after integration
  425.      * @return the PV coordinates in the ECEF PZ-90.
  426.      */
  427.     private PVCoordinates getPVInPZ90(final SpacecraftState state) {

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

  430.         // Position and velocity vectors
  431.         final PVCoordinates pv = state.getPVCoordinates();
  432.         final Vector3D pos = pv.getPosition();
  433.         final Vector3D vel = pv.getVelocity();

  434.         // Components of position and velocity vectors
  435.         final double x0 = pos.getX();
  436.         final double y0 = pos.getY();
  437.         final double z0 = pos.getZ();
  438.         final double vx0 = vel.getX();
  439.         final double vy0 = vel.getY();
  440.         final double vz0 = vel.getZ();

  441.         // Greenwich Mean Sidereal Time (GMST)
  442.         final GLONASSDate gloDate = new GLONASSDate(
  443.                 state.getDate(),
  444.                 dataContext.getTimeScales().getGLONASS());
  445.         final double gmst = gloDate.getGMST();

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

  449.         // Commons Parameters
  450.         final SinCos scS  = FastMath.sinCos(s);
  451.         final double cosS = scS.cos();
  452.         final double sinS = scS.sin();

  453.         // Transformed coordinates
  454.         final double x = x0 * cosS + y0 * sinS;
  455.         final double y = -x0 * sinS + y0 * cosS;
  456.         final double z = z0;
  457.         final double vx = vx0 * cosS + vy0 * sinS + GLONASS_AV * y;
  458.         final double vy = -vx0 * sinS + vy0 * cosS - GLONASS_AV * x;
  459.         final double vz = vz0;

  460.         // Transformed orbit
  461.         return new PVCoordinates(new Vector3D(x, y, z),
  462.                                  new Vector3D(vx, vy, vz));
  463.     }

  464.     /**
  465.      * This method computes the PV coordinates of the spacecraft center of mass.
  466.      * The returned PV are expressed in inertial coordinates system at the instant tb.
  467.      *
  468.      * @param date the computation date in GLONASS scale
  469.      * @return the PV Coordinates in inertial coordinates system
  470.      */
  471.     private PVCoordinates getPVInInertial(final GLONASSDate date) {

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

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

  478.         // Commons Parameters
  479.         final SinCos scS  = FastMath.sinCos(s);
  480.         final double cosS = scS.cos();
  481.         final double sinS = scS.sin();

  482.         // PV coordinates in inertial frame
  483.         final double x0  = glonassOrbit.getX() * cosS - glonassOrbit.getY() * sinS;
  484.         final double y0  = glonassOrbit.getX() * sinS + glonassOrbit.getY() * cosS;
  485.         final double z0  = glonassOrbit.getZ();
  486.         final double vx0 = glonassOrbit.getXDot() * cosS - glonassOrbit.getYDot() * sinS - GLONASS_AV * y0;
  487.         final double vy0 = glonassOrbit.getXDot() * sinS + glonassOrbit.getYDot() * cosS + GLONASS_AV * x0;
  488.         final double vz0 = glonassOrbit.getZDot();
  489.         return new PVCoordinates(new Vector3D(x0, y0, z0),
  490.                                  new Vector3D(vx0, vy0, vz0));
  491.     }

  492.     @Override
  493.     protected StateMapper createMapper(final AbsoluteDate referenceDate, final double mu,
  494.                                        final OrbitType orbitType, final PositionAngle positionAngleType,
  495.                                        final AttitudeProvider attitudeProvider, final Frame frame) {
  496.         return new Mapper(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  497.     }

  498.     /** Internal mapper. */
  499.     private static class Mapper extends StateMapper {

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

  515.         @Override
  516.         public SpacecraftState mapArrayToState(final AbsoluteDate date, final double[] y,
  517.                                                final double[] yDot, final PropagationType type) {
  518.             // The parameter meanOnly is ignored for the GLONASS Propagator
  519.             final double mass = y[6];
  520.             if (mass <= 0.0) {
  521.                 throw new OrekitException(OrekitMessages.SPACECRAFT_MASS_BECOMES_NEGATIVE, mass);
  522.             }

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

  525.             return new SpacecraftState(orbit, attitude, mass);
  526.         }

  527.         @Override
  528.         public void mapStateToArray(final SpacecraftState state, final double[] y,
  529.                                     final double[] yDot) {
  530.             getOrbitType().mapOrbitToArray(state.getOrbit(), getPositionAngleType(), y, yDot);
  531.             y[6] = state.getMass();
  532.         }

  533.     }

  534.     @Override
  535.     protected MainStateEquations getMainStateEquations(final ODEIntegrator integ) {
  536.         return new Main();
  537.     }

  538.     /** Internal class for orbital parameters integration. */
  539.     private class Main implements MainStateEquations {

  540.         /** Derivatives array. */
  541.         private final double[] yDot;

  542.         /**
  543.          * Simple constructor.
  544.          */
  545.         Main() {
  546.             yDot = new double[7];
  547.         }

  548.         @Override
  549.         public double[] computeDerivatives(final SpacecraftState state) {

  550.             // Date in Glonass form
  551.             final GLONASSDate gloDate = new GLONASSDate(
  552.                     state.getDate(),
  553.                     dataContext.getTimeScales().getGLONASS());

  554.             // Position and Velocity vectors
  555.             final Vector3D vel = state.getPVCoordinates().getVelocity();
  556.             final Vector3D pos = state.getPVCoordinates().getPosition();

  557.             Arrays.fill(yDot, 0.0);

  558.             // dPos/dt = Vel
  559.             yDot[0] += vel.getX();
  560.             yDot[1] += vel.getY();
  561.             yDot[2] += vel.getZ();

  562.             // Components of position and velocity vectors
  563.             final double x0 = pos.getX();
  564.             final double y0 = pos.getY();
  565.             final double z0 = pos.getZ();

  566.             // Normalized values
  567.             final double r  = pos.getNorm();
  568.             final double r2 = r * r;
  569.             final double oor = 1. / r;
  570.             final double oor2 = 1. / r2;
  571.             final double x = x0 * oor;
  572.             final double y = y0 * oor;
  573.             final double z = z0 * oor;
  574.             final double g = GNSSConstants.GLONASS_MU * oor2;
  575.             final double ro = GLONASS_EARTH_EQUATORIAL_RADIUS * oor;

  576.             yDot[3] += x * (-g + (-1.5 * GLONASS_J20 * g * ro * ro * (1. - 5. * z * z)));
  577.             yDot[4] += y * (-g + (-1.5 * GLONASS_J20 * g * ro * ro * (1. - 5. * z * z)));
  578.             yDot[5] += z * (-g + (-1.5 * GLONASS_J20 * g * ro * ro * (3. - 5. * z * z)));

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

  595.             yDot[3] += acc.getX();
  596.             yDot[4] += acc.getY();
  597.             yDot[5] += acc.getZ();

  598.             return yDot.clone();
  599.         }

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

  616.             // Current pv coordinates
  617.             final PVCoordinates pv = state.getPVCoordinates();

  618.             final double oor = 1. / r;
  619.             final double oor2 = oor * oor;

  620.             // Normalized variable
  621.             final double x = pv.getPosition().getX() * oor;
  622.             final double y = pv.getPosition().getY() * oor;
  623.             final double z = pv.getPosition().getZ() * oor;
  624.             final double gm = g * oor2;

  625.             final double epsmX  = eps - x;
  626.             final double etamY  = eta - y;
  627.             final double psimZ  = psi - z;
  628.             final Vector3D vector = new Vector3D(epsmX, etamY, psimZ);
  629.             final double d2 = vector.getNormSq();
  630.             final double deltaM = FastMath.sqrt(d2) * d2;

  631.             // Accelerations
  632.             final double accX = gm * ((epsmX / deltaM) - eps);
  633.             final double accY = gm * ((etamY / deltaM) - eta);
  634.             final double accZ = gm * ((psimZ / deltaM) - psi);

  635.             return new Vector3D(accX, accY, accZ);
  636.         }

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

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

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

  656.             // Commons Parameters
  657.             final SinCos scS  = FastMath.sinCos(s);
  658.             final double cosS = scS.cos();
  659.             final double sinS = scS.sin();

  660.             // Accelerations
  661.             final double accX = glonassOrbit.getXDotDot() * cosS - glonassOrbit.getYDotDot() * sinS;
  662.             final double accY = glonassOrbit.getXDotDot() * sinS + glonassOrbit.getYDotDot() * cosS;
  663.             final double accZ = glonassOrbit.getZDotDot();

  664.             return new Vector3D(accX, accY, accZ);
  665.         }

  666.     }

  667. }