-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
316 lines (242 loc) · 11.7 KB
/
main.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
import PySimpleGUI as sg
import numpy as np
from PIL import Image, ImageTk
import time
from threading import Thread
import random
from matplotlib import pyplot as plt
from models.ising import Ising
from models.potts import Potts
from models.xy import XY
from shapes.triangle import Triangle
from shapes.square import Square
from algorithms.metropolis import Metropolis
from algorithms.heatbath import Heatbath
from algorithms.wolff import Wolff
from boundary_conditions.empty import Empty
from boundary_conditions.periodic import Periodic
from boundary_conditions.fixed import Fixed
def create_window(sliders):
shapes = ['Square', 'Triangular']
models = ['Ising', 'Potts', 'XY']
algorithms = ['Metropolis', 'Heat bath', 'Wolff']
bcs = ['Empty', 'Periodic', 'Fixed']
sliders_column = [[sg.Text('Choose the shape of your grid', key='-OUT-', expand_x=True, justification='left')],
[sg.Combo(shapes, font=('Arial Bold', 12), expand_x=True, enable_events=True, readonly=True,
key='Shape')],
[sg.Text('Choose the type of boundary conditions', key='-OUT-', expand_x=True,
justification='left')],
[sg.Combo(bcs, font=('Arial Bold', 12), expand_x=True, enable_events=True, readonly=True,
key='BC')],
[sg.Text('Choose the model you want to use', key='-OUT-', expand_x=True, justification='left')],
[sg.Combo(models, font=('Arial Bold', 12), expand_x=True, enable_events=True, readonly=True,
key='Model')],
[sg.Text('Choose the algorithm you want to use', key='-OUT-', expand_x=True, justification='left')],
[sg.Combo(algorithms, font=('Arial Bold', 12), expand_x=True, enable_events=True, readonly=True,
key='Algorithm')],
[sg.Text('Do you want to plot the energy after finishing?', key='-OUT-',
expand_x=True, justification='left')],
[sg.Checkbox("Plot", key='plot', enable_events=True, default=False)],
[sg.Text('Choose your grid size and press Initialize', key='-OUT-', expand_x=True, justification='left')],
[sg.Input(default_text='20', key='-INPUT-', font=('Arial Bold', 12), size=(10, 1),
justification='left'),
sg.Button('Initialize', enable_events=True, key="-OK-")],
[sg.Text('Use the sliders to change the parameters of the model', key='-OUT-', expand_x=True,
justification='left')]]
for name, min, max, res, initial in sliders:
sliders_column.append([sg.Text(name),
sg.Slider(range=(min, max), default_value=initial, resolution=res, orientation='h',
enable_events=True, key=name.strip())])
sliders_column.append([sg.Text('Use the Play button to play and pause the demo', key='-OUT-', expand_x=True,
justification='left')])
sliders_column.append([sg.Button('Play', enable_events=True, key="-PLAY-")])
demo_column = [[sg.Image(key="-IMAGE-")]]
layout = [[
sg.Column(sliders_column),
sg.VSeperator(),
sg.Column(demo_column),
]]
window = sg.Window("Image Viewer", layout)
return window
class App:
def __init__(self):
self.params = dict()
self.simulation_running = False
self.shape = Square()
self.model = Ising()
self.algorithm = Metropolis()
self.bc = Empty()
self.resolution = self.shape.resolution
self.plot = False
self.popup = True
self.Npotts = None
self.init()
def init(self):
sliders = [
('Steps ', 1, 100, 1, 1),
('Waiting time ', 0, 2, 0.1, 0),
('Temperature ', 0.1, 4, 0.1, 0.1),
('Magnetic field (H)', -1, 1, 0.1, 0),
('Interaction (J) ', 0.1, 1.0, 0.1, 1)
]
self.window = create_window(sliders)
for slider in sliders:
self.params[slider[0].strip()] = slider[4]
def init_grid(self):
possible_spins = self.model.get_possible_spins(self.Npotts)
actual_dimension = self.shape.get_dimensions(self.gridsize)
init_array = []
for y in range(actual_dimension[0]):
init_array.append(random.choices(possible_spins, k=actual_dimension[1]))
return np.array(init_array)
def compute_energies(self, spinposition, new_spin):
h = self.params['Magnetic field (H)']
J = self.params['Interaction (J)']
actual_dimensions = self.shape.get_dimensions(self.gridsize)
spin = self.array[spinposition[0], spinposition[1]]
nb_positions = self.shape.find_nb_positions(spinposition)
spin_neighbors = self.bc.get_neighbors(self.array, nb_positions, actual_dimensions)
initial_energy = self.model.energy_calculator(J, h, spin, spin_neighbors)
final_energy = self.model.energy_calculator(J, h, new_spin, spin_neighbors)
return (initial_energy, final_energy)
def complete_lattice_energy(self):
h = self.params['Magnetic field (H)']
J = self.params['Interaction (J)']
energy = 0
actual_dimensions = self.shape.get_dimensions(self.gridsize)
for y in range(actual_dimensions[0]):
for x in range(actual_dimensions[1]):
spin = self.array[y, x]
nb_positions = self.shape.find_nb_positions((y,x))
spin_neighbors = self.bc.get_neighbors(self.array, nb_positions, actual_dimensions)
energy += self.model.energy_calculator(J / 2, h, spin, spin_neighbors)
return energy
def simulation(self):
temperature = self.params['Temperature']
J = self.params['Interaction (J)']
for i in range(int(self.params['Steps'])):
targetpos = self.shape.get_targetpos(self.gridsize)
new_spin = self.model.get_new_spin(self.array[targetpos[0],targetpos[1]],self.Npotts)
(self.array, deltaE) = self.algorithm.spinflip(J, self.array, targetpos, new_spin, self.shape.get_dimensions(self.gridsize), self.compute_energies, temperature, self.shape.find_nb_positions, self.bc.get_neighbors, self.bc.get_actual_position)
if self.plot:
current_energy = self.en[-1]
self.en.append(current_energy + deltaE)
return self.array
def simulation_thread(self):
try:
while self.simulation_running:
new_array = self.simulation()
array1d = np.append(new_array.flatten(), 0)
im = array1d[self.position_lookup]
result = self.model.color_lookup(im, self.Npotts)
img = ImageTk.PhotoImage(image=Image.fromarray(result.astype(np.uint8)))
self.window["-IMAGE-"].update(data=img)
time.sleep(self.params['Waiting time'])
except Exception as e:
print(e)
def run(self):
while True:
event, values = self.window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
if self.plot:
plt.show()
plt.plot(self.en)
plt.xlabel("Metropolis time step")
plt.ylabel("Energy of the lattice")
plt.show()
break
elif event == 'Shape':
if self.simulation_running:
print('Stop!')
self.simulation_running = False
self.thread.join()
self.popup = True
if values['Shape'] == 'Square':
self.shape = Square()
else:
self.shape = Triangle()
elif event == 'Model':
if self.simulation_running:
print('Stop!')
self.simulation_running = False
self.thread.join()
self.popup = True
if values['Model'] == 'Ising':
self.model = Ising()
elif values['Model'] == 'Potts':
self.model = Potts()
self.Npotts = int(sg.popup_get_text('Number of possible spins/colors:', title="Potts"))
else:
self.model = XY()
elif event == 'Algorithm':
if self.simulation_running:
print('Stop!')
self.simulation_running = False
self.thread.join()
self.popup = True
if values['Algorithm'] == 'Heat bath':
self.algorithm = Heatbath()
elif values['Algorithm'] == 'Wolff':
self.algorithm = Wolff()
else:
self.algorithm = Metropolis()
elif event == 'BC':
if self.simulation_running:
print('Stop!')
self.simulation_running = False
self.thread.join()
self.popup = True
if values['BC'] == 'Empty':
self.bc = Empty()
elif values['BC'] == 'Periodic':
self.bc = Periodic()
else:
self.bc = Fixed()
elif event == 'plot':
self.popup = True
self.plot = values['plot']
self.en = []
elif event == '-OK-':
if self.simulation_running:
print('Stop!')
self.simulation_running = False
self.thread.join()
self.popup = False
self.gridsize = int(values['-INPUT-'])
self.array = self.init_grid()
self.position_lookup = self.shape.create_position_lookup(self.gridsize, self.resolution)
if self.plot:
self.en.append(self.complete_lattice_energy())
array1d = np.append(self.array.flatten(), 0)
im = array1d[self.position_lookup]
result = self.model.color_lookup(im, self.Npotts)
img = ImageTk.PhotoImage(image=Image.fromarray(result.astype(np.uint8)))
self.window["-IMAGE-"].update(data=img)
elif event == "-PLAY-":
if self.popup:
sg.popup("Please choose starting conditions and press 'Initialize' before starting the demo.", title='Error')
else:
if not self.simulation_running:
print('Play!')
self.simulation_running = True
self.thread = Thread(target=self.simulation_thread)
self.thread.start()
else:
print('Stop!')
self.simulation_running = False
self.thread.join()
if self.model.show_arrows:
plt.quiver(np.cos(self.array), np.sin(self.array))
plt.show()
else:
for name in self.params.keys():
if name == event:
self.params[name] = values[name]
if self.plot:
if event == 'Magnetic field (H)' or event == 'Interaction (J)':
self.en.append(self.complete_lattice_energy())
self.window.close()
self.thread.join()
if __name__ == '__main__':
app = App()
app.run()