Converting Trajectory File to xyz Format

The optimization trajectory file is written in .traj format by ASE package. In order to visualize it you can convert it to .xyz format using this script.
You can append this script at the end of your python script for geometry optimization. When the optimization is over, it will read the trajectory file (line 8) and convert it to .xyz format. Make sure name of the trajectory file created during optimization and the one specified on line 8 match.

 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
#!/usr/bin/python

from ase.io.trajectory import PickleTrajectory
import io, os

#reading in the trajectory file created during 
#optimization
traj = PickleTrajectory("CO-Pd-ontop.traj")
#getting number of steps it took for geometry 
#optimization
nsteps = dyn.get_number_of_steps()

string = 'structure'

#get current working directory and make a scratch 
#directory
path = os.getcwd()
path = path + '/scratch'
if not os.path.exists(path): os.makedirs(path)

#output file name
outFileName = 'trajectory.xyz'
#write each structure from the .traj file in .xyz format
for i in range(0, nsteps+1):
    atoms = traj[i]
    string = 'structure%03d' % (i,) +'.xyz'
    outStruct = os.path.join(path, string)
    write(outStruct, atoms)
#combines all optimization structures in one trajectory 
#file
    inFile = open(os.path.join(path, 'structure%03d' % 
                  (i,)  +'.xyz'), 'r')
    fileStr = inFile.read()
    outFile = open(outFileName, 'a')
    outFile.write(fileStr)