Python Forum
Question about Creating an Automated Process in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Question about Creating an Automated Process in Python (/thread-39181.html)



Question about Creating an Automated Process in Python - Supratik1234 - Jan-13-2023

Hi everyone, I am very new to Python so I don't know a lot about coding whatsoever but I am a part of a research group involved in ab initio md. For this python script, we are trying to find O-H distance between two different molecules using a XYZ file with 50 time steps of a reaction run through NWChem. I had to individually type "OX" and "HX" to specify which oxygen and hydrogen atoms I wanted to use to measure the distance between them for each time step (50 total). So I had to write "OX" 50 times for the specific oxygen I wanted to use. Is there any way to automate this process, so I can just choose the oxygen I want in the first time step and that will apply to all of them? I have attached an image of the python script to show everyone what I have as of right now! Please let me know and thank you in advance.[Image: 4K6f7C5Z]
import os
import os.path
import matplotlib.pyplot as plt
import time as ti
import numpy as np

def main(): 
    data_folder = os.path.join("OHNWCHEM")
    file_input = "T3Fmolecule copy.xyz"
    data_file = os.path.join(data_folder, file_input)
    
    ts = []
    Hs = []
    Os = []
    
    #Opens a file, goes through every line, and pulls the Ns and Os
    with open(data_file, "r") as f:
        f_lines = f.readlines()
        count = 0 
        Hcount = 0
        for line in f_lines:
            l=line.split()
            if l[0] == "HX":
                Hs.append(np.array((float(l[1]),float(l[2]),float(l[3]))))
            elif l[0] == "OX":
                Os.append(np.array((float(l[1]),float(l[2]),float(l[3]))))
            elif l[0].isnumeric() == True and len(l)>1:
                ts.append(float(l[0]))
    
                
    #Turns a vector position into a vector matrix (one big list)
    f.close()
    Hs = np.vstack(Hs)
    Os = np.vstack(Os)
   # print(ts)    #include when you want to see the number of timesteps
    
    #Going down the list for x,y,z for N and O and then plotting it
    r = distCalc(Hs[:,0],Hs[:,1],Hs[:,2],Os[:,0],Os[:,1],Os[:,2])
    float_ts = list(np.float_(ts)*0.242)
    plt.plot(float_ts,r)
    plt.xlabel("Time (fs)")
    plt.ylabel("O-H Distance (nm)")
    plt.grid()    
    plt.show()
    
    
#Performs the Distance Calculation for O-N
def distCalc (a1,b1,c1,a2,b2,c2):
    return pow(sum((pow((a2-a1)*0.1,2),pow((b2-b1)*0.1,2),pow((c2-c1)*0.1,2))),0.5)

main()