asyncmd is a library to write concurrent code to run and analyze molecular dynamics simulations using pythons async/await synthax.
Project description
asyncmd
Synopsis
asyncmd is a library to write concurrent code to run and analyze molecular dynamics simulations using pythons async/await synthax.
Motivation
Molecular dynamics simulations are fun and we can learn a lot about the simulated system. Running many molecular dynamics simulations of the same system concurrently is tedious, error-prone and boring but we can learn even more about the simulated system and are more efficient in doing so. This library addresses the tedious, error-prone and boring part of setting up many similar simulations, but it leaves you with the fun part of understanding the simulated system.
Code Example
Run N GROMACS engines concurently from configurations randomly picked up along a trajectory (traj.trr) for n_steps integration steps each, drawing random Maxwell-Boltzmann velocities for each configuration on the way. Finally turn the python function func (which acts on Trajectory objects) into an asyncronous and cached function by wrapping it and apply it on all generated trajectories concurrently:
import asyncio
import numpy as np
import asyncmd
import asyncmd.gromacs as asyncgmx
in_traj = asyncmd.Trajectory(trajectory_files="traj.trr", structure_file="conf.gro")
# get a random number generator and draw N random frames (with replacement)
rng = np.default_rng()
frame_idxs = rng.choice(len(in_traj), size=N)
# use the RandomVelocitiesFrameExtractor to directly get the frames with MB-vels
extractor = asyncmd.trajectory.convert.RandomVelocitiesFrameExtractor(T=303)
mdps = [asyncgmx.MDP("config.mdp") for _ in range(N)]
# MDConfig objects (like MDP) behave like dictionaries and are easy to modify
for i, mdp in enumerate(mdps):
# here we just modify the output frequency for every engine separately
# but you can set any mdp option like this
# Note how the values are in the correct types? I.e. that they are ints?
mdp["nstxout"] *= (i + 1)
mdp["nstvout"] *= (i + 1)
# create N gromacs engines
engines = [asyncgmx.GmxEngine(mdp=mdp, gro_file="conf.gro", top_file="topol.top",
# optional (can be omited or None), however naturally without an index file
# you can not reference custom groups in the .mdp-file or MDP object
ndx_file="index.ndx",
)
for mdp in mdps]
# extract starting configurations with MB-vels and save them to current directory
start_confs = await asyncio.gather(*(extractor.extract_async(
outfile=f"start_conf{i}.trr",
traj_in=in_traj, idx=idx)
for i, idx in enumerate(frame_idxs)))
# prepare the MD (for gromacs this is essentially a `grompp` call)
await asyncio.gather(*(e.prepare(starting_configuration=conf,
workdir=".", deffnm=f"engine{i}")
for i, (conf, e) in enumerate(zip(start_confs, engines))
)
)
# and run the molecular dynamics
out_trajs = await asyncio.gather(*(e.run_steps(nsteps=n_steps) for e in engines))
# wrapp `func` and apply it on all output trajectories concurrently
wrapped_func = asyncmd.trajectory.PyTrajectoryFunctionWrapper(function=func)
cv_vals = await asyncio.gather(*(wrapped_func(traj) for traj in out_trajs))
Note that running via the SLURM queueing system is as easy as replacing the GmxEngine with a SlurmGmxEngine and the PyTrajectoryFunctionWrapper with a SlurmTrajectoryFunctionWrapper (and suppling them both with sbatch script skeletons).
For an in-depth introduction see also the examples folder in this repository which contains jupyter notebooks on various topics.
Installation
pip install from PyPi
asyncmd is published on PyPi (since v0.3.2), installing is as easy as:
pip install asyncmd
pip install directly from the repository
Please note that you need to have git-lfs (an open source git extension) setup to get all input files needed to run the notebooks in the examples folder. However, no git-lfs is needed to get a working version of the library.
git clone https://github.com/bio-phys/asyncmd.git
cd asyncmd
pip install .
Documentation and API Reference
The documentation can be build with sphinx, use e.g. the following to build it in html format:
cd asyncmd # Need to be at the top folder of the repository for the next line to work
sphinx-build -b html docs/source docs/build/html
Use pip install .\[docs\] to install the requirements needed to build the documentation.
Tests
Tests use pytest. To run them just install asycmd with the test requirements
git clone https://github.com/bio-phys/asyncmd.git
cd asyncmd
pip install .\[tests\]
# or use
pip install .\[tests-all\]
# to also install optional dependencies needed to run all tests
And then run the tests (against the installed version) as
pytest
Contribute
If you discover any issues or want to propose a new feature please feel free to open an issue or a pull request!
Developer install
For the developer install I recommend:
git clone https://github.com/bio-phys/asyncmd.git
cd asyncmd
pip install -e .\[dev\]
This will in addition to the requirements to run the tests and to build the documentation install coverage and its pytest-cov plugin such that you have an idea of the test coverage for your newly added code. To get a nice html coverage report you can run the tests as
pytest --cov=asyncmd --cov-report=html
Contributors
This project was originally conceived and started by Hendrik Jung in 2021/2022. For more check the pyproject.toml file. When you contribute code dont forget to add your name there to claim the credit for your work!
License
asyncmd is under the terms of the GNU general public license version 3 or later, i.e. SPDX identifier "GPL-3.0-or-later".
This README.md is printed from 100% recycled electrons.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncmd-0.3.3.tar.gz.
File metadata
- Download URL: asyncmd-0.3.3.tar.gz
- Upload date:
- Size: 100.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be84f6958dfd7b43cea0160e145325b8ff9dc73a58229351425d0b081eaca4c5
|
|
| MD5 |
342c04c70aa8474defa8d0fefb9cb102
|
|
| BLAKE2b-256 |
59ba3467c887e8b455a164c03d03e7ad1e01e051c1a2fc5008c2d38b4699ad7a
|
Provenance
The following attestation bundles were made for asyncmd-0.3.3.tar.gz:
Publisher:
publish-to-pypi.yml on bio-phys/asyncmd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asyncmd-0.3.3.tar.gz -
Subject digest:
be84f6958dfd7b43cea0160e145325b8ff9dc73a58229351425d0b081eaca4c5 - Sigstore transparency entry: 207319223
- Sigstore integration time:
-
Permalink:
bio-phys/asyncmd@c9ede9ed2f4a064a6cfb11851ec61fb5f9a23654 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/bio-phys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@c9ede9ed2f4a064a6cfb11851ec61fb5f9a23654 -
Trigger Event:
push
-
Statement type:
File details
Details for the file asyncmd-0.3.3-py3-none-any.whl.
File metadata
- Download URL: asyncmd-0.3.3-py3-none-any.whl
- Upload date:
- Size: 104.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb16f8fbc46c730ca2343a132d4ccbb7d4b851c2b26e626c7d040de776760837
|
|
| MD5 |
a6874c512e95c833c4e9ae1f3767df1b
|
|
| BLAKE2b-256 |
7174c5945acf138d99a6b003b32393748170ed6f884dca35838231aede57ce3b
|
Provenance
The following attestation bundles were made for asyncmd-0.3.3-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on bio-phys/asyncmd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asyncmd-0.3.3-py3-none-any.whl -
Subject digest:
bb16f8fbc46c730ca2343a132d4ccbb7d4b851c2b26e626c7d040de776760837 - Sigstore transparency entry: 207319226
- Sigstore integration time:
-
Permalink:
bio-phys/asyncmd@c9ede9ed2f4a064a6cfb11851ec61fb5f9a23654 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/bio-phys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@c9ede9ed2f4a064a6cfb11851ec61fb5f9a23654 -
Trigger Event:
push
-
Statement type: