-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattice.py
473 lines (380 loc) · 14.1 KB
/
lattice.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
""" lattice.py
File containg lattices of different dimenions for different purposes.
Each lattice tpe is represented by a class.
The different lattices contained are:
Ising: represents a 1D isling lattice
Ising2D: represents a 2D isling lattice
Potts: represents a 1D q-pott lattice
Potts2D: represents a 2D q-pott lattice
last updated: May 31
@author: organicWinesOnly
"""
import numpy as np
from typing import *
import random as r
from helpers_mc import flip_coin
class Spin:
"""
Repersent a spin in the lattice. Used for wolff algorithm.
==== attributes ===
spin: spin value. Value can only be a one or zero.
cluster: True if spin is in a cluster
location: (row, column) location of spin in lattice
considered: True if spin was already considered to join a latttice
"""
spin: int
cluster: bool
location: tuple
considered: bool
def __init__(self, location_: tuple, set_=0):
# set_ = 0 implies a value of the spin has no been assigned
if set_ == 0:
self.spin = flip_coin(-1, 1)
else:
self.spin = set_
self.cluster = False
self.location = location_
self.considered = False
class Lattice:
"""1D lattice
==== Attributes ===
spins: collection of Spins representing the lattice
size: size of lattice
total_energy: total energy per spin
m: mean magnetizaion per spin
"""
spins: np.ndarray #List[Spin]
size: int
m: float
beta: float
temp: int
total_energy: int
def __init__(self, n: int, temp: int, run_at: float) -> None:
"""Initialize Lattice class
=== parameters ===
n: size of the lattice
temp: the temp you want the sytem to start at, 0 or -1
A zero value implies that temperature is 0 k. -1 implies the
temperature is starting at positive infinity
run_at: temperture you want the system to come to
Representation Invariants:
temp = 0 or 1
"""
self.size = n # needed?
# build lattice
# spin class is not needed for 1d metropolis
if temp == 0:
# k = flip_coin(-1, 1)
self.spins = np.array([1 for _ in range(n)])
else:
self.spins = np.array([flip_coin(-1, 1) for _ in range(n)])
self.m = np.sum(self.spins) / n
self.beta = 1 / run_at
interaction_ = np.zeros(n)
for k in range(n):
spin_j = self._neighbours(k)
interaction_[k] = np.sum(spin_j) * self.spins[k]
self.total_energy = -1 * np.sum(interaction_) /self.size # J = 1
def _neighbours(self, site: int) -> np.ndarray:
"""Return neighbouring spins [<left>, <right>].
Periodic Boundary conditions are applied.
"""
neighbours = np.zeros(2)
if site + 1 == self.spins.shape[0]:
right_ = self.spins[0]
else:
right_ = self.spins[site + 1]
if site - 1 < 0:
left_ = self.spins[-1]
else:
left_ = self.spins[site - 1]
neighbours[0], neighbours[1] = left_, right_
return neighbours
def update(self, delta_energy, delta_m) -> None:
""" Recalculate attributes of the lattice that depend on spin
"""
self.m += delta_m / self.size # not true?
self.total_energy += delta_energy / self.size
########################################################################
# Lattice2D
########################################################################
class Lattice2D:
"""Create a 2d lattice
=== Attributes ===
size: amount of spins in the lattice
energy: total energy in the lattice
m: magnetization per spin
temp: starting temperature
beta: inverse of temp we want our system to come to at equilibrium
spins: ndarray repersenting the spins in our lattice
Repersentation Invariants:
temp = 0 or 1
"""
size: Tuple
total_energy: float
m: float
temp: int
spins: np.ndarray
def __init__(self, n: tuple, initial_temp: int, run_at: float) -> None:
"""Initailize a 2d lattice
=== parameters ===
n: size of the lattice
temp: the temp you want the sytem to start at, 0 or -1
run_at: temperture you want the system to come to
"""
self.spins = np.zeros(n)
self.size = self.spins.size
self.temp = initial_temp
self.beta = 1 / run_at
if initial_temp == 0:
k = flip_coin(-1, 1)
self.spins.fill(k)
else:
for i in range(self.spins.shape[0]):
for j in range(self.spins.shape[1]):
self.spins[i, j] = flip_coin(-1, 1)
self.m = np.sum(self.spins) / self.size
interaction_ = np.zeros(self.spins.shape)
for i in range(self.spins.shape[0]):
for j in range(self.spins.shape[1]):
nn_spins = self._neighbours((i, j))
interaction_[i, j] = np.sum(nn_spins) * self.spins[i, j]
self.total_energy = -1 * np.sum(interaction_) / self.size
def _neighbours(self, site: tuple) -> List[int]:
"""Return neighbouring spins [<left>, <right>, <above> , <below>].
Periodic Boundary conditions are applied
"""
x = site[0] # corridnates of lattice
y = site[1]
# left right
if x + 1 == self.spins.shape[0]:
right_ = self.spins[0][y]
else:
right_ = self.spins[x + 1][y]
if x - 1 < 0:
left_ = self.spins[-1][y]
else:
left_ = self.spins[x - 1][y]
# up, down
if y + 1 == self.spins.shape[1]:
up_ = self.spins[x][0]
else:
up_ = self.spins[x][y + 1]
if y - 1 < 0:
down_ = self.spins[x][-1]
else:
down_ = self.spins[x][y - 1]
return [left_, right_, up_, down_]
def update(self, delta_energy, delta_m) -> None:
""" Recalculate attributes of the lattice that depend on spin
"""
self.m += delta_m / self.size # not true?
self.total_energy += delta_energy / self.size
class Plotts2:
"""Create a 2d lattice that can spin in q directions
=== Attributes ===
size: 2 dim tupple repersenting (num of rows, num of coloums)
energy: total energy of lattice
magnetization: total magnetization of the lattice
temp: we want our sytem to come to equilibrium at
beta: inverse of temp we want are ystem to come to equilibrium at
spins: ndarray repersenting the spins in our lattice
Repersentation Invariants:
temp = 0 or 1
"""
size: Tuple
energy: float
magnetization: int
temp: int
spins: np.ndarray
def __init__(self, n: tuple, q: int, initial_temp: int, run_at: float)\
-> None:
"""Initailize a 2d lattice
=== parameters ===
n: size of the lattice
temp: the temp you want the sytem to start at, 0 or -1
run_at: temperture you want the system to come to
"""
self.size = n
columns = []
self.intial_temp = initial_temp
mag = []
self.size_tot = n[1] * n[0]
for i in range(n[0]):
if initial_temp == 0:
k = r.randint(1, q)
u = [Spin(q, (i, i_), set_=k) for i_ in range(n[1])]
mag.append(sum(item.spin for item in u))
else:
u = [Spin(q, (i, i_)) for i_ in range(n[1])]
mag.append(sum(item.spin for item in u))
columns.append(u)
self.spins = columns
# assert self.spins.shape == self.size
self.temp = run_at
self.magnetization = sum(mag)
interaction_ = []
for column in self.spins:
for spin_loc in column:
spin_j_right = self.neighbours(spin_loc)[1].spin
interaction_.append(spin_j_right *
spin_loc.spin)
spin_j_down = self.neighbours(spin_loc)[3].spin
interaction_.append(spin_j_down *
spin_loc.spin)
spin_j_left = self.neighbours(spin_loc)[2].spin
interaction_.append(spin_j_left *
spin_loc.spin)
spin_j_up = self.neighbours(spin_loc)[0].spin
interaction_.append(spin_j_up *
spin_loc.spin)
self.beta = 1 / run_at
self.energy = -1 * sum(interaction_) * self.beta
def __copy__(self):
"""Return a copy of the lattice"""
other = Plotts(self.size, self.intial_temp, self.temp)
other.spins = self.spins.copy()
other.magnetization = self.magnetization
other.energy = self.energy
# assert other.magnetization == self.magnetization
# assert other.spins is not self.spins
return other
def neighbours(self, spin: Spin) -> List[Spin]:
"""Return neighbouring spins [<left>, <right>, <above> , <below>].
Periodic Boundary conditions are applied
"""
x = spin.location[0] # corridnates of lattice
y = spin.location[1]
# left right
if x + 1 == self.size[0]:
right_ = self.spins[0][y]
else:
right_ = self.spins[x + 1][y]
if x - 1 < 0:
left_ = self.spins[-1][y]
else:
left_ = self.spins[x - 1][y]
# up, down
if y + 1 == self.size[1]:
up_ = self.spins[x][0]
else:
up_ = self.spins[x][y + 1]
if y - 1 < 0:
down_ = self.spins[x][-1]
else:
down_ = self.spins[x][y - 1]
return [left_, right_, up_, down_]
def update(self):
interaction_ = []
for column in self.spins:
for spin_loc in column:
spin_j_right = self.neighbours(spin_loc)[1].spin
interaction_.append(spin_j_right *
spin_loc.spin)
spin_j_down = self.neighbours(spin_loc)[-1].spin
interaction_.append(spin_j_down *
spin_loc.spin)
spin_j_left = self.neighbours(spin_loc)[2].spin
interaction_.append(spin_j_left *
spin_loc.spin)
spin_j_up = self.neighbours(spin_loc)[0].spin
interaction_.append(spin_j_up *
spin_loc.spin)
self.energy = -1 * sum(interaction_) * self.beta
magnet = []
for i in range(self.size[0]):
magnet.append(sum(self.spins[i][p].spin for p in range(self.size[1])))
self.magnetization = sum(magnet)
########################################################################
# rectLattice
########################################################################
def populate(x: np.ndarray, n: tuple, temp):
spin_counter = 0 # the sum of spin values
if len(n) == 1:
for i in range(n[0]):
x[i] = Spin((i,), temp)
spin_counter += x[i].spin
elif len(n) == 2:
for i in range(n[0]):
for j in range(n[1]):
x[i][j] = Spin((i,j), temp)
spin_counter += x[i][j].spin
else:
for i in range(n[0]):
for j in range(n[1]):
for k in range(n[2])
x[i][j][k] = Spin((i,j, k), temp)
spin_counter += x[i][j][k].spin
return spin_counter
class rectLattice:
"""Create a rectangle lattice (straight that can spin
where the elements are objects of the spin class.
=== Attributes ===
shape: 2 dimensional tuple representing(num of rows, num of columns)
total_energy: energy per spin
m: magnetization per spin
temp: the starting temperature (0 -> absolute zero, 1 -> infinity)
spins: ndarray representing the spins in our lattice
beta: energy at equilibrium
Representation Invariants:
temp = 0 or 1
"""
shape: tuple
total_energy: float
m: int
temp: int
spins: np.ndarray
beta = float
def __init__(self, n: tuple, initial_temp: int, run_at: float)\
-> None:
"""Initialize a 2d lattice
=== parameters ===
n: shape of the lattice
temp: the temp you want the system to start at, 0 or -1
run_at: temperature you want the system to come to
"""
# dimension of lattice
dim = len(n)
self.shape = n
self.temp = initial_temp
self.beta = 1 / run_at
# build lattice
self.spins = np.zeros(self.shape)
self.m = populate(self.spins, dim. self.temp) / self.spins.size
# compute the energy of the lattice
interaction_ = np.zeros(n)
for k in range(n):
spin_j = self._neighbours(k)
interaction_[k] = np.sum(spin_j) * self.spins[k]
self.total_energy = -1 * np.sum(interaction_) /self.size # J = 1
def _neighbours(self, atom: Spin) -> List[int]:
"""Return neighbouring spins [<left>, <right>, <above> , <below>].
Periodic Boundary conditions are applied
"""
x, y = atom.location
# left right
if x + 1 == self.spins.shape[0]:
right_ = self.spins[0][y]
else:
right_ = self.spins[x + 1][y]
if x - 1 < 0:
left_ = self.spins[-1][y]
else:
left_ = self.spins[x - 1][y]
# up, down
if y + 1 == self.spins.shape[1]:
up_ = self.spins[x][0]
else:
up_ = self.spins[x][y + 1]
if y - 1 < 0:
down_ = self.spins[x][-1]
else:
down_ = self.spins[x][y - 1]
return [left_, right_, up_, down_]
def update(self, delta_energy, delta_m) -> None:
""" Recalculate attributes of the lattice that depend on spin
"""
self.m += delta_m / self.size # not true?
self.total_energy += delta_energy / self.size
if __name__ == '__main__':
pass