Coverage for ase/build/general_surface.py: 84.62%

65 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1from math import gcd 

2 

3import numpy as np 

4from numpy.linalg import norm, solve 

5 

6from ase.build import bulk 

7from ase.build.surface import create_tags 

8 

9 

10def surface(lattice, indices, layers, vacuum=None, tol=1e-10, periodic=False): 

11 """Create surface from a given lattice and Miller indices. 

12 

13 lattice: Atoms object or str 

14 Bulk lattice structure of alloy or pure metal. Note that the 

15 unit-cell must be the conventional cell - not the primitive cell. 

16 One can also give the chemical symbol as a string, in which case the 

17 correct bulk lattice will be generated automatically. 

18 indices: sequence of three int 

19 Surface normal in Miller indices (h,k,l). 

20 layers: int 

21 Number of equivalent layers of the slab. 

22 vacuum: float or None 

23 Amount of vacuum added on both sides of the slab. If `None` (default), 

24 the Z-axis length is determined strictly by the bulk interlayer 

25 spacing, creating a continuous, bulk-like cell. Passing `vacuum <= 0.0` 

26 is deprecated and will result in an error in a future version. 

27 It will shrink the cell to the atomic bounding box, which may cause the 

28 top and bottom layers to unphysically overlap under periodic boundary 

29 conditions. 

30 periodic: bool 

31 Whether the surface is periodic in the normal to the surface 

32 """ 

33 

34 indices = np.asarray(indices) 

35 

36 if indices.shape != (3,) or not indices.any() or indices.dtype != int: 

37 raise ValueError(f'{indices} is an invalid surface type') 

38 

39 if isinstance(lattice, str): 

40 lattice = bulk(lattice, cubic=True) 

41 

42 h, k, l = indices # noqa (E741, the variable l) 

43 h0, k0, l0 = indices == 0 

44 

45 if h0 and k0 or h0 and l0 or k0 and l0: # if two indices are zero 

46 if not h0: 

47 c1, c2, c3 = [(0, 1, 0), (0, 0, 1), (1, 0, 0)] 

48 if not k0: 

49 c1, c2, c3 = [(0, 0, 1), (1, 0, 0), (0, 1, 0)] 

50 if not l0: 

51 c1, c2, c3 = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] 

52 else: 

53 p, q = ext_gcd(k, l) 

54 a1, a2, a3 = lattice.cell 

55 

56 # constants describing the dot product of basis c1 and c2: 

57 # dot(c1,c2) = k1+i*k2, i in Z 

58 k1 = np.dot( 

59 p * (k * a1 - h * a2) + q * (l * a1 - h * a3), l * a2 - k * a3 

60 ) 

61 k2 = np.dot( 

62 l * (k * a1 - h * a2) - k * (l * a1 - h * a3), l * a2 - k * a3 

63 ) 

64 

65 if abs(k2) > tol: 

66 i = -int(round(k1 / k2)) # i corresponding to the optimal basis 

67 p, q = p + i * l, q - i * k 

68 

69 a, b = ext_gcd(p * k + q * l, h) 

70 

71 c1 = (p * k + q * l, -p * h, -q * h) 

72 c2 = np.array((0, l, -k)) // abs(gcd(l, k)) 

73 c3 = (b, a * p, a * q) 

74 

75 surf = build(lattice, np.array([c1, c2, c3]), layers, tol, periodic) 

76 

77 if vacuum is not None: 

78 if vacuum <= 0.0: 

79 import warnings 

80 

81 warnings.warn( 

82 'Passing vacuum <= 0 is deprecated and will raise a ValueError ' 

83 'in a future release. To create a continuous bulk-like slab ' 

84 'without adding extra space, use vacuum=None.', 

85 FutureWarning, 

86 stacklevel=2, 

87 ) 

88 

89 surf.center(vacuum=vacuum, axis=2) 

90 

91 return surf 

92 

93 

94def build(lattice, basis, layers, tol, periodic): 

95 surf = lattice.copy() 

96 scaled = solve(basis.T, surf.get_scaled_positions().T).T 

97 scaled -= np.floor(scaled + tol) 

98 

99 scaled[abs(scaled) < tol] = 0.0 

100 

101 surf.set_scaled_positions(scaled) 

102 surf.set_cell(np.dot(basis, surf.cell), scale_atoms=True) 

103 surf *= (1, 1, layers) 

104 surf.set_tags(create_tags((1, len(lattice), layers))) 

105 

106 a1, a2, a3 = surf.cell 

107 surf.set_cell( 

108 [ 

109 a1, 

110 a2, 

111 np.cross(a1, a2) 

112 * np.dot(a3, np.cross(a1, a2)) 

113 / norm(np.cross(a1, a2)) ** 2, 

114 ] 

115 ) 

116 

117 # Change unit cell to have the x-axis parallel with a surface vector 

118 # and z perpendicular to the surface: 

119 a1, a2, a3 = surf.cell 

120 surf.set_cell( 

121 [ 

122 (norm(a1), 0, 0), 

123 ( 

124 np.dot(a1, a2) / norm(a1), 

125 np.sqrt(norm(a2) ** 2 - (np.dot(a1, a2) / norm(a1)) ** 2), 

126 0, 

127 ), 

128 (0, 0, norm(a3)), 

129 ], 

130 scale_atoms=True, 

131 ) 

132 

133 surf.pbc = (True, True, periodic) 

134 

135 # Move atoms into the unit cell: 

136 scaled = surf.get_scaled_positions() 

137 scaled[:, :2] %= 1 

138 surf.set_scaled_positions(scaled) 

139 

140 if not periodic: 

141 surf.cell[2] = 0.0 

142 

143 return surf 

144 

145 

146def ext_gcd(a, b): 

147 if b == 0: 

148 return 1, 0 

149 elif a % b == 0: 

150 return 0, 1 

151 else: 

152 x, y = ext_gcd(b, a % b) 

153 return y, x - y * (a // b)