Jump to content

Mod:Hunt Research Group:freq script

From ChemWiki
  • Save the following as script_name.py wherever you want.
  • Follow the instructions to run
  • Feel free to improve!
import os
import glob
import sys

############
#HOW TO RUN#
############

#go to the folder with frequency log files in
#this script currently doesn't work if there are .log file present which don't contain frequency analysis, you will have to change it yourself if you want that.
#this script outputs various data from the frequency files to the terminal including relative values, which are calculated relative to the reference_file.log
#run as follows:

#python path/to/script.py reference_file.log

##################################################################

#setting the reference file, this file is the 0 level for all relative values

ref_file = sys.argv[1]
print "ref_file = " + (ref_file)

#making a list of all .log files in current directory

log_files = []
for log_file in glob.glob('*.log'):
    if log_file != ref_file:
        log_files.append(log_file)

print "list of files: "
print log_files

#defining the strings which will be searched for in each file

e_elec_string = 'SCF Done'
zpe_string = 'Zero-point correction='
e_string = 'Sum of electronic and thermal Energies='
h_string = 'Sum of electronic and thermal Enthalpies='
g_string = 'Sum of electronic and thermal Free Energies='
freq_string = 'Low frequencies ---'
im_freq_string = 'imaginary frequencies (negative Signs)'
conver_string = 'Stationary point found.'

#fetching all data for the reference file

f = open(ref_file, 'r')

low_freqs_list = []
conver = 'no'
im_freq_num = '0'

for line in f:
    if e_elec_string in line:
        ref_e_elec_au = line[(len(e_elec_string)+15):(len(e_elec_string)+35)]
        ref_e_elec_au = float(ref_e_elec_au)
    if zpe_string in line:
        ref_zpe_au = line[(len(zpe_string)+1):(len(zpe_string)+37)]
        ref_zpe_au = float(ref_zpe_au)
    if e_string in line:
        ref_e_au = line[(len(e_string)+1):(len(e_string)+35)]
        ref_e_au = float(ref_e_au)
    if h_string in line:
        ref_h_au = line[(len(h_string)+1):(len(h_string)+35)]
        ref_h_au = float(ref_h_au)
    if g_string in line:
        ref_g_au = line[(len(g_string)+1):(len(g_string)+35)]
        ref_g_au = float(ref_g_au)
    if freq_string in line:
        low_freqs = line[(len(freq_string)+1):]
        low_freqs = low_freqs.split()
        low_freqs_list = low_freqs_list + low_freqs;
    if conver_string in line:
        conver = 'yes'
    if im_freq_string in line:
        im_freq_num = line[7:12]
        im_freq_num = str(int(im_freq_num))

f.close()

#assigning/calculating all data fields to print for the reference file

ref_low_freqs = []
for low_freq in low_freqs_list:
    low_freq = str(int(round(float(low_freq))))
    ref_low_freqs.append(low_freq)
low_freqs_string = ",".join(ref_low_freqs)
ref_e_elec_kj = str(ref_e_elec_au*2625.5)
ref_zpe_kj = str(ref_zpe_au*2625.5)
ref_e_kj = str(ref_e_au*2625.5)
ref_h_kj = str(ref_h_au*2625.5)
ref_ts_au = ref_h_au - ref_g_au
ref_ts_kj = str(ref_ts_au*2625.5)
ref_g_kj = str(ref_g_au*2625.5)

#printing header and reference rows

print "Filename,E_elec,DE_elec,zpe,Dzpe,E,DE,H,DH,TS,TDS,G,DG,Low freqencies,,,,,,,,,Converged?,# of imaginary frequencies"
print ref_file+','+ref_e_elec_kj+',0.00,'+ref_zpe_kj+',0.00,'+ref_e_kj+',0.00,'+ref_h_kj+',0.00,'+ref_ts_kj+',0.00,'+ref_g_kj+',0.00,'+low_freqs_string+','+conver+','+im_freq_num

#doing the other files

for log_file in log_files:

##resetting the variables
    low_freqs_list = []
    conver = 'no'
    im_freq_num = '0'
    e_elec_au = 'ERROR'
    de_elec_au = 'ERROR'
    zpe_au = 'ERROR'
    dzpe_au = 'ERROR'
    e_au = 'ERROR'
    de_au = 'ERROR'
    h_au = 'ERROR'
    dh_au = 'ERROR'
    ts_au = 'ERROR'
    ts_au = 'ERROR'
    tds_au = 'ERROR'
    g_au = 'ERROR'
    dg_au = 'ERROR'

##fetching all data for the file

    f = open(log_file, 'r')
    file_name = log_file

    for line in f:
        if e_elec_string in line:
            e_elec_au = line[(len(e_elec_string)+15):(len(e_elec_string)+35)]
            e_elec_au = float(e_elec_au)
        if zpe_string in line:
            zpe_au = line[(len(zpe_string)+1):(len(zpe_string)+37)]
            zpe_au = float(zpe_au)
        if e_string in line:
            e_au = line[(len(e_string)+1):(len(e_string)+35)]
            e_au = float(e_au)
        if h_string in line:
            h_au = line[(len(h_string)+1):(len(h_string)+35)]
            h_au = float(h_au)
        if g_string in line:
            g_au = line[(len(g_string)+1):(len(g_string)+35)]
            g_au = float(g_au)
        if freq_string in line:
            low_freqs = line[(len(freq_string)+1):]
            low_freqs = low_freqs.split()
            low_freqs_list = low_freqs_list + low_freqs;
        if conver_string in line:
            conver = 'yes'
        if im_freq_string in line:
            im_freq_num = line[7:12]
            im_freq_num = str(int(im_freq_num))

##assigning/calculating all data fields to print for the file

    e_elec_kj = str(e_elec_au*2625.5)
    de_elec_kj = str(round(((e_elec_au - ref_e_elec_au)*2625.5),2))
    zpe_kj = str(zpe_au*2625.5)
    dzpe_kj = str(round(((zpe_au - ref_zpe_au)*2625.5),2))
    e_kj = str(e_au*2625.5)
    de_kj = str(round(((e_au - ref_e_au)*2625.5),2))
    h_kj = str(h_au*2625.5)
    dh_kj = str(round(((h_au - ref_h_au)*2625.5),2))
    ts_au = h_au - g_au
    ts_kj = str(ts_au*2625.5)
    tds_kj = str(round(((ts_au - ref_ts_au)*2625.5),2))
    g_kj = str(g_au*2625.5)
    dg_kj = str(round(((g_au - ref_g_au)*2625.5),2))
    low_freqs = []
    for low_freq in low_freqs_list:
        low_freq = str(int(round(float(low_freq))))
        low_freqs.append(low_freq)
    low_freqs_string = ",".join(low_freqs)

##printing file row

    print file_name+','+e_elec_kj+','+de_elec_kj+','+zpe_kj+','+dzpe_kj+','+e_kj+','+de_kj+','+h_kj+','+dh_kj+','+ts_kj+','+tds_kj+','+g_kj+','+dg_kj+','+low_freqs_string+','+conver+','+im_freq_num
    f.close()