Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update the doc for openmm dmff plugin usage. #146

Merged
merged 35 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f533581
Add issue templates for feature request and bug-report
dingye18 Feb 13, 2023
3f4d29b
Merge branch 'devel' of https://github.com/dingye18/DMFF into devel
dingye18 Jul 3, 2023
f1a7919
Merge branch 'devel' of https://github.com/deepmodeling/DMFF into devel
dingye18 Jul 3, 2023
3cdf6a1
Merge branch 'devel' of https://github.com/deepmodeling/DMFF into devel
dingye18 Jul 30, 2023
bec7eb4
Merge branch 'devel' of https://github.com/deepmodeling/DMFF into devel
dingye18 Aug 9, 2023
d11057a
Merge branch 'devel' of https://github.com/dingye18/DMFF into devel
dingye18 Aug 18, 2023
5fbd560
Add script for dmff model saving.
dingye18 Aug 18, 2023
63ffd61
Remove issue template from devel branch.
dingye18 Aug 18, 2023
38b2b2f
debug workflow
dingye18 Aug 18, 2023
cbb35b8
remove debug
dingye18 Aug 18, 2023
14a45cd
Update ut.yml. Install mdtraj by conda.
dingye18 Aug 20, 2023
e816bd1
Merge branch 'deepmodeling:devel' into devel
dingye18 Oct 8, 2023
8e9bede
Add openmm dmff plugin to backend
dingye18 Oct 9, 2023
7eedaad
Update .gitignore
dingye18 Oct 9, 2023
0d2aaad
Add test workflow for openmm dmff plugin
dingye18 Oct 10, 2023
1a93eed
Update workflow for openmm dmff plugin test.
dingye18 Oct 10, 2023
c3b060b
Update workflow for openmm dmff plugin test.
dingye18 Oct 10, 2023
925a09a
Update workflow for openmm dmff plugin test.
dingye18 Oct 10, 2023
fc5aa00
Update workflow for openmm dmff plugin test.
dingye18 Oct 10, 2023
c60de88
Update workflow for openmm dmff plugin test.
dingye18 Oct 10, 2023
22bf802
Specific setuptools version in workflow yaml
dingye18 Oct 10, 2023
2ba5d30
Update tensorflow version
dingye18 Oct 10, 2023
3f04d60
Enable double precision in test omm dmff plugin workflow
dingye18 Oct 10, 2023
86d47e2
update nve assert checking in omm dmff plugin workflow
dingye18 Oct 10, 2023
e0877ee
Fix no axis type bug (#120)
plumbum082 Oct 20, 2023
ac94a96
Merge branch 'deepmodeling:devel' into devel
dingye18 Nov 5, 2023
fa695a6
Merge branch 'wangxy/v1.0.0-devel' of https://github.com/deepmodeling…
dingye18 Nov 5, 2023
447a73a
Update omm plugin and save_dmff2tf.py for aux compatible.
dingye18 Nov 5, 2023
33d622b
Fix test issue.
dingye18 Nov 6, 2023
26b44f6
Update test_openmm_dmff_plugin.yml
dingye18 Nov 6, 2023
f8fe2fe
Update the doc for openmm dmff plugin usage
dingye18 Nov 8, 2023
236cb1c
Merge branch 'wangxy/v1.0.0-devel' of https://github.com/deepmodeling…
dingye18 Nov 8, 2023
6416580
Revision on the doc of DMFF plugin
dingye18 Nov 8, 2023
1f7522d
Upload tf c directory.
dingye18 Nov 8, 2023
d740579
Remove tf2.9.1_c.tar
dingye18 Nov 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions backend/openmm_dmff_plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


This is a plugin for [OpenMM](http://openmm.org) that used the trained JAX model by [DMFF](https://github.com/deepmodeling/DMFF) as an independent Force class for dynamics.
To use it, you need to save you DMFF model with the script in `DMFF/backend/save_dmff2tf.py`.
To use it, you need to save your DMFF model with the script in `DMFF/backend/save_dmff2tf.py`.

## Installation

Expand All @@ -15,10 +15,9 @@ conda create -n dmff_omm -c conda-forge python=3.9 openmm cudatoolkit=11.6
conda activate dmff_omm
```
### Download `libtensorflow_cc` and install `cppflow` package
Install the precompiled libtensorflow_cc library from deepmodeling channel.
Install the precompiled libtensorflow_cc library from conda.
```shell

conda install -c deepmodeling libtensorflow_cc=2.9.1=cuda112h02da4e0_0
conda install -y libtensorflow_cc=2.9.1 -c conda-forge
```
Download the tensorflow sources file. Copy the `c` direcotry in source code to installed header files of tensorflow library, since it's needed by package `cppflow`.
```shell
Expand All @@ -37,9 +36,9 @@ mkdir ${CONDA_PREFIX}/include/cppflow
cp -r include/cppflow ${CONDA_PREFIX}/include/
```

### Install the OpenMM DMFF plugin from source
### Install the OpenMM DMFF plugin from the source

Compile the plugin from source with following steps.
Compile the plugin from the source with the following steps.
1. Set up environment variables.
```shell
export OPENMM_INSTALLED_DIR=$CONDA_PREFIX
Expand All @@ -49,7 +48,7 @@ Compile the plugin from source with following steps.
mkdir build && cd build
```

2. Run `cmake` command with required parameters.
2. Run `cmake` command with the required parameters.
```shell
cmake .. -DOPENMM_DIR=${OPENMM_INSTALLED_DIR} -DCPPFLOW_DIR=${CPPFLOW_INSTALLED_DIR} -DTENSORFLOW_DIR=${LIBTENSORFLOW_INSTALLED_DIR}
make && make install
Expand Down
110 changes: 51 additions & 59 deletions docs/user_guide/4.7OpenMMplugin.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
# OpenMM Plugin for DMFF
# OpenMM DMFF Plugin

## 1. Theory

This is a plugin for [OpenMM](http://openmm.org) that used the trained JAX model by [DMFF](https://github.com/deepmodeling/DMFF) as an independent Force class for dynamics.
To use it, you need to save you DMFF model with the script in `DMFF/backend/save_dmff2tf.py`.
OpenMM DMFF plugin was developed for [OpenMM](http://openmm.org) to incorporate the trained JAX model from [DMFF](https://github.com/deepmodeling/DMFF) as an independent Force class for molecular dynamics simulations.
To utilize this plugin, you need to save your DMFF model using the `DMFF/backend/save_dmff2tf.py` script.
The `save_dmff2tf.py` script converts the DMFF model to a TensorFlow module using the experimental feature of JAX called [`jax2tf`](https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md).
The integration of the saved TensorFlow module with the DMFF plugin is accomplished using [cppflow](https://github.com/serizba/cppflow) and the OpenMM C++ interface.
To install the DMFF plugin, please refer to the instructions provided in the [Readme](backend/openmm_dmff_plugin/README.md).

## Installation

### Create environment with conda
Install the python, openmm and cudatoolkit.
```shell
## 2. Save DMFF model with script `save_dmff2tf.py`

mkdir omm_dmff_working_dir && cd omm_dmff_working_dir
conda create -n dmff_omm -c conda-forge python=3.9 openmm cudatoolkit=11.6
conda activate dmff_omm
```
### Download `libtensorflow_cc` and install `cppflow` package
Install the precompiled libtensorflow_cc library from deepmodeling channel.
```shell

conda install -c deepmodeling libtensorflow_cc=2.9.1=cuda112h02da4e0_0
```
Download the tensorflow sources file. Copy the `c` direcotry in source code to installed header files of tensorflow library, since it's needed by package `cppflow`.
```shell

wget https://github.com/tensorflow/tensorflow/archive/refs/tags/v2.9.1.tar.gz
tar -xvf v2.9.1.tar.gz
cp -r tensorflow-2.9.1/tensorflow/c ${CONDA_PREFIX}/include/tensorflow/
The `save_dmff2tf.py` script requires at least two input files to run:
- input_pdb: A .pdb file containing the initial coordinates and box information.
- xml_files: The force field parameters optimized by DMFF.
Additionally, the `output` argument is required to specify the directory for saving the DMFF model.
The saved model produces `energy` and `forces` tensors.
Below is an example usage of the script:
```python
# For saving classical DMFF model.
python backend/save_dmff2tf.py --input_pdb examples/classical/lig.pdb
--xml_files examples/classical/lig-prm.xml
--output /tmp/dmff_classical_lig
```
Download `cppflow` and move the headers library to environment path.
```shell
If additional .xml files are needed for topology definition in input files, the `--bond_definitions_xml` argument can be used to specify the path to the .xml file defining bond connections.

git clone https://github.com/serizba/cppflow.git
cd cppflow
git apply DMFF/backend/openmm_dmff_plugin/tests/cppflow_empty_constructor.patch
mkdir ${CONDA_PREFIX}/include/cppflow
cp -r include/cppflow ${CONDA_PREFIX}/include/
Please note that when saving the DMFF model that uses the `ADMPPmeForce` module, the `--has_aux=True` argument is required.
In this case, the saved model produces `energy`, `forces`, and `U_ind` tensors.
Below is an example usage of the script:
```python
# For saving ADMP DMFF model.
python backend/save_dmff2tf.py --input_pdb examples/water_fullpol/water_dimer.pdb
--xml_files examples/water_fullpol/forcefield.xml
--bond_definitions_xml examples/water_fullpol/residues.xml
--output /tmp/dmff_admp_water_dimer
```

### Install the OpenMM DMFF plugin from source

Compile the plugin from source with following steps.
1. Set up environment variables.
```shell
export OPENMM_INSTALLED_DIR=$CONDA_PREFIX
export CPPFLOW_INSTALLED_DIR=$CONDA_PREFIX
export LIBTENSORFLOW_INSTALLED_DIR=$CONDA_PREFIX
cd DMFF/backend/openmm_dmff_plugin/
mkdir build && cd build
```

2. Run `cmake` command with required parameters.
```shell
cmake .. -DOPENMM_DIR=${OPENMM_INSTALLED_DIR} -DCPPFLOW_DIR=${CPPFLOW_INSTALLED_DIR} -DTENSORFLOW_DIR=${LIBTENSORFLOW_INSTALLED_DIR}
make && make install
make PythonInstall
```

3. Test the plugin in Python interface, reference platform.
```shell
python -m OpenMMDMFFPlugin.tests.test_dmff_plugin_nve -n 100
python -m OpenMMDMFFPlugin.tests.test_dmff_plugin_nvt -n 100 --platform CUDA
```
## Usage
Add the following lines to your Python script to use the plugin.
More details can refer to the script in `python/OpenMMDMFFPlugin/tests/test_dmff_plugin_nve.py`.
## 3. Class `DMFFModel`

The OpenMM DMFF Plugin can be easily used through the `DMFFModel` class.
This class provides a convenient way to work with the plugin.
See the example code below:
```python

# Create an OpenMM System object (dmff_system) with saved DMFF model.
from OpenMMDMFFPlugin import DMFFModel
# Set up the dmff_system with the dmff_model.
dmff_model = DMFFModel(dp_model)
dmff_model = DMFFModel(dp_model_dir)
dmff_model.setUnitTransformCoefficients(1, 1, 1)
dmff_system = dmff_model.createSystem(topology)
```

The `DMFFModel` class includes three methods:

Function `setUnitTransformCoefficients(coordCoefficient, forceCoefficient, energyCoefficient)`:
- In the OpenMM context, the units for coordinates, energy and forces are constrained to nm, kJ/mol and kJ/(mol * nm) respectively.
- You need to provide three coefficients if the saved DMFF model requires different input and output units for coordinates, energy, and forces.

Function `setHasAux(has_aux=False)`:
- Use this method when running simulations with the `ADMPPmeForce` module in the saved DMFF model.

Function `createSystem(topology)`:
- This method constructs an OpenMM System object, with the OpenMM Topology object being the input.

Once the OpenMM System object is constructed,
further simulation settings with the DMFF model are similar to those with other force fields in OpenMM.
A complete script for simulation with this plugin can be found in [test_dmff_plugin_nve.py](/backend/openmm_dmff_plugin//python/OpenMMDMFFPlugin/tests/test_dmff_plugin_nve.py)
Loading