JingHuangLab / openmm_deepmd_plugin

19 stars 5 forks source link

OpenMM Plugin for Deep Potential Model

This is a plugin for OpenMM that allows DeepPotential model to be used for defining forces. It is implemented with DeePMD-kit C API interface. To use it, you need a trained Deep Potential model with DeePMD-kit that takes particle positions as input and produces forces and energy as output. This plugin uses the Deep Potential model to apply forces to particles during a simulation.

Installation

Install from source

This plugin requires the library of OpenMM, DeePMD-kit C API package. Compile the plugin from the source with the following steps.

  1. Prepare a conda environment by running the following commands:

    conda create -n dp_openmm
    conda activate dp_openmm
    conda install -c conda-forge openmm
  2. Download and install the DeePMD-kit C API library by running the following command:

    wget https://github.com/deepmodeling/deepmd-kit/releases/latest/download/libdeepmd_c.tar.gz
    # Extract the C API library of Deepmd-kit to the directory of your choice.
    tar -xf libdeepmd_c.tar.gz -C ${LIBDEEPMD_C_INSTALLED_DIR}
  3. Clone this repository and create a directory in which to build the plugin.

    git clone https://github.com/JingHuangLab/openmm_deepmd_plugin.git
    cd openmm_deepmd_plugin && mkdir build && cd build
  4. Run cmake command with the required parameters.

    cmake .. -DOPENMM_DIR=${OPENMM_INSTALLED_DIR} -DDEEPMD_DIR=${LIBDEEPMD_C_INSTALLED_DIR}

    OPENMM_INSTALLED_DIR is the directory where OpenMM is installed. If you installed OpenMM from conda, it is the directory of the location of environment dp_openmm. LIBDEEPMD_C_INSTALLED_DIR is the directory where the DeePMD-kit C API library is installed.

  5. Compile the shared library.

    make && make install

    It will install the plugin to the subdirectory of OPENMM_DIR automatically.

  6. Test the plugin C interface and install the Python module of this plugin into conda environment by running the following commands:

    make test
    make PythonInstall
    export LD_LIBRARY_PATH=${LIBDEEPMD_C_INSTALLED_DIR}/lib:$LD_LIBRARY_PATH
    python -m OpenMMDeepmdPlugin.tests.test_dp_plugin_nve
    python -m OpenMMDeepmdPlugin.tests.test_dp_plugin_nve --platform CUDA

Usage of DeepPotentialModel class

To make the plugin more user-friendly, we have created a Python class called DeepPotentialModel. This class wraps the C++ interface and raw force object DeepmdForce, and provides several methods to facilitate the use of the plugin.

Creating a DeepPotentialModel object

To create a DeepPotentialModel object, use the following code:

dp_model = DeepPotentialModel(dp_model_file, Lambda=1.0)

Here, dp_model_file refers to the path of the Deep Potential model file. The parameter Lambda is used for interpolating the Deep Potential model. The output forces and energy values from the Deep Potential model are multiplied by Lambda before being added into the OpenMM context. By default, the value of Lambda is set to 1.0.

Setting the unit transformation coefficients

To set the unit transformation coefficients, use the following code:

dp_model.setUnitTransformCoefficients(coord_coefficient, force_coefficient, energy_coefficient)

In OpenMM, the units for coordinates, forces, and energy are nanometers, kJ/(mol*nm), and kJ/mol, respectively. However, the Deep Potential models have their own units, which are determined by the training data. To make the Deep Potential model compatible with OpenMM, three coefficients are needed for transforming the units.

Creating an OpenMM System object with the Deep Potential model

To create an OpenMM System object with the Deep Potential model, use the following code:

dp_system = dp_model.createSystem(topology)

The returned dp_system is an OpenMM System object with the Deep Potential model.

Passing part of the system to the Deep Potential model

To pass part of the system to the Deep Potential model, use the following code:

dp_force = dp_model.addParticlesToDPRegion(dp_particles, topology, particleNameLabeler="element")

The returned dp_force is the Force object that can be added to the simulation system.

Adapting the selection of particles to be passed to the Deep Potential model

To adaptively select the particles to be passed to the Deep Potential model, use the following code:

dp_force = dp_model.addCenterParticlesToAdaptiveDPRegion(center_particles, topology, sel_num4each_type=None, radius=0.35, atom_names_to_add_forces=None, extend_residues=True)

Here, addCenterParticlesToAdaptiveDPRegion dynamically selects the center_particles and their surrounding particles (within a distance less than radius) to the Deep Potential models.

The returned dp_force is the Force object that can be added to the simulation system.

Application Scenarios

Conventional Simulations with Deep Potential

To construct a DP simulation with OpenMM, use the following three lines of code:

dp_model = DeepPotentialModel(dp_model_file)
dp_model.setUnitTransformCoefficients(coord_coeffi, force_coeffi, energy_coeffi)
dp_system = dp_model.createSystem(topology)

Here, dp_model_file is the path to the Deep Potential model file. coord_coeffi, force_coeffi, and energy_coeffi are the coefficients for unit transformation between the DP model and OpenMM. topology is the OpenMM Topology object of the whole system.

For more practical examples, refer to the provided simulation scripts:

DP/MM or DP+MM Simulations

To perform DP/MM simulations with fixed or adaptive DP regions, use the methods addParticlesToDPRegion and addCenterParticlesToAdaptiveDPRegion.

For example, to include ligand intramolecular interactions governed by the DP models, use addParticlesToDPRegion. If we select the ligand particles as the center particles in addCenterParticlesToAdaptiveDPRegion, the adaptively selected DP region could include protein residues and water molecules that directly interacted with the ligand.

For more practical usage of these two methods, refer to the provided simulation scripts:

Alchemical Simulations with Deep Potential

To perform alchemical simulations with the DP models based, combine lambda and addParticlesToDPRegion. A detailed description about the alchemical simulations protocol with the DP models could be refered to the paper.

For an example script about alchemical simulation for water's hydration-free energy calculation, refer to test_deepmd_alchemical.py.

Citing OpenMM Deepmd Plugin

To cite this repository:

@article{ding2024dp,
  title={DP/MM: A Hybrid Model for Zinc--Protein Interactions in Molecular Dynamics},
  author={Ding, Ye and Huang, Jing},
  journal={The Journal of Physical Chemistry Letters},
  volume={15},
  number={2},
  pages={616--627},
  year={2024},
  publisher={ACS Publications}
}

@article{ding2024implementation,
  title={Implementation and validation of an openmm plugin for the deep potential representation of potential energy},
  author={Ding, Ye and Huang, Jing},
  journal={International Journal of Molecular Sciences},
  volume={25},
  number={3},
  pages={1448},
  year={2024},
  publisher={MDPI}
}