Skip to content
Open
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
6a75d39
Adds basic framework for reading NCRST files
Sep 2, 2018
5a88100
Removing unused imports
Sep 2, 2018
8b7dbd5
Major updates to NCRST reader
Sep 4, 2018
0c457ce
Try/except instead of it statements + docstring changes
Sep 5, 2018
d245e33
64 bit offset check
Sep 7, 2018
33cbe3e
Merge branch 'develop' into NCrestart
May 28, 2019
b420032
Need to check things
May 28, 2019
08bb2b1
Merge branch 'develop' into NCrestart
Aug 2, 2019
7676741
Some changes
Aug 5, 2019
a382762
Merge branch 'develop' into NCrestart
IAlibay Aug 5, 2019
df24168
Exception + Minor docs update
IAlibay Aug 6, 2019
93f2edb
More exceptions + start on tests
IAlibay Aug 6, 2019
78e92d0
More inpcrd changes and tests
IAlibay Aug 7, 2019
474ab59
Error type change
IAlibay Aug 7, 2019
e5d7cb6
Changes to match initial PR 2326
IAlibay Aug 16, 2019
cee802c
Merge branch 'develop' into NCrestart
IAlibay Aug 19, 2019
2554ff0
Swapping to ValueErrors
IAlibay Aug 19, 2019
8cc8161
Update _verify_units and typos
IAlibay Aug 19, 2019
5277cd0
Merge branch 'develop' into NCrestart
IAlibay Dec 29, 2019
f9d2ab5
Merge branch 'MDAnalysis:develop' into NCrestart
IAlibay Nov 30, 2022
bcc58c8
Update INPCRD.py
IAlibay Nov 30, 2022
30a2b12
working commit
jeremyleung521 Apr 16, 2025
e2e0455
test for NCRST
jeremyleung521 Apr 17, 2025
d6627c7
changelog + authors
jeremyleung521 Apr 17, 2025
c5988e6
Merge branch 'develop' of github.com:mdanalysis/mdanalysis into ncrst…
jeremyleung521 Apr 17, 2025
fb0d2e9
lint test_netcdf.py
jeremyleung521 Apr 17, 2025
0f27fc4
small code cleanup of TRJ.py
jeremyleung521 Apr 17, 2025
eb83b42
small formatting in CHANGELOG
jeremyleung521 Apr 17, 2025
eb7ba03
Merge branch 'develop' into ncrst-reader
jeremyleung521 Apr 19, 2025
50eb893
Merge branch 'develop' of github.com:mdanalysis/mdanalysis into ncrst…
jeremyleung521 May 22, 2025
abfb5a7
Merge branch 'pr/3942' into ncrst-reader
jeremyleung521 May 22, 2025
d98ebab
working, failing ExceptionWarning Tests
jeremyleung521 May 22, 2025
37b7e1e
cleanup
jeremyleung521 May 22, 2025
736db88
make NCRST work
jeremyleung521 May 27, 2025
2ab9fe1
typo in SingleFrameReaderBase
jeremyleung521 May 27, 2025
14daa07
Merge branch 'ncrst-reader' of github.com:jeremyleung521/MDAnalysis i…
jeremyleung521 May 27, 2025
5bf1ce0
Revert "typo in SingleFrameReaderBase"
jeremyleung521 May 27, 2025
614f9f6
black lint
jeremyleung521 May 27, 2025
bf68954
Reapply "typo in SingleFrameReaderBase"
jeremyleung521 May 27, 2025
264215f
black lint
jeremyleung521 May 27, 2025
01234f1
Merge branch 'develop' of github.com:jeremyleung521/MDAnalysis into n…
jeremyleung521 Jun 12, 2025
6613a27
fix tests
jeremyleung521 Jun 12, 2025
4dcbf2f
Merge branch 'develop' of github.com:mdanalysis/mdanalysis into ncrst…
jeremyleung521 Jun 26, 2025
eca17ab
fix docstring
jeremyleung521 Jun 26, 2025
aac6b8a
Merge branch 'develop' of github.com:mdanalysis/mdanalysis into ncrst…
jeremyleung521 Jul 22, 2025
b44df73
typo and doc-links fix
jeremyleung521 Jul 22, 2025
32848f4
doc update
jeremyleung521 Jul 22, 2025
9262e75
n_frames now read during init
jeremyleung521 Jul 22, 2025
ec8fd95
update tests
jeremyleung521 Jul 22, 2025
444ece0
lint for test_netcdf.py
jeremyleung521 Jul 22, 2025
921bc34
Merge branch 'develop' into ncrst-reader
yuxuanzhuang Aug 5, 2025
fbcf292
Merge branch 'develop' into ncrst-reader
jeremyleung521 Aug 7, 2025
1e4aba1
Merge branch 'develop' into ncrst-reader
jeremyleung521 Oct 13, 2025
44f5dd4
bump PR to v2.11.0
jeremyleung521 Oct 13, 2025
a52a573
Enhancements
jeremyleung521 Oct 13, 2025
3ac1abd
Merge branch 'develop' into ncrst-reader
jeremyleung521 Oct 14, 2025
d66dc43
Merge branch 'develop' into ncrst-reader
jeremyleung521 Oct 16, 2025
ab2c6c9
Merge branch 'develop' into ncrst-reader
jeremyleung521 Oct 20, 2025
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
2 changes: 1 addition & 1 deletion package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Chronological list of authors
- Abdulrahman Elbanna
- Tulga-Erdene Sodjargal
- Gareth Elliott

- Jeremy M.G. Leung

External code
-------------
Expand Down
7 changes: 4 additions & 3 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ The rules for this file:

-------------------------------------------------------------------------------
??/??/?? IAlibay, orbeckst, BHM-Bob, TRY-ER, Abdulrahman-PROG, pbuslaev,
yuxuanzhuang, yuyuan871111, tanishy7777, tulga-rdn, Gareth-elliott,
hmacdope

yuxuanzhuang, yuyuan871111, tanishy7777, jpkrowe, tulga-rdn,
Gareth-elliott, hmacdope, jeremyleung521

* 2.10.0

Expand All @@ -38,6 +37,8 @@ Fixes
directly passing them. (Issue #3520, PR #5006)

Enhancements
* Allow NCDF reader to read Amber Restart Files with extension `.ncrst`
(Issue #5030, PR #5031)
* Improve parsing of topology information from LAMMPS dump files to allow
reading of mass, charge and element attributes. (Issue #3449, PR #4995)
* Added timestep support for XDR writers and readers (Issue #4905, PR #4908)
Expand Down
190 changes: 183 additions & 7 deletions package/MDAnalysis/coordinates/INPCRD.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,92 @@
#


"""INPCRD structure files in MDAnalysis --- :mod:`MDAnalysis.coordinates.INPCRD`
"""AMBER restart files in MDAnalysis --- :mod:`MDAnalysis.coordinates.INPCRD`
================================================================================
Read coordinates in Amber_ coordinate/restart file (suffix "inpcrd").
AMBER_ can write :ref:`ASCII restart<ascii-restart>` ("inpcrd") and
:ref:`binary restart<netcdf-restart>` ("ncrst") coordinate files. MDAnalysis
supports reading of both file formats.
.. _Amber: https://ambermd.org/FileFormats.php
.. rubric:: Units
AMBER restart files are assumed to be in the following units:
Classes
-------
* length in Angstrom (Å)
* time in ps
* velocity (NCRST only) in Å / ps
* force (NCRST only) in kcal / (mol * Å)
.. _ascii-restart:
ASCII INPCRD restart files
--------------------------
ASCII AMBER_ INPCRD coordinate files (as defined in `AMBER INPCRD FORMAT`_)
are handled by the :class:`INPReader`.
AMBER ASICC restart files are recognised by the suffix '.inpcrd', '.restrt', or
'.rst7'
.. autoclass:: INPReader
:members:
.. _netcdf-restart:
Binary NetCDF restart files
---------------------------
The `AMBER netcdf`_ restart format makes use of NetCDF_ (Network Common Data
Form) format. Such binary restart files are recognised in MDAnalysis by the
suffix '.ncrst', '.ncrestrt' or '.ncrst7' and read by the :class:`NCRSTReader`.
Binary restart files can also contain velocity and force information, and can
record the simulation time step. Whilst the `AMBER netcdf`_ format details
default unit values of ångström and picoseconds, these can in theory occupy
any unit type. However, at the moment MDAnalysis only supports the default
types and will raise a :exc:`NotImplementedError` if anything else is detected.
.. autoclass:: NCRSTReader
:members:
.. Links
.. _AMBER: http://ambermd.org
.. _AMBER INPCRD FORMAT: http://ambermd.org/formats.html#restart
.. _AMBER netcdf: http://ambermd.org/netcdf/nctraj.xhtml
.. _NetCDF: http://www.unidata.ucar.edu/software/netcdf
"""

from . import base
import warnings
import logging
from scipy.io import netcdf_file

from .timestep import Timestep
from ..lib.util import store_init_arguments
from .TRJ import NCDFPicklable, NCDFMixin

logger = logging.getLogger("MDAnalysis.coordinates.AMBER")


class INPReader(base.SingleFrameReaderBase):
"""Reader for Amber restart files."""
"""Reader for Amber restart files.
.. rubric:: Limitations
* Box information is not read (or checked for).
* Velocities are currently *not supported*.
.. versionchanged: 0.20.0
Now automatically detects files with .rst7 extension.
"""

format = ["INPCRD", "RESTRT"]
format = ["INPCRD", "RESTRT", "RST7"]
units = {"length": "Angstrom"}

def _read_first_frame(self):
Expand Down Expand Up @@ -89,3 +152,116 @@
f.readline()
n_atoms = int(f.readline().split()[0])
return n_atoms


class NCRSTReader(base.SingleFrameReaderBase, NCDFMixin):
"""Reader for `AMBER NETCDF format`_ (version 1.0 rev C) restart files.
This reader is a :class:`SingleFrameReaderBase` adaptation of the
:class:`NCDFReader` AMBER NETCDF trajectory reader.
AMBER binary restart files are automatically recognised by the file
extensions ".ncrst", ".ncrestrt", and ".ncrst7".
The number of atoms (`n_atoms`) does not have to be provided as it can
be read from the input NETCDF file.
Current simulation time is autodetected and if available is read into the
:attr:`Timestep.time` attribute.
Velocities are autodetected and read into the :attr:`Timestep._velocities`
attribute.
Forces are autodetected and read into the :attr:`Timestep._forces`
attribute.
Periodic unit cell information is detected and used to populate the
:attr:`Timestep.dimensions` attribute. (If no unit cell is available in
the restart file, then :attr:`Timestep.dimensions` will return
``[0,0,0,0,0,0]``).
Support for the *mmap* keyword is available as detailed
in :class:`NCDFReader` and :mod:`scipy.io.netcdf.netcdf_file`. The use of
``mmap=True`` leads to around a 2x read speed improvement in a ~ 1 million
atom system (AMBER STMV benchmark). As per the :class:`NCDFReader`, the
default behaviour is ``mmap=None``, which means that the default behaviour
of :class:`scipy.io.netcdf.netcdf_file` prevails.
The NCRST reader also uses a custom Timestep object with C-style memory
mapping in order to match the NCDFReader.
.. rubric:: Limitations
* Only NCRST files with time in ps, lengths in Angstroem and angles in
degree are processed.
* Restart files without coordinate information are not supported.
* Replica exchange variables are not supported.
.. _AMBER NETCDF format: http://ambermd.org/netcdf/nctraj.xhtml
See Also
--------
:class:`NCDFReader`
:class:`NCDFWriter`
.. versionadded: 2.10.0
"""

format = ["NCRST", "NCRESTRT", "NCRST7"]
version = "1.0"
units = {
"time": "ps",
"length": "Angstrom",
"velocity": "Angstrom/ps",
"force": "kcal/(mol*Angstrom)",
}

_Timestep = Timestep

@store_init_arguments
def __init__(
self, filename, n_atoms=None, convert_units=None, mmap=None, **kwargs
):
# Assign input mmap value
self._mmap = mmap

super(NCRSTReader, self).__init__(
filename, convert_units, n_atoms, **kwargs
)

@staticmethod
def parse_n_atoms(filename, **kwargs):
with scipy.io.netcdf_file(filename, mmap=None) as f:
n_atoms = f.dimensions["atom"]
return n_atoms

Check warning on line 237 in package/MDAnalysis/coordinates/INPCRD.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/coordinates/INPCRD.py#L235-L237

Added lines #L235 - L237 were not covered by tests

@staticmethod
def _verify_units(eval_units, expected_units):
if eval_units.decode("utf-8") != expected_units:
errmsg = (
"NCRSTReader currently assumes that the trajectory "
"was written in units of {0} instead of {1}".format(
eval_units.decode("utf-8"), expected_units
)
)
raise NotImplementedError(errmsg)

def _read_first_frame(self):
"""Function to read NetCDF restart file and fill timestep"""
# Open netcdf file via context manager
# ensure maskandscale is off so we don't end up double scaling

with NCDFPicklable(
self.filename, mode="r", mmap=self._mmap, maskandscale=False
) as self.trjfile:

self._check_conventions(n_atoms=self.n_atoms)

self.n_frames = 1

self._read_values(
frame=()
) # AMBERRESTART convention files have dimensionless datasets

self.ts.frame = 0 # 0-indexed
Loading
Loading