forked from tobacco-mofs/tobacco_3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplace_bbs.py
291 lines (201 loc) · 6.56 KB
/
place_bbs.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
import numpy as np
from numpy.linalg import norm
from bbcif_properties import bb2array, X_vecs, bbbonds, bbcharges
from Bio import SVDSuperimposer
import itertools
import re
def match_vectors(a1,a2,num):
dist1 = [(np.linalg.norm(a1[0]-a1[i]),i) for i in range(len(a1))]
dist2 = [(np.linalg.norm(a2[0]-a2[i]),i) for i in range(len(a2))]
dist1.sort(key=lambda x: x[0])
dist2.sort(key=lambda x: x[0])
vecs1 = np.array([a1[i] for i in [dist1[j][1] for j in range(num)]])
vecs2 = np.array([a2[i] for i in [dist2[j][1] for j in range(num)]])
return vecs1,vecs2
def mag_superimpose(a1,a2):
sup = SVDSuperimposer()
a1 = np.asarray(a1)
a2 = np.asarray(a2)
mags = [norm(v) for v in a2]
if len(a1) <= 7:
min_dist = (1.0E6, 'foo', 'bar')
for l in itertools.permutations(a1):
p = np.array([m*v/norm(v) for m,v in zip(mags,l)])
sup.set(a2,p)
sup.run()
rot,tran = sup.get_rotran()
rms = sup.get_rms()
if rms < min_dist[0]:
min_dist = (rms,rot,tran)
else:
a1,a2 = match_vectors(a1,a2,6)
mags = [norm(v) for v in a2]
min_dist = (1.0E6, 'foo', 'bar')
for l in itertools.permutations(a1):
p = np.array([m*v/norm(v) for m,v in zip(mags,l)])
sup.set(a2,p)
sup.run()
rot,tran = sup.get_rotran()
rms = sup.get_rms()
if rms < min_dist[0]:
min_dist = (rms,rot,tran)
return min_dist
def superimpose(a1,a2):
sup = SVDSuperimposer()
a1 = np.asarray(a1)
a2 = np.asarray(a2)
if len(a1) <= 7:
min_dist = (1.0E6, 'foo', 'bar')
for l in itertools.permutations(a1):
p = np.asarray(l)
sup.set(a2,p)
sup.run()
rot,tran = sup.get_rotran()
rms = sup.get_rms()
if rms < min_dist[0]:
min_dist = (rms,rot,tran)
else:
a1,a2 = match_vectors(a1,a2,6)
min_dist = (1.0E6, 'foo', 'bar')
for l in itertools.permutations(a1):
p = np.asarray(l)
sup.set(a2,p)
sup.run()
rot,tran = sup.get_rotran()
rms = sup.get_rms()
if rms < min_dist[0]:
min_dist = (rms,rot,tran)
return min_dist
def scaled_node_and_edge_vectors(sc_coords, sc_omega_plus, sc_unit_cell, ea_dict):
nvecs = []
evecs = []
already_placed_edges = []
nvecs_append = nvecs.append
evecs_append = evecs.append
already_placed_edges_append = already_placed_edges.append
for n in sc_coords:
vertex,vcif,vfvec,indicent_edges = n
vcvec = np.dot(sc_unit_cell, vfvec)
ie = []
ie_append = ie.append
for e in indicent_edges:
ind = e[0]
positive_direction = e[1]
ecif = e[2]
if vertex == positive_direction[0]:
direction = 1
on = positive_direction[1]
else:
direction = -1
on = positive_direction[0]
dxn = ea_dict[vertex][ind][1]
dxon = ea_dict[on][ind][1]
ie_append((ind, direction, ecif, dxn, dxon))
efvec = []
ecvec = []
efvec_append = efvec.append
ecvec_append = ecvec.append
for e in ie:
ind, d, ecif, dxn, dxon = e
cs = np.dot(sc_unit_cell, sc_omega_plus[ind - 1])
fvec = vfvec + d * sc_omega_plus[ind - 1]
cvec = vcvec + d * cs
ec1 = vcvec + d * dxn * (cs/np.linalg.norm(cs))
ec2 = cvec - d * dxon * (cs/np.linalg.norm(cs))
ecoords = np.average([ec1,ec2],axis=0)
ecvec_append(cvec)
efvec_append(fvec)
if ind not in already_placed_edges:
evecs_append((ind, ecif, ecoords, np.array([vcvec,cvec])))
already_placed_edges_append(ind)
nvecs_append((vertex, vcvec, vcif, np.asarray(ecvec)))
return nvecs, evecs
def place_nodes(nvecs, charges, ORIENTATION_DEPENDENT_NODES):
placed_nbb_coords = []
placed_nbb_coords_extend = placed_nbb_coords.extend
all_bonds = []
all_bonds_extend = all_bonds.extend
ind_seg = 0
bbind = 1
for n in nvecs:
bbind = bbind + 1
name,cvec,cif,nvec = n
ll = 0
for v in nvec:
mag = np.linalg.norm(v - np.average(nvec, axis = 0))
if mag > ll:
ll = mag
bbxvec = np.array(X_vecs(cif,'nodes',False))
#if ORIENTATION_DEPENDENT_NODES:
nbbxvec = bbxvec
#else:
# nbbxvec = np.array([ll*(v / np.linalg.norm(v)) for v in bbxvec])
min_dist,rot,tran = superimpose(nbbxvec,nvec)
all_bb = bb2array(cif, 'nodes')
all_coords = np.array([v[1] for v in all_bb])
all_inds = np.array([v[0] for v in all_bb])
chg, elem = bbcharges(cif, 'nodes')
all_names = [o + re.sub('[A-Za-z]','',p) for o,p in zip(elem,all_inds)]
all_names_indices = np.array([int(re.sub('[A-Za-z]','',e)) for e in all_names]) + ind_seg
elem_dict = dict((k,'') for k in all_inds)
for i,j in zip(all_inds, elem):
elem_dict[i] = j
ind_dict = dict((k,'') for k in all_inds)
for i,j in zip(all_inds, all_names_indices):
ind_dict[i] = j
bonds = bbbonds(cif, 'nodes')
anf = [str(elem_dict[n]) + str(ind_dict[n]) for n in all_inds]
abf = []
for b in bonds:
b1 = str(elem_dict[b[0]]) + str(ind_dict[b[0]])
b2 = str(elem_dict[b[1]]) + str(ind_dict[b[1]])
abf.append([b1,b2] + b[2:])
aff_all = np.dot(all_coords,rot) + cvec
laff_all = np.c_[anf, aff_all, chg, all_inds, [bbind] * len(anf)]
placed_nbb_coords_extend(laff_all)
all_bonds_extend(abf)
ind_seg = ind_seg + len(all_names)
return placed_nbb_coords, all_bonds
def place_edges(evecs, charges, nnodes):
placed_ebb_coords = []
placed_ebb_coords_extend = placed_ebb_coords.extend
all_bonds = []
all_bonds_extend = all_bonds.extend
ind_seg = nnodes
bbind = -1
for e in evecs:
bbind = bbind - 1
index,cif,ecoords,evec=e
ll = 0
for v in evec:
mag = np.linalg.norm(v - np.average(evec, axis = 0))
if mag > ll:
ll = mag
bbxvec = np.array(X_vecs(cif,'edges',False))
nbbxvec = np.array([ll*(v / np.linalg.norm(v)) for v in bbxvec])
min_dist,rot,tran = superimpose(nbbxvec,evec)
all_bb = bb2array(cif, 'edges')
all_coords = np.array([v[1] for v in all_bb])
all_inds = np.array([v[0] for v in all_bb])
chg, elem = bbcharges(cif, 'edges')
all_names = [o + re.sub('[A-Za-z]','',p) for o,p in zip(elem,all_inds)]
all_names_indices = np.array([int(re.sub('[A-Za-z]','',e)) for e in all_names]) + ind_seg
elem_dict = dict((k,'') for k in all_inds)
for i,j in zip(all_inds, elem):
elem_dict[i] = j
ind_dict = dict((k,'') for k in all_inds)
for i,j in zip(all_inds, all_names_indices):
ind_dict[i] = j
bonds = bbbonds(cif, 'edges')
anf = [str(elem_dict[n]) + str(ind_dict[n]) for n in all_inds]
abf = []
for b in bonds:
b1 = str(elem_dict[b[0]]) + str(ind_dict[b[0]])
b2 = str(elem_dict[b[1]]) + str(ind_dict[b[1]])
abf.append([b1,b2] + b[2:])
aff_all = np.dot(all_coords,rot) + ecoords
laff_all = np.c_[anf, aff_all, chg, all_inds, [bbind] * len(anf)]
placed_ebb_coords_extend(laff_all)
all_bonds_extend(abf)
ind_seg = ind_seg + len(all_names)
return placed_ebb_coords, all_bonds