-
Notifications
You must be signed in to change notification settings - Fork 0
/
transition.py
527 lines (381 loc) · 9.95 KB
/
transition.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/python
import numpy as np
from Polygon import Polygon
from geometry import periodic_diff
from energy import *
import copy
from plot import *
"""
transition.py - implements T1 transition for short bond lengths
author: Lexi Signoriello
date: 2/12/16
4 polys involved in transition are 1-4 counter-clockwise order
Cells defined such that:
Cell 0: i4, i1, i2, i5
Cell 1: i3, i1, i4
Cell 2: i6, i2, i1, i3
Cell 3: i5, i2, i6
Edges defined such that:
Edge 0: i1 - i2
Edge 1: i1 - i3
Edge 2: i1 - i4
Edge 3: i2 - i1
Edge 4: i2 - i5
Edge 5: i2 - i6
Edge 6: i3 - i1 # reverse edges
Edge 7: i4 - i1
Edge 8: i5 - i2
Edge 9: i6 - i2
"""
def get_6_indices(polys, i1, i2, poly_ids):
polys_copy = []
for i in poly_ids:
poly = copy.deepcopy(polys[i])
polys_copy.append(poly)
# define polys
poly_0 = polys_copy[0]
poly_1 = polys_copy[1]
poly_2 = polys_copy[2]
poly_3 = polys_copy[3]
# Find indices wrt Cell 1
pos = int(np.where(poly_1.indices == i1)[0])
# i3: poly 1 before i1
if pos == 0:
i_left = len(poly_1.indices) - 1
else:
i_left = pos - 1
i3 = poly_1.indices[i_left]
# i4: poly 1 after i1
if pos == len(poly_1.indices) - 1:
i_right = 0
else:
i_right = pos + 1
i4 = poly_1.indices[i_right]
# i5: poly 3 before i2
pos = int(np.where(poly_3.indices == i2)[0])
if pos == 0:
i_left = len(poly_3.indices) - 1
else:
i_left = pos - 1
i5 = poly_3.indices[i_left]
# i6: poly 3 after i2
if pos == len(poly_3.indices) - 1:
i_right = 0
else:
i_right = pos + 1
i6 = poly_3.indices[i_right]
indices = [i1,i2,i3,i4,i5,i6]
return indices
# get polys and edges associated with short bond length
def T1_0(polys, i1, i2, poly_ids, indices):
polys_0 = []
for i in poly_ids:
# copy poly so it can be manipulated without changing
# current configuation
poly = copy.deepcopy(polys[i])
polys_0.append(poly)
# define polys
poly_0 = polys_0[0]
poly_1 = polys_0[1]
poly_2 = polys_0[2]
poly_3 = polys_0[3]
i1 = indices[0]
i2 = indices[1]
i3 = indices[2]
i4 = indices[3]
i5 = indices[4]
i6 = indices[5]
edges_0 = np.zeros((10,2))
# Edge 0: i1 - i2
edges_0[0,0] = i1
edges_0[0,1] = i2
# Edge 1: i1 - i3
edges_0[1,0] = i1
edges_0[1,1] = i3
# Edge 2: i1 - i4
edges_0[2,0] = i1
edges_0[2,1] = i4
# Edge 3: i2 - i1
edges_0[3,0] = i2
edges_0[3,1] = i1
# Edge 4: i2 - i5
edges_0[4,0] = i2
edges_0[4,1] = i5
# Edge 5: i2 - i6
edges_0[5,0] = i2
edges_0[5,1] = i6
# Edge 6: i3 - i1 # reverse edges
edges_0[6,0] = i3
edges_0[6,1] = i1
# Edge 7: i4 - i1
edges_0[7,0] = i4
edges_0[7,1] = i1
# Edge 8: i5 - i2
edges_0[8,0] = i5
edges_0[8,1] = i2
# Edge 9: i6 - i2
edges_0[9,0] = i6
edges_0[9,1] = i2
return polys_0, edges_0
# get polys and edges associated with
def T1_left(polys, i1, i2, poly_ids, indices):
# Cells
polys_l = []
# ids in correct order already
for i in poly_ids:
poly = copy.deepcopy(polys[i])
polys_l.append(poly)
# define polys
poly_0 = polys_l[0]
poly_1 = polys_l[1]
poly_2 = polys_l[2]
poly_3 = polys_l[3]
# define indices
i1 = indices[0]
i2 = indices[1]
i3 = indices[2]
i4 = indices[3]
i5 = indices[4]
i6 = indices[5]
# Cell 0: remove i2
pos = int(np.where(poly_0.indices == i2)[0])
indices = np.delete(poly_0.indices, pos)
polys_l[0].indices = indices
# Cell 1: insert i2 before i1
pos = int(np.where(poly_1.indices == i1)[0])
left_indices = poly_1.indices[:pos]
right_indices = poly_1.indices[pos:]
indices = np.concatenate((left_indices, [i2], right_indices))
polys_l[1].indices = indices
# Cell 2: remove i1
pos = int(np.where(poly_2.indices == i1)[0])
indices = np.delete(poly_2.indices, pos)
polys_l[2].indices = indices
# Cell 3: insert i1 before i2
pos = int(np.where(poly_3.indices == i2)[0])
left_indices = poly_3.indices[:pos]
right_indices = poly_3.indices[pos:]
indices = np.concatenate((left_indices, [i1], right_indices))
polys_l[3].indices = indices
# Edges
edges_l = np.zeros((10,2))
# Edge 0: i1 - i2
edges_l[0,0] = i1
edges_l[0,1] = i2
# Edge 1: i2 - i3
edges_l[1,0] = i2
edges_l[1,1] = i3
# Edge 2: i1 - i4
edges_l[2,0] = i1
edges_l[2,1] = i4
# Edge 3: i2 - i1
edges_l[3,0] = i2
edges_l[3,1] = i1
# Edge 4: i1 - i5
edges_l[4,0] = i1
edges_l[4,1] = i5
# Edge 5: i2 - i6
edges_l[5,0] = i2
edges_l[5,1] = i6
# Edge 6: i3 - i2 # reverse edges
edges_l[6,0] = i3
edges_l[6,1] = i2
# Edge 7: i4 - i1
edges_l[7,0] = i4
edges_l[7,1] = i1
# Edge 8: i5 - i1
edges_l[8,0] = i5
edges_l[8,1] = i1
# Edge 9: i6 - i2
edges_l[9,0] = i6
edges_l[9,1] = i2
return polys_l, edges_l
def T1_right(polys, i1, i2, poly_ids, indices):
polys_r = []
for i in poly_ids:
poly = copy.deepcopy(polys[i])
polys_r.append(poly)
# define polys
poly_0 = polys_r[0]
poly_1 = polys_r[1]
poly_2 = polys_r[2]
poly_3 = polys_r[3]
# define indices
i1 = indices[0]
i2 = indices[1]
i3 = indices[2]
i4 = indices[3]
i5 = indices[4]
i6 = indices[5]
# Cell 0: remove i1
pos = int(np.where(poly_0.indices == i1)[0])
indices = np.delete(poly_0.indices, pos)
polys_r[0].indices = indices
# Cell 1: insert i2 after i1
pos = int(np.where(poly_1.indices == i1)[0])
left_indices = poly_1.indices[:pos+1]
right_indices = poly_1.indices[pos+1:]
indices = np.concatenate((left_indices, [i2], right_indices))
polys_r[1].indices = indices
# Cell 2: remove i2
pos = int(np.where(poly_2.indices == i2)[0])
indices = np.delete(poly_2.indices, pos)
polys_r[2].indices = indices
# Cell 3: insert i1 after i2
pos = int(np.where(poly_3.indices == i2)[0])
left_indices = poly_3.indices[:pos+1]
right_indices = poly_3.indices[pos+1:]
indices = np.concatenate((left_indices, [i1], right_indices))
polys_r[3].indices = indices
# # Edges
edges_r = np.zeros((10,2))
# Edge 0: i1 - i2
edges_r[0,0] = i1
edges_r[0,1] = i2
# Edge 1: i1 - i3
edges_r[1,0] = i1
edges_r[1,1] = i3
# Edge 2: i1 - i6
edges_r[2,0] = i1
edges_r[2,1] = i6
# Edge 3: i2 - i1
edges_r[3,0] = i2
edges_r[3,1] = i1
# Edge 4: i2 - i5
edges_r[4,0] = i2
edges_r[4,1] = i5
# Edge 5: i2 - i4
edges_r[5,0] = i2
edges_r[5,1] = i4
# Edge 6: i3 - i1 # reverse edges
edges_r[6,0] = i3
edges_r[6,1] = i1
# Edge 7: i4 - i2
edges_r[7,0] = i4
edges_r[7,1] = i2
# Edge 8: i5 - i2
edges_r[8,0] = i5
edges_r[8,1] = i2
# Edge 9: i6 - i1
edges_r[9,0] = i6
edges_r[9,1] = i1
return polys_r, edges_r
# # find 4 polys involved with 2 vertices
# Labeled polys 0-3 in counter-clockwise order
# Cell 0 and Cell 3 are neighbors
def get_4_polys(polys, i1, i2):
poly_ids = np.zeros(4).astype(int)
poly_ids.fill(-1) # catch errors later
for poly in polys:
# Cell 0 or Cell 2
# Current neighboring polys
# Cell 1 should have i1 before i2 in counter-clockwise orde
if i1 in poly.indices and i2 in poly.indices:
pos1 = np.where(poly.indices == i1)
pos2 = np.where(poly.indices == i2)
if pos1 == len(poly.indices) - 1:
pos1 = -1
if pos2 == len(poly.indices) - 1:
pos2 = -1
# if Cell 1: i1 is before i2
if pos1 < pos2:
poly_ids[0] = poly.id
# if Cell 3: i2 is before i1
if pos2 < pos1:
poly_ids[2] = poly.id
# Cell 3
if i2 in poly.indices and i1 not in poly.indices:
poly_ids[3] = poly.id
# Cell 1
if i1 in poly.indices and i2 not in poly.indices:
poly_ids[1] = poly.id
return poly_ids
def T1_transition(vertices, polys, edges, parameters):
lx = parameters['lx']
ly = parameters['ly']
L = np.array([lx,ly])
lmin = parameters['lmin']
for edge in edges:
i1 = edge[0]
i2 = edge[1]
v1 = vertices[i1]
vertex2 = vertices[i2]
v2 = v1 + periodic_diff(vertex2, v1, L)
dist = euclidean_distance(v1[0], v1[1], v2[0], v2[1])
if dist < lmin:
# print "T1", i1, i2, dist
poly_ids = get_4_polys(polys, i1, i2)
if -1 in poly_ids:
pass
else:
# find minimum configuration
# 6 indices for vertices involved in transition
indices = get_6_indices(polys, i1, i2, poly_ids)
# original configuration
polys_0, edges_0 = T1_0(polys, i1, i2, poly_ids, indices)
E0 = get_energy(vertices, polys_0, edges_0, parameters)
# left T1 transition
polys_l, edges_l = T1_left(polys, i1, i2, poly_ids, indices)
E_left = get_energy(vertices, polys_l, edges_l, parameters)
# # right T1 transition
polys_r, edges_r = T1_right(polys, i1, i2, poly_ids, indices)
E_right = get_energy(vertices, polys_r, edges_r, parameters)
# get minimum
min_energy = np.min((E0, E_left, E_right))
min_i = np.argmin((E0, E_left, E_right))
# # do nothing - same configuration
if min_i == 0:
pass
if min_i == 1:
set_T1_left(polys, polys_l, poly_ids, edges, indices)
if min_i == 2:
set_T1_right(polys, polys_r, poly_ids, edges, indices)
return polys, edges
def set_T1_left(polys, polys_l, poly_ids, edges, indices):
# set new poly indices
for i,poly in enumerate(polys_l):
polys[poly_ids[i]].indices = poly.indices
# set new edges
i1 = indices[0]
i2 = indices[1]
i3 = indices[2]
i5 = indices[4]
for i,edge in enumerate(edges):
# i1 - i3 becomes i2 - i3
if edge[0] == i1 and edge[1] == i3:
edges[i][0] = i2
# i2 - i5 becomes i1 - i5
if edge[0] == i2 and edge[1] == i5:
edges[i][0] = i1
# i3 - i1 becomes i3 - i2
if edge[0] == i3 and edge[1] == i1:
edges[i][1] = i2
# i5 - i2 becomes i5 - i1
if edge[0] == i5 and edge[1] == i2:
edges[i][1] = i1
return
def set_T1_right(polys, polys_r, poly_ids, edges, indices):
# set new poly indices
for i,poly in enumerate(polys_r):
polys[poly_ids[i]].indices = poly.indices
# set new edges
i1 = indices[0]
i2 = indices[1]
i4 = indices[3]
i6 = indices[5]
for i,edge in enumerate(edges):
# i1 - i4 becomes i2 - i4
if edge[0] == i1 and edge[1] == i4:
edges[i][0] = i2
# i2 - i6 becomes i1 - i6
if edge[0] == i2 and edge[1] == i6:
edges[i][0] = i1
# i4 - i1 becomes i4 - i2
if edge[0] == i4 and edge[1] == i1:
edges[i][1] = i2
# i6 - i2 becomes i6 - i1
if edge[0] == i6 and edge[1] == i2:
edges[i][1] = i1
return
def T2_transition(network, vertices, polys, edges, min_area):
pass