Coverage for ase/build/root.py: 100.00%

71 statements  

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

1from math import atan2, cos, log10, sin 

2 

3import numpy as np 

4 

5 

6def hcp0001_root( 

7 symbol, root, size, a=None, c=None, vacuum=None, orthogonal=False 

8): 

9 """HCP(0001) surface maniupulated to have a x unit side length 

10 of *root* before repeating. This also results in *root* number 

11 of repetitions of the cell. 

12 

13 

14 The first 20 valid roots for nonorthogonal are... 

15 1, 3, 4, 7, 9, 12, 13, 16, 19, 21, 25, 

16 27, 28, 31, 36, 37, 39, 43, 48, 49""" 

17 from ase.build import hcp0001 

18 

19 atoms = hcp0001( 

20 symbol=symbol, 

21 size=(1, 1, size[2]), 

22 a=a, 

23 c=c, 

24 vacuum=vacuum, 

25 orthogonal=orthogonal, 

26 ) 

27 atoms = root_surface(atoms, root) 

28 atoms *= (size[0], size[1], 1) 

29 return atoms 

30 

31 

32def fcc111_root(symbol, root, size, a=None, vacuum=None, orthogonal=False): 

33 """FCC(111) surface maniupulated to have a x unit side length 

34 of *root* before repeating. This also results in *root* number 

35 of repetitions of the cell. 

36 

37 The first 20 valid roots for nonorthogonal are... 

38 1, 3, 4, 7, 9, 12, 13, 16, 19, 21, 25, 27, 

39 28, 31, 36, 37, 39, 43, 48, 49""" 

40 from ase.build import fcc111 

41 

42 atoms = fcc111( 

43 symbol=symbol, 

44 size=(1, 1, size[2]), 

45 a=a, 

46 vacuum=vacuum, 

47 orthogonal=orthogonal, 

48 ) 

49 atoms = root_surface(atoms, root) 

50 atoms *= (size[0], size[1], 1) 

51 return atoms 

52 

53 

54def bcc111_root(symbol, root, size, a=None, vacuum=None, orthogonal=False): 

55 """BCC(111) surface maniupulated to have a x unit side length 

56 of *root* before repeating. This also results in *root* number 

57 of repetitions of the cell. 

58 

59 

60 The first 20 valid roots for nonorthogonal are... 

61 1, 3, 4, 7, 9, 12, 13, 16, 19, 21, 25, 

62 27, 28, 31, 36, 37, 39, 43, 48, 49""" 

63 from ase.build import bcc111 

64 

65 atoms = bcc111( 

66 symbol=symbol, 

67 size=(1, 1, size[2]), 

68 a=a, 

69 vacuum=vacuum, 

70 orthogonal=orthogonal, 

71 ) 

72 atoms = root_surface(atoms, root) 

73 atoms *= (size[0], size[1], 1) 

74 return atoms 

75 

76 

77def point_in_cell_2d(point, cell, eps=1e-8): 

78 """This function takes a 2D slice of the cell in the XY plane and calculates 

79 if a point should lie in it. This is used as a more accurate method of 

80 ensuring we find all of the correct cell repetitions in the root surface 

81 code. The Z axis is totally ignored but for most uses this should be fine. 

82 """ 

83 

84 # Define area of a triangle 

85 def tri_area(t1, t2, t3): 

86 t1x, t1y = t1[0:2] 

87 t2x, t2y = t2[0:2] 

88 t3x, t3y = t3[0:2] 

89 return ( 

90 abs(t1x * (t2y - t3y) + t2x * (t3y - t1y) + t3x * (t1y - t2y)) / 2 

91 ) 

92 

93 # c0, c1, c2, c3 define a parallelogram 

94 c0 = (0, 0) 

95 c1 = cell[0, 0:2] 

96 c2 = cell[1, 0:2] 

97 c3 = c1 + c2 

98 

99 # Get area of parallelogram 

100 cA = tri_area(c0, c1, c2) + tri_area(c1, c2, c3) 

101 

102 # Get area of triangles formed from adjacent vertices of parallelogram and 

103 # point in question. 

104 pA = ( 

105 tri_area(point, c0, c1) 

106 + tri_area(point, c1, c2) 

107 + tri_area(point, c2, c3) 

108 + tri_area(point, c3, c0) 

109 ) 

110 

111 # If combined area of triangles from point is larger than area of 

112 # parallelogram, point is not inside parallelogram. 

113 return pA <= cA + eps 

114 

115 

116def _root_cell_normalization(primitive_slab): 

117 """Returns the scaling factor for x axis and cell normalized by that 

118 factor""" 

119 

120 xscale = np.linalg.norm(primitive_slab.cell[0, 0:2]) 

121 cell_vectors = primitive_slab.cell[0:2, 0:2] / xscale 

122 return xscale, cell_vectors 

123 

124 

125def _root_surface_analysis(primitive_slab, root, eps=1e-8): 

126 """A tool to analyze a slab and look for valid roots that exist, up to 

127 the given root. This is useful for generating all possible cells 

128 without prior knowledge. 

129 

130 *primitive slab* is the primitive cell to analyze. 

131 

132 *root* is the desired root to find, and all below. 

133 

134 This is the internal function which gives extra data to root_surface. 

135 """ 

136 

137 # Setup parameters for cell searching 

138 logeps = int(-log10(eps)) 

139 _xscale, cell_vectors = _root_cell_normalization(primitive_slab) 

140 

141 # Allocate grid for cell search search 

142 points = np.indices((root + 1, root + 1)).T.reshape(-1, 2) 

143 

144 # Find points corresponding to full cells 

145 cell_points = [cell_vectors[0] * x + cell_vectors[1] * y for x, y in points] 

146 

147 # Find point close to the desired cell (floating point error possible) 

148 roots = np.around(np.linalg.norm(cell_points, axis=1) ** 2, logeps) 

149 

150 valid_roots = np.nonzero(roots == root)[0] 

151 if len(valid_roots) == 0: 

152 raise ValueError( 

153 'Invalid root {} for cell {}'.format(root, cell_vectors) 

154 ) 

155 int_roots = np.array( 

156 [ 

157 int(this_root) 

158 for this_root in roots 

159 if this_root.is_integer() and this_root <= root 

160 ] 

161 ) 

162 return ( 

163 cell_points, 

164 cell_points[np.nonzero(roots == root)[0][0]], 

165 set(int_roots[1:]), 

166 ) 

167 

168 

169def root_surface_analysis(primitive_slab, root, eps=1e-8): 

170 """A tool to analyze a slab and look for valid roots that exist, up to 

171 the given root. This is useful for generating all possible cells 

172 without prior knowledge. 

173 

174 *primitive slab* is the primitive cell to analyze. 

175 

176 *root* is the desired root to find, and all below.""" 

177 return _root_surface_analysis( 

178 primitive_slab=primitive_slab, root=root, eps=eps 

179 )[2] 

180 

181 

182def root_surface(primitive_slab, root, eps=1e-8): 

183 """Creates a cell from a primitive cell that repeats along the x and y 

184 axis in a way consisent with the primitive cell, that has been cut 

185 to have a side length of *root*. 

186 

187 *primitive cell* should be a primitive 2d cell of your slab, repeated 

188 as needed in the z direction. 

189 

190 *root* should be determined using an analysis tool such as the 

191 root_surface_analysis function, or prior knowledge. It should always 

192 be a whole number as it represents the number of repetitions.""" 

193 

194 atoms = primitive_slab.copy() 

195 

196 xscale, cell_vectors = _root_cell_normalization(primitive_slab) 

197 

198 # Do root surface analysis 

199 cell_points, root_point, _roots = _root_surface_analysis( 

200 primitive_slab, root, eps=eps 

201 ) 

202 

203 # Find new cell 

204 root_angle = -atan2(root_point[1], root_point[0]) 

205 root_rotation = [ 

206 [cos(root_angle), -sin(root_angle)], 

207 [sin(root_angle), cos(root_angle)], 

208 ] 

209 root_scale = np.linalg.norm(root_point) 

210 

211 cell = np.array( 

212 [np.dot(x, root_rotation) * root_scale for x in cell_vectors] 

213 ) 

214 

215 # Find all cell centers within the cell 

216 shift = cell_vectors.sum(axis=0) / 2 

217 cell_points = [ 

218 point 

219 for point in cell_points 

220 if point_in_cell_2d(point + shift, cell, eps=eps) 

221 ] 

222 

223 # Setup new cell 

224 atoms.rotate(root_angle, v='z') 

225 atoms *= (root, root, 1) 

226 atoms.cell[0:2, 0:2] = cell * xscale 

227 atoms.center() 

228 

229 # Remove all extra atoms 

230 del atoms[ 

231 [ 

232 atom.index 

233 for atom in atoms 

234 if not point_in_cell_2d(atom.position, atoms.cell, eps=eps) 

235 ] 

236 ] 

237 

238 # Rotate cell back to original orientation 

239 standard_rotation = [ 

240 [cos(-root_angle), -sin(-root_angle), 0], 

241 [sin(-root_angle), cos(-root_angle), 0], 

242 [0, 0, 1], 

243 ] 

244 

245 new_cell = np.array([np.dot(x, standard_rotation) for x in atoms.cell]) 

246 new_positions = np.array( 

247 [np.dot(x, standard_rotation) for x in atoms.positions] 

248 ) 

249 

250 atoms.cell = new_cell 

251 atoms.positions = new_positions 

252 return atoms