How to use one of the elements of the list to call out a specific data list

65 Views Asked by At

I have 4000 atoms and I have 5 different time frames: for each one of the time frames there are 4000 sets of XY and Z coordinates for each one of the atoms. I am writing a code in python to read the data from the output file. I got The coordinates to be called out on a list how do I manipulate the timeframe so that when I call out a specific atom in a specific time frame to be called out rather than the data in the rest of the other time frames. Thanks for any help. ok sure: Here is a sample: t = 0 Number of Atoms = 4000 0.0 16.795961913825074 0.0 16.795961913825074 0.0 16.795961913825074

ITEM: ATOMS id type x y z vx vy vz fx fy fz

[1.0, 1.0, 0.0, 0.0, 0.0, 1.1087, 0.233604, 0.0980598, -6.4837e-14, -6.26166e-14, -6.25611e-14] [2.0, 1.0, 0.839798, 0.839798, 0.0, -1.85044, 0.929038, -1.30119, 9.32587e-15, 1.11855e-14, -6.19504e-14]...

Focus on x,y,z. There are similar data for 4 other time frames. The goal is to call out the atom based on id, coordinate(x,y,and z individually) and be able to select the one in a given time frame. So in sum: x[id][x or y or z][t] should out put the coordinate of that atom id in the right time frame.

Here is my code:

with open('/Users/marx/Documents/Research/PL_Files/lata4olivia', 'r') as reader: line = reader.readline()

# Read and print the entire file line by line
x = []
while line != '':
    
    if line.rstrip() == "ITEM: TIMESTEP":
        line = reader.readline()
        t = int (line)
        print ('t =', t) 
        line = reader.readline()

    if line.rstrip() == "ITEM: NUMBER OF ATOMS":
        line = reader.readline()        
        na = int (line)
        print ('Number of Atoms =', na)

        line = reader.readline()        
    if line.rstrip() == "ITEM: BOX BOUNDS pp pp pp":
        line = reader.readline()
        line = line.split(' ')
        xlo = float(line[0])
        xhi = float(line[1])
        print (xlo,xhi)
        line = reader.readline()
        line = line.split(' ')
        ylo = float(line[0])
        yhi = float(line[1])
        print(ylo,yhi)
        line = reader.readline()
        line = line.split(' ')
        zlo = float(line[0])
        zhi = float(line[1])
        print(zlo,zhi)

        line = reader.readline()  
    if line.rstrip() == "ITEM: ATOMS id type x y z vx vy vz fx fy fz":
        for i in range (1,na):
            line = reader.readline()        
            outcomes = line
            outcomes = line.rstrip('\n')
            outcomes = line.split(' ')
            outcomes = [float(ele) for ele in outcomes]
            iid,itype,ix,iy,iz,ivx,ivy,ivz,ifx,ify,ifz = list(outcomes)
            print (outcomes)
            x.append([iid,ix,iy,iz])
    #print(x)


            
    line = reader.readline()
1

There are 1 best solutions below

5
On

EDIT- now that you have given an example I suggest you store data like this:

data = {
    time_frame: {
        atom_id: {
            'x': value_of_x,
            'y': value_of_y,
            'z': value_of_z,
        }
    }
}
time = 0
atom_id = 1
# get x of atom 1 in time frame 0
data[time][atom_id][x]
>>> value_of_x

I believe if you share your code it would be easier to help - but here is a way to store the data and keep it accessible and easy to manipulate (if I understood you correctly):

data = {
    'time_frame1': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame2': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame3': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame4': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)],
    'time_frame5': [(x1,y1,z1),(x2,y2,z2)...(x4000,y4000,x4000)]
}

you can easily access the data:

data['time_frame1'][3999]
>>> (x4000,y4000,x4000)

if you want to change/manipulate the time frame (key in our case):

# changed/manipulate time_frame1 
time_frame = 'new_time_frame'
data[time_frame] = data.pop('time_frame1')