Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Chronological list of authors
- Francesco Siciliani
- Akshit Boora
- Sumit Saraswat
- Nayan Sharma


External code
Expand Down
17 changes: 17 additions & 0 deletions package/MDAnalysis/coordinates/LAMMPS.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@ def __init__(self, dcdfilename, **kwargs):
raise ValueError(f"LAMMPS DCDReader: unknown unit {unit}")
super(DCDReader, self).__init__(dcdfilename, **kwargs)

def Writer(self, filename, n_atoms=None, **kwargs):
"""Return a DCD writer with the correct LAMMPS time conversion."""
if n_atoms is None:
n_atoms = self.n_atoms

dt = units.convert(self.ts.dt, "ps", "fs") / units.convert(
1.0, "ps", "AKMA"
)

return DCD.DCDWriter(
filename,
n_atoms=n_atoms,
dt=dt,
convert_units=self.convert_units,
**kwargs,
)


class DATAReader(base.SingleFrameReaderBase):
"""Reads a single frame of coordinate information from a LAMMPS DATA file.
Expand Down
8 changes: 4 additions & 4 deletions testsuite/MDAnalysisTests/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,15 @@ def test_write_different_box(self, ref, universe, tmpdir):
def test_write_trajectory_atomgroup(self, ref, reader, universe, tmpdir):
outfile = "write-atoms-test." + ref.ext
with tmpdir.as_cwd():
with ref.writer(outfile, universe.atoms.n_atoms) as w:
with reader.Writer(outfile) as w:
for ts in universe.trajectory:
w.write(universe.atoms)
self._check_copy(outfile, ref, reader)

def test_write_trajectory_universe(self, ref, reader, universe, tmpdir):
outfile = "write-uni-test." + ref.ext
with tmpdir.as_cwd():
with ref.writer(outfile, universe.atoms.n_atoms) as w:
with reader.Writer(outfile) as w:
for ts in universe.trajectory:
w.write(universe)
self._check_copy(outfile, ref, reader)
Expand Down Expand Up @@ -924,12 +924,12 @@ def test_no_container(self, ref, tmpdir):
with pytest.raises(TypeError):
ref.writer("foo")

def test_write_not_changing_ts(self, ref, universe, tmpdir):
def test_write_not_changing_ts(self, ref, reader, universe, tmpdir):
outfile = "write-not-changing-ts." + ref.ext

copy_ts = universe.trajectory.ts.copy()
with tmpdir.as_cwd():
with ref.writer(outfile, n_atoms=5) as W:
with reader.Writer(outfile, n_atoms=universe.atoms.n_atoms) as W:
W.write(universe)
assert_timestep_almost_equal(copy_ts, universe.trajectory.ts)

Expand Down
88 changes: 86 additions & 2 deletions testsuite/MDAnalysisTests/coordinates/test_lammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import MDAnalysis as mda
from MDAnalysis import NoDataError
from MDAnalysis.coordinates.LAMMPS import DCDReader
from MDAnalysis.coordinates.DCD import DCDWriter

from numpy.testing import assert_equal, assert_allclose

Expand All @@ -38,6 +40,11 @@
RefLAMMPSDataDCD,
RefLAMMPSDataAdditionalColumns,
)
from MDAnalysisTests.coordinates.base import (
BaseReference,
BaseReaderTest,
BaseWriterTest,
)
from MDAnalysisTests.datafiles import (
LAMMPScnt,
LAMMPShyd,
Expand Down Expand Up @@ -328,7 +335,75 @@ def test_n_angles(self, LAMMPSDATA_partial):
# need more tests of the LAMMPS DCDReader


class TestLAMMPSDCDReader(RefLAMMPSDataDCD):
class LAMMPSDCDReference(BaseReference):
def __init__(self):
super().__init__()
self.topology = RefLAMMPSDataDCD.topology
self.trajectory = RefLAMMPSDataDCD.trajectory
self.reader = DCDReader

self.writer = DCDWriter
self.mean_dimensions = RefLAMMPSDataDCD.mean_dimensions
trajectory = self.reader(self.trajectory)

self.n_atoms = trajectory.n_atoms
self.n_frames = len(trajectory)
self.dt = trajectory.dt
self.totaltime = trajectory.totaltime
self.aux_lowf_data = [
[2**0],
[np.nan],
[np.nan],
[np.nan],
[2**1],
]
self.aux_highf_data = [
[2**0],
[2**1],
[2**2],
[2**3],
[2**4],
]
self.ext = "dcd"

self.first_frame = trajectory[0].copy()
self.second_frame = trajectory[1].copy()
self.last_frame = trajectory[-1].copy()
self.jump_to_frame = trajectory[3].copy()
for i, frame in enumerate(
[
self.first_frame,
self.second_frame,
self.last_frame,
self.jump_to_frame,
]
):
frame.aux.lowf = np.array(self.aux_lowf_data[[0, 1, 4, 3][i]])
frame.aux.highf = np.array(self.aux_highf_data[[0, 1, 4, 3][i]])

self.dimensions = self.first_frame.dimensions.copy()
self.dimensions_second_frame = self.second_frame.dimensions.copy()
self.volume = self.first_frame.volume

trajectory.close()

def iter_ts(self, i):
trajectory = self.reader(self.trajectory)
ts = trajectory[i].copy()
trajectory.close()

ts.aux.lowf = np.array(self.aux_lowf_data[i])
ts.aux.highf = np.array(self.aux_highf_data[i])

return ts


class TestLAMMPSDCDReader(BaseReaderTest, RefLAMMPSDataDCD):
@staticmethod
@pytest.fixture(scope="class")
def ref():
return LAMMPSDCDReference()

flavor = "LAMMPS"

@pytest.fixture(scope="class")
Expand Down Expand Up @@ -409,7 +484,16 @@ def wrong_load(unit="GARBAGE"):
wrong_load()


class TestLAMMPSDCDWriter(RefLAMMPSDataDCD):
class TestLAMMPSDCDWriter(BaseWriterTest, RefLAMMPSDataDCD):
@pytest.fixture()
def universe(self):
return mda.Universe(self.topology, self.trajectory, format=self.format)

@staticmethod
@pytest.fixture(scope="class")
def ref():
return LAMMPSDCDReference()

flavor = "LAMMPS"

@pytest.fixture(scope="class")
Expand Down
Loading