Coverage for ase/gui/surfaceslab.py: 70.95%

148 statements  

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

1"""surfaceslab.py - Window for setting up surfaces.""" 

2 

3import ase.build as build 

4import ase.gui.ui as ui 

5from ase.data import reference_states 

6from ase.gui.i18n import _, ngettext 

7from ase.gui.widgets import Element, pybutton 

8 

9introtext = _("""\ 

10 Use this dialog to create surface slabs. Select the element by 

11writing the chemical symbol or the atomic number in the box. Then 

12select the desired surface structure. Note that some structures can 

13be created with an othogonal or a non-orthogonal unit cell, in these 

14cases the non-orthogonal unit cell will contain fewer atoms. 

15 

16 If the structure matches the experimental crystal structure, you can 

17look up the lattice constant, otherwise you have to specify it 

18yourself.""") 

19 

20# Name, structure, orthogonal, function 

21surfaces = [ 

22 (_('FCC(100)'), _('fcc'), 'ortho', build.fcc100), 

23 (_('FCC(110)'), _('fcc'), 'ortho', build.fcc110), 

24 (_('FCC(111)'), _('fcc'), 'both', build.fcc111), 

25 (_('FCC(211)'), _('fcc'), 'ortho', build.fcc211), 

26 (_('BCC(100)'), _('bcc'), 'ortho', build.bcc100), 

27 (_('BCC(110)'), _('bcc'), 'both', build.bcc110), 

28 (_('BCC(111)'), _('bcc'), 'both', build.bcc111), 

29 (_('HCP(0001)'), _('hcp'), 'both', build.hcp0001), 

30 (_('HCP(10-10)'), _('hcp'), 'ortho', build.hcp10m10), 

31 (_('DIAMOND(100)'), _('diamond'), 'ortho', build.diamond100), 

32 (_('DIAMOND(111)'), _('diamond'), 'non-ortho', build.diamond111), 

33] 

34 

35structures, crystal, orthogonal, functions = zip(*surfaces) 

36 

37py_template = """ 

38from ase.build import {func} 

39 

40atoms = {func}(symbol='{symbol}', size={size}, 

41 a={a}, {c}vacuum={vacuum}, orthogonal={ortho}) 

42""" 

43 

44 

45class SetupSurfaceSlab: 

46 """Window for setting up a surface.""" 

47 

48 def __init__(self, gui): 

49 self.element = Element('', self.apply) 

50 self.structure = ui.ComboBox( 

51 structures, structures, self.structure_changed 

52 ) 

53 self.structure_warn = ui.Label('', 'red') 

54 self.orthogonal = ui.CheckButton('', True, self.make) 

55 self.lattice_a = ui.SpinBox(3.2, 0.0, 10.0, 0.001, self.make) 

56 self.retrieve = ui.Button( 

57 _('Get from database'), self.structure_changed 

58 ) 

59 self.lattice_c = ui.SpinBox(None, 0.0, 10.0, 0.001, self.make) 

60 self.x = ui.SpinBox(1, 1, 30, 1, self.make) 

61 self.x_warn = ui.Label('', 'red') 

62 self.y = ui.SpinBox(1, 1, 30, 1, self.make) 

63 self.y_warn = ui.Label('', 'red') 

64 self.z = ui.SpinBox(1, 1, 30, 1, self.make) 

65 self.vacuum_check = ui.CheckButton('', False, self.vacuum_checked) 

66 self.vacuum = ui.SpinBox(5, 0, 40, 0.01, self.make) 

67 self.description = ui.Label('') 

68 

69 win = self.win = ui.Window(_('Surface')) 

70 win.add(ui.Text(introtext)) 

71 win.add(self.element) 

72 win.add([_('Structure:'), self.structure, self.structure_warn]) 

73 win.add([_('Orthogonal cell:'), self.orthogonal]) 

74 win.add([_('Lattice constant:')]) 

75 win.add([_('\ta'), self.lattice_a, ('Å'), self.retrieve]) 

76 win.add([_('\tc'), self.lattice_c, ('Å')]) 

77 win.add([_('Size:')]) 

78 win.add([_('\tx: '), self.x, _(' unit cells'), self.x_warn]) 

79 win.add([_('\ty: '), self.y, _(' unit cells'), self.y_warn]) 

80 win.add([_('\tz: '), self.z, _(' unit cells')]) 

81 win.add([_('Vacuum: '), self.vacuum_check, self.vacuum, ('Å')]) 

82 win.add(self.description) 

83 # TRANSLATORS: This is a title of a window. 

84 win.add( 

85 [ 

86 pybutton(_('Creating a surface.'), self.make), 

87 ui.Button(_('Apply'), self.apply), 

88 ui.Button(_('OK'), self.ok), 

89 ] 

90 ) 

91 

92 self.element.grab_focus() 

93 self.gui = gui 

94 self.atoms = None 

95 self.lattice_c.active = False 

96 self.vacuum.active = False 

97 self.structure_changed() 

98 

99 def vacuum_checked(self, *args): 

100 if self.vacuum_check.var.get(): 

101 self.vacuum.active = True 

102 else: 

103 self.vacuum.active = False 

104 self.make() 

105 

106 def get_lattice(self, *args): 

107 if self.element.symbol is None: 

108 return 

109 ref = reference_states[self.element.Z] 

110 symmetry = 'unknown' 

111 for struct in surfaces: 

112 if struct[0] == self.structure.value: 

113 symmetry = struct[1] 

114 if ref['symmetry'] != symmetry: 

115 # TRANSLATORS: E.g. "... assume fcc crystal structure for Au" 

116 self.structure_warn.text = _( 

117 'Error: Reference values assume {} crystal structure for {}!' 

118 ).format(ref['symmetry'], self.element.symbol) 

119 else: 

120 if symmetry == 'fcc' or symmetry == 'bcc' or symmetry == 'diamond': 

121 self.lattice_a.value = ref['a'] 

122 elif symmetry == 'hcp': 

123 self.lattice_a.value = ref['a'] 

124 self.lattice_c.value = ref['a'] * ref['c/a'] 

125 self.make() 

126 

127 def structure_changed(self, *args): 

128 for surface in surfaces: 

129 if surface[0] == self.structure.value: 

130 if surface[2] == 'ortho': 

131 self.orthogonal.var.set(True) 

132 self.orthogonal.check['state'] = ['disabled'] 

133 elif surface[2] == 'non-ortho': 

134 self.orthogonal.var.set(False) 

135 self.orthogonal.check['state'] = ['disabled'] 

136 else: 

137 self.orthogonal.check['state'] = ['normal'] 

138 

139 if surface[1] == _('hcp'): 

140 self.lattice_c.active = True 

141 self.lattice_c.value = round( 

142 self.lattice_a.value * ((8.0 / 3.0) ** (0.5)), 3 

143 ) 

144 else: 

145 self.lattice_c.active = False 

146 self.lattice_c.value = 'None' 

147 self.get_lattice() 

148 

149 def make(self, *args): 

150 symbol = self.element.symbol 

151 self.atoms = None 

152 self.description.text = '' 

153 self.python = None 

154 self.x_warn.text = '' 

155 self.y_warn.text = '' 

156 if symbol is None: 

157 return None 

158 

159 x = self.x.value 

160 y = self.y.value 

161 z = self.z.value 

162 size = (x, y, z) 

163 a = self.lattice_a.value 

164 c = self.lattice_c.value 

165 vacuum = self.vacuum.value 

166 if not self.vacuum_check.var.get(): 

167 vacuum = None 

168 ortho = self.orthogonal.var.get() 

169 

170 ortho_warn_even = _('Please enter an even value for orthogonal cell') 

171 

172 struct = self.structure.value 

173 if struct == _('BCC(111)') and y % 2 != 0 and ortho: 

174 self.y_warn.text = ortho_warn_even 

175 return None 

176 if struct == _('BCC(110)') and y % 2 != 0 and ortho: 

177 self.y_warn.text = ortho_warn_even 

178 return None 

179 if struct == _('FCC(111)') and y % 2 != 0 and ortho: 

180 self.y_warn.text = ortho_warn_even 

181 return None 

182 if struct == _('FCC(211)') and x % 3 != 0 and ortho: 

183 self.x_warn.text = _( 

184 'Please enter a value divisible by 3 for orthogonal cell' 

185 ) 

186 return None 

187 if struct == _('HCP(0001)') and y % 2 != 0 and ortho: 

188 self.y_warn.text = ortho_warn_even 

189 return None 

190 if struct == _('HCP(10-10)') and y % 2 != 0 and ortho: 

191 self.y_warn.text = ortho_warn_even 

192 return None 

193 

194 for surface in surfaces: 

195 if surface[0] == struct: 

196 c_py = '' 

197 if surface[1] == _('hcp'): 

198 self.atoms = surface[3](symbol, size, a, c, vacuum, ortho) 

199 c_py = f'{c}, ' 

200 else: 

201 self.atoms = surface[3](symbol, size, a, vacuum, ortho) 

202 

203 if vacuum is not None: 

204 vacuumtext = _(' Vacuum: {} Å.').format(vacuum) 

205 else: 

206 vacuumtext = '' 

207 

208 natoms = len(self.atoms) 

209 label = ngettext( 

210 # TRANSLATORS: e.g. "Au fcc100 surface with 2 atoms." 

211 # or "Au fcc100 surface with 2 atoms. Vacuum: 5 Å." 

212 '{symbol} {surf} surface with one atom.{vacuum}', 

213 '{symbol} {surf} surface with {natoms} atoms.{vacuum}', 

214 natoms, 

215 ).format( 

216 symbol=symbol, 

217 surf=surface[3].__name__, 

218 natoms=natoms, 

219 vacuum=vacuumtext, 

220 ) 

221 

222 self.description.text = label 

223 return py_template.format( 

224 func=surface[3].__name__, 

225 a=a, 

226 c=c_py, 

227 symbol=symbol, 

228 size=size, 

229 ortho=ortho, 

230 vacuum=vacuum, 

231 ) 

232 

233 def apply(self, *args): 

234 self.make() 

235 if self.atoms is not None: 

236 self.gui.new_atoms(self.atoms) 

237 self.gui.clear_history() 

238 return True 

239 else: 

240 ui.error( 

241 _('No valid atoms.'), 

242 _( 

243 'You have not (yet) specified a consistent ' 

244 'set of parameters.' 

245 ), 

246 ) 

247 return False 

248 

249 def ok(self, *args): 

250 if self.apply(): 

251 self.win.close()