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
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
1import pytest
3from ase import Atoms
4from ase.build import fcc111, graphene_nanoribbon
6from gpaw.utilities.adjust_cell import adjust_cell
7from gpaw.utilities import h2gpts
8from gpaw.grid_descriptor import GridDescriptor
11def test_non_periodic():
12 R = 2.0
13 b = 4.0
14 h = 0.2
16 CO = Atoms(['C', 'O'], [(1, 0, 0), (1, 0, R)])
18 adjust_cell(CO, b, h)
19 cc = CO.get_cell()
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)
28def test_non_orthogonal_unitcell():
29 a = 3.912
30 box = 3.
31 h = 0.2
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()
38 adjust_cell(atoms, box, h)
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()
45 N_c = h2gpts(h, atoms.cell)
46 gd = GridDescriptor(N_c, atoms.cell, atoms.pbc)
47 h_c = gd.get_grid_spacings()
49 assert h_c[2] == pytest.approx(h_c[:2].sum() / 2)
52def test_rotated_unitcell():
53 h = 0.2
54 box = 3
56 gnt = graphene_nanoribbon(2, 2, sheet=True, vacuum=3)
58 gnt.rotate('y', 'z', rotate_cell=True)
60 adjust_cell(gnt, box, h)
62 # check if the atoms are inside the unitcell
63 gnt2 = gnt.copy()
64 gnt2.pbc = False
65 spos = gnt2.get_scaled_positions()
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()
72 N_c = h2gpts(h, gnt.cell)
73 gd = GridDescriptor(N_c, gnt.cell, gnt.pbc)
74 h_c = gd.get_grid_spacings()
76 assert h_c[1] == pytest.approx((h_c[0] + h_c[2]) / 2)