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_functions.py

225 lines
6.5 KiB
Python
Raw Normal View History

2022-07-21 14:05:19 +02:00
from math import log
2022-07-12 10:12:34 +02:00
from pathlib import Path
import numpy as np
2022-12-13 11:42:07 +01:00
from colossus.cosmology import cosmology
from colossus.lss import mass_function
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
2022-07-21 14:05:19 +02:00
from paths import base_dir, has_1024_simulations
from read_vr_files import read_velo_halos
2022-07-21 14:10:34 +02:00
from utils import print_progress, figsize_from_page_fraction
2022-06-10 11:06:32 +02:00
2022-06-08 11:47:02 +02:00
def counts_without_inf(number_halos):
with np.errstate(divide="ignore", invalid="ignore"):
2022-06-10 11:06:32 +02:00
number_halos_inverse = 1 / np.sqrt(number_halos)
2022-06-08 11:47:02 +02:00
number_halos_inverse[np.abs(number_halos_inverse) == np.inf] = 0
2022-06-01 14:21:25 +02:00
2022-06-10 11:06:32 +02:00
return number_halos_inverse
2022-06-08 11:47:02 +02:00
2022-07-12 10:12:34 +02:00
2022-07-21 14:00:32 +02:00
def monofonic_tests():
2022-07-21 14:10:34 +02:00
fig: Figure = plt.figure(figsize=figsize_from_page_fraction())
2022-06-10 11:06:32 +02:00
ax: Axes = fig.gca()
2022-07-21 14:23:30 +02:00
linestyles = ["solid", "dotted"]
resolutions = [128]
2022-07-21 14:05:19 +02:00
if has_1024_simulations:
resolutions.append(1024)
2022-07-21 14:23:30 +02:00
else:
resolutions.append(512)
2022-06-10 11:06:32 +02:00
2022-12-13 11:42:07 +01:00
plank_cosmo = cosmology.cosmologies["planck18"]
our_cosmo = {
# "sigma8": 0.807,
"H0": 0.67742 * 100,
"Om0": 0.3099,
"Ob0": 0.048891054,
"ns": 0.96822,
}
cosmology.addCosmology("ourCosmo", params={**plank_cosmo, **our_cosmo})
cosmology.setCosmology("ourCosmo")
print(cosmology.getCurrent())
2022-12-13 17:04:07 +01:00
x = None
for i, waveform in enumerate(
["DB2", "shannon", "shannon_rehalo", "resim_master", "resim_newrandom", "resim_newerrandom",
"resim_newswift"]):
2022-07-21 14:08:48 +02:00
for j, resolution in enumerate(resolutions):
2022-12-13 17:04:07 +01:00
if (waveform == "shannon_rehalo" or "resim" in waveform) and resolution != 128:
2022-12-13 11:42:07 +01:00
continue
2022-06-10 11:06:32 +02:00
print(waveform, resolution)
dir = base_dir / f"{waveform}_{resolution}_100"
halos = read_velo_halos(dir)
2022-12-13 11:42:07 +01:00
halo_masses: np.ndarray = halos["Mvir"].to_numpy() * 1e10 * 0.67742
2022-12-13 17:04:07 +01:00
# halo_masses = halo_masses[halo_masses > 0]
# halos = read_halo_file(dir/"fof_output_0004.hdf5")
# halo_masses: np.ndarray = halos["Masses"].to_numpy() * 1e10 * 0.67742
2022-07-12 10:12:34 +02:00
(
Ns,
deltas,
left_edges,
number_densities,
lower_error_limit,
upper_error_limit,
) = halo_mass_function(halo_masses)
2022-06-10 11:06:32 +02:00
ax.set_xscale("log")
ax.set_yscale("log")
2022-12-13 17:04:07 +01:00
if resolution == resolutions[-1]:
x = left_edges
2022-06-10 11:06:32 +02:00
# ax.bar(centers, number_densities, width=widths, log=True, fill=False)
name = f"{waveform} {resolution}"
ax.step(
left_edges,
2022-12-13 11:42:07 +01:00
number_densities / (0.67742 ** 3),
where="post",
2022-12-13 17:04:07 +01:00
color=f"C{i + 1}",
linestyle=linestyles[j],
label=name,
)
2022-06-10 11:06:32 +02:00
2022-12-13 17:04:07 +01:00
# ax.fill_between(
# left_edges,
# lower_error_limit,
# upper_error_limit,
# alpha=0.5,
# linewidth=0,
# step="post",
# )
2022-06-10 11:06:32 +02:00
2022-06-20 16:51:14 +02:00
# break
# break
2022-12-13 11:42:07 +01:00
mfunc = mass_function.massFunction(
2022-12-13 17:04:07 +01:00
x, 1, mdef="vir", model="tinker08", q_out="dndlnM"
2022-12-13 11:42:07 +01:00
)
2022-12-13 17:04:07 +01:00
ax.plot(x, mfunc, label="tinker08 (vir)", color="C0")
ax.set_xlabel("M")
ax.set_ylabel("dndlnM")
2022-12-13 11:42:07 +01:00
2022-12-13 17:04:07 +01:00
ax.legend()
fig.tight_layout()
fig.savefig(Path(f"~/tmp/halo_mass_function.svg").expanduser(), transparent=True)
2022-06-10 11:06:32 +02:00
plt.show()
2022-06-08 11:47:02 +02:00
2022-12-13 17:04:07 +01:00
def halo_mass_function(halo_masses, num_bins=60, sim_volume=100 ** 3):
2022-07-12 10:12:34 +02:00
bins = np.geomspace(halo_masses.min(), halo_masses.max(), num_bins + 1)
digits = np.digitize(halo_masses, bins)
number_densities = []
widths = []
centers = []
left_edges = []
Ns = []
deltas = []
for bin_id in range(num_bins):
print_progress(bin_id + 1, num_bins)
2022-07-12 10:12:34 +02:00
mass_low = bins[bin_id]
mass_high = bins[bin_id + 1]
counter = 0
for val in halo_masses:
if mass_low <= val < mass_high:
counter += 1
delta_mass = mass_high - mass_low
delta_log_mass = log(mass_high) - log(mass_low)
widths.append(delta_mass)
centers.append(mass_low + delta_mass / 2)
left_edges.append(mass_low)
values = np.where(digits == bin_id + 1)[0]
# print(halo_masses[values])
# print(values)
num_halos = values.shape[0]
assert num_halos == counter
nd = num_halos / sim_volume / delta_log_mass
number_densities.append(nd)
Ns.append(num_halos)
deltas.append(delta_mass)
deltas = np.array(deltas)
Ns = np.array(Ns)
left_edges = np.array(left_edges)
number_densities = np.array(number_densities)
lower_error_limit = number_densities - counts_without_inf(Ns) / sim_volume / deltas
upper_error_limit = number_densities + counts_without_inf(Ns) / sim_volume / deltas
return (
Ns,
deltas,
left_edges,
number_densities,
lower_error_limit,
upper_error_limit,
)
2022-07-12 10:12:34 +02:00
def hmf_from_rockstar_tree(file: Path):
masses = []
with file.open() as f:
for line in f:
if line.startswith("#"):
continue
cols = line.split()
Mvir = float(cols[10])
masses.append(Mvir)
masses = np.array(masses)
# agora_box_h = 0.702
# masses /= agora_box_h
box_size = 85.47
(
Ns,
deltas,
left_edges,
number_densities,
lower_error_limit,
upper_error_limit,
) = halo_mass_function(masses, num_bins=50, sim_volume=box_size ** 3)
2022-07-12 10:12:34 +02:00
fig: Figure = plt.figure()
ax: Axes = fig.gca()
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel("Halo Mass [$M_\\odot$]")
ax.set_ylabel("Number Density [$\\textrm{\\#}/Mpc^3/dlogM$]")
ax.step(left_edges, number_densities, where="post")
plank_cosmo = cosmology.cosmologies["planck18"]
2022-07-12 10:12:34 +02:00
auriga_cosmo = {
"sigma8": 0.807,
"H0": 70.2,
"Om0": 0.272,
"Ob0": 0.0455,
"ns": 0.961,
2022-07-12 10:12:34 +02:00
}
cosmology.addCosmology("aurigaCosmo", params={**plank_cosmo, **auriga_cosmo})
cosmology.setCosmology("aurigaCosmo")
2022-07-12 10:12:34 +02:00
print(cosmology.getCurrent())
mfunc = mass_function.massFunction(
left_edges, 1, mdef="vir", model="tinker08", q_out="dndlnM"
)
2022-07-12 10:12:34 +02:00
ax.plot(left_edges, mfunc)
ax.fill_between(
left_edges,
lower_error_limit,
upper_error_limit,
alpha=0.5,
linewidth=0,
step="post",
)
2022-07-12 10:12:34 +02:00
plt.show()
if __name__ == "__main__":
2022-07-21 14:00:32 +02:00
monofonic_tests()
# hmf_from_rockstar_tree(Path(argv[1]))