forked from tobacco-mofs/tobacco_3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbcif_properties.py
306 lines (254 loc) · 7.21 KB
/
bbcif_properties.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
import re
import sys
import numpy as np
import networkx as nx
import os
PT = ['H' , 'He', 'Li', 'Be', 'B' , 'C' , 'N' , 'O' , 'F' , 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P' , 'S' , 'Cl', 'Ar',
'K' , 'Ca', 'Sc', 'Ti', 'V' , 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr',
'Rb', 'Sr', 'Y' , 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I' , 'Xe',
'Cs', 'Ba', 'Hf', 'Ta', 'W' , 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr',
'Ra', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Ac', 'Th',
'Pa', 'U' , 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', 'FG', 'X' ]
def isfloat(value):
"""
determines if a value is a float
"""
try:
float(value)
return True
except ValueError:
return False
def iscoord(line):
"""
identifies coordinates in CIFs
"""
if (re.sub('[^a-zA-Z]','', line[0]) in PT and
line[1] in PT and
isfloat(line[2]) and
isfloat(line[3]) and
isfloat(line[4])):
return True
else:
return False
def isbond(line):
"""
identifies bonding in cifs
"""
if (re.sub('[^a-zA-Z]','', line[0]) in PT and
re.sub('[^a-zA-Z]','', line[1]) in PT and
isfloat(line[2]) and
not isfloat(line[3]) and
not isfloat(line[4])):
return True
else:
return False
def PBC3DF(c1, c2):
"""
c1 and c2 are coordinates, either numpy arrays or lists
"""
diffa = c1[0] - c2[0]
diffb = c1[1] - c2[1]
diffc = c1[2] - c2[2]
if diffa > 0.5:
c2[0] = c2[0] + 1.0
elif diffa < -0.5:
c2[0] = c2[0] - 1.0
if diffb > 0.5:
c2[1] = c2[1] + 1.0
elif diffb < -0.5:
c2[1] = c2[1] - 1.0
if diffc > 0.5:
c2[2] = c2[2] + 1.0
elif diffc < -0.5:
c2[2] = c2[2] - 1.0
return c2
def bbelems(cifname, direc):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
elems = []
elems_append = elems.append
for line in cif:
s = line.split()
if '_cell_length_a' in line:
a = s[1]
if '_cell_length_b' in line:
b = s[1]
if '_cell_length_c' in line:
c = s[1]
if '_cell_angle_alpha' in line:
alpha = s[1]
if '_cell_angle_beta' in line:
beta = s[1]
if '_cell_angle_gamma' in line:
gamma = s[1]
if iscoord(s):
elems_append(s[1])
return elems
def bb2array(cifname, direc):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
fcoords = []
fcoords_append = fcoords.append
for line in cif:
s = line.split()
if '_cell_length_a' in line:
a = s[1]
if '_cell_length_b' in line:
b = s[1]
if '_cell_length_c' in line:
c = s[1]
if '_cell_angle_alpha' in line:
alpha = s[1]
if '_cell_angle_beta' in line:
beta = s[1]
if '_cell_angle_gamma' in line:
gamma = s[1]
if iscoord(s):
fvec = np.array(map(float, s[2:5]))
fcoords_append([s[0],fvec])
pi = np.pi
a,b,c,alpha,beta,gamma = map(float, (a,b,c,alpha,beta,gamma))
ax = a
ay = 0.0
az = 0.0
bx = b * np.cos(gamma * pi / 180.0)
by = b * np.sin(gamma * pi / 180.0)
bz = 0.0
cx = c * np.cos(beta * pi / 180.0)
cy = (c * b * np.cos(alpha * pi /180.0) - bx * cx) / by
cz = (c ** 2.0 - cx ** 2.0 - cy ** 2.0) ** 0.5
unit_cell = np.asarray([[ax,ay,az],[bx,by,bz],[cx,cy,cz]]).T
norm_vec = fcoords[0][1]
ccoords = [[n[0],np.dot(unit_cell, PBC3DF(norm_vec, n[1]))] for n in fcoords]
#ccoords = [[n[0],np.dot(unit_cell, n[1])] for n in fcoords]
com = np.average(np.array([n[1] for n in ccoords if re.sub('[0-9]','',n[0]) == 'X']), axis = 0)
sccoords = [[n[0], n[1] - com] for n in ccoords]
return sccoords
def bbbonds(cifname, direc):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
bonds = []
bonds_append = bonds.append
for line in cif:
s = line.split()
if isbond(s):
bonds_append(s)
return bonds
def X_vecs(cifname, direc, label):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
fcoords = []
fcoords_append = fcoords.append
for line in cif:
s = line.split()
if '_cell_length_a' in line:
a = s[1]
if '_cell_length_b' in line:
b = s[1]
if '_cell_length_c' in line:
c = s[1]
if '_cell_angle_alpha' in line:
alpha = s[1]
if '_cell_angle_beta' in line:
beta = s[1]
if '_cell_angle_gamma' in line:
gamma = s[1]
if iscoord(s) and 'X' in s[0]:
fcoords_append([s[0],np.array(map(float, s[2:5]))])
pi = np.pi
a,b,c,alpha,beta,gamma = map(float, (a,b,c,alpha,beta,gamma))
ax = a
ay = 0.0
az = 0.0
bx = b * np.cos(gamma * pi / 180.0)
by = b * np.sin(gamma * pi / 180.0)
bz = 0.0
cx = c * np.cos(beta * pi / 180.0)
cy = (c * b * np.cos(alpha * pi /180.0) - bx * cx) / by
cz = (c ** 2.0 - cx ** 2.0 - cy ** 2.0) ** 0.5
unit_cell = np.asarray([[ax,ay,az],[bx,by,bz],[cx,cy,cz]]).T
mic_fcoords = [[vec[0],PBC3DF(fcoords[0][1],vec[1])] for vec in fcoords]
if label:
ccoords = [[vec[0],np.dot(unit_cell,vec[1])] for vec in mic_fcoords]
com = np.average(np.asarray([vec[1] for vec in ccoords]), axis=0)
shifted_ccoords = [[vec[0],vec[1] - com] for vec in ccoords]
else:
ccoords = [np.dot(unit_cell,vec[1]) for vec in mic_fcoords]
com = np.average(ccoords, axis=0)
shifted_ccoords = [vec - com for vec in ccoords]
return shifted_ccoords
def bbcharges(cifname, direc):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
charges = []
charges_append = charges.append
elements = []
elements_append = elements.append
for line in cif:
s = line.split()
if iscoord(s):
charges_append(s[-1])
elements_append(s[1])
return charges, elements
def calc_edge_len(cifname, direc):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
fcoords = []
fcoords_append = fcoords.append
for line in cif:
s = line.split()
if '_cell_length_a' in line:
a = s[1]
if '_cell_length_b' in line:
b = s[1]
if '_cell_length_c' in line:
c = s[1]
if '_cell_angle_alpha' in line:
alpha = s[1]
if '_cell_angle_beta' in line:
beta = s[1]
if '_cell_angle_gamma' in line:
gamma = s[1]
if iscoord(s) and 'X' in s[0]:
fcoords_append([s[0],np.array(map(float, s[2:5]))])
pi = np.pi
a,b,c,alpha,beta,gamma = map(float, (a,b,c,alpha,beta,gamma))
ax = a
ay = 0.0
az = 0.0
bx = b * np.cos(gamma * pi / 180.0)
by = b * np.sin(gamma * pi / 180.0)
bz = 0.0
cx = c * np.cos(beta * pi / 180.0)
cy = (c * b * np.cos(alpha * pi /180.0) - bx * cx) / by
cz = (c ** 2.0 - cx ** 2.0 - cy ** 2.0) ** 0.5
unit_cell = np.asarray([[ax,ay,az],[bx,by,bz],[cx,cy,cz]]).T
mic_fcoords = [PBC3DF(fcoords[0][1],vec[1]) for vec in fcoords]
ccoords = [np.dot(unit_cell,vec) for vec in mic_fcoords]
return np.linalg.norm(ccoords[0] - ccoords[1])
def cncalc(cifname, direc, cn1):
path = os.path.join(direc, cifname)
with open(path, 'r') as cif:
cif = cif.read()
cif = filter(None, cif.split('\n'))
cn = 0
nc = 0
for line in cif:
s = line.split()
if iscoord(s):
nc += 1
if re.sub('[^a-zA-Z]','',s[0]) == 'X':
cn += 1
return cn