Coverage for ase/gui/nanotube.py: 98.04%
51 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
1"""Window for setting up Carbon nanotubes and similar tubes."""
3import ase.gui.ui as ui
4from ase.build import nanotube
5from ase.gui.i18n import _
6from ase.gui.widgets import Element, pybutton
8introtext = _("""\
9Set up a Carbon nanotube by specifying the (n,m) roll-up vector.
10Please note that m <= n.
12Nanotubes of other elements can be made by specifying the element
13and bond length.\
14""")
16py_template = """\
17from ase.build import nanotube
18atoms = nanotube({n}, {m}, length={length}, bond={bl:.3f}, symbol='{symb}')
19"""
21label_template = _(
22 '{natoms} atoms, diameter: {diameter:.3f} Å, '
23 'total length: {total_length:.3f} Å'
24)
27class SetupNanotube:
28 """Window for setting up a (Carbon) nanotube."""
30 def __init__(self, gui):
31 self.element = Element('C', self.make)
32 self.bondlength = ui.SpinBox(1.42, 0.0, 10.0, 0.01, self.make)
33 self.n = ui.SpinBox(5, 1, 100, 1, self.make)
34 self.m = ui.SpinBox(5, 0, 100, 1, self.make)
35 self.length = ui.SpinBox(1, 1, 100, 1, self.make)
36 self.description = ui.Label('')
38 win = self.win = ui.Window(_('Nanotube'))
39 win.add(ui.Text(introtext))
40 win.add(self.element)
41 win.add([_('Bond length: '), self.bondlength, _('Å')])
42 win.add(_('Select roll-up vector (n,m) and tube length:'))
43 win.add(['n:', self.n, 'm:', self.m, _('Length:'), self.length])
44 win.add(self.description)
45 win.add(
46 [
47 pybutton(_('Creating a nanoparticle.'), self.make),
48 ui.Button(_('Apply'), self.apply),
49 ui.Button(_('OK'), self.ok),
50 ]
51 )
53 self.gui = gui
54 self.atoms = None
56 def make(self, element=None):
57 symbol = self.element.symbol
58 if symbol is None:
59 self.atoms = None
60 self.python = None
61 self.description.text = ''
62 return None
64 n = self.n.value
65 m = self.m.value
66 length = self.length.value
67 bl = self.bondlength.value
68 self.atoms = nanotube(n, m, length=length, bond=bl, symbol=symbol)
69 label = label_template.format(
70 natoms=len(self.atoms),
71 total_length=self.atoms.cell[2, 2],
72 diameter=self.atoms.cell[0, 0] / 2,
73 )
74 self.description.text = label
75 return py_template.format(n=n, m=m, length=length, symb=symbol, bl=bl)
77 def apply(self):
78 self.make()
79 if self.atoms is not None:
80 self.gui.new_atoms(self.atoms)
81 self.gui.clear_history()
82 return True
83 else:
84 ui.error(
85 _('No valid atoms.'),
86 _(
87 'You have not (yet) specified a consistent '
88 'set of parameters.'
89 ),
90 )
91 return False
93 def ok(self, *args):
94 if self.apply():
95 self.win.close()