-
Notifications
You must be signed in to change notification settings - Fork 95
/
TwoPowerSphericalPotential.py
1092 lines (963 loc) · 37.1 KB
/
TwoPowerSphericalPotential.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
###############################################################################
# TwoPowerSphericalPotential.py: General class for potentials derived from
# densities with two power-laws
#
# amp
# rho(r)= ------------------------------------
# (r/a)^\alpha (1+r/a)^(\beta-\alpha)
###############################################################################
import numpy
from scipy import optimize, special
from ..util import conversion
from ..util._optional_deps import _APY_LOADED, _JAX_LOADED
from .Potential import Potential, kms_to_kpcGyrDecorator
if _APY_LOADED:
from astropy import units
if _JAX_LOADED:
import jax.numpy as jnp
class TwoPowerSphericalPotential(Potential):
"""Class that implements spherical potentials that are derived from
two-power density models
.. math::
\\rho(r) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(r/a)^\\alpha\\,(1+r/a)^{\\beta-\\alpha}}
"""
def __init__(
self, amp=1.0, a=5.0, alpha=1.5, beta=3.5, normalize=False, ro=None, vo=None
):
"""
Initialize a 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.
beta : float, optional
Outer power.
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
-----
- Started - 2010-07-09 - Bovy (NYU)
"""
# Instantiate
Potential.__init__(self, amp=amp, ro=ro, vo=vo, amp_units="mass")
# _specialSelf for special cases (Dehnen class, Dehnen core, Hernquist, Jaffe, NFW)
self._specialSelf = None
if (
(self.__class__ == TwoPowerSphericalPotential)
& (alpha == round(alpha))
& (beta == round(beta))
):
if int(alpha) == 0 and int(beta) == 4:
self._specialSelf = DehnenCoreSphericalPotential(
amp=1.0, a=a, normalize=False
)
elif int(alpha) == 1 and int(beta) == 4:
self._specialSelf = HernquistPotential(amp=1.0, a=a, normalize=False)
elif int(alpha) == 2 and int(beta) == 4:
self._specialSelf = JaffePotential(amp=1.0, a=a, normalize=False)
elif int(alpha) == 1 and int(beta) == 3:
self._specialSelf = NFWPotential(amp=1.0, a=a, normalize=False)
# correcting quantities
a = conversion.parse_length(a, ro=self._ro)
# setting properties
self.a = a
self._scale = self.a
self.alpha = alpha
self.beta = beta
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
): # pragma: no cover
self.normalize(normalize)
return None
def _evaluate(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._evaluate(R, z, phi=phi, t=t)
elif self.beta == 3.0:
r = numpy.sqrt(R**2.0 + z**2.0)
return (
(1.0 / self.a)
* (
r
- self.a
* (r / self.a) ** (3.0 - self.alpha)
/ (3.0 - self.alpha)
* special.hyp2f1(
3.0 - self.alpha,
2.0 - self.alpha,
4.0 - self.alpha,
-r / self.a,
)
)
/ (self.alpha - 2.0)
/ r
)
else:
r = numpy.sqrt(R**2.0 + z**2.0)
return (
special.gamma(self.beta - 3.0)
* (
(r / self.a) ** (3.0 - self.beta)
/ special.gamma(self.beta - 1.0)
* special.hyp2f1(
self.beta - 3.0,
self.beta - self.alpha,
self.beta - 1.0,
-self.a / r,
)
- special.gamma(3.0 - self.alpha)
/ special.gamma(self.beta - self.alpha)
)
/ r
)
def _Rforce(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._Rforce(R, z, phi=phi, t=t)
else:
r = numpy.sqrt(R**2.0 + z**2.0)
return (
-R
/ r**self.alpha
* self.a ** (self.alpha - 3.0)
/ (3.0 - self.alpha)
* special.hyp2f1(
3.0 - self.alpha,
self.beta - self.alpha,
4.0 - self.alpha,
-r / self.a,
)
)
def _zforce(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._zforce(R, z, phi=phi, t=t)
else:
r = numpy.sqrt(R**2.0 + z**2.0)
return (
-z
/ r**self.alpha
* self.a ** (self.alpha - 3.0)
/ (3.0 - self.alpha)
* special.hyp2f1(
3.0 - self.alpha,
self.beta - self.alpha,
4.0 - self.alpha,
-r / self.a,
)
)
def _dens(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
return (
(self.a / r) ** self.alpha
/ (1.0 + r / self.a) ** (self.beta - self.alpha)
/ 4.0
/ numpy.pi
/ self.a**3.0
)
def _ddensdr(self, r, t=0.0):
return (
-self._amp
* (self.a / r) ** (self.alpha - 1.0)
* (1.0 + r / self.a) ** (self.alpha - self.beta - 1.0)
* (self.a * self.alpha + r * self.beta)
/ r**2
/ 4.0
/ numpy.pi
/ self.a**3.0
)
def _d2densdr2(self, r, t=0.0):
return (
self._amp
* (self.a / r) ** (self.alpha - 2.0)
* (1.0 + r / self.a) ** (self.alpha - self.beta - 2.0)
* (
self.alpha * (self.alpha + 1.0) * self.a**2
+ 2.0 * self.alpha * self.a * (self.beta + 1.0) * r
+ self.beta * (self.beta + 1.0) * r**2
)
/ r**4
/ 4.0
/ numpy.pi
/ self.a**3.0
)
def _ddenstwobetadr(self, r, beta=0):
"""
Evaluate the radial density derivative x r^(2beta) for this potential.
Parameters
----------
r : float
Spherical radius.
beta : float, optional
Power of r in the density derivative. Default is 0.
Returns
-------
float
The derivative of the density times r^(2beta).
Notes
-----
- 2021-02-14 - Written - Bovy (UofT)
"""
return (
self._amp
/ 4.0
/ numpy.pi
/ self.a**3.0
* r ** (2.0 * beta - 2.0)
* (self.a / r) ** (self.alpha - 1.0)
* (1.0 + r / self.a) ** (self.alpha - self.beta - 1.0)
* (self.a * (2.0 * beta - self.alpha) + r * (2.0 * beta - self.beta))
)
def _R2deriv(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
A = self.a ** (self.alpha - 3.0) / (3.0 - self.alpha)
hyper = special.hyp2f1(
3.0 - self.alpha, self.beta - self.alpha, 4.0 - self.alpha, -r / self.a
)
hyper_deriv = (
(3.0 - self.alpha)
* (self.beta - self.alpha)
/ (4.0 - self.alpha)
* special.hyp2f1(
4.0 - self.alpha,
1.0 + self.beta - self.alpha,
5.0 - self.alpha,
-r / self.a,
)
)
term1 = A * r ** (-self.alpha) * hyper
term2 = -self.alpha * A * R**2.0 * r ** (-self.alpha - 2.0) * hyper
term3 = -A * R**2 * r ** (-self.alpha - 1.0) / self.a * hyper_deriv
return term1 + term2 + term3
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
A = self.a ** (self.alpha - 3.0) / (3.0 - self.alpha)
hyper = special.hyp2f1(
3.0 - self.alpha, self.beta - self.alpha, 4.0 - self.alpha, -r / self.a
)
hyper_deriv = (
(3.0 - self.alpha)
* (self.beta - self.alpha)
/ (4.0 - self.alpha)
* special.hyp2f1(
4.0 - self.alpha,
1.0 + self.beta - self.alpha,
5.0 - self.alpha,
-r / self.a,
)
)
term1 = -self.alpha * A * R * r ** (-self.alpha - 2.0) * z * hyper
term2 = -A * R * r ** (-self.alpha - 1.0) * z / self.a * hyper_deriv
return term1 + term2
def _z2deriv(self, R, z, phi=0.0, t=0.0):
return self._R2deriv(numpy.fabs(z), R) # Spherical potential
def _mass(self, R, z=None, t=0.0):
if z is not None:
raise AttributeError # use general implementation
return (
(R / self.a) ** (3.0 - self.alpha)
/ (3.0 - self.alpha)
* special.hyp2f1(
3.0 - self.alpha, -self.alpha + self.beta, 4.0 - self.alpha, -R / self.a
)
)
class DehnenSphericalPotential(TwoPowerSphericalPotential):
"""Class that implements the Dehnen Spherical Potential from `Dehnen (1993) <https://ui.adsabs.harvard.edu/abs/1993MNRAS.265..250D>`_
.. math::
\\rho(r) = \\frac{\\mathrm{amp}(3-\\alpha)}{4\\,\\pi\\,a^3}\\,\\frac{1}{(r/a)^{\\alpha}\\,(1+r/a)^{4-\\alpha}}
"""
def __init__(self, amp=1.0, a=1.0, alpha=1.5, normalize=False, ro=None, vo=None):
"""
Initialize a Dehnen Spherical 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, restricted to [0, 3).
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
-----
- Started - Starkman (UofT) - 2019-10-07
"""
if (alpha < 0.0) or (alpha >= 3.0):
raise OSError("DehnenSphericalPotential requires 0 <= alpha < 3")
# instantiate
TwoPowerSphericalPotential.__init__(
self, amp=amp, a=a, alpha=alpha, beta=4, normalize=normalize, ro=ro, vo=vo
)
# make special-self and protect subclasses
self._specialSelf = None
if (self.__class__ == DehnenSphericalPotential) & (alpha == round(alpha)):
if round(alpha) == 0:
self._specialSelf = DehnenCoreSphericalPotential(
amp=1.0, a=a, normalize=False
)
elif round(alpha) == 1:
self._specialSelf = HernquistPotential(amp=1.0, a=a, normalize=False)
elif round(alpha) == 2:
self._specialSelf = JaffePotential(amp=1.0, a=a, normalize=False)
# set properties
self.hasC = True
self.hasC_dxdv = True
self.hasC_dens = True
return None
def _evaluate(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._evaluate(R, z, phi=phi, t=t)
else: # valid for alpha != 2, 3
r = numpy.sqrt(R**2.0 + z**2.0)
return -(1.0 - 1.0 / (1.0 + self.a / r) ** (2.0 - self.alpha)) / (
self.a * (2.0 - self.alpha) * (3.0 - self.alpha)
)
def _Rforce(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._Rforce(R, z, phi=phi, t=t)
else:
r = numpy.sqrt(R**2.0 + z**2.0)
return (
-R
/ r**self.alpha
* (self.a + r) ** (self.alpha - 3.0)
/ (3.0 - self.alpha)
)
def _R2deriv(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._R2deriv(R, z, phi=phi, t=t)
a, alpha = self.a, self.alpha
r = numpy.sqrt(R**2.0 + z**2.0)
# formula not valid for alpha=2,3, (integers?)
return (
numpy.power(r, -2.0 - alpha)
* numpy.power(r + a, alpha - 4.0)
* (-a * r**2.0 + (2.0 * R**2.0 - z**2.0) * r + a * alpha * R**2.0)
/ (alpha - 3.0)
)
def _zforce(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._zforce(R, z, phi=phi, t=t)
else:
r = numpy.sqrt(R**2.0 + z**2.0)
return (
-z
/ r**self.alpha
* (self.a + r) ** (self.alpha - 3.0)
/ (3.0 - self.alpha)
)
def _z2deriv(self, R, z, phi=0.0, t=0.0):
return self._R2deriv(z, R, phi=phi, t=t)
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
if self._specialSelf is not None:
return self._specialSelf._Rzderiv(R, z, phi=phi, t=t)
a, alpha = self.a, self.alpha
r = numpy.sqrt(R**2.0 + z**2.0)
return (
R
* z
* numpy.power(r, -2.0 - alpha)
* numpy.power(a + r, alpha - 4.0)
* (3 * r + a * alpha)
) / (alpha - 3)
def _dens(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
return (
(self.a / r) ** self.alpha
/ (1.0 + r / self.a) ** (4.0 - self.alpha)
/ 4.0
/ numpy.pi
/ self.a**3.0
)
def _mass(self, R, z=None, t=0.0):
if z is not None:
raise AttributeError # use general implementation
return (
1.0 / (1.0 + self.a / R) ** (3.0 - self.alpha) / (3.0 - self.alpha)
) # written so it works for r=numpy.inf
class DehnenCoreSphericalPotential(DehnenSphericalPotential):
"""Class that implements the Dehnen Spherical Potential from `Dehnen (1993) <https://ui.adsabs.harvard.edu/abs/1993MNRAS.265..250D>`_ with alpha=0 (corresponding to an inner core)
.. math::
\\rho(r) = \\frac{\\mathrm{amp}}{12\\,\\pi\\,a^3}\\,\\frac{1}{(1+r/a)^{4}}
"""
def __init__(self, amp=1.0, a=1.0, normalize=False, ro=None, vo=None):
"""
Initialize a cored Dehnen Spherical Potential; note that the amplitude definition used here does NOT match that of Dehnen (1993)
Parameters
----------
amp : float, 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.
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
-----
- 2019-10-07 - Started - Starkman (UofT)
"""
DehnenSphericalPotential.__init__(
self, amp=amp, a=a, alpha=0, normalize=normalize, ro=ro, vo=vo
)
# set properties explicitly
self.hasC = True
self.hasC_dxdv = True
self.hasC_dens = True
return None
def _evaluate(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
return -(1.0 - 1.0 / (1.0 + self.a / r) ** 2.0) / (6.0 * self.a)
def _Rforce(self, R, z, phi=0.0, t=0.0):
return -R / numpy.power(numpy.sqrt(R**2.0 + z**2.0) + self.a, 3.0) / 3.0
def _rforce_jax(self, r):
# No need for actual JAX!
return -self._amp * r / (r + self.a) ** 3.0 / 3.0
def _R2deriv(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
return -(
((2.0 * R**2.0 - z**2.0) - self.a * r)
/ (3.0 * r * numpy.power(r + self.a, 4.0))
)
def _zforce(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
return -z / numpy.power(self.a + r, 3.0) / 3.0
def _z2deriv(self, R, z, phi=0.0, t=0.0):
return self._R2deriv(z, R, phi=phi, t=t)
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
a = self.a
r = numpy.sqrt(R**2.0 + z**2.0)
return -(R * z / r / numpy.power(a + r, 4.0))
def _dens(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
return 1.0 / (1.0 + r / self.a) ** 4.0 / 4.0 / numpy.pi / self.a**3.0
def _mass(self, R, z=None, t=0.0):
if z is not None:
raise AttributeError # use general implementation
return (
1.0 / (1.0 + self.a / R) ** 3.0 / 3.0
) # written so it works for r=numpy.inf
class HernquistPotential(DehnenSphericalPotential):
"""Class that implements the Hernquist potential
.. math::
\\rho(r) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(r/a)\\,(1+r/a)^{3}}
"""
def __init__(self, amp=1.0, a=1.0, normalize=False, ro=None, vo=None):
"""
Initialize a Two Power Spherical 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 (note that amp is 2 x [total mass] for the chosen definition of the Two Power Spherical potential).
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.
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 (NYU).
"""
DehnenSphericalPotential.__init__(
self, amp=amp, a=a, alpha=1, normalize=normalize, ro=ro, vo=vo
)
self._nemo_accname = "Dehnen"
# set properties explicitly
self.hasC = True
self.hasC_dxdv = True
self.hasC_dens = True
return None
def _evaluate(self, R, z, phi=0.0, t=0.0):
return -1.0 / (1.0 + numpy.sqrt(R**2.0 + z**2.0) / self.a) / 2.0 / self.a
def _Rforce(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return -R / self.a / sqrtRz / (1.0 + sqrtRz / self.a) ** 2.0 / 2.0 / self.a
def _zforce(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return -z / self.a / sqrtRz / (1.0 + sqrtRz / self.a) ** 2.0 / 2.0 / self.a
def _rforce_jax(self, r):
# No need for actual JAX!
return -self._amp / 2.0 / (r + self.a) ** 2.0
def _R2deriv(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return (
(self.a * z**2.0 + (z**2.0 - 2.0 * R**2.0) * sqrtRz)
/ sqrtRz**3.0
/ (self.a + sqrtRz) ** 3.0
/ 2.0
)
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return (
-R
* z
* (self.a + 3.0 * sqrtRz)
* (sqrtRz * (self.a + sqrtRz)) ** -3.0
/ 2.0
)
def _surfdens(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
Rma = numpy.sqrt(R**2.0 - self.a**2.0 + 0j)
if Rma == 0.0:
return (
(
-12.0 * self.a**3
- 5.0 * self.a * z**2
+ numpy.sqrt(1.0 + z**2 / self.a**2)
* (12.0 * self.a**3 - self.a * z**2 + 2 / self.a * z**4)
)
/ 30.0
/ numpy.pi
* z**-5.0
)
else:
return (
self.a
* (
(2.0 * self.a**2.0 + R**2.0)
* Rma**-5
* (numpy.arctan(z / Rma) - numpy.arctan(self.a * z / r / Rma))
+ z
* (
5.0 * self.a**3.0 * r
- 4.0 * self.a**4
+ self.a**2 * (2.0 * r**2.0 + R**2)
- self.a * r * (5.0 * R**2.0 + 3.0 * z**2.0)
+ R**2.0 * r**2.0
)
/ (self.a**2.0 - R**2.0) ** 2.0
/ (r**2 - self.a**2.0) ** 2.0
).real
/ 4.0
/ numpy.pi
)
def _mass(self, R, z=None, t=0.0):
if z is not None:
raise AttributeError # use general implementation
return (
1.0 / (1.0 + self.a / R) ** 2.0 / 2.0
) # written so it works for r=numpy.inf
@kms_to_kpcGyrDecorator
def _nemo_accpars(self, vo, ro):
"""
Return the accpars potential parameters for use of this potential with NEMO.
Parameters
----------
vo : float
Velocity unit in km/s.
ro : float
Length unit in kpc.
Returns
-------
str
accpars string.
Notes
-----
- 2018-09-14 - Written - Bovy (UofT)
"""
GM = self._amp * vo**2.0 * ro / 2.0
return f"0,1,{GM},{self.a*ro},0"
class JaffePotential(DehnenSphericalPotential):
"""Class that implements the Jaffe potential
.. math::
\\rho(r) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(r/a)^2\\,(1+r/a)^{2}}
"""
def __init__(self, amp=1.0, a=1.0, normalize=False, ro=None, vo=None):
"""
Initialize a Jaffe 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 (can be Quantity).
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 (NYU)
"""
Potential.__init__(self, amp=amp, ro=ro, vo=vo, amp_units="mass")
a = conversion.parse_length(a, ro=self._ro)
self.a = a
self._scale = self.a
self.alpha = 2
self.beta = 4
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
): # pragma: no cover
self.normalize(normalize)
self.hasC = True
self.hasC_dxdv = True
self.hasC_dens = True
return None
def _evaluate(self, R, z, phi=0.0, t=0.0):
return -numpy.log(1.0 + self.a / numpy.sqrt(R**2.0 + z**2.0)) / self.a
def _Rforce(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return -R / sqrtRz**3.0 / (1.0 + self.a / sqrtRz)
def _zforce(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return -z / sqrtRz**3.0 / (1.0 + self.a / sqrtRz)
def _R2deriv(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return (
(self.a * (z**2.0 - R**2.0) + (z**2.0 - 2.0 * R**2.0) * sqrtRz)
/ sqrtRz**4.0
/ (self.a + sqrtRz) ** 2.0
)
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
sqrtRz = numpy.sqrt(R**2.0 + z**2.0)
return (
-R
* z
* (2.0 * self.a + 3.0 * sqrtRz)
* sqrtRz**-4.0
* (self.a + sqrtRz) ** -2.0
)
def _surfdens(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
Rma = numpy.sqrt(R**2.0 - self.a**2.0 + 0j)
if Rma == 0.0:
return (
(
3.0 * z**2.0
- 2.0 * self.a**2.0
+ 2.0
* numpy.sqrt(1.0 + (z / self.a) ** 2.0)
* (self.a**2.0 - 2.0 * z**2.0)
+ 3.0 * z**3.0 / self.a * numpy.arctan(z / self.a)
)
/ self.a
/ z**3.0
/ 6.0
/ numpy.pi
)
else:
return (
(
(2.0 * self.a**2.0 - R**2.0)
* Rma**-3
* (numpy.arctan(z / Rma) - numpy.arctan(self.a * z / r / Rma))
+ numpy.arctan(z / R) / R
- self.a * z / (R**2 - self.a**2) / (r + self.a)
).real
/ self.a
/ 2.0
/ numpy.pi
)
def _mass(self, R, z=None, t=0.0):
if z is not None:
raise AttributeError # use general implementation
return 1.0 / (1.0 + self.a / R) # written so it works for r=numpy.inf
class NFWPotential(TwoPowerSphericalPotential):
"""Class that implements the NFW potential
.. math::
\\rho(r) = \\frac{\\mathrm{amp}}{4\\,\\pi\\,a^3}\\,\\frac{1}{(r/a)\\,(1+r/a)^{2}}
"""
def __init__(
self,
amp=1.0,
a=1.0,
normalize=False,
rmax=None,
vmax=None,
conc=None,
mvir=None,
vo=None,
ro=None,
H=70.0,
Om=0.3,
overdens=200.0,
wrtcrit=False,
):
"""
Initialize a 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 (can be Quantity).
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.
rmax : float or Quantity, optional
Radius where the rotation curve peak.
vmax : float or Quantity, optional
Maximum circular velocity.
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
* rmax and vmax
* conc, mvir, H, Om, overdens, wrtcrit
- 2010-07-09 - Written - Bovy (NYU)
- 2014-04-03 - Initialization w/ concentration and mass - Bovy (IAS)
- 2020-04-29 - Initialization w/ rmax and vmax - Bovy (UofT)
"""
Potential.__init__(self, amp=amp, ro=ro, vo=vo, amp_units="mass")
a = conversion.parse_length(a, ro=self._ro)
if conc is None and rmax is None:
self.a = a
self.alpha = 1
self.beta = 3
if normalize or (
isinstance(normalize, (int, float)) and not isinstance(normalize, bool)
):
self.normalize(normalize)
elif not rmax is None:
if _APY_LOADED and isinstance(rmax, units.Quantity):
rmax = conversion.parse_length(rmax, ro=self._ro)
self._roSet = True
if _APY_LOADED and isinstance(vmax, units.Quantity):
vmax = conversion.parse_velocity(vmax, vo=self._vo)
self._voSet = True
self.a = rmax / 2.1625815870646098349
self._amp = vmax**2.0 * self.a / 0.21621659550187311005
else:
if wrtcrit:
od = overdens / conversion.dens_in_criticaldens(self._vo, self._ro, H=H)
else:
od = overdens / conversion.dens_in_meanmatterdens(
self._vo, self._ro, H=H, Om=Om
)
mvirNatural = mvir * 100.0 / conversion.mass_in_1010msol(self._vo, self._ro)
rvir = (3.0 * mvirNatural / od / 4.0 / numpy.pi) ** (1.0 / 3.0)
self.a = rvir / conc
self._amp = mvirNatural / (numpy.log(1.0 + conc) - conc / (1.0 + conc))
# Turn on physical output, because mass is given in 1e12 Msun (see #465)
self._roSet = True
self._voSet = True
self._scale = self.a
self.hasC = True
self.hasC_dxdv = True
self.hasC_dens = True
self._nemo_accname = "NFW"
return None
def _evaluate(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
if isinstance(r, (float, int)) and r == 0:
return -1.0 / self.a
elif isinstance(r, (float, int)):
return -special.xlogy(1.0 / r, 1.0 + r / self.a) # stable as r -> infty
else:
out = -special.xlogy(1.0 / r, 1.0 + r / self.a) # stable as r -> infty
out[r == 0] = -1.0 / self.a
return out
def _Rforce(self, R, z, phi=0.0, t=0.0):
Rz = R**2.0 + z**2.0
sqrtRz = numpy.sqrt(Rz)
return R * (
1.0 / Rz / (self.a + sqrtRz)
- numpy.log(1.0 + sqrtRz / self.a) / sqrtRz / Rz
)
def _zforce(self, R, z, phi=0.0, t=0.0):
Rz = R**2.0 + z**2.0
sqrtRz = numpy.sqrt(Rz)
return z * (
1.0 / Rz / (self.a + sqrtRz)
- numpy.log(1.0 + sqrtRz / self.a) / sqrtRz / Rz
)
def _rforce_jax(self, r):
if not _JAX_LOADED: # pragma: no cover
raise ImportError(
"Making use of _rforce_jax function requires the google/jax library"
)
return self._amp * (1.0 / r / (self.a + r) - jnp.log(1.0 + r / self.a) / r**2.0)
def _R2deriv(self, R, z, phi=0.0, t=0.0):
Rz = R**2.0 + z**2.0
sqrtRz = numpy.sqrt(Rz)
return (
(
3.0 * R**4.0
+ 2.0 * R**2.0 * (z**2.0 + self.a * sqrtRz)
- z**2.0 * (z**2.0 + self.a * sqrtRz)
- (2.0 * R**2.0 - z**2.0)
* (self.a**2.0 + R**2.0 + z**2.0 + 2.0 * self.a * sqrtRz)
* numpy.log(1.0 + sqrtRz / self.a)
)
/ Rz**2.5
/ (self.a + sqrtRz) ** 2.0
)
def _Rzderiv(self, R, z, phi=0.0, t=0.0):
Rz = R**2.0 + z**2.0
sqrtRz = numpy.sqrt(Rz)
return (
-R
* z
* (
-4.0 * Rz
- 3.0 * self.a * sqrtRz
+ 3.0
* (self.a**2.0 + Rz + 2.0 * self.a * sqrtRz)
* numpy.log(1.0 + sqrtRz / self.a)
)
* Rz**-2.5
* (self.a + sqrtRz) ** -2.0
)
def _surfdens(self, R, z, phi=0.0, t=0.0):
r = numpy.sqrt(R**2.0 + z**2.0)
Rma = numpy.sqrt(R**2.0 - self.a**2.0 + 0j)
if Rma == 0.0:
za2 = (z / self.a) ** 2
return (
self.a
* (2.0 + numpy.sqrt(za2 + 1.0) * (za2 - 2.0))
/ 6.0
/ numpy.pi
/ z**3
)
else:
return (
(
self.a
* Rma**-3
* (numpy.arctan(self.a * z / r / Rma) - numpy.arctan(z / Rma))
+ z / (r + self.a) / (R**2.0 - self.a**2.0)
).real
/ 2.0
/ numpy.pi
)
def _mass(self, R, z=None, t=0.0):
if z is not None:
raise AttributeError # use general implementation
return numpy.log(1 + R / self.a) - R / self.a / (1.0 + R / self.a)
@conversion.physical_conversion("position", pop=False)
def rvir(
self,
H=70.0,
Om=0.3,
t=0.0,
overdens=200.0,
wrtcrit=False,
ro=None,
vo=None,
use_physical=False,
): # use_physical necessary bc of pop=False, does nothing inside
"""
Calculate the virial radius for this density distribution.
Parameters
----------
H : float, optional
Hubble constant in km/s/Mpc. Default is 70.0.
Om : float, optional
Omega matter. Default is 0.3.
t : float, optional
Time. Default is 0.0.
overdens : float, optional
Overdensity which defines the virial radius. Default is 200.0.
wrtcrit : bool, optional
If True, the overdensity is wrt the critical density rather than the mean matter density. Default is False.
ro : float or Quantity, optional
Distance scale for translation into internal units (default is the object-wide value).