Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use-defined precision in XYZWriter #4771

Closed
wants to merge 1 commit into from
Closed
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 @@ -18,7 +18,7 @@ The rules for this file:
ljwoods2, aditya292002, pstaerk, PicoCentauri, BFedder,
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, orionarcher,
yuxuanzhuang, PythonFZ, laksh-krishna-sharma, orbeckst, MattTDavies,
talagayev, aya9aladdin
talagayev, aya9aladdin, ChiahsinChu

* 2.8.0

Expand Down Expand Up @@ -63,6 +63,7 @@ Fixes
* Fix groups.py doctests using sphinx directives (Issue #3925, PR #4374)

Enhancements
* Added `precision` for XYZWriter (PR #4771)
* Removed type and mass guessing from all topology parsers (PR #3753)
* Added guess_TopologyAttrs() API to the Universe to handle attribute
guessing (PR #3753)
Expand Down
20 changes: 16 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,8 @@ 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.


.. versionchanged:: 1.0.0
Expand All @@ -175,6 +184,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 +306,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