-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2twin.py
executable file
·391 lines (321 loc) · 12.7 KB
/
2twin.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
#! /usr/bin/env python
#Please supply the path to a python3 installation with access to NumPy.
#If you do not have NumPy, try running "pip3 install numpy".
'''
2twin: Multiple independent twinning in ShelXL.
Copyright 2017, Daniel W. Paley.
Contact: [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GPL, see <http://www.gnu.org/licenses/>.
'''
import sys
from itertools import combinations
from copy import deepcopy
def help():
print('''
Usage: To process structure.hkl, run "2twin structure" and follow the
prompts. Manually duplicate structure.ins to structure-2twin.ins and ensure it
has HKLF 5 and the correct number of BASF parameters.
Description: 2twin takes an input hkl file (HKLF 4 or 5 format), applies one or
more (pseudo-)merohedral twin laws, and outputs an HKLF 5 file containing one
batch for every twin-related domain.
For example, you might have a three-component HKLF 5 file and wish to apply
2[110] as an additional merohedral twin law. In ShelXL, TWIN and HKLF 5 are not
mutually compatible, so both forms of twinning cannot be refined together.
Instead, we transform every reflection in the HKLF 5 file by the merohedral
twin law, generating a new file with twice the number of entries.
The input HKLF 5 file might contain the following group of three overlapping
reflections, where "m should be positive for the last contributing component
and negative for the remaining ones".
2 2 1 89993.4 1839.96 -3
3 -1 0 89993.4 1839.96 -2
-1 1 2 89993.4 1839.96 1
After processing with 2twin, each reflection in the group has been copied and
transformed by the merohedral twin law 010/100/00-1. The hkl file contains
all merohedrally and non-merohedrally related twin components, and the
refinement proceeds with 6 - 1 = 5 BASF parameters.
2 2 -1 89993.4 1839.96 -6
2 2 1 89993.4 1839.96 -3
-1 3 0 89993.4 1839.96 -5
3 -1 0 89993.4 1839.96 -2
1 -1 -2 89993.4 1839.96 -4
-1 1 2 89993.4 1839.96 1
The following combinations of twin laws are currently supported. Other
combinations may be added; contact the author or try editing compDict.
1 merohedral TL, order 2
1 merohedral TL, order 3
2 merohedral TLs, orders 2 and 2
2 merohedral TLs, orders 3 and 2
3 merohedral TLs, all order 2
1 non-merohedral and 1 merohedral, each order 2
1 non-merohedral and 1 merohedral, orders 3 and 2
1 non-merohedral and 1 merohedral, orders 2 and 3
1 non-merohedral and 2 merohedral, all order 2
1 non-merohedral and 2 merohedral; orders 3, 2, 2
1 non-merohedral and 3 merohedral; orders 3, 2, 2, 2
Note that your agreement factors may be artifically increased if you have
switched from TWIN/HKLF 4 refinement to HKLF 5 refinement. This is because
merging is disabled for HKLF 5 data. For a fair comparison, run your TWIN/HKLF 4
refinement with MERG 0.
''')
def makeCompDict():
compDict = {
#This dictionary is only for hklf entries that we generate using
#transform, so output component #s are generally negative. Components
#that would never be generated by transform are written with positive
#comp #s and only included for completeness.'''
# 1 merohedral TL, order 2
('1',''): 1,
('1','1'): -2,
# 1 merohedral TL, order 3
('11',''): 1,
('11','1'): -2,
('11','11'): -3,
# 2 merohedral TLs, orders 2 and 2
('12',''): 1,
('12','1'): -2,
('12','2'): -3,
('12','12'): -4,
# 2 merohedral TLs, orders 3 and 2
('112',''): 1,
('112','1'): -2,
('112','11'): -3,
('112','2'): -4,
('112','12'): -5,
('112','112'): -6,
# 3 merohedral TLs, all order 2
('123',''): 1,
('123','1'): -2,
('123','2'): -3,
('123','3'): -4,
('123','12'): -5,
('123','13'): -6,
('123','23'): -7,
('123','123'): -8,
# 1 non-merohedral and 1 merohedral, each order 2:
('01',''): 1,
('01','0'): 2,
('01','1'): -3,
('01','01'): -4,
# 1 non-merohedral and 1 merohedral, orders 3 and 2:
('001',''): 1,
('001','0'): 2,
('001','00'): 3,
('001','1'): -4,
('001','01'): -5,
('001','001'): -6,
# 1 non-merohedral and 1 merohedral, orders 4 and 2:
('0001',''): 1,
('0001','0'): 2,
('0001','00'): 3,
('0001','000'): 4,
('0001','1'): -5,
('0001','01'): -6,
('0001','001'): -7,
('0001','0001'): -8,
# 1 non-merohedral and 1 merohedral, orders 2 and 3:
('011',''): 1,
('011','0'): 2,
('011','1'): -3,
('011','01'): -4,
('011','11'): -5,
('011','011'): -6,
# 1 non-merohedral and 2 merohedral, all order 2:
('012',''): 1,
('012','0'): 2,
('012','1'): -3,
('012','2'): -4,
('012','01'): -5,
('012','02'): -6,
('012','12'): -7,
('012','012'): -8,
#1 non-merohedral and 2 merohedral; orders 3, 2, 2
('0012', ''): 1,
('0012', '0'): 2,
('0012', '00'): 3,
('0012', '1'): -4,
('0012', '01'): -5,
('0012', '001'): -6,
('0012', '2'): -7,
('0012', '02'): -8,
('0012', '002'): -9,
('0012', '12'): -10,
('0012', '012'): -11,
('0012', '0012'): -12,
# 1 non-merohedral and 3 merohedral; orders 3, 2, 2, 2
('00123', ''): 1,
('00123', '0'): 2,
('00123', '00'): 3,
('00123', '1'): -4,
('00123', '01'): -5,
('00123', '001'): -6,
('00123', '2'): -7,
('00123', '02'): -8,
('00123', '002'): -9,
('00123', '3'): -10,
('00123', '03'): -11,
('00123', '003'): -12,
('00123', '12'): -13,
('00123', '012'): -14,
('00123', '0012'): -15,
('00123', '13'): -16,
('00123', '013'): -17,
('00123', '0013'): -18,
('00123', '23'): -19,
('00123', '023'): -20,
('00123', '0023'): -21,
('00123', '123'): -22,
('00123', '0123'): -23,
('00123', '00123'): -24
}
return compDict
def mat_vec_3_product(m, v):
r0 = sum([x*y for x, y in zip(m[0], v)])
r1 = sum([x*y for x, y in zip(m[1], v)])
r2 = sum([x*y for x, y in zip(m[2], v)])
return [r0, r1, r2]
class Hklf(object):
def __init__(self, hklfString, fileInfo):
self.h = int(hklfString[0:4])
self.k = int(hklfString[4:8])
self.l = int(hklfString[8:12])
self.f2 = hklfString[12:20]
self.sig = hklfString[20:28]
self.hkl = [self.h, self.k, self.l]
if fileInfo['hklType'] == '5':
self.comp = int(hklfString[28:32])
else:
self.comp = 1
self.tlFlags = '0' * (-1 + abs(self.comp))
def transform(self, twinLaw, compDict, tlAll):
self.hkl = mat_vec_3_product(twinLaw.tlArray, self.hkl)
self.h, self.k, self.l = self.hkl
self.tlFlags += twinLaw.tlNumber
self.comp = compDict[ (tlAll, self.tlFlags) ]
def nTransform(self, transString, compDict, fileInfo):
hklfTemp = deepcopy(self)
for trans in transString:
tlNumber = int(trans)
twinLaw = fileInfo['tlList'][tlNumber-1]
hklfTemp.transform(twinLaw, compDict, fileInfo['tlAll'])
return hklfTemp
def output(self):
if self.h % 1 + self.k % 1 + self.l % 1 > 0.1:
return ''
else:
return '{: 4}{: 4}{: 4}{}{}{: 4}\n'.format(
int(round(self.h)), int(round(self.k)), int(round(self.l)),
self.f2, self.sig, self.comp
)
class TwinLaw(object):
def __init__(self, tlString, tlNumber):
tlList = [float(n) for n in tlString.split()]
self.tlArray = [tlList[0:3], tlList[3:6], tlList[6:9]]
self.tlMult = int(tlList[9])
self.tlNumber = tlNumber
def makeFamily(hklfIn, fileInfo, compDict):
'''Takes an input hklf object. Transforms it the appropriate number of
times to generate additional hklf objects with new indices and comp #s.
Returns them all, formatted, in the correct order.
'''
familyList = [hklfIn.output()]
for trans in fileInfo['transList']:
familyList.insert(
0,hklfIn.nTransform(trans, compDict, fileInfo).output())
return ''.join(familyList)
def getInfo():
'''This makes a dictionary of some information about the run.
hklType: 4 or 5.
hklf5TwinMult: The number of twin components in the input hkl file.
tlCount: Number of independent twin laws to be supplied by user.
tlList: A list of TwinLaw objects.
tlAll: A string encoding all the twin operators that may be applied, with
their multiplicity. Non-merohedral twin laws are encoded as 0's. User-
supplied merohedral twin laws are numbered 1, 2, ...
If the input file is hklf4 and the user supplies a 3fold and a 2fold
twin law, tlAll is '112'. If the input file is hklf5 with 3 components
and the user supplies two 2fold twin laws, tlAll is '0012'.
transList: a list of substrings of tlAll encoding the transformations that
actually must be applied to each reflection. If tlAll is '0112', then
transList is ['1', '2', '11', '12', '112']. The 0's are omitted from
transList because non-merohedrally twin-related reflections have
already been sorted out in data reduction.
nComp: total number of twin-related orientations.'''
#define prompts:
hklTypePrompt = '\nWhat is the format of the input hkl file? Enter 4 or 5: '
hklf5TwinMultPrompt = '\nHow many twin components are in the input file? '
tlCountPrompt = '\nHow many independent (pseudo-)merohedral twin laws will\
be applied? \nInversion twinning should be included if necessary. '
tlPrompt = '\nEnter twin law #{} in ShelXL format. Do not use a negative\
value of N.\n'
#collect information:
hklType = input(hklTypePrompt)
if hklType == '5':
hklf5TwinMult = int(input(hklf5TwinMultPrompt))
else:
hklf5TwinMult = 1
nComp = hklf5TwinMult
tlCount = int(input(tlCountPrompt))
tlList = []
for i in range(tlCount):
tlNumber = i+1
tlStringTemp = input(tlPrompt.format(tlNumber))
tlList.append(TwinLaw(tlStringTemp, str(tlNumber)))
nComp *= tlList[-1].tlMult
#make the transformation list for family generation
tlAll = ''
tlAll += (-1 + hklf5TwinMult) * '0'
for tl in tlList:
tlAll += (-1 + tl.tlMult) * tl.tlNumber
tlAllMero = tlAll.lstrip('0')
transList = []
for i in range(len(tlAllMero)):
transSubListTemp = list(combinations(tlAllMero, i+1))
for trans in transSubListTemp:
if trans not in transList:
transList.append(trans)
#make a dictionary:
fileInfo = {}
for i in ['hklType', 'hklf5TwinMult', 'tlCount', 'tlList', 'tlAll',
'transList', 'nComp']:
fileInfo[i] = locals()[i]
return fileInfo
def main(hklName):
fileInfo = getInfo()
compDict = makeCompDict()
print('\nProcessing...')
inFile = open((hklName + '.hkl'), 'r')
outFile = open((hklName + '-2twin.hkl'), 'w')
while True:
entry = inFile.readline()
if not entry.split() or entry.split()[0:3] == ['0','0','0']: break
hklf = Hklf(entry, fileInfo)
outFile.write(makeFamily(hklf, fileInfo, compDict))
inFile.close()
outFile.close()
print(
'''\nOutput written to {}.
Continue refinement with HKLF 5 and {} BASF parameters.'''
.format((hklName + '-2twin.hkl'), fileInfo['nComp']-1)
)
print('''
################################################################################
2twin: Multiple independent twinning in ShelXL.
Daniel W. Paley, 2017.
Contact: [email protected]
If this program is of use, please cite:
https://doi.org/10.1021/acscentsci.7b00328
################################################################################
''')
if len(sys.argv) == 1 or sys.argv[1] == '--help':
help()
else:
assert sys.version_info[0]==3, "Please use a Python 3 interpreter."
main(sys.argv[1])