forked from jchelly/SOAP
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaperture_properties.py
3761 lines (3419 loc) · 127 KB
/
aperture_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
#! /usr/bin/env python
"""
aperture_properties.py
Halo properties within 3D apertures. These include either all the particles
(inclusive) or all the gravitionally bound particles (exclusive) of a subhalo,
within a fixed physical radius.
Just like the other HaloProperty implementations, the calculation of the
properties is done lazily: only calculations that are actually needed are
performed. To achieve this, we use a somewhat weird coding pattern: the
halo property calculations correspond to methods of an ApertureParticleData
object, decorated with the 'lazy_property' decorator. Consider the following
naive calculation of the stellar mass and stellar metal mass fraction:
radius = data["PartType4/Radius"] # (this dataset does not actually exist)
aperture_mask = radius < aperture_radius
star_mass = data["PartType4/Masses"][aperture_mask]
Mstar = star_mass.sum()
metal_frac = data["PartType4/MetalMassFractions"][aperture_mask]
star_metal_mass = (star_mass * metal_frac).sum()
MetalFracStar = star_metal_mass / Mstar
In this code excerpt, every line corresponds to a new variable that will be
computed. The stellar mass and aperture mask are used multiple times. So far,
everything is fine. Problems arise however if we want to disable the calculation
of for example the stellar mass, based on some flag. We could write
radius = data["PartType4/Radius"]
aperture_mask = radius < aperture_radius
if flag:
star_mass = data["PartType4/Masses"][aperture_mask]
Mstar = star_mass.sum()
metal_frac = data["PartType4/MetalMassFractions"][aperture_mask]
star_metal_mass = (star_mass * metal_frac).sum()
MetalFracStar = star_metal_mass / Mstar
but this is obviously wrong, since we still need 'star_mass' and 'Mstar' to
compute the metal mass fraction. In a lot of cases, these dependencies are
not that clear, and it becomes very tricky to figure out how to disable some
properties without breaking other property calculations. It is possible, but
it is painful to do and very prone to mistakes.
Instead of figuring out all the depencies, we can instead use this:
class PropertyCalculations:
def __init__(self, data):
self.data = data
@lazy_property
def aperture_mask(self):
radius = self.data["PartType4/Radius"]
return radius < aperture_radius
@lazy_property
def star_mass(self):
return self.data["PartType4/Masses"][self.aperture_mask]
@lazy_property
def Mstar(self):
return self.star_mass.sum()
@lazy_property
def star_metal_mass(self):
metal_frac = self.data["PartType4/MetalMassFractions"][self.aperture_mask]
return (self.star_mass * metal_frac).sum()
@lazy_property
def MetalFracStar(self):
return self.star_metal_mass / self.Mstar
This looks the same as the previous code excerpt, but then a lot more
complicated. The key difference is that all of these methods are 'lazy', which
means they only get evaluated when they are actually used. The advantage becomes
clear when we consider the various scenarios:
1. We want to compute Mstar, but not MetalFracStar:
- we call Mstar()
- Mstar() has not been called before, so it is run
- Mstar() calls star_mass()
- star_mass() has not been called before, so it is run
- star_mass() calls aperture_mask()
- aperture_mask() has not been called before, so it is run
- done.
2. We want to compute MetalFracStar, but not Mstar:
- we call MetalFracStar()
- MetalFracStar() has not been called before, so it is run
- MetalFracStar() calls star_metal_mass() and Mstar()
- star_metal_mass() has not been called before, so it is run
- star_metal_mass() calls aperture_mask() and star_mass()
- aperture_mask() has not been called before, so it is run
- star_mass() has not been called before, so it is run
- star_mass() calls aperture_mask(), but that has already run
- Mstar() calls star_mass(), but that has already run
- done.
3. We want to compute both Mstar and MetalFracStar:
- we call Mstar()
- Mstar() has not been called before, so it is run
- Mstar() calls star_mass()
- star_mass() has not been called before, so it is run
- star_mass() calls aperture_mask()
- aperture_mask() has not been called before, so it is run
- we call MetalFracStar()
- MetalFracStar() has not been called before, so it is run
- MetalFracStar() calls star_metal_mass() and Mstar(), but that has already
run
- star_metal_mass() has not been called before, so it is run
- star_metal_mass() calls aperture_mask() and star_mass(), both have already
run
- done.
Depending on what we want to calculate, we get a different order in which
variables are calculated (and methods are called), but only the variables that
are actually used are calculated. This way to evaluate methods when they are
needed dynamically adapts to the particular situation, without the need to
figure out the dependencies yourself.
In the HaloProperty implementation, we need at least one method for every
halo property in the table (property_table.py) that we want to compute. But that
does not eliminate the overhead of auxiliary variables (like aperture_mask) that
are needed by multiple properties. To make this lazy evaluation work, you
therefore need to determine which variables are used multiple times, and which
variables are not and can hence stay local to a particular lazy method. There is
still some decision making needed there.
On top of that, we also need to deal with borderline cases, like computing the
stellar mass for halos with no star particles. These need to be dealt with in
each lazy method separately, because you cannot/should not expect that a lazy
method will never be called in that case. That is why the implementation looks
very messy and complex. But it is in fact quite neat and powerful.
"""
import numpy as np
import unyt
from halo_properties import HaloProperty, SearchRadiusTooSmallError
from dataset_names import mass_dataset
from half_mass_radius import get_half_mass_radius
from kinematic_properties import (
get_velocity_dispersion_matrix,
get_angular_momentum,
get_angular_momentum_and_kappa_corot,
get_vmax,
)
from swift_cells import SWIFTCellGrid
from recently_heated_gas_filter import RecentlyHeatedGasFilter
from stellar_age_calculator import StellarAgeCalculator
from cold_dense_gas_filter import ColdDenseGasFilter
from property_table import PropertyTable
from lazy_properties import lazy_property
from category_filter import CategoryFilter
from parameter_file import ParameterFile
from snapshot_datasets import SnapshotDatasets
from typing import Dict, List, Tuple
from numpy.typing import NDArray
class ApertureParticleData:
"""
Halo calculation class.
All properties we want to compute in apertures are implemented as lazy
methods of this class.
Note that this class internally uses and requires two different masks:
- *_mask_all: Mask that masks out particles belonging to this halo: either
only gravitationally bound particles (exclusive apertures) or all
particles (no mask -- inclusive apertures). This mask needs to be
applied _first_ to raw "PartTypeX" datasets.
- *_mask_ap: Mask that masks out particles that are inside the aperture
radius. This mask can only be applied after *_mask_all has been applied.
compute_basics() furthermore defines some arrays that contain variables
(e.g. masses, positions) for all particles that belong to the halo (so
after applying *_mask_all, but before applying *_mask_ap). To retrieve the
variables for a single particle type, these have to be masked with
"PartTypeX == 'type'".
All of these masks have different lengths, so using the wrong mask will
lead to errors. Those are captured by the unit tests, so make sure to run
those after you implement a new property!
"""
def __init__(
self,
input_halo: Dict,
data: Dict,
types_present: List[str],
inclusive: bool,
aperture_radius: unyt.unyt_quantity,
stellar_age_calculator: StellarAgeCalculator,
recently_heated_gas_filter: RecentlyHeatedGasFilter,
cold_dense_gas_filter: ColdDenseGasFilter,
snapshot_datasets: SnapshotDatasets,
softening_of_parttype: unyt.unyt_array,
):
"""
Constructor.
Parameters:
- input_halo: Dict
Dictionary containing properties of the halo read from the VR catalogue.
- data: Dict
Dictionary containing particle data.
- types_present: List
List of all particle types (e.g. 'PartType0') that are present in the data
dictionary.
- inclusive: bool
Whether or not to include particles not gravitationally bound to the subhalo
in the property calculations.
- aperture_radius: unyt.unyt_quantity
Aperture radius.
- stellar_age_calculator: StellarAgeCalculator
Object used to compute stellar ages from the current cosmological scale factor
and the birth scale factors of star particles.
- recently_heated_gas_filter: RecentlyHeatedGasFilter
Filter used to mask out gas particles that were recently heated by
AGN feedback.
- cold_dense_gas_filter: ColdDenseGasFilter
Filter used to mask out gas particles containing cold, dense gas.
- snapshot_datasets: SnapshotDatasets
Object containing metadata about the datasets in the snapshot, like
appropriate aliases and column names.
- softening_of_parttype: unyt.unyt_array
Softening length of each particle types
"""
self.input_halo = input_halo
self.data = data
self.types_present = types_present
self.inclusive = inclusive
self.aperture_radius = aperture_radius
self.stellar_age_calculator = stellar_age_calculator
self.recently_heated_gas_filter = recently_heated_gas_filter
self.cold_dense_gas_filter = cold_dense_gas_filter
self.snapshot_datasets = snapshot_datasets
self.softening_of_parttype = softening_of_parttype
self.compute_basics()
def get_dataset(self, name: str) -> unyt.unyt_array:
"""
Local wrapper for SnapshotDatasets.get_dataset().
"""
return self.snapshot_datasets.get_dataset(name, self.data)
def compute_basics(self):
"""
Compute some properties that are always needed, regardless of which
properties we actually want to compute.
"""
self.centre = self.input_halo["cofp"]
self.index = self.input_halo["index"]
mass = []
position = []
radius = []
velocity = []
types = []
softening = []
for ptype in self.types_present:
grnr = self.get_dataset(f"{ptype}/GroupNr_bound")
if self.inclusive:
in_halo = np.ones(grnr.shape, dtype=bool)
else:
in_halo = grnr == self.index
mass.append(self.get_dataset(f"{ptype}/{mass_dataset(ptype)}")[in_halo])
pos = (
self.get_dataset(f"{ptype}/Coordinates")[in_halo, :]
- self.centre[None, :]
)
position.append(pos)
r = np.sqrt(pos[:, 0] ** 2 + pos[:, 1] ** 2 + pos[:, 2] ** 2)
radius.append(r)
velocity.append(self.get_dataset(f"{ptype}/Velocities")[in_halo, :])
typearr = int(ptype[-1]) * np.ones(r.shape, dtype=np.int32)
types.append(typearr)
s = np.ones(r.shape, dtype=np.float64) * self.softening_of_parttype[ptype]
softening.append(s)
self.mass = np.concatenate(mass)
self.position = np.concatenate(position)
self.radius = np.concatenate(radius)
self.velocity = np.concatenate(velocity)
self.types = np.concatenate(types)
self.softening = np.concatenate(softening)
self.mask = self.radius <= self.aperture_radius
self.mass = self.mass[self.mask]
self.position = self.position[self.mask]
self.velocity = self.velocity[self.mask]
self.radius = self.radius[self.mask]
self.type = self.types[self.mask]
self.softening = self.softening[self.mask]
@lazy_property
def gas_mask_ap(self) -> NDArray[bool]:
"""
Mask that filters out gas particles that are inside the aperture radius.
This mask can be used on arrays of all gas particles that are included
in the calculation (so either the raw "PartType0" array for inclusive
apertures, or only the bound particles in that array for exclusive
apertures).
"""
return self.mask[self.types == 0]
@lazy_property
def dm_mask_ap(self) -> NDArray[bool]:
"""
Mask that filters out DM particles that are inside the aperture radius.
This mask can be used on arrays of all DM particles that are included
in the calculation (so either the raw "PartType1" array for inclusive
apertures, or only the bound particles in that array for exclusive
apertures).
"""
return self.mask[self.types == 1]
@lazy_property
def star_mask_ap(self) -> NDArray[bool]:
"""
Mask that filters out star particles that are inside the aperture radius.
This mask can be used on arrays of all star particles that are included
in the calculation (so either the raw "PartType4" array for inclusive
apertures, or only the bound particles in that array for exclusive
apertures).
"""
return self.mask[self.types == 4]
@lazy_property
def bh_mask_ap(self) -> NDArray[bool]:
"""
Mask that filters out BH particles that are inside the aperture radius.
This mask can be used on arrays of all BH particles that are included
in the calculation (so either the raw "PartType5" array for inclusive
apertures, or only the bound particles in that array for exclusive
apertures).
"""
return self.mask[self.types == 5]
@lazy_property
def baryon_mask_ap(self) -> NDArray[bool]:
"""
Mask that filters out baryon particles that are inside the aperture radius.
This mask can be used on arrays of all baryon particles that are included
in the calculation. Note that baryons are gas and star particles,
so "PartType0" and "PartType4".
"""
return self.mask[(self.types == 0) | (self.types == 4)]
@lazy_property
def Ngas(self) -> int:
"""
Number of gas particles in the aperture.
"""
return self.gas_mask_ap.sum()
@lazy_property
def Ndm(self) -> int:
"""
Number of DM particles in the aperture.
"""
return self.dm_mask_ap.sum()
@lazy_property
def Nstar(self) -> int:
"""
Number of star particles in the aperture.
"""
return self.star_mask_ap.sum()
@lazy_property
def Nbh(self) -> int:
"""
Number of BH particles in the aperture.
"""
return self.bh_mask_ap.sum()
@lazy_property
def Nbaryon(self) -> int:
"""
Number of baryon particles in the aperture.
"""
return self.baryon_mask_ap.sum()
@lazy_property
def mass_gas(self) -> unyt.unyt_array:
"""
Mass of the gas particles.
"""
return self.mass[self.type == 0]
@lazy_property
def mass_dm(self) -> unyt.unyt_array:
"""
Mass of the DM particles.
"""
return self.mass[self.type == 1]
@lazy_property
def mass_star(self) -> unyt.unyt_array:
"""
Mass of the star particles.
"""
return self.mass[self.type == 4]
@lazy_property
def mass_baryons(self) -> unyt.unyt_array:
"""
Mass of the baryon particles (gas + stars).
"""
return self.mass[(self.type == 0) | (self.type == 4)]
@lazy_property
def pos_gas(self) -> unyt.unyt_array:
"""
Position of the gas particles.
"""
return self.position[self.type == 0]
@lazy_property
def pos_dm(self) -> unyt.unyt_array:
"""
Position of the DM particles.
"""
return self.position[self.type == 1]
@lazy_property
def pos_star(self) -> unyt.unyt_array:
"""
Position of the star particles.
"""
return self.position[self.type == 4]
@lazy_property
def pos_baryons(self) -> unyt.unyt_array:
"""
Position of the baryon (gas+stars) particles.
"""
return self.position[(self.type == 0) | (self.type == 4)]
@lazy_property
def vel_gas(self) -> unyt.unyt_array:
"""
Velocity of the gas particles.
"""
return self.velocity[self.type == 0]
@lazy_property
def vel_dm(self) -> unyt.unyt_array:
"""
Velocity of the DM particles.
"""
return self.velocity[self.type == 1]
@lazy_property
def vel_star(self) -> unyt.unyt_array:
"""
Velocity of the star particles.
"""
return self.velocity[self.type == 4]
@lazy_property
def vel_baryons(self) -> unyt.unyt_array:
"""
Velocity of the baryon (gas+star) particles.
"""
return self.velocity[(self.type == 0) | (self.type == 4)]
@lazy_property
def Mtot(self) -> unyt.unyt_quantity:
"""
Total mass of all particles.
"""
return self.mass.sum()
@lazy_property
def Mgas(self) -> unyt.unyt_quantity:
"""
Total mass of gas particles.
"""
return self.mass_gas.sum()
@lazy_property
def Mdm(self) -> unyt.unyt_quantity:
"""
Total mass of DM particles.
"""
return self.mass_dm.sum()
@lazy_property
def Mstar(self) -> unyt.unyt_quantity:
"""
Total mass of star particles.
"""
return self.mass_star.sum()
@lazy_property
def Mbh_dynamical(self) -> unyt.unyt_quantity:
"""
Total dynamical mass of BH particles.
"""
return self.mass[self.type == 5].sum()
@lazy_property
def Mbaryons(self) -> unyt.unyt_quantity:
"""
Total mass of baryon (gas+star) particles.
"""
return self.Mgas + self.Mstar
@lazy_property
def star_mask_all(self) -> NDArray[bool]:
"""
Mask for masking out star particles in raw PartType4 arrays.
This is the mask that masks out unbound particles for exclusive halos.
For inclusive halos, this mask does nothing.
"""
if self.Nstar == 0:
return None
groupnr_bound = self.get_dataset("PartType4/GroupNr_bound")
if self.inclusive:
return np.ones(groupnr_bound.shape, dtype=bool)
else:
return groupnr_bound == self.index
@lazy_property
def Mstar_init(self) -> unyt.unyt_quantity:
"""
Total initial mass of star particles.
"""
if self.Nstar == 0:
return None
return self.get_dataset("PartType4/InitialMasses")[self.star_mask_all][
self.star_mask_ap
].sum()
@lazy_property
def stellar_luminosities(self) -> unyt.unyt_array:
"""
Stellar luminosities.
"""
if self.Nstar == 0:
return None
return self.get_dataset("PartType4/Luminosities")[self.star_mask_all][
self.star_mask_ap
]
@lazy_property
def StellarLuminosity(self) -> unyt.unyt_array:
"""
Total luminosity of star particles.
Note that this returns an array with total luminosities in multiple
bands.
"""
if self.Nstar == 0:
return None
return self.stellar_luminosities.sum(axis=0)
@lazy_property
def starmetalfrac(self) -> unyt.unyt_quantity:
"""
Total metal mass fraction of star particles.
"""
if self.Nstar == 0:
return None
return (
self.mass_star
* self.get_dataset("PartType4/MetalMassFractions")[self.star_mask_all][
self.star_mask_ap
]
).sum() / self.Mstar
@lazy_property
def star_element_fractions(self) -> unyt.unyt_array:
"""
Element mass fractions of star particles.
"""
if self.Nstar == 0:
return None
return self.get_dataset("PartType4/ElementMassFractions")[self.star_mask_all][
self.star_mask_ap
]
@lazy_property
def star_mass_O(self) -> unyt.unyt_array:
"""
Oxygen masses of star particles.
"""
if self.Nstar == 0:
return None
return (
self.star_element_fractions[
:,
self.snapshot_datasets.get_column_index(
"ElementMassFractions", "Oxygen"
),
]
* self.mass_star
)
@lazy_property
def star_mass_Mg(self) -> unyt.unyt_array:
"""
Magnesium mass fractions of star particles.
"""
if self.Nstar == 0:
return None
return (
self.star_element_fractions[
:,
self.snapshot_datasets.get_column_index(
"ElementMassFractions", "Magnesium"
),
]
* self.mass_star
)
@lazy_property
def star_mass_Fe(self) -> unyt.unyt_array:
"""
Iron mass fractions of star particles.
"""
if self.Nstar == 0:
return None
return (
self.star_element_fractions[
:,
self.snapshot_datasets.get_column_index("ElementMassFractions", "Iron"),
]
* self.mass_star
)
@lazy_property
def starOfrac(self) -> unyt.unyt_quantity:
"""
Total oxygen mass fraction of star particles.
"""
if self.Nstar == 0 or self.Mstar == 0.0:
return None
return self.star_mass_O.sum() / self.Mstar
@lazy_property
def starMgfrac(self) -> unyt.unyt_quantity:
"""
Total magnesium mass fraction of star particles.
"""
if self.Nstar == 0 or self.Mstar == 0.0:
return None
return self.star_mass_Mg.sum() / self.Mstar
@lazy_property
def starFefrac(self) -> unyt.unyt_quantity:
"""
Total iron mass fraction of star particles.
"""
if self.Nstar == 0 or self.Mstar == 0.0:
return None
return self.star_mass_Fe.sum() / self.Mstar
@lazy_property
def stellar_ages(self) -> unyt.unyt_array:
"""
Ages of star particles.
Note that these are computed from the birth scale factor using the
provided StellarAgeCalculator (which uses the correct cosmology and
snapshot redshift).
"""
if self.Nstar == 0:
return None
birth_a = self.get_dataset("PartType4/BirthScaleFactors")[self.star_mask_all][
self.star_mask_ap
]
return self.stellar_age_calculator.stellar_age(birth_a)
@lazy_property
def star_mass_fraction(self) -> unyt.unyt_array:
"""
Mass fraction of each star particle.
Used to avoid numerical overflow in calculations like
com = (mass_star * pos_star).sum() / Mstar
by rewriting it as
com = ((mass_star / Mstar) * pos_star).sum()
= (star_mass_fraction * pos_star).sum()
This is more accurate, since the stellar mass fractions are numbers
of the order of 1e-5 or so, while the masses themselves can be much
larger, if expressed in the wrong units (and that is up to unyt).
"""
if self.Mstar == 0:
return None
return self.mass_star / self.Mstar
@lazy_property
def stellar_age_mw(self) -> unyt.unyt_quantity:
"""
Mass-weighted average stellar age.
"""
if self.Nstar == 0 or self.Mstar == 0:
return None
return (self.star_mass_fraction * self.stellar_ages).sum()
@lazy_property
def stellar_age_lw(self) -> unyt.unyt_quantity:
"""
Luminosity-weighted average stellar age.
"""
if self.Nstar == 0:
return None
Lr = self.stellar_luminosities[
:, self.snapshot_datasets.get_column_index("Luminosities", "GAMA_r")
]
Lrtot = Lr.sum()
if Lrtot == 0:
return None
return ((Lr / Lrtot) * self.stellar_ages).sum()
@lazy_property
def TotalSNIaRate(self) -> unyt.unyt_quantity:
"""
Total SNIa rate.
"""
if self.Nstar == 0:
return None
return self.get_dataset("PartType4/SNIaRates")[self.star_mask_all][
self.star_mask_ap
].sum()
@lazy_property
def bh_mask_all(self) -> NDArray[bool]:
"""
Mask for masking out BH particles in raw PartType5 arrays.
This is the mask that masks out unbound particles for exclusive halos.
For inclusive halos, this mask does nothing.
"""
if self.Nbh == 0:
return None
groupnr_bound = self.get_dataset("PartType5/GroupNr_bound")
if self.inclusive:
return np.ones(groupnr_bound.shape, dtype=bool)
else:
return groupnr_bound == self.index
@lazy_property
def BH_subgrid_masses(self) -> unyt.unyt_array:
"""
Subgrid masses of BH particles.
"""
return self.get_dataset("PartType5/SubgridMasses")[self.bh_mask_all][
self.bh_mask_ap
]
@lazy_property
def Mbh_subgrid(self) -> unyt.unyt_quantity:
"""
Total subgrid mass of BH particles.
"""
if self.Nbh == 0:
return None
return self.BH_subgrid_masses.sum()
@lazy_property
def agn_eventa(self) -> unyt.unyt_array:
"""
Last AGN feedback event scale factors for BH particles.
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/LastAGNFeedbackScaleFactors")[
self.bh_mask_all
][self.bh_mask_ap]
@lazy_property
def BHlasteventa(self) -> unyt.unyt_quantity:
"""
Maximum AGN feedback scale factor among all BH particles.
"""
if self.Nbh == 0:
return None
return np.max(self.agn_eventa)
@lazy_property
def BlackHolesTotalInjectedThermalEnergy(self) -> unyt.unyt_quantity:
"""
Total thermal energy injected into gas particles by all BH particles.
"""
if self.Nbh == 0:
return None
return np.sum(
self.get_dataset("PartType5/AGNTotalInjectedEnergies")[self.bh_mask_all][
self.bh_mask_ap
]
)
@lazy_property
def BlackHolesTotalInjectedJetEnergy(self) -> unyt.unyt_quantity:
"""
Total jet energy injected into gas particles by all BH particles.
"""
if self.Nbh == 0:
return None
return np.sum(
self.get_dataset("PartType5/InjectedJetEnergies")[self.bh_mask_all][
self.bh_mask_ap
]
)
@lazy_property
def iBHmax(self) -> int:
"""
Index of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return np.argmax(self.BH_subgrid_masses)
@lazy_property
def BHmaxM(self) -> unyt.unyt_quantity:
"""
Sub-grid mass of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.BH_subgrid_masses[self.iBHmax]
@lazy_property
def BHmaxID(self) -> int:
"""
ID of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/ParticleIDs")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def BHmaxpos(self) -> unyt.unyt_array:
"""
Position of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/Coordinates")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def BHmaxvel(self) -> unyt.unyt_array:
"""
Velocity of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/Velocities")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def BHmaxAR(self) -> unyt.unyt_quantity:
"""
Accretion rate of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/AccretionRates")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleAveragedAccretionRate(self) -> unyt.unyt_quantity:
"""
Averaged accretion rate of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/AveragedAccretionRates")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleInjectedThermalEnergy(self) -> unyt.unyt_quantity:
"""
Total energy injected into gas particles by the most massive
BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/AGNTotalInjectedEnergies")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleAccretionMode(self) -> unyt.unyt_quantity:
"""
Accretion flow regime of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/AccretionModes")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleGWMassLoss(self) -> unyt.unyt_quantity:
"""
Cumulative mass lost to GW of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/GWMassLosses")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleInjectedJetEnergyByMode(self) -> unyt.unyt_quantity:
"""
Total energy injected in the kinetic jet AGN feedback mode, split by accretion mode,
of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/InjectedJetEnergiesByMode")[
self.bh_mask_all
][self.bh_mask_ap][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleLastJetEventScalefactor(self) -> unyt.unyt_quantity:
"""
Scale-factor of last jet event of the most massive BH particle (largest sub-grid mass).
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/LastAGNJetScaleFactors")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleNumberOfAGNEvents(self) -> unyt.unyt_quantity:
"""
Number of AGN events the most massive black hole has had so far.
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/NumberOfAGNEvents")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleNumberOfAGNJetEvents(self) -> unyt.unyt_quantity:
"""
Number of jet events the most massive black hole has had so far.
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/NumberOfAGNJetEvents")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleNumberOfMergers(self) -> unyt.unyt_quantity:
"""
Number of mergers the most massive black hole has had so far.
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/NumberOfMergers")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleRadiatedEnergyByMode(self) -> unyt.unyt_quantity:
"""
The total energy launched into radiation by the most massive black hole, split by accretion mode.
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/RadiatedEnergiesByMode")[self.bh_mask_all][
self.bh_mask_ap
][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleTotalAccretedMassesByMode(self) -> unyt.unyt_quantity:
"""
The total mass accreted by the most massive black hole, split by accretion mode.
"""
if self.Nbh == 0:
return None
return self.get_dataset("PartType5/TotalAccretedMassesByMode")[
self.bh_mask_all
][self.bh_mask_ap][self.iBHmax]
@lazy_property
def MostMassiveBlackHoleWindEnergyByMode(self) -> unyt.unyt_quantity:
"""
The total energy launched into accretion disc winds by the most massive black hole, split by accretion mode.
"""
if self.Nbh == 0:
return None