1
0
Fork 0
mirror of https://github.com/Findus23/rebound-collisions.git synced 2024-09-19 15:53:48 +02:00
rebound-collisions/particle_numbers.py

77 lines
2 KiB
Python
Raw Normal View History

2021-12-28 16:05:20 +01:00
import pickle
import random
from os.path import expanduser
from pathlib import Path
2021-04-05 17:39:57 +02:00
from sys import argv
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from rebound import SimulationArchive, Simulation
from extradata import ExtraData
2022-01-30 19:53:25 +01:00
from utils import filename_from_argv, plot_settings, is_ci, scenario_colors, mode_from_fn
2021-12-28 16:05:20 +01:00
cache_file = Path("particle_numbers_cache.pickle.xz")
if cache_file.exists():
with cache_file.open("rb") as f:
cache = pickle.load(f)
else:
cache = {}
plot_settings()
2021-04-05 17:39:57 +02:00
fig: Figure = plt.figure()
ax: Axes = plt.gca()
2021-12-28 16:05:20 +01:00
files = argv[1:]
2022-01-30 19:53:25 +01:00
random.seed(1)
random.shuffle(files)
2021-12-28 16:05:20 +01:00
for file in files:
2021-04-05 17:39:57 +02:00
fn = filename_from_argv(file)
print(fn)
2021-12-28 16:05:20 +01:00
if "bak" in str(fn):
2021-04-05 17:39:57 +02:00
continue
2021-12-28 16:05:20 +01:00
if fn.name in cache:
Ns, ts = cache[fn.name]
else:
try:
ed = ExtraData.load(fn)
sa = SimulationArchive(str(fn.with_suffix(".bin")))
except:
print("skipping")
continue
ts = []
Ns = []
sim: Simulation
for sim in sa:
num_planetesimals = sum(ed.pd(p).type == "planetesimal" for p in sim.particles)
num_embryos = sum(ed.pd(p).type == "embryo" for p in sim.particles)
N = num_embryos + num_planetesimals
Ns.append(N)
ts.append(sim.t)
cache[fn.name] = Ns, ts
2022-01-30 19:53:25 +01:00
mode = mode_from_fn(fn)
ax.step(ts, Ns, label=mode, where="post", color=scenario_colors[mode], linewidth=0.7,alpha=.5)
2021-12-28 16:05:20 +01:00
with cache_file.open("wb") as f:
pickle.dump(cache, f)
2021-04-05 17:39:57 +02:00
ax.set_xlabel("time [yr]")
ax.set_ylabel("number of objects")
2022-01-30 19:53:25 +01:00
ax.set_xscale("log")
2021-12-28 16:05:20 +01:00
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
2022-01-30 19:53:25 +01:00
new_labels = {
"RBF": by_label["rbf"],
"NN": by_label["nn"],
"LZ": by_label["lz"],
"PM": by_label["pm"],
}
ax.legend(new_labels.values(), new_labels.keys())
plt.tight_layout()
2021-05-03 16:31:30 +02:00
if not is_ci():
2021-12-28 16:05:20 +01:00
plt.savefig(expanduser(f"~/tmp/particle_numbers.pdf"))
2021-04-05 17:39:57 +02:00
plt.show()