forked from yale-phys-382L/ising
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsingLattice_cpp.py
144 lines (134 loc) · 4.7 KB
/
IsingLattice_cpp.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
import numpy as np
import sys
from ctypes import *
libc = CDLL("./ising_lattice_lib.so")
libc.newMatrix.restype = c_void_p
libc.newMatrix.argtypes = [c_int, c_int]
libc.step.argtypes = [ c_void_p, c_float, c_float ]
libc.nsteps.argtypes = [ c_void_p, c_int, c_float, c_float ]
libc.get_E.restype = c_float
libc.get_M.restype = c_float
libc.auto_correlation.restype = c_float
libc.auto_correlation.restype = c_float
class IsingLattice(object):
def __init__(self, N, flip_prop):
n_flip = N*N*flip_prop
self.N = N
if n_flip != int(n_flip):
n_flip = int(n_flip) + 1
self.matrix_ptr = c_void_p(libc.newMatrix(int(N),int(n_flip)))
self.auto_correlation = []
def free_memory(self):
# print ('deleting ising_lattice_lib object')
libc.delMatrix( self.matrix_ptr )
def step(self, T, B):
return libc.step(self.matrix_ptr, c_float(T), c_float(B))
def nsteps(self, T, B, n):
return libc.nsteps(self.matrix_ptr,n,T,B)
def get_E(self):
return float( libc.get_E(self.matrix_ptr) )
def get_M(self):
return float( libc.get_M(self.matrix_ptr) )
def calc_auto_correlation(self):
libc.calc_auto_correlation(self.matrix_ptr)
self.auto_correlation = []
for i in range(1,int(self.N/2)):
val = libc.auto_correlation(self.matrix_ptr, i)
self.auto_correlation.append([i,val])
return self.auto_correlation
def set_Nflip(self,npick):
return libc.set_Npick(self.matrix_ptr, c_int(npick))
def set_flip_prop(self,flip_prop):
n_flip = self.N*flip_prop
if n_flip != int(n_flip):
n_flip = int(n_flip) + 1
else:
n_flip = int(n_flip)
return (libc.set_Npick(self.matrix_ptr, c_int(n_flip)))
def get_Nspin(self):
return libc.get_Nspin(self.matrix_ptr)
def get_Nalign(self):
return libc.get_Nbond(self.matrix_ptr)
def get_spin(self, i, j):
return libc.get_spin(self.matrix_ptr, i, j)
def set_spin(self, i, j, k):
return libc.set_spin(self.matrix_ptr, i, j, k)
def print_spins(self):
return libc.print_spins(self.matrix_ptr)
def print_aligned(self):
return libc.print_bonds(self.matrix_ptr)
def randomize_spins(self):
return libc.initialize_spins(self.matrix_ptr)
def get_numpy_spin_matrix(self):
matrix = np.ones((self.N, self.N))
for i in range(self.N):
for j in range(self.N):
matrix[i,j] = libc.get_spin(self.matrix_ptr,i,j)
return matrix
if __name__ == '__main__':
from sys import argv
n = 10
if len(argv) == 1:
pass
elif argv[1] == "1":
lattice = IsingLattice(n,.1)
lattice.print_spins()
print("----")
for i in range(n):
for j in range(n):
continue
lattice.set_spin(i,j,1)
lattice.print_spins()
print("Nspin : %i"%lattice.get_Nspin())
print(" This is mag: %f"%lattice.get_M())
print("auto_corr:")
x = lattice.calc_auto_correlation()
for val in x:
print(val)
lattice.free_memory()
elif argv[1] == "0":
lattice = IsingLattice(5,.1)
print(argv[0])
lattice.print_spins()
print('---')
lattice.set_spin(0,0,1)
lattice.print_spins()
print('---')
lattice.set_spin(0,0,-1)
lattice.print_spins()
print('---')
flip = True
for i in range(n):
for j in range(n):
flip = not flip
if flip:
lattice.set_spin(i,j,1)
else:
lattice.set_spin(i,j,-1)
lattice.print_spins()
print('---')
lattice.free_memory()
else:
lattice = IsingLattice(10,.01)
lattice.print_spins()
lattice.print_aligned()
lattice.nsteps(4.,2.1,10000)
# lattice.randomize_spins()
print('E: ',lattice.get_E())
print('M: ',lattice.get_M())
print('autocorrelation ', lattice.calc_auto_correlation())
print("set_Nflip: ", lattice.set_Nflip(20))
print("get_Nspin: " , lattice.get_Nspin())
print("get_Nalign: " , lattice.get_Nalign())
# print("step: " , lattice.step(2.9,1.0))
print("get_Nspin: " , lattice.get_Nspin())
print("get_Nalign: " , lattice.get_Nalign())
print('E: ' , lattice.get_E())
print("N " , lattice.N)
print('aligned')
# lattice.print_aligned()
print('spins')
# lattice.print_spins()
lattice.free_memory()
print('exit')
sys.exit()