Statistiques
| Révision :

root / ase / gui / simulation.py @ 1

Historique | Voir | Annoter | Télécharger (4,77 ko)

1
"Base class for simulation windows"
2

    
3
import gtk
4
from ase.gui.widgets import oops, pack
5
from ase import Atoms
6

    
7
class Simulation(gtk.Window):
8
    def __init__(self, gui):
9
        gtk.Window.__init__(self)
10
        self.gui = gui
11

    
12
    def packtext(self, vbox, text, label=None):
13
        "Pack an text frame into the window."
14
        pack(vbox, gtk.Label(""))
15
        txtframe = gtk.Frame(label)
16
        txtlbl = gtk.Label(text)
17
        txtframe.add(txtlbl)
18
        txtlbl.show()
19
        pack(vbox, txtframe)
20
        pack(vbox, gtk.Label(""))
21

    
22
    def packimageselection(self, outerbox, txt1=" (rerun simulation)",
23
                           txt2=" (continue simulation)"):
24
        "Make the frame for selecting starting config if more than one."
25
        self.startframe = gtk.Frame("Select starting configuration:")
26
        pack(outerbox, [self.startframe])
27
        vbox = gtk.VBox()
28
        self.startframe.add(vbox)
29
        vbox.show()
30
        self.numconfig_format = "There are currently %i configurations loaded."
31
        self.numconfig_label = gtk.Label("")
32
        pack(vbox, [self.numconfig_label])
33
        lbl = gtk.Label("Choose which one to use as the initial configuration")
34
        pack(vbox, [lbl])
35
        self.start_radio_first = gtk.RadioButton(
36
            None, "The first configuration"+txt1+".")
37
        pack(vbox, [self.start_radio_first])
38
        self.start_radio_nth = gtk.RadioButton(self.start_radio_first,
39
                                               "Configuration number ")
40
        self.start_nth_adj = gtk.Adjustment(0, 0, 1, 1)
41
        self.start_nth_spin = gtk.SpinButton(self.start_nth_adj, 0, 0)
42
        self.start_nth_spin.set_sensitive(False)
43
        pack(vbox, [self.start_radio_nth, self.start_nth_spin])
44
        self.start_radio_last = gtk.RadioButton(self.start_radio_first,
45
            "The last configuration"+txt2+".")
46
        self.start_radio_last.set_active(True)
47
        pack(vbox, self.start_radio_last)
48
        self.start_radio_nth.connect("toggled", self.start_radio_nth_toggled)
49
        self.setupimageselection()
50
        
51
    def start_radio_nth_toggled(self, widget):
52
        self.start_nth_spin.set_sensitive(self.start_radio_nth.get_active())
53

    
54
    def setupimageselection(self):
55
        "Decide if the start image selection frame should be shown."
56
        n = self.gui.images.nimages
57
        if n <= 1:
58
            self.startframe.hide()
59
        else:
60
            self.startframe.show()
61
            if self.start_nth_adj.value >= n:
62
                self.start_nth_adj.value = n-1
63
            self.start_nth_adj.upper = n-1
64
            self.numconfig_label.set_text(self.numconfig_format % (n,))
65

    
66
    def getimagenumber(self):
67
        "Get the image number selected in the start image frame."
68
        nmax = self.gui.images.nimages
69
        if nmax <= 1:
70
            return 0
71
        elif self.start_radio_first.get_active():
72
            return 0
73
        elif self.start_radio_nth.get_active():
74
            return self.start_nth_adj.value
75
        else:
76
            assert self.start_radio_last.get_active()
77
            return nmax-1
78

    
79
    def makebutbox(self, vbox):
80
        self.buttons = gtk.HButtonBox()
81
        runbut = gtk.Button("Run")
82
        runbut.connect('clicked', self.run)
83
        closebut = gtk.Button(stock=gtk.STOCK_CLOSE)
84
        closebut.connect('clicked', lambda x: self.destroy())
85
        for w in (runbut, closebut):
86
            self.buttons.pack_start(w, 0, 0)
87
            w.show()
88
        pack(vbox, [self.buttons], end=True, bottom=True)
89

    
90
    def setup_atoms(self):
91
        self.atoms = self.get_atoms()
92
        if self.atoms is None:
93
            return False
94
        try:
95
            self.calculator = self.gui.simulation['calc']
96
        except KeyError:
97
            oops("No calculator: Use Calculate/Set Calculator on the menu.")
98
            return False
99
        self.atoms.set_calculator(self.calculator())
100
        return True
101
    
102
    def get_atoms(self):
103
        "Make an atoms object from the active image"
104
        images = self.gui.images
105
        if images.natoms < 1:
106
            oops("No atoms present")
107
            return None
108
        n = self.getimagenumber()
109
        return Atoms(positions=images.P[n], symbols=images.Z,
110
                     cell=images.A[n], pbc=images.pbc)
111

    
112
    def begin(self, **kwargs):
113
        if self.gui.simulation.has_key('progress'):
114
            self.gui.simulation['progress'].begin(**kwargs)
115

    
116
    def end(self):
117
        if self.gui.simulation.has_key('progress'):
118
            self.gui.simulation['progress'].end()
119

    
120
    def prepare_store_atoms(self):
121
        "Informs the gui that the next configuration should be the first."
122
        self.gui.prepare_new_atoms()
123
        self.count_steps = 0
124
        
125
    def store_atoms(self):
126
        "Observes the minimization and stores the atoms in the gui."
127
        self.gui.append_atoms(self.atoms)
128
        self.count_steps += 1
129