-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrystal17_to_cif.py
71 lines (66 loc) · 2.96 KB
/
crystal17_to_cif.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
import re, os, glob, subprocess
#Remove PROCESS lines that might be interfering with file order
subprocess.run('sed -i "/^ PROCESS/d" *.out',shell=True, check=True)
def crystal17_to_cif(input_file, output_file):
with open(input_file, 'r') as f:
cell_counter = atom_counter = num_atoms = 0
atom_data = []
for i,line in enumerate(f):
# Extract cell parameters
if line.startswith(" PRIMITIVE CELL"):
cell_counter = i
if cell_counter > 0 and i == cell_counter+2:
cell_params = line
# Extract number of atoms
if line.startswith(' ATOMS IN THE ASYMMETRIC UNIT'):
num_atoms = int(line.split()[-1])
atom_counter = i
# Extract atomic positions
if i >= atom_counter+3 and i < atom_counter+3+num_atoms and i>3:
atom_data.append([line.split()[3].capitalize(), line.split()[4],line.split()[5],line.split()[6]])
# Determine if bulk or slab calculation
if line.startswith(' CRYSTAL CALCULATION'):
calc_type = 'bulk'
if line.startswith(' SLAB CALCULATION') or line.startswith(' * TWO DIMENSIONAL SLAB'):
calc_type = 'slab'
f.close()
atom_data = atom_data[-num_atoms:]
a,b,c,alpha,beta,gamma = cell_params.split()
# Write CIF file
with open(output_file, 'w') as f:
f.write('data_'+str(input_file)+'\n')
f.write('_cell_length_a {}\n'.format(a))
f.write('_cell_length_b {}\n'.format(b))
if calc_type == 'bulk':
f.write('_cell_length_c {}\n'.format(c))
f.write('_cell_angle_alpha {}\n'.format(alpha))
f.write('_cell_angle_beta {}\n'.format(beta))
f.write('_cell_angle_gamma {}\n'.format(gamma))
f.write('loop_\n')
f.write('_atom_site_label\n')
f.write('_atom_site_fract_x\n')
f.write('_atom_site_fract_y\n')
f.write('_atom_site_fract_z\n')
for atom in atom_data:
f.write('{} {} {} {}\n'.format(atom[0], atom[1], atom[2], atom[3]))
if calc_type == 'slab':
f.write('_cell_length_c {}\n'.format(40))
f.write('_cell_angle_alpha {}\n'.format(alpha))
f.write('_cell_angle_beta {}\n'.format(beta))
f.write('_cell_angle_gamma {}\n'.format(gamma))
f.write('loop_\n')
f.write('_atom_site_label\n')
f.write('_atom_site_fract_x\n')
f.write('_atom_site_fract_y\n')
f.write('_atom_site_fract_z\n')
for atom in atom_data:
f.write('{} {} {} {}\n'.format(atom[0], atom[1], atom[2], str(float(atom[3])/40)))
DIR = (os.getcwd()+'/')
nDIR = len(DIR)
ntype = len(".out")
pathlist = glob.glob(DIR+'*.out')
for path in pathlist:
path_in_str = str(path)
material = path_in_str[nDIR:-ntype]
if material == "":break
crystal17_to_cif(material+'.out',material+'.cif')