From 49212e5fd2e17980996e90293c881fe73f2b7148 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 10 Aug 2021 06:13:24 -0400 Subject: [PATCH 1/8] add LAMMPS plugin - Breaking changes: update fix_dplr to support lammps/lammps#2560. Old version may be not supported. No idea how to support both versions. - Feature: support LAMMPS's new plugin package. See document for details. - update document and examples for LAMMPS plugin. The old installation method will be still supported, although it's removed from documents. - LAMMPS hasn't released a stable version, but I think it will be released in one or two months. --- .github/workflows/build_cc.yml | 16 +------ doc/getting-started.md | 6 +++ doc/install.md | 48 +++++++++---------- doc/troubleshooting/index.md | 1 - examples/water/lmp/in.lammps | 3 +- source/install/build_cc.sh | 2 +- source/install/build_lammps.sh | 32 ++----------- source/lmp/CMakeLists.txt | 2 + source/lmp/fix_dplr.cpp | 6 ++- source/lmp/plugin/CMakeLists.txt | 75 ++++++++++++++++++++++++++++++ source/lmp/plugin/deepmdplugin.cpp | 50 ++++++++++++++++++++ 11 files changed, 166 insertions(+), 75 deletions(-) create mode 100644 source/lmp/plugin/CMakeLists.txt create mode 100644 source/lmp/plugin/deepmdplugin.cpp diff --git a/.github/workflows/build_cc.yml b/.github/workflows/build_cc.yml index d94c49d180..115bdb017f 100644 --- a/.github/workflows/build_cc.yml +++ b/.github/workflows/build_cc.yml @@ -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: @@ -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 diff --git a/doc/getting-started.md b/doc/getting-started.md index 0965b72661..8b2cc0bac7 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -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 +``` + #### pair_style `deepmd` The DeePMD-kit package provides the pair_style `deepmd` diff --git a/doc/install.md b/doc/install.md index 89c2d4ad2c..b878b0739a 100644 --- a/doc/install.md +++ b/doc/install.md @@ -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 @@ -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). | If the cmake has executed successfully, then ```bash @@ -215,46 +216,39 @@ 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 +wget https://github.com/lammps/lammps/archive/patch_30Jul2021.tar.gz +tar xf patch_30Jul2021.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 +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 -cd lammps-stable_29Oct2020/src/ -cp -r $deepmd_source_dir/source/build/USER-DEEPMD . +mkdir -p lammps-patch_30Jul2021/build/ +cd lammps-patch_30Jul2021/build/ ``` -Now build LAMMPS +Now build LAMMPS. Note that `PLUGIN` and `KSPACE` package must be enabled, and `BUILD_SHARED_LIBS` must be set to `yes`. ```bash -make yes-kspace -make yes-user-deepmd -make mpi -j4 -``` - -If everything works fine, you will end up with an executable `lmp_mpi`. -```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 ``` -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 diff --git a/doc/troubleshooting/index.md b/doc/troubleshooting/index.md index 1c7d642355..55c2aa8b1b 100644 --- a/doc/troubleshooting/index.md +++ b/doc/troubleshooting/index.md @@ -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 diff --git a/examples/water/lmp/in.lammps b/examples/water/lmp/in.lammps index a501a5ab27..f0c89ea68e 100644 --- a/examples/water/lmp/in.lammps +++ b/examples/water/lmp/in.lammps @@ -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 diff --git a/source/install/build_cc.sh b/source/install/build_cc.sh index dafcde7606..382735b6c6 100755 --- a/source/install/build_cc.sh +++ b/source/install/build_cc.sh @@ -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 .. make -j${NPROC} make install diff --git a/source/install/build_lammps.sh b/source/install/build_lammps.sh index 28002dde6f..9e932b51cd 100755 --- a/source/install/build_lammps.sh +++ b/source/install/build_lammps.sh @@ -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 @@ -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 CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ../cmake make -j${NPROC} make install diff --git a/source/lmp/CMakeLists.txt b/source/lmp/CMakeLists.txt index 2c68352385..6a2334e363 100644 --- a/source/lmp/CMakeLists.txt +++ b/source/lmp/CMakeLists.txt @@ -15,3 +15,5 @@ _add_lmp_variant("${LOW_PREC_VARIANT}" "${LOW_PREC_DEF}") configure_file("pair_deepmd.h.in" "pair_deepmd.h" @ONLY) configure_file("lammps_install_list.txt.in" "lammps_install_list.txt" @ONLY) + +add_subdirectory(plugin) \ No newline at end of file diff --git a/source/lmp/fix_dplr.cpp b/source/lmp/fix_dplr.cpp index fc633190ec..34d436f8ac 100644 --- a/source/lmp/fix_dplr.cpp +++ b/source/lmp/fix_dplr.cpp @@ -41,7 +41,9 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg) efield_fsum_all(4, 0.0), efield_force_flag(0) { - virial_flag = 1; + // lammps/lammps#2560 + energy_global_flag = 1; + virial_global_flag = 1; if (strcmp(update->unit_style,"metal") != 0) { error->all(FLERR,"Pair deepmd requires metal unit, please set it by \"units metal\""); @@ -117,7 +119,7 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg) int FixDPLR::setmask() { int mask = 0; - mask |= THERMO_ENERGY; + // THERMO_ENERGY removed in lammps/lammps#2560 mask |= POST_INTEGRATE; mask |= PRE_FORCE; mask |= POST_FORCE; diff --git a/source/lmp/plugin/CMakeLists.txt b/source/lmp/plugin/CMakeLists.txt new file mode 100644 index 0000000000..4be7686722 --- /dev/null +++ b/source/lmp/plugin/CMakeLists.txt @@ -0,0 +1,75 @@ +if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) + add_library(lammps_interface INTERFACE) + if (DEFINED LAMMPS_VERSION) + include(ExternalProject) + ExternalProject_Add(lammps_download + GIT_REPOSITORY https://github.com/lammps/lammps + GIT_TAG ${LAMMPS_VERSION} + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/lammps-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/lammps-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + ) + set(LAMMPS_SOURCE_ROOT ${CMAKE_CURRENT_BINARY_DIR}/lammps-src) + 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() + + 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 libdeepmd_op 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 + COMPILE_DEFINITIONS ${prec_def} + INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}" + LINK_FLAGS "-rdynamic" + ) + + 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() \ No newline at end of file diff --git a/source/lmp/plugin/deepmdplugin.cpp b/source/lmp/plugin/deepmdplugin.cpp new file mode 100644 index 0000000000..ace1dc93bd --- /dev/null +++ b/source/lmp/plugin/deepmdplugin.cpp @@ -0,0 +1,50 @@ +/** +* See https://docs.lammps.org/Developer_plugins.html +*/ +#include "lammpsplugin.h" +#include "version.h" +#include "pair_deepmd.h" +#include "fix_dplr.h" +#include "compute_deeptensor_atom.h" + +using namespace LAMMPS_NS; + +static Pair *pairdeepmd(LAMMPS *lmp) +{ + return new PairDeepMD(lmp); +} + +static Compute *computedeepmdtensoratom(LAMMPS *lmp, int narg, char **arg) +{ + return new ComputeDeeptensorAtom(lmp, narg, arg); +} + +static Fix *fixdplr(LAMMPS *lmp, int narg, char **arg) +{ + return new FixDPLR(lmp, narg, arg); +} + +extern "C" void lammpsplugin_init(void *lmp, void *handle, void *regfunc) +{ + lammpsplugin_t plugin; + lammpsplugin_regfunc register_plugin = (lammpsplugin_regfunc) regfunc; + + plugin.version = LAMMPS_VERSION; + plugin.style = "pair"; + plugin.name = "deepmd"; + plugin.info = "deepmd pair style v2.0"; + plugin.author = "Han Wang"; + plugin.creator.v1 = (lammpsplugin_factory1 *) &pairdeepmd; + plugin.handle = handle; + (*register_plugin)(&plugin, lmp); + + plugin.style = "compute"; + plugin.info = "compute deeptensor/atom v2.0"; + plugin.creator.v1 = (lammpsplugin_factory1 *) &computedeepmdtensoratom; + (*register_plugin)(&plugin, lmp); + + plugin.style = "fix"; + plugin.info = "fix dplr v2.0"; + plugin.creator.v1 = (lammpsplugin_factory1 *) &fixdplr; + (*register_plugin)(&plugin, lmp); +} \ No newline at end of file From c097b11b20f87ea6ca9384fdc1c9ed55d5377721 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 10 Aug 2021 16:01:57 -0400 Subject: [PATCH 2/8] fix include path --- source/lmp/compute_deeptensor_atom.h | 4 ++++ source/lmp/fix_dplr.h | 5 +++++ source/lmp/pair_deepmd.h.in | 4 ++++ source/lmp/plugin/CMakeLists.txt | 1 + 4 files changed, 14 insertions(+) diff --git a/source/lmp/compute_deeptensor_atom.h b/source/lmp/compute_deeptensor_atom.h index 2499e5fda0..60bafc5d7c 100644 --- a/source/lmp/compute_deeptensor_atom.h +++ b/source/lmp/compute_deeptensor_atom.h @@ -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 { diff --git a/source/lmp/fix_dplr.h b/source/lmp/fix_dplr.h index 6de055529b..cdb5b36b1f 100644 --- a/source/lmp/fix_dplr.h +++ b/source/lmp/fix_dplr.h @@ -10,8 +10,13 @@ FixStyle(dplr,FixDPLR) #include #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 diff --git a/source/lmp/pair_deepmd.h.in b/source/lmp/pair_deepmd.h.in index d2716dc1d2..6c2373e114 100644 --- a/source/lmp/pair_deepmd.h.in +++ b/source/lmp/pair_deepmd.h.in @@ -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 #include diff --git a/source/lmp/plugin/CMakeLists.txt b/source/lmp/plugin/CMakeLists.txt index 4be7686722..270014ea14 100644 --- a/source/lmp/plugin/CMakeLists.txt +++ b/source/lmp/plugin/CMakeLists.txt @@ -62,6 +62,7 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) ${libname} PROPERTIES COMPILE_DEFINITIONS ${prec_def} + COMPILE_DEFINITIONS "LMPPLUGIN" # fix header path INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}" LINK_FLAGS "-rdynamic" ) From e424369bee84cb6c749a356e942aef9fec071442 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 10 Aug 2021 22:32:33 -0400 Subject: [PATCH 3/8] add compatibility --- source/CMakeLists.txt | 10 ---------- source/install/build_cc.sh | 2 +- source/install/build_lammps.sh | 2 +- source/lmp/CMakeLists.txt | 9 +++++++-- source/lmp/env.sh.in | 2 +- source/lmp/fix_dplr.cpp | 7 +++++++ source/lmp/plugin/CMakeLists.txt | 32 ++++++++++++++++++++------------ source/lmp/pppm_dplr.cpp | 3 ++- source/lmp/pppm_dplr.h | 3 ++- 9 files changed, 41 insertions(+), 29 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 414b5b1564..7a5600f5f9 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -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) diff --git a/source/install/build_cc.sh b/source/install/build_cc.sh index 382735b6c6..967576106d 100755 --- a/source/install/build_cc.sh +++ b/source/install/build_cc.sh @@ -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} -D LAMMPS_INSTALL_RPATH=ON -DINSTALL_TENSORFLOW=TRUE ${CUDA_ARGS} -DLAMMPS_VERSION=patch_30Jul2021 .. +cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D LAMMPS_INSTALL_RPATH=ON -DINSTALL_TENSORFLOW=TRUE ${CUDA_ARGS} -DLAMMPS_VERSION=patch_30Jul2021 -DUSE_TTM=TRUE .. make -j${NPROC} make install diff --git a/source/install/build_lammps.sh b/source/install/build_lammps.sh index 9e932b51cd..c11aa2c733 100755 --- a/source/install/build_lammps.sh +++ b/source/install/build_lammps.sh @@ -25,7 +25,7 @@ fi 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_PLUGIN=ON -D PKG_KSPACE=ON -D BUILD_SHARED_LIBS=yes -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ../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 diff --git a/source/lmp/CMakeLists.txt b/source/lmp/CMakeLists.txt index 6a2334e363..bb89a6357b 100644 --- a/source/lmp/CMakeLists.txt +++ b/source/lmp/CMakeLists.txt @@ -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) @@ -15,5 +22,3 @@ _add_lmp_variant("${LOW_PREC_VARIANT}" "${LOW_PREC_DEF}") configure_file("pair_deepmd.h.in" "pair_deepmd.h" @ONLY) configure_file("lammps_install_list.txt.in" "lammps_install_list.txt" @ONLY) - -add_subdirectory(plugin) \ No newline at end of file diff --git a/source/lmp/env.sh.in b/source/lmp/env.sh.in index c29519913c..4bfe613f16 100644 --- a/source/lmp/env.sh.in +++ b/source/lmp/env.sh.in @@ -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" diff --git a/source/lmp/fix_dplr.cpp b/source/lmp/fix_dplr.cpp index 34d436f8ac..bfb2b9f543 100644 --- a/source/lmp/fix_dplr.cpp +++ b/source/lmp/fix_dplr.cpp @@ -41,9 +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\""); @@ -119,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; diff --git a/source/lmp/plugin/CMakeLists.txt b/source/lmp/plugin/CMakeLists.txt index 270014ea14..ee9ac92863 100644 --- a/source/lmp/plugin/CMakeLists.txt +++ b/source/lmp/plugin/CMakeLists.txt @@ -1,18 +1,17 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) add_library(lammps_interface INTERFACE) if (DEFINED LAMMPS_VERSION) - include(ExternalProject) - ExternalProject_Add(lammps_download + cmake_minimum_required(VERSION 3.11) + include(FetchContent) + FetchContent_Declare(lammps_download GIT_REPOSITORY https://github.com/lammps/lammps GIT_TAG ${LAMMPS_VERSION} - SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/lammps-src" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/lammps-build" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" ) - set(LAMMPS_SOURCE_ROOT ${CMAKE_CURRENT_BINARY_DIR}/lammps-src) + 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}") @@ -28,6 +27,12 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) 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 @@ -42,7 +47,7 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) add_library(${libname} MODULE ${LMP_SRC}) - # link: libdeepmd libdeepmd_op libtensorflow_cc libtensorflow_framework + # link: libdeepmd libtensorflow_cc libtensorflow_framework target_link_libraries (${libname} PUBLIC lammps_interface ${LIB_DEEPMD_CC}${variant_name} @@ -61,11 +66,14 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) set_target_properties( ${libname} PROPERTIES - COMPILE_DEFINITIONS ${prec_def} - COMPILE_DEFINITIONS "LMPPLUGIN" # fix header path 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/) diff --git a/source/lmp/pppm_dplr.cpp b/source/lmp/pppm_dplr.cpp index 7b2fbe6e65..cd4e7da2ba 100644 --- a/source/lmp/pppm_dplr.cpp +++ b/source/lmp/pppm_dplr.cpp @@ -28,7 +28,8 @@ enum{FORWARD_IK,FORWARD_AD,FORWARD_IK_PERATOM,FORWARD_AD_PERATOM}; /* ---------------------------------------------------------------------- */ -#ifdef OLD_LMP_PPPM +#if LAMMPS_VERSION_NUMBER<20190201 +// See lammps/lammps#1165 PPPMDPLR::PPPMDPLR(LAMMPS *lmp, int narg, char **arg) : PPPM(lmp, narg, arg) #else diff --git a/source/lmp/pppm_dplr.h b/source/lmp/pppm_dplr.h index 4f22a9c621..df436170f9 100644 --- a/source/lmp/pppm_dplr.h +++ b/source/lmp/pppm_dplr.h @@ -21,7 +21,8 @@ namespace LAMMPS_NS { class PPPMDPLR : public PPPM { public: -#ifdef OLD_LMP_PPPM +#if LAMMPS_VERSION_NUMBER<20190201 +// See lammps/lammps#1165 PPPMDPLR(class LAMMPS *, int, char **); #else PPPMDPLR(class LAMMPS *); From 40724950019d0297cf5da2c71a036182ba25196a Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 10 Aug 2021 22:37:03 -0400 Subject: [PATCH 4/8] add `make install` to lammps document --- doc/install.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/install.md b/doc/install.md index b878b0739a..0225ab9a38 100644 --- a/doc/install.md +++ b/doc/install.md @@ -241,6 +241,7 @@ Now build LAMMPS. Note that `PLUGIN` and `KSPACE` package must be enabled, and ` ```bash 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 ``` If everything works fine, you will end up with an executable `${deepmd_root}/lmp`. From 4d1cffa2f19ec4e0a6591f25ec440b49c97b59b9 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 11 Aug 2021 16:46:00 -0400 Subject: [PATCH 5/8] update document for built-in mode --- doc/getting-started.md | 6 +++- doc/install.md | 44 +++++++++++++++++++++++++---- examples/water/lmp/in.lammps | 1 - examples/water/lmp/in.plugin.lammps | 28 ++++++++++++++++++ 4 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 examples/water/lmp/in.plugin.lammps diff --git a/doc/getting-started.md b/doc/getting-started.md index 8b2cc0bac7..c32bc4b31c 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -414,12 +414,16 @@ 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: +#### Enable DeePMD-kit plugin (plugin mode) + +If you are using the plugin mode, enable DeePMD-kit package in LAMMPS with `plugin` command: ``` plugin load path/to/deepmd/lib/libdeepmd_lmp.so ``` +The built-in mode doesn't need this step. + #### pair_style `deepmd` The DeePMD-kit package provides the pair_style `deepmd` diff --git a/doc/install.md b/doc/install.md index 0225ab9a38..5dab6c3aab 100644 --- a/doc/install.md +++ b/doc/install.md @@ -204,7 +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). | +| -DLAMMPS_SOURCE_ROOT=<value> | Path | - | The path to the LAMMPS source code (later than 8Apr2021). | If the cmake has executed successfully, then ```bash @@ -223,10 +223,44 @@ libdeepmd_cc.so libdeepmd_ipi.so libdeepmd_lmp.so libdeepmd_op_cu ``` ## Install third-party packages -### Install LAMMPS -DeePMD-kit provide module for running MD simulation with LAMMPS. +### Install LAMMPS's DeePMD-kit module (built-in mode) +DeePMD-kit provide module for running MD simulation with LAMMPS. Now make the DeePMD-kit module for LAMMPS. If you want to use the plugin mode instead of the built-in mode, you can directly go to the next section. +```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: +```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 . +``` +Now build LAMMPS +```bash +make yes-kspace +make yes-user-deepmd +make mpi -j4 +``` + +If everything works fine, you will end up with an executable `lmp_mpi`. +```bash +./lmp_mpi -h +``` + +The DeePMD-kit module can be removed from LAMMPS source code by +```bash +make no-user-deepmd +``` + +### Install LAMMPS (plugin mode) +Starting from `8Apr2021`, LAMMPS also provides a plugin mode, allowing one build LAMMPS and a plugin separately. You can skip the section if you are using the built-in mode. -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: +Now download the LAMMPS code (`8Apr2021` or later), and uncompress it: ```bash cd /some/workspace wget https://github.com/lammps/lammps/archive/patch_30Jul2021.tar.gz @@ -237,7 +271,7 @@ The source code of LAMMPS is stored in directory `lammps-patch_30Jul2021`. Now g mkdir -p lammps-patch_30Jul2021/build/ cd lammps-patch_30Jul2021/build/ ``` -Now build LAMMPS. Note that `PLUGIN` and `KSPACE` package must be enabled, and `BUILD_SHARED_LIBS` must be set to `yes`. +Now build LAMMPS. Note that `PLUGIN` and `KSPACE` package must be enabled, and `BUILD_SHARED_LIBS` must be set to `yes`. You can install any other package you want. ```bash 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 diff --git a/examples/water/lmp/in.lammps b/examples/water/lmp/in.lammps index f0c89ea68e..81864aad0e 100644 --- a/examples/water/lmp/in.lammps +++ b/examples/water/lmp/in.lammps @@ -11,7 +11,6 @@ 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 * * diff --git a/examples/water/lmp/in.plugin.lammps b/examples/water/lmp/in.plugin.lammps new file mode 100644 index 0000000000..c27cc54d36 --- /dev/null +++ b/examples/water/lmp/in.plugin.lammps @@ -0,0 +1,28 @@ +# bulk water + +units metal +boundary p p p +atom_style atomic + +neighbor 2.0 bin +neigh_modify every 10 delay 0 check no + +read_data water.lmp +mass 1 16 +mass 2 2 + +# load the plugin at /lib/libdeepmd_lmp.so +plugin load ../../../dp/lib/libdeepmd_lmp.so + +pair_style deepmd frozen_model.pb +pair_coeff * * + +velocity all create 330.0 23456789 + +fix 1 all nvt temp 330.0 330.0 0.5 +timestep 0.0005 +thermo_style custom step pe ke etotal temp press vol +thermo 100 +dump 1 all custom 100 water.dump id type x y z + +run 1000 From 2de6748f6a72d31c1da6ff4120f1a82ab7f06ad3 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 11 Aug 2021 16:47:16 -0400 Subject: [PATCH 6/8] revert troublesome --- doc/troubleshooting/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/troubleshooting/index.md b/doc/troubleshooting/index.md index 55c2aa8b1b..1c7d642355 100644 --- a/doc/troubleshooting/index.md +++ b/doc/troubleshooting/index.md @@ -6,6 +6,7 @@ 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 From f19856b4af92229534efb1e1dcd409b2857ebe04 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 11 Aug 2021 16:54:23 -0400 Subject: [PATCH 7/8] update docs --- doc/install.md | 3 ++- source/lmp/plugin/CMakeLists.txt | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/install.md b/doc/install.md index 5dab6c3aab..4d8168d945 100644 --- a/doc/install.md +++ b/doc/install.md @@ -204,7 +204,8 @@ 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. | -| -DLAMMPS_SOURCE_ROOT=<value> | Path | - | The path to the LAMMPS source code (later than 8Apr2021). | +| -DLAMMPS_VERSION_NUMBER=<value> | Number | `20201029` | Only neccessary for LAMMPS built-in mode. The version number of LAMMPS (yyyymmdd). | +| -DLAMMPS_SOURCE_ROOT=<value> | Path | - | Only neccessary for LAMMPS plugin mode. The path to the LAMMPS source code (later than 8Apr2021). If not assigned, the plugin mode will not be enabled. | If the cmake has executed successfully, then ```bash diff --git a/source/lmp/plugin/CMakeLists.txt b/source/lmp/plugin/CMakeLists.txt index ee9ac92863..9cca8ca771 100644 --- a/source/lmp/plugin/CMakeLists.txt +++ b/source/lmp/plugin/CMakeLists.txt @@ -1,4 +1,5 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) + message(STATUS "enable LAMMPS plugin mode") add_library(lammps_interface INTERFACE) if (DEFINED LAMMPS_VERSION) cmake_minimum_required(VERSION 3.11) @@ -81,4 +82,6 @@ if (DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) _add_lmp_plugin_variant("${HIGH_PREC_VARIANT}" "${HIGH_PREC_DEF}") _add_lmp_plugin_variant("${LOW_PREC_VARIANT}" "${LOW_PREC_DEF}") +else() + message(STATUS "disable LAMMPS plugin mode") endif() \ No newline at end of file From d50523b3d7503fa23b8cf3e6f40f01b6db3757c8 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 11 Aug 2021 20:33:29 -0400 Subject: [PATCH 8/8] Update source/install/build_cc.sh --- source/install/build_cc.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/install/build_cc.sh b/source/install/build_cc.sh index 967576106d..16a269404d 100755 --- a/source/install/build_cc.sh +++ b/source/install/build_cc.sh @@ -20,10 +20,9 @@ NPROC=$(nproc --all) BUILD_TMP_DIR=${SCRIPT_PATH}/../build mkdir -p ${BUILD_TMP_DIR} cd ${BUILD_TMP_DIR} -cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D LAMMPS_INSTALL_RPATH=ON -DINSTALL_TENSORFLOW=TRUE ${CUDA_ARGS} -DLAMMPS_VERSION=patch_30Jul2021 -DUSE_TTM=TRUE .. +cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DINSTALL_TENSORFLOW=TRUE ${CUDA_ARGS} -DLAMMPS_VERSION=patch_30Jul2021 -DUSE_TTM=TRUE .. make -j${NPROC} make install #------------------ echo "Congratulations! DeePMD-kit has been installed at ${INSTALL_PREFIX}" -