Pymatgen Core Classes Reference
This reference documents the fundamental classes in `pymatgen.core` that form the foundation for materials analysis.
Overview
Pymatgen Core Classes Reference
This reference documents the fundamental classes in pymatgen.core that form the foundation for materials analysis.
Architecture Principles
Pymatgen follows an object-oriented design where elements, sites, and structures are represented as objects. The framework emphasizes periodic boundary conditions for crystal representation while maintaining flexibility for molecular systems.
Unit Conventions: All units in pymatgen are typically assumed to be in atomic units:
- Lengths: angstroms (Å)
- Energies: electronvolts (eV)
- Angles: degrees
Element and Periodic Table
Element
Represents periodic table elements with comprehensive properties.
Creation methods:
from pymatgen.core import Element
# Create from symbol
si = Element("Si")
# Create from atomic number
si = Element.from_Z(14)
# Create from name
si = Element.from_name("silicon")
Key properties:
atomic_mass: Atomic mass in amuatomic_radius: Atomic radius in angstromselectronegativity: Pauling electronegativityionization_energy: First ionization energy in eVcommon_oxidation_states: List of common oxidation statesis_metal,is_halogen,is_noble_gas, etc.: Boolean propertiesX: Element symbol as string
Species
Extends Element for charged ions and specific oxidation states.
from pymatgen.core import Species
# Create an Fe2+ ion
fe2 = Species("Fe", 2)
# Or with explicit sign
fe2 = Species("Fe", +2)
DummySpecies
Placeholder atoms for special structural representations (e.g., vacancies).
from pymatgen.core import DummySpecies
vacancy = DummySpecies("X")
Composition
Represents chemical formulas and compositions, enabling chemical analysis and manipulation.
Creation
from pymatgen.core import Composition
# From string formula
comp = Composition("Fe2O3")
# From dictionary
comp = Composition({"Fe": 2, "O": 3})
# From weight dictionary
comp = Composition.from_weight_dict({"Fe": 111.69, "O": 48.00})
Key methods
get_reduced_formula_and_factor(): Returns reduced formula and multiplication factoroxi_state_guesses(): Attempts to determine oxidation statesreplace(replacements_dict): Replace elementsadd_charges_from_oxi_state_guesses(): Infer and add oxidation statesis_element: Check if composition is a single element
Key properties
weight: Molecular weightreduced_formula: Reduced chemical formulahill_formula: Formula in Hill notation (C, H, then alphabetical)num_atoms: Total number of atomschemical_system: Alphabetically sorted elements (e.g., "Fe-O")element_composition: Dictionary of element to amount
Lattice
Defines unit cell geometry for crystal structures.
Creation
from pymatgen.core import Lattice
# From lattice parameters
lattice = Lattice.from_parameters(a=3.84, b=3.84, c=3.84,
alpha=120, beta=90, gamma=60)
# From matrix (row vectors are lattice vectors)
lattice = Lattice([[3.84, 0, 0],
[0, 3.84, 0],
[0, 0, 3.84]])
# Cubic lattice
lattice = Lattice.cubic(3.84)
# Hexagonal lattice
lattice = Lattice.hexagonal(a=2.95, c=4.68)
Key methods
get_niggli_reduced_lattice(): Returns Niggli-reduced latticeget_distance_and_image(frac_coords1, frac_coords2): Distance between fractional coordinates with periodic boundary conditionsget_all_distances(frac_coords1, frac_coords2): Distances including periodic images
Key properties
volume: Volume of the unit cell (ų)abc: Lattice parameters (a, b, c) as tupleangles: Lattice angles (alpha, beta, gamma) as tuplematrix: 3x3 matrix of lattice vectorsreciprocal_lattice: Reciprocal lattice objectis_orthogonal: Whether lattice vectors are orthogonal
Sites
Site
Represents an atomic position in non-periodic systems.
from pymatgen.core import Site
site = Site("Si", [0.0, 0.0, 0.0]) # Species and Cartesian coordinates
PeriodicSite
Represents an atomic position in a periodic lattice with fractional coordinates.
from pymatgen.core import PeriodicSite
site = PeriodicSite("Si", [0.5, 0.5, 0.5], lattice) # Species, fractional coords, lattice
Key methods:
distance(other_site): Distance to another siteis_periodic_image(other_site): Check if sites are periodic images
Key properties:
species: Species or element at the sitecoords: Cartesian coordinatesfrac_coords: Fractional coordinates (for PeriodicSite)x,y,z: Individual Cartesian coordinates
Structure
Represents a crystal structure as a collection of periodic sites. Structure is mutable, while IStructure is immutable.
Creation
from pymatgen.core import Structure, Lattice
# From scratch
coords = [[0, 0, 0], [0.75, 0.5, 0.75]]
lattice = Lattice.from_parameters(a=3.84, b=3.84, c=3.84,
alpha=120, beta=90, gamma=60)
struct = Structure(lattice, ["Si", "Si"], coords)
# From file (automatic format detection)
struct = Structure.from_file("POSCAR")
struct = Structure.from_file("structure.cif")
# From spacegroup
struct = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3.5),
["Si"], [[0, 0, 0]])
File I/O
# Write to file (format inferred from extension)
struct.to(filename="output.cif")
struct.to(filename="POSCAR")
struct.to(filename="structure.xyz")
# Get string representation
cif_string = struct.to(fmt="cif")
poscar_string = struct.to(fmt="poscar")
Key methods
Structure modification:
append(species, coords): Add a siteinsert(i, species, coords): Insert site at indexremove_sites(indices): Remove sites by indexreplace(i, species): Replace species at indexapply_strain(strain): Apply strain to structureperturb(distance): Randomly perturb atomic positionsmake_supercell(scaling_matrix): Create supercellget_primitive_structure(): Get primitive cell
Analysis:
get_distance(i, j): Distance between sites i and jget_neighbors(site, r): Get neighbors within radius rget_all_neighbors(r): Get all neighbors for all sitesget_space_group_info(): Get space group informationmatches(other_struct): Check if structures match
Interpolation:
interpolate(end_structure, nimages): Interpolate between structures
Key properties
lattice: Lattice objectspecies: List of species at each sitesites: List of PeriodicSite objectsnum_sites: Number of sitesvolume: Volume of the structuredensity: Density in g/cm³composition: Composition objectformula: Chemical formuladistance_matrix: Matrix of pairwise distances
Molecule
Represents non-periodic collections of atoms. Molecule is mutable, while IMolecule is immutable.
Creation
from pymatgen.core import Molecule
# From scratch
coords = [[0.00, 0.00, 0.00],
[0.00, 0.00, 1.08]]
mol = Molecule(["C", "O"], coords)
# From file
mol = Molecule.from_file("molecule.xyz")
mol = Molecule.from_file("molecule.mol")
Key methods
get_covalent_bonds(): Returns bonds based on covalent radiiget_neighbors(site, r): Get neighbors within radiusget_zmatrix(): Get Z-matrix representationget_distance(i, j): Distance between sitesget_centered_molecule(): Center molecule at origin
Key properties
species: List of speciessites: List of Site objectsnum_sites: Number of atomscharge: Total charge of moleculespin_multiplicity: Spin multiplicitycenter_of_mass: Center of mass coordinates
Serialization
All core objects implement as_dict() and from_dict() methods for robust JSON/YAML persistence.
# Serialize to dictionary
struct_dict = struct.as_dict()
# Write to JSON
with open("structure.json", "w") as f:
json.dump(struct_dict, f)
# Read from JSON
with open("structure.json", "r") as f:
struct_dict = json.load(f)
struct = Structure.from_dict(struct_dict)
This approach addresses limitations of Python pickling and maintains compatibility across pymatgen versions.
Additional Core Classes
CovalentBond
Represents bonds in molecules.
Key properties:
length: Bond lengthget_bond_order(): Returns bond order (single, double, triple)
Ion
Represents charged ionic species with oxidation states.
from pymatgen.core import Ion
# Create Fe2+ ion
fe2_ion = Ion.from_formula("Fe2+")
Interface
Represents substrate-film combinations for heterojunction analysis.
GrainBoundary
Represents crystallographic grain boundaries.
Spectrum
Represents spectroscopic data with methods for normalization and processing.
Key methods:
normalize(mode="max"): Normalize spectrumsmear(sigma): Apply Gaussian smearing
Best Practices
- Immutability: Use immutable versions (
IStructure,IMolecule) when structures shouldn't be modified - Serialization: Prefer
as_dict()/from_dict()over pickle for long-term storage - Units: Always work in atomic units (Å, eV) - conversions are available in
pymatgen.core.units - File I/O: Use
from_file()for automatic format detection - Coordinates: Pay attention to whether methods expect Cartesian or fractional coordinates