1   /* Copyright 2002-2018 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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 org.hipparchus.util.FastMath;
20  import org.hipparchus.util.MathUtils;
21  import org.orekit.attitudes.AttitudeProvider;
22  import org.orekit.errors.OrekitException;
23  import org.orekit.time.AbsoluteDate;
24  import org.orekit.time.TimeScalesFactory;
25  import org.orekit.utils.Constants;
26  
27  
28  /** This class contains the methods that compute deep space perturbation terms.
29   * <p>
30   * The user should not bother in this class since it is handled internaly by the
31   * {@link TLEPropagator}.
32   * </p>
33   * <p>This implementation is largely inspired from the paper and source code <a
34   * href="http://www.celestrak.com/publications/AIAA/2006-6753/">Revisiting Spacetrack
35   * Report #3</a> and is fully compliant with its results and tests cases.</p>
36   * @author Felix R. Hoots, Ronald L. Roehrich, December 1980 (original fortran)
37   * @author David A. Vallado, Paul Crawford, Richard Hujsak, T.S. Kelso (C++ translation and improvements)
38   * @author Fabien Maussion (java translation)
39   */
40  public class DeepSDP4 extends SDP4 {
41  
42      // CHECKSTYLE: stop JavadocVariable check
43  
44      // Internal constants
45      private static final double ZNS      = 1.19459E-5;
46      private static final double ZES      = 0.01675;
47      private static final double ZNL      = 1.5835218E-4;
48      private static final double ZEL      = 0.05490;
49      private static final double THDT     = 4.3752691E-3;
50      private static final double C1SS     =  2.9864797E-6;
51      private static final double C1L      = 4.7968065E-7;
52  
53      private static final double ROOT22   = 1.7891679E-6;
54      private static final double ROOT32   = 3.7393792E-7;
55      private static final double ROOT44   = 7.3636953E-9;
56      private static final double ROOT52   = 1.1428639E-7;
57      private static final double ROOT54   = 2.1765803E-9;
58  
59      private static final double Q22      =  1.7891679E-6;
60      private static final double Q31      =  2.1460748E-6;
61      private static final double Q33      =  2.2123015E-7;
62  
63      private static final double C_FASX2  =  0.99139134268488593;
64      private static final double S_FASX2  =  0.13093206501640101;
65      private static final double C_2FASX4 =  0.87051638752972937;
66      private static final double S_2FASX4 = -0.49213943048915526;
67      private static final double C_3FASX6 =  0.43258117585763334;
68      private static final double S_3FASX6 =  0.90159499016666422;
69  
70      private static final double C_G22    =  0.87051638752972937;
71      private static final double S_G22    = -0.49213943048915526;
72      private static final double C_G32    =  0.57972190187001149;
73      private static final double S_G32    =  0.81481440616389245;
74      private static final double C_G44    = -0.22866241528815548;
75      private static final double S_G44    =  0.97350577801807991;
76      private static final double C_G52    =  0.49684831179884198;
77      private static final double S_G52    =  0.86783740128127729;
78      private static final double C_G54    = -0.29695209575316894;
79      private static final double S_G54    = -0.95489237761529999;
80  
81      /** Integration step (seconds). */
82      private static final double SECULAR_INTEGRATION_STEP  = 720.0;
83  
84      /** Intermediate values. */
85      private double thgr;
86      private double xnq;
87      private double omegaq;
88      private double zcosil;
89      private double zsinil;
90      private double zsinhl;
91      private double zcoshl;
92      private double zmol;
93      private double zcosgl;
94      private double zsingl;
95      private double zmos;
96      private double savtsn;
97  
98      private double ee2;
99      private double e3;
100     private double xi2;
101     private double xi3;
102     private double xl2;
103     private double xl3;
104     private double xl4;
105     private double xgh2;
106     private double xgh3;
107     private double xgh4;
108     private double xh2;
109     private double xh3;
110 
111     private double d2201;
112     private double d2211;
113     private double d3210;
114     private double d3222;
115     private double d4410;
116     private double d4422;
117     private double d5220;
118     private double d5232;
119     private double d5421;
120     private double d5433;
121     private double xlamo;
122 
123     private double sse;
124     private double ssi;
125     private double ssl;
126     private double ssh;
127     private double ssg;
128     private double se2;
129     private double si2;
130     private double sl2;
131     private double sgh2;
132     private double sh2;
133     private double se3;
134     private double si3;
135     private double sl3;
136     private double sgh3;
137     private double sh3;
138     private double sl4;
139     private double sgh4;
140 
141     private double del1;
142     private double del2;
143     private double del3;
144     private double xfact;
145     private double xli;
146     private double xni;
147     private double atime;
148 
149     private double pe;
150     private double pinc;
151     private double pl;
152     private double pgh;
153     private double ph;
154 
155     private double[] derivs;
156 
157     // CHECKSTYLE: resume JavadocVariable check
158 
159     /** Flag for resonant orbits. */
160     private boolean resonant;
161 
162     /** Flag for synchronous orbits. */
163     private boolean synchronous;
164 
165     /** Flag for compliance with Dundee modifications. */
166     private boolean isDundeeCompliant = true;
167 
168     /** Constructor for a unique initial TLE.
169      * @param initialTLE the TLE to propagate.
170      * @param attitudeProvider provider for attitude computation
171      * @param mass spacecraft mass (kg)
172      * @exception OrekitException if some specific error occurs
173      */
174     public DeepSDP4(final TLE initialTLE, final AttitudeProvider attitudeProvider,
175                        final double mass) throws OrekitException {
176         super(initialTLE, attitudeProvider, mass);
177     }
178 
179     /** Computes luni - solar terms from initial coordinates and epoch.
180      * @exception OrekitException when UTC time steps can't be read
181      */
182     protected void luniSolarTermsComputation() throws OrekitException {
183 
184         final double sing = FastMath.sin(tle.getPerigeeArgument());
185         final double cosg = FastMath.cos(tle.getPerigeeArgument());
186 
187         final double sinq = FastMath.sin(tle.getRaan());
188         final double cosq = FastMath.cos(tle.getRaan());
189         final double aqnv = 1.0 / a0dp;
190 
191         // Compute julian days since 1900
192         final double daysSince1900 =
193             (tle.getDate().durationFrom(AbsoluteDate.JULIAN_EPOCH) +
194              tle.getDate().timeScalesOffset(TimeScalesFactory.getUTC(), TimeScalesFactory.getTT())) / Constants.JULIAN_DAY - 2415020;
195 
196 
197         double cc = C1SS;
198         double ze = ZES;
199         double zn = ZNS;
200         double zsinh = sinq;
201         double zcosh = cosq;
202 
203         thgr = thetaG(tle.getDate());
204         xnq = xn0dp;
205         omegaq = tle.getPerigeeArgument();
206 
207         final double xnodce = 4.5236020 - 9.2422029e-4 * daysSince1900;
208         final double stem = FastMath.sin(xnodce);
209         final double ctem = FastMath.cos(xnodce);
210         final double c_minus_gam = 0.228027132 * daysSince1900 - 1.1151842;
211         final double gam = 5.8351514 + 0.0019443680 * daysSince1900;
212 
213         zcosil = 0.91375164 - 0.03568096 * ctem;
214         zsinil = FastMath.sqrt(1.0 - zcosil * zcosil);
215         zsinhl = 0.089683511 * stem / zsinil;
216         zcoshl = FastMath.sqrt(1.0 - zsinhl * zsinhl);
217         zmol = MathUtils.normalizeAngle(c_minus_gam, FastMath.PI);
218 
219         double zx = 0.39785416 * stem / zsinil;
220         final double zy = zcoshl * ctem + 0.91744867 * zsinhl * stem;
221         zx = FastMath.atan2( zx, zy) + gam - xnodce;
222         zcosgl = FastMath.cos( zx);
223         zsingl = FastMath.sin( zx);
224         zmos = MathUtils.normalizeAngle(6.2565837 + 0.017201977 * daysSince1900, FastMath.PI);
225 
226         // Do solar terms
227         savtsn = 1e20;
228 
229         double zcosi =  0.91744867;
230         double zsini =  0.39785416;
231         double zsing = -0.98088458;
232         double zcosg =  0.1945905;
233 
234         double se = 0;
235         double sgh = 0;
236         double sh = 0;
237         double si = 0;
238         double sl = 0;
239 
240         // There was previously some convoluted logic here, but it boils
241         // down to this:  we compute the solar terms,  then the lunar terms.
242         // On a second pass,  we recompute the solar terms, taking advantage
243         // of the improved data that resulted from computing lunar terms.
244         for (int iteration = 0; iteration < 2; ++iteration) {
245             final double a1 = zcosg * zcosh + zsing * zcosi * zsinh;
246             final double a3 = -zsing * zcosh + zcosg * zcosi * zsinh;
247             final double a7 = -zcosg * zsinh + zsing * zcosi * zcosh;
248             final double a8 = zsing * zsini;
249             final double a9 = zsing * zsinh + zcosg * zcosi * zcosh;
250             final double a10 = zcosg * zsini;
251             final double a2 = cosi0 * a7 + sini0 * a8;
252             final double a4 = cosi0 * a9 + sini0 * a10;
253             final double a5 = -sini0 * a7 + cosi0 * a8;
254             final double a6 = -sini0 * a9 + cosi0 * a10;
255             final double x1 = a1 * cosg + a2 * sing;
256             final double x2 = a3 * cosg + a4 * sing;
257             final double x3 = -a1 * sing + a2 * cosg;
258             final double x4 = -a3 * sing + a4 * cosg;
259             final double x5 = a5 * sing;
260             final double x6 = a6 * sing;
261             final double x7 = a5 * cosg;
262             final double x8 = a6 * cosg;
263             final double z31 = 12 * x1 * x1 - 3 * x3 * x3;
264             final double z32 = 24 * x1 * x2 - 6 * x3 * x4;
265             final double z33 = 12 * x2 * x2 - 3 * x4 * x4;
266             final double z11 = -6 * a1 * a5 + e0sq * (-24 * x1 * x7 - 6 * x3 * x5);
267             final double z12 = -6 * (a1 * a6 + a3 * a5) +
268                                e0sq * (-24 * (x2 * x7 + x1 * x8) - 6 * (x3 * x6 + x4 * x5));
269             final double z13 = -6 * a3 * a6 + e0sq * (-24 * x2 * x8 - 6 * x4 * x6);
270             final double z21 = 6 * a2 * a5 + e0sq * (24 * x1 * x5 - 6 * x3 * x7);
271             final double z22 = 6 * (a4 * a5 + a2 * a6) +
272                                e0sq * (24 * (x2 * x5 + x1 * x6) - 6 * (x4 * x7 + x3 * x8));
273             final double z23 = 6 * a4 * a6 + e0sq * (24 * x2 * x6 - 6 * x4 * x8);
274             final double s3 = cc / xnq;
275             final double s2 = -0.5 * s3 / beta0;
276             final double s4 = s3 * beta0;
277             final double s1 = -15 * tle.getE() * s4;
278             final double s5 = x1 * x3 + x2 * x4;
279             final double s6 = x2 * x3 + x1 * x4;
280             final double s7 = x2 * x4 - x1 * x3;
281             double z1 = 3 * (a1 * a1 + a2 * a2) + z31 * e0sq;
282             double z2 = 6 * (a1 * a3 + a2 * a4) + z32 * e0sq;
283             double z3 = 3 * (a3 * a3 + a4 * a4) + z33 * e0sq;
284 
285             z1 = z1 + z1 + beta02 * z31;
286             z2 = z2 + z2 + beta02 * z32;
287             z3 = z3 + z3 + beta02 * z33;
288             se = s1 * zn * s5;
289             si = s2 * zn * (z11 + z13);
290             sl = -zn * s3 * (z1 + z3 - 14 - 6 * e0sq);
291             sgh = s4 * zn * (z31 + z33 - 6);
292             if (tle.getI() < (FastMath.PI / 60.0)) {
293                 // inclination smaller than 3 degrees
294                 sh = 0;
295             } else {
296                 sh = -zn * s2 * (z21 + z23);
297             }
298             ee2  =  2 * s1 * s6;
299             e3   =  2 * s1 * s7;
300             xi2  =  2 * s2 * z12;
301             xi3  =  2 * s2 * (z13 - z11);
302             xl2  = -2 * s3 * z2;
303             xl3  = -2 * s3 * (z3 - z1);
304             xl4  = -2 * s3 * (-21 - 9 * e0sq) * ze;
305             xgh2 =  2 * s4 * z32;
306             xgh3 =  2 * s4 * (z33 - z31);
307             xgh4 = -18 * s4 * ze;
308             xh2  = -2 * s2 * z22;
309             xh3  = -2 * s2 * (z23 - z21);
310 
311             if (iteration == 0) { // we compute lunar terms only on the first pass:
312                 sse = se;
313                 ssi = si;
314                 ssl = sl;
315                 ssh = (tle.getI() < (FastMath.PI / 60.0)) ? 0 : sh / sini0;
316                 ssg = sgh - cosi0 * ssh;
317                 se2 = ee2;
318                 si2 = xi2;
319                 sl2 = xl2;
320                 sgh2 = xgh2;
321                 sh2 = xh2;
322                 se3 = e3;
323                 si3 = xi3;
324                 sl3 = xl3;
325                 sgh3 = xgh3;
326                 sh3 = xh3;
327                 sl4 = xl4;
328                 sgh4 = xgh4;
329                 zcosg = zcosgl;
330                 zsing = zsingl;
331                 zcosi = zcosil;
332                 zsini = zsinil;
333                 zcosh = zcoshl * cosq + zsinhl * sinq;
334                 zsinh = sinq * zcoshl - cosq * zsinhl;
335                 zn = ZNL;
336                 cc = C1L;
337                 ze = ZEL;
338             }
339         } // end of solar - lunar - solar terms computation
340 
341         sse += se;
342         ssi += si;
343         ssl += sl;
344         ssg += sgh - ((tle.getI() < (FastMath.PI / 60.0)) ? 0 : (cosi0 / sini0 * sh));
345         ssh += (tle.getI() < (FastMath.PI / 60.0)) ? 0 : sh / sini0;
346 
347 
348 
349         //        Start the resonant-synchronous tests and initialization
350 
351         double bfact = 0;
352 
353         // if mean motion is 1.893053 to 2.117652 revs/day, and eccentricity >= 0.5,
354         // start of the 12-hour orbit, e > 0.5 section
355         if ((xnq >= 0.00826) && (xnq <= 0.00924) && (tle.getE() >= 0.5)) {
356 
357             final double g201 = -0.306 - (tle.getE() - 0.64) * 0.440;
358             final double eoc = tle.getE() * e0sq;
359             final double sini2 = sini0 * sini0;
360             final double f220 = 0.75 * (1 + 2 * cosi0 + theta2);
361             final double f221 = 1.5 * sini2;
362             final double f321 =  1.875 * sini0 * (1 - 2 * cosi0 - 3 * theta2);
363             final double f322 = -1.875 * sini0 * (1 + 2 * cosi0 - 3 * theta2);
364             final double f441 = 35 * sini2 * f220;
365             final double f442 = 39.3750 * sini2 * sini2;
366             final double f522 = 9.84375 * sini0 * (sini2 * (1 - 2 * cosi0 - 5 * theta2) +
367                                                    0.33333333 * (-2 + 4 * cosi0 + 6 * theta2));
368             final double f523 = sini0 * (4.92187512 * sini2 * (-2 - 4 * cosi0 + 10 * theta2) +
369                                          6.56250012 * (1 + 2 * cosi0 - 3 * theta2));
370             final double f542 = 29.53125 * sini0 * (2 - 8 * cosi0 + theta2 * (-12 + 8 * cosi0 + 10 * theta2));
371             final double f543 = 29.53125 * sini0 * (-2 - 8 * cosi0 + theta2 * (12 + 8 * cosi0 - 10 * theta2));
372             final double g211;
373             final double g310;
374             final double g322;
375             final double g410;
376             final double g422;
377             final double g520;
378 
379             resonant = true;       // it is resonant...
380             synchronous = false;     // but it's not synchronous
381 
382             // Geopotential resonance initialization for 12 hour orbits :
383             if (tle.getE() <= 0.65) {
384                 g211 =    3.616  -   13.247  * tle.getE() +   16.290  * e0sq;
385                 g310 =  -19.302  +  117.390  * tle.getE() -  228.419  * e0sq +  156.591  * eoc;
386                 g322 =  -18.9068 +  109.7927 * tle.getE() -  214.6334 * e0sq +  146.5816 * eoc;
387                 g410 =  -41.122  +  242.694  * tle.getE() -  471.094  * e0sq +  313.953  * eoc;
388                 g422 = -146.407  +  841.880  * tle.getE() - 1629.014  * e0sq + 1083.435  * eoc;
389                 g520 = -532.114  + 3017.977  * tle.getE() - 5740.032  * e0sq + 3708.276  * eoc;
390             } else  {
391                 g211 =   -72.099 +   331.819 * tle.getE() -   508.738 * e0sq +   266.724 * eoc;
392                 g310 =  -346.844 +  1582.851 * tle.getE() -  2415.925 * e0sq +  1246.113 * eoc;
393                 g322 =  -342.585 +  1554.908 * tle.getE() -  2366.899 * e0sq +  1215.972 * eoc;
394                 g410 = -1052.797 +  4758.686 * tle.getE() -  7193.992 * e0sq +  3651.957 * eoc;
395                 g422 = -3581.69  + 16178.11  * tle.getE() - 24462.77  * e0sq + 12422.52  * eoc;
396                 if (tle.getE() <= 0.715) {
397                     g520 = 1464.74 - 4664.75 * tle.getE() + 3763.64 * e0sq;
398                 } else {
399                     g520 = -5149.66 + 29936.92 * tle.getE() - 54087.36 * e0sq + 31324.56 * eoc;
400                 }
401             }
402 
403             final double g533;
404             final double g521;
405             final double g532;
406             if (tle.getE() < 0.7) {
407                 g533 = -919.2277  + 4988.61   * tle.getE() - 9064.77   * e0sq + 5542.21  * eoc;
408                 g521 = -822.71072 + 4568.6173 * tle.getE() - 8491.4146 * e0sq + 5337.524 * eoc;
409                 g532 = -853.666   + 4690.25   * tle.getE() - 8624.77   * e0sq + 5341.4   * eoc;
410             } else {
411                 g533 = -37995.78  + 161616.52 * tle.getE() - 229838.2  * e0sq + 109377.94 * eoc;
412                 g521 = -51752.104 + 218913.95 * tle.getE() - 309468.16 * e0sq + 146349.42 * eoc;
413                 g532 = -40023.88  + 170470.89 * tle.getE() - 242699.48 * e0sq + 115605.82 * eoc;
414             }
415 
416             double temp1 = 3 * xnq * xnq * aqnv * aqnv;
417             double temp = temp1 * ROOT22;
418             d2201 = temp * f220 * g201;
419             d2211 = temp * f221 * g211;
420             temp1 *= aqnv;
421             temp = temp1 * ROOT32;
422             d3210 = temp * f321 * g310;
423             d3222 = temp * f322 * g322;
424             temp1 *= aqnv;
425             temp = 2 * temp1 * ROOT44;
426             d4410 = temp * f441 * g410;
427             d4422 = temp * f442 * g422;
428             temp1 *= aqnv;
429             temp = temp1 * ROOT52;
430             d5220 = temp * f522 * g520;
431             d5232 = temp * f523 * g532;
432             temp = 2 * temp1 * ROOT54;
433             d5421 = temp * f542 * g521;
434             d5433 = temp * f543 * g533;
435             xlamo = tle.getMeanAnomaly() + tle.getRaan() + tle.getRaan() - thgr - thgr;
436             bfact = xmdot + xnodot + xnodot - THDT - THDT;
437             bfact += ssl + ssh + ssh;
438         } else if ((xnq < 0.0052359877) && (xnq > 0.0034906585)) {
439             // if mean motion is .8 to 1.2 revs/day : (geosynch)
440 
441             final double cosio_plus_1 = 1.0 + cosi0;
442             final double g200 = 1 + e0sq * (-2.5 + 0.8125  * e0sq);
443             final double g300 = 1 + e0sq * (-6   + 6.60937 * e0sq);
444             final double f311 = 0.9375 * sini0 * sini0 * (1 + 3 * cosi0) - 0.75 * cosio_plus_1;
445             final double g310 = 1 + 2 * e0sq;
446             final double f220 = 0.75 * cosio_plus_1 * cosio_plus_1;
447             final double f330 = 2.5 * f220 * cosio_plus_1;
448 
449             resonant = true;
450             synchronous = true;
451 
452             // Synchronous resonance terms initialization
453             del1 = 3 * xnq * xnq * aqnv * aqnv;
454             del2 = 2 * del1 * f220 * g200 * Q22;
455             del3 = 3 * del1 * f330 * g300 * Q33 * aqnv;
456             del1 = del1 * f311 * g310 * Q31 * aqnv;
457             xlamo = tle.getMeanAnomaly() + tle.getRaan() + tle.getPerigeeArgument() - thgr;
458             bfact = xmdot + omgdot + xnodot - THDT;
459             bfact = bfact + ssl + ssg + ssh;
460         } else {
461             // it's neither a high-e 12-hours orbit nor a geosynchronous:
462             resonant = false;
463             synchronous = false;
464         }
465 
466         if (resonant) {
467             xfact = bfact - xnq;
468 
469             // Initialize integrator
470             xli   = xlamo;
471             xni   = xnq;
472             atime = 0;
473         }
474         derivs = new double[2];
475     }
476 
477     /** Computes secular terms from current coordinates and epoch.
478      * @param t offset from initial epoch (minutes)
479      */
480     protected void deepSecularEffects(final double t)  {
481 
482         xll    += ssl * t;
483         omgadf += ssg * t;
484         xnode  += ssh * t;
485         em      = tle.getE() + sse * t;
486         xinc    = tle.getI() + ssi * t;
487 
488         if (resonant) {
489             // If we're closer to t = 0 than to the currently-stored data
490             // from the previous call to this function,  then we're
491             // better off "restarting",  going back to the initial data.
492             // The Dundee code rigs things up to _always_ take 720-minute
493             // steps from epoch to end time,  except for the final step.
494             // Easiest way to arrange similar behavior in this code is
495             // just to always do a restart,  if we're in Dundee-compliant
496             // mode.
497             if (FastMath.abs(t) < FastMath.abs(t - atime) || isDundeeCompliant)  {
498                 // Epoch restart
499                 atime = 0;
500                 xni = xnq;
501                 xli = xlamo;
502             }
503             boolean lastIntegrationStep = false;
504             // if |step|>|step max| then do one step at step max
505             while (!lastIntegrationStep) {
506                 double delt = t - atime;
507                 if (delt > SECULAR_INTEGRATION_STEP) {
508                     delt = SECULAR_INTEGRATION_STEP;
509                 } else if (delt < -SECULAR_INTEGRATION_STEP) {
510                     delt = -SECULAR_INTEGRATION_STEP;
511                 } else {
512                     lastIntegrationStep = true;
513                 }
514 
515                 computeSecularDerivs();
516 
517                 final double xldot = xni + xfact;
518 
519                 double xlpow = 1.;
520                 xli += delt * xldot;
521                 xni += delt * derivs[0];
522                 double delt_factor = delt;
523                 xlpow *= xldot;
524                 derivs[1] *= xlpow;
525                 delt_factor *= delt / 2;
526                 xli += delt_factor * derivs[0];
527                 xni += delt_factor * derivs[1];
528                 atime += delt;
529             }
530             xn = xni;
531             final double temp = -xnode + thgr + t * THDT;
532             xll = xli + temp + (synchronous ? -omgadf : temp);
533         }
534     }
535 
536     /** Computes periodic terms from current coordinates and epoch.
537      * @param t offset from initial epoch (min)
538      */
539     protected void deepPeriodicEffects(final double t)  {
540 
541         // If the time didn't change by more than 30 minutes,
542         // there's no good reason to recompute the perturbations;
543         // they don't change enough over so short a time span.
544         // However,  the Dundee code _always_ recomputes,  so if
545         // we're attempting to replicate its results,  we've gotta
546         // recompute everything,  too.
547         if ((FastMath.abs(savtsn - t) >= 30.0) || isDundeeCompliant)  {
548 
549             savtsn = t;
550 
551             // Update solar perturbations for time T
552             double zm = zmos + ZNS * t;
553             double zf = zm + 2 * ZES * FastMath.sin(zm);
554             double sinzf = FastMath.sin(zf);
555             double f2 = 0.5 * sinzf * sinzf - 0.25;
556             double f3 = -0.5 * sinzf * FastMath.cos(zf);
557             final double ses = se2 * f2 + se3 * f3;
558             final double sis = si2 * f2 + si3 * f3;
559             final double sls = sl2 * f2 + sl3 * f3 + sl4 * sinzf;
560             final double sghs = sgh2 * f2 + sgh3 * f3 + sgh4 * sinzf;
561             final double shs = sh2 * f2 + sh3 * f3;
562 
563             // Update lunar perturbations for time T
564             zm = zmol + ZNL * t;
565             zf = zm + 2 * ZEL * FastMath.sin(zm);
566             sinzf = FastMath.sin(zf);
567             f2 =  0.5 * sinzf * sinzf - 0.25;
568             f3 = -0.5 * sinzf * FastMath.cos(zf);
569             final double sel = ee2 * f2 + e3 * f3;
570             final double sil = xi2 * f2 + xi3 * f3;
571             final double sll = xl2 * f2 + xl3 * f3 + xl4 * sinzf;
572             final double sghl = xgh2 * f2 + xgh3 * f3 + xgh4 * sinzf;
573             final double sh1 = xh2 * f2 + xh3 * f3;
574 
575             // Sum the solar and lunar contributions
576             pe   = ses  + sel;
577             pinc = sis  + sil;
578             pl   = sls  + sll;
579             pgh  = sghs + sghl;
580             ph   = shs  + sh1;
581         }
582 
583         xinc += pinc;
584 
585         final double sinis = FastMath.sin( xinc);
586         final double cosis = FastMath.cos( xinc);
587 
588         /* Add solar/lunar perturbation correction to eccentricity: */
589         em     += pe;
590         xll    += pl;
591         omgadf += pgh;
592         xinc    = MathUtils.normalizeAngle(xinc, 0);
593 
594         if (FastMath.abs(xinc) >= 0.2) {
595             // Apply periodics directly
596             final double temp_val = ph / sinis;
597             omgadf -= cosis * temp_val;
598             xnode += temp_val;
599         } else {
600             // Apply periodics with Lyddane modification
601             final double sinok = FastMath.sin(xnode);
602             final double cosok = FastMath.cos(xnode);
603             final double alfdp =  ph * cosok + (pinc * cosis + sinis) * sinok;
604             final double betdp = -ph * sinok + (pinc * cosis + sinis) * cosok;
605             final double delta_xnode = MathUtils.normalizeAngle(FastMath.atan2(alfdp, betdp) - xnode, 0);
606             final double dls = -xnode * sinis * pinc;
607             omgadf += dls - cosis * delta_xnode;
608             xnode  += delta_xnode;
609         }
610     }
611 
612     /** Computes internal secular derivs. */
613     private void computeSecularDerivs() {
614 
615         final double sin_li = FastMath.sin(xli);
616         final double cos_li = FastMath.cos(xli);
617         final double sin_2li = 2. * sin_li * cos_li;
618         final double cos_2li = 2. * cos_li * cos_li - 1.;
619 
620         // Dot terms calculated :
621         if (synchronous)  {
622             final double sin_3li = sin_2li * cos_li + cos_2li * sin_li;
623             final double cos_3li = cos_2li * cos_li - sin_2li * sin_li;
624             final double term1a = del1 * (sin_li  * C_FASX2  - cos_li  * S_FASX2);
625             final double term2a = del2 * (sin_2li * C_2FASX4 - cos_2li * S_2FASX4);
626             final double term3a = del3 * (sin_3li * C_3FASX6 - cos_3li * S_3FASX6);
627             final double term1b = del1 * (cos_li  * C_FASX2  + sin_li  * S_FASX2);
628             final double term2b = 2.0 * del2 * (cos_2li * C_2FASX4 + sin_2li * S_2FASX4);
629             final double term3b = 3.0 * del3 * (cos_3li * C_3FASX6 + sin_3li * S_3FASX6);
630             derivs[0] = term1a + term2a + term3a;
631             derivs[1] = term1b + term2b + term3b;
632         } else {
633             // orbit is a 12-hour resonant one
634             final double xomi = omegaq + omgdot * atime;
635             final double sin_omi = FastMath.sin(xomi);
636             final double cos_omi = FastMath.cos(xomi);
637             final double sin_li_m_omi = sin_li * cos_omi - sin_omi * cos_li;
638             final double sin_li_p_omi = sin_li * cos_omi + sin_omi * cos_li;
639             final double cos_li_m_omi = cos_li * cos_omi + sin_omi * sin_li;
640             final double cos_li_p_omi = cos_li * cos_omi - sin_omi * sin_li;
641             final double sin_2omi = 2. * sin_omi * cos_omi;
642             final double cos_2omi = 2. * cos_omi * cos_omi - 1.;
643             final double sin_2li_m_omi = sin_2li * cos_omi - sin_omi * cos_2li;
644             final double sin_2li_p_omi = sin_2li * cos_omi + sin_omi * cos_2li;
645             final double cos_2li_m_omi = cos_2li * cos_omi + sin_omi * sin_2li;
646             final double cos_2li_p_omi = cos_2li * cos_omi - sin_omi * sin_2li;
647             final double sin_2li_p_2omi = sin_2li * cos_2omi + sin_2omi * cos_2li;
648             final double cos_2li_p_2omi = cos_2li * cos_2omi - sin_2omi * sin_2li;
649             final double sin_2omi_p_li = sin_li * cos_2omi + sin_2omi * cos_li;
650             final double cos_2omi_p_li = cos_li * cos_2omi - sin_2omi * sin_li;
651             final double term1a = d2201 * (sin_2omi_p_li * C_G22 - cos_2omi_p_li * S_G22) +
652                                   d2211 * (sin_li * C_G22 - cos_li * S_G22) +
653                                   d3210 * (sin_li_p_omi * C_G32 - cos_li_p_omi * S_G32) +
654                                   d3222 * (sin_li_m_omi * C_G32 - cos_li_m_omi * S_G32) +
655                                   d5220 * (sin_li_p_omi * C_G52 - cos_li_p_omi * S_G52) +
656                                   d5232 * (sin_li_m_omi * C_G52 - cos_li_m_omi * S_G52);
657             final double term2a = d4410 * (sin_2li_p_2omi * C_G44 - cos_2li_p_2omi * S_G44) +
658                                   d4422 * (sin_2li * C_G44 - cos_2li * S_G44) +
659                                   d5421 * (sin_2li_p_omi * C_G54 - cos_2li_p_omi * S_G54) +
660                                   d5433 * (sin_2li_m_omi * C_G54 - cos_2li_m_omi * S_G54);
661             final double term1b = d2201 * (cos_2omi_p_li * C_G22 + sin_2omi_p_li * S_G22) +
662                                   d2211 * (cos_li * C_G22 + sin_li * S_G22) +
663                                   d3210 * (cos_li_p_omi * C_G32 + sin_li_p_omi * S_G32) +
664                                   d3222 * (cos_li_m_omi * C_G32 + sin_li_m_omi * S_G32) +
665                                   d5220 * (cos_li_p_omi * C_G52 + sin_li_p_omi * S_G52) +
666                                   d5232 * (cos_li_m_omi * C_G52 + sin_li_m_omi * S_G52);
667             final double term2b = 2.0 * (d4410 * (cos_2li_p_2omi * C_G44 + sin_2li_p_2omi * S_G44) +
668                                          d4422 * (cos_2li * C_G44 + sin_2li * S_G44) +
669                                          d5421 * (cos_2li_p_omi * C_G54 + sin_2li_p_omi * S_G54) +
670                                          d5433 * (cos_2li_m_omi * C_G54 + sin_2li_m_omi * S_G54));
671 
672             derivs[0] = term1a + term2a;
673             derivs[1] = term1b + term2b;
674 
675         }
676     }
677 
678 }