40 KiB
40 KiB
In [58]:
import numpy as np
import matplotlib.pyplot as plt
import configparser
In [3]:
dirname = '/data54/lavaux/VELMASS/halo_central/halos_new/'
lam = 5
R_max = 100
Mmin = 3e12
with open(dirname + "/halos_0.0.ascii", "rb") as f:
for _ in range(30):
r = str(f.readline())
if "Units" in r:
print(r)
# Get box size
with open(dirname + '/auto-rockstar.cfg') as f:
data = [r for r in f]
Lbox = [r for r in data if r.startswith('BOX_SIZE')][0].strip()
Lbox = float(Lbox.split('=')[1])
origin = np.array([Lbox/2, Lbox/2, Lbox/2])
omega_m = [r for r in data if r.startswith('Om')][0].strip()
omega_m = float(omega_m.split('=')[1])
halo_file = dirname + '/out.npy'
halos = np.load(halo_file)
# Cut by mass
# Mmin = ???
halos = halos[halos['Mvir'] > Mmin]
xtrue = halos['X'] - origin[0] # Mpc / h
ytrue = halos['Y'] - origin[1] # Mpc / h
ztrue = halos['Z'] - origin[2] # Mpc / h
rtrue = np.sqrt(xtrue**2 + ytrue**2 + ztrue**2) # Mpc / h
In [56]:
# Sample
Nt = 400
R_lim = 100
print(len(rtrue))
print(Lbox)
# Get relative probability of each object according to selection function
# lam = float(config[f'sample_{i}']['lam'])
# R_max = float(config['mock']['R_max'])
prob = np.exp(- lam * rtrue / R_max)
prob /= np.sum(prob)
accepted_count = 0
rsamp = np.zeros(Nt)
# Loop until we have Nt valid positions
while accepted_count < Nt:
ids = np.random.choice(len(rtrue), size=Nt, p=prob, replace=False)
# Here I would actually scatter
valid_indices = rtrue[ids] < R_lim
ids = ids[valid_indices]
# Calculate how many valid positions we need to reach Nt
remaining_needed = Nt - accepted_count
selected_count = min(len(ids), remaining_needed)
ids = ids[:selected_count]
# Append only the needed number of valid positions
rsamp[accepted_count:accepted_count+selected_count] = rtrue[ids]
# Update the accepted count
accepted_count += selected_count
print(f'\tMade {accepted_count} of {Nt}')
# Set up for next iteration
prob[ids] = 0
prob /= np.sum(prob)
plt.hist(rsamp, bins=30, density=True, alpha=0.5)
x = plt.gca().get_xlim()
x = [max(0, x[0]), x[1]]
x = np.linspace(x[0], x[1], 100)
y = x ** 2 * np.exp( - lam * x / R_max) * (lam / R_max) ** 3 / 2
plt.plot(x, y, color='k')
Out[56]:
In [57]:
comb_x = np.array([xtrue, ytrue, ztrue])
print(comb_x.shape)
In [62]:
dirname = '/data54/lavaux/VELMASS/halo_central/halos_new/'
def parse_file_to_dict(file_path):
config_dict = {}
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if not line or line.startswith("#"): # Skip empty lines and comments
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
# Convert the value to the appropriate type (int, float, or leave as string)
if value.isdigit():
value = int(value)
else:
try:
value = float(value)
except ValueError:
pass # Leave it as a string if it cannot be converted
config_dict[key] = value
return config_dict
config_dict = parse_file_to_dict(f'{dirname}/rockstar.cfg')
print(config_dict.keys())
print(config_dict['Om'])
print(type(config_dict['Om']))
In [ ]: