Coverage for gpaw/test/utilities/test_adjust_cell.py: 100%

47 statements  

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

1import pytest 

2 

3from ase import Atoms 

4from ase.build import fcc111, graphene_nanoribbon 

5 

6from gpaw.utilities.adjust_cell import adjust_cell 

7from gpaw.utilities import h2gpts 

8from gpaw.grid_descriptor import GridDescriptor 

9 

10 

11def test_non_periodic(): 

12 R = 2.0 

13 b = 4.0 

14 h = 0.2 

15 

16 CO = Atoms(['C', 'O'], [(1, 0, 0), (1, 0, R)]) 

17 

18 adjust_cell(CO, b, h) 

19 cc = CO.get_cell() 

20 

21 for c in range(3): 

22 width = 2 * b 

23 if c == 2: 

24 width += R + 2 * h 

25 assert cc[c, c] == pytest.approx(width, abs=1e-10) 

26 

27 

28def test_non_orthogonal_unitcell(): 

29 a = 3.912 

30 box = 3. 

31 h = 0.2 

32 

33 for atoms in [ 

34 fcc111('Pt', (1, 1, 1), a=a), 

35 fcc111('Pt', (5, 6, 2), a=a, orthogonal=True)]: 

36 old_cell = atoms.cell.copy() 

37 

38 adjust_cell(atoms, box, h) 

39 

40 # check that the box ajusts for h in only non periodic directions 

41 assert atoms.cell[:2, :2] == pytest.approx(old_cell[:2, :2]) 

42 # check that the atom is shifted in non periodic direction 

43 assert (atoms.positions[:, 2] >= box).all() 

44 

45 N_c = h2gpts(h, atoms.cell) 

46 gd = GridDescriptor(N_c, atoms.cell, atoms.pbc) 

47 h_c = gd.get_grid_spacings() 

48 

49 assert h_c[2] == pytest.approx(h_c[:2].sum() / 2) 

50 

51 

52def test_rotated_unitcell(): 

53 h = 0.2 

54 box = 3 

55 

56 gnt = graphene_nanoribbon(2, 2, sheet=True, vacuum=3) 

57 

58 gnt.rotate('y', 'z', rotate_cell=True) 

59 

60 adjust_cell(gnt, box, h) 

61 

62 # check if the atoms are inside the unitcell 

63 gnt2 = gnt.copy() 

64 gnt2.pbc = False 

65 spos = gnt2.get_scaled_positions() 

66 

67 assert ((spos) <= 1).all() and (spos >= 0).all() 

68 # check that the vacuum is equal on both sides in non periodic direction 

69 cell_z = gnt.cell[1, 2] 

70 assert (gnt.positions[:, 2] == cell_z / 2).all() 

71 

72 N_c = h2gpts(h, gnt.cell) 

73 gd = GridDescriptor(N_c, gnt.cell, gnt.pbc) 

74 h_c = gd.get_grid_spacings() 

75 

76 assert h_c[1] == pytest.approx((h_c[0] + h_c[2]) / 2)