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

34 statements  

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

1"""Basic test of elph/DisplacementRunner 

2 

3The main functionality is imported from ase.phonons.Displacement, 

4so won't test this here. Just test, whether the potential is reliably 

5extracted. 

6""" 

7import numpy as np 

8import pytest 

9 

10from ase.build import bulk 

11from ase.utils.filecache import MultiFileJSONCache 

12 

13from gpaw import GPAW 

14from gpaw.elph import DisplacementRunner 

15from gpaw.mpi import world 

16 

17 

18@pytest.mark.elph 

19def test_displacements(in_tmp_dir): 

20 # 2 atoms with one 1 valence electron each 

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

22 assert len(atoms) == 2 

23 # Note: usually we need to disable point group symmetry for displacements, 

24 # but we won't run actual displacements, so it saves time not to bother. 

25 calc = GPAW(mode='lcao', 

26 basis='sz(dzp)', 

27 # need more than one point for proper test 

28 kpts={'size': (2, 2, 2), 'gamma': True}, 

29 # symmetry={'point_group': False}, 

30 # parallel={'sl_auto': True, 'augment_grids':True, 

31 # 'band': 1, 'kpt': 1, 'domain': 4 }, 

32 txt='elph_displacements.txt') 

33 atoms.calc = calc 

34 

35 elph = DisplacementRunner(atoms=atoms, calc=atoms.calc, 

36 supercell=(1, 1, 1), name='elph', 

37 calculate_forces=False) 

38 elph.indices = [] 

39 elph.run() 

40 del elph 

41 

42 # read stuff back 

43 if world.rank == 0: 

44 cache = MultiFileJSONCache('elph') 

45 info = cache['info'] 

46 assert info['supercell'] == [1, 1, 1] 

47 assert info['natom'] == 2 

48 assert info['delta'] == 0.01 

49 assert 'dr_version' in info 

50 

51 # the following might change if defaults are changed 

52 Vt_G = cache['eq']['Vt_sG'][0] 

53 # print(Vt_G.shape, np.min(Vt_G), np.max(Vt_G)) 

54 assert Vt_G.shape == (16, 16, 16) 

55 assert pytest.approx(np.min(Vt_G), abs=1e-6) == -0.5636114 

56 assert pytest.approx(np.max(Vt_G), abs=1e-6) == -0.1254635 

57 

58 dH_all_asp = cache['eq']['dH_all_asp'] 

59 # print(np.min(dH_all_asp[0][0]), np.max(dH_all_asp[0][0])) 

60 assert len(dH_all_asp) == 2 

61 dH_p = dH_all_asp[0][0] 

62 assert dH_p.shape == (15,) 

63 assert pytest.approx(np.min(dH_p), abs=1e-6) == -0.2249569 

64 assert pytest.approx(np.max(dH_p), abs=1e-6) == 0.0335627