Coverage for ase/build/bulk.py: 91.67%

204 statements  

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

1"""Build crystalline systems""" 

2 

3from math import sqrt 

4from typing import Any 

5 

6from ase.atoms import Atoms 

7from ase.data import atomic_numbers, chemical_symbols, reference_states 

8from ase.symbols import string2symbols 

9from ase.utils import plural 

10 

11 

12def incompatible_cell(*, want, have): 

13 return RuntimeError(f'Cannot create {want} cell for {have} structure') 

14 

15 

16def bulk( 

17 name: str, 

18 crystalstructure: str | None = None, 

19 a: float | None = None, 

20 b: float | None = None, 

21 c: float | None = None, 

22 *, 

23 alpha: float | None = None, 

24 covera: float | None = None, 

25 u: float | None = None, 

26 orthorhombic: bool = False, 

27 cubic: bool = False, 

28 basis=None, 

29) -> Atoms: 

30 """Creating bulk systems. 

31 

32 Crystal structure and lattice constant(s) will be guessed if not 

33 provided. 

34 

35 name: str 

36 Chemical symbol or symbols as in 'MgO' or 'NaCl'. 

37 crystalstructure: str 

38 Must be one of sc, fcc, bcc, tetragonal, bct, hcp, rhombohedral, 

39 orthorhombic, mcl, diamond, zincblende, rocksalt, cesiumchloride, 

40 fluorite or wurtzite. 

41 a: float 

42 Lattice constant. 

43 b: float 

44 Lattice constant. If only a and b is given, b will be interpreted 

45 as c instead. 

46 c: float 

47 Lattice constant. 

48 alpha: float 

49 Angle in degrees for rhombohedral lattice. 

50 covera: float 

51 c/a ratio used for hcp. Default is ideal ratio: sqrt(8/3). 

52 u: float 

53 Internal coordinate for Wurtzite structure. 

54 orthorhombic: bool 

55 Construct orthorhombic unit cell instead of primitive cell 

56 which is the default. 

57 cubic: bool 

58 Construct cubic unit cell if possible. 

59 

60 Examples 

61 -------- 

62 >>> from ase.build import bulk 

63 >>> a1 = bulk('Cu', 'fcc', a=3.6) 

64 >>> a2 = bulk('Cu', 'fcc', a=3.6, orthorhombic=True) 

65 >>> a3 = bulk('Cu', 'fcc', a=3.6, cubic=True) 

66 >>> print(a1.cell) 

67 Cell([[0, 1.8, 1.8], [1.8, 0, 1.8], [1.8, 1.8, 0]]) 

68 >>> print(a2.cell) 

69 Cell([2.54558, 2.54558, 3.6]) 

70 >>> print(a3.cell) 

71 Cell([3.6, 3.6, 3.6]) 

72 """ 

73 

74 if c is None and b is not None: 

75 # If user passes (a, b) positionally, we want it as (a, c) instead: 

76 c, b = b, c 

77 

78 if covera is not None and c is not None: 

79 raise ValueError("Don't specify both c and c/a!") 

80 

81 xref = '' 

82 ref: Any = {} 

83 

84 if name in chemical_symbols: # single element 

85 atomic_number = atomic_numbers[name] 

86 ref = reference_states[atomic_number] 

87 if ref is None: 

88 ref = {} # easier to 'get' things from empty dictionary than None 

89 else: 

90 xref = ref['symmetry'] 

91 

92 if crystalstructure is None: 

93 # `ref` requires `basis` but not given and not pre-defined 

94 if basis is None and 'basis' in ref and ref['basis'] is None: 

95 raise ValueError('This structure requires an atomic basis') 

96 if xref == 'cubic': 

97 # P and Mn are listed as 'cubic' but the lattice constants 

98 # are 7 and 9. They must be something other than simple cubic 

99 # then. We used to just return the cubic one but that must 

100 # have been wrong somehow. --askhl 

101 raise ValueError( 

102 f'The reference structure of {name} is not implemented' 

103 ) 

104 

105 # Mapping of name to number of atoms in primitive cell. 

106 structures = { 

107 'sc': 1, 

108 'fcc': 1, 

109 'bcc': 1, 

110 'tetragonal': 1, 

111 'bct': 1, 

112 'hcp': 1, 

113 'rhombohedral': 1, 

114 'orthorhombic': 1, 

115 'mcl': 1, 

116 'diamond': 1, 

117 'zincblende': 2, 

118 'rocksalt': 2, 

119 'cesiumchloride': 2, 

120 'fluorite': 3, 

121 'wurtzite': 2, 

122 } 

123 

124 if crystalstructure is None: 

125 crystalstructure = xref 

126 if crystalstructure not in structures: 

127 raise ValueError( 

128 f'No suitable reference data for bulk {name}.' 

129 f' Reference data: {ref}' 

130 ) 

131 

132 magmom_per_atom = None 

133 if crystalstructure == xref: 

134 magmom_per_atom = ref.get('magmom_per_atom') 

135 

136 if crystalstructure not in structures: 

137 raise ValueError(f'Unknown structure: {crystalstructure}.') 

138 

139 # Check name: 

140 natoms = len(string2symbols(name)) 

141 natoms0 = structures[crystalstructure] 

142 if natoms != natoms0: 

143 raise ValueError( 

144 'Please specify {} for {} and not {}'.format( 

145 plural(natoms0, 'atom'), crystalstructure, natoms 

146 ) 

147 ) 

148 

149 if alpha is None: 

150 alpha = ref.get('alpha') 

151 

152 if a is None: 

153 if xref != crystalstructure: 

154 raise ValueError('You need to specify the lattice constant.') 

155 if 'a' in ref: 

156 a = ref['a'] 

157 else: 

158 raise KeyError(f'No reference lattice parameter "a" for "{name}"') 

159 

160 if b is None: 

161 bovera = ref.get('b/a') 

162 if bovera is not None and a is not None: 

163 b = bovera * a 

164 

165 if crystalstructure in ['hcp', 'wurtzite']: 

166 if c is not None: 

167 covera = c / a 

168 elif covera is None: 

169 if xref == crystalstructure: 

170 covera = ref['c/a'] 

171 else: 

172 covera = sqrt(8 / 3) 

173 

174 if covera is None: 

175 covera = ref.get('c/a') 

176 if c is None and covera is not None: 

177 c = covera * a 

178 

179 if crystalstructure == 'bct': 

180 from ase.lattice import BCT 

181 

182 if basis is None: 

183 basis = ref.get('basis') 

184 if basis is not None: 

185 natoms = len(basis) 

186 lat = BCT(a=a, c=c) 

187 atoms = Atoms( 

188 [name] * natoms, cell=lat.tocell(), pbc=True, scaled_positions=basis 

189 ) 

190 elif crystalstructure == 'rhombohedral': 

191 atoms = _build_rhl(name, a, alpha, basis) 

192 elif crystalstructure == 'orthorhombic': 

193 atoms = Atoms(name, cell=[a, b, c], pbc=True) 

194 elif orthorhombic: 

195 atoms = _orthorhombic_bulk(name, crystalstructure, a, covera, u) 

196 elif cubic: 

197 atoms = _cubic_bulk(name, crystalstructure, a) 

198 else: 

199 atoms = _primitive_bulk(name, crystalstructure, a, covera, u) 

200 

201 if magmom_per_atom is not None: 

202 magmoms = [magmom_per_atom] * len(atoms) 

203 atoms.set_initial_magnetic_moments(magmoms) 

204 

205 if cubic or orthorhombic: 

206 assert atoms.cell.orthorhombic 

207 

208 return atoms 

209 

210 

211def _build_rhl(name, a, alpha, basis): 

212 from ase.lattice import RHL 

213 

214 lat = RHL(a, alpha) 

215 cell = lat.tocell() 

216 if basis is None: 

217 # RHL: Given by A&M as scaled coordinates "x" of cell.sum(0): 

218 basis_x = reference_states[atomic_numbers[name]]['basis_x'] 

219 basis = basis_x[:, None].repeat(3, axis=1) 

220 natoms = len(basis) 

221 return Atoms([name] * natoms, cell=cell, scaled_positions=basis, pbc=True) 

222 

223 

224def _orthorhombic_bulk(name, crystalstructure, a, covera=None, u=None): 

225 if crystalstructure in ('sc', 'bcc', 'cesiumchloride'): 

226 atoms = _cubic_bulk(name, crystalstructure, a) 

227 elif crystalstructure == 'fcc': 

228 b = a / sqrt(2) 

229 cell = (b, b, a) 

230 scaled_positions = ((0.0, 0.0, 0.0), (0.5, 0.5, 0.5)) 

231 atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions) 

232 elif crystalstructure == 'hcp': 

233 cell = (a, a * sqrt(3), covera * a) 

234 scaled_positions = [ 

235 (0.0, 0 / 6, 0.0), 

236 (0.5, 3 / 6, 0.0), 

237 (0.5, 1 / 6, 0.5), 

238 (0.0, 4 / 6, 0.5), 

239 ] 

240 atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions) 

241 elif crystalstructure == 'diamond': 

242 b = a / sqrt(2) 

243 cell = (b, b, a) 

244 scaled_positions = [ 

245 (0.0, 0.0, 0.0), 

246 (0.5, 0.0, 0.25), 

247 (0.5, 0.5, 0.5), 

248 (0.0, 0.5, 0.75), 

249 ] 

250 atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions) 

251 elif crystalstructure == 'rocksalt': 

252 b = a / sqrt(2) 

253 cell = (b, b, a) 

254 scaled_positions = [ 

255 (0.0, 0.0, 0.0), 

256 (0.5, 0.5, 0.0), 

257 (0.5, 0.5, 0.5), 

258 (0.0, 0.0, 0.5), 

259 ] 

260 atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions) 

261 elif crystalstructure == 'zincblende': 

262 symbol0, symbol1 = string2symbols(name) 

263 atoms = _orthorhombic_bulk(symbol0, 'diamond', a) 

264 atoms.symbols[[1, 3]] = symbol1 

265 elif crystalstructure == 'wurtzite': 

266 cell = (a, a * sqrt(3), covera * a) 

267 u = u or 0.25 + 1 / 3 / covera**2 

268 scaled_positions = [ 

269 (0.0, 0 / 6, 0.0), 

270 (0.0, 2 / 6, 0.5 - u), 

271 (0.0, 2 / 6, 0.5), 

272 (0.0, 0 / 6, 1.0 - u), 

273 (0.5, 3 / 6, 0.0), 

274 (0.5, 5 / 6, 0.5 - u), 

275 (0.5, 5 / 6, 0.5), 

276 (0.5, 3 / 6, 1.0 - u), 

277 ] 

278 atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions) 

279 else: 

280 raise incompatible_cell(want='orthorhombic', have=crystalstructure) 

281 

282 atoms.pbc = True 

283 

284 return atoms 

285 

286 

287def _cubic_bulk(name: str, crystalstructure: str, a: float) -> Atoms: 

288 cell = (a, a, a) 

289 if crystalstructure == 'sc': 

290 atoms = Atoms(name, cell=cell) 

291 elif crystalstructure == 'fcc': 

292 scaled_positions = [ 

293 (0.0, 0.0, 0.0), 

294 (0.0, 0.5, 0.5), 

295 (0.5, 0.0, 0.5), 

296 (0.5, 0.5, 0.0), 

297 ] 

298 atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions) 

299 elif crystalstructure == 'bcc': 

300 scaled_positions = [ 

301 (0.0, 0.0, 0.0), 

302 (0.5, 0.5, 0.5), 

303 ] 

304 atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions) 

305 elif crystalstructure == 'diamond': 

306 scaled_positions = [ 

307 (0.0, 0.0, 0.0), 

308 (0.25, 0.25, 0.25), 

309 (0.0, 0.5, 0.5), 

310 (0.25, 0.75, 0.75), 

311 (0.5, 0.0, 0.5), 

312 (0.75, 0.25, 0.75), 

313 (0.5, 0.5, 0.0), 

314 (0.75, 0.75, 0.25), 

315 ] 

316 atoms = Atoms(8 * name, cell=cell, scaled_positions=scaled_positions) 

317 elif crystalstructure == 'cesiumchloride': 

318 symbol0, symbol1 = string2symbols(name) 

319 atoms = _cubic_bulk(symbol0, 'bcc', a) 

320 atoms.symbols[[1]] = symbol1 

321 elif crystalstructure == 'zincblende': 

322 symbol0, symbol1 = string2symbols(name) 

323 atoms = _cubic_bulk(symbol0, 'diamond', a) 

324 atoms.symbols[[1, 3, 5, 7]] = symbol1 

325 elif crystalstructure == 'rocksalt': 

326 scaled_positions = [ 

327 (0.0, 0.0, 0.0), 

328 (0.5, 0.0, 0.0), 

329 (0.0, 0.5, 0.5), 

330 (0.5, 0.5, 0.5), 

331 (0.5, 0.0, 0.5), 

332 (0.0, 0.0, 0.5), 

333 (0.5, 0.5, 0.0), 

334 (0.0, 0.5, 0.0), 

335 ] 

336 atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions) 

337 elif crystalstructure == 'fluorite': 

338 scaled_positions = [ 

339 (0.00, 0.00, 0.00), 

340 (0.25, 0.25, 0.25), 

341 (0.75, 0.75, 0.75), 

342 (0.00, 0.50, 0.50), 

343 (0.25, 0.75, 0.75), 

344 (0.75, 0.25, 0.25), 

345 (0.50, 0.00, 0.50), 

346 (0.75, 0.25, 0.75), 

347 (0.25, 0.75, 0.25), 

348 (0.50, 0.50, 0.00), 

349 (0.75, 0.75, 0.25), 

350 (0.25, 0.25, 0.75), 

351 ] 

352 atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions) 

353 else: 

354 raise incompatible_cell(want='cubic', have=crystalstructure) 

355 

356 atoms.pbc = True 

357 

358 return atoms 

359 

360 

361def _primitive_bulk(name, crystalstructure, a, covera=None, u=None): 

362 if crystalstructure == 'sc': 

363 atoms = Atoms(name, cell=(a, a, a)) 

364 elif crystalstructure == 'fcc': 

365 b = 0.5 * a 

366 cell = ((0, b, b), (b, 0, b), (b, b, 0)) 

367 atoms = Atoms(name, cell=cell) 

368 elif crystalstructure == 'bcc': 

369 b = 0.5 * a 

370 cell = ((-b, b, b), (b, -b, b), (b, b, -b)) 

371 atoms = Atoms(name, cell=cell) 

372 elif crystalstructure == 'hcp': 

373 c = covera * a 

374 cell = ((a, 0, 0), (-0.5 * a, 0.5 * sqrt(3) * a, 0), (0, 0, c)) 

375 scaled_positions = [ 

376 (0 / 3, 0 / 3, 0.0), 

377 (1 / 3, 2 / 3, 0.5), 

378 ] 

379 atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions) 

380 elif crystalstructure == 'diamond': 

381 atoms = _primitive_bulk(name, 'fcc', a) + _primitive_bulk( 

382 name, 'fcc', a 

383 ) 

384 atoms.positions[1, :] += 0.25 * a 

385 elif crystalstructure == 'rocksalt': 

386 symbol0, symbol1 = string2symbols(name) 

387 atoms = _primitive_bulk(symbol0, 'fcc', a) + _primitive_bulk( 

388 symbol1, 'fcc', a 

389 ) 

390 atoms.positions[1, 0] += 0.5 * a 

391 elif crystalstructure == 'cesiumchloride': 

392 symbol0, symbol1 = string2symbols(name) 

393 atoms = _primitive_bulk(symbol0, 'sc', a) + _primitive_bulk( 

394 symbol1, 'sc', a 

395 ) 

396 atoms.positions[1, :] += 0.5 * a 

397 elif crystalstructure == 'zincblende': 

398 symbol0, symbol1 = string2symbols(name) 

399 atoms = _primitive_bulk(symbol0, 'fcc', a) + _primitive_bulk( 

400 symbol1, 'fcc', a 

401 ) 

402 atoms.positions[1, :] += 0.25 * a 

403 elif crystalstructure == 'fluorite': 

404 symbol0, symbol1, symbol2 = string2symbols(name) 

405 atoms = ( 

406 _primitive_bulk(symbol0, 'fcc', a) 

407 + _primitive_bulk(symbol1, 'fcc', a) 

408 + _primitive_bulk(symbol2, 'fcc', a) 

409 ) 

410 atoms.positions[1, :] += 0.25 * a 

411 atoms.positions[2, :] += 0.75 * a 

412 elif crystalstructure == 'wurtzite': 

413 c = covera * a 

414 cell = ((a, 0, 0), (-0.5 * a, 0.5 * sqrt(3) * a, 0), (0, 0, c)) 

415 u = u or 0.25 + 1 / 3 / covera**2 

416 scaled_positions = [ 

417 (0 / 3, 0 / 3, 0.0), 

418 (1 / 3, 2 / 3, 0.5 - u), 

419 (1 / 3, 2 / 3, 0.5), 

420 (0 / 3, 0 / 3, 1.0 - u), 

421 ] 

422 atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions) 

423 else: 

424 raise incompatible_cell(want='primitive', have=crystalstructure) 

425 

426 atoms.pbc = True 

427 

428 return atoms