1   /* Copyright 2002-2021 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  
19  import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
20  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.util.FastMath;
23  import org.hipparchus.util.MathUtils;
24  import org.hipparchus.util.Precision;
25  import org.orekit.attitudes.AttitudeProvider;
26  import org.orekit.errors.OrekitException;
27  import org.orekit.errors.OrekitMessages;
28  import org.orekit.frames.Frame;
29  import org.orekit.orbits.CartesianOrbit;
30  import org.orekit.orbits.Orbit;
31  import org.orekit.propagation.SpacecraftState;
32  import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
33  import org.orekit.propagation.analytical.gnss.data.GNSSOrbitalElements;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.utils.PVCoordinates;
36  
37  /** Common handling of {@link AbstractAnalyticalPropagator} methods for GNSS propagators.
38   * <p>
39   * This class allows to provide easily a subset of {@link AbstractAnalyticalPropagator} methods
40   * for specific GNSS propagators.
41   * </p>
42   * @author Pascal Parraud
43   */
44  public class GNSSPropagator extends AbstractAnalyticalPropagator {
45  
46      // Data used to solve Kepler's equation
47      /** First coefficient to compute Kepler equation solver starter. */
48      private static final double A;
49  
50      /** Second coefficient to compute Kepler equation solver starter. */
51      private static final double B;
52  
53      static {
54          final double k1 = 3 * FastMath.PI + 2;
55          final double k2 = FastMath.PI - 1;
56          final double k3 = 6 * FastMath.PI - 1;
57          A  = 3 * k2 * k2 / k1;
58          B  = k3 * k3 / (6 * k1);
59      }
60  
61      /** The GNSS orbital elements used. */
62      private final GNSSOrbitalElements gnssOrbit;
63  
64      /** The spacecraft mass (kg). */
65      private final double mass;
66  
67      /** The ECI frame used for GNSS propagation. */
68      private final Frame eci;
69  
70      /** The ECEF frame used for GNSS propagation. */
71      private final Frame ecef;
72  
73      /**
74       * Build a new instance.
75       * @param gnssOrbit GNSS orbital elements
76       * @param eci Earth Centered Inertial frame
77       * @param ecef Earth Centered Earth Fixed frame
78       * @param provider Attitude provider
79       * @param mass Satellite mass (kg)
80       */
81      GNSSPropagator(final GNSSOrbitalElements gnssOrbit, final Frame eci,
82                     final Frame ecef, final AttitudeProvider provider,
83                     final double mass) {
84          super(provider);
85          // Stores the GNSS orbital elements
86          this.gnssOrbit = gnssOrbit;
87          // Sets the start date as the date of the orbital elements
88          setStartDate(gnssOrbit.getDate());
89          // Sets the mass
90          this.mass = mass;
91          // Sets the Earth Centered Inertial frame
92          this.eci  = eci;
93          // Sets the Earth Centered Earth Fixed frame
94          this.ecef = ecef;
95      }
96  
97      /**
98       * Gets the Earth Centered Inertial frame used to propagate the orbit.
99       *
100      * @return the ECI frame
101      */
102     public Frame getECI() {
103         return eci;
104     }
105 
106     /**
107      * Gets the Earth Centered Earth Fixed frame used to propagate GNSS orbits according to the
108      * Interface Control Document.
109      *
110      * @return the ECEF frame
111      */
112     public Frame getECEF() {
113         return ecef;
114     }
115 
116     /**
117      * Gets the Earth gravity coefficient used for GNSS propagation.
118      *
119      * @return the Earth gravity coefficient.
120      */
121     public double getMU() {
122         return gnssOrbit.getMu();
123     }
124 
125     /**
126      * Get the underlying GNSS orbital elements.
127      *
128      * @return the underlying GNSS orbital elements
129      */
130     public GNSSOrbitalElements getOrbitalElements() {
131         return gnssOrbit;
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     protected Orbit propagateOrbit(final AbsoluteDate date) {
137         // Gets the PVCoordinates in ECEF frame
138         final PVCoordinates pvaInECEF = propagateInEcef(date);
139         // Transforms the PVCoordinates to ECI frame
140         final PVCoordinates pvaInECI = ecef.getTransformTo(eci, date).transformPVCoordinates(pvaInECEF);
141         // Returns the Cartesian orbit
142         return new CartesianOrbit(pvaInECI, eci, date, getMU());
143     }
144 
145     /**
146      * Gets the PVCoordinates of the GNSS SV in {@link #getECEF() ECEF frame}.
147      *
148      * <p>The algorithm uses automatic differentiation to compute velocity and
149      * acceleration.</p>
150      *
151      * @param date the computation date
152      * @return the GNSS SV PVCoordinates in {@link #getECEF() ECEF frame}
153      */
154     public PVCoordinates propagateInEcef(final AbsoluteDate date) {
155         // Duration from GNSS ephemeris Reference date
156         final UnivariateDerivative2 tk = new UnivariateDerivative2(getTk(date), 1.0, 0.0);
157         // Mean anomaly
158         final UnivariateDerivative2 mk = tk.multiply(gnssOrbit.getMeanMotion()).add(gnssOrbit.getM0());
159         // Eccentric Anomaly
160         final UnivariateDerivative2 ek = getEccentricAnomaly(mk);
161         // True Anomaly
162         final UnivariateDerivative2 vk =  getTrueAnomaly(ek);
163         // Argument of Latitude
164         final UnivariateDerivative2 phik    = vk.add(gnssOrbit.getPa());
165         final UnivariateDerivative2 twoPhik = phik.multiply(2);
166         final UnivariateDerivative2 c2phi   = twoPhik.cos();
167         final UnivariateDerivative2 s2phi   = twoPhik.sin();
168         // Argument of Latitude Correction
169         final UnivariateDerivative2 dphik = c2phi.multiply(gnssOrbit.getCuc()).add(s2phi.multiply(gnssOrbit.getCus()));
170         // Radius Correction
171         final UnivariateDerivative2 drk = c2phi.multiply(gnssOrbit.getCrc()).add(s2phi.multiply(gnssOrbit.getCrs()));
172         // Inclination Correction
173         final UnivariateDerivative2 dik = c2phi.multiply(gnssOrbit.getCic()).add(s2phi.multiply(gnssOrbit.getCis()));
174         // Corrected Argument of Latitude
175         final UnivariateDerivative2 uk = phik.add(dphik);
176         // Corrected Radius
177         final UnivariateDerivative2 rk = ek.cos().multiply(-gnssOrbit.getE()).add(1).multiply(gnssOrbit.getSma()).add(drk);
178         // Corrected Inclination
179         final UnivariateDerivative2 ik  = tk.multiply(gnssOrbit.getIDot()).add(gnssOrbit.getI0()).add(dik);
180         final UnivariateDerivative2 cik = ik.cos();
181         // Positions in orbital plane
182         final UnivariateDerivative2 xk = uk.cos().multiply(rk);
183         final UnivariateDerivative2 yk = uk.sin().multiply(rk);
184         // Corrected longitude of ascending node
185         final UnivariateDerivative2 omk = tk.multiply(gnssOrbit.getOmegaDot() - gnssOrbit.getAngularVelocity()).
186                                         add(gnssOrbit.getOmega0() - gnssOrbit.getAngularVelocity() * gnssOrbit.getTime());
187         final UnivariateDerivative2 comk = omk.cos();
188         final UnivariateDerivative2 somk = omk.sin();
189         // returns the Earth-fixed coordinates
190         final FieldVector3D<UnivariateDerivative2> positionwithDerivatives =
191                         new FieldVector3D<>(xk.multiply(comk).subtract(yk.multiply(somk).multiply(cik)),
192                                             xk.multiply(somk).add(yk.multiply(comk).multiply(cik)),
193                                             yk.multiply(ik.sin()));
194         return new PVCoordinates(new Vector3D(positionwithDerivatives.getX().getValue(),
195                                               positionwithDerivatives.getY().getValue(),
196                                               positionwithDerivatives.getZ().getValue()),
197                                  new Vector3D(positionwithDerivatives.getX().getFirstDerivative(),
198                                               positionwithDerivatives.getY().getFirstDerivative(),
199                                               positionwithDerivatives.getZ().getFirstDerivative()),
200                                  new Vector3D(positionwithDerivatives.getX().getSecondDerivative(),
201                                               positionwithDerivatives.getY().getSecondDerivative(),
202                                               positionwithDerivatives.getZ().getSecondDerivative()));
203     }
204 
205     /**
206      * Gets the duration from GNSS Reference epoch.
207      * <p>This takes the GNSS week roll-over into account.</p>
208      * @param date the considered date
209      * @return the duration from GNSS orbit Reference epoch (s)
210      */
211     private double getTk(final AbsoluteDate date) {
212         final double cycleDuration = gnssOrbit.getCycleDuration();
213         // Time from ephemeris reference epoch
214         double tk = date.durationFrom(gnssOrbit.getDate());
215         // Adjusts the time to take roll over week into account
216         while (tk > 0.5 * cycleDuration) {
217             tk -= cycleDuration;
218         }
219         while (tk < -0.5 * cycleDuration) {
220             tk += cycleDuration;
221         }
222         // Returns the time from ephemeris reference epoch
223         return tk;
224     }
225 
226     /**
227      * Gets eccentric anomaly from mean anomaly.
228      * <p>The algorithm used to solve the Kepler equation has been published in:
229      * "Procedures for  solving Kepler's Equation", A. W. Odell and R. H. Gooding,
230      * Celestial Mechanics 38 (1986) 307-334</p>
231      * <p>It has been copied from the OREKIT library (KeplerianOrbit class).</p>
232      *
233      * @param mk the mean anomaly (rad)
234      * @return the eccentric anomaly (rad)
235      */
236     private UnivariateDerivative2 getEccentricAnomaly(final UnivariateDerivative2 mk) {
237 
238         // reduce M to [-PI PI] interval
239         final UnivariateDerivative2 reducedM = new UnivariateDerivative2(MathUtils.normalizeAngle(mk.getValue(), 0.0),
240                                                                          mk.getFirstDerivative(),
241                                                                          mk.getSecondDerivative());
242 
243         // compute start value according to A. W. Odell and R. H. Gooding S12 starter
244         UnivariateDerivative2 ek;
245         if (FastMath.abs(reducedM.getValue()) < 1.0 / 6.0) {
246             if (FastMath.abs(reducedM.getValue()) < Precision.SAFE_MIN) {
247                 // this is an Orekit change to the S12 starter.
248                 // If reducedM is 0.0, the derivative of cbrt is infinite which induces NaN appearing later in
249                 // the computation. As in this case E and M are almost equal, we initialize ek with reducedM
250                 ek = reducedM;
251             } else {
252                 // this is the standard S12 starter
253                 ek = reducedM.add(reducedM.multiply(6).cbrt().subtract(reducedM).multiply(gnssOrbit.getE()));
254             }
255         } else {
256             if (reducedM.getValue() < 0) {
257                 final UnivariateDerivative2 w = reducedM.add(FastMath.PI);
258                 ek = reducedM.add(w.multiply(-A).divide(w.subtract(B)).subtract(FastMath.PI).subtract(reducedM).multiply(gnssOrbit.getE()));
259             } else {
260                 final UnivariateDerivative2 minusW = reducedM.subtract(FastMath.PI);
261                 ek = reducedM.add(minusW.multiply(A).divide(minusW.add(B)).add(FastMath.PI).subtract(reducedM).multiply(gnssOrbit.getE()));
262             }
263         }
264 
265         final double e1 = 1 - gnssOrbit.getE();
266         final boolean noCancellationRisk = (e1 + ek.getValue() * ek.getValue() / 6) >= 0.1;
267 
268         // perform two iterations, each consisting of one Halley step and one Newton-Raphson step
269         for (int j = 0; j < 2; ++j) {
270             final UnivariateDerivative2 f;
271             UnivariateDerivative2 fd;
272             final UnivariateDerivative2 fdd  = ek.sin().multiply(gnssOrbit.getE());
273             final UnivariateDerivative2 fddd = ek.cos().multiply(gnssOrbit.getE());
274             if (noCancellationRisk) {
275                 f  = ek.subtract(fdd).subtract(reducedM);
276                 fd = fddd.subtract(1).negate();
277             } else {
278                 f  = eMeSinE(ek).subtract(reducedM);
279                 final UnivariateDerivative2 s = ek.multiply(0.5).sin();
280                 fd = s.multiply(s).multiply(2 * gnssOrbit.getE()).add(e1);
281             }
282             final UnivariateDerivative2 dee = f.multiply(fd).divide(f.multiply(0.5).multiply(fdd).subtract(fd.multiply(fd)));
283 
284             // update eccentric anomaly, using expressions that limit underflow problems
285             final UnivariateDerivative2 w = fd.add(dee.multiply(0.5).multiply(fdd.add(dee.multiply(fdd).divide(3))));
286             fd = fd.add(dee.multiply(fdd.add(dee.multiply(0.5).multiply(fdd))));
287             ek = ek.subtract(f.subtract(dee.multiply(fd.subtract(w))).divide(fd));
288         }
289 
290         // expand the result back to original range
291         ek = ek.add(mk.getValue() - reducedM.getValue());
292 
293         // Returns the eccentric anomaly
294         return ek;
295     }
296 
297     /**
298      * Accurate computation of E - e sin(E).
299      *
300      * @param E eccentric anomaly
301      * @return E - e sin(E)
302      */
303     private UnivariateDerivative2 eMeSinE(final UnivariateDerivative2 E) {
304         UnivariateDerivative2 x = E.sin().multiply(1 - gnssOrbit.getE());
305         final UnivariateDerivative2 mE2 = E.negate().multiply(E);
306         UnivariateDerivative2 term = E;
307         UnivariateDerivative2 d    = E.getField().getZero();
308         // the inequality test below IS intentional and should NOT be replaced by a check with a small tolerance
309         for (UnivariateDerivative2 x0 = d.add(Double.NaN); !Double.valueOf(x.getValue()).equals(Double.valueOf(x0.getValue()));) {
310             d = d.add(2);
311             term = term.multiply(mE2.divide(d.multiply(d.add(1))));
312             x0 = x;
313             x = x.subtract(term);
314         }
315         return x;
316     }
317 
318     /** Gets true anomaly from eccentric anomaly.
319      *
320      * @param ek the eccentric anomaly (rad)
321      * @return the true anomaly (rad)
322      */
323     private UnivariateDerivative2 getTrueAnomaly(final UnivariateDerivative2 ek) {
324         final UnivariateDerivative2 svk = ek.sin().multiply(FastMath.sqrt(1. - gnssOrbit.getE() * gnssOrbit.getE()));
325         final UnivariateDerivative2 cvk = ek.cos().subtract(gnssOrbit.getE());
326         return svk.atan2(cvk);
327     }
328 
329     /** {@inheritDoc} */
330     @Override
331     public Frame getFrame() {
332         return eci;
333     }
334 
335     /** {@inheritDoc} */
336     @Override
337     protected double getMass(final AbsoluteDate date) {
338         return mass;
339     }
340 
341     /** {@inheritDoc} */
342     @Override
343     public void resetInitialState(final SpacecraftState state) {
344         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
345     }
346 
347     /** {@inheritDoc} */
348     @Override
349     protected void resetIntermediateState(final SpacecraftState state, final boolean forward) {
350         throw new OrekitException(OrekitMessages.NON_RESETABLE_STATE);
351     }
352 
353 }