Coverage for gpaw/test/elph/test_indices.py: 100%
49 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
1"""Unit test for elph/supercell and elph/gmatrix checking
2whether they properly respect the self.indices.
4Note: If the users gives an index twice that's passed through, because that's
5 how ase.phonons does it.
7"""
8import numpy as np
9import pytest
11from ase.build import bulk
12# from ase.phonons import Phonons
13from gpaw.elph import Supercell, ElectronPhononMatrix
14# from gpaw.mpi import world
17class FakeElectronPhononMatrix(ElectronPhononMatrix):
18 def _set_supercell_cache(self, supercell_cache: str,
19 load_sc_as_needed: bool):
20 pass
22 def _read_phonon_cache(self):
23 pass
26def _supercell_all(atoms):
27 natoms = len(atoms)
28 sc = Supercell(atoms)
29 assert len(sc.indices) == natoms
30 assert sc.indices[0] == 0
31 assert sc.indices[-1] == natoms - 1
34def _gmatrix_all(atoms):
35 natoms = len(atoms)
36 fepm = FakeElectronPhononMatrix(atoms, None, {"name": "phonon"})
37 assert len(fepm.indices) == natoms
38 assert fepm.indices[0] == 0
39 assert fepm.indices[-1] == natoms - 1
42def _supercell_indices(atoms, indices):
43 natoms = len(atoms)
44 nindices = len(indices)
45 sci = Supercell(atoms, indices=indices)
46 assert len(sci.indices) != natoms
47 assert len(sci.indices) == nindices
48 assert sci.indices[0] == indices[0]
49 assert sci.indices[-1] == indices[-1]
52def _gmatrix_indices(atoms, indices):
53 natoms = len(atoms)
54 nindices = len(indices)
55 fepm = FakeElectronPhononMatrix(atoms, None, {"name": "phonon"},
56 indices=indices)
57 assert len(fepm.indices) != natoms
58 assert len(fepm.indices) == nindices
59 assert fepm.indices[0] == indices[0]
60 assert fepm.indices[-1] == indices[-1]
63@pytest.mark.elph
64def test_indices():
66 # arbitrary atoms object
67 n = 3
68 atoms = bulk('Li', crystalstructure='bcc', a=3.51, cubic=True) * (n, n, n)
69 assert len(atoms) == 2 * n**3
71 # case 1: self.indices not given
72 _supercell_all(atoms)
73 _gmatrix_all(atoms)
75 # TODO: Supercell.calculate_gradient()? Would need fake cache.
76 # TODO: Supercell.calculate_supercell_matrix()? Would need fake cache and
77 # stuff.
79 # case 2: gives some random indices
80 seed = 123456
81 rng = np.random.default_rng(seed)
82 indices = rng.integers(0, len(atoms),
83 rng.integers(0, len(atoms) - 1))
84 _supercell_indices(atoms, indices)
85 _gmatrix_indices(atoms, indices)
86 # TODO: Supercell.calculate_gradient()? Would need fake cache.
87 # TODO: Supercell.calculate_supercell_matrix()? Would need fake cache
88 # and stuff.