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

89 lines
2.4 KiB
Python
Raw Normal View History

2021-12-28 16:05:20 +01:00
import pickle
import random
import re
from os.path import expanduser
from pathlib import Path
from random import shuffle
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
2021-12-28 16:05:20 +01:00
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, inset_axes
2021-04-05 17:39:57 +02:00
from rebound import SimulationArchive, Simulation
from extradata import ExtraData
2021-05-03 16:31:30 +02:00
from utils import filename_from_argv, plot_settings, is_ci
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
# axins2 = zoomed_inset_axes(ax, zoom=1, loc="center")
axins2 = inset_axes(ax, 2,2, loc="center right") # no zoom
random.seed(1)
2021-04-05 17:39:57 +02:00
2021-12-28 16:05:20 +01:00
colors = {
"rbf": "C0",
"nn": "C1",
"lz": "C2",
"pm": "C3",
}
files = argv[1:]
shuffle(files)
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
mode = re.search(r"final_(\w+)_", str(fn)).group(1)
for a in [ax, axins2]:
a.step(ts, Ns, label=mode, where="post", color=colors[mode], linewidth=0.7)
# break
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")
# ax.set_xscale("log")
2021-12-28 16:05:20 +01:00
axins2.set_xlim(1e8, 2e8)
axins2.set_ylim(0, 15)
axins2.tick_params(labelleft=False, labelbottom=False)
mark_inset(ax, axins2, loc1=2, loc2=4, fc="none", ec="0.5")
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
ax.legend(by_label.values(), by_label.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()