Coverage for ase/lattice/hexagonal.py: 74.14%
58 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"""Function-like object creating hexagonal lattices.
3The following lattice creators are defined:
5* Hexagonal
6* HexagonalClosedPacked
7* Graphite
8* Graphene
10Example for using Graphene to create atoms object gra::
12 from ase.lattice.hexagonal import *
13 import ase.io as io
14 from ase import Atoms, Atom
16 index1=6
17 index2=7
18 mya = 2.45
19 myc = 20.0
21 gra = Graphene(symbol = 'C',latticeconstant={'a':mya,'c':myc},
22 size=(index1,index2,1))
23 io.write('test.xyz', gra, format='xyz')
24"""
26from ase.lattice.triclinic import TriclinicFactory
29class HexagonalFactory(TriclinicFactory):
30 "A factory for creating simple hexagonal lattices."
32 # The name of the crystal structure in ChemicalElements
33 xtal_name = 'hexagonal'
35 def make_crystal_basis(self):
36 """Make the basis matrix for the crystal and system unit cells."""
37 # First convert the basis specification to a triclinic one
38 if isinstance(self.latticeconstant, type({})):
39 self.latticeconstant['alpha'] = 90
40 self.latticeconstant['beta'] = 90
41 self.latticeconstant['gamma'] = 120
42 self.latticeconstant['b/a'] = 1.0
43 else:
44 if len(self.latticeconstant) == 2:
45 a, c = self.latticeconstant
46 self.latticeconstant = (a, a, c, 90, 90, 120)
47 else:
48 raise ValueError(
49 'Improper lattice constants for hexagonal crystal.'
50 )
51 TriclinicFactory.make_crystal_basis(self)
53 def find_directions(self, directions, miller):
54 """Find missing directions and miller indices from the specified ones.
56 Also handles the conversion of hexagonal-style 4-index notation to
57 the normal 3-index notation.
58 """
59 directions = list(directions)
60 miller = list(miller)
61 if miller != [None, None, None]:
62 raise NotImplementedError(
63 'Specifying Miller indices of surfaces currently '
64 'broken for hexagonal crystals.'
65 )
66 for obj in (directions, miller):
67 for i in range(3):
68 if obj[i] is not None:
69 (a, b, c, d) = obj[i]
70 if a + b + c != 0:
71 raise ValueError(
72 (
73 '(%d,%d,%d,%d) is not a valid hexagonal Miller '
74 'index, as the sum of the first three numbers '
75 'should be zero.'
76 )
77 % (a, b, c, d)
78 )
79 x = 4 * a + 2 * b
80 y = 2 * a + 4 * b
81 z = 3 * d
82 obj[i] = (x, y, z)
83 TriclinicFactory.find_directions(self, directions, miller)
85 def print_directions_and_miller(self, txt=''):
86 "Print direction vectors and Miller indices."
87 print(f'Direction vectors of unit cell{txt}:')
88 for i in (0, 1, 2):
89 self.print_four_vector('[]', self.directions[i])
90 print(f'Miller indices of surfaces{txt}:')
91 for i in (0, 1, 2):
92 self.print_four_vector('()', self.miller[i])
94 def print_four_vector(self, bracket, numbers):
95 bra, ket = bracket
96 (x, y, z) = numbers
97 a = 2 * x - y
98 b = -x + 2 * y
99 c = -x - y
100 d = 2 * z
101 print(
102 ' %s%d, %d, %d%s ~ %s%d, %d, %d, %d%s'
103 % (bra, x, y, z, ket, bra, a, b, c, d, ket)
104 )
107Hexagonal = HexagonalFactory()
110class HexagonalClosedPackedFactory(HexagonalFactory):
111 "A factory for creating HCP lattices."
113 xtal_name = 'hcp'
114 bravais_basis = [[0, 0, 0], [1.0 / 3.0, 2.0 / 3.0, 0.5]]
117HexagonalClosedPacked = HexagonalClosedPackedFactory()
120class GraphiteFactory(HexagonalFactory):
121 "A factory for creating graphite lattices."
123 xtal_name = 'graphite'
124 bravais_basis = [
125 [0, 0, 0],
126 [1.0 / 3.0, 2.0 / 3.0, 0],
127 [1.0 / 3.0, 2.0 / 3.0, 0.5],
128 [2.0 / 3.0, 1.0 / 3.0, 0.5],
129 ]
132Graphite = GraphiteFactory()
135class GrapheneFactory(HexagonalFactory):
136 "A factory for creating graphene lattices."
138 xtal_name = 'graphene'
139 bravais_basis = [[0, 0, 0], [1.0 / 3.0, 2.0 / 3.0, 0]]
142Graphene = GrapheneFactory()