1
0
Fork 0
mirror of https://github.com/Findus23/halo_comparison.git synced 2024-09-19 16:03:50 +02:00
halo_comparison/read_vr_files.py

138 lines
4.8 KiB
Python
Raw Normal View History

2022-05-09 12:02:05 +02:00
from pathlib import Path
from typing import Dict
2022-05-09 12:02:05 +02:00
import h5py
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
2022-05-09 12:02:05 +02:00
from paths import base_dir
2022-05-09 12:02:05 +02:00
from readfiles import read_file
from utils import print_progress
2022-05-09 12:02:05 +02:00
def all_children(df, id):
subhalos: pd.DataFrame = df.loc[df.parent_halo_id == id]
2022-05-09 12:02:05 +02:00
if len(subhalos) == 0:
yield id
2022-05-09 12:02:05 +02:00
for sh, value in subhalos.iterrows():
yield from all_children(df, sh)
2022-05-09 12:02:05 +02:00
def particles_in_halo(df, particle_ids, recursivly: bool, unbound: bool):
halo_particle_ids: Dict[int, set[int]] = {}
pointer = 0
for halo_id in range(1, len(df) + 1):
halo = df.loc[halo_id]
print_progress(halo_id, len(df), halo.group_size)
if halo_id != len(df):
next_halo = df.loc[halo_id + 1]
end = int(next_halo["offset_unbound" if unbound else "offset"])
else: # special handling for last halo
end = -1
particles_in_halo = particle_ids[pointer:end]
ids = set(particles_in_halo)
halo_particle_ids[halo_id] = ids
pointer = end
if recursivly:
for halo_id in range(1, len(df) + 1):
sub_n_children = list(all_children(df, halo_id)) # IDs of children, subchildren, ...
if not sub_n_children:
continue
for child_id in sub_n_children:
child_particles = halo_particle_ids[child_id]
halo_particle_ids[halo_id].update(child_particles)
return halo_particle_ids
2022-05-11 14:22:34 +02:00
def read_velo_halos(directory: Path, recursivly=True, skip_unbound=False):
group_catalog = h5py.File(directory / "vroutput.catalog_groups")
2022-05-11 14:22:34 +02:00
group_properties = h5py.File(directory / "vroutput.properties")
df = pd.DataFrame(
{
"group_size": group_catalog["Group_Size"],
"offset": group_catalog["Offset"],
"offset_unbound": group_catalog["Offset_unbound"],
"parent_halo_id": group_catalog["Parent_halo_ID"],
2022-05-11 14:22:34 +02:00
"X": group_properties["Xc"],
"Y": group_properties["Yc"],
"Z": group_properties["Zc"],
"Rvir": group_properties["Rvir"],
2022-05-12 16:03:43 +02:00
"Mass_tot": group_properties["Mass_200crit"]
}
)
df.index += 1 # set Halo IDs start at 1
2022-05-09 12:02:05 +02:00
particle_catalog = h5py.File(directory / "vroutput.catalog_particles")
particle_ids = particle_catalog["Particle_IDs"][:]
2022-05-09 12:02:05 +02:00
particle_catalog_unbound = h5py.File(
directory / "vroutput.catalog_particles.unbound"
)
particle_ids_unbound = particle_catalog_unbound["Particle_IDs"][:]
2022-05-09 12:02:05 +02:00
print("look up bound particle IDs")
halo_particle_ids = particles_in_halo(df, particle_ids, recursivly, unbound=False)
2022-05-11 14:22:34 +02:00
if skip_unbound:
halo_particle_unbound_ids = {}
else:
print("look up unbound particle IDs")
halo_particle_unbound_ids = particles_in_halo(df, particle_ids_unbound, recursivly, unbound=True)
return df, halo_particle_ids, halo_particle_unbound_ids
2022-05-09 12:02:05 +02:00
def main():
waveform = "shannon"
2022-05-11 14:22:34 +02:00
Nres = 512
directory = base_dir / f"{waveform}_{Nres}_100"
2022-05-09 12:02:05 +02:00
2022-05-11 14:22:34 +02:00
df_halo, halo_particle_ids, halo_particle_unbound_ids = read_velo_halos(directory, recursivly=True)
particles, meta = read_file(directory)
HALO = 1000
while True:
fig: Figure = plt.figure()
ax: Axes = fig.gca()
2022-05-09 12:02:05 +02:00
bound_particles = particles.loc[list(halo_particle_ids[HALO])]
unbound_particles = particles.loc[list(halo_particle_unbound_ids[HALO])]
ax.scatter(bound_particles.X, bound_particles.Y, label="bound", s=1)
ax.scatter(unbound_particles.X, unbound_particles.Y, label="unbound", s=1)
plt.show()
HALO += 1
if __name__ == '__main__':
main()
2022-05-09 12:02:05 +02:00
# #This could be used to make a 2-D plot, but that doesn't teach us much about the halos:
# particles, _ = read_file(directory)
# print(particles)
# print(particle_ids_in_halo)
# bound_particles = particles.loc[list(particle_ids_in_halo)]
# unbound_particles = particles.loc[list(particle_ids_unbound_in_halo)]
2022-05-09 12:02:05 +02:00
# plt.scatter(bound_particles['X'], bound_particles['Z'], label='bound', s=1)
# plt.scatter(unbound_particles['X'], unbound_particles['Z'], label='unbound', s=1)
# plt.legend()
# plt.show()
2022-05-09 12:02:05 +02:00
# #This was an initial test to see if reading in works as expected
# lower_offset = df['offset'][test_group]
# higher_offset = df['offset'][test_group + 1]
# particle_ids_in_halo = set(particle_ids[lower_offset:higher_offset])
2022-05-09 12:02:05 +02:00
# lower_offset_unbound = df['offset_unbound'][test_group]
# higher_offset_unbound = df['offset_unbound'][test_group + 1]
# particle_ids_unbound_in_halo = set(particle_ids_unbound[lower_offset_unbound:higher_offset_unbound])
2022-05-09 12:02:05 +02:00
# size = df['group_size'][test_group]
2022-05-09 12:02:05 +02:00
# assert size == len(particle_ids_in_halo) + len(particle_ids_unbound_in_halo)