aboutsummaryrefslogtreecommitdiffstats
path: root/Helpers/resonance_helpers.py
blob: e05ed052edf455119477aafbd609f4593be14ebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
"""
Helpers for Solar System Resonances.

*** Quick Ref -- Secular Resonances, Linear Theory, Two Massive Bodies ***
- g_{1,2}: JS Perigree (omega) precession, eccentricity pumping.
- f_{1,2}: JS Line of nodes (Omega) precession, inclination pumping.
"""

import numpy as np
import constants as C
import potential_helpers as ph


def laplace_coefficient(s, j, alpha):
    """
    Compute Laplace Coefficoents beta_s^j(alpha) from Series Expansion.
    
    Cf. Murray & Dermott (1999), Eq. (6.67), Page 237
        http://adsabs.harvard.edu/abs/1999ssd..book.....M
    Cf. Mardling (2013), App C., Fig. C1
        http://arxiv.org/pdf/1308.0607.pdf
    Cf. Predictability, Stability, and Chaos in N-Body Dynamical Systems,
        Annex A
        https://books.google.ch/books?id=zV_TBwAAQBAJ

    NB: Series convergence is guaranteed for alpha < 1.

    See Murray & Dermott (1999), Eq. (7.33), Page 280 for Test Case.

    @param: s - Half Integer (1/2, 3/2, ...)
    @param: j - Integer (1, 2, 3, ...)
    @param: alpha - Semi-Major Axis Ratio (alpha < 1)
    @return: beta_s^j(alpha) - [Float]
    """
    
    # Leading Term
    if j == 0:
        ss = 1.0
    else:
        ss = s
        for jj in range(1,j):
            ss *= (s+jj)    
    term_01 = ss / np.math.factorial(j) * alpha**j
    
    # Cast
    j = float(j)
    
    # Terms in Square Brackets
    term_02 = s * ( s + j ) / \
                  ( j + 1.0 ) * alpha**2.0 
    
    term_03 = s * ( s + 1.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) \
                  / \
                  ( 2.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) ) * alpha**4.0

    term_04 = s * ( s + 1.0 ) * \
                ( s + 2.0 ) * \
                ( s + j ) * \
                ( s + j + 1.0 ) * \
                ( s + j + 2.0 ) \
                / \
                ( 2.0 * \
                  3.0 * \
                  ( j + 1.0 ) * \
                  ( j + 2.0 ) * \
                  ( j + 3.0 ) ) * alpha**6.0
        
    term_05 = s * ( s + 1.0 ) * \
                  ( s + 2.0 ) * \
                  ( s + 3.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) * \
                  ( s + j + 2.0 ) * \
                  ( s + j + 3.0 ) \
                  / \
                  ( 2.0 * \
                    3.0 * \
                    4.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) * \
                    ( j + 3.0 ) * \
                    ( j + 4.0 ) ) * alpha**8.0
        
    term_06 = s * ( s + 1.0 ) * \
                  ( s + 2.0 ) * \
                  ( s + 3.0 ) * \
                  ( s + 4.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) * \
                  ( s + j + 2.0 ) * \
                  ( s + j + 3.0 ) * \
                  ( s + j + 4.0 ) \
                  / \
                  ( \
                    2.0 * \
                    3.0 * \
                    4.0 * \
                    5.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) * \
                    ( j + 3.0 ) * \
                    ( j + 4.0 ) * \
                    ( j + 5.0 ) ) * alpha**10.0
        
    term_07 = s * ( s + 1.0 ) * \
                  ( s + 2.0 ) * \
                  ( s + 3.0 ) * \
                  ( s + 4.0 ) * \
                  ( s + 5.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) * \
                  ( s + j + 2.0 ) * \
                  ( s + j + 3.0 ) * \
                  ( s + j + 4.0 ) * \
                  ( s + j + 5.0 ) \
                  / \
                  ( \
                    2.0 * \
                    3.0 * \
                    4.0 * \
                    5.0 * \
                    6.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) * \
                    ( j + 3.0 ) * \
                    ( j + 4.0 ) * \
                    ( j + 5.0 ) * \
                    ( j + 6.0 ) ) * alpha**12.0
    
    term_08 = s * ( s + 1.0 ) * \
                  ( s + 2.0 ) * \
                  ( s + 3.0 ) * \
                  ( s + 4.0 ) * \
                  ( s + 5.0 ) * \
                  ( s + 6.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) * \
                  ( s + j + 2.0 ) * \
                  ( s + j + 3.0 ) * \
                  ( s + j + 4.0 ) * \
                  ( s + j + 5.0 ) * \
                  ( s + j + 6.0 ) \
                  / \
                  ( \
                    2.0 * \
                    3.0 * \
                    4.0 * \
                    5.0 * \
                    6.0 * \
                    7.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) * \
                    ( j + 3.0 ) * \
                    ( j + 4.0 ) * \
                    ( j + 5.0 ) * \
                    ( j + 6.0 ) * \
                    ( j + 7.0 ) ) * alpha**14.0
        
    term_09 = s * ( s + 1.0 ) * \
                  ( s + 2.0 ) * \
                  ( s + 3.0 ) * \
                  ( s + 4.0 ) * \
                  ( s + 5.0 ) * \
                  ( s + 6.0 ) * \
                  ( s + 7.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) * \
                  ( s + j + 2.0 ) * \
                  ( s + j + 3.0 ) * \
                  ( s + j + 4.0 ) * \
                  ( s + j + 5.0 ) * \
                  ( s + j + 6.0 ) * \
                  ( s + j + 7.0 ) \
                  / \
                  ( \
                    2.0 * \
                    3.0 * \
                    4.0 * \
                    5.0 * \
                    6.0 * \
                    7.0 * \
                    8.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) * \
                    ( j + 3.0 ) * \
                    ( j + 4.0 ) * \
                    ( j + 5.0 ) * \
                    ( j + 6.0 ) * \
                    ( j + 7.0 ) * \
                    ( j + 8.0 ) ) * alpha**16.0
        
    term_10 = s * ( s + 1.0 ) * \
                  ( s + 2.0 ) * \
                  ( s + 3.0 ) * \
                  ( s + 4.0 ) * \
                  ( s + 5.0 ) * \
                  ( s + 6.0 ) * \
                  ( s + 7.0 ) * \
                  ( s + 8.0 ) * \
                  ( s + j ) * \
                  ( s + j + 1.0 ) * \
                  ( s + j + 2.0 ) * \
                  ( s + j + 3.0 ) * \
                  ( s + j + 4.0 ) * \
                  ( s + j + 5.0 ) * \
                  ( s + j + 6.0 ) * \
                  ( s + j + 7.0 ) * \
                  ( s + j + 8.0 ) \
                  / \
                  ( \
                    2.0 * \
                    3.0 * \
                    4.0 * \
                    5.0 * \
                    6.0 * \
                    7.0 * \
                    8.0 * \
                    9.0 * \
                    ( j + 1.0 ) * \
                    ( j + 2.0 ) * \
                    ( j + 3.0 ) * \
                    ( j + 4.0 ) * \
                    ( j + 5.0 ) * \
                    ( j + 6.0 ) * \
                    ( j + 7.0 ) * \
                    ( j + 8.0 ) * \
                    ( j + 9.0 ) ) * alpha**18.0

    # Laplace Coefficient
    beta = term_01 * ( 1.0 + term_02 + term_03 + term_04 + \
                             term_05 + term_06 + term_07 + \
                             term_08 + term_09 + term_10 )
    beta *= 2.0
    
    # Return
    return beta


def secular_frequencies_planets(a_1, a_2, m_1, m_2):
    """
    Compute secular frequencies g_{1,2} and f_{1,2} for a two planet system.

    g_{1,2}: Perigree (omega) precession frequencies, eccentricity pumping.
    f_{1,2}: Line of nodes (Omega) precession frequencies, inclination pumping.

    *** !!! ***
    This is linear theory. Higher order expansions give (a,e,i) dependence.
    *** !!! ***

    Cf. Murray & Dermott (1999), Sect. 7.3, Pages 270, 280, 281
        http://adsabs.harvard.edu/abs/1999ssd..book.....M
    Cf. Nagasawa (2000), Sect. 2.1, Appendix
        http://adsabs.harvard.edu/abs/2000AJ....119.1480N

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @returns: g_1, g_2 - Perigee Precession Frequencies (Rad/Twopi)
    @returns: f_1, f_2 - Line of Nodes Precession Frequencies (Rad/Twopi)
    """

    # Solar System Units
    G = 1.0; M = 1.0

    # Mean Motion
    n_1 = np.sqrt(G * (M + m_1) / a_1**3.0)
    n_2 = np.sqrt(G * (M + m_2) / a_2**3.0)

    # Debug
    # print "n_j = %.6f deg/yr" % (n_j * C.r2d * C.twopi)
    # print "n_s = %.6f deg/yr" % (n_s * C.r2d * C.twopi)

    # What are these called?
    P_12 = a_1 / 8.0 / a_2**2.0 * laplace_coefficient(1.5, 2, a_1 / a_2)
    N_12 = a_1 / 8.0 / a_2**2.0 * laplace_coefficient(1.5, 1, a_1 / a_2)

    # Matrix Elements
    A_11 = 2.0 / n_1 / a_1**2.0 * ( G * m_2 * N_12 )
    A_22 = 2.0 / n_2 / a_2**2.0 * ( G * m_1 * N_12 )
    A_12 = - 2.0 * G * P_12 / a_1 / a_2 * np.sqrt( m_1 * m_2 / n_1 / n_2 )

    B_11 = - 2.0 / n_1 / a_1**2.0 * ( G * m_2 * N_12 )
    B_22 = - 2.0 / n_2 / a_2**2.0 * ( G * m_1 * N_12 )
    B_12 = - 2.0 * G * N_12 / a_1 / a_2 * np.sqrt( m_1 * m_2 / n_1 / n_2 )

    # Frequencies (Rad/Twopi) = Eigenvalues of Matrices A and B
    # Converstion: Multiply (* C.r2d * 3600.0 * C.twopi) [Arcsec/Year]
    g_1 = 0.5 * (A_11 + A_22 - np.sqrt( (A_11 - A_22)**2.0 + 4.0 * A_12**2.0 ))
    g_2 = 0.5 * (A_11 + A_22 + np.sqrt( (A_11 - A_22)**2.0 + 4.0 * A_12**2.0 ))
    f_1 = 0.5 * (B_11 + B_22 + np.sqrt( (B_11 - B_22)**2.0 + 4.0 * B_12**2.0 ))
    f_2 = 0.5 * (B_11 + B_22 - np.sqrt( (B_11 - B_22)**2.0 + 4.0 * B_12**2.0 ))

    # Return Frequencies
    return g_1, g_2, f_1, f_2


def secular_frequencies_planets_gas(a_1, a_2, m_1, m_2, t_over_tau):
    """
    Compute secular frequencies g_{1,2} and f_{1,2} for a two planet system.

    g_{1,2}: Perigree (omega) precession frequencies, eccentricity pumping.
    f_{1,2}: Line of nodes (Omega) precession frequencies, inclination pumping.

    *** !!! ***
    This is linear theory. Higher order expansions give (a,e,i) dependence.
    *** !!! ***

    Cf. Murray & Dermott (1999), Sect. 7.3, Pages 270, 280, 281
        http://adsabs.harvard.edu/abs/1999ssd..book.....M
    Cf. Nagasawa (2000), Sect. 2.1, Appendix
        http://adsabs.harvard.edu/abs/2000AJ....119.1480N

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @param: t_over_tau - Exponential Decay Factor for Gas Density (-)
    @returns: g_1, g_2 - Perigee Precession Rate (Rad/Second)
    @returns: f_1, f_2 - Line of Nodes Precession Rate (Rad/Second)
    """

    # Gas Terms (Internal CGS Conversion - Needs AU!)
    S_1, T_1 = ph.s_and_t(a_1, t_over_tau)
    S_2, T_2 = ph.s_and_t(a_2, t_over_tau)

    # CGS 
    G = C.G_cgs; Msun = C.msun * 1000.0
    m_1 *= Msun
    m_2 *= Msun
    a_1 *= C.au2km * 1000.0 * 100.0
    a_2 *= C.au2km * 1000.0 * 100.0

    # What are these called?
    P_12 = a_1 / 8.0 / a_2**2.0 * laplace_coefficient(1.5, 2, a_1 / a_2)
    N_12 = a_1 / 8.0 / a_2**2.0 * laplace_coefficient(1.5, 1, a_1 / a_2)

    # Mean Motion
    n_1 = np.sqrt(G * (Msun + m_1) / a_1**3.0)
    n_2 = np.sqrt(G * (Msun + m_2) / a_2**3.0)

    # Matrix Elements
    A_11 = 2.0 / n_1 / a_1**2.0 * ( G * m_2 * N_12 + T_1 )
    A_22 = 2.0 / n_2 / a_2**2.0 * ( G * m_1 * N_12 + T_2 )
    A_12 = - 2.0 * G * P_12 / a_1 / a_2 * np.sqrt( m_1 * m_2 / n_1 / n_2 )

    B_11 = - 2.0 / n_1 / a_1**2.0 * ( G * m_2 * N_12 + S_1 )
    B_22 = - 2.0 / n_2 / a_2**2.0 * ( G * m_1 * N_12 + S_2 )
    B_12 = - 2.0 * G * N_12 / a_1 / a_2 * np.sqrt( m_1 * m_2 / n_1 / n_2 )

    # Frequencies (Rad/Second) = Eigenvalues of Matrices A and B
    # Conversion: Multiply (C.r2d*3600.0) * (3600.0*365.25*24.0) [Arcsec/Year]
    g_1 = 0.5 * (A_11 + A_22 - np.sqrt( (A_11 - A_22)**2.0 + 4.0 * A_12**2.0 ))
    g_2 = 0.5 * (A_11 + A_22 + np.sqrt( (A_11 - A_22)**2.0 + 4.0 * A_12**2.0 ))
    f_1 = 0.5 * (B_11 + B_22 + np.sqrt( (B_11 - B_22)**2.0 + 4.0 * B_12**2.0 ))
    f_2 = 0.5 * (B_11 + B_22 - np.sqrt( (B_11 - B_22)**2.0 + 4.0 * B_12**2.0 ))

    # Return Frequencies
    return g_1, g_2, f_1, f_2


def secular_frequencies_js_today():
    """
    Compute Present Day Jupiter/Saturn System Frequencies. Test Case.

    @returns: g_1, g_2 - Perigee Precession Frequencies (Rad/Twopi)
    @returns: f_1, f_2 - Line of Nodes Precession Frequencies (Rad/Twopi)
    """

    # Semi-Major Axes
    a_j = 5.202545
    a_s = 9.554841

    # Masses
    m_j = 9.54786e-4
    m_s = 2.85837e-4

    # Compute
    g_1, g_2, f_1, f_2 = secular_frequencies_planets(a_j, a_s, m_j, m_s)

    # Debug
    # For the 9-th order series expansion of the Laplace coefficients, we
    # should get the following frequencies
    # g_1 = 9.637822 10^-4 deg/yr
    # g_2 = 6.100148 10^-3 deg/yr
    # f_1 = 0.000000
    # f_2 = -7.063930 10^-3 deg/yr
    # print "g_1 = %.6f 10^-4 deg/yr" % (g_1 * C.r2d * C.twopi * 1.0e4)
    # print "g_2 = %.6f 10^-3 deg/yr" % (g_2 * C.r2d * C.twopi * 1.0e3)
    # print "f_1 = %.6f" % (f_1 * C.r2d * C.twopi)
    # print "f_2 = %.6f 10^-3 deg/yr" % (f_2 * C.r2d * C.twopi * 1.0e3)

    # Return Frequencies
    return g_1, g_2, f_1, f_2


def secular_frequencies_planets_and_asteroid(a_1, a_2, m_1, m_2, a_ast):
    """
    Compute secular frequencies g and f for an asteroid (massless test
    particles) in the presence of two massive planets.

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @param: a_ast - Asteroid Semi-Major Axis (AU)
    @returns: g - Asteroid Perigee Precession Frequency (Rad/Twopi)
    @returns: f - Asteroid Line of Nodes Precession Frequencies (Rad/Twopi)
    """

    # Solar System Units
    G = M = GM = 1.0

    # Mean Motion
    n_ast = np.sqrt(GM/a_ast**3.0)

    # What are these called?
    N_1 = a_ast / 8.0 / a_1**2.0 * laplace_coefficient(1.5, 1, a_ast / a_1)
    N_2 = a_ast / 8.0 / a_2**2.0 * laplace_coefficient(1.5, 1, a_ast / a_2)

    # Frequencies (Rad/Twopi)
    # Converstion: Multiply (* C.r2d * 3600.0 * C.twopi) [Arcsec/Year]
    g = 2.0 * G / n_ast / a_ast**2.0 * ( m_1 * N_1 + m_2 * N_2 )
    # f = - 2.0 * G / n_ast / a_ast**2.0 * ( m_1 * N_1 + m_2 * N_2 )
    f = -g

    # Return Frequencies 
    return g, f


def secular_frequencies_planets_and_asteroid_gas(a_1, a_2, m_1, m_2, \
                                                 a_ast, t_over_tau):
    """
    Compute secular frequencies g and f for an asteroid (massless test
    particles) in the presence of two massive planets.

    Be careful if you request too many (>~10) samples for a_ast. The code
    runs a for-loop (in Fortran) to integrate the disk potential for each
    value of a_ast, which can take a long time if you request many samples.

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @param: a_ast - Asteroid Semi-Major Axis (AU)
    @param: t_over_tau - Exponential Decay Factor for Gas Density (-)
    @returns: g - Asteroid Perigee Precession Rate (Rad/Second)
    @returns: f - Asteroid Line of Nodes Precession Frequencies (Rad/Second)
    """

    # Gas Terms (Internal CGS Conversion - Needs AU!)
    if np.isscalar(a_ast):
        S_ast, T_ast = ph.s_and_t(a_ast, t_over_tau)
    else:
        S_ast = np.zeros_like(a_ast) * np.nan
        T_ast = np.zeros_like(a_ast) * np.nan
        for ia, a_ast_loc in enumerate(a_ast):
            S_ast[ia], T_ast[ia] = ph.s_and_t(a_ast_loc, t_over_tau)

    # CGS 
    G = C.G_cgs; Msun = C.msun * 1000.0
    m_1 *= Msun
    m_2 *= Msun
    a_1 *= C.au2km * 1000.0 * 100.0
    a_2 *= C.au2km * 1000.0 * 100.0
    a_ast *= C.au2km * 1000.0 * 100.0

    # Mean Motion
    n_ast = np.sqrt(G*Msun/a_ast**3.0)

    # What are these called?
    N_1 = a_ast / 8.0 / a_1**2.0 * laplace_coefficient(1.5, 1, a_ast / a_1)
    N_2 = a_ast / 8.0 / a_2**2.0 * laplace_coefficient(1.5, 1, a_ast / a_2)

    # Frequencies (Rad/s)
    # Conversion: Multiply (C.r2d*3600.0) * (3600.0*365.25*24.0) [Arcsec/Year]
    g = 2.0 * G / n_ast / a_ast**2.0 * ( m_1 * N_1 + m_2 * N_2 ) + \
        2.0 / n_ast / a_ast**2.0 * T_ast
    f = - 2.0 * G / n_ast / a_ast**2.0 * ( m_1 * N_1 + m_2 * N_2 ) - \
        2.0 / n_ast / a_ast**2.0 * S_ast

    # Return Frequencies 
    return g, f


def secular_resonance_location(a_1, a_2, m_1, m_2):
    """
    Compute Location of Secular Resonances.

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @returns: a_nu_5, a_nu_6 - Perigee (omega) Precession Res. (AU)
    @returns: a_nu_15, a_nu_16 - Line of Nodes (Omega) Precession Res. (AU)
    """

    # Scanning Range, Semi-Major Axis
    a_ast = np.mgrid[0.1:5.0:0.01]

    # Compute Planetary Frequencies
    g_1, g_2, f_1, f_2 = secular_frequencies_planets(a_1, a_2, m_1, m_2)

    # Compute Asteroid (Massless Test Particle) Frequencies
    g, f = secular_frequencies_planets_and_asteroid(a_1, a_2, m_1, m_2, a_ast)

    # Locate
    arg_nu_5 = np.argwhere(np.diff(np.sign(g-g_1))**2.0 > 1.0e-16).squeeze()
    arg_nu_6 = np.argwhere(np.diff(np.sign(g-g_2))**2.0 > 1.0e-16).squeeze()
    arg_nu_15 = np.argwhere(np.diff(np.sign(f-f_1))**2.0 > 1.0e-16).squeeze()
    arg_nu_16 = np.argwhere(np.diff(np.sign(f-f_2))**2.0 > 1.0e-16).squeeze()

    a_nu_5  = a_ast[arg_nu_5]
    a_nu_6  = a_ast[arg_nu_6]
    a_nu_15  = a_ast[arg_nu_15]
    a_nu_16  = a_ast[arg_nu_16]

    # Write NaN for Non-Existing Resonances
    if (not np.isscalar(a_nu_5)) and len(a_nu_5) == 0:
        a_nu_5 = np.nan
    if (not np.isscalar(a_nu_6)) and len(a_nu_6) == 0:
        a_nu_6 = np.nan
    if (not np.isscalar(a_nu_15)) and len(a_nu_15) == 0:
        a_nu_15 = np.nan
    if (not np.isscalar(a_nu_16)) and len(a_nu_16) == 0:
        a_nu_16 = np.nan

    # Return
    return a_nu_5, a_nu_6, a_nu_15, a_nu_16


def secular_resonance_location_gas(a_1, a_2, m_1, m_2, t_over_tau):
    """
    Compute Location of Secular Resonances.

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @param: t_over_tau - Exponential Decay Factor for Gas Density (-)
    @returns: a_nu_5, a_nu_6 - Perigee (omega) Precession Res. (AU)
    @returns: a_nu_15, a_nu_16 - Line of Nodes (Omega) Precession Res. (AU)
    """

    # Compute Planetary Frequencies
    g_1, g_2, f_1, f_2 = \
        secular_frequencies_planets_gas(a_1, a_2, m_1, m_2, t_over_tau)

    #
    # #########################################################################
    #
    # Coarse Grid
    #
    # #########################################################################
    #

    # Grid at 0.1 AU Resolution
    a_coarse = np.mgrid[0.1:5.0:0.1]

    # Compute Asteroid (Massless Test Particle) Frequencies
    g = np.zeros_like(a_coarse)
    f = np.zeros_like(a_coarse)
    for ii, a_loc in enumerate(a_coarse):
        g[ii], f[ii] = \
            secular_frequencies_planets_and_asteroid_gas(a_1, a_2, m_1, m_2, \
                                                         a_loc.copy(), \
                                                         t_over_tau)

    # Locate
    arg_nu_5 = np.argwhere(np.diff(np.sign(g-g_1))**2.0 > 1.0e-16).squeeze()
    arg_nu_6 = np.argwhere(np.diff(np.sign(g-g_2))**2.0 > 1.0e-16).squeeze()
    arg_nu_15 = np.argwhere(np.diff(np.sign(f-f_1))**2.0 > 1.0e-16).squeeze()
    arg_nu_16 = np.argwhere(np.diff(np.sign(f-f_2))**2.0 > 1.0e-16).squeeze()

    a_nu_5  = a_coarse[arg_nu_5]
    a_nu_6  = a_coarse[arg_nu_6]
    a_nu_15  = a_coarse[arg_nu_15]
    a_nu_16  = a_coarse[arg_nu_16]

    # Write NaN for Non-Existing Resonances
    if (not np.isscalar(a_nu_5)) and len(a_nu_5) == 0:
        a_nu_5 = np.nan
    if (not np.isscalar(a_nu_6)) and len(a_nu_6) == 0:
        a_nu_6 = np.nan
    if (not np.isscalar(a_nu_15)) and len(a_nu_15) == 0:
        a_nu_15 = np.nan
    if (not np.isscalar(a_nu_16)) and len(a_nu_16) == 0:
        a_nu_16 = np.nan

    #
    # #########################################################################
    #
    # Refined Grid
    #
    # #########################################################################
    #

    # Fine Grid @ 0.01 AU Resolution
    a_fine = np.array([])

    if not np.isnan(a_nu_5):
        a_fine = \
            np.concatenate([a_fine, np.mgrid[a_nu_5-0.1:a_nu_5+0.11:0.01]])

    if not np.isnan(a_nu_6):
        a_fine = \
            np.concatenate([a_fine, np.mgrid[a_nu_6-0.1:a_nu_6+0.11:0.01]])

    if not np.isnan(a_nu_15):
        a_fine = \
            np.concatenate([a_fine, np.mgrid[a_nu_15-0.1:a_nu_15+0.11:0.01]])

    # Can occur twice!
    if np.isscalar(a_nu_16):
        if not np.isnan(a_nu_16):
            a_fine = \
                np.concatenate([a_fine, \
                                np.mgrid[a_nu_16-0.11:a_nu_16+0.11:0.01]])
    else:
        for a_nu_16_loc in a_nu_16:
            a_fine = \
                np.concatenate([a_fine, \
                                np.mgrid[a_nu_16_loc - 0.11:\
                                         a_nu_16_loc + 0.11:\
                                         0.01]])

    # Reorder
    a_fine = np.sort(a_fine)

    # Inner Cutoff
    a_fine = a_fine[a_fine>0.1]

    # Compute Asteroid (Massless Test Particle) Frequencies
    g = np.zeros_like(a_fine)
    f = np.zeros_like(a_fine)
    for ii, a_loc in enumerate(a_fine):
        g[ii], f[ii] = \
            secular_frequencies_planets_and_asteroid_gas(a_1, a_2, m_1, m_2, \
                                                         a_loc.copy(), \
                                                         t_over_tau)

    # Locate
    arg_nu_5 = np.argwhere(np.diff(np.sign(g-g_1))**2.0 > 1.0e-16).squeeze()
    arg_nu_6 = np.argwhere(np.diff(np.sign(g-g_2))**2.0 > 1.0e-16).squeeze()
    arg_nu_15 = np.argwhere(np.diff(np.sign(f-f_1))**2.0 > 1.0e-16).squeeze()
    arg_nu_16 = np.argwhere(np.diff(np.sign(f-f_2))**2.0 > 1.0e-16).squeeze()

    a_nu_5  = a_fine[arg_nu_5]
    a_nu_6  = a_fine[arg_nu_6]
    a_nu_15  = a_fine[arg_nu_15]
    a_nu_16  = a_fine[arg_nu_16]

    # Write NaN for Non-Existing Resonances
    if (not np.isscalar(a_nu_5)) and len(a_nu_5) == 0:
        a_nu_5 = np.nan
    if (not np.isscalar(a_nu_6)) and len(a_nu_6) == 0:
        a_nu_6 = np.nan
    if (not np.isscalar(a_nu_15)) and len(a_nu_15) == 0:
        a_nu_15 = np.nan
    if (not np.isscalar(a_nu_16)) and len(a_nu_16) == 0:
        a_nu_16 = np.nan

    # Return
    return a_nu_5, a_nu_6, a_nu_15, a_nu_16


def secular_resonance_location_js_today():
    """
    Compute Present Day JS Secular Resonance Locations. Test Case.

    @returns: a_nu_5, a_nu_6 - Perigee (omega) Precession Res. (AU)
    @returns: a_nu_15, a_nu_16 - Line of Nodes (Omega) Precession Res. (AU)
    """

    # Semi-Major Axes
    a_j = 5.202545
    a_s = 9.554841

    # Masses
    m_j = 9.54786e-4
    m_s = 2.85837e-4

    # Compute
    a_nu_5, a_nu_6, a_nu_15, a_nu_16 = \
        secular_resonance_location(a_j, a_s, m_j, m_s)

    # Debug
    # The resonance locations should be at
    # nu_5  @ a = 0.62 AU
    # nu_6  @ a = 1.84 AU
    # nu_15 @ a = NaN     (DOES NOT EXIST, f_1 = 0)
    # nu_16 @ a = 1.97 AU
    # print "nu_5  @ a = %.2f AU" % a_nu_5
    # print "nu_6  @ a = %.2f AU" % a_nu_6
    # print "nu_15 @ a = %.2f AU" % a_nu_15
    # print "nu_16 @ a = %.2f AU" % a_nu_16

    # Return
    return a_nu_5, a_nu_6, a_nu_15, a_nu_16


def scan_resonances(a_j, a_s, m_j, m_s, t_over_tau):
    """
    Locate Secular Resonances.

    @param: a_1, a_2 - Planet Semi-Major Axis of Planets (AU)
    @param: m_1, m_2 - Planet Masses (Solar Masses)
    @param: t_over_tau - Exponential Decay Factor for Gas Density (-)
    @returns: a_nu_5, a_nu_6 - Perigee (omega) Precession Res. (AU)
    @returns: a_nu_15, a_nu_16 - Line of Nodes (Omega) Precession Res. (AU)
    """

    # Gas Disk?
    if t_over_tau < 20:
        a_nu_5, a_nu_6, a_nu_15, a_nu_16 = \
            secular_resonance_location_gas(a_j, a_s, m_j, m_s, t_over_tau)

    # No Gas
    else:
        a_nu_5, a_nu_6, a_nu_15, a_nu_16 = \
            secular_resonance_location(a_j, a_s, m_j, m_s)

    # Return Resonances
    return a_nu_5, a_nu_6, a_nu_15, a_nu_16