AbstractGNSSPropagator.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.MathUtils;
  23. import org.hipparchus.util.Precision;
  24. import org.orekit.attitudes.AttitudeProvider;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitMessages;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.orbits.CartesianOrbit;
  29. import org.orekit.orbits.Orbit;
  30. import org.orekit.propagation.SpacecraftState;
  31. import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.utils.PVCoordinates;

  34. /** Common handling of {@link AbstractAnalyticalPropagator} methods for GNSS propagators.
  35.  * <p>
  36.  * This abstract class allows to provide easily a subset of {@link AbstractAnalyticalPropagator} methods
  37.  * for specific GNSS propagators.
  38.  * </p>
  39.  * @author Pascal Parraud
  40.  */
  41. public abstract class AbstractGNSSPropagator extends AbstractAnalyticalPropagator {

  42.     // Data used to solve Kepler's equation
  43.     /** First coefficient to compute Kepler equation solver starter. */
  44.     private static final double A;

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

  47.     static {
  48.         final double k1 = 3 * FastMath.PI + 2;
  49.         final double k2 = FastMath.PI - 1;
  50.         final double k3 = 6 * FastMath.PI - 1;
  51.         A  = 3 * k2 * k2 / k1;
  52.         B  = k3 * k3 / (6 * k1);
  53.     }

  54.     /** The GNSS orbital elements used. */
  55.     private final GNSSOrbitalElements gnssOrbit;

  56.     /** Mean angular velocity of the Earth. */
  57.     private final double av;

  58.     /** Duration of the GNSS cycle in seconds. */
  59.     private final double cycleDuration;

  60.     /** The spacecraft mass (kg). */
  61.     private final double mass;

  62.     /** The Earth gravity coefficient used for GNSS propagation. */
  63.     private final double mu;

  64.     /** The ECI frame used for GNSS propagation. */
  65.     private final Frame eci;

  66.     /** The ECEF frame used for GNSS propagation. */
  67.     private final Frame ecef;

  68.     /** Build a new instance.
  69.      * @param gnssOrbit the common GNSS orbital elements to be used by the Abstract GNSS propagator
  70.      * @param attitudeProvider provider for attitude computation
  71.      * @param eci the ECI frame used for GNSS propagation
  72.      * @param ecef the ECEF frame used for GNSS propagation
  73.      * @param mass the spacecraft mass (kg)
  74.      * @param av mean angular velocity of the Earth (rad/s)
  75.      * @param cycleDuration duration of the GNSS cycle in seconds
  76.      * @param mu the Earth gravity coefficient used for GNSS propagation
  77.      */
  78.     protected AbstractGNSSPropagator(final GNSSOrbitalElements gnssOrbit, final AttitudeProvider attitudeProvider,
  79.                                      final Frame eci, final Frame ecef, final double mass,
  80.                                      final double av, final double cycleDuration, final double mu) {
  81.         super(attitudeProvider);
  82.         this.gnssOrbit     = gnssOrbit;
  83.         this.av            = av;
  84.         this.cycleDuration = cycleDuration;
  85.         this.mass          = mass;
  86.         this.mu            = mu;
  87.         // Sets the Earth Centered Inertial frame
  88.         this.eci  = eci;
  89.         // Sets the Earth Centered Earth Fixed frame
  90.         this.ecef = ecef;
  91.         // Sets the start date as the date of the orbital elements
  92.         setStartDate(gnssOrbit.getDate());
  93.     }

  94.     /** Get the duration from GNSS Reference epoch.
  95.      * <p>This takes the GNSS week roll-over into account.</p>
  96.      * @param date the considered date
  97.      * @return the duration from GNSS orbit Reference epoch (s)
  98.      */
  99.     private double getTk(final AbsoluteDate date) {
  100.         // Time from ephemeris reference epoch
  101.         double tk = date.durationFrom(gnssOrbit.getDate());
  102.         // Adjusts the time to take roll over week into account
  103.         while (tk > 0.5 * cycleDuration) {
  104.             tk -= cycleDuration;
  105.         }
  106.         while (tk < -0.5 * cycleDuration) {
  107.             tk += cycleDuration;
  108.         }
  109.         // Returns the time from ephemeris reference epoch
  110.         return tk;
  111.     }

  112.     /**
  113.      * Gets the PVCoordinates of the GNSS SV in {@link #getECEF() ECEF frame}.
  114.      *
  115.      * <p>The algorithm uses automatic differentiation to compute velocity and
  116.      * acceleration.</p>
  117.      *
  118.      * @param date the computation date
  119.      * @return the GNSS SV PVCoordinates in {@link #getECEF() ECEF frame}
  120.      */
  121.     public PVCoordinates propagateInEcef(final AbsoluteDate date) {
  122.         // Duration from GNSS ephemeris Reference date
  123.         final UnivariateDerivative2 tk = new UnivariateDerivative2(getTk(date), 1.0, 0.0);
  124.         // Mean anomaly
  125.         final UnivariateDerivative2 mk = tk.multiply(gnssOrbit.getMeanMotion()).add(gnssOrbit.getM0());
  126.         // Eccentric Anomaly
  127.         final UnivariateDerivative2 ek = getEccentricAnomaly(mk);
  128.         // True Anomaly
  129.         final UnivariateDerivative2 vk =  getTrueAnomaly(ek);
  130.         // Argument of Latitude
  131.         final UnivariateDerivative2 phik    = vk.add(gnssOrbit.getPa());
  132.         final UnivariateDerivative2 twoPhik = phik.multiply(2);
  133.         final UnivariateDerivative2 c2phi   = twoPhik.cos();
  134.         final UnivariateDerivative2 s2phi   = twoPhik.sin();
  135.         // Argument of Latitude Correction
  136.         final UnivariateDerivative2 dphik = c2phi.multiply(gnssOrbit.getCuc()).add(s2phi.multiply(gnssOrbit.getCus()));
  137.         // Radius Correction
  138.         final UnivariateDerivative2 drk = c2phi.multiply(gnssOrbit.getCrc()).add(s2phi.multiply(gnssOrbit.getCrs()));
  139.         // Inclination Correction
  140.         final UnivariateDerivative2 dik = c2phi.multiply(gnssOrbit.getCic()).add(s2phi.multiply(gnssOrbit.getCis()));
  141.         // Corrected Argument of Latitude
  142.         final UnivariateDerivative2 uk = phik.add(dphik);
  143.         // Corrected Radius
  144.         final UnivariateDerivative2 rk = ek.cos().multiply(-gnssOrbit.getE()).add(1).multiply(gnssOrbit.getSma()).add(drk);
  145.         // Corrected Inclination
  146.         final UnivariateDerivative2 ik  = tk.multiply(gnssOrbit.getIDot()).add(gnssOrbit.getI0()).add(dik);
  147.         final UnivariateDerivative2 cik = ik.cos();
  148.         // Positions in orbital plane
  149.         final UnivariateDerivative2 xk = uk.cos().multiply(rk);
  150.         final UnivariateDerivative2 yk = uk.sin().multiply(rk);
  151.         // Corrected longitude of ascending node
  152.         final UnivariateDerivative2 omk = tk.multiply(gnssOrbit.getOmegaDot() - av).
  153.                                         add(gnssOrbit.getOmega0() - av * gnssOrbit.getTime());
  154.         final UnivariateDerivative2 comk = omk.cos();
  155.         final UnivariateDerivative2 somk = omk.sin();
  156.         // returns the Earth-fixed coordinates
  157.         final FieldVector3D<UnivariateDerivative2> positionwithDerivatives =
  158.                         new FieldVector3D<>(xk.multiply(comk).subtract(yk.multiply(somk).multiply(cik)),
  159.                                             xk.multiply(somk).add(yk.multiply(comk).multiply(cik)),
  160.                                             yk.multiply(ik.sin()));
  161.         return new PVCoordinates(new Vector3D(positionwithDerivatives.getX().getValue(),
  162.                                               positionwithDerivatives.getY().getValue(),
  163.                                               positionwithDerivatives.getZ().getValue()),
  164.                                  new Vector3D(positionwithDerivatives.getX().getFirstDerivative(),
  165.                                               positionwithDerivatives.getY().getFirstDerivative(),
  166.                                               positionwithDerivatives.getZ().getFirstDerivative()),
  167.                                  new Vector3D(positionwithDerivatives.getX().getSecondDerivative(),
  168.                                               positionwithDerivatives.getY().getSecondDerivative(),
  169.                                               positionwithDerivatives.getZ().getSecondDerivative()));
  170.     }

  171.     /**
  172.      * Gets eccentric anomaly from mean anomaly.
  173.      * <p>The algorithm used to solve the Kepler equation has been published in:
  174.      * "Procedures for  solving Kepler's Equation", A. W. Odell and R. H. Gooding,
  175.      * Celestial Mechanics 38 (1986) 307-334</p>
  176.      * <p>It has been copied from the OREKIT library (KeplerianOrbit class).</p>
  177.      *
  178.      * @param mk the mean anomaly (rad)
  179.      * @return the eccentric anomaly (rad)
  180.      */
  181.     private UnivariateDerivative2 getEccentricAnomaly(final UnivariateDerivative2 mk) {

  182.         // reduce M to [-PI PI] interval
  183.         final UnivariateDerivative2 reducedM = new UnivariateDerivative2(MathUtils.normalizeAngle(mk.getValue(), 0.0),
  184.                                                                          mk.getFirstDerivative(),
  185.                                                                          mk.getSecondDerivative());

  186.         // compute start value according to A. W. Odell and R. H. Gooding S12 starter
  187.         UnivariateDerivative2 ek;
  188.         if (FastMath.abs(reducedM.getValue()) < 1.0 / 6.0) {
  189.             if (FastMath.abs(reducedM.getValue()) < Precision.SAFE_MIN) {
  190.                 // this is an Orekit change to the S12 starter.
  191.                 // If reducedM is 0.0, the derivative of cbrt is infinite which induces NaN appearing later in
  192.                 // the computation. As in this case E and M are almost equal, we initialize ek with reducedM
  193.                 ek = reducedM;
  194.             } else {
  195.                 // this is the standard S12 starter
  196.                 ek = reducedM.add(reducedM.multiply(6).cbrt().subtract(reducedM).multiply(gnssOrbit.getE()));
  197.             }
  198.         } else {
  199.             if (reducedM.getValue() < 0) {
  200.                 final UnivariateDerivative2 w = reducedM.add(FastMath.PI);
  201.                 ek = reducedM.add(w.multiply(-A).divide(w.subtract(B)).subtract(FastMath.PI).subtract(reducedM).multiply(gnssOrbit.getE()));
  202.             } else {
  203.                 final UnivariateDerivative2 minusW = reducedM.subtract(FastMath.PI);
  204.                 ek = reducedM.add(minusW.multiply(A).divide(minusW.add(B)).add(FastMath.PI).subtract(reducedM).multiply(gnssOrbit.getE()));
  205.             }
  206.         }

  207.         final double e1 = 1 - gnssOrbit.getE();
  208.         final boolean noCancellationRisk = (e1 + ek.getValue() * ek.getValue() / 6) >= 0.1;

  209.         // perform two iterations, each consisting of one Halley step and one Newton-Raphson step
  210.         for (int j = 0; j < 2; ++j) {
  211.             final UnivariateDerivative2 f;
  212.             UnivariateDerivative2 fd;
  213.             final UnivariateDerivative2 fdd  = ek.sin().multiply(gnssOrbit.getE());
  214.             final UnivariateDerivative2 fddd = ek.cos().multiply(gnssOrbit.getE());
  215.             if (noCancellationRisk) {
  216.                 f  = ek.subtract(fdd).subtract(reducedM);
  217.                 fd = fddd.subtract(1).negate();
  218.             } else {
  219.                 f  = eMeSinE(ek).subtract(reducedM);
  220.                 final UnivariateDerivative2 s = ek.multiply(0.5).sin();
  221.                 fd = s.multiply(s).multiply(2 * gnssOrbit.getE()).add(e1);
  222.             }
  223.             final UnivariateDerivative2 dee = f.multiply(fd).divide(f.multiply(0.5).multiply(fdd).subtract(fd.multiply(fd)));

  224.             // update eccentric anomaly, using expressions that limit underflow problems
  225.             final UnivariateDerivative2 w = fd.add(dee.multiply(0.5).multiply(fdd.add(dee.multiply(fdd).divide(3))));
  226.             fd = fd.add(dee.multiply(fdd.add(dee.multiply(0.5).multiply(fdd))));
  227.             ek = ek.subtract(f.subtract(dee.multiply(fd.subtract(w))).divide(fd));
  228.         }

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

  231.         // Returns the eccentric anomaly
  232.         return ek;
  233.     }

  234.     /**
  235.      * Accurate computation of E - e sin(E).
  236.      *
  237.      * @param E eccentric anomaly
  238.      * @return E - e sin(E)
  239.      */
  240.     private UnivariateDerivative2 eMeSinE(final UnivariateDerivative2 E) {
  241.         UnivariateDerivative2 x = E.sin().multiply(1 - gnssOrbit.getE());
  242.         final UnivariateDerivative2 mE2 = E.negate().multiply(E);
  243.         UnivariateDerivative2 term = E;
  244.         UnivariateDerivative2 d    = E.getField().getZero();
  245.         // the inequality test below IS intentional and should NOT be replaced by a check with a small tolerance
  246.         for (UnivariateDerivative2 x0 = d.add(Double.NaN); !Double.valueOf(x.getValue()).equals(Double.valueOf(x0.getValue()));) {
  247.             d = d.add(2);
  248.             term = term.multiply(mE2.divide(d.multiply(d.add(1))));
  249.             x0 = x;
  250.             x = x.subtract(term);
  251.         }
  252.         return x;
  253.     }

  254.     /** Gets true anomaly from eccentric anomaly.
  255.      *
  256.      * @param ek the eccentric anomaly (rad)
  257.      * @return the true anomaly (rad)
  258.      */
  259.     private UnivariateDerivative2 getTrueAnomaly(final UnivariateDerivative2 ek) {
  260.         final UnivariateDerivative2 svk = ek.sin().multiply(FastMath.sqrt(1. - gnssOrbit.getE() * gnssOrbit.getE()));
  261.         final UnivariateDerivative2 cvk = ek.cos().subtract(gnssOrbit.getE());
  262.         return svk.atan2(cvk);
  263.     }

  264.     /** {@inheritDoc} */
  265.     protected Orbit propagateOrbit(final AbsoluteDate date) {
  266.         // Gets the PVCoordinates in ECEF frame
  267.         final PVCoordinates pvaInECEF = propagateInEcef(date);
  268.         // Transforms the PVCoordinates to ECI frame
  269.         final PVCoordinates pvaInECI = ecef.getTransformTo(eci, date).transformPVCoordinates(pvaInECEF);
  270.         // Returns the Cartesian orbit
  271.         return new CartesianOrbit(pvaInECI, eci, date, mu);
  272.     }

  273.     /**
  274.      * Get the Earth gravity coefficient used for GNSS propagation.
  275.      * @return the Earth gravity coefficient.
  276.      */
  277.     public double getMU() {
  278.         return mu;
  279.     }

  280.     /** {@inheritDoc} */
  281.     public Frame getFrame() {
  282.         return eci;
  283.     }

  284.     /** {@inheritDoc} */
  285.     protected double getMass(final AbsoluteDate date) {
  286.         return mass;
  287.     }

  288.     /** {@inheritDoc} */
  289.     public void resetInitialState(final SpacecraftState state) {
  290.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  291.     }

  292.     /** {@inheritDoc} */
  293.     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
  294.         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
  295.     }

  296.     /**
  297.      * Gets the Earth Centered Inertial frame used to propagate the orbit.
  298.      *
  299.      * @return the ECI frame
  300.      */
  301.     public Frame getECI() {
  302.         return eci;
  303.     }

  304.     /**
  305.      * Gets the Earth Centered Earth Fixed frame used to propagate GNSS orbits according to the
  306.      * Interface Control Document.
  307.      *
  308.      * @return the ECEF frame
  309.      */
  310.     public Frame getECEF() {
  311.         return ecef;
  312.     }

  313. }