forked from golddoushi/mcsolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guiMain.py
360 lines (300 loc) · 13.7 KB
/
guiMain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import tkinter as tk
import toolbox as toolbox
import multiprocessing
import numpy as np
import matplotlib.backends.backend_tkagg as tkagg#import FigureCanvasTkAgg,NavigationToolbar2Tk
import matplotlib.figure as fig
import WannierKit as wan
from mpl_toolkits.mplot3d.axes3d import Axes3D
global gui # root gui
global latticeGui, supercellGui, latticeData # read lattice matrix
global nOrbnBondGui, nOrb, nBonds # read number of orbitals and bonds
global OrbListBox, IDandTypeNote, PosNote
global BondBox, IDandTypeOfBondNote, BondDetailNote
global TlistGui, MCparamGui, modelGui, algorithmGui, coreGui
global resultViewerBase, resultViewer, structureFrame, structureViewer
global submitBtn
#gui=tk.Tk(className='mc solver v0.0.1')
###################
# latice settings #
###################
def loadLatticePannel():
global gui, latticeGui, supercellGui
LatticeFrame=tk.LabelFrame(gui,text='Lattice')
LatticeFrame.grid(row=0,column=0)
a0_base=tk.Frame(LatticeFrame)
noteFrame0=toolbox.NoteFrm(a0_base, init_notes=['a1:','',''],init_data=[1,0,0],row=True)
a0_base.grid(row=0,column=0)
a1_base=tk.Frame(LatticeFrame)
noteFrame1=toolbox.NoteFrm(a1_base, init_notes=['a2:','',''],init_data=[0,1,0],row=True)
a1_base.grid(row=1,column=0)
a2_base=tk.Frame(LatticeFrame)
noteFrame2=toolbox.NoteFrm(a2_base, init_notes=['a3:','',''],init_data=[0,0,1],row=True)
a2_base.grid(row=2,column=0)
latticeGui=[noteFrame0,noteFrame1,noteFrame2]
supercell_base=tk.Frame(LatticeFrame)
supercellGui=toolbox.NoteFrm(supercell_base,init_notes=['SC:','x','x'],init_data=[16,16,1],row=True,entryWidth=3)
supercell_base.grid(row=0,column=1,sticky='SE')
def updateLatticeData():
global latticeGui, latticeData
latticeData=[]
for reporter in latticeGui:
latticeData.append(reporter.report())
latticeData=np.array(latticeData)
####################
# orbital settings #
####################
def correspondToOrbList(*arg):
global OrbListBox, IDandTypeNote, PosNote
data=OrbListBox.report()
if len(data)>0:
IDandTypeNote.entry_list[0].config(state='normal')
IDandTypeNote.setValue([data[0],data[1],data[2]]) # id, type, spin
IDandTypeNote.entry_list[0].config(state='disabled')
PosNote.setValue(data[3]) # fractional coordinates
def addOrb():
global OrbListBox, IDandTypeNote, PosNote
newData=list(OrbListBox.infoData)
idAndType=IDandTypeNote.report()
pos=PosNote.report()
#idAndType.append(pos)
newData.append([len(newData),int(idAndType[1]),idAndType[2],tuple(pos)])
OrbListBox.updateInfo(newData)
updateStructureViewer()
def deletOrb():
global OrbListBox, IDandTypeNote, PosNote
newData=list(OrbListBox.infoData)
if len(newData)==0:
return
idAndType=IDandTypeNote.report()
idxs=int(idAndType[0])
newData.pop(idxs)
OrbListBox.updateInfo(newData)
updateStructureViewer()
def resetOrb():
global OrbListBox, IDandTypeNote, PosNote
newData=list(OrbListBox.infoData)
if len(newData)==0:
return
idAndType=IDandTypeNote.report()
pos=PosNote.report()
idxs=int(idAndType[0])
#print(idxs)
newData.pop(idxs)
newData.insert(idxs,[int(idAndType[0]),int(idAndType[1]),idAndType[2],tuple(pos)])
OrbListBox.updateInfo(newData)
updateStructureViewer()
def orbitalDataFormat(info):
return 'ID: %d Type: %d Spin: %.1f FracX: %.4f %.4f %.4f'%(info[0],info[1],info[2],info[3][0],info[3][1],info[3][2])
def loadOrbitals():
global gui, OrbListBox, IDandTypeNote, PosNote
OrbFrame=tk.LabelFrame(gui,text='Orbital list')
OrbFrame.grid(row=1,column=0,columnspan=1)
list_base=tk.Frame(OrbFrame)
list_base.grid(row=0,column=0,columnspan=2)
OrbListBox=toolbox.InfoList(list_base, correspondToOrbList, orbitalDataFormat, initialInfo=[[0,0,0.5,(0.,0.,0.)]],width=45,height=5)
addOrbFrameBase=tk.Frame(OrbFrame)
addOrbFrameBase.grid(row=1,column=0)
id_base=tk.Frame(addOrbFrameBase)
id_base.grid(row=0,column=0)
IDandTypeNote=toolbox.NoteFrm(id_base, init_notes=['ID:','Type:','Spin:'],init_data=[0,0,0.5],row=True,entryWidth=6)
IDandTypeNote.entry_list[0].config(state='disabled')
pos_base=tk.Frame(addOrbFrameBase)
pos_base.grid(row=1,column=0,sticky='W')
PosNote=toolbox.NoteFrm(pos_base, init_notes=['pos','',''],init_data=[0.,0.,0.],row=True,entryWidth=6)
addBtn=tk.Button(addOrbFrameBase,text='add',command=addOrb)
addBtn.grid(row=0,column=1,rowspan=2)
resetBtn=tk.Button(addOrbFrameBase,text='reset',command=resetOrb)
resetBtn.grid(row=0,column=2,rowspan=2)
delBtn=tk.Button(addOrbFrameBase,text='delet',command=deletOrb)
delBtn.grid(row=0,column=3,rowspan=2)
##################
# Bonds settings #
##################
def correspondToBondList(*arg):
global BondBox, IDandTypeOfBondNote, BondDetailNote
data=BondBox.report()
if len(data)>0:
IDandTypeOfBondNote.entry_list[0].config(state='normal')
IDandTypeOfBondNote.setValue([data[0],data[1][0],data[1][1],data[1][2]]) # id, [Jz, Jx, Jy]
IDandTypeOfBondNote.entry_list[0].config(state='disabled')
BondDetailNote.setValue([data[2][0],data[2][1],data[2][2][0],data[2][2][1],data[2][2][2]]) # fractional coordinates
updateStructureViewer(lightID=data[0])
def addBond():
global BondBox, IDandTypeOfBondNote, BondDetailNote
newData=list(BondBox.infoData)
idAndType=IDandTypeOfBondNote.report()
bondDetail=BondDetailNote.report()
newData.append([len(newData),
[idAndType[1],idAndType[2],idAndType[3]],
[int(bondDetail[0]),int(bondDetail[1]),[int(bondDetail[2]),int(bondDetail[3]),int(bondDetail[4])]]])
BondBox.updateInfo(newData)
updateStructureViewer()
def deletBond():
global BondBox, IDandTypeOfBondNote, BondDetailNote
newData=list(BondBox.infoData)
if len(newData)==0:
return
idAndType=IDandTypeOfBondNote.report()
idxs=int(idAndType[0])
newData.pop(idxs)
BondBox.updateInfo(newData)
updateStructureViewer()
def resetBond():
global BondBox, IDandTypeOfBondNote, BondDetailNote
newData=list(BondBox.infoData)
if len(newData)==0:
return
idAndType=IDandTypeOfBondNote.report()
bondDetail=BondDetailNote.report()
idxs=int(idAndType[0])
newData.pop(idxs)
newData.insert(idxs,[len(newData),
[idAndType[1],idAndType[2],idAndType[3]],
[int(bondDetail[0]),int(bondDetail[1]),[int(bondDetail[2]),int(bondDetail[3]),int(bondDetail[4])]]])
BondBox.updateInfo(newData)
updateStructureViewer()
def bondDataFormat(info):
return 'ID: %d J: %.3f source: %d target: %d overLat: %d %d %d'%(info[0],info[1][0],info[2][0],info[2][1],info[2][2][0],info[2][2][1],info[2][2][2])
def loadBonds():
global gui, BondBox, IDandTypeOfBondNote, BondDetailNote
BondFrame=tk.LabelFrame(gui,text='Bond list')
BondFrame.grid(row=2,column=0,columnspan=1)
list_base=tk.Frame(BondFrame)
list_base.grid(row=0,column=0,columnspan=2)
BondBox=toolbox.InfoList(list_base, correspondToBondList, bondDataFormat,
initialInfo=[[0,[-1,-1,-1],[0,0,(1,0,0)]],[1,[-1,-1,-1],[0,0,(0,1,0)]]],
width=45,height=5)
addBondFrameBase=tk.Frame(BondFrame)
addBondFrameBase.grid(row=1,column=0)
id_base=tk.Frame(addBondFrameBase)
id_base.grid(row=0,column=0)
IDandTypeOfBondNote=toolbox.NoteFrm(id_base, init_notes=['ID:','Jz','Jx','Jy'],init_data=[1,-1,-1,-1],row=True,entryWidth=5)
IDandTypeOfBondNote.entry_list[0].config(state='disabled')
detail_base=tk.Frame(addBondFrameBase)
detail_base.grid(row=1,column=0,sticky=(tk.W,tk.E))
BondDetailNote=toolbox.NoteFrm(detail_base, init_notes=['s','t','over lat.','',''],init_data=[0,0,1,0,0],row=True,entryWidth=2)
addBtn=tk.Button(addBondFrameBase,text='add',command=addBond)
addBtn.grid(row=0,column=1,rowspan=2,sticky='E')
resetBtn=tk.Button(addBondFrameBase,text='reset',command=resetBond)
resetBtn.grid(row=0,column=2,rowspan=2,sticky='E')
delBtn=tk.Button(addBondFrameBase,text='delet',command=deletBond)
delBtn.grid(row=0,column=3,rowspan=2,sticky='E')
###############
# MC settings #
###############
def loadMCSettings():
global gui, TListGui, MCparamGui, modelGui, algorithmGui, coreGui
SettingFrame=tk.LabelFrame(gui,text='Other settings')
SettingFrame.grid(row=3,column=0,sticky=(tk.W,tk.E))
temp_base=tk.Frame(SettingFrame)
temp_base.grid(row=0,column=0)
TListGui=toolbox.NoteFrm(temp_base, init_notes=['T start:','T end','total points:'], init_data=[2.0,2.4,20],row=True,entryWidth=6)
MCparam_base=tk.Frame(SettingFrame)
MCparam_base.grid(row=1,column=0,sticky='W')
MCparamGui=toolbox.NoteFrm(MCparam_base, init_notes=['nthermal:','nsweep:'], init_data=[20000,40000],row=True)
model_base=tk.Frame(SettingFrame)
model_base.grid(row=2,column=0,sticky='W')
label1=tk.Label(model_base,text='Model:')
label1.grid(row=0,column=0)
modelGui=tk.Spinbox(model_base,from_=1, to=3, values=['Ising','XY','Heisenberg'],width=12)
modelGui.grid(row=0,column=1)
label2=tk.Label(model_base,text='Algorithm:')
label2.grid(row=0,column=2)
algorithmGui=tk.Spinbox(model_base,from_=1, to=3, values=['Wolff','Metroplis','Sweden-Wang'],width=12)
algorithmGui.grid(row=0,column=3)
core_base=tk.Frame(SettingFrame)
core_base.grid(row=3,column=0,sticky='W')
coreGui=toolbox.NoteFrm(core_base, init_notes=['core:'], init_data=[np.max([1,int(multiprocessing.cpu_count()/2)])])
########################
# Structure visualizer #
########################
def loadStructureViewer():
global gui, latticeGui, OrbListBox, BondBox, supercellGui, structureFrame, structureViewer
structureFrame=tk.LabelFrame(gui, text='Structure viewer')
structureFrame.grid(row=0,column=1,rowspan=2)
tb=wan.TBmodel()
a1=latticeGui[0].report()
a2=latticeGui[1].report()
a3=latticeGui[2].report()
tb.lattice=np.array([a1,a2,a3])
tb.orbital_coor=np.array([ele[3] for ele in OrbListBox.infoData])
tb.norbital=len(tb.orbital_coor)
tb.hopping=[[bond_data[2][0],bond_data[2][1],np.array(bond_data[2][2]),bond_data[1][0],'green', 2] for bond_data in BondBox.infoData]
tb.nhoppings=len(tb.hopping)
#print(tb.hopping)
Lx, Ly, Lz=[3 if x>1 else 1 for x in supercellGui.report() ]
tb.make_supercell([Lx,0,0],[0,Ly,0],[0,0,Lz])
f, ax=tb.viewStructure()
structureViewer=tkagg.FigureCanvasTkAgg(f,structureFrame)
structureViewer.draw()
structureViewer.get_tk_widget().pack()
ax.figure.canvas=structureViewer
ax.mouse_init()
def updateStructureViewer(lightID=-1):
global gui, latticeGui, OrbListBox, BondBox, supercellGui, structureFrame, structureViewer
tb=wan.TBmodel()
a1=latticeGui[0].report()
a2=latticeGui[1].report()
a3=latticeGui[2].report()
tb.lattice=np.array([a1,a2,a3])
tb.orbital_coor=np.array([ele[3] for ele in OrbListBox.infoData])
tb.norbital=len(tb.orbital_coor)
tb.hopping=[[bond_data[2][0],bond_data[2][1],np.array(bond_data[2][2]),bond_data[1][0],'yellow' if bond_data[0]==lightID else 'green', 4 if bond_data[0]==lightID else 2] for bond_data in BondBox.infoData]
tb.nhoppings=len(tb.hopping)
Lx, Ly, Lz=[3 if x>1 else 1 for x in supercellGui.report() ]
tb.make_supercell([Lx,0,0],[0,Ly,0],[0,0,Lz])
f, ax=tb.viewStructure()
structureViewer.get_tk_widget().destroy()
structureViewer=tkagg.FigureCanvasTkAgg(f,structureFrame)
structureViewer.draw()
structureViewer.get_tk_widget().pack()
ax.figure.canvas=structureViewer
ax.mouse_init()
#####################
# Resutl visualizer #
#####################
def loadResultViewer():
global gui, resultViewerBase, resultViewer
resultViewerBase=tk.LabelFrame(gui, text='Result viewer')
resultViewerBase.grid(row=2,column=1,rowspan=2)
f=fig.Figure(figsize=(4,3))
f.add_subplot(111).plot([0,2],[0,0],color='black')
resultViewer=tkagg.FigureCanvasTkAgg(f,resultViewerBase)
resultViewer.draw()
resultViewer.get_tk_widget().pack()
def updateResultViewer(TList=[],magList=[]):
global gui, resultViewerBase, resultViewer
#print('updating:',TList,magList)
f=fig.Figure(figsize=(4,3))
plt=f.add_subplot(111)
plt.scatter(TList,magList,color='red')
plt.set_title('<Spin> per cell')
resultViewer.get_tk_widget().destroy()
resultViewer=tkagg.FigureCanvasTkAgg(f,resultViewerBase)
resultViewer.draw()
resultViewer.get_tk_widget().pack()
#############
# start btn #
#############
def loadStartBtn(submitFunc):
global gui, submitBtn
submit_base=tk.Frame(gui)
submit_base.grid(row=4,column=0,columnspan=2)
note=tk.Label(submit_base, text='Thanks for your attention and wish you find sth. helpful. \n\
Please cite Ref: Magnetic switches via electric field in BN nanoribbons. Applied Surface Science 480(2019)\n\
Thank you very much!')
note.grid(row=0,column=0)
submitBtn=tk.Button(submit_base,text='Submit',command=submitFunc)
submitBtn.grid(row=1,column=0)
def loadEverything(root,submitFunc):
global gui
gui=root
loadLatticePannel()
updateLatticeData()
loadOrbitals()
loadBonds()
loadMCSettings()
loadStructureViewer()
loadResultViewer()
loadStartBtn(submitFunc)