Coverage for ase/build/ribbon.py: 90.00%
80 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1from math import sqrt
3import numpy as np
5from ase.atoms import Atoms
8def graphene_nanoribbon(
9 n,
10 m,
11 type='zigzag',
12 saturated=False,
13 C_H=1.09,
14 C_C=1.42,
15 vacuum=None,
16 magnetic=False,
17 initial_mag=1.12,
18 sheet=False,
19 main_element='C',
20 saturate_element='H',
21):
22 """Create a graphene nanoribbon.
24 Creates a graphene nanoribbon in the x-z plane, with the nanoribbon
25 running along the z axis.
27 Parameters
28 ----------
30 n: int
31 The width of the nanoribbon. For armchair nanoribbons, this
32 n may be half-integer to repeat by half a cell.
33 m: int
34 The length of the nanoribbon.
35 type: str
36 The orientation of the ribbon. Must be either 'zigzag'
37 or 'armchair'.
38 saturated: bool
39 If true, hydrogen atoms are placed along the edge.
40 C_H: float
41 Carbon-hydrogen bond length. Default: 1.09 Angstrom.
42 C_C: float
43 Carbon-carbon bond length. Default: 1.42 Angstrom.
44 vacuum: None (default) or float
45 Amount of vacuum added to non-periodic directions, if present.
46 magnetic: bool
47 Make the edges magnetic.
48 initial_mag: float
49 Magnitude of magnetic moment if magnetic.
50 sheet: bool
51 If true, make an infinite sheet instead of a ribbon (default: False)
53 Examples
54 --------
55 >>> from ase.build import graphene_nanoribbon
56 >>> gnr1 = graphene_nanoribbon(3, 4, type='armchair', saturated=True,
57 ... vacuum=3.5)
58 >>> gnr2 = graphene_nanoribbon(2, 6, type='zigzag', saturated=True,
59 ... C_H=1.1, C_C=1.4, vacuum=3.0,
60 ... magnetic=True, initial_mag=1.12)
61 """
63 if m % 1 != 0:
64 raise ValueError('m must be integer')
65 if type == 'zigzag' and n % 1 != 0:
66 raise ValueError('n must be an integer for zigzag ribbons')
68 b = sqrt(3) * C_C / 4
69 arm_unit = Atoms(
70 main_element + '4', pbc=(1, 0, 1), cell=[4 * b, 0, 3 * C_C]
71 )
72 arm_unit.positions = [
73 [0, 0, 0],
74 [b * 2, 0, C_C / 2.0],
75 [b * 2, 0, 3 * C_C / 2.0],
76 [0, 0, 2 * C_C],
77 ]
78 arm_unit_half = Atoms(
79 main_element + '2', pbc=(1, 0, 1), cell=[2 * b, 0, 3 * C_C]
80 )
81 arm_unit_half.positions = [[b * 2, 0, C_C / 2.0], [b * 2, 0, 3 * C_C / 2.0]]
82 zz_unit = Atoms(
83 main_element + '2', pbc=(1, 0, 1), cell=[3 * C_C / 2.0, 0, b * 4]
84 )
85 zz_unit.positions = [[0, 0, 0], [C_C / 2.0, 0, b * 2]]
86 atoms = Atoms()
88 if type == 'zigzag':
89 edge_index0 = np.arange(m) * 2
90 edge_index1 = (n - 1) * m * 2 + np.arange(m) * 2 + 1
92 if magnetic:
93 mms = np.zeros(m * n * 2)
94 for i in edge_index0:
95 mms[i] = initial_mag
96 for i in edge_index1:
97 mms[i] = -initial_mag
99 for i in range(n):
100 layer = zz_unit.repeat((1, 1, m))
101 layer.positions[:, 0] += 3 * C_C / 2 * i
102 if i % 2 == 1:
103 layer.positions[:, 2] += 2 * b
104 layer[-1].position[2] -= b * 4 * m
105 atoms += layer
107 xmin = atoms.positions[0, 0]
109 if magnetic:
110 atoms.set_initial_magnetic_moments(mms)
111 if saturated:
112 H_atoms0 = Atoms(saturate_element + str(m))
113 H_atoms0.positions = atoms[edge_index0].positions
114 H_atoms0.positions[:, 0] -= C_H
115 H_atoms1 = Atoms(saturate_element + str(m))
116 H_atoms1.positions = atoms[edge_index1].positions
117 H_atoms1.positions[:, 0] += C_H
118 atoms += H_atoms0 + H_atoms1
119 atoms.cell = [n * 3 * C_C / 2, 0, m * 4 * b]
121 elif type == 'armchair':
122 n *= 2
123 n_int = int(round(n))
124 if abs(n_int - n) > 1e-10:
125 raise ValueError(
126 'The argument n has to be half-integer for armchair ribbons.'
127 )
128 n = n_int
130 for i in range(n // 2):
131 layer = arm_unit.repeat((1, 1, m))
132 layer.positions[:, 0] -= 4 * b * i
133 atoms += layer
134 if n % 2:
135 layer = arm_unit_half.repeat((1, 1, m))
136 layer.positions[:, 0] -= 4 * b * (n // 2)
137 atoms += layer
139 xmin = atoms.positions[-1, 0]
141 if saturated:
142 if n % 2:
143 arm_right_saturation = Atoms(
144 saturate_element + '2',
145 pbc=(1, 0, 1),
146 cell=[2 * b, 0, 3 * C_C],
147 )
148 arm_right_saturation.positions = [
149 [-sqrt(3) / 2 * C_H, 0, C_C / 2 - C_H * 0.5],
150 [-sqrt(3) / 2 * C_H, 0, 3 * C_C / 2.0 + C_H * 0.5],
151 ]
152 else:
153 arm_right_saturation = Atoms(
154 saturate_element + '2',
155 pbc=(1, 0, 1),
156 cell=[4 * b, 0, 3 * C_C],
157 )
158 arm_right_saturation.positions = [
159 [-sqrt(3) / 2 * C_H, 0, C_H * 0.5],
160 [-sqrt(3) / 2 * C_H, 0, 2 * C_C - C_H * 0.5],
161 ]
162 arm_left_saturation = Atoms(
163 saturate_element + '2', pbc=(1, 0, 1), cell=[4 * b, 0, 3 * C_C]
164 )
165 arm_left_saturation.positions = [
166 [b * 2 + sqrt(3) / 2 * C_H, 0, C_C / 2 - C_H * 0.5],
167 [b * 2 + sqrt(3) / 2 * C_H, 0, 3 * C_C / 2.0 + C_H * 0.5],
168 ]
169 arm_right_saturation.positions[:, 0] -= 4 * b * (n / 2.0 - 1)
171 atoms += arm_right_saturation.repeat((1, 1, m))
172 atoms += arm_left_saturation.repeat((1, 1, m))
174 atoms.cell = [b * 4 * n / 2.0, 0, 3 * C_C * m]
176 atoms.set_pbc([sheet, False, True])
178 # The ribbon was 'built' from x=0 towards negative x.
179 # Move the ribbon to positive x:
180 atoms.positions[:, 0] -= xmin
181 if not sheet:
182 atoms.cell[0] = 0.0
183 if vacuum is not None:
184 atoms.center(vacuum, axis=1)
185 if not sheet:
186 atoms.center(vacuum, axis=0)
187 return atoms