-
Notifications
You must be signed in to change notification settings - Fork 95
/
Potential.py
4280 lines (3734 loc) · 128 KB
/
Potential.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
###############################################################################
# Potential.py: top-level class for a full potential
#
# Evaluate by calling the instance: Pot(R,z,phi)
#
# API for Potentials:
# function _evaluate(self,R,z,phi) returns Phi(R,z,phi)
# for orbit integration you need
# function _Rforce(self,R,z,phi) return -d Phi d R
# function _zforce(self,R,z,phi) return - d Phi d Z
# density
# function _dens(self,R,z,phi) return BOVY??
# for epicycle frequency
# function _R2deriv(self,R,z,phi) return d2 Phi dR2
###############################################################################
import os
import os.path
import pickle
from functools import wraps
import numpy
from scipy import integrate, optimize
from ..util import conversion, coords, galpyWarning, plot
from ..util._optional_deps import _APY_LOADED
from ..util.conversion import (
freq_in_Gyr,
get_physical,
physical_conversion,
potential_physical_input,
velocity_in_kpcGyr,
)
from .DissipativeForce import DissipativeForce, _isDissipative
from .Force import Force
from .plotEscapecurve import _INF, plotEscapecurve
from .plotRotcurve import plotRotcurve, vcirc
if _APY_LOADED:
from astropy import units
def check_potential_inputs_not_arrays(func):
"""
Decorator to check inputs and throw TypeError if any of the inputs are arrays for Potentials that do not support array evaluation.
Parameters
----------
func : function
Function to be decorated.
Returns
-------
function
Decorated function.
Notes
-----
- 2017-summer - Written for SpiralArmsPotential - Jack Hong (UBC)
- 2019-05-23 - Moved to Potential for more general use - Bovy (UofT)
"""
@wraps(func)
def func_wrapper(self, R, z, phi, t):
if (
(hasattr(R, "shape") and R.shape != () and len(R) > 1)
or (hasattr(z, "shape") and z.shape != () and len(z) > 1)
or (hasattr(phi, "shape") and phi.shape != () and len(phi) > 1)
or (hasattr(t, "shape") and t.shape != () and len(t) > 1)
):
raise TypeError(
f"Methods in {self.__class__.__name__} do not accept array inputs. Please input scalars"
)
return func(self, R, z, phi, t)
return func_wrapper
def potential_positional_arg(func):
@wraps(func)
def wrapper(Pot, /, *args, **kwargs):
return func(Pot, *args, **kwargs)
return wrapper
class Potential(Force):
"""Top-level class for a potential"""
def __init__(self, amp=1.0, ro=None, vo=None, amp_units=None):
"""
Initialize a Potential object.
Parameters
----------
amp : float, optional
Amplitude to be applied when evaluating the potential and its forces.
amp_units : str, optional
Type of units that `amp` should have if it has units. Possible values are 'mass', 'velocity2', and 'density'.
ro : float or Quantity, optional
Physical distance scale (in kpc or as Quantity). Default is from the configuration file.
vo : float or Quantity, optional
Physical velocity scale (in km/s or as Quantity). Default is from the configuration file.
"""
Force.__init__(self, amp=amp, ro=ro, vo=vo, amp_units=amp_units)
self.dim = 3
self.isRZ = True
self.isNonAxi = False
self.hasC = False
self.hasC_dxdv = False
self.hasC_dens = False
return None
@potential_physical_input
@physical_conversion("energy", pop=True)
def __call__(self, R, z, phi=0.0, t=0.0, dR=0, dphi=0):
"""
Evaluate the potential at the specified position and time.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
dR : int, optional
Order of radial derivative (default: 0).
dphi : int, optional
Order of azimuthal derivative (default: 0).
Returns
-------
float or Quantity
The potential at the specified position and time.
Notes
-----
- 2010-04-16 - Written - Bovy (NYU)
"""
return self._call_nodecorator(R, z, phi=phi, t=t, dR=dR, dphi=dphi)
def _call_nodecorator(self, R, z, phi=0.0, t=0.0, dR=0.0, dphi=0):
if dR == 0 and dphi == 0:
try:
rawOut = self._evaluate(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
raise PotentialError(
"'_evaluate' function not implemented for this potential"
)
return self._amp * rawOut if not rawOut is None else rawOut
elif dR == 1 and dphi == 0:
return -self.Rforce(R, z, phi=phi, t=t, use_physical=False)
elif dR == 0 and dphi == 1:
return -self.phitorque(R, z, phi=phi, t=t, use_physical=False)
elif dR == 2 and dphi == 0:
return self.R2deriv(R, z, phi=phi, t=t, use_physical=False)
elif dR == 0 and dphi == 2:
return self.phi2deriv(R, z, phi=phi, t=t, use_physical=False)
elif dR == 1 and dphi == 1:
return self.Rphideriv(R, z, phi=phi, t=t, use_physical=False)
elif dR != 0 or dphi != 0:
raise NotImplementedError(
"Higher-order derivatives not implemented for this potential"
)
@potential_physical_input
@physical_conversion("force", pop=True)
def Rforce(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the cylindrical radial force F_R.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
F_R (R,z,phi,t).
Notes
-----
- 2010-04-16 - Written - Bovy (NYU)
"""
return self._Rforce_nodecorator(R, z, phi=phi, t=t)
def _Rforce_nodecorator(self, R, z, phi=0.0, t=0.0):
# Separate, so it can be used during orbit integration
try:
return self._amp * self._Rforce(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
raise PotentialError(
"'_Rforce' function not implemented for this potential"
)
@potential_physical_input
@physical_conversion("force", pop=True)
def zforce(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the vertical force F_z.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
F_z (R,z,phi,t).
Notes
-----
- 2010-04-16 - Written - Bovy (NYU)
"""
return self._zforce_nodecorator(R, z, phi=phi, t=t)
def _zforce_nodecorator(self, R, z, phi=0.0, t=0.0):
# Separate, so it can be used during orbit integration
try:
return self._amp * self._zforce(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
raise PotentialError(
"'_zforce' function not implemented for this potential"
)
@potential_physical_input
@physical_conversion("forcederivative", pop=True)
def r2deriv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the second spherical radial derivative.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2phi/dr2.
Notes
-----
- 2018-03-21 - Written - Webb (UofT)
"""
r = numpy.sqrt(R**2.0 + z**2.0)
return (
self.R2deriv(R, z, phi=phi, t=t, use_physical=False) * R / r
+ self.Rzderiv(R, z, phi=phi, t=t, use_physical=False) * z / r
) * R / r + (
self.Rzderiv(R, z, phi=phi, t=t, use_physical=False) * R / r
+ self.z2deriv(R, z, phi=phi, t=t, use_physical=False) * z / r
) * z / r
@potential_physical_input
@physical_conversion("density", pop=True)
def dens(self, R, z, phi=0.0, t=0.0, forcepoisson=False):
"""
Evaluate the density rho(R,z,t).
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
forcepoisson : bool, optional
If True, calculate the density through the Poisson equation, even if an explicit expression for the density exists (default: False).
Returns
-------
float or Quantity
rho (R,z,phi,t).
Notes
-----
- 2010-08-08 - Written - Bovy (NYU)
- 2018-03-21 - Modified - Webb (UofT)
"""
try:
if forcepoisson:
raise AttributeError # Hack!
return self._amp * self._dens(R, z, phi=phi, t=t)
except AttributeError:
# Use the Poisson equation to get the density
return (
(
-self.Rforce(R, z, phi=phi, t=t, use_physical=False) / R
+ self.R2deriv(R, z, phi=phi, t=t, use_physical=False)
+ self.phi2deriv(R, z, phi=phi, t=t, use_physical=False) / R**2.0
+ self.z2deriv(R, z, phi=phi, t=t, use_physical=False)
)
/ 4.0
/ numpy.pi
)
@potential_physical_input
@physical_conversion("surfacedensity", pop=True)
def surfdens(self, R, z, phi=0.0, t=0.0, forcepoisson=False):
"""
Evaluate the surface density Sigma(R,z,phi,t) = int_{-z}^{+z} dz' rho(R,z',phi,t).
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
forcepoisson : bool, optional
If True, calculate the surface density through the Poisson equation, even if an explicit expression for the surface density exists (default: False).
Returns
-------
float or Quantity
Sigma(R,z,phi,t).
Notes
-----
- 2018-08-19 - Written - Bovy (UofT)
- 2021-04-19 - Adjusted for non-z-symmetric densities - Bovy (UofT)
"""
try:
if forcepoisson:
raise AttributeError # Hack!
return self._amp * self._surfdens(R, z, phi=phi, t=t)
except AttributeError:
# Use the Poisson equation to get the surface density
return (
(
-self.zforce(R, numpy.fabs(z), phi=phi, t=t, use_physical=False)
+ self.zforce(R, -numpy.fabs(z), phi=phi, t=t, use_physical=False)
+ integrate.quad(
lambda x: -self.Rforce(R, x, phi=phi, t=t, use_physical=False)
/ R
+ self.R2deriv(R, x, phi=phi, t=t, use_physical=False)
+ self.phi2deriv(R, x, phi=phi, t=t, use_physical=False)
/ R**2.0,
-numpy.fabs(z),
numpy.fabs(z),
)[0]
)
/ 4.0
/ numpy.pi
)
def _surfdens(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the surface density for this potential.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
The surface density.
Notes
-----
- 2018-08-19 - Written - Bovy (UofT).
- 2021-04-19 - Adjusted for non-z-symmetric densities by Bovy (UofT).
"""
return integrate.quad(
lambda x: self._dens(R, x, phi=phi, t=t), -numpy.fabs(z), numpy.fabs(z)
)[0]
@potential_physical_input
@physical_conversion("mass", pop=True)
def mass(self, R, z=None, t=0.0, forceint=False):
"""
Evaluate the mass enclosed.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity, optional
Vertical height up to which to integrate (default: None).
t : float or Quantity, optional
Time (default: 0.0).
forceint : bool, optional
If True, calculate the mass through integration of the density, even if an explicit expression for the mass exists (default: False).
Returns
-------
float or Quantity
Mass enclosed within the spherical shell with radius R if z is None else mass in the slab <R and between -z and z; except: potentials inheriting from EllipsoidalPotential, which if z is None return the mass within the ellipsoidal shell with semi-major axis R.
Notes
-----
- 2014-01-29 - Written - Bovy (IAS)
- 2019-08-15 - Added spherical warning - Bovy (UofT)
- 2021-03-15 - Changed to integrate to spherical shell for z is None slab otherwise - Bovy (UofT)
- 2021-03-18 - Switched to using Gauss' theorem - Bovy (UofT)
"""
from .EllipsoidalPotential import EllipsoidalPotential
if self.isNonAxi and not isinstance(self, EllipsoidalPotential):
raise NotImplementedError(
"mass for non-axisymmetric potentials that are not EllipsoidalPotentials is not currently supported"
)
if self.isNonAxi and isinstance(self, EllipsoidalPotential) and not z is None:
raise NotImplementedError(
"mass for EllipsoidalPotentials is not currently supported for z != None"
)
if not z is None: # Make sure z is positive, bc we integrate from -z to z
z = numpy.fabs(z)
try:
if forceint:
raise AttributeError # Hack!
return self._amp * self._mass(R, z=z, t=t)
except AttributeError:
# Use numerical integration to get the mass, using Gauss' theorem
if z is None: # Within spherical shell
def _integrand(theta):
tz = R * numpy.cos(theta)
tR = R * numpy.sin(theta)
return self.rforce(tR, tz, t=t, use_physical=False) * numpy.sin(
theta
)
return -(R**2.0) * integrate.quad(_integrand, 0.0, numpy.pi)[0] / 2.0
else: # Within disk at <R, -z --> z
return (
-R
* integrate.quad(
lambda x: self.Rforce(R, x, t=t, use_physical=False), -z, z
)[0]
/ 2.0
- integrate.quad(
lambda x: x * self.zforce(x, z, t=t, use_physical=False), 0.0, R
)[0]
)
@physical_conversion("position", pop=True)
def rhalf(self, t=0.0, INF=numpy.inf):
"""
Calculate the half-mass radius, the radius of the spherical shell that contains half the total mass.
Parameters
----------
t : float or Quantity, optional
Time (default: 0.0).
INF : float or Quantity, optional
Radius at which the total mass is calculated (default: numpy.inf).
Returns
-------
float or Quantity
Half-mass radius.
Notes
-----
- 2021-03-18 - Written - Bovy (UofT)
"""
return rhalf(self, t=t, INF=INF, use_physical=False)
@potential_physical_input
@physical_conversion("time", pop=True)
def tdyn(self, R, t=0.0):
"""
Calculate the dynamical time from tdyn^2 = 3pi/[G<rho>]
Parameters
----------
R : float or Quantity
Galactocentric radius.
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
Dynamical time.
Notes
-----
- 2021-03-18 - Written - Bovy (UofT)
"""
return 2.0 * numpy.pi * R * numpy.sqrt(R / self.mass(R, use_physical=False))
@physical_conversion("mass", pop=False)
def mvir(
self,
H=70.0,
Om=0.3,
t=0.0,
overdens=200.0,
wrtcrit=False,
forceint=False,
ro=None,
vo=None,
use_physical=False,
): # use_physical necessary bc of pop=False, does nothing inside
"""
Calculate the virial mass.
Parameters
----------
H : float, optional
Hubble constant in km/s/Mpc (default: 70).
Om : float, optional
Omega matter (default: 0.3).
overdens : float, optional
Overdensity which defines the virial radius (default: 200).
wrtcrit : bool, optional
If True, the overdensity is wrt the critical density rather than the mean matter density (default: False).
ro : float or Quantity, optional
Distance scale in kpc (default: object-wide, which if not set is 8 kpc).
vo : float or Quantity, optional
Velocity scale in km/s (default: object-wide, which if not set is 220 km/s).
forceint : bool, optional
If True, calculate the mass through integration of the density, even if an explicit expression for the mass exists.
Returns
-------
float or Quantity
M(<rvir).
Notes
-----
- 2014-09-12 - Written - Bovy (IAS)
"""
if ro is None:
ro = self._ro
if vo is None:
vo = self._vo
# Evaluate the virial radius
try:
rvir = self.rvir(
H=H,
Om=Om,
t=t,
overdens=overdens,
wrtcrit=wrtcrit,
use_physical=False,
ro=ro,
vo=vo,
)
except AttributeError:
raise AttributeError(
"This potential does not have a '_scale' defined to base the concentration on or does not support calculating the virial radius"
)
return self.mass(rvir, t=t, forceint=forceint, use_physical=False, ro=ro, vo=vo)
@potential_physical_input
@physical_conversion("forcederivative", pop=True)
def R2deriv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the second radial derivative.
Parameters
----------
R : float or Quantity
Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Galactocentric azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2phi/dR2.
Notes
-----
- 2011-10-09 - Written - Bovy (IAS)
"""
try:
return self._amp * self._R2deriv(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
raise PotentialError(
"'_R2deriv' function not implemented for this potential"
)
@potential_physical_input
@physical_conversion("forcederivative", pop=True)
def z2deriv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the second vertical derivative.
Parameters
----------
R : float or Quantity
Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Galactocentric azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2phi/dz2.
Notes
-----
- 2012-07-25 - Written - Bovy (IAS@MPIA)
"""
try:
return self._amp * self._z2deriv(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
raise PotentialError(
"'_z2deriv' function not implemented for this potential"
)
@potential_physical_input
@physical_conversion("forcederivative", pop=True)
def Rzderiv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the mixed R,z derivative.
Parameters
----------
R : float or Quantity
Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Galactocentric azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2phi/dz/dR.
Notes
-----
- 2013-08-26 - Written - Bovy (IAS)
"""
try:
return self._amp * self._Rzderiv(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
raise PotentialError(
"'_Rzderiv' function not implemented for this potential"
)
def normalize(self, norm):
"""
Normalize a potential in such a way that vc(R=1,z=0)=1., or a fraction of this.
Parameters
----------
norm : float
Normalize such that Rforce(R=1,z=0) is such that it is 'norm' of the force necessary to make vc(R=1,z=0)=1 (if True, norm=1).
Returns
-------
None
Notes
-----
- 2010-07-10 - Written - Bovy (NYU)
"""
self._amp *= norm / numpy.fabs(self.Rforce(1.0, 0.0, use_physical=False))
@potential_physical_input
@physical_conversion("energy", pop=True)
def phitorque(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the azimuthal torque.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
tau_phi(R, z, phi, t).
Notes
-----
- 2010-07-10 - Written - Bovy (NYU)
"""
return self._phitorque_nodecorator(R, z, phi=phi, t=t)
def _phitorque_nodecorator(self, R, z, phi=0.0, t=0.0):
# Separate, so it can be used during orbit integration
try:
return self._amp * self._phitorque(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
if self.isNonAxi:
raise PotentialError(
"'_phitorque' function not implemented for this non-axisymmetric potential"
)
return 0.0
@potential_physical_input
@physical_conversion("energy", pop=True)
def phi2deriv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the second azimuthal derivative.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2Phi/dphi2.
Notes
-----
- 2013-09-24 - Written - Bovy (IAS)
"""
try:
return self._amp * self._phi2deriv(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
if self.isNonAxi:
raise PotentialError(
"'_phi2deriv' function not implemented for this non-axisymmetric potential"
)
return 0.0
@potential_physical_input
@physical_conversion("force", pop=True)
def Rphideriv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the mixed radial, azimuthal derivative.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2Phi/dphidR.
Notes
-----
- 2014-06-30 - Written - Bovy (IAS)
"""
try:
return self._amp * self._Rphideriv(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
if self.isNonAxi:
raise PotentialError(
"'_Rphideriv' function not implemented for this non-axisymmetric potential"
)
return 0.0
@potential_physical_input
@physical_conversion("force", pop=True)
def phizderiv(self, R, z, phi=0.0, t=0.0):
"""
Evaluate the mixed azimuthal, vertical derivative.
Parameters
----------
R : float or Quantity
Cylindrical Galactocentric radius.
z : float or Quantity
Vertical height.
phi : float or Quantity, optional
Azimuth (default: 0.0).
t : float or Quantity, optional
Time (default: 0.0).
Returns
-------
float or Quantity
d2Phi/dphidz.
Notes
-----
- 2021-04-30 - Written - Bovy (UofT)
"""
try:
return self._amp * self._phizderiv(R, z, phi=phi, t=t)
except AttributeError: # pragma: no cover
if self.isNonAxi:
raise PotentialError(
"'_phizderiv' function not implemented for this non-axisymmetric potential"
)
return 0.0
def toPlanar(self):
"""
Convert a 3D potential into a planar potential in the mid-plane.
Returns
-------
planarPotential
"""
from ..potential import toPlanarPotential
return toPlanarPotential(self)
def toVertical(self, R, phi=None, t0=0.0):
"""
Convert a 3D potential into a linear (vertical) potential at R.
Parameters
----------
R : float or Quantity
Galactocentric radius at which to create the vertical potential.
phi : float or Quantity, optional
Galactocentric azimuth at which to create the vertical potential; required for non-axisymmetric potential.
t0 : float or Quantity, optional
Time at which to create the vertical potential (default: 0.0)
Returns
-------
linear (vertical) potential : function
Phi(z,phi,t) = Phi(R,z,phi,t)-Phi(R,0.,phi0,t0) where phi0 and t0 are the phi and t inputs.
"""
from ..potential import toVerticalPotential
return toVerticalPotential(self, R, phi=phi, t0=t0)
def plot(
self,
t=0.0,
rmin=0.0,
rmax=1.5,
nrs=21,
zmin=-0.5,
zmax=0.5,
nzs=21,
effective=False,
Lz=None,
phi=None,
xy=False,
xrange=None,
yrange=None,
justcontours=False,
levels=None,
cntrcolors=None,
ncontours=21,
savefilename=None,
):
"""
Plot the potential.
Parameters
----------
t : float, optional
Time to plot potential at. Default is 0.0.
rmin : float or Quantity, optional
Minimum R. Default is 0.0.
rmax : float or Quantity, optional
Maximum R. Default is 1.5.
nrs : int, optional
Grid in R. Default is 21.
zmin : float or Quantity, optional
Minimum z. Default is -0.5.
zmax : float or Quantity, optional
Maximum z. Default is 0.5.
nzs : int, optional
Grid in z. Default is 21.
phi : float or Quantity, optional
Azimuth to use for non-axisymmetric potentials. Default is None.
xy : bool, optional
If True, plot the potential in X-Y. Default is False.
effective : bool, optional
If True, plot the effective potential Phi + Lz^2/2/R^2. Default is False.
Lz : float or Quantity, optional
Angular momentum to use for the effective potential when effective=True. Default is None.
justcontours : bool, optional
If True, just plot contours. Default is False.
savefilename : str, optional
Save to or restore from this savefile (pickle). Default is None.
xrange : list, optional
Can be specified independently from rmin, zmin, etc. Default is None.
yrange : list, optional
Can be specified independently from rmin, zmin, etc. Default is None.
levels : list, optional
Contours to plot. Default is None.
cntrcolors : str or list, optional
Colors of the contours (single color or array with length ncontours). Default is None.
ncontours : int, optional
Number of contours when levels is None. Default is 21.
Returns
-------
galpy.util.plot.dens2d return value
Notes
-----
- 2010-07-09 - Written - Bovy (NYU)
- 2014-04-08 - Added effective= - Bovy (IAS)
"""
if effective and xy:
raise RuntimeError("xy and effective cannot be True at the same time")
rmin = conversion.parse_length(rmin, ro=self._ro)
rmax = conversion.parse_length(rmax, ro=self._ro)
zmin = conversion.parse_length(zmin, ro=self._ro)
zmax = conversion.parse_length(zmax, ro=self._ro)
Lz = conversion.parse_angmom(Lz, ro=self._ro, vo=self._vo)
if xrange is None:
xrange = [rmin, rmax]
if yrange is None:
yrange = [zmin, zmax]
if not savefilename is None and os.path.exists(savefilename):
print("Restoring savefile " + savefilename + " ...")
savefile = open(savefilename, "rb")
potRz = pickle.load(savefile)
Rs = pickle.load(savefile)
zs = pickle.load(savefile)
savefile.close()
else:
if effective and Lz is None:
raise RuntimeError("When effective=True, you need to specify Lz=")
Rs = numpy.linspace(xrange[0], xrange[1], nrs)
zs = numpy.linspace(yrange[0], yrange[1], nzs)
potRz = numpy.zeros((nrs, nzs))
for ii in range(nrs):
for jj in range(nzs):
if xy:
R, phi, z = coords.rect_to_cyl(Rs[ii], zs[jj], 0.0)