forked from jchelly/SOAP
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread_rockstar.py
306 lines (237 loc) · 9.98 KB
/
read_rockstar.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
#!/bin/env python
import os
import numpy as np
import pandas as pd
import unyt
import virgo.mpi.parallel_hdf5 as phdf5
from virgo.formats.rockstar import HalosFile
def locate_files(basename):
"""
Generate format strings for rockstar binary and merger tree files,
given the path to a binary file without the trailing .N.bin.
Assumes we have a filenames along the lines of
{rockstar_dir}/snapshot_XXXX/halos_XXXX.0.bin
...
{rockstar_dir}/merger_tree/snapshot_XXXX/parents_XXXX.list
...
"""
snap_format_string = basename + ".%(file_nr)d.bin"
# Check that the first snapshot file exists
snap_file = snap_format_string % {"file_nr": 0}
if not (os.path.exists(snap_file)):
raise IOError("Snapshot file does not exist: " + snap_file)
# Get the number of binary files
n_bin_file = 1
while os.path.exists(snap_format_string % {"file_nr": n_bin_file}):
n_bin_file += 1
# Find the base directory
top_dir = os.path.dirname(os.path.dirname(snap_file))
# Get the snapshot number from the filename
snap_nr = int(basename[-4:])
group_dir = f"{top_dir}/merger_tree/snapshot_{snap_nr:04d}/"
group_format_string = f"{group_dir}parents_{snap_nr:04d}.%(file_nr)04d.list"
# Check group file exists
if not (os.path.exists(group_format_string % {"file_nr": 0})):
raise IOError("Group file does not exist: " + group_format_string)
# Get the number of group files
n_group_file = 1
while os.path.exists(group_format_string % {"file_nr": n_group_file}):
n_group_file += 1
return snap_format_string, group_format_string, n_bin_file, n_group_file
def read_rockstar_groupnr(basename):
"""
Read particle IDs and group numbers from rockstar binary output.
basename should be the name of the rockstar binary files without the
trailing .N.bin
"""
from mpi4py import MPI
comm = MPI.COMM_WORLD
comm_rank = comm.Get_rank()
comm_size = comm.Get_size()
# Locate the snapshot and fof_subhalo_tab files
if comm_rank == 0:
snap_format_string, _, n_bin_file, _ = locate_files(basename)
else:
snap_format_string = None
n_bin_file = None
snap_format_string, n_bin_file = comm.bcast((snap_format_string, n_bin_file))
# Assign files to ranks
files_on_rank = phdf5.assign_files(n_bin_file, comm_size)
first_file = np.cumsum(files_on_rank) - files_on_rank
# Determine total number of halos and particles processed by this rank
local_nr_halos = 0
n_part = 0
for file_nr in range(
first_file[comm_rank], first_file[comm_rank] + files_on_rank[comm_rank]
):
filename = snap_format_string % {"file_nr": file_nr}
halo_file = HalosFile(filename, hydro=True)
halo_file.sanity_check()
local_nr_halos += halo_file["Header"].attrs["num_halos"]
n_part += halo_file["IDs"].shape[0]
local_ids = np.zeros(n_part, dtype="int64")
local_grnr = np.zeros(n_part, dtype="int64")
offset = 0
for file_nr in range(
first_file[comm_rank], first_file[comm_rank] + files_on_rank[comm_rank]
):
filename = snap_format_string % {"file_nr": file_nr}
halo_file = HalosFile(filename, hydro=True)
n_part_file = halo_file["IDs"].shape[0]
# Store particle ids associated with all halos in this file
local_ids[offset : offset + n_part_file] = halo_file["IDs"]
# Store halo id of each halo in this file
file_offset = 0
for halo_id, halo_offset in zip(
halo_file["Halo"]["id"], halo_file["Halo"]["num_p"]
):
local_grnr[
offset + file_offset : offset + file_offset + halo_offset
] = halo_id
file_offset += halo_offset
offset += n_part_file
total_nr_halos = comm.allreduce(local_nr_halos)
return total_nr_halos, local_ids, local_grnr
def read_rockstar_catalogue(comm, basename, a_unit, registry, boxsize):
"""
Read in the Rockstar halo catalogue, distributed over communicator comm.
comm - communicator to distribute catalogue over
basename - Rockstar binary filename without the .N suffix
a_unit - unyt a factor
registry - unyt unit registry
boxsize - box size as a unyt quantity
Returns a dict of unyt arrays with the halo properies.
Arrays which must always be returned:
index - index of each halo in the input catalogue
cofp - (N,3) array with centre to use for SO calculations
search_radius - initial search radius which includes all member particles
is_central - integer 1 for centrals, 0 for satellites
nr_bound_part - number of bound particles in each halo
Any other arrays will be passed through to the output ONLY IF they are
documented in property_table.py.
"""
comm_size = comm.Get_size()
comm_rank = comm.Get_rank()
# Locate the snapshot and fof_subhalo_tab files
if comm_rank == 0:
snap_format_string, group_format_string, _, n_group_file = locate_files(
basename
)
else:
snap_format_string = None
group_format_string = None
n_group_file = None
snap_format_string, group_format_string, n_group_file = comm.bcast(
(snap_format_string, group_format_string, n_group_file)
)
# Assign files to ranks
files_on_rank = phdf5.assign_files(n_group_file, comm_size)
first_file = np.cumsum(files_on_rank) - files_on_rank
# Extract properties from group catalogue files
local_halo = {
"index": [],
"cofp": [],
"is_central": [],
"PID": [],
"DescID": [],
"nr_bound_part": [],
"search_radius": [],
}
for file_nr in range(
first_file[comm_rank], first_file[comm_rank] + files_on_rank[comm_rank]
):
filename = group_format_string % {"file_nr": file_nr}
with open(filename, "r") as file:
cols = file.readline()[1:]
cols = cols.split()
data = pd.read_csv(filename, names=cols, comment="#", delim_whitespace=True)
local_halo["index"].append(np.array(data["ID"]))
# Note this is not the most bound particle
x = np.array(data["X"]).reshape(-1, 1)
y = np.array(data["Y"]).reshape(-1, 1)
z = np.array(data["Z"]).reshape(-1, 1)
local_halo["cofp"].append(np.concatenate([x, y, z], axis=1))
parent_id = np.array(data["PID"])
local_halo["is_central"].append(parent_id == -1)
local_halo["PID"].append(parent_id)
local_halo["DescID"].append(np.array(data["DescID"]))
local_halo["nr_bound_part"].append(np.array(data["Np"]))
local_halo["search_radius"].append(np.array(data["Rvir"]))
# Get SWIFT's definition of physical and comoving Mpc units
swift_pmpc = unyt.Unit("swift_mpc", registry=registry)
swift_cmpc = unyt.Unit(a_unit * swift_pmpc, registry=registry)
a = a_unit.base_value
# Get hubble param
snap_file = snap_format_string % {"file_nr": 0}
halo_file = HalosFile(snap_file, hydro=True)
hubble_param = halo_file["Header"].attrs["h0"]
# Unit conversion and creation of unyt arrays
for name in local_halo:
if local_halo[name]:
local_halo[name] = np.concatenate(local_halo[name], axis=0)
# Handling cases where rank didn't process any files
elif name == "cofp":
local_halo[name] = np.array([]).reshape(-1, 3)
else:
local_halo[name] = np.array([])
if name == "cofp":
# Rockstar units are Mpc/h (comoving)
local_halo[name] = unyt.unyt_array(
(local_halo[name] / hubble_param) * swift_cmpc, registry=registry
)
elif name == "search_radius":
# Rockstar units are kpc/h (comoving)
local_halo[name] *= a / (hubble_param * 1000)
local_halo[name] = unyt.unyt_array(
local_halo[name] * swift_pmpc, registry=registry
)
else:
local_halo[name] = unyt.unyt_array(
local_halo[name], dtype=int, units=unyt.dimensionless, registry=registry
)
return local_halo
def test_read_rockstar_groupnr(basename):
"""
Read in rockstar group numbers and compute the number of particles
in each group. This is then compared with the input catalogue as a
sanity check on the group membershp files.
"""
from mpi4py import MPI
comm = MPI.COMM_WORLD
comm_rank = comm.Get_rank()
comm_size = comm.Get_size()
_, ids, grnr = read_rockstar_groupnr(basename)
del ids # Don't need the particle IDs
# Find maximum group number
max_grnr = comm.allreduce(np.amax(grnr), op=MPI.MAX)
nr_groups_from_grnr = max_grnr + 1
if comm_rank == 0:
print(f"Number of groups from membership files = {nr_groups_from_grnr}")
# Discard particles in no group
keep = grnr >= 0
grnr = grnr[keep]
# Compute group sizes
import virgo.mpi.parallel_sort as psort
nbound_from_grnr = psort.parallel_bincount(grnr, comm=comm)
# Rockstar outputs are csv, so can't use phdf5 to read
if comm_rank == 0:
snap_format_string, group_format_string, _, n_group_file = locate_files(
basename
)
for file_nr in range(n_group_file):
filename = group_format_string % {"file_nr": file_nr}
with open(filename, "r") as file:
cols = file.readline()[1:]
cols = cols.split()
data = pd.read_csv(filename, names=cols, comment="#", delim_whitespace=True)
# Extract halo ids and number of particles from group files
halo_ids = np.array(data["ID"], dtype=int)
num_p = np.array(data["Np"], dtype=int)
# Compare
if not np.all(nbound_from_grnr[halo_ids] == num_p):
different = nbound_from_grnr[halo_ids] != num_p
print("The following halo ids differ:", halo_ids[different])
if __name__ == "__main__":
import sys
basename = sys.argv[1]
test_read_rockstar_groupnr(basename)