|
| 1 | +# %% |
| 2 | +import gzip |
| 3 | +import json |
| 4 | +import os |
| 5 | +import pickle |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +import pymatviz |
| 10 | +from pymatgen.analysis.phase_diagram import PatchedPhaseDiagram |
| 11 | +from pymatgen.entries.compatibility import MaterialsProject2020Compatibility |
| 12 | +from pymatgen.entries.computed_entries import ComputedEntry |
| 13 | +from pymatgen.ext.matproj import MPRester |
| 14 | + |
| 15 | +from mb_discovery import ROOT |
| 16 | +from mb_discovery.compute_formation_energy import ( |
| 17 | + get_elemental_ref_entries, |
| 18 | + get_form_energy_per_atom, |
| 19 | +) |
| 20 | + |
| 21 | +today = f"{datetime.now():%Y-%m-%d}" |
| 22 | +module_dir = os.path.dirname(__file__) |
| 23 | + |
| 24 | + |
| 25 | +# %% |
| 26 | +all_mp_computed_structure_entries = MPRester().get_entries("") # run on 2022-09-16 |
| 27 | + |
| 28 | +# save all ComputedStructureEntries to disk |
| 29 | +pd.Series( |
| 30 | + {e.entry_id: e for e in all_mp_computed_structure_entries} |
| 31 | +).drop_duplicates().to_json( # mp-15590 appears twice so we drop_duplicates() |
| 32 | + f"{ROOT}/data/{today}-all-mp-entries.json.gz", default_handler=lambda x: x.as_dict() |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +# %% |
| 37 | +all_mp_computed_entries = ( |
| 38 | + pd.read_json(f"{ROOT}/data/2022-09-16-all-mp-entries.json.gz") |
| 39 | + .set_index("material_id") |
| 40 | + .entry.map(ComputedEntry.from_dict) # drop the structure, just load ComputedEntry |
| 41 | + .to_dict() |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +print(f"{len(all_mp_computed_entries) = :,}") |
| 46 | +# len(all_mp_computed_entries) = 146,323 |
| 47 | + |
| 48 | + |
| 49 | +# %% build phase diagram with MP entries only |
| 50 | +ppd_mp = PatchedPhaseDiagram(all_mp_computed_entries) |
| 51 | +# prints: |
| 52 | +# PatchedPhaseDiagram |
| 53 | +# Covering 44805 Sub-Spaces |
| 54 | + |
| 55 | +# save MP PPD to disk |
| 56 | +with gzip.open(f"{module_dir}/{today}-ppd-mp.pkl.gz", "wb") as zip_file: |
| 57 | + pickle.dump(ppd_mp, zip_file) |
| 58 | + |
| 59 | + |
| 60 | +# %% build phase diagram with both MP entries + WBM entries |
| 61 | +df_wbm = pd.read_json( |
| 62 | + f"{ROOT}/data/2022-06-26-wbm-cses-and-initial-structures.json.gz" |
| 63 | +).set_index("material_id") |
| 64 | + |
| 65 | +wbm_computed_entries: list[ComputedEntry] = df_wbm.query("n_elements > 1").cse.map( |
| 66 | + ComputedEntry.from_dict |
| 67 | +) |
| 68 | + |
| 69 | +wbm_computed_entries = MaterialsProject2020Compatibility().process_entries( |
| 70 | + wbm_computed_entries, verbose=True, clean=True |
| 71 | +) |
| 72 | + |
| 73 | +n_skipped = len(df_wbm) - len(wbm_computed_entries) |
| 74 | +print(f"{n_skipped:,} ({n_skipped / len(df_wbm):.1%}) entries not processed") |
| 75 | + |
| 76 | + |
| 77 | +# %% merge MP and WBM entries into a single PatchedPhaseDiagram |
| 78 | +mp_wbm_ppd = PatchedPhaseDiagram( |
| 79 | + wbm_computed_entries + all_mp_computed_entries, verbose=True |
| 80 | +) |
| 81 | + |
| 82 | + |
| 83 | +# %% compute terminal reference entries across all MP (can be used to compute MP |
| 84 | +# compatible formation energies quickly) |
| 85 | +elemental_ref_entries = get_elemental_ref_entries(all_mp_computed_entries) |
| 86 | + |
| 87 | +# save elemental_ref_entries to disk as json |
| 88 | +with open(f"{module_dir}/{today}-elemental-ref-entries.json", "w") as file: |
| 89 | + json.dump(elemental_ref_entries, file, default=lambda x: x.as_dict()) |
| 90 | + |
| 91 | + |
| 92 | +# %% load MP elemental reference entries to compute formation energies |
| 93 | +mp_elem_refs_path = f"{ROOT}/data/2022-09-19-mp-elemental-reference-entries.json" |
| 94 | +mp_reference_entries = ( |
| 95 | + pd.read_json(mp_elem_refs_path, typ="series").map(ComputedEntry.from_dict).to_dict() |
| 96 | +) |
| 97 | + |
| 98 | + |
| 99 | +df_mp = pd.read_json(f"{ROOT}/data/2022-08-13-mp-all-energies.json.gz").set_index( |
| 100 | + "material_id" |
| 101 | +) |
| 102 | + |
| 103 | + |
| 104 | +# %% |
| 105 | +df_mp["our_mp_e_form"] = [ |
| 106 | + get_form_energy_per_atom(all_mp_computed_entries[mp_id], mp_reference_entries) |
| 107 | + for mp_id in df_mp.index |
| 108 | +] |
| 109 | + |
| 110 | + |
| 111 | +# make sure get_form_energy_per_atom() reproduces MP formation energies |
| 112 | +ax = pymatviz.density_scatter( |
| 113 | + df_mp["formation_energy_per_atom"], df_mp["our_mp_e_form"] |
| 114 | +) |
| 115 | +ax.set( |
| 116 | + title="MP Formation Energy Comparison", |
| 117 | + xlabel="MP Formation Energy (eV/atom)", |
| 118 | + ylabel="Our Formation Energy (eV/atom)", |
| 119 | +) |
| 120 | +ax.figure.savefig(f"{ROOT}/tmp/{today}-mp-formation-energy-comparison.png", dpi=300) |
0 commit comments