"""Matbench Discovery: what "stable" means, on one real binary system. Data (hull.csv, cut from the benchmark's registered files on figshare, https://figshare.com/articles/dataset/22715158): mp rows: Materials Project entries on the hull, from 2023-01-10-mp-energies.csv.gz (https://ndownloader.figshare.com/files/49083124) wbm rows: WBM test crystals in the unique-prototype set, from wbm/2023-12-13-wbm-summary.csv.gz (https://ndownloader.figshare.com/files/64706751): e_form_per_atom_mp2020_corrected, e_above_hull_mp2020_corrected_ppd_mp Only the one system drawn is kept. The figure rebuilds the lower convex hull from the mp rows and checks that every candidate's height above it matches the benchmark's own e_above_hull, so the stable ones are the benchmark's stable ones. """ import csv from pathlib import Path import kit HERE = Path(__file__).resolve().parent A, B = 'Mg', 'Pt' # x is the fraction of B def lower_hull(pts): pts, h = sorted(set(pts)), [] for p in pts: while len(h) >= 2 and (h[-1][0] - h[-2][0]) * (p[1] - h[-2][1]) - (h[-1][1] - h[-2][1]) * (p[0] - h[-2][0]) <= 0: h.pop() h.append(p) return h def under(h, x): for (a, b), (c, d) in zip(h, h[1:]): if a <= x <= c: return b + (d - b) * (x - a) / (c - a) rows = list(csv.DictReader(open(HERE / 'hull.csv'))) mp = [(float(r[f'x_{B}']), float(r['e_form_per_atom'])) for r in rows if r['set'] == 'mp'] wbm = [(float(r[f'x_{B}']), float(r['e_form_per_atom']), float(r['e_above_hull'])) for r in rows if r['set'] == 'wbm'] hull = lower_hull(mp) assert hull[0] == (0.0, 0.0) and hull[-1] == (1.0, 0.0), 'the elements anchor the hull' for x, e, eh in wbm: assert abs(e - under(hull, x) - eh) < 1e-4, (x, e, eh) stable = [(x, e) for x, e, eh in wbm if eh <= 0] above = [(x, e) for x, e, eh in wbm if eh > 0] lo = min(e for _, e in mp + [(x, e) for x, e, _ in wbm]) hi = max(0.0, max(e for _, e, _ in wbm)) X = lambda x: 44 + x * 552 Y = lambda e: 22 + (hi - e) / (hi - lo) * 222 body = kit.polyline([(X(x), Y(e)) for x, e in hull], 'ink-s', 4) body += ''.join(kit.circle(X(x), Y(e), 8, 'faint-s', ' stroke-width="3"') for x, e in above) body += ''.join(kit.dot(X(x), Y(e), 7, 'ink') for x, e in hull) body += ''.join(kit.dot(X(x), Y(e), 11, 'f') for x, e in stable) label = f'{A}–{B} · {len(wbm)} candidates · {len(stable)} on or below the hull' kit.write(__file__, 'chemistry-materials', body, label, f'Formation energy against composition for {A}–{B}: the Materials Project convex hull, ' f'with {len(stable)} of {len(wbm)} WBM candidate crystals on or below it and the rest above') print(label)