-
Notifications
You must be signed in to change notification settings - Fork 20
/
crystal2cif
executable file
·189 lines (167 loc) · 7.31 KB
/
crystal2cif
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# crystal2cif
#
# purpose: extract information from CRYSTAL17 output file into CIF format
# license: MIT License
# author: François-Xavier Coudert
# e-mail: fxcoudert@gmail.com
#
import string
import sys
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: " + sys.argv[0] + " crystal.out [outfile.cif]")
sys.exit(1)
def readUntil(f, s):
while True:
line = f.readline()
if len(line) == 0:
return ""
if s in line:
return line.strip()
spacegroups = {
"P 1": 1, "P -1": 2, "P 2": 3, "P 21": 4, "C 2": 5, "P M": 6, "P C": 7,
"C M": 8, "C C": 9, "P 2/M": 10, "P 21/M": 11, "C 2/M": 12, "P 2/C": 13,
"P 21/C": 14, "C 2/C": 15, "P 2 2 2": 16, "P 2 2 21": 17,
"P 21 21 2": 18, "P 21 21 21": 19, "C 2 2 21": 20, "C 2 2 2": 21,
"F 2 2 2": 22, "I 2 2 2": 23, "I 21 21 21": 24, "P M M 2": 25,
"P M C 21": 26, "P C C 2": 27, "P M A 2": 28, "P C A 21": 29,
"P N C 2": 30, "P M N 21": 31, "P B A 2": 32, "P N A 21": 33,
"P N N 2": 34, "C M M 2": 35, "C M C 21": 36, "C C C 2": 37,
"A M M 2": 38, "A B M 2": 39, "A M A 2": 40, "A B A 2": 41,
"F M M 2": 42, "F D D 2": 43, "I M M 2": 44, "I B A 2": 45,
"I M A 2": 46, "P M M M": 47, "P N N N": 48, "P C C M": 49,
"P B A N": 50, "P M M A": 51, "P N N A": 52, "P M N A": 53,
"P C C A": 54, "P B A M": 55, "P C C N": 56, "P B C M": 57,
"P N N M": 58, "P M M N": 59, "P B C N": 60, "P B C A": 61,
"P N M A": 62, "C M C M": 63, "C M C A": 64, "C M M M": 65,
"C C C M": 66, "C M M A": 67, "C C C A": 68, "F M M M": 69,
"F D D D": 70, "I M M M": 71, "I B A M": 72, "I B C A": 73,
"I M M A": 74, "P 4": 75, "P 41": 76, "P 42": 77, "P 43": 78, "I 4": 79,
"I 41": 80, "P -4": 81, "I -4": 82, "P 4/M": 83, "P 42/M": 84,
"P 4/N": 85, "P 42/N": 86, "I 4/M": 87, "I 41/A": 88, "P 4 2 2": 89,
"P 4 21 2": 90, "P 41 2 2": 91, "P 41 21 2": 92, "P 42 2 2": 93,
"P 42 21 2": 94, "P 43 2 2": 95, "P 43 21 2": 96, "I 4 2 2": 97,
"I 41 2 2": 98, "P 4 M M": 99, "P 4 B M": 100, "P 42 C M": 101,
"P 42 N M": 102, "P 4 C C": 103, "P 4 N C": 104, "P 42 M C": 105,
"P 42 B C": 106, "I 4 M M": 107, "I 4 C M": 108, "I 41 M D": 109,
"I 41 C D": 110, "P -4 2 M": 111, "P -4 2 C": 112, "P -4 21 M": 113,
"P -4 21 C": 114, "P -4 M 2": 115, "P -4 C 2": 116, "P -4 B 2": 117,
"P -4 N 2": 118, "I -4 M 2": 119, "I -4 C 2": 120, "I -4 2 M": 121,
"I -4 2 D": 122, "P 4/M M M": 123, "P 4/M C C": 124, "P 4/N B M": 125,
"P 4/N N C": 126, "P 4/M B M": 127, "P 4/M N C": 128, "P 4/N M M": 129,
"P 4/N C C": 130, "P 42/M M C": 131, "P 42/M C M": 132,
"P 42/N B C": 133, "P 42/N N M": 134, "P 42/M B C": 135,
"P 42/M N M": 136, "P 42/N M C": 137, "P 42/N C M": 138,
"I 4/M M M": 139, "I 4/M C M": 140, "I 41/A M D": 141, "I 41/A C D": 142,
"P 3": 143, "P 31": 144, "P 32": 145, "R 3": 146, "P -3": 147,
"R -3": 148, "P 3 1 2": 149, "P 3 2 1": 150, "P 31 1 2": 151,
"P 31 2 1": 152, "P 32 1 2": 153, "P 32 2 1": 154, "R 3 2": 155,
"P 3 M 1": 156, "P 3 1 M": 157, "P 3 C 1": 158, "P 3 1 C": 159,
"R 3 M": 160, "R 3 C": 161, "P -3 1 M": 162, "P -3 1 C": 163,
"P -3 M 1": 164, "P -3 C 1": 165, "R -3 M": 166, "R -3 C": 167,
"P 6": 168, "P 61": 169, "P 65": 170, "P 62": 171, "P 64": 172,
"P 63": 173, "P -6": 174, "P 6/M": 175, "P 63/M": 176, "P 6 2 2": 177,
"P 61 2 2": 178, "P 65 2 2": 179, "P 62 2 2": 180, "P 64 2 2": 181,
"P 63 2 2": 182, "P 6 M M": 183, "P 6 C C": 184, "P 63 C M": 185,
"P 63 M C": 186, "P -6 M 2": 187, "P -6 C 2": 188, "P -6 2 M": 189,
"P -6 2 C": 190, "P 6/M M M": 191, "P 6/M C C": 192, "P 63/M C M": 193,
"P 63/M M C": 194, "P 2 3": 195, "F 2 3": 196, "I 2 3": 197,
"P 21 3": 198, "I 21 3": 199, "P M 3": 200, "P N 3": 201, "F M 3": 202,
"F D 3": 203, "I M 3": 204, "P A 3": 205, "I A 3": 206, "P 4 3 2": 207,
"P 42 3 2": 208, "F 4 3 2": 209, "F 41 3 2": 210, "I 4 3 2": 211,
"P 43 3 2": 212, "P 41 3 2": 213, "I 41 3 2": 214, "P -4 3 M": 215,
"F -4 3 M": 216, "I -4 3 M": 217, "P -4 3 N": 218, "F -4 3 C": 219,
"I -4 3 D": 220, "P M 3 M": 221, "P N 3 N": 222, "P M 3 N": 223,
"P N 3 M": 224, "F M 3 M": 225, "F M 3 C": 226, "F D 3 M": 227,
"F D 3 C": 228, "I M 3 M": 229, "I A 3 D": 230,
}
try:
f = open(sys.argv[1], "r")
except IOError:
print("Could not open CRYSTAL output file: " + sys.argv[1])
sys.exit(1)
sg = readUntil(f, "SPACE GROUP")
if len(sg) > 0:
sg_name = sg.split(":")[-1].strip()
sg_num = spacegroups[sg_name]
else:
f.seek(0)
sg = readUntil(f, "SPACE GROUP")
if len(sg) > 0:
sg_num = int(sg.split(":")[-1].strip())
for i, j in spacegroups.items():
if sg_num == j:
sg_name = i
else:
print("Space group could not be found. Assuming P1. Please check.")
sg_name = "P 1"
sg_num = 1
f.seek(0)
if len(sys.argv) >= 3:
outname = sys.argv[2]
else:
outname = sys.argv[1].replace(".out", "") + ".cif"
try:
fout = open(outname, "w")
except IOError:
print("Could not open CIF file for writing: " + outname)
sys.exit(1)
# Check whether we have a crystallographic cell that differs from the
# primitive cell (in which case, we'll use the crystallographic cell).
pos = f.tell()
if len(readUntil(f, "TRANSFORMATION MATRIX PRIMITIVE-CRYSTALLOGRAPHIC CELL")) > 0:
crystCell = True
else:
crystCell = False
f.seek(pos)
counter = 1
while True:
# Read while we can still find structures
if crystCell:
line = readUntil(f, "CRYSTALLOGRAPHIC CELL (VOLUME=")
else:
line = readUntil(f, "PRIMITIVE CELL -")
if len(line) == 0:
break
f.readline()
cell = f.readline().split()
if len(cell) != 6:
print("Weird number of cell parameters. Aborting.")
sys.exit(1)
fout.write("data_foo_" + str(counter) + "\n")
fout.write("_symmetry_space_group_name_H-M '" + sg_name + "'\n")
fout.write("_space_group_IT_number " + str(sg_num) + "\n")
fout.write("_cell_length_a " + str(cell[0]) + "\n")
fout.write("_cell_length_b " + str(cell[1]) + "\n")
fout.write("_cell_length_c " + str(cell[2]) + "\n")
fout.write("_cell_angle_alpha " + str(cell[3]) + "\n")
fout.write("_cell_angle_beta " + str(cell[4]) + "\n")
fout.write("_cell_angle_gamma " + str(cell[5]) + "\n")
fout.write("loop_\n")
fout.write("_atom_site_label\n")
fout.write("_atom_site_type_symbol\n")
fout.write("_atom_site_fract_x\n")
fout.write("_atom_site_fract_y\n")
fout.write("_atom_site_fract_z\n")
readUntil(f, "*************************************************")
if not crystCell:
readUntil(f, "*************************************************")
n = 0
while True:
line = f.readline().split()
if len(line) != 7:
break
if line[1] != "T":
continue
n = n + 1
at = line[3][0] + line[3][1:].lower()
fout.write(at + str(n) + " " + at + " " + " ".join(line[4:]) + "\n")
ener = readUntil(f, "TOTAL ENERGY(DFT)(AU)")
if len(ener) > 48:
fout.write("# Energy: " + ener[26:48].strip() + "\n")
fout.write("\n\n")
counter = counter + 1
f.close()
print(str(counter-1) + " structures successfully written!")