Coverage for gpaw/test/utilities/test_simple_stm.py: 94%

67 statements  

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

1import pytest 

2from ase import Atoms 

3 

4from gpaw import GPAW, FermiDirac 

5from gpaw.analyse.simple_stm import SimpleStm 

6from gpaw.mpi import rank, size 

7 

8 

9def test_utilities_simple_stm(in_tmp_dir): 

10 load = False 

11 txt = '/dev/null' 

12 txt = '-' 

13 

14 me = '' 

15 if size > 1: 

16 me += 'rank ' + str(rank) + ': ' 

17 

18 BH = Atoms('BH', [[.0, .0, .41], [.0, .0, -1.23]], 

19 cell=[5, 6, 6.5]) 

20 BH.center() 

21 

22 f3dname = 'stm3d.cube' 

23 

24 def testSTM(calc): 

25 stm = SimpleStm(calc) 

26 stm.write_3D([1, 0, 0], f3dname) # single wf 

27 wf = stm.gd.integrate(stm.ldos) 

28 

29 if size == 1: # XXXX might have trouble reading in parallel 

30 stm2 = SimpleStm(f3dname) 

31 wf2 = stm2.gd.integrate(stm2.ldos) 

32 print('Integrals: written, read=', wf, wf2) 

33 assert wf == pytest.approx(wf2, abs=2.e-6) 

34 

35 stm.scan_const_current(0.02, 5) 

36 # print eigenvalue_string(calc) 

37 stm.write_3D(3.1, f3dname) 

38 wf2 = stm.gd.integrate(stm.ldos) 

39 # print "wf2=", wf2 

40 assert wf2 == pytest.approx(2, abs=0.12) 

41 

42 return wf 

43 

44 # finite system without spin and width 

45 fname = 'BH-nospin_wfs.gpw' 

46 if not load: 

47 BH.set_pbc(False) 

48 cf = GPAW(mode='fd', nbands=3, h=.3, txt=txt) 

49 BH.calc = cf 

50 e1 = BH.get_potential_energy() 

51 cf.write(fname, 'all') 

52 else: 

53 cf = GPAW(fname, txt=txt) 

54 wf = testSTM(cf) 

55 

56 # finite system with spin 

57 fname = 'BH-spin_Sz2_wfs.gpw' 

58 BH.set_initial_magnetic_moments([1, 1]) 

59 if not load: 

60 BH.set_pbc(False) 

61 cf = GPAW(mode='fd', 

62 occupations=FermiDirac(0.1, fixmagmom=True), 

63 nbands=5, 

64 h=0.3, 

65 txt=txt) 

66 BH.calc = cf 

67 e2 = BH.get_potential_energy() 

68 cf.write(fname, 'all') 

69 else: 

70 cf = GPAW(fname, txt=txt) 

71 testSTM(cf) 

72 

73 # periodic system 

74 if not load: 

75 BH.set_pbc(True) 

76 cp = GPAW(mode='fd', spinpol=True, nbands=3, h=.3, 

77 kpts=(2, 1, 1), txt=txt, symmetry='off') 

78 BH.calc = cp 

79 e3 = BH.get_potential_energy() 

80 cp.write('BH-4kpts_wfs.gpw', 'all') 

81 else: 

82 cp = GPAW('BH-4kpts_wfs.gpw', txt=txt) 

83 

84 stmp = SimpleStm(cp) 

85 

86 stmp.write_3D(-4., f3dname) 

87 print(me + 'Integrals(occ): 2 * wf, bias=', 2 * wf, 

88 stmp.gd.integrate(stmp.ldos)) 

89 assert 2 * wf == pytest.approx(stmp.gd.integrate(stmp.ldos), abs=0.02) 

90 

91 stmp.write_3D(+4., f3dname) 

92 print(me + 'Integrals(unocc): 2 * wf, bias=', end=' ') 

93 print(2 * wf, stmp.gd.integrate(stmp.ldos)) 

94 assert 2 * wf == pytest.approx(stmp.gd.integrate(stmp.ldos), abs=0.02) 

95 

96 energy_tolerance = 0.007 

97 assert e1 == pytest.approx(-2.54026, abs=energy_tolerance) 

98 assert e2 == pytest.approx(-1.51101, abs=energy_tolerance) 

99 assert e3 == pytest.approx(-2.83573, abs=energy_tolerance)