Coverage for ase/build/surfaces_with_termination.py: 98.73%

79 statements  

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

1import numpy as np 

2 

3from ase.build.general_surface import surface 

4from ase.geometry import get_layers 

5from ase.symbols import string2symbols 

6 

7 

8def surfaces_with_termination( 

9 lattice, 

10 indices, 

11 layers, 

12 vacuum=None, 

13 tol=1e-10, 

14 termination=None, 

15 return_all=False, 

16 verbose=False, 

17): 

18 """Create surface from a given lattice and Miller indices with a given 

19 termination 

20 

21 Parameters 

22 ---------- 

23 lattice: Atoms object or str 

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

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

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

27 correct bulk lattice will be generated automatically. 

28 indices: sequence of three int 

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

30 layers: int 

31 Number of equivalent layers of the slab. (not the same as the layers 

32 you choose from for terminations) 

33 vacuum: float 

34 Amount of vacuum added on both sides of the slab. 

35 termination: str 

36 the atoms you wish to be in the top layer. There may be many such 

37 terminations, this function returns all terminations with the same 

38 atomic composition. 

39 e.g. 'O' will return oxygen terminated surfaces. 

40 e.g.'TiO' returns surfaces terminated with layers containing both 

41 O and Ti 

42 Returns: 

43 return_surfs: List 

44 a list of surfaces that match the specifications given 

45 

46 """ 

47 lats = translate_lattice(lattice, indices) 

48 return_surfs = [] 

49 check = [] 

50 check2 = [] 

51 for item in lats: 

52 too_similar = False 

53 surf = surface(item, indices, layers, vacuum=vacuum, tol=tol) 

54 surf.wrap(pbc=[True] * 3) # standardize slabs 

55 

56 positions = surf.get_scaled_positions().flatten() 

57 for i, value in enumerate(positions): 

58 if value >= 1 - tol: # move things closer to zero within tol 

59 positions[i] -= 1 

60 surf.set_scaled_positions(np.reshape(positions, (len(surf), 3))) 

61 # rep = find_z_layers(surf) 

62 z_layers, _hs = get_layers(surf, (0, 0, 1)) # just z layers matter 

63 # get the indicies of the atoms in the highest layer 

64 top_layer = [ 

65 i for i, val in enumerate(z_layers == max(z_layers)) if val 

66 ] 

67 

68 if termination is not None: 

69 comp = [surf.get_chemical_symbols()[a] for a in top_layer] 

70 term = string2symbols(termination) 

71 # list atoms in top layer and not in requested termination 

72 check = [a for a in comp if a not in term] 

73 # list of atoms in requested termination and not in top layer 

74 check2 = [a for a in term if a not in comp] 

75 if len(return_surfs) > 0: 

76 pos_diff = [ 

77 a.get_positions() - surf.get_positions() for a in return_surfs 

78 ] 

79 for i, su in enumerate(pos_diff): 

80 similarity_test = su.flatten() < tol * 1000 

81 if similarity_test.all(): 

82 # checks if surface is too similar to another surface 

83 too_similar = True 

84 if too_similar: 

85 continue 

86 if return_all is True: 

87 pass 

88 elif check != [] or check2 != []: 

89 continue 

90 return_surfs.append(surf) 

91 return return_surfs 

92 

93 

94def translate_lattice(lattice, indices, tol=10**-3): 

95 """translates a bulk unit cell along a normal vector given by the a set of 

96 miller indices to the next symetric position. This is used to control the 

97 termination of the surface in the smart_surface command 

98 Parameters 

99 ---------- 

100 lattice: Atoms object 

101 atoms object of the bulk unit cell 

102 indices: 1x3 list,tuple, or numpy array 

103 the miller indices you wish to cut along. 

104 returns: 

105 lattice_list: list of Atoms objects 

106 a list of all the different translations of the unit cell that will 

107 yield different terminations of a surface cut along the miller 

108 indices provided. 

109 """ 

110 lattice_list = [] 

111 cell = lattice.get_cell() 

112 pt = [0, 0, 0] 

113 h, k, l = indices # noqa (E741 ambiguous name 'l') 

114 millers = list(indices) 

115 for index, item in enumerate(millers): 

116 if item == 0: 

117 millers[index] = 10**9 # make zeros large numbers 

118 elif pt == [0, 0, 0]: # for numerical stability 

119 pt = list(cell[index] / float(item) / np.linalg.norm(cell[index])) 

120 h1, k1, l1 = millers 

121 N = np.array(cell[0] / h1 + cell[1] / k1 + cell[2] / l1) 

122 n = N / np.linalg.norm(N) # making a unit vector normal to cut plane 

123 # finding distance from cut plan vector 

124 d = [ 

125 np.round(np.dot(n, (a - pt)) * n, 5) 

126 for a in lattice.get_scaled_positions() 

127 ] 

128 duplicates = [] 

129 for i, item in enumerate(d): 

130 g = [True for a in d[i + 1 :] if np.linalg.norm(a - item) < tol] 

131 if g != []: 

132 duplicates.append(i) 

133 duplicates.reverse() 

134 for i in duplicates: 

135 del d[i] 

136 # put distance to the plane at the end of the array 

137 for i, item in enumerate(d): 

138 d[i] = np.append( 

139 item, np.dot(n, (lattice.get_scaled_positions()[i] - pt)) 

140 ) 

141 d = np.array(d) 

142 d = d[d[:, 3].argsort()] # sort by distance to the plane 

143 d = [a[:3] for a in d] # remove distance 

144 d = list(d) # make it a list again 

145 for i in d: 

146 """ 

147 The above method gives you the boundries of between terminations that 

148 will allow you to build a complete set of terminations. However, it 

149 does not return all the boundries. Thus you must check both above and 

150 below the boundary, and not stray too far from the boundary. If you move 

151 too far away, you risk hitting another boundary you did not find. 

152 """ 

153 lattice1 = lattice.copy() 

154 displacement = (h * cell[0] + k * cell[1] + l * cell[2]) * (i + 10**-8) 

155 lattice1.positions -= displacement 

156 lattice_list.append(lattice1) 

157 lattice1 = lattice.copy() 

158 displacement = (h * cell[0] + k * cell[1] + l * cell[2]) * (i - 10**-8) 

159 lattice1.positions -= displacement 

160 lattice_list.append(lattice1) 

161 return lattice_list