From c85e082e2e5067896819b12e549e6810e7d1eb0a Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Tue, 3 Mar 2026 17:00:31 +0900 Subject: [PATCH] Avoid ndarray copy --- phono3py/api_phono3py.py | 2 +- phono3py/cui/phono3py_script.py | 2 +- phono3py/file_IO.py | 4 ++-- phono3py/phonon/solver.py | 2 +- phono3py/phonon3/fc3.py | 17 ++++------------- phono3py/phonon3/interaction.py | 6 +++--- 6 files changed, 12 insertions(+), 21 deletions(-) diff --git a/phono3py/api_phono3py.py b/phono3py/api_phono3py.py index f973f449..8a02b5a1 100644 --- a/phono3py/api_phono3py.py +++ b/phono3py/api_phono3py.py @@ -822,7 +822,7 @@ def _set_band_indices(self, band_indices=None): self._band_indices = [np.arange(num_band, dtype="int64")] else: self._band_indices = [np.array(bi, dtype="int64") for bi in band_indices] - self._band_indices_flatten = np.hstack(self._band_indices).astype("int64") + self._band_indices_flatten = np.hstack(self._band_indices, dtype="int64") @property def masses(self) -> NDArray: diff --git a/phono3py/cui/phono3py_script.py b/phono3py/cui/phono3py_script.py index d602900b..512eeee7 100644 --- a/phono3py/cui/phono3py_script.py +++ b/phono3py/cui/phono3py_script.py @@ -929,7 +929,7 @@ def _run_isotope_then_exit( """Run isotope scattering calculation.""" mass_variances = settings.mass_variances if settings.band_indices is not None: - band_indices = np.hstack(settings.band_indices).astype("int64") + band_indices = np.hstack(settings.band_indices, dtype="int64") else: band_indices = None iso = Phono3pyIsotope( diff --git a/phono3py/file_IO.py b/phono3py/file_IO.py index 374c76db..37745dd6 100644 --- a/phono3py/file_IO.py +++ b/phono3py/file_IO.py @@ -335,7 +335,7 @@ def write_fc3_to_hdf5( Filename to be used. p2s_map : ndarray, optional Primitive atom indices in supercell index system shape=(n_patom,), - dtype=intc + dtype=int64 fc3_cutoff : float, optional Cutoff distance for fc3. compression : str or int, optional @@ -770,7 +770,7 @@ def write_spectral_function_to_hdf5( if all_band_exist: _band_indices = None else: - _band_indices = np.hstack(band_indices).astype("int64") + _band_indices = np.hstack(band_indices, dtype="int64") suffix = _get_filename_suffix( mesh, grid_point=grid_point, band_indices=_band_indices, sigma=sigma ) diff --git a/phono3py/phonon/solver.py b/phono3py/phonon/solver.py index 7a7b2134..248cc0b8 100644 --- a/phono3py/phonon/solver.py +++ b/phono3py/phonon/solver.py @@ -270,4 +270,4 @@ def _get_fc_elements_mapping(dm: DynamicalMatrix, fc: NDArray[np.double]): fc_p2s = np.arange(len(p2s_map), dtype="int64") fc_s2p = s2pp_map - return np.array(fc_p2s, dtype="int64"), np.array(fc_s2p, dtype="int64") + return fc_p2s, fc_s2p diff --git a/phono3py/phonon3/fc3.py b/phono3py/phonon3/fc3.py index c4a24d9f..74e71fcf 100644 --- a/phono3py/phonon3/fc3.py +++ b/phono3py/phonon3/fc3.py @@ -280,10 +280,10 @@ def set_permutation_symmetry_compact_fc3(fc3, primitive): s2pp_map, nsym_list = get_nsym_list_and_s2pp(s2p_map, p2p_map, permutations) phono3c.permutation_symmetry_compact_fc3( fc3, - np.array(permutations, dtype="int64", order="C"), - np.array(s2pp_map, dtype="int64"), - np.array(p2s_map, dtype="int64"), - np.array(nsym_list, dtype="int64"), + permutations, + s2pp_map, + p2s_map, + nsym_list, ) except ImportError as exc: text = ( @@ -333,11 +333,6 @@ def set_translational_invariance_compact_fc3(fc3, primitive: Primitive): p2p_map = primitive.p2p_map permutations = primitive.atomic_permutations s2pp_map, nsym_list = get_nsym_list_and_s2pp(s2p_map, p2p_map, permutations) - - permutations = np.array(permutations, dtype="int64", order="C") - s2pp_map = np.array(s2pp_map, dtype="int64") - p2s_map = np.array(p2s_map, dtype="int64") - nsym_list = np.array(nsym_list, dtype="int64") phono3c.transpose_compact_fc3( fc3, permutations, s2pp_map, p2s_map, nsym_list, 0 ) # dim[0] <--> dim[1] @@ -628,10 +623,6 @@ def get_drift_fc3( p2p_map = primitive.p2p_map permutations = primitive.atomic_permutations s2pp_map, nsym_list = get_nsym_list_and_s2pp(s2p_map, p2p_map, permutations) - permutations = np.array(permutations, dtype="int64", order="C") - s2pp_map = np.array(s2pp_map, dtype="int64") - p2s_map = np.array(p2s_map, dtype="int64") - nsym_list = np.array(nsym_list, dtype="int64") num_patom = fc3.shape[0] num_satom = fc3.shape[1] maxval1 = 0 diff --git a/phono3py/phonon3/interaction.py b/phono3py/phonon3/interaction.py index b30b31eb..20da0a48 100644 --- a/phono3py/phonon3/interaction.py +++ b/phono3py/phonon3/interaction.py @@ -174,8 +174,8 @@ def __init__( self._svecs, self._multi = self._primitive.get_smallest_vectors() self._masses = np.array(self._primitive.masses, dtype="double") - self._p2s = np.array(self._primitive.p2s_map, dtype="int64") - self._s2p = np.array(self._primitive.s2p_map, dtype="int64") + self._p2s = self._primitive.p2s_map + self._s2p = self._primitive.s2p_map n_satom, n_patom, _ = self._multi.shape self._all_shortest = np.zeros( (n_patom, n_satom, n_satom), dtype="byte", order="C" @@ -680,7 +680,7 @@ def run_phonon_solver_with_eigvec_rotation(self): d2r_map = self._get_reciprocal_rotations_in_space_group_operations() - # perms.shape = (len(spg_ops), len(primitive)), dtype='intc' + # perms.shape = (len(spg_ops), len(primitive)), dtype='int64' perms = compute_all_sg_permutations( self._primitive.scaled_positions, self._bz_grid.symmetry_dataset.rotations, # type: ignore