Coverage for /builds/ase/ase/ase/gui/nanotube.py : 98.00%

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."""
31 def __init__(self, gui):
32 self.element = Element('C', self.make)
33 self.bondlength = ui.SpinBox(1.42, 0.0, 10.0, 0.01, self.make)
34 self.n = ui.SpinBox(5, 1, 100, 1, self.make)
35 self.m = ui.SpinBox(5, 0, 100, 1, self.make)
36 self.length = ui.SpinBox(1, 1, 100, 1, self.make)
37 self.description = ui.Label('')
39 win = self.win = ui.Window(_('Nanotube'), wmtype='utility')
40 win.add(ui.Text(introtext))
41 win.add(self.element)
42 win.add([_('Bond length: '),
43 self.bondlength,
44 _(u'Å')])
45 win.add(_('Select roll-up vector (n,m) and tube length:'))
46 win.add(['n:', self.n,
47 'm:', self.m,
48 _('Length:'), self.length])
49 win.add(self.description)
50 win.add([pybutton(_('Creating a nanoparticle.'), self.make),
51 ui.Button(_('Apply'), self.apply),
52 ui.Button(_('OK'), self.ok)])
54 self.gui = gui
55 self.atoms = None
57 def make(self, element=None):
58 symbol = self.element.symbol
59 if symbol is None:
60 self.atoms = None
61 self.python = None
62 self.description.text = ''
63 return
65 n = self.n.value
66 m = self.m.value
67 length = self.length.value
68 bl = self.bondlength.value
69 self.atoms = nanotube(n, m, length=length, bond=bl, symbol=symbol)
70 label = label_template.format(
71 natoms=len(self.atoms),
72 total_length=self.atoms.cell[2, 2],
73 diameter=self.atoms.cell[0, 0] / 2)
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 return True
82 else:
83 ui.error(_('No valid atoms.'),
84 _('You have not (yet) specified a consistent '
85 'set of parameters.'))
86 return False
88 def ok(self, *args):
89 if self.apply():
90 self.win.close()