Coverage for ase/geometry/dimensionality/topology_scaling.py: 100.00%

43 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1"""Implements the Topology-Scaling Algorithm (TSA) 

2 

3Method is described in: 

4Topology-Scaling Identification of Layered Solids and Stable Exfoliated 

52D Materials 

6M. Ashton, J. Paul, S.B. Sinnott, and R.G. Hennig 

7Phys. Rev. Lett. 118, 106101 

82017 

9 

10 

11A disjoint set is used here to allow insertion of bonds one at a time. 

12This permits k-interval analysis. 

13""" 

14 

15import itertools 

16 

17import numpy as np 

18 

19from ase.geometry.dimensionality.disjoint_set import DisjointSet 

20 

21 

22class TSA: 

23 def __init__(self, num_atoms, n=2): 

24 """Initializes the TSA class. 

25 

26 A disjoint set is maintained for the single cell and for the supercell. 

27 For some materials, such as interpenetrating networks, 

28 the dimensionality classification is dependent on the size of the 

29 initial cell. 

30 

31 Parameters 

32 ---------- 

33 

34 num_atoms: int The number of atoms in the unit cell. 

35 n: int The number size of the (n, n, n) periodic supercell. 

36 """ 

37 self.n = n 

38 self.num_atoms = num_atoms 

39 self.gsingle = DisjointSet(num_atoms) 

40 self.gsuper = DisjointSet(num_atoms * n**3) 

41 

42 self.m = [1, n, n**2] 

43 self.cells = np.array(list(itertools.product(range(n), repeat=3))) 

44 self.offsets = num_atoms * np.dot(self.m, self.cells.T) 

45 

46 def insert_bond(self, i, j, offset): 

47 """Inserts a bond into the component graph, both in the single cell and 

48 each of the n^3 subcells of the supercell. 

49 

50 Parameters 

51 ---------- 

52 

53 i: int The index of the first atom. 

54 n: int The index of the second atom. 

55 offset: tuple The cell offset of the second atom. 

56 """ 

57 nbr_cells = (self.cells + offset) % self.n 

58 nbr_offsets = self.num_atoms * np.dot(self.m, nbr_cells.T) 

59 

60 self.gsingle.union(i, j) 

61 for a, b in zip(self.offsets, nbr_offsets): 

62 self.gsuper.union(a + i, b + j) 

63 self.gsuper.union(b + i, a + j) 

64 

65 def _get_component_dimensionalities(self): 

66 

67 n = self.n 

68 offsets = self.offsets 

69 single_roots = np.unique(self.gsingle.find_all()) 

70 super_components = self.gsuper.find_all() 

71 

72 component_dim = {} 

73 for i in single_roots: 

74 num_clusters = len(np.unique(super_components[offsets + i])) 

75 dim = {n**3: 0, n**2: 1, n: 2, 1: 3}[num_clusters] 

76 component_dim[i] = dim 

77 return component_dim 

78 

79 def check(self): 

80 """Determines the dimensionality histogram. 

81 

82 Returns 

83 ------- 

84 hist : tuple Dimensionality histogram. 

85 """ 

86 cdim = self._get_component_dimensionalities() 

87 hist = np.zeros(4).astype(int) 

88 bc = np.bincount(list(cdim.values())) 

89 hist[: len(bc)] = bc 

90 return tuple(hist) 

91 

92 def get_components(self): 

93 """Determines the dimensionality and constituent atoms of each 

94 component. 

95 

96 Returns 

97 ------- 

98 components: array The component ID every atom 

99 """ 

100 relabelled_dim = {} 

101 relabelled_components = self.gsingle.find_all(relabel=True) 

102 cdim = self._get_component_dimensionalities() 

103 for k, v in cdim.items(): 

104 relabelled_dim[relabelled_components[k]] = v 

105 

106 return relabelled_components, relabelled_dim