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

add LAMMPS plugin #945

Merged
merged 8 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
16 changes: 2 additions & 14 deletions .github/workflows/build_cc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ jobs:
strategy:
matrix:
include:
- float_prec: high
variant: cpu
- float_prec: low
variant: cpu
- float_prec: high
variant: cuda
- float_prec: low
variant: cuda
- variant: cpu
- variant: cuda
steps:
- uses: actions/checkout@master
with:
Expand All @@ -26,12 +20,6 @@ jobs:
if: matrix.variant == 'cuda'
- run: source/install/build_cc.sh
env:
FLOAT_PREC: ${{ matrix.float_prec }}
DP_VARIANT: ${{ matrix.variant }}
CC: gcc-7
CXX: g++-7
- run: source/install/build_lammps.sh
env:
FLOAT_PREC: ${{ matrix.float_prec }}
CC: gcc-7
CXX: g++-7
6 changes: 6 additions & 0 deletions doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ Note that the model for MD simulations is required to be compatible with the Dee

### Run MD with LAMMPS

Firstly, enable DeePMD-kit package in LAMMPS with `plugin` command:

```
plugin load path/to/deepmd/lib/libdeepmd_lmp.so
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the built-in mode is not supported by this PR. Then it is a breaking change, because all lammps input scripts have to be changed.

It is ideal to support both built-in and plugin modes. We still need the built-in mode because it is easy to use, one do not need to load the shared lib in every lammps script.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Note that I just removed built-in mode in the document, and I'll revert them.

#### pair_style `deepmd`

The DeePMD-kit package provides the pair_style `deepmd`
Expand Down
49 changes: 22 additions & 27 deletions doc/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- [Easy installation methods](#easy-installation-methods)
- [Install from source code](#install-from-source-code)
- [Install i-PI](#install-i-pi)
- [Install third-party packages](#install-third-party-packages)
- [Building conda packages](#building-conda-packages)

## Easy installation methods
Expand Down Expand Up @@ -204,6 +204,7 @@ One may add the following arguments to `cmake`:
| -DCUDA_TOOLKIT_ROOT_DIR=<value> | Path | Detected automatically | The path to the CUDA toolkit directory. |
| -DUSE_ROCM_TOOLKIT=<value> | `TRUE` or `FALSE` | `FALSE` | If `TRUE`, Build GPU support with ROCM toolkit. |
| -DROCM_ROOT=<value> | Path | Detected automatically | The path to the ROCM toolkit directory. |
| -LAMMPS_SOURCE_ROOT=<value> | Path | - | The path to the LAMMPS source code (later than 8Apr2021). |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be -DLAMMPS_SOURCE_ROOT


If the cmake has executed successfully, then
```bash
Expand All @@ -215,46 +216,40 @@ The option `-j4` means using 4 processes in parallel. You may want to use a diff
If everything works fine, you will have the following executable and libraries installed in `$deepmd_root/bin` and `$deepmd_root/lib`
```bash
$ ls $deepmd_root/bin
dp_ipi
dp_ipi dp_ipi_low
$ ls $deepmd_root/lib
libdeepmd_ipi.so libdeepmd_op.so libdeepmd.so
libdeepmd_cc_low.so libdeepmd_ipi_low.so libdeepmd_lmp_low.so libdeepmd_low.so libdeepmd_op_cuda.so libdeepmd_op.so
libdeepmd_cc.so libdeepmd_ipi.so libdeepmd_lmp.so libdeepmd_op_cuda_low.so libdeepmd_op_low.so libdeepmd.so
```

### Install LAMMPS's DeePMD-kit module
DeePMD-kit provide module for running MD simulation with LAMMPS. Now make the DeePMD-kit module for LAMMPS.
```bash
cd $deepmd_source_dir/source/build
make lammps
```
DeePMD-kit will generate a module called `USER-DEEPMD` in the `build` directory. If you need low precision version, move `env_low.sh` to `env.sh` in the directory. Now download the LAMMPS code (`29Oct2020` or later), and uncompress it:
## Install third-party packages
### Install LAMMPS
DeePMD-kit provide module for running MD simulation with LAMMPS.

DeePMD-kit will generate a module called `USER-DEEPMD` in the `build` directory. If you need low precision version, move `env_low.sh` to `env.sh` in the directory. Now download the LAMMPS code (`8Apr2021` or later), and uncompress it:
```bash
cd /some/workspace
wget https://github.com/lammps/lammps/archive/stable_29Oct2020.tar.gz
tar xf stable_29Oct2020.tar.gz
```
The source code of LAMMPS is stored in directory `lammps-stable_29Oct2020`. Now go into the LAMMPS code and copy the DeePMD-kit module like this
```bash
cd lammps-stable_29Oct2020/src/
cp -r $deepmd_source_dir/source/build/USER-DEEPMD .
wget https://github.com/lammps/lammps/archive/patch_30Jul2021.tar.gz
tar xf patch_30Jul2021.tar.gz
```
Now build LAMMPS
The source code of LAMMPS is stored in directory `lammps-patch_30Jul2021`. Now go into the LAMMPS code and create a directory called `build`
```bash
make yes-kspace
make yes-user-deepmd
make mpi -j4
mkdir -p lammps-patch_30Jul2021/build/
cd lammps-patch_30Jul2021/build/
```

If everything works fine, you will end up with an executable `lmp_mpi`.
Now build LAMMPS. Note that `PLUGIN` and `KSPACE` package must be enabled, and `BUILD_SHARED_LIBS` must be set to `yes`.
```bash
./lmp_mpi -h
cmake -D PKG_PLUGIN=ON -D PKG_KSPACE=ON -D LAMMPS_INSTALL_RPATH=ON -D BUILD_SHARED_LIBS=yes -D CMAKE_INSTALL_PREFIX=${deepmd_root} ../cmake
make -j4
make install
```

The DeePMD-kit module can be removed from LAMMPS source code by
If everything works fine, you will end up with an executable `${deepmd_root}/lmp`.
```bash
make no-user-deepmd
${deepmd_root}/lmp -h
```

## Install i-PI
### Install i-PI
The i-PI works in a client-server model. The i-PI provides the server for integrating the replica positions of atoms, while the DeePMD-kit provides a client named `dp_ipi` that computes the interactions (including energy, force and virial). The server and client communicates via the Unix domain socket or the Internet socket. A full instruction of i-PI can be found [here](http://ipi-code.org/). The source code and a complete installation instructions of i-PI can be found [here](https://github.com/i-pi/i-pi).
To use i-PI with already existing drivers, install and update using Pip:
```bash
Expand Down
1 change: 0 additions & 1 deletion doc/troubleshooting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ If other unexpected problems occur, you're welcome to contact us for help.
## Trouble shooting
- [Installation](installation.md)
- [The temperature undulates violently during early stages of MD](md-energy-undulation.md)
- [MD: cannot run LAMMPS after installing a new version of DeePMD-kit](md-version-compatibility.md)
- [Model compatability](model-compatability.md)

## Parameters setting
Expand Down
3 changes: 2 additions & 1 deletion examples/water/lmp/in.lammps
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ read_data water.lmp
mass 1 16
mass 2 2

plugin load ../../../dp/lib/libdeepmd_lmp.so
pair_style deepmd frozen_model.pb
pair_coeff
pair_coeff * *

velocity all create 330.0 23456789

Expand Down
10 changes: 0 additions & 10 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,6 @@ if (USE_TTM)
set(TTM_DEF "-DUSE_TTM")
endif (USE_TTM)

# old pppm interface
if(NOT DEFINED OLD_LMP_PPPM)
set(OLD_LMP_PPPM FALSE)
endif(NOT DEFINED OLD_LMP_PPPM)
if (OLD_LMP_PPPM)
set(OLD_LMP_PPPM_DEF "-DOLD_LMP_PPPM")
message(STATUS "Use old lammps pppm interface")
endif()
add_definitions (${OLD_LMP_PPPM_DEF})

# define build type
if ((NOT DEFINED CMAKE_BUILD_TYPE) OR CMAKE_BUILD_TYPE STREQUAL "")
set (CMAKE_BUILD_TYPE release)
Expand Down
2 changes: 1 addition & 1 deletion source/install/build_cc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ NPROC=$(nproc --all)
BUILD_TMP_DIR=${SCRIPT_PATH}/../build
mkdir -p ${BUILD_TMP_DIR}
cd ${BUILD_TMP_DIR}
cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DINSTALL_TENSORFLOW=TRUE ${CUDA_ARGS} ..
cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D LAMMPS_INSTALL_RPATH=ON -DINSTALL_TENSORFLOW=TRUE ${CUDA_ARGS} -DLAMMPS_VERSION=patch_30Jul2021 -DUSE_TTM=TRUE ..
njzjz marked this conversation as resolved.
Show resolved Hide resolved
make -j${NPROC}
make install

Expand Down
32 changes: 3 additions & 29 deletions source/install/build_lammps.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
set -e

# You need to first run ./build_cc.sh

if [ -z "$FLOAT_PREC" ]
then
FLOAT_PREC=high
fi

if [ ${FLOAT_PREC} == "high" ]; then
PREC_DEF="-DHIGH_PREC"
PREC_SUFFIX=""
else
PREC_DEF="-DLOW_PREC"
PREC_SUFFIX="_low"
fi
#------------------

SCRIPT_PATH=$(dirname $(realpath -s $0))
if [ -z "$INSTALL_PREFIX" ]
then
Expand All @@ -25,33 +9,23 @@ mkdir -p ${INSTALL_PREFIX}
echo "Installing LAMMPS to ${INSTALL_PREFIX}"
NPROC=$(nproc --all)

#------------------
# copy lammps plugin
BUILD_TMP_DIR2=${SCRIPT_PATH}/../build
cd ${BUILD_TMP_DIR2}
make lammps

#------------------

BUILD_TMP_DIR=${SCRIPT_PATH}/../build_lammps
mkdir -p ${BUILD_TMP_DIR}
cd ${BUILD_TMP_DIR}
# download LAMMMPS
LAMMPS_VERSION=stable_29Oct2020
LAMMPS_VERSION=patch_30Jul2021
if [ ! -d "lammps-${LAMMPS_VERSION}" ]
then
curl -L -o lammps.tar.gz https://github.com/lammps/lammps/archive/refs/tags/${LAMMPS_VERSION}.tar.gz
tar vxzf lammps.tar.gz
fi
curl -L -o lammps.patch https://github.com/deepmd-kit-recipes/lammps-dp-feedstock/raw/fdd954a1af4fadabe5c0dd2f3bed260a484175a4/recipe/deepmd.patch
cd ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}
patch -f -p1 < ../lammps.patch || true
mkdir -p ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/src/USER-DEEPMD
cp -r ${BUILD_TMP_DIR2}/USER-DEEPMD/* ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/src/USER-DEEPMD

cd ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}
mkdir -p ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/build
cd ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/build
cmake -C ../cmake/presets/all_off.cmake -D PKG_USER-DEEPMD=ON -D PKG_KSPACE=ON -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D CMAKE_CXX_FLAGS="${PREC_DEF} -I${INSTALL_PREFIX}/include -L${INSTALL_PREFIX}/lib -Wl,--no-as-needed -lrt -ldeepmd_cc${PREC_SUFFIX} -ltensorflow_cc -ltensorflow_framework" ../cmake
cmake -C ../cmake/presets/all_off.cmake -D PKG_PLUGIN=ON -D PKG_KSPACE=ON -D BUILD_SHARED_LIBS=yes -D LAMMPS_INSTALL_RPATH=ON -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ../cmake

make -j${NPROC}
make install
Expand Down
7 changes: 7 additions & 0 deletions source/lmp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
add_subdirectory(plugin)
if (NOT DEFINED LAMMPS_VERSION_NUMBER)
# set the default to 29 Oct 2020
set(LAMMPS_VERSION_NUMBER 20201029)
endif()
message(STATUS "LAMMPS version is ${LAMMPS_VERSION_NUMBER}")

file(GLOB LMP_HEADER *.h)
file(GLOB LMP_SRC *.cpp)
file(GLOB LMP_SHSCRIPT *.sh)
Expand Down
4 changes: 4 additions & 0 deletions source/lmp/compute_deeptensor_atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ ComputeStyle(deeptensor/atom,ComputeDeeptensorAtom)

#include "compute.h"
#include "pair_deepmd.h"
#ifdef LMPPLUGIN
#include "DeepTensor.h"
#else
#include "deepmd/DeepTensor.h"
#endif

namespace LAMMPS_NS {

Expand Down
2 changes: 1 addition & 1 deletion source/lmp/env.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"`
TF_LIBRARY_PATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -L/g"`
TF_RPATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -Wl,-rpath=/g"`

NNP_INC=" -std=c++11 -D@prec_def@ @TTM_DEF@ @OLD_LMP_PPPM_DEF@ -I$TF_INCLUDE_DIRS -I$DEEPMD_ROOT/include/ "
NNP_INC=" -std=c++11 -D@prec_def@ @TTM_DEF@ -DLAMMPS_VERSION_NUMBER=@LAMMPS_VERSION_NUMBER@ -I$TF_INCLUDE_DIRS -I$DEEPMD_ROOT/include/ "
NNP_PATH=" -L$TF_LIBRARY_PATH -L$DEEPMD_ROOT/lib"
NNP_LIB=" -Wl,--no-as-needed -l@LIB_DEEPMD_CC@@variant_name@ -ltensorflow_cc -ltensorflow_framework -Wl,-rpath=$TF_RPATH -Wl,-rpath=$DEEPMD_ROOT/lib"
9 changes: 9 additions & 0 deletions source/lmp/fix_dplr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg)
efield_fsum_all(4, 0.0),
efield_force_flag(0)
{
#if LAMMPS_VERSION_NUMBER>=20210702
// lammps/lammps#2560
energy_global_flag = 1;
virial_global_flag = 1;
#else
virial_flag = 1;
#endif

if (strcmp(update->unit_style,"metal") != 0) {
error->all(FLERR,"Pair deepmd requires metal unit, please set it by \"units metal\"");
Expand Down Expand Up @@ -117,7 +123,10 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg)
int FixDPLR::setmask()
{
int mask = 0;
#if LAMMPS_VERSION_NUMBER<20210702
// THERMO_ENERGY removed in lammps/lammps#2560
mask |= THERMO_ENERGY;
#endif
mask |= POST_INTEGRATE;
mask |= PRE_FORCE;
mask |= POST_FORCE;
Expand Down
5 changes: 5 additions & 0 deletions source/lmp/fix_dplr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ FixStyle(dplr,FixDPLR)
#include <stdio.h>
#include "fix.h"
#include "pair_deepmd.h"
#ifdef LMPPLUGIN
#include "DeepTensor.h"
#include "DataModifier.h"
#else
#include "deepmd/DeepTensor.h"
#include "deepmd/DataModifier.h"
#endif

#ifdef HIGH_PREC
#define FLOAT_PREC double
Expand Down
4 changes: 4 additions & 0 deletions source/lmp/pair_deepmd.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ PairStyle(deepmd,PairDeepMD)
#define LMP_PAIR_NNP_H

#include "pair.h"
#ifdef LMPPLUGIN
#include "DeepPot.h"
#else
#include "deepmd/DeepPot.h"
#endif
#include <iostream>
#include <fstream>

Expand Down
84 changes: 84 additions & 0 deletions source/lmp/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION)
add_library(lammps_interface INTERFACE)
if (DEFINED LAMMPS_VERSION)
cmake_minimum_required(VERSION 3.11)
include(FetchContent)
FetchContent_Declare(lammps_download
GIT_REPOSITORY https://github.com/lammps/lammps
GIT_TAG ${LAMMPS_VERSION}
)
FetchContent_GetProperties(lammps_download)
if(NOT lammps_download_POPULATED)
FetchContent_Populate(lammps_download)
set(LAMMPS_SOURCE_ROOT ${lammps_download_SOURCE_DIR})
endif()
endif()
set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_ROOT}/src)
message(STATUS "LAMMPS_HEADER_DIR is ${LAMMPS_HEADER_DIR}")

target_include_directories(lammps_interface INTERFACE ${LAMMPS_HEADER_DIR})

find_package(MPI)
if(MPI_FOUND)
set(LAMMPS_MPI_INCLUDE_DIRS ${MPI_CXX_INCLUDE_DIRS})
target_link_libraries(lammps_interface INTERFACE MPI::MPI_CXX)
else()
# Use LAMMPS serial mpi.h header
target_include_directories(lammps_interface INTERFACE "${LAMMPS_HEADER_DIR}/STUBS")
endif()

# get_lammps_version
# https://github.com/lammps/lammps/blob/c2a12f97c5f665852fb38fdd4922f7dd2e77a0a1/cmake/Modules/LAMMPSUtils.cmake#L27-L46
include(${LAMMPS_SOURCE_ROOT}/cmake/Modules/LAMMPSUtils.cmake)
get_lammps_version(${LAMMPS_HEADER_DIR}/version.h LAMMPS_VERSION_NUMBER)
set(LAMMPS_VERSION_NUMBER ${LAMMPS_VERSION_NUMBER} PARENT_SCOPE)

configure_file("../pair_deepmd.h.in" "${CMAKE_CURRENT_BINARY_DIR}/pair_deepmd.h" @ONLY)

file(GLOB LMP_SRC
deepmdplugin.cpp
../*.cpp
${LAMMPS_SOURCE_ROOT}/src/kspace.cpp # for pppm_dplr
${LAMMPS_SOURCE_ROOT}/src/KSPACE/pppm.cpp
)

function(_add_lmp_plugin_variant variant_name prec_def)
set (libname "deepmd_lmp${variant_name}")

add_library(${libname} MODULE ${LMP_SRC})

# link: libdeepmd libtensorflow_cc libtensorflow_framework
target_link_libraries (${libname} PUBLIC
lammps_interface
${LIB_DEEPMD_CC}${variant_name}
${TensorFlow_LIBRARY}
${TensorFlowFramework_LIBRARY}
)
target_include_directories(${libname} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/..
${TensorFlow_INCLUDE_DIRS}
${LAMMPS_SOURCE_ROOT}/src/PLUGIN
${LAMMPS_SOURCE_ROOT}/src/KSPACE
${LAMMPS_SOURCE_ROOT}/src
)

set_target_properties(
${libname}
PROPERTIES
INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}"
LINK_FLAGS "-rdynamic"
)
target_compile_definitions(${libname}
PUBLIC ${prec_def}
PRIVATE "LMPPLUGIN" # fix header path
PRIVATE "LAMMPS_VERSION_NUMBER=${LAMMPS_VERSION_NUMBER}"
)

install(TARGETS ${libname} DESTINATION lib/)

endfunction()
_add_lmp_plugin_variant("${HIGH_PREC_VARIANT}" "${HIGH_PREC_DEF}")
_add_lmp_plugin_variant("${LOW_PREC_VARIANT}" "${LOW_PREC_DEF}")

endif()
Loading