2022-06-14 16:38:50 +02:00
|
|
|
import pickle
|
2022-06-10 11:04:54 +02:00
|
|
|
from dataclasses import dataclass
|
2022-06-14 16:38:50 +02:00
|
|
|
from enum import Enum
|
2022-06-10 11:04:54 +02:00
|
|
|
from pathlib import Path
|
2022-06-23 10:07:10 +02:00
|
|
|
from pprint import pprint
|
2022-06-24 16:55:16 +02:00
|
|
|
from typing import List, Tuple
|
2022-06-10 11:04:54 +02:00
|
|
|
|
2022-06-21 11:11:21 +02:00
|
|
|
import h5py
|
2022-06-10 11:04:54 +02:00
|
|
|
import numpy as np
|
2022-06-21 11:11:21 +02:00
|
|
|
import pandas as pd
|
2022-06-10 11:04:54 +02:00
|
|
|
from matplotlib import pyplot as plt
|
|
|
|
from matplotlib.axes import Axes
|
|
|
|
from matplotlib.colors import LogNorm
|
|
|
|
from matplotlib.figure import Figure
|
|
|
|
|
|
|
|
from cic import cic_from_radius
|
2022-06-14 16:38:50 +02:00
|
|
|
from halo_mass_profile import halo_mass_profile
|
2022-06-24 16:55:16 +02:00
|
|
|
from nfw import fit_nfw
|
2022-06-14 16:38:50 +02:00
|
|
|
from paths import auriga_dir, richings_dir
|
2022-06-28 16:59:44 +02:00
|
|
|
from read_vr_files import read_velo_halos
|
2022-06-21 11:11:21 +02:00
|
|
|
from readfiles import read_file, read_halo_file, ParticlesMeta
|
2022-06-14 10:53:19 +02:00
|
|
|
from utils import read_swift_config
|
|
|
|
|
|
|
|
|
2022-06-14 16:38:50 +02:00
|
|
|
class Mode(Enum):
|
|
|
|
richings = 1
|
|
|
|
auriga6 = 2
|
|
|
|
|
|
|
|
|
2022-06-28 16:59:44 +02:00
|
|
|
mode = Mode.auriga6
|
2022-06-14 16:38:50 +02:00
|
|
|
|
2022-06-14 10:53:19 +02:00
|
|
|
|
|
|
|
def dir_name_to_parameter(dir_name: str):
|
2022-06-14 16:38:50 +02:00
|
|
|
return map(int, dir_name.lstrip("auriga6_halo").lstrip("richings21_").split("_"))
|
2022-06-14 10:53:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def levelmax_to_softening_length(levelmax: int) -> float:
|
2022-06-14 16:38:50 +02:00
|
|
|
box_size = 100
|
|
|
|
return box_size / 30 / 2 ** levelmax
|
2022-06-10 11:04:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
fig1: Figure = plt.figure(figsize=(9, 6))
|
|
|
|
ax1: Axes = fig1.gca()
|
|
|
|
fig2: Figure = plt.figure(figsize=(9, 6))
|
|
|
|
ax2: Axes = fig2.gca()
|
|
|
|
|
2022-06-14 10:53:19 +02:00
|
|
|
for ax in [ax1, ax2]:
|
2022-06-17 17:29:13 +02:00
|
|
|
ax.set_xlabel(r'R [Mpc]')
|
2022-06-10 11:04:54 +02:00
|
|
|
ax1.set_ylabel(r'M [$10^{10} \mathrm{M}_\odot$]')
|
|
|
|
ax2.set_ylabel("density [$\\frac{10^{10} \\mathrm{M}_\\odot}{Mpc^3}$]")
|
|
|
|
|
2022-06-14 16:38:50 +02:00
|
|
|
part_numbers = []
|
|
|
|
|
2022-06-21 11:11:21 +02:00
|
|
|
reference_file = Path(f"auriga_reference_{mode}.pickle")
|
2022-06-14 16:38:50 +02:00
|
|
|
|
2022-06-23 10:07:10 +02:00
|
|
|
centers = {}
|
|
|
|
|
2022-06-10 11:04:54 +02:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Result:
|
|
|
|
title: str
|
|
|
|
rho: np.ndarray
|
2022-06-24 16:55:16 +02:00
|
|
|
levels: Tuple[int, int, int]
|
2022-06-03 15:20:12 +02:00
|
|
|
|
2022-06-10 11:04:54 +02:00
|
|
|
|
|
|
|
images = []
|
|
|
|
vmin = np.Inf
|
|
|
|
vmax = -np.Inf
|
2022-06-14 16:38:50 +02:00
|
|
|
root_dir = auriga_dir if mode == Mode.auriga6 else richings_dir
|
2022-06-24 16:55:16 +02:00
|
|
|
i = 0
|
|
|
|
for dir in sorted(root_dir.glob("*")):
|
|
|
|
if not dir.is_dir() or "bak" in dir.name:
|
|
|
|
continue
|
2022-06-14 10:53:19 +02:00
|
|
|
is_by_adrian = "arj" in dir.name
|
|
|
|
print(dir.name)
|
2022-06-10 11:04:54 +02:00
|
|
|
|
2022-06-14 10:53:19 +02:00
|
|
|
if not is_by_adrian:
|
|
|
|
levelmin, levelmin_TF, levelmax = dir_name_to_parameter(dir.name)
|
|
|
|
print(levelmin, levelmin_TF, levelmax)
|
2022-06-14 16:38:50 +02:00
|
|
|
# if levelmax != 12:
|
2022-06-14 10:53:19 +02:00
|
|
|
# continue
|
2022-06-03 15:20:12 +02:00
|
|
|
|
|
|
|
input_file = dir / "output_0007.hdf5"
|
2022-06-14 16:38:50 +02:00
|
|
|
if mode == Mode.richings:
|
|
|
|
input_file = dir / "output_0004.hdf5"
|
2022-06-03 15:20:12 +02:00
|
|
|
if is_by_adrian:
|
2022-06-10 11:04:54 +02:00
|
|
|
input_file = dir / "output_0000.hdf5"
|
2022-06-14 10:53:19 +02:00
|
|
|
softening_length = None
|
|
|
|
else:
|
2022-06-24 16:55:16 +02:00
|
|
|
try:
|
|
|
|
swift_conf = read_swift_config(dir)
|
|
|
|
except FileNotFoundError:
|
|
|
|
continue
|
2022-06-14 10:53:19 +02:00
|
|
|
softening_length = swift_conf["Gravity"]["comoving_DM_softening"]
|
|
|
|
assert softening_length == swift_conf["Gravity"]["max_physical_DM_softening"]
|
2022-06-14 16:38:50 +02:00
|
|
|
ideal_softening_length = levelmax_to_softening_length(levelmax)
|
|
|
|
# if not np.isclose(softening_length, levelmax_to_softening_length(levelmax)):
|
|
|
|
# raise ValueError(f"softening length for levelmax {levelmax} should be {ideal_softening_length} "
|
|
|
|
# f"but is {softening_length}")
|
2022-06-03 15:20:12 +02:00
|
|
|
print(input_file)
|
2022-06-21 11:11:21 +02:00
|
|
|
if mode == Mode.richings and is_by_adrian:
|
2022-06-21 16:26:42 +02:00
|
|
|
h = 0.6777
|
2022-06-21 11:11:21 +02:00
|
|
|
with h5py.File(dir / "Richings_object_z0.h5") as f:
|
2022-06-21 16:26:42 +02:00
|
|
|
df = pd.DataFrame(f["Coordinates"][:] / h, columns=["X", "Y", "Z"])
|
2022-06-21 11:11:21 +02:00
|
|
|
particles_meta = ParticlesMeta(particle_mass=1.1503e7 / 1e10)
|
2022-06-21 16:26:42 +02:00
|
|
|
center = np.array([60.7, 29, 64]) / h
|
2022-06-21 11:11:21 +02:00
|
|
|
softening_length = None
|
|
|
|
else:
|
|
|
|
df, particles_meta = read_file(input_file)
|
|
|
|
df_halos = read_halo_file(input_file.with_name("fof_" + input_file.name))
|
2022-06-28 16:59:44 +02:00
|
|
|
vr_halo = read_velo_halos(dir, veloname="velo_out").loc[1]
|
2022-06-21 11:11:21 +02:00
|
|
|
# particles_in_halo = df.loc[df["FOFGroupIDs"] == 3]
|
|
|
|
|
|
|
|
halo_id = 1
|
|
|
|
while True:
|
|
|
|
particles_in_halo = df.loc[df["FOFGroupIDs"] == halo_id]
|
|
|
|
if len(particles_in_halo) > 1:
|
|
|
|
break
|
|
|
|
halo_id += 1
|
|
|
|
|
|
|
|
halo = df_halos.loc[halo_id]
|
|
|
|
part_numbers.append(len(df) * particles_meta.particle_mass)
|
|
|
|
# halo = halos.loc[1]
|
|
|
|
center = np.array([halo.X, halo.Y, halo.Z])
|
2022-06-14 16:38:50 +02:00
|
|
|
log_radial_bins, bin_masses, bin_densities, center = halo_mass_profile(
|
2022-06-21 11:11:21 +02:00
|
|
|
df, center, particles_meta, plot=False, num_bins=100,
|
2022-06-14 16:38:50 +02:00
|
|
|
vmin=0.002, vmax=6.5
|
2022-06-10 11:04:54 +02:00
|
|
|
)
|
2022-06-23 10:07:10 +02:00
|
|
|
i_min_border = np.argmax(0.01 < log_radial_bins) # first bin outside of specific radius
|
|
|
|
i_max_border = np.argmax(1.5 < log_radial_bins)
|
|
|
|
popt = fit_nfw(log_radial_bins[i_min_border:i_max_border], bin_densities[i_min_border:i_max_border]) # = rho_0, r_s
|
|
|
|
print(popt)
|
2022-06-24 16:55:16 +02:00
|
|
|
# # Plot NFW profile
|
|
|
|
# ax.loglog(
|
|
|
|
# log_radial_bins[i_min_border:i_max_border],
|
|
|
|
# nfw(log_radial_bins[i_min_border:i_max_border], *popt),
|
|
|
|
# linestyle="dotted"
|
|
|
|
# )
|
2022-06-23 10:07:10 +02:00
|
|
|
centers[dir.name] = center
|
2022-06-14 16:38:50 +02:00
|
|
|
if is_by_adrian:
|
|
|
|
with reference_file.open("wb") as f:
|
|
|
|
pickle.dump([log_radial_bins, bin_masses, bin_densities], f)
|
2022-06-10 11:04:54 +02:00
|
|
|
ax1.loglog(log_radial_bins[:-1], bin_masses, label=str(dir.name), c=f"C{i}")
|
|
|
|
|
|
|
|
ax2.loglog(log_radial_bins[:-1], bin_densities, label=str(dir.name), c=f"C{i}")
|
|
|
|
|
2022-06-21 11:11:21 +02:00
|
|
|
if reference_file.exists() and not is_by_adrian:
|
2022-06-14 16:38:50 +02:00
|
|
|
with reference_file.open("rb") as f:
|
|
|
|
data: List[np.ndarray] = pickle.load(f)
|
|
|
|
ref_log_radial_bins, ref_bin_masses, ref_bin_densities = data
|
|
|
|
mass_deviation: np.ndarray = np.abs(bin_masses - ref_bin_masses)
|
|
|
|
density_deviation: np.ndarray = np.abs(bin_densities - ref_bin_densities)
|
|
|
|
ax1.loglog(log_radial_bins[:-1], mass_deviation, c=f"C{i}",
|
|
|
|
linestyle="dotted")
|
|
|
|
|
|
|
|
ax2.loglog(log_radial_bins[:-1], density_deviation, c=f"C{i}",
|
|
|
|
linestyle="dotted")
|
|
|
|
accuracy = mass_deviation / ref_bin_masses
|
|
|
|
print(accuracy)
|
|
|
|
print("mean accuracy", accuracy.mean())
|
|
|
|
|
2022-06-14 10:53:19 +02:00
|
|
|
if softening_length:
|
|
|
|
for ax in [ax1, ax2]:
|
|
|
|
ax.axvline(4 * softening_length, color=f"C{i}", linestyle="dotted")
|
2022-06-28 16:59:44 +02:00
|
|
|
for ax in [ax1, ax2]:
|
|
|
|
ax.axvline(vr_halo.Rvir, color=f"C{i}", linestyle="dashed")
|
2022-06-10 11:04:54 +02:00
|
|
|
|
|
|
|
X, Y, Z = df.X.to_numpy(), df.Y.to_numpy(), df.Z.to_numpy()
|
2022-06-21 11:11:46 +02:00
|
|
|
|
2022-06-03 15:20:12 +02:00
|
|
|
# shift: (-6, 0, -12)
|
2022-06-14 16:38:50 +02:00
|
|
|
# if not is_by_adrian:
|
|
|
|
# xshift = Xc - Xc_adrian
|
|
|
|
# yshift = Yc - Yc_adrian
|
|
|
|
# zshift = Zc - Zc_adrian
|
|
|
|
# print("shift", xshift, yshift, zshift)
|
2022-06-03 15:20:12 +02:00
|
|
|
|
2022-06-14 16:38:50 +02:00
|
|
|
X -= center[0]
|
|
|
|
Y -= center[1]
|
|
|
|
Z -= center[2]
|
2022-06-10 11:04:54 +02:00
|
|
|
|
2022-06-24 16:55:16 +02:00
|
|
|
rho, extent = cic_from_radius(X, Z, 1000, 0, 0, 5, periodic=False)
|
2022-06-03 15:20:12 +02:00
|
|
|
|
2022-06-10 11:04:54 +02:00
|
|
|
vmin = min(vmin, rho.min())
|
|
|
|
vmax = max(vmax, rho.max())
|
|
|
|
|
|
|
|
images.append(Result(
|
|
|
|
rho=rho,
|
2022-06-24 16:55:16 +02:00
|
|
|
title=str(dir.name),
|
|
|
|
levels=(levelmin, levelmin_TF, levelmax) if levelmin else None
|
2022-06-10 11:04:54 +02:00
|
|
|
))
|
2022-06-24 16:55:16 +02:00
|
|
|
i += 1
|
2022-06-10 11:04:54 +02:00
|
|
|
# plot_cic(
|
|
|
|
# rho, extent,
|
|
|
|
# title=str(dir.name)
|
|
|
|
# )
|
|
|
|
ax1.legend()
|
|
|
|
ax2.legend()
|
|
|
|
|
2022-06-14 16:38:50 +02:00
|
|
|
# fig3: Figure = plt.figure(figsize=(9, 9))
|
|
|
|
# axes: List[Axes] = fig3.subplots(3, 3, sharex=True, sharey=True).flatten()
|
2022-06-24 16:55:16 +02:00
|
|
|
fig3: Figure = plt.figure(figsize=(9, 9))
|
|
|
|
axes: List[Axes] = fig3.subplots(3, 3, sharex=True, sharey=True).flatten()
|
2022-06-10 11:04:54 +02:00
|
|
|
|
|
|
|
for result, ax in zip(images, axes):
|
|
|
|
data = 1.1 + result.rho
|
|
|
|
vmin_scaled = 1.1 + vmin
|
|
|
|
vmax_scaled = 1.1 + vmax
|
|
|
|
img = ax.imshow(data.T, norm=LogNorm(vmin=vmin_scaled, vmax=vmax_scaled), extent=extent,
|
2022-06-14 10:53:19 +02:00
|
|
|
origin="lower")
|
2022-06-10 11:04:54 +02:00
|
|
|
ax.set_title(result.title)
|
|
|
|
|
|
|
|
fig3.tight_layout()
|
|
|
|
fig3.subplots_adjust(right=0.825)
|
|
|
|
cbar_ax = fig3.add_axes([0.85, 0.15, 0.05, 0.7])
|
|
|
|
fig3.colorbar(img, cax=cbar_ax)
|
|
|
|
|
2022-06-21 11:11:21 +02:00
|
|
|
fig1.savefig(Path(f"~/tmp/auriga1.pdf").expanduser())
|
|
|
|
fig2.savefig(Path(f"~/tmp/auriga2.pdf").expanduser())
|
2022-06-10 11:04:54 +02:00
|
|
|
fig3.savefig(Path("~/tmp/auriga3.pdf").expanduser())
|
2022-06-23 10:07:10 +02:00
|
|
|
pprint(centers)
|
2022-06-10 11:04:54 +02:00
|
|
|
plt.show()
|
2022-06-14 16:38:50 +02:00
|
|
|
print(part_numbers)
|