-
Notifications
You must be signed in to change notification settings - Fork 0
/
properties.py
383 lines (313 loc) · 15 KB
/
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
import os
import sys
import unyt
import h5py
import numpy as np
import argparse
import pandas as pd
from mpi4py import MPI
from swiftsimio.visualisation.rotation import rotation_matrix_from_vector
# Make the register backend visible to the script
sys.path.append(
os.path.abspath(
os.path.join(
os.path.dirname(__file__),
os.path.pardir,
)
)
)
comm = MPI.COMM_WORLD
num_processes = comm.size
rank = comm.rank
from read import MacsisDataset
from register import Macsis
parser = argparse.ArgumentParser()
parser.add_argument(
'-r',
'--redshift-index',
type=int,
default=22,
required=False,
choices=list(range(23))
)
args = parser.parse_args()
ksz_const = - unyt.thompson_cross_section / 1.16 / unyt.speed_of_light / unyt.proton_mass
tsz_const = unyt.thompson_cross_section * unyt.boltzmann_constant / 1.16 / \
unyt.speed_of_light ** 2 / unyt.proton_mass / unyt.electron_mass
def rotate(coord: np.ndarray, angular_momentum_hot_gas: np.ndarray, tilt: str = 'x'):
if tilt == 'x':
rotation_matrix = rotation_matrix_from_vector(np.array([1, 0, 0], dtype=np.float))
elif tilt == 'y':
rotation_matrix = rotation_matrix_from_vector(np.array([0, 1, 0], dtype=np.float))
elif tilt == 'z':
rotation_matrix = rotation_matrix_from_vector(np.array([0, 0, 1], dtype=np.float))
elif tilt == 'faceon':
rotation_matrix = rotation_matrix_from_vector(angular_momentum_hot_gas)
elif tilt == 'edgeon':
rotation_matrix = rotation_matrix_from_vector(angular_momentum_hot_gas, axis='y')
new_coord = np.matmul(rotation_matrix, coord.T).T
return new_coord
def mean_velocity(halo, particle_type: str = 'gas'):
# Switch the type of angular momentum analysis
if particle_type == 'gas':
pt_number = '0'
elif particle_type == 'dm':
pt_number = '1'
elif particle_type == 'stars':
pt_number = '4'
data = MacsisDataset(halo)
# Read data
coordinates = data.read_snapshot(f'PartType{pt_number}/Coordinates')
if particle_type == 'dm':
dm_particle_mass = data.read_header('MassTable')[1]
masses = np.ones(len(coordinates), dtype=np.float) * dm_particle_mass
else:
masses = data.read_snapshot(f'PartType{pt_number}/Mass')
velocities = data.read_snapshot(f'PartType{pt_number}/Velocity')
# Remember that the largest FOF has index 1
centre_of_potential = data.read_catalogue_subfindtab('FOF/GroupCentreOfPotential')[1] / data.a
r500_crit = data.read_catalogue_subfindtab('FOF/Group_R_Crit500')[1] / data.a
# Select ionised hot gas
if particle_type == 'gas':
temperatures = data.read_snapshot(f'PartType{pt_number}/Temperature')
temperature_cut = np.where(temperatures > 1.e5)[0]
coordinates = coordinates[temperature_cut]
masses = masses[temperature_cut]
velocities = velocities[temperature_cut]
# Rescale coordinates to CoP
coordinates[:, 0] -= centre_of_potential[0]
coordinates[:, 1] -= centre_of_potential[1]
coordinates[:, 2] -= centre_of_potential[2]
# Compute mean velocity inside R500
radial_dist = np.sqrt(
coordinates[:, 0] ** 2 +
coordinates[:, 1] ** 2 +
coordinates[:, 2] ** 2
)
r500_mask = np.where(radial_dist < r500_crit)[0]
mean_velocity_r500 = np.sum(velocities[r500_mask] * masses[r500_mask, None], axis=0) / np.sum(masses[r500_mask])
return mean_velocity_r500
def velocity_dispersion(halo, particle_type: str = 'gas'):
# Switch the type of angular momentum analysis
if particle_type == 'gas':
pt_number = '0'
elif particle_type == 'dm':
pt_number = '1'
elif particle_type == 'stars':
pt_number = '4'
data = MacsisDataset(halo)
# Read data
coordinates = data.read_snapshot(f'PartType{pt_number}/Coordinates')
if particle_type == 'dm':
dm_particle_mass = data.read_header('MassTable')[1]
masses = np.ones(len(coordinates), dtype=np.float) * dm_particle_mass
else:
masses = data.read_snapshot(f'PartType{pt_number}/Mass')
velocities = data.read_snapshot(f'PartType{pt_number}/Velocity')
# Remember that the largest FOF has index 1
centre_of_potential = data.read_catalogue_subfindtab('FOF/GroupCentreOfPotential')[1] / data.a
r500_crit = data.read_catalogue_subfindtab('FOF/Group_R_Crit500')[1] / data.a
# Select ionised hot gas
if particle_type == 'gas':
temperatures = data.read_snapshot(f'PartType{pt_number}/Temperature')
temperature_cut = np.where(temperatures > 1.e5)[0]
coordinates = coordinates[temperature_cut]
masses = masses[temperature_cut]
velocities = velocities[temperature_cut]
# Rescale coordinates to CoP
coordinates[:, 0] -= centre_of_potential[0]
coordinates[:, 1] -= centre_of_potential[1]
coordinates[:, 2] -= centre_of_potential[2]
# Compute mean velocity inside R500
radial_dist = np.sqrt(
coordinates[:, 0] ** 2 +
coordinates[:, 1] ** 2 +
coordinates[:, 2] ** 2
)
r500_mask = np.where(radial_dist < r500_crit)[0]
mean_velocity_r500 = np.sum(velocities[r500_mask] * masses[r500_mask, None], axis=0) / np.sum(masses[r500_mask])
velocities_rest_frame = velocities.copy()
velocities_rest_frame[:, 0] -= mean_velocity_r500[0]
velocities_rest_frame[:, 1] -= mean_velocity_r500[1]
velocities_rest_frame[:, 2] -= mean_velocity_r500[2]
velocity_dispersion_r500 = np.sqrt(
np.sum(
np.power(velocities_rest_frame * masses[:, None], 2.),
axis=0
) / np.sum(masses[r500_mask])
)
return velocity_dispersion_r500
def angular_momentum(halo, particle_type: str = 'gas'):
# Switch the type of angular momentum analysis
if particle_type == 'gas':
pt_number = '0'
elif particle_type == 'dm':
pt_number = '1'
elif particle_type == 'stars':
pt_number = '4'
data = MacsisDataset(halo)
# Read data
coordinates = data.read_snapshot(f'PartType{pt_number}/Coordinates')
if particle_type == 'dm':
dm_particle_mass = data.read_header('MassTable')[1]
masses = np.ones(len(coordinates), dtype=np.float) * dm_particle_mass
else:
masses = data.read_snapshot(f'PartType{pt_number}/Mass')
velocities = data.read_snapshot(f'PartType{pt_number}/Velocity')
# Remember that the largest FOF has index 1
centre_of_potential = data.read_catalogue_subfindtab('FOF/GroupCentreOfPotential')[1] / data.a
r500_crit = data.read_catalogue_subfindtab('FOF/Group_R_Crit500')[1] / data.a
# Select ionised hot gas
if particle_type == 'gas':
temperatures = data.read_snapshot(f'PartType{pt_number}/Temperature')
temperature_cut = np.where(temperatures > 1.e5)[0]
coordinates = coordinates[temperature_cut]
masses = masses[temperature_cut]
velocities = velocities[temperature_cut]
# Rescale coordinates to CoP
coordinates[:, 0] -= centre_of_potential[0]
coordinates[:, 1] -= centre_of_potential[1]
coordinates[:, 2] -= centre_of_potential[2]
# Compute mean velocity inside R500
radial_dist = np.sqrt(
coordinates[:, 0] ** 2 +
coordinates[:, 1] ** 2 +
coordinates[:, 2] ** 2
)
r500_mask = np.where(radial_dist < r500_crit)[0]
mean_velocity_r500 = np.sum(velocities[r500_mask] * masses[r500_mask, None], axis=0) / np.sum(masses[r500_mask])
angular_momentum_r500 = np.sum(
np.cross(coordinates[r500_mask], velocities[r500_mask] * masses[r500_mask, None]), axis=0
) / np.sum(masses[r500_mask])
velocities_rest_frame = velocities.copy()
velocities_rest_frame[:, 0] -= mean_velocity_r500[0]
velocities_rest_frame[:, 1] -= mean_velocity_r500[1]
velocities_rest_frame[:, 2] -= mean_velocity_r500[2]
return angular_momentum_r500
def component_mass_r500(halo, particle_type: str = 'gas'):
# Switch the type of angular momentum analysis
if particle_type == 'gas':
pt_number = '0'
elif particle_type == 'dm':
pt_number = '1'
elif particle_type == 'stars':
pt_number = '4'
data = MacsisDataset(halo)
# Read data
coordinates = data.read_snapshot(f'PartType{pt_number}/Coordinates')
if particle_type == 'dm':
dm_particle_mass = data.read_header('MassTable')[1]
masses = np.ones(len(coordinates), dtype=np.float) * dm_particle_mass
else:
masses = data.read_snapshot(f'PartType{pt_number}/Mass')
velocities = data.read_snapshot(f'PartType{pt_number}/Velocity')
# Remember that the largest FOF has index 1
centre_of_potential = data.read_catalogue_subfindtab('FOF/GroupCentreOfPotential')[1] / data.a
r500_crit = data.read_catalogue_subfindtab('FOF/Group_R_Crit500')[1] / data.a
# Select ionised hot gas
if particle_type == 'gas':
temperatures = data.read_snapshot(f'PartType{pt_number}/Temperature')
temperature_cut = np.where(temperatures > 1.e5)[0]
coordinates = coordinates[temperature_cut]
masses = masses[temperature_cut]
velocities = velocities[temperature_cut]
# Rescale coordinates to CoP
coordinates[:, 0] -= centre_of_potential[0]
coordinates[:, 1] -= centre_of_potential[1]
coordinates[:, 2] -= centre_of_potential[2]
# Compute mean velocity inside R500
radial_dist = np.sqrt(
coordinates[:, 0] ** 2 +
coordinates[:, 1] ** 2 +
coordinates[:, 2] ** 2
)
r500_mask = np.where(radial_dist < r500_crit)[0]
total_mass_500 = np.sum(masses[r500_mask])
return total_mass_500
def dump_to_hdf5_parallel():
macsis = Macsis()
with h5py.File(
f'{macsis.output_dir}/properties_{args.redshift_index:03d}.hdf5', 'w',
driver='mpio', comm=comm
) as f:
# Retrieve all zoom handles in parallel (slow otherwise)
data_handles = np.empty(0, dtype=np.object)
for zoom_id in range(macsis.num_zooms):
if zoom_id % num_processes == rank:
print(f"Collecting metadata for process ({zoom_id:03d}/{macsis.num_zooms - 1})...")
data_handles = np.append(
data_handles,
macsis.get_zoom(zoom_id).get_redshift(args.redshift_index)
)
zoom_handles = comm.allgather(data_handles)
zoom_handles = np.concatenate(zoom_handles).ravel()
zoom_handles = zoom_handles[~pd.isnull(zoom_handles)]
sort_keys = np.argsort(np.array([int(z.run_name[-4:]) for z in zoom_handles]))
zoom_handles = zoom_handles[sort_keys]
if rank == 0:
print(
f"Redshift index (from argvals): {args.redshift_index:d}\n"
f"Handles working on redshift {zoom_handles[0].z:.3f}\n"
f"Analysing {len(data_handles):d} data-handles. Names printed below.\n",
[data_handle.run_name for data_handle in data_handles]
)
# Editing the structure of the file MUST be done collectively
if rank == 0:
print("Preparing structure of the file (collective operations)...")
halo_number = f.create_dataset("halo_number", (macsis.num_zooms,), dtype=np.uint16)
m_500crit = f.create_dataset("m_500crit", (macsis.num_zooms,), dtype=np.float)
r_500crit = f.create_dataset("r_500crit", (macsis.num_zooms,), dtype=np.float)
hot_gas_mass_500crit = f.create_dataset("hot_gas_mass_500crit", (macsis.num_zooms,), dtype=np.float)
dark_matter_mass_500crit = f.create_dataset("dark_matter_mass_500crit", (macsis.num_zooms,), dtype=np.float)
star_mass_500crit = f.create_dataset("star_mass_500crit", (macsis.num_zooms,), dtype=np.float)
angular_momentum_hotgas_r500 = f.create_dataset(
"angular_momentum_hotgas_r500", (macsis.num_zooms, 3), dtype=np.float
)
angular_momentum_dark_matter_r500 = f.create_dataset(
"angular_momentum_dark_matter_r500", (macsis.num_zooms, 3), dtype=np.float
)
angular_momentum_stars_r500 = f.create_dataset(
"angular_momentum_stars_r500", (macsis.num_zooms, 3), dtype=np.float
)
mean_velocity_hotgas_r500 = f.create_dataset(
"mean_velocity_hotgas_r500", (macsis.num_zooms, 3), dtype=np.float
)
mean_velocity_dark_matter_r500 = f.create_dataset(
"mean_velocity_dark_matter_r500", (macsis.num_zooms, 3), dtype=np.float
)
mean_velocity_stars_r500 = f.create_dataset(
"mean_velocity_stars_r500", (macsis.num_zooms, 3), dtype=np.float
)
velocity_dispersion_hotgas_r500 = f.create_dataset(
"velocity_dispersion_hotgas_r500", (macsis.num_zooms, 3), dtype=np.float
)
velocity_dispersion_dark_matter_r500 = f.create_dataset(
"velocity_dispersion_dark_matter_r500", (macsis.num_zooms, 3), dtype=np.float
)
velocity_dispersion_stars_r500 = f.create_dataset(
"velocity_dispersion_stars_r500", (macsis.num_zooms, 3), dtype=np.float
)
# Data assignment can be done through independent operations
for zoom_id, data_handle in enumerate(zoom_handles):
if zoom_id % num_processes == rank:
print((
f"Rank {rank:03d} processing halo ({zoom_id:03d}/{macsis.num_zooms - 1}) | "
f"MACSIS name: {data_handle.run_name}"
))
halo_number[zoom_id] = int(data_handle.run_name[-4:])
m_500crit[zoom_id] = MacsisDataset(data_handle).read_catalogue_subfindtab('FOF/Group_M_Crit500')[1]
r_500crit[zoom_id] = MacsisDataset(data_handle).read_catalogue_subfindtab('FOF/Group_R_Crit500')[1] / MacsisDataset(data_handle).a
hot_gas_mass_500crit[zoom_id] = component_mass_r500(data_handle, particle_type='gas')
dark_matter_mass_500crit[zoom_id] = component_mass_r500(data_handle, particle_type='dm')
star_mass_500crit[zoom_id] = component_mass_r500(data_handle, particle_type='stars')
angular_momentum_hotgas_r500[zoom_id] = angular_momentum(data_handle, particle_type='gas')
angular_momentum_dark_matter_r500[zoom_id] = angular_momentum(data_handle, particle_type='dm')
angular_momentum_stars_r500[zoom_id] = angular_momentum(data_handle, particle_type='stars')
mean_velocity_hotgas_r500[zoom_id] = mean_velocity(data_handle, particle_type='gas')
mean_velocity_dark_matter_r500[zoom_id] = mean_velocity(data_handle, particle_type='dm')
mean_velocity_stars_r500[zoom_id] = mean_velocity(data_handle, particle_type='stars')
velocity_dispersion_hotgas_r500[zoom_id] = velocity_dispersion(data_handle, particle_type='gas')
velocity_dispersion_dark_matter_r500[zoom_id] = velocity_dispersion(data_handle, particle_type='dm')
velocity_dispersion_stars_r500[zoom_id] = velocity_dispersion(data_handle, particle_type='stars')
if __name__ == "__main__":
dump_to_hdf5_parallel()