Skip to content
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
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ dependencies = [
"typing_extensions",
"numpy>=1.20.1",
"spglib>=2.5",
"hsnf>=0.3.15", # Hermite normal form
"hsnf>=0.3.15", # Hermite normal form
"spgrep>=0.3.2",
"moyopy>=0.7.1",
]

[project.urls]
Expand All @@ -64,6 +65,7 @@ dev = [
"spinspg[docs]",
"spgrep-modulation>=0.2.5",
"toml",
"ipython",
]

[dependency-groups]
Expand Down
11 changes: 9 additions & 2 deletions src/spinspg/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

import numpy as np

from spinspg.group import get_primitive_spin_symmetry, get_symmetry_with_cell
from spinspg.group import (
SYMMETRY_FINDER_BACKEND,
get_primitive_spin_symmetry,
get_symmetry_with_cell,
)

if TYPE_CHECKING:
from spinspg.spin import SpinOnlyGroup
Expand All @@ -20,6 +24,7 @@ def get_spin_symmetry(
magmoms: NDArrayFloat,
symprec: float = 1e-5,
angle_tolerance: float = -1.0,
backend: SYMMETRY_FINDER_BACKEND = "spglib",
) -> tuple[SpinOnlyGroup, NDArrayInt, NDArrayFloat, NDArrayFloat]:
"""Return spin symmetry operations of a given spin arrangement.

Expand Down Expand Up @@ -53,7 +58,9 @@ def get_spin_symmetry(
Spin rotation parts of spin symmetry operations in Cartesian coordinates.

"""
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, angle_tolerance)
ns = get_symmetry_with_cell(
lattice, positions, numbers, symprec, angle_tolerance, backend=backend
)
ssg = get_primitive_spin_symmetry(ns, magmoms, symprec)

spin_only_group = ssg.spin_only_group
Expand Down
28 changes: 24 additions & 4 deletions src/spinspg/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Literal

import numpy as np
from hsnf import column_style_hermite_normal_form
from moyopy import Cell, MoyoDataset
from spglib import get_symmetry_dataset
from typing_extensions import assert_never

from spinspg.permutation import Permutation, get_symmetry_permutations
from spinspg.spin import SpinOnlyGroup, SpinOnlyGroupType, get_spin_only_group, solve_procrustes
Expand All @@ -17,6 +20,8 @@
ndarray2d_to_integer_tuple,
)

SYMMETRY_FINDER_BACKEND = Literal["spglib", "moyopy"]


@dataclass
class NonmagneticSymmetry:
Expand Down Expand Up @@ -55,12 +60,27 @@ def get_symmetry_with_cell(
numbers: NDArrayInt,
symprec: float,
angle_tolerance: float,
backend: SYMMETRY_FINDER_BACKEND = "spglib",
) -> NonmagneticSymmetry:
"""Find spatial symmetry operations from nonmagnetic crystal structure."""
dataset = get_symmetry_dataset((lattice, positions, numbers), symprec, angle_tolerance)
rotations = dataset.rotations
translations = dataset.translations
prim_lattice = dataset.primitive_lattice
if backend == "spglib":
dataset = get_symmetry_dataset((lattice, positions, numbers), symprec, angle_tolerance)
rotations = dataset.rotations
translations = dataset.translations
prim_lattice = dataset.primitive_lattice
elif backend == "moyopy":
cell = Cell(basis=lattice.tolist(), positions=positions.tolist(), numbers=numbers.tolist())
dataset = MoyoDataset(
cell,
symprec=symprec,
angle_tolerance=float(np.deg2rad(angle_tolerance)) if angle_tolerance > 0 else None,
rotate_basis=False,
)
rotations = np.array(dataset.operations.rotations, dtype=int)
translations = np.array(dataset.operations.translations)
prim_lattice = np.array(dataset.prim_std_cell.basis)
else:
assert_never(backend)

# Unique by rotation parts
uniq_rotations = []
Expand Down
43 changes: 29 additions & 14 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,40 @@
from spglib import get_magnetic_symmetry

from spinspg.core import get_spin_symmetry
from spinspg.group import get_primitive_spin_symmetry, get_symmetry_with_cell, purify_spin_rotation
from spinspg.group import (
SYMMETRY_FINDER_BACKEND,
get_primitive_spin_symmetry,
get_symmetry_with_cell,
purify_spin_rotation,
)
from spinspg.spin import SpinOnlyGroup, SpinOnlyGroupType
from spinspg.utils import NDArrayFloat


def test_get_symmetry_with_cell(fcc):
@pytest.mark.parametrize("backend", ["spglib", "moyopy"])
def test_get_symmetry_with_cell(fcc, backend: SYMMETRY_FINDER_BACKEND):
lattice, positions, numbers, _ = fcc
symmetry = get_symmetry_with_cell(lattice, positions, numbers, 1e-5, -1)
symmetry = get_symmetry_with_cell(lattice, positions, numbers, 1e-5, -1, backend=backend)
assert symmetry.prim_rotations.shape == (48, 3, 3)
assert symmetry.prim_centerings.shape == (4, 3)


def test_spin_space_group_fcc(fcc):
@pytest.mark.parametrize("backend", ["spglib", "moyopy"])
def test_spin_space_group_fcc(fcc, backend: SYMMETRY_FINDER_BACKEND):
lattice, positions, numbers, magmoms = fcc
symprec = 1e-5
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1)
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1, backend=backend)
ssg = get_primitive_spin_symmetry(ns, magmoms, symprec)

assert ssg.spin_only_group.spin_only_group_type == SpinOnlyGroupType.COPLANAR
assert len(ssg.spin_translation_coset) == 2


def test_spin_space_group_kagome(layer_triangular_kagome):
@pytest.mark.parametrize("backend", ["spglib", "moyopy"])
def test_spin_space_group_kagome(layer_triangular_kagome, backend: SYMMETRY_FINDER_BACKEND):
lattice, positions, numbers, magmoms = layer_triangular_kagome
symprec = 1e-5
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1)
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1, backend=backend)
ssg = get_primitive_spin_symmetry(ns, magmoms, symprec)

assert ssg.spin_only_group.spin_only_group_type == SpinOnlyGroupType.COPLANAR
Expand All @@ -42,10 +50,11 @@ def test_spin_space_group_kagome(layer_triangular_kagome):
assert len(ssg.nontrivial_coset) == 24 # 6/mmm


def test_spin_space_group_rutile(rutile):
@pytest.mark.parametrize("backend", ["spglib", "moyopy"])
def test_spin_space_group_rutile(rutile, backend: SYMMETRY_FINDER_BACKEND):
lattice, positions, numbers, magmoms = rutile
symprec = 1e-5
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1)
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1, backend=backend)
ssg = get_primitive_spin_symmetry(ns, magmoms, symprec)

assert ssg.spin_only_group.spin_only_group_type == SpinOnlyGroupType.COLLINEAR
Expand All @@ -72,10 +81,13 @@ def test_spin_space_group_rutile(rutile):
("Ni_in_NiTa2O6", SpinOnlyGroupType.COLLINEAR, [1, -1, 0]),
],
)
def test_spin_space_groups(request, testcase, spin_only_group_type, axis):
@pytest.mark.parametrize("backend", ["spglib", "moyopy"])
def test_spin_space_groups(
request, testcase, spin_only_group_type, axis, backend: SYMMETRY_FINDER_BACKEND
):
lattice, positions, numbers, magmoms = request.getfixturevalue(testcase)
symprec = 1e-5
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1)
ns = get_symmetry_with_cell(lattice, positions, numbers, symprec, -1, backend=backend)
ssg = get_primitive_spin_symmetry(ns, magmoms, symprec)

assert ssg.spin_only_group.spin_only_group_type == spin_only_group_type
Expand All @@ -90,10 +102,11 @@ def test_spin_space_groups(request, testcase, spin_only_group_type, axis):
assert num_sym >= len(mag_symmetry["rotations"])


def test_get_spin_symmetry(rutile):
@pytest.mark.parametrize("backend", ["spglib", "moyopy"])
def test_get_spin_symmetry(rutile, backend: SYMMETRY_FINDER_BACKEND):
lattice, positions, numbers, magmoms = rutile
sog, rotations, translations, spin_rotations = get_spin_symmetry(
lattice, positions, numbers, magmoms
lattice, positions, numbers, magmoms, backend=backend
)

assert sog.spin_only_group_type == SpinOnlyGroupType.COLLINEAR
Expand Down Expand Up @@ -205,7 +218,9 @@ def test_get_spin_symmetry(rutile):
for rot, trans, srot in zip(rotations, translations, spin_rotations):
for i in range(len(expects)):
if (not found[i]) and np.allclose(rot, expects[i][0]):
assert np.allclose(trans, expects[i][1])
diff = trans - expects[i][1]
diff -= np.rint(diff)
assert np.allclose(diff, 0)
assert np.allclose(srot, expects[i][2])
found[i] = True

Expand Down
Loading