1   /* Copyright 2002-2026 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.tle;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
24  import org.hipparchus.util.FastMath;
25  import org.hipparchus.util.MathArrays;
26  import org.hipparchus.util.MathUtils;
27  import org.hipparchus.util.Pair;
28  import org.orekit.annotation.DefaultDataContext;
29  import org.orekit.attitudes.AttitudeProvider;
30  import org.orekit.attitudes.FieldAttitude;
31  import org.orekit.attitudes.FrameAlignedProvider;
32  import org.orekit.data.DataContext;
33  import org.orekit.errors.OrekitException;
34  import org.orekit.errors.OrekitMessages;
35  import org.orekit.frames.Frame;
36  import org.orekit.orbits.FieldCartesianOrbit;
37  import org.orekit.orbits.FieldOrbit;
38  import org.orekit.propagation.FieldSpacecraftState;
39  import org.orekit.propagation.analytical.FieldAbstractAnalyticalPropagator;
40  import org.orekit.propagation.analytical.tle.generation.TleGenerationAlgorithm;
41  import org.orekit.time.FieldAbsoluteDate;
42  import org.orekit.time.TimeScale;
43  import org.orekit.utils.FieldPVCoordinates;
44  import org.orekit.utils.PVCoordinates;
45  import org.orekit.utils.ParameterDriver;
46  import org.orekit.utils.TimeSpanMap;
47  
48  
49  /** This class provides elements to propagate TLE's.
50   * <p>
51   * The models used are SGP4 and SDP4, initially proposed by NORAD as the unique convenient
52   * propagator for TLE's. Inputs and outputs of this propagator are only suited for
53   * NORAD two lines elements sets, since it uses estimations and mean values appropriate
54   * for TLE's only.
55   * </p>
56   * <p>
57   * Deep- or near- space propagator is selected internally according to NORAD recommendations
58   * so that the user has not to worry about the used computation methods. One instance is created
59   * for each TLE (this instance can only be get using {@link #selectExtrapolator(FieldTLE)} method,
60   * and can compute {@link PVCoordinates position and velocity coordinates} at any
61   * time. Maximum accuracy is guaranteed in a 24h range period before and after the provided
62   * TLE epoch (of course this accuracy is not really measurable nor predictable: according to
63   * <a href="https://www.celestrak.com/">CelesTrak</a>, the precision is close to one kilometer
64   * and error won't probably rise above 2 km).
65   * </p>
66   * <p>This implementation is largely inspired from the paper and source code <a
67   * href="https://www.celestrak.com/publications/AIAA/2006-6753/">Revisiting Spacetrack
68   * Report #3</a> and is fully compliant with its results and tests cases.</p>
69   * @author Felix R. Hoots, Ronald L. Roehrich, December 1980 (original fortran)
70   * @author David A. Vallado, Paul Crawford, Richard Hujsak, T.S. Kelso (C++ translation and improvements)
71   * @author Fabien Maussion (java translation)
72   * @author Thomas Paulet (field translation)
73   * @since 11.0
74   * @see FieldTLE
75   * @param <T> type of the field elements
76   */
77  public abstract class FieldTLEPropagator<T extends CalculusFieldElement<T>> extends FieldAbstractAnalyticalPropagator<T> {
78  
79      // CHECKSTYLE: stop VisibilityModifier check
80  
81      /** Initial state. */
82      protected FieldTLE<T> tle;
83  
84      /** UTC time scale. */
85      protected final TimeScale utc;
86  
87      /** final RAAN. */
88      protected T xnode;
89  
90      /** final semi major axis. */
91      protected T a;
92  
93      /** final eccentricity. */
94      protected T e;
95  
96      /** final inclination. */
97      protected T i;
98  
99      /** final periapsis argument. */
100     protected T omega;
101 
102     /** L from SPTRCK #3. */
103     protected T xl;
104 
105     /** original recovered semi major axis. */
106     protected T a0dp;
107 
108     /** original recovered mean motion. */
109     protected T xn0dp;
110 
111     /** cosinus original inclination. */
112     protected T cosi0;
113 
114     /** cos io squared. */
115     protected T theta2;
116 
117     /** sinus original inclination. */
118     protected T sini0;
119 
120     /** common parameter for mean anomaly (M) computation. */
121     protected T xmdot;
122 
123     /** common parameter for periapsis argument (omega) computation. */
124     protected T omgdot;
125 
126     /** common parameter for raan (OMEGA) computation. */
127     protected T xnodot;
128 
129     /** original eccentricity squared. */
130     protected T e0sq;
131     /** 1 - e2. */
132     protected T beta02;
133 
134     /** sqrt (1 - e2). */
135     protected T beta0;
136 
137     /** periapsis, expressed in KM and ALTITUDE. */
138     protected T perige;
139 
140     /** eta squared. */
141     protected T etasq;
142 
143     /** original eccentricity * eta. */
144     protected T eeta;
145 
146     /** s* new value for the contant s. */
147     protected T s4;
148 
149     /** tsi from SPTRCK #3. */
150     protected T tsi;
151 
152     /** eta from SPTRCK #3. */
153     protected T eta;
154 
155     /** coef for SGP C3 computation. */
156     protected T coef;
157 
158     /** coef for SGP C5 computation. */
159     protected T coef1;
160 
161     /** C1 from SPTRCK #3. */
162     protected T c1;
163 
164     /** C2 from SPTRCK #3. */
165     protected T c2;
166 
167     /** C4 from SPTRCK #3. */
168     protected T c4;
169 
170     /** common parameter for raan (OMEGA) computation. */
171     protected T xnodcf;
172 
173     /** 3/2 * C1. */
174     protected T t2cof;
175 
176     // CHECKSTYLE: resume VisibilityModifier check
177 
178     /** TLE frame. */
179     private final Frame teme;
180 
181     /** All TLEs and masses. */
182     private TimeSpanMap<Pair<FieldTLE<T>, T>> tlesAndMasses;
183 
184     /** Driver for the ballistic parameter.
185      * @since 14.0
186      */
187     private final ParameterDriver bStarDriver;
188 
189     /** TLE generation algorithm used when resetting TLE from state. */
190     private TleGenerationAlgorithm generationAlgorithm;
191 
192     /** Protected constructor for derived classes.
193      *
194      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
195      *
196      * @param initialTLE the unique TLE to propagate
197      * @param attitudeProvider provider for attitude computation
198      * @param mass spacecraft mass (kg)
199      * @see #FieldTLEPropagator(FieldTLE, AttitudeProvider, CalculusFieldElement, Frame)
200      * @since 14.0
201      */
202     @DefaultDataContext
203     protected FieldTLEPropagator(final FieldTLE<T> initialTLE, final AttitudeProvider attitudeProvider, final T mass) {
204         this(initialTLE, attitudeProvider, mass, DataContext.getDefault().getFrames().getTEME());
205     }
206 
207     /** Protected constructor for derived classes.
208      * @param initialTLE the unique TLE to propagate
209      * @param attitudeProvider provider for attitude computation
210      * @param mass spacecraft mass (kg)
211      * @param teme the TEME frame to use for propagation.
212      * @since 14.0
213      */
214     protected FieldTLEPropagator(final FieldTLE<T> initialTLE, final AttitudeProvider attitudeProvider, final T mass,
215                                  final Frame teme) {
216         super(initialTLE.getE().getField(), attitudeProvider);
217         setStartDate(initialTLE.getDate());
218         this.utc           = initialTLE.getUtc();
219         initializeTle(initialTLE);
220         this.teme          = teme;
221         this.tlesAndMasses = new TimeSpanMap<>(new Pair<>(tle, mass));
222         this.bStarDriver   = new ParameterDriver(TleGenerationAlgorithm.B_STAR,
223                                                  initialTLE.getBStar().getReal(),
224                                                  TleGenerationAlgorithm.B_STAR_SCALE,
225                                                  Double.NEGATIVE_INFINITY,
226                                                  Double.POSITIVE_INFINITY);
227         this.generationAlgorithm = TLEPropagator.getDefaultTleGenerationAlgorithm(initialTLE.toTLE(), this.utc, teme);
228 
229         initializeCommons(tle.getBStar());
230         sxpInitialize(tle.getBStar());
231         // set the initial state
232         final T[] parameters = MathArrays.buildArray(initialTLE.getDate().getField(), 1);
233         parameters[0] = initialTLE.getBStar();
234         final FieldOrbit<T> orbit = propagateOrbit(initialTLE.getDate(), parameters);
235         final FieldAttitude<T> attitude = attitudeProvider.getAttitude(orbit, orbit.getDate(), orbit.getFrame());
236         super.resetInitialState(new FieldSpacecraftState<>(orbit, attitude).withMass(mass));
237     }
238 
239     /** Selects the extrapolator to use with the selected TLE.
240      *
241      * <p>This method uses the {@link DataContext#getDefault() default data context}.
242      *
243      * @param tle the TLE to propagate.
244      * @return the correct propagator.
245      * @param <T> elements type
246      * @see #selectExtrapolator(FieldTLE, Frame)
247      * @since 14.0
248      */
249     @DefaultDataContext
250     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(final FieldTLE<T> tle) {
251         return selectExtrapolator(tle, DataContext.getDefault().getFrames().getTEME());
252     }
253 
254     /** Selects the extrapolator to use with the selected TLE.
255      *
256      *<p>This method uses the {@link DataContext#getDefault() default data context}.
257      *
258      * @param tle the TLE to propagate.
259      * @param teme TEME frame.
260      * @return the correct propagator.
261      * @param <T> elements type
262      * @since 14.0
263      */
264     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(final FieldTLE<T> tle,
265                                                                                                final Frame teme) {
266         return selectExtrapolator(tle, FrameAlignedProvider.of(teme),
267                                   tle.getE().getField().getZero().newInstance(DEFAULT_MASS), teme);
268     }
269 
270     /** Selects the extrapolator to use with the selected TLE.
271      *
272      * <p>This method uses the {@link DataContext#getDefault() default data context}.
273      *
274      * @param tle the TLE to propagate.
275      * @param attitudeProvider provider for attitude computation
276      * @param mass spacecraft mass (kg)
277      * @return the correct propagator.
278      * @param <T> elements type
279      * @see #selectExtrapolator(FieldTLE, AttitudeProvider, CalculusFieldElement, Frame)
280      * @since 14.0
281      */
282     @DefaultDataContext
283     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(final FieldTLE<T> tle,
284                                                                                                final AttitudeProvider attitudeProvider,
285                                                                                                final T mass) {
286         return selectExtrapolator(tle, attitudeProvider, mass,
287                                   DataContext.getDefault().getFrames().getTEME());
288     }
289 
290     /** Selects the extrapolator to use with the selected TLE.
291      *
292      * @param tle the TLE to propagate.
293      * @param attitudeProvider provider for attitude computation
294      * @param mass spacecraft mass (kg)
295      * @param teme the TEME frame to use for propagation.
296      * @return the correct propagator.
297      * @param <T> elements type
298      * @since 14.0
299      */
300     public static <T extends CalculusFieldElement<T>> FieldTLEPropagator<T> selectExtrapolator(
301             final FieldTLE<T> tle,
302             final AttitudeProvider attitudeProvider,
303             final T mass,
304             final Frame teme) {
305 
306         final T xkeOverN = tle.getMeanMotion().multiply(60.0).reciprocal().multiply(TLEConstants.XKE);
307         final T a1 = xkeOverN.multiply(xkeOverN).cbrt();
308         final T cosi0 = FastMath.cos(tle.getI());
309         final T temp1 = cosi0.multiply(cosi0.multiply(3.0)).subtract(1.0).multiply(1.5 * TLEConstants.CK2);
310         final T oMe2  =  tle.getE().multiply(tle.getE()).negate().add(1.0);
311         final T temp = temp1.divide(oMe2.multiply(oMe2.sqrt()));
312         final T delta1 = temp.divide(a1.multiply(a1));
313         final T a0 = a1.multiply(delta1.multiply(delta1.multiply(
314                         delta1.multiply(134.0 / 81.0).add(1.0)).add(TLEConstants.ONE_THIRD)).negate().add(1.0));
315         final T delta0 = temp.divide(a0.multiply(a0));
316 
317         // recover original mean motion :
318         final T xn0dp = tle.getMeanMotion().multiply(60.0).divide(delta0.add(1.0));
319 
320         final FieldTLEPropagator<T> propagator;
321         // Period >= 225 minutes is deep space
322         if (MathUtils.TWO_PI / (xn0dp.multiply(TLEConstants.MINUTES_PER_DAY).getReal()) >= (1.0 / 6.4)) {
323             propagator = new FieldDeepSDP4<>(tle, attitudeProvider, mass, teme);
324         } else {
325             propagator = new FieldSGP4<>(tle, attitudeProvider, mass, teme);
326         }
327 
328         return propagator;
329 
330     }
331 
332     /** Get the Earth gravity coefficient used for TLE propagation.
333      * @return the Earth gravity coefficient.
334      */
335     public static double getMU() {
336         return TLEConstants.MU;
337     }
338 
339     /** Get the extrapolated position and velocity from an initial TLE.
340      * @param date the final date
341      * @param bStar value of the ballistic coefficient to use for propagation
342      * @return the final PVCoordinates
343      */
344     public FieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> date, final T bStar) {
345 
346         sxpPropagate(date.durationFrom(tle.getDate()).divide(60.0), bStar);
347 
348         // Compute PV with previous calculated parameters
349         return computePVCoordinates();
350     }
351 
352     /** Computation of the first commons parameters.
353      * @param bStar value of the ballistic coefficient to use for propagation
354      */
355     private void initializeCommons(final T bStar) {
356 
357         final T zero = tle.getDate().getField().getZero();
358         final T xkeOverN = tle.getMeanMotion().multiply(60.0).reciprocal().multiply(TLEConstants.XKE);
359         final T a1 = xkeOverN.multiply(xkeOverN).cbrt();
360         cosi0 = FastMath.cos(tle.getI());
361         theta2 = cosi0.multiply(cosi0);
362         final T x3thm1 = theta2.multiply(3.0).subtract(1.0);
363         e0sq = tle.getE().square();
364         beta02 = e0sq.negate().add(1.0);
365         beta0 = FastMath.sqrt(beta02);
366         final T tval = x3thm1.multiply(1.5 * TLEConstants.CK2).divide(beta0.multiply(beta02));
367         final T delta1 = tval.divide(a1.multiply(a1));
368         final T a0 = a1.multiply(delta1.multiply(
369                      delta1.multiply(134.0 / 81.0).add(1.0).multiply(delta1).add(TLEConstants.ONE_THIRD)).negate().add(1.0));
370         final T delta0 = tval.divide(a0.multiply(a0));
371 
372         // recover original mean motion and semi-major axis :
373         xn0dp = tle.getMeanMotion().multiply(60.0).divide(delta0.add(1.0));
374         a0dp = a0.divide(delta0.negate().add(1.0));
375 
376         // Values of s and qms2t :
377         s4 = zero.newInstance(TLEConstants.S);  // unmodified value for s
378         T q0ms24 = zero.newInstance(TLEConstants.QOMS2T); // unmodified value for q0ms2T
379 
380         perige = a0dp.multiply(tle.getE().negate().add(1.0)).subtract(TLEConstants.NORMALIZED_EQUATORIAL_RADIUS).multiply(
381                 TLEConstants.EARTH_RADIUS); // perige
382 
383         //  For periapsis below 156 km, the values of s and qoms2t are changed :
384         if (perige.getReal() < 156.0) {
385             if (perige.getReal() <= 98.0) {
386                 s4 = zero.newInstance(20.0);
387             } else {
388                 s4 = perige.subtract(78.0);
389             }
390             final T temp_val = s4.negate().add(120.0).multiply(TLEConstants.NORMALIZED_EQUATORIAL_RADIUS / TLEConstants.EARTH_RADIUS);
391             final T temp_val_squared = temp_val.multiply(temp_val);
392             q0ms24 = temp_val_squared.square();
393             s4 = s4.divide(TLEConstants.EARTH_RADIUS).add(TLEConstants.NORMALIZED_EQUATORIAL_RADIUS); // new value for q0ms2T and s
394         }
395 
396         final T pinv = a0dp.multiply(beta02).reciprocal();
397         final T pinvsq = pinv.square();
398         tsi = a0dp.subtract(s4).reciprocal();
399         eta = a0dp.multiply(tle.getE()).multiply(tsi);
400         etasq = eta.square();
401         eeta = tle.getE().multiply(eta);
402 
403         final T psisq = etasq.negate().add(1.0).abs(); // abs because pow 3.5 needs positive value
404         final T tsi_squared = tsi.multiply(tsi);
405         coef = q0ms24.multiply(tsi_squared.square());
406         coef1 = coef.divide(psisq.pow(3.5));
407 
408         // C2 and C1 coefficients computation :
409         c2 = coef1.multiply(xn0dp).multiply(a0dp.multiply(
410                 etasq.multiply(1.5).add(eeta.multiply(etasq.add(4.0))).add(1.0)).add(
411                 tsi.divide(psisq).multiply(x3thm1).multiply(0.75 * TLEConstants.CK2).multiply(
412                         etasq.multiply(etasq.add(8.0)).multiply(3.0).add(8.0))));
413         c1 = bStar.multiply(c2);
414         sini0 = FastMath.sin(tle.getI());
415 
416         final T x1mth2 = theta2.negate().add(1.0);
417 
418         // C4 coefficient computation :
419         c4 = xn0dp.multiply(coef1).multiply(a0dp).multiply(2.0).multiply(beta02).multiply(
420                 eta.multiply(etasq.multiply(0.5).add(2.0)).add(tle.getE().multiply(etasq.multiply(2.0).add(0.5))).subtract(
421                         tsi.divide(a0dp.multiply(psisq)).multiply(2 * TLEConstants.CK2).multiply(
422                                 x3thm1.multiply(-3).multiply(etasq.multiply(eeta.multiply(-0.5).add(1.5)).add(eeta.multiply(-2.0)).add(1.0)).add(
423                                         x1mth2.multiply(0.75).multiply(etasq.multiply(2.0).subtract(eeta.multiply(etasq.add(1.0)))).multiply(FastMath.cos(tle.getPeriapsisArgument().multiply(2.0)))))));
424 
425         final T theta4 = theta2.multiply(theta2);
426         final T temp1  = pinvsq.multiply(xn0dp).multiply(3 * TLEConstants.CK2);
427         final T temp2  = temp1.multiply(pinvsq).multiply(TLEConstants.CK2);
428         final T temp3  = pinvsq.multiply(pinvsq).multiply(xn0dp).multiply(1.25 * TLEConstants.CK4);
429 
430         // atmospheric and gravitation coefs :(Mdf and OMEGAdf)
431         xmdot = xn0dp.add(
432                 temp1.multiply(0.5).multiply(beta0).multiply(x3thm1)).add(
433                 temp2.multiply(0.0625).multiply(beta0).multiply(
434                         theta2.multiply(78.0).negate().add(13.0).add(theta4.multiply(137.0))));
435 
436         final T x1m5th = theta2.multiply(5.0).negate().add(1.0);
437 
438         omgdot = temp1.multiply(-0.5).multiply(x1m5th).add(
439                 temp2.multiply(0.0625).multiply(theta2.multiply(114.0).negate().add(
440                         theta4.multiply(395.0)).add(7.0))).add(
441                 temp3.multiply(theta2.multiply(36.0).negate().add(theta4.multiply(49.0)).add(3.0)));
442 
443         final T xhdot1 = temp1.negate().multiply(cosi0);
444 
445         xnodot = xhdot1.add(temp2.multiply(0.5).multiply(theta2.multiply(19.0).negate().add(4.0)).add(
446                 temp3.multiply(2.0).multiply(theta2.multiply(7.0).negate().add(3.0))).multiply(cosi0));
447         xnodcf = beta02.multiply(xhdot1).multiply(c1).multiply(3.5);
448         t2cof = c1.multiply(1.5);
449 
450     }
451 
452     /** Retrieves the position and velocity.
453      * @return the computed PVCoordinates.
454      */
455     private FieldPVCoordinates<T> computePVCoordinates() {
456 
457         final T zero = tle.getDate().getField().getZero();
458         // Long period periodics
459         final T axn = e.multiply(FastMath.cos(omega));
460         T temp = a.multiply(e.multiply(e).negate().add(1.0)).reciprocal();
461         final T xlcof = sini0.multiply(0.125 * TLEConstants.A3OVK2).multiply(
462                 cosi0.multiply(5.0).add(3.0).divide(cosi0.add(1.0)));
463         final T aycof = sini0.multiply(0.25 * TLEConstants.A3OVK2);
464         final T xll   = temp.multiply(xlcof).multiply(axn);
465         final T aynl  = temp.multiply(aycof);
466         final T xlt   = xl.add(xll);
467         final T ayn   = e.multiply(FastMath.sin(omega)).add(aynl);
468         final T elsq  = axn.square().add(ayn.square());
469         final T capu  = MathUtils.normalizeAngle(xlt.subtract(xnode), zero.getPi());
470         T epw    = capu;
471         T ecosE  = zero;
472         T esinE  = zero;
473         T sinEPW = zero;
474         T cosEPW = zero;
475 
476         // Dundee changes:  items dependent on cosio get recomputed:
477         final T cosi0Sq = cosi0.square();
478         final T x3thm1  = cosi0Sq.multiply(3.0).subtract(1.0);
479         final T x1mth2  = cosi0Sq.negate().add(1.0);
480         final T x7thm1  = cosi0Sq.multiply(7.0).subtract(1.0);
481 
482         if (e.getReal() > (1 - 1e-6)) {
483             throw new OrekitException(OrekitMessages.TOO_LARGE_ECCENTRICITY_FOR_PROPAGATION_MODEL, e.getReal());
484         }
485 
486         // Solve Kepler's' Equation.
487         final double newtonRaphsonEpsilon = 1e-12;
488         for (int j = 0; j < 10; j++) {
489 
490             boolean doSecondOrderNewtonRaphson = true;
491 
492             sinEPW = FastMath.sin( epw);
493             cosEPW = FastMath.cos( epw);
494             ecosE  = axn.multiply(cosEPW).add(ayn.multiply(sinEPW));
495             esinE  = axn.multiply(sinEPW).subtract(ayn.multiply(cosEPW));
496             final T f = capu.subtract(epw).add(esinE);
497             if (FastMath.abs(f.getReal()) < newtonRaphsonEpsilon) {
498                 break;
499             }
500             final T fdot = ecosE.negate().add(1.0);
501             T delta_epw = f.divide(fdot);
502             if (j == 0) {
503                 final T maxNewtonRaphson = e.abs().multiply(1.25);
504                 doSecondOrderNewtonRaphson = false;
505                 if (delta_epw.getReal() > maxNewtonRaphson.getReal()) {
506                     delta_epw = maxNewtonRaphson;
507                 } else if (delta_epw.getReal() < -maxNewtonRaphson.getReal()) {
508                     delta_epw = maxNewtonRaphson.negate();
509                 } else {
510                     doSecondOrderNewtonRaphson = true;
511                 }
512             }
513             if (doSecondOrderNewtonRaphson) {
514                 delta_epw = f.divide(fdot.add(esinE.multiply(0.5).multiply(delta_epw)));
515             }
516             epw = epw.add(delta_epw);
517         }
518 
519         // Short period preliminary quantities
520         temp = elsq.negate().add(1.0);
521         final T pl = a.multiply(temp);
522         final T r  = a.multiply(ecosE.negate().add(1.0));
523         T temp2 = a.divide(r);
524         final T betal = FastMath.sqrt(temp);
525         temp = esinE.divide(betal.add(1.0));
526         final T cosu  = temp2.multiply(cosEPW.subtract(axn).add(ayn.multiply(temp)));
527         final T sinu  = temp2.multiply(sinEPW.subtract(ayn).subtract(axn.multiply(temp)));
528         final T u     = FastMath.atan2(sinu, cosu);
529         final T sin2u = sinu.multiply(cosu).multiply(2.0);
530         final T cos2u = cosu.multiply(cosu).multiply(2.0).subtract(1.0);
531         final T temp1 = pl.reciprocal().multiply(TLEConstants.CK2);
532         temp2         = temp1.divide(pl);
533 
534         // Update for short periodics
535         final T rk = r.multiply(temp2.multiply(betal).multiply(x3thm1).multiply(-1.5).add(1.0)).add(
536                      temp1.multiply(x1mth2).multiply(cos2u).multiply(0.5));
537         final T uk = u.subtract(temp2.multiply(x7thm1).multiply(sin2u).multiply(0.25));
538         final T xnodek = xnode.add(temp2.multiply(cosi0).multiply(sin2u).multiply(1.5));
539         final T xinck = i.add(temp2.multiply(cosi0).multiply(sini0).multiply(cos2u).multiply(1.5));
540 
541         // Orientation vectors
542         final T sinuk  = FastMath.sin(uk);
543         final T cosuk  = FastMath.cos(uk);
544         final T sinik  = FastMath.sin(xinck);
545         final T cosik  = FastMath.cos(xinck);
546         final T sinnok = FastMath.sin(xnodek);
547         final T cosnok = FastMath.cos(xnodek);
548         final T xmx    = sinnok.negate().multiply(cosik);
549         final T xmy    = cosnok.multiply(cosik);
550         final T ux     = xmx.multiply(sinuk).add(cosnok.multiply(cosuk));
551         final T uy     = xmy.multiply(sinuk).add(sinnok.multiply(cosuk));
552         final T uz     = sinik.multiply(sinuk);
553 
554         // Position and velocity
555         final T cr = rk.multiply(1000 * TLEConstants.EARTH_RADIUS);
556         final FieldVector3D<T> pos = new FieldVector3D<>(cr.multiply(ux), cr.multiply(uy), cr.multiply(uz));
557 
558         final T sqrtA  = FastMath.sqrt(a);
559         final T rdot   = sqrtA.multiply(esinE.divide(r)).multiply(TLEConstants.XKE);
560         final T rfdot  = FastMath.sqrt(pl).divide(r).multiply(TLEConstants.XKE);
561         final T xn     = a.multiply(sqrtA).reciprocal().multiply(TLEConstants.XKE);
562         final T rdotk  = rdot.subtract(xn.multiply(temp1).multiply(x1mth2).multiply(sin2u));
563         final T rfdotk = rfdot.add(xn.multiply(temp1).multiply(x1mth2.multiply(cos2u).add(x3thm1.multiply(1.5))));
564         final T vx     = xmx.multiply(cosuk).subtract(cosnok.multiply(sinuk));
565         final T vy     = xmy.multiply(cosuk).subtract(sinnok.multiply(sinuk));
566         final T vz     = sinik.multiply(cosuk);
567 
568         final double cv = 1000.0 * TLEConstants.EARTH_RADIUS / 60.0;
569         final FieldVector3D<T> vel = new FieldVector3D<>(rdotk.multiply(ux).add(rfdotk.multiply(vx)).multiply(cv),
570                                                           rdotk.multiply(uy).add(rfdotk.multiply(vy)).multiply(cv),
571                                                           rdotk.multiply(uz).add(rfdotk.multiply(vz)).multiply(cv));
572         return new FieldPVCoordinates<>(pos, vel);
573 
574     }
575 
576     /** {@inheritDoc} */
577     @Override
578     public List<ParameterDriver> getParametersDrivers() {
579         return Collections.singletonList(bStarDriver);
580     }
581 
582     /** Initialization proper to each propagator (SGP or SDP).
583      * @param bStar value of the ballistic coefficient to use for propagation
584      */
585     protected abstract void sxpInitialize(T bStar);
586 
587     /** Propagation proper to each propagator (SGP or SDP).
588      * @param t the offset from initial epoch (min)
589      * @param bStar value of the ballistic coefficient to use for propagation
590      */
591     protected abstract void sxpPropagate(T t, T bStar);
592 
593     /** {@inheritDoc}
594      * <p>
595      * For TLE propagator, calling this method is only recommended
596      * for covariance propagation when the new <code>state</code>
597      * differs from the previous one by only adding the additional
598      * state containing the derivatives.
599      * </p>
600      */
601     public void resetInitialState(final FieldSpacecraftState<T> state) {
602         super.resetInitialState(state);
603         resetTle(state);
604         tlesAndMasses = new TimeSpanMap<>(new Pair<>(tle, state.getMass()));
605     }
606 
607     /** {@inheritDoc} */
608     protected void resetIntermediateState(final FieldSpacecraftState<T> state, final boolean forward) {
609         resetTle(state);
610         final Pair<FieldTLE<T>, T> tleAndMass = new Pair<>(tle, state.getMass());
611         if (forward) {
612             tlesAndMasses.addValidAfter(tleAndMass, state.getDate().toAbsoluteDate(), false);
613         } else {
614             tlesAndMasses.addValidBefore(tleAndMass, state.getDate().toAbsoluteDate(), false);
615         }
616         stateChanged(state);
617     }
618 
619     /** Set the TLE generation algorithm used when resetting TLE from state.
620      * @param tleGenerationAlgorithm TLE generation algorithm
621      * @since 14.0
622      */
623     public void setTleGenerationAlgorithm(final TleGenerationAlgorithm tleGenerationAlgorithm) {
624         this.generationAlgorithm = tleGenerationAlgorithm;
625     }
626 
627     /** Reset internal TLE from a SpacecraftState.
628      * @param state spacecraft state on which to base new TLE
629      */
630     private void resetTle(final FieldSpacecraftState<T> state) {
631         final FieldTLE<T> newTle = generationAlgorithm.generate(state, tle);
632         initializeTle(newTle);
633     }
634 
635     /** Initialize internal TLE.
636      * @param newTle tle to replace current one
637      */
638     private void initializeTle(final FieldTLE<T> newTle) {
639         tle = newTle;
640         initializeCommons(newTle.getBStar());
641         sxpInitialize(newTle.getBStar());
642     }
643 
644     /** {@inheritDoc} */
645     protected T getMass(final FieldAbsoluteDate<T> date) {
646         return tlesAndMasses.get(date.toAbsoluteDate()).getValue();
647     }
648 
649     /** {@inheritDoc} */
650     public FieldOrbit<T> propagateOrbit(final FieldAbsoluteDate<T> date, final T[] parameters) {
651         final FieldTLE<T> closestTle = tlesAndMasses.get(date.toAbsoluteDate()).getKey();
652         if (parameters[0] != closestTle.getBStar() || !tle.equals(closestTle)) {
653             initializeTle(closestTle);
654         }
655         final T mu = date.getField().getZero().newInstance(TLEConstants.MU);
656         return new FieldCartesianOrbit<>(getPVCoordinates(date, parameters[0]), teme, date, mu);
657     }
658 
659     /** Get the underlying TLE.
660      * If there has been calls to #resetInitialState or #resetIntermediateState,
661      * it will not be the same as given to the constructor.
662      * @return underlying TLE
663      */
664     public FieldTLE<T> getTLE() {
665         return tle;
666     }
667 
668     /** {@inheritDoc} */
669     public Frame getFrame() {
670         return teme;
671     }
672 
673 }