Jump to content

Talk:Mod:Hunt Research Group/extract scan geom

From ChemWiki

python script to extract each optimised structure from a scan

  • copy the code into a script called "extract_steps.py"
  • to execute the script type "python3 extract_steps.py file_name (without .log extension)
  • this will print some info to the terminal and create a file file_name.xyz
  • the Standard Orientaion is extracted
#
# extract_steps.py
#
# python script to extract and print the standard orintation of the optimised structure for each step of a scan 
# from a gaussian log file
# to run the script type python extract_steps.py input_file_name (without extension)
#
import sys
import os
dir=os.getcwd()
#
if len(sys.argv) == 1:
  print('to run the script please type: python extract_structure.py input_file_name \(without extension\)')
  sys.exit()
else:
  base=str(sys.argv[1])
  file=base+'.log'
  out=base+'.xyz'
  s='directory is '
  print('{0:}{1:}'.format(s,dir))
  s='input file is '
  print('{0:}{1:}'.format(s,file))
  s='output file is '
  print('{0:}{1:}'.format(s,out))
#close if
#
# set some defaults
col_num=[]
col_atom=[]
col_type=[]
col_x=[]
col_y=[]
col_z=[]
step_num=[]
step_atom=[]
step_type=[]
step_x=[]
step_y=[]
step_z=[]
n=0
scan_step=0
n_step=0
no_atoms=0
ntotal=0
nstep=0
go=False
ntotal=0
rewind=0

# open file and read lines one by one
f=open(file,"r")

line =f.readline()
while line :
  line =f.readline()

#==extract GEOMETRY ==
# check for standard orientation text set go if the text appears
  if 'Standard orientation:' in line:
    go=True
#    s='Standard orientation found '
#    print('{0:}'.format(s))
    no_atoms=0
    n=0
# close if
#
# if go is on then save the structure
  while go:
    n = n+1
    m = 0
    line =f.readline()
    if 'Rotational constants (GHZ):' in line: m=-1
    if '---------' in line: m=1
#    s1='n=  '
#    s2=' m=  '
#    print('{0:}{1:}{2:}{3:}'.format(s1,n,s2,m))
    if n > 4 and m == 0 :
#      print(line)
      no_atoms = no_atoms+1
      ntotal=ntotal+1
      a1,b1,c1,d1,e1,f1=line.rstrip().split()
      col_num.append(int(a1))
      col_atom.append(int(b1))
      col_type.append(int(c1))
      col_x.append(float(d1))
      col_y.append(float(e1))
      col_z.append(float(f1))
#      s='number of atoms  '
#      print('{0:}{1:}'.format(s,no_atoms))
    if m == -1 : go=False
# close while
#
# now check to see if this is a completed opt step, if it is
# then save the last geometry stored
#
# scan_step is the surrent scan step number
# no_atoms is the total number of atoms
# ntotal is the total number in the col array at this point
# rewind takes us back to the previous set of coordinated
# m is the local atom counter
# n is the current counter for the position in the col array
# n_step is the current counter for the position in the step array
  m=0
  if ' !   Optimized Parameters   !' in line:
    s='Optimised structure found '
    print('{0:}'.format(s))
    scan_step=scan_step+1
    rewind=ntotal-(no_atoms+1)
#    s='rewind '
#    print('{0:}{1:}'.format(s,rewind))
    while m < no_atoms :
      m=m+1
      n=rewind+m
#      s1=' scan step ='
#      s2=' step n='
#      s3=' col n='
#      s4=' local m='
#      print('{0:}{1:}{2:}{3:}{4:}{5:}{6:}{7:}'.format(s1,scan_step,s2,nstep,s3,n,s4,m))
      step_num.append(col_num[n])
      step_atom.append(col_atom[n])
      step_type.append(col_type[n])
      step_x.append(col_x[n])
      step_y.append(col_y[n])
      step_z.append(col_z[n])
#      print('{0:>4d} {1: 09.6f} {2: 09.6f} {3: 09.6f}'.format(step_atom[nstep],step_x[nstep],step_y[nstep],step_z[nstep]))
      nstep=nstep+1
#   close while
# close if
#close while
#
# now write to the .xyz file
c=open(out,"w")
s='number of atoms '
print('{0:}{1:}'.format(s,no_atoms))
#c.write('{0:}{1:} \n'.format(s,no_atoms))
s='number of scan steps '
print('{0:}{1:}'.format(s,scan_step))
#c.write('{0:}{1:} \n'.format(s,scan_step))
s='file format: atomic no, xyz coords'
print('{0:}'.format(s))
#c.write('{0:} \n'.format(s))
#s='array counter total '
#print('{0:}{1:}'.format(s,ntotal))
#
# scan_step is the total number of scan steps
# no_atoms is the total number of atoms
# step_n is the scan step
# m is the local atom counter
# n is the current number in the overall array
step_n=0
m=0
n=0
while step_n < scan_step :
  step_n=step_n+1
  m=0
  s='print step '
  print('{0:}{1:}'.format(s,step_n))
  c.write('{0:} \n'.format(step_n))
  while m < no_atoms :
    m=m+1
    s1=' step='
    s3=' array n='
    s2=' local m='
#    print('{0:}{1:}{2:}{3:}{4:}{5:}'.format(s1,step_n,s2,m,s3,n))
#    print('{0:>4d} {1: 09.6f}  {2: 09.6f}  {3: 09.6f}'.format(step_atom[n],step_x[n],step_y[n],step_z[n]))
    c.write('{0:>4d}  {1: 09.6f}  {2: 09.6f}  {3: 09.6f} \n'.format(step_atom[n],step_x[n],step_y[n],step_z[n]))
    n=n+1
# close while
#close while

# close all open files
f.close()
c.close()