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

77 lines
2.3 KiB
Python
Raw Normal View History

2022-06-01 15:09:40 +02:00
import sys
from pathlib import Path
2022-05-05 10:40:25 +02:00
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.axes import Axes
from matplotlib.figure import Figure
2022-06-14 10:53:19 +02:00
from find_center import find_center
2022-06-01 15:09:40 +02:00
from readfiles import ParticlesMeta, read_file, read_halo_file
2022-05-05 11:10:07 +02:00
2022-05-05 10:40:25 +02:00
def V(r):
2022-06-14 16:38:50 +02:00
return 4 / 3 * np.pi * r ** 3
2022-05-05 10:40:25 +02:00
2022-06-21 11:11:21 +02:00
def halo_mass_profile(particles: pd.DataFrame, center: np.ndarray,
2022-06-14 16:38:50 +02:00
particles_meta: ParticlesMeta, vmin: float, vmax: float, plot=False, num_bins=30):
2022-06-14 10:53:19 +02:00
center = find_center(particles, center)
positions = particles[["X", "Y", "Z"]].to_numpy()
distances = np.linalg.norm(positions - center, axis=1)
2022-05-05 10:40:25 +02:00
group_radius = distances.max()
2022-06-14 16:38:50 +02:00
log_radial_bins = np.geomspace(vmin, vmax, num_bins)
2022-05-05 10:40:25 +02:00
2022-05-05 11:10:07 +02:00
bin_masses = []
2022-05-05 10:40:25 +02:00
bin_densities = []
for k in range(num_bins - 1):
bin_start = log_radial_bins[k]
bin_end = log_radial_bins[k + 1]
2022-06-10 11:06:32 +02:00
in_bin = np.where((bin_start < distances) & (distances < bin_end))[0]
2022-05-05 10:40:25 +02:00
count = in_bin.shape[0]
2022-05-05 11:10:07 +02:00
mass = count * particles_meta.particle_mass
2022-06-14 16:38:50 +02:00
volume = V(bin_end) - V(bin_start)
2022-05-05 11:10:07 +02:00
bin_masses.append(mass)
2022-06-02 00:12:44 +02:00
density = mass / volume
2022-05-05 10:40:25 +02:00
bin_densities.append(density)
2022-06-14 16:38:50 +02:00
bin_masses = np.array(bin_masses)
bin_densities = np.array(bin_densities)
2022-06-10 11:06:32 +02:00
bin_masses = np.cumsum(bin_masses)
2022-05-05 11:12:37 +02:00
if plot:
fig: Figure = plt.figure()
ax: Axes = fig.gca()
2022-05-05 10:40:25 +02:00
2022-05-05 11:12:37 +02:00
ax2 = ax.twinx()
2022-05-05 10:40:25 +02:00
2022-05-05 11:12:37 +02:00
ax.loglog(log_radial_bins[:-1], bin_masses, label="counts")
ax2.loglog(log_radial_bins[:-1], bin_densities, label="densities", c="C1")
2022-06-14 16:38:50 +02:00
# ax.set_xlabel(r'R / R$_\mathrm{group}$')
2022-05-05 11:12:37 +02:00
ax.set_ylabel(r'M [$10^{10} \mathrm{M}_\odot$]')
2022-06-06 23:29:44 +02:00
ax2.set_ylabel("density [$\\frac{10^{10} \\mathrm{M}_\\odot}{Mpc^3}$]")
2022-06-02 00:12:44 +02:00
plt.legend()
2022-05-05 11:12:37 +02:00
plt.show()
2022-05-05 10:40:25 +02:00
2022-06-14 16:38:50 +02:00
return log_radial_bins, bin_masses, bin_densities, center
2022-06-01 15:09:40 +02:00
if __name__ == '__main__':
input_file = Path(sys.argv[1])
df, particles_meta = read_file(input_file)
2022-06-10 11:06:32 +02:00
df_halos = read_halo_file(input_file.with_name("fof_" + input_file.name))
2022-06-01 15:09:40 +02:00
2022-06-10 11:06:32 +02:00
print(df)
2022-06-01 15:09:40 +02:00
halo_id = 1
2022-06-10 11:06:32 +02:00
while True:
particles_in_halo = df.loc[df["FOFGroupIDs"] == halo_id]
if len(particles_in_halo) > 1:
break
halo_id += 1
2022-06-01 15:09:40 +02:00
halo = df_halos.loc[halo_id]
2022-06-14 16:38:50 +02:00
halo_mass_profile(particles_in_halo, halo, particles_meta, plot=True)