Coverage for gpaw/test/response/test_chi0.py: 100%

50 statements  

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

1import pytest 

2from ase.build import bulk 

3from ase.dft.kpoints import monkhorst_pack 

4# from ase.units import Bohr 

5 

6from gpaw import GPAW, FermiDirac, PW 

7from gpaw.response.frequencies import FrequencyDescriptor 

8from gpaw.response.chi0 import Chi0Calculator 

9from gpaw.mpi import serial_comm 

10from itertools import product 

11 

12 

13@pytest.mark.response 

14@pytest.mark.slow 

15def test_response_chi0(in_tmp_dir): 

16 # inputs to loop over [k, gamma, center, sym] 

17 settings = product([2, 3], *[[False, True]] * 3) 

18 

19 for k, gamma, center, sym in settings: 

20 if k == 3 and gamma: 

21 continue 

22 a = bulk('Si', 'diamond') 

23 q_c = [0, 0, 1.0 / k] 

24 kpts = monkhorst_pack((k, k, k)) 

25 if gamma: 

26 kpts += 0.5 / k 

27 if center: 

28 a.center() 

29 name = 'si.k%d.g%d.c%d.s%d' % (k, gamma, center, bool(sym)) 

30 

31 calc = a.calc = GPAW( 

32 kpts=kpts, 

33 symmetry={'point_group': sym}, 

34 mode=PW(150), 

35 occupations=FermiDirac(width=0.001), 

36 convergence={'bands': 8}, 

37 txt=name + '.txt') 

38 a.get_potential_energy() 

39 calc.write(name, 'all') 

40 

41 calc = GPAW(name, txt=None, communicator=serial_comm) 

42 

43 chi0_calc = Chi0Calculator( 

44 gs=calc, context=name + '.log', 

45 wd=FrequencyDescriptor.from_array_or_dict([0, 1.0, 2.0]), 

46 hilbert=False, ecut=100) 

47 chi0 = chi0_calc.calculate(q_c) 

48 assert chi0.body.blockdist.blockcomm.size == 1 

49 chi0_wGG = chi0.chi0_WgG # no block distribution 

50 

51 # sym and center: False 

52 if not sym and not center: 

53 chi00_w = chi0_wGG[:, 0, 0] 

54 elif -1 not in calc.wfs.kd.bz2bz_ks: 

55 assert abs(chi0_wGG[:, 0, 0] - chi00_w).max() < 35e-5 

56 

57 if not sym: 

58 chi00_wGG = chi0_wGG 

59 elif -1 not in calc.wfs.kd.bz2bz_ks: 

60 assert chi0_wGG == pytest.approx(chi00_wGG, abs=3e-5) 

61 

62 chi0 = chi0_calc.calculate([0, 0, 0]) 

63 assert chi0.body.blockdist.blockcomm.size == 1 

64 chi0_wGG = chi0.chi0_WgG # no block distribution 

65 

66 if not sym and not center: 

67 chi000_w = chi0_wGG[:, 0, 0] 

68 elif -1 not in calc.wfs.kd.bz2bz_ks: 

69 assert abs(chi0_wGG[:, 0, 0] - chi000_w).max() < 0.0015 

70 

71 if not sym: 

72 chi000_wGG = chi0_wGG 

73 elif -1 not in calc.wfs.kd.bz2bz_ks: 

74 assert abs(chi0_wGG - chi000_wGG).max() < 0.0015