Python Forum
Question about Creating an Automated Process in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about Creating an Automated Process in Python
#1
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()
Larz60+ write Jan-14-2023, 01:03 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Rather than attaching. I have done this for you this time. Please use bbcode tags on future posts. Thank you.

Attached Files

.py   OHNWCHEM.py (Size: 1.49 KB / Downloads: 107)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,101 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,467 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Process the image on the Python HTTP server Aleks 0 3,219 Dec-02-2021, 11:43 PM
Last Post: Aleks
  Creating symbolic matrix automated NMMST 2 2,111 Oct-05-2020, 01:42 AM
Last Post: scidam
  Automated service status for windows Pkhan 2 2,160 Sep-30-2020, 10:30 AM
Last Post: Pkhan
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,057 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,238 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 2,160 May-28-2020, 09:41 PM
Last Post: rangeshgupta
  Spawning a new process that is not attached to python cman234 3 1,922 Apr-25-2020, 05:24 PM
Last Post: cman234
  Automated compile - Multiple Target OS? nogi 5 2,610 Mar-24-2020, 11:44 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020