Skip to content

Instantly share code, notes, and snippets.

@yangyushi
Created March 5, 2019 09:45
Show Gist options
  • Save yangyushi/21001037663128ce20bf8e465b8b324f to your computer and use it in GitHub Desktop.
Save yangyushi/21001037663128ce20bf8e465b8b324f to your computer and use it in GitHub Desktop.
def array_to_xyz(filename, array):
"""
Append many frames to the same xyz file that ovito can read
The file is overwritten everytime
array: [frame_1, frame_2, ...]
coordinate in one frame: (dimension, number_of_particles)
"""
if '.xyz' == filename[-4:]:
fname = filename
else:
fname = name + '.xyz'
f = open(fname, 'w')
f.close()
for i, frame in enumerate(array):
dim, num = frame.shape
with open(fname, 'a') as f:
np.savetxt(
f, frame.T,
delimiter='\t',
fmt=['A\t%.8e'] + ['%.8e' for i in range(dim - 1)],
comments='',
header=f'{num}\nframe {i}'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment