lammpsio.DumpFile¶
- class lammpsio.DumpFile(filename, schema=None, sort_ids=True, copy_from=None)¶
LAMMPS dump file.
The LAMMPS dump file is a highly flexible file format. The
schemaof the file is deduced from theITEM: ATOMSheader unless one is manually specified. If specificed, theschemamust be a dictionary mapping pieces of data to column indexes (0-indexed). Valid keys for the schema match the names and shapes in theSnapshot. The keys requiring only 1 column index are:"id""typeid""molecule""charge""mass"
The keys requiring 3 column indexes are:
"position""velocity""image"
LAMMPS dumps particles in an unknown order unless you have used the
dump_modify sortoption. If you want particles to be ordered byidin theSnapshot, usesort_ids=True(default). Note that slightly faster reading may be possible by setting this option toFalse.A
DumpFileis iterable, so you can use it to go through all the snapshots of a trajectory. It supports LAMMPS dump file with different compression formats such as gzip and Zstandard (pyzstd package needs to be installed separately for Zstandard support). Random access to snapshots is not currently implemented, but it may be added in future. If you want to randomly access snapshots, you should load the whole file into a list, but the memory requirements to do so may be large.The dump file may not contain certain information about your particles, for example, topology, or you may choose not to write this information in the dump file because it does not change frame-to-frame. The
copy_fromoption allows this information to be copied into a new snapshot from a reference one, e.g, that was read from aDataFile.- Parameters:
filename (str) – Path to dump file.
schema (dict) – Schema for the contents of the file. Defaults to
None, which means to read it from the file.sort_ids (bool) – If
True, sort the particles by ID in each snapshot.copy_from (
Snapshot) – If specified, copy supported fields that are missing in the dump file but are set in a referenceSnapshot.
Example
Create a dump file object:
traj = lammpsio.DumpFile(filename)
Iterate snapshots:
for snapshot in traj: print(snapshot.step)
You can also use multiprocessing to read snapshots in parallel:
import multiprocessing def process_snapshot(snapshot): return snapshot.N if __name__ == '__main__': num_workers = max(2, multiprocessing.cpu_count()) with multiprocessing.Pool(num_workers) as p: num_particles = p.map(process_snapshot, traj) print(num_particles)
You can also get the number of snapshots in the
DumpFile, but this does require reading the entire file: use with caution!num_frames = len(traj)
Random access by creating a list:
snapshots = [snap for snap in traj] print(snapshots[3].step)
Methods:
create(filename, schema, snapshots)Create a LAMMPS dump file.
Attributes:
Copy fields that are missing in a dump file.
Path to the file.
Data schema.
- classmethod create(filename, schema, snapshots)¶
Create a LAMMPS dump file.
- Parameters:
- Returns:
The object representing the new dump file.
- Return type:
Example
A
DumpFilecan be created from a list of snapshots:schema = {"id":0, "position":(1, 2, 3), "image": (4, 5, 6)} lammpsio.DumpFile.create(filename, schema, snapshots)
- property copy_from¶
Copy fields that are missing in a dump file.
The fields that can be copied are:
- Type: