-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcry2cif.py
More file actions
executable file
·73 lines (68 loc) · 2.69 KB
/
Copy pathcry2cif.py
File metadata and controls
executable file
·73 lines (68 loc) · 2.69 KB
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
#!/usr/bin/env python3
from sys import argv
import numpy as np
def SkipLines(fd, nline):
for i in range(nline):
fd.readline()
def WriteCif(param, atomsymbol, factcoor, filename = 'naphthalene'):
outp = []
atomamount = {}
atomformat = ' {0:<8s}1.0{1[0]:15.10f}{1[1]:15.10f}{1[2]:15.10f} Biso{2:15.10f} {3:2s}\n'.format
outp.append('#' * 70 + '\n')
outp.append('#' + ' ' * 68 + '#\n')
outp.append('#' + ' ' * 23 + 'Optimized Crystal Data' + ' ' * 23 + '#\n')
outp.append('#' + ' ' * 68 + '#\n')
outp.append('#' + ' ' * 68 + '#\n')
outp.append('#' * 70 + '\n')
outp.append('\ndata_%s\n\n' % filename)
outp.append('%-38s%-15s\n' % ('_cell_length_a', param[0]))
outp.append('%-38s%-15s\n' % ('_cell_length_b', param[1]))
outp.append('%-38s%-15s\n' % ('_cell_length_c', param[2]))
outp.append('%-38s%-15s\n' % ('_cell_angle_alpha', param[3]))
outp.append('%-38s%-15s\n' % ('_cell_angle_beta', param[4]))
outp.append('%-38s%-15s\n' % ('_cell_angle_gamma', param[5]))
outp.append('%-38s%-15s\n' % ('_symmetry_space_group_name_H-M', "'P 1'"))
outp.append('%-38s%-15s\n\n' % ('_symmetry_Int_Tables_number', '1'))
outp.append("loop_\n_symmetry_equiv_pos_as_xyz\n 'x, y, z'\n\nloop_\n")
outp.append(' _atom_site_label\n _atom_site_occupancy\n')
outp.append(' _atom_site_fract_x\n _atom_site_fract_y\n _atom_site_fract_z\n')
outp.append(' _atom_site_adp_type\n _atom_site_B_iso_or_equiv\n _atom_site_type_symbol\n')
for symbol, coor in zip(atomsymbol, factcoor):
atomamount[symbol] = atomamount.get(symbol, 0) + 1
atomname = symbol + str(atomamount[symbol])
outp.append(atomformat(atomname, coor, 1.0, symbol))
with open(filename + '.cif', 'w') as f:
f.write(''.join(outp))
factcoor = [] # Fractional coordinates
lattice = [] # Lattice vectors of supercell
if len(argv) != 2:
print("Usage: cry2cif.py xx.out")
elif argv[1][-4 :] == '.out':
readf = False
f = open(argv[1], 'r')
print('Reading ' + argv[1] + '...')
while True:
try:
line = f.readline()
except UnicodeDecodeError:
continue
if line[: 25] == ' FINAL OPTIMIZED GEOMETRY':
atomsymbol, factcoor = [], []
SkipLines(f, 5)
param = f.readline().split() #lattice parameters
SkipLines(f, 4)
while True:
line = f.readline()
if line == '\n': break
sline = line.split()
atomsymbol.append(sline[3])
factcoor.append(sline[4 :])
readf = True
break
if not readf:
raise IOError('Final optimized geometry not found!')
f.close()
factcoor = np.array(factcoor, dtype = 'double')
WriteCif(param, atomsymbol, factcoor, argv[1].split('.')[0]) # Write cif for visualization
else:
raise ValueError('Only output file of CRYSTAL17 named "xx.out" is supported!')