/
traj_process.py
182 lines (137 loc) · 5.04 KB
/
traj_process.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
pyscal module containing methods for processing of a trajectory. Methods for
reading of input files formats, writing of output files etc are provided in
this module.
"""
import numpy as np
import gzip
import pyscal.catom as pca
from ase import Atom, Atoms
import gzip
import io
import os
import warnings
from ase.io import write, read
import pyscal.formats.ase as ptase
import pyscal.formats.lammps as ptlammps
import pyscal.formats.mdtraj as ptmdtraj
import pyscal.formats.vasp as ptvasp
def read_file(filename, format="lammps-dump",
compressed = False, customkeys=None):
"""
Read input file
Parameters
----------
filename : string
name of the input file.
format : {'lammps-dump', 'poscar', 'ase', 'mdtraj'}
format of the input file, in case of `ase` the ASE Atoms object
compressed : bool, optional
If True, force to read a `gz` compressed format, default False.
customkeys : list
A list containing names of headers of extra data that needs to be read in from the
input file.
Returns
-------
None
"""
if customkeys == None:
customkeys = []
customread = (len(customkeys) > 0)
if format=='lammps-dump':
atoms, box, is_triclinic = ptlammps.read_snap(filename, compressed=compressed, check_triclinic=True, customkeys=customkeys)
elif format == 'ase':
atoms, box = ptase.read_snap(filename)
elif format == 'mdtraj':
atoms, box = ptmdtraj.read_snap(filename)
elif format == 'poscar':
atoms, box = ptvasp.read_snap(filename, compressed=compressed)
else:
try:
#try to use ASE backend
aseobject = read(filename, format=format)
atoms, box = ptase.read_snap(aseobject)
warnings.info("Using ase backend to read file")
except:
raise TypeError("format recieved an unknown option %s"%format)
return atoms, box
def write_file(sys, outfile, format="lammps-dump", compressed = False,
customkeys=None, customvals=None, timestep=0, species=None):
"""
Write the state of the system to a trajectory file.
Parameters
----------
sys : `System` object
the system object to be written out
outfile : string
name of the output file
format : string, optional
format of the output file
compressed : bool, default false
write a `.gz` format
customkey : string or list of strings, optional
If specified, it adds this custom column to the dump file. Default None.
customvals : list or list of lists, optional
If `customkey` is specified, `customvals` take an array of the same length
as number of atoms, which contains the values to be written out.
timestep: int, optional
Specify the timestep value, default 0
species : None, optional
species of the atoms. Required if any format other than 'lammps-dump' is used. Required
for convertion to ase object.
Returns
-------
None
"""
if format=='lammps-dump':
ptlammps.write_snap(sys, outfile, compressed = compressed,
customkeys = customkeys, customvals = customvals, timestep = timestep)
elif format == 'lammps-data':
aseobject = ptase.convert_snap(sys, species=species)
write(outfile, aseobject, format='lammps-data')
elif format == 'poscar':
ptvasp.write_snap(sys, outfile, species=species)
#aseobject = ptase.convert_snap(sys, species=species)
#write(outfile, aseobject, format='vasp')
else:
#try a write using ase
try:
aseobject = ptase.convert_snap(sys, species=species)
write(outfile, aseobject, format=format)
warnings.info("Using ase backend to write file")
except:
raise TypeError("format recieved an unknown option %s"%format)
def split_trajectory(infile, format='lammps-dump', compressed=False):
"""
Read in a trajectory file and convert it to individual time slices.
Parameters
----------
filename : string
name of input file
format : format of the input file
only `lammps-dump` is supported now.
compressed : bool, optional
force to read a `gz` zipped file. If the filename ends with `.gz`, use of this keyword is not
necessary.
Returns
-------
snaps : list of strings
a list of filenames which contain individual frames from the main trajectory.
Notes
-----
This is a wrapper function around `split_traj_lammps_dump` function.
"""
if not os.path.exists(infile):
raise FileNotFoundError("Filename %s not found"%infile)
snaps = []
if format=='lammps-dump':
snaps = ptlammps.split_snaps(infile, compressed = compressed)
elif format == 'ase':
snaps = ptase.split_snaps()
elif format == 'mdtraj':
snaps = ptmdtraj.split_snaps()
elif format == 'poscar':
snaps = ptvasp.split_snaps()
else:
raise TypeError("format recieved an unknown option %s"%format)
return snaps