pymatgen
What this library is for
pymatgen is the core Python library behind many Materials Project workflows. It
provides robust representations of Composition, Molecule, Lattice, and
Structure, plus materials-specific analysis tools for crystallography,
thermodynamics, phase stability, electronic structure, VASP I/O, and Materials
Project API access.
When to use this vs. alternatives
- Use pymatgen for materials analysis: compositions, structures, symmetry,
phase diagrams, Materials Project queries, VASP parsing, Pourbaix diagrams,
reaction balancing, and computed-entry workflows.
- Use ASE when the immediate task is calculator orchestration, geometry
optimization, trajectories, MD, NEB, or a workflow built around an ASE
Atoms object.
- Use RDKit for cheminformatics, molecular graphs, SMILES/reactions, and
conformers; use pymatgen's
Molecule only when the downstream workflow is
materials/solid-state analysis or file conversion.
- Use
pymatgen.ext.matproj.MPRester for Materials Project access that should
return pymatgen objects and fit pymatgen analysis workflows. Use the
Materials Project API directly or mp-api when the user explicitly needs
lower-level API-client behavior outside pymatgen's MPRester coverage.
- Do not hand-roll CIF/POSCAR parsing, formula parsing, or phase-diagram convex
hulls. pymatgen already encodes the domain conventions and compatibility
corrections that generic SciPy/Pandas code will miss.
Canonical workflow
Start from a Structure or Composition, use pymatgen's domain objects for
analysis, and only convert to ASE when a calculator workflow is needed.
from pymatgen.analysis.phase_diagram import PhaseDiagram
from pymatgen.core import Composition, Structure
from pymatgen.ext.matproj import MPRester
from pymatgen.io.ase import AseAtomsAdaptor
structure = Structure.from_file("POSCAR") # also reads CIF and many other formats
print(structure.composition.reduced_formula)
print(structure.lattice.abc, structure.lattice.angles)
composition = Composition("LiFePO4")
print(composition.reduced_formula, composition.get_atomic_fraction("Li"))
with MPRester() as mpr: # uses PMG_MAPI_KEY if configured
entries = mpr.get_entries_in_chemsys(["Li", "Fe", "P", "O"])
mp_structure = mpr.get_structure_by_material_id("mp-19017")
phase_diagram = PhaseDiagram(entries)
entry = min(entries, key=lambda e: abs(e.composition.get_atomic_fraction("Fe") - 0.25))
print("e_above_hull_eV_per_atom", phase_diagram.get_e_above_hull(entry))
atoms = AseAtomsAdaptor.get_atoms(mp_structure)
assert len(atoms) == len(mp_structure)
For deeper examples, read:
Key conventions and gotchas
Structure coordinates are usually fractional relative to a Lattice;
Molecule coordinates are Cartesian and non-periodic. Check whether code is
using frac_coords, cart_coords, or coords.
Composition("Fe2O3") is not the same as an oxidation-state-resolved
composition. Use species with oxidation states, add_oxidation_state_by_*,
or oxidation-state guessers when valence matters.
Structure.from_file() is the normal entry point for CIF/POSCAR-like files,
but CIFs can contain disorder, partial occupancies, symmetry expansion, or
duplicate/near-duplicate sites. Inspect site count, formula, occupancies, and
warnings before using imported structures.
- Recent pymatgen
MPRester mirrors Materials Project REST API field names more
directly and is not the legacy-only client. If an MP query fails after copying
older examples, check the current API docs and field names instead of assuming
mp-api aliases apply.
- Materials Project entries used in phase diagrams should be compatible entries
from the same API/database context. Do not mix arbitrary DFT energies with MP
entries without applying the relevant compatibility processing and documenting
the correction scheme.
- Converting between pymatgen and ASE can lose or reinterpret metadata such as
site properties, selective dynamics, oxidation states, magnetic moments,
charges, and molecule bonding. Validate after conversion.
Anti-patterns
- Do not parse chemical formulas with regexes for materials logic. Use
Composition so reduced formula, element amounts, atomic fractions, and
anonymized formulas are handled consistently.
- Do not manually implement symmetry expansion or neighbor finding unless the
user is developing a new method. Use pymatgen's symmetry and local-environment
tools, then inspect edge cases such as self-neighbor periodic images.
- Do not use
Structure.from_spacegroup() as a blind replacement for a vetted
crystallographic file. Wyckoff positions, tolerances, and origin choices can
change atom counts; compare formula, site count, and symmetry against a
trusted reference.
- Do not report
e_above_hull from a hand-built hull without specifying the
entry set, database version, compatibility scheme, and units.
- Do not treat a Materials Project
Structure as the exact experimental
structure unless the provenance supports that claim.
Diagnostic checks
Before trusting outputs, the agent should:
- Print formula, reduced formula, site count, lattice parameters, volume, and
whether sites have partial occupancies or oxidation states.
- For imported CIF/POSCAR files, compare the expected and parsed composition,
check warnings, and inspect suspicious short distances or duplicate sites.
- For MP queries, record the API package/client, database version if available,
material IDs, fields requested, and whether deprecated or task-level data were
used.
- For phase diagrams, record the chemical system, number of entries,
compatibility processing, and units (
eV/atom for hull energies).
- After ASE conversion, compare atom count, species order, cell, PBC, and any
needed site properties before attaching a calculator.
Pointers to deeper material
1---2name: pymatgen3description: Use when the user is working with materials structures, compositions, crystallography, Materials Project data, phase diagrams, Pourbaix diagrams, VASP input/output, computed entries, symmetry analysis, oxidation states, diffusion analysis, electronic structures, or conversions between materials data formats. Prefer pymatgen over generic NumPy/Pandas or ASE when the task is materials analysis rather than running a calculator.4---56# pymatgen78## What this library is for910pymatgen is the core Python library behind many Materials Project workflows. It11provides robust representations of `Composition`, `Molecule`, `Lattice`, and12`Structure`, plus materials-specific analysis tools for crystallography,13thermodynamics, phase stability, electronic structure, VASP I/O, and Materials14Project API access.1516## When to use this vs. alternatives1718- Use pymatgen for materials analysis: compositions, structures, symmetry,19 phase diagrams, Materials Project queries, VASP parsing, Pourbaix diagrams,20 reaction balancing, and computed-entry workflows.21- Use ASE when the immediate task is calculator orchestration, geometry22 optimization, trajectories, MD, NEB, or a workflow built around an ASE23 `Atoms` object.24- Use RDKit for cheminformatics, molecular graphs, SMILES/reactions, and25 conformers; use pymatgen's `Molecule` only when the downstream workflow is26 materials/solid-state analysis or file conversion.27- Use `pymatgen.ext.matproj.MPRester` for Materials Project access that should28 return pymatgen objects and fit pymatgen analysis workflows. Use the29 Materials Project API directly or `mp-api` when the user explicitly needs30 lower-level API-client behavior outside pymatgen's `MPRester` coverage.31- Do not hand-roll CIF/POSCAR parsing, formula parsing, or phase-diagram convex32 hulls. pymatgen already encodes the domain conventions and compatibility33 corrections that generic SciPy/Pandas code will miss.3435## Canonical workflow3637Start from a `Structure` or `Composition`, use pymatgen's domain objects for38analysis, and only convert to ASE when a calculator workflow is needed.3940```python41from pymatgen.analysis.phase_diagram import PhaseDiagram42from pymatgen.core import Composition, Structure43from pymatgen.ext.matproj import MPRester44from pymatgen.io.ase import AseAtomsAdaptor4546structure = Structure.from_file("POSCAR") # also reads CIF and many other formats47print(structure.composition.reduced_formula)48print(structure.lattice.abc, structure.lattice.angles)4950composition = Composition("LiFePO4")51print(composition.reduced_formula, composition.get_atomic_fraction("Li"))5253with MPRester() as mpr: # uses PMG_MAPI_KEY if configured54 entries = mpr.get_entries_in_chemsys(["Li", "Fe", "P", "O"])55 mp_structure = mpr.get_structure_by_material_id("mp-19017")5657phase_diagram = PhaseDiagram(entries)58entry = min(entries, key=lambda e: abs(e.composition.get_atomic_fraction("Fe") - 0.25))59print("e_above_hull_eV_per_atom", phase_diagram.get_e_above_hull(entry))6061atoms = AseAtomsAdaptor.get_atoms(mp_structure)62assert len(atoms) == len(mp_structure)63```6465For deeper examples, read:6667- Usage guide: https://pymatgen.org/usage.html68- API docs: https://pymatgen.org/pymatgen.html69- Materials Project API docs: https://api.materialsproject.org/docs70- matgenb notebooks: https://github.com/materialsvirtuallab/matgenb7172## Key conventions and gotchas7374- `Structure` coordinates are usually fractional relative to a `Lattice`;75 `Molecule` coordinates are Cartesian and non-periodic. Check whether code is76 using `frac_coords`, `cart_coords`, or `coords`.77- `Composition("Fe2O3")` is not the same as an oxidation-state-resolved78 composition. Use species with oxidation states, `add_oxidation_state_by_*`,79 or oxidation-state guessers when valence matters.80- `Structure.from_file()` is the normal entry point for CIF/POSCAR-like files,81 but CIFs can contain disorder, partial occupancies, symmetry expansion, or82 duplicate/near-duplicate sites. Inspect site count, formula, occupancies, and83 warnings before using imported structures.84- Recent pymatgen `MPRester` mirrors Materials Project REST API field names more85 directly and is not the legacy-only client. If an MP query fails after copying86 older examples, check the current API docs and field names instead of assuming87 `mp-api` aliases apply.88- Materials Project entries used in phase diagrams should be compatible entries89 from the same API/database context. Do not mix arbitrary DFT energies with MP90 entries without applying the relevant compatibility processing and documenting91 the correction scheme.92- Converting between pymatgen and ASE can lose or reinterpret metadata such as93 site properties, selective dynamics, oxidation states, magnetic moments,94 charges, and molecule bonding. Validate after conversion.9596## Anti-patterns9798- Do not parse chemical formulas with regexes for materials logic. Use99 `Composition` so reduced formula, element amounts, atomic fractions, and100 anonymized formulas are handled consistently.101- Do not manually implement symmetry expansion or neighbor finding unless the102 user is developing a new method. Use pymatgen's symmetry and local-environment103 tools, then inspect edge cases such as self-neighbor periodic images.104- Do not use `Structure.from_spacegroup()` as a blind replacement for a vetted105 crystallographic file. Wyckoff positions, tolerances, and origin choices can106 change atom counts; compare formula, site count, and symmetry against a107 trusted reference.108- Do not report `e_above_hull` from a hand-built hull without specifying the109 entry set, database version, compatibility scheme, and units.110- Do not treat a Materials Project `Structure` as the exact experimental111 structure unless the provenance supports that claim.112113## Diagnostic checks114115Before trusting outputs, the agent should:116117- Print formula, reduced formula, site count, lattice parameters, volume, and118 whether sites have partial occupancies or oxidation states.119- For imported CIF/POSCAR files, compare the expected and parsed composition,120 check warnings, and inspect suspicious short distances or duplicate sites.121- For MP queries, record the API package/client, database version if available,122 material IDs, fields requested, and whether deprecated or task-level data were123 used.124- For phase diagrams, record the chemical system, number of entries,125 compatibility processing, and units (`eV/atom` for hull energies).126- After ASE conversion, compare atom count, species order, cell, PBC, and any127 needed site properties before attaching a calculator.128129## Pointers to deeper material130131- Documentation: https://pymatgen.org/132- Usage examples: https://pymatgen.org/usage.html133- GitHub repository: https://github.com/materialsproject/pymatgen134- Materials Project API docs: https://api.materialsproject.org/docs135- matgenb tutorial notebooks: https://github.com/materialsvirtuallab/matgenb136- Paper: Ong et al. (2013), "Python Materials Genomics (pymatgen): A robust,137 open-source python library for materials analysis", Computational Materials138 Science 68, 314-319. https://doi.org/10.1016/j.commatsci.2012.10.028