-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom2xyz.py
57 lines (50 loc) · 2.04 KB
/
atom2xyz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import pylab as pl
import sys
path='../glass_KA21/facilitation/facilitation/'
sys.path.append(path)
import singPartDist as sp
inFile = '/home/xn18583/Downloads/pos_mW-eq2.dat'
outFile = 'test/test.xyz'
frameCounter = 0
numPart = 4000
numFrames = 201
def readCoords(filexyz, numFrames, numPart):
"""
Reads data from an xyz file
Args:
filexyz(string): name of the xyz file to read
numFrames (int): number of frames in file
numPart (int): number of particles
Return:
allCoords (list of list) for each frame a list of all
particles consisting of list of all three coordinates
for each particle (x,y,z)
"""
frame = -1
allCoords = np.zeros((numFrames,numPart,4))
with open(filexyz, 'r') as readFile:
for line in readFile:
splitL = line.split()
if len(splitL)>1 and splitL[1] =='ATOMS':
frame +=1
particleCounter = 0
if len(splitL) ==5:
allCoords[frame][particleCounter,0] =splitL[2]
allCoords[frame][particleCounter,1] =splitL[3]
allCoords[frame][particleCounter,2] =splitL[4]
allCoords[frame][particleCounter,3] =splitL[0]
particleCounter+=1
return allCoords
allCoords = readCoords(inFile,numFrames,numPart)
with open(outFile,'w') as outF:
for frame in range(numFrames):
outF.write('{} \n'.format(numPart))
outF.write('Atoms. Timestep: {}\n'.format(frame))
sorting = np.argsort(allCoords[frame][:,3])
coordsx = allCoords[frame][sorting,0]
coordsy = allCoords[frame][sorting,1]
coordsz = allCoords[frame][sorting,2]
print(frame)
for particle in range(numPart):
outF.write('A {} {} {} \n'.format(coordsx[particle],coordsy[particle],coordsz[particle]))