File: operation.py

package info (click to toggle)
spglib 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,180 kB
  • sloc: ansic: 125,066; python: 7,717; cpp: 2,197; f90: 2,143; ruby: 792; makefile: 22; sh: 18
file content (150 lines) | stat: -rw-r--r-- 4,312 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import annotations

from typing import List
from fractions import Fraction
from queue import Queue

import numpy as np
from numpy.typing import NDArray


# Maximal denominator of fractional coordinates of translations
DENOMINATOR = 12


class MagneticOperation:

    ndim = 3

    def __init__(self, augmented_matrix: NDArray, time_reversal: bool):
        self._augmented_matrix = augmented_matrix
        self._time_reversal = time_reversal

        self._linear = self.augmented_matrix[:self.ndim, :self.ndim]
        self._translation = self.augmented_matrix[:self.ndim, -1]

        # For comparison, store linear and translations as tuple of integers
        self._linear_tuple = ndarray_to_integer_tuple(
            self.augmented_matrix[: self.ndim, : self.ndim]
        )
        self._translation_tuple = tuple(
            Fraction(t).limit_denominator(DENOMINATOR)
            for t in self.augmented_matrix[: self.ndim, -1]
        )

    def __eq__(self, other) -> bool:
        if not isinstance(other, MagneticOperation):
            return False

        return (self._linear_tuple == other._linear_tuple) and (
            self._translation_tuple == other._translation_tuple
        ) and (
            self.time_reversal == other.time_reversal
        )

    def __hash__(self):
        return hash((self._linear_tuple, self._translation_tuple, self.time_reversal))

    def __mul__(self, rhs: MagneticOperation) -> MagneticOperation:
        if not isinstance(rhs, MagneticOperation):
            raise ValueError("undefined operation")

        return MagneticOperation(
            augmented_matrix=np.dot(self.augmented_matrix, rhs.augmented_matrix),
            time_reversal=(self.time_reversal != rhs.time_reversal),
        )

    def inverse(self) -> MagneticOperation:
        return MagneticOperation(
            augmented_matrix=np.linalg.inv(self.augmented_matrix),
            time_reversal=self.time_reversal,
        )

    @property
    def linear(self) -> NDArray:
        return self._linear

    @property
    def translation(self) -> NDArray:
        return self._translation

    @property
    def time_reversal(self) -> bool:
        return self._time_reversal

    @property
    def augmented_matrix(self) -> NDArray:
        return self._augmented_matrix

    @classmethod
    def identity(cls) -> MagneticOperation:
        return MagneticOperation.from_linear_translation_time_reversal()

    @classmethod
    def from_linear_translation_time_reversal(cls, linear=None, translation=None, time_reversal=False):
        aug = np.zeros((cls.ndim + 1, cls.ndim + 1))
        aug[-1, -1] = 1

        if linear is None:
            aug[:cls.ndim, :cls.ndim] = np.eye(cls.ndim)
        else:
            aug[:cls.ndim, :cls.ndim] = linear

        if translation is not None:
            aug[:cls.ndim, -1] = translation

        return cls(aug, time_reversal)


def ndarray_to_integer_tuple(array: NDArray):
    array_int = np.around(array).astype(int)
    array_t = tuple(map(tuple, array_int))
    return array_t


def remainder1_with_denominator(arr: NDArray, denominator: int) -> NDArray:
    """
    return arr (mod 1)
    """
    arr_int = np.around(arr * denominator).astype(int)
    arr_int_mod = np.remainder(arr_int, denominator)
    arr_mod = arr_int_mod / denominator
    return arr_mod


def remainder1_symmetry_operation(
    ops: MagneticOperation,
) -> MagneticOperation:
    new_translation = remainder1_with_denominator(ops.translation, DENOMINATOR)
    return MagneticOperation.from_linear_translation_time_reversal(
        ops.linear, new_translation, ops.time_reversal
    )


def traverse(
    generators: List[MagneticOperation],
) -> List[MagneticOperation]:
    """
    Generate all coset operations
    """
    coset = set()
    que = Queue()  # type: ignore
    identity = MagneticOperation.identity()
    que.put(identity)

    while not que.empty():
        g = que.get()
        if g in coset:
            continue
        coset.add(g)

        for h in generators:
            # Take modulus by translation subgroup
            gh = remainder1_symmetry_operation(g * h)
            que.put(gh)

    # Put identity in the first
    coset.remove(identity)
    ret = [identity] + list(coset)  # type: ignore

    return ret