Coverage for /builds/ase/ase/ase/cluster/hexagonal.py: 76.74%

43 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-08-02 00:12 +0000

1# fmt: off 

2 

3""" 

4Function-like objects that creates cubic clusters. 

5""" 

6 

7import numpy as np 

8 

9from ase.cluster.factory import ClusterFactory 

10from ase.data import reference_states as _refstate 

11 

12 

13class HexagonalFactory(ClusterFactory): 

14 spacegroup = 191 

15 

16 xtal_name = 'hexagonal' 

17 

18 def get_lattice_constant(self): 

19 "Get the lattice constant of an element with cubic crystal structure." 

20 symmetry = _refstate[self.atomic_numbers[0]]['symmetry'] 

21 if symmetry != self.xtal_name: 

22 raise ValueError(f"Cannot guess the {self.xtal_name} " + 

23 "lattice constant of an element with crystal " + 

24 f"structure {symmetry}.") 

25 return _refstate[self.atomic_numbers[0]].copy() 

26 

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 

42 self.lattice_constant = (a, c) 

43 self.lattice_basis = np.array([[a, 0., 0.], 

44 [-a / 2., a * np.sqrt(3.) / 2., 0.], 

45 [0., 0., c]]) 

46 self.resiproc_basis = self.get_resiproc_basis(self.lattice_basis) 

47 

48 def set_surfaces_layers(self, surfaces, layers): 

49 for i, s in enumerate(surfaces): 

50 if len(s) == 4: 

51 (a, b, c, d) = s 

52 if a + b + c != 0: 

53 raise ValueError( 

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

55 "index, as the sum of the first three numbers " 

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

57 surfaces[i] = [a, b, d] 

58 

59 ClusterFactory.set_surfaces_layers(self, surfaces, layers) 

60 

61 

62Hexagonal = HexagonalFactory() 

63 

64 

65class HexagonalClosedPackedFactory(HexagonalFactory): 

66 """A factory for creating HCP clusters.""" 

67 spacegroup = 194 

68 

69 xtal_name = 'hcp' 

70 

71 atomic_basis = np.array([[0., 0., 0.], 

72 [1. / 3., 2. / 3., .5]]) 

73 

74 

75HexagonalClosedPacked = HexagonalClosedPackedFactory() 

76 

77 

78class GraphiteFactory(HexagonalFactory): 

79 """A factory for creating graphite clusters.""" 

80 xtal_name = "graphite" 

81 

82 atomic_basis = np.array([[0., 0., 0.], 

83 [1. / 3., 2. / 3., 0.], 

84 [1. / 3., 2. / 3., .5], 

85 [2. / 3., 1. / 3., .5]]) 

86 

87 

88Graphite = GraphiteFactory()