Skip to content

Commit

Permalink
Add SymmetryUndetermined(ValueError) exception type (materialsproje…
Browse files Browse the repository at this point in the history
…ct#3872)

* Add `SymmetryUndetermined(ValueError)` exception

* Add test for `SymmetryUndetermined`

* Update docstring for `get_space_group_info`

---------

Co-authored-by: Matthew Horton <[email protected]>
  • Loading branch information
mkhorton and Matthew Horton authored Jun 9, 2024
1 parent 735db5b commit 624e6eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,11 @@ def get_space_group_info(
Returns:
spacegroup_symbol, international_number
Raises:
pymatgen.symmetry.analyzer.SymmetryUndetermined if symmetry cannot
be determined. This can happen for numerical reasons, for example if
atoms are placed unphysically close together.
"""
# Avoid circular import
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
Expand Down
12 changes: 11 additions & 1 deletion pymatgen/symmetry/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@
)


class SymmetryUndetermined(ValueError):
"""
An Exception for when symmetry cannot be determined. This might happen
when, for example, atoms are very close together.
"""


@lru_cache(maxsize=32)
def _get_symmetry_dataset(cell, symprec, angle_tolerance):
"""Simple wrapper to cache results of spglib.get_symmetry_dataset since this call is
expensive.
"""
return spglib.get_symmetry_dataset(cell, symprec=symprec, angle_tolerance=angle_tolerance)
dataset = spglib.get_symmetry_dataset(cell, symprec=symprec, angle_tolerance=angle_tolerance)
if dataset is None:
raise SymmetryUndetermined
return dataset


class SpacegroupAnalyzer:
Expand Down
17 changes: 14 additions & 3 deletions tests/symmetry/test_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import numpy as np
import pytest
from numpy.testing import assert_allclose
from pytest import approx
from pytest import approx, raises

from pymatgen.core import Molecule, PeriodicSite, Site, Species, Structure
from pymatgen.core import Lattice, Molecule, PeriodicSite, Site, Species, Structure
from pymatgen.io.vasp.outputs import Vasprun
from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, cluster_sites, iterative_symmetrize
from pymatgen.symmetry.analyzer import (
PointGroupAnalyzer,
SpacegroupAnalyzer,
SymmetryUndetermined,
cluster_sites,
iterative_symmetrize,
)
from pymatgen.symmetry.structure import SymmetrizedStructure
from pymatgen.util.testing import TEST_FILES_DIR, VASP_IN_DIR, VASP_OUT_DIR, PymatgenTest

Expand Down Expand Up @@ -368,6 +374,11 @@ def test_tricky_structure(self):
assert spg_analyzer.get_crystal_system() == "tetragonal"
assert spg_analyzer.get_hall() == "-I 4 2"

def test_bad_structure(self):
struct = Structure(Lattice.cubic(5), ["H", "H"], [[0.0, 0.0, 0.0], [0.001, 0.0, 0.0]])
with raises(SymmetryUndetermined):
SpacegroupAnalyzer(struct, 0.1)


class TestSpacegroup(TestCase):
def setUp(self):
Expand Down

0 comments on commit 624e6eb

Please sign in to comment.