Loading...
Loading...
Materials science toolkit. Crystal structures (CIF, POSCAR), phase diagrams, band structure, DOS, Materials Project integration, format conversion, for computational materials science.
npx skill4agent add davila7/claude-code-templates pymatgen# Core pymatgen
uv pip install pymatgen
# With Materials Project API access
uv pip install pymatgen mp-api
# Optional dependencies for extended functionality
uv pip install pymatgen[analysis] # Additional analysis tools
uv pip install pymatgen[vis] # Visualization toolsfrom 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³")# 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)
)# Automatic format detection
struct = Structure.from_file("structure.cif")
struct = Structure.from_file("POSCAR")
mol = Molecule.from_file("molecule.xyz")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]]
)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)references/core_classes.md# 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")# Single file conversion
python scripts/structure_converter.py POSCAR structure.cif
# Batch conversion
python scripts/structure_converter.py *.cif --output-dir ./poscar_files --format poscarreferences/io_formats.mdfrom 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()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} Å")# Comprehensive analysis
python scripts/structure_analyzer.py POSCAR --symmetry --neighbors
# Export results
python scripts/structure_analyzer.py structure.cif --symmetry --export jsonreferences/analysis_modules.mdfrom 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()# 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" --showreferences/analysis_modules.mdreferences/transformations_workflows.mdfrom 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")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()references/analysis_modules.mdreferences/io_formats.mdfrom 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")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()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])references/analysis_modules.mdreferences/transformations_workflows.mdexport MP_API_KEY="your_key_here"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")references/materials_project_api.mdfrom 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")# 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")references/io_formats.mdreferences/transformations_workflows.mdfrom 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()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")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']references/analysis_modules.mdscripts/structure_converter.pypython scripts/structure_converter.py POSCAR structure.cifstructure_analyzer.pypython scripts/structure_analyzer.py structure.cif --symmetry --neighborsphase_diagram_generator.pypython scripts/phase_diagram_generator.py Li-Fe-O --analyze "LiFeO2"python scripts/script_name.py --helpreferences/core_classes.mdio_formats.mdanalysis_modules.mdmaterials_project_api.mdtransformations_workflows.mdfrom 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}")# 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()# 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²references/transformations_workflows.mdStructure.from_file()IStructureSpacegroupAnalyzerfrom_file()to()as_dict()from_dict()with MPRester() as mpr:MPRelaxSetMPStaticSetTransformedStructurepymatgen.core.unitsuv pip install pymatgen[analysis,vis]export MP_API_KEY="your_key_here"# Try explicit format specification
struct = Structure.from_file("file.txt", fmt="cif")# Increase tolerance
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
sga = SpacegroupAnalyzer(struct, symprec=0.1)mp-apipymatgen.ext.matproj