Talk:Mod:Hunt Research Group/lam to xyz

From ChemWiki
Jump to: navigation, search

A script to reduce the number of steps in a LAMMPS traj file

  • copy and paste the script into a file called lam_cut.py
  • run by typing: python lam_cut.py lammps_input_file lammps_output_file no_steps
lammps_input_file is the long lammps trajectory file
lammps_output_file is the new short lammps trajectory file
no_steps is the number of steps to cut from a large file
#!/usr/bin/env python
import sys,string

# run this script by typing python lam_cut lammps_traj_file short_traj_file no_steps 
# lammps_file is the input lammps trajectory file
# short_traj_file is the output still as lammps trajectory but shorter 
# no_steps is the number of trajectory frames to record

# arguments from running the file,
# argv[1]=lammps in file
# argv[2]=lammps out file 
# argv[3]=number of frames to cut
inputFile=open(sys.argv[1],'r')
outFile=open(sys.argv[2],'w')
no_frames=int(sys.argv[3])
print('number of frames to save {0}'.format(no_frames))

#lammps file foromat is
#title time step followed by timestep
#  exammple:
#  ITEM: TIMESTEP
#  0
#title number of atoms folled by the number
# exammple:
# ITEM: NUMBER OF ATOMS
# 14608
#title box information followed by numbers
#where xlo and xhi define the box dimension ends (in this case for x axis)
#  example:
#  ITEM: BOX BOUNDS pp pp pp
#  1.8280820780474468e+01 6.1719179219536784e+01
#  2.7421231170696949e+01 9.2578768829227556e+01
#  2.5136128573138141e+01 8.4863871426850039e+01
#then the list of atoms and their coordinates
#  example:
#  ITEM: ATOMS element xu yu zu
#  N 24.4607 26.7811 30.8739
#  O 25.8947 28.7289 31.7749
#  O 22.0142 26.5087 30.1196

#now lets start the conversion
#loop through reading the lines
#otherwise quit
count=int(1)
while count <= no_frames:
   print('count {0}'.format(count))

#  timestep
   line=inputFile.readline()
   outFile.write(line)
   line=inputFile.readline()
   outFile.write(line)
   line=line.rstrip().split()
   time_step=line[0]
   print('timestep is {0}'.format(time_step))

#  number of atoms
   line=inputFile.readline()
   outFile.write(line)
   line=inputFile.readline()
   outFile.write(line)
   line=line.rstrip().split()
   total_atoms=int(line[0])
#   print('total atoms is {0}'.format(total_atoms))

#  box dimensions
   line=inputFile.readline()
   outFile.write(line)
   line=inputFile.readline()
   outFile.write(line)
   line=inputFile.readline()
   outFile.write(line)
   line=inputFile.readline()
   outFile.write(line)

#   reading atom name and coordinates
#   write out atom name and xyz coorinates 
   line=inputFile.readline()
   outFile.write(line)
   i=1
   while i <= total_atoms:
      line=inputFile.readline()
      outFile.write(line)
      i=i + 1
   count=count + 1
#endwhile

#close the open files
inputFile.close()
outFile.close()
#print('requested number of frames = {0}'.format(no_frames))
print('printed number of frames = {0}'.format(count-1))