Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@computron
Last active February 27, 2022 09:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save computron/c05be478c63af4d84b25 to your computer and use it in GitHub Desktop.
Save computron/c05be478c63af4d84b25 to your computer and use it in GitHub Desktop.
Create, plot, and analyze OPEN (grand canonical) Phase Diagrams using the pymatgen codebase and Materials Project database
"""
This is a basic example of how to create, plot, and analyze OPEN Phase Diagrams using the pymatgen
codebase and Materials Project database. To run this example, you should:
* have pymatgen (www.pymatgen.org) installed along with matplotlib
* obtain a Materials Project API key (https://www.materialsproject.org/open)
* paste that API key in the MAPI_KEY variable below, e.g. MAPI_KEY = "foobar1234"
For citation, see https://www.materialsproject.org/citing
For the accompanying comic book, see http://www.hackingmaterials.com/pdcomic
"""
from __future__ import print_function
from pymatgen import MPRester, Element
from pymatgen.analysis.phase_diagram import GrandPotentialPhaseDiagram, \
PhaseDiagram, PDPlotter
def plot_pd(pd, show_unstable=False):
plotter = PDPlotter(pd, show_unstable=show_unstable)
plotter.show()
# plotter.write_image("{}.png".format('-'.join(system)), "png") # save figure
def analyze_pd(pd):
print('Stable Entries (formula, materials_id)\n--------')
for e in pd.stable_entries:
print(e.composition.reduced_formula, e.entry_id)
print('\nUnstable Entries (formula, materials_id, e_above_hull (eV/atom), decomposes_to)\n--------')
for e in pd.unstable_entries:
decomp, e_above_hull = pd.get_decomp_and_e_above_hull(e)
pretty_decomp = [("{}:{}".format(k.composition.reduced_formula, k.entry_id), round(v, 2)) for k, v in decomp.items()]
print(e.composition.reduced_formula, e.entry_id, "%.3f" % e_above_hull, pretty_decomp)
if __name__ == "__main__":
MAPI_KEY = None # You must change this to your Materials API key! (or set MAPI_KEY env variable)
system = ["Fe", "P", "O"] # system we want to get open PD for
# system = ["Li", "Fe", "P", "O"] # alternate system example
open_elements_specific = None # e.g., {Element("O"): 0} where 0 is the specific chemical potential
open_element_all = Element("O") # plot a series of open phase diagrams at critical chem pots with this element open
mpr = MPRester(MAPI_KEY) # object for connecting to MP Rest interface
# get data
entries = mpr.get_entries_in_chemsys(system, compatible_only=True)
if open_elements_specific:
gcpd = GrandPotentialPhaseDiagram(entries, open_elements_specific)
plot_pd(gcpd, False)
analyze_pd(gcpd)
if open_element_all:
pd = PhaseDiagram(entries)
chempots = pd.get_transition_chempots(open_element_all)
all_gcpds = list()
for idx in range(len(chempots)):
if idx == len(chempots) - 1:
avgchempot = chempots[idx] - 0.1
else:
avgchempot = 0.5 * (chempots[idx] + chempots[idx + 1])
gcpd = GrandPotentialPhaseDiagram(entries, {open_element_all: avgchempot}, pd.elements)
min_chempot = None if idx == len(chempots) - 1 else chempots[idx + 1]
max_chempot = chempots[idx]
print("Chempot range for diagram {} is: {} to {}".format(idx, min_chempot, max_chempot))
plot_pd(gcpd, False)
analyze_pd(gcpd)
@computron
Copy link
Author

Updated 3/2/18 to support Python 3 and account for latest pymatgen (2018.03.02)

@shyuep
Copy link

shyuep commented Mar 2, 2018

Thanks. Just a suggest that it might be helpful to move this to the matgenb repo as as notebook.

@Asif-Iqbal-Bhatti
Copy link

On my system I am not able to save the figure using:

plotter.write_image("{}.png".format('-'.join(system)), "png") # save figure

It says:
phase.py:36: FutureWarning:

init is deprecated
MaterialsProjectCompatibility will be updated with new correction classes as well as new values of corrections and uncertainties in 2020

/home/asif_swan/.local/lib/python3.6/site-packages/pymatgen/ext/matproj.py:559: FutureWarning:

init is deprecated
MaterialsProjectCompatibility will be updated with new correction classes as well as new values of corrections and uncertainties in 2020

Traceback (most recent call last):
File "phase.py", line 46, in
plotter.write_image("{}.jpg".format('-'.join(system)), "jpg") # save figure
File "/home/user/.local/lib/python3.6/site-packages/pymatgen/analysis/phase_diagram.py", line 2118, in write_image
f = plt.gcf()
AttributeError: 'Figure' object has no attribute 'gcf'

Why is that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment