Coverage for ase/geometry/bravais_type_engine.py: 89.19%

74 statements  

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

1import itertools 

2 

3import numpy as np 

4 

5from ase.cell import Cell 

6from ase.lattice import UnconventionalLattice, bravais_lattices, bravais_names 

7 

8"""This module implements a crude method to recognize most Bravais lattices. 

9 

10Suppose we use the ase.lattice module to generate many 

11lattices of some particular type, say, BCT(a, c), and then we 

12Niggli-reduce all of them. The Niggli-reduced forms are not 

13immediately recognizable, but we know the mapping from each reduced 

14form back to the original form. As it turns out, there are apparently 

155 such mappings (the proof is left as an exercise for the reader). 

16 

17Hence, presumably, every BCT lattice (whether generated by BCT(a, c) 

18or in some other form) Niggli-reduces to a form which, through the 

19inverse of one of those five operations, is mapped back to the 

20recognizable one. Knowing all five operations (or equivalence 

21classes), we can characterize any BCT lattice. Same goes for the 

22other lattices of sufficiently low dimension. 

23 

24For MCL, MCLC, and TRI, we may not recognize all forms correctly, 

25but we aspire that this will work for all common inputs.""" 

26 

27 

28niggli_op_table = { # Generated by generate_niggli_op_table() 

29 'LINE': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

30 'SQR': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

31 'RECT': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

32 'CRECT': [(-1, 1, 0, 1, 0, 0, 0, 0, -1), (1, 0, 0, 0, -1, 0, 0, 0, -1)], 

33 'HEX2D': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

34 'OBL': [ 

35 (-1, 1, 0, 1, 0, 0, 0, 0, -1), 

36 (1, -1, 0, 0, 1, 0, 0, 0, 1), 

37 (1, 0, 0, 0, -1, 0, 0, 0, -1), 

38 (-1, -1, 0, 1, 0, 0, 0, 0, 1), 

39 (1, 1, 0, 0, -1, 0, 0, 0, -1), 

40 ], 

41 'BCC': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

42 'BCT': [ 

43 (1, 0, 0, 0, 1, 0, 0, 0, 1), 

44 (0, 1, 0, 0, 0, 1, 1, 0, 0), 

45 (0, 1, 0, 1, 0, 0, 1, 1, -1), 

46 (-1, 0, 1, 0, 1, 0, -1, 1, 0), 

47 (1, 1, 0, 1, 0, 0, 0, 0, -1), 

48 ], 

49 'CUB': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

50 'FCC': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

51 'HEX': [(1, 0, 0, 0, 1, 0, 0, 0, 1), (0, 1, 0, 0, 0, 1, 1, 0, 0)], 

52 'ORC': [(1, 0, 0, 0, 1, 0, 0, 0, 1)], 

53 'ORCC': [ 

54 (1, 0, 0, 0, 1, 0, 0, 0, 1), 

55 (1, 0, -1, 1, 0, 0, 0, -1, 0), 

56 (-1, 1, 0, -1, 0, 0, 0, 0, 1), 

57 (0, 1, 0, 0, 0, 1, 1, 0, 0), 

58 (0, -1, 1, 0, -1, 0, 1, 0, 0), 

59 ], 

60 'ORCF': [(0, -1, 0, 0, 1, -1, 1, 0, 0), (-1, 0, 0, 1, 0, 1, 1, 1, 0)], 

61 'ORCI': [ 

62 (0, 0, -1, 0, -1, 0, -1, 0, 0), 

63 (0, 0, 1, -1, 0, 0, -1, -1, 0), 

64 (0, 1, 0, 1, 0, 0, 1, 1, -1), 

65 (0, -1, 0, 1, 0, -1, 1, -1, 0), 

66 ], 

67 'RHL': [ 

68 (0, -1, 0, 1, 1, 1, -1, 0, 0), 

69 (1, 0, 0, 0, 1, 0, 0, 0, 1), 

70 (1, -1, 0, 1, 0, -1, 1, 0, 0), 

71 ], 

72 'TET': [(1, 0, 0, 0, 1, 0, 0, 0, 1), (0, 1, 0, 0, 0, 1, 1, 0, 0)], 

73 'MCL': [ 

74 (0, 0, 1, -1, -1, 0, 1, 0, 0), 

75 (-1, 0, 0, 0, 1, 0, 0, 0, -1), 

76 (0, 0, -1, 1, 1, 0, 0, -1, 0), 

77 (0, -1, 0, 1, 0, 1, -1, 0, 0), 

78 (0, 1, 0, -1, 0, -1, 0, 0, 1), 

79 (-1, 0, 0, 0, 1, 1, 0, 0, -1), 

80 (0, 1, 0, 1, 0, -1, -1, 0, 0), 

81 (0, 0, 1, 1, -1, 0, 0, 1, 0), 

82 (0, 1, 0, -1, 0, 0, 0, 0, 1), 

83 (0, 0, -1, -1, 1, 0, 1, 0, 0), 

84 (1, 0, 0, 0, 1, -1, 0, 0, 1), 

85 (0, -1, 0, -1, 0, 1, 0, 0, -1), 

86 (-1, 0, 0, 0, -1, 1, 0, 1, 0), 

87 (1, 0, 0, 0, -1, -1, 0, 1, 0), 

88 (0, 0, -1, 1, 0, 0, 0, -1, 0), 

89 ], 

90 'MCLC': [ 

91 (1, 1, 1, 1, 0, 1, 0, 0, -1), 

92 (1, 1, 1, 1, 1, 0, -1, 0, 0), 

93 (1, -1, 1, -1, 0, 1, 0, 0, -1), 

94 (-1, 1, 0, 1, 0, 0, 0, 0, -1), 

95 (1, 0, 0, 0, 1, 0, 0, 0, 1), 

96 (-1, 0, -1, 1, -1, -1, 0, 0, 1), 

97 (1, -1, -1, 1, -1, 0, -1, 0, 0), 

98 (-1, -1, 0, -1, 0, -1, 1, 0, 0), 

99 (1, 0, 1, 1, 0, 0, 0, 1, 0), 

100 (-1, 1, 0, -1, 0, 1, 1, 0, 0), 

101 (0, -1, 1, -1, 0, 1, 0, 0, -1), 

102 (-1, -1, 0, -1, 0, 0, 0, 0, -1), 

103 (-1, -1, 1, -1, 0, 1, 0, 0, -1), 

104 (1, 0, 0, 0, -1, 1, 0, 0, -1), 

105 (-1, 0, -1, 0, -1, -1, 0, 0, 1), 

106 (1, 0, -1, -1, 1, -1, 0, 0, 1), 

107 (1, -1, 1, 1, -1, 0, 0, 1, 0), 

108 (0, -1, 0, 1, 0, -1, 0, 0, 1), 

109 (-1, 0, 0, 1, 1, 1, 0, 0, -1), 

110 (1, 0, -1, 0, 1, -1, 0, 0, 1), 

111 (-1, 1, 0, 1, 1, -1, 0, -1, 0), 

112 (1, 1, -1, 1, -1, 0, -1, 0, 0), 

113 (-1, -1, -1, -1, -1, 0, 0, 1, 0), 

114 (-1, 1, 1, 1, 0, 1, 0, 0, -1), 

115 (-1, 0, 0, 0, -1, 0, 0, 0, 1), 

116 (-1, -1, 1, 1, -1, 0, 0, 1, 0), 

117 (1, 1, 0, -1, 0, -1, 0, 0, 1), 

118 ], 

119 'TRI': [ 

120 (0, -1, 0, -1, 0, 0, 0, 0, -1), 

121 (0, 1, 0, 0, 0, 1, 1, 0, 0), 

122 (0, 0, -1, 0, -1, 0, -1, 1, 0), 

123 (0, 0, 1, 0, 1, 0, -1, 0, 0), 

124 (0, -1, 0, 0, 0, -1, 1, 1, 1), 

125 (0, 1, 0, 0, 0, 1, 1, -1, 0), 

126 (0, 0, -1, 0, -1, 0, -1, 0, 0), 

127 (-1, 1, 0, 0, 0, -1, 0, -1, 0), 

128 (0, 0, 1, 1, -1, 0, 0, 1, 0), 

129 (0, 0, -1, 1, 1, 1, 0, -1, 0), 

130 (-1, 0, 0, 0, 1, 0, 0, -1, -1), 

131 (0, 0, 1, 1, 0, 0, 0, 1, 0), 

132 (0, 0, 1, 0, 1, 0, -1, -1, -1), 

133 (-1, 0, 0, 0, 0, -1, 0, -1, 0), 

134 (0, -1, 0, 0, 0, -1, 1, 0, 0), 

135 (1, 0, 0, 0, 1, 0, 0, 0, 1), 

136 (0, 0, -1, -1, 0, 0, 1, 1, 1), 

137 (0, 0, -1, -1, 0, 0, 0, 1, 0), 

138 (-1, -1, -1, 0, 0, 1, 0, 1, 0), 

139 ], 

140} 

141 

142# XXX The TRI list was generated by looping over all TRI structures in 

143# the COD (Crystallography Open Database) and seeing what operations 

144# were necessary to map all those to standard form. Hence if the 

145# data does not cover all possible inputs, we could miss something. 

146# 

147# Looping over all possible TRI lattices in general would generate 

148# 100+ operations, we don't want to tabulate that. 

149 

150 

151def lattice_loop(latcls, length_grid, angle_grid): 

152 """Yield all lattices defined by the length and angle grids.""" 

153 param_grids = [] 

154 for varname in latcls.parameters: 

155 # Actually we could choose one parameter, a, to always be 1, 

156 # reducing the dimension of the problem by 1. The lattice 

157 # recognition code should do something like that as well, but 

158 # it doesn't. This could affect the impact of the eps value 

159 # on lattice determination, so we just loop over the whole 

160 # thing in order not to worry. 

161 if latcls.name in ['MCL', 'MCLC']: 

162 special_var = 'c' 

163 else: 

164 special_var = 'a' 

165 if varname == special_var: 

166 values = np.ones(1) 

167 elif varname in 'abc': 

168 values = length_grid 

169 elif varname in ['alpha', 'beta', 'gamma']: 

170 values = angle_grid 

171 else: 

172 raise ValueError(varname) 

173 param_grids.append(values) 

174 

175 for latpars in itertools.product(*param_grids): 

176 kwargs = dict(zip(latcls.parameters, latpars)) 

177 try: 

178 lat = latcls(**kwargs) 

179 except (UnconventionalLattice, AssertionError): 

180 # XXX assertion error can happen because cellpar_to_cell 

181 # makes certain assumptions. Should be investigated. 

182 # {'b': 0.1, 'gamma': 60.0, 'c': 0.1, 'a': 1.0, 

183 # 'alpha': 30.0, 'beta': 30.0} <-- this won't work 

184 pass 

185 else: 

186 yield lat 

187 

188 

189def find_niggli_ops(latcls, length_grid, angle_grid): 

190 niggli_ops = {} 

191 

192 for lat in lattice_loop(latcls, length_grid, angle_grid): 

193 cell = lat.tocell() 

194 

195 try: 

196 rcell, op = cell.niggli_reduce() 

197 except RuntimeError: 

198 print('Niggli reduce did not converge') 

199 continue 

200 assert op.dtype == int 

201 op_key = tuple(op.ravel()) 

202 

203 if op_key in niggli_ops: 

204 niggli_ops[op_key] += 1 

205 else: 

206 niggli_ops[op_key] = 1 

207 

208 rcell_test = Cell(op.T @ cell) 

209 rcellpar_test = rcell_test.cellpar() 

210 rcellpar = rcell.cellpar() 

211 err = np.abs(rcellpar_test - rcellpar).max() 

212 assert err < 1e-7, err 

213 

214 return niggli_ops 

215 

216 

217def find_all_niggli_ops(length_grid, angle_grid, lattices=None): 

218 all_niggli_ops = {} 

219 if lattices is None: 

220 lattices = [ 

221 name for name in bravais_names if name not in ['MCL', 'MCLC', 'TRI'] 

222 ] 

223 

224 for latname in lattices: 

225 latcls = bravais_lattices[latname] 

226 print(f'Working on {latname}...') 

227 niggli_ops = find_niggli_ops(latcls, length_grid, angle_grid) 

228 print(f'Found {len(niggli_ops)} ops for {latname}') 

229 for key, count in niggli_ops.items(): 

230 print(f' {np.array(key)!s:>40}: {count}') 

231 print() 

232 all_niggli_ops[latname] = niggli_ops 

233 return all_niggli_ops 

234 

235 

236def generate_niggli_op_table(lattices=None, length_grid=None, angle_grid=None): 

237 

238 if length_grid is None: 

239 length_grid = np.logspace(-0.5, 1.5, 50).round(3) 

240 if angle_grid is None: 

241 angle_grid = np.linspace(10, 179, 50).round() 

242 all_niggli_ops_and_counts = find_all_niggli_ops( 

243 length_grid, angle_grid, lattices=lattices 

244 ) 

245 

246 niggli_op_table = {} 

247 for latname, ops in all_niggli_ops_and_counts.items(): 

248 ops = [op for op in ops if np.abs(op).max() < 2] 

249 niggli_op_table[latname] = ops 

250 

251 import pprint 

252 

253 print(pprint.pformat(niggli_op_table)) 

254 return niggli_op_table 

255 

256 

257# For generation of the table, please see the test_bravais_type_engine 

258# unit test. In case there's any trouble, some legacy code can be 

259# found also in 6e2b1c6cae0ae6ee04638a9887821e7b1a1f2f3f .