Guest User

Not correct script to extract structure from G09 log file

a guest
Dec 8th, 2016
424
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # python Extract_Optimized_From_Gaussian.py filename
  3.  
  4. from __future__ import print_function
  5. import sys, os
  6.  
  7. def extract_all(text, target):
  8.     linenums = []
  9.     # Start count at 1 because files start at line 1 not 0
  10.     count = 1
  11.     for line in text:
  12.         if (line.find(target)) > -1:
  13.             linenums.append(count)
  14.         count += 1
  15.     return linenums
  16.  
  17. code = {"1" : "H", "2" : "He", "3" : "Li", "4" : "Be", "5" : "B", \
  18. "6"  : "C", "7"  : "N", "8"  : "O", "9" : "F", "10" : "Ne", \
  19. "11" : "Na" , "12" : "Mg" , "13" : "Al" , "14" : "Si" , "15" : "P", \
  20. "16" : "S"  , "17" : "Cl" , "18" : "Ar" , "19" : "K"  , "20" : "Ca", \
  21. "21" : "Sc" , "22" : "Ti" , "23" : "V"  , "24" : "Cr" , "25" : "Mn", \
  22. "26" : "Fe" , "27" : "Co" , "28" : "Ni" , "29" : "Cu" , "30" : "Zn", \
  23. "31" : "Ga" , "32" : "Ge" , "33" : "As" , "34" : "Se" , "35" : "Br", \
  24. "36" : "Kr" , "37" : "Rb" , "38" : "Sr" , "39" : "Y"  , "40" : "Zr", \
  25. "41" : "Nb" , "42" : "Mo" , "43" : "Tc" , "44" : "Ru" , "45" : "Rh", \
  26. "46" : "Pd" , "47" : "Ag" , "48" : "Cd" , "49" : "In" , "50" : "Sn", \
  27. "51" : "Sb" , "52" : "Te" , "53" : "I"  , "54" : "Xe" , "55" : "Cs", \
  28. "56" : "Ba" , "57" : "La" , "58" : "Ce" , "59" : "Pr" , "60" : "Nd", \
  29. "61" : "Pm" , "62" : "Sm" , "63" : "Eu" , "64" : "Gd" , "65" : "Tb", \
  30. "66" : "Dy" , "67" : "Ho" , "68" : "Er" , "69" : "Tm" , "70" : "Yb", \
  31. "71" : "Lu" , "72" : "Hf" , "73" : "Ta" , "74" : "W"  , "75" : "Re", \
  32. "76" : "Os" , "77" : "Ir" , "78" : "Pt" , "79" : "Au" , "80" : "Hg", \
  33. "81" : "Tl" , "82" : "Pb" , "83" : "Bi" , "84" : "Po" , "85" : "At", \
  34. "86" : "Rn" , "87" : "Fr" , "88" : "Ra" , "89" : "Ac" , "90" : "Th", \
  35. "91" : "Pa" , "92" : "U"  , "93" : "Np" , "94" : "Pu" , "95" : "Am", \
  36. "96" : "Cm" , "97" : "Bk" , "98" : "Cf" , "99" : "Es" ,"100" : "Fm", \
  37. "101": "Md" ,"102" : "No" ,"103" : "Lr" ,"104" : "Rf" ,"105" : "Db", \
  38. "106": "Sg" ,"107" : "Bh" ,"108" : "Hs" ,"109" : "Mt" ,"110" : "Ds", \
  39. "111": "Rg" ,"112" : "Uub","113" : "Uut","114" : "Uuq","115" : "Uup", \
  40. "116": "Uuh","117" : "Uus","118" : "Uuo"}
  41.  
  42. logfile_fn = os.path.basename(sys.argv[1])
  43. logfile_bn = os.path.splitext(logfile_fn)[0]
  44. logfile_fh = open(logfile_fn, 'r')
  45. text = logfile_fh.readlines()
  46. logfile_fh.close()
  47.  
  48. # Find all lines that contain "!   Optimized Parameters   !"
  49. opt_param_line_nums = extract_all(text, '!   Optimized Parameters   !')
  50. # Then find all lines that have "Input orientation:"
  51. input_orient_line_nums = extract_all(text, 'Input orientation')
  52. # And all lines that have the ---- which we will use to find the ends of the coordinate sections
  53. end_coor_line_nums = extract_all(text, '---------')
  54. # Add 5 because the first actual coordinate is +5 lines
  55. input_orient_line_nums = [x+5 for x in input_orient_line_nums]
  56. # Make sure they're sorted
  57. opt_param_line_nums.sort()
  58. input_orient_line_nums.sort()
  59. end_coor_line_nums.sort()
  60.  
  61. # Now we find the "Input Orientation" that come after "!   Optimized Parameters   !"
  62. new_input_lns = []
  63. count = 0
  64. for x in input_orient_line_nums:
  65.     if x > opt_param_line_nums[count]:
  66.         new_input_lns.append(x)
  67.         count += 1
  68.  
  69. new_end_lns = []
  70. count = 0
  71. for x in end_coor_line_nums:
  72.     if count > len(new_input_lns)-1:
  73.         break
  74.     if x > new_input_lns[count]:
  75.         new_end_lns.append(x)
  76.         count += 1
  77.  
  78. intervals = zip(new_input_lns, new_end_lns)
  79.  
  80. # Now we convert each interval into a .xyz
  81. outfile = open(logfile_bn+'.xyz','w')
  82. for intvl in intervals:
  83.     n_atoms = intvl[1] - intvl[0]
  84.     outfile.write(str(n_atoms)+"\n\n")
  85.     for x in range(intvl[0]-1, intvl[1]-1):
  86.         column = text[x].split()
  87.         print(code[column[1]],float(column[3]),float(column[4]),float(column[5]), file=outfile)
  88.  
  89. outfile.close()
RAW Paste Data