Mod:Hunt Research Group:model freq script2

From wiki
Revision as of 09:31, 30 July 2024 by Wikiadmin (talk | contribs)
Jump to navigation Jump to search

test

  • script to extract data and directly produce a csv file that can be opened in MS Excel. It runs and uses all the log files (*.log) in the current directory.
  • simply copy and paste this script into a file get_frequencies_data.py
  • then run with "python3 get_frequencies_data.py"
import glob
import os
import sys
import csv

print("#=====================================================================================#")
print("#-------------------------------------------------------------------------------------#")
print("#       A Script to Extract ΔE, ΔG, ΔH, TΔS, and ZPE from Gaussian Output Files       #")
print("#-------------------------------------------------------------------------------------#")
print("#=====================================================================================#")
print("\n")

#---------------------------------------------------------------------------------------------------------------------------------------#
# A function to take out SCF & Gibb's Free Energy Values from a Gaussian log file and save the Filename., SCF E, & Gibb's E to a list   #
#---------------------------------------------------------------------------------------------------------------------------------------#
#---------- Start of Function ----------#
def Extract_Energies(input_file):
    SCF_Energy = "--NOT FOUND--"
    Gibbs_Energy = "--NOT FOUND--"
    Rxn_Enthalpy_H = "--NOT FOUND--"
    Zero_point_energy = "--NOT FOUND--"
    TDS = "--NOT FOUND--"
    SCF_E_kJ = "--NOT FOUND--"
    Gibbs_E_kJ = "--NOT FOUND--"
    Enthalpy_of_Rxn_kJ = "--NOT FOUND--"
    ZPE_kJ = "--NOT FOUND--"
    found_low_frequencies = False
    f = open(input_file, 'r')
    input = f.readlines()
    filename=os.path.splitext(os.path.basename(input_file))[0]
    for line in input:
        if "SCF Done:" in line:
            complete_line = line.split()
            SCF_Energy = complete_line[4] # ΔE
            SCF_E_kJ = float(SCF_Energy)*2625.50
        if "Sum of electronic and thermal Free Energies" in line:
            gibbs_line = line.split()
            Gibbs_Energy = gibbs_line[7] # ΔG
            Gibbs_E_kJ = float(Gibbs_Energy) * 2625.50
        if "Sum of electronic and thermal Enthalpies" in line:
            enthalpies_line = line.split()
            Rxn_Enthalpy_H = enthalpies_line[6] # ΔH
            Enthalpy_of_Rxn_kJ = float(Rxn_Enthalpy_H) * 2625.50
            #TDS_au = float(Rxn_Enthalpy_H) - float(Gibbs_Energy)
            #print('Here is the TDS value', TDS_au)
        if "Zero-point correction=" in line:
            complete_line = line.split()
            Zero_point_energy = complete_line[2] # ZPE
            ZPE_kJ = float(Zero_point_energy)*2625.50
        if "Low frequencies ---" in line:
            if found_low_frequencies:
                continue  # Skip processing if already found
            complete_line = line.split()
            LF01 = complete_line[3]
            LF02 = complete_line[4]
            LF03 = complete_line[5]
            LF04 = complete_line[6]
            LF05 = complete_line[7]
            LF06 = complete_line[8]
            found_low_frequencies = True
    return [filename, SCF_Energy, SCF_E_kJ, Gibbs_Energy, Gibbs_E_kJ, Rxn_Enthalpy_H, Enthalpy_of_Rxn_kJ, round(float(Rxn_Enthalpy_H) - float(Gibbs_Energy),10), round((float(Rxn_Enthalpy_H) - float(Gibbs_Energy))*2625.50,10), Zero_point_energy, float(SCF_Energy)+float(Zero_point_energy), (float(SCF_Energy)+float(Zero_point_energy))*2625.50, LF01, LF02, LF03, LF04, LF05, LF06]
#---------- End of Function ----------#

#Define the user input for input files (log files)
#input_files = input('Enter the names of input files or a range (e.g. *.log):  ')
input_files = '*.log'
Final_Energies_List = [['File Name', 'SCF Energy (a.u.)', u'Δ'+'E (KJ/mol)', u'Δ'+'G (a.u.)', u'Δ'+'G (KJ/mol)', u'Δ'+'H (a.u)', u'Δ'+'H (KJ/mol)', 'T'+u'Δ'+'S (a.u)', 'T'+u'Δ'+'S (kJ/mol)', 'ZPE (a.u.)', u'Δ'+'E+ZPE (a.u.)', u'Δ'+'E+ZPE (kJ/mol)', 'Low Frequencies', ' ', ' ', ' ', ' ', ' ']] # A list which will contain all the data from all log files

#-------------------------------------------------------------------------------------------------------------------------#
# A for loop to go through all the .log files in the current directory and use the function defined above on all of them  #
#-------------------------------------------------------------------------------------------------------------------------#
#Uncomment below if your log files are located in the same directory or use the loop below if log files are in multiple subdirectories.
for files in glob.glob(input_files):
    Final_Energies_List.append(Extract_Energies(files))

#-----------------------------------------------------------------------------------------------------------------------------------#
# A for loop to go through all the subdirectories in the current directory and use the function defined above on all the log files  #
#-----------------------------------------------------------------------------------------------------------------------------------#
'''for files in glob.iglob(os.path.join('*', '*.log')):
    Final_Energies_List.append(SCF_and_Gibbs_Energy(files))'''

print("Here are the ΔE, ΔG, ΔH, TΔS, and ZPE of all the Files\n")
for i in Final_Energies_List:
    print(
        " {:<20}  {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<16}   {:<10}   {:<10}   {:<10}   {:<10}   {:<10}".format(i[0], i[1], i[2], i[3],
                                                                                                i[4], i[5], i[6], i[7], i[8], i[9], i[10], i[11], i[12], i[13], i[14], i[15], i[16], i[17]))

print("\nAll Done Boss :)")

# ------------------------------------------#
# A loop to write the output to a csv file  #
# ------------------------------------------#
with open('frequencies_database.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) # Defining general csv writing template
    for i in Final_Energies_List:
        writer.writerow(i)


print("\n")
print("#-------------------------------------------------------#")
print("# A Script by Dr. Muhammad Ali Hashmi (30-July-2024)    #")
print("#-------------------------------------------------------#")