-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdumplammps2raw.py
More file actions
107 lines (94 loc) · 4.49 KB
/
dumplammps2raw.py
File metadata and controls
107 lines (94 loc) · 4.49 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# coding=utf-8
import numpy as np
import pandas as pd
import sys
def wrap_coord(lb,x):
while(x > lb or x < 0):
x = x - np.sign(x) *lb
return
def read_write_dat(infile,outdir='./',step=-1):
"""
INPUT:
infile = input file name
outfile = output file name
step = max number of timestep (if negative use all timesteps)
OUTPUT:
"""
snap = {}
with open(infile, "r") as in_file, open(outdir + 'box.raw','w') as boxf, open(outdir + 'coord.raw','w') as coordf ,\
open(outdir + 'force.raw','w') as forcef, open(outdir + 'Nstep.data','w') as stepf :
end = True
line = in_file.readline()
if(line == ''):
print('first line empty')
return
dt=0
while (end):
dt += 1
timestep = int(in_file.readline().split()[0])
stepf.write('{} \n'.format(timestep))
line = in_file.readline()
natoms=int(in_file.readline().split()[0])
snap['coord'] = np.zeros((natoms,3))
snap['force'] = np.zeros((natoms,3))
snap['type'] = np.zeros(natoms,dtype=int)
snap['atoms'] = np.zeros(natoms,dtype=int)
old = np.zeros(natoms)
line = in_file.readline()
box = np.zeros(3)
llbox = np.zeros(3)
for i in range(3):
line = in_file.readline().split()
llbox[i] = float(line[0])
box[i] = float(line[1]) - llbox[i]
boxf.write('{} 0.0 0.0 0.0 {} 0.0 0.0 0.0 {} \n'.format(box[0],box[1],box[2]))
llbox = -llbox
line = in_file.readline().split()[2:]
#TODO understand what is dumped
for iatom in np.arange(natoms):
line = in_file.readline().split()
snap['coord'][iatom,0] = float(line[2]) + llbox[0]
snap['coord'][iatom,1] = float(line[3]) + llbox[1]
snap['coord'][iatom,2] = float(line[4]) + llbox[2]
for ii in range(3):
wrap_coord(box[ii],snap['coord'][iatom,ii])
snap['force'][iatom,0] = float(line[11])
snap['force'][iatom,1] = float(line[12])
snap['force'][iatom,2] = float(line[13])
snap['type'][iatom] = int(line[1])-1
snap['atoms'][iatom] = int(line[1])
for iatom in np.argsort(snap['atoms']):
coordf.write('{} {} {} '.format(snap['coord'][iatom,0],snap['coord'][iatom,1],snap['coord'][iatom,2]))
forcef.write('{} {} {} '.format(snap['force'][iatom,0],snap['force'][iatom,1],snap['force'][iatom,2]))
# if (old[iatom] != snap['type'][iatom] and dt > 1 ):
# print('old[',iatom,'] != snap[\'type\'] [',iatom,'] -> ',old[iatom], ' != ', snap['type'] [iatom])
# print('at step ',dt)
# return
old[:] = snap['type'][:]
coordf.write('\n')
forcef.write('\n')
line = in_file.readline()
if(line == '' or (step == dt and step >0)):
end=False
np.savetxt(outdir + 'type.raw',snap['type'][np.argsort(snap['atoms'])].reshape((1,-1)),fmt='% 4d')
return
def write_ener(enerfile,outdir='./', stepf='./Nstep.data'):
ener = np.loadtxt(enerfile,skiprows=1)
np.savetxt(outdir + 'energy.raw', ener[:,2])
#ener = pd.read_csv(enerfile, sep=None, engine='python').to_dict()
#en = dict(zip(int(ener['Step']),ener['PotEng']))
#steps = pd.read_csv(stepf,sep=None, engine='python').to_numpy(dtype='int')
#with open(outdir + 'energy.raw') as efile:
# for i in range(steps.shape[0]):
# efile.write('{} \n'.format(en[i]))
return
if len(sys.argv)<3:
print ("Uso: {} [prefix_file_in] [file_out] [enerfile] [stop (optional)] ".format(sys.argv[0]))
print ("script to read a lammps dump file and write them on a .raw file without any other line")
print ("the structure of the dump is ITEM: ATOMS id type x y z ix iy iz vx vy vz fx fy fz mass")
exit(-1)
stop=-1
if (len(sys.argv)==5):
stop=int(sys.argv[4])
read_write_dat(sys.argv[1],sys.argv[2],stop)
#write_ener(enerfile=sys.argv[3],outdir=sys.argv[2],stepf=sys.argv[2] + '/Nstep.data')