-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
72 lines (62 loc) · 2.27 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from os import environ
import numpy as np
from graphviz import Digraph
from matplotlib.colors import to_hex
from extradata import ExtraData, CollisionMeta
from utils import filename_from_argv, get_water_cmap
fn = filename_from_argv()
ed = ExtraData.load(fn)
dot = Digraph(comment='Collisions')
# dot.engine = "neato"
if dot.engine == "neato":
dot.attr("graph", overlap="false")
interacting_objects = set()
for merged, originals in ed.tree.get_tree().items():
first_parent = True
for parent in originals["parents"]:
meta: CollisionMeta = originals["meta"]
water_ret = meta.water_retention
mantle_ret = meta.mantle_retention
core_ret = meta.core_retention
if first_parent:
label = f"{water_ret:.2f}/{mantle_ret:.2f}/{core_ret:.2f}"
first_parent = False
dot.node(label=label, name=f"{merged}-collision", shape="diamond")
dot.edge(f"{merged}-collision", str(merged))
else:
label = None
dot.edge(str(parent), f"{merged}-collision")
interacting_objects.add(parent)
interacting_objects.add(int(merged))
cmap = get_water_cmap()
for name in ed.pdata.keys():
object = ed.pdata[name]
if object.type == "sun":
displayname = f"{name} (Sun)"
elif object.type == "gas giant":
displayname = f"{name} (gas giant)"
else:
displayname = str(name)
try:
mass = object.total_mass
except KeyError:
mass = 0
with np.errstate(divide='ignore'): # allow 0 water (becomes -inf)
color_val = (np.log10(object.water_mass_fraction) + 5) / 5
color = cmap(color_val)
if color_val > 0.55:
textcolor = "white"
else:
textcolor = "black"
print(to_hex(color), color_val)
dot.node(name=str(name), label=f"m={mass:.1e}\nwmf={object.water_mass_fraction:.1e}",
shape='box' if object.type == "planetesimal" else "ellipse", style="filled", fillcolor=to_hex(color),
fontcolor=textcolor)
# if object.escaped:
# dot.edge(str(name), str("escaped"))
# if object.collided_with_sun:
# dot.edge(str(name), str("collided with sun"))
if environ.get("CI"):
dot.save(fn.with_suffix(".gv"))
else:
dot.render(fn.with_suffix(".gv"), view=True, format="svg")