Coverage for gpaw/test/elph/test_gmatrix.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-19 00:19 +0000

1"""Test for elph/gmatrix""" 

2import numpy as np 

3import pytest 

4 

5from ase.build import bulk 

6# from ase.phonons import Phonons 

7 

8from gpaw import GPAW 

9from gpaw.elph import ElectronPhononMatrix 

10 

11 

12@pytest.mark.serial 

13@pytest.mark.elph 

14def test_gmatrix(module_tmp_path, supercell_cache): 

15 atoms = bulk('Li', crystalstructure='bcc', a=3.51, cubic=True) 

16 supercell_cache 

17 elph = ElectronPhononMatrix(atoms, 'supercell', 'elph') 

18 q = [[0, 0, 0], [0.5, 0.5, 0.5]] 

19 

20 # phonon = Phonons(atoms, name='elph', supercell=(2, 1, 1), 

21 # center_refcell=True) 

22 # phonon.read() 

23 # w_ql, u_ql = phonon.band_structure(q, modes=True) 

24 # print(w_ql) 

25 # print(u_ql) 

26 # [[0.00124087 0.00044091 0.00132004 0.0421112 0.04212737 0.04218485] 

27 # [0.03031018 0.03031948 0.03041029 0.03041035 0.04326759 0.04327498]] 

28 

29 calc = GPAW(mode='lcao', 

30 basis='sz(dzp)', 

31 kpts={'size': (2, 2, 2), 'gamma': False}, 

32 symmetry='off', 

33 txt='li_gs_nosym.txt') 

34 atoms.calc = calc 

35 atoms.get_potential_energy() 

36 

37 g_sqklnn = elph.bloch_matrix(calc, k_qc=q, 

38 savetofile=False, prefactor=False) 

39 

40 assert g_sqklnn.shape == (1, 2, 8, 6, 2, 2) 

41 

42 # NOTE: It seems g is Hermitian if q=0 and symmetric otherwise. CHECK THIS! 

43 

44 # Li has lots of degenerate phonon modes, average/sum those 

45 

46 # q = 0 

47 print("q0 checks") 

48 

49 # accoustic sum rule 

50 assert np.allclose(g_sqklnn[0, 0, :, 0:3], 0.) 

51 

52 g_knn = np.sum(g_sqklnn[0, 0, :, 3:6], axis=1) # modes 4-6 

53 assert g_knn.shape == (8, 2, 2) 

54 # Hermitian 

55 assert np.allclose(g_knn[:, 0, 1], g_knn[:, 1, 0].conj()) 

56 # and check one specific value 

57 print(g_knn[0]) 

58 

59 # the phonon modes are degenerate. Easier to test sum_nu |g_nu|^2 

60 g_abs = np.sum(np.abs(g_sqklnn[0, 0, 0, 3:6, 0, 1])**2) 

61 print(g_abs) 

62 assert g_abs == pytest.approx(0.023668, rel=1e-4) 

63 

64 # q = 1 

65 print("q1 checks") 

66 g_knn = np.sum(g_sqklnn[0, 1, :, 4:6], axis=1) # modes 5-6 

67 assert g_knn.shape == (8, 2, 2) 

68 # Hermitian, actually symmetric 

69 assert np.allclose(g_knn[:, 0, 1], g_knn[:, 1, 0]) 

70 # and check one specific value 

71 print(g_knn[0]) 

72 

73 # the phonon modes are degenerate. Easier to test sum_nu |g_nu|^2 

74 g_abs = np.sum(np.abs(g_sqklnn[0, 1, 0, 4:6, 0, 1])**2) 

75 print(g_abs) 

76 assert g_abs == pytest.approx(0.29253, rel=1e-4)