-
Notifications
You must be signed in to change notification settings - Fork 95
/
plotEscapecurve.py
211 lines (187 loc) · 6 KB
/
plotEscapecurve.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
import os
import pickle
import numpy
from ..util import conversion, plot
from ..util.conversion import (
parse_length,
parse_velocity,
physical_conversion,
potential_physical_input,
)
_INF = 10**12.0
def plotEscapecurve(Pot, *args, **kwargs):
"""
Plot the escape velocity curve for this potential (in the z=0 plane for non-spherical potentials).
Parameters
----------
Pot : Potential or list of Potential instances
Potential(s) for which to plot the escape velocity curve.
Rrange : numpy.ndarray or Quantity, optional
Range in R to consider (can be Quantity).
grid : int, optional
Grid in R.
savefilename : str, optional
Save to or restore from this savefile (pickle).
*args, **kwargs : dict
Arguments and keyword arguments for `galpy.util.plot.plot`.
Returns
-------
matplotlib.AxesSubplot
Plot to output device.
Notes
-----
- 2010-08-08 - Written by Bovy (NYU).
"""
# Using physical units or not?
if isinstance(Pot, list):
potro = Pot[0]._ro
roSet = Pot[0]._roSet
potvo = Pot[0]._vo
voSet = Pot[0]._voSet
else:
potro = Pot._ro
roSet = Pot._roSet
potvo = Pot._vo
voSet = Pot._voSet
# Following just to deal with Quantity ro/vo and check whether they are set
_ro = parse_length(kwargs.get("ro", None), ro=potro)
_vo = parse_velocity(kwargs.get("vo", None), vo=potvo)
if (
kwargs.get("use_physical", True)
and (not _ro is None or roSet)
and (not _vo is None or voSet)
):
use_physical = True
potro = kwargs.get("ro", potro)
potvo = kwargs.get("vo", potvo)
xlabel = r"$R\,(\mathrm{kpc})$"
ylabel = r"$v_e(R)\,(\mathrm{km\,s}^{-1})$"
Rrange = kwargs.pop("Rrange", [0.01 * potro, 5.0 * potro])
else:
use_physical = False
xlabel = r"$R/R_0$"
ylabel = r"$v_e(R)/v_c(R_0)$"
Rrange = kwargs.pop("Rrange", [0.01, 5.0])
# Parse ro
potro = conversion.parse_length_kpc(potro)
potvo = conversion.parse_velocity_kms(potvo)
Rrange[0] = conversion.parse_length_kpc(Rrange[0])
Rrange[1] = conversion.parse_length_kpc(Rrange[1])
if use_physical:
Rrange[0] /= potro
Rrange[1] /= potro
grid = kwargs.pop("grid", 1001)
savefilename = kwargs.pop("savefilename", None)
if not savefilename == None and os.path.exists(savefilename):
print("Restoring savefile " + savefilename + " ...")
savefile = open(savefilename, "rb")
esccurve = pickle.load(savefile)
Rs = pickle.load(savefile)
savefile.close()
else:
Rs = numpy.linspace(Rrange[0], Rrange[1], grid)
esccurve = calcEscapecurve(Pot, Rs)
if not savefilename == None:
print("Writing savefile " + savefilename + " ...")
savefile = open(savefilename, "wb")
pickle.dump(esccurve, savefile)
pickle.dump(Rs, savefile)
savefile.close()
if use_physical:
Rs *= potro
esccurve *= potvo
Rrange[0] *= potro
Rrange[1] *= potro
if not "xlabel" in kwargs:
kwargs["xlabel"] = xlabel
if not "ylabel" in kwargs:
kwargs["ylabel"] = ylabel
if not "xrange" in kwargs:
kwargs["xrange"] = Rrange
if not "yrange" in kwargs:
kwargs["yrange"] = [
0.0,
1.2 * numpy.amax(esccurve[True ^ numpy.isnan(esccurve)]),
]
kwargs.pop("ro", None)
kwargs.pop("vo", None)
kwargs.pop("use_physical", None)
return plot.plot(Rs, esccurve, *args, **kwargs)
def calcEscapecurve(Pot, Rs, t=0.0):
"""
Calculate the escape velocity curve for this potential (in the z=0 plane for non-spherical potentials).
Parameters
----------
Pot : Potential or list of Potential instances
Potential or list of Potential instances.
Rs : numpy.ndarray or Quantity
Radius(i).
t : float, optional
Instantaneous time (default is 0.0).
Returns
-------
numpy.ndarray or Quantity
Array of v_esc.
Raises
------
AttributeError
Escape velocity curve plotting for non-axisymmetric potentials is not currently supported.
Notes
-----
- 2011-04-16 - Written - Bovy (NYU)
"""
isList = isinstance(Pot, list)
isNonAxi = (isList and Pot[0].isNonAxi) or (not isList and Pot.isNonAxi)
if isNonAxi:
raise AttributeError(
"Escape velocity curve plotting for non-axisymmetric potentials is not currently supported"
)
try:
grid = len(Rs)
except TypeError:
grid = 1
Rs = numpy.array([Rs])
esccurve = numpy.zeros(grid)
for ii in range(grid):
esccurve[ii] = vesc(Pot, Rs[ii], t=t, use_physical=False)
return esccurve
@potential_physical_input
@physical_conversion("velocity", pop=True)
def vesc(Pot, R, t=0.0):
"""
Calculate the escape velocity at R for potential Pot.
Parameters
----------
Pot : Potential or list of Potential instances
Potential or list of Potential instances.
R : numpy.ndarray or Quantity
Galactocentric radius.
t : float, optional
Time (default is 0.0).
Returns
-------
numpy.ndarray or Quantity
Escape velocity.
Notes
-----
- 2011-10-09 - Written - Bovy (IAS)
"""
from ..potential import PotentialError, evaluateplanarPotentials
try:
return numpy.sqrt(
2.0
* (
evaluateplanarPotentials(Pot, _INF, t=t, use_physical=False)
- evaluateplanarPotentials(Pot, R, t=t, use_physical=False)
)
)
except PotentialError:
from ..potential import RZToplanarPotential
Pot = RZToplanarPotential(Pot)
return numpy.sqrt(
2.0
* (
evaluateplanarPotentials(Pot, _INF, t=t, use_physical=False)
- evaluateplanarPotentials(Pot, R, t=t, use_physical=False)
)
)