Skip to content

Commit

Permalink
Update Chemfiles tests for 0.10.3 (MDAnalysis#3808)
Browse files Browse the repository at this point in the history
* Update chemfiles tests for 0.10.3

* Update package/CHANGELOG

Co-authored-by: Irfan Alibay <[email protected]>

Co-authored-by: Irfan Alibay <[email protected]>
  • Loading branch information
Luthaf and IAlibay authored Sep 1, 2022
1 parent ba491be commit cb6e4f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 64 deletions.
5 changes: 3 additions & 2 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
??/??/?? IAlibay
??/??/?? IAlibay, Luthaf

* 2.4.0

Fixes
* Upgrade to chemfiles 0.10.3 (Issue #3798)

Enhancements

Expand Down Expand Up @@ -122,7 +123,7 @@ Enhancements
* Added equations for `center_of_mass` and `center_of_geometry`to
documentation (PR #3671)
* Added `center_of_charge` attribute (PR #3671)
* Added `frames` argument to AnalysisBase.run to allow analysis to run on
* Added `frames` argument to AnalysisBase.run to allow analysis to run on
arbitrary list of frames (Issue #1985)
* LinearDensity now works with updating AtomGroups (Issue #2508, PR #3617)
* Link PMDA in documentation (PR #3652)
Expand Down
89 changes: 27 additions & 62 deletions testsuite/MDAnalysisTests/coordinates/test_chemfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#
import numpy as np
from numpy.testing import assert_allclose
import scipy
import pytest
from packaging.version import Version

import MDAnalysis as mda
from MDAnalysis.coordinates.chemfiles import ChemfilesReader, ChemfilesWriter
Expand All @@ -40,9 +42,9 @@
chemfiles = pytest.importorskip("chemfiles")


@pytest.mark.parametrize('version', ['0.9.3', '0.11.0', '1.1.0'])
@pytest.mark.parametrize("version", ["0.9.3", "0.11.0", "1.1.0"])
def test_version_check(version, monkeypatch):
monkeypatch.setattr('chemfiles.__version__', version)
monkeypatch.setattr("chemfiles.__version__", version)
assert not check_chemfiles_version()

with pytest.raises(RuntimeError, match="Please install Chemfiles > 0.10"):
Expand Down Expand Up @@ -104,25 +106,30 @@ def ref():

def test_copy(self, ref):
# Issue #3664 - test not done in test_copying due to dependencies
original = ChemfilesReader(ref.trajectory, convert_units=False, dt=2,
time_offset=10, foo="bar")
original = ChemfilesReader(
ref.trajectory,
convert_units=False,
dt=2,
time_offset=10,
foo="bar",
)
copy = original.copy()

assert original.format not in ('MEMORY', 'CHAIN')
assert original.format not in ("MEMORY", "CHAIN")
assert original.convert_units is False
assert copy.convert_units is False
assert original._ts_kwargs['time_offset'] == 10
assert copy._ts_kwargs['time_offset'] == 10
assert original._ts_kwargs['dt'] == 2
assert copy._ts_kwargs['dt'] == 2
assert original._ts_kwargs["time_offset"] == 10
assert copy._ts_kwargs["time_offset"] == 10
assert original._ts_kwargs["dt"] == 2
assert copy._ts_kwargs["dt"] == 2

assert original.ts.data['time_offset'] == 10
assert copy.ts.data['time_offset'] == 10
assert original.ts.data["time_offset"] == 10
assert copy.ts.data["time_offset"] == 10

assert original.ts.data['dt'] == 2
assert copy.ts.data['dt'] == 2
assert original.ts.data["dt"] == 2
assert copy.ts.data["dt"] == 2

assert copy._kwargs['foo'] == 'bar'
assert copy._kwargs["foo"] == "bar"

# check coordinates
assert original.ts.frame == copy.ts.frame
Expand Down Expand Up @@ -168,7 +175,7 @@ def test_read_chemfiles_format(self):
# check that chemfiles format is passed to copies Issue #3664
new_reader = u.trajectory.copy()
assert new_reader._format == "LAMMPS Data"
assert new_reader._kwargs['chemfiles_format'] == "LAMMPS Data"
assert new_reader._kwargs["chemfiles_format"] == "LAMMPS Data"

def test_changing_system_size(self, tmpdir):
outfile = "chemfiles-changing-size.xyz"
Expand Down Expand Up @@ -236,10 +243,8 @@ def test_write_atom_group(self, tmpdir):
check = mda.Universe(outfile)
assert check.trajectory.ts.n_atoms == group.n_atoms

@pytest.mark.xfail # Issue #3798 - float writing not consisten
def test_write_velocities(self, tmpdir):
u = mda.Universe.empty(4, trajectory=True)
u.add_TopologyAttr("type", values=["H", "H", "H", "H"])

ts = u.trajectory.ts
ts.dimensions = [20, 30, 41, 90, 90, 90]
Expand All @@ -256,16 +261,14 @@ def test_write_velocities(self, tmpdir):
[40, 40, 40],
]

outfile = "chemfiles-write-velocities.lmp"
outfile = "chemfiles-write-velocities.nc"
with tmpdir.as_cwd():
with ChemfilesWriter(
outfile, topology=u, chemfiles_format="LAMMPS Data"
) as writer:
with ChemfilesWriter(outfile, topology=u) as writer:
writer.write(u)

with open(outfile) as file:
content = file.read()
assert content == EXPECTED_LAMMPS_DATA
with scipy.io.netcdf_file(outfile) as file:
assert np.all(file.variables["coordinates"][0] == ts.positions)
assert np.all(file.variables["velocities"][0] == ts.velocities)


VARYING_XYZ = """2
Expand All @@ -279,41 +282,3 @@ def test_write_velocities(self, tmpdir):
A 0 0 0
A 0 0 0
"""


EXPECTED_LAMMPS_DATA = """LAMMPS data file -- atom_style full -- generated by chemfiles
4 atoms
0 bonds
0 angles
0 dihedrals
0 impropers
1 atom types
0 bond types
0 angle types
0 dihedral types
0 improper types
0 20.0 xlo xhi
0 30.0 ylo yhi
0 41.0 zlo zhi
# Pair Coeffs
# 1 H
Masses
1 0.0 # H
Atoms # full
1 1 1 0.0 1.0 1.0 1.0 # H
2 2 1 0.0 2.0 2.0 2.0 # H
3 3 1 0.0 3.0 3.0 3.0 # H
4 4 1 0.0 4.0 4.0 4.0 # H
Velocities
1 10.0 10.0 10.0
2 20.0 20.0 20.0
3 30.0 30.0 30.0
4 40.0 40.0 40.0
"""

0 comments on commit cb6e4f5

Please sign in to comment.