-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpebble.pyx
267 lines (224 loc) · 7.92 KB
/
pebble.pyx
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
"""
This is a pebble game algorithm written in python, translated from the original program in Fortran.
To run this program, you need to have python and cython installed.
By Leyou Zhang, 2016, 04
"""
class lattice(object):
def __init__(self):
self.digraph = {} # directed graph
self.graph = {} # undirected graph (complete)
self.cluster={} # cluster information
self.bond = []
self.stress = []
self.visited = []
self.statistics = {}
def clear(self):
self.__init__()
def add_bond(self,int x,int y):
"""
:param x: a site
:param y: a different site
:return: if the new bond is independent: True, otherwise False.
"""
# no self-loop bonds:
if x == y:
raise ValueError('add_bond must have two different sites')
# smaller site first:
x,y = sorted((x,y))
# update bonds:
self.bond.append((x,y))
# update complete graph
sites = self.graph.keys()
if x not in sites:
self.graph[x] = [y]
else:
self.graph[x].append(y)
if y not in sites:
self.graph[y] = [x]
else:
self.graph[y].append(x)
# update directed graph
sites = self.digraph.keys()
if x not in sites:
self.digraph[x] = [[y],1]
if y not in sites:
self.digraph[y] = [[],2]
return True
elif y not in sites:
self.digraph[y] = [[x],1]
return True
elif self.collect_four_pebble(x,y):
if y not in self.digraph[x][0]:
self.digraph[x][0].append(y)
try:
self.digraph[x][1] = self.digraph[x][1] -1
except Exception as f:
raise KeyError(f.message)
return True
else:
return False
# check independence
def depth_first_search(self,int x,int y, z = False, status = 'start'):
if status == 'start':
if not z:
self.visited = [x,y]
else:
self.visited = [x,y,z]
else:
self.visited.append(x)
# exclude y (or y,z) in the search
if not z:
if x == y:
raise ValueError('depth_first_search must have two or three different sites')
for i in self.digraph[x][0]:
if i not in self.visited:
if self.digraph[i][1] > 0:
tree = [i]
return tree
tree = self.depth_first_search(i,y,status='next')
if tree:
return [i]+tree
else:
if x == y or x == z or y == z:
raise ValueError('depth_first_search must have two or three different sites')
for i in self.digraph[x][0]:
if i not in self.visited:
if self.digraph[i][1] > 0:
tree = [i]
return tree
tree = self.depth_first_search(i,y, z = z,status='next')
if tree:
return [i]+tree
return None
def collect_one_pebble(self,int x,int y):
"""
:param x: a site
:param y: a different site
:return: if the one pebble can be collected, return True, otherwise False.
"""
sites = self.graph.keys()
if x in sites:
tree = self.depth_first_search(x,y)
if tree:
self.digraph[x][1] += 1
while tree:
site = tree.pop(0)
self.digraph[x][0].remove(site)
self.digraph[site][0].append(x)
x = site
self.digraph[site][1] += - 1
return True
else:
return False
else:
raise ValueError('site %d is not in the lattice.'%x)
def collect_four_pebble(self, int x, int y):
"""
:param x: a site
:param y: a different site
:return: if the four pebble can be collected, return True, otherwise False.
"""
if x == y:
raise ValueError('collect_four_pebble must have two different sites')
freex = self.digraph[x][1]
freey = self.digraph[y][1]
while freex < 2:
if self.collect_one_pebble(x,y):
freex += 1
else:
break
while freey < 2:
if self.collect_one_pebble(y,x):
freey += 1
else:
break
if freex==2 and freey==2:
return True
else:
return False
def decompose_into_cluster(self):
cluster = {}
bond = self.bond[:]
index = 0
while bond:
index += 1
i = bond.pop()
self.collect_four_pebble(i[0],i[1]) # collect 3 pebble (because it can't find the forth pebble)
cluster[i] = index
for j in bond[:]:
check = [self.digraph[k][1]<1 and not self.depth_first_search(k,i[0],z=i[1]) for k in j if k not in i]+[True]
if all(check):
cluster[j] = index
bond.remove(j)
self.cluster['bond'] = cluster.copy()
cluster_site = {}
for bond,index in cluster.iteritems():
for i in bond:
if i in cluster_site.keys():
if index not in cluster_site[i]:
cluster_site[i] = cluster_site[i]+(index,)
else:
cluster_site[i] = (index,)
self.cluster['site'] = cluster_site.copy()
self.cluster['count'] = index
cluster_index = {}
for key,value in cluster.iteritems():
if value in cluster_index.keys():
cluster_index[value].append(key)
else:
cluster_index[value]=[key]
self.cluster['index'] = cluster_index.copy()
def decompose_stress(self):
bond = self.bond[:]
bond_single = []
self.stress = []
while bond:
i = bond.pop()
bond_single.append(i)
if i in bond:
self.stress.append(i)
while i in bond:
self.stress.append(i)
bond.remove(i)
bond = bond_single[:]
while bond:
i = bond.pop()
if i[0] in self.digraph[i[1]][0] or i[1] in self.digraph[i[0]][0]:
continue
else:
self.collect_four_pebble(i[0],i[1])
if self.digraph[i[0]][1] == 2:
start = [i[1]]
else:
start = [i[0]]
visited = [i]
while start:
next = []
for j in start:
for k in self.digraph[j][0]:
if (j,k) not in visited:
next.append(k)
visited.append((j,k))
b = tuple(sorted((j,k)))
if b not in self.stress:
self.stress.append(b)
if b in bond:
bond.remove(b)
if next:
if i not in self.stress:
self.stress.append(i)
start = next[:]
def stat(self):
site = len(self.graph.keys())
bond = len(self.bond)
floppy_mode = 0
for value in self.digraph.values():
floppy_mode += value[1]
self_stress = bond + floppy_mode - 2 * site
self.statistics = {
'site':site,
'bond':bond,
'floppy_mode':floppy_mode,
'self_stress':self_stress,
}
return True