-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (82 loc) · 2.44 KB
/
main.py
File metadata and controls
114 lines (82 loc) · 2.44 KB
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
import math as mt
import numpy as np
import random
import matplotlib.pyplot as plt
def start():
print("~~~ Welcome the the 2D Ising Model ~~~\n")
print("Please enter the size of the array you would like to use")
arraySize = input("Size of array: ")
print("\nGreat, let's get started!\n")
print ("Initializing domain\n")
A = initialize_Array(arraySize)
print(len(A))
return(A,arraySize)
def initialize_Array(N):
# Array will have dimensions of nPoints x nPoints
nPoints = N
A = np.zeros((nPoints,nPoints))
# loop through array and assign each point +1 or -1
for i in range(nPoints):
for j in range(nPoints):
# Generate random +1 or -1
A[i,j] = random.choice([1,-1])
return(A)
def main(domain,N):
nPoints = N
# Initialize domain temperature
Temp = 5.1
fig = plt.gcf()
fig.show()
fig.canvas.draw()
ax = fig.add_subplot(111)
ax.set_title('Ising Domain')
ax.set_aspect('equal')
cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
# Begin the main, outside loop
for outerLoop in range(1,51):
Temp -= 0.1
print("Temperature = {}".format(Temp))
# Go into calculation loop
domain = updateDomain(domain,nPoints,Temp)
# Plot the domain figure
plt.imshow(domain)
fig.canvas.draw()
return(domain)
def updateDomain(A,nPoints,Temp):
for iteration in range(1,10*nPoints**2):
i = random.randrange(nPoints)
j = random.randrange(nPoints)
if j == 0: #Top
nu = nPoints-1
else:
nu = j-1
if j == nPoints-1: # bottom
nd = 0
else:
nd = j-1
if i == 0: # Right
nr = nPoints-1
else:
nr = i-1
if i == nPoints-1: # left
nl = 0
else:
nl = i+1
# Calculate delta u
deltaU = A[i,j] * (A[i,nu] + A[i,nd] + A[nr,j] + A[nl,j])
if deltaU <= 0:
# If the energy difference warrents, flip
A[i,j] = A[i,j] * -1
else:
savingThrow = random.random()
if savingThrow < mt.exp(-deltaU/Temp):
A[i,j] = A[i,j] * -1
return(A)
#### This part runs the porgram
random.seed()
[domain,N] = start()
domain = main(domain,N)