Skip to content

Tutorial 12

Lee Burton edited this page Jul 16, 2025 · 9 revisions

๐Ÿงช Tutorial 12: PyMatGen

The Python Materials Genomics (PyMatGen) toolkit is one of the most powerful Python libraries in computational materials science.

Developed as a core part of the Materials Project, PyMatGen helps you analyze, manipulate, and generate crystal structures, perform symmetry analysis, and more.

๐Ÿ”— Official Website: https://pymatgen.org/
๐Ÿ“น YouTube Tutorials: Materials Virtual Lab


๐Ÿ› ๏ธ Installing PyMatGen

To install PyMatGen on your Mac or on the POWER cluster, activate your mamba environment and run:

mamba install pymatgen

โณ Note: This may take a few minutes. PyMatGen has several dependencies.


โœ… Verifying Your Installation

Let's start with a quick check to see if PyMatGen works:

from pymatgen.symmetry.groups import SpaceGroup

symmetrynumber = 10

print(SpaceGroup.from_int_number(symmetrynumber).symbol)

This should print the Hermannโ€“Mauguin symbol for space group #10.

Now, letโ€™s extend that to print all 230 space group symbols:

from pymatgen.symmetry.groups import SpaceGroup

for sg_num in range(1, 231):
    sg = SpaceGroup.from_int_number(sg_num)
    print(sg)

๐Ÿ” Comparing Crystal Structures

PyMatGen can also help you compare two structures, either fully or partially (e.g. ignoring atomic species).

Basic Structure Comparison

from pymatgen.core import Structure
from pymatgen.analysis.structure_matcher import StructureMatcher

structure1 = Structure.from_file("path/to/file/my_structure.cif")
structure2 = Structure.from_file("path/to/file/target_structure.cif")

matcher = StructureMatcher()
print(matcher.fit(structure1, structure2))  # Returns True or False

๐Ÿ› ๏ธ You can tune the comparison with parameters like:

ltol       # Length tolerance (default: 0.2)
stol       # Site tolerance (default: 0.3)
angle_tol  # Angle tolerance in degrees (default: 5)

๐Ÿงฑ Framework Comparison

You can also compare structures ignoring the atom types, to see if they share the same framework (e.g. Si and C in diamond structure):

from pymatgen.core import Structure
from pymatgen.analysis.structure_matcher import StructureMatcher, FrameworkComparator

diamond = Structure.from_file("path/to/file/diamond.cif")
silicon = Structure.from_file("path/to/file/Si.cif")

matcher = StructureMatcher(comparator=FrameworkComparator())
print(matcher.fit(diamond, silicon))  # Should return True

๐ŸŽ‰ Success!

If you've reached this point:

โœ… PyMatGen is installed and working
โœ… You can analyze symmetry and compare structures
โœ… You're ready for more advanced structure analysis!

๐Ÿ‘‰ Continue to Tutorial 13 โ†’

Clone this wiki locally