from __future__ import annotations
from collections import defaultdict
from collections.abc import Iterable, Sequence
from functools import cached_property
from typing import Any
import numpy as np
from ase import Atoms
from ase.units import Bohr
from gpaw import debug
from gpaw.core.domain import normalize_cell
from gpaw.core.atom_arrays import AtomArrays
from gpaw.new import zips
from gpaw.rotation import rotation
from gpaw.symmetry import Symmetry as OldSymmetry
from gpaw.symmetry import frac
from gpaw.typing import Array2D, Array3D, ArrayLike1D, ArrayLike2D, ArrayLike3D
from gpaw.utilities.symmetry import (check_one_symmetry,
find_set_of_lattice_symmetries,
prune_symmetries)
class SymmetryBrokenError(Exception):
"""Broken-symmetry error."""
class SymmetryAnalysisBug(Exception):
"""Symmetries do not form a proper group."""
def create_symmetries_object(atoms: Atoms,
*,
setup_ids: Sequence | None = None,
magmoms: ArrayLike2D | None = None,
rotation_scc: ArrayLike3D | None = None,
translation_sc: ArrayLike2D | None = None,
atommap_sa: ArrayLike2D | None = None,
extra_ids: Sequence[int] | None = None,
tolerance: float | None = None, # Å
point_group: bool = True,
symmorphic: bool = True,
guarantee_group: bool = True,
_backwards_compatible: bool = False
) -> Symmetries:
"""Find symmetries from atoms object.
>>> atoms = Atoms('H', cell=[1, 1, 1], pbc=True)
>>> sym = create_symmetries_object(atoms)
>>> len(sym)
48
>>> sym.rotation_scc.shape
(48, 3, 3)
"""
cell_cv = atoms.cell.complete()
pbc_c = atoms.pbc
if tolerance is None:
tolerance = 1e-7 if _backwards_compatible else 1e-5
if _backwards_compatible:
cell_cv *= 1 / Bohr
# Create int atom-ids from setups, magmoms and user-supplied
# (extra_ids) ids:
if setup_ids is None:
id_a = atoms.numbers
else:
id_a = integer_ids(setup_ids)
if magmoms is not None:
id_a = integer_ids((id, m) for id, m in zips(id_a, safe_id(magmoms)))
if extra_ids is not None:
id_a = integer_ids((id, x) for id, x in zips(id_a, extra_ids))
relpos_ac = atoms.get_scaled_positions()
if rotation_scc is None:
# Find symmetries from cell, ids and positions:
if point_group:
rotation_scc = find_set_of_lattice_symmetries(
cell_cv, pbc_c, tolerance,
guarantee_group, _backwards_compatible)
rotation_scc, translation_sc, atommap_sa = prune_symmetries(
rotation_scc, cell_cv, relpos_ac, id_a, tolerance,
symmorphic=symmorphic, guarantee_group=guarantee_group,
_backwards_compatible=_backwards_compatible)
sym = Symmetries(
cell=cell_cv,
rotations=rotation_scc,
translations=translation_sc,
atommaps=atommap_sa,
tolerance=tolerance,
_backwards_compatible=_backwards_compatible)
else:
# No symmetries (identity only):
sym = Symmetries(cell=cell_cv,
tolerance=tolerance,
_backwards_compatible=_backwards_compatible)
sym = sym.analyze_positions(
relpos_ac, id_a, symmorphic=symmorphic)
else:
sym = Symmetries(cell=cell_cv,
rotations=rotation_scc,
translations=translation_sc,
atommaps=atommap_sa,
tolerance=tolerance,
_backwards_compatible=_backwards_compatible)
if atommap_sa is None:
sym = sym.with_atom_maps(relpos_ac, id_a=id_a)
if debug:
sym.check_positions(relpos_ac)
# Legacy:
sym._old_symmetry = OldSymmetry(
id_a, cell_cv, pbc_c, tolerance,
point_group,
time_reversal='?',
symmorphic=symmorphic)
sym._old_symmetry.op_scc = sym.rotation_scc
sym._old_symmetry.ft_sc = sym.translation_sc
sym._old_symmetry.a_sa = sym.atommap_sa
sym._old_symmetry.has_inversion = sym.has_inversion
sym._old_symmetry.gcd_c = sym.gcd_c
return sym
[docs]
class Symmetries:
def __init__(self,
*,
cell: ArrayLike1D | ArrayLike2D,
rotations: ArrayLike3D | None = None,
translations: ArrayLike2D | None = None,
atommaps: ArrayLike2D | None = None,
tolerance: float | None = None,
_backwards_compatible: bool = False):
"""Symmetries object.
"Rotations" here means rotations, mirror and inversion operations.
Units of "cell" and "tolerance" should match.
>>> sym = Symmetries.from_cell([1, 2, 3])
>>> sym.has_inversion
True
>>> len(sym)
8
>>> sym2 = sym.analyze_positions([[0, 0, 0], [0, 0, 0.4]], ids=[1, 2])
>>> sym2.has_inversion
False
>>> len(sym2)
4
"""
self.cell_cv = normalize_cell(cell)
if tolerance is None:
tolerance = 1e-7 if _backwards_compatible else 1e-5
self.tolerance = tolerance
self._backwards_compatible = _backwards_compatible
if rotations is None:
rotations = [[[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
self.rotation_scc = np.array(rotations, dtype=int)
assert (self.rotation_scc == rotations).all()
if translations is None:
self.translation_sc = np.zeros((len(self.rotation_scc), 3))
else:
self.translation_sc = np.array(translations)
if atommaps is None:
self.atommap_sa = np.empty((len(self.rotation_scc), 0), int)
else:
self.atommap_sa = np.array(atommaps)
assert self.atommap_sa.dtype == int
# Legacy stuff:
self.op_scc = self.rotation_scc # old name
self._old_symmetry: OldSymmetry
self.group_check()
@cached_property
def symmorphic(self):
return not self.translation_sc.any()
@cached_property
def has_inversion(self):
inv_cc = -np.eye(3, dtype=int)
for r_cc, t_c in zips(self.rotation_scc, self.translation_sc):
if (r_cc == inv_cc).all() and not t_c.any():
return True
return False
[docs]
@classmethod
def from_cell(cls,
cell: ArrayLike1D | ArrayLike2D,
*,
pbc: ArrayLike1D = (True, True, True),
tolerance: float | None = None,
guarantee_group: bool = True,
_backwards_compatible: bool = False) -> Symmetries:
cell_cv = normalize_cell(cell)
pbc_c = np.full(3, pbc, dtype=bool)
if tolerance is None:
tolerance = 1e-7 if _backwards_compatible else 1e-5
rotation_scc = find_set_of_lattice_symmetries(
cell_cv, pbc_c, tolerance, guarantee_group, _backwards_compatible)
return cls(cell=cell_cv,
rotations=rotation_scc,
tolerance=tolerance,
_backwards_compatible=_backwards_compatible)
[docs]
def analyze_positions(self,
relative_positions: ArrayLike2D,
ids: Sequence[int],
*,
guarantee_group: bool = True,
symmorphic: bool = True) -> Symmetries:
relative_positions = np.asarray(relative_positions)
rotation_scc, translation_sc, atommap_sa = prune_symmetries(
self.rotation_scc, self.cell_cv,
relative_positions, ids, self.tolerance,
symmorphic=symmorphic, guarantee_group=guarantee_group,
_backwards_compatible=self._backwards_compatible)
return Symmetries(cell=self.cell_cv,
rotations=rotation_scc,
translations=translation_sc,
atommaps=atommap_sa,
tolerance=self.tolerance,
_backwards_compatible=self._backwards_compatible)
[docs]
def with_atom_maps(self,
relpos_ac: Array2D,
id_a: Sequence[int]) -> Symmetries:
atommap_sa = np.empty((len(self), len(relpos_ac)), int)
a_ib = defaultdict(list)
for a, id in enumerate(id_a):
a_ib[id].append(a)
for U_cc, t_c, map_a in zips(self.rotation_scc,
self.translation_sc,
atommap_sa):
map_a[:] = check_one_symmetry(
U_cc, t_c, self.cell_cv, relpos_ac, a_ib,
self.tolerance, self._backwards_compatible)
return Symmetries(cell=self.cell_cv,
rotations=self.rotation_scc,
translations=self.translation_sc,
atommaps=atommap_sa,
tolerance=self.tolerance,
_backwards_compatible=self._backwards_compatible)
def __len__(self):
return len(self.rotation_scc)
[docs]
def summary(self, log, verbose=True):
log(f'Number of symmetries: {len(self)}')
header = ['', 'kind', 'matrix']
allign = '>^^'
nt = self.translation_sc.any(1).sum()
if nt > 0:
log(f'Number of symmetries with translation: {nt}')
header.append('translation')
allign += '>'
if not verbose:
return
rows = []
for s, (rot_cc, t_c) in enumerate(zip(self.rotation_scc,
self.translation_sc)):
kind = symmetry_symbol(rot_cc)
row = [str(s), kind, mat(rot_cc)]
if nt > 0:
a, b, c = t_c
row.append(f'({a:6.3f}, {b:6.3f}, {c:6.3f})')
rows.append(row)
log.table('Symmetry operations # r_c * U_cc + t_c -> r_c',
header=header,
rows=rows,
allign=allign)
[docs]
def check_positions(self, fracpos_ac):
for U_cc, t_c, b_a in zip(self.rotation_scc,
self.translation_sc,
self.atommap_sa):
error_ac = fracpos_ac @ U_cc + t_c - fracpos_ac[b_a]
error_ac -= error_ac.round()
if self._backwards_compatible:
if abs(error_ac).max(initial=0.0) > self.tolerance:
raise SymmetryBrokenError
else:
error_av = error_ac @ self.cell_cv
if (error_av**2).sum(1).max(initial=0.0) > self.tolerance**2:
raise SymmetryBrokenError
[docs]
def symmetrize_forces(self, F0_av):
"""Symmetrize forces."""
F_av = np.zeros_like(F0_av)
for map_a, op_cc in zip(self.atommap_sa, self.rotation_scc):
op_vv = np.linalg.inv(self.cell_cv) @ op_cc @ self.cell_cv
for a1, a2 in enumerate(map_a):
F_av[a2] += np.dot(F0_av[a1], op_vv)
return F_av / len(self)
[docs]
def lcm(self) -> list[int]:
"""Find lowest common multiple compatible with translations."""
return [np.lcm.reduce([frac(t)[1] for t in t_s])
for t_s in self.translation_sc.T]
@cached_property
def gcd_c(self):
# Needed for old gpaw.utilities.gpts.get_number_of_grid_points()
# function ...
return np.array(self.lcm())
[docs]
def check_grid(self, N_c) -> bool:
"""Check that symmetries are commensurate with grid."""
for U_cc, t_c in zip(self.rotation_scc, self.translation_sc):
t_c = t_c * N_c
# Make sure all grid-points map onto another grid-point:
if (((N_c * U_cc).T % N_c).any() or
not np.allclose(t_c, t_c.round())):
return False
return True
[docs]
def group_check(self) -> None:
"""Sanity check."""
for U1_cc, t1_c in zip(self.rotation_scc, self.translation_sc):
for U2_cc, t2_c in zip(self.rotation_scc, self.translation_sc):
U_cc = U1_cc @ U2_cc
t_c = t1_c @ U2_cc + t2_c
for U3_cc, t3_c in zip(self.rotation_scc, self.translation_sc):
dt_c = t_c - t3_c
if abs(dt_c - dt_c.round()).max() > 1e-10:
continue
if (U_cc != U3_cc).any():
continue
break
else: # no break
raise SymmetryAnalysisBug(
'Sorry! Try using spglib.standardize_cell(...)')
def symmetry_symbol(M_cc: np.ndarray) -> str:
"""Convert symmetry operation to string.
>>> symmetry_symbol(np.eye(3))
'E'
>>> symmetry_symbol(-np.eye(3))
'i'
>>> symmetry_symbol([[0, 1, 0], [-1, 0, 0], [0, 0, 1]])
'C4'
>>> symmetry_symbol([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
'σ'
>>> symmetry_symbol([[0, 1, 0], [-1, -1, 0], [0, 0, -1]])
'S3'
"""
d = np.linalg.det(M_cc)
t = np.trace(M_cc)
if d == 1:
if t == 3:
return 'E'
n = int(round(2 * np.pi / np.arccos((t - 1) / 2)))
return f'C{n}'
if t == -3:
return 'i'
if t == 1:
return 'σ'
n = int(round(2 * np.pi / np.arccos((t + 1) / 2)))
return f'S{n}'
class SymmetrizationPlan:
def __init__(self,
symmetries: Symmetries,
l_aj):
self.symmetries = symmetries
self.l_aj = l_aj
self.rotation_svv = np.einsum('vc, scd, dw -> svw',
np.linalg.inv(symmetries.cell_cv),
symmetries.rotation_scc,
symmetries.cell_cv)
lmax = max((max(l_j) for l_j in l_aj), default=-1)
self.rotation_lsmm = [
np.array([rotation(l, r_vv) for r_vv in self.rotation_svv])
for l in range(lmax + 1)]
self._rotations: dict[tuple[int, ...], Array3D] = {}
def rotations(self, l_j, xp=np):
ells = tuple(l_j)
rotation_sii = self._rotations.get(ells)
if rotation_sii is None:
ni = sum(2 * l + 1 for l in l_j)
rotation_sii = np.zeros((len(self.symmetries), ni, ni))
i1 = 0
for l in l_j:
i2 = i1 + 2 * l + 1
rotation_sii[:, i1:i2, i1:i2] = self.rotation_lsmm[l]
i1 = i2
rotation_sii = xp.asarray(rotation_sii)
self._rotations[ells] = rotation_sii
return rotation_sii
def apply_distributed(self, D_asii, dist_D_asii):
for a1, D_sii in dist_D_asii.items():
D_sii[:] = 0.0
rotation_sii = self.rotations(self.l_aj[a1])
for a2, rotation_ii in zips(self.symmetries.atommap_sa[:, a1],
rotation_sii):
D_sii += np.einsum('ij, sjk, lk -> sil',
rotation_ii, D_asii[a2], rotation_ii)
dist_D_asii.data *= 1.0 / len(self.symmetries)
def rotate_projections(self,
P_ani: AtomArrays,
*,
symnum: int,
time_reversal: bool,
ignore_phase=False,
relpos_ac=None,
kpt_c=None):
if kpt_c is None or not kpt_c.any():
ignore_phase = True
if not ignore_phase:
1 / 0 # not verified yet!
dpos_ac = (relpos_ac @ self.symmetries.rotation_scc[symnum] +
self.symmetries.translation_sc[symnum] -
relpos_ac[self.symmetries.atommap_sa[symnum]])
assert np.abs(dpos_ac - np.round(dpos_ac)).max() < 1e-9
x_a = np.exp(2j * np.pi * kpt_c @ dpos_ac.T)
P2_ani = P_ani.new()
for a, P_ni in P_ani.items():
R_ii = self.rotations(self.l_aj[a])[symnum]
b = self.symmetries.atommap_sa[symnum, a]
P2_ani[b][:] = P_ni @ R_ii.T
if not ignore_phase:
P2_ani[b] *= x_a[a] # or b?
if time_reversal:
np.negative(P2_ani.data.imag, P2_ani.data.imag)
return P2_ani
class GPUSymmetrizationPlan(SymmetrizationPlan):
def __init__(self,
symmetries: Symmetries,
l_aj,
layout):
super().__init__(symmetries, l_aj)
xp = layout.xp
a_sa = symmetries.atommap_sa
ns = a_sa.shape[0] # Number of symmetries
na = a_sa.shape[1] # Number of atoms
if xp is np:
import scipy
sparse = scipy.sparse
else:
from gpaw.gpu import cupyx
sparse = cupyx.scipy.sparse
# Find orbits, i.e. point group action,
# which also equals to set of all cosets.
# In practical terms, these are just atoms which map
# to each other via symmetry operations.
# Mathematically {{as: s∈ S}: a∈ A}, where a is an atom.
cosets = {frozenset(a_sa[:, a]) for a in range(na)}
S_aZZ = {}
work = []
for coset in map(list, cosets):
nA = len(coset) # Number of atoms in this orbit
a = coset[0] # Representative atom for coset
# The atomic density matrices transform as
# ρ'_ii = R_sii ρ_ii R^T_sii
# Which equals to vec(ρ'_ii) = (R^s_ii ⊗ R^s_ii) vec(ρ_ii)
# Here we to the Kronecker product for each of the
# symmetry transformations.
R_sii = xp.asarray(self.rotations(l_aj[a], xp))
i2 = R_sii.shape[1]**2
R_sPP = xp.einsum('sab, scd -> sacbd', R_sii, R_sii)
R_sPP = R_sPP.reshape((ns, i2, i2)) / ns
S_ZZ = xp.zeros((nA * i2,) * 2)
# For each orbit, the symetrization operation is represented by
# a full matrix operating on a subset of indices to the full array.
for loca1, a1 in enumerate(coset):
Z1 = loca1 * i2
Z2 = Z1 + i2
for s, a2 in enumerate(a_sa[:, a1]):
loca2 = coset.index(a2)
Z3 = loca2 * i2
Z4 = Z3 + i2
S_ZZ[Z1:Z2, Z3:Z4] += R_sPP[s]
# Utilize sparse matrices if sizes get out of hand
# Limit is hard coded to 100MB per orbit
if S_ZZ.nbytes > 100 * 1024**2:
S_ZZ = sparse.csr_matrix(S_ZZ)
S_aZZ[a] = S_ZZ
indices = []
for loca1, a1 in enumerate(coset):
a1_, start, end = layout.myindices[a1]
# When parallelization is done, this needs to be rewritten
assert a1_ == a1
for X in range(i2):
indices.append(start + X)
work.append((a, xp.array(indices)))
self.work = work
self.S_aZZ = S_aZZ
self.xp = xp
def apply(self, source, target):
total = 0
for a, ind in self.work:
for spin in range(len(source)):
total += len(ind)
target[spin, ind] = self.S_aZZ[a] @ source[spin, ind]
assert total / len(source) == source.shape[1]
def mat(rot_cc) -> str:
"""Convert 3x3 matrix to str.
>>> mat([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
'-1, 0, 0, 0, 1, 0, 0, 0, 1'
"""
return ', '.join(','.join(f'{r:2}'
for r in rot_c)
for rot_c in rot_cc)
def integer_ids(ids: Iterable) -> list[int]:
"""Convert arbitrary ids to int ids.
>>> integer_ids([(1, 'a'), (12, 'b'), (1, 'a')])
[0, 1, 0]
"""
dct: dict[Any, int] = {}
iids = []
for id in ids:
iid = dct.get(id)
if iid is None:
iid = len(dct)
dct[id] = iid
iids.append(iid)
return iids
def safe_id(magmom_av, tolerance=1e-3):
"""Convert magnetic moments to integer id's.
While calculating id's for atoms, there may be rounding errors
in magnetic moments supplied. This will create an unique integer
identifier for each magnetic moment double, based on the range
as set by the first occurence of each floating point number:
[magmom_a - tolerance, magmom_a + tolerance].
>>> safe_id([1.01, 0.99, 0.5], tolerance=0.025)
[0, 0, 2]
"""
id_a = []
for a, magmom_v in enumerate(magmom_av):
quantized = None
for a2 in range(a):
if np.linalg.norm(magmom_av[a2] - magmom_v) < tolerance:
quantized = a2
break
if quantized is None:
quantized = a
id_a.append(quantized)
return id_a
def main() -> None:
import argparse
from ase.io import read
parser = argparse.ArgumentParser(
description='Analyze symmetry.')
parser.color = True # type: ignore
parser.add_argument('-t', '--tolerance', type=float, default=0.001,
help='Default is 0.001 Å.')
parser.add_argument(
'filename',
help='Atomic structure (any file-format that ASE can read).')
args = parser.parse_args()
atoms = read(args.filename)
if isinstance(atoms, list):
atoms = atoms[-1]
s = create_symmetries_object(atoms,
symmorphic=False,
tolerance=args.tolerance)
print(s)
if __name__ == '__main__':
main()