Coverage for /builds/kinetik161/ase/ase/lattice/hexagonal.py: 74.14%

58 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +0000

1"""Function-like object creating hexagonal lattices. 

2 

3The following lattice creators are defined: 

4 

5* Hexagonal 

6* HexagonalClosedPacked 

7* Graphite 

8* Graphene 

9 

10Example for using Graphene to create atoms object gra:: 

11 

12 from ase.lattice.hexagonal import * 

13 import ase.io as io 

14 from ase import Atoms, Atom 

15 

16 index1=6 

17 index2=7 

18 mya = 2.45 

19 myc = 20.0 

20 

21 gra = Graphene(symbol = 'C',latticeconstant={'a':mya,'c':myc}, 

22 size=(index1,index2,1)) 

23 io.write('test.xyz', gra, format='xyz') 

24""" 

25 

26from ase.lattice.triclinic import TriclinicFactory 

27 

28 

29class HexagonalFactory(TriclinicFactory): 

30 "A factory for creating simple hexagonal lattices." 

31 # The name of the crystal structure in ChemicalElements 

32 xtal_name = "hexagonal" 

33 

34 def make_crystal_basis(self): 

35 """Make the basis matrix for the crystal and system unit cells.""" 

36 # First convert the basis specification to a triclinic one 

37 if isinstance(self.latticeconstant, type({})): 

38 self.latticeconstant['alpha'] = 90 

39 self.latticeconstant['beta'] = 90 

40 self.latticeconstant['gamma'] = 120 

41 self.latticeconstant['b/a'] = 1.0 

42 else: 

43 if len(self.latticeconstant) == 2: 

44 a, c = self.latticeconstant 

45 self.latticeconstant = (a, a, c, 90, 90, 120) 

46 else: 

47 raise ValueError( 

48 "Improper lattice constants for hexagonal crystal.") 

49 TriclinicFactory.make_crystal_basis(self) 

50 

51 def find_directions(self, directions, miller): 

52 """Find missing directions and miller indices from the specified ones. 

53 

54 Also handles the conversion of hexagonal-style 4-index notation to 

55 the normal 3-index notation. 

56 """ 

57 directions = list(directions) 

58 miller = list(miller) 

59 if miller != [None, None, None]: 

60 raise NotImplementedError( 

61 'Specifying Miller indices of surfaces currently ' 

62 'broken for hexagonal crystals.') 

63 for obj in (directions, miller): 

64 for i in range(3): 

65 if obj[i] is not None: 

66 (a, b, c, d) = obj[i] 

67 if a + b + c != 0: 

68 raise ValueError( 

69 ("(%d,%d,%d,%d) is not a valid hexagonal Miller " + 

70 "index, as the sum of the first three numbers " + 

71 "should be zero.") % (a, b, c, d)) 

72 x = 4 * a + 2 * b 

73 y = 2 * a + 4 * b 

74 z = 3 * d 

75 obj[i] = (x, y, z) 

76 TriclinicFactory.find_directions(self, directions, miller) 

77 

78 def print_directions_and_miller(self, txt=""): 

79 "Print direction vectors and Miller indices." 

80 print(f"Direction vectors of unit cell{txt}:") 

81 for i in (0, 1, 2): 

82 self.print_four_vector("[]", self.directions[i]) 

83 print(f"Miller indices of surfaces{txt}:") 

84 for i in (0, 1, 2): 

85 self.print_four_vector("()", self.miller[i]) 

86 

87 def print_four_vector(self, bracket, numbers): 

88 bra, ket = bracket 

89 (x, y, z) = numbers 

90 a = 2 * x - y 

91 b = -x + 2 * y 

92 c = -x - y 

93 d = 2 * z 

94 print(" %s%d, %d, %d%s ~ %s%d, %d, %d, %d%s" % 

95 (bra, x, y, z, ket, bra, a, b, c, d, ket)) 

96 

97 

98Hexagonal = HexagonalFactory() 

99 

100 

101class HexagonalClosedPackedFactory(HexagonalFactory): 

102 "A factory for creating HCP lattices." 

103 xtal_name = "hcp" 

104 bravais_basis = [[0, 0, 0], [1.0 / 3.0, 2.0 / 3.0, 0.5]] 

105 

106 

107HexagonalClosedPacked = HexagonalClosedPackedFactory() 

108 

109 

110class GraphiteFactory(HexagonalFactory): 

111 "A factory for creating graphite lattices." 

112 xtal_name = "graphite" 

113 bravais_basis = [[0, 0, 0], [1.0 / 3.0, 2.0 / 3.0, 0], 

114 [1.0 / 3.0, 2.0 / 3.0, 0.5], [2.0 / 3.0, 1.0 / 3.0, 0.5]] 

115 

116 

117Graphite = GraphiteFactory() 

118 

119 

120class GrapheneFactory(HexagonalFactory): 

121 "A factory for creating graphene lattices." 

122 xtal_name = "graphene" 

123 bravais_basis = [[0, 0, 0], [1.0 / 3.0, 2.0 / 3.0, 0]] 

124 

125 

126Graphene = GrapheneFactory()