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

43 statements  

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

1""" 

2Function-like objects that creates cubic clusters. 

3""" 

4 

5import numpy as np 

6 

7from ase.cluster.factory import ClusterFactory 

8from ase.data import reference_states as _refstate 

9 

10 

11class HexagonalFactory(ClusterFactory): 

12 spacegroup = 191 

13 

14 xtal_name = 'hexagonal' 

15 

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(f"Cannot guess the {self.xtal_name} " + 

21 "lattice constant of an element with crystal " + 

22 f"structure {symmetry}.") 

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

24 

25 def set_basis(self): 

26 lattice = self.lattice_constant 

27 if isinstance(lattice, dict): 

28 a = lattice['a'] 

29 try: 

30 c = lattice['c'] 

31 except KeyError: 

32 c = a * lattice['c/a'] 

33 else: 

34 if len(lattice) == 2: 

35 (a, c) = lattice 

36 else: 

37 raise ValueError( 

38 f"Improper lattice constants for {self.xtal_name} crystal.") 

39 

40 self.lattice_constant = (a, c) 

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

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

43 [0., 0., c]]) 

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

45 

46 def set_surfaces_layers(self, surfaces, layers): 

47 for i, s in enumerate(surfaces): 

48 if len(s) == 4: 

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

50 if a + b + c != 0: 

51 raise ValueError( 

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

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

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

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

56 

57 ClusterFactory.set_surfaces_layers(self, surfaces, layers) 

58 

59 

60Hexagonal = HexagonalFactory() 

61 

62 

63class HexagonalClosedPackedFactory(HexagonalFactory): 

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

65 spacegroup = 194 

66 

67 xtal_name = 'hcp' 

68 

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

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

71 

72 

73HexagonalClosedPacked = HexagonalClosedPackedFactory() 

74 

75 

76class GraphiteFactory(HexagonalFactory): 

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

78 xtal_name = "graphite" 

79 

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

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

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

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

84 

85 

86Graphite = GraphiteFactory()