-
Notifications
You must be signed in to change notification settings - Fork 95
/
test_actionAngle.py
2785 lines (2656 loc) · 162 KB
/
test_actionAngle.py
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function, division
import os
import pytest
import warnings
import numpy
from galpy.util import galpyWarning
_TRAVIS= bool(os.getenv('TRAVIS'))
# Print all galpyWarnings always for tests of warnings
warnings.simplefilter("always",galpyWarning)
#Basic sanity checking of the actionAngleIsochrone actions
def test_actionAngleIsochrone_basic_actions():
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
aAI= actionAngleIsochrone(b=1.2)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAI(R,vR,vT,z,vz)
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the isochrone potential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the isochrone potential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAI(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the isochrone potential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-4., 'Close-to-circular orbit in the isochrone potential does not have small Jz'
#Close-to-circular orbit, called with time
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAI(Orbit([R,vR,vT,z,vz]),0.)
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the isochrone potential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-4., 'Close-to-circular orbit in the isochrone potential does not have small Jz'
return None
#Basic sanity checking of the actionAngleIsochrone actions
def test_actionAngleIsochrone_basic_freqs():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
jos= aAI.actionsFreqs(R,vR,vT,z,vz)
assert numpy.fabs((jos[3]-ip.epifreq(1.))/ip.epifreq(1.)) < 10.**-12., 'Circular orbit in the isochrone potential does not have Or=kappa at %g%%' % (100.*numpy.fabs((jos[3]-ip.epifreq(1.))/ip.epifreq(1.)))
assert numpy.fabs((jos[4]-ip.omegac(1.))/ip.omegac(1.)) < 10.**-12., 'Circular orbit in the isochrone potential does not have Op=Omega at %g%%' % (100.*numpy.fabs((jos[4]-ip.omegac(1.))/ip.omegac(1.)))
assert numpy.fabs((jos[5]-ip.verticalfreq(1.))/ip.verticalfreq(1.)) < 10.**-12., 'Circular orbit in the isochrone potential does not have Oz=nu at %g%%' % (100.*numpy.fabs((jos[5]-ip.verticalfreq(1.))/ip.verticalfreq(1.)))
#close-to-circular orbit
R,vR,vT,z,vz= 1.,0.01,1.01,0.01,0.01
jos= aAI.actionsFreqs(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs((jos[3]-ip.epifreq(1.))/ip.epifreq(1.)) < 10.**-2., 'Close-to-circular orbit in the isochrone potential does not have Or=kappa at %g%%' % (100.*numpy.fabs((jos[3]-ip.epifreq(1.))/ip.epifreq(1.)))
assert numpy.fabs((jos[4]-ip.omegac(1.))/ip.omegac(1.)) < 10.**-2., 'Close-to-circular orbit in the isochrone potential does not have Op=Omega at %g%%' % (100.*numpy.fabs((jos[4]-ip.omegac(1.))/ip.omegac(1.)))
assert numpy.fabs((jos[5]-ip.verticalfreq(1.))/ip.verticalfreq(1.)) < 10.**-2., 'Close-to-circular orbit in the isochrone potential does not have Oz=nu at %g%%' % (100.*numpy.fabs((jos[5]-ip.verticalfreq(1.))/ip.verticalfreq(1.)))
return None
# Test that EccZmaxRperiRap for an IsochronePotential are correctly computed
# by comparing to a numerical orbit integration
def test_actionAngleIsochrone_EccZmaxRperiRap_againstOrbit():
from galpy.potential import IsochronePotential
from galpy.orbit import Orbit
from galpy.actionAngle import actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
o= Orbit([1.,0.1,1.1,0.2,0.03,0.])
ecc, zmax, rperi, rap= aAI.EccZmaxRperiRap(o)
ts= numpy.linspace(0.,100.,100001)
o.integrate(ts,ip)
assert numpy.fabs(ecc-o.e()) < 1e-10, 'Analytically calculated eccentricity does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(zmax-o.zmax()) < 1e-5, 'Analytically calculated zmax does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(rperi-o.rperi()) < 1e-10, 'Analytically calculated rperi does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(rap-o.rap()) < 1e-10, 'Analytically calculated rap does not agree with numerically calculated one for an IsochronePotential'
# Another one
o= Orbit([1.,0.1,1.1,0.2,-0.3,0.])
ecc, zmax, rperi, rap= aAI.EccZmaxRperiRap(o.R(),o.vR(),o.vT(),
o.z(),o.vz(),o.phi())
ts= numpy.linspace(0.,100.,100001)
o.integrate(ts,ip)
assert numpy.fabs(ecc-o.e()) < 1e-10, 'Analytically calculated eccentricity does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(zmax-o.zmax()) < 1e-3, 'Analytically calculated zmax does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(rperi-o.rperi()) < 1e-10, 'Analytically calculated rperi does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(rap-o.rap()) < 1e-10, 'Analytically calculated rap does not agree with numerically calculated one for an IsochronePotential'
return None
# Test that EccZmaxRperiRap for an IsochronePotential are correctly computed
# by comparing to a numerical orbit integration for a Kepler potential
def test_actionAngleIsochrone_EccZmaxRperiRap_againstOrbit_kepler():
from galpy.potential import IsochronePotential
from galpy.orbit import Orbit
from galpy.actionAngle import actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=0)
aAI= actionAngleIsochrone(ip=ip)
o= Orbit([1.,0.1,1.1,0.2,0.03,0.])
ecc, zmax, rperi, rap= aAI.EccZmaxRperiRap(o.R(),o.vR(),o.vT(),o.z(),o.vz())
ts= numpy.linspace(0.,100.,100001)
o.integrate(ts,ip)
assert numpy.fabs(ecc-o.e()) < 1e-10, 'Analytically calculated eccentricity does not agree with numerically calculated one for an IsochronePotential'
# Don't do zmax, because zmax for Kepler is approximate
assert numpy.fabs(rperi-o.rperi()) < 1e-10, 'Analytically calculated rperi does not agree with numerically calculated one for an IsochronePotential'
assert numpy.fabs(rap-o.rap()) < 1e-10, 'Analytically calculated rap does not agree with numerically calculated one for an IsochronePotential'
return None
#Test the actions of an actionAngleIsochrone
def test_actionAngleIsochrone_conserved_actions():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5])
from galpy.orbit_src.FullOrbit import ext_loaded
if not ext_loaded: #odeint is not as accurate as dopr54_c
check_actionAngle_conserved_actions(aAI,obs,ip,-5.,-5.,-5.)
else:
check_actionAngle_conserved_actions(aAI,obs,ip,-8.,-8.,-8.)
return None
#Test that the angles of an actionAngleIsochrone increase linearly
def test_actionAngleIsochrone_linear_angles():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5,2.])
from galpy.orbit_src.FullOrbit import ext_loaded
if not ext_loaded: #odeint is not as accurate as dopr54_c
check_actionAngle_linear_angles(aAI,obs,ip,
-5.,-5.,-5.,
-6.,-6.,-6.,
-5.,-5.,-5.)
else:
check_actionAngle_linear_angles(aAI,obs,ip,
-6.,-6.,-6.,
-8.,-8.,-8.,
-8.,-8.,-8.)
return None
#Test that the Kelperian limit of the isochrone actions/angles works
def test_actionAngleIsochrone_kepler_actions():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=0.)
aAI= actionAngleIsochrone(ip=ip)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5,2.])
times= numpy.linspace(0.,100.,101)
obs.integrate(times,ip,method='dopr54_c')
jrs,jps,jzs= aAI(obs.R(times),obs.vR(times),obs.vT(times),
obs.z(times),obs.vz(times),obs.phi(times))
jc= ip._amp/numpy.sqrt(-2.*obs.E())
L= numpy.sqrt(numpy.sum(obs.L()**2.))
# Jr = Jc-L
assert numpy.all(numpy.fabs(jrs-(jc-L)) < 10.**-5.), 'Radial action for the Kepler potential not correct'
assert numpy.all(numpy.fabs(jps-obs.R()*obs.vT()) < 10.**-10.), 'Azimuthal action for the Kepler potential not correct'
assert numpy.all(numpy.fabs(jzs-(L-numpy.fabs(obs.R()*obs.vT()))) < 10.**-10.), 'Vertical action for the Kepler potential not correct'
return None
def test_actionAngleIsochrone_kepler_freqs():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=0.)
aAI= actionAngleIsochrone(ip=ip)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5,2.])
times= numpy.linspace(0.,100.,101)
obs.integrate(times,ip,method='dopr54_c')
_, _, _, ors,ops,ozs= aAI.actionsFreqs(obs.R(times),obs.vR(times),
obs.vT(times),obs.z(times),
obs.vz(times),obs.phi(times))
jc= ip._amp/numpy.sqrt(-2.*obs.E())
oc= ip._amp**2./jc**3. # (BT08 eqn. E4)
assert numpy.all(numpy.fabs(ors-oc) < 10.**-10.), 'Radial frequency for the Kepler potential not correct'
assert numpy.all(numpy.fabs(ops-oc) < 10.**-10.), 'Azimuthal frequency for the Kepler potential not correct'
assert numpy.all(numpy.fabs(ozs-numpy.sign(obs.R()*obs.vT())*oc) < 10.**-10.), 'Vertical frequency for the Kepler potential not correct'
return None
def test_actionAngleIsochrone_kepler_angles():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=0.)
aAI= actionAngleIsochrone(ip=ip)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5,2.])
times= numpy.linspace(0.,100.,101)
obs.integrate(times,ip,method='dopr54_c')
_, _, _, _, _, _,ars,aps,azs= \
aAI.actionsFreqsAngles(obs.R(times),obs.vR(times),
obs.vT(times),obs.z(times),
obs.vz(times),obs.phi(times))
jc= ip._amp/numpy.sqrt(-2.*obs.E())
oc= ip._amp**2./jc**3. # (BT08 eqn. E4)
# theta_r = Or x times + theta_r,0
assert numpy.all(numpy.fabs(ars-oc*times-ars[0]) < 10.**-10.), 'Radial angle for the Kepler potential not correct'
assert numpy.all(numpy.fabs(aps-oc*times-aps[0]) < 10.**-10.), 'Azimuthal angle for the Kepler potential not correct'
assert numpy.all(numpy.fabs(azs-oc*times-azs[0]) < 10.**-10.), 'Vertical angle for the Kepler potential not correct'
return None
#Basic sanity checking of the actionAngleSpherical actions
def test_actionAngleSpherical_basic_actions():
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
from galpy.potential import LogarithmicHaloPotential
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAS(Orbit([R,vR,vT]))
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the spherical LogarithmicHaloPotential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the spherical LogarithmicHaloPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAS(Orbit([R,vR,vT,z,vz])._orb) #with OrbitTop
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-4., 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have small Jz'
return None
#Basic sanity checking of the actionAngleSpherical actions
def test_actionAngleSpherical_basic_freqs():
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=[lp])
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
jos= aAS.actionsFreqs(R,vR,vT,z,vz)
assert numpy.fabs((jos[3]-lp.epifreq(1.))/lp.epifreq(1.)) < 10.**-12., 'Circular orbit in the spherical LogarithmicHaloPotential does not have Or=kappa at %g%%' % (100.*numpy.fabs((jos[3]-lp.epifreq(1.))/lp.epifreq(1.)))
assert numpy.fabs((jos[4]-lp.omegac(1.))/lp.omegac(1.)) < 10.**-12., 'Circular orbit in the spherical LogarithmicHaloPotential does not have Op=Omega at %g%%' % (100.*numpy.fabs((jos[4]-lp.omegac(1.))/lp.omegac(1.)))
assert numpy.fabs((jos[5]-lp.verticalfreq(1.))/lp.verticalfreq(1.)) < 10.**-12., 'Circular orbit in the spherical LogarithmicHaloPotential does not have Oz=nu at %g%%' % (100.*numpy.fabs((jos[5]-lp.verticalfreq(1.))/lp.verticalfreq(1.)))
#close-to-circular orbit
R,vR,vT,z,vz= 1.,0.01,1.01,0.01,0.01
jos= aAS.actionsFreqs(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs((jos[3]-lp.epifreq(1.))/lp.epifreq(1.)) < 10.**-1.9, 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have Or=kappa at %g%%' % (100.*numpy.fabs((jos[3]-lp.epifreq(1.))/lp.epifreq(1.)))
assert numpy.fabs((jos[4]-lp.omegac(1.))/lp.omegac(1.)) < 10.**-1.9, 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have Op=Omega at %g%%' % (100.*numpy.fabs((jos[4]-lp.omegac(1.))/lp.omegac(1.)))
assert numpy.fabs((jos[5]-lp.verticalfreq(1.))/lp.verticalfreq(1.)) < 10.**-1.9, 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have Oz=nu at %g%%' % (100.*numpy.fabs((jos[5]-lp.verticalfreq(1.))/lp.verticalfreq(1.)))
#Basic sanity checking of the actionAngleSpherical actions
def test_actionAngleSpherical_basic_freqsAngles():
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
#v. close-to-circular orbit using actionsFreqsAngles
R,vR,vT,z,vz= 1.,10.**-8.,1.,10.**-8.,0.
jos= aAS.actionsFreqsAngles(R,vR,vT,z,vz,0.)
assert numpy.fabs((jos[3]-lp.epifreq(1.))/lp.epifreq(1.)) < 10.**-1.9, 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have Or=kappa at %g%%' % (100.*numpy.fabs((jos[3]-lp.epifreq(1.))/lp.epifreq(1.)))
assert numpy.fabs((jos[4]-lp.omegac(1.))/lp.omegac(1.)) < 10.**-1.9, 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have Op=Omega at %g%%' % (100.*numpy.fabs((jos[4]-lp.omegac(1.))/lp.omegac(1.)))
assert numpy.fabs((jos[5]-lp.verticalfreq(1.))/lp.verticalfreq(1.)) < 10.**-1.9, 'Close-to-circular orbit in the spherical LogarithmicHaloPotential does not have Oz=nu at %g%%' % (100.*numpy.fabs((jos[5]-lp.verticalfreq(1.))/lp.verticalfreq(1.)))
return None
# Test that EccZmaxRperiRap for a spherical potential are correctly computed
# by comparing to a numerical orbit integration
def test_actionAngleSpherical_EccZmaxRperiRap_againstOrbit():
from galpy.potential import LogarithmicHaloPotential
from galpy.orbit import Orbit
from galpy.actionAngle import actionAngleSpherical
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
o= Orbit([1.,0.1,1.1,0.2,0.03,0.])
ecc, zmax, rperi, rap= aAS.EccZmaxRperiRap(o)
ts= numpy.linspace(0.,100.,100001)
o.integrate(ts,lp)
assert numpy.fabs(ecc-o.e()) < 1e-9, 'Analytically calculated eccentricity does not agree with numerically calculated one for a spherical potential'
assert numpy.fabs(zmax-o.zmax()) < 1e-4, 'Analytically calculated zmax does not agree with numerically calculated one for a spherical potential'
assert numpy.fabs(rperi-o.rperi()) < 1e-8, 'Analytically calculated rperi does not agree with numerically calculated one for a spherical potential'
assert numpy.fabs(rap-o.rap()) < 1e-8, 'Analytically calculated rap does not agree with numerically calculated one for a spherical potential'
# Another one
o= Orbit([1.,0.1,1.1,0.2,-0.3,0.])
ecc, zmax, rperi, rap= aAS.EccZmaxRperiRap(o.R(),o.vR(),o.vT(),
o.z(),o.vz())
ts= numpy.linspace(0.,100.,100001)
o.integrate(ts,lp)
assert numpy.fabs(ecc-o.e()) < 1e-9, 'Analytically calculated eccentricity does not agree with numerically calculated one for a spherical potential'
assert numpy.fabs(zmax-o.zmax()) < 1e-3, 'Analytically calculated zmax does not agree with numerically calculated one for a spherical potential'
assert numpy.fabs(rperi-o.rperi()) < 1e-8, 'Analytically calculated rperi does not agree with numerically calculated one for a spherical potential'
assert numpy.fabs(rap-o.rap()) < 1e-8, 'Analytically calculated rap does not agree with numerically calculated one for a spherical potential'
return None
#Test the actions of an actionAngleSpherical
def test_actionAngleSpherical_conserved_actions():
from galpy import potential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
lp= potential.LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5])
from galpy.orbit_src.FullOrbit import ext_loaded
if not ext_loaded: #odeint is not as accurate as dopr54_c
check_actionAngle_conserved_actions(aAS,obs,lp,-5.,-5.,-5.,ntimes=101)
else:
check_actionAngle_conserved_actions(aAS,obs,lp,-8.,-8.,-8.,ntimes=101)
return None
#Test the actions of an actionAngleSpherical
def test_actionAngleSpherical_conserved_actions_fixed_quad():
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5])
from galpy.orbit_src.FullOrbit import ext_loaded
if not ext_loaded: #odeint is not as accurate as dopr54_c
check_actionAngle_conserved_actions(aAS,obs,lp,-5.,-5.,-5.,ntimes=101,
fixed_quad=True)
else:
check_actionAngle_conserved_actions(aAS,obs,lp,-8.,-8.,-8.,ntimes=101,
fixed_quad=True)
return None
#Test that the angles of an actionAngleIsochrone increase linearly
def test_actionAngleSpherical_linear_angles():
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5,2.])
from galpy.orbit_src.FullOrbit import ext_loaded
if not ext_loaded: #odeint is not as accurate as dopr54_c
check_actionAngle_linear_angles(aAS,obs,lp,
-4.,-4.,-4.,
-4.,-4.,-4.,
-4.,-4.,-4.,
ntimes=501) #need fine sampling for de-period
else:
check_actionAngle_linear_angles(aAS,obs,lp,
-6.,-6.,-6.,
-8.,-8.,-8.,
-8.,-8.,-8.,
ntimes=501) #need fine sampling for de-period
return None
#Test that the angles of an actionAngleIsochrone increase linearly
def test_actionAngleSpherical_linear_angles_fixed_quad():
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
lp= LogarithmicHaloPotential(normalize=1.,q=1.)
aAS= actionAngleSpherical(pot=lp)
obs= Orbit([1.1, 0.3, 1.2, 0.2,0.5,2.])
from galpy.orbit_src.FullOrbit import ext_loaded
if not ext_loaded: #odeint is not as accurate as dopr54_c
check_actionAngle_linear_angles(aAS,obs,lp,
-4.,-4.,-4.,
-4.,-4.,-4.,
-4.,-4.,-4.,
ntimes=501, #need fine sampling for de-period
fixed_quad=True)
else:
check_actionAngle_linear_angles(aAS,obs,lp,
-6.,-6.,-6.,
-8.,-8.,-8.,
-8.,-8.,-8.,
ntimes=501, #need fine sampling for de-period
fixed_quad=True)
return None
#Test the conservation of ecc, zmax, rperi, rap of an actionAngleSpherical
def test_actionAngleSpherical_conserved_EccZmaxRperiRap_ecc():
from galpy.potential import NFWPotential
from galpy.actionAngle import actionAngleSpherical
from galpy.orbit import Orbit
np= NFWPotential(normalize=1.,a=2.)
aAS= actionAngleSpherical(pot=np)
obs= Orbit([1.1,0.2, 1.3, 0.1,0.,2.])
check_actionAngle_conserved_EccZmaxRperiRap(aAS,obs,np,
-1.1,-0.4,-1.8,-1.8,ntimes=101,
inclphi=True)
return None
#Test the actionAngleSpherical against an isochrone potential: actions
def test_actionAngleSpherical_otherIsochrone_actions():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleSpherical, \
actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
aAS= actionAngleSpherical(pot=ip)
R,vR,vT,z,vz,phi= 1.1, 0.3, 1.2, 0.2,0.5,2.
ji= aAI(R,vR,vT,z,vz,phi)
jia= aAS(R,vR,vT,z,vz,phi)
djr= numpy.fabs((ji[0]-jia[0])/ji[0])
dlz= numpy.fabs((ji[1]-jia[1])/ji[1])
djz= numpy.fabs((ji[2]-jia[2])/ji[2])
assert djr < 10.**-10., 'actionAngleSpherical applied to isochrone potential fails for Jr at %g%%' % (djr*100.)
#Lz and Jz are easy, because ip is a spherical potential
assert dlz < 10.**-10., 'actionAngleSpherical applied to isochrone potential fails for Lz at %g%%' % (dlz*100.)
assert djz < 10.**-10., 'actionAngleSpherical applied to isochrone potential fails for Jz at %g%%' % (djz*100.)
return None
#Test the actionAngleSpherical against an isochrone potential: frequencies
def test_actionAngleSpherical_otherIsochrone_freqs():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleSpherical, \
actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
aAS= actionAngleSpherical(pot=ip)
R,vR,vT,z,vz,phi= 1.1, 0.3, 1.2, 0.2,0.5,2.
jiO= aAI.actionsFreqs(R,vR,vT,z,vz,phi)
jiaO= aAS.actionsFreqs(R,vR,vT,z,vz,phi)
dOr= numpy.fabs((jiO[3]-jiaO[3])/jiO[3])
dOp= numpy.fabs((jiO[4]-jiaO[4])/jiO[4])
dOz= numpy.fabs((jiO[5]-jiaO[5])/jiO[5])
assert dOr < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for Or at %g%%' % (dOr*100.)
assert dOp < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for Op at %g%%' % (dOp*100.)
assert dOz < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for Oz at %g%%' % (dOz*100.)
return None
#Test the actionAngleSpherical against an isochrone potential: frequencies
def test_actionAngleSpherical_otherIsochrone_freqs_fixed_quad():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleSpherical, \
actionAngleIsochrone
from galpy.orbit import Orbit
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
aAS= actionAngleSpherical(pot=ip)
R,vR,vT,z,vz,phi= 1.1, 0.3, 1.2, 0.2,0.5,2.
jiO= aAI.actionsFreqs(R,vR,vT,z,vz,phi)
jiaO= aAS.actionsFreqs(Orbit([R,vR,vT,z,vz,phi]),fixed_quad=True)
dOr= numpy.fabs((jiO[3]-jiaO[3])/jiO[3])
dOp= numpy.fabs((jiO[4]-jiaO[4])/jiO[4])
dOz= numpy.fabs((jiO[5]-jiaO[5])/jiO[5])
assert dOr < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for Or at %g%%' % (dOr*100.)
assert dOp < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for Op at %g%%' % (dOp*100.)
assert dOz < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for Oz at %g%%' % (dOz*100.)
return None
#Test the actionAngleSpherical against an isochrone potential: angles
def test_actionAngleSpherical_otherIsochrone_angles():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleSpherical, \
actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
aAS= actionAngleSpherical(pot=ip,b=0.8)
R,vR,vT,z,vz,phi= 1.1, 0.3, 1.2, 0.2,0.5,2.
jiO= aAI.actionsFreqsAngles(R,vR,vT,z,vz,phi)
jiaO= aAS.actionsFreqsAngles(R,vR,vT,z,vz,phi)
dar= numpy.fabs((jiO[6]-jiaO[6])/jiO[6])
dap= numpy.fabs((jiO[7]-jiaO[7])/jiO[7])
daz= numpy.fabs((jiO[8]-jiaO[8])/jiO[8])
assert dar < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for ar at %g%%' % (dar*100.)
assert dap < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for ap at %g%%' % (dap*100.)
assert daz < 10.**-6., 'actionAngleSpherical applied to isochrone potential fails for az at %g%%' % (daz*100.)
return None
#Basic sanity checking of the actionAngleAdiabatic actions
def test_actionAngleAdiabatic_basic_actions():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
from galpy.potential import MWPotential
aAA= actionAngleAdiabatic(pot=MWPotential,gamma=1.)
#circular orbit
R,vR,vT,phi= 1.,0.,1.,2.
js= aAA(Orbit([R,vR,vT,phi]))
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,0.99,0.0,0.0
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,1.01,0.0,0.0
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
return None
#Basic sanity checking of the actionAngleAdiabatic actions
def test_actionAngleAdiabatic_basic_actions_gamma0():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
from galpy.potential import MWPotential
aAA= actionAngleAdiabatic(pot=[MWPotential[0],MWPotential[1:]],gamma=0.)
#circular orbit
R,vR,vT,phi= 1.,0.,1.,2.
js= aAA(Orbit([R,vR,vT,phi]))
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,0.99,0.0,0.0
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,1.01,0.0,0.0
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
return None
#Basic sanity checking of the actionAngleAdiabatic actions
def test_actionAngleAdiabatic_basic_actions_c():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
from galpy.potential import MWPotential
# test nested list of potentials
aAA= actionAngleAdiabatic(pot=[MWPotential[0],MWPotential[1:]],c=True)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAA(R,vR,vT,z,vz)
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#Basic sanity checking of the actionAngleAdiabatic actions
def test_actionAngleAdiabatic_unboundz_actions_c():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.potential import MWPotential
aAA= actionAngleAdiabatic(pot=MWPotential,c=True,gamma=0.)
#Unbound in z, so jz should be very large
R,vR,vT,z,vz= 1.,0.,1.,0., 10.
js= aAA(R,vR,vT,z,vz)
assert js[2] > 1000., 'Unbound orbit in z in the MWPotential does not have large Jz'
return None
#Basic sanity checking of the actionAngleAdiabatic actions
def test_actionAngleAdiabatic_zerolz_actions_c():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.potential import MWPotential
aAA= actionAngleAdiabatic(pot=MWPotential,c=True,gamma=0.)
#Zero angular momentum, so rperi=0, but should have finite jr
R,vR,vT,z,vz= 1.,0.,0.,0., 0.
js= aAA(R,vR,vT,z,vz)
R,vR,vT,z,vz= 1.,0.,0.0000001,0., 0.
js2= aAA(R,vR,vT,z,vz)
assert numpy.fabs(js[0]-js2[0]) < 10.**-6., 'Orbit with zero angular momentum does not have the correct Jr'
#Zero angular momentum, so rperi=0, but should have finite jr
R,vR,vT,z,vz= 1.,-0.5,0.,0., 0.
js= aAA(R,vR,vT,z,vz)
R,vR,vT,z,vz= 1.,-0.5,0.0000001,0., 0.
js2= aAA(R,vR,vT,z,vz)
assert numpy.fabs(js[0]-js2[0]) < 10.**-6., 'Orbit with zero angular momentum does not have the correct Jr'
return None
#Basic sanity checking of the actionAngleAdiabatic ecc, zmax, rperi, rap calc.
def test_actionAngleAdiabatic_basic_EccZmaxRperiRap():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.potential import MWPotential
aAA= actionAngleAdiabatic(pot=MWPotential,gamma=1.)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz)
assert numpy.fabs(te) < 10.**-16., 'Circular orbit in the MWPotential does not have e=0'
assert numpy.fabs(tzmax) < 10.**-16., 'Circular orbit in the MWPotential does not have zmax=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz)
assert numpy.fabs(te) < 10.**-2., 'Close-to-circular orbit in the MWPotential does not have small eccentricity'
assert numpy.fabs(tzmax) < 2.*10.**-2., 'Close-to-circular orbit in the MWPotential does not have small zmax'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,0.99,0.0,0.0
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz)
assert numpy.fabs(te) < 10.**-2., 'Close-to-circular orbit in the MWPotential does not have small eccentricity'
assert numpy.fabs(tzmax) < 2.*10.**-2., 'Close-to-circular orbit in the MWPotential does not have small zmax'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,1.,0.01,0.0
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz)
assert numpy.fabs(te) < 10.**-2., 'Close-to-circular orbit in the MWPotential does not have small eccentricity'
assert numpy.fabs(tzmax) < 2.*10.**-2., 'Close-to-circular orbit in the MWPotential does not have small zmax'
return None
#Basic sanity checking of the actionAngleAdiabatic ecc, zmax, rperi, rap calc.
def test_actionAngleAdiabatic_basic_EccZmaxRperiRap_gamma0():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.potential import MiyamotoNagaiPotential
mp= MiyamotoNagaiPotential(normalize=1.,a=1.5,b=0.3)
aAA= actionAngleAdiabatic(pot=mp,gamma=0.,c=False)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz)
assert numpy.fabs(te) < 10.**-16., 'Circular orbit in the MWPotential does not have e=0'
assert numpy.fabs(tzmax) < 10.**-16., 'Circular orbit in the MWPotential does not have zmax=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz)
assert numpy.fabs(te) < 10.**-2., 'Close-to-circular orbit in the MWPotential does not have small eccentricity'
assert numpy.fabs(tzmax) < 2.*10.**-2., 'Close-to-circular orbit in the MWPotential does not have small zmax'
return None
#Basic sanity checking of the actionAngleAdiabatic ecc, zmax, rperi, rap calc.
def test_actionAngleAdiabatic_basic_EccZmaxRperiRap_gamma_c():
from galpy.actionAngle import actionAngleAdiabatic
from galpy.potential import MWPotential
from galpy.orbit import Orbit
aAA= actionAngleAdiabatic(pot=MWPotential,gamma=1.,c=True)
#circular orbit
R,vR,vT,z,vz,phi= 1.,0.,1.,0.,0.,2.
te,tzmax,_,_= aAA.EccZmaxRperiRap(Orbit([R,vR,vT,z,vz,phi]))
assert numpy.fabs(te) < 10.**-16., 'Circular orbit in the MWPotential does not have e=0'
assert numpy.fabs(tzmax) < 10.**-16., 'Circular orbit in the MWPotential does not have zmax=0'
#Close-to-circular orbit
R,vR,vT,z,vz,phi= 1.01,0.01,1.,0.01,0.01,2.
te,tzmax,_,_= aAA.EccZmaxRperiRap(R,vR,vT,z,vz,phi)
assert numpy.fabs(te) < 10.**-2., 'Close-to-circular orbit in the MWPotential does not have small eccentricity'
assert numpy.fabs(tzmax) < 2.*10.**-2., 'Close-to-circular orbit in the MWPotential does not have small zmax'
return None
#Test the actions of an actionAngleAdiabatic
def test_actionAngleAdiabatic_conserved_actions():
from galpy.potential import MWPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
aAA= actionAngleAdiabatic(pot=MWPotential,c=False)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.])
check_actionAngle_conserved_actions(aAA,obs,MWPotential,
-1.2,-8.,-1.7,ntimes=101)
return None
#Test the actions of an actionAngleAdiabatic
def test_actionAngleAdiabatic_conserved_actions_c():
from galpy.potential import MWPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.])
aAA= actionAngleAdiabatic(pot=MWPotential,c=True)
check_actionAngle_conserved_actions(aAA,obs,MWPotential,
-1.4,-8.,-1.7,ntimes=101)
return None
#Test the actions of an actionAngleAdiabatic, single pot
def test_actionAngleAdiabatic_conserved_actions_singlepot():
from galpy.potential import MiyamotoNagaiPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
mp= MiyamotoNagaiPotential(normalize=1.)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.,2.])
aAA= actionAngleAdiabatic(pot=mp,c=False)
check_actionAngle_conserved_actions(aAA,obs,mp,
-1.5,-8.,-2.,ntimes=101,
inclphi=True)
return None
#Test the actions of an actionAngleAdiabatic, single pot, C
def test_actionAngleAdiabatic_conserved_actions_singlepot_c():
from galpy.potential import MiyamotoNagaiPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
mp= MiyamotoNagaiPotential(normalize=1.)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.,2.])
aAA= actionAngleAdiabatic(pot=mp,c=True)
check_actionAngle_conserved_actions(aAA,obs,mp,
-1.5,-8.,-2.,ntimes=101,
inclphi=True)
return None
#Test the actions of an actionAngleAdiabatic, interpolated pot
def test_actionAngleAdiabatic_conserved_actions_interppot_c():
from galpy.potential import MWPotential, interpRZPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
ip= interpRZPotential(RZPot=MWPotential,
rgrid=(numpy.log(0.01),numpy.log(20.),101),
zgrid=(0.,1.,101),logR=True,use_c=True,enable_c=True,
interpPot=True,interpRforce=True,interpzforce=True)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.,2.])
aAA= actionAngleAdiabatic(pot=ip,c=True)
check_actionAngle_conserved_actions(aAA,obs,ip,
-1.4,-8.,-1.7,ntimes=101)
return None
#Test the conservation of ecc, zmax, rperi, rap of an actionAngleAdiabatic
def test_actionAngleAdiabatic_conserved_EccZmaxRperiRap():
from galpy.potential import MWPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
aAA= actionAngleAdiabatic(pot=MWPotential,c=False,gamma=1.)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.,0.])
check_actionAngle_conserved_EccZmaxRperiRap(aAA,obs,MWPotential,
-1.7,-1.4,-2.,-2.,ntimes=101)
return None
#Test the conservation of ecc, zmax, rperi, rap of an actionAngleAdiabatic
def test_actionAngleAdiabatic_conserved_EccZmaxRperiRap_ecc():
from galpy.potential import MWPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
aAA= actionAngleAdiabatic(pot=MWPotential,c=False,gamma=1.)
obs= Orbit([1.1,0.2, 1.3, 0.1,0.,2.])
check_actionAngle_conserved_EccZmaxRperiRap(aAA,obs,MWPotential,
-1.1,-0.4,-1.8,-1.8,ntimes=101,
inclphi=True)
return None
#Test the conservation of ecc, zmax, rperi, rap of an actionAngleAdiabatic
def test_actionAngleAdiabatic_conserved_EccZmaxRperiRap_singlepot_c():
from galpy.potential import MiyamotoNagaiPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
mp= MiyamotoNagaiPotential(normalize=1.)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.,2.])
aAA= actionAngleAdiabatic(pot=mp,c=True)
check_actionAngle_conserved_EccZmaxRperiRap(aAA,obs,mp,
-1.7,-1.4,-2.,-2.,ntimes=101)
return None
#Test the conservation of ecc, zmax, rperi, rap of an actionAngleAdiabatic
def test_actionAngleAdiabatic_conserved_EccZmaxRperiRa_interppot_c():
from galpy.potential import MWPotential, interpRZPotential
from galpy.actionAngle import actionAngleAdiabatic
from galpy.orbit import Orbit
ip= interpRZPotential(RZPot=MWPotential,
rgrid=(numpy.log(0.01),numpy.log(20.),101),
zgrid=(0.,1.,101),logR=True,use_c=True,enable_c=True,
interpPot=True,interpRforce=True,interpzforce=True)
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.,2.])
aAA= actionAngleAdiabatic(pot=ip,c=True)
check_actionAngle_conserved_EccZmaxRperiRap(aAA,obs,ip,
-1.7,-1.4,-2.,-2.,ntimes=101)
return None
#Test the actionAngleAdiabatic against an isochrone potential: actions
def test_actionAngleAdiabatic_Isochrone_actions():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleAdiabatic, \
actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
aAA= actionAngleAdiabatic(pot=ip,c=True)
R,vR,vT,z,vz,phi= 1.01, 0.05, 1.05, 0.05,0.,2.
ji= aAI(R,vR,vT,z,vz,phi)
jia= aAA(R,vR,vT,z,vz,phi)
djr= numpy.fabs((ji[0]-jia[0])/ji[0])
dlz= numpy.fabs((ji[1]-jia[1])/ji[1])
djz= numpy.fabs((ji[2]-jia[2])/ji[2])
assert djr < 10.**-1.2, 'actionAngleAdiabatic applied to isochrone potential fails for Jr at %f%%' % (djr*100.)
#Lz and Jz are easy, because ip is a spherical potential
assert dlz < 10.**-10., 'actionAngleAdiabatic applied to isochrone potential fails for Lz at %f%%' % (dlz*100.)
assert djz < 10.**-1.2, 'actionAngleAdiabatic applied to isochrone potential fails for Jz at %f%%' % (djz*100.)
return None
#Basic sanity checking of the actionAngleAdiabatic actions (incl. conserved, bc takes a lot of time)
def test_actionAngleAdiabaticGrid_basicAndConserved_actions():
from galpy.actionAngle import actionAngleAdiabaticGrid
from galpy.orbit import Orbit
from galpy.potential import MWPotential
aAA= actionAngleAdiabaticGrid(pot=MWPotential,gamma=1.,c=False)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAA(R,vR,vT,z,vz,0.)
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(aAA.Jz(R,vR,vT,z,vz,0.)) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#setup w/ multi
aAA= actionAngleAdiabaticGrid(pot=MWPotential,gamma=1.,c=False,numcores=2)
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#Check that actions are conserved along the orbit
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.])
check_actionAngle_conserved_actions(aAA,obs,MWPotential,
-1.2,-8.,-1.7,ntimes=101)
return None
#Basic sanity checking of the actionAngleAdiabatic actions
def test_actionAngleAdiabaticGrid_basic_actions_c():
from galpy.actionAngle import actionAngleAdiabaticGrid
from galpy.orbit import Orbit
from galpy.potential import MWPotential
aAA= actionAngleAdiabaticGrid(pot=MWPotential,c=True)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAA(R,vR,vT,z,vz)
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAA(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 10.**-3., 'Close-to-circular orbit in the MWPotentialspherical LogarithmicHalo does not have small Jz'
#actionAngleAdiabaticGrid actions outside the grid
def test_actionAngleAdiabaticGrid_outsidegrid_c():
from galpy.actionAngle import actionAngleAdiabaticGrid, \
actionAngleAdiabatic
from galpy.potential import MWPotential
aA= actionAngleAdiabatic(pot=MWPotential,c=True)
aAA= actionAngleAdiabaticGrid(pot=MWPotential,c=True,Rmax=2.,zmax=0.2)
R,vR,vT,z,vz,phi= 3.,0.1,1.,0.1,0.1,2.
js= aA(R,vR,vT,z,vz,phi)
jsa= aAA(R,vR,vT,z,vz,phi)
assert numpy.fabs(js[0]-jsa[0]) < 10.**-8., 'actionAngleAdiabaticGrid evaluation outside of the grid fails'
assert numpy.fabs(js[2]-jsa[2]) < 10.**-8., 'actionAngleAdiabaticGrid evaluation outside of the grid fails'
assert numpy.fabs(js[2]-aAA.Jz(R,vR,vT,z,vz,phi)) < 10.**-8., 'actionAngleAdiabaticGrid evaluation outside of the grid fails'
#Also for array
s= numpy.ones(2)
js= aA(R,vR,vT,z,vz,phi)
jsa= aAA(R*s,vR*s,vT*s,z*s,vz*s,phi*s)
assert numpy.all(numpy.fabs(js[0]-jsa[0]) < 10.**-8.), 'actionAngleAdiabaticGrid evaluation outside of the grid fails'
assert numpy.all(numpy.fabs(js[2]-jsa[2]) < 10.**-8.), 'actionAngleAdiabaticGrid evaluation outside of the grid fails'
return None
#Test the actions of an actionAngleAdiabatic
def test_actionAngleAdiabaticGrid_conserved_actions_c():
from galpy.potential import MWPotential
from galpy.actionAngle import actionAngleAdiabaticGrid
from galpy.orbit import Orbit
obs= Orbit([1.05, 0.02, 1.05, 0.03,0.])
aAA= actionAngleAdiabaticGrid(pot=MWPotential,c=True)
check_actionAngle_conserved_actions(aAA,obs,MWPotential,
-1.4,-8.,-1.7,ntimes=101)
return None
#Test the actionAngleAdiabatic against an isochrone potential: actions
def test_actionAngleAdiabaticGrid_Isochrone_actions():
from galpy.potential import IsochronePotential
from galpy.actionAngle import actionAngleAdiabaticGrid, \
actionAngleIsochrone
ip= IsochronePotential(normalize=1.,b=1.2)
aAI= actionAngleIsochrone(ip=ip)
aAA= actionAngleAdiabaticGrid(pot=ip,c=True)
R,vR,vT,z,vz,phi= 1.01, 0.05, 1.05, 0.05,0.,2.
ji= aAI(R,vR,vT,z,vz,phi)
jia= aAA(R,vR,vT,z,vz,phi)
djr= numpy.fabs((ji[0]-jia[0])/ji[0])
dlz= numpy.fabs((ji[1]-jia[1])/ji[1])
djz= numpy.fabs((ji[2]-jia[2])/ji[2])
assert djr < 10.**-1.2, 'actionAngleAdiabatic applied to isochrone potential fails for Jr at %f%%' % (djr*100.)
#Lz and Jz are easy, because ip is a spherical potential
assert dlz < 10.**-10., 'actionAngleAdiabatic applied to isochrone potential fails for Lz at %f%%' % (dlz*100.)
assert djz < 10.**-1.2, 'actionAngleAdiabatic applied to isochrone potential fails for Jz at %f%%' % (djz*100.)
return None
#Basic sanity checking of the actionAngleStaeckel actions
def test_actionAngleStaeckel_basic_actions():
from galpy.actionAngle import actionAngleStaeckel
from galpy.orbit import Orbit
from galpy.potential import MWPotential
aAS= actionAngleStaeckel(pot=MWPotential,delta=0.71,c=False)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAS(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,0.99,0.0,0.0
js= aAS(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
#Another close-to-circular orbit
R,vR,vT,z,vz= 1.0,0.0,1.,0.01,0.0
js= aAS(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
return None
#Basic sanity checking of the actionAngleStaeckel actions
def test_actionAngleStaeckel_basic_actions_u0():
from galpy.actionAngle import actionAngleStaeckel
from galpy.orbit import Orbit
from galpy.potential import MWPotential
# test nested list of potentials
aAS= actionAngleStaeckel(pot=[MWPotential[0],MWPotential[1:]],
delta=0.71,c=False,useu0=True)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAS(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
return None
#Basic sanity checking of the actionAngleStaeckel actions
def test_actionAngleStaeckel_basic_actions_u0_c():
from galpy.actionAngle import actionAngleStaeckel
from galpy.orbit import Orbit
from galpy.potential import MWPotential
# test nested list of potentials
aAS= actionAngleStaeckel(pot=[MWPotential[0],MWPotential[1:]],
delta=0.71,c=True,useu0=True)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAS(Orbit([R,vR,vT,z,vz]),u0=1.15)
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
return None
#Basic sanity checking of the actionAngleStaeckel actions, w/ u0, and interppot
def test_actionAngleStaeckel_basic_actions_u0_interppot_c():
from galpy.actionAngle import actionAngleStaeckel
from galpy.orbit import Orbit
from galpy.potential import MWPotential, interpRZPotential
ip= interpRZPotential(RZPot=MWPotential,
rgrid=(numpy.log(0.01),numpy.log(20.),101),
zgrid=(0.,1.,101),logR=True,use_c=True,enable_c=True,
interpPot=True)
aAS= actionAngleStaeckel(pot=ip,delta=0.71,c=True,useu0=True)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2][0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAS(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
return None
#Basic sanity checking of the actionAngleStaeckel actions
def test_actionAngleStaeckel_basic_actions_c():
from galpy.actionAngle import actionAngleStaeckel
from galpy.orbit import Orbit
from galpy.potential import MWPotential
aAS= actionAngleStaeckel(pot=MWPotential,delta=0.71,c=True)
#circular orbit
R,vR,vT,z,vz= 1.,0.,1.,0.,0.
js= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jr=0'
assert numpy.fabs(js[2]) < 10.**-16., 'Circular orbit in the MWPotential does not have Jz=0'
#Close-to-circular orbit
R,vR,vT,z,vz= 1.01,0.01,1.,0.01,0.01
js= aAS(Orbit([R,vR,vT,z,vz]))
assert numpy.fabs(js[0]) < 10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jr'
assert numpy.fabs(js[2]) < 2.*10.**-4., 'Close-to-circular orbit in the MWPotential does not have small Jz'
return None
#Basic sanity checking of the actionAngleStaeckel actions, unbound
def test_actionAngleStaeckel_unboundr_actions_c():
from galpy.actionAngle import actionAngleStaeckel
from galpy.potential import MWPotential
aAS= actionAngleStaeckel(pot=MWPotential,delta=0.71,c=True)
#Unbound orbit, shouldn't fail
R,vR,vT,z,vz= 1.,0.,10.,0.1,0.
js= aAS(R,vR,vT,z,vz)
assert js[0] > 1000., 'Unbound in R orbit in the MWPotential does not have large Jr'
#Another unbound orbit, shouldn't fail
R,vR,vT,z,vz= 1.,0.1,10.,0.1,0.
js= aAS(R,vR,vT,z,vz)
assert js[0] > 1000., 'Unbound in R orbit in the MWPotential does not have large Jr'
return None
#Basic sanity checking of the actionAngleStaeckel actions
def test_actionAngleStaeckel_zerolz_actions_c():
from galpy.actionAngle import actionAngleStaeckel
from galpy.potential import MWPotential
aAS= actionAngleStaeckel(pot=MWPotential,c=True,delta=0.71)
#Zero angular momentum, so rperi=0, but should have finite jr
R,vR,vT,z,vz= 1.,0.,0.,0., 0.
js= aAS(R,vR,vT,z,vz)
R,vR,vT,z,vz= 1.,0.,0.0000001,0., 0.
js2= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0]-js2[0]) < 10.**-6., 'Orbit with zero angular momentum does not have the correct Jr'
#Zero angular momentum, so rperi=0, but should have finite jr
R,vR,vT,z,vz= 1.,-0.5,0.,0., 0.
js= aAS(R,vR,vT,z,vz)
R,vR,vT,z,vz= 1.,-0.5,0.0000001,0., 0.
js2= aAS(R,vR,vT,z,vz)
assert numpy.fabs(js[0]-js2[0]) < 10.**-6., 'Orbit with zero angular momentum does not have the correct Jr'
return None
# Check that precision increases with increasing Gauss-Legendre order
def test_actionAngleStaeckel_actions_order():
from galpy.potential import KuzminKutuzovStaeckelPotential
from galpy.orbit import Orbit
from galpy.actionAngle import actionAngleStaeckel
kksp= KuzminKutuzovStaeckelPotential(normalize=1.,ac=4.,Delta=1.4)
o= Orbit([1.,0.5,1.1,0.2,-0.3,0.4])
aAS= actionAngleStaeckel(pot=kksp,delta=kksp._Delta,c=False)
# We'll assume that order=10000 is the truth, so 50 should be better than 5
jrt,jpt,jzt= aAS(o,order=10000,fixed_quad=True)
jr1,jp1,jz1= aAS(o,order=5,fixed_quad=True)
jr2,jp2,jz2= aAS(o,order=50,fixed_quad=True)
assert numpy.fabs(jr1-jrt) > numpy.fabs(jr2-jrt), 'Accuracy of actionAngleStaeckel does not increase with increasing order of integration'
assert numpy.fabs(jz1-jzt) > numpy.fabs(jz2-jzt), 'Accuracy of actionAngleStaeckel does not increase with increasing order of integration'
return None