Spglib
Spglib is a C library for finding and handling crystal symmetries, exposed as a Python module. It is widely used in materials science and solid-state physics for tasks such as space group determination, primitive cell reduction, and irreducible k-point generation. The current stable version is 2.7.0, with regular updates and maintenance.
Warnings
- gotcha The `symprec` parameter (symmetry precision) is crucial for accurate results. It defines the maximum displacement or distance for atoms to be considered symmetrically equivalent. An incorrect `symprec` (too large or too small) can lead to incorrect symmetry detection, especially for structures with small distortions or specific computational precisions.
- gotcha Input cell format requires strict adherence: `(lattice, positions, numbers)`. `lattice` must be a 3x3 NumPy array where rows are the lattice vectors. `positions` must be an Nx3 NumPy array of *fractional* coordinates. `numbers` must be a list or array of *integer* atom types (not elemental symbols).
- gotcha The behavior of `spglib.get_primitive` and `spglib.find_primitive` regarding the primitive cell origin changed significantly with v2.0.0 (and C library v1.16). From v2.0.0, these functions accept an optional `cell_origin` parameter. If not provided, a default origin is used, which might differ from the implicit behavior of older versions, potentially affecting consistency for users relying on a specific primitive cell definition.
Install
-
pip install spglib
Imports
- spglib
import spglib
- get_spacegroup
spglib.get_spacegroup(cell, symprec=1e-5)
- get_symmetry_dataset
spglib.get_symmetry_dataset(cell, symprec=1e-5)
Quickstart
import spglib
import numpy as np
# Define a simple cubic cell
# Lattice vectors as rows of a 3x3 matrix
lattice = np.array([[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]])
# Fractional coordinates of atoms
positions = np.array([[0.0, 0.0, 0.0]])
# Integer atom types (e.g., atomic number, or arbitrary unique integers)
numbers = [1]
# Combine into a cell tuple: (lattice, positions, numbers)
cell = (lattice, positions, numbers)
# Determine the space group using a symmetry precision
spacegroup_data = spglib.get_spacegroup(cell, symprec=1e-5)
print(f"Spacegroup: {spacegroup_data}")
# Get a full dataset of symmetry information
dataset = spglib.get_symmetry_dataset(cell, symprec=1e-5)
print(f"Spacegroup number: {dataset['number']}")
print(f"International short symbol: {dataset['international_short']}")