Coverage for gpaw/test/utilities/test_wannier_ethylene.py: 98%

40 statements  

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

1"""GPAW wannier example for ethylene corresponding to the ASE Wannier 

2tutorial. 

3""" 

4import pytest 

5import numpy as np 

6from ase import Atoms 

7 

8from gpaw import GPAW 

9from gpaw.mpi import size 

10from gpaw.wannier.overlaps import calculate_overlaps 

11from gpaw.wannier.edmiston_ruedenberg import localize 

12 

13pytestmark = pytest.mark.ci 

14 

15 

16@pytest.fixture(scope='module') 

17def ethylene(): 

18 a = 6.0 # Size of unit cell (Angstrom) 

19 

20 mol = Atoms('H2C2H2', 

21 [(-1.235, -0.936, 0), 

22 (-1.235, 0.936, 0), 

23 (-0.660, 0.000, 0), 

24 (0.660, 0.000, 0), 

25 (1.235, -0.936, 0), 

26 (1.235, 0.936, 0)], 

27 cell=(a, a, a), 

28 pbc=True) 

29 mol.center() 

30 

31 mol.calc = GPAW(mode='fd', 

32 txt=None, 

33 nbands=8, 

34 gpts=(32, 32, 32), 

35 convergence={'eigenstates': 3.3e-5}) 

36 mol.get_potential_energy() 

37 return mol 

38 

39 

40@pytest.mark.skipif(size > 1, reason='Not parallelized') 

41def test_ethylene_energy(ethylene): 

42 e = ethylene.get_potential_energy() 

43 assert e == pytest.approx(-33.328, abs=0.002) 

44 

45 

46def check(calc): 

47 wannier = localize(calculate_overlaps(calc, n1=0, n2=6, nwannier=6)) 

48 

49 centers = wannier.centers 

50 print(centers) 

51 expected = [[1.950, 2.376, 3.000], 

52 [1.950, 3.624, 3.000], 

53 [3.000, 3.000, 2.671], 

54 [3.000, 3.000, 3.329], 

55 [4.050, 2.376, 3.000], 

56 [4.050, 3.624, 3.000]] 

57 assert wannier.value == pytest.approx(13.7995, abs=0.016) 

58 for center in centers: 

59 i = 0 

60 while np.sum((expected[i] - center)**2) > 0.01: 

61 i += 1 

62 if i == len(expected): 

63 raise RuntimeError('Correct center not found') 

64 expected.pop(i) 

65 

66 

67@pytest.mark.skipif(size > 1, reason='Not parallelized') 

68def test_wannier_centers(ethylene): 

69 check(ethylene.calc) 

70 

71 

72@pytest.mark.skipif(size > 1, reason='Not parallelized') 

73def test_wannier_centers_gpw(ethylene, in_tmp_dir): 

74 ethylene.calc.write('ethylene.gpw', 'all') 

75 check(GPAW('ethylene.gpw', txt=None))