Initial version
This commit is contained in:
commit
68b8e1a305
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
doc/_build
|
||||||
|
.idea
|
||||||
|
__pycache__
|
||||||
|
build/
|
||||||
|
dist/
|
||||||
|
*.egg-info
|
||||||
|
logarithmic.*.so
|
||||||
|
logarithmic.c
|
||||||
|
coordinates.*.so
|
||||||
|
coordinates.c
|
||||||
|
.cache/
|
||||||
|
doc/gallery
|
||||||
|
doc/modules
|
||||||
|
tmp/
|
||||||
|
*.xtcindex
|
30
LICENSE
Normal file
30
LICENSE
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
Copyright (c) 2017, Niels Müller, Matthias Bartelmeß, Robin Horstmann.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer in the documentation and/or other materials provided
|
||||||
|
with the distribution.
|
||||||
|
|
||||||
|
* Neither the name of the copyright holder nor the names of any
|
||||||
|
contributors may be used to endorse or promote products derived
|
||||||
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
45
README.md
Normal file
45
README.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# mdevaluate
|
||||||
|
|
||||||
|
Mdevaluate is a Python package to perform analyses of Molecular Dynamics simulations.
|
||||||
|
An online documentation is available at [mdevaluate.github.io](https://mdevaluate.github.io).
|
||||||
|
Mdevaluate provides a flexible interface for the detailed analysis of dynamical and statical properties of molecular systems.
|
||||||
|
It's main focus is the analysis of Gromacs data, but with the help of external packages ([MDAnalysis](https://www.mdanalysis.org/))
|
||||||
|
it can also handle file formats, used by other simulation software.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import mdevaluate as md
|
||||||
|
|
||||||
|
# load the simulation
|
||||||
|
tr = md.open(
|
||||||
|
directory='/path/to/simulation',
|
||||||
|
topology='topol.tpr',
|
||||||
|
trajectory='traj.xtc'
|
||||||
|
)
|
||||||
|
# select a subset of atoms
|
||||||
|
water_oxygen = tr.subset(residue_name='SOL', atom_name='OW')
|
||||||
|
|
||||||
|
# calculate the mean squared displacement for this subset
|
||||||
|
time, msd = md.correlation.shifted_correlation(
|
||||||
|
md.correlation.msd, water_oxygen, average=True
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
The package requires the Python package [pygmx](https://github.com/mdevaluate/pygmx),
|
||||||
|
which handles reading of Gromacs file formats.
|
||||||
|
Installation of pygmx is described in its own repository.
|
||||||
|
|
||||||
|
The mdevaluate package itself is plain Python code and, hence, can be imported from its directory directly,
|
||||||
|
or may be installed via setuptools to the local Python environment by running
|
||||||
|
|
||||||
|
python setup.py install
|
||||||
|
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Mdevaluate includes a test suite that can be used to check if the installation was succesful.
|
||||||
|
It is based on `py.test` and located in the test directory of this repository.
|
||||||
|
Make sure py.test is installed and run `py.test` within the repository to check if all tests pass.
|
||||||
|
|
||||||
|
|
195
doc/Makefile
Normal file
195
doc/Makefile
Normal file
@ -0,0 +1,195 @@
|
|||||||
|
# Makefile for Sphinx documentation
|
||||||
|
#
|
||||||
|
|
||||||
|
# You can set these variables from the command line.
|
||||||
|
SPHINXOPTS =
|
||||||
|
SPHINXBUILD = sphinx-build
|
||||||
|
PAPER =
|
||||||
|
BUILDDIR = _build
|
||||||
|
|
||||||
|
# User-friendly check for sphinx-build
|
||||||
|
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
|
||||||
|
$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/)
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Internal variables.
|
||||||
|
PAPEROPT_a4 = -D latex_paper_size=a4
|
||||||
|
PAPEROPT_letter = -D latex_paper_size=letter
|
||||||
|
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
|
||||||
|
# the i18n builder cannot share the environment and doctrees with the others
|
||||||
|
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
|
||||||
|
|
||||||
|
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext
|
||||||
|
|
||||||
|
help:
|
||||||
|
@echo "Please use \`make <target>' where <target> is one of"
|
||||||
|
@echo " html to make standalone HTML files"
|
||||||
|
@echo " dirhtml to make HTML files named index.html in directories"
|
||||||
|
@echo " singlehtml to make a single large HTML file"
|
||||||
|
@echo " pickle to make pickle files"
|
||||||
|
@echo " json to make JSON files"
|
||||||
|
@echo " htmlhelp to make HTML files and a HTML help project"
|
||||||
|
@echo " qthelp to make HTML files and a qthelp project"
|
||||||
|
@echo " applehelp to make an Apple Help Book"
|
||||||
|
@echo " devhelp to make HTML files and a Devhelp project"
|
||||||
|
@echo " epub to make an epub"
|
||||||
|
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
|
||||||
|
@echo " latexpdf to make LaTeX files and run them through pdflatex"
|
||||||
|
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
|
||||||
|
@echo " text to make text files"
|
||||||
|
@echo " man to make manual pages"
|
||||||
|
@echo " texinfo to make Texinfo files"
|
||||||
|
@echo " info to make Texinfo files and run them through makeinfo"
|
||||||
|
@echo " gettext to make PO message catalogs"
|
||||||
|
@echo " changes to make an overview of all changed/added/deprecated items"
|
||||||
|
@echo " xml to make Docutils-native XML files"
|
||||||
|
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
|
||||||
|
@echo " linkcheck to check all external links for integrity"
|
||||||
|
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
|
||||||
|
@echo " coverage to run coverage check of the documentation (if enabled)"
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILDDIR)/*
|
||||||
|
|
||||||
|
html:
|
||||||
|
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
|
||||||
|
|
||||||
|
dirhtml:
|
||||||
|
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
|
||||||
|
|
||||||
|
singlehtml:
|
||||||
|
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
|
||||||
|
|
||||||
|
pickle:
|
||||||
|
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can process the pickle files."
|
||||||
|
|
||||||
|
json:
|
||||||
|
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can process the JSON files."
|
||||||
|
|
||||||
|
htmlhelp:
|
||||||
|
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can run HTML Help Workshop with the" \
|
||||||
|
".hhp project file in $(BUILDDIR)/htmlhelp."
|
||||||
|
|
||||||
|
qthelp:
|
||||||
|
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
|
||||||
|
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
|
||||||
|
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/mdevaluate.qhcp"
|
||||||
|
@echo "To view the help file:"
|
||||||
|
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/mdevaluate.qhc"
|
||||||
|
|
||||||
|
applehelp:
|
||||||
|
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
|
||||||
|
@echo "N.B. You won't be able to view it unless you put it in" \
|
||||||
|
"~/Library/Documentation/Help or install it in your application" \
|
||||||
|
"bundle."
|
||||||
|
|
||||||
|
devhelp:
|
||||||
|
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished."
|
||||||
|
@echo "To view the help file:"
|
||||||
|
@echo "# mkdir -p $$HOME/.local/share/devhelp/mdevaluate"
|
||||||
|
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/mdevaluate"
|
||||||
|
@echo "# devhelp"
|
||||||
|
|
||||||
|
epub:
|
||||||
|
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
|
||||||
|
|
||||||
|
latex:
|
||||||
|
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
|
||||||
|
@echo "Run \`make' in that directory to run these through (pdf)latex" \
|
||||||
|
"(use \`make latexpdf' here to do that automatically)."
|
||||||
|
|
||||||
|
latexpdf:
|
||||||
|
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
|
||||||
|
@echo "Running LaTeX files through pdflatex..."
|
||||||
|
$(MAKE) -C $(BUILDDIR)/latex all-pdf
|
||||||
|
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
|
||||||
|
|
||||||
|
latexpdfja:
|
||||||
|
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
|
||||||
|
@echo "Running LaTeX files through platex and dvipdfmx..."
|
||||||
|
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
|
||||||
|
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
|
||||||
|
|
||||||
|
text:
|
||||||
|
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The text files are in $(BUILDDIR)/text."
|
||||||
|
|
||||||
|
man:
|
||||||
|
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
|
||||||
|
|
||||||
|
texinfo:
|
||||||
|
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
|
||||||
|
@echo "Run \`make' in that directory to run these through makeinfo" \
|
||||||
|
"(use \`make info' here to do that automatically)."
|
||||||
|
|
||||||
|
info:
|
||||||
|
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
|
||||||
|
@echo "Running Texinfo files through makeinfo..."
|
||||||
|
make -C $(BUILDDIR)/texinfo info
|
||||||
|
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
|
||||||
|
|
||||||
|
gettext:
|
||||||
|
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
|
||||||
|
|
||||||
|
changes:
|
||||||
|
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
|
||||||
|
@echo
|
||||||
|
@echo "The overview file is in $(BUILDDIR)/changes."
|
||||||
|
|
||||||
|
linkcheck:
|
||||||
|
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
|
||||||
|
@echo
|
||||||
|
@echo "Link check complete; look for any errors in the above output " \
|
||||||
|
"or in $(BUILDDIR)/linkcheck/output.txt."
|
||||||
|
|
||||||
|
doctest:
|
||||||
|
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
|
||||||
|
@echo "Testing of doctests in the sources finished, look at the " \
|
||||||
|
"results in $(BUILDDIR)/doctest/output.txt."
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
|
||||||
|
@echo "Testing of coverage in the sources finished, look at the " \
|
||||||
|
"results in $(BUILDDIR)/coverage/python.txt."
|
||||||
|
|
||||||
|
xml:
|
||||||
|
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
|
||||||
|
|
||||||
|
pseudoxml:
|
||||||
|
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
|
||||||
|
|
||||||
|
deploy: html
|
||||||
|
rsync -r _build/html/ /autohome/niels/public_html/mdevaluate/
|
317
doc/conf.py
Normal file
317
doc/conf.py
Normal file
@ -0,0 +1,317 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# mdevaluate documentation build configuration file, created by
|
||||||
|
# sphinx-quickstart on Tue Nov 10 11:46:41 2015.
|
||||||
|
#
|
||||||
|
# This file is execfile()d with the current directory set to its
|
||||||
|
# containing dir.
|
||||||
|
#
|
||||||
|
# Note that not all possible configuration values are present in this
|
||||||
|
# autogenerated file.
|
||||||
|
#
|
||||||
|
# All configuration values have a default; values that are commented out
|
||||||
|
# serve to show the default.
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import shlex
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.abspath('..'))
|
||||||
|
import mdevaluate
|
||||||
|
|
||||||
|
# If extensions (or modules to document with autodoc) are in another directory,
|
||||||
|
# add these directories to sys.path here. If the directory is relative to the
|
||||||
|
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||||
|
#sys.path.insert(0, os.path.abspath('.'))
|
||||||
|
|
||||||
|
# -- General configuration ------------------------------------------------
|
||||||
|
|
||||||
|
# If your documentation needs a minimal Sphinx version, state it here.
|
||||||
|
#needs_sphinx = '1.0'
|
||||||
|
|
||||||
|
# Add any Sphinx extension module names here, as strings. They can be
|
||||||
|
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||||
|
# ones.
|
||||||
|
extensions = [
|
||||||
|
'sphinx.ext.autodoc',
|
||||||
|
'sphinx.ext.doctest',
|
||||||
|
'sphinx.ext.mathjax',
|
||||||
|
'sphinx.ext.viewcode',
|
||||||
|
'sphinx.ext.napoleon',
|
||||||
|
'sphinx.ext.intersphinx',
|
||||||
|
# 'sphinx.ext.autosummary',
|
||||||
|
# 'sphinx.ext.inheritance_diagram',
|
||||||
|
'sphinx_gallery.gen_gallery',
|
||||||
|
'sphinxcontrib.github_ribbon'
|
||||||
|
]
|
||||||
|
|
||||||
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
|
templates_path = ['_templates']
|
||||||
|
|
||||||
|
# The suffix(es) of source filenames.
|
||||||
|
# You can specify multiple suffix as a list of string:
|
||||||
|
# source_suffix = ['.rst', '.md']
|
||||||
|
source_suffix = '.rst'
|
||||||
|
|
||||||
|
# The encoding of source files.
|
||||||
|
source_encoding = 'utf-8-sig'
|
||||||
|
|
||||||
|
# The master toctree document.
|
||||||
|
master_doc = 'index'
|
||||||
|
|
||||||
|
# General information about the project.
|
||||||
|
project = 'mdevaluate'
|
||||||
|
copyright = '2017, Niels Müller'
|
||||||
|
author = 'Niels Müller'
|
||||||
|
|
||||||
|
# The version info for the project you're documenting, acts as replacement for
|
||||||
|
# |version| and |release|, also used in various other places throughout the
|
||||||
|
# built documents.
|
||||||
|
#
|
||||||
|
# The short X.Y version.
|
||||||
|
version = mdevaluate.__version__
|
||||||
|
# The full version, including alpha/beta/rc tags.
|
||||||
|
release = mdevaluate.__version__
|
||||||
|
|
||||||
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||||
|
# for a list of supported languages.
|
||||||
|
#
|
||||||
|
# This is also used if you do content translation via gettext catalogs.
|
||||||
|
# Usually you set "language" from the command line for these cases.
|
||||||
|
language = None
|
||||||
|
|
||||||
|
# There are two options for replacing |today|: either, you set today to some
|
||||||
|
# non-false value, then it is used:
|
||||||
|
#today = ''
|
||||||
|
# Else, today_fmt is used as the format for a strftime call.
|
||||||
|
#today_fmt = '%B %d, %Y'
|
||||||
|
|
||||||
|
# List of patterns, relative to source directory, that match files and
|
||||||
|
# directories to ignore when looking for source files.
|
||||||
|
exclude_patterns = ['_build']
|
||||||
|
|
||||||
|
# The reST default role (used for this markup: `text`) to use for all
|
||||||
|
# documents.
|
||||||
|
#default_role = None
|
||||||
|
|
||||||
|
# If true, '()' will be appended to :func: etc. cross-reference text.
|
||||||
|
#add_function_parentheses = True
|
||||||
|
|
||||||
|
# If true, the current module name will be prepended to all description
|
||||||
|
# unit titles (such as .. function::).
|
||||||
|
#add_module_names = True
|
||||||
|
|
||||||
|
# If true, sectionauthor and moduleauthor directives will be shown in the
|
||||||
|
# output. They are ignored by default.
|
||||||
|
#show_authors = False
|
||||||
|
|
||||||
|
# The name of the Pygments (syntax highlighting) style to use.
|
||||||
|
pygments_style = 'sphinx'
|
||||||
|
highlight_language = "python3"
|
||||||
|
|
||||||
|
# A list of ignored prefixes for module index sorting.
|
||||||
|
#modindex_common_prefix = []
|
||||||
|
|
||||||
|
# If true, keep warnings as "system message" paragraphs in the built documents.
|
||||||
|
#keep_warnings = False
|
||||||
|
|
||||||
|
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
||||||
|
todo_include_todos = False
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for HTML output ----------------------------------------------
|
||||||
|
|
||||||
|
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||||
|
# a list of builtin themes.
|
||||||
|
html_theme = 'sphinx_rtd_theme'
|
||||||
|
|
||||||
|
# Theme options are theme-specific and customize the look and feel of a theme
|
||||||
|
# further. For a list of options available for each theme, see the
|
||||||
|
# documentation.
|
||||||
|
#html_theme_options = {}
|
||||||
|
|
||||||
|
# Add any paths that contain custom themes here, relative to this directory.
|
||||||
|
#html_theme_path = []
|
||||||
|
|
||||||
|
# The name for this set of Sphinx documents. If None, it defaults to
|
||||||
|
# "<project> v<release> documentation".
|
||||||
|
#html_title = None
|
||||||
|
|
||||||
|
# A shorter title for the navigation bar. Default is the same as html_title.
|
||||||
|
#html_short_title = None
|
||||||
|
|
||||||
|
# The name of an image file (relative to this directory) to place at the top
|
||||||
|
# of the sidebar.
|
||||||
|
#html_logo = None
|
||||||
|
|
||||||
|
# The name of an image file (within the static path) to use as favicon of the
|
||||||
|
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
|
||||||
|
# pixels large.
|
||||||
|
#html_favicon = None
|
||||||
|
|
||||||
|
# Add any paths that contain custom static files (such as style sheets) here,
|
||||||
|
# relative to this directory. They are copied after the builtin static files,
|
||||||
|
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||||
|
html_static_path = ['_static']
|
||||||
|
|
||||||
|
# Add any extra paths that contain custom files (such as robots.txt or
|
||||||
|
# .htaccess) here, relative to this directory. These files are copied
|
||||||
|
# directly to the root of the documentation.
|
||||||
|
#html_extra_path = []
|
||||||
|
|
||||||
|
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
|
||||||
|
# using the given strftime format.
|
||||||
|
#html_last_updated_fmt = '%b %d, %Y'
|
||||||
|
|
||||||
|
# If true, SmartyPants will be used to convert quotes and dashes to
|
||||||
|
# typographically correct entities.
|
||||||
|
#html_use_smartypants = True
|
||||||
|
|
||||||
|
# Custom sidebar templates, maps document names to template names.
|
||||||
|
#html_sidebars = {}
|
||||||
|
|
||||||
|
# Additional templates that should be rendered to pages, maps page names to
|
||||||
|
# template names.
|
||||||
|
#html_additional_pages = {}
|
||||||
|
|
||||||
|
# If false, no module index is generated.
|
||||||
|
#html_domain_indices = True
|
||||||
|
|
||||||
|
# If false, no index is generated.
|
||||||
|
#html_use_index = True
|
||||||
|
|
||||||
|
# If true, the index is split into individual pages for each letter.
|
||||||
|
#html_split_index = False
|
||||||
|
|
||||||
|
# If true, links to the reST sources are added to the pages.
|
||||||
|
#html_show_sourcelink = True
|
||||||
|
|
||||||
|
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
|
||||||
|
#html_show_sphinx = True
|
||||||
|
|
||||||
|
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
|
||||||
|
#html_show_copyright = True
|
||||||
|
|
||||||
|
# If true, an OpenSearch description file will be output, and all pages will
|
||||||
|
# contain a <link> tag referring to it. The value of this option must be the
|
||||||
|
# base URL from which the finished HTML is served.
|
||||||
|
#html_use_opensearch = ''
|
||||||
|
|
||||||
|
# This is the file name suffix for HTML files (e.g. ".xhtml").
|
||||||
|
#html_file_suffix = None
|
||||||
|
|
||||||
|
# Language to be used for generating the HTML full-text search index.
|
||||||
|
# Sphinx supports the following languages:
|
||||||
|
# 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja'
|
||||||
|
# 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr'
|
||||||
|
#html_search_language = 'en'
|
||||||
|
|
||||||
|
# A dictionary with options for the search language support, empty by default.
|
||||||
|
# Now only 'ja' uses this config value
|
||||||
|
#html_search_options = {'type': 'default'}
|
||||||
|
|
||||||
|
# The name of a javascript file (relative to the configuration directory) that
|
||||||
|
# implements a search results scorer. If empty, the default will be used.
|
||||||
|
#html_search_scorer = 'scorer.js'
|
||||||
|
|
||||||
|
# Output file base name for HTML help builder.
|
||||||
|
htmlhelp_basename = 'mdevaluatedoc'
|
||||||
|
|
||||||
|
# -- Options for LaTeX output ---------------------------------------------
|
||||||
|
|
||||||
|
latex_elements = {
|
||||||
|
# The paper size ('letterpaper' or 'a4paper').
|
||||||
|
#'papersize': 'letterpaper',
|
||||||
|
|
||||||
|
# The font size ('10pt', '11pt' or '12pt').
|
||||||
|
#'pointsize': '10pt',
|
||||||
|
|
||||||
|
# Additional stuff for the LaTeX preamble.
|
||||||
|
#'preamble': '',
|
||||||
|
|
||||||
|
# Latex figure (float) alignment
|
||||||
|
#'figure_align': 'htbp',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Grouping the document tree into LaTeX files. List of tuples
|
||||||
|
# (source start file, target name, title,
|
||||||
|
# author, documentclass [howto, manual, or own class]).
|
||||||
|
latex_documents = [
|
||||||
|
(master_doc, 'mdevaluate.tex', 'mdevaluate Documentation',
|
||||||
|
'mbartelm', 'manual'),
|
||||||
|
]
|
||||||
|
|
||||||
|
# The name of an image file (relative to this directory) to place at the top of
|
||||||
|
# the title page.
|
||||||
|
#latex_logo = None
|
||||||
|
|
||||||
|
# For "manual" documents, if this is true, then toplevel headings are parts,
|
||||||
|
# not chapters.
|
||||||
|
#latex_use_parts = False
|
||||||
|
|
||||||
|
# If true, show page references after internal links.
|
||||||
|
#latex_show_pagerefs = False
|
||||||
|
|
||||||
|
# If true, show URL addresses after external links.
|
||||||
|
#latex_show_urls = False
|
||||||
|
|
||||||
|
# Documents to append as an appendix to all manuals.
|
||||||
|
#latex_appendices = []
|
||||||
|
|
||||||
|
# If false, no module index is generated.
|
||||||
|
#latex_domain_indices = True
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for manual page output ---------------------------------------
|
||||||
|
|
||||||
|
# One entry per manual page. List of tuples
|
||||||
|
# (source start file, name, description, authors, manual section).
|
||||||
|
man_pages = [
|
||||||
|
(master_doc, 'mdevaluate', 'mdevaluate Documentation',
|
||||||
|
[author], 1)
|
||||||
|
]
|
||||||
|
|
||||||
|
# If true, show URL addresses after external links.
|
||||||
|
#man_show_urls = False
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for Texinfo output -------------------------------------------
|
||||||
|
|
||||||
|
# Grouping the document tree into Texinfo files. List of tuples
|
||||||
|
# (source start file, target name, title, author,
|
||||||
|
# dir menu entry, description, category)
|
||||||
|
texinfo_documents = [
|
||||||
|
(master_doc, 'mdevaluate', 'mdevaluate Documentation',
|
||||||
|
author, 'mdevaluate', 'One line description of project.',
|
||||||
|
'Miscellaneous'),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Documents to append as an appendix to all manuals.
|
||||||
|
#texinfo_appendices = []
|
||||||
|
|
||||||
|
# If false, no module index is generated.
|
||||||
|
#texinfo_domain_indices = True
|
||||||
|
|
||||||
|
# How to display URL addresses: 'footnote', 'no', or 'inline'.
|
||||||
|
#texinfo_show_urls = 'footnote'
|
||||||
|
|
||||||
|
# If true, do not generate a @detailmenu in the "Top" node's menu.
|
||||||
|
#texinfo_no_detailmenu = False
|
||||||
|
|
||||||
|
intersphinx_mapping = {
|
||||||
|
'python': ('http://docs.python.org/3/', None),
|
||||||
|
'numpy': ('http://docs.scipy.org/doc/numpy/', None),
|
||||||
|
'ipython': ('http://ipython.org/ipython-doc/dev/', None),
|
||||||
|
'scipy': ('http://docs.scipy.org/doc/scipy/reference/', None),
|
||||||
|
}
|
||||||
|
|
||||||
|
sphinx_gallery_conf = {
|
||||||
|
# path to your examples scripts
|
||||||
|
'examples_dirs' : '../examples',
|
||||||
|
# path where to save gallery generated examples
|
||||||
|
'gallery_dirs' : 'gallery'}
|
||||||
|
|
||||||
|
|
||||||
|
github_ribbon_repo = 'mdevaluate/mdevaluate'
|
||||||
|
github_ribbon_color = 'green'
|
244
doc/contributing.rst
Normal file
244
doc/contributing.rst
Normal file
@ -0,0 +1,244 @@
|
|||||||
|
|
||||||
|
Contributing
|
||||||
|
============
|
||||||
|
|
||||||
|
This document aims to lay out the basics of contributing code to the ``mdevaluate`` package.
|
||||||
|
The code is managed through a git repository, hence this guides gives basic information on the usage of `git <https://git-scm.com>`_.
|
||||||
|
Int this document the prefix ``$`` indicates commands which should be ran on a shell.
|
||||||
|
For a brief 15 min interactive tutorial visit `try.github.org <https://try.gitbhub.org>`_.
|
||||||
|
|
||||||
|
|
||||||
|
Let's start with a short introduction to the terminology.
|
||||||
|
Python code is organized in *packages* and *modules*:
|
||||||
|
|
||||||
|
Modules:
|
||||||
|
Any python file (e.g. ``test.py``) is called a module. A module can be imported (``import test``) an then used
|
||||||
|
in other python code if in the python path, for example the working directory.
|
||||||
|
In principle, importing a package means executing the code inside the file.
|
||||||
|
All definitions, like variables or functions, are then available under the modules name.
|
||||||
|
|
||||||
|
Packages:
|
||||||
|
Python modules can be grouped into packages. A python package is basically a folder,
|
||||||
|
which contains at least one mandatory file ``__init__.py``. This file is the entry
|
||||||
|
point into the module that is imported if the package is imported.
|
||||||
|
All modules in the folder are treated as submodules, which can be accessed via
|
||||||
|
a dot syntax, e.g. ``import package.test``. Packages can also contain sub packages.
|
||||||
|
|
||||||
|
A more `detailed explanation <https://docs.python.org/3/tutorial/modules.html>`_ can be found in the official python documentation.
|
||||||
|
|
||||||
|
Extending the documentation
|
||||||
|
+++++++++++++++++++++++++++
|
||||||
|
|
||||||
|
One of the most important parts of software is its documentation.
|
||||||
|
For modular packages like ``mdevaluate`` it's crucial to have a good coverage of the API,
|
||||||
|
since users need to know which functions are provided and how they are used.
|
||||||
|
To help others by extending the documentation is thereby a nice way of contributing to mdevaluate.
|
||||||
|
|
||||||
|
The documentation is generated with a third party tools named `Sphinx <http://www.sphinx-doc.org/en/stable/>`_.
|
||||||
|
The contents of the documentation are based on the source code (for the reference guide)
|
||||||
|
and documents written in the markup language *reStructuredText* (rst).
|
||||||
|
The source of every page can be viewed in the browser through the *View page source* link in the upper right of the page.
|
||||||
|
The name of the rst files can also be derived from the page URL.
|
||||||
|
The rst files are placed in the ``doc`` directory of the repository.
|
||||||
|
|
||||||
|
Extending the documentation can be done in different ways, e.g.
|
||||||
|
|
||||||
|
- Correct, clarify or extend existing sections
|
||||||
|
- Add new sections about the general use of mdevaluate
|
||||||
|
- Add use cases to the special topics section.
|
||||||
|
|
||||||
|
To add a new sections to special topics, first create a new file for this guide in ``doc/special``.
|
||||||
|
Then add the name of this file (without the .rst extension) to the toctree in the file ``special-topics.rst``.
|
||||||
|
Now write the guide in the newly created file.
|
||||||
|
|
||||||
|
Building the docs
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
When you have made changes to the docs, first re-build them locally.
|
||||||
|
You will need to have the ``sphinx`` python package installed and of course a working environment for ``mdevaluate``.
|
||||||
|
When those requirements are fulfilled build the docs by:
|
||||||
|
|
||||||
|
1. Navigate to the ``doc`` directory
|
||||||
|
2. Run ``make html`` in the shell
|
||||||
|
3. View the produced html files in the browser: ``firefox _build/html/index.html``
|
||||||
|
|
||||||
|
Organization of the code
|
||||||
|
++++++++++++++++++++++++
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
The code for the evaluation software is organized in two python packages:
|
||||||
|
|
||||||
|
- ``pygmx``: This package provides a python wrapper for the Gromacs library and
|
||||||
|
thereby functionality to read file formats used within Gromacs.
|
||||||
|
- ``mdevaluate``: This package provides functionality for evaluation of molecular
|
||||||
|
dynamics simulations. It uses the ``pygmx`` package to read files, but is
|
||||||
|
(in theory) not limited to Gromacs data.
|
||||||
|
|
||||||
|
Submodules
|
||||||
|
----------
|
||||||
|
|
||||||
|
Below the content of the submodules of the package is described.
|
||||||
|
|
||||||
|
atoms.py
|
||||||
|
........
|
||||||
|
|
||||||
|
Definition of the ``Atom`` class and related functions for atom selection and information.
|
||||||
|
|
||||||
|
autosave.py
|
||||||
|
...........
|
||||||
|
|
||||||
|
Experimental functionality for automatic saving and loading of evaluated data,
|
||||||
|
like correlation functions. For each function call a checksum is calculated
|
||||||
|
from the input, which changes if the output of the function changes.
|
||||||
|
|
||||||
|
coordinates.py
|
||||||
|
..............
|
||||||
|
|
||||||
|
Definition of the ``Coordinates`` class and ``CoordinatesMap`` for coordinates
|
||||||
|
transformations and related functions.
|
||||||
|
|
||||||
|
correlation.py
|
||||||
|
..............
|
||||||
|
|
||||||
|
Functionality to calculate correlation functions.
|
||||||
|
|
||||||
|
distribution.py
|
||||||
|
...............
|
||||||
|
|
||||||
|
Functionality to calculate distribution functions.
|
||||||
|
|
||||||
|
reader.py
|
||||||
|
.........
|
||||||
|
|
||||||
|
Defines reader classes that handle trajectory reading and caching.
|
||||||
|
|
||||||
|
utils.py
|
||||||
|
........
|
||||||
|
|
||||||
|
A collection of utility functions.
|
||||||
|
|
||||||
|
Set up a development environment
|
||||||
|
++++++++++++++++++++++++++++++++
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git clone https://github.com/mdevaluate/mdevaluate.git
|
||||||
|
|
||||||
|
Organization of the repository
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
The repository is organized through git branches.
|
||||||
|
At the moment there exist two branches in the remote repository: *master* and *dev*.
|
||||||
|
|
||||||
|
|
||||||
|
Adding code to the repository
|
||||||
|
+++++++++++++++++++++++++++++
|
||||||
|
|
||||||
|
All changes to the code are done in your local clone of the repository.
|
||||||
|
If a feature is complete, or at least works, the code can be pushed to the remote,
|
||||||
|
to make it accessible for others.
|
||||||
|
|
||||||
|
A standard work flow to submit new code is the following
|
||||||
|
|
||||||
|
1. Fork the main repository o github and clone your fork to your local machine.
|
||||||
|
2. Create a new branch locally and apply the desired changes.
|
||||||
|
3. If the master branch was updated, merge it into the local branch.
|
||||||
|
4. Push the changes to github and create a pull request for your fork.
|
||||||
|
|
||||||
|
Pulling updates from remote
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
Before working with the code, the latest updates should be pulled for the master branch
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git checkout master
|
||||||
|
$ git pull
|
||||||
|
|
||||||
|
Create a new branch
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Before changing any code, create a new branch in your local repository.
|
||||||
|
This helps to keep an overview of all the changes and simplifies merging.
|
||||||
|
To create a new branch locally enter the following commands
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git checkout master
|
||||||
|
$ git branch my-feature
|
||||||
|
$ git checkout my-feature
|
||||||
|
|
||||||
|
First switch to the master branch to make sure the new branch is based on it.
|
||||||
|
Then create the new branch, called `my-feature` and switch to it.
|
||||||
|
Now you can start making changes in the code.
|
||||||
|
|
||||||
|
Committing changes
|
||||||
|
------------------
|
||||||
|
|
||||||
|
A bundle of changes in the code is called a *commit*.
|
||||||
|
These changes can happen in different files and should be associated with each other.
|
||||||
|
Let's assume, two files have been changed (``atoms.py`` and ``utils.py``).
|
||||||
|
The command
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git diff atoms.py
|
||||||
|
|
||||||
|
will show you all changes that were made in the file since the latest commit.
|
||||||
|
Before committing changes have to be *staged*, which is done by
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git add atoms.py utils.py
|
||||||
|
|
||||||
|
This my be repeated as often as necessary.
|
||||||
|
When all changes for a commit are staged, it can actually be created
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git commit
|
||||||
|
|
||||||
|
This will open up an editor where a commit message has to be entered.
|
||||||
|
After writing the commit message, save & close the file, which will create the commit.
|
||||||
|
|
||||||
|
Create Pullrequest
|
||||||
|
------------------
|
||||||
|
|
||||||
|
When all changes are made and the new feature should be made public, you can open a new pull request on github.
|
||||||
|
Most of the time, the master branch will have been updated, so first pull any updates
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git checkout master
|
||||||
|
$ git pull
|
||||||
|
|
||||||
|
When the master branch is up to date, it can be merged into the feature branch
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git checkout my-feature
|
||||||
|
$ git merge master
|
||||||
|
|
||||||
|
If no conflicting changes were made, merging works automatically.
|
||||||
|
If for example the same line was modified in a commit in master and your commits, a merge conflict will occur.
|
||||||
|
Git tells you which files have conflicts and asks you to resolve these.
|
||||||
|
The respective lines will be marked with conflict-resolution markers in the files.
|
||||||
|
The most basic way of resolving a conflict is by editing these files and choosing the appropriate version of the code.
|
||||||
|
See the `git documentation <https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging#Basic-Merge-Conflicts>`_ for an explanation.
|
||||||
|
After resolving the conflict, the files need to be staged and the merge has to be committed
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git add utils.py
|
||||||
|
$ git commit
|
||||||
|
|
||||||
|
The commit message will be generated automatically, indicating the merge.
|
||||||
|
|
||||||
|
After merging, the changes can be pushed to the remote
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ git push
|
||||||
|
|
||||||
|
The new code is now available in the remote.
|
50
doc/dynamic-evaluation.rst
Normal file
50
doc/dynamic-evaluation.rst
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
Evaluation of dynamic properties
|
||||||
|
================================
|
||||||
|
|
||||||
|
Dynamic properties like mean square displacement are calculated with the
|
||||||
|
function :func:`mdevaluate.correlation.shifted_correlation`.
|
||||||
|
This function takes a correlation function and calculates the averaged
|
||||||
|
time series of it, by shifting a time interval over the trajectory.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
from mdevaluate import correlation
|
||||||
|
|
||||||
|
time, msd_amim = correlation.shifted_correlation(correlation.msd, com_amim, average=True)
|
||||||
|
plot(time,msd_amim)
|
||||||
|
|
||||||
|
The result of :func:`shifted_correlation` are two lists, the first one (``time``)
|
||||||
|
contains the times of the frames that have been used for the correlation.
|
||||||
|
The second list ``msd_amim`` is the correlation function at these times.
|
||||||
|
If the keyword ``average=False`` is given, the correlation function for each shifted
|
||||||
|
time window will be returned.
|
||||||
|
|
||||||
|
Arguments of ``shifted_correlation``
|
||||||
|
------------------------------------
|
||||||
|
|
||||||
|
The function :func:`mdevaluate.correlation.shifted_correlation` accepts several keyword arguments.
|
||||||
|
With those arguments, the calculation of the correlation function may be controlled in detail.
|
||||||
|
The mathematical expression for a correlation function is the following:
|
||||||
|
|
||||||
|
.. math:: S(t) = \frac{1}{N} \sum_{i=1}^N C(f, R, t_i, t)
|
||||||
|
|
||||||
|
Here :math:`S(t)` denotes the correlation function at time t, :math:`R` are the coordinates of all atoms
|
||||||
|
and :math:`t_i` are the onset times (:math:`N` is the number of onset times or time windows).
|
||||||
|
Note that the outer sum and division by :math:`N` is only carried out if ``average=True``.
|
||||||
|
The onset times are defined by the keywords ``segments`` and ``window``, with
|
||||||
|
:math:`N = segments` and :math:`t_i = \frac{ (1 - window) \cdot t_{max}}{N} (i - 1)` with the total simulation time :math:`t_{max}`.
|
||||||
|
As can be seen ``segments`` gives the number of onset times and ``window`` defines the part of the simulation time the correlation is calculated for,
|
||||||
|
hence ``window - 1`` is the part of the simulation the onset times a distributed over.
|
||||||
|
|
||||||
|
|
||||||
|
:math:`C(f, R, t_0, t)` is the function that actually correlates the function :math:`f`.
|
||||||
|
For standard correlations the functions :math:`C(...)` and :math:`f` are defined as:
|
||||||
|
|
||||||
|
.. math:: C(f, R, t_0, t) = f(R(t_0), R(t_0 + t))
|
||||||
|
|
||||||
|
.. math:: f(r_0, r) = \langle s(r_0, r) \rangle
|
||||||
|
|
||||||
|
Here the brackets denote an ensemble average, small :math:`r` are coordinates of one frame and :math:`s(r_0, r)` is the value that is correlated,
|
||||||
|
e.g. for the MSD :math:`s(r_0, r) = (r - r_0)^2`.
|
||||||
|
|
||||||
|
The function :math:`C(f, R, t_0, t)` is specified by the keyword ``correlation``, the function :math:`f(r_0, r)` is given by ``function``.
|
80
doc/general-hints.rst
Normal file
80
doc/general-hints.rst
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
General Hints for Python Programming
|
||||||
|
====================================
|
||||||
|
|
||||||
|
This page collects some general hints for data centered programming with Python.
|
||||||
|
Some resources for tutorials on the topics can be found here:
|
||||||
|
|
||||||
|
* http://www.scipy-lectures.org/
|
||||||
|
* The `Python Data Science Handbook <https://jakevdp.github.io/PythonDataScienceHandbook/>`_, by Jake VanderPlas
|
||||||
|
* PyCon-Talk on Numpy arrays: `Losing Your Loops, by Jake VanderPlas <https://www.youtube.com/watch?v=EEUXKG97YRw>`_
|
||||||
|
|
||||||
|
Programming Environments
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
There exist different environments for Python programming, each with their pros and cons.
|
||||||
|
Some examples are:
|
||||||
|
|
||||||
|
* **IPython Console**: The most basic way to use Python is on the interactive console, the ipython console is a suffisticated Python console. After the mdevaluate module is loaded, ipython can be started with the command ``ipython``.
|
||||||
|
* **Jupyter Notebook**: Provides a Mathematica-style notebook, which is accesed through a web browser. After the mdevaluate module is loaded a (local) notebook server can be started with the command ``jupyter-notebook``. See the help menu in the notebook for a short introduction and http://jupyter.org/ for a detailed user guide.
|
||||||
|
* **Atom Editor**: When developing more complex code, like modules an editor comes in handy. Besides basic preinstalled editors (e.g. Gedit) the `atom editor <https://atom.io>`_ is a nice option. Recommended atpm packages for Python development are: language-python, autocomplete-python and linter-flake8.
|
||||||
|
|
||||||
|
Common Pitfalls
|
||||||
|
---------------
|
||||||
|
|
||||||
|
* **For-Loops**: The biggest pitfall of data-intensive Python programming are ``for``-loops. Those loops perform bad in Python, but can be avoided in most cases through Numpy arrays, see the mentioned talk by Jake VdP.
|
||||||
|
* **Non-Portable Code**: Most non-programmers tend to write complex scripts. It's always advisable to source out your code into seperate Python modules (i.e. seperate files) and split the code into reusable functions. Since these modules can be imported from any Python code, this will save time in the long run and often reduces errors.
|
||||||
|
|
||||||
|
|
||||||
|
Pandas Dataframes
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Most data in Mdevaluate is handled as Numpy arrays.
|
||||||
|
For example the function :func:`~mdevaluate.correlation.shifted_correlation` returns a multidimensional array, which contains the time steps and the value of the correlation function.
|
||||||
|
As pointed out above, those arrays a good for computation and can be used to plot data with, e.g. matplotlib.
|
||||||
|
But often there is metadata associated with this data, for example the temperature or the specific subset of atoms that were analyzed.
|
||||||
|
This is the point where **`Pandas dataframes <http://pandas.pydata.org/>`_** come in handy.
|
||||||
|
Dataframes are most basically tables of samples, with named columns.
|
||||||
|
The dataframe class allows easy acces of columns by label and complex operations, like grouping by columns or merging different datasets.
|
||||||
|
|
||||||
|
As an example say we have simulations at some temperatures and want to calculate the ISF and do a KWW-Fit for each of these trajectories.
|
||||||
|
Details of the analysis will be explained at a later point of this document, thereby they will be omitted here::
|
||||||
|
|
||||||
|
import pandas
|
||||||
|
datasets = []
|
||||||
|
|
||||||
|
for T in [250, 260, 270, 280, 290, 300]:
|
||||||
|
# calculate the isf for this temperature
|
||||||
|
t, Sqt = ...
|
||||||
|
|
||||||
|
# DataFrames can be created from dictionaries
|
||||||
|
datasets.append(pandas.DataFrame({'time': t, 'Sqt': Sqt, 'T': T}))
|
||||||
|
|
||||||
|
# join the individual dataframes into one
|
||||||
|
isf_data = pandas.concat(datasets)
|
||||||
|
|
||||||
|
# Now calculate the KWW fits for each temperature
|
||||||
|
from scipy.optimize import curve_fit
|
||||||
|
from mdevaluate.functions import kww
|
||||||
|
kww_datasets = []
|
||||||
|
# The isf data is grouped by temperature,
|
||||||
|
# that is the loop iterates over all T values and the part of the data where isf_data['T'] == T
|
||||||
|
for T, data in isf_data.groupby('T'):
|
||||||
|
fit, cuv = curve_fit(kww, data['time'], data['Sqt'])
|
||||||
|
# DataFrames can also be cerated from arrays and a defintion of columns
|
||||||
|
df = pandas.DataFrame(fit, columns=['A', 'τ', 'β'])
|
||||||
|
# columns can be added dynamically
|
||||||
|
df['T'] = T
|
||||||
|
kww_datasets.append(df)
|
||||||
|
kww_data = pandas.concat(kww_datasets)
|
||||||
|
|
||||||
|
# We have two dataframes now, one with time series of the ISF at each temperature
|
||||||
|
# and one with the fit parameters of the KWW for each temperature
|
||||||
|
|
||||||
|
# We can merge this data into one dataframe, by the overlapping columns (i.e. 'T' in this example)
|
||||||
|
data = pandas.merge(isf_data, kww_data)
|
||||||
|
# We can now compute the kww fit value of each sample point of the isf in one line:
|
||||||
|
data['kww_fit'] = kww(data['time'], data['A'], data['τ'], data['β'])
|
||||||
|
# And plot the data, resolved by temperature.
|
||||||
|
for T, df in data.groupby('T'):
|
||||||
|
plot(df['time'], df['Sqt'], 'o') # The actual correlation value
|
||||||
|
plot(df['time'], df['kww_fit'], '-') # The kww fit
|
11
doc/guide.rst
Normal file
11
doc/guide.rst
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
User Guide
|
||||||
|
==========
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 2
|
||||||
|
|
||||||
|
loading
|
||||||
|
static-evaluation
|
||||||
|
dynamic-evaluation
|
||||||
|
special-topics
|
29
doc/index.rst
Normal file
29
doc/index.rst
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
.. mdevaluate documentation master file, created by
|
||||||
|
sphinx-quickstart on Tue Nov 10 11:46:41 2015.
|
||||||
|
You can adapt this file completely to your liking, but it should at least
|
||||||
|
contain the root `toctree` directive.
|
||||||
|
|
||||||
|
Documentation of mdevaluate
|
||||||
|
===========================
|
||||||
|
|
||||||
|
A python package for evaluation of molecular dynamics simulation data.
|
||||||
|
|
||||||
|
Contents
|
||||||
|
--------
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
installation
|
||||||
|
general-hints
|
||||||
|
guide
|
||||||
|
gallery/index
|
||||||
|
contributing
|
||||||
|
modules
|
||||||
|
|
||||||
|
Indices and tables
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* :ref:`genindex`
|
||||||
|
* :ref:`modindex`
|
||||||
|
* :ref:`search`
|
44
doc/installation.rst
Normal file
44
doc/installation.rst
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
Installation
|
||||||
|
============
|
||||||
|
|
||||||
|
Mdevaluate itself is a pure Python package and can be imported directly from the source directory, if needed.
|
||||||
|
The Gromacs dependency pygmx has to be installed into the Python distribution,
|
||||||
|
since parts are compiled with Cython.
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
------------
|
||||||
|
|
||||||
|
The package depends on some python packages that can all be installed via pip or conda:
|
||||||
|
|
||||||
|
- Python 3.5 (or higher)
|
||||||
|
- NumPy
|
||||||
|
- SciPy
|
||||||
|
|
||||||
|
|
||||||
|
Install pygmx & mdevaluate
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
To instal pygmx, first get the source from its repository, https://github.com/mdevaluate/pygmx.
|
||||||
|
Installation instructions are given in the respective readme file.
|
||||||
|
Two steps have to be performed:
|
||||||
|
|
||||||
|
1. Install Gromacs 2016
|
||||||
|
2. Install pygmx
|
||||||
|
|
||||||
|
When this requirement is met, installing mdevaluate simply means getting the source code from the repository and running
|
||||||
|
|
||||||
|
python setup.py install
|
||||||
|
|
||||||
|
form within the source directory.
|
||||||
|
|
||||||
|
|
||||||
|
Running Tests
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Some tests are included with the source that can be used too test the installation.
|
||||||
|
The testsuite requires `pytest <https://pytest.org>`_.
|
||||||
|
To run the test simply execute
|
||||||
|
|
||||||
|
pytest
|
||||||
|
|
||||||
|
in the source directory.
|
115
doc/loading.rst
Normal file
115
doc/loading.rst
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
Loading of simulation data
|
||||||
|
==========================
|
||||||
|
|
||||||
|
Mdevaulate provides a convenient function :func:`mdevaluate.load_simulation`
|
||||||
|
that loads a simulation more or less automatically.
|
||||||
|
It takes a path as input and looks for all files it needs in this directory.
|
||||||
|
|
||||||
|
For information about the topology either a `tpr` or `gro` a file is read,
|
||||||
|
where the former is the preferred choice.
|
||||||
|
Trajectory data will be read from a xtc file.
|
||||||
|
If the directory contains more than one file of any type, the desired file
|
||||||
|
has to be specified with the appropriate keyword argument.
|
||||||
|
For details see :func:`mdevaluate.open`.
|
||||||
|
|
||||||
|
The function will return a coordinates object, for the whole system.
|
||||||
|
A subset of the system may be obtained directly from the coordinates object by
|
||||||
|
calling its :func:`~mdevaluate.coordinates.Coordinates.subset` method.
|
||||||
|
This function accepts the same input as :func:`mdevaluate.atoms.AtomSubset.subset`.
|
||||||
|
A new feature that was introduced in the function is the possibility to chose
|
||||||
|
atoms with regular expressions.
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
|
||||||
|
The following code loads the example trajectory and selects a subset of all CW atoms.
|
||||||
|
Since there are two CW atoms in each molecule (CW1 and CW2) a regular expression is
|
||||||
|
used when selecting the subset.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
import mdevaluate as md
|
||||||
|
|
||||||
|
trajectory = md.open('/data/niels/tutorial')
|
||||||
|
CW_atoms = trajectory.subset(atom_name='CW.')
|
||||||
|
|
||||||
|
And that's it, now one can evaluate stuff for this subset of atoms.
|
||||||
|
|
||||||
|
Selecting a subset
|
||||||
|
------------------
|
||||||
|
|
||||||
|
As shown in the example above it is often necessary to select a subset of the system for analysis.
|
||||||
|
This can be a special group of atoms (e.g. all C atoms) or a whole residue for which the center of mass should be computed.
|
||||||
|
Subsets are selected with the :func:`~mdevaluate.Coordinates.subset` method of Coordinates objects.
|
||||||
|
|
||||||
|
This method accepts four keyword arguments, with which the atom name, residue name and residue id or atom indices can be specified.
|
||||||
|
The former two name arguments accept a regular expression which allows two include several different names in one subset.
|
||||||
|
Some examples:
|
||||||
|
|
||||||
|
- All carbon atoms (which are named CW1, CT1, CA, ...): ``tr.subset(atom_name='C.*')``
|
||||||
|
- Atoms NA1, NA2 and OW: ``tr.subset(atom_name='NA.|OW')``
|
||||||
|
- All oxygen atoms of residue EG: ``tr.subset(atom_name='O.*', residue_name='EG')``
|
||||||
|
|
||||||
|
|
||||||
|
Specifying data files
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
The above example only works if the directory contains exactly one tpr file and
|
||||||
|
one xtc file.
|
||||||
|
If your data files are located in subdirectories or multiple files of these types exist,
|
||||||
|
they can be specified by the keywords ``topology`` and ``trajectory``.
|
||||||
|
Those filenames can be a relative path to the simulation directory and can also make
|
||||||
|
use of *shell globing*. For example::
|
||||||
|
|
||||||
|
traj = md.open('/path/to/sim', topology='atoms.gro', trajectory='out/traj_*.xtc')
|
||||||
|
|
||||||
|
Note that the topology can be specified as a gro file, with the limitation that
|
||||||
|
only atom and residue names will be read from those files.
|
||||||
|
Information about atom masses and charges for example will only be read from tpr files,
|
||||||
|
therefore it is generally recommended to use the latter topologies.
|
||||||
|
|
||||||
|
The trajectory above is specified through a shell globing, meaning the ``*`` may be
|
||||||
|
expanded to any string (without containing a forward slash).
|
||||||
|
If more than one file exists which match this pattern an error will be raised,
|
||||||
|
since the trajectory can not be identified clearly.
|
||||||
|
|
||||||
|
Caching of frames
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
One bottleneck in the analysis of MD data is the reading speed of the trajectory.
|
||||||
|
In many cases frames will be needed repeatedly and hence the amount of time spend reading
|
||||||
|
data from disk (or worse over the network) is huge.
|
||||||
|
Therefore the mdevaluate package implements a simple caching mechanism, which holds
|
||||||
|
on to a number of read frames.
|
||||||
|
The downside if this is increased memory usage which may slow down the computation too.
|
||||||
|
|
||||||
|
Caching is done on the level of the trajectory readers, so that all ``Coordinate`` and
|
||||||
|
``CoordinateMap`` objects working on the same trajectory will be sharing a cache.
|
||||||
|
Caching has to be activated when opening a trajectory::
|
||||||
|
|
||||||
|
traj = md.open('/path/to/sim', cached=True)
|
||||||
|
|
||||||
|
The ``cached`` keyword takes either a boolean, a integer or None as input value.
|
||||||
|
The value of ``cached`` controls the size of the cache and thereby the additional memory usage.
|
||||||
|
Setting it to True will activate the caching with a maximum size of 128 frames,
|
||||||
|
with an integer any other maximum size may be set.
|
||||||
|
The special value ``None`` will set the cache size to infinite, so all frames will be cached.
|
||||||
|
This will prevent the frames from being loaded twice but can also consume a whole lot of memory,
|
||||||
|
since a single frame can easily take 1 MB of memory.
|
||||||
|
|
||||||
|
Clearing cached frames
|
||||||
|
++++++++++++++++++++++
|
||||||
|
|
||||||
|
In some scenarios it may be advisable to free cached frames which are no longer needed.
|
||||||
|
For this case the reader has a function ``clear_cache()``.
|
||||||
|
The current state of the cache can be displayed with the ``cache_info`` property::
|
||||||
|
|
||||||
|
>>> traj.frames.cache_info
|
||||||
|
CacheInfo(hits=12, misses=20, maxsize=128, currsize=20)
|
||||||
|
>>> traj.frames.clear_cache()
|
||||||
|
>>> traj.frames.cache_info
|
||||||
|
CacheInfo(hits=0, misses=0, maxsize=128, currsize=0)
|
||||||
|
|
||||||
|
Clearing the cache when it is not needed anymore is advisable since this will help the
|
||||||
|
Python interpreter to reuse the memory.
|
||||||
|
|
263
doc/make.bat
Normal file
263
doc/make.bat
Normal file
@ -0,0 +1,263 @@
|
|||||||
|
@ECHO OFF
|
||||||
|
|
||||||
|
REM Command file for Sphinx documentation
|
||||||
|
|
||||||
|
if "%SPHINXBUILD%" == "" (
|
||||||
|
set SPHINXBUILD=sphinx-build
|
||||||
|
)
|
||||||
|
set BUILDDIR=_build
|
||||||
|
set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% .
|
||||||
|
set I18NSPHINXOPTS=%SPHINXOPTS% .
|
||||||
|
if NOT "%PAPER%" == "" (
|
||||||
|
set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
|
||||||
|
set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS%
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "" goto help
|
||||||
|
|
||||||
|
if "%1" == "help" (
|
||||||
|
:help
|
||||||
|
echo.Please use `make ^<target^>` where ^<target^> is one of
|
||||||
|
echo. html to make standalone HTML files
|
||||||
|
echo. dirhtml to make HTML files named index.html in directories
|
||||||
|
echo. singlehtml to make a single large HTML file
|
||||||
|
echo. pickle to make pickle files
|
||||||
|
echo. json to make JSON files
|
||||||
|
echo. htmlhelp to make HTML files and a HTML help project
|
||||||
|
echo. qthelp to make HTML files and a qthelp project
|
||||||
|
echo. devhelp to make HTML files and a Devhelp project
|
||||||
|
echo. epub to make an epub
|
||||||
|
echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter
|
||||||
|
echo. text to make text files
|
||||||
|
echo. man to make manual pages
|
||||||
|
echo. texinfo to make Texinfo files
|
||||||
|
echo. gettext to make PO message catalogs
|
||||||
|
echo. changes to make an overview over all changed/added/deprecated items
|
||||||
|
echo. xml to make Docutils-native XML files
|
||||||
|
echo. pseudoxml to make pseudoxml-XML files for display purposes
|
||||||
|
echo. linkcheck to check all external links for integrity
|
||||||
|
echo. doctest to run all doctests embedded in the documentation if enabled
|
||||||
|
echo. coverage to run coverage check of the documentation if enabled
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "clean" (
|
||||||
|
for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
|
||||||
|
del /q /s %BUILDDIR%\*
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
REM Check if sphinx-build is available and fallback to Python version if any
|
||||||
|
%SPHINXBUILD% 2> nul
|
||||||
|
if errorlevel 9009 goto sphinx_python
|
||||||
|
goto sphinx_ok
|
||||||
|
|
||||||
|
:sphinx_python
|
||||||
|
|
||||||
|
set SPHINXBUILD=python -m sphinx.__init__
|
||||||
|
%SPHINXBUILD% 2> nul
|
||||||
|
if errorlevel 9009 (
|
||||||
|
echo.
|
||||||
|
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
||||||
|
echo.installed, then set the SPHINXBUILD environment variable to point
|
||||||
|
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
||||||
|
echo.may add the Sphinx directory to PATH.
|
||||||
|
echo.
|
||||||
|
echo.If you don't have Sphinx installed, grab it from
|
||||||
|
echo.http://sphinx-doc.org/
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
|
||||||
|
:sphinx_ok
|
||||||
|
|
||||||
|
|
||||||
|
if "%1" == "html" (
|
||||||
|
%SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The HTML pages are in %BUILDDIR%/html.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "dirhtml" (
|
||||||
|
%SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "singlehtml" (
|
||||||
|
%SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "pickle" (
|
||||||
|
%SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished; now you can process the pickle files.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "json" (
|
||||||
|
%SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished; now you can process the JSON files.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "htmlhelp" (
|
||||||
|
%SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished; now you can run HTML Help Workshop with the ^
|
||||||
|
.hhp project file in %BUILDDIR%/htmlhelp.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "qthelp" (
|
||||||
|
%SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished; now you can run "qcollectiongenerator" with the ^
|
||||||
|
.qhcp project file in %BUILDDIR%/qthelp, like this:
|
||||||
|
echo.^> qcollectiongenerator %BUILDDIR%\qthelp\mdevaluate.qhcp
|
||||||
|
echo.To view the help file:
|
||||||
|
echo.^> assistant -collectionFile %BUILDDIR%\qthelp\mdevaluate.ghc
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "devhelp" (
|
||||||
|
%SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "epub" (
|
||||||
|
%SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The epub file is in %BUILDDIR%/epub.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "latex" (
|
||||||
|
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "latexpdf" (
|
||||||
|
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
|
||||||
|
cd %BUILDDIR%/latex
|
||||||
|
make all-pdf
|
||||||
|
cd %~dp0
|
||||||
|
echo.
|
||||||
|
echo.Build finished; the PDF files are in %BUILDDIR%/latex.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "latexpdfja" (
|
||||||
|
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
|
||||||
|
cd %BUILDDIR%/latex
|
||||||
|
make all-pdf-ja
|
||||||
|
cd %~dp0
|
||||||
|
echo.
|
||||||
|
echo.Build finished; the PDF files are in %BUILDDIR%/latex.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "text" (
|
||||||
|
%SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The text files are in %BUILDDIR%/text.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "man" (
|
||||||
|
%SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The manual pages are in %BUILDDIR%/man.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "texinfo" (
|
||||||
|
%SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "gettext" (
|
||||||
|
%SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The message catalogs are in %BUILDDIR%/locale.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "changes" (
|
||||||
|
%SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.The overview file is in %BUILDDIR%/changes.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "linkcheck" (
|
||||||
|
%SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Link check complete; look for any errors in the above output ^
|
||||||
|
or in %BUILDDIR%/linkcheck/output.txt.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "doctest" (
|
||||||
|
%SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Testing of doctests in the sources finished, look at the ^
|
||||||
|
results in %BUILDDIR%/doctest/output.txt.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "coverage" (
|
||||||
|
%SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Testing of coverage in the sources finished, look at the ^
|
||||||
|
results in %BUILDDIR%/coverage/python.txt.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "xml" (
|
||||||
|
%SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The XML files are in %BUILDDIR%/xml.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
if "%1" == "pseudoxml" (
|
||||||
|
%SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
echo.
|
||||||
|
echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml.
|
||||||
|
goto end
|
||||||
|
)
|
||||||
|
|
||||||
|
:end
|
83
doc/mdevaluate.rst
Normal file
83
doc/mdevaluate.rst
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
|
||||||
|
Module contents
|
||||||
|
---------------
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.autosave
|
||||||
|
...................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.autosave
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.atoms
|
||||||
|
................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.atoms
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.coordinates
|
||||||
|
......................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.coordinates
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.correlation
|
||||||
|
......................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.correlation
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.distribution
|
||||||
|
.......................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.distribution
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.evaluation
|
||||||
|
.....................
|
||||||
|
|
||||||
|
mdevaluate.functions
|
||||||
|
....................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.functions
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.pbc
|
||||||
|
..............
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.pbc
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.reader
|
||||||
|
.....................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.reader
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
mdevaluate.utils
|
||||||
|
.....................
|
||||||
|
|
||||||
|
.. automodule:: mdevaluate.utils
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
9
doc/modules.rst
Normal file
9
doc/modules.rst
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
.. _reference-guide:
|
||||||
|
|
||||||
|
Reference Guide
|
||||||
|
===============
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 4
|
||||||
|
|
||||||
|
mdevaluate
|
12
doc/special-topics.rst
Normal file
12
doc/special-topics.rst
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
Special Topics
|
||||||
|
==============
|
||||||
|
|
||||||
|
This part of the documentation describes advanced ways of the use of mdevaluate.
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
|
||||||
|
special/autosave
|
||||||
|
special/spatial
|
||||||
|
special/overlap
|
||||||
|
special/energies
|
93
doc/special/autosave.rst
Normal file
93
doc/special/autosave.rst
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
Automatic Saving of Analysis Data
|
||||||
|
=================================
|
||||||
|
|
||||||
|
Mdevaluate provides a functionality to save the result of analysis functions automatically.
|
||||||
|
The data is saved to a file after it was computed.
|
||||||
|
If an analysis was done in the exact same way before, the result is loaded from this file.
|
||||||
|
|
||||||
|
This function may be activated through calling :func:`mdevaluate.autosave.enable`, which takes a directory as input.
|
||||||
|
If this directory is a relative path (e.g. no trailing slash) the results will be saved in a location
|
||||||
|
relative to the directory of the trajectory file.
|
||||||
|
If the output files of your simulations are located in a subdirectory, like ``/path/to/sim/Output`` it is possible
|
||||||
|
to specify the auto save location as ``../data`` such that the result files will be placed under ``/path/to/sim/data``.
|
||||||
|
|
||||||
|
At the moment the two functions which use this behavior are:
|
||||||
|
|
||||||
|
- :func:`~mdevaluate.correlation.shifted_correlation`
|
||||||
|
- :func:`~mdevaluate.distribution.time_average`
|
||||||
|
|
||||||
|
Any other function can make use of the autosave mechanism by decorating it with :func:`mdevaluate.autosave.autosave_data`.
|
||||||
|
|
||||||
|
A full example
|
||||||
|
--------------
|
||||||
|
|
||||||
|
This is how it works, for a detailed explanation see below::
|
||||||
|
|
||||||
|
import mdevaluate as md
|
||||||
|
md.autosave.enable('data')
|
||||||
|
water = md.open('/path/to/sim').subset(atom_name='OW')
|
||||||
|
md.correlation.shifted_correlation(
|
||||||
|
md.correlation.msd,
|
||||||
|
water,
|
||||||
|
description='test'
|
||||||
|
)
|
||||||
|
# The result will be saved to the file:
|
||||||
|
# /path/to/sim/data/shifted_correlation_msd_OW_test.npz
|
||||||
|
|
||||||
|
Checksum of the Analysis Call
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
The autosave module calculates a checksum for each call of an analysis function,
|
||||||
|
which is used to validate a present the data file.
|
||||||
|
This way the result should only be loaded from file if the analysis is exactly the same.
|
||||||
|
This includes the function code that is evaluated, so the result will be recomputed if any bit of the code changes.
|
||||||
|
But there is always the possibility that checksums coincide accidentally,
|
||||||
|
by chance or due to a bug in the code, which should be kept in mind when using this functionality.
|
||||||
|
|
||||||
|
Special Keyword Arguments
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
The autosave module introduces two special keyword arguments to the decorated functions:
|
||||||
|
|
||||||
|
- ``autoload``: This prevents the loading of previously calculated data even if a valid file was found.
|
||||||
|
- ``description``: A descriptive string of the specific analysis, see below.
|
||||||
|
|
||||||
|
Those keywords may be passed to those function (shifted_correlation, time_average) like any other keyword argument.
|
||||||
|
If autosave was not enabled, they will be ignored.
|
||||||
|
|
||||||
|
File names and Analysis Descriptions
|
||||||
|
------------------------------------
|
||||||
|
|
||||||
|
The evaluated data is saved to human readable files, whose name is derived from the function call
|
||||||
|
and the automatic description of the subset.
|
||||||
|
The latter one is assigned based on the ``atom_name`` and ``residue_name`` of the :func:`~mdevaluate.atoms.AtomSubset.subset` method.
|
||||||
|
|
||||||
|
In some cases this is not enough, for example if the same subset is analyzed spatially resolved,
|
||||||
|
which would lead to identical filenames that would be overwritten.
|
||||||
|
Therefore a more detailed description of each specific analysis call needs to be provided.
|
||||||
|
For this reason the autosave module introduces the before mentioned keyword argument ``description``.
|
||||||
|
The value of this keyword is appended to the filename and in addition if any of
|
||||||
|
the other arguments of the function call has a attribute description, this will appended as well.
|
||||||
|
For example this (pseudo) code will lead to the filename ``shifted_correlation_isf_OW_1-2nm_nice.npz``::
|
||||||
|
|
||||||
|
OW = traj.subset(atom_name='OW')
|
||||||
|
|
||||||
|
corr = subensemble_correlation(spatial_selector)
|
||||||
|
corr.description = '1-2nm'
|
||||||
|
|
||||||
|
shifted_correlation(
|
||||||
|
isf,
|
||||||
|
OW,
|
||||||
|
correlation=corr,
|
||||||
|
description='nice'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Reusing the autosaved data
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
The results of the functions are saved in NumPy's npz format, see :func:`numpy.savez`.
|
||||||
|
If the result should be used in a different place, it can either be loaded with
|
||||||
|
:func:`numpy.load` or :func:`mdevaluate.autosave.load_data`.
|
||||||
|
The latter function will return the result of the function call directly, the former
|
||||||
|
returns a dict with the keys ``checksum`` and ``data``, the latter yielding the results data.
|
18
doc/special/energies.rst
Normal file
18
doc/special/energies.rst
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
Gromacs Energy Files
|
||||||
|
====================
|
||||||
|
|
||||||
|
It is possible to read the energy files (.edr) written out by Gromacs with mdevaluate.
|
||||||
|
Those files contain thermodynamic properties of the system, like temperature or pressure.
|
||||||
|
The exact contents of an energy file depend on the type of ensemble that was simulated,
|
||||||
|
an NVT simulation's energy file for example will not contain information about the box size.
|
||||||
|
|
||||||
|
To open these files use the function :func:`mdevaluate.open_energy`, which takes the filename of an energy file.
|
||||||
|
The types of energies stored in the file can be shown with the :attr:`types` attribute of the class :class:`~mdevaluate.reader.EnergyReader`,
|
||||||
|
the :attr:`units` attribute gives the units of these energy types.
|
||||||
|
The timesteps at which those energies were written out are accessible through the :attr:`~mdevaluate.reader.EnergyReader.time` property.
|
||||||
|
The time series of one of these energies can be accessed through the named index, comparable to python dictionaries.
|
||||||
|
::
|
||||||
|
import mdevaluate as md
|
||||||
|
edr = md.open_energy('/path/to/energy.edr')
|
||||||
|
# plot the evolution of temperature
|
||||||
|
plot(edr.time, edr['Temperature'])
|
76
doc/special/overlap.rst
Normal file
76
doc/special/overlap.rst
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
Computing the Overlap Function
|
||||||
|
==============================
|
||||||
|
|
||||||
|
The overlap function is defined as the portion of particles of a given set,
|
||||||
|
whose positions *overlap* after a given time :math:`t` with the reference configuration at :math:`t=0`.
|
||||||
|
This is calculated as follows:
|
||||||
|
The Initial positions define spheres of a given radius :math:`r` which then are used
|
||||||
|
to test how many of the particles at a later time are found within those spheres.
|
||||||
|
Normalized by the number of spheres this gives the correlation of the configurational overlap.
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
|
||||||
|
Q(t) = \frac{1}{N} \left\langle \sum\limits_{i=1}^N n_i(t) \right\rangle
|
||||||
|
|
||||||
|
Where :math:`n_i(t)` defines the :math:`N` spheres, with :math:`n_i(t)=1` if a particle
|
||||||
|
is found within this sphere at time :math:`t` and :math:`n_i(0) = 1` for :math:`1\leq i \leq N`.
|
||||||
|
|
||||||
|
Evaluation with mdevaluate
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Computation of the overlap requires the relatively expensive computation of next neighbor distances,
|
||||||
|
which scales with the order of :math:`\mathcal{O}(N^2)`.
|
||||||
|
There are more efficient ways for the solution of this problem, the one used here is
|
||||||
|
the so called :class:`~scipy.spatial.cKDTree`.
|
||||||
|
This is much more efficient and allows to compute the overlap relatively fast::
|
||||||
|
|
||||||
|
OW = md.open('/path/to/sim').subset(atom_name='OW')
|
||||||
|
tree = md.coordinates.CoordinatesKDTree(OW)
|
||||||
|
Qol = md.correlation.shifted_correlation(
|
||||||
|
partial(md.correlation.overlap, crds_tree=tree, radius=0.11),
|
||||||
|
OW
|
||||||
|
)
|
||||||
|
|
||||||
|
As seen above, mdevaluate provides the function :func:`~mdevaluate.correlation.overlap`
|
||||||
|
for this evaluation, which uses a special object of type :class:`~mdevaluate.coordinates.CoordinatesKDTree`
|
||||||
|
for the neighbor search.
|
||||||
|
The latter provides two features, necessary for the computation:
|
||||||
|
First it computes a :class:`~scipy.spatial.cKDTree` for each necessary frame of the trajectory;
|
||||||
|
second it caches those trees, since assembly of KDTrees is expensive.
|
||||||
|
The size of the cache can be controlled with the keyword argument ``maxsize`` of the CoordinatesKDTree initialization.
|
||||||
|
|
||||||
|
Note that this class uses the C version (hence the lowercase C) rather than
|
||||||
|
the pure Python version :class:`~scipy.spatial.KDTree` since the latter is significantly slower.
|
||||||
|
The only downside is, that the C version had a memory leak before SciPy 0.17,
|
||||||
|
but as long as a recent version of SciPy is used, this shouldn't be a problem.
|
||||||
|
|
||||||
|
Overlap of a Subsystem
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
In many cases the overlap of a subsystem, e.g. a spatial region, should be computed.
|
||||||
|
This is done by selecting a subset of the initial configuration before defining the spheres.
|
||||||
|
The overlap is then probed with the whole system.
|
||||||
|
This has two benefits:
|
||||||
|
|
||||||
|
1. It yields the correct results
|
||||||
|
2. The KDTree structures are smaller and thereby less computation and memory expensive
|
||||||
|
|
||||||
|
An example of a spatial resolved analysis, where ``OW`` is loaded as above::
|
||||||
|
|
||||||
|
selector = partial(
|
||||||
|
md.coordinates.spatial_selector,
|
||||||
|
transform=md.coordinates.spherical_radius,
|
||||||
|
rmin=1.0,
|
||||||
|
rmax=1.5
|
||||||
|
)
|
||||||
|
tree = md.coordinates.CoordinatesKDTree(OW, selector=selector)
|
||||||
|
Qol = md.correlation.shifted_correlation(
|
||||||
|
partial(md.correlation.overlap, crds_tree=tree, radius=0.11),
|
||||||
|
OW
|
||||||
|
)
|
||||||
|
|
||||||
|
This computes the overlap of OW atoms in the region :math:`1.0 \leq r \leq 1.5`.
|
||||||
|
This method can of course be used to probe the overlap of any subsystem, which is selected by the given selector function.
|
||||||
|
It should return a viable index for a (m, 3) sized NumPy array when called with original frame of size (N, 3)::
|
||||||
|
|
||||||
|
subset = frame[selector(frame)]
|
38
doc/special/spatial.rst
Normal file
38
doc/special/spatial.rst
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
Spatial Resolved Analysis
|
||||||
|
=========================
|
||||||
|
|
||||||
|
This section describes how spatially resolved correlation can be analyzed with mdevaluate.
|
||||||
|
This guide assumes that the variable ``traj`` holds a trajectory where the subset of atoms that should be analyzed are selected.
|
||||||
|
For example::
|
||||||
|
|
||||||
|
traj = md.open('/path/to/sim', cached=1000).subset(atom_name='OW')
|
||||||
|
|
||||||
|
Which would load a simulation from the directory ``/path/to/sim`` and select all ``OW`` atoms.
|
||||||
|
Note that for this use case, the caching is quite useful since it enables us to iterate over spatial regions
|
||||||
|
without significant time penalty.
|
||||||
|
Moving on let's calculate the ISF of water oxygens with spherical radius between 0.5 and 0.7 nm::
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
func = partial(md.correlation.isf, q=22.7)
|
||||||
|
selector = partial(
|
||||||
|
md.coordinates.spatial_selector,
|
||||||
|
transform=md.coordinates.spherical_radius,
|
||||||
|
rmin=0.5, rmax=0.7
|
||||||
|
)
|
||||||
|
t, S = md.correlation.shifted_correlation(
|
||||||
|
func, traj,
|
||||||
|
correlation=md.correlation.subensemble_correlation(selector)
|
||||||
|
)
|
||||||
|
|
||||||
|
To explain how this works, let's go through the code from bottom to top.
|
||||||
|
The spatial filtering is done inside the shifted_correlation by the function
|
||||||
|
:func:`mdevaluate.correlation.subensemble_correlation`.
|
||||||
|
This function takes a selector function as argument that should take a frame as input
|
||||||
|
and return the selection of the coordinates that should be selected.
|
||||||
|
A new selection is taken for the starting frame of each shifted time segment.
|
||||||
|
|
||||||
|
In this case the selection is done with the function :func:`mdevaluate.coordinates.spatial_selector`.
|
||||||
|
This function takes four arguments, the first being the frame of coordinates which is handed by :func:`subensemble_correlation`.
|
||||||
|
The second argument is a transformation function, which transforms the input coordinates to the coordinate which will be filtered,
|
||||||
|
in this case the spherical radius.
|
||||||
|
The two last arguments define the minimum and maximum value of this quantity.
|
76
doc/static-evaluation.rst
Normal file
76
doc/static-evaluation.rst
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
|
||||||
|
Evaluation of static properties
|
||||||
|
===============================
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
All examples in this section assume, that the packages has been imported and a trajectory was loaded::
|
||||||
|
|
||||||
|
import mdevaluate.distribution as dist
|
||||||
|
|
||||||
|
coords = mdevaluate.open('/path/to/simulation')
|
||||||
|
|
||||||
|
Static properties of the system, like density distribution or pair correlation function,
|
||||||
|
can be evaluated with the :mod:`mdevaluate.distribution` module.
|
||||||
|
It provides the function :func:`mdevaluate.distribution.time_average`
|
||||||
|
that computes the average of a property over the whole trajectory.
|
||||||
|
An example call of this function is::
|
||||||
|
|
||||||
|
tetra = dist.time_average(dist.tetrahedral_order, coords)
|
||||||
|
|
||||||
|
This will calculate the average of the tetrahedral order parameter for each atom.
|
||||||
|
The first argument of :func:`time_average` is a function that takes one argument.
|
||||||
|
It will be called for each frame in the trajectory and the output of this function
|
||||||
|
is than averaged over all these frames.
|
||||||
|
|
||||||
|
Slicing of the trajectory
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
In most cases averaging each frame of the trajectory is not necessary,
|
||||||
|
since the conformation of the atoms doesn't change significantly between two frames.
|
||||||
|
Hence it is sufficient to skip some frames without suffering significant statistics.
|
||||||
|
The exact amount of frames which can be skipped before the statistics suffer depends strongly
|
||||||
|
on the calculated property, therefore it has to be chosen manually.
|
||||||
|
For this purpose the Coordinates objects can be sliced like any python list::
|
||||||
|
|
||||||
|
tetra = dist.time_average(dist.tetrahedral_order, coords[1000::50])
|
||||||
|
|
||||||
|
This makes it possible to skip a number of frames at the start (or end) and with every step.
|
||||||
|
The above call would start with frame 1000 of the trajectory and evaluate each 50th frame until the end.
|
||||||
|
Since the number of frames read and evaluated is reduced by about a factor of 50, the computational cost will decrease accordingly.
|
||||||
|
|
||||||
|
Calculating distributions
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
In many cases the static distributions of a property is of interest.
|
||||||
|
For example, the tetrahedral order parameter is often wanted as a distribution.
|
||||||
|
This can too be calculated with ``time_average`` but the bins of the distribution have to be specified::
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
func = partial(dist.tetrahedral_order_distribution, bins=np.linspace(-3, 1, 401)
|
||||||
|
tetra_dist = dist.time_average(func, coords)
|
||||||
|
|
||||||
|
The bins (which are ultimately used with the function :func:`numpy.histogram`) are specified
|
||||||
|
by partially evaluating the evaluation function with :func:`functools.partial`.
|
||||||
|
See the documentation of :func:`numpy.histogram` for details on bin specification.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
If :func:`numpy.histogram` is used with :func:`time_average` the bins have to be given explicitly.
|
||||||
|
When not specified, the bins will be chosen automatically for each call of ``histogram`` leading to
|
||||||
|
different bins for each frame, hence an incorrect average.
|
||||||
|
|
||||||
|
Advanced evaluations
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
The function that will be evaluated by ``time_average`` can return numpy arrays of arbitrary shape.
|
||||||
|
It is for example possible to calculate the distribution of a property for several subsets of the system at once::
|
||||||
|
|
||||||
|
def subset_tetra(frame, bins):
|
||||||
|
tetra = dist.tetrahedral_order(frame)
|
||||||
|
return array([np.histogram(tetra[0::2], bins=bins),
|
||||||
|
np.histogram(tetra[1::2], bins=bins),])
|
||||||
|
|
||||||
|
func = partial(subset, bins=np.linspace(-1,1,201))
|
||||||
|
tetra_subdist = dist.time_average(func, coords)
|
||||||
|
|
||||||
|
In this example the tetrahedral order parameter is first calculated for each atom of the system.
|
||||||
|
Then the distribution is calculated for two subsets, containing atoms (0, 2, 4, 6, ...) and (1, 3, 5, 7, ...).
|
2
examples/README.txt
Normal file
2
examples/README.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Example Gallery
|
||||||
|
===============
|
47
examples/plot_chi4.py
Normal file
47
examples/plot_chi4.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
r"""
|
||||||
|
Four-Point susceptibility
|
||||||
|
=========================
|
||||||
|
|
||||||
|
The dynamic four-point susceptibility :math:`\chi_4(t)` is a measure for heterogenous dynamics. [Berthier]_
|
||||||
|
It can be calculated from the variance of the incoherent intermediate scattering function
|
||||||
|
:math:`F_q(t)`.
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
\chi_4 (t) = N\cdot\left( \left\langle F_q^2(t) \right\rangle - \left\langle F_q(t) \right\rangle^2 \right)
|
||||||
|
|
||||||
|
This is astraight forward calculation in mdevaluate.
|
||||||
|
First calculate the ISF without time average and then take the variance along the first axis of this data.
|
||||||
|
Note that this quantity requires good statistics, hence it is adviced to use a small time window
|
||||||
|
and a sufficient number of segments for the analysis.
|
||||||
|
Another way to reduce scatter is to smooth the data with a running mean,
|
||||||
|
calling :func:`~mdevaluate.utils.runningmean` as shown below.
|
||||||
|
|
||||||
|
.. [Berthier] http://link.aps.org/doi/10.1103/Physics.4.42
|
||||||
|
"""
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import mdevaluate as md
|
||||||
|
import tudplot
|
||||||
|
|
||||||
|
OW = md.open('/data/niels/sim/water/bulk/260K', trajectory='out/*.xtc').subset(atom_name='OW')
|
||||||
|
|
||||||
|
t, Fqt = md.correlation.shifted_correlation(
|
||||||
|
partial(md.correlation.isf, q=22.7),
|
||||||
|
OW,
|
||||||
|
average=False,
|
||||||
|
window=0.2,
|
||||||
|
skip=0.1,
|
||||||
|
segments=20
|
||||||
|
)
|
||||||
|
chi4 = len(OW[0]) * Fqt.var(axis=0)
|
||||||
|
|
||||||
|
tudplot.activate()
|
||||||
|
|
||||||
|
plt.plot(t, chi4, 'h', label=r'$\chi_4$')
|
||||||
|
plt.plot(t[2:-2], md.utils.runningmean(chi4, 5), '-', label='smoothed')
|
||||||
|
|
||||||
|
plt.semilogx()
|
||||||
|
plt.xlabel('time / ps')
|
||||||
|
plt.ylabel('$\\chi_4$')
|
||||||
|
plt.legend(loc='best')
|
30
examples/plot_isf.py
Normal file
30
examples/plot_isf.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"""
|
||||||
|
Calculating the ISF of Water
|
||||||
|
=======================================================
|
||||||
|
|
||||||
|
In this example the ISF of water oxygens is calculated for a bulk simulation.
|
||||||
|
Additionally a KWW function is fitted to the results.
|
||||||
|
"""
|
||||||
|
from functools import partial
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from scipy.optimize import curve_fit
|
||||||
|
import mdevaluate as md
|
||||||
|
import tudplot
|
||||||
|
|
||||||
|
OW = md.open('/data/niels/sim/water/bulk/260K', trajectory='out/*.xtc').subset(atom_name='OW')
|
||||||
|
t, S = md.correlation.shifted_correlation(
|
||||||
|
partial(md.correlation.isf, q=22.7),
|
||||||
|
OW,
|
||||||
|
average=True
|
||||||
|
)
|
||||||
|
# Only include data-points of the alpha-relaxation for the fit
|
||||||
|
mask = t > 3e-1
|
||||||
|
fit, cov = curve_fit(md.functions.kww, t[mask], S[mask])
|
||||||
|
tau = md.functions.kww_1e(*fit)
|
||||||
|
|
||||||
|
tudplot.activate()
|
||||||
|
plt.figure()
|
||||||
|
plt.plot(t, S, '.', label='ISF of Bulk Water')
|
||||||
|
plt.plot(t, md.functions.kww(t, *fit), '-', label=r'KWW, $\tau$={:.2f}ps'.format(tau))
|
||||||
|
plt.xscale('log')
|
||||||
|
plt.legend()
|
121
examples/plot_spatialisf.py
Normal file
121
examples/plot_spatialisf.py
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
"""
|
||||||
|
Spatially resolved analysis in a cylindrical pore
|
||||||
|
=======================================================
|
||||||
|
|
||||||
|
Calculate the spatially resolved ISF inside a cylindrical neutral water pore
|
||||||
|
In this case the bins describe the shortest distance of an oxygen atom to any wall atom
|
||||||
|
"""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import mdevaluate as md
|
||||||
|
import tudplot
|
||||||
|
from scipy import spatial
|
||||||
|
from scipy.optimize import curve_fit
|
||||||
|
|
||||||
|
#trajectory with index file
|
||||||
|
#TODO eine allgemeinere stelle?
|
||||||
|
traj = md.open('/data/robin/sim/nvt/12kwater/240_r25_0_NVT',
|
||||||
|
trajectory='nojump.xtc', index_file='indexSL.ndx',topology='*.gro')
|
||||||
|
#Liquid oxygens
|
||||||
|
LO = traj.subset(indices= traj.atoms.indices['LH2O'])
|
||||||
|
#Solid oxygens
|
||||||
|
SO = traj.subset(indices= traj.atoms.indices['SH2O'])
|
||||||
|
#Solid oxygens and bonded hydrogens
|
||||||
|
SW = traj.subset(residue_id = SO.atom_subset.residue_ids)
|
||||||
|
|
||||||
|
#TODO die folgenden beiden zusammen sind nochmal deutlich schneller als
|
||||||
|
#md.atom.distance_to_atoms, kannst du entweder in irgendeiner weise einbauen
|
||||||
|
#oder hier lassen, man muss aber auf thickness achten, dass das sinn macht
|
||||||
|
#adds periodic layers of the atoms
|
||||||
|
def pbc_points(points, box_vector, thickness=0, index=False, inclusive=True):
|
||||||
|
coordinates = np.copy(points)%box_vector
|
||||||
|
allcoordinates = np.copy(coordinates)
|
||||||
|
indices = np.tile(np.arange(len(points)),(27))
|
||||||
|
for x in range(-1, 2, 1):
|
||||||
|
for y in range(-1, 2, 1):
|
||||||
|
for z in range(-1, 2, 1):
|
||||||
|
vv = np.array([x, y, z], dtype=float)
|
||||||
|
if not (vv == 0).all() :
|
||||||
|
allcoordinates = np.concatenate((allcoordinates, coordinates + vv*box_vector), axis=0)
|
||||||
|
|
||||||
|
if thickness != 0:
|
||||||
|
mask = np.all(allcoordinates < box_vector+thickness, axis=1)
|
||||||
|
allcoordinates = allcoordinates[mask]
|
||||||
|
indices = indices[mask]
|
||||||
|
mask = np.all(allcoordinates > -thickness, axis=1)
|
||||||
|
allcoordinates = allcoordinates[mask]
|
||||||
|
indices = indices[mask]
|
||||||
|
if not inclusive:
|
||||||
|
allcoordinates = allcoordinates[len(points):]
|
||||||
|
indices = indices[len(points):]
|
||||||
|
if index:
|
||||||
|
return (allcoordinates, indices)
|
||||||
|
return allcoordinates
|
||||||
|
|
||||||
|
#fast calculation of shortest distance from one subset to another, uses pbc_points
|
||||||
|
def distance_to_atoms(ref, observed_atoms, box=None, thickness=0.5):
|
||||||
|
if box is not None:
|
||||||
|
start_coords = np.copy(observed_atoms)%box
|
||||||
|
all_frame_coords = pbc_points(ref, box, thickness = thickness)
|
||||||
|
else:
|
||||||
|
start_coords = np.copy(observed_atoms)
|
||||||
|
all_frame_coords = np.copy(ref)
|
||||||
|
|
||||||
|
tree = spatial.cKDTree(all_frame_coords)
|
||||||
|
first_neighbors = tree.query(start_coords)[0]
|
||||||
|
return first_neighbors
|
||||||
|
|
||||||
|
#this is used to reduce the number of wall atoms to those relevant, speeds up the rest
|
||||||
|
dist = distance_to_atoms(LO[0], SW[0], np.diag(LO[0].box))
|
||||||
|
wall_atoms = SW.atom_subset.indices[0]
|
||||||
|
wall_atoms = wall_atoms[dist < 0.35]
|
||||||
|
SW = traj.subset(indices = wall_atoms)
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
func = partial(md.correlation.isf, q=22.7)
|
||||||
|
|
||||||
|
#selector function to choose liquid oxygens with a certain distance to wall atoms
|
||||||
|
def selector_func(coords, lindices, windices, dmin, dmax):
|
||||||
|
lcoords = coords[lindices]
|
||||||
|
wcoords = coords[windices]
|
||||||
|
dist = distance_to_atoms(wcoords, lcoords,box=np.diag(coords.box))
|
||||||
|
#radial distance to pore center to ignore molecules that entered the wall
|
||||||
|
rad = np.sum((lcoords[:,:2]-np.diag(coords.box)[:2]/2)**2,axis=1)**.5
|
||||||
|
return lindices[(dist >= dmin) & (dist < dmax) & (rad < 2.7)]
|
||||||
|
|
||||||
|
#calculate the shifted correlation for several bins
|
||||||
|
#bin positions are roughly the average of the limits
|
||||||
|
bins = np.array([0.15,0.2,0.3,0.4,0.5,0.8,1.0,1.4,1.8,2.3])
|
||||||
|
binpos = (bins[1:]+bins[:-1])/2
|
||||||
|
S = np.empty(len(bins)-1, dtype='object')
|
||||||
|
for i in range(len(bins)-1):
|
||||||
|
selector = partial(selector_func,lindices=LO.atom_subset.indices[0],
|
||||||
|
windices=SW.atom_subset.indices[0],dmin=bins[i],
|
||||||
|
dmax = bins[i+1])
|
||||||
|
t, S[i] = md.correlation.shifted_correlation(
|
||||||
|
func, traj,segments=50, skip=0.1,average=True,
|
||||||
|
correlation=md.correlation.subensemble_correlation(selector),
|
||||||
|
description=str(bins[i])+','+str(bins[i+1]))
|
||||||
|
|
||||||
|
taus = np.zeros(len(S))
|
||||||
|
tudplot.activate()
|
||||||
|
plt.figure()
|
||||||
|
for i,s in enumerate(S):
|
||||||
|
pl = plt.plot(t, s, '.', label='d = ' + str(binpos[i]) + ' nm')
|
||||||
|
#only includes the relevant data for 1/e fitting
|
||||||
|
mask = s < 0.6
|
||||||
|
fit, cov = curve_fit(md.functions.kww, t[mask], s[mask],
|
||||||
|
p0=[1.0,t[t>1/np.e][-1],0.5])
|
||||||
|
taus[i] = md.functions.kww_1e(*fit)
|
||||||
|
plt.plot(t, md.functions.kww(t, *fit), c=pl[0].get_color())
|
||||||
|
plt.xscale('log')
|
||||||
|
plt.legend()
|
||||||
|
#plt.show()
|
||||||
|
|
||||||
|
tudplot.activate()
|
||||||
|
plt.figure()
|
||||||
|
plt.plot(binpos, taus,'.',label=r'$\tau$(d)')
|
||||||
|
plt.yscale('log')
|
||||||
|
plt.legend()
|
||||||
|
#plt.show()
|
17
examples/plot_temperature.py
Normal file
17
examples/plot_temperature.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
"""
|
||||||
|
Plotting the Temperature from an Energy File
|
||||||
|
============================================
|
||||||
|
|
||||||
|
This example reads an Gromacs energy file and plots the evolultion and mean of the temperature.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
import mdevaluate as md
|
||||||
|
import tudplot
|
||||||
|
|
||||||
|
tudplot.activate()
|
||||||
|
|
||||||
|
edr = md.open_energy('/data/niels/sim/water/bulk/300K/out/energy_water1000bulk300.edr')
|
||||||
|
T = edr['Temperature']
|
||||||
|
plt.plot(edr.time, T)
|
||||||
|
plt.plot(edr.time[[0, -1]], [T.mean(), T.mean()])
|
75
mdevaluate/__init__.py
Normal file
75
mdevaluate/__init__.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import os
|
||||||
|
from glob import glob
|
||||||
|
|
||||||
|
from . import atoms
|
||||||
|
from . import coordinates
|
||||||
|
from . import correlation
|
||||||
|
from . import distribution
|
||||||
|
from . import functions
|
||||||
|
from . import pbc
|
||||||
|
from . import autosave
|
||||||
|
from . import reader
|
||||||
|
from .logging import logger
|
||||||
|
|
||||||
|
__version__ = '2022.1.dev1'
|
||||||
|
|
||||||
|
|
||||||
|
def open(directory='', topology='*.tpr', trajectory='*.xtc', cached=False,
|
||||||
|
nojump=False):
|
||||||
|
"""
|
||||||
|
Open a simulation from a directory.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
directory: Directory of the simulation.
|
||||||
|
topology (opt.):
|
||||||
|
Descriptor of the topology file (tpr or gro). By default a tpr file is
|
||||||
|
used, if there is exactly one in the directoy.
|
||||||
|
trajectory (opt.): Descriptor of the trajectory (xtc file).
|
||||||
|
cached (opt.):
|
||||||
|
If the trajectory reader should be cached. Can be True, an integer or None.
|
||||||
|
If this is True maxsize is 128, otherwise this is used as maxsize for
|
||||||
|
the cache, None means infinite cache (this is a potential memory leak!).
|
||||||
|
nojump (opt.): If nojump matrixes should be generated. They will alwyas be loaded if present
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A Coordinate object of the simulation.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
Open a simulation located in '/path/to/sim', where the trajectory is
|
||||||
|
located in a sub-directory '/path/to/sim/out' and named for Example
|
||||||
|
'nojump_traj.xtc'. All read frames will be cached in memory.
|
||||||
|
|
||||||
|
>>> open('/path/to/sim', trajectory='out/nojump*.xtc', cached=None)
|
||||||
|
|
||||||
|
The file descriptors can use unix style pathname expansion to define the filenames.
|
||||||
|
The default patterns use the recursive placeholder `**` which matches the base or
|
||||||
|
any subdirctory, thus files in subdirectories with matching file type will be found too.
|
||||||
|
For example: 'out/nojump*.xtc' would match xtc files in a subdirectory `out` that
|
||||||
|
start with `nojump` and end with `.xtc`.
|
||||||
|
|
||||||
|
For more details see: https://docs.python.org/3/library/glob.html
|
||||||
|
"""
|
||||||
|
top_glob = glob(os.path.join(directory, topology), recursive=True)
|
||||||
|
if top_glob is not None and len(top_glob) == 1:
|
||||||
|
top_file, = top_glob
|
||||||
|
logger.info('Loading topology: {}'.format(top_file))
|
||||||
|
else:
|
||||||
|
raise FileNotFoundError('Topology file could not be identified.')
|
||||||
|
|
||||||
|
traj_glob = glob(os.path.join(directory, trajectory), recursive=True)
|
||||||
|
if traj_glob is not None and len(traj_glob) == 1:
|
||||||
|
traj_file = traj_glob[0]
|
||||||
|
logger.info('Loading trajectory: {}'.format(traj_file))
|
||||||
|
else:
|
||||||
|
raise FileNotFoundError('Trajectory file could not be identified.')
|
||||||
|
|
||||||
|
atom_set, frames = reader.open_with_mdanalysis(
|
||||||
|
top_file, traj_file, cached=cached
|
||||||
|
)
|
||||||
|
coords = coordinates.Coordinates(frames, atom_subset=atom_set)
|
||||||
|
if nojump:
|
||||||
|
try:
|
||||||
|
frames.nojump_matrixes
|
||||||
|
except reader.NojumpError:
|
||||||
|
reader.generate_nojump_matrixes(coords)
|
||||||
|
return coords
|
270
mdevaluate/atoms.py
Normal file
270
mdevaluate/atoms.py
Normal file
@ -0,0 +1,270 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from scipy.spatial.distance import cdist
|
||||||
|
from .pbc import pbc_diff
|
||||||
|
from .checksum import checksum
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
import scipy
|
||||||
|
if scipy.version.version >= '0.17.0':
|
||||||
|
from scipy.spatial import cKDTree as KDTree
|
||||||
|
else:
|
||||||
|
from scipy.spatial import KDTree
|
||||||
|
|
||||||
|
def compare_regex(list, exp):
|
||||||
|
"""
|
||||||
|
Compare a list of strings with a regular expression.
|
||||||
|
"""
|
||||||
|
if not exp.endswith('$'):
|
||||||
|
exp += '$'
|
||||||
|
regex = re.compile(exp)
|
||||||
|
return np.array([regex.match(s) is not None for s in list])
|
||||||
|
|
||||||
|
class Atoms:
|
||||||
|
"""
|
||||||
|
Basic container class for atom information.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
atoms: N tuples of residue id, residue name and atom name.
|
||||||
|
indices (optional): Dictionary of named atom index groups.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
residue_ids: Indices of the atoms residues
|
||||||
|
residue_names: Names of the atoms residues
|
||||||
|
atom_names: Names of the atoms
|
||||||
|
indices: Dictionary of named atom index groups, if specified
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, atoms, indices=None, masses=None, charges=None, reader=None):
|
||||||
|
self.residue_ids, self.residue_names, self.atom_names = atoms.T
|
||||||
|
self.residue_ids = np.array([int(m) for m in self.residue_ids])
|
||||||
|
self.indices = indices
|
||||||
|
self.masses = masses
|
||||||
|
self.charges = charges
|
||||||
|
self.reader = reader
|
||||||
|
|
||||||
|
def subset(self, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Return a subset of these atoms with all atoms selected.
|
||||||
|
|
||||||
|
All arguments are passed to the :meth:`AtomSubset.subset` method directly.
|
||||||
|
|
||||||
|
"""
|
||||||
|
return AtomSubset(self).subset(*args, **kwargs)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.atom_names)
|
||||||
|
|
||||||
|
|
||||||
|
class AtomMismatch(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class AtomSubset:
|
||||||
|
|
||||||
|
def __init__(self, atoms, selection=None, description=''):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
atoms: Base atom object
|
||||||
|
selection (opt.): Selected atoms
|
||||||
|
description (opt.): Descriptive string of the subset.
|
||||||
|
"""
|
||||||
|
if selection is None:
|
||||||
|
selection = np.ones(len(atoms), dtype='bool')
|
||||||
|
self.selection = selection
|
||||||
|
self.atoms = atoms
|
||||||
|
self.description = description
|
||||||
|
|
||||||
|
def subset(self, atom_name=None, residue_name=None, residue_id=None, indices=None):
|
||||||
|
"""
|
||||||
|
Return a subset of the system. The selection is specified by one or more of
|
||||||
|
the keyworss below. Names are matched as a regular expression with `re.match`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
atom_name: Specification of the atom name
|
||||||
|
residue_name: Specification of the resiude name
|
||||||
|
residue_id: Residue ID or list of IDs
|
||||||
|
indices: List of atom indices
|
||||||
|
"""
|
||||||
|
new_subset = self
|
||||||
|
if atom_name is not None:
|
||||||
|
new_subset &= AtomSubset(
|
||||||
|
self.atoms,
|
||||||
|
selection=compare_regex(self.atoms.atom_names, atom_name),
|
||||||
|
description=atom_name
|
||||||
|
)
|
||||||
|
|
||||||
|
if residue_name is not None:
|
||||||
|
new_subset &= AtomSubset(
|
||||||
|
self.atoms,
|
||||||
|
selection=compare_regex(self.atoms.residue_names, residue_name),
|
||||||
|
description=residue_name
|
||||||
|
)
|
||||||
|
|
||||||
|
if residue_id is not None:
|
||||||
|
if np.iterable(residue_id):
|
||||||
|
selection = np.zeros(len(self.selection), dtype='bool')
|
||||||
|
selection[np.in1d(self.atoms.residue_ids, residue_id)] = True
|
||||||
|
new_subset &= AtomSubset(self.atoms, selection)
|
||||||
|
else:
|
||||||
|
new_subset &= AtomSubset(self.atoms, self.atoms.residue_ids == residue_id)
|
||||||
|
|
||||||
|
if indices is not None:
|
||||||
|
selection = np.zeros(len(self.selection), dtype='bool')
|
||||||
|
selection[indices] = True
|
||||||
|
new_subset &= AtomSubset(self.atoms, selection)
|
||||||
|
return new_subset
|
||||||
|
|
||||||
|
@property
|
||||||
|
def atom_names(self):
|
||||||
|
return self.atoms.atom_names[self.selection]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def residue_names(self):
|
||||||
|
return self.atoms.residue_names[self.selection]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def residue_ids(self):
|
||||||
|
return self.atoms.residue_ids[self.selection]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def indices(self):
|
||||||
|
return np.where(self.selection)
|
||||||
|
|
||||||
|
def __getitem__(self, slice):
|
||||||
|
if isinstance(slice, str):
|
||||||
|
indices = self.atoms.indices[slice]
|
||||||
|
return self.atoms.subset()[indices] & self
|
||||||
|
|
||||||
|
return self.subset(indices=self.indices[0].__getitem__(slice))
|
||||||
|
|
||||||
|
def __and__(self, other):
|
||||||
|
if self.atoms != other.atoms:
|
||||||
|
raise AtomMismatch
|
||||||
|
selection = (self.selection & other.selection)
|
||||||
|
description = '{}_{}'.format(self.description, other.description).strip('_')
|
||||||
|
return AtomSubset(self.atoms, selection, description)
|
||||||
|
|
||||||
|
def __or__(self, other):
|
||||||
|
if self.atoms != other.atoms:
|
||||||
|
raise AtomMismatch
|
||||||
|
selection = (self.selection | other.selection)
|
||||||
|
description = '{}_{}'.format(self.description, other.description).strip('_')
|
||||||
|
return AtomSubset(self.atoms, selection, description)
|
||||||
|
|
||||||
|
def __invert__(self):
|
||||||
|
selection = ~self.selection
|
||||||
|
return AtomSubset(self.atoms, selection, self.description)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return 'Subset of Atoms ({} of {})'.format(len(self.atoms.residue_names[self.selection]),
|
||||||
|
len(self.atoms))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def summary(self):
|
||||||
|
return "\n".join(["{}{} {}".format(resid, resname, atom_names)
|
||||||
|
for resid, resname, atom_names in zip(self.residue_ids, self.residue_names, self.atom_names)
|
||||||
|
])
|
||||||
|
|
||||||
|
def __checksum__(self):
|
||||||
|
return checksum(self.description)
|
||||||
|
|
||||||
|
|
||||||
|
def center_of_mass(position, mass=None):
|
||||||
|
if mass is not None:
|
||||||
|
return 1 / mass.sum() * (mass * position).sum(axis=0)
|
||||||
|
else:
|
||||||
|
return 1 / len(position) * position.sum(axis=0)
|
||||||
|
|
||||||
|
|
||||||
|
def gyration_radius(position):
|
||||||
|
r"""
|
||||||
|
Calculates a list of all radii of gyration of all molecules given in the coordinate frame,
|
||||||
|
weighted with the masses of the individual atoms.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
position: Coordinate frame object
|
||||||
|
|
||||||
|
..math::
|
||||||
|
R_G = \left(\frac{\sum_{i=1}^{n} m_i |\vec{r_i} - \vec{r_{COM}}|^2 }{\sum_{i=1}^{n} m_i }
|
||||||
|
\rigth)^{\frac{1}{2}}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
gyration_radii = np.array([])
|
||||||
|
|
||||||
|
for resid in np.unique(position.residue_ids):
|
||||||
|
pos = position.whole[position.residue_ids==resid]
|
||||||
|
mass = position.masses[position.residue_ids==resid][:,np.newaxis]
|
||||||
|
COM = center_of_mass(pos,mass)
|
||||||
|
r_sq = ((pbc_diff(pos,COM,pos.box.diagonal()))**2).sum(1)[:,np.newaxis]
|
||||||
|
g_radius = ((r_sq*mass).sum()/mass.sum())**(0.5)
|
||||||
|
|
||||||
|
gyration_radii = np.append(gyration_radii,g_radius)
|
||||||
|
|
||||||
|
return gyration_radii
|
||||||
|
|
||||||
|
|
||||||
|
def layer_of_atoms(atoms,
|
||||||
|
thickness,
|
||||||
|
plane_offset=np.array([0, 0, 0]),
|
||||||
|
plane_normal=np.array([1, 0, 0])):
|
||||||
|
|
||||||
|
p_ = atoms - plane_offset
|
||||||
|
distance = np.dot(p_, plane_normal)
|
||||||
|
|
||||||
|
return abs(distance) <= thickness
|
||||||
|
|
||||||
|
|
||||||
|
def distance_to_atoms(ref, atoms, box=None):
|
||||||
|
"""Get the minimal distance from atoms to ref.
|
||||||
|
The result is an array of with length == len(atoms)
|
||||||
|
"""
|
||||||
|
out = np.empty(atoms.shape[0])
|
||||||
|
for i, atom in enumerate(atoms):
|
||||||
|
diff = (pbc_diff(atom, ref, box) ** 2).sum(axis=1).min()
|
||||||
|
out[i] = np.sqrt(diff)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def distance_to_atoms_cKDtree(ref, atoms, box=None, thickness=None):
|
||||||
|
"""
|
||||||
|
Get the minimal distance from atoms to ref.
|
||||||
|
The result is an array of with length == len(atoms)
|
||||||
|
Can be faster than distance_to_atoms.
|
||||||
|
Thickness defaults to box/5. If this is too small results may be wrong.
|
||||||
|
If box is not given then periodic boundary conditions are not applied!
|
||||||
|
"""
|
||||||
|
if thickness == None:
|
||||||
|
thickness = box/5
|
||||||
|
if box is not None:
|
||||||
|
start_coords = np.copy(atoms)%box
|
||||||
|
all_frame_coords = pbc_points(ref, box, thickness = thickness)
|
||||||
|
else:
|
||||||
|
start_coords = atoms
|
||||||
|
all_frame_coords = ref
|
||||||
|
|
||||||
|
tree = spatial.cKDTree(all_frame_coords)
|
||||||
|
return tree.query(start_coords)[0]
|
||||||
|
|
||||||
|
|
||||||
|
def next_neighbors(atoms, query_atoms=None, number_of_neighbors=1, distance_upper_bound=np.inf, distinct=False):
|
||||||
|
"""
|
||||||
|
Find the N next neighbors of a set of atoms.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
atoms: The reference atoms and also the atoms which are queried if `query_atoms` is net provided
|
||||||
|
query_atoms (opt.): If this is not None, these atoms will be queried
|
||||||
|
number_of_neighbors (int, opt.): Number of neighboring atoms to find
|
||||||
|
distance_upper_bound (float, opt.): Upper bound of the distance between neighbors
|
||||||
|
distinct (bool, opt.): If this is true, the atoms and query atoms are taken as distinct sets of atoms
|
||||||
|
"""
|
||||||
|
tree = KDTree(atoms)
|
||||||
|
dnn = 0
|
||||||
|
if query_atoms is None:
|
||||||
|
query_atoms = atoms
|
||||||
|
elif not distinct:
|
||||||
|
dnn = 1
|
||||||
|
dist, indices = tree.query(query_atoms, number_of_neighbors + dnn,
|
||||||
|
distance_upper_bound=distance_upper_bound)
|
||||||
|
return indices[:, dnn:]
|
183
mdevaluate/autosave.py
Normal file
183
mdevaluate/autosave.py
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import functools
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
from .checksum import checksum
|
||||||
|
from .logging import logger
|
||||||
|
|
||||||
|
autosave_directory = None
|
||||||
|
load_autosave_data = False
|
||||||
|
verbose_print = True
|
||||||
|
user_autosave_directory = os.path.join(os.environ['HOME'], '.mdevaluate/autosave')
|
||||||
|
|
||||||
|
|
||||||
|
def notify(msg):
|
||||||
|
if verbose_print:
|
||||||
|
logger.info(msg)
|
||||||
|
else:
|
||||||
|
logger.debug(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def enable(dir, load_data=True, verbose=True):
|
||||||
|
"""
|
||||||
|
Enable auto saving results of functions decorated with :func:`autosave_data`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
dir: Directory where the data should be saved.
|
||||||
|
load_data (opt., bool): If data should also be loaded.
|
||||||
|
"""
|
||||||
|
global autosave_directory, load_autosave_data, verbose_print
|
||||||
|
verbose_print = verbose
|
||||||
|
# absolute = os.path.abspath(dir)
|
||||||
|
# os.makedirs(absolute, exist_ok=True)
|
||||||
|
autosave_directory = dir
|
||||||
|
load_autosave_data = load_data
|
||||||
|
notify('Enabled autosave in directory: {}'.format(autosave_directory))
|
||||||
|
|
||||||
|
|
||||||
|
def disable():
|
||||||
|
"""Disable autosave."""
|
||||||
|
global autosave_directory, load_autosave_data
|
||||||
|
autosave_directory = None
|
||||||
|
load_autosave_data = False
|
||||||
|
|
||||||
|
|
||||||
|
class disabled:
|
||||||
|
"""
|
||||||
|
A context manager that disbales the autosave module within its context.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
import mdevaluate as md
|
||||||
|
md.autosave.enable('data')
|
||||||
|
with md.autosave.disabled():
|
||||||
|
# Autosave functionality is disabled within this context.
|
||||||
|
md.correlation.shifted_correlation(
|
||||||
|
...
|
||||||
|
)
|
||||||
|
|
||||||
|
# After the context is exited, autosave will work as before.
|
||||||
|
"""
|
||||||
|
def __enter__(self):
|
||||||
|
self._autosave_directory = autosave_directory
|
||||||
|
disable()
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
enable(self._autosave_directory)
|
||||||
|
|
||||||
|
|
||||||
|
def get_directory(reader):
|
||||||
|
"""Get the autosave directory for a trajectory reader."""
|
||||||
|
outdir = os.path.dirname(reader.filename)
|
||||||
|
savedir = os.path.join(outdir, autosave_directory)
|
||||||
|
if not os.path.exists(savedir):
|
||||||
|
try:
|
||||||
|
os.makedirs(savedir)
|
||||||
|
except PermissionError:
|
||||||
|
pass
|
||||||
|
if not os.access(savedir, os.W_OK):
|
||||||
|
savedir = os.path.join(user_autosave_directory, savedir.lstrip('/'))
|
||||||
|
logger.info('Switched autosave directory to {}, since original location is not writeable.'.format(savedir))
|
||||||
|
os.makedirs(savedir, exist_ok=True)
|
||||||
|
return savedir
|
||||||
|
|
||||||
|
|
||||||
|
def get_filename(function, checksum, description, *args):
|
||||||
|
"""Get the autosave filename for a specific function call."""
|
||||||
|
func_desc = function.__name__
|
||||||
|
for arg in args:
|
||||||
|
if hasattr(arg, '__name__'):
|
||||||
|
func_desc += '_{}'.format(arg.__name__)
|
||||||
|
elif isinstance(arg, functools.partial):
|
||||||
|
func_desc += '_{}'.format(arg.func.__name__)
|
||||||
|
|
||||||
|
if hasattr(arg, 'frames'):
|
||||||
|
savedir = get_directory(arg.frames)
|
||||||
|
|
||||||
|
if hasattr(arg, 'description') and arg.description != '':
|
||||||
|
description += '_{}'.format(arg.description)
|
||||||
|
filename = '{}_{}.npz'.format(func_desc.strip('_'), description.strip('_'))
|
||||||
|
return os.path.join(savedir, filename)
|
||||||
|
|
||||||
|
|
||||||
|
def verify_file(filename, checksum):
|
||||||
|
"""Verify if the file matches the function call."""
|
||||||
|
file_checksum = 0
|
||||||
|
if os.path.exists(filename):
|
||||||
|
data = np.load(filename)
|
||||||
|
if 'checksum' in data:
|
||||||
|
file_checksum = data['checksum']
|
||||||
|
return file_checksum == checksum
|
||||||
|
|
||||||
|
|
||||||
|
def save_data(filename, checksum, data):
|
||||||
|
"""Save data and checksum to a file."""
|
||||||
|
notify('Saving result to file: {}'.format(filename))
|
||||||
|
try:
|
||||||
|
data = np.array(data)
|
||||||
|
except ValueError:
|
||||||
|
arr = np.empty((len(data),), dtype=object)
|
||||||
|
arr[:] = data
|
||||||
|
data = arr
|
||||||
|
|
||||||
|
np.savez(filename, checksum=checksum, data=data)
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(filename):
|
||||||
|
"""Load data from a npz file."""
|
||||||
|
notify('Loading result from file: {}'.format(filename))
|
||||||
|
fdata = np.load(filename)
|
||||||
|
if 'data' in fdata:
|
||||||
|
return fdata['data']
|
||||||
|
else:
|
||||||
|
data = tuple(fdata[k] for k in sorted(fdata) if ('arr' in k))
|
||||||
|
save_data(filename, fdata['checksum'], data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def autosave_data(nargs, kwargs_keys=None, version=None):
|
||||||
|
"""
|
||||||
|
Enable autosaving of results for a function.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
nargs: Number of args which are relevant for the calculation.
|
||||||
|
kwargs_keys (opt.): List of keyword arguments which are relevant for the calculation.
|
||||||
|
version (opt.):
|
||||||
|
An optional version number of the decorated function, which replaces the checksum of
|
||||||
|
the function code, hence the checksum does not depend on the function code.
|
||||||
|
"""
|
||||||
|
def decorator_function(function):
|
||||||
|
# make sure too include names of positional arguments in kwargs_keys,
|
||||||
|
# sice otherwise they will be ignored if passed via keyword.
|
||||||
|
# nonlocal kwargs_keys
|
||||||
|
posargs_keys = list(inspect.signature(function).parameters)[:nargs]
|
||||||
|
|
||||||
|
@functools.wraps(function)
|
||||||
|
def autosave(*args, **kwargs):
|
||||||
|
description = kwargs.pop('description', '')
|
||||||
|
autoload = kwargs.pop('autoload', True) and load_autosave_data
|
||||||
|
if autosave_directory is not None:
|
||||||
|
relevant_args = list(args[:nargs])
|
||||||
|
if kwargs_keys is not None:
|
||||||
|
for key in [*posargs_keys, *kwargs_keys]:
|
||||||
|
if key in kwargs:
|
||||||
|
relevant_args.append(kwargs[key])
|
||||||
|
|
||||||
|
if version is None:
|
||||||
|
csum = legacy_csum = checksum(function, *relevant_args)
|
||||||
|
else:
|
||||||
|
csum = checksum(version, *relevant_args)
|
||||||
|
legacy_csum = checksum(function, *relevant_args)
|
||||||
|
|
||||||
|
filename = get_filename(function, csum, description, *relevant_args)
|
||||||
|
if autoload and (verify_file(filename, csum) or verify_file(filename, legacy_csum)):
|
||||||
|
result = load_data(filename)
|
||||||
|
else:
|
||||||
|
result = function(*args, **kwargs)
|
||||||
|
save_data(filename, csum, result)
|
||||||
|
else:
|
||||||
|
result = function(*args, **kwargs)
|
||||||
|
|
||||||
|
return result
|
||||||
|
return autosave
|
||||||
|
return decorator_function
|
91
mdevaluate/checksum.py
Executable file
91
mdevaluate/checksum.py
Executable file
@ -0,0 +1,91 @@
|
|||||||
|
|
||||||
|
import functools
|
||||||
|
import hashlib
|
||||||
|
from .logging import logger
|
||||||
|
from types import ModuleType, FunctionType
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# This variable is used within the checksum function to salt the sha1 sum.
|
||||||
|
# May be changed to force a different checksum for similar objects.
|
||||||
|
SALT = 42
|
||||||
|
|
||||||
|
|
||||||
|
def version(version_nr, calls=[]):
|
||||||
|
"""Function decorator that assigns a custom checksum to a function."""
|
||||||
|
def decorator(func):
|
||||||
|
cs = checksum(func.__name__, version_nr, *calls)
|
||||||
|
func.__checksum__ = lambda: cs
|
||||||
|
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapped(*args, **kwargs):
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapped
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def strip_comments(s):
|
||||||
|
"""Strips comment lines and docstring from Python source string."""
|
||||||
|
o = ''
|
||||||
|
in_docstring = False
|
||||||
|
for l in s.split('\n'):
|
||||||
|
if l.strip().startswith(('#', '"', "'")) or in_docstring:
|
||||||
|
in_docstring = l.strip().startswith(('"""', "'''")) + in_docstring == 1
|
||||||
|
continue
|
||||||
|
o += l + '\n'
|
||||||
|
return o
|
||||||
|
|
||||||
|
|
||||||
|
def checksum(*args, csum=None):
|
||||||
|
"""
|
||||||
|
Calculate a checksum of any object, by sha1 hash.
|
||||||
|
|
||||||
|
Input for the hash are some salt bytes and the byte encoding of a string
|
||||||
|
that depends on the object and its type:
|
||||||
|
|
||||||
|
- If a method __checksum__ is available, it's return value is converted to bytes
|
||||||
|
- str or bytes are used as sha1 input directly
|
||||||
|
- modules use the __name__ attribute
|
||||||
|
- functions use the function code and any closures of the function
|
||||||
|
- functools.partial uses the checksum of the function and any arguments, that were defined
|
||||||
|
- numpy.ndarray uses bytes representation of the array (arr.tobytes())
|
||||||
|
- Anything else is converted to a str
|
||||||
|
"""
|
||||||
|
if csum is None:
|
||||||
|
csum = hashlib.sha1()
|
||||||
|
csum.update(str(SALT).encode())
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
if hasattr(arg, '__checksum__'):
|
||||||
|
logger.debug('Checksum via __checksum__: %s', str(arg))
|
||||||
|
csum.update(str(arg.__checksum__()).encode())
|
||||||
|
elif isinstance(arg, bytes):
|
||||||
|
csum.update(arg)
|
||||||
|
elif isinstance(arg, str):
|
||||||
|
csum.update(arg.encode())
|
||||||
|
elif isinstance(arg, ModuleType):
|
||||||
|
csum.update(arg.__name__.encode())
|
||||||
|
elif isinstance(arg, FunctionType):
|
||||||
|
csum.update(strip_comments(inspect.getsource(arg)).encode())
|
||||||
|
c = inspect.getclosurevars(arg)
|
||||||
|
for v in {**c.nonlocals, **c.globals}.values():
|
||||||
|
if v is not arg:
|
||||||
|
checksum(v, csum=csum)
|
||||||
|
elif isinstance(arg, functools.partial):
|
||||||
|
logger.debug('Checksum via partial for %s', str(arg))
|
||||||
|
checksum(arg.func, csum=csum)
|
||||||
|
for x in arg.args:
|
||||||
|
checksum(x, csum=csum)
|
||||||
|
for k in sorted(arg.keywords.keys()):
|
||||||
|
csum.update(k.encode())
|
||||||
|
checksum(arg.keywords[k], csum=csum)
|
||||||
|
elif isinstance(arg, np.ndarray):
|
||||||
|
csum.update(arg.tobytes())
|
||||||
|
else:
|
||||||
|
logger.debug('Checksum via str for %s', str(arg))
|
||||||
|
csum.update(str(arg).encode())
|
||||||
|
|
||||||
|
return int.from_bytes(csum.digest(), 'big')
|
||||||
|
|
35
mdevaluate/cli.py
Normal file
35
mdevaluate/cli.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import argparse
|
||||||
|
from . import logging
|
||||||
|
from . import open as md_open
|
||||||
|
|
||||||
|
|
||||||
|
def run(*args, **kwargs):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
'xtcfile',
|
||||||
|
help='The xtc file to index.',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--tpr',
|
||||||
|
help='The tprfile of the trajectory.',
|
||||||
|
dest='tpr', default=None
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--nojump',
|
||||||
|
help='Generate Nojump Matrices, requires a tpr file.',
|
||||||
|
dest='nojump', action='store_true', default=False
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--debug',
|
||||||
|
help='Set logging level to debug.',
|
||||||
|
dest='debug', action='store_true', default=False
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
if args.debug:
|
||||||
|
logging.setlevel('DEBUG')
|
||||||
|
|
||||||
|
md_open('', trajectory=args.xtcfile, topology=args.tpr, nojump=args.nojump)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
563
mdevaluate/coordinates.py
Executable file
563
mdevaluate/coordinates.py
Executable file
@ -0,0 +1,563 @@
|
|||||||
|
from functools import partial, lru_cache, wraps
|
||||||
|
from copy import copy
|
||||||
|
from .logging import logger
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from scipy.spatial import cKDTree, KDTree
|
||||||
|
|
||||||
|
from .atoms import AtomSubset
|
||||||
|
from .pbc import whole, nojump, pbc_diff
|
||||||
|
from .utils import mask2indices, singledispatchmethod
|
||||||
|
from .checksum import checksum
|
||||||
|
|
||||||
|
|
||||||
|
class UnknownCoordinatesMode(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def rotate_axis(coords, axis):
|
||||||
|
"""
|
||||||
|
Rotate a set of coordinates to a given axis.
|
||||||
|
"""
|
||||||
|
axis = np.array(axis) / np.linalg.norm(axis)
|
||||||
|
zaxis = np.array([0, 0, 1])
|
||||||
|
if (axis == zaxis).sum() == 3:
|
||||||
|
return coords
|
||||||
|
rotation_axis = np.cross(axis, zaxis)
|
||||||
|
rotation_axis = rotation_axis / np.linalg.norm(rotation_axis)
|
||||||
|
|
||||||
|
theta = np.arccos(axis @ zaxis / np.linalg.norm(axis))
|
||||||
|
|
||||||
|
# return theta/pi, rotation_axis
|
||||||
|
|
||||||
|
ux, uy, uz = rotation_axis
|
||||||
|
cross_matrix = np.array([
|
||||||
|
[0, -uz, uy],
|
||||||
|
[uz, 0, -ux],
|
||||||
|
[-uy, ux, 0]
|
||||||
|
])
|
||||||
|
rotation_matrix = np.cos(theta) * np.identity(len(axis)) \
|
||||||
|
+ (1 - np.cos(theta)) * rotation_axis.reshape(-1, 1) @ rotation_axis.reshape(1, -1) \
|
||||||
|
+ np.sin(theta) * cross_matrix
|
||||||
|
|
||||||
|
if len(coords.shape) == 2:
|
||||||
|
rotated = np.array([rotation_matrix @ xyz for xyz in coords])
|
||||||
|
else:
|
||||||
|
rotated = rotation_matrix @ coords
|
||||||
|
return rotated
|
||||||
|
|
||||||
|
|
||||||
|
def spherical_radius(frame, origin=None):
|
||||||
|
"""
|
||||||
|
Transform a frame of cartesian coordinates into the sperical radius.
|
||||||
|
If origin=None the center of the box is taken as the coordinates origin.
|
||||||
|
"""
|
||||||
|
if origin is None:
|
||||||
|
origin = frame.box.diagonal() / 2
|
||||||
|
return ((frame - origin)**2).sum(axis=-1)**0.5
|
||||||
|
|
||||||
|
|
||||||
|
def polar_coordinates(x, y):
|
||||||
|
"""Convert cartesian to polar coordinates."""
|
||||||
|
radius = (x**2 + y**2)**0.5
|
||||||
|
phi = np.arctan2(y, x)
|
||||||
|
return radius, phi
|
||||||
|
|
||||||
|
|
||||||
|
def spherical_coordinates(x, y, z):
|
||||||
|
"""Convert cartesian to spherical coordinates."""
|
||||||
|
xy, phi = polar_coordinates(x, y)
|
||||||
|
radius = (x**2 + y**2 + z**2)**0.5
|
||||||
|
theta = np.arccos(z / radius)
|
||||||
|
return radius, phi, theta
|
||||||
|
|
||||||
|
|
||||||
|
def radial_selector(frame, coordinates, rmin, rmax):
|
||||||
|
"""
|
||||||
|
Return a selection of all atoms with radius in the interval [rmin, rmax].
|
||||||
|
"""
|
||||||
|
crd = coordinates[frame.step]
|
||||||
|
rad, _ = polar_coordinates(crd[:, 0], crd[:, 1])
|
||||||
|
selector = (rad >= rmin) & (rad <= rmax)
|
||||||
|
return mask2indices(selector)
|
||||||
|
|
||||||
|
|
||||||
|
def spatial_selector(frame, transform, rmin, rmax):
|
||||||
|
"""
|
||||||
|
Select a subset of atoms which have a radius between rmin and rmax.
|
||||||
|
Coordinates are filtered by the condition::
|
||||||
|
|
||||||
|
rmin <= transform(frame) <= rmax
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The coordinates of the actual trajectory
|
||||||
|
transform:
|
||||||
|
A function that transforms the coordinates of the frames into
|
||||||
|
the one-dimensional spatial coordinate (e.g. radius).
|
||||||
|
rmin: Minimum value of the radius
|
||||||
|
rmax: Maximum value of the radius
|
||||||
|
"""
|
||||||
|
r = transform(frame)
|
||||||
|
selector = (rmin <= r) & (rmax >= r)
|
||||||
|
return mask2indices(selector)
|
||||||
|
|
||||||
|
|
||||||
|
class CoordinateFrame(np.ndarray):
|
||||||
|
|
||||||
|
_known_modes = ('pbc', 'whole', 'nojump')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def box(self):
|
||||||
|
return np.array(self.coordinates.frames[self.step].triclinic_dimensions)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def volume(self):
|
||||||
|
return self.box.diagonal().cumprod()[-1]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def time(self):
|
||||||
|
return self.coordinates.frames[self.step].time
|
||||||
|
|
||||||
|
@property
|
||||||
|
def masses(self):
|
||||||
|
return self.coordinates.atoms.masses[self.coordinates.atom_subset.selection]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def charges(self):
|
||||||
|
return self.coordinates.atoms.charges[self.coordinates.atom_subset.selection]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def residue_ids(self):
|
||||||
|
return self.coordinates.atom_subset.residue_ids
|
||||||
|
|
||||||
|
@property
|
||||||
|
def residue_names(self):
|
||||||
|
return self.coordinates.atom_subset.residue_names
|
||||||
|
|
||||||
|
@property
|
||||||
|
def atom_names(self):
|
||||||
|
return self.coordinates.atom_subset.atom_names
|
||||||
|
|
||||||
|
@property
|
||||||
|
def indices(self):
|
||||||
|
return self.coordinates.atom_subset.indices
|
||||||
|
|
||||||
|
@property
|
||||||
|
def selection(self):
|
||||||
|
return self.coordinates.atom_subset.selection
|
||||||
|
|
||||||
|
@property
|
||||||
|
def whole(self):
|
||||||
|
frame = whole(self)
|
||||||
|
frame.mode = 'whole'
|
||||||
|
return frame
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pbc(self):
|
||||||
|
frame = self % self.box.diagonal()
|
||||||
|
frame.mode = 'pbc'
|
||||||
|
return frame
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nojump(self):
|
||||||
|
if self.mode != 'nojump':
|
||||||
|
if self.mode is not None:
|
||||||
|
logger.warn('Combining Nojump with other Coordinate modes is not supported and may cause unexpected results.')
|
||||||
|
frame = nojump(self)
|
||||||
|
frame.mode = 'nojump'
|
||||||
|
return frame
|
||||||
|
else:
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __new__(subtype, shape, dtype=float, buffer=None, offset=0, strides=None, order=None,
|
||||||
|
coordinates=None, step=None, box=None, mode=None):
|
||||||
|
obj = np.ndarray.__new__(subtype, shape, dtype, buffer, offset, strides)
|
||||||
|
|
||||||
|
obj.coordinates = coordinates
|
||||||
|
obj.step = step
|
||||||
|
obj.mode = mode
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def __array_finalize__(self, obj):
|
||||||
|
if obj is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.coordinates = getattr(obj, 'coordinates', None)
|
||||||
|
self.step = getattr(obj, 'step', None)
|
||||||
|
self.mode = getattr(obj, 'mode', None)
|
||||||
|
if hasattr(obj, 'reference'):
|
||||||
|
self.reference = getattr(obj, 'reference')
|
||||||
|
|
||||||
|
|
||||||
|
class Coordinates:
|
||||||
|
"""
|
||||||
|
Coordinates represent trajectory data, which is used for evaluation functions.
|
||||||
|
|
||||||
|
Atoms may be selected by specifing a atom_subset or a atom_filter.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_mode(self, mode):
|
||||||
|
if self.atom_subset is not None:
|
||||||
|
return Coordinates(frames=self.frames, atom_subset=self.atom_subset, mode=mode)[self._slice]
|
||||||
|
else:
|
||||||
|
return Coordinates(frames=self.frames, atom_filter=self.atom_filter, mode=mode)[self._slice]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pbc(self):
|
||||||
|
return self.get_mode('pbc')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def whole(self):
|
||||||
|
return self.get_mode('whole')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nojump(self):
|
||||||
|
return self.get_mode('nojump')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mode(self):
|
||||||
|
return self._mode
|
||||||
|
|
||||||
|
@mode.setter
|
||||||
|
def mode(self, val):
|
||||||
|
if val in CoordinateFrame._known_modes:
|
||||||
|
logger.warn('Changing the Coordinates mode directly is deprecated. Use Coordinates.%s instead, which returns a copy.', val)
|
||||||
|
self._mode = val
|
||||||
|
else:
|
||||||
|
raise UnknownCoordinatesMode('No such mode: {}'.format(val))
|
||||||
|
|
||||||
|
def __init__(self, frames, atom_filter=None, atom_subset: AtomSubset=None, mode=None):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
frames: The trajectory reader
|
||||||
|
atom_filter (opt.): A mask which selects a subset of the system
|
||||||
|
atom_subset (opt.): A AtomSubset that selects a subset of the system
|
||||||
|
mode (opt.): PBC mode of the Coordinates, can be pbc, whole or nojump.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
The caching in Coordinates is deprecated, use the CachedReader or the function open
|
||||||
|
from the reader module instead.
|
||||||
|
"""
|
||||||
|
self._mode = mode
|
||||||
|
self.frames = frames
|
||||||
|
self._slice = slice(None)
|
||||||
|
assert atom_filter is None or atom_subset is None, "Cannot use both: subset and filter"
|
||||||
|
|
||||||
|
if atom_filter is not None:
|
||||||
|
self.atom_filter = atom_filter
|
||||||
|
self.atom_subset = None
|
||||||
|
elif atom_subset is not None:
|
||||||
|
self.atom_filter = atom_subset.selection
|
||||||
|
self.atom_subset = atom_subset
|
||||||
|
self.atoms = atom_subset.atoms
|
||||||
|
else:
|
||||||
|
self.atom_filter = np.ones(shape=(len(frames[0].coordinates),), dtype=bool)
|
||||||
|
self.atom_subset = None
|
||||||
|
|
||||||
|
def get_frame(self, fnr):
|
||||||
|
"""Returns the fnr-th frame."""
|
||||||
|
try:
|
||||||
|
if self.atom_filter is not None:
|
||||||
|
frame = self.frames[fnr].positions[self.atom_filter].view(CoordinateFrame)
|
||||||
|
else:
|
||||||
|
frame = self.frames.__getitem__(fnr).positions.view(CoordinateFrame)
|
||||||
|
frame.coordinates = self
|
||||||
|
frame.step = fnr
|
||||||
|
if self.mode is not None:
|
||||||
|
frame = getattr(frame, self.mode)
|
||||||
|
except EOFError:
|
||||||
|
raise IndexError
|
||||||
|
|
||||||
|
return frame
|
||||||
|
|
||||||
|
def clear_cache(self):
|
||||||
|
"""Clears the frame cache, if it is enabled."""
|
||||||
|
if hasattr(self.get_frame, 'clear_cache'):
|
||||||
|
self.get_frame.clear_cache()
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for i in range(len(self))[self._slice]:
|
||||||
|
yield self[i]
|
||||||
|
|
||||||
|
@singledispatchmethod
|
||||||
|
def __getitem__(self, item):
|
||||||
|
return self.get_frame(item)
|
||||||
|
|
||||||
|
@__getitem__.register(slice)
|
||||||
|
def _(self, item):
|
||||||
|
sliced = copy(self)
|
||||||
|
sliced._slice = item
|
||||||
|
return sliced
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.frames)
|
||||||
|
|
||||||
|
def __checksum__(self):
|
||||||
|
return checksum(self.frames, self.atom_filter, self._slice, self.mode)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "Coordinates <{}>: {}".format(self.frames.filename, self.atom_subset)
|
||||||
|
|
||||||
|
@wraps(AtomSubset.subset)
|
||||||
|
def subset(self, **kwargs):
|
||||||
|
return Coordinates(self.frames, atom_subset=self.atom_subset.subset(**kwargs), mode=self._mode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
return self.atom_subset.description
|
||||||
|
|
||||||
|
@description.setter
|
||||||
|
def description(self, desc):
|
||||||
|
self.atom_subset.description = desc
|
||||||
|
|
||||||
|
|
||||||
|
class MeanCoordinates(Coordinates):
|
||||||
|
|
||||||
|
def __init__(self, frames, atom_filter=None, mean=1):
|
||||||
|
super().__init__(frames, atom_filter)
|
||||||
|
self.mean = mean
|
||||||
|
assert mean >= 1, "Mean must be positive"
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
frame = super().__getitem__(item)
|
||||||
|
for i in range(item + 1, item + self.mean):
|
||||||
|
frame += super().__getitem__(i)
|
||||||
|
|
||||||
|
return frame / self.mean
|
||||||
|
|
||||||
|
def len(self):
|
||||||
|
return len(super() - self.mean + 1)
|
||||||
|
|
||||||
|
|
||||||
|
class CoordinatesMap:
|
||||||
|
|
||||||
|
def __init__(self, coordinates, function):
|
||||||
|
self.coordinates = coordinates
|
||||||
|
self.frames = self.coordinates.frames
|
||||||
|
self.atom_subset = self.coordinates.atom_subset
|
||||||
|
self.function = function
|
||||||
|
if isinstance(function, partial):
|
||||||
|
self._description = self.function.func.__name__
|
||||||
|
else:
|
||||||
|
self._description = self.function.__name__
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for frame in self.coordinates:
|
||||||
|
step = frame.step
|
||||||
|
frame = self.function(frame)
|
||||||
|
if not isinstance(frame, CoordinateFrame):
|
||||||
|
frame = frame.view(CoordinateFrame)
|
||||||
|
frame.coordinates = self
|
||||||
|
frame.step = step
|
||||||
|
yield frame
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
if isinstance(item, slice):
|
||||||
|
return self.__class__(self.coordinates[item], self.function)
|
||||||
|
else:
|
||||||
|
frame = self.function(self.coordinates.__getitem__(item))
|
||||||
|
if not isinstance(frame, CoordinateFrame):
|
||||||
|
frame = frame.view(CoordinateFrame)
|
||||||
|
frame.coordinates = self
|
||||||
|
frame.step = item
|
||||||
|
return frame
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.coordinates.frames)
|
||||||
|
|
||||||
|
def __checksum__(self):
|
||||||
|
return checksum(self.coordinates, self.function)
|
||||||
|
|
||||||
|
@wraps(Coordinates.subset)
|
||||||
|
def subset(self, **kwargs):
|
||||||
|
return CoordinatesMap(self.coordinates.subset(**kwargs), self.function)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
return '{}_{}'.format(self._description, self.coordinates.description)
|
||||||
|
|
||||||
|
@description.setter
|
||||||
|
def description(self, desc):
|
||||||
|
self._description = desc
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nojump(self):
|
||||||
|
return CoordinatesMap(self.coordinates.nojump, self.function)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def whole(self):
|
||||||
|
return CoordinatesMap(self.coordinates.whole, self.function)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pbc(self):
|
||||||
|
return CoordinatesMap(self.coordinates.pbc, self.function)
|
||||||
|
|
||||||
|
class CoordinatesFilter:
|
||||||
|
|
||||||
|
@property
|
||||||
|
def atom_subset(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __init__(self, coordinates, atom_filter):
|
||||||
|
self.coordinates = coordinates
|
||||||
|
self.atom_filter = atom_filter
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
if isinstance(item, slice):
|
||||||
|
sliced = copy(self)
|
||||||
|
sliced.coordinates = self.coordinates[item]
|
||||||
|
return sliced
|
||||||
|
else:
|
||||||
|
frame = self.coordinates[item]
|
||||||
|
return frame[self.atom_filter]
|
||||||
|
|
||||||
|
|
||||||
|
class CoordinatesKDTree:
|
||||||
|
"""
|
||||||
|
A KDTree of coordinates frames. The KDtrees are cached by a :func:`functools.lru_cache`.
|
||||||
|
Uses :class:`scipy.spatial.cKDTree` by default, since it's significantly faster.
|
||||||
|
Make sure to use scipy 0.17 or later or switch to the normal KDTree, since cKDTree has
|
||||||
|
a memory leak in earlier versions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def clear_cache(self):
|
||||||
|
"""Clear the LRU cache."""
|
||||||
|
self._get_tree_at_index.cache_clear()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cache_info(self):
|
||||||
|
"""Return info about the state of the cache."""
|
||||||
|
return self._get_tree_at_index.cache_info()
|
||||||
|
|
||||||
|
def _get_tree_at_index(self, index):
|
||||||
|
frame = self.frames[index]
|
||||||
|
return self.kdtree(frame[self.selector(frame)])
|
||||||
|
|
||||||
|
def __init__(self, frames, selector=None, boxsize=None, maxcache=128, ckdtree=True):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
frames: Trajectory of the simulation, can be Coordinates object or reader
|
||||||
|
selector: Selector function that selects a subset of each frame
|
||||||
|
maxcache: Maxsize of the :func:`~functools.lru_cache`
|
||||||
|
ckdtree: Use :class:`~scipy.spatial.cKDTree` or :class:`~scipy.spatial.KDTree` if False
|
||||||
|
"""
|
||||||
|
if selector is not None:
|
||||||
|
self.selector = selector
|
||||||
|
else:
|
||||||
|
self.selector = lambda x: slice(None)
|
||||||
|
self.frames = frames
|
||||||
|
self.kdtree = cKDTree if ckdtree else KDTree
|
||||||
|
if boxsize is not None:
|
||||||
|
self.kdtree = partial(self.kdtree, boxsize=boxsize)
|
||||||
|
self._get_tree_at_index = lru_cache(maxsize=maxcache)(self._get_tree_at_index)
|
||||||
|
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self._get_tree_at_index(index)
|
||||||
|
|
||||||
|
def __checksum__(self):
|
||||||
|
return checksum(self.selector, self.frames)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return super().__eq__(other)
|
||||||
|
|
||||||
|
|
||||||
|
def map_coordinates(func):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapped(coordinates, **kwargs):
|
||||||
|
return CoordinatesMap(coordinates, partial(func, **kwargs))
|
||||||
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
|
@map_coordinates
|
||||||
|
def centers_of_mass(c, *, masses=None):
|
||||||
|
"""
|
||||||
|
|
||||||
|
A- 1
|
||||||
|
B- 2
|
||||||
|
A- 1
|
||||||
|
C 3
|
||||||
|
A-
|
||||||
|
B-
|
||||||
|
A-
|
||||||
|
C
|
||||||
|
A-
|
||||||
|
B-
|
||||||
|
A-
|
||||||
|
C
|
||||||
|
|
||||||
|
|
||||||
|
Example:
|
||||||
|
rd = XTCReader('t.xtc')
|
||||||
|
coordinates = Coordinates(rd)
|
||||||
|
com = centers_of_mass(coordinates, (1.0, 2.0, 1.0, 3.0))
|
||||||
|
|
||||||
|
"""
|
||||||
|
# At first, regroup our array
|
||||||
|
number_of_masses = len(masses)
|
||||||
|
number_of_coordinates, number_of_dimensions = c.shape
|
||||||
|
number_of_new_coordinates = number_of_coordinates // number_of_masses
|
||||||
|
grouped_masses = c.reshape(number_of_new_coordinates, number_of_masses, number_of_dimensions)
|
||||||
|
|
||||||
|
return np.average(grouped_masses, axis=1, weights=masses)
|
||||||
|
|
||||||
|
|
||||||
|
@map_coordinates
|
||||||
|
def pore_coordinates(coordinates, origin, sym_axis='z'):
|
||||||
|
"""
|
||||||
|
Map coordinates of a pore simulation so the pore has cylindrical symmetry.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
coordinates: Coordinates of the simulation
|
||||||
|
origin: Origin of the pore which will be the coordinates origin after mapping
|
||||||
|
sym_axis (opt.): Symmtery axis of the pore, may be a literal direction
|
||||||
|
'x', 'y' or 'z' or an array of shape (3,)
|
||||||
|
"""
|
||||||
|
if sym_axis in ('x', 'y', 'z'):
|
||||||
|
rot_axis = np.zeros(shape=(3,))
|
||||||
|
rot_axis[['x', 'y', 'z'].index(sym_axis)] = 1
|
||||||
|
else:
|
||||||
|
rot_axis = sym_axis
|
||||||
|
|
||||||
|
return rotate_axis(coordinates - origin, rot_axis)
|
||||||
|
|
||||||
|
|
||||||
|
@map_coordinates
|
||||||
|
def vectors(coordinates, atoms_a, atoms_b, normed=False, box=None):
|
||||||
|
"""
|
||||||
|
Compute the vectors between the atoms of two subsets.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
coordinates: The Coordinates object the atoms will be taken from
|
||||||
|
atoms_a: Mask or indices of the first atom subset
|
||||||
|
atoms_b: Mask or indices of the second atom subset
|
||||||
|
normed (opt.): If the vectors should be normed
|
||||||
|
box (opt.): If not None, the vectors are calcualte with PBC
|
||||||
|
|
||||||
|
The defintion of atoms_a/b can be any possible subript of a numpy array.
|
||||||
|
They can, for example, be given as a masking array of bool values with the
|
||||||
|
same length as the frames of the coordinates. Or they can be a list of
|
||||||
|
indices selecting the atoms of these indices from each frame.
|
||||||
|
|
||||||
|
It is possible to compute the mean of several atoms before calculating the vectors,
|
||||||
|
by using a two-dimensional list of indices. The following code computes the vectors
|
||||||
|
between atoms 0, 3, 6 and the mean coordinate of atoms 1, 4, 7 and 2, 5, 8::
|
||||||
|
|
||||||
|
>>> inds_a = [0, 3, 6]
|
||||||
|
>>> inds_b = [[1, 4, 7], [2, 5, 8]]
|
||||||
|
>>> vectors(coords, inds_a, inds_b)
|
||||||
|
array([
|
||||||
|
coords[0] - (coords[1] + coords[2])/2,
|
||||||
|
coords[3] - (coords[4] + coords[5])/2,
|
||||||
|
coords[6] - (coords[7] + coords[8])/2,
|
||||||
|
])
|
||||||
|
"""
|
||||||
|
coords_a = coordinates[atoms_a]
|
||||||
|
if len(coords_a.shape) > 2:
|
||||||
|
coords_a = coords_a.mean(axis=0)
|
||||||
|
coords_b = coordinates[atoms_b]
|
||||||
|
if len(coords_b.shape) > 2:
|
||||||
|
coords_b = coords_b.mean(axis=0)
|
||||||
|
vectors = pbc_diff(coords_a, coords_b, box=box)
|
||||||
|
norm = np.linalg.norm(vectors, axis=-1).reshape(-1, 1) if normed else 1
|
||||||
|
vectors.reference = coords_a
|
||||||
|
return vectors / norm
|
358
mdevaluate/correlation.py
Normal file
358
mdevaluate/correlation.py
Normal file
@ -0,0 +1,358 @@
|
|||||||
|
import numpy as np
|
||||||
|
from scipy.special import legendre
|
||||||
|
from itertools import chain
|
||||||
|
import dask.array as darray
|
||||||
|
|
||||||
|
from .autosave import autosave_data
|
||||||
|
from .utils import filon_fourier_transformation, coherent_sum, histogram
|
||||||
|
from .pbc import pbc_diff
|
||||||
|
from .logging import logger
|
||||||
|
|
||||||
|
def set_has_counter(func):
|
||||||
|
func.has_counter = True
|
||||||
|
return func
|
||||||
|
|
||||||
|
def log_indices(first, last, num=100):
|
||||||
|
ls = np.logspace(0, np.log10(last - first + 1), num=num)
|
||||||
|
return np.unique(np.int_(ls) - 1 + first)
|
||||||
|
|
||||||
|
|
||||||
|
def correlation(function, frames):
|
||||||
|
iterator = iter(frames)
|
||||||
|
start_frame = next(iterator)
|
||||||
|
return map(lambda f: function(start_frame, f), chain([start_frame], iterator))
|
||||||
|
|
||||||
|
|
||||||
|
def subensemble_correlation(selector_function, correlation_function=correlation):
|
||||||
|
|
||||||
|
def c(function, frames):
|
||||||
|
iterator = iter(frames)
|
||||||
|
start_frame = next(iterator)
|
||||||
|
selector = selector_function(start_frame)
|
||||||
|
subensemble = map(lambda f: f[selector], chain([start_frame], iterator))
|
||||||
|
return correlation_function(function, subensemble)
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
def multi_subensemble_correlation(selector_function):
|
||||||
|
"""
|
||||||
|
selector_function has to expect a frame and to
|
||||||
|
return either valid indices (as with subensemble_correlation)
|
||||||
|
or a multidimensional array whose entries are valid indices
|
||||||
|
|
||||||
|
e.g. slice(10,100,2)
|
||||||
|
|
||||||
|
e.g. [1,2,3,4,5]
|
||||||
|
|
||||||
|
e.g. [[[0,1],[2],[3]],[[4],[5],[6]] -> shape: 2,3 with
|
||||||
|
list of indices of varying length
|
||||||
|
|
||||||
|
e.g. [slice(1653),slice(1653,None,3)]
|
||||||
|
|
||||||
|
e.g. [np.ones(len_of_frames, bool)]
|
||||||
|
|
||||||
|
in general using slices is the most efficient.
|
||||||
|
if the selections are small subsets of a frame or when many subsets are empty
|
||||||
|
using indices will be more efficient than using masks.
|
||||||
|
"""
|
||||||
|
@set_has_counter
|
||||||
|
def cmulti(function, frames):
|
||||||
|
iterator = iter(frames)
|
||||||
|
start_frame = next(iterator)
|
||||||
|
selectors = np.asarray(selector_function(start_frame))
|
||||||
|
sel_shape = selectors.shape
|
||||||
|
if sel_shape[-1] == 0: selectors = np.asarray(selectors,int)
|
||||||
|
if (selectors.dtype != object): sel_shape = sel_shape[:-1]
|
||||||
|
f_values = np.zeros(sel_shape + function(start_frame,start_frame).shape,)
|
||||||
|
count = np.zeros(sel_shape, dtype=int)
|
||||||
|
is_first_frame_loop = True
|
||||||
|
def cc(act_frame):
|
||||||
|
nonlocal is_first_frame_loop
|
||||||
|
for index in np.ndindex(sel_shape):
|
||||||
|
sel = selectors[index]
|
||||||
|
sf_sel = start_frame[sel]
|
||||||
|
if is_first_frame_loop:
|
||||||
|
count[index] = len(sf_sel)
|
||||||
|
f_values[index] = function(sf_sel, act_frame[sel]) if count[index] != 0 else 0
|
||||||
|
is_first_frame_loop = False
|
||||||
|
return np.asarray(f_values.copy())
|
||||||
|
return map(cc, chain([start_frame], iterator)), count
|
||||||
|
return cmulti
|
||||||
|
|
||||||
|
@autosave_data(nargs=2, kwargs_keys=(
|
||||||
|
'index_distribution', 'correlation', 'segments', 'window', 'skip', 'average'
|
||||||
|
), version='shifted_correlation-1')
|
||||||
|
def shifted_correlation(function, frames,
|
||||||
|
index_distribution=log_indices, correlation=correlation,
|
||||||
|
segments=10, window=0.5, skip=None,
|
||||||
|
average=False, ):
|
||||||
|
|
||||||
|
"""
|
||||||
|
Calculate the time series for a correlation function.
|
||||||
|
|
||||||
|
The times at which the correlation is calculated are determined automatically by the
|
||||||
|
function given as ``index_distribution``. The default is a logarithmic distribution.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
function: The function that should be correlated
|
||||||
|
frames: The coordinates of the simulation data
|
||||||
|
index_distribution (opt.):
|
||||||
|
A function that returns the indices for which the timeseries
|
||||||
|
will be calculated
|
||||||
|
correlation (function, opt.):
|
||||||
|
The correlation function
|
||||||
|
segments (int, opt.):
|
||||||
|
The number of segments the time window will be shifted
|
||||||
|
window (float, opt.):
|
||||||
|
The fraction of the simulation the time series will cover
|
||||||
|
skip (float, opt.):
|
||||||
|
The fraction of the trajectory that will be skipped at the beginning,
|
||||||
|
if this is None the start index of the frames slice will be used,
|
||||||
|
which defaults to 0.
|
||||||
|
counter (bool, opt.):
|
||||||
|
If True, returns length of frames (in general number of particles specified)
|
||||||
|
average (bool, opt.):
|
||||||
|
If True, returns averaged correlation function
|
||||||
|
Returns:
|
||||||
|
tuple:
|
||||||
|
A list of length N that contains the indices of the frames at which
|
||||||
|
the time series was calculated and a numpy array of shape (segments, N)
|
||||||
|
that holds the (non-avaraged) correlation data
|
||||||
|
|
||||||
|
if has_counter == True: adds number of counts to output tupel.
|
||||||
|
if average is returned it will be weighted.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
Calculating the mean square displacement of a coordinates object named ``coords``:
|
||||||
|
|
||||||
|
>>> indices, data = shifted_correlation(msd, coords)
|
||||||
|
"""
|
||||||
|
if skip is None:
|
||||||
|
try:
|
||||||
|
skip = frames._slice.start / len(frames)
|
||||||
|
except (TypeError, AttributeError):
|
||||||
|
skip = 0
|
||||||
|
assert window + skip < 1
|
||||||
|
|
||||||
|
start_frames = np.unique(np.linspace(
|
||||||
|
len(frames) * skip, len(frames) * (1 - window),
|
||||||
|
num=segments, endpoint=False, dtype=int
|
||||||
|
))
|
||||||
|
num_frames = int(len(frames) * (window))
|
||||||
|
|
||||||
|
idx = index_distribution(0, num_frames)
|
||||||
|
|
||||||
|
|
||||||
|
def correlate(start_frame):
|
||||||
|
shifted_idx = idx + start_frame
|
||||||
|
return correlation(function, map(frames.__getitem__, shifted_idx))
|
||||||
|
|
||||||
|
times = np.array([frames[i].time for i in idx]) - frames[0].time
|
||||||
|
|
||||||
|
if getattr(correlation, "has_counter", False):
|
||||||
|
if average:
|
||||||
|
for i, start_frame in enumerate(start_frames):
|
||||||
|
act_result, act_count = correlate(start_frame)
|
||||||
|
act_result = np.array(list(act_result))
|
||||||
|
act_count = np.array(act_count)
|
||||||
|
if i == 0:
|
||||||
|
count = act_count
|
||||||
|
cdim = act_count.ndim
|
||||||
|
rdim = act_result.ndim
|
||||||
|
bt = np.newaxis,
|
||||||
|
for i in range(rdim - 1):
|
||||||
|
if i >= cdim:
|
||||||
|
bt += np.newaxis,
|
||||||
|
else:
|
||||||
|
bt += slice(None),
|
||||||
|
result = act_result * act_count[bt]
|
||||||
|
else:
|
||||||
|
result += act_result * act_count[bt]
|
||||||
|
count += act_count
|
||||||
|
np.divide(result, count[bt], out = result, where = count[bt] != 0)
|
||||||
|
result = np.moveaxis(result,0,cdim)
|
||||||
|
count = count / len(start_frames)
|
||||||
|
output = times, result, count
|
||||||
|
else:
|
||||||
|
count = []
|
||||||
|
result = []
|
||||||
|
for i, start_frame in enumerate(start_frames):
|
||||||
|
act_result, act_count = correlate(start_frame)
|
||||||
|
act_result = list(act_result)
|
||||||
|
result.append(act_result)
|
||||||
|
count.append(act_count)
|
||||||
|
count = np.asarray(count)
|
||||||
|
cdim = count.ndim
|
||||||
|
result = np.asarray(result)
|
||||||
|
result = np.moveaxis(result,1,cdim)
|
||||||
|
output = times, result, count
|
||||||
|
else:
|
||||||
|
result = 0 if average else []
|
||||||
|
for i, start_frame in enumerate(start_frames):
|
||||||
|
if average:
|
||||||
|
result += np.array(list(correlate(start_frame)))
|
||||||
|
else:
|
||||||
|
result.append(list(correlate(start_frame)))
|
||||||
|
result = np.array(result)
|
||||||
|
if average:
|
||||||
|
result = result / len(start_frames)
|
||||||
|
output = times, result
|
||||||
|
return output
|
||||||
|
|
||||||
|
def msd(start, frame):
|
||||||
|
"""
|
||||||
|
Mean square displacement
|
||||||
|
"""
|
||||||
|
vec = start - frame
|
||||||
|
return (vec ** 2).sum(axis=1).mean()
|
||||||
|
|
||||||
|
|
||||||
|
def isf(start, frame, q, box=None):
|
||||||
|
"""
|
||||||
|
Incoherent intermediate scattering function. To specify q, use
|
||||||
|
water_isf = functools.partial(isf, q=22.77) # q has the value 22.77 nm^-1
|
||||||
|
|
||||||
|
:param q: length of scattering vector
|
||||||
|
"""
|
||||||
|
vec = start - frame
|
||||||
|
distance = (vec ** 2).sum(axis=1) ** .5
|
||||||
|
return np.sinc(distance * q / np.pi).mean()
|
||||||
|
|
||||||
|
|
||||||
|
def rotational_autocorrelation(onset, frame, order=2):
|
||||||
|
"""
|
||||||
|
Compute the rotaional autocorrelation of the legendre polynamial for the given vectors.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
onset, frame: CoordinateFrames of vectors
|
||||||
|
order (opt.): Order of the legendre polynomial.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Skalar value of the correltaion function.
|
||||||
|
"""
|
||||||
|
scalar_prod = (onset * frame).sum(axis=-1)
|
||||||
|
poly = legendre(order)
|
||||||
|
return poly(scalar_prod).mean()
|
||||||
|
|
||||||
|
|
||||||
|
def van_hove_self(start, end, bins):
|
||||||
|
r"""
|
||||||
|
Compute the self part of the Van Hove autocorrelation function.
|
||||||
|
|
||||||
|
..math::
|
||||||
|
G(r, t) = \sum_i \delta(|\vec r_i(0) - \vec r_i(t)| - r)
|
||||||
|
"""
|
||||||
|
vec = start - end
|
||||||
|
delta_r = ((vec)**2).sum(axis=-1)**.5
|
||||||
|
return 1 / len(start) * histogram(delta_r, bins)[0]
|
||||||
|
|
||||||
|
|
||||||
|
def van_hove_distinct(onset, frame, bins, box=None, use_dask=True, comp=False, bincount=True):
|
||||||
|
r"""
|
||||||
|
Compute the distinct part of the Van Hove autocorrelation function.
|
||||||
|
|
||||||
|
..math::
|
||||||
|
G(r, t) = \sum_{i, j} \delta(|\vec r_i(0) - \vec r_j(t)| - r)
|
||||||
|
"""
|
||||||
|
if box is None:
|
||||||
|
box = onset.box.diagonal()
|
||||||
|
dimension = len(box)
|
||||||
|
N = len(onset)
|
||||||
|
if use_dask:
|
||||||
|
onset = darray.from_array(onset, chunks=(500, dimension)).reshape(1, N, dimension)
|
||||||
|
frame = darray.from_array(frame, chunks=(500, dimension)).reshape(N, 1, dimension)
|
||||||
|
dist = ((pbc_diff(onset, frame, box)**2).sum(axis=-1)**0.5).ravel()
|
||||||
|
if np.diff(bins).std() < 1e6:
|
||||||
|
dx = bins[0] - bins[1]
|
||||||
|
hist = darray.bincount((dist // dx).astype(int), minlength=(len(bins) - 1))
|
||||||
|
else:
|
||||||
|
hist = darray.histogram(dist, bins=bins)[0]
|
||||||
|
return hist.compute() / N
|
||||||
|
else:
|
||||||
|
if comp:
|
||||||
|
|
||||||
|
dx = bins[1] - bins[0]
|
||||||
|
minlength = len(bins) - 1
|
||||||
|
|
||||||
|
def f(x):
|
||||||
|
d = (pbc_diff(x, frame, box)**2).sum(axis=-1)**0.5
|
||||||
|
return np.bincount((d // dx).astype(int), minlength=minlength)[:minlength]
|
||||||
|
hist = sum(f(x) for x in onset)
|
||||||
|
else:
|
||||||
|
dist = (pbc_diff(onset.reshape(1, -1, 3), frame.reshape(-1, 1, 3), box)**2).sum(axis=-1)**0.5
|
||||||
|
hist = histogram(dist, bins=bins)[0]
|
||||||
|
return hist / N
|
||||||
|
|
||||||
|
|
||||||
|
def overlap(onset, frame, crds_tree, radius):
|
||||||
|
"""
|
||||||
|
Compute the overlap with a reference configuration defined in a CoordinatesTree.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
onset: Initial frame, this is only used to get the frame index
|
||||||
|
frame: The current configuration
|
||||||
|
crds_tree: A CoordinatesTree of the reference configurations
|
||||||
|
radius: The cutoff radius for the overlap
|
||||||
|
|
||||||
|
This function is intended to be used with :func:`shifted_correlation`.
|
||||||
|
As usual the first two arguments are used internally and the remaining ones
|
||||||
|
should be defined with :func:`functools.partial`.
|
||||||
|
|
||||||
|
If the overlap of a subset of the system should be calculated, this has to be
|
||||||
|
defined through a selection of the reference configurations in the CoordinatesTree.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> shifted_correlation(
|
||||||
|
... partial(overlap, crds_tree=CoordinatesTree(traj), radius=0.11),
|
||||||
|
... traj
|
||||||
|
... )
|
||||||
|
"""
|
||||||
|
tree = crds_tree[onset.step]
|
||||||
|
return (tree.query(frame)[0] <= radius).sum() / tree.n
|
||||||
|
|
||||||
|
|
||||||
|
def susceptibility(time, correlation, **kwargs):
|
||||||
|
"""
|
||||||
|
Calculate the susceptibility of a correlation function.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
time: Timesteps of the correlation data
|
||||||
|
correlation: Value of the correlation function
|
||||||
|
**kwargs (opt.):
|
||||||
|
Additional keyword arguments will be passed to :func:`filon_fourier_transformation`.
|
||||||
|
"""
|
||||||
|
frequencies, fourier = filon_fourier_transformation(time, correlation, imag=False, **kwargs)
|
||||||
|
return frequencies, frequencies * fourier
|
||||||
|
|
||||||
|
def coherent_scattering_function(onset, frame, q):
|
||||||
|
"""
|
||||||
|
Calculate the coherent scattering function.
|
||||||
|
"""
|
||||||
|
box = onset.box.diagonal()
|
||||||
|
dimension = len(box)
|
||||||
|
|
||||||
|
def scfunc(x, y):
|
||||||
|
sqdist = 0
|
||||||
|
for i in range(dimension):
|
||||||
|
d = x[i] - y[i]
|
||||||
|
if d > box[i] / 2:
|
||||||
|
d -= box[i]
|
||||||
|
if d < -box[i] / 2:
|
||||||
|
d += box[i]
|
||||||
|
sqdist += d**2
|
||||||
|
x = sqdist**0.5 * q
|
||||||
|
if x == 0:
|
||||||
|
return 1.0
|
||||||
|
else:
|
||||||
|
return np.sin(x) / x
|
||||||
|
|
||||||
|
return coherent_sum(scfunc, onset.pbc, frame.pbc) / len(onset)
|
||||||
|
|
||||||
|
def non_gaussian(onset, frame):
|
||||||
|
"""
|
||||||
|
Calculate the Non-Gaussian parameter :
|
||||||
|
..math:
|
||||||
|
\alpha_2 (t) = \frac{3}{5}\frac{\langle r_i^4(t)\rangle}{\langle r_i^2(t)\rangle^2} - 1
|
||||||
|
"""
|
||||||
|
r_2 = ((frame - onset)**2).sum(axis=-1)
|
||||||
|
return 3 / 5 * (r_2**2).mean() / r_2.mean()**2 - 1
|
359
mdevaluate/distribution.py
Normal file
359
mdevaluate/distribution.py
Normal file
@ -0,0 +1,359 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from .coordinates import rotate_axis, polar_coordinates, spherical_coordinates
|
||||||
|
from .atoms import next_neighbors
|
||||||
|
from .autosave import autosave_data
|
||||||
|
from .utils import runningmean
|
||||||
|
from .pbc import pbc_diff, pbc_points
|
||||||
|
from .logging import logger
|
||||||
|
from scipy import spatial
|
||||||
|
|
||||||
|
|
||||||
|
@autosave_data(nargs=2, kwargs_keys=('coordinates_b',), version='time_average-1')
|
||||||
|
def time_average(function, coordinates, coordinates_b=None, pool=None):
|
||||||
|
"""
|
||||||
|
Compute the time average of a function.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
function:
|
||||||
|
The function that will be averaged, it has to accept exactly one argument
|
||||||
|
which is the current atom set
|
||||||
|
coordinates: The coordinates object of the simulation
|
||||||
|
pool (multiprocessing.Pool, opt.):
|
||||||
|
A multiprocessing pool which will be used for cocurrent calculation of the
|
||||||
|
averaged function
|
||||||
|
|
||||||
|
"""
|
||||||
|
if pool is not None:
|
||||||
|
_map = pool.imap
|
||||||
|
else:
|
||||||
|
_map = map
|
||||||
|
|
||||||
|
number_of_averages = 0
|
||||||
|
result = 0
|
||||||
|
|
||||||
|
if coordinates_b is not None:
|
||||||
|
if coordinates._slice != coordinates_b._slice:
|
||||||
|
logger.warning("Different slice for coordinates and coordinates_b.")
|
||||||
|
coordinate_iter = (iter(coordinates), iter(coordinates_b))
|
||||||
|
else:
|
||||||
|
coordinate_iter = (iter(coordinates),)
|
||||||
|
|
||||||
|
evaluated = _map(function, *coordinate_iter)
|
||||||
|
|
||||||
|
for ev in evaluated:
|
||||||
|
number_of_averages += 1
|
||||||
|
result += ev
|
||||||
|
if number_of_averages % 100 == 0:
|
||||||
|
logger.debug('time_average: %d', number_of_averages)
|
||||||
|
|
||||||
|
return result / number_of_averages
|
||||||
|
|
||||||
|
|
||||||
|
def time_histogram(function, coordinates, bins, hist_range, pool=None):
|
||||||
|
coordinate_iter = iter(coordinates)
|
||||||
|
|
||||||
|
if pool is not None:
|
||||||
|
_map = pool.imap
|
||||||
|
else:
|
||||||
|
_map = map
|
||||||
|
|
||||||
|
evaluated = _map(function, coordinate_iter)
|
||||||
|
|
||||||
|
results = []
|
||||||
|
hist_results = []
|
||||||
|
for num, ev in enumerate(evaluated):
|
||||||
|
results.append(ev)
|
||||||
|
|
||||||
|
if num % 100 == 0 and num > 0:
|
||||||
|
print(num)
|
||||||
|
r = np.array(results).T
|
||||||
|
for i, row in enumerate(r):
|
||||||
|
histo, _ = np.histogram(row, bins=bins, range=hist_range)
|
||||||
|
if len(hist_results) <= i:
|
||||||
|
hist_results.append(histo)
|
||||||
|
else:
|
||||||
|
hist_results[i] += histo
|
||||||
|
results = []
|
||||||
|
return hist_results
|
||||||
|
|
||||||
|
|
||||||
|
def rdf(atoms_a, atoms_b=None, bins=None, box=None, kind=None, chunksize=50000, returnx=False, **kwargs):
|
||||||
|
r"""
|
||||||
|
Compute the radial pair distribution of one or two sets of atoms.
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
g_{AB}(r) = \frac{1}{\langle \rho_B\rangle N_A}\sum\limits_{i\in A}^{N_A}
|
||||||
|
\sum\limits_{j\in B}^{N_B}\frac{\delta(r_{ij} -r)}{4\pi r^2}
|
||||||
|
|
||||||
|
For use with :func:`time_average`, define bins through the use of :func:`~functools.partial`,
|
||||||
|
the atom sets are passed to :func:`time_average`, if a second set of atoms should be used
|
||||||
|
specify it as ``coordinates_b`` and it will be passed to this function.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
atoms_a: First set of atoms, used internally
|
||||||
|
atoms_b (opt.): Second set of atoms, used internally
|
||||||
|
bins: Bins of the radial distribution function
|
||||||
|
box (opt.): Simulations box, if not specified this is taken from ``atoms_a.box``
|
||||||
|
kind (opt.): Can be 'inter', 'intra' or None (default).
|
||||||
|
chunksize (opt.):
|
||||||
|
For large systems (N > 1000) the distaces have to be computed in chunks so the arrays
|
||||||
|
fit into memory, this parameter controlls the size of these chunks. It should be
|
||||||
|
as large as possible, depending on the available memory.
|
||||||
|
returnx (opt.): If True the x ordinate of the histogram is returned.
|
||||||
|
"""
|
||||||
|
assert bins is not None, 'Bins of the pair distribution have to be defined.'
|
||||||
|
assert kind in ['intra', 'inter', None], 'Argument kind must be one of the following: intra, inter, None.'
|
||||||
|
if box is None:
|
||||||
|
box = atoms_a.box.diagonal()
|
||||||
|
if atoms_b is None:
|
||||||
|
atoms_b = atoms_a
|
||||||
|
nr_of_atoms = len(atoms_a)
|
||||||
|
indices = np.triu_indices(nr_of_atoms, k=1)
|
||||||
|
else:
|
||||||
|
nr_a, dim = atoms_a.shape
|
||||||
|
nr_b, dim = atoms_b.shape
|
||||||
|
indices = np.array([(i, j) for i in range(nr_a) for j in range(nr_b)]).T
|
||||||
|
|
||||||
|
# compute the histogram in chunks for large systems
|
||||||
|
hist = 0
|
||||||
|
nr_of_samples = 0
|
||||||
|
for chunk in range(0, len(indices[0]), chunksize):
|
||||||
|
sl = slice(chunk, chunk + chunksize)
|
||||||
|
diff = pbc_diff(atoms_a[indices[0][sl]], atoms_b[indices[1][sl]], box)
|
||||||
|
dist = (diff**2).sum(axis=1)**0.5
|
||||||
|
if kind == 'intra':
|
||||||
|
mask = atoms_a.residue_ids[indices[0][sl]] == atoms_b.residue_ids[indices[1][sl]]
|
||||||
|
dist = dist[mask]
|
||||||
|
elif kind == 'inter':
|
||||||
|
mask = atoms_a.residue_ids[indices[0][sl]] != atoms_b.residue_ids[indices[1][sl]]
|
||||||
|
dist = dist[mask]
|
||||||
|
|
||||||
|
nr_of_samples += len(dist)
|
||||||
|
hist += np.histogram(dist, bins)[0]
|
||||||
|
|
||||||
|
volume = 4 / 3 * np.pi * (bins[1:]**3 - bins[:-1]**3)
|
||||||
|
density = nr_of_samples / np.prod(box)
|
||||||
|
res = hist / volume / density
|
||||||
|
if returnx:
|
||||||
|
return np.vstack((runningmean(bins, 2), res))
|
||||||
|
else:
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_tree_rdf(atoms_a, atoms_b=None, bins=None, box=None, exclude=0, returnx=False, **kwargs):
|
||||||
|
if box is None:
|
||||||
|
box = atoms_a.box.diagonal()
|
||||||
|
all_coords = pbc_points(pbc_diff(atoms_b,box=box), box, thickness=np.amax(bins)+0.1, center=0)
|
||||||
|
to_tree = spatial.cKDTree(all_coords)
|
||||||
|
dist = to_tree.query(pbc_diff(atoms_a,box=box),k=len(atoms_b), distance_upper_bound=np.amax(bins)+0.1)[0].flatten()
|
||||||
|
dist = dist[dist < np.inf]
|
||||||
|
hist = np.histogram(dist, bins)[0]
|
||||||
|
volume = 4/3*np.pi*(bins[1:]**3-bins[:-1]**3)
|
||||||
|
res = (hist) * np.prod(box) / volume / len(atoms_a) / (len(atoms_b)-exclude)
|
||||||
|
if returnx:
|
||||||
|
return np.vstack((runningmean(bins, 2), res))
|
||||||
|
else:
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_spm_rdf(atoms_a, atoms_b=None, bins=None, box=None, exclude=0, returnx=False, **kwargs):
|
||||||
|
if box is None:
|
||||||
|
box = atoms_a.box.diagonal()
|
||||||
|
all_coords = pbc_points(pbc_diff(atoms_b,box=box), box, thickness=np.amax(bins)+0.1, center=0)
|
||||||
|
to_tree = spatial.cKDTree(all_coords)
|
||||||
|
if all_coords.nbytes/1024**3 * len(atoms_a) < 2:
|
||||||
|
from_tree = spatial.cKDTree(pbc_diff(atoms_a,box=box))
|
||||||
|
dist = to_tree.sparse_distance_matrix(from_tree, max_distance=np.amax(bins)+0.1, output_type='ndarray')
|
||||||
|
dist = np.asarray(dist.tolist())[:,2]
|
||||||
|
hist = np.histogram(dist, bins)[0]
|
||||||
|
else:
|
||||||
|
chunksize = int(2 * len(atoms_a) / (all_coords.nbytes/1024**3 * len(atoms_a)))
|
||||||
|
hist = 0
|
||||||
|
for chunk in range(0, len(atoms_a), chunksize):
|
||||||
|
sl = slice(chunk, chunk + chunksize)
|
||||||
|
from_tree = spatial.cKDTree(pbc_diff(atoms_a[sl],box=box))
|
||||||
|
dist = to_tree.sparse_distance_matrix(from_tree, max_distance=np.amax(bins)+0.1, output_type='ndarray')
|
||||||
|
dist = np.asarray(dist.tolist())[:,2]
|
||||||
|
hist += np.histogram(dist, bins)[0]
|
||||||
|
|
||||||
|
volume = 4/3*np.pi*(bins[1:]**3-bins[:-1]**3)
|
||||||
|
res = (hist) * np.prod(box) / volume / len(atoms_a) / (len(atoms_b)-exclude)
|
||||||
|
if returnx:
|
||||||
|
return np.vstack((runningmean(bins, 2), res))
|
||||||
|
else:
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
@autosave_data(nargs=2, kwargs_keys=('to_coords','times'))
|
||||||
|
def fast_averaged_rdf(from_coords, bins, to_coords=None, times=10, exclude=0, **kwargs):
|
||||||
|
if to_coords is None:
|
||||||
|
to_coords = from_coords
|
||||||
|
exclude = 1
|
||||||
|
# first find timings for the different rdf functions
|
||||||
|
import time
|
||||||
|
# only consider sparse matrix for this condition
|
||||||
|
if (len(from_coords[0])*len(to_coords[0]) <= 3000 * 2000 ) & (len(from_coords[0])/len(to_coords[0]) > 5 ):
|
||||||
|
funcs = [rdf, pbc_tree_rdf, pbc_spm_rdf]
|
||||||
|
else:
|
||||||
|
funcs = [rdf, pbc_tree_rdf]
|
||||||
|
timings = []
|
||||||
|
for f in funcs:
|
||||||
|
start = time.time()
|
||||||
|
f(from_coords[0], atoms_b=to_coords[0], bins=bins, box=np.diag(from_coords[0].box))
|
||||||
|
end = time.time()
|
||||||
|
timings.append(end-start)
|
||||||
|
timings = np.array(timings)
|
||||||
|
timings[0] = 2*timings[0] # statistics for the other functions is twice as good per frame
|
||||||
|
logger.debug('rdf function timings: ' + str(timings))
|
||||||
|
rdffunc = funcs[np.argmin(timings)]
|
||||||
|
logger.debug('rdf function used: ' + str(rdffunc))
|
||||||
|
if rdffunc == rdf:
|
||||||
|
times = times*2 # duplicate times for same statistics
|
||||||
|
|
||||||
|
frames = np.array(range(0, len(from_coords), int(len(from_coords)/times)))[:times]
|
||||||
|
out = np.zeros(len(bins)-1)
|
||||||
|
for j, i in enumerate(frames):
|
||||||
|
logger.debug('multi_radial_pair_distribution: %d/%d', j, len(frames))
|
||||||
|
out += rdffunc(from_coords[i], to_coords[i], bins, box=np.diag(from_coords[i].box), exclude=exclude)
|
||||||
|
return out/len(frames)
|
||||||
|
|
||||||
|
|
||||||
|
def distance_distribution(atoms, bins):
|
||||||
|
connection_vectors = atoms[:-1, :] - atoms[1:, :]
|
||||||
|
connection_lengths = (connection_vectors**2).sum(axis=1)**.5
|
||||||
|
return np.histogram(connection_lengths, bins)[0]
|
||||||
|
|
||||||
|
|
||||||
|
def tetrahedral_order(atoms, reference_atoms=None):
|
||||||
|
if reference_atoms is None:
|
||||||
|
reference_atoms = atoms
|
||||||
|
indices = next_neighbors(reference_atoms, query_atoms=atoms, number_of_neighbors=4)
|
||||||
|
neighbors = reference_atoms[indices]
|
||||||
|
neighbors_1, neighbors_2, neighbors_3, neighbors_4 = \
|
||||||
|
neighbors[:, 0, :], neighbors[:, 1, :], neighbors[:, 2, :], neighbors[:, 3, :]
|
||||||
|
|
||||||
|
# Connection vectors
|
||||||
|
neighbors_1 -= atoms
|
||||||
|
neighbors_2 -= atoms
|
||||||
|
neighbors_3 -= atoms
|
||||||
|
neighbors_4 -= atoms
|
||||||
|
|
||||||
|
# Normed Connection vectors
|
||||||
|
neighbors_1 /= np.linalg.norm(neighbors_1, axis=-1).reshape(-1, 1)
|
||||||
|
neighbors_2 /= np.linalg.norm(neighbors_2, axis=-1).reshape(-1, 1)
|
||||||
|
neighbors_3 /= np.linalg.norm(neighbors_3, axis=-1).reshape(-1, 1)
|
||||||
|
neighbors_4 /= np.linalg.norm(neighbors_4, axis=-1).reshape(-1, 1)
|
||||||
|
|
||||||
|
a_1_2 = ((neighbors_1 * neighbors_2).sum(axis=1) + 1 / 3)**2
|
||||||
|
a_1_3 = ((neighbors_1 * neighbors_3).sum(axis=1) + 1 / 3)**2
|
||||||
|
a_1_4 = ((neighbors_1 * neighbors_4).sum(axis=1) + 1 / 3)**2
|
||||||
|
|
||||||
|
a_2_3 = ((neighbors_2 * neighbors_3).sum(axis=1) + 1 / 3)**2
|
||||||
|
a_2_4 = ((neighbors_2 * neighbors_4).sum(axis=1) + 1 / 3)**2
|
||||||
|
|
||||||
|
a_3_4 = ((neighbors_3 * neighbors_4).sum(axis=1) + 1 / 3)**2
|
||||||
|
|
||||||
|
q = 1 - 3 / 8 * (a_1_2 + a_1_3 + a_1_4 + a_2_3 + a_2_4 + a_3_4)
|
||||||
|
|
||||||
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
def tetrahedral_order_distribution(atoms, reference_atoms=None, bins=None):
|
||||||
|
assert bins is not None, 'Bin edges of the distribution have to be specified.'
|
||||||
|
Q = tetrahedral_order(atoms, reference_atoms=reference_atoms)
|
||||||
|
return np.histogram(Q, bins=bins)[0]
|
||||||
|
|
||||||
|
|
||||||
|
def radial_density(atoms, bins, symmetry_axis=(0, 0, 1), origin=(0, 0, 0), height=1, returnx=False):
|
||||||
|
"""
|
||||||
|
Calculate the radial density distribution.
|
||||||
|
|
||||||
|
This function is meant to be used with time_average.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
atoms:
|
||||||
|
Set of coordinates.
|
||||||
|
bins:
|
||||||
|
Bin specification that is passed to numpy.histogram. This needs to be
|
||||||
|
a list of bin edges if the function is used within time_average.
|
||||||
|
symmetry_axis (opt.):
|
||||||
|
Vector of the symmetry axis, around which the radial density is calculated,
|
||||||
|
default is z-axis.
|
||||||
|
origin (opt.):
|
||||||
|
Origin of the rotational symmetry, e.g. center of the pore.
|
||||||
|
height (opt.):
|
||||||
|
Height of the pore, necessary for correct normalization of the density.
|
||||||
|
returnx (opt.):
|
||||||
|
If True, the x ordinate of the distribution is returned.
|
||||||
|
"""
|
||||||
|
cartesian = rotate_axis(atoms - origin, symmetry_axis)
|
||||||
|
radius, _ = polar_coordinates(cartesian[:, 0], cartesian[:, 1])
|
||||||
|
hist = np.histogram(radius, bins=bins)[0]
|
||||||
|
volume = np.pi * (bins[1:]**2 - bins[:-1]**2) * height
|
||||||
|
res = hist / volume
|
||||||
|
if returnx:
|
||||||
|
return np.vstack((runningmean(bins, 2), res))
|
||||||
|
else:
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def shell_density(atoms, shell_radius, bins, shell_thickness=0.5,
|
||||||
|
symmetry_axis=(0, 0, 1), origin=(0, 0, 0)):
|
||||||
|
"""
|
||||||
|
Compute the density distribution on a cylindrical shell.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
atoms: The coordinates of the atoms
|
||||||
|
shell_radius: Inner radius of the shell
|
||||||
|
bins: Histogramm bins, this has to be a two-dimensional list of bins: [angle, z]
|
||||||
|
shell_thickness (opt.): Thicknes of the shell, default is 0.5
|
||||||
|
symmetry_axis (opt.): The symmtery axis of the pore, the coordinates will be
|
||||||
|
rotated such that this axis is the z-axis
|
||||||
|
origin (opt.): Origin of the pore, the coordinates will be moved such that this
|
||||||
|
is the new origin.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Two-dimensional density distribution of the atoms in the defined shell.
|
||||||
|
"""
|
||||||
|
cartesian = rotate_axis(atoms-origin, symmetry_axis)
|
||||||
|
radius, theta = polar_coordinates(cartesian[:, 0], cartesian[:, 1])
|
||||||
|
shell_indices = (shell_radius <= radius) & (radius <= shell_radius + shell_thickness)
|
||||||
|
hist = np.histogram2d(theta[shell_indices], cartesian[shell_indices, 2], bins)[0]
|
||||||
|
|
||||||
|
return hist
|
||||||
|
|
||||||
|
|
||||||
|
def spatial_density(atoms, bins, weights=None):
|
||||||
|
"""
|
||||||
|
Compute the spatial density distribution.
|
||||||
|
"""
|
||||||
|
density, _ = np.histogramdd(atoms, bins=bins, weights=weights)
|
||||||
|
return density
|
||||||
|
|
||||||
|
|
||||||
|
def mixing_ratio_distribution(atoms_a, atoms_b, bins_ratio, bins_density,
|
||||||
|
weights_a=None, weights_b=None, weights_ratio=None):
|
||||||
|
"""
|
||||||
|
Compute the distribution of the mixing ratio of two sets of atoms.
|
||||||
|
"""
|
||||||
|
|
||||||
|
density_a, _ = time_average
|
||||||
|
density_b, _ = np.histogramdd(atoms_b, bins=bins_density, weights=weights_b)
|
||||||
|
mixing_ratio = density_a/(density_a + density_b)
|
||||||
|
good_inds = (density_a != 0) & (density_b != 0)
|
||||||
|
hist, _ = np.histogram(mixing_ratio[good_inds], bins=bins_ratio, weights=weights_ratio)
|
||||||
|
return hist
|
||||||
|
|
||||||
|
|
||||||
|
def next_neighbor_distribution(atoms, reference=None, number_of_neighbors=4, bins=None, normed=True):
|
||||||
|
"""
|
||||||
|
Compute the distribution of next neighbors with the same residue name.
|
||||||
|
"""
|
||||||
|
assert bins is not None, 'Bins have to be specified.'
|
||||||
|
if reference is None:
|
||||||
|
reference = atoms
|
||||||
|
nn = next_neighbors(reference, query_atoms=atoms, number_of_neighbors=number_of_neighbors)
|
||||||
|
resname_nn = reference.residue_names[nn]
|
||||||
|
count_nn = (resname_nn == atoms.residue_names.reshape(-1, 1)).sum(axis=1)
|
||||||
|
return np.histogram(count_nn, bins=bins, normed=normed)[0]
|
38
mdevaluate/functions.py
Normal file
38
mdevaluate/functions.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def kww(t, A, τ, β):
|
||||||
|
return A * np.exp(-(t / τ)**β)
|
||||||
|
|
||||||
|
|
||||||
|
def kww_1e(A, τ, β):
|
||||||
|
return τ * (-np.log(1 / (np.e * A)))**(1 / β)
|
||||||
|
|
||||||
|
|
||||||
|
def cole_davidson(w, A, b, t0):
|
||||||
|
P = np.arctan(w * t0)
|
||||||
|
return A * np.cos(P)**b * np.sin(b * P)
|
||||||
|
|
||||||
|
|
||||||
|
def cole_cole(w, A, b, t0):
|
||||||
|
return A * (w * t0)**b * np.sin(np.pi * b / 2) / (1 + 2 * (w * t0)**b * np.cos(np.pi * b / 2) + (w * t0)**(2 * b))
|
||||||
|
|
||||||
|
|
||||||
|
def havriliak_negami(ω, A, β, α, τ):
|
||||||
|
r"""
|
||||||
|
Imaginary part of the Havriliak-Negami function.
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
\chi_{HN}(\omega) = \Im\left(\frac{A}{(1 + (i\omega\tau)^\alpha)^\beta}\right)
|
||||||
|
"""
|
||||||
|
return -(A / (1 + (1j * ω * τ)**α)**β).imag
|
||||||
|
|
||||||
|
|
||||||
|
# fits decay of correlation times, e.g. with distance to pore walls
|
||||||
|
def colen(d, X, t8, A):
|
||||||
|
return t8 * np.exp(A*np.exp(-d/X))
|
||||||
|
|
||||||
|
|
||||||
|
# fits decay of the plateau height of the overlap function, e.g. with distance to pore walls
|
||||||
|
def colenQ(d, X, Qb, g):
|
||||||
|
return (1-Qb)*np.exp(-(d/X)**g)+Qb
|
26
mdevaluate/logging.py
Normal file
26
mdevaluate/logging.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('mdevaluate')
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
stream_handler = logging.StreamHandler()
|
||||||
|
stream_handler.setLevel(logging.INFO)
|
||||||
|
logger.addHandler(stream_handler)
|
||||||
|
|
||||||
|
formatter = logging.Formatter('{levelname[0]}{levelname[1]}{levelname[2]}[{asctime}]:{funcName}: {message}', style='{')
|
||||||
|
stream_handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
|
||||||
|
def setlevel(level, file=None):
|
||||||
|
"""
|
||||||
|
Change the level of logging. If `file` is specified, logs are written to this file.
|
||||||
|
"""
|
||||||
|
if isinstance(level, str):
|
||||||
|
level = getattr(logging, level.upper())
|
||||||
|
logger.setLevel(level)
|
||||||
|
if file is not None:
|
||||||
|
handler = logging.FileHandler(file)
|
||||||
|
handler.setLevel(level)
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
else:
|
||||||
|
stream_handler.setLevel(level)
|
260
mdevaluate/pbc.py
Normal file
260
mdevaluate/pbc.py
Normal file
@ -0,0 +1,260 @@
|
|||||||
|
from collections import OrderedDict
|
||||||
|
import os
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from scipy.spatial import cKDTree
|
||||||
|
from itertools import product
|
||||||
|
|
||||||
|
from .logging import logger
|
||||||
|
|
||||||
|
def pbc_diff_old(v1, v2, box):
|
||||||
|
"""
|
||||||
|
Calculate the difference of two vestors, considering optional boundary conditions.
|
||||||
|
"""
|
||||||
|
if box is None:
|
||||||
|
v = v1 - v2
|
||||||
|
else:
|
||||||
|
v = v1 % box - v2 % box
|
||||||
|
v -= (v > box / 2) * box
|
||||||
|
v += (v < -box / 2) * box
|
||||||
|
|
||||||
|
return v
|
||||||
|
|
||||||
|
def pbc_diff(v1, v2=None, box=None):
|
||||||
|
if box is None:
|
||||||
|
out = v1 - v2
|
||||||
|
elif len(getattr(box, 'shape', [])) == 1:
|
||||||
|
out = pbc_diff_rect(v1, v2, box)
|
||||||
|
elif len(getattr(box, 'shape', [])) == 2:
|
||||||
|
out = pbc_diff_tric(v1, v2, box)
|
||||||
|
else: raise NotImplementedError("cannot handle box")
|
||||||
|
return out
|
||||||
|
|
||||||
|
def pbc_diff_rect(v1, v2, box):
|
||||||
|
"""
|
||||||
|
Calculate the difference of two vectors, considering periodic boundary conditions.
|
||||||
|
"""
|
||||||
|
if v2 is None:
|
||||||
|
v = v1
|
||||||
|
else:
|
||||||
|
v = v1 -v2
|
||||||
|
|
||||||
|
s = v / box
|
||||||
|
v = box * (s - s.round())
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_diff_tric(v1, v2=None, box=None):
|
||||||
|
"""
|
||||||
|
difference vector for arbitrary pbc
|
||||||
|
|
||||||
|
Args:
|
||||||
|
box_matrix: CoordinateFrame.box
|
||||||
|
"""
|
||||||
|
if len(box.shape) == 1: box = np.diag(box)
|
||||||
|
if v1.shape == (3,): v1 = v1.reshape((1,3)) #quick 'n dirty
|
||||||
|
if v2.shape == (3,): v2 = v2.reshape((1,3))
|
||||||
|
if box is not None:
|
||||||
|
r3 = np.subtract(v1, v2)
|
||||||
|
r2 = np.subtract(r3, (np.rint(np.divide(r3[:,2],box[2][2])))[:,np.newaxis] * box[2][np.newaxis,:])
|
||||||
|
r1 = np.subtract(r2, (np.rint(np.divide(r2[:,1],box[1][1])))[:,np.newaxis] * box[1][np.newaxis,:])
|
||||||
|
v = np.subtract(r1, (np.rint(np.divide(r1[:,0],box[0][0])))[:,np.newaxis] * box[0][np.newaxis,:])
|
||||||
|
else:
|
||||||
|
v = v1 - v2
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_dist(a1,a2,box = None):
|
||||||
|
return ((pbc_diff(a1,a2,box)**2).sum(axis=1))**0.5
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_extend(c, box):
|
||||||
|
"""
|
||||||
|
in: c is frame, box is frame.box
|
||||||
|
out: all atoms in frame and their perio. image (shape => array(len(c)*27,3))
|
||||||
|
"""
|
||||||
|
c=np.asarray(c)
|
||||||
|
if c.shape == (3,): c = c.reshape((1,3)) #quick 'n dirty
|
||||||
|
comb = np.array([np.asarray(i) for i in product([0,-1,1],[0,-1,1],[0,-1,1])])
|
||||||
|
b_matrices = comb[:,:,np.newaxis]*box[np.newaxis,:,:]
|
||||||
|
b_vectors = b_matrices.sum(axis=1)[np.newaxis,:,:]
|
||||||
|
return (c[:,np.newaxis,:]+b_vectors)
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_kdtree(v1,box, leafsize = 32, compact_nodes = False, balanced_tree = False):
|
||||||
|
"""
|
||||||
|
kd_tree with periodic images
|
||||||
|
box - whole matrix
|
||||||
|
rest: optional optimization
|
||||||
|
"""
|
||||||
|
r0 = cKDTree(pbc_extend(v1,box).reshape((-1,3)),leafsize ,compact_nodes ,balanced_tree)
|
||||||
|
return r0
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_kdtree_query(v1,v2,box,n = 1):
|
||||||
|
"""
|
||||||
|
kd_tree query with periodic images
|
||||||
|
"""
|
||||||
|
r0, r1 = pbc_kdtree(v1,box).query(v2,n)
|
||||||
|
r1 = r1 // 27
|
||||||
|
return r0, r1
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_backfold_rect(act_frame,box_matrix):
|
||||||
|
"""
|
||||||
|
mimics "trjconv ... -pbc atom -ur rect"
|
||||||
|
|
||||||
|
folds coords of act_frame in cuboid
|
||||||
|
|
||||||
|
"""
|
||||||
|
af=np.asarray(act_frame)
|
||||||
|
if af.shape == (3,): act_frame = act_frame.reshape((1,3)) #quick 'n dirty
|
||||||
|
b = box_matrix
|
||||||
|
c = np.diag(b)/2
|
||||||
|
af = pbc_diff(np.zeros((1,3)),af-c,b)
|
||||||
|
return af + c
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_backfold_compact(act_frame,box_matrix):
|
||||||
|
"""
|
||||||
|
mimics "trjconv ... -pbc atom -ur compact"
|
||||||
|
|
||||||
|
folds coords of act_frame in wigner-seitz-cell (e.g. dodecahedron)
|
||||||
|
"""
|
||||||
|
c = act_frame
|
||||||
|
box = box_matrix
|
||||||
|
ctr = box.sum(0)/2
|
||||||
|
c=np.asarray(c)
|
||||||
|
shape = c.shape
|
||||||
|
if shape == (3,):
|
||||||
|
c = c.reshape((1,3))
|
||||||
|
shape = (1,3) #quick 'n dirty
|
||||||
|
comb = np.array([np.asarray(i) for i in product([0,-1,1],[0,-1,1],[0,-1,1])])
|
||||||
|
b_matrices = comb[:,:,np.newaxis]*box[np.newaxis,:,:]
|
||||||
|
b_vectors = b_matrices.sum(axis=1)[np.newaxis,:,:]
|
||||||
|
sc = c[:,np.newaxis,:]+b_vectors
|
||||||
|
w = np.argsort((((sc)-ctr)**2).sum(2),1)[:,0]
|
||||||
|
return sc[range(shape[0]),w]
|
||||||
|
|
||||||
|
|
||||||
|
# Parameter used to switch reference position for whole molecules
|
||||||
|
# 'com': use center of mass of each molecule
|
||||||
|
# 'simple': use first atom in each molecule
|
||||||
|
WHOLEMODE = 'com'
|
||||||
|
|
||||||
|
fname = os.path.expanduser('~/.mdevaluate/WHOLEMODE')
|
||||||
|
if os.path.exists(fname):
|
||||||
|
with open(fname) as f:
|
||||||
|
WHOLEMODE = f.read().strip()
|
||||||
|
logger.info('Setting WHOLEMODE to %s, according to file ~/.mdevaluate/WHOLEMODE', WHOLEMODE)
|
||||||
|
|
||||||
|
def whole(frame):
|
||||||
|
"""
|
||||||
|
Apply ``-pbc whole`` to a CoordinateFrame.
|
||||||
|
"""
|
||||||
|
residue_ids = frame.coordinates.atom_subset.residue_ids
|
||||||
|
box = frame.box.diagonal()
|
||||||
|
|
||||||
|
if WHOLEMODE == 'com':
|
||||||
|
logger.debug('Using COM as reference for whole.')
|
||||||
|
coms = np.array([
|
||||||
|
np.bincount(residue_ids, weights=c * frame.masses)[1:] / np.bincount(residue_ids, weights=frame.masses)[1:]
|
||||||
|
for c in frame.T
|
||||||
|
]).T[residue_ids - 1]
|
||||||
|
|
||||||
|
else:
|
||||||
|
# make sure, residue_ids are sorted, then determine indices at which the res id changes
|
||||||
|
# kind='stable' assures that any existent ordering is preserved
|
||||||
|
logger.debug('Using first atom as reference for whole.')
|
||||||
|
sort_ind = residue_ids.argsort(kind='stable')
|
||||||
|
i = np.concatenate([[0], np.where(np.diff(residue_ids[sort_ind]) > 0)[0] + 1])
|
||||||
|
coms = frame[sort_ind[i]][residue_ids - 1]
|
||||||
|
|
||||||
|
|
||||||
|
cor = np.zeros_like(frame)
|
||||||
|
cd = frame - coms
|
||||||
|
n, d = np.where(cd > box / 2 * 0.9)
|
||||||
|
cor[n, d] = -box[d]
|
||||||
|
n, d = np.where(cd < -box / 2 * 0.9)
|
||||||
|
cor[n, d] = box[d]
|
||||||
|
|
||||||
|
# this fix is only necessary when COM is the reference
|
||||||
|
if WHOLEMODE == 'com':
|
||||||
|
duomask = np.bincount(residue_ids)[1:][residue_ids - 1] == 2
|
||||||
|
if np.any(duomask):
|
||||||
|
duomask[::2] = False
|
||||||
|
cor[duomask] = 0
|
||||||
|
|
||||||
|
return frame + cor
|
||||||
|
|
||||||
|
|
||||||
|
NOJUMP_CACHESIZE = 128
|
||||||
|
|
||||||
|
|
||||||
|
def nojump(frame, usecache=True):
|
||||||
|
"""
|
||||||
|
Return the nojump coordinates of a frame, based on a jump matrix.
|
||||||
|
"""
|
||||||
|
selection = frame.selection
|
||||||
|
reader = frame.coordinates.frames
|
||||||
|
if usecache:
|
||||||
|
if not hasattr(reader, '_nojump_cache'):
|
||||||
|
reader._nojump_cache = OrderedDict()
|
||||||
|
# make sure to use absolute (non negative) index
|
||||||
|
abstep = frame.step % len(frame.coordinates)
|
||||||
|
i0s = [x for x in reader._nojump_cache if x <= abstep]
|
||||||
|
if len(i0s) > 0:
|
||||||
|
i0 = max(i0s)
|
||||||
|
delta = reader._nojump_cache[i0]
|
||||||
|
i0 += 1
|
||||||
|
else:
|
||||||
|
i0 = 0
|
||||||
|
delta = 0
|
||||||
|
|
||||||
|
delta = delta + np.array(np.vstack(
|
||||||
|
[m[i0:abstep + 1].sum(axis=0) for m in reader.nojump_matrixes]
|
||||||
|
).T) * frame.box.diagonal()
|
||||||
|
|
||||||
|
reader._nojump_cache[abstep] = delta
|
||||||
|
while len(reader._nojump_cache) > NOJUMP_CACHESIZE:
|
||||||
|
reader._nojump_cache.popitem(last=False)
|
||||||
|
delta = delta[selection, :]
|
||||||
|
else:
|
||||||
|
delta = np.array(np.vstack(
|
||||||
|
[m[:frame.step + 1, selection].sum(axis=0) for m in reader.nojump_matrixes]
|
||||||
|
).T) * frame.box.diagonal()
|
||||||
|
return frame - delta
|
||||||
|
|
||||||
|
|
||||||
|
def pbc_points(coordinates, box, thickness=0, index=False, inclusive=True, center=None):
|
||||||
|
"""
|
||||||
|
Returns the points their first periodic images. Does not fold them back into the box.
|
||||||
|
Thickness 0 means all 27 boxes. Positive means the box+thickness. Negative values mean that less than the box is returned.
|
||||||
|
index=True also returns the indices with indices of images being their originals values.
|
||||||
|
inclusive=False returns only images, does not work with thickness <= 0
|
||||||
|
"""
|
||||||
|
if center is None:
|
||||||
|
center = box/2
|
||||||
|
allcoordinates = np.copy(coordinates)
|
||||||
|
indices = np.tile(np.arange(len(coordinates)),(27))
|
||||||
|
for x in range(-1, 2, 1):
|
||||||
|
for y in range(-1, 2, 1):
|
||||||
|
for z in range(-1, 2, 1):
|
||||||
|
vv = np.array([x, y, z], dtype=float)
|
||||||
|
if not (vv == 0).all() :
|
||||||
|
allcoordinates = np.concatenate((allcoordinates, coordinates + vv*box), axis=0)
|
||||||
|
|
||||||
|
if thickness != 0:
|
||||||
|
mask = np.all(allcoordinates < center+box/2+thickness, axis=1)
|
||||||
|
allcoordinates = allcoordinates[mask]
|
||||||
|
indices = indices[mask]
|
||||||
|
mask = np.all(allcoordinates > center-box/2-thickness, axis=1)
|
||||||
|
allcoordinates = allcoordinates[mask]
|
||||||
|
indices = indices[mask]
|
||||||
|
if not inclusive and thickness > 0:
|
||||||
|
allcoordinates = allcoordinates[len(coordinates):]
|
||||||
|
indices = indices[len(coordinates):]
|
||||||
|
if index:
|
||||||
|
return (allcoordinates, indices)
|
||||||
|
return allcoordinates
|
283
mdevaluate/reader.py
Executable file
283
mdevaluate/reader.py
Executable file
@ -0,0 +1,283 @@
|
|||||||
|
"""
|
||||||
|
Module that provides different readers for trajectory files.
|
||||||
|
|
||||||
|
It also provides a common interface layer between the file IO packages,
|
||||||
|
namely pygmx and mdanalysis, and mdevaluate.
|
||||||
|
"""
|
||||||
|
from .checksum import checksum
|
||||||
|
from .logging import logger
|
||||||
|
from . import atoms
|
||||||
|
|
||||||
|
from functools import lru_cache
|
||||||
|
from collections import namedtuple
|
||||||
|
import os
|
||||||
|
from os import path
|
||||||
|
from array import array
|
||||||
|
from zipfile import BadZipFile
|
||||||
|
import builtins
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import MDAnalysis as mdanalysis
|
||||||
|
from scipy import sparse
|
||||||
|
from dask import delayed, __version__ as DASK_VERSION
|
||||||
|
|
||||||
|
|
||||||
|
class NojumpError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class WrongTopologyError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def open_with_mdanalysis(topology, trajectory, cached=False):
|
||||||
|
"""Open a the topology and trajectory with mdanalysis."""
|
||||||
|
uni = mdanalysis.Universe(topology, trajectory, convert_units=False)
|
||||||
|
if cached is not False:
|
||||||
|
if cached is True:
|
||||||
|
maxsize = 128
|
||||||
|
else:
|
||||||
|
maxsize = cached
|
||||||
|
reader = CachedReader(uni.trajectory, maxsize)
|
||||||
|
else:
|
||||||
|
reader = BaseReader(uni.trajectory)
|
||||||
|
reader.universe = uni
|
||||||
|
if topology.endswith('.tpr'):
|
||||||
|
atms = atoms.Atoms(
|
||||||
|
np.stack((uni.atoms.resids, uni.atoms.resnames, uni.atoms.names), axis=1),
|
||||||
|
charges=uni.atoms.charges, masses=uni.atoms.masses
|
||||||
|
).subset()
|
||||||
|
elif topology.endswith('.gro'):
|
||||||
|
atms = atoms.Atoms(
|
||||||
|
np.stack((uni.atoms.resids, uni.atoms.resnames, uni.atoms.names), axis=1),
|
||||||
|
charges=None, masses=None
|
||||||
|
).subset()
|
||||||
|
else:
|
||||||
|
raise WrongTopologyError('Topology file should end with ".tpr" or ".gro"')
|
||||||
|
return atms, reader
|
||||||
|
|
||||||
|
|
||||||
|
def is_writeable(fname):
|
||||||
|
"""Test if a directory is actually writeable, by writing a temporary file."""
|
||||||
|
fdir = os.path.dirname(fname)
|
||||||
|
ftmp = os.path.join(fdir, str(np.random.randint(999999999)))
|
||||||
|
while os.path.exists(ftmp):
|
||||||
|
ftmp = os.path.join(fdir, str(np.random.randint(999999999)))
|
||||||
|
|
||||||
|
if os.access(fdir, os.W_OK):
|
||||||
|
try:
|
||||||
|
with builtins.open(ftmp, 'w'):
|
||||||
|
pass
|
||||||
|
os.remove(ftmp)
|
||||||
|
return True
|
||||||
|
except PermissionError:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def nojump_filename(reader):
|
||||||
|
directory, fname = path.split(reader.filename)
|
||||||
|
fname = path.join(directory, '.{}.nojump.npz'.format(fname))
|
||||||
|
if os.path.exists(fname) or is_writeable(directory):
|
||||||
|
return fname
|
||||||
|
else:
|
||||||
|
fname = os.path.join(
|
||||||
|
os.path.join(os.environ['HOME'], '.mdevaluate/nojump'),
|
||||||
|
directory.lstrip('/'),
|
||||||
|
'.{}.nojump.npz'.format(fname)
|
||||||
|
)
|
||||||
|
logger.info('Saving nojump to {}, since original location is not writeable.'.format(fname))
|
||||||
|
os.makedirs(os.path.dirname(fname), exist_ok=True)
|
||||||
|
return fname
|
||||||
|
|
||||||
|
|
||||||
|
CSR_ATTRS = ('data', 'indices', 'indptr')
|
||||||
|
NOJUMP_MAGIC = 2016
|
||||||
|
|
||||||
|
|
||||||
|
def parse_jumps(trajectory):
|
||||||
|
prev = trajectory[0].whole
|
||||||
|
box = prev.box.diagonal()
|
||||||
|
SparseData = namedtuple('SparseData', ['data', 'row', 'col'])
|
||||||
|
jump_data = (
|
||||||
|
SparseData(data=array('b'), row=array('l'), col=array('l')),
|
||||||
|
SparseData(data=array('b'), row=array('l'), col=array('l')),
|
||||||
|
SparseData(data=array('b'), row=array('l'), col=array('l'))
|
||||||
|
)
|
||||||
|
|
||||||
|
for i, curr in enumerate(trajectory):
|
||||||
|
if i % 500 == 0:
|
||||||
|
logger.debug('Parse jumps Step: %d', i)
|
||||||
|
delta = ((curr - prev) / box).round().astype(np.int8)
|
||||||
|
prev = curr
|
||||||
|
for d in range(3):
|
||||||
|
col, = np.where(delta[:, d] != 0)
|
||||||
|
jump_data[d].col.extend(col)
|
||||||
|
jump_data[d].row.extend([i] * len(col))
|
||||||
|
jump_data[d].data.extend(delta[col, d])
|
||||||
|
|
||||||
|
return jump_data
|
||||||
|
|
||||||
|
|
||||||
|
def generate_nojump_matrixes(trajectory):
|
||||||
|
"""
|
||||||
|
Create the matrixes with pbc jumps for a trajectory.
|
||||||
|
"""
|
||||||
|
logger.info('generate Nojump Matrixes for: {}'.format(trajectory))
|
||||||
|
|
||||||
|
jump_data = parse_jumps(trajectory)
|
||||||
|
N = len(trajectory)
|
||||||
|
M = len(trajectory[0])
|
||||||
|
|
||||||
|
trajectory.frames.nojump_matrixes = tuple(
|
||||||
|
sparse.csr_matrix((np.array(m.data), (m.row, m.col)), shape=(N, M)) for m in jump_data
|
||||||
|
)
|
||||||
|
save_nojump_matrixes(trajectory.frames)
|
||||||
|
|
||||||
|
|
||||||
|
def save_nojump_matrixes(reader, matrixes=None):
|
||||||
|
if matrixes is None:
|
||||||
|
matrixes = reader.nojump_matrixes
|
||||||
|
data = {'checksum': checksum(NOJUMP_MAGIC, checksum(reader))}
|
||||||
|
for d, mat in enumerate(matrixes):
|
||||||
|
data['shape'] = mat.shape
|
||||||
|
for attr in CSR_ATTRS:
|
||||||
|
data['{}_{}'.format(attr, d)] = getattr(mat, attr)
|
||||||
|
|
||||||
|
np.savez(nojump_filename(reader), **data)
|
||||||
|
|
||||||
|
|
||||||
|
def load_nojump_matrixes(reader):
|
||||||
|
zipname = nojump_filename(reader)
|
||||||
|
try:
|
||||||
|
data = np.load(zipname, allow_pickle=True)
|
||||||
|
except (AttributeError, BadZipFile, OSError):
|
||||||
|
# npz-files can be corrupted, propably a bug for big arrays saved with savez_compressed?
|
||||||
|
logger.info('Removing zip-File: %s', zipname)
|
||||||
|
os.remove(nojump_filename(reader))
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
if data['checksum'] == checksum(NOJUMP_MAGIC, checksum(reader)):
|
||||||
|
reader.nojump_matrixes = tuple(
|
||||||
|
sparse.csr_matrix(
|
||||||
|
tuple(data['{}_{}'.format(attr, d)] for attr in CSR_ATTRS),
|
||||||
|
shape=data['shape']
|
||||||
|
)
|
||||||
|
for d in range(3)
|
||||||
|
)
|
||||||
|
logger.info('Loaded Nojump Matrixes: {}'.format(nojump_filename(reader)))
|
||||||
|
else:
|
||||||
|
logger.info('Invlaid Nojump Data: {}'.format(nojump_filename(reader)))
|
||||||
|
except KeyError:
|
||||||
|
logger.info('Removing zip-File: %s', zipname)
|
||||||
|
os.remove(nojump_filename(reader))
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def correct_nojump_matrixes_for_whole(trajectory):
|
||||||
|
reader = trajectory.frames
|
||||||
|
frame = trajectory[0]
|
||||||
|
box = frame.box.diagonal()
|
||||||
|
cor = ((frame - frame.whole) / box).round().astype(np.int8)
|
||||||
|
for d in range(3):
|
||||||
|
reader.nojump_matrixes[d][0] = cor[:, d]
|
||||||
|
save_nojump_matrixes(reader)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseReader:
|
||||||
|
"""Base class for trajectory readers."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def filename(self):
|
||||||
|
return self.rd.filename
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nojump_matrixes(self):
|
||||||
|
if self._nojump_matrixes is None:
|
||||||
|
raise NojumpError('Nojump Data not available: {}'.format(self.filename))
|
||||||
|
return self._nojump_matrixes
|
||||||
|
|
||||||
|
@nojump_matrixes.setter
|
||||||
|
def nojump_matrixes(self, mats):
|
||||||
|
self._nojump_matrixes = mats
|
||||||
|
|
||||||
|
def __init__(self, rd):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
filename: Trajectory file to open.
|
||||||
|
reindex (bool, opt.): If True, regenerate the index file if necessary.
|
||||||
|
"""
|
||||||
|
self.rd = rd
|
||||||
|
self._nojump_matrixes = None
|
||||||
|
if path.exists(nojump_filename(self)):
|
||||||
|
load_nojump_matrixes(self)
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
return self.rd[item]
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.rd)
|
||||||
|
|
||||||
|
def __checksum__(self):
|
||||||
|
if hasattr(self.rd, 'cache'):
|
||||||
|
# Has an pygmx reader
|
||||||
|
return checksum(self.filename, str(self.rd.cache))
|
||||||
|
elif hasattr(self.rd, '_xdr'):
|
||||||
|
# Has an mdanalysis reader
|
||||||
|
cache = array('L', self.rd._xdr.offsets.tobytes())
|
||||||
|
return checksum(self.filename, str(cache))
|
||||||
|
|
||||||
|
|
||||||
|
class CachedReader(BaseReader):
|
||||||
|
"""A reader that has a least-recently-used cache for frames."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cache_info(self):
|
||||||
|
"""Get Information about the lru cache."""
|
||||||
|
return self._get_item.cache_info()
|
||||||
|
|
||||||
|
def clear_cache(self):
|
||||||
|
"""Clear the cache of the frames."""
|
||||||
|
self._get_item.cache_clear()
|
||||||
|
|
||||||
|
def __init__(self, rd, maxsize):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
filename (str): Trajectory file that will be opened.
|
||||||
|
maxsize: Maximum size of the lru_cache or None for infinite cache.
|
||||||
|
"""
|
||||||
|
super().__init__(rd)
|
||||||
|
self._get_item = lru_cache(maxsize=maxsize)(self._get_item)
|
||||||
|
|
||||||
|
def _get_item(self, item):
|
||||||
|
"""Buffer function for lru_cache, since __getitem__ can not be cached."""
|
||||||
|
return super().__getitem__(item)
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
return self._get_item(item)
|
||||||
|
|
||||||
|
|
||||||
|
class DelayedReader(BaseReader):
|
||||||
|
|
||||||
|
@property
|
||||||
|
def filename(self):
|
||||||
|
if self.rd is not None:
|
||||||
|
return self.rd.filename
|
||||||
|
else:
|
||||||
|
return self._filename
|
||||||
|
|
||||||
|
def __init__(self, filename, reindex=False, ignore_index_timestamps=False):
|
||||||
|
super().__init__(filename, reindex=False, ignore_index_timestamps=False)
|
||||||
|
self.natoms = len(self.rd[0].coordinates)
|
||||||
|
self.cache = self.rd.cache
|
||||||
|
self._filename = self.rd.filename
|
||||||
|
self.rd = None
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.cache)
|
||||||
|
|
||||||
|
def _get_item(self, frame):
|
||||||
|
return read_xtcframe_delayed(self.filename, self.cache[frame], self.natoms)
|
||||||
|
|
||||||
|
def __getitem__(self, frame):
|
||||||
|
return self._get_item(frame)
|
||||||
|
|
366
mdevaluate/utils.py
Normal file
366
mdevaluate/utils.py
Normal file
@ -0,0 +1,366 @@
|
|||||||
|
"""
|
||||||
|
Collection of utility functions.
|
||||||
|
"""
|
||||||
|
import functools
|
||||||
|
from types import FunctionType
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
from .functions import kww, kww_1e
|
||||||
|
from scipy.ndimage import uniform_filter1d
|
||||||
|
|
||||||
|
from scipy.interpolate import interp1d
|
||||||
|
from scipy.optimize import curve_fit
|
||||||
|
|
||||||
|
from .logging import logger
|
||||||
|
|
||||||
|
|
||||||
|
def five_point_stencil(xdata, ydata):
|
||||||
|
"""
|
||||||
|
Calculate the derivative dy/dx with a five point stencil.
|
||||||
|
This algorith is only valid for equally distributed x values.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
xdata: x values of the data points
|
||||||
|
ydata: y values of the data points
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Values where the derivative was estimated and the value of the derivative at these points.
|
||||||
|
|
||||||
|
This algorithm is only valid for values on a regular grid, for unevenly distributed
|
||||||
|
data it is only an approximation, albeit a quite good one.
|
||||||
|
|
||||||
|
See: https://en.wikipedia.org/wiki/Five-point_stencil
|
||||||
|
"""
|
||||||
|
return xdata[2:-2], (
|
||||||
|
(-ydata[4:] + 8 * ydata[3:-1] - 8 * ydata[1:-3] + ydata[:-4]) /
|
||||||
|
(3 * (xdata[4:] - xdata[:-4]))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def filon_fourier_transformation(time, correlation,
|
||||||
|
frequencies=None, derivative='linear', imag=True,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Fourier-transformation for slow varrying functions. The filon algorithmus is
|
||||||
|
described in detail in ref [Blochowicz]_, ch. 3.2.3.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
time: List of times where the correlation function was sampled.
|
||||||
|
correlation: Values of the correlation function.
|
||||||
|
frequencies (opt.):
|
||||||
|
List of frequencies where the fourier transformation will be calculated.
|
||||||
|
If None the frequencies will be choosen based on the input times.
|
||||||
|
derivative (opt.):
|
||||||
|
Approximation algorithmus for the derivative of the correlation function.
|
||||||
|
Possible values are: 'linear', 'stencil' or a list of derivatives.
|
||||||
|
imag (opt.): If imaginary part of the integral should be calculated.
|
||||||
|
|
||||||
|
If frequencies are not explicitly given they will be evenly placed on a log scale
|
||||||
|
in the interval [1/tmax, 0.1/tmin] where tmin and tmax are the smallest respectively
|
||||||
|
the biggest time (greater than 0) of the provided times. The frequencies are cut off
|
||||||
|
at high values by one decade, since the fourier transformation deviates quite strongly
|
||||||
|
in this regime.
|
||||||
|
|
||||||
|
.. [Blochowicz]
|
||||||
|
T. Blochowicz, Broadband dielectric spectroscopy in neat and binary
|
||||||
|
molecular glass formers, Ph.D. thesis, Universität Bayreuth (2003)
|
||||||
|
"""
|
||||||
|
if frequencies is None:
|
||||||
|
f_min = 1 / max(time)
|
||||||
|
f_max = 0.05**(1.2 - max(correlation)) / min(time[time > 0])
|
||||||
|
frequencies = 2 * np.pi * np.logspace(
|
||||||
|
np.log10(f_min), np.log10(f_max), num=60
|
||||||
|
)
|
||||||
|
frequencies.reshape(1, -1)
|
||||||
|
|
||||||
|
if derivative == 'linear':
|
||||||
|
derivative = (np.diff(correlation) / np.diff(time)).reshape(-1, 1)
|
||||||
|
elif derivative == 'stencil':
|
||||||
|
_, derivative = five_point_stencil(time, correlation)
|
||||||
|
time = ((time[2:-1] * time[1:-2])**.5).reshape(-1, 1)
|
||||||
|
derivative = derivative.reshape(-1, 1)
|
||||||
|
elif np.iterable(derivative) and len(time) is len(derivative):
|
||||||
|
derivative.reshape(-1, 1)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
'Invalid approximation method {}. Possible values are "linear", "stencil" or a list of values.'
|
||||||
|
)
|
||||||
|
time = time.reshape(-1, 1)
|
||||||
|
|
||||||
|
integral = (np.cos(frequencies * time[1:]) - np.cos(frequencies * time[:-1])) / frequencies**2
|
||||||
|
fourier = (derivative * integral).sum(axis=0)
|
||||||
|
|
||||||
|
if imag:
|
||||||
|
integral = 1j * (np.sin(frequencies * time[1:]) - np.sin(frequencies * time[:-1])) / frequencies**2
|
||||||
|
fourier = fourier + (derivative * integral).sum(axis=0) + 1j * correlation[0] / frequencies
|
||||||
|
|
||||||
|
return frequencies.reshape(-1,), fourier
|
||||||
|
|
||||||
|
|
||||||
|
def mask2indices(mask):
|
||||||
|
"""
|
||||||
|
Return the selected indices of an array mask.
|
||||||
|
If the mask is two-dimensional, the indices will be calculated for the second axis.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> mask2indices([True, False, True, False])
|
||||||
|
array([0, 2])
|
||||||
|
>>> mask2indices([[True, True, False], [True, False, True]])
|
||||||
|
array([[0, 1], [0, 2]])
|
||||||
|
"""
|
||||||
|
mask = np.array(mask)
|
||||||
|
if len(mask.shape) == 1:
|
||||||
|
indices = np.where(mask)
|
||||||
|
else:
|
||||||
|
indices = np.array([np.where(m) for m in mask])
|
||||||
|
return indices
|
||||||
|
|
||||||
|
|
||||||
|
def superpose(x1, y1, x2, y2, N=100, damping=1.0):
|
||||||
|
if x2[0] == 0:
|
||||||
|
x2 = x2[1:]
|
||||||
|
y2 = y2[1:]
|
||||||
|
|
||||||
|
reg1 = x1 < x2[0]
|
||||||
|
reg2 = x2 > x1[-1]
|
||||||
|
x_ol = np.logspace(
|
||||||
|
np.log10(max(x1[~reg1][0], x2[~reg2][0]) + 0.001),
|
||||||
|
np.log10(min(x1[~reg1][-1], x2[~reg2][-1]) - 0.001),
|
||||||
|
(sum(~reg1) + sum(~reg2)) / 2
|
||||||
|
)
|
||||||
|
|
||||||
|
def w(x):
|
||||||
|
A = x_ol.min()
|
||||||
|
B = x_ol.max()
|
||||||
|
return (np.log10(B / x) / np.log10(B / A))**damping
|
||||||
|
|
||||||
|
xdata = np.concatenate((x1[reg1], x_ol, x2[reg2]))
|
||||||
|
y1_interp = interp1d(x1[~reg1], y1[~reg1])
|
||||||
|
y2_interp = interp1d(x2[~reg2], y2[~reg2])
|
||||||
|
ydata = np.concatenate((
|
||||||
|
y1[x1 < x2.min()],
|
||||||
|
w(x_ol) * y1_interp(x_ol) + (1 - w(x_ol)) * y2_interp(x_ol),
|
||||||
|
y2[x2 > x1.max()]
|
||||||
|
))
|
||||||
|
return xdata, ydata
|
||||||
|
|
||||||
|
|
||||||
|
def runningmean(data, nav):
|
||||||
|
"""
|
||||||
|
Compute the running mean of a 1-dimenional array.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data: Input data of shape (N, )
|
||||||
|
nav: Number of points over which the data will be averaged
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Array of shape (N-(nav-1), )
|
||||||
|
"""
|
||||||
|
return np.convolve(data, np.ones((nav,)) / nav, mode='valid')
|
||||||
|
|
||||||
|
def moving_average(A,n=3):
|
||||||
|
"""
|
||||||
|
Compute the running mean of an array.
|
||||||
|
Uses the second axis if it is of higher dimensionality.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data: Input data of shape (N, )
|
||||||
|
n: Number of points over which the data will be averaged
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Array of shape (N-(n-1), )
|
||||||
|
|
||||||
|
Supports 2D-Arrays.
|
||||||
|
Slower than runningmean for small n but faster for large n.
|
||||||
|
"""
|
||||||
|
k1 = int(n/2)
|
||||||
|
k2 = int((n-1)/2)
|
||||||
|
if k2 == 0:
|
||||||
|
if A.ndim > 1:
|
||||||
|
return uniform_filter1d(A,n)[:,k1:]
|
||||||
|
return uniform_filter1d(A,n)[k1:]
|
||||||
|
if A.ndim > 1:
|
||||||
|
return uniform_filter1d(A,n)[:,k1:-k2]
|
||||||
|
return uniform_filter1d(A,n)[k1:-k2]
|
||||||
|
|
||||||
|
|
||||||
|
def coherent_sum(func, coord_a, coord_b):
|
||||||
|
"""
|
||||||
|
Perform a coherent sum over two arrays :math:`A, B`.
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
\\frac{1}{N_A N_B}\\sum_i\\sum_j f(A_i, B_j)
|
||||||
|
|
||||||
|
For numpy arrays this is equal to::
|
||||||
|
|
||||||
|
N, d = x.shape
|
||||||
|
M, d = y.shape
|
||||||
|
coherent_sum(f, x, y) == f(x.reshape(N, 1, d), x.reshape(1, M, d)).sum()
|
||||||
|
|
||||||
|
Args:
|
||||||
|
func: The function is called for each two items in both arrays, this should return a scalar value.
|
||||||
|
coord_a, coord_b: The two arrays.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if isinstance(func, FunctionType):
|
||||||
|
func = numba.jit(func, nopython=True, cache=True)
|
||||||
|
|
||||||
|
def cohsum(coord_a, coord_b):
|
||||||
|
res = 0
|
||||||
|
for i in range(len(coord_a)):
|
||||||
|
for j in range(len(coord_b)):
|
||||||
|
res += func(coord_a[i], coord_b[j])
|
||||||
|
return res
|
||||||
|
|
||||||
|
return cohsum(coord_a, coord_b)
|
||||||
|
|
||||||
|
|
||||||
|
def coherent_histogram(func, coord_a, coord_b, bins, distinct=False):
|
||||||
|
"""
|
||||||
|
Compute a coherent histogram over two arrays, equivalent to coherent_sum.
|
||||||
|
For numpy arrays ofthis is equal to::
|
||||||
|
|
||||||
|
N, d = x.shape
|
||||||
|
M, d = y.shape
|
||||||
|
bins = np.arange(1, 5, 0.1)
|
||||||
|
coherent_histogram(f, x, y, bins) == histogram(f(x.reshape(N, 1, d), x.reshape(1, M, d)), bins=bins)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
func: The function is called for each two items in both arrays, this should return a scalar value.
|
||||||
|
coord_a, coord_b: The two arrays.
|
||||||
|
bins: The bins used for the histogram must be distributed regular on a linear scale.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if isinstance(func, FunctionType):
|
||||||
|
func = numba.jit(func, nopython=True, cache=True)
|
||||||
|
|
||||||
|
assert np.isclose(np.diff(bins).mean(), np.diff(bins)).all(), 'A regular distribution of bins is required.'
|
||||||
|
hmin = bins[0]
|
||||||
|
hmax = bins[-1]
|
||||||
|
N = len(bins) - 1
|
||||||
|
dh = (hmax - hmin) / N
|
||||||
|
|
||||||
|
def cohsum(coord_a, coord_b):
|
||||||
|
res = np.zeros((N,))
|
||||||
|
for i in range(len(coord_a)):
|
||||||
|
for j in range(len(coord_b)):
|
||||||
|
if not (distinct and i == j):
|
||||||
|
h = func(coord_a[i], coord_b[j])
|
||||||
|
if hmin <= h < hmax:
|
||||||
|
res[int((h - hmin) / dh)] += 1
|
||||||
|
return res
|
||||||
|
|
||||||
|
return cohsum(coord_a, coord_b)
|
||||||
|
|
||||||
|
|
||||||
|
def Sq_from_gr(r, gr, q, ρ):
|
||||||
|
r"""
|
||||||
|
Compute the static structure factor as fourier transform of the pair correlation function. [Yarnell]_
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
S(q) - 1 = \\frac{4\\pi \\rho}{q}\\int\\limits_0^\\infty (g(r) - 1)\\,r \\sin(qr) dr
|
||||||
|
|
||||||
|
Args:
|
||||||
|
r: Radii of the pair correlation function
|
||||||
|
gr: Values of the pair correlation function
|
||||||
|
q: List of q values
|
||||||
|
ρ: Average number density
|
||||||
|
|
||||||
|
.. [Yarnell]
|
||||||
|
Yarnell, J. L., Katz, M. J., Wenzel, R. G., & Koenig, S. H. (1973). Physical Review A, 7(6), 2130–2144.
|
||||||
|
http://doi.org/10.1017/CBO9781107415324.004
|
||||||
|
|
||||||
|
"""
|
||||||
|
ydata = ((gr - 1) * r).reshape(-1, 1) * np.sin(r.reshape(-1, 1) * q.reshape(1, -1))
|
||||||
|
return np.trapz(x=r, y=ydata, axis=0) * (4 * np.pi * ρ / q) + 1
|
||||||
|
|
||||||
|
|
||||||
|
def Fqt_from_Grt(data, q):
|
||||||
|
"""
|
||||||
|
Calculate the ISF from the van Hove function for a given q value by fourier transform.
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
F_q(t) = \\int\\limits_0^\\infty dr \\; G(r, t) \\frac{\\sin(qr)}{qr}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data:
|
||||||
|
Input data can be a pandas dataframe with columns 'r', 'time' and 'G'
|
||||||
|
or an array of shape (N, 3), of tuples (r, t, G).
|
||||||
|
q: Value of q.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
If input data was a dataframe the result will be returned as one too, else two arrays
|
||||||
|
will be returned, which will contain times and values of Fq(t) respectively.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if isinstance(data, pd.DataFrame):
|
||||||
|
df = data.copy()
|
||||||
|
else:
|
||||||
|
df = pd.DataFrame(data, columns=['r', 'time', 'G'])
|
||||||
|
df['isf'] = df['G'] * np.sinc(q / np.pi * df['r'])
|
||||||
|
isf = df.groupby('time')['isf'].sum()
|
||||||
|
if isinstance(data, pd.DataFrame):
|
||||||
|
return pd.DataFrame({'time': isf.index, 'isf': isf.values, 'q': q})
|
||||||
|
else:
|
||||||
|
return isf.index, isf.values
|
||||||
|
|
||||||
|
'''
|
||||||
|
@numba.jit
|
||||||
|
def norm(vec):
|
||||||
|
return (vec**2).sum()**0.5
|
||||||
|
'''
|
||||||
|
|
||||||
|
def singledispatchmethod(func):
|
||||||
|
"""A decorator to define a genric instance method, analogue to functools.singledispatch."""
|
||||||
|
dispatcher = functools.singledispatch(func)
|
||||||
|
|
||||||
|
def wrapper(*args, **kw):
|
||||||
|
return dispatcher.dispatch(args[1].__class__)(*args, **kw)
|
||||||
|
wrapper.register = dispatcher.register
|
||||||
|
functools.update_wrapper(wrapper, func)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def histogram(data, bins):
|
||||||
|
"""Compute the histogram of the given data. Uses numpy.bincount function, if possible."""
|
||||||
|
dbins = np.diff(bins)
|
||||||
|
dx = dbins.mean()
|
||||||
|
if bins.min() == 0 and dbins.std() < 1e-6:
|
||||||
|
logger.debug("Using numpy.bincount for histogramm compuation.")
|
||||||
|
hist = np.bincount((data // dx).astype(int), minlength=len(dbins))[:len(dbins)]
|
||||||
|
else:
|
||||||
|
hist = np.histogram(data, bins=bins)[0]
|
||||||
|
|
||||||
|
return hist, runningmean(bins, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def quick1etau(t, C, n=7):
|
||||||
|
"""
|
||||||
|
Estimate the time for a correlation function that goes from 1 to 0 to decay to 1/e.
|
||||||
|
|
||||||
|
If successful, returns tau as fine interpolation with a kww fit.
|
||||||
|
The data is reduce to points around 1/e to remove short and long times from the kww fit!
|
||||||
|
t is the time
|
||||||
|
C is C(t) the correlation function
|
||||||
|
n is the minimum number of points around 1/e required
|
||||||
|
"""
|
||||||
|
# first rough estimate, the closest time. This is returned if the interpolation fails!
|
||||||
|
tau_est = t[np.argmin(np.fabs(C-np.exp(-1)))]
|
||||||
|
# reduce the data to points around 1/e
|
||||||
|
k = 0.1
|
||||||
|
mask = (C < np.exp(-1)+k) & (C > np.exp(-1)-k)
|
||||||
|
while np.sum(mask) < n:
|
||||||
|
k += 0.01
|
||||||
|
mask = (C < np.exp(-1)+k) & (C > np.exp(-1)-k)
|
||||||
|
if k + np.exp(-1) > 1.0:
|
||||||
|
break
|
||||||
|
# if enough points are found, try a curve fit, else and in case of failing keep using the estimate
|
||||||
|
if np.sum(mask) >= n:
|
||||||
|
try:
|
||||||
|
with np.errstate(invalid='ignore'):
|
||||||
|
fit, _ = curve_fit(kww, t[mask], C[mask], p0=[0.9, tau_est, 0.9], maxfev=100000)
|
||||||
|
tau_est = kww_1e(*fit)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return tau_est
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
mdanalysis
|
||||||
|
dask
|
27
setup.py
Normal file
27
setup.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from setuptools import setup
|
||||||
|
|
||||||
|
|
||||||
|
def get_version(module):
|
||||||
|
version = ''
|
||||||
|
with open(module) as f:
|
||||||
|
for line in f:
|
||||||
|
if '__version__' in line:
|
||||||
|
version = line.split('=')[-1].strip("' \n\t")
|
||||||
|
break
|
||||||
|
return version
|
||||||
|
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name='mdevaluate',
|
||||||
|
description='Collection of python utilities for md simulations',
|
||||||
|
author_email='niels.mueller@physik.tu-darmstadt.de',
|
||||||
|
|
||||||
|
packages=['mdevaluate',],
|
||||||
|
entry_points={
|
||||||
|
'console_scripts': [
|
||||||
|
'index-xtc = mdevaluate.cli:run'
|
||||||
|
]
|
||||||
|
},
|
||||||
|
version='2022.1.dev1',
|
||||||
|
requires=['numpy', 'scipy'],
|
||||||
|
)
|
BIN
test/data/water/topol.tpr
Normal file
BIN
test/data/water/topol.tpr
Normal file
Binary file not shown.
BIN
test/data/water/traj.xtc
Normal file
BIN
test/data/water/traj.xtc
Normal file
Binary file not shown.
7
test/test_atoms.py
Normal file
7
test/test_atoms.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from mdevaluate import atoms
|
||||||
|
|
||||||
|
|
||||||
|
def test_compare_regex():
|
||||||
|
assert atoms.compare_regex(['OW', ], 'O')[0] == False
|
||||||
|
assert atoms.compare_regex(['WO', ], 'O')[0] == False
|
||||||
|
assert atoms.compare_regex(['O', ], 'O')[0] == True
|
39
test/test_checksum.py
Normal file
39
test/test_checksum.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from mdevaluate import checksum
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def test_checksum():
|
||||||
|
salt = checksum.SALT
|
||||||
|
checksum.SALT = ''
|
||||||
|
assert checksum.checksum(1) == 304942582444936629325699363757435820077590259883
|
||||||
|
assert checksum.checksum('42') == checksum.checksum(42)
|
||||||
|
cs1 = checksum.checksum(999)
|
||||||
|
checksum.SALT = '999'
|
||||||
|
assert cs1 != checksum.checksum(999)
|
||||||
|
|
||||||
|
a = np.array([1, 2, 3])
|
||||||
|
assert checksum.checksum(a) == checksum.checksum(a.tobytes())
|
||||||
|
|
||||||
|
checksum.SALT = salt
|
||||||
|
|
||||||
|
|
||||||
|
def test_version():
|
||||||
|
|
||||||
|
@checksum.version(1)
|
||||||
|
def f1():
|
||||||
|
pass
|
||||||
|
|
||||||
|
cs1 = checksum.checksum(f1)
|
||||||
|
|
||||||
|
@checksum.version(1)
|
||||||
|
def f1(x, y):
|
||||||
|
return x + y
|
||||||
|
|
||||||
|
assert cs1 == checksum.checksum(f1)
|
||||||
|
|
||||||
|
@checksum.version(2)
|
||||||
|
def f1(x, y):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert cs1 != checksum.checksum(f1)
|
31
test/test_coordinates.py
Normal file
31
test/test_coordinates.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import mdevaluate
|
||||||
|
from mdevaluate import coordinates
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def trajectory(request):
|
||||||
|
return mdevaluate.open(os.path.join(os.path.dirname(__file__), 'data/water'))
|
||||||
|
|
||||||
|
|
||||||
|
def test_coordinates_getitem(trajectory):
|
||||||
|
"""
|
||||||
|
Tests for the Coordinates class.
|
||||||
|
"""
|
||||||
|
assert isinstance(trajectory[0], coordinates.CoordinateFrame)
|
||||||
|
assert isinstance(trajectory[len(trajectory) - 1], coordinates.CoordinateFrame)
|
||||||
|
i = 0
|
||||||
|
dt = trajectory[1].time - trajectory[0].time
|
||||||
|
for f in trajectory:
|
||||||
|
assert f.step == i
|
||||||
|
assert round(f.time, 3) == round(i * dt, 3)
|
||||||
|
i += 1
|
||||||
|
sl = trajectory[0::10]
|
||||||
|
assert isinstance(sl, coordinates.Coordinates)
|
||||||
|
i = 0
|
||||||
|
for f in sl:
|
||||||
|
assert f.step == i
|
||||||
|
assert round(f.time, 3) == round(i * dt, 3)
|
||||||
|
i += 10
|
14
test/test_pbc.py
Normal file
14
test/test_pbc.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from pytest import approx
|
||||||
|
|
||||||
|
from mdevaluate import pbc
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def test_pbc_diff():
|
||||||
|
x = np.random.rand(10, 3)
|
||||||
|
y = np.random.rand(10, 3)
|
||||||
|
box = np.ones((3,))
|
||||||
|
|
||||||
|
assert (pbc.pbc_diff(x, x, box) == approx(0))
|
||||||
|
dxy = (pbc.pbc_diff(x, y, box)**2).sum(axis=1)**0.5
|
||||||
|
assert (dxy <= 0.75**0.5).all()
|
46
test/test_utils.py
Normal file
46
test/test_utils.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from copy import copy
|
||||||
|
import pytest
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from mdevaluate import utils
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def logdata(request):
|
||||||
|
xdata = np.logspace(-1, 3, 50)
|
||||||
|
ydata = np.exp(- (xdata)**0.7)
|
||||||
|
return xdata, ydata
|
||||||
|
|
||||||
|
|
||||||
|
def test_filon_fourier_transformation(logdata):
|
||||||
|
xdata, ydata = logdata
|
||||||
|
|
||||||
|
xdata_zero = copy(xdata)
|
||||||
|
xdata_zero[0] = 0
|
||||||
|
_, filon = utils.filon_fourier_transformation(xdata_zero, ydata)
|
||||||
|
assert not np.isnan(filon).any(), 'There are NaN values in the filon result!'
|
||||||
|
|
||||||
|
freqs = np.logspace(-4, 1)
|
||||||
|
filon_freqs, filon_imag = utils.filon_fourier_transformation(
|
||||||
|
xdata, xdata, frequencies=freqs, derivative='linear', imag=True
|
||||||
|
)
|
||||||
|
|
||||||
|
assert (freqs == filon_freqs).all()
|
||||||
|
|
||||||
|
freqs, filon_real = utils.filon_fourier_transformation(
|
||||||
|
xdata, xdata, frequencies=freqs, derivative='linear', imag=False
|
||||||
|
)
|
||||||
|
assert np.isclose(filon_imag.real, filon_real).all()
|
||||||
|
|
||||||
|
|
||||||
|
def test_histogram():
|
||||||
|
data = np.random.rand(100)
|
||||||
|
bins = np.linspace(0, 1)
|
||||||
|
np_hist = np.histogram(data, bins=bins)[0]
|
||||||
|
ut_hist = utils.histogram(data, bins=bins)[0]
|
||||||
|
assert (np_hist == ut_hist).all()
|
||||||
|
|
||||||
|
bins = np.linspace(0.3, 1.5)
|
||||||
|
np_hist = np.histogram(data, bins=bins)[0]
|
||||||
|
ut_hist = utils.histogram(data, bins=bins)[0]
|
||||||
|
assert (np_hist == ut_hist).all()
|
Loading…
Reference in New Issue
Block a user