Skip to content
Merged
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 @@ -244,6 +244,7 @@ Chronological list of authors
- Fabian Zills
- Laksh Krishna Sharma
- Matthew Davies
- Jia-Xin Zhu


External code
Expand Down
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ The rules for this file:


-------------------------------------------------------------------------------
??/??/?? IAlibay
??/??/?? IAlibay, ChiahsinChu

* 2.9.0

Fixes

Enhancements
* Added `precision` for XYZWriter (Issue #4775, PR #4771)

Changes

Expand Down
22 changes: 18 additions & 4 deletions package/MDAnalysis/coordinates/XYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,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, **kwargs):
def __init__(
self,
filename,
n_atoms=None,
convert_units=True,
remark=None,
precision=5,
**kwargs,
):
"""Initialize the XYZ trajectory writer

Parameters
Expand All @@ -161,6 +168,10 @@ def __init__(self, filename, n_atoms=None, convert_units=True,
remark: str (optional)
single line of text ("molecule name"). By default writes MDAnalysis
version and frame
precision: int (optional)
set precision of saved trjactory to this number of decimal places.

.. versionadded:: 2.9.0


.. versionchanged:: 1.0.0
Expand All @@ -175,6 +186,7 @@ def __init__(self, filename, n_atoms=None, convert_units=True,
self.remark = remark
self.n_atoms = n_atoms
self.convert_units = convert_units
self.precision = precision

# can also be gz, bz2
self._xyz = util.anyopen(self.filename, 'wt')
Expand Down Expand Up @@ -296,8 +308,10 @@ def _write_next_frame(self, ts=None):

# Write content
for atom, (x, y, z) in zip(self.atomnames, coordinates):
self._xyz.write("{0!s:>8} {1:10.5f} {2:10.5f} {3:10.5f}\n"
"".format(atom, x, y, z))
self._xyz.write(
"{0!s:>8} {1:10.{p}f} {2:10.{p}f} {3:10.{p}f}\n"
"".format(atom, x, y, z, p=self.precision)
)


class XYZReader(base.ReaderBase):
Expand Down
11 changes: 11 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ def test_remark(self, universe, remarkout, remarkin, ref, tmpdir):

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

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
Loading