Triaging hypothetical crystals for stability
Classify 215,488 hypothetical crystals as stable or not against a fixed DFT convex hull, scored by F1.
| 1 | """Matbench Discovery: what "stable" means, on one real binary system. |
| 2 | |
| 3 | Data (hull.csv, cut from the benchmark's registered files on figshare, https://figshare.com/articles/dataset/22715158): |
| 4 | mp rows: Materials Project entries on the hull, from 2023-01-10-mp-energies.csv.gz (https://ndownloader.figshare.com/files/49083124) |
| 5 | wbm rows: WBM test crystals in the unique-prototype set, from wbm/2023-12-13-wbm-summary.csv.gz |
| 6 | (https://ndownloader.figshare.com/files/64706751): e_form_per_atom_mp2020_corrected, e_above_hull_mp2020_corrected_ppd_mp |
| 7 | Only the one system drawn is kept. The figure rebuilds the lower convex hull from the mp rows and checks that every |
| 8 | candidate's height above it matches the benchmark's own e_above_hull, so the stable ones are the benchmark's stable ones. |
| 9 | """ |
| 10 | import csv |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import kit |
| 14 | |
| 15 | HERE = Path(__file__).resolve().parent |
| 16 | A, B = 'Mg', 'Pt' # x is the fraction of B |
| 17 | |
| 18 | |
| 19 | def lower_hull(pts): |
| 20 | pts, h = sorted(set(pts)), [] |
| 21 | for p in pts: |
| 22 | 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: |
| 23 | h.pop() |
| 24 | h.append(p) |
| 25 | return h |
| 26 | |
| 27 | |
| 28 | def under(h, x): |
| 29 | for (a, b), (c, d) in zip(h, h[1:]): |
| 30 | if a <= x <= c: |
| 31 | return b + (d - b) * (x - a) / (c - a) |
| 32 | |
| 33 | |
| 34 | rows = list(csv.DictReader(open(HERE / 'hull.csv'))) |
| 35 | mp = [(float(r[f'x_{B}']), float(r['e_form_per_atom'])) for r in rows if r['set'] == 'mp'] |
| 36 | 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'] |
| 37 | hull = lower_hull(mp) |
| 38 | assert hull[0] == (0.0, 0.0) and hull[-1] == (1.0, 0.0), 'the elements anchor the hull' |
| 39 | for x, e, eh in wbm: |
| 40 | assert abs(e - under(hull, x) - eh) < 1e-4, (x, e, eh) |
| 41 | stable = [(x, e) for x, e, eh in wbm if eh <= 0] |
| 42 | above = [(x, e) for x, e, eh in wbm if eh > 0] |
| 43 | |
| 44 | lo = min(e for _, e in mp + [(x, e) for x, e, _ in wbm]) |
| 45 | hi = max(0.0, max(e for _, e, _ in wbm)) |
| 46 | X = lambda x: 44 + x * 552 |
| 47 | Y = lambda e: 22 + (hi - e) / (hi - lo) * 222 |
| 48 | |
| 49 | body = kit.polyline([(X(x), Y(e)) for x, e in hull], 'ink-s', 4) |
| 50 | body += ''.join(kit.circle(X(x), Y(e), 8, 'faint-s', ' stroke-width="3"') for x, e in above) |
| 51 | body += ''.join(kit.dot(X(x), Y(e), 7, 'ink') for x, e in hull) |
| 52 | body += ''.join(kit.dot(X(x), Y(e), 11, 'f') for x, e in stable) |
| 53 | label = f'{A}–{B} · {len(wbm)} candidates · {len(stable)} on or below the hull' |
| 54 | kit.write(__file__, 'chemistry-materials', body, label, |
| 55 | f'Formation energy against composition for {A}–{B}: the Materials Project convex hull, ' |
| 56 | f'with {len(stable)} of {len(wbm)} WBM candidate crystals on or below it and the rest above') |
| 57 | print(label) |