Jump to content

Talk:Mod:Hunt Research Group/hist to xyz

From ChemWiki

A script to convert a PL_POLY HISTORY file to an xyz file

  • copy and paste the script into a file called his2xyz.py
  • you will need to alter the atomList to match the 3letter names for your atoms!
  • run by typing: python his2xyz.py HISTORY_file XYZ_file
once you have downloaded it remove the .rtf extension and save with the simple filename "HISTORY"
#!/usr/bin/env python
import sys,string

#run this script by typing python his2xyz.py his_file *.xyz
# his_file is the input HISTORY file
# *.xyz is the output xyz coordinates file

# a list of the atom labels that can be read
# you will need to change these for your file!
# watch out for missing one or two, then the number of atoms in the *.xyz file will be wrong
atomList=['NA','CR','CW','C1','CE','HCR','HCW','HC','H1','OBT','SBT','NBT','FSI']

# arguments from running the file,
# argv[1]=HISTORY file
# argv[2]=output *.xyz file
inputFile=open(sys.argv[1],'r')
outFile=open(sys.argv[2],'w')

#HISTORY file foromat is
#title line (a80)
#data line (3i10) 
#  example:          0         1      7168                30000            430200002
#  this is (traj key) (periodic boundry key) (number of atoms in cell)
#  don't know what the rest is
#
#then lines repeat for each timestep with the following
#data line (a8,4i10,f12.6)
#  example: timestep         1      7168 0 1            0.001000            0.001000
#  (timestep) (current time step) (number of atoms in cell) i
#  (traj key) (periodic boundry key) (timestep) (current time)
#cell parameters (3g12.4)
#  example:       44.0395918372        0.0000000000        0.0000000000
#  one line each for a,b,c cell unit vectors in x,y,z coords
#  so a cubic cell will only have values on the diagonals 
#then series of lines for the atoms (a8,i10,2f12.6)
#  example:NA               1     14.007000      0.150000      0.010361
#  atomic label, atom index, atomic mass,  atomic charge
#then the coordinates x,y,z (3e12.4)
#  example:    -10.59313518        -14.80289434         2.774588572
#then the forces x,y,z (3e12.4)
#  but only if trajectory key is greater than one

#the VMD xyz file format is 
#number of atoms
#comment (normally the time step)
#atom_name x y z 
#
#read the first line of history file
title=inputFile.readline()
#print(title)

#read second line of history file
#and break the stings up into their constituents
#print out the total atoms and totoal number of frames
line=inputFile.readline()
#print(line)
line=line.rstrip().split()
#print(line[0])
if line[0]=='0':
    total_steps=line[3]
    print('total number of steps is {0}'.format(total_steps))
    total_atoms=line[2]+'\n'
    print('total atoms {0}'.format(total_atoms))

#read a new line
#now loop through reading the lines
#if this line has content do the following 
#otherwise quit
line=inputFile.readline()
line=line.rstrip().split()
while line: 

#if the first component if the line is the word timestep
#then identify the timestep 
#and write the total atoms and timestep to the output *.xyz file
   if line[0]=='timestep':
      time_step=line[1]+'\n'
      print('timestep is {0}'.format(time_step))
      outFile.write('{0}'.format(total_atoms))
      outFile.write('time step {0}'.format(time_step))
      exit

#extract everything before the 3rd character from the line string
#if this letter combination is found in the atomList array 
#then write this atomName into the *.xyz file
#and read and write out the xys coorinates on the same line (ie use \t ranther than \n)
# 
   if line[0][:3] in atomList:
      atom_name=line[0]+'\t'
#      print('atom name is {0}'.format(atom_name))
      outFile.write(atom_name)
      xyzline=inputFile.readline()
      outFile.write(xyzline)

#read another line from the input file
#and cycle back in the while loop
   line=inputFile.readline()
   line=line.rstrip().split()
#   print(line)

#close the open files
inputFile.close()
outFile.close()