Coverage for gpaw/test/sic/test_fdsic.py: 87%

38 statements  

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

1import pytest 

2 

3from gpaw import GPAW 

4import numpy as np 

5from ase.dft.bandgap import bandgap 

6from ase.units import Ha 

7 

8 

9@pytest.mark.slow 

10@pytest.mark.old_gpaw_only 

11@pytest.mark.sic 

12def test_fdsic(in_tmp_dir, gpw_files): 

13 """ 

14 Test Perdew-Zunger Self-Interaction 

15 Correction in PW mode using DirectMin 

16 :param in_tmp_dir: 

17 :return: 

18 """ 

19 calc = GPAW(gpw_files['h2o_fdsic']) 

20 H2O = calc.atoms 

21 H2O.calc = calc 

22 e = H2O.get_potential_energy() 

23 f = H2O.get_forces() 

24 efermi = calc.wfs.fermi_levels[0] * Ha 

25 gap = bandgap(calc, efermi=efermi)[0] 

26 e_old = -18.144428 

27 

28 assert e == pytest.approx(e_old, abs=1e-3) 

29 # Numeric forces, generated by disabled code below 

30 f_num = np.array([[2.11270273, 4.89616573e-01, -6.00482442e-04], 

31 [-2.15829241, 3.54950512e-01, 1.04418211e-04], 

32 [6.67703026e-01, -8.89596180e-01, 8.83126024e-05]]) 

33 

34 assert f == pytest.approx(f_num, abs=5e-2) 

35 assert gap == pytest.approx(10.215, abs=1e-2) 

36 

37 numeric = False 

38 if numeric: 

39 from gpaw.test import calculate_numerical_forces 

40 f_num = calculate_numerical_forces(H2O, 0.001) 

41 print('Numerical forces') 

42 print(f_num) 

43 print(f - f_num, np.abs(f - f_num).max()) 

44 

45 calc.write('h2o.gpw', mode='all') 

46 from gpaw import restart 

47 H2O, calc = restart('h2o.gpw', txt='-') 

48 H2O.positions += 1.0e-6 

49 e = H2O.get_potential_energy() 

50 f = H2O.get_forces() 

51 niter = calc.get_number_of_iterations() 

52 assert niter == pytest.approx(4, abs=3) 

53 assert e == pytest.approx(e_old, abs=1e-3) 

54 assert f == pytest.approx(f_num, abs=5e-2)