Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Window for setting up Carbon nanotubes and similar tubes.
2"""
5import ase.gui.ui as ui
6from ase.build import nanotube
7from ase.gui.i18n import _
8from ase.gui.widgets import Element, pybutton
11introtext = _("""\
12Set up a Carbon nanotube by specifying the (n,m) roll-up vector.
13Please note that m <= n.
15Nanotubes of other elements can be made by specifying the element
16and bond length.\
17""")
19py_template = """\
20from ase.build import nanotube
21atoms = nanotube({n}, {m}, length={length}, bond={bl:.3f}, symbol='{symb}')
22"""
24label_template = _('{natoms} atoms, diameter: {diameter:.3f} Å, '
25 'total length: {total_length:.3f} Å')
28class SetupNanotube:
29 """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: '),
42 self.bondlength,
43 _(u'Å')])
44 win.add(_('Select roll-up vector (n,m) and tube length:'))
45 win.add(['n:', self.n,
46 'm:', self.m,
47 _('Length:'), self.length])
48 win.add(self.description)
49 win.add([pybutton(_('Creating a nanoparticle.'), self.make),
50 ui.Button(_('Apply'), self.apply),
51 ui.Button(_('OK'), self.ok)])
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
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 self.description.text = label
74 return py_template.format(n=n, m=m, length=length, symb=symbol, bl=bl)
76 def apply(self):
77 self.make()
78 if self.atoms is not None:
79 self.gui.new_atoms(self.atoms)
80 return True
81 else:
82 ui.error(_('No valid atoms.'),
83 _('You have not (yet) specified a consistent '
84 'set of parameters.'))
85 return False
87 def ok(self, *args):
88 if self.apply():
89 self.win.close()