From e058270b6f9366d0248216dc17d769b092dbd8ff Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Fri, 23 Aug 2024 11:30:14 +0900 Subject: [PATCH] Update to follow phonopy update for spglib v2.5 --- phono3py/cui/phono3py_script.py | 5 ++- phono3py/phonon/grid.py | 62 ++++++++++++++++++++------------- phono3py/phonon3/interaction.py | 10 +++--- test/phonon/test_grid.py | 4 +-- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/phono3py/cui/phono3py_script.py b/phono3py/cui/phono3py_script.py index 4ac0fe75..943b9311 100644 --- a/phono3py/cui/phono3py_script.py +++ b/phono3py/cui/phono3py_script.py @@ -244,7 +244,10 @@ def start_phono3py(**argparse_control) -> tuple[argparse.Namespace, int]: print("Python version %d.%d.%d" % sys.version_info[:3]) import spglib - print("Spglib version %d.%d.%d" % spglib.get_version()) + try: # spglib.get_version() is deprecated. + print(f"Spglib version {spglib.spg_get_version()}") + except AttributeError: + print("Spglib version %d.%d.%d" % spglib.get_version()) if deprecated: show_deprecated_option_warnings(deprecated) diff --git a/phono3py/phonon/grid.py b/phono3py/phonon/grid.py index a3407b96..1aa34fc6 100644 --- a/phono3py/phonon/grid.py +++ b/phono3py/phonon/grid.py @@ -38,6 +38,13 @@ from __future__ import annotations import warnings from collections.abc import Sequence + +try: + from spglib import SpglibDataset +except ImportError: + from types import SimpleNamespace as SpglibDataset + + from typing import Optional, Union import numpy as np @@ -137,7 +144,7 @@ class BZGrid: mesh: Union[int, float, Sequence, np.ndarray], reciprocal_lattice=None, lattice=None, - symmetry_dataset: Optional[dict] = None, + symmetry_dataset: Optional[Union[SpglibDataset]] = None, transformation_matrix: Optional[Union[Sequence, np.ndarray]] = None, is_shift: Optional[Union[list, np.ndarray]] = None, is_time_reversal: bool = True, @@ -156,9 +163,10 @@ class BZGrid: lattice : array_like Direct primitive basis vectors given as row vectors shape=(3, 3), dtype='double', order='C' - symmetry_dataset : dict, optional + symmetry_dataset : SpglibDataset, optional Symmetry dataset (Symmetry.dataset) searched for the primitive cell - corresponding to ``reciprocal_lattice`` or ``lattice``. + corresponding to ``reciprocal_lattice`` or ``lattice``. For spglib < + v2.5, SimpleNamespace is used instead of SpglibDataset. transformation_matrix : array_like, optional Transformation matrix equivalent to ``transformation_matrix`` in spglib-dataset. This is only used when ``use_grg=True`` and @@ -384,7 +392,7 @@ class BZGrid: return self._reciprocal_operations @property - def symmetry_dataset(self): + def symmetry_dataset(self) -> SpglibDataset: """Return Symmetry.dataset.""" return self._symmetry_dataset @@ -472,7 +480,7 @@ class BZGrid: direct_rotations = np.eye(3, dtype="int_", order="C").reshape(1, 3, 3) else: direct_rotations = np.array( - self._symmetry_dataset["rotations"], dtype="int_", order="C" + self._symmetry_dataset.rotations, dtype="int_", order="C" ) rec_rotations = np.zeros((48, 3, 3), dtype="int_", order="C") num_rec_rot = recgrid.reciprocal_rotations( @@ -530,7 +538,7 @@ class GridMatrix: self, mesh: Union[int, float, Sequence, np.ndarray], lattice: Union[Sequence, np.ndarray], - symmetry_dataset: Optional[dict] = None, + symmetry_dataset: Optional[SpglibDataset] = None, transformation_matrix: Optional[Union[list, np.ndarray]] = None, use_grg: bool = True, force_SNF: bool = False, @@ -547,9 +555,10 @@ class GridMatrix: lattice : array_like Primitive basis vectors in direct space given as row vectors. shape=(3, 3), dtype='double', order='C' - symmetry_dataset : dict, optional + symmetry_dataset : SpglibDataset, optional Symmetry dataset of spglib (Symmetry.dataset) of primitive cell that - has `lattice`. Default is None. + has `lattice`. Default is None. For spglib < + v2.5, SimpleNamespace is used instead of SpglibDataset. transformation_matrix : array_like, optional Transformation matrix equivalent to ``transformation_matrix`` in spglib-dataset. This is only used when ``use_grg=True`` and @@ -627,7 +636,7 @@ class GridMatrix: self, mesh: Union[int, float, Sequence, np.ndarray], use_grg: bool = False, - symmetry_dataset: Optional[dict] = None, + symmetry_dataset: Optional[SpglibDataset] = None, transformation_matrix: Optional[Union[list, np.ndarray]] = None, force_SNF=False, coordinates="reciprocal", @@ -673,7 +682,7 @@ class GridMatrix: self._D_diag = length2mesh(length, self._lattice) else: self._D_diag = length2mesh( - length, self._lattice, rotations=symmetry_dataset["rotations"] + length, self._lattice, rotations=symmetry_dataset.rotations ) if num_values == 9: self._run_grg( @@ -703,7 +712,7 @@ class GridMatrix: sym_dataset = symmetry_dataset else: # transformation_matrix is not None sym_dataset = self._get_mock_symmetry_dataset(transformation_matrix) - if is_primitive_cell(sym_dataset["rotations"]): + if is_primitive_cell(sym_dataset.rotations): self._set_GRG_mesh( sym_dataset, length=length, @@ -742,18 +751,23 @@ class GridMatrix: "be equal to or larger than 1." ) raise RuntimeError(msg) - sym_dataset = { - "rotations": np.eye(3, dtype="intc", order="C").reshape(1, 3, 3), - "transformation_matrix": transformation_matrix, - "std_lattice": self._lattice, - "std_types": np.array([1], dtype="intc"), - "number": 1, - } + + from types import SimpleNamespace + + sym_dataset = SimpleNamespace( + **{ + "rotations": np.eye(3, dtype="intc", order="C").reshape(1, 3, 3), + "transformation_matrix": transformation_matrix, + "std_lattice": self._lattice, + "std_types": np.array([1], dtype="intc"), + "number": 1, + } + ) return sym_dataset def _set_GRG_mesh( self, - sym_dataset: dict, + sym_dataset: SpglibDataset, length: Optional[float] = None, grid_matrix=None, force_SNF=False, @@ -807,22 +821,22 @@ class GridMatrix: `reciprocal` (default) or `direct`. """ - tmat = sym_dataset["transformation_matrix"] + tmat = sym_dataset.transformation_matrix conv_lat = np.dot(np.linalg.inv(tmat).T, self._lattice) # GRG is wanted to be generated with respect to std_lattice if possible. if _can_use_std_lattice( conv_lat, tmat, - sym_dataset["std_lattice"], - sym_dataset["rotations"], + sym_dataset.std_lattice, + sym_dataset.rotations, ): - conv_lat = sym_dataset["std_lattice"] + conv_lat = sym_dataset.std_lattice tmat = np.dot(self._lattice, np.linalg.inv(conv_lat)).T if coordinates == "direct": num_cells = int(np.prod(length2mesh(length, conv_lat))) - max_num_atoms = num_cells * len(sym_dataset["std_types"]) + max_num_atoms = num_cells * len(sym_dataset.std_types) conv_mesh_numbers = estimate_supercell_matrix( sym_dataset, max_num_atoms=max_num_atoms, max_iter=200 ) diff --git a/phono3py/phonon3/interaction.py b/phono3py/phonon3/interaction.py index 6461fc0c..6bf486f7 100644 --- a/phono3py/phonon3/interaction.py +++ b/phono3py/phonon3/interaction.py @@ -772,8 +772,8 @@ class Interaction: # perms.shape = (len(spg_ops), len(primitive)), dtype='intc' perms = compute_all_sg_permutations( self._primitive.scaled_positions, - self._bz_grid.symmetry_dataset["rotations"], - self._bz_grid.symmetry_dataset["translations"], + self._bz_grid.symmetry_dataset.rotations, + self._bz_grid.symmetry_dataset.translations, np.array(self._primitive.cell.T, dtype="double", order="C"), symprec=self._symprec, ) @@ -821,13 +821,13 @@ class Interaction: """ d2r_map = [] - for r in self._bz_grid.symmetry_dataset["rotations"]: + for r in self._bz_grid.symmetry_dataset.rotations: for i, rec_r in enumerate(self._bz_grid.reciprocal_operations): if (rec_r.T == r).all(): d2r_map.append(i) break - assert len(d2r_map) == len(self._bz_grid.symmetry_dataset["rotations"]) + assert len(d2r_map) == len(self._bz_grid.symmetry_dataset.rotations) return d2r_map @@ -838,7 +838,7 @@ class Interaction: """ Rq = np.dot(self._bz_grid.QDinv, self._bz_grid.addresses[bzgp]) - tau = self._bz_grid.symmetry_dataset["translations"][t_i] + tau = self._bz_grid.symmetry_dataset.translations[t_i] phase_factor = np.exp(-2j * np.pi * np.dot(Rq, tau)) self._phonon_done[bzgp] = 1 self._frequencies[bzgp, :] = self._frequencies[orig_gp, :] diff --git a/test/phonon/test_grid.py b/test/phonon/test_grid.py index a770d047..a43646f0 100644 --- a/test/phonon/test_grid.py +++ b/test/phonon/test_grid.py @@ -2425,7 +2425,7 @@ def test_GridMatrix_with_grid_matrix(ph_nacl: Phonopy): np.testing.assert_array_equal(gm.grid_matrix, mesh) np.testing.assert_array_equal(gm.D_diag, [9, 18, 18]) - tmat = ph_nacl.primitive_symmetry.dataset["transformation_matrix"] + tmat = ph_nacl.primitive_symmetry.dataset.transformation_matrix gm = GridMatrix(mesh, ph_nacl.primitive.cell, transformation_matrix=tmat) np.testing.assert_array_equal(gm.grid_matrix, mesh) np.testing.assert_array_equal(gm.D_diag, [9, 18, 18]) @@ -2439,7 +2439,7 @@ def test_GridMatrix_with_transformation_matrix(ph_nacl: Phonopy): """ mesh = 50.0 - tmat = ph_nacl.primitive_symmetry.dataset["transformation_matrix"] + tmat = ph_nacl.primitive_symmetry.dataset.transformation_matrix gm = GridMatrix( mesh, ph_nacl.primitive.cell,