-
Notifications
You must be signed in to change notification settings - Fork 95
/
EllipsoidalPotential.py
479 lines (434 loc) · 17.4 KB
/
EllipsoidalPotential.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
###############################################################################
# EllipsoidalPotential.py: base class for potentials corresponding to
# density profiles that are stratified on
# ellipsoids:
#
# \rho(x,y,z) ~ \rho(m)
#
# with m^2 = x^2+y^2/b^2+z^2/c^2
#
###############################################################################
import hashlib
import numpy
from scipy import integrate
from ..util import _rotate_to_arbitrary_vector, conversion, coords
from .Potential import Potential, check_potential_inputs_not_arrays
class EllipsoidalPotential(Potential):
"""Base class for potentials corresponding to density profiles that are stratified on ellipsoids:
.. math::
\\rho(x,y,z) \\equiv \\rho(m^2)
where :math:`m^2 = x^2+y^2/b^2+z^2/c^2`. Note that :math:`b` and :math:`c` are defined to be the axis ratios (rather than using :math:`m^2 = x^2/a^2+y^2/b^2+z^2/c^2` as is common).
Implement a specific density distribution with this form by inheriting from this class and defining functions ``_mdens(self,m)`` (the density as a function of ``m``), ``_mdens_deriv(self,m)`` (the derivative of the density as a function of ``m``), and ``_psi(self,m)``, which is:
.. math::
\\psi(m) = -\\int_{m^2}^\\infty d m^2 \\rho(m^2)
See PerfectEllipsoidPotential for an example and `Merritt & Fridman (1996) <http://adsabs.harvard.edu/abs/1996ApJ...460..136M>`_ for the formalism.
"""
def __init__(
self,
amp=1.0,
b=1.0,
c=1.0,
zvec=None,
pa=None,
glorder=50,
ro=None,
vo=None,
amp_units=None,
):
"""
Initialize an ellipsoidal potential.
Parameters
----------
amp : float or Quantity, optional
Amplitude to be applied to the potential (default: 1); can be a Quantity with units that depend on the specific spheroidal potential.
b : float, optional
y-to-x axis ratio of the density.
c : float, optional
z-to-x axis ratio of the density.
zvec : numpy.ndarray, optional
If set, a unit vector that corresponds to the z axis.
pa : float or Quantity, optional
If set, the position angle of the x axis (rad or Quantity).
glorder : int, optional
If set, compute the relevant force and potential integrals with Gaussian quadrature of this order.
ro : float, optional
Distance scale for translation into internal units (default from configuration file).
vo : float, optional
Velocity scale for translation into internal units (default from configuration file).
amp_units : str, optional
Type of units that amp should have if it has units (passed to Potential.__init__).
Notes
-----
- 2018-08-06 - Started - Bovy (UofT)
"""
Potential.__init__(self, amp=amp, ro=ro, vo=vo, amp_units=amp_units)
# Setup axis ratios
self._b = b
self._c = c
self._b2 = self._b**2.0
self._c2 = self._c**2.0
self._force_hash = None
# Setup rotation
self._setup_zvec_pa(zvec, pa)
# Setup integration
self._setup_gl(glorder)
if not self._aligned or numpy.fabs(self._b - 1.0) > 10.0**-10.0:
self.isNonAxi = True
return None
def _setup_zvec_pa(self, zvec, pa):
if not pa is None:
pa = conversion.parse_angle(pa)
if zvec is None and (pa is None or numpy.fabs(pa) < 10.0**-10.0):
self._aligned = True
else:
self._aligned = False
if not pa is None:
pa_rot = numpy.array(
[
[numpy.cos(pa), numpy.sin(pa), 0.0],
[-numpy.sin(pa), numpy.cos(pa), 0.0],
[0.0, 0.0, 1.0],
]
)
else:
pa_rot = numpy.eye(3)
if not zvec is None:
if not isinstance(zvec, numpy.ndarray):
zvec = numpy.array(zvec)
zvec /= numpy.sqrt(numpy.sum(zvec**2.0))
zvec_rot = _rotate_to_arbitrary_vector(
numpy.array([[0.0, 0.0, 1.0]]), zvec, inv=True
)[0]
else:
zvec_rot = numpy.eye(3)
self._rot = numpy.dot(pa_rot, zvec_rot)
return None
def _setup_gl(self, glorder):
self._glorder = glorder
if self._glorder is None:
self._glx, self._glw = None, None
else:
self._glx, self._glw = numpy.polynomial.legendre.leggauss(self._glorder)
# Interval change
self._glx = 0.5 * self._glx + 0.5
self._glw *= 0.5
return None
@check_potential_inputs_not_arrays
def _evaluate(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if numpy.isinf(R):
y = 0.0
if self._aligned:
return self._evaluate_xyz(x, y, z)
else:
xyzp = numpy.dot(self._rot, numpy.array([x, y, z]))
return self._evaluate_xyz(xyzp[0], xyzp[1], xyzp[2])
def _evaluate_xyz(self, x, y, z):
"""Evaluation of the potential as a function of (x,y,z) in the
aligned coordinate frame"""
return (
2.0
* numpy.pi
* self._b
* self._c
* _potInt(
x, y, z, self._psi, self._b2, self._c2, glx=self._glx, glw=self._glw
)
)
@check_potential_inputs_not_arrays
def _Rforce(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
# Compute all rectangular forces
new_hash = hashlib.md5(numpy.array([x, y, z])).hexdigest()
if new_hash == self._force_hash:
Fx = self._cached_Fx
Fy = self._cached_Fy
Fz = self._cached_Fz
else:
if self._aligned:
xp, yp, zp = x, y, z
else:
xyzp = numpy.dot(self._rot, numpy.array([x, y, z]))
xp, yp, zp = xyzp[0], xyzp[1], xyzp[2]
Fx = self._force_xyz(xp, yp, zp, 0)
Fy = self._force_xyz(xp, yp, zp, 1)
Fz = self._force_xyz(xp, yp, zp, 2)
self._force_hash = new_hash
self._cached_Fx = Fx
self._cached_Fy = Fy
self._cached_Fz = Fz
if not self._aligned:
Fxyz = numpy.dot(self._rot.T, numpy.array([Fx, Fy, Fz]))
Fx, Fy = Fxyz[0], Fxyz[1]
return numpy.cos(phi) * Fx + numpy.sin(phi) * Fy
@check_potential_inputs_not_arrays
def _phitorque(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
# Compute all rectangular forces
new_hash = hashlib.md5(numpy.array([x, y, z])).hexdigest()
if new_hash == self._force_hash:
Fx = self._cached_Fx
Fy = self._cached_Fy
Fz = self._cached_Fz
else:
if self._aligned:
xp, yp, zp = x, y, z
else:
xyzp = numpy.dot(self._rot, numpy.array([x, y, z]))
xp, yp, zp = xyzp[0], xyzp[1], xyzp[2]
Fx = self._force_xyz(xp, yp, zp, 0)
Fy = self._force_xyz(xp, yp, zp, 1)
Fz = self._force_xyz(xp, yp, zp, 2)
self._force_hash = new_hash
self._cached_Fx = Fx
self._cached_Fy = Fy
self._cached_Fz = Fz
if not self._aligned:
Fxyz = numpy.dot(self._rot.T, numpy.array([Fx, Fy, Fz]))
Fx, Fy = Fxyz[0], Fxyz[1]
return R * (-numpy.sin(phi) * Fx + numpy.cos(phi) * Fy)
@check_potential_inputs_not_arrays
def _zforce(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
# Compute all rectangular forces
new_hash = hashlib.md5(numpy.array([x, y, z])).hexdigest()
if new_hash == self._force_hash:
Fx = self._cached_Fx
Fy = self._cached_Fy
Fz = self._cached_Fz
else:
if self._aligned:
xp, yp, zp = x, y, z
else:
xyzp = numpy.dot(self._rot, numpy.array([x, y, z]))
xp, yp, zp = xyzp[0], xyzp[1], xyzp[2]
Fx = self._force_xyz(xp, yp, zp, 0)
Fy = self._force_xyz(xp, yp, zp, 1)
Fz = self._force_xyz(xp, yp, zp, 2)
self._force_hash = new_hash
self._cached_Fx = Fx
self._cached_Fy = Fy
self._cached_Fz = Fz
if not self._aligned:
Fxyz = numpy.dot(self._rot.T, numpy.array([Fx, Fy, Fz]))
Fz = Fxyz[2]
return Fz
def _force_xyz(self, x, y, z, i):
"""Evaluation of the i-th force component as a function of (x,y,z)"""
return (
-4.0
* numpy.pi
* self._b
* self._c
* _forceInt(
x,
y,
z,
lambda m: self._mdens(m),
self._b2,
self._c2,
i,
glx=self._glx,
glw=self._glw,
)
)
@check_potential_inputs_not_arrays
def _R2deriv(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if not self._aligned:
raise NotImplementedError(
"2nd potential derivatives of TwoPowerTriaxialPotential not implemented for rotated coordinated frames (non-trivial zvec and pa); use RotateAndTiltWrapperPotential for this functionality instead"
)
phixx = self._2ndderiv_xyz(x, y, z, 0, 0)
phixy = self._2ndderiv_xyz(x, y, z, 0, 1)
phiyy = self._2ndderiv_xyz(x, y, z, 1, 1)
return (
numpy.cos(phi) ** 2.0 * phixx
+ numpy.sin(phi) ** 2.0 * phiyy
+ 2.0 * numpy.cos(phi) * numpy.sin(phi) * phixy
)
@check_potential_inputs_not_arrays
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if not self._aligned:
raise NotImplementedError(
"2nd potential derivatives of TwoPowerTriaxialPotential not implemented for rotated coordinated frames (non-trivial zvec and pa; use RotateAndTiltWrapperPotential for this functionality instead)"
)
phixz = self._2ndderiv_xyz(x, y, z, 0, 2)
phiyz = self._2ndderiv_xyz(x, y, z, 1, 2)
return numpy.cos(phi) * phixz + numpy.sin(phi) * phiyz
@check_potential_inputs_not_arrays
def _z2deriv(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if not self._aligned:
raise NotImplementedError(
"2nd potential derivatives of TwoPowerTriaxialPotential not implemented for rotated coordinated frames (non-trivial zvec and pa; use RotateAndTiltWrapperPotential for this functionality instead)"
)
return self._2ndderiv_xyz(x, y, z, 2, 2)
@check_potential_inputs_not_arrays
def _phi2deriv(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if not self._aligned:
raise NotImplementedError(
"2nd potential derivatives of TwoPowerTriaxialPotential not implemented for rotated coordinated frames (non-trivial zvec and pa; use RotateAndTiltWrapperPotential for this functionality instead)"
)
Fx = self._force_xyz(x, y, z, 0)
Fy = self._force_xyz(x, y, z, 1)
phixx = self._2ndderiv_xyz(x, y, z, 0, 0)
phixy = self._2ndderiv_xyz(x, y, z, 0, 1)
phiyy = self._2ndderiv_xyz(x, y, z, 1, 1)
return R**2.0 * (
numpy.sin(phi) ** 2.0 * phixx
+ numpy.cos(phi) ** 2.0 * phiyy
- 2.0 * numpy.cos(phi) * numpy.sin(phi) * phixy
) + R * (numpy.cos(phi) * Fx + numpy.sin(phi) * Fy)
@check_potential_inputs_not_arrays
def _Rphideriv(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if not self._aligned:
raise NotImplementedError(
"2nd potential derivatives of TwoPowerTriaxialPotential not implemented for rotated coordinated frames (non-trivial zvec and pa; use RotateAndTiltWrapperPotential for this functionality instead)"
)
Fx = self._force_xyz(x, y, z, 0)
Fy = self._force_xyz(x, y, z, 1)
phixx = self._2ndderiv_xyz(x, y, z, 0, 0)
phixy = self._2ndderiv_xyz(x, y, z, 0, 1)
phiyy = self._2ndderiv_xyz(x, y, z, 1, 1)
return (
R * numpy.cos(phi) * numpy.sin(phi) * (phiyy - phixx)
+ R * numpy.cos(2.0 * phi) * phixy
+ numpy.sin(phi) * Fx
- numpy.cos(phi) * Fy
)
@check_potential_inputs_not_arrays
def _phizderiv(self, R, z, phi=0.0, t=0.0):
if not self.isNonAxi:
phi = 0.0
x, y, z = coords.cyl_to_rect(R, phi, z)
if not self._aligned:
raise NotImplementedError(
"2nd potential derivatives of TwoPowerTriaxialPotential not implemented for rotated coordinated frames (non-trivial zvec and pa; use RotateAndTiltWrapperPotential for this functionality instead)"
)
phixz = self._2ndderiv_xyz(x, y, z, 0, 2)
phiyz = self._2ndderiv_xyz(x, y, z, 1, 2)
return R * (numpy.cos(phi) * phiyz - numpy.sin(phi) * phixz)
def _2ndderiv_xyz(self, x, y, z, i, j):
"""General 2nd derivative of the potential as a function of (x,y,z)
in the aligned coordinate frame"""
return (
4.0
* numpy.pi
* self._b
* self._c
* _2ndDerivInt(
x,
y,
z,
lambda m: self._mdens(m),
lambda m: self._mdens_deriv(m),
self._b2,
self._c2,
i,
j,
glx=self._glx,
glw=self._glw,
)
)
@check_potential_inputs_not_arrays
def _dens(self, R, z, phi=0.0, t=0.0):
x, y, z = coords.cyl_to_rect(R, phi, z)
if self._aligned:
xp, yp, zp = x, y, z
else:
xyzp = numpy.dot(self._rot, numpy.array([x, y, z]))
xp, yp, zp = xyzp[0], xyzp[1], xyzp[2]
m = numpy.sqrt(xp**2.0 + yp**2.0 / self._b2 + zp**2.0 / self._c2)
return self._mdens(m)
def _mass(self, R, z=None, t=0.0):
if not z is None:
raise AttributeError # Hack to fall back to general
return (
4.0
* numpy.pi
* self._b
* self._c
* integrate.quad(lambda m: m**2.0 * self._mdens(m), 0, R)[0]
)
def OmegaP(self):
return 0.0
def _potInt(x, y, z, psi, b2, c2, glx=None, glw=None):
r"""int_0^\infty [psi(m)-psi(\infy)]/sqrt([1+tau]x[b^2+tau]x[c^2+tau])dtau"""
def integrand(s):
t = 1 / s**2.0 - 1.0
return psi(
numpy.sqrt(x**2.0 / (1.0 + t) + y**2.0 / (b2 + t) + z**2.0 / (c2 + t))
) / numpy.sqrt((1.0 + (b2 - 1.0) * s**2.0) * (1.0 + (c2 - 1.0) * s**2.0))
if glx is None:
return integrate.quad(integrand, 0.0, 1.0)[0]
else:
return numpy.sum(glw * integrand(glx))
def _forceInt(x, y, z, dens, b2, c2, i, glx=None, glw=None):
"""Integral that gives the force in x,y,z"""
def integrand(s):
t = 1 / s**2.0 - 1.0
return (
dens(numpy.sqrt(x**2.0 / (1.0 + t) + y**2.0 / (b2 + t) + z**2.0 / (c2 + t)))
* (
x / (1.0 + t) * (i == 0)
+ y / (b2 + t) * (i == 1)
+ z / (c2 + t) * (i == 2)
)
/ numpy.sqrt((1.0 + (b2 - 1.0) * s**2.0) * (1.0 + (c2 - 1.0) * s**2.0))
)
if glx is None:
return integrate.quad(integrand, 0.0, 1.0)[0]
else:
return numpy.sum(glw * integrand(glx))
def _2ndDerivInt(x, y, z, dens, densDeriv, b2, c2, i, j, glx=None, glw=None):
"""Integral that gives the 2nd derivative of the potential in x,y,z"""
def integrand(s):
t = 1 / s**2.0 - 1.0
m = numpy.sqrt(x**2.0 / (1.0 + t) + y**2.0 / (b2 + t) + z**2.0 / (c2 + t))
return (
densDeriv(m)
* (
x / (1.0 + t) * (i == 0)
+ y / (b2 + t) * (i == 1)
+ z / (c2 + t) * (i == 2)
)
* (
x / (1.0 + t) * (j == 0)
+ y / (b2 + t) * (j == 1)
+ z / (c2 + t) * (j == 2)
)
/ m
+ dens(m)
* (i == j)
* (
1.0 / (1.0 + t) * (i == 0)
+ 1.0 / (b2 + t) * (i == 1)
+ 1.0 / (c2 + t) * (i == 2)
)
) / numpy.sqrt((1.0 + (b2 - 1.0) * s**2.0) * (1.0 + (c2 - 1.0) * s**2.0))
if glx is None:
return integrate.quad(integrand, 0.0, 1.0)[0]
else:
return numpy.sum(glw * integrand(glx))