Comprehensive guidance for using pymatgen (Python Materials Genomics) for computational materials science...
Systematic guidance for using pymatgen to analyze crystal structures, access materials databases, and interface with computational chemistry codes.
What do you want to do?
Structure Creation/Loading:
references/core-objects.mdreferences/file-io.mdreferences/materials-project.mdreferences/structure-analysis.mdStructure Analysis:
references/structure-analysis.mdreferences/structure-analysis.mdreferences/properties.mdreferences/visualization.mdStructure Manipulation:
references/transformations.mdreferences/transformations.mdreferences/transformations.mdreferences/transformations.mdMaterials Database:
references/materials-project.mdreferences/materials-project.mdreferences/phase-diagrams.mdElectronic Structure:
references/electronic-structure.mdreferences/electronic-structure.mdreferences/vasp-integration.mdInput Generation:
references/vasp-integration.mdreferences/file-io.mdreferences/vasp-integration.mdBasic pattern:
# 1. Import modules
from pymatgen.core import Structure, Lattice, Element
# 2. Load or create structure
structure = Structure.from_file("POSCAR")
# 3. Analyze or manipulate
print(f"Formula: {structure.composition.reduced_formula}")
print(f"Space group: {structure.get_space_group_info()}")
# 4. Transform if needed
supercell = structure * (2, 2, 1) # 2x2x1 supercell
# 5. Write output
supercell.to(filename="POSCAR_supercell")
Load structure: Structure.from_file("file.cif") - Auto-detects format
Create structure: See references/core-objects.md for Element, Lattice, Structure creation
Analyze symmetry: SpacegroupAnalyzer(struct).get_space_group_symbol()
Make supercell: structure * (2, 2, 1) or use transformations
Query MP: MPRester(key).get_structure_by_material_id("mp-149")
Generate VASP: MPRelaxSet(struct).write_input("dir")
Plot bands/DOS: See references/electronic-structure.md
Detailed examples for all tasks in reference files.
Route to: references/core-objects.md
When:
Key classes:
Route to: references/file-io.md
When:
Supported formats:
Route to: references/structure-analysis.md
When:
Key tools:
Route to: references/transformations.md
When:
Key modules:
Route to: references/materials-project.md
When:
Key class:
Route to: references/phase-diagrams.md
When:
Key classes:
Route to: references/electronic-structure.md
When:
Key modules:
Route to: references/vasp-integration.md
When:
Key classes:
Pattern 1: Structure Analysis
examples/structure_analysis.py for complete workflowPattern 2: Materials Project to Calculation
examples/mp_to_vasp.pyPattern 3: High-Throughput Substitution
examples/substitution_study.pyPattern 4: Electronic Structure Analysis
examples/band_structure.pyAll patterns detailed in examples/ directory with complete code.
pip install pymatgen
# Or with optional dependencies
pip install pymatgen[all]
# Register at materialsproject.org to get API key
pmg config --add PMG_MAPI_KEY your_api_key_here
pmg config --add PMG_VASP_PSP_DIR /path/to/vasp/potentials
Located at ~/.pmgrc.yaml:
PMG_MAPI_KEY: your_api_key_here
PMG_VASP_PSP_DIR: /path/to/vasp/potentials
PMG_DEFAULT_FUNCTIONAL: PBE
Problem: NameError: name 'Structure' is not defined
Solution:
from pymatgen.core import Structure
Problem: MPRester returns authentication error
Solution:
pmg config --add PMG_MAPI_KEY your_keyMPRester("your_key")Problem: Cannot write POTCAR files
Solution:
pmg config --add PMG_VASP_PSP_DIR /pathProblem: ImportError for plotting or specialized modules
Solution:
pip install pymatgen[all] # Install all optional dependencies
# Or specific ones:
pip install matplotlib # For plotting
pip install scipy # For analysis tools
as_dict() / from_dict() for persistencewith MPRester(...) as mpr:See examples/ for complete workflows:
structure_analysis.py - Comprehensive structure analysismp_query.py - Materials Project queriesphase_diagram.py - Phase diagram constructionband_structure.py - Electronic structure analysisvasp_workflow.py - VASP calculation setupsubstitution_study.py - High-throughput substitutionsreferences/core-objects.md - Element, Structure, Lattice, Compositionreferences/file-io.md - Reading/writing all file formatsreferences/structure-analysis.md - Symmetry, comparison, neighborsreferences/transformations.md - Supercells, substitutions, perturbationsreferences/materials-project.md - API usage and queriesreferences/phase-diagrams.md - Phase diagram constructionreferences/electronic-structure.md - Band structures and DOSreferences/vasp-integration.md - VASP input/output handlingreferences/properties.md - Calculated propertiesreferences/visualization.md - Structure visualization