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: 204)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to get JSON object in python and process it further Creepy 2 1,136 Oct-24-2024, 08:46 AM
Last Post: buran
  Process the image on the Python HTTP server Aleks 0 4,170 Dec-02-2021, 11:43 PM
Last Post: Aleks
  Creating symbolic matrix automated NMMST 2 2,971 Oct-05-2020, 01:42 AM
Last Post: scidam
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,576 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 6,356 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Spawning a new process that is not attached to python cman234 3 3,204 Apr-25-2020, 05:24 PM
Last Post: cman234
  Automated Bet placement redmercury 2 10,638 Dec-04-2019, 10:53 AM
Last Post: redmercury
  How to background another process in Python? Kalet 2 3,355 Oct-21-2019, 05:17 AM
Last Post: newbieAuggie2019
  Python Automated Response System Help altoon 3 4,983 Oct-10-2019, 07:09 AM
Last Post: perfringo
  How to sharing object between multiple process from main process using Pipe Subrata 1 4,558 Sep-03-2019, 09:49 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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