Wednesday, July 24, 2013

A python program for reading coordinates and gradients from a GAMESS output file

Some people liked the previous post on python, so I dusted off this python program for reading locating coordinates and gradients in a GAMESS log file and writing out and .xyz+vib file (to create this post).  I have only used it on this output file, but it should work for other files as well.

Even if you have no interest in .xyz+vib files, the program might be useful as an example of how to locate specific information in a file and read it in. If you have similar programs (or questions) please feel free to share them in the comments

"""\
This python program extracts coordinates and gradients from a GAMESS output
file and creates a .xzy+vib file. Note that the sign of the gradient is chaged to produce the force
To use it type (assuming opt.py and the output file is in the same directory):
python opt.py x xx > xxx.xyz
where x is the number of atoms in the molecule and xx is the name of the GAMESS output file. xxx is whatever you want to call your xyz file.
Written by Jan H. Jensen, released under the GNU GPL license
"""
import string,sys
atoms = eval(sys.argv[1]) # convert x to number
filename = sys.argv[2]
file = open(filename,'r')
coord = []
grad = []
while 1:
line = file.readline() # read a line from he file
if not line: break # if end-of-file: quit
words = string.split(line) # split line into words
# equilibrium coordinates are printed out a second time and should not be read
if len(words) > 1 and words[1] == 'EQUILIBRIUM': break
# find and read coordniates
if len(words) > 0 and words[0] == 'COORDINATES': # find line with EQUILIRBIUM as the 1st word
file.readline() # skip a line
file.readline() # skip a line
for i in range(atoms): # read in x, y, z coordinates
line = file.readline()
words = string.split(line)
coord.append((words[0],words[2],words[3],words[4]))
# find and read gradient
if len(words) > 0 and words[0] == 'NSERCH=': # find line with NSERCH= as the 1st word
for i in range(6):
file.readline() # skip 6 lines
for i in range(atoms): # read x, y, z component of gradient
line = file.readline()
words = string.split(line)
grad.append((eval(words[3]),eval(words[4]),eval(words[5]))) # gradients to be multiplied by -1, so convert to number
# print in .xyz+vib format and change sign of gradient
scale = -1.
for i in range( len(coord) ):
if i%atoms == 0: print atoms
if i%atoms == 0: print 'title'
tog = (coord[i][0],coord[i][1],coord[i][2],coord[i][3],scale*grad[i][0],scale*grad[i][1],scale*grad[i][2])
print '%s %s %s %s %f %f %f' % tog
view raw opt.py hosted with ❤ by GitHub

No comments: