Statistiques
| Révision :

root / prepareQMX.GUI.py @ 9

Historique | Voir | Annoter | Télécharger (11,56 ko)

1
#!/usr/bin/env python
2
from Tkinter import *
3
from copy import deepcopy
4
import StringIO
5

    
6
#-------------------------------------------------------------------------------
7
#--- GENERAL -------------------------------------------------------------------
8
#-------------------------------------------------------------------------------
9
class Definition(object):
10
    def getValue(self, key):
11
        if self.keywords.__contains__(key):
12
            return self.keywords[key]
13
        return ''
14
        
15
#-------------------------------------------------------------------------------
16
#--- STR DEFINITIONS -----------------------------------------------------------
17
#-------------------------------------------------------------------------------
18
class VASPStrDefinition(Definition):
19
    def __init__(self):
20
        self.name='VASP'
21
        self.keywords = {}
22
        self.keywords['import']='ase.io.vasp'
23
        self.keywords['class']='read_vasp'
24
        self.keywords['class.options']="'POSCAR'"
25
    
26
#-------------------------------------------------------------------------------
27
class TURBOMOLEStrDefintion(Definition):
28
    def __init__(self):
29
        self.name='TURBOMOLE'
30
        self.keywords = {}
31
        self.keywords['import']='ase.io.turbomole'
32
        self.keywords['class']='read_turbomole'
33
        self.keywords['class.options']="'coord'"
34

    
35
#-------------------------------------------------------------------------------
36
class EmbedStrDefinition(Definition):
37
    def __init__(self):
38
        self.name='Embed'
39
        self.system='Embed'
40
        self.keywords = {}
41
        self.keywords['import']='ase.embed'
42
        self.keywords['class']='Embed'
43
        self.keywords['class.options']='system, cluster'
44
        self.keywords['method']='embed'
45
        self.keywords['calculator']='qmx'
46

    
47
#-------------------------------------------------------------------------------
48
#--- PRG DEFINITIONS -----------------------------------------------------------
49
#-------------------------------------------------------------------------------
50
class VASPPrgDefinition(Definition):
51
    def __init__(self):
52
        self.name='VASP'
53
        self.keywords = {}
54
        self.keywords['import']='ase.calculators.vasp'
55
        self.keywords['class']='Vasp'
56
        self.keywords['class.options']='write_input=False'
57

    
58
#-------------------------------------------------------------------------------
59
class QmxPrgDefinition(Definition):
60
    def __init__(self):
61
        self.name='QMX'
62
        self.keywords = {}
63
        self.keywords['import']='ase.calculators.qmx'
64
        self.keywords['class']='Qmx'
65
        self.keywords['class.options']='high_level, low_level'
66

    
67
#-------------------------------------------------------------------------------
68
class TURBOMOLEPrgDefinition(Definition):
69
    def __init__(self):
70
        self.name='TURBOMOLE'
71
        self.keywords = {}
72
        self.keywords['import']='ase.calculators.turbomole'
73
        self.keywords['class']='Turbomole'
74

    
75
#-------------------------------------------------------------------------------
76
#--- JOB DEFINITIONS -----------------------------------------------------------
77
#-------------------------------------------------------------------------------
78
class QNJobDefintion(Definition):
79
    def __init__(self):
80
        self.name='QuasiNewton'
81
        self.keywords = {}
82
        self.keywords['import']='ase.optimize'
83
        self.keywords['class']= 'QuasiNewton'
84
        self.keywords['method']='run'
85

    
86
#-------------------------------------------------------------------------------
87
#-------------------------------------------------------------------------------
88
#-------------------------------------------------------------------------------
89

    
90
class MyLabelFrame(LabelFrame):
91
    def __init__(self, mainWnd, master, title, def_list, extra=[]):
92
        LabelFrame.__init__(self, master, text=title)
93
        self.mainWnd = mainWnd
94
        self.definitions=[]
95
        self.title = title
96

    
97
        for definition in def_list:
98
            definition_new = deepcopy(definition)
99
            definition_new.system = title
100
            self.definitions.append(definition_new)
101

    
102
        fields=['import', 'class', 'class.options']
103
        for key in extra:
104
            fields.append(key)
105

    
106
        self.entries=[]
107
        for key in fields:
108
            text=StringVar()
109
            text.set('')
110
            self.entries.append((key, text, Entry(self, textvariable=text, width=15)))
111
            
112
    def grid(self, **arguments):
113
        LabelFrame.grid(self, arguments, sticky=(N+W+E))
114
        self.listbox.selection_set(first=0)
115
        self.select(None)
116
        
117
    def doLayout(self):
118
        irow=0
119
        for (key, text, textfield) in self.entries:
120
            Label(self, text=key, width=15, anchor=W).grid(row=irow)
121
            textfield.grid(row=irow, column=1)
122
            textfield.bind("<KeyRelease>", self.update)
123
            irow+=1
124
        
125
        scrollbar=Scrollbar(self, orient=VERTICAL)
126
        self.listbox=Listbox(self, yscrollcommand=scrollbar.set, height=0, width=15, selectmode=SINGLE)
127
        self.listbox.grid(row=0, column=2, rowspan=irow, columnspan=2, sticky=NS)
128
        self.listbox.bind('<ButtonRelease-1>', self.select)
129

    
130
        scrollbar.config(command=self.listbox.yview)
131
        scrollbar.grid(row=0, column=4, rowspan=irow, sticky=NS)
132
       
133
        for item in self.definitions:
134
            self.listbox.insert(END, item.name)
135
        
136
    def select(self, event):
137
        selection=self.listbox.curselection()
138
        if len(selection) == 1:
139
            self.curr_definition=deepcopy(self.definitions[int(selection[0])])
140
            for (key, text, textfield) in self.entries:
141
                text.set(self.curr_definition.getValue(key))
142
        self.mainWnd.preview()
143
    
144
    def getSelection(self):
145
        return self.curr_definition
146
        
147
    def update(self, event):
148
        for (key, text, textfield) in self.entries:
149
            self.curr_definition.keywords[key]=text.get()
150
        self.mainWnd.preview()
151

    
152
class MainWindow(Frame):
153
    frames=[]
154
    text=None
155
    def __init__(self, master):
156
        Frame.__init__(self, master)
157
        
158
        #--- Structures ---
159
        bigFrame = LabelFrame(self, text='Structures')
160
        irow=0; icol=0
161
        def_list=[EmbedStrDefinition()]
162
        extras=['method', 'method.options', 'calculator']
163
        frame=MyLabelFrame(self, bigFrame, 'Embed', def_list, extras)
164
        frame.doLayout()
165
        frame.grid(row=irow, column=icol)
166
        self.frames.append(frame)
167

    
168
        icol+=1;
169
        def_list=[VASPStrDefinition(), TURBOMOLEStrDefintion()]
170
        for label in ['Cluster', 'System']:
171
            extras = ['cell']
172
            frame=MyLabelFrame(self, bigFrame, label, def_list, extras)
173
            frame.doLayout()
174
            frame.grid(row=irow, column=icol)
175
            self.frames.append(frame)
176
            icol+=1
177
        bigFrame.grid(row=0, columnspan=3)
178

    
179
        #--- Methods ---
180
        bigFrame = LabelFrame(self, text='Methods')
181
        irow=0; icol=0
182
        def_list=[QmxPrgDefinition()]
183
        frame=MyLabelFrame(self, bigFrame, 'Qmx', def_list)
184
        frame.doLayout()
185
        frame.grid(row=irow, column=icol)
186
        self.frames.append(frame)
187

    
188
        icol+=1
189
        def_list=[VASPPrgDefinition(), TURBOMOLEPrgDefinition()]
190
        for label in ['High-Level', 'Low-Level']:
191
            def_list=[VASPPrgDefinition(), TURBOMOLEPrgDefinition()]
192
            frame=MyLabelFrame(self, bigFrame, label, def_list)
193
            frame.doLayout()
194
            frame.grid(row=irow, column=icol)
195
            self.frames.append(frame)
196
            icol+=1
197
        bigFrame.grid(row=1, columnspan=3)
198
        
199
        #--- General ---
200
        bigFrame = LabelFrame(self, text='General')
201
        extras=['method', 'method.options']
202
        frame=MyLabelFrame(self, bigFrame, 'Job', [QNJobDefintion()], extras)
203
        frame.doLayout()
204
        frame.grid()
205
        self.frames.append(frame)
206
        bigFrame.grid(row=2, sticky=NW)
207

    
208
        frame=LabelFrame(self, text="PREVIEW")
209
        scrollbar=Scrollbar(frame, orient=VERTICAL)
210
        scrollbar.set(0.0, 1.0)
211
        self.text=Text(frame, yscrollcommand=scrollbar.set, width=60, heigh=10, state=DISABLED)
212
        self.text.grid(sticky=(N+W+S+E))
213
        scrollbar.config(command=self.text.yview)
214
        scrollbar.grid(row=0, column=1, sticky=NS)        
215
        frame.grid(row=2, column=1)
216

    
217
        frame = Frame(self)
218
        buttonSave=Button(frame, text='save settings', state=DISABLED)
219
        buttonSave.grid(row=1, sticky=(EW))
220

    
221
        buttonPrint=Button(frame, text='Write qmx.py input file', command=self.writeButton)
222
        buttonPrint.grid(row=2, sticky=EW)
223

    
224
        frame.grid(row=2, column=2, sticky=SE)
225

    
226
        #--- MainWindow ---
227
        self.grid(sticky=(W+N+S+E))
228
        self.preview()
229
    
230
    def writeButton(self):
231
        file = open("data","w")
232
        self.writeData(file)
233
        file.close()
234

    
235
    def writeData(self, stream):
236
        keys=['import']
237
        definitions=[]
238

    
239
        for frame in self.frames:
240
            definition = frame.getSelection()
241
            if definition is not None:
242
                definitions.append(definition)
243

    
244
        arrImports=[]
245
        for definition in definitions:
246
            strClass=definition.getValue('class')
247
            strImport=definition.getValue('import')
248
            
249
            if (strClass is not '' and strImport is not ''):
250
                s='from '+strImport+' import '+strClass+'\n'
251
                if not s in arrImports:
252
                    stream.write(s)
253
                    arrImports.append(s)
254

    
255
        stream.write("\n")
256
        for name in ['High-Level', 'Low-Level', 'Qmx']:
257
            for definition in definitions:
258
                if definition.system != name:
259
                    continue
260
                strSystem=definition.system.lower().replace("-", "_")
261
                strClass=definition.getValue('class')
262
                strOptions=definition.getValue('class.options')
263

    
264
                if (strClass is not ''):
265
                    stream.write(strSystem+'='+strClass+'('+strOptions+')\n')
266

    
267
        stream.write("\n")
268
        for name in ['System', 'Cluster', 'Embed']:
269
            for definition in definitions:
270
                if definition.system != name:
271
                    continue
272
                strSystem=definition.system.lower()
273
                strClass=definition.getValue('class')
274
                strOptions=definition.getValue('class.options')
275

    
276
                if (strClass is not ''):
277
                    stream.write(strSystem+'='+strClass+'('+strOptions+')\n')
278
                    
279
                if name is 'Embed':
280
                    strMethod=definition.getValue('method')
281
                    strOptions=definition.getValue('method.options')
282
                    stream.write(strSystem+'.'+strMethod+'('+strOptions+')\n')
283

    
284
                    strOptions=definition.getValue('calculator')
285
                    stream.write(strSystem+'.set_calculator('+strOptions+')\n')
286
            
287
        stream.write("\n")
288
        for definition in definitions:
289
            if (definition.system == 'Job'):
290
                strSystem=definition.system.lower()
291
                strMethod=definition.getValue('method')
292
                strOptions=definition.getValue('method.options')
293
                stream.write(strSystem+'.'+strMethod+'('+strOptions+')\n')
294
                
295
    def preview(self):
296
        stream = StringIO.StringIO()
297
        self.writeData(stream)
298
        s = stream.getvalue()
299
        
300
        if self.text is not None:
301
            pos, end = self.text.yview()
302
            self.text.config(state=NORMAL)
303
            self.text.delete(1.0, END)
304
            self.text.insert(END, s)
305
            self.text.config(state=DISABLED)
306
            self.text.yview(MOVETO, pos)
307

    
308
print
309
mainWnd=MainWindow(Tk())
310
mainWnd.mainloop()