-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxyz2gaussian_aux.py
432 lines (319 loc) · 10.2 KB
/
xyz2gaussian_aux.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A collection of functions for xyz2gaussian
@authors: A. Gomółka, Z. Wojdyla, T. Borowski
last update: 18.05.2023
"""
import math, re
'''
Note:
For the head_add_chk_label() and head_add_oldchk() functions - the file must end with a ".chk" extension
'''
def count_lines(file):
"""
Counts number of lines in a file
Parameters
----------
file : file object
Returns
-------
i: int
number of lines in a file
"""
file.seek(0)
i = -1
for i, l in enumerate(file):
pass
file.seek(0)
return i + 1
def int_digits(n):
"""
For a positive integer n returns its number of digits
Parameters
----------
n : INT
Returns
-------
digits : int
number of digits
"""
digits = int(math.log10(n))+1
return digits
def read_head_tail(file):
"""
Reading the contents of a file
Parameters
----------
file : file object
Returns
-------
split_line: list
a list containing the following lines of the file
"""
file.seek(0)
line = file.read() # this line reads the entire file
split_line = line.splitlines()
file.seek(0)
return split_line
def read_xyz(file):
"""
Listing x, y and z coordinates
Parameters
----------
file : file object
Returns
-------
numberOfatoms : int
number of atoms
comment : str
string containing the comment
geo : list
a list with x, y and z coordinates
"""
comment = ""
numberOfatoms = int(file.readline())
comment = file.readline() # taking a comment
comment = comment.rstrip('\n')
comment = comment.lstrip()
geo = []
for j in range(numberOfatoms):
floatString = ""
line = file.readline()
split_line = line.split()
split_line = ['{:10.6f}'.format(float(x)) for x in split_line[1:]]
floatString = " ".join(split_line)
geo.append(floatString)
return numberOfatoms, comment, geo
def read_body(file):
"""
Listing all data except for coordinates x, y and z
Parameters
----------
file : file object
Returns
-------
body : list
a list of tuples containing all data except x, y and z
"""
file.seek(0)
body = []
n_lines = count_lines(file)
split_line = file.readline()
split_line = split_line.split()
index_floatList = []
for i, variable in enumerate(split_line):
if ((re.match("^[-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$", variable)) or (re.match("^[-]?[0-9]+$", variable))):
index_floatList.append(i)
ints = [int(x) for x in index_floatList]
if (len(index_floatList) == 4):
ints.pop(0)
for _ in range(n_lines):
bodyStringRight = ""
bodyStringLeft = ""
bodyStringLeft = "\t".join(split_line[:ints[0]])
bodyStringRight = "\t".join(split_line[(ints[2] + 1):])
body.append(tuple([bodyStringLeft,bodyStringRight]))
split_line = file.readline()
split_line = split_line.split()
file.seek(0)
return body
def head_remove_guess(head):
"""
Removing "guess = read" expression from head
Parameters
----------
head : list
Returns
-------
new_head : list
a list containing elements of a new head
"""
head_new = head.copy()
new_head = []
for i in head_new:
if "guess=read" in i:
i = i.replace("guess=read","")
new_head.append(i)
return new_head
def head_add_chk_label(head,label_digits,index):
"""
Adding a label specifying the file number and data set number
Parameters
----------
head : list
label_digits : int
index : int
Returns
-------
head_new : list
a list containing elements of a new head
"""
head_new = head.copy()
line_withoutLabel = ', '.join([item for item in head_new if item.startswith('%Chk' or '%chk')])
index_line_withoutLabel = head_new.index(line_withoutLabel)
start_fileName = line_withoutLabel.find("=") + len("=")
end_filename = line_withoutLabel.find(".chk") # the file must end with a ".chk" extension
file_name = line_withoutLabel[start_fileName:end_filename]
label = str(index).zfill(label_digits)
fileName_withLabel = file_name + label
head_withLabel = line_withoutLabel.replace(file_name,fileName_withLabel)
head_new[index_line_withoutLabel] = head_withLabel
return head_new
def head_add_oldchk(head,label_digits,index):
"""
Adding a label specifying the previous file number with the phrase "Old"
Parameters
----------
head : list
label_digits : int
index : int
Returns
-------
head_new : list
a list containing elements of a new head
"""
head_new = head.copy()
head_firstLine = ', '.join([item for item in head_new if item.startswith('%Chk' or '%chk')])
head_new = head_add_chk_label(head_new, label_digits, index) # head z label
start_fileName = head_firstLine.find("=") + len("=")
end_filename = head_firstLine.find(".chk") # the file must end with a ".chk" extension
file_name = head_firstLine[start_fileName:end_filename]
label = str(index-1).zfill(label_digits)
fileName_withLabel = file_name + label
head_withLabel = head_firstLine.replace(file_name, fileName_withLabel)
start_old = head_withLabel.find("%") + len("%")
head_with_oldAndlabel = head_withLabel[:start_old] + "Old" + head_withLabel[start_old:]
head_new.insert(0, head_with_oldAndlabel)
return head_new
def head_change_comment(head, comm_line):
"""
Adding a comment in place of the word "test"
Parameters
----------
head : list
comm_line : str
Returns
-------
new_head : list
a list containing elements of a new head
"""
new_head = head.copy()
new_head[-3] = comm_line
return new_head
def gen_file_name(xyz_fileName, label_digits,index):
"""
Generating a file name - adding a label and changing the extension
Parameters
----------
xyz_fileName : str
label_digits : int
index : int
Returns
-------
new_fileName : str
a string that specifies the new file name
"""
start_fileName = 0
end_filename = xyz_fileName.find(".xyz")
new_fileName = xyz_fileName[start_fileName:end_filename]
label = str(index).zfill(label_digits) + ".com"
new_fileName = new_fileName + label
return new_fileName
def gen_new_body(body, geo):
"""
Combining the contents of the body list with the contents of the geo list
Parameters
----------
body : tuples list
geo : tuples list
Returns
-------
newBody_list : list
a list consisting of geo and body lists
"""
list_length = len(body)
newBody_list = []
newBody = ""
for i in range(list_length):
if (len(body[i][1]) > 0):
newBody = body[i][0] + "\t" + geo[i] + "\t" + body[i][1]
else:
newBody = body[i][0] + "\t" + geo[i]
newBody_list.append(newBody)
return newBody_list
def write_g_input(out_file_name, head_new, body_new, tail):
"""
Create a file with specific content
Parameters
----------
out_file_name : str
head_new : list
body_new : list
tail : list
Returns
-------
Specified file containing head, body, x, y and z coordinates, and tail
"""
file_output = open(out_file_name, 'w')
for part in [head_new, body_new, tail]:
for i in range(len(part)):
file_output.write(part[i] + "\n")
file_output.close()
def div_into_lists(read_file):
"""
Dividing the main input file into 3 lists: head, body, and tail
Parameters
----------
read_file : file
Returns
-------
lists : head, body, tail
"""
blankLine_counter = 0
head_end_index = 0
body_end_index = 0
for i, var in enumerate(read_file):
if (read_file[i].strip() == ''):
blankLine_counter += 1
if (blankLine_counter == 2):
head_end_index = i + 2 # the beginning of the body (index)
if (blankLine_counter == 3):
body_end_index = i + 1 # the beginning of the tail (index)
break
head = ("\n".join(read_file[0:head_end_index]))
tail = " \n"
tail = tail + ("\n".join(read_file[body_end_index:]))
tail = tail + " \n"
head = head.splitlines()
tail = tail.splitlines()
body = []
split_lines = read_file[head_end_index:body_end_index]
n_lines = len(split_lines)
split_line = split_lines[0].split()
index_floatList = []
for i, variable in enumerate(split_line):
if ((re.match("^[-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$", variable)) or (re.match("^[-]?[0-9]+$", variable))):
index_floatList.append(i)
ints = [int(x) for x in index_floatList]
if (len(index_floatList) == 4):
ints.pop(0)
for i in range(1,n_lines):
bodyStringRight = ""
bodyStringLeft = ""
bodyStringLeft = "\t".join(split_line[:ints[0]])
bodyStringRight = "\t".join(split_line[(ints[2] + 1):])
body.append(tuple([bodyStringLeft,bodyStringRight]))
split_line = split_lines[i]
split_line = split_line.split()
return head, body, tail
def print_help():
help_text = """
A script to generate a series of Gaussian input files for a series of geometries
stored in a single xyz file
Reads: #1 gaussian template input file with %Chk= line
#2 xyz file with geometries for which input files are to be created
Outputs: a series of files with names based on the name of the xyz file and ending
with a sequence number: _0XYZ.com
"""
print(help_text)