Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in SymmetryData #1118

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions emmet-core/emmet/core/symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from pydantic import BaseModel, Field
from pymatgen.core import Structure
from pymatgen.core.structure import Molecule
from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, spglib
from pymatgen.symmetry.analyzer import (
PointGroupAnalyzer,
SpacegroupAnalyzer,
spglib,
SymmetryUndeterminedError,
)

from emmet.core.settings import EmmetSettings
from emmet.core.utils import ValueEnum
Expand Down Expand Up @@ -139,29 +144,56 @@ class SymmetryData(BaseModel):
symprec: Optional[float] = Field(
None,
title="Symmetry Finding Precision",
description="The precision given to spglib to determine the symmetry of this lattice.",
description="The precision provided to spglib to determine the symmetry of this structure.",
)

version: Optional[str] = Field(None, title="SPGLib version")
angle_tolerance: Optional[float] = Field(
None,
title="Angle Tolerance",
description="Angle tolerance provided to spglib to determine the symmetry of this structure.",
)

version: Optional[str] = Field(None, title="spglib version")

@classmethod
def from_structure(cls, structure: Structure) -> "SymmetryData":
symprec = SETTINGS.SYMPREC
sg = SpacegroupAnalyzer(structure, symprec=symprec)
symmetry: Dict[str, Any] = {"symprec": symprec}
if not sg.get_symmetry_dataset():
sg = SpacegroupAnalyzer(structure, 1e-3, 1)
symmetry["symprec"] = 1e-3
symmetry: Dict[str, Any] = {
"source": "spglib",
"symbol": None,
"number": None,
"point_group": None,
"crystal_system": None,
"hall": None,
"version": spglib.__version__,
"symprec": SETTINGS.SYMPREC,
"angle_tolerance": SETTINGS.ANGLE_TOL,
}

try:
sg = SpacegroupAnalyzer(
structure,
symprec=symmetry["symprec"],
angle_tolerance=symmetry["angle_tolerance"],
)
except SymmetryUndeterminedError:
try:
symmetry["symprec"] = 1e-3
symmetry["angle_tolerance"] = 1
sg = SpacegroupAnalyzer(
structure,
symprec=symmetry["symprec"],
angle_tolerance=symmetry["angle_tolerance"],
)
except SymmetryUndeterminedError:
return SymmetryData(**symmetry)

symmetry.update(
{
"source": "spglib",
"symbol": sg.get_space_group_symbol(),
"number": sg.get_space_group_number(),
"point_group": sg.get_point_group_symbol(),
"crystal_system": CrystalSystem(sg.get_crystal_system().title()),
"hall": sg.get_hall(),
"version": spglib.__version__,
}
)

Expand Down
2 changes: 1 addition & 1 deletion emmet-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
include_package_data=True,
install_requires=[
"numpy<2",
"pymatgen",
"pymatgen>=2024.6.10",
"monty>=2024.2.2",
"pydantic>=2.0",
"pydantic-settings>=2.0",
Expand Down
Loading