-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlattice.py
109 lines (85 loc) · 3.35 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
import os, sys, math, random, io
'''
usage:
python isingmModel.py [TEMPURATURE] [DIM1] [DIM2]...
'''
class Lattice:
def __init__(self, name, kt, dims):
#initialize global variables
self.dimensions = dims
self.kt = kt
self.lattice = self.recursive_allocator(0)
self.name = name
# This recursive code is accreditted to:
# http://www.holehouse.org/programming/creating-n-dimensional-arrays-in-python/
# recursive internal function that gives a multi-dimensional array
# given an array of dimensions to create
def recursive_allocator(self, depth):
# Base case
if depth == len(self.dimensions)-1:
currentDimension = self.dimensions[depth]
array = []
for i in xrange(0,currentDimension):
array.append(random.choice([-1,1]))
return array
# Recursive case
else:
array=[]
currentDimension = self.dimensions[depth]
# for each element in each dimension recursivly
# call the function
for i in xrange(0,currentDimension):
array.append(self.recursive_allocator(depth+1))
return array
def getPositionStr(self, position):
temp = "self.lattice"
for i in position:
temp = temp + "[" + str(i) + "]"
return temp
#flips the value at a given position
def flipPositionValue(self, position):
exec self.getPositionStr(position) + " = " + self.getPositionStr(position) + "*(-1)"
return self.getValue(position)
def updatePositionValue(self, position, newVal):
exec self.getPositionStr(position) + " = newVal"
#returns a list of positions that are neighbors of the
#given postion
def getNeighbors(self, position):
neighbors = list()
for i in range(len(position)):
maxLen = self.dimensions[i]
neighborPositionA = position[:]
neighborPositionA[i] = (position[i] + 1)%maxLen
neighborPositionB = position[:]
neighborPositionB[i] = (position[i] - 1)%maxLen
neighbors.append(neighborPositionA)
neighbors.append(neighborPositionB)
return neighbors
#gets value (-1 or +1) from lattice for a given position
def getValue(self,position):
val = 0
exec "val = " + self.getPositionStr(position)
return val
#pass in a function and a closure and this method will
#perform the function on every member of the lattice
def mapLattice(self, func, closure):
depth = 0
position = [0 for i in self.dimensions]
self.recursiveMapHelper(func, closure, self.lattice, depth, position)
def recursiveMapHelper(self, func, closure, sublattice, depth, position):
if depth == len(self.dimensions)-1:
for i in range(len(sublattice)):
newPos = position
newPos[depth] = i
func(self, newPos, closure)
else:
for i in range(len(sublattice)):
newPos = position
newPos[depth] = i
self.recursiveMapHelper(func, closure, sublattice[i], depth+1, newPos)
# print object Lattice
def printLattice(self):
print "name: ", self.name
print "kt: ", self.kt
print "dims: ", self.dimensions
print "lattice: ", self.lattice