-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.py
43 lines (39 loc) · 1.51 KB
/
utils.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
import mdtraj as md
import numpy as np
import torch
def save_traj(traj, Z, name):
"""Summary
Args:
traj (np.array): traj
Z (atomic number): Description
name (filename): Description
"""
traj = np.array(traj)
Z = np.array(Z * traj.shape[0]).reshape(traj.shape[0], len(Z), 1)
traj_write = np.dstack(( Z, traj))
write_traj(filename=name, frames=traj_write)
def write_traj(filename, frames):
'''
Write trajectory dataframes into .xyz format for VMD visualization
to do: include multiple atom types
example:
path = "../../sim/topotools_ethane/ethane-nvt_unwrap.xyz"
traj2write = trajconv(n_mol, n_atom, box_len, path)
write_traj(path, traj2write)
'''
file = open(filename,'w')
atom_no = frames.shape[1]
for i, frame in enumerate(frames):
file.write( str(atom_no) + '\n')
file.write('Atoms. Timestep: '+ str(i)+'\n')
for atom in frame:
if atom.shape[0] == 4:
try:
file.write(str(int(atom[0])) + " " + str(atom[1]) + " " + str(atom[2]) + " " + str(atom[3]) + "\n")
except:
file.write(str(atom[0]) + " " + str(atom[1]) + " " + str(atom[2]) + " " + str(atom[3]) + "\n")
elif atom.shape[0] == 3:
file.write("1" + " " + str(atom[0]) + " " + str(atom[1]) + " " + str(atom[2]) + "\n")
else:
raise ValueError("wrong format")
file.close()