From f7b87c384c1ca256973464e4549c27999c0f3b59 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 2 Oct 2023 23:16:25 -0400 Subject: [PATCH] cmake: support LAMMPS in built-in mode; remove kspace requirement (#2891) Fix https://github.com/deepmodeling/deepmd-kit/discussions/2875. Add documentation and supporting file to install LAMMPS in built-in mode with CMake. Compile required C++ files from the kspace package (pppm.cpp, ffft3d.cpp, ffft3d_wrap.cpp, remap.cpp, remap_wrap.cpp) in the CMake files for both built-in and plugin modes, so there is no need to set PKG_KSAPCE=ON. --------- Signed-off-by: Jinzhe Zeng --- doc/install/install-lammps.md | 41 +++++++++++++++++++++++++++++--- source/install/build_lammps.sh | 2 +- source/lmp/builtin.cmake | 31 ++++++++++++++++++++++++ source/lmp/plugin/CMakeLists.txt | 13 +++++++--- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 source/lmp/builtin.cmake diff --git a/doc/install/install-lammps.md b/doc/install/install-lammps.md index d9d6a28bc6..e643660cd1 100644 --- a/doc/install/install-lammps.md +++ b/doc/install/install-lammps.md @@ -17,7 +17,13 @@ cd /some/workspace wget https://github.com/lammps/lammps/archive/stable_2Aug2023_update1.tar.gz tar xf stable_2Aug2023_update1.tar.gz ``` -The source code of LAMMPS is stored in the directory `lammps-stable_2Aug2023_update1`. Now go into the LAMMPS code and copy the DeePMD-kit module like this +The source code of LAMMPS is stored in the directory `lammps-stable_2Aug2023_update1`. + +Then, you can [build LAMMPS](https://docs.lammps.org/Build.html) with either make or CMake. + +### With make + +Now go into the LAMMPS code and copy the DeePMD-kit module like this ```bash cd lammps-stable_2Aug2023_update1/src/ cp -r $deepmd_source_dir/source/build/USER-DEEPMD . @@ -40,6 +46,35 @@ The DeePMD-kit module can be removed from the LAMMPS source code by make no-user-deepmd ``` +### With CMake + +Now go into the LAMMPS directory and create a directory called `build`: + +```bash +mkdir -p lammps-stable_2Aug2023_update1/build/ +cd lammps-stable_2Aug2023_update1/build/ +``` + +Patch the LAMMPS `CMakeLists.txt` file: + +```bash +echo "include(${deepmd_source_dir}/source/lmp/builtin.cmake)" >> ../cmake/CMakeLists.txt +``` + +It's expected to see one extra line in the end of `CMakeLists.txt`. + +Now build LAMMPS. You can install any other package you want. +```bash +cmake -D LAMMPS_INSTALL_RPATH=ON -D BUILD_SHARED_LIBS=yes -D CMAKE_INSTALL_PREFIX=${deepmd_root} -DCMAKE_PREFIX_PATH=${deepmd_root} ../cmake +make -j4 +make install +``` + +If everything works fine, you will end up with an executable `${deepmd_root}/bin/lmp`. +```bash +${deepmd_root}/bin/lmp -h +``` + ## Install LAMMPS (plugin mode) Starting from `8Apr2021`, LAMMPS also provides a plugin mode, allowing one to build LAMMPS and a plugin separately. @@ -56,9 +91,9 @@ The source code of LAMMPS is stored in the directory `lammps-stable_2Aug2023_upd mkdir -p lammps-stable_2Aug2023_update1/build/ cd lammps-stable_2Aug2023_update1/build/ ``` -Now build LAMMPS. Note that `PLUGIN` and `KSPACE` packages must be enabled, and `BUILD_SHARED_LIBS` must be set to `yes`. You can install any other package you want. +Now build LAMMPS. Note that `PLUGIN` 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} -D CMAKE_INSTALL_LIBDIR=lib -D CMAKE_INSTALL_FULL_LIBDIR=${deepmd_root}/lib ../cmake +cmake -D PKG_PLUGIN=ON -D LAMMPS_INSTALL_RPATH=ON -D BUILD_SHARED_LIBS=yes -D CMAKE_INSTALL_PREFIX=${deepmd_root} -D CMAKE_INSTALL_LIBDIR=lib -D CMAKE_INSTALL_FULL_LIBDIR=${deepmd_root}/lib ../cmake make -j4 make install ``` diff --git a/source/install/build_lammps.sh b/source/install/build_lammps.sh index 6798212086..c8cfa6ea54 100755 --- a/source/install/build_lammps.sh +++ b/source/install/build_lammps.sh @@ -23,7 +23,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 PKG_MOLECULE=ON -DLAMMPS_EXCEPTIONS=yes -D BUILD_SHARED_LIBS=yes -D LAMMPS_INSTALL_RPATH=ON -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D CMAKE_INSTALL_LIBDIR=lib -D CMAKE_INSTALL_FULL_LIBDIR=${INSTALL_PREFIX}/lib ../cmake +cmake -C ../cmake/presets/all_off.cmake -D PKG_PLUGIN=ON -D PKG_MOLECULE=ON -DLAMMPS_EXCEPTIONS=yes -D BUILD_SHARED_LIBS=yes -D LAMMPS_INSTALL_RPATH=ON -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D CMAKE_INSTALL_LIBDIR=lib -D CMAKE_INSTALL_FULL_LIBDIR=${INSTALL_PREFIX}/lib ../cmake make -j${NPROC} make install diff --git a/source/lmp/builtin.cmake b/source/lmp/builtin.cmake new file mode 100644 index 0000000000..507fe7bf1a --- /dev/null +++ b/source/lmp/builtin.cmake @@ -0,0 +1,31 @@ +# This file should be included in the end of +# ${LAMMPS_SOURCE_DIR}/cmake/CMakeLists.txt +# include(/path/to/deepmd_source/source/lmp/builtin.cmake) + +# assume LAMMPS CMake file has been executed, so these target/variables exist: +# lammps LAMMPS_SOURCE_DIR get_lammps_version + +get_lammps_version(${LAMMPS_SOURCE_DIR}/version.h LAMMPS_VERSION_NUMBER) + +configure_file("${CMAKE_CURRENT_LIST_DIR}/deepmd_version.h.in" + "${CMAKE_CURRENT_BINARY_DIR}/deepmd_version.h" @ONLY) + +file(GLOB DEEPMD_LMP_SRC ${CMAKE_CURRENT_LIST_DIR}/*.cpp) + +find_package(DeePMD REQUIRED) +target_sources( + lammps + PRIVATE ${DEEPMD_LMP_SRC} + ${LAMMPS_SOURCE_DIR}/KSPACE/pppm.cpp # for pppm_dplr + ${LAMMPS_SOURCE_DIR}/KSPACE/fft3d.cpp + ${LAMMPS_SOURCE_DIR}/KSPACE/fft3d_wrap.cpp + ${LAMMPS_SOURCE_DIR}/KSPACE/remap.cpp + ${LAMMPS_SOURCE_DIR}/KSPACE/remap_wrap.cpp + ${LAMMPS_SOURCE_DIR}/EXTRA-FIX/fix_ttm.cpp # for ttm +) +target_link_libraries(lammps PUBLIC DeePMD::deepmd_c) +target_include_directories( + lammps PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR} + ${LAMMPS_SOURCE_DIR}/KSPACE ${LAMMPS_SOURCE_DIR}/EXTRA-FIX) +target_compile_definitions( + lammps PRIVATE "LAMMPS_VERSION_NUMBER=${LAMMPS_VERSION_NUMBER}") diff --git a/source/lmp/plugin/CMakeLists.txt b/source/lmp/plugin/CMakeLists.txt index 9b5f68b574..bfc2253412 100644 --- a/source/lmp/plugin/CMakeLists.txt +++ b/source/lmp/plugin/CMakeLists.txt @@ -49,9 +49,16 @@ if(DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) configure_file("../deepmd_version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/deepmd_version.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) + file( + GLOB + LMP_SRC + deepmdplugin.cpp + ../*.cpp + ${LAMMPS_SOURCE_ROOT}/src/KSPACE/pppm.cpp # for pppm_dplr + ${LAMMPS_SOURCE_ROOT}/src/KSPACE/fft3d.cpp + ${LAMMPS_SOURCE_ROOT}/src/KSPACE/fft3d_wrap.cpp + ${LAMMPS_SOURCE_ROOT}/src/KSPACE/remap.cpp + ${LAMMPS_SOURCE_ROOT}/src/KSPACE/remap_wrap.cpp) if(LAMMPS_VERSION_NUMBER GREATER 20210831) list(APPEND LMP_SRC ${LAMMPS_SOURCE_ROOT}/src/EXTRA-FIX/fix_ttm.cpp )# for ttm