Coverage for gpaw/test/directopt/test_directmin_pwfd.py: 95%

38 statements  

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

1import pytest 

2import numpy as np 

3 

4from gpaw import GPAW, PW, FD 

5from ase import Atoms 

6from gpaw.mpi import world 

7 

8 

9# @pytest.mark.new_gpaw_ready 

10@pytest.mark.do 

11@pytest.mark.parametrize('mode', ['pw', 'fd']) 

12def test_directmin_pw(in_tmp_dir, mode, gpaw_new): 

13 if gpaw_new and world.size > 1: 

14 pytest.skip('Does not work yet for new GPAW') 

15 atoms = Atoms('CCHHHH', 

16 positions=[ 

17 [-0.66874198, -0.00001714, -0.00001504], 

18 [0.66874210, 0.00001699, 0.00001504], 

19 [-1.24409879, 0.00000108, -0.93244784], 

20 [-1.24406253, 0.00000112, 0.93242153], 

21 [1.24406282, -0.93242148, 0.00000108], 

22 [1.24409838, 0.93244792, 0.00000112] 

23 ] 

24 ) 

25 atoms.center(vacuum=4.0) 

26 atoms.set_pbc(False) 

27 

28 if mode == 'pw': 

29 kwargs = dict(mode=PW(300, force_complex_dtype=True)) 

30 e0 = -26.205455 

31 f0 = np.array([[-3.73061, 0.00020, -0.00011], 

32 [3.72978, 0.00002, -0.00020], 

33 [-0.61951, -2.63437, -0.47774], 

34 [-0.62006, 2.63439, 0.47806], 

35 [0.62080, -0.47803, -2.63261], 

36 [0.62030, 0.47779, 2.63259]]) 

37 else: 

38 kwargs = dict(mode=FD(), h=0.3) 

39 e0 = -24.789097 

40 f0 = np.array([[9.23321, -0.01615, -0.00169], 

41 [-9.23057, 0.00276, 0.01506], 

42 [-3.42781, -2.65716, -2.17586], 

43 [-3.43347, 2.64956, 2.17609], 

44 [3.43284, -2.17649, -2.65107], 

45 [3.42732, 2.17634, 2.66001]]) 

46 

47 calc = GPAW(**kwargs, 

48 xc='PBE', 

49 occupations={'name': 'fixed-uniform'}, 

50 eigensolver={'name': 'etdm-fdpw', 

51 'converge_unocc': True}, 

52 mixer={'backend': 'no-mixing'}, 

53 spinpol=True, 

54 symmetry='off', 

55 nbands=-5, 

56 convergence={'eigenstates': 4.0e-6}, 

57 ) 

58 atoms.calc = calc 

59 energy = atoms.get_potential_energy() 

60 f = atoms.get_forces() 

61 

62 assert f0 == pytest.approx(f, abs=1e-2) 

63 assert energy == pytest.approx(e0, abs=1.0e-4) 

64 assert calc.wfs.kpt_u[0].eps_n[5] > calc.wfs.kpt_u[0].eps_n[6] 

65 

66 calc.write('ethylene.gpw', mode='all') 

67 from gpaw import restart 

68 atoms, calc = restart('ethylene.gpw', txt='-') 

69 atoms.positions += 1.0e-6 

70 f2 = atoms.get_forces() 

71 niter = calc.get_number_of_iterations() 

72 

73 assert niter == pytest.approx(3, abs=1) 

74 assert f0 == pytest.approx(f2, abs=1e-2) 

75 assert calc.wfs.kpt_u[0].eps_n[5] > calc.wfs.kpt_u[0].eps_n[6] 

76 

77 

78if __name__ == '__main__': 

79 test_directmin_pw(1, 'fd')