Statistiques
| Révision :

root / ase / gui / energyforces.py @ 11

Historique | Voir | Annoter | Télécharger (3,06 ko)

1
# encoding: utf-8
2

    
3
"Module for calculating energies and forces."
4

    
5
import gtk
6
from ase.gui.simulation import Simulation
7
from ase.gui.widgets import oops, pack
8

    
9
class OutputFieldMixin:
10
    def makeoutputfield(self, box, label="Output:"):
11
        frame = gtk.Frame(label)
12
        if box is not None:
13
            box.pack_start(frame, True, True, 0)
14
        box2 = gtk.VBox()
15
        frame.add(box2)
16
        #pack(box, [gtk.Label("Output:")])
17
        scrwin = gtk.ScrolledWindow()
18
        scrwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
19
        self.output = gtk.TextBuffer()
20
        txtview = gtk.TextView(self.output)
21
        txtview.set_editable(False)
22
        scrwin.add(txtview)
23
        scrwin.show_all()
24
        box2.pack_start(scrwin, True, True, 0)
25
        self.savebutton = gtk.Button(stock=gtk.STOCK_SAVE)
26
        self.savebutton.connect('clicked', self.saveoutput)
27
        self.savebutton.set_sensitive(False)
28
        pack(box2, [self.savebutton])
29
        box2.show()
30
        frame.show()
31
        return frame
32
    
33
    def activate_output(self):
34
        self.savebutton.set_sensitive(True)
35

    
36
    def saveoutput(self, widget):
37
        chooser = gtk.FileChooserDialog(
38
            'Save output', None, gtk.FILE_CHOOSER_ACTION_SAVE,
39
            (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
40
             gtk.STOCK_SAVE, gtk.RESPONSE_OK))
41
        ok = chooser.run()
42
        if ok == gtk.RESPONSE_OK:
43
            filename = chooser.get_filename()
44
            txt = self.output.get_text(self.output.get_start_iter(),
45
                                       self.output.get_end_iter())
46
            f = open(filename, "w")
47
            f.write(txt)
48
            f.close()
49
        chooser.destroy()
50
        
51
class EnergyForces(Simulation, OutputFieldMixin):
52
    def __init__(self, gui):
53
        Simulation.__init__(self, gui)
54

    
55
        self.set_default_size(-1, 300)
56
        vbox = gtk.VBox()
57
        self.packtext(vbox,
58
                      "Calculate potential energy and the force on all atoms")
59
        self.packimageselection(vbox)
60
        pack(vbox, gtk.Label(""))
61
        self.forces = gtk.CheckButton("Write forces on the atoms")
62
        self.forces.set_active(True)
63
        pack(vbox, [self.forces])
64
        pack(vbox, [gtk.Label("")])
65
        self.makeoutputfield(vbox)
66
        pack(vbox, gtk.Label(""))
67
        self.makebutbox(vbox)
68
        vbox.show()
69
        self.add(vbox)
70
        self.show()
71
        self.gui.register_vulnerable(self)
72
        
73
    def run(self, *args):
74
        if not self.setup_atoms():
75
            return
76
        self.begin()
77
        e = self.atoms.get_potential_energy()
78
        txt = "Potential Energy:\n"
79
        txt += "  %8.3f eV\n\n" % (e,)
80
        if self.forces.get_active():
81
            txt +="Forces:\n"
82
            forces = self.atoms.get_forces()
83
            for f in forces:
84
                txt += "  %8.3f, %8.3f, %8.3f eV/Å\n" % tuple(f)
85
        self.output.set_text(txt)
86
        self.activate_output()
87
        self.end()
88
                
89
    def notify_atoms_changed(self):
90
        "When atoms have changed, check for the number of images."
91
        self.setupimageselection()