Skip to content

Commit

Permalink
add UT for new features in XYZWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiahsinChu committed Oct 29, 2024
1 parent bc588dc commit e7753b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions package/MDAnalysis/coordinates/XYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ class XYZWriter(base.WriterBase):
# these are assumed!
units = {'time': 'ps', 'length': 'Angstrom'}

def __init__(self, filename, n_atoms=None, convert_units=True,
remark=None, precision=5, **kwargs):
def __init__(
self,
filename,
n_atoms=None,
convert_units=True,
remark=None,
precision=5,
**kwargs,
):
"""Initialize the XYZ trajectory writer
Parameters
Expand Down
32 changes: 32 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import pytest

import MDAnalysis as mda
from MDAnalysis import transformations as trans
import numpy as np
from numpy.testing import (
assert_almost_equal,
Expand Down Expand Up @@ -122,6 +123,37 @@ def test_remark(self, universe, remarkout, remarkin, ref, tmpdir):

assert lines[1].strip() == remarkin

def test_remark_cellinfo(self, universe, tmpdir):
outfile = "write-remark-cellinfo.xyz"
universe_cpy = universe.copy()
transform = trans.boxdimensions.set_dimensions(
[10.0, 10.0, 20.0, 90, 90, 90]
)
universe_cpy.trajectory.add_transformations(transform)
cell = np.array(
[[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 20.0]]
).flatten()
ref_header = (
'Lattice="%s" pbc"T T T"'
% np.array2string(cell, max_line_width=500)[1:-1]
)
with tmpdir.as_cwd():
universe_cpy.atoms.write(outfile)
with open(outfile, "r") as xyzout:
lines = xyzout.readlines()
assert lines[1].strip() == ref_header

def test_precision(self, universe, tmpdir):
outfile = "write-precision.xyz"
precision = 10

with tmpdir.as_cwd():
universe.atoms.write(outfile, precision=precision)
with open(outfile, "r") as xyzout:
lines = xyzout.readlines()
# check that the precision is set correctly
assert len(lines[2].split()[1].split(".")[1]) == precision


class XYZ_BZ_Reference(XYZReference):
def __init__(self):
Expand Down

0 comments on commit e7753b0

Please sign in to comment.