Coverage for ase/cluster/hexagonal.py: 76.74%
43 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""
2Function-like objects that creates cubic clusters.
3"""
5import numpy as np
7from ase.cluster.factory import ClusterFactory
8from ase.data import reference_states as _refstate
11class HexagonalFactory(ClusterFactory):
12 spacegroup = 191
14 xtal_name = 'hexagonal'
16 def get_lattice_constant(self):
17 "Get the lattice constant of an element with cubic crystal structure."
18 symmetry = _refstate[self.atomic_numbers[0]]['symmetry']
19 if symmetry != self.xtal_name:
20 raise ValueError(
21 f'Cannot guess the {self.xtal_name} '
22 + 'lattice constant of an element with crystal '
23 + f'structure {symmetry}.'
24 )
25 return _refstate[self.atomic_numbers[0]].copy()
27 def set_basis(self):
28 lattice = self.lattice_constant
29 if isinstance(lattice, dict):
30 a = lattice['a']
31 try:
32 c = lattice['c']
33 except KeyError:
34 c = a * lattice['c/a']
35 else:
36 if len(lattice) == 2:
37 (a, c) = lattice
38 else:
39 raise ValueError(
40 f'Improper lattice constants for {self.xtal_name} crystal.'
41 )
43 self.lattice_constant = (a, c)
44 self.lattice_basis = np.array(
45 [
46 [a, 0.0, 0.0],
47 [-a / 2.0, a * np.sqrt(3.0) / 2.0, 0.0],
48 [0.0, 0.0, c],
49 ]
50 )
51 self.resiproc_basis = self.get_resiproc_basis(self.lattice_basis)
53 def set_surfaces_layers(self, surfaces, layers):
54 for i, s in enumerate(surfaces):
55 if len(s) == 4:
56 (a, b, c, d) = s
57 if a + b + c != 0:
58 raise ValueError(
59 '(%d,%d,%d,%d) is not a valid hexagonal Miller '
60 'index, as the sum of the first three numbers '
61 'should be zero.' % (a, b, c, d)
62 )
63 surfaces[i] = [a, b, d]
65 ClusterFactory.set_surfaces_layers(self, surfaces, layers)
68Hexagonal = HexagonalFactory()
71class HexagonalClosedPackedFactory(HexagonalFactory):
72 """A factory for creating HCP clusters."""
74 spacegroup = 194
76 xtal_name = 'hcp'
78 atomic_basis = np.array([[0.0, 0.0, 0.0], [1.0 / 3.0, 2.0 / 3.0, 0.5]])
81HexagonalClosedPacked = HexagonalClosedPackedFactory()
84class GraphiteFactory(HexagonalFactory):
85 """A factory for creating graphite clusters."""
87 xtal_name = 'graphite'
89 atomic_basis = np.array(
90 [
91 [0.0, 0.0, 0.0],
92 [1.0 / 3.0, 2.0 / 3.0, 0.0],
93 [1.0 / 3.0, 2.0 / 3.0, 0.5],
94 [2.0 / 3.0, 1.0 / 3.0, 0.5],
95 ]
96 )
99Graphite = GraphiteFactory()