Coverage for gpaw/test/new/test_cell_change.py: 100%

57 statements  

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

1import pytest 

2from ase import Atoms 

3from gpaw.new.ase_interface import GPAW 

4from gpaw.mpi import broadcast_string 

5from io import StringIO 

6 

7 

8@pytest.mark.parametrize('gpu', [False, True]) 

9def test_new_cell(gpu): 

10 a = 2.1 

11 az = 2.099 

12 atoms = Atoms('Li', pbc=True, cell=[a, a, az]) 

13 atoms.positions += 0.01 

14 output = StringIO() 

15 atoms.calc = GPAW( 

16 xc='PBE', 

17 mode={'name': 'pw'}, 

18 kpts=(2, 2, 1), 

19 parallel={'gpu': gpu, 'domain': 1}, 

20 txt=output) 

21 e0 = atoms.get_potential_energy() 

22 s0 = atoms.get_stress() 

23 f0 = atoms.get_forces() 

24 print(e0, s0, f0) 

25 assert e0 == pytest.approx(-1.27648045935401) 

26 assert f0 == pytest.approx(0, abs=1e-5) 

27 assert s0 == pytest.approx([-3.97491456e-01] * 2 

28 + [3.29507807e-03] + [0, 0, 0], abs=5e-6) 

29 

30 atoms.cell[2, 2] = 0.9 * az 

31 atoms.positions += 0.1 

32 e1 = atoms.get_potential_energy() 

33 s1 = atoms.get_stress() 

34 f1 = atoms.get_forces() 

35 print(e1, s1, f1) 

36 assert e1 == pytest.approx(-1.2359952570422994) 

37 assert f1 == pytest.approx(0, abs=1e-4) 

38 assert s1 == pytest.approx([-4.37458548e-01] * 2 + 

39 [-9.41665221e-02, 0.0, 0.0, 0.0], abs=5e-6) 

40 out = broadcast_string(output.getvalue() or None) 

41 assert 'Interpolating wave fun' in out 

42 

43 

44@pytest.mark.parametrize('gpu', [False, True]) 

45def test_new_cell_1d(gpu): 

46 a = 3.1 

47 az = 2.099 

48 atoms = Atoms('Li', pbc=(0, 0, 1), cell=[a, a, az, 90, 90, 120]) 

49 atoms.center() 

50 output = StringIO() 

51 atoms.calc = GPAW( 

52 xc='PBE', 

53 mode={'name': 'pw'}, 

54 kpts=(1, 1, 4), 

55 parallel={'gpu': gpu, 'domain': 1}, 

56 txt=output) 

57 e0 = atoms.get_potential_energy() 

58 s0 = atoms.get_stress() 

59 f0 = atoms.get_forces() 

60 print(e0, s0, f0) 

61 assert e0 == pytest.approx(-3.367005531386283) 

62 assert f0 == pytest.approx(0, abs=1e-5) 

63 assert s0 == pytest.approx( 

64 [8.05730258e-02] * 2 + [-1.45549945e-01, 0, 0, 0], abs=5e-6) 

65 

66 atoms.cell[2, 2] = 1.05 * az 

67 atoms.positions += 0.1 

68 e1 = atoms.get_potential_energy() 

69 s1 = atoms.get_stress() 

70 f1 = atoms.get_forces() 

71 print(e1, s1, f1) 

72 assert e1 == pytest.approx(-3.4761627073672816) 

73 assert f1 == pytest.approx(0, abs=1e-4) 

74 assert s1 == pytest.approx( 

75 [7.51550293e-02] * 2 + [-1.05616472e-01, 0.0, 0.0, 0.0], abs=5e-5) 

76 out = broadcast_string(output.getvalue() or None) 

77 assert 'Interpolating wave fun' in out