
Pymatgen
- 36 installs
- 16 repo stars
- Updated November 20, 2025
- jackspace/claudeskillz
Analyze crystal structures, phase diagrams, and electronic structure for computational materials science with pymatgen.
About
Pymatgen is a Python materials-science toolkit for crystal structures, phase diagrams, band structure, and DOS. Developers use it for computational materials analysis and format conversion with Materials Project integration.
- Reads and writes CIF, POSCAR, and other structure formats with automatic detection
- Integrates with the Materials Project API for entries and phase-diagram stability checks
Pymatgen by the numbers
- 36 all-time installs (skills.sh)
- Ranked #1,041 of 2,065 Data Science & ML skills by installs in the Skillselion catalog
- Data as of Aug 2, 2026 (Skillselion catalog sync)
npx skills add https://github.com/jackspace/claudeskillz --skill pymatgenAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 36 |
|---|---|
| repo stars | ★ 16 |
| Last updated | November 20, 2025 |
| Repository | jackspace/claudeskillz ↗ |
What it does
Analyze crystal structures, phase diagrams, and electronic structure for computational materials science with pymatgen.
Files
Pymatgen - Python Materials Genomics
Overview
Pymatgen is a comprehensive Python library for materials analysis that powers the Materials Project. Create, analyze, and manipulate crystal structures and molecules, compute phase diagrams and thermodynamic properties, analyze electronic structure (band structures, DOS), generate surfaces and interfaces, and access Materials Project's database of computed materials. Supports 100+ file formats from various computational codes.
When to Use This Skill
This skill should be used when:
- Working with crystal structures or molecular systems in materials science
- Converting between structure file formats (CIF, POSCAR, XYZ, etc.)
- Analyzing symmetry, space groups, or coordination environments
- Computing phase diagrams or assessing thermodynamic stability
- Analyzing electronic structure data (band gaps, DOS, band structures)
- Generating surfaces, slabs, or studying interfaces
- Accessing the Materials Project database programmatically
- Setting up high-throughput computational workflows
- Analyzing diffusion, magnetism, or mechanical properties
- Working with VASP, Gaussian, Quantum ESPRESSO, or other computational codes
Quick Start Guide
Installation
# Core pymatgen
pip install pymatgen
# With Materials Project API access
pip install pymatgen mp-api
# Optional dependencies for extended functionality
pip install pymatgen[analysis] # Additional analysis tools
pip install pymatgen[vis] # Visualization toolsBasic Structure Operations
from pymatgen.core import Structure, Lattice
# Read structure from file (automatic format detection)
struct = Structure.from_file("POSCAR")
# Create structure from scratch
lattice = Lattice.cubic(3.84)
struct = Structure(lattice, ["Si", "Si"], [[0,0,0], [0.25,0.25,0.25]])
# Write to different format
struct.to(filename="structure.cif")
# Basic properties
print(f"Formula: {struct.composition.reduced_formula}")
print(f"Space group: {struct.get_space_group_info()}")
print(f"Density: {struct.density:.2f} g/cm³")Materials Project Integration
# Set up API key
export MP_API_KEY="your_api_key_here"from mp_api.client import MPRester
with MPRester() as mpr:
# Get structure by material ID
struct = mpr.get_structure_by_material_id("mp-149")
# Search for materials
materials = mpr.materials.summary.search(
formula="Fe2O3",
energy_above_hull=(0, 0.05)
)Core Capabilities
1. Structure Creation and Manipulation
Create structures using various methods and perform transformations.
From files:
# Automatic format detection
struct = Structure.from_file("structure.cif")
struct = Structure.from_file("POSCAR")
mol = Molecule.from_file("molecule.xyz")From scratch:
from pymatgen.core import Structure, Lattice
# Using lattice parameters
lattice = Lattice.from_parameters(a=3.84, b=3.84, c=3.84,
alpha=120, beta=90, gamma=60)
coords = [[0, 0, 0], [0.75, 0.5, 0.75]]
struct = Structure(lattice, ["Si", "Si"], coords)
# From space group
struct = Structure.from_spacegroup(
"Fm-3m",
Lattice.cubic(3.5),
["Si"],
[[0, 0, 0]]
)Transformations:
from pymatgen.transformations.standard_transformations import (
SupercellTransformation,
SubstitutionTransformation,
PrimitiveCellTransformation
)
# Create supercell
trans = SupercellTransformation([[2,0,0],[0,2,0],[0,0,2]])
supercell = trans.apply_transformation(struct)
# Substitute elements
trans = SubstitutionTransformation({"Fe": "Mn"})
new_struct = trans.apply_transformation(struct)
# Get primitive cell
trans = PrimitiveCellTransformation()
primitive = trans.apply_transformation(struct)Reference: See references/core_classes.md for comprehensive documentation of Structure, Lattice, Molecule, and related classes.
2. File Format Conversion
Convert between 100+ file formats with automatic format detection.
Using convenience methods:
# Read any format
struct = Structure.from_file("input_file")
# Write to any format
struct.to(filename="output.cif")
struct.to(filename="POSCAR")
struct.to(filename="output.xyz")Using the conversion script:
# Single file conversion
python scripts/structure_converter.py POSCAR structure.cif
# Batch conversion
python scripts/structure_converter.py *.cif --output-dir ./poscar_files --format poscarReference: See references/io_formats.md for detailed documentation of all supported formats and code integrations.
3. Structure Analysis and Symmetry
Analyze structures for symmetry, coordination, and other properties.
Symmetry analysis:
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
sga = SpacegroupAnalyzer(struct)
# Get space group information
print(f"Space group: {sga.get_space_group_symbol()}")
print(f"Number: {sga.get_space_group_number()}")
print(f"Crystal system: {sga.get_crystal_system()}")
# Get conventional/primitive cells
conventional = sga.get_conventional_standard_structure()
primitive = sga.get_primitive_standard_structure()Coordination environment:
from pymatgen.analysis.local_env import CrystalNN
cnn = CrystalNN()
neighbors = cnn.get_nn_info(struct, n=0) # Neighbors of site 0
print(f"Coordination number: {len(neighbors)}")
for neighbor in neighbors:
site = struct[neighbor['site_index']]
print(f" {site.species_string} at {neighbor['weight']:.3f} Å")Using the analysis script:
# Comprehensive analysis
python scripts/structure_analyzer.py POSCAR --symmetry --neighbors
# Export results
python scripts/structure_analyzer.py structure.cif --symmetry --export jsonReference: See references/analysis_modules.md for detailed documentation of all analysis capabilities.
4. Phase Diagrams and Thermodynamics
Construct phase diagrams and analyze thermodynamic stability.
Phase diagram construction:
from mp_api.client import MPRester
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter
# Get entries from Materials Project
with MPRester() as mpr:
entries = mpr.get_entries_in_chemsys("Li-Fe-O")
# Build phase diagram
pd = PhaseDiagram(entries)
# Check stability
from pymatgen.core import Composition
comp = Composition("LiFeO2")
# Find entry for composition
for entry in entries:
if entry.composition.reduced_formula == comp.reduced_formula:
e_above_hull = pd.get_e_above_hull(entry)
print(f"Energy above hull: {e_above_hull:.4f} eV/atom")
if e_above_hull > 0.001:
# Get decomposition
decomp = pd.get_decomposition(comp)
print("Decomposes to:", decomp)
# Plot
plotter = PDPlotter(pd)
plotter.show()Using the phase diagram script:
# Generate phase diagram
python scripts/phase_diagram_generator.py Li-Fe-O --output li_fe_o.png
# Analyze specific composition
python scripts/phase_diagram_generator.py Li-Fe-O --analyze "LiFeO2" --showReference: See references/analysis_modules.md (Phase Diagrams section) and references/transformations_workflows.md (Workflow 2) for detailed examples.
5. Electronic Structure Analysis
Analyze band structures, density of states, and electronic properties.
Band structure:
from pymatgen.io.vasp import Vasprun
from pymatgen.electronic_structure.plotter import BSPlotter
# Read from VASP calculation
vasprun = Vasprun("vasprun.xml")
bs = vasprun.get_band_structure()
# Analyze
band_gap = bs.get_band_gap()
print(f"Band gap: {band_gap['energy']:.3f} eV")
print(f"Direct: {band_gap['direct']}")
print(f"Is metal: {bs.is_metal()}")
# Plot
plotter = BSPlotter(bs)
plotter.save_plot("band_structure.png")Density of states:
from pymatgen.electronic_structure.plotter import DosPlotter
dos = vasprun.complete_dos
# Get element-projected DOS
element_dos = dos.get_element_dos()
for element, element_dos_obj in element_dos.items():
print(f"{element}: {element_dos_obj.get_gap():.3f} eV")
# Plot
plotter = DosPlotter()
plotter.add_dos("Total DOS", dos)
plotter.show()Reference: See references/analysis_modules.md (Electronic Structure section) and references/io_formats.md (VASP section).
6. Surface and Interface Analysis
Generate slabs, analyze surfaces, and study interfaces.
Slab generation:
from pymatgen.core.surface import SlabGenerator
# Generate slabs for specific Miller index
slabgen = SlabGenerator(
struct,
miller_index=(1, 1, 1),
min_slab_size=10.0, # Å
min_vacuum_size=10.0, # Å
center_slab=True
)
slabs = slabgen.get_slabs()
# Write slabs
for i, slab in enumerate(slabs):
slab.to(filename=f"slab_{i}.cif")Wulff shape construction:
from pymatgen.analysis.wulff import WulffShape
# Define surface energies
surface_energies = {
(1, 0, 0): 1.0,
(1, 1, 0): 1.1,
(1, 1, 1): 0.9,
}
wulff = WulffShape(struct.lattice, surface_energies)
print(f"Surface area: {wulff.surface_area:.2f} Ų")
print(f"Volume: {wulff.volume:.2f} ų")
wulff.show()Adsorption site finding:
from pymatgen.analysis.adsorption import AdsorbateSiteFinder
from pymatgen.core import Molecule
asf = AdsorbateSiteFinder(slab)
# Find sites
ads_sites = asf.find_adsorption_sites()
print(f"On-top sites: {len(ads_sites['ontop'])}")
print(f"Bridge sites: {len(ads_sites['bridge'])}")
print(f"Hollow sites: {len(ads_sites['hollow'])}")
# Add adsorbate
adsorbate = Molecule("O", [[0, 0, 0]])
ads_struct = asf.add_adsorbate(adsorbate, ads_sites["ontop"][0])Reference: See references/analysis_modules.md (Surface and Interface section) and references/transformations_workflows.md (Workflows 3 and 9).
7. Materials Project Database Access
Programmatically access the Materials Project database.
Setup: 1. Get API key from https://next-gen.materialsproject.org/ 2. Set environment variable: export MP_API_KEY="your_key_here"
Search and retrieve:
from mp_api.client import MPRester
with MPRester() as mpr:
# Search by formula
materials = mpr.materials.summary.search(formula="Fe2O3")
# Search by chemical system
materials = mpr.materials.summary.search(chemsys="Li-Fe-O")
# Filter by properties
materials = mpr.materials.summary.search(
chemsys="Li-Fe-O",
energy_above_hull=(0, 0.05), # Stable/metastable
band_gap=(1.0, 3.0) # Semiconducting
)
# Get structure
struct = mpr.get_structure_by_material_id("mp-149")
# Get band structure
bs = mpr.get_bandstructure_by_material_id("mp-149")
# Get entries for phase diagram
entries = mpr.get_entries_in_chemsys("Li-Fe-O")Reference: See references/materials_project_api.md for comprehensive API documentation and examples.
8. Computational Workflow Setup
Set up calculations for various electronic structure codes.
VASP input generation:
from pymatgen.io.vasp.sets import MPRelaxSet, MPStaticSet, MPNonSCFSet
# Relaxation
relax = MPRelaxSet(struct)
relax.write_input("./relax_calc")
# Static calculation
static = MPStaticSet(struct)
static.write_input("./static_calc")
# Band structure (non-self-consistent)
nscf = MPNonSCFSet(struct, mode="line")
nscf.write_input("./bandstructure_calc")
# Custom parameters
custom = MPRelaxSet(struct, user_incar_settings={"ENCUT": 600})
custom.write_input("./custom_calc")Other codes:
# Gaussian
from pymatgen.io.gaussian import GaussianInput
gin = GaussianInput(
mol,
functional="B3LYP",
basis_set="6-31G(d)",
route_parameters={"Opt": None}
)
gin.write_file("input.gjf")
# Quantum ESPRESSO
from pymatgen.io.pwscf import PWInput
pwin = PWInput(struct, control={"calculation": "scf"})
pwin.write_file("pw.in")Reference: See references/io_formats.md (Electronic Structure Code I/O section) and references/transformations_workflows.md for workflow examples.
9. Advanced Analysis
Diffraction patterns:
from pymatgen.analysis.diffraction.xrd import XRDCalculator
xrd = XRDCalculator()
pattern = xrd.get_pattern(struct)
# Get peaks
for peak in pattern.hkls:
print(f"2θ = {peak['2theta']:.2f}°, hkl = {peak['hkl']}")
pattern.plot()Elastic properties:
from pymatgen.analysis.elasticity import ElasticTensor
# From elastic tensor matrix
elastic_tensor = ElasticTensor.from_voigt(matrix)
print(f"Bulk modulus: {elastic_tensor.k_voigt:.1f} GPa")
print(f"Shear modulus: {elastic_tensor.g_voigt:.1f} GPa")
print(f"Young's modulus: {elastic_tensor.y_mod:.1f} GPa")Magnetic ordering:
from pymatgen.transformations.advanced_transformations import MagOrderingTransformation
# Enumerate magnetic orderings
trans = MagOrderingTransformation({"Fe": 5.0})
mag_structs = trans.apply_transformation(struct, return_ranked_list=True)
# Get lowest energy magnetic structure
lowest_energy_struct = mag_structs[0]['structure']Reference: See references/analysis_modules.md for comprehensive analysis module documentation.
Bundled Resources
Scripts (scripts/)
Executable Python scripts for common tasks:
- `structure_converter.py`: Convert between structure file formats
- Supports batch conversion and automatic format detection
- Usage:
python scripts/structure_converter.py POSCAR structure.cif
- `structure_analyzer.py`: Comprehensive structure analysis
- Symmetry, coordination, lattice parameters, distance matrix
- Usage:
python scripts/structure_analyzer.py structure.cif --symmetry --neighbors
- `phase_diagram_generator.py`: Generate phase diagrams from Materials Project
- Stability analysis and thermodynamic properties
- Usage:
python scripts/phase_diagram_generator.py Li-Fe-O --analyze "LiFeO2"
All scripts include detailed help: python scripts/script_name.py --help
References (references/)
Comprehensive documentation loaded into context as needed:
- `core_classes.md`: Element, Structure, Lattice, Molecule, Composition classes
- `io_formats.md`: File format support and code integration (VASP, Gaussian, etc.)
- `analysis_modules.md`: Phase diagrams, surfaces, electronic structure, symmetry
- `materials_project_api.md`: Complete Materials Project API guide
- `transformations_workflows.md`: Transformations framework and common workflows
Load references when detailed information is needed about specific modules or workflows.
Common Workflows
High-Throughput Structure Generation
from pymatgen.transformations.standard_transformations import SubstitutionTransformation
from pymatgen.io.vasp.sets import MPRelaxSet
# Generate doped structures
base_struct = Structure.from_file("POSCAR")
dopants = ["Mn", "Co", "Ni", "Cu"]
for dopant in dopants:
trans = SubstitutionTransformation({"Fe": dopant})
doped_struct = trans.apply_transformation(base_struct)
# Generate VASP inputs
vasp_input = MPRelaxSet(doped_struct)
vasp_input.write_input(f"./calcs/Fe_{dopant}")Band Structure Calculation Workflow
# 1. Relaxation
relax = MPRelaxSet(struct)
relax.write_input("./1_relax")
# 2. Static (after relaxation)
relaxed = Structure.from_file("1_relax/CONTCAR")
static = MPStaticSet(relaxed)
static.write_input("./2_static")
# 3. Band structure (non-self-consistent)
nscf = MPNonSCFSet(relaxed, mode="line")
nscf.write_input("./3_bandstructure")
# 4. Analysis
from pymatgen.io.vasp import Vasprun
vasprun = Vasprun("3_bandstructure/vasprun.xml")
bs = vasprun.get_band_structure()
bs.get_band_gap()Surface Energy Calculation
# 1. Get bulk energy
bulk_vasprun = Vasprun("bulk/vasprun.xml")
bulk_E_per_atom = bulk_vasprun.final_energy / len(bulk)
# 2. Generate and calculate slabs
slabgen = SlabGenerator(bulk, (1,1,1), 10, 15)
slab = slabgen.get_slabs()[0]
MPRelaxSet(slab).write_input("./slab_calc")
# 3. Calculate surface energy (after calculation)
slab_vasprun = Vasprun("slab_calc/vasprun.xml")
E_surf = (slab_vasprun.final_energy - len(slab) * bulk_E_per_atom) / (2 * slab.surface_area)
E_surf *= 16.021766 # Convert eV/Ų to J/m²More workflows: See references/transformations_workflows.md for 10 detailed workflow examples.
Best Practices
Structure Handling
1. Use automatic format detection: Structure.from_file() handles most formats 2. Prefer immutable structures: Use IStructure when structure shouldn't change 3. Check symmetry: Use SpacegroupAnalyzer to reduce to primitive cell 4. Validate structures: Check for overlapping atoms or unreasonable bond lengths
File I/O
1. Use convenience methods: from_file() and to() are preferred 2. Specify formats explicitly: When automatic detection fails 3. Handle exceptions: Wrap file I/O in try-except blocks 4. Use serialization: as_dict()/from_dict() for version-safe storage
Materials Project API
1. Use context manager: Always use with MPRester() as mpr: 2. Batch queries: Request multiple items at once 3. Cache results: Save frequently used data locally 4. Filter effectively: Use property filters to reduce data transfer
Computational Workflows
1. Use input sets: Prefer MPRelaxSet, MPStaticSet over manual INCAR 2. Check convergence: Always verify calculations converged 3. Track transformations: Use TransformedStructure for provenance 4. Organize calculations: Use clear directory structures
Performance
1. Reduce symmetry: Use primitive cells when possible 2. Limit neighbor searches: Specify reasonable cutoff radii 3. Use appropriate methods: Different analysis tools have different speed/accuracy tradeoffs 4. Parallelize when possible: Many operations can be parallelized
Units and Conventions
Pymatgen uses atomic units throughout:
- Lengths: Angstroms (Å)
- Energies: Electronvolts (eV)
- Angles: Degrees (°)
- Magnetic moments: Bohr magnetons (μB)
- Time: Femtoseconds (fs)
Convert units using pymatgen.core.units when needed.
Integration with Other Tools
Pymatgen integrates seamlessly with:
- ASE (Atomic Simulation Environment)
- Phonopy (phonon calculations)
- BoltzTraP (transport properties)
- Atomate/Fireworks (workflow management)
- AiiDA (provenance tracking)
- Zeo++ (pore analysis)
- OpenBabel (molecule conversion)
Troubleshooting
Import errors: Install missing dependencies
pip install pymatgen[analysis,vis]API key not found: Set MP_API_KEY environment variable
export MP_API_KEY="your_key_here"Structure read failures: Check file format and syntax
# Try explicit format specification
struct = Structure.from_file("file.txt", fmt="cif")Symmetry analysis fails: Structure may have numerical precision issues
# Increase tolerance
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
sga = SpacegroupAnalyzer(struct, symprec=0.1)Additional Resources
- Documentation: https://pymatgen.org/
- Materials Project: https://materialsproject.org/
- GitHub: https://github.com/materialsproject/pymatgen
- Forum: https://matsci.org/
- Example notebooks: https://matgenb.materialsvirtuallab.org/
Version Notes
This skill is designed for pymatgen 2024.x and later. For the Materials Project API, use the mp-api package (separate from legacy pymatgen.ext.matproj).
Requirements:
- Python 3.10 or higher
- pymatgen >= 2023.x
- mp-api (for Materials Project access)
{
"description": "\"Materials science toolkit. Crystal structures (CIF, POSCAR), phase diagrams, band structure, DOS, Materials Project integration, format conversion, for computational materials science.\"",
"references": {
"files": [
"references/analysis_modules.md",
"references/core_classes.md",
"references/io_formats.md",
"references/materials_project_api.md",
"references/transformations_workflows.md"
]
},
"content": "### Installation\r\n\r\n```bash\r\npip install pymatgen\r\n\r\npip install pymatgen mp-api\r\n\r\npip install pymatgen[analysis] # Additional analysis tools\r\npip install pymatgen[vis] # Visualization tools\r\n```\r\n\r\n### Basic Structure Operations\r\n\r\n```python\r\nfrom pymatgen.core import Structure, Lattice\r\n\r\nstruct = Structure.from_file(\"POSCAR\")\r\n\r\nlattice = Lattice.cubic(3.84)\r\nstruct = Structure(lattice, [\"Si\", \"Si\"], [[0,0,0], [0.25,0.25,0.25]])\r\n\r\nstruct.to(filename=\"structure.cif\")\r\n\r\nprint(f\"Formula: {struct.composition.reduced_formula}\")\r\nprint(f\"Space group: {struct.get_space_group_info()}\")\r\nprint(f\"Density: {struct.density:.2f} g/cm³\")\r\n```\r\n\r\n### Materials Project Integration\r\n\r\n```bash\r\n\r\n### 1. Structure Creation and Manipulation\r\n\r\nCreate structures using various methods and perform transformations.\r\n\r\n**From files:**\r\n```python\r\nstruct = Structure.from_file(\"structure.cif\")\r\nstruct = Structure.from_file(\"POSCAR\")\r\nmol = Molecule.from_file(\"molecule.xyz\")\r\n```\r\n\r\n**From scratch:**\r\n```python\r\nfrom pymatgen.core import Structure, Lattice\r\n\r\nlattice = Lattice.from_parameters(a=3.84, b=3.84, c=3.84,\r\n alpha=120, beta=90, gamma=60)\r\ncoords = [[0, 0, 0], [0.75, 0.5, 0.75]]\r\nstruct = Structure(lattice, [\"Si\", \"Si\"], coords)\r\n\r\nstruct = Structure.from_spacegroup(\r\n \"Fm-3m\",\r\n Lattice.cubic(3.5),\r\n [\"Si\"],\r\n [[0, 0, 0]]\r\n)\r\n```\r\n\r\n**Transformations:**\r\n```python\r\nfrom pymatgen.transformations.standard_transformations import (\r\n SupercellTransformation,\r\n SubstitutionTransformation,\r\n PrimitiveCellTransformation\r\n)\r\n\r\ntrans = SupercellTransformation([[2,0,0],[0,2,0],[0,0,2]])\r\nsupercell = trans.apply_transformation(struct)\r\n\r\ntrans = SubstitutionTransformation({\"Fe\": \"Mn\"})\r\nnew_struct = trans.apply_transformation(struct)\r\n\r\ntrans = PrimitiveCellTransformation()\r\nprimitive = trans.apply_transformation(struct)\r\n```\r\n\r\n**Reference:** See `references/core_classes.md` for comprehensive documentation of Structure, Lattice, Molecule, and related classes.\r\n\r\n### 2. File Format Conversion\r\n\r\nConvert between 100+ file formats with automatic format detection.\r\n\r\n**Using convenience methods:**\r\n```python\r\nstruct = Structure.from_file(\"input_file\")\r\n\r\nstruct.to(filename=\"output.cif\")\r\nstruct.to(filename=\"POSCAR\")\r\nstruct.to(filename=\"output.xyz\")\r\n```\r\n\r\n**Using the conversion script:**\r\n```bash\r\npython scripts/structure_converter.py POSCAR structure.cif\r\n\r\npython scripts/structure_converter.py *.cif --output-dir ./poscar_files --format poscar\r\n```\r\n\r\n**Reference:** See `references/io_formats.md` for detailed documentation of all supported formats and code integrations.\r\n\r\n### 3. Structure Analysis and Symmetry\r\n\r\nAnalyze structures for symmetry, coordination, and other properties.\r\n\r\n**Symmetry analysis:**\r\n```python\r\nfrom pymatgen.symmetry.analyzer import SpacegroupAnalyzer\r\n\r\nsga = SpacegroupAnalyzer(struct)\r\n\r\nprint(f\"Space group: {sga.get_space_group_symbol()}\")\r\nprint(f\"Number: {sga.get_space_group_number()}\")\r\nprint(f\"Crystal system: {sga.get_crystal_system()}\")\r\n\r\nconventional = sga.get_conventional_standard_structure()\r\nprimitive = sga.get_primitive_standard_structure()\r\n```\r\n\r\n**Coordination environment:**\r\n```python\r\nfrom pymatgen.analysis.local_env import CrystalNN\r\n\r\ncnn = CrystalNN()\r\nneighbors = cnn.get_nn_info(struct, n=0) # Neighbors of site 0\r\n\r\nprint(f\"Coordination number: {len(neighbors)}\")\r\nfor neighbor in neighbors:\r\n site = struct[neighbor['site_index']]\r\n print(f\" {site.species_string} at {neighbor['weight']:.3f} Å\")\r\n```\r\n\r\n**Using the analysis script:**\r\n```bash\r\npython scripts/structure_analyzer.py POSCAR --symmetry --neighbors\r\n\r\npython scripts/structure_analyzer.py structure.cif --symmetry --export json\r\n```\r\n\r\n**Reference:** See `references/analysis_modules.md` for detailed documentation of all analysis capabilities.\r\n\r\n### 4. Phase Diagrams and Thermodynamics\r\n\r\nConstruct phase diagrams and analyze thermodynamic stability.\r\n\r\n**Phase diagram construction:**\r\n```python\r\nfrom mp_api.client import MPRester\r\nfrom pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter\r\n\r\nwith MPRester() as mpr:\r\n entries = mpr.get_entries_in_chemsys(\"Li-Fe-O\")\r\n\r\npd = PhaseDiagram(entries)\r\n\r\nfrom pymatgen.core import Composition\r\ncomp = Composition(\"LiFeO2\")\r\n\r\nfor entry in entries:\r\n if entry.composition.reduced_formula == comp.reduced_formula:\r\n e_above_hull = pd.get_e_above_hull(entry)\r\n print(f\"Energy above hull: {e_above_hull:.4f} eV/atom\")\r\n\r\n if e_above_hull > 0.001:\r\n # Get decomposition\r\n decomp = pd.get_decomposition(comp)\r\n print(\"Decomposes to:\", decomp)\r\n\r\nplotter = PDPlotter(pd)\r\nplotter.show()\r\n```\r\n\r\n**Using the phase diagram script:**\r\n```bash\r\npython scripts/phase_diagram_generator.py Li-Fe-O --output li_fe_o.png\r\n\r\npython scripts/phase_diagram_generator.py Li-Fe-O --analyze \"LiFeO2\" --show\r\n```\r\n\r\n**Reference:** See `references/analysis_modules.md` (Phase Diagrams section) and `references/transformations_workflows.md` (Workflow 2) for detailed examples.\r\n\r\n### 5. Electronic Structure Analysis\r\n\r\nAnalyze band structures, density of states, and electronic properties.\r\n\r\n**Band structure:**\r\n```python\r\nfrom pymatgen.io.vasp import Vasprun\r\nfrom pymatgen.electronic_structure.plotter import BSPlotter\r\n\r\nvasprun = Vasprun(\"vasprun.xml\")\r\nbs = vasprun.get_band_structure()\r\n\r\nband_gap = bs.get_band_gap()\r\nprint(f\"Band gap: {band_gap['energy']:.3f} eV\")\r\nprint(f\"Direct: {band_gap['direct']}\")\r\nprint(f\"Is metal: {bs.is_metal()}\")\r\n\r\nplotter = BSPlotter(bs)\r\nplotter.save_plot(\"band_structure.png\")\r\n```\r\n\r\n**Density of states:**\r\n```python\r\nfrom pymatgen.electronic_structure.plotter import DosPlotter\r\n\r\ndos = vasprun.complete_dos\r\n\r\nelement_dos = dos.get_element_dos()\r\nfor element, element_dos_obj in element_dos.items():\r\n print(f\"{element}: {element_dos_obj.get_gap():.3f} eV\")\r\n\r\nplotter = DosPlotter()\r\nplotter.add_dos(\"Total DOS\", dos)\r\nplotter.show()\r\n```\r\n\r\n**Reference:** See `references/analysis_modules.md` (Electronic Structure section) and `references/io_formats.md` (VASP section).\r\n\r\n### 6. Surface and Interface Analysis\r\n\r\nGenerate slabs, analyze surfaces, and study interfaces.\r\n\r\n**Slab generation:**\r\n```python\r\nfrom pymatgen.core.surface import SlabGenerator\r\n\r\nslabgen = SlabGenerator(\r\n struct,\r\n miller_index=(1, 1, 1),\r\n min_slab_size=10.0, # Å\r\n min_vacuum_size=10.0, # Å\r\n center_slab=True\r\n)\r\n\r\nslabs = slabgen.get_slabs()\r\n\r\nfor i, slab in enumerate(slabs):\r\n slab.to(filename=f\"slab_{i}.cif\")\r\n```\r\n\r\n**Wulff shape construction:**\r\n```python\r\nfrom pymatgen.analysis.wulff import WulffShape\r\n\r\nsurface_energies = {\r\n (1, 0, 0): 1.0,\r\n (1, 1, 0): 1.1,\r\n (1, 1, 1): 0.9,\r\n}\r\n\r\nwulff = WulffShape(struct.lattice, surface_energies)\r\nprint(f\"Surface area: {wulff.surface_area:.2f} Ų\")\r\nprint(f\"Volume: {wulff.volume:.2f} ų\")\r\n\r\nwulff.show()\r\n```\r\n\r\n**Adsorption site finding:**\r\n```python\r\nfrom pymatgen.analysis.adsorption import AdsorbateSiteFinder\r\nfrom pymatgen.core import Molecule\r\n\r\nasf = AdsorbateSiteFinder(slab)\r\n\r\nads_sites = asf.find_adsorption_sites()\r\nprint(f\"On-top sites: {len(ads_sites['ontop'])}\")\r\nprint(f\"Bridge sites: {len(ads_sites['bridge'])}\")\r\nprint(f\"Hollow sites: {len(ads_sites['hollow'])}\")\r\n\r\nadsorbate = Molecule(\"O\", [[0, 0, 0]])\r\nads_struct = asf.add_adsorbate(adsorbate, ads_sites[\"ontop\"][0])\r\n```\r\n\r\n**Reference:** See `references/analysis_modules.md` (Surface and Interface section) and `references/transformations_workflows.md` (Workflows 3 and 9).\r\n\r\n### 7. Materials Project Database Access\r\n\r\nProgrammatically access the Materials Project database.\r\n\r\n**Setup:**\r\n1. Get API key from https://next-gen.materialsproject.org/\r\n2. Set environment variable: `export MP_API_KEY=\"your_key_here\"`\r\n\r\n**Search and retrieve:**\r\n```python\r\nfrom mp_api.client import MPRester\r\n\r\nwith MPRester() as mpr:\r\n # Search by formula\r\n materials = mpr.materials.summary.search(formula=\"Fe2O3\")\r\n\r\n # Search by chemical system\r\n materials = mpr.materials.summary.search(chemsys=\"Li-Fe-O\")\r\n\r\n # Filter by properties\r\n materials = mpr.materials.summary.search(\r\n chemsys=\"Li-Fe-O\",\r\n energy_above_hull=(0, 0.05), # Stable/metastable\r\n band_gap=(1.0, 3.0) # Semiconducting\r\n )\r\n\r\n # Get structure\r\n struct = mpr.get_structure_by_material_id(\"mp-149\")\r\n\r\n # Get band structure\r\n bs = mpr.get_bandstructure_by_material_id(\"mp-149\")\r\n\r\n # Get entries for phase diagram\r\n entries = mpr.get_entries_in_chemsys(\"Li-Fe-O\")\r\n```\r\n\r\n**Reference:** See `references/materials_project_api.md` for comprehensive API documentation and examples.\r\n\r\n### 8. Computational Workflow Setup\r\n\r\nSet up calculations for various electronic structure codes.\r\n\r\n**VASP input generation:**\r\n```python\r\nfrom pymatgen.io.vasp.sets import MPRelaxSet, MPStaticSet, MPNonSCFSet\r\n\r\nrelax = MPRelaxSet(struct)\r\nrelax.write_input(\"./relax_calc\")\r\n\r\nstatic = MPStaticSet(struct)\r\nstatic.write_input(\"./static_calc\")\r\n\r\nnscf = MPNonSCFSet(struct, mode=\"line\")\r\nnscf.write_input(\"./bandstructure_calc\")\r\n\r\ncustom = MPRelaxSet(struct, user_incar_settings={\"ENCUT\": 600})\r\ncustom.write_input(\"./custom_calc\")\r\n```\r\n\r\n**Other codes:**\r\n```python\r\nfrom pymatgen.io.gaussian import GaussianInput\r\n\r\ngin = GaussianInput(\r\n mol,\r\n functional=\"B3LYP\",\r\n basis_set=\"6-31G(d)\",\r\n route_parameters={\"Opt\": None}\r\n)\r\ngin.write_file(\"input.gjf\")\r\n\r\nfrom pymatgen.io.pwscf import PWInput\r\n\r\npwin = PWInput(struct, control={\"calculation\": \"scf\"})\r\npwin.write_file(\"pw.in\")\r\n```\r\n\r\n**Reference:** See `references/io_formats.md` (Electronic Structure Code I/O section) and `references/transformations_workflows.md` for workflow examples.\r\n\r\n### 9. Advanced Analysis\r\n\r\n**Diffraction patterns:**\r\n```python\r\nfrom pymatgen.analysis.diffraction.xrd import XRDCalculator\r\n\r\nxrd = XRDCalculator()\r\npattern = xrd.get_pattern(struct)\r\n\r\nfor peak in pattern.hkls:\r\n print(f\"2θ = {peak['2theta']:.2f}°, hkl = {peak['hkl']}\")\r\n\r\npattern.plot()\r\n```\r\n\r\n**Elastic properties:**\r\n```python\r\nfrom pymatgen.analysis.elasticity import ElasticTensor\r\n\r\nelastic_tensor = ElasticTensor.from_voigt(matrix)\r\n\r\nprint(f\"Bulk modulus: {elastic_tensor.k_voigt:.1f} GPa\")\r\nprint(f\"Shear modulus: {elastic_tensor.g_voigt:.1f} GPa\")\r\nprint(f\"Young's modulus: {elastic_tensor.y_mod:.1f} GPa\")\r\n```\r\n\r\n**Magnetic ordering:**\r\n```python\r\nfrom pymatgen.transformations.advanced_transformations import MagOrderingTransformation\r\n\r\ntrans = MagOrderingTransformation({\"Fe\": 5.0})\r\nmag_structs = trans.apply_transformation(struct, return_ranked_list=True)\r\n\r\n\r\n### High-Throughput Structure Generation\r\n\r\n```python\r\nfrom pymatgen.transformations.standard_transformations import SubstitutionTransformation\r\nfrom pymatgen.io.vasp.sets import MPRelaxSet\r\n\r\nbase_struct = Structure.from_file(\"POSCAR\")\r\ndopants = [\"Mn\", \"Co\", \"Ni\", \"Cu\"]\r\n\r\nfor dopant in dopants:\r\n trans = SubstitutionTransformation({\"Fe\": dopant})\r\n doped_struct = trans.apply_transformation(base_struct)\r\n\r\n # Generate VASP inputs\r\n vasp_input = MPRelaxSet(doped_struct)\r\n vasp_input.write_input(f\"./calcs/Fe_{dopant}\")\r\n```\r\n\r\n### Band Structure Calculation Workflow\r\n\r\n```python\r\nrelax = MPRelaxSet(struct)\r\nrelax.write_input(\"./1_relax\")\r\n\r\nrelaxed = Structure.from_file(\"1_relax/CONTCAR\")\r\nstatic = MPStaticSet(relaxed)\r\nstatic.write_input(\"./2_static\")\r\n\r\nnscf = MPNonSCFSet(relaxed, mode=\"line\")\r\nnscf.write_input(\"./3_bandstructure\")\r\n\r\nfrom pymatgen.io.vasp import Vasprun\r\nvasprun = Vasprun(\"3_bandstructure/vasprun.xml\")\r\nbs = vasprun.get_band_structure()\r\nbs.get_band_gap()\r\n```\r\n\r\n### Surface Energy Calculation\r\n\r\n```python\r\nbulk_vasprun = Vasprun(\"bulk/vasprun.xml\")\r\nbulk_E_per_atom = bulk_vasprun.final_energy / len(bulk)\r\n\r\nslabgen = SlabGenerator(bulk, (1,1,1), 10, 15)\r\nslab = slabgen.get_slabs()[0]\r\n\r\nMPRelaxSet(slab).write_input(\"./slab_calc\")\r\n\r\n\r\n**Import errors**: Install missing dependencies\r\n```bash\r\npip install pymatgen[analysis,vis]\r\n```\r\n\r\n**API key not found**: Set MP_API_KEY environment variable\r\n```bash\r\nexport MP_API_KEY=\"your_key_here\"\r\n```\r\n\r\n**Structure read failures**: Check file format and syntax\r\n```python\r\nstruct = Structure.from_file(\"file.txt\", fmt=\"cif\")\r\n```\r\n\r\n**Symmetry analysis fails**: Structure may have numerical precision issues\r\n```python",
"name": "pymatgen",
"id": "scientific-pkg-pymatgen",
"sections": {
"Version Notes": "This skill is designed for pymatgen 2024.x and later. For the Materials Project API, use the `mp-api` package (separate from legacy `pymatgen.ext.matproj`).\r\n\r\nRequirements:\r\n- Python 3.10 or higher\r\n- pymatgen >= 2023.x\r\n- mp-api (for Materials Project access)",
"Quick Start Guide": "export MP_API_KEY=\"your_api_key_here\"\r\n```\r\n\r\n```python\r\nfrom mp_api.client import MPRester\r\n\r\nwith MPRester() as mpr:\r\n # Get structure by material ID\r\n struct = mpr.get_structure_by_material_id(\"mp-149\")\r\n\r\n # Search for materials\r\n materials = mpr.materials.summary.search(\r\n formula=\"Fe2O3\",\r\n energy_above_hull=(0, 0.05)\r\n )\r\n```",
"Units and Conventions": "Pymatgen uses atomic units throughout:\r\n- **Lengths**: Angstroms (Å)\r\n- **Energies**: Electronvolts (eV)\r\n- **Angles**: Degrees (°)\r\n- **Magnetic moments**: Bohr magnetons (μB)\r\n- **Time**: Femtoseconds (fs)\r\n\r\nConvert units using `pymatgen.core.units` when needed.",
"Troubleshooting": "from pymatgen.symmetry.analyzer import SpacegroupAnalyzer\r\nsga = SpacegroupAnalyzer(struct, symprec=0.1)\r\n```",
"Integration with Other Tools": "Pymatgen integrates seamlessly with:\r\n- **ASE** (Atomic Simulation Environment)\r\n- **Phonopy** (phonon calculations)\r\n- **BoltzTraP** (transport properties)\r\n- **Atomate/Fireworks** (workflow management)\r\n- **AiiDA** (provenance tracking)\r\n- **Zeo++** (pore analysis)\r\n- **OpenBabel** (molecule conversion)",
"Overview": "Pymatgen is a comprehensive Python library for materials analysis that powers the Materials Project. Create, analyze, and manipulate crystal structures and molecules, compute phase diagrams and thermodynamic properties, analyze electronic structure (band structures, DOS), generate surfaces and interfaces, and access Materials Project's database of computed materials. Supports 100+ file formats from various computational codes.",
"Best Practices": "### Structure Handling\r\n\r\n1. **Use automatic format detection**: `Structure.from_file()` handles most formats\r\n2. **Prefer immutable structures**: Use `IStructure` when structure shouldn't change\r\n3. **Check symmetry**: Use `SpacegroupAnalyzer` to reduce to primitive cell\r\n4. **Validate structures**: Check for overlapping atoms or unreasonable bond lengths\r\n\r\n### File I/O\r\n\r\n1. **Use convenience methods**: `from_file()` and `to()` are preferred\r\n2. **Specify formats explicitly**: When automatic detection fails\r\n3. **Handle exceptions**: Wrap file I/O in try-except blocks\r\n4. **Use serialization**: `as_dict()`/`from_dict()` for version-safe storage\r\n\r\n### Materials Project API\r\n\r\n1. **Use context manager**: Always use `with MPRester() as mpr:`\r\n2. **Batch queries**: Request multiple items at once\r\n3. **Cache results**: Save frequently used data locally\r\n4. **Filter effectively**: Use property filters to reduce data transfer\r\n\r\n### Computational Workflows\r\n\r\n1. **Use input sets**: Prefer `MPRelaxSet`, `MPStaticSet` over manual INCAR\r\n2. **Check convergence**: Always verify calculations converged\r\n3. **Track transformations**: Use `TransformedStructure` for provenance\r\n4. **Organize calculations**: Use clear directory structures\r\n\r\n### Performance\r\n\r\n1. **Reduce symmetry**: Use primitive cells when possible\r\n2. **Limit neighbor searches**: Specify reasonable cutoff radii\r\n3. **Use appropriate methods**: Different analysis tools have different speed/accuracy tradeoffs\r\n4. **Parallelize when possible**: Many operations can be parallelized",
"When to Use This Skill": "This skill should be used when:\r\n- Working with crystal structures or molecular systems in materials science\r\n- Converting between structure file formats (CIF, POSCAR, XYZ, etc.)\r\n- Analyzing symmetry, space groups, or coordination environments\r\n- Computing phase diagrams or assessing thermodynamic stability\r\n- Analyzing electronic structure data (band gaps, DOS, band structures)\r\n- Generating surfaces, slabs, or studying interfaces\r\n- Accessing the Materials Project database programmatically\r\n- Setting up high-throughput computational workflows\r\n- Analyzing diffusion, magnetism, or mechanical properties\r\n- Working with VASP, Gaussian, Quantum ESPRESSO, or other computational codes",
"Bundled Resources": "### Scripts (`scripts/`)\r\n\r\nExecutable Python scripts for common tasks:\r\n\r\n- **`structure_converter.py`**: Convert between structure file formats\r\n - Supports batch conversion and automatic format detection\r\n - Usage: `python scripts/structure_converter.py POSCAR structure.cif`\r\n\r\n- **`structure_analyzer.py`**: Comprehensive structure analysis\r\n - Symmetry, coordination, lattice parameters, distance matrix\r\n - Usage: `python scripts/structure_analyzer.py structure.cif --symmetry --neighbors`\r\n\r\n- **`phase_diagram_generator.py`**: Generate phase diagrams from Materials Project\r\n - Stability analysis and thermodynamic properties\r\n - Usage: `python scripts/phase_diagram_generator.py Li-Fe-O --analyze \"LiFeO2\"`\r\n\r\nAll scripts include detailed help: `python scripts/script_name.py --help`\r\n\r\n### References (`references/`)\r\n\r\nComprehensive documentation loaded into context as needed:\r\n\r\n- **`core_classes.md`**: Element, Structure, Lattice, Molecule, Composition classes\r\n- **`io_formats.md`**: File format support and code integration (VASP, Gaussian, etc.)\r\n- **`analysis_modules.md`**: Phase diagrams, surfaces, electronic structure, symmetry\r\n- **`materials_project_api.md`**: Complete Materials Project API guide\r\n- **`transformations_workflows.md`**: Transformations framework and common workflows\r\n\r\nLoad references when detailed information is needed about specific modules or workflows.",
"Core Capabilities": "lowest_energy_struct = mag_structs[0]['structure']\r\n```\r\n\r\n**Reference:** See `references/analysis_modules.md` for comprehensive analysis module documentation.",
"Common Workflows": "slab_vasprun = Vasprun(\"slab_calc/vasprun.xml\")\r\nE_surf = (slab_vasprun.final_energy - len(slab) * bulk_E_per_atom) / (2 * slab.surface_area)\r\nE_surf *= 16.021766 # Convert eV/Ų to J/m²\r\n```\r\n\r\n**More workflows:** See `references/transformations_workflows.md` for 10 detailed workflow examples.",
"Additional Resources": "- **Documentation**: https://pymatgen.org/\r\n- **Materials Project**: https://materialsproject.org/\r\n- **GitHub**: https://github.com/materialsproject/pymatgen\r\n- **Forum**: https://matsci.org/\r\n- **Example notebooks**: https://matgenb.materialsvirtuallab.org/"
}
}---
name: pymatgen
description: "Materials science toolkit. Crystal structures (CIF, POSCAR), phase diagrams, band structure, DOS, Materials Project integration, format conversion, for computational materials science."
---
# Pymatgen - Python Materials Genomics
## Overview
Pymatgen is a comprehensive Python library for materials analysis that powers the Materials Project. Create, analyze, and manipulate crystal structures and molecules, compute phase diagrams and thermodynamic properties, analyze electronic structure (band structures, DOS), generate surfaces and interfaces, and access Materials Project's database of computed materials. Supports 100+ file formats from various computational codes.
## When to Use This Skill
This skill should be used when:
- Working with crystal structures or molecular systems in materials science
- Converting between structure file formats (CIF, POSCAR, XYZ, etc.)
- Analyzing symmetry, space groups, or coordination environments
- Computing phase diagrams or assessing thermodynamic stability
- Analyzing electronic structure data (band gaps, DOS, band structures)
- Generating surfaces, slabs, or studying interfaces
- Accessing the Materials Project database programmatically
- Setting up high-throughput computational workflows
- Analyzing diffusion, magnetism, or mechanical properties
- Working with VASP, Gaussian, Quantum ESPRESSO, or other computational codes
## Quick Start Guide
### Installation
```bash
# Core pymatgen
pip install pymatgen
# With Materials Project API access
pip install pymatgen mp-api
# Optional dependencies for extended functionality
pip install pymatgen[analysis] # Additional analysis tools
pip install pymatgen[vis] # Visualization tools
```
### Basic Structure Operations
```python
from pymatgen.core import Structure, Lattice
# Read structure from file (automatic format detection)
struct = Structure.from_file("POSCAR")
# Create structure from scratch
lattice = Lattice.cubic(3.84)
struct = Structure(lattice, ["Si", "Si"], [[0,0,0], [0.25,0.25,0.25]])
# Write to different format
struct.to(filename="structure.cif")
# Basic properties
print(f"Formula: {struct.composition.reduced_formula}")
print(f"Space group: {struct.get_space_group_info()}")
print(f"Density: {struct.density:.2f} g/cm³")
```
### Materials Project Integration
```bash
# Set up API key
export MP_API_KEY="your_api_key_here"
```
```python
from mp_api.client import MPRester
with MPRester() as mpr:
# Get structure by material ID
struct = mpr.get_structure_by_material_id("mp-149")
# Search for materials
materials = mpr.materials.summary.search(
formula="Fe2O3",
energy_above_hull=(0, 0.05)
)
```
## Core Capabilities
### 1. Structure Creation and Manipulation
Create structures using various methods and perform transformations.
**From files:**
```python
# Automatic format detection
struct = Structure.from_file("structure.cif")
struct = Structure.from_file("POSCAR")
mol = Molecule.from_file("molecule.xyz")
```
**From scratch:**
```python
from pymatgen.core import Structure, Lattice
# Using lattice parameters
lattice = Lattice.from_parameters(a=3.84, b=3.84, c=3.84,
alpha=120, beta=90, gamma=60)
coords = [[0, 0, 0], [0.75, 0.5, 0.75]]
struct = Structure(lattice, ["Si", "Si"], coords)
# From space group
struct = Structure.from_spacegroup(
"Fm-3m",
Lattice.cubic(3.5),
["Si"],
[[0, 0, 0]]
)
```
**Transformations:**
```python
from pymatgen.transformations.standard_transformations import (
SupercellTransformation,
SubstitutionTransformation,
PrimitiveCellTransformation
)
# Create supercell
trans = SupercellTransformation([[2,0,0],[0,2,0],[0,0,2]])
supercell = trans.apply_transformation(struct)
# Substitute elements
trans = SubstitutionTransformation({"Fe": "Mn"})
new_struct = trans.apply_transformation(struct)
# Get primitive cell
trans = PrimitiveCellTransformation()
primitive = trans.apply_transformation(struct)
```
**Reference:** See `references/core_classes.md` for comprehensive documentation of Structure, Lattice, Molecule, and related classes.
### 2. File Format Conversion
Convert between 100+ file formats with automatic format detection.
**Using convenience methods:**
```python
# Read any format
struct = Structure.from_file("input_file")
# Write to any format
struct.to(filename="output.cif")
struct.to(filename="POSCAR")
struct.to(filename="output.xyz")
```
**Using the conversion script:**
```bash
# Single file conversion
python scripts/structure_converter.py POSCAR structure.cif
# Batch conversion
python scripts/structure_converter.py *.cif --output-dir ./poscar_files --format poscar
```
**Reference:** See `references/io_formats.md` for detailed documentation of all supported formats and code integrations.
### 3. Structure Analysis and Symmetry
Analyze structures for symmetry, coordination, and other properties.
**Symmetry analysis:**
```python
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
sga = SpacegroupAnalyzer(struct)
# Get space group information
print(f"Space group: {sga.get_space_group_symbol()}")
print(f"Number: {sga.get_space_group_number()}")
print(f"Crystal system: {sga.get_crystal_system()}")
# Get conventional/primitive cells
conventional = sga.get_conventional_standard_structure()
primitive = sga.get_primitive_standard_structure()
```
**Coordination environment:**
```python
from pymatgen.analysis.local_env import CrystalNN
cnn = CrystalNN()
neighbors = cnn.get_nn_info(struct, n=0) # Neighbors of site 0
print(f"Coordination number: {len(neighbors)}")
for neighbor in neighbors:
site = struct[neighbor['site_index']]
print(f" {site.species_string} at {neighbor['weight']:.3f} Å")
```
**Using the analysis script:**
```bash
# Comprehensive analysis
python scripts/structure_analyzer.py POSCAR --symmetry --neighbors
# Export results
python scripts/structure_analyzer.py structure.cif --symmetry --export json
```
**Reference:** See `references/analysis_modules.md` for detailed documentation of all analysis capabilities.
### 4. Phase Diagrams and Thermodynamics
Construct phase diagrams and analyze thermodynamic stability.
**Phase diagram construction:**
```python
from mp_api.client import MPRester
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter
# Get entries from Materials Project
with MPRester() as mpr:
entries = mpr.get_entries_in_chemsys("Li-Fe-O")
# Build phase diagram
pd = PhaseDiagram(entries)
# Check stability
from pymatgen.core import Composition
comp = Composition("LiFeO2")
# Find entry for composition
for entry in entries:
if entry.composition.reduced_formula == comp.reduced_formula:
e_above_hull = pd.get_e_above_hull(entry)
print(f"Energy above hull: {e_above_hull:.4f} eV/atom")
if e_above_hull > 0.001:
# Get decomposition
decomp = pd.get_decomposition(comp)
print("Decomposes to:", decomp)
# Plot
plotter = PDPlotter(pd)
plotter.show()
```
**Using the phase diagram script:**
```bash
# Generate phase diagram
python scripts/phase_diagram_generator.py Li-Fe-O --output li_fe_o.png
# Analyze specific composition
python scripts/phase_diagram_generator.py Li-Fe-O --analyze "LiFeO2" --show
```
**Reference:** See `references/analysis_modules.md` (Phase Diagrams section) and `references/transformations_workflows.md` (Workflow 2) for detailed examples.
### 5. Electronic Structure Analysis
Analyze band structures, density of states, and electronic properties.
**Band structure:**
```python
from pymatgen.io.vasp import Vasprun
from pymatgen.electronic_structure.plotter import BSPlotter
# Read from VASP calculation
vasprun = Vasprun("vasprun.xml")
bs = vasprun.get_band_structure()
# Analyze
band_gap = bs.get_band_gap()
print(f"Band gap: {band_gap['energy']:.3f} eV")
print(f"Direct: {band_gap['direct']}")
print(f"Is metal: {bs.is_metal()}")
# Plot
plotter = BSPlotter(bs)
plotter.save_plot("band_structure.png")
```
**Density of states:**
```python
from pymatgen.electronic_structure.plotter import DosPlotter
dos = vasprun.complete_dos
# Get element-projected DOS
element_dos = dos.get_element_dos()
for element, element_dos_obj in element_dos.items():
print(f"{element}: {element_dos_obj.get_gap():.3f} eV")
# Plot
plotter = DosPlotter()
plotter.add_dos("Total DOS", dos)
plotter.show()
```
**Reference:** See `references/analysis_modules.md` (Electronic Structure section) and `references/io_formats.md` (VASP section).
### 6. Surface and Interface Analysis
Generate slabs, analyze surfaces, and study interfaces.
**Slab generation:**
```python
from pymatgen.core.surface import SlabGenerator
# Generate slabs for specific Miller index
slabgen = SlabGenerator(
struct,
miller_index=(1, 1, 1),
min_slab_size=10.0, # Å
min_vacuum_size=10.0, # Å
center_slab=True
)
slabs = slabgen.get_slabs()
# Write slabs
for i, slab in enumerate(slabs):
slab.to(filename=f"slab_{i}.cif")
```
**Wulff shape construction:**
```python
from pymatgen.analysis.wulff import WulffShape
# Define surface energies
surface_energies = {
(1, 0, 0): 1.0,
(1, 1, 0): 1.1,
(1, 1, 1): 0.9,
}
wulff = WulffShape(struct.lattice, surface_energies)
print(f"Surface area: {wulff.surface_area:.2f} Ų")
print(f"Volume: {wulff.volume:.2f} ų")
wulff.show()
```
**Adsorption site finding:**
```python
from pymatgen.analysis.adsorption import AdsorbateSiteFinder
from pymatgen.core import Molecule
asf = AdsorbateSiteFinder(slab)
# Find sites
ads_sites = asf.find_adsorption_sites()
print(f"On-top sites: {len(ads_sites['ontop'])}")
print(f"Bridge sites: {len(ads_sites['bridge'])}")
print(f"Hollow sites: {len(ads_sites['hollow'])}")
# Add adsorbate
adsorbate = Molecule("O", [[0, 0, 0]])
ads_struct = asf.add_adsorbate(adsorbate, ads_sites["ontop"][0])
```
**Reference:** See `references/analysis_modules.md` (Surface and Interface section) and `references/transformations_workflows.md` (Workflows 3 and 9).
### 7. Materials Project Database Access
Programmatically access the Materials Project database.
**Setup:**
1. Get API key from https://next-gen.materialsproject.org/
2. Set environment variable: `export MP_API_KEY="your_key_here"`
**Search and retrieve:**
```python
from mp_api.client import MPRester
with MPRester() as mpr:
# Search by formula
materials = mpr.materials.summary.search(formula="Fe2O3")
# Search by chemical system
materials = mpr.materials.summary.search(chemsys="Li-Fe-O")
# Filter by properties
materials = mpr.materials.summary.search(
chemsys="Li-Fe-O",
energy_above_hull=(0, 0.05), # Stable/metastable
band_gap=(1.0, 3.0) # Semiconducting
)
# Get structure
struct = mpr.get_structure_by_material_id("mp-149")
# Get band structure
bs = mpr.get_bandstructure_by_material_id("mp-149")
# Get entries for phase diagram
entries = mpr.get_entries_in_chemsys("Li-Fe-O")
```
**Reference:** See `references/materials_project_api.md` for comprehensive API documentation and examples.
### 8. Computational Workflow Setup
Set up calculations for various electronic structure codes.
**VASP input generation:**
```python
from pymatgen.io.vasp.sets import MPRelaxSet, MPStaticSet, MPNonSCFSet
# Relaxation
relax = MPRelaxSet(struct)
relax.write_input("./relax_calc")
# Static calculation
static = MPStaticSet(struct)
static.write_input("./static_calc")
# Band structure (non-self-consistent)
nscf = MPNonSCFSet(struct, mode="line")
nscf.write_input("./bandstructure_calc")
# Custom parameters
custom = MPRelaxSet(struct, user_incar_settings={"ENCUT": 600})
custom.write_input("./custom_calc")
```
**Other codes:**
```python
# Gaussian
from pymatgen.io.gaussian import GaussianInput
gin = GaussianInput(
mol,
functional="B3LYP",
basis_set="6-31G(d)",
route_parameters={"Opt": None}
)
gin.write_file("input.gjf")
# Quantum ESPRESSO
from pymatgen.io.pwscf import PWInput
pwin = PWInput(struct, control={"calculation": "scf"})
pwin.write_file("pw.in")
```
**Reference:** See `references/io_formats.md` (Electronic Structure Code I/O section) and `references/transformations_workflows.md` for workflow examples.
### 9. Advanced Analysis
**Diffraction patterns:**
```python
from pymatgen.analysis.diffraction.xrd import XRDCalculator
xrd = XRDCalculator()
pattern = xrd.get_pattern(struct)
# Get peaks
for peak in pattern.hkls:
print(f"2θ = {peak['2theta']:.2f}°, hkl = {peak['hkl']}")
pattern.plot()
```
**Elastic properties:**
```python
from pymatgen.analysis.elasticity import ElasticTensor
# From elastic tensor matrix
elastic_tensor = ElasticTensor.from_voigt(matrix)
print(f"Bulk modulus: {elastic_tensor.k_voigt:.1f} GPa")
print(f"Shear modulus: {elastic_tensor.g_voigt:.1f} GPa")
print(f"Young's modulus: {elastic_tensor.y_mod:.1f} GPa")
```
**Magnetic ordering:**
```python
from pymatgen.transformations.advanced_transformations import MagOrderingTransformation
# Enumerate magnetic orderings
trans = MagOrderingTransformation({"Fe": 5.0})
mag_structs = trans.apply_transformation(struct, return_ranked_list=True)
# Get lowest energy magnetic structure
lowest_energy_struct = mag_structs[0]['structure']
```
**Reference:** See `references/analysis_modules.md` for comprehensive analysis module documentation.
## Bundled Resources
### Scripts (`scripts/`)
Executable Python scripts for common tasks:
- **`structure_converter.py`**: Convert between structure file formats
- Supports batch conversion and automatic format detection
- Usage: `python scripts/structure_converter.py POSCAR structure.cif`
- **`structure_analyzer.py`**: Comprehensive structure analysis
- Symmetry, coordination, lattice parameters, distance matrix
- Usage: `python scripts/structure_analyzer.py structure.cif --symmetry --neighbors`
- **`phase_diagram_generator.py`**: Generate phase diagrams from Materials Project
- Stability analysis and thermodynamic properties
- Usage: `python scripts/phase_diagram_generator.py Li-Fe-O --analyze "LiFeO2"`
All scripts include detailed help: `python scripts/script_name.py --help`
### References (`references/`)
Comprehensive documentation loaded into context as needed:
- **`core_classes.md`**: Element, Structure, Lattice, Molecule, Composition classes
- **`io_formats.md`**: File format support and code integration (VASP, Gaussian, etc.)
- **`analysis_modules.md`**: Phase diagrams, surfaces, electronic structure, symmetry
- **`materials_project_api.md`**: Complete Materials Project API guide
- **`transformations_workflows.md`**: Transformations framework and common workflows
Load references when detailed information is needed about specific modules or workflows.
## Common Workflows
### High-Throughput Structure Generation
```python
from pymatgen.transformations.standard_transformations import SubstitutionTransformation
from pymatgen.io.vasp.sets import MPRelaxSet
# Generate doped structures
base_struct = Structure.from_file("POSCAR")
dopants = ["Mn", "Co", "Ni", "Cu"]
for dopant in dopants:
trans = SubstitutionTransformation({"Fe": dopant})
doped_struct = trans.apply_transformation(base_struct)
# Generate VASP inputs
vasp_input = MPRelaxSet(doped_struct)
vasp_input.write_input(f"./calcs/Fe_{dopant}")
```
### Band Structure Calculation Workflow
```python
# 1. Relaxation
relax = MPRelaxSet(struct)
relax.write_input("./1_relax")
# 2. Static (after relaxation)
relaxed = Structure.from_file("1_relax/CONTCAR")
static = MPStaticSet(relaxed)
static.write_input("./2_static")
# 3. Band structure (non-self-consistent)
nscf = MPNonSCFSet(relaxed, mode="line")
nscf.write_input("./3_bandstructure")
# 4. Analysis
from pymatgen.io.vasp import Vasprun
vasprun = Vasprun("3_bandstructure/vasprun.xml")
bs = vasprun.get_band_structure()
bs.get_band_gap()
```
### Surface Energy Calculation
```python
# 1. Get bulk energy
bulk_vasprun = Vasprun("bulk/vasprun.xml")
bulk_E_per_atom = bulk_vasprun.final_energy / len(bulk)
# 2. Generate and calculate slabs
slabgen = SlabGenerator(bulk, (1,1,1), 10, 15)
slab = slabgen.get_slabs()[0]
MPRelaxSet(slab).write_input("./slab_calc")
# 3. Calculate surface energy (after calculation)
slab_vasprun = Vasprun("slab_calc/vasprun.xml")
E_surf = (slab_vasprun.final_energy - len(slab) * bulk_E_per_atom) / (2 * slab.surface_area)
E_surf *= 16.021766 # Convert eV/Ų to J/m²
```
**More workflows:** See `references/transformations_workflows.md` for 10 detailed workflow examples.
## Best Practices
### Structure Handling
1. **Use automatic format detection**: `Structure.from_file()` handles most formats
2. **Prefer immutable structures**: Use `IStructure` when structure shouldn't change
3. **Check symmetry**: Use `SpacegroupAnalyzer` to reduce to primitive cell
4. **Validate structures**: Check for overlapping atoms or unreasonable bond lengths
### File I/O
1. **Use convenience methods**: `from_file()` and `to()` are preferred
2. **Specify formats explicitly**: When automatic detection fails
3. **Handle exceptions**: Wrap file I/O in try-except blocks
4. **Use serialization**: `as_dict()`/`from_dict()` for version-safe storage
### Materials Project API
1. **Use context manager**: Always use `with MPRester() as mpr:`
2. **Batch queries**: Request multiple items at once
3. **Cache results**: Save frequently used data locally
4. **Filter effectively**: Use property filters to reduce data transfer
### Computational Workflows
1. **Use input sets**: Prefer `MPRelaxSet`, `MPStaticSet` over manual INCAR
2. **Check convergence**: Always verify calculations converged
3. **Track transformations**: Use `TransformedStructure` for provenance
4. **Organize calculations**: Use clear directory structures
### Performance
1. **Reduce symmetry**: Use primitive cells when possible
2. **Limit neighbor searches**: Specify reasonable cutoff radii
3. **Use appropriate methods**: Different analysis tools have different speed/accuracy tradeoffs
4. **Parallelize when possible**: Many operations can be parallelized
## Units and Conventions
Pymatgen uses atomic units throughout:
- **Lengths**: Angstroms (Å)
- **Energies**: Electronvolts (eV)
- **Angles**: Degrees (°)
- **Magnetic moments**: Bohr magnetons (μB)
- **Time**: Femtoseconds (fs)
Convert units using `pymatgen.core.units` when needed.
## Integration with Other Tools
Pymatgen integrates seamlessly with:
- **ASE** (Atomic Simulation Environment)
- **Phonopy** (phonon calculations)
- **BoltzTraP** (transport properties)
- **Atomate/Fireworks** (workflow management)
- **AiiDA** (provenance tracking)
- **Zeo++** (pore analysis)
- **OpenBabel** (molecule conversion)
## Troubleshooting
**Import errors**: Install missing dependencies
```bash
pip install pymatgen[analysis,vis]
```
**API key not found**: Set MP_API_KEY environment variable
```bash
export MP_API_KEY="your_key_here"
```
**Structure read failures**: Check file format and syntax
```python
# Try explicit format specification
struct = Structure.from_file("file.txt", fmt="cif")
```
**Symmetry analysis fails**: Structure may have numerical precision issues
```python
# Increase tolerance
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
sga = SpacegroupAnalyzer(struct, symprec=0.1)
```
## Additional Resources
- **Documentation**: https://pymatgen.org/
- **Materials Project**: https://materialsproject.org/
- **GitHub**: https://github.com/materialsproject/pymatgen
- **Forum**: https://matsci.org/
- **Example notebooks**: https://matgenb.materialsvirtuallab.org/
## Version Notes
This skill is designed for pymatgen 2024.x and later. For the Materials Project API, use the `mp-api` package (separate from legacy `pymatgen.ext.matproj`).
Requirements:
- Python 3.10 or higher
- pymatgen >= 2023.x
- mp-api (for Materials Project access)