Coverage for gpaw/test/response/test_mos2_polarizability.py: 73%

33 statements  

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

1import pytest 

2 

3import numpy as np 

4 

5from gpaw import GPAW 

6import gpaw.mpi as mpi 

7from gpaw.response.df import DielectricFunction, read_response_function 

8 

9from gpaw.test import findpeak 

10 

11 

12@pytest.mark.dielectricfunction 

13@pytest.mark.response 

14def test_mos2_polarizability(in_tmp_dir, gpw_files): 

15 calc = GPAW(gpw_files['mos2_pw'], communicator=mpi.serial_comm) 

16 

17 # Calculate the 2D polarizability within the RPA and ALDA 

18 df = DielectricFunction(calc, truncation='2D', 

19 frequencies={'type': 'nonlinear', 'domega0': 0.05}, 

20 integrationmode='tetrahedron integration', 

21 nblocks='max') 

22 df.get_polarizability(xc='RPA', filename='rpa_pol.csv') 

23 mpi.world.barrier() # give rank 0 some time to write the files 

24 

25 # Test against reference values 

26 refs = [ # rpa 

27 # w0r, w0i, wr, wi 

28 [1.875, 2.745, 1.949, 2.856], 

29 # I0r, I0i, Ir, Ii 

30 [10.602, 11.145, 9.886, 10.866], 

31 ] 

32 omega_w, alpha0_w, alpha_w = read_response_function('rpa_pol.csv') 

33 # plot_pol(omega_w, alpha0_w, alpha_w) 

34 w0r, I0r, w0i, I0i = identify_maxima(omega_w, alpha0_w) 

35 wr, Ir, wi, Ii = identify_maxima(omega_w, alpha_w) 

36 assert np.array([w0r, w0i, wr, wi]) == pytest.approx( 

37 np.array(refs[0]), abs=0.01) 

38 assert np.array([I0r, I0i, Ir, Ii]) == pytest.approx( 

39 np.array(refs[1]), abs=0.05) 

40 

41 

42def identify_maxima(omega_w, a_w): 

43 wr, Ir = findpeak(omega_w, a_w.real) 

44 wi, Ii = findpeak(omega_w, a_w.imag) 

45 return wr, Ir, wi, Ii 

46 

47 

48def plot_pol(omega_w, a0_w, a_w): 

49 import matplotlib.pyplot as plt 

50 plt.subplot(1, 2, 1) 

51 plt.plot(omega_w, a0_w.real) 

52 plt.plot(omega_w, a_w.real) 

53 plt.subplot(1, 2, 2) 

54 plt.plot(omega_w, a0_w.imag) 

55 plt.plot(omega_w, a_w.imag) 

56 if mpi.world.rank == 0: 

57 plt.show()