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

34 lines
1.1 KiB
Python
Raw Normal View History

2022-06-14 10:53:19 +02:00
import numpy as np
import pandas as pd
2022-06-14 16:38:50 +02:00
from utils import print_progress
2022-06-14 10:53:19 +02:00
def find_center(df: pd.DataFrame, center: np.ndarray, initial_radius=1):
# plt.figure()
all_particles = df[["X", "Y", "Z"]].to_numpy()
radius = initial_radius
center_history = []
2022-06-14 16:38:50 +02:00
i = 0
2022-06-14 10:53:19 +02:00
while True:
center_history.append(center)
distances = np.linalg.norm(all_particles - center, axis=1)
in_radius_particles = all_particles[distances < radius]
num_particles = in_radius_particles.shape[0]
2022-06-14 16:38:50 +02:00
print_progress(i, "?", f"n={num_particles}, r={radius}, c={center}")
2022-06-14 10:53:19 +02:00
if num_particles < 10:
break
center_of_mass = in_radius_particles.mean(axis=0)
new_center = (center_of_mass + center) / 2
shift = np.linalg.norm(center - new_center)
radius = max(2 * shift, radius * 0.9)
center = new_center
2022-06-14 16:38:50 +02:00
i += 1
2022-06-14 10:53:19 +02:00
center_history = np.array(center_history)
# print(center_history)
# plt.scatter(center_history[::, 0], center_history[::, 1], c=range(len(center_history[::, 1])))
# plt.colorbar(label="step")
# plt.show()
2022-06-21 16:26:42 +02:00
print()
2022-06-14 10:53:19 +02:00
return center