-
Notifications
You must be signed in to change notification settings - Fork 95
/
TwoPowerTriaxialPotential.py
564 lines (494 loc) · 19.1 KB
/
TwoPowerTriaxialPotential.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
###############################################################################
# TwoPowerTriaxialPotential.py: General class for triaxial potentials
# derived from densities with two power-laws
#
# amp/[4pia^3]
# rho(r)= ------------------------------------
# (m/a)^\alpha (1+m/a)^(\beta-\alpha)
#
# with
#
# m^2 = x^2 + y^2/b^2 + z^2/c^2
###############################################################################
import numpy
from scipy import special
from ..util import conversion
from .EllipsoidalPotential import EllipsoidalPotential
class TwoPowerTriaxialPotential(EllipsoidalPotential):
"""Class that implements triaxial potentials that are derived from
two-power density models
.. math::
\\rho(x,y,z) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(m/a)^\\alpha\\,(1+m/a)^{\\beta-\\alpha}}
with
.. math::
m^2 = x'^2 + \\frac{y'^2}{b^2}+\\frac{z'^2}{c^2}
and :math:`(x',y',z')` is a rotated frame wrt :math:`(x,y,z)` specified by parameters ``zvec`` and ``pa`` which specify (a) ``zvec``: the location of the :math:`z'` axis in the :math:`(x,y,z)` frame and (b) ``pa``: the position angle of the :math:`x'` axis wrt the :math:`\\tilde{x}` axis, that is, the :math:`x` axis after rotating to ``zvec``.
Note that this general class of potentials does *not* automatically revert to the special TriaxialNFWPotential, TriaxialHernquistPotential, or TriaxialJaffePotential when using their (alpha,beta) values (like TwoPowerSphericalPotential).
"""
def __init__(
self,
amp=1.0,
a=5.0,
alpha=1.5,
beta=3.5,
b=1.0,
c=1.0,
zvec=None,
pa=None,
glorder=50,
normalize=False,
ro=None,
vo=None,
):
"""
Initialize a triaxial two-power-density potential.
Parameters
----------
amp : float or Quantity, optional
Amplitude to be applied to the potential (default: 1); can be a Quantity with units of mass or Gxmass.
a : float or Quantity, optional
Scale radius.
alpha : float, optional
Inner power (0 <= alpha < 3).
beta : float, optional
Outer power ( beta > 2).
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.
glorder : int, optional
If set, compute the relevant force and potential integrals with Gaussian quadrature of this order.
normalize : bool or float, optional
If True, normalize such that vc(1.,0.)=1., or, if given as a number, such that the force is this fraction of the force necessary to make vc(1.,0.)=1.
ro : float or Quantity, optional
Distance scale for translation into internal units (default from configuration file).
vo : float or Quantity, optional
Velocity scale for translation into internal units (default from configuration file).
Notes
-----
- 2016-05-30 - Started - Bovy (UofT)
- 2018-08-07 - Re-written using the general EllipsoidalPotential class - Bovy (UofT)
"""
EllipsoidalPotential.__init__(
self,
amp=amp,
b=b,
c=c,
zvec=zvec,
pa=pa,
glorder=glorder,
ro=ro,
vo=vo,
amp_units="mass",
)
a = conversion.parse_length(a, ro=self._ro)
self.a = a
self._scale = self.a
if beta <= 2.0 or alpha >= 3.0:
raise OSError(
"TwoPowerTriaxialPotential requires 0 <= alpha < 3 and beta > 2"
)
self.alpha = alpha
self.beta = beta
self.betaminusalpha = self.beta - self.alpha
self.twominusalpha = 2.0 - self.alpha
self.threeminusalpha = 3.0 - self.alpha
if self.twominusalpha != 0.0:
self.psi_inf = (
special.gamma(self.beta - 2.0)
* special.gamma(3.0 - self.alpha)
/ special.gamma(self.betaminusalpha)
)
# Adjust amp
self._amp /= 4.0 * numpy.pi * self.a**3
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
): # pragma: no cover
self.normalize(normalize)
return None
def _psi(self, m):
"""\\psi(m) = -\\int_m^\\infty d m^2 \rho(m^2)"""
if self.twominusalpha == 0.0:
return (
-2.0
* self.a**2
* (self.a / m) ** self.betaminusalpha
/ self.betaminusalpha
* special.hyp2f1(
self.betaminusalpha,
self.betaminusalpha,
self.betaminusalpha + 1,
-self.a / m,
)
)
else:
return (
-2.0
* self.a**2
* (
self.psi_inf
- (m / self.a) ** self.twominusalpha
/ self.twominusalpha
* special.hyp2f1(
self.twominusalpha,
self.betaminusalpha,
self.threeminusalpha,
-m / self.a,
)
)
)
def _mdens(self, m):
"""Density as a function of m"""
return (self.a / m) ** self.alpha / (1.0 + m / self.a) ** (self.betaminusalpha)
def _mdens_deriv(self, m):
"""Derivative of the density as a function of m"""
return (
-self._mdens(m) * (self.a * self.alpha + self.beta * m) / m / (self.a + 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.a**self.alpha
* R ** (3.0 - self.alpha)
/ (3.0 - self.alpha)
* self._b
* self._c
* special.hyp2f1(
3.0 - self.alpha, self.betaminusalpha, 4.0 - self.alpha, -R / self.a
)
)
class TriaxialHernquistPotential(EllipsoidalPotential):
"""Class that implements the triaxial Hernquist potential
.. math::
\\rho(x,y,z) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(m/a)\\,(1+m/a)^{3}}
with
.. math::
m^2 = x'^2 + \\frac{y'^2}{b^2}+\\frac{z'^2}{c^2}
and :math:`(x',y',z')` is a rotated frame wrt :math:`(x,y,z)` specified by parameters ``zvec`` and ``pa`` which specify (a) ``zvec``: the location of the :math:`z'` axis in the :math:`(x,y,z)` frame and (b) ``pa``: the position angle of the :math:`x'` axis wrt the :math:`\\tilde{x}` axis, that is, the :math:`x` axis after rotating to ``zvec``.
"""
def __init__(
self,
amp=1.0,
a=2.0,
normalize=False,
b=1.0,
c=1.0,
zvec=None,
pa=None,
glorder=50,
ro=None,
vo=None,
):
"""
Initialize a triaxial two-power-density potential.
Parameters
----------
amp : float or Quantity, optional
Amplitude to be applied to the potential (default: 1); can be a Quantity with units of mass or Gxmass.
a : float or Quantity, optional
Scale radius.
normalize : bool or float, optional
If True, normalize such that vc(1.,0.)=1., or, if given as a number, such that the force is this fraction of the force necessary to make vc(1.,0.)=1.
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.
glorder : int, optional
If set, compute the relevant force and potential integrals with Gaussian quadrature of this order.
ro : float or Quantity, optional
Distance scale for translation into internal units (default from configuration file).
vo : float or Quantity, optional
Velocity scale for translation into internal units (default from configuration file).
Notes
-----
- 2010-07-09 - Written - Bovy (UofT)
- 2018-08-07 - Re-written using the general EllipsoidalPotential class - Bovy (UofT)
"""
EllipsoidalPotential.__init__(
self,
amp=amp,
b=b,
c=c,
zvec=zvec,
pa=pa,
glorder=glorder,
ro=ro,
vo=vo,
amp_units="mass",
)
a = conversion.parse_length(a, ro=self._ro)
self.a = a
self._scale = self.a
# Adjust amp
self.a4 = self.a**4
self._amp /= 4.0 * numpy.pi * self.a**3
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
):
self.normalize(normalize)
self.hasC = not self._glorder is None
self.hasC_dxdv = False
self.hasC_dens = self.hasC # works if mdens is defined, necessary for hasC
return None
def _psi(self, m):
"""\\psi(m) = -\\int_m^\\infty d m^2 \rho(m^2)"""
return -self.a4 / (m + self.a) ** 2.0
def _mdens(self, m):
"""Density as a function of m"""
return self.a4 / m / (m + self.a) ** 3
def _mdens_deriv(self, m):
"""Derivative of the density as a function of m"""
return -self.a4 * (self.a + 4.0 * m) / m**2 / (self.a + m) ** 4
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.a4
/ self.a
/ (1.0 + self.a / R) ** 2.0
/ 2.0
* self._b
* self._c
)
class TriaxialJaffePotential(EllipsoidalPotential):
"""Class that implements the Jaffe potential
.. math::
\\rho(x,y,z) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(m/a)^2\\,(1+m/a)^{2}}
with
.. math::
m^2 = x'^2 + \\frac{y'^2}{b^2}+\\frac{z'^2}{c^2}
and :math:`(x',y',z')` is a rotated frame wrt :math:`(x,y,z)` specified by parameters ``zvec`` and ``pa`` which specify (a) ``zvec``: the location of the :math:`z'` axis in the :math:`(x,y,z)` frame and (b) ``pa``: the position angle of the :math:`x'` axis wrt the :math:`\\tilde{x}` axis, that is, the :math:`x` axis after rotating to ``zvec``.
"""
def __init__(
self,
amp=1.0,
a=2.0,
b=1.0,
c=1.0,
zvec=None,
pa=None,
normalize=False,
glorder=50,
ro=None,
vo=None,
):
"""
Two-power-law triaxial potential
Parameters
----------
amp : float or Quantity, optional
Amplitude to be applied to the potential (default: 1); can be a Quantity with units of mass or Gxmass
a : float or Quantity, optional
Scale radius.
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
glorder : int, optional
If set, compute the relevant force and potential integrals with Gaussian quadrature of this order
normalize : bool or float, optional
If True, normalize such that vc(1.,0.)=1., or, if given as a number, such that the force is this fraction of the force necessary to make vc(1.,0.)=1.
ro : float or Quantity, optional
Distance scale for translation into internal units (default from configuration file).
vo : float or Quantity, optional
Velocity scale for translation into internal units (default from configuration file).
Notes
-----
- 2010-07-09 - Written - Bovy (UofT)
- 2018-08-07 - Re-written using the general EllipsoidalPotential class - Bovy (UofT)
"""
EllipsoidalPotential.__init__(
self,
amp=amp,
b=b,
c=c,
zvec=zvec,
pa=pa,
glorder=glorder,
ro=ro,
vo=vo,
amp_units="mass",
)
a = conversion.parse_length(a, ro=self._ro)
self.a = a
self._scale = self.a
# Adjust amp
self.a2 = self.a**2
self._amp /= 4.0 * numpy.pi * self.a2 * self.a
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
): # pragma: no cover
self.normalize(normalize)
self.hasC = not self._glorder is None
self.hasC_dxdv = False
self.hasC_dens = self.hasC # works if mdens is defined, necessary for hasC
return None
def _psi(self, m):
"""\\psi(m) = -\\int_m^\\infty d m^2 \rho(m^2)"""
return (
2.0
* self.a2
* (1.0 / (1.0 + m / self.a) + numpy.log(1.0 / (1.0 + self.a / m)))
)
def _mdens(self, m):
"""Density as a function of m"""
return self.a2 / m**2 / (1.0 + m / self.a) ** 2
def _mdens_deriv(self, m):
"""Derivative of the density as a function of m"""
return -2.0 * self.a2**2 * (self.a + 2.0 * m) / m**3 / (self.a + m) ** 3
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.a * self.a2 / (1.0 + self.a / R) * self._b * self._c
)
class TriaxialNFWPotential(EllipsoidalPotential):
"""Class that implements the triaxial NFW potential
.. math::
\\rho(x,y,z) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(m/a)\\,(1+m/a)^{2}}
with
.. math::
m^2 = x'^2 + \\frac{y'^2}{b^2}+\\frac{z'^2}{c^2}
and :math:`(x',y',z')` is a rotated frame wrt :math:`(x,y,z)` specified by parameters ``zvec`` and ``pa`` which specify (a) ``zvec``: the location of the :math:`z'` axis in the :math:`(x,y,z)` frame and (b) ``pa``: the position angle of the :math:`x'` axis wrt the :math:`\\tilde{x}` axis, that is, the :math:`x` axis after rotating to ``zvec``.
"""
def __init__(
self,
amp=1.0,
a=2.0,
b=1.0,
c=1.0,
zvec=None,
pa=None,
normalize=False,
conc=None,
mvir=None,
glorder=50,
vo=None,
ro=None,
H=70.0,
Om=0.3,
overdens=200.0,
wrtcrit=False,
):
"""
Initialize a triaxial NFW potential
Parameters
----------
amp : float or Quantity, optional
Amplitude to be applied to the potential (default: 1); can be a Quantity with units of mass or Gxmass
a : float or Quantity, optional
Scale radius.
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
glorder : int, optional
If set, compute the relevant force and potential integrals with Gaussian quadrature of this order
normalize : bool or float, optional
If True, normalize such that vc(1.,0.)=1., or, if given as a number, such that the force is this fraction of the force necessary to make vc(1.,0.)=1.
conc : float, optional
Concentration.
mvir : float, optional
Virial mass in 10^12 Msolar.
H : float, optional
Hubble constant in km/s/Mpc.
Om : float, optional
Omega matter.
overdens : float, optional
Overdensity which defines the virial radius.
wrtcrit : bool, optional
If True, the overdensity is wrt the critical density rather than the mean matter density.
ro : float or Quantity, optional
Distance scale for translation into internal units (default from configuration file).
vo : float or Quantity, optional
Velocity scale for translation into internal units (default from configuration file).
Notes
-----
- Initialize with one of:
* a and amp or normalize
* mvir, conc, H, Om, wrtcrit, overdens.
- 2010-07-09 - Written - Bovy (UofT)
- 2018-08-07 - Re-written using the general EllipsoidalPotential class - Bovy (UofT)
"""
EllipsoidalPotential.__init__(
self,
amp=amp,
b=b,
c=c,
zvec=zvec,
pa=pa,
glorder=glorder,
ro=ro,
vo=vo,
amp_units="mass",
)
a = conversion.parse_length(a, ro=self._ro)
if conc is None:
self.a = a
else:
from ..potential import NFWPotential
dumb = NFWPotential(
mvir=mvir,
conc=conc,
ro=self._ro,
vo=self._vo,
H=H,
Om=Om,
wrtcrit=wrtcrit,
overdens=overdens,
)
self.a = dumb.a
self._amp = dumb._amp
self._scale = self.a
self.hasC = not self._glorder is None
self.hasC_dxdv = False
self.hasC_dens = self.hasC # works if mdens is defined, necessary for hasC
# Adjust amp
self.a3 = self.a**3
self._amp /= 4.0 * numpy.pi * self.a3
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
):
self.normalize(normalize)
return None
def _psi(self, m):
"""\\psi(m) = -\\int_m^\\infty d m^2 \rho(m^2)"""
return -2.0 * self.a3 / (self.a + m)
def _mdens(self, m):
"""Density as a function of m"""
return self.a / m / (1.0 + m / self.a) ** 2
def _mdens_deriv(self, m):
"""Derivative of the density as a function of m"""
return -self.a3 * (self.a + 3.0 * m) / m**2 / (self.a + m) ** 3
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.a3
* self._b
* self._c
* (numpy.log(1 + R / self.a) - R / self.a / (1.0 + R / self.a))
)