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

68 statements  

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

1"""Implements the dimensionality scoring parameter. 

2 

3Method is described in: 

4Definition of a scoring parameter to identify low-dimensional materials 

5components 

6P.M. Larsen, M. Pandey, M. Strange, and K. W. Jacobsen 

7Phys. Rev. Materials 3 034003, 2019 

8https://doi.org/10.1103/PhysRevMaterials.3.034003 

9""" 

10 

11from collections import namedtuple 

12 

13import numpy as np 

14 

15from ase.geometry.dimensionality import rank_determination, topology_scaling 

16from ase.geometry.dimensionality.bond_generator import next_bond 

17 

18KInterval = namedtuple('KInterval', 'dimtype score a b h components cdim') 

19 

20 

21def f(x): 

22 if x == float('inf'): 

23 return 1 

24 k = 1 / 0.15**2 

25 return k * max(0, x - 1) ** 2 / (1.0 + k * max(0, x - 1) ** 2) 

26 

27 

28def calculate_score(a, b): 

29 return f(b) - f(a) 

30 

31 

32def reduced_histogram(h): 

33 h = [int(e > 0) for e in h] 

34 return tuple(h) 

35 

36 

37def build_dimtype(h): 

38 h = reduced_histogram(h) 

39 return ''.join([str(i) for i, e in enumerate(h) if e > 0]) + 'D' 

40 

41 

42def build_kinterval(a, b, h, components, cdim, score=None): 

43 if score is None: 

44 score = calculate_score(a, b) 

45 

46 return KInterval( 

47 dimtype=build_dimtype(h), 

48 score=score, 

49 a=a, 

50 b=b, 

51 h=h, 

52 components=components, 

53 cdim=cdim, 

54 ) 

55 

56 

57def merge_intervals(intervals): 

58 """Merges intervals of the same dimensionality type. 

59 

60 For example, two histograms with component histograms [10, 4, 0, 0] and 

61 [6, 2, 0, 0] are both 01D structures so they will be merged. 

62 

63 Intervals are merged by summing the scores, and taking the minimum and 

64 maximum k-values. Component IDs in the merged interval are taken from the 

65 interval with the highest score. 

66 

67 On rare occasions, intervals to be merged are not adjacent. In this case, 

68 the score of the merged interval is not equal to the score which would be 

69 calculated from its k-interval. This is necessary to maintain the property 

70 that the scores sum to 1. 

71 """ 

72 dimtypes = {e.dimtype for e in intervals} 

73 

74 merged_intervals = [] 

75 for dimtype in dimtypes: 

76 relevant = [e for e in intervals if e.dimtype == dimtype] 

77 combined_score = sum(e.score for e in relevant) 

78 amin = min(e.a for e in relevant) 

79 bmax = max(e.b for e in relevant) 

80 best = max(relevant, key=lambda x: x.score) 

81 merged = build_kinterval( 

82 amin, bmax, best.h, best.components, best.cdim, score=combined_score 

83 ) 

84 merged_intervals.append(merged) 

85 return merged_intervals 

86 

87 

88def build_kintervals(atoms, method_name): 

89 """The interval analysis is performed by inserting bonds one at a time 

90 until the component analysis finds a single component.""" 

91 method = {'RDA': rank_determination.RDA, 'TSA': topology_scaling.TSA}[ 

92 method_name 

93 ] 

94 

95 assert all(e in [0, 1] for e in atoms.pbc) 

96 num_atoms = len(atoms) 

97 intervals = [] 

98 kprev = 0 

99 calc = method(num_atoms) 

100 hprev = calc.check() 

101 components_prev, cdim_prev = calc.get_components() 

102 

103 """ 

104 The end state is a single component, whose dimensionality depends on 

105 the periodic boundary conditions: 

106 """ 

107 end_state = np.zeros(4) 

108 end_dim = sum(atoms.pbc) 

109 end_state[end_dim] = 1 

110 end_state = tuple(end_state) 

111 

112 # Insert each new bond into the component graph. 

113 for k, i, j, offset in next_bond(atoms): 

114 calc.insert_bond(i, j, offset) 

115 h = calc.check() 

116 if h == hprev: # Test if any components were merged 

117 continue 

118 

119 components, cdim = calc.get_components() 

120 

121 # If any components were merged, create a new interval 

122 if k != kprev: 

123 # Only keep intervals of non-zero width 

124 intervals.append( 

125 build_kinterval(kprev, k, hprev, components_prev, cdim_prev) 

126 ) 

127 kprev = k 

128 hprev = h 

129 components_prev = components 

130 cdim_prev = cdim 

131 

132 # Stop once all components are merged 

133 if h == end_state: 

134 intervals.append( 

135 build_kinterval(k, float('inf'), h, components, cdim) 

136 ) 

137 return intervals 

138 

139 

140def analyze_kintervals(atoms, method='RDA', merge=True): 

141 """Performs a k-interval analysis. 

142 

143 In each k-interval the components (connected clusters) are identified. 

144 The intervals are sorted according to the scoring parameter, from high 

145 to low. 

146 

147 Parameters 

148 ---------- 

149 

150 atoms: ASE atoms object 

151 The system to analyze. The periodic boundary conditions determine 

152 the maximum achievable component dimensionality, i.e. pbc=[1,1,0] sets 

153 a maximum dimensionality of 2. 

154 method: string 

155 Analysis method to use, either 'RDA' (default option) or 'TSA'. 

156 These correspond to the Rank Determination Algorithm of Mounet et al. 

157 and the Topological Scaling Algorithm (TSA) of Ashton et al. 

158 merge: boolean 

159 Decides if k-intervals of the same type (e.g. 01D or 3D) should be 

160 merged. Default: true 

161 

162 Returns 

163 ------- 

164 

165 intervals: list 

166 List of KIntervals for each interval identified. A KInterval is a 

167 namedtuple with the following field names: 

168 

169 score: float 

170 Dimensionality score in the range [0, 1] 

171 a: float 

172 The start of the k-interval 

173 b: float 

174 The end of the k-interval 

175 dimtype: str 

176 The dimensionality type 

177 h: tuple 

178 The histogram of the number of components of each dimensionality. 

179 For example, (8, 0, 3, 0) means eight 0D and three 2D components. 

180 components: array 

181 The component ID of each atom. 

182 cdim: dict 

183 The component dimensionalities 

184 """ 

185 intervals = build_kintervals(atoms, method) 

186 if merge: 

187 intervals = merge_intervals(intervals) 

188 

189 # Sort intervals by score. Interval width resolves ambiguity when score=0. 

190 return sorted(intervals, reverse=True, key=lambda x: (x.score, x.b - x.a))