This repository has been archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSO_properties.py
1562 lines (1380 loc) · 52.9 KB
/
SO_properties.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
#!/bin/env python
import numpy as np
import unyt
from scipy.optimize import brentq
from halo_properties import HaloProperty, ReadRadiusTooSmallError
from kinematic_properties import (
get_velocity_dispersion_matrix,
get_angular_momentum,
get_angular_momentum_and_kappa_corot,
get_vmax,
get_axis_lengths,
)
from recently_heated_gas_filter import RecentlyHeatedGasFilter
from property_table import PropertyTable
from dataset_names import mass_dataset
from lazy_properties import lazy_property
from category_filter import CategoryFilter
# index of elements O and Fe in the SmoothedElementMassFractions dataset
indexO = 4
indexFe = 8
def cumulative_mass_intersection(r, rho_dim, slope_dim):
"""
Function used to find the intersection of the cumulative mass curve at fixed
mean density, and the actual cumulative mass as obtained from linear
interpolation on a cumulative mass profile.
The equation we want to solve is:
4*pi/3*rho * r^3 - (M2-M1)/(r2-r1) * r + (M2-M1)/(r2-r1)*r1 - M1 = 0
Since all quantities have units and scipy cannot handle those, we actually
solve
4*pi/3*rho_d * u^3 - S_d * u + S_d - 1 = 0,
with
rho_d = rho * r1**3 / M1
S_d = (M2-M1)/(r2-r1) * (r1/M1)
The result then needs to be multiplied with r1 to get the intersection radius
"""
return 4.0 * np.pi / 3.0 * rho_dim * r**3 - slope_dim * r + slope_dim - 1.0
def find_SO_radius_and_mass(
ordered_radius, density, cumulative_mass, reference_density
):
"""
Find the radius and mass of an SO from the ordered density and cumulative
mass profiles.
The profiles are constructed by sorting the particles within the spherical
region and then summing their masses in that order (assigning the full
mass of the particle to the particle's radius). The density for every radius
is then computed by dividing the cumulative mass profile by the volume of a
sphere with that radius.
The SO radius is defined as the radius at which the density profile dips
below the given reference density. Unfortunately, real density profiles
are noisy and can sometimes fluctuate around the threshold. We therefore
define the SO radius as the first radius for which this happens, at machine
precision.
If no particles are below the threshold, then we raise an error and force an
increase of the search radius.
If all particles are below the threshold, we assume that the cumulative
mass profile of the halo is linear out to the radius of the first particle,
and then use the corresponding density profile to find the intersection.
In all other cases, we find the actual SO radius by assuming a linear
cumulative mass profile in the bin where the density dips below the
threshold, and intersecting the corresponding density profile with the
threshold. This approach requires a root finding algorithm and does not
yield exactly the same result as linear interpolation in r-log(rho) space
for the same bin (which is for example used by VELOCIraptor). It however
guarantees that the SO mass we find is contained within the intersecting
bin, which would otherwise not necessarily be true (especially if the
intersecting bin is relatively wide). We could also interpolate both the
radius and mass, but then the mean density of the SO would not necessarily
match the target density, which is also weird.
"""
# Compute a mask that marks particles above the threshold. We do this
# exactly once.
above_mask = density > reference_density
if above_mask[0]:
# Get the complementary mask of particles below the threshold.
# By using the complementary, we avoid any ambiguity about '>' vs '<='
below_mask = ~above_mask
# Find smallest radius where the density is below the threshold
i = np.argmax(below_mask)
if i == 0:
# There are no particles below the threshold
# We need to increase the search radius
if ordered_radius[-1] > 20.0 * unyt.Mpc:
raise RuntimeError(
"Cannot find SO radius, but search radius is already larger than 20 Mpc!"
)
raise ReadRadiusTooSmallError("SO radius multiple estimate was too small!")
else:
# all non-zero radius particles are below the threshold
# we linearly interpolate the mass from 0 to the particle radius
# and determine the radius at which this interpolation matches the
# target density
# This is simply the solution of
# 4*pi/3*r^3*rho = M[0]/r[0]*r
# Note that if masses are allowed to be negative, the first cumulative
# mass value could be negative. We make sure to avoid this problem
ipos = 0
while ipos < len(cumulative_mass) and cumulative_mass[ipos] < 0.0:
ipos += 1
if ipos == len(cumulative_mass):
raise RuntimeError("Should never happen!")
SO_r = np.sqrt(
0.75
* cumulative_mass[ipos]
/ (np.pi * ordered_radius[ipos] * reference_density)
)
SO_mass = cumulative_mass[ipos] * SO_r / ordered_radius[ipos]
return SO_r, SO_mass, 4.0 * np.pi / 3.0 * SO_r**3
# We now have the intersecting interval. Get the limits.
r1 = ordered_radius[i - 1]
r2 = ordered_radius[i]
M1 = cumulative_mass[i - 1]
M2 = cumulative_mass[i]
# deal with the pathological case where r1==r2
# we also need an interval where the density intersects
while r1 == r2 or (above_mask[i - 1] == above_mask[i]):
i += 1
# if we run out of 'i', we need to increase the search radius
if i >= len(density):
if ordered_radius[-1] > 20.0 * unyt.Mpc:
raise RuntimeError(
"Cannot find SO radius, but search radius is already larger than 20 Mpc!"
)
raise ReadRadiusTooSmallError("SO radius multiple estimate was too small!")
# take the next interval
r1 = r2
r2 = ordered_radius[i]
M1 = M2
M2 = cumulative_mass[i]
# compute the dimensionless quantities that enter the intersection equation
# remember, we are simply solving
# 4*pi/3*r^3*rho = M1 + (M2-M1)/(r2-r1)*(r-r1)
rho_dim = reference_density * r1**3 / M1
slope_dim = (M2 - M1) / (r2 - r1) * (r1 / M1)
SO_r = r1 * brentq(
cumulative_mass_intersection, 1.0, r2 / r1, args=(rho_dim, slope_dim)
)
SO_volume = 4.0 / 3.0 * np.pi * SO_r**3
# compute the SO mass by requiring that the mean density in the SO is the
# target density
SO_mass = SO_volume * reference_density
return SO_r, SO_mass, SO_volume
class SOParticleData:
def __init__(
self,
input_halo,
data,
types_present,
recently_heated_gas_filter,
nu_density,
observer_position,
):
self.input_halo = input_halo
self.data = data
self.types_present = types_present
self.recently_heated_gas_filter = recently_heated_gas_filter
self.nu_density = nu_density
self.observer_position = observer_position
self.compute_basics()
def compute_basics(self):
self.centre = self.input_halo["cofp"]
self.index = self.input_halo["index"]
# Make an array of particle masses, radii and positions
mass = []
radius = []
position = []
velocity = []
types = []
groupnr = []
for ptype in self.types_present:
if ptype == "PartType6":
# add neutrinos separately, since we need to treat them
# differently
continue
mass.append(self.data[ptype][mass_dataset(ptype)])
pos = self.data[ptype]["Coordinates"] - self.centre[None, :]
position.append(pos)
r = np.sqrt(np.sum(pos**2, axis=1))
radius.append(r)
velocity.append(self.data[ptype]["Velocities"])
typearr = np.zeros(r.shape, dtype="U9")
typearr[:] = ptype
types.append(typearr)
groupnr.append(self.data[ptype]["GroupNr_bound"])
self.mass = unyt.array.uconcatenate(mass)
self.radius = unyt.array.uconcatenate(radius)
self.position = unyt.array.uconcatenate(position)
self.velocity = unyt.array.uconcatenate(velocity)
self.types = np.concatenate(types)
self.groupnr = unyt.array.uconcatenate(groupnr)
# figure out which particles in the list are bound to a halo that is not the
# central halo
self.is_bound_to_satellite = (self.groupnr >= 0) & (self.groupnr != self.index)
def compute_SO_radius_and_mass(self, reference_density, physical_radius):
# add neutrinos
if "PartType6" in self.data:
numass = (
self.data["PartType6"]["Masses"] * self.data["PartType6"]["Weights"]
)
pos = self.data["PartType6"]["Coordinates"] - self.centre[None, :]
nur = np.sqrt(np.sum(pos**2, axis=1))
all_mass = unyt.array.uconcatenate([self.mass, numass])
all_r = unyt.array.uconcatenate([self.radius, nur])
else:
all_mass = self.mass
all_r = self.radius
# Sort by radius
order = np.argsort(all_r)
ordered_radius = all_r[order]
cumulative_mass = np.cumsum(all_mass[order], dtype=np.float64).astype(
self.mass.dtype
)
# add mean neutrino mass
cumulative_mass += self.nu_density * 4.0 / 3.0 * np.pi * ordered_radius**3
# Compute density within radius of each particle.
# Will need to skip any at zero radius.
# Note that because of the definition of the centre of potential, the first
# particle *should* be at r=0. We need to manually exclude it, in case round
# off error places it at a very small non-zero radius.
nskip = max(1, np.argmax(ordered_radius > 0.0 * ordered_radius.units))
ordered_radius = ordered_radius[nskip:]
cumulative_mass = cumulative_mass[nskip:]
nr_parts = len(ordered_radius)
density = cumulative_mass / (4.0 / 3.0 * np.pi * ordered_radius**3)
# Check if we ever reach the density threshold
if reference_density > 0:
if nr_parts > 0:
try:
self.SO_r, self.SO_mass, self.SO_volume = find_SO_radius_and_mass(
ordered_radius,
density,
cumulative_mass,
reference_density,
)
except ReadRadiusTooSmallError:
raise ReadRadiusTooSmallError("SO radius multiple was too small!")
else:
self.SO_volume = 0 * ordered_radius.units**3
elif physical_radius > 0:
self.SO_r = physical_radius
self.SO_volume = 4.0 * np.pi / 3.0 * self.SO_r**3
if nr_parts > 0:
# find the enclosed mass using interpolation
outside_radius = ordered_radius > self.SO_r
if not np.any(outside_radius):
# all particles are within the radius, we cannot interpolate
self.SO_mass = cumulative_mass[-1]
else:
i = np.argmax(outside_radius)
if i == 0:
# we only have particles in the centre, so we cannot interpolate
self.SO_mass = cumulative_mass[i]
else:
r1 = ordered_radius[i - 1]
r2 = ordered_radius[i]
M1 = cumulative_mass[i - 1]
M2 = cumulative_mass[i]
self.SO_mass = M1 + (self.SO_r - r1) / (r2 - r1) * (M2 - M1)
# check if we were successful. We only compute SO properties if we
# have both a radius and mass (the mass criterion covers the case where
# the radius is set to a physical size but we have no mass nonetheless)
SO_exists = self.SO_r > 0 and self.SO_mass > 0
if SO_exists:
self.gas_selection = self.radius[self.types == "PartType0"] < self.SO_r
self.dm_selection = self.radius[self.types == "PartType1"] < self.SO_r
self.star_selection = self.radius[self.types == "PartType4"] < self.SO_r
self.bh_selection = self.radius[self.types == "PartType5"] < self.SO_r
self.all_selection = self.radius < self.SO_r
self.mass = self.mass[self.all_selection]
self.radius = self.radius[self.all_selection]
self.position = self.position[self.all_selection]
self.velocity = self.velocity[self.all_selection]
self.types = self.types[self.all_selection]
self.is_bound_to_satellite = self.is_bound_to_satellite[self.all_selection]
return SO_exists
@property
def r(self):
return self.SO_r
@property
def Mtot(self):
return self.SO_mass
@lazy_property
def Mtotpart(self):
return self.mass.sum()
@lazy_property
def mass_fraction(self):
# note that we cannot divide by mSO here, since that was based on an interpolation
return self.mass / self.Mtotpart
@lazy_property
def com(self):
return (self.mass_fraction[:, None] * self.position).sum(axis=0) + self.centre
@lazy_property
def vcom(self):
return (self.mass_fraction[:, None] * self.velocity).sum(axis=0)
@lazy_property
def spin_parameter(self):
if self.Mtotpart == 0:
return None
_, vmax = get_vmax(self.mass, self.radius)
if vmax > 0:
vrel = self.velocity - self.vcom[None, :]
Ltot = unyt.array.unorm(
(self.mass[:, None] * unyt.array.ucross(self.position, vrel)).sum(
axis=0
)
)
return Ltot / (np.sqrt(2.0) * self.Mtotpart * self.SO_r * vmax)
return None
@lazy_property
def TotalAxisLengths(self):
if self.Mtotpart == 0:
return None
return get_axis_lengths(self.mass, self.position)
@lazy_property
def Mfrac_satellites(self):
return self.mass[self.is_bound_to_satellite].sum() / self.SO_mass
@lazy_property
def gas_masses(self):
return self.mass[self.types == "PartType0"]
@lazy_property
def gas_pos(self):
return self.position[self.types == "PartType0"]
@lazy_property
def gas_vel(self):
return self.velocity[self.types == "PartType0"]
@lazy_property
def Mgas(self):
return self.gas_masses.sum()
@lazy_property
def gas_mass_fraction(self):
if self.Mgas == 0:
return None
return self.gas_masses / self.Mgas
@lazy_property
def com_gas(self):
if self.Mgas == 0:
return None
return (self.gas_mass_fraction[:, None] * self.gas_pos).sum(
axis=0
) + self.centre
@lazy_property
def vcom_gas(self):
if self.Mgas == 0:
return None
return (self.gas_mass_fraction[:, None] * self.gas_vel).sum(axis=0)
def compute_Lgas_props(self):
(
self.internal_Lgas,
_,
self.internal_Mcountrot_gas,
) = get_angular_momentum_and_kappa_corot(
self.gas_masses,
self.gas_pos,
self.gas_vel,
ref_velocity=self.vcom_gas,
do_counterrot_mass=True,
)
@lazy_property
def Lgas(self):
if self.Mgas == 0:
return None
if not hasattr(self, "internal_Lgas"):
self.compute_Lgas_props()
return self.internal_Lgas
@lazy_property
def DtoTgas(self):
if self.Mgas == 0:
return None
if not hasattr(self, "internal_Mcountrot_gas"):
self.compute_Lgas_props()
return 1.0 - 2.0 * self.internal_Mcountrot_gas / self.Mgas
@lazy_property
def GasAxisLengths(self):
if self.Mgas == 0:
return None
return get_axis_lengths(self.gas_masses, self.gas_pos)
@lazy_property
def dm_masses(self):
return self.mass[self.types == "PartType1"]
@lazy_property
def dm_pos(self):
return self.position[self.types == "PartType1"]
@lazy_property
def dm_vel(self):
return self.velocity[self.types == "PartType1"]
@lazy_property
def Mdm(self):
return self.dm_masses.sum()
@lazy_property
def dm_mass_fraction(self):
if self.Mdm == 0:
return None
return self.dm_masses / self.Mdm
@lazy_property
def vcom_dm(self):
if self.Mdm == 0:
return None
return (self.dm_mass_fraction[:, None] * self.dm_vel).sum(axis=0)
@lazy_property
def Ldm(self):
if self.Mdm == 0:
return None
return get_angular_momentum(
self.dm_masses, self.dm_pos, self.dm_vel, ref_velocity=self.vcom_dm
)
@lazy_property
def DMAxisLengths(self):
if self.Mdm == 0:
return None
return get_axis_lengths(self.dm_masses, self.dm_pos)
@lazy_property
def star_masses(self):
return self.mass[self.types == "PartType4"]
@lazy_property
def star_pos(self):
return self.position[self.types == "PartType4"]
@lazy_property
def star_vel(self):
return self.velocity[self.types == "PartType4"]
@lazy_property
def Mstar(self):
return self.star_masses.sum()
@lazy_property
def star_mass_fraction(self):
if self.Mstar == 0:
return None
return self.star_masses / self.Mstar
@lazy_property
def com_star(self):
if self.Mstar == 0:
return None
return (self.star_mass_fraction[:, None] * self.star_pos).sum(
axis=0
) + self.centre
@lazy_property
def vcom_star(self):
if self.Mstar == 0:
return None
return (self.star_mass_fraction[:, None] * self.star_vel).sum(axis=0)
def compute_Lstar_props(self):
(
self.internal_Lstar,
_,
self.internal_Mcountrot_star,
) = get_angular_momentum_and_kappa_corot(
self.star_masses,
self.star_pos,
self.star_vel,
ref_velocity=self.vcom_star,
do_counterrot_mass=True,
)
@lazy_property
def Lstar(self):
if self.Mstar == 0:
return None
if not hasattr(self, "internal_Lstar"):
self.compute_Lstar_props()
return self.internal_Lstar
@lazy_property
def DtoTstar(self):
if self.Mstar == 0:
return None
if not hasattr(self, "internal_Mcountrot_star"):
self.compute_Lstar_props()
return 1.0 - 2.0 * self.internal_Mcountrot_star / self.Mstar
@lazy_property
def StellarAxisLengths(self):
if self.Mstar == 0:
return None
return get_axis_lengths(self.star_masses, self.star_pos)
@lazy_property
def baryon_masses(self):
return self.mass[(self.types == "PartType0") | (self.types == "PartType4")]
@lazy_property
def baryon_pos(self):
return self.position[(self.types == "PartType0") | (self.types == "PartType4")]
@lazy_property
def baryon_vel(self):
return self.velocity[(self.types == "PartType0") | (self.types == "PartType4")]
@lazy_property
def Mbaryons(self):
return self.baryon_masses.sum()
@lazy_property
def baryon_mass_fraction(self):
if self.Mbaryons == 0:
return None
return self.baryon_masses / self.Mbaryons
@lazy_property
def baryon_vcom(self):
if self.Mbaryons == 0:
return None
return (self.baryon_mass_fraction[:, None] * self.baryon_vel).sum(axis=0)
@lazy_property
def Lbaryons(self):
if self.Mbaryons == 0:
return None
baryon_relvel = self.baryon_vel - self.baryon_vcom[None, :]
return (
self.baryon_masses[:, None]
* unyt.array.ucross(self.baryon_pos, baryon_relvel)
).sum(axis=0)
@lazy_property
def BaryonAxisLengths(self):
if self.Mbaryons == 0:
return None
return get_axis_lengths(self.baryon_masses, self.baryon_pos)
@lazy_property
def Mbh_dynamical(self):
return self.mass[self.types == "PartType5"].sum()
@lazy_property
def Ngas(self):
return self.gas_selection.sum()
@lazy_property
def gas_metal_masses(self):
if self.Ngas == 0:
return None
return (
self.gas_masses
* self.data["PartType0"]["MetalMassFractions"][self.gas_selection]
)
@lazy_property
def gasmetalfrac(self):
if self.Ngas == 0:
return None
return self.gas_metal_masses.sum() / self.Mgas
@lazy_property
def gasOfrac(self):
if self.Ngas == 0:
return None
return (
self.gas_masses
* self.data["PartType0"]["SmoothedElementMassFractions"][
self.gas_selection
][:, indexO]
).sum() / self.Mgas
@lazy_property
def gasFefrac(self):
if self.Ngas == 0:
return None
return (
self.gas_masses
* self.data["PartType0"]["SmoothedElementMassFractions"][
self.gas_selection
][:, indexFe]
).sum() / self.Mgas
@lazy_property
def gas_temperatures(self):
if self.Ngas == 0:
return None
return self.data["PartType0"]["Temperatures"][self.gas_selection]
@lazy_property
def Tgas(self):
if self.Ngas == 0:
return None
return (self.gas_temperatures * (self.gas_masses / self.Mgas)).sum()
@lazy_property
def gas_no_cool(self):
if self.Ngas == 0:
return None
return self.gas_temperatures > 1.0e5 * unyt.K
@lazy_property
def Mhotgas(self):
if self.Ngas == 0:
return None
return self.gas_masses[self.gas_no_cool].sum()
@lazy_property
def Tgas_no_cool(self):
if self.Ngas == 0:
return None
if np.any(self.gas_no_cool):
return (
self.gas_temperatures[self.gas_no_cool]
* self.gas_masses[self.gas_no_cool]
).sum() / self.Mhotgas
@lazy_property
def gas_SFR(self):
if self.Ngas == 0:
return None
SFR = self.data["PartType0"]["StarFormationRates"][self.gas_selection]
is_SFR = SFR > 0.0
SFR[~is_SFR] = 0.0
return SFR
@lazy_property
def SFR(self):
if self.Ngas == 0:
return None
return self.gas_SFR.sum()
@lazy_property
def Mgas_SF(self):
if self.Ngas == 0:
return None
return self.gas_masses[self.gas_SFR > 0.0].sum()
@lazy_property
def gasmetalfrac_SF(self):
if self.Ngas == 0 or self.Mgas_SF == 0.0:
return None
return self.gas_metal_masses[self.gas_SFR > 0.0].sum() / self.Mgas_SF
@lazy_property
def gas_xraylum(self):
if self.Ngas == 0:
return None
return self.data["PartType0"]["XrayLuminosities"][self.gas_selection]
@lazy_property
def Xraylum(self):
if self.Ngas == 0:
return None
return self.gas_xraylum.sum(axis=0)
@lazy_property
def gas_xrayphlum(self):
if self.Ngas == 0:
return None
return self.data["PartType0"]["XrayPhotonLuminosities"][self.gas_selection]
@lazy_property
def Xrayphlum(self):
if self.Ngas == 0:
return None
return self.gas_xrayphlum.sum(axis=0)
@lazy_property
def gas_compY(self):
if self.Ngas == 0:
return None
return self.data["PartType0"]["ComptonYParameters"][self.gas_selection]
@lazy_property
def compY_unit(self):
# We need to manually convert the units for compY to avoid numerical
# overflow inside unyt
if self.Ngas == 0:
return None
unit = 1.0 * self.gas_compY.units
new_unit = unit.to(PropertyTable.full_property_list["compY"][3])
return new_unit
@lazy_property
def compY(self):
if self.Ngas == 0:
return None
return self.gas_compY.sum().value * self.compY_unit
@lazy_property
def gas_no_agn(self):
if self.Ngas == 0:
return None
last_agn_gas = self.data["PartType0"]["LastAGNFeedbackScaleFactors"][
self.gas_selection
]
return ~self.recently_heated_gas_filter.is_recently_heated(
last_agn_gas, self.gas_temperatures
)
@lazy_property
def Xraylum_no_agn(self):
if self.Ngas == 0:
return None
return self.gas_xraylum[self.gas_no_agn].sum(axis=0)
@lazy_property
def Xrayphlum_no_agn(self):
if self.Ngas == 0:
return None
return self.gas_xrayphlum[self.gas_no_agn].sum(axis=0)
@lazy_property
def compY_no_agn(self):
if self.Ngas == 0:
return None
if np.any(self.gas_no_agn):
return self.gas_compY[self.gas_no_agn].sum().value * self.compY_unit
else:
return None
@lazy_property
def Tgas_no_agn(self):
if self.Ngas == 0:
return None
mass_gas_no_agn = self.gas_masses[self.gas_no_agn]
Mgas_no_agn = mass_gas_no_agn.sum()
if Mgas_no_agn > 0:
return (
(mass_gas_no_agn / Mgas_no_agn) * self.gas_temperatures[self.gas_no_agn]
).sum()
@lazy_property
def gas_no_cool_no_agn(self):
if self.Ngas == 0:
return None
return self.gas_no_cool & self.gas_no_agn
@lazy_property
def Tgas_no_cool_no_agn(self):
if self.Ngas == 0:
return None
mass_gas_no_cool_no_agn = self.gas_masses[self.gas_no_cool_no_agn]
Mgas_no_cool_no_agn = mass_gas_no_cool_no_agn.sum()
if Mgas_no_cool_no_agn > 0:
return (
(mass_gas_no_cool_no_agn / Mgas_no_cool_no_agn)
* self.gas_temperatures[self.gas_no_cool_no_agn]
).sum()
@lazy_property
def Ekin_gas(self):
if self.Ngas == 0:
return None
# below we need to force conversion to np.float64 before summing up particles
# to avoid overflow
ekin_gas = self.gas_masses * ((self.gas_vel - self.vcom_gas[None, :]) ** 2).sum(
axis=1
)
ekin_gas = unyt.unyt_array(
ekin_gas.value, dtype=np.float64, units=ekin_gas.units
)
return 0.5 * ekin_gas.sum()
@lazy_property
def gas_densities(self):
if self.Ngas == 0:
return None
return self.data["PartType0"]["Densities"][self.gas_selection]
@lazy_property
def Etherm_gas(self):
if self.Ngas == 0:
return None
etherm_gas = (
1.5
* self.gas_masses
* self.data["PartType0"]["Pressures"][self.gas_selection]
/ self.gas_densities
)
etherm_gas = unyt.unyt_array(
etherm_gas.value, dtype=np.float64, units=etherm_gas.units
)
return etherm_gas.sum()
@lazy_property
def DopplerB(self):
if self.Ngas == 0:
return None
ne = self.data["PartType0"]["ElectronNumberDensities"][self.gas_selection]
# note: the positions where relative to the centre, so we have
# to make them absolute again before subtracting the observer
# position
relpos = self.gas_pos + self.centre[None, :] - self.observer_position[None, :]
distance = np.sqrt((relpos**2).sum(axis=1))
# we need to exclude particles at zero distance
# (we assume those have no relative velocity)
vr = unyt.unyt_array(
np.zeros(self.gas_vel.shape[0]),
dtype=self.gas_vel.dtype,
units=self.gas_vel.units,
)
has_distance = distance > 0.0
vr[has_distance] = (
self.gas_vel[has_distance, 0] * relpos[has_distance, 0]
+ self.gas_vel[has_distance, 1] * relpos[has_distance, 1]
+ self.gas_vel[has_distance, 2] * relpos[has_distance, 2]
) / distance[has_distance]
fac = unyt.sigma_thompson / unyt.c
volumes = self.gas_masses / self.gas_densities
area = np.pi * self.SO_r**2
return (fac * ne * vr * (volumes / area)).sum()
@lazy_property
def Ndm(self):
return self.dm_selection.sum()
@lazy_property
def Nstar(self):
return self.star_selection.sum()
@lazy_property
def Mstar_init(self):
if self.Nstar == 0:
return None
return self.data["PartType4"]["InitialMasses"][self.star_selection].sum()
@lazy_property
def starmetalfrac(self):
if self.Nstar == 0:
return None
return (
self.star_masses
* self.data["PartType4"]["MetalMassFractions"][self.star_selection]
).sum() / self.Mstar
@lazy_property
def starOfrac(self):
if self.Nstar == 0:
return None
return (
self.star_masses
* self.data["PartType4"]["SmoothedElementMassFractions"][
self.star_selection
][:, indexO]
).sum() / self.Mstar
@lazy_property
def starFefrac(self):
if self.Nstar == 0:
return None
return (
self.star_masses
* self.data["PartType4"]["SmoothedElementMassFractions"][
self.star_selection
][:, indexFe]
).sum() / self.Mstar
@lazy_property
def StellarLuminosity(self):
if self.Nstar == 0:
return None
return self.data["PartType4"]["Luminosities"][self.star_selection].sum(axis=0)
@lazy_property
def Ekin_star(self):
if self.Nstar == 0:
return None
# below we need to force conversion to np.float64 before summing up particles
# to avoid overflow
ekin_star = self.star_masses * (
(self.star_vel - self.vcom_star[None, :]) ** 2
).sum(axis=1)
ekin_star = unyt.unyt_array(
ekin_star.value, dtype=np.float64, units=ekin_star.units
)
return 0.5 * ekin_star.sum()
@lazy_property
def Nbh(self):
return self.bh_selection.sum()
@lazy_property
def Mbh_subgrid(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["SubgridMasses"][self.bh_selection].sum()
@lazy_property
def agn_eventa(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["LastAGNFeedbackScaleFactors"][self.bh_selection]
@lazy_property
def BHlasteventa(self):
if self.Nbh == 0:
return None
return np.max(self.agn_eventa)
@lazy_property
def iBHmax(self):
if self.Nbh == 0:
return None
return np.argmax(self.data["PartType5"]["SubgridMasses"][self.bh_selection])
@lazy_property
def BHmaxM(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["SubgridMasses"][self.bh_selection][self.iBHmax]
@lazy_property
def BHmaxID(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["ParticleIDs"][self.bh_selection][self.iBHmax]
@lazy_property
def BHmaxpos(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["Coordinates"][self.bh_selection][self.iBHmax]
@lazy_property
def BHmaxvel(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["Velocities"][self.bh_selection][self.iBHmax]
@lazy_property
def BHmaxAR(self):
if self.Nbh == 0:
return None
return self.data["PartType5"]["AccretionRates"][self.bh_selection][self.iBHmax]
@lazy_property
def BHmaxlasteventa(self):
if self.Nbh == 0:
return None
return self.agn_eventa[self.iBHmax]
@lazy_property
def Nnu(self):
if "PartType6" in self.data:
pos = self.data["PartType6"]["Coordinates"] - self.centre[None, :]
nur = np.sqrt(np.sum(pos**2, axis=1))
self.nu_selection = nur < self.SO_r
return self.nu_selection.sum()
else:
return unyt.unyt_array(0, dtype=np.uint32, units="dimensionless")
@lazy_property
def Mnu(self):
if self.Nnu == 0:
return None