-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmetropolisModule.py
More file actions
107 lines (84 loc) · 2.92 KB
/
metropolisModule.py
File metadata and controls
107 lines (84 loc) · 2.92 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
import lattice, os, sys, io, random, math
from lattice import Lattice
# calcluate hamiltonian for lattice
def calculate_hamiltonian_lattice(lattice):
depth = 0
return recursive_hamiltonian(lattice,lattice.lattice,depth)
def recursive_hamiltonian(lattice, sublattice, depth):
if depth == len(lattice.dimensions)-1:
sum = calculate_hamiltonian_array(sublattice)
return sum
else:
sum = 0
for i in range(len(sublattice)):
sum = sum + recursive_hamiltonian(lattice,sublattice[i],depth+1)
return sum
# calculate the hamiltonian for any 1D array
def calculate_hamiltonian_array(array):
sum=0
for i in range(len(array)):
sum=sum+(array[i]*array[(i+1)%len(array)])
return sum
# calculate magnitization for lattice
def calculate_lattice_magnitization(lattice):
depth = 0
return recursive_magnitization(lattice,lattice.lattice,depth)
def recursive_magnitization(lattice, sublattice, depth):
if depth == len(lattice.dimensions)-1:
sum = calculate_array_magnitization(sublattice)
return sum
else:
sum = 0
for i in range(len(sublattice)):
sum = sum + recursive_magnitization(lattice,sublattice[i],depth+1)
return sum
# calculate the hamiltonian for any 1D array
def calculate_array_magnitization(array):
sum=0
for i in range(len(array)):
sum=sum+array[i]
return sum
def delta_hamiltonian(lattice,position):
newSum = 0
oldSum = 0
positionValue = lattice.getValue(position)
newPositionValue = -1*positionValue
neighbors = lattice.getNeighbors(position)
for i in neighbors:
oldSum += positionValue*lattice.getValue(i)
newSum += newPositionValue*lattice.getValue(i)
return -1*(newSum - oldSum)
# TODO REMOVE THIS LATER
def getPos(thing, position):
temp = "temp"
for i in position:
temp = temp + "[" + str(i) + "]"
return temp
def posFlipTest(lattice, position, closure):
if random.random() < math.exp(delta_hamiltonian(lattice, position)/lattice.kt):
lattice.flipPositionValue(position)
def make2(lattice, pos, closure):
lattice.updatePositionValue(pos, 2)
def runMetropolis(lattice):
lattice.mapLattice(posFlipTest, None)
def main():
if len(sys.argv) < 3:
print "Exception: Too few arguments"
exit(0)
time = sys.argv[1]
kt = float(sys.argv[2])
dims = []
for i in range(3, len(sys.argv)):
dims.append(int(sys.argv[i]))
print dims
lattice = Lattice("lattice", kt, dims)
lattice.printLattice()
print "neighbors: ", lattice.getNeighbors([1,1,2])
print "H: ", delta_hamiltonian(lattice,[1,1,2])
print "Latt: ", runMetropolis(lattice)
for i in range(1000):
runMetropolis(lattice)
print lattice.lattice
exit(1)
if __name__ == "__main__":
exit(main())