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

111 lines
3.5 KiB
Python
Raw Permalink Normal View History

2022-06-01 15:09:40 +02:00
import sys
from pathlib import Path
2022-08-30 15:35:00 +02:00
from typing import Dict
2022-06-01 15:09:40 +02:00
2022-05-05 10:40:25 +02:00
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.axes import Axes
from matplotlib.figure import Figure
2022-06-01 15:09:40 +02:00
from readfiles import ParticlesMeta, read_file, read_halo_file
from temperatures import calculate_T
from utils import print_progress
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
def halo_mass_profile(
positions: np.ndarray,
2022-08-30 15:35:00 +02:00
center: np.ndarray,
particles_meta: ParticlesMeta,
rmin: float,
rmax: float,
plot=False,
num_bins=30,
):
2022-06-14 10:53:19 +02:00
distances = np.linalg.norm(positions - center, axis=1)
2022-05-05 10:40:25 +02:00
2022-08-30 15:35:00 +02:00
log_radial_bins = np.geomspace(rmin, rmax, 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}$')
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
def property_profile(positions: np.ndarray, center: np.ndarray, masses: np.ndarray, properties: Dict[str, np.ndarray],
2022-08-30 15:35:00 +02:00
rmin: float, rmax: float, num_bins: int):
distances = np.linalg.norm(positions - center, axis=1)
2022-08-30 15:35:00 +02:00
log_radial_bins = np.geomspace(rmin, rmax, num_bins)
means = {}
for key in properties.keys():
means[key] = []
for k in range(num_bins - 1):
bin_start = log_radial_bins[k]
bin_end = log_radial_bins[k + 1]
print_progress(k, num_bins - 2, bin_end)
in_bin = np.where((bin_start < distances) & (distances < bin_end))[0]
masses_in_ring = masses[in_bin]
2022-08-30 15:35:00 +02:00
for property, property_values in properties.items():
if property == "InternalEnergies":
continue
prop_in_ring = property_values[in_bin]
if property == "Temperatures":
prop_in_ring = np.array([calculate_T(u) for u in prop_in_ring])
# mean_in_ring_unweighted = np.mean(prop_in_ring)
mean_in_ring = (prop_in_ring * masses_in_ring).sum() / masses_in_ring.sum()
# print(mean_in_ring_unweighted, mean_in_ring)
2022-08-30 15:35:00 +02:00
means[property].append(mean_in_ring)
return log_radial_bins, means
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]
halo_mass_profile(particles_in_halo[["X", "Y", "Z"]].to_numpy(), halo, particles_meta, plot=True)