forked from QijingZheng/pyband
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtraj.py
70 lines (58 loc) · 2.26 KB
/
xtraj.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
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, argparse
import numpy as np
from ase.io import read, write
def ase2axsf(trajs, ofile='traj.axsf'):
'''
Save the trajectory to the Xcrysden variable-cell-axsf format.
'''
nsteps = len(trajs)
with open(ofile, 'w+') as out:
out.write("ANIMSTEPS {}\nCRYSTAL\n".format(nsteps))
for ii in range(nsteps):
snap = trajs[ii]
natoms = snap.get_number_of_atoms()
if ii == 0:
out.write('PRIMVEC\n')
out.write(
'\n'.join([''.join(["%20.16f" % xx for xx in row])
for row in snap.cell])
)
out.write("\nPRIMCOORD {}\n".format(ii+1))
out.write("{} 1\n".format(natoms))
np.savetxt(out, np.c_[snap.get_atomic_numbers(), snap.positions],
fmt='%5d %22.16f %22.16f %22.16f'
)
def xdatcar2traj(cml):
arg = parse_cml_args(cml)
if arg.snaps:
trajs = [read(f) for f in arg.snaps]
else:
trajs = read(arg.inputFile, index=':')
if arg.outFmt == 'xyz':
write('{}.xyz'.format(arg.outPrefix), trajs)
elif arg.outFmt == 'pdb':
write('{}.pdb'.format(arg.outPrefix), trajs)
else:
ase2axsf(trajs, ofile='{}.axsf'.format(arg.outPrefix))
def parse_cml_args(cml):
'''
CML parser.
'''
arg = argparse.ArgumentParser(add_help=True)
arg.add_argument('-i', dest='inputFile', action='store', type=str,
default='XDATCAR',
help='The input file, either OUTCAR or XDATCAR.')
arg.add_argument('-f', dest='outFmt', action='store', type=str,
default='axsf', choices=['xyz', 'pdb', 'axsf'],
help='The format of the trajectory file.')
arg.add_argument('-o', dest='outPrefix', action='store', type=str,
default='traj',
help='The prefix of the output file.')
arg.add_argument('-l', dest='snaps', action='store', type=str,
default=[], nargs='+',
help='List of structure files.')
return arg.parse_args(cml)
if __name__ == '__main__':
xdatcar2traj(sys.argv[1:])