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

37 statements  

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

1import pytest 

2import numpy as np 

3from gpaw.mpi import world 

4from gpaw.test import findpeak 

5from gpaw.response.bse import BSE 

6from gpaw.response.df import read_response_function 

7 

8 

9def create_bse(gpwfile, q_c=(0, 0, 0)): 

10 bse = BSE(gpwfile, 

11 q_c=q_c, 

12 soc_tol=0.01, 

13 add_soc=True, 

14 ecut=10, 

15 valence_bands=2, 

16 conduction_bands=2, 

17 eshift=0.8, 

18 nbands=15, 

19 mode='BSE', 

20 truncation='2D') 

21 return bse 

22 

23 

24@pytest.mark.response 

25def test_response_bse_MoS2_cut(in_tmp_dir, scalapack, gpw_files): 

26 gpwfile = gpw_files['mos2_5x5_pw'] 

27 bse = create_bse(gpwfile) 

28 

29 outw_w, outalpha_w = bse.get_polarizability(eta=0.02, 

30 w_w=np.linspace(0., 5., 5001)) 

31 world.barrier() 

32 w_w, alphareal_w, alphaimag_w = read_response_function('pol_bse.csv') 

33 

34 # Check consistency with written results 

35 assert np.allclose(outw_w, w_w, atol=1e-5, rtol=1e-4) 

36 assert np.allclose(outalpha_w.real, alphareal_w, atol=1e-5, rtol=1e-4) 

37 assert np.allclose(outalpha_w.imag, alphaimag_w, atol=1e-5, rtol=1e-4) 

38 

39 w0, I0 = findpeak(w_w[:1100], alphaimag_w[:1100]) 

40 w1, I1 = findpeak(w_w[1100:1300], alphaimag_w[1100:1300]) 

41 w1 += 1.1 

42 

43 assert w0 == pytest.approx(0.58, abs=0.01) 

44 assert I0 == pytest.approx(38.8, abs=0.35) 

45 assert w1 == pytest.approx(2.22, abs=0.01) 

46 assert I1 == pytest.approx(6.3, abs=0.35) 

47 

48 ################################################################# 

49 # Absorption and EELS spectra for 2D materials should be identical 

50 # for q=0. 

51 ################################################################# 

52 

53 bse = create_bse(gpwfile) 

54 outw_w, eels = bse.get_eels_spectrum(w_w=w_w) 

55 

56 bse = create_bse(gpwfile) 

57 factor = bse.gs.cd.nonperiodic_hypervolume / (4 * np.pi) 

58 outw_w, pol = bse.get_polarizability(w_w=w_w) 

59 assert np.allclose(pol.imag / factor, eels) 

60 

61 ##################################################################### 

62 # Absorption and EELS spectra for 2D materials should NOT be identical 

63 # for finite q. 

64 ##################################################################### 

65 bse = create_bse(gpwfile, q_c=[0.2, 0.2, 0.0]) 

66 outw_w, eels = bse.get_eels_spectrum(w_w=w_w) 

67 bse = create_bse(gpwfile, q_c=[0.2, 0.2, 0.0]) 

68 outw_w, pol = bse.get_polarizability(w_w) 

69 

70 assert not np.allclose(pol.imag / factor, eels)