Coverage for gpaw/test/response/test_graphene.py: 97%

36 statements  

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

1import pytest 

2import numpy as np 

3from ase import Atoms 

4 

5from gpaw import GPAW, PW, FermiDirac, Mixer 

6from gpaw.utilities import compiled_with_sl 

7from gpaw.response.df import DielectricFunction 

8from gpaw.response.symmetry import QSymmetryAnalyzer 

9from gpaw.mpi import world 

10 

11# This test assures that some things that 

12# should be equal, are. 

13 

14 

15@pytest.mark.dielectricfunction 

16@pytest.mark.response 

17@pytest.mark.slow 

18def test_response_graphene(in_tmp_dir): 

19 a = 2.5 

20 c = 3.22 

21 

22 GR = Atoms(symbols='C2', 

23 positions=[(0.5 * a, 0.2 - np.sqrt(3) / 6 * a, 0.0), 

24 (0.5 * a, 0.2 + np.sqrt(3) / 6 * a, 0.0)], 

25 cell=[(0.5 * a, -0.5 * 3**0.5 * a, 0), 

26 (0.5 * a, 0.5 * 3**0.5 * a, 0), 

27 (0.0, 0.0, c * 2.0)]) 

28 GR.set_pbc((True, True, True)) 

29 atoms = GR 

30 # These are not physically-motivated kpoint grid sizes. 

31 # They are the (nearly) the minimum number we need to test 

32 # the symmetry off-on and gamma off-on difference. 

33 GSsettings = [ 

34 {'symmetry': 'off', 'kpts': {'size': [3, 2, 1], 'gamma': False}}, 

35 {'symmetry': {}, 'kpts': {'size': [3, 2, 1], 'gamma': False}}, 

36 {'symmetry': 'off', 'kpts': {'size': [3, 2, 1], 'gamma': True}}, 

37 {'symmetry': {}, 'kpts': {'size': [3, 2, 1], 'gamma': True}}] 

38 

39 DFsettings = [ 

40 {'qsymmetry': QSymmetryAnalyzer(pointgroup, timerev)} 

41 for pointgroup in [False, True] 

42 for timerev in [False, True]] 

43 

44 if world.size > 1 and compiled_with_sl(): 

45 DFsettings.append({'qsymmetry': True, 'nblocks': 2}) 

46 

47 for GSkwargs in GSsettings: 

48 calc = GPAW(mode=PW(200), 

49 occupations=FermiDirac(0.2), 

50 mixer=Mixer(0.4), 

51 convergence={'eigenstates': 1e-4, 'density': 1e-3}, 

52 **GSkwargs) 

53 

54 atoms.calc = calc 

55 atoms.get_potential_energy() 

56 calc.write('gr.gpw', 'all') 

57 

58 dfs = [] 

59 for kwargs in DFsettings: 

60 DF = DielectricFunction(calc='gr.gpw', 

61 frequencies={'type': 'nonlinear', 

62 'domega0': 0.2}, 

63 eta=0.2, 

64 ecut=15.0, 

65 rate=0.001, 

66 **kwargs) 

67 df1, df2 = DF.get_dielectric_function() 

68 if world.rank == 0: 

69 dfs.append(df1) 

70 

71 # Check the calculated dielectric functions against 

72 # each other. 

73 while len(dfs): 

74 df = dfs.pop() 

75 for DFkwargs, df2 in zip(DFsettings[-len(dfs):], dfs): 

76 assert df == pytest.approx(df2)