Coverage for gpaw/test/pw/test_par_strategies.py: 100%

34 statements  

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

1import numpy as np 

2import pytest 

3from ase import Atoms 

4from gpaw import PW, FermiDirac 

5from gpaw.new.ase_interface import GPAW as NewGPAW 

6from gpaw import GPAW as AnyGPAW 

7from gpaw.mpi import world 

8 

9# Domain and k-point parallelization: 

10dk = [] 

11for d in [1, 2, 4, 8]: 

12 for k in [1, 2]: 

13 if d * k > world.size: 

14 continue 

15 dk.append((d, k)) 

16 

17 

18@pytest.mark.stress 

19@pytest.mark.parametrize('d, k', dk) 

20@pytest.mark.parametrize( 

21 'gpu', [False, 

22 pytest.param( 

23 True, 

24 marks=[pytest.mark.gpu])]) 

25def test_pw_par_strategies(in_tmp_dir, d, k, gpu, gpaw_new): 

26 ecut = 200 

27 kpoints = [1, 1, 4] 

28 atoms = Atoms('HLi', 

29 cell=[6, 6, 3.4], 

30 pbc=True, 

31 positions=[[3, 3, 0], 

32 [3, 3, 1.6]]) 

33 parallel = {'domain': d, 'kpt': k} 

34 if gpu: 

35 parallel['gpu'] = True 

36 GPAW = NewGPAW 

37 else: 

38 GPAW = AnyGPAW 

39 atoms.calc = GPAW(mode=PW(ecut), 

40 txt='hli.txt', 

41 parallel=parallel, 

42 kpts={'size': kpoints}, 

43 convergence={'density': 1e-6}, 

44 occupations=FermiDirac(width=0.1)) 

45 

46 e = atoms.get_potential_energy() 

47 assert e == pytest.approx(-5.2238, abs=1e-4) 

48 

49 f = atoms.get_forces() 

50 assert f == pytest.approx(np.array([[0, 0, -0.776], 

51 [0, 0, 0.776]]), abs=0.001) 

52 

53 s = atoms.get_stress() 

54 assert s == pytest.approx( 

55 [0.0043, 0.0043, 0.0005, 0, 0, 0], abs=0.0001) 

56 

57 atoms.calc.write('hli.gpw', mode='all') 

58 GPAW('hli.gpw', txt=None)