Coverage for ase/build/tube.py: 88.24%

119 statements  

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

1from math import gcd, sqrt 

2 

3import numpy as np 

4 

5from ase.atoms import Atoms 

6 

7 

8def nanotube(n, m, length=1, bond=1.42, symbol='C', verbose=False, vacuum=None): 

9 """Create an atomic structure. 

10 

11 Creates a single-walled nanotube whose structure is specified using the 

12 standardized (n, m) notation. 

13 

14 Parameters 

15 ---------- 

16 n : int 

17 n in the (n, m) notation. 

18 m : int 

19 m in the (n, m) notation. 

20 length : int, optional 

21 Length (axial repetitions) of the nanotube. 

22 bond : float, optional 

23 Bond length between neighboring atoms. 

24 symbol : str, optional 

25 Chemical element to construct the nanotube from. 

26 verbose : bool, optional 

27 If True, will display key geometric parameters. 

28 

29 Returns 

30 ------- 

31 ase.atoms.Atoms 

32 An ASE Atoms object corresponding to the specified molecule. 

33 

34 Examples 

35 -------- 

36 >>> from ase.build import nanotube 

37 >>> atoms1 = nanotube(6, 0, length=4) 

38 >>> atoms2 = nanotube(3, 3, length=6, bond=1.4, symbol='Si') 

39 """ 

40 if n < m: 

41 m, n = n, m 

42 sign = -1 

43 else: 

44 sign = 1 

45 

46 nk = 6000 

47 sq3 = sqrt(3.0) 

48 a = sq3 * bond 

49 l2 = n * n + m * m + n * m 

50 l1 = sqrt(l2) 

51 

52 nd = gcd(n, m) 

53 if (n - m) % (3 * nd) == 0: 

54 ndr = 3 * nd 

55 else: 

56 ndr = nd 

57 

58 nr = (2 * m + n) // ndr 

59 ns = -(2 * n + m) // ndr 

60 nn = 2 * l2 // ndr 

61 

62 ichk = 0 

63 if nr == 0: 

64 n60 = 1 

65 else: 

66 n60 = nr * 4 

67 

68 absn = abs(n60) 

69 nnp = [] 

70 nnq = [] 

71 for i in range(-absn, absn + 1): 

72 for j in range(-absn, absn + 1): 

73 j2 = nr * j - ns * i 

74 if j2 == 1: 

75 j1 = m * i - n * j 

76 if j1 > 0 and j1 < nn: 

77 ichk += 1 

78 nnp.append(i) 

79 nnq.append(j) 

80 

81 if ichk == 0: 

82 raise RuntimeError('not found p, q strange!!') 

83 if ichk >= 2: 

84 raise RuntimeError('more than 1 pair p, q strange!!') 

85 

86 nnnp = nnp[0] 

87 nnnq = nnq[0] 

88 

89 if verbose: 

90 print('the symmetry vector is', nnnp, nnnq) 

91 

92 lp = nnnp * nnnp + nnnq * nnnq + nnnp * nnnq 

93 r = a * sqrt(lp) 

94 c = a * l1 

95 t = sq3 * c / ndr 

96 

97 if 2 * nn > nk: 

98 raise RuntimeError('parameter nk is too small!') 

99 

100 rs = c / (2.0 * np.pi) 

101 

102 if verbose: 

103 print('radius=', rs, t) 

104 

105 q1 = np.arctan((sq3 * m) / (2 * n + m)) 

106 q2 = np.arctan((sq3 * nnnq) / (2 * nnnp + nnnq)) 

107 q3 = q1 - q2 

108 

109 q4 = 2.0 * np.pi / nn 

110 q5 = bond * np.cos((np.pi / 6.0) - q1) / c * 2.0 * np.pi 

111 

112 h1 = abs(t) / abs(np.sin(q3)) 

113 h2 = bond * np.sin((np.pi / 6.0) - q1) 

114 

115 ii = 0 

116 x, y, z = [], [], [] 

117 for i in range(nn): 

118 x1, y1, z1 = 0, 0, 0 

119 

120 k = np.floor(i * abs(r) / h1) 

121 x1 = rs * np.cos(i * q4) 

122 y1 = rs * np.sin(i * q4) 

123 z1 = (i * abs(r) - k * h1) * np.sin(q3) 

124 kk2 = abs(np.floor((z1 + 0.0001) / t)) 

125 if z1 >= t - 0.0001: 

126 z1 -= t * kk2 

127 elif z1 < 0: 

128 z1 += t * kk2 

129 ii += 1 

130 

131 x.append(x1) 

132 y.append(y1) 

133 z.append(z1) 

134 z3 = (i * abs(r) - k * h1) * np.sin(q3) - h2 

135 ii += 1 

136 

137 if z3 >= 0 and z3 < t: 

138 x2 = rs * np.cos(i * q4 + q5) 

139 y2 = rs * np.sin(i * q4 + q5) 

140 z2 = (i * abs(r) - k * h1) * np.sin(q3) - h2 

141 x.append(x2) 

142 y.append(y2) 

143 z.append(z2) 

144 else: 

145 x2 = rs * np.cos(i * q4 + q5) 

146 y2 = rs * np.sin(i * q4 + q5) 

147 z2 = (i * abs(r) - (k + 1) * h1) * np.sin(q3) - h2 

148 kk = abs(np.floor(z2 / t)) 

149 if z2 >= t - 0.0001: 

150 z2 -= t * kk 

151 elif z2 < 0: 

152 z2 += t * kk 

153 x.append(x2) 

154 y.append(y2) 

155 z.append(z2) 

156 

157 ntotal = 2 * nn 

158 X = [] 

159 for i in range(ntotal): 

160 X.append([x[i], y[i], sign * z[i]]) 

161 

162 if length > 1: 

163 xx = X[:] 

164 for mnp in range(2, length + 1): 

165 for i in range(len(xx)): 

166 X.append(xx[i][:2] + [xx[i][2] + (mnp - 1) * t]) 

167 

168 transvec = t 

169 numatom = ntotal * length 

170 diameter = rs * 2 

171 chiralangle = np.arctan((sq3 * n) / (2 * m + n)) / np.pi * 180 

172 

173 cell = [[0, 0, 0], [0, 0, 0], [0, 0, length * t]] 

174 atoms = Atoms( 

175 symbol + str(numatom), positions=X, cell=cell, pbc=[False, False, True] 

176 ) 

177 if vacuum: 

178 atoms.center(vacuum, axis=(0, 1)) 

179 if verbose: 

180 print('translation vector =', transvec) 

181 print('diameter = ', diameter) 

182 print('chiral angle = ', chiralangle) 

183 return atoms