From c131c8f7d2632f9692226e12769eaa6d4934f336 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 11 Feb 2024 05:06:53 -0500 Subject: [PATCH] ipi: remove normalize_coord (#3257) (1) Remove `normalize_coord` and add a test to confirm it still works; (2) Remove the include of `SimulationRegion.h` and the direct link to `libdeepmd.so`; (3) Allow using a pre-compiled C library to build `dp_ipi`. --------- Signed-off-by: Jinzhe Zeng --- doc/install/install-from-c-library.md | 5 ++-- source/CMakeLists.txt | 5 +--- source/ipi/CMakeLists.txt | 2 +- source/ipi/driver.cc | 23 --------------- source/ipi/tests/test_driver.py | 40 +++++++++++++++++++++++---- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/doc/install/install-from-c-library.md b/doc/install/install-from-c-library.md index 36944f03e6..f1a5496b59 100644 --- a/doc/install/install-from-c-library.md +++ b/doc/install/install-from-c-library.md @@ -14,7 +14,7 @@ tar xzf libdeepmd_c.tar.gz The library is built in Linux (GLIBC 2.17) with CUDA 12.2 (`libdeepmd_c.tar.gz`) or 11.8 (`libdeepmd_c_cu11.tar.gz`). It's noted that this package does not contain CUDA Toolkit and cuDNN, so one needs to download them from the NVIDIA website. -## Use Pre-compiled C Library to build the LAMMPS plugin and GROMACS patch +## Use Pre-compiled C Library to build the LAMMPS plugin, i-PI driver, and GROMACS patch When one [installs DeePMD-kit's C++ interface](./install-from-source.md#install-deepmd-kits-c-interface), one can use the CMake argument `DEEPMD_C_ROOT` to the path `libdeepmd_c`. @@ -27,4 +27,5 @@ make -j8 make install ``` -Then one can follow the manual [Install LAMMPS](./install-lammps.md) and/or [Install GROMACS](./install-gromacs.md). +Then the i-PI driver `dp_ipi` will be built and installed. +One can also follow the manual [Install LAMMPS](./install-lammps.md) and/or [Install GROMACS](./install-gromacs.md). diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index a4971a4d2a..41e596f85e 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -297,10 +297,7 @@ if(BUILD_CPP_IF) endif() if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8) # add_subdirectory (md/) - if(ENABLE_IPI - OR NOT BUILD_PY_IF - AND NOT DEEPMD_C_ROOT) - # ipi has a dependency on libdeepmd + if(ENABLE_IPI OR NOT BUILD_PY_IF) add_subdirectory(ipi/) endif() if(NOT BUILD_PY_IF) diff --git a/source/ipi/CMakeLists.txt b/source/ipi/CMakeLists.txt index 158f98aea5..5d571a1950 100644 --- a/source/ipi/CMakeLists.txt +++ b/source/ipi/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable(${ipiname} ${DRIVER_SOURCE_FILES}) # link: libdeepmd_cc if(DP_USING_C_API) # SimulationRegion.h - target_link_libraries(${ipiname} PRIVATE ${LIB_DEEPMD_C} ${LIB_DEEPMD}) + target_link_libraries(${ipiname} PRIVATE ${LIB_DEEPMD_C}) target_precompile_headers(${ipiname} PRIVATE [["deepmd.hpp"]]) remove_definitions(-D_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}) else() diff --git a/source/ipi/driver.cc b/source/ipi/driver.cc index 1e3d92eb5e..9a91a27ad3 100644 --- a/source/ipi/driver.cc +++ b/source/ipi/driver.cc @@ -12,7 +12,6 @@ namespace deepmd_compat = deepmd; #include "deepmd.hpp" namespace deepmd_compat = deepmd::hpp; #endif -#include "SimulationRegion.h" #include "XyzFileManager.h" #include "json.hpp" #include "sockets.h" @@ -49,25 +48,6 @@ char *trimwhitespace(char *str) { return str; } -void normalize_coord(std::vector &coord, - const SimulationRegion ®ion) { - int natoms = coord.size() / 3; - - for (int ii = 0; ii < natoms; ++ii) { - double inter[3]; - region.phys2Inter(inter, &coord[3 * ii]); - for (int dd = 0; dd < 3; ++dd) { - inter[dd] -= int(floor(inter[dd])); - if (inter[dd] < 0) { - inter[dd] += 1.; - } else if (inter[dd] >= 1) { - inter[dd] -= 1.; - } - } - region.inter2Phys(&coord[3 * ii], inter); - } -} - int main(int argc, char *argv[]) { if (argc == 1) { std::cerr << "usage " << std::endl; @@ -122,7 +102,6 @@ int main(int argc, char *argv[]) { std::vector dcoord_tmp; std::vector dtype = cvt.get_type(); std::vector dbox(9, 0); - SimulationRegion region; double *msg_buff = NULL; double ener; double virial[9]; @@ -179,7 +158,6 @@ int main(int argc, char *argv[]) { for (int dd = 0; dd < 9; ++dd) { dbox[dd] = cell_h[(dd % 3) * 3 + (dd / 3)] * cvt_len; } - region.reinitBox(&dbox[0]); // get number of atoms readbuffer_(&socket, (char *)(&cbuf), sizeof(int32_t)); @@ -203,7 +181,6 @@ int main(int argc, char *argv[]) { dcoord_tmp[ii] = msg_buff[ii] * cvt_len; } cvt.forward(dcoord, dcoord_tmp, 3); - normalize_coord(dcoord, region); // nnp over writes ener, force and virial nnp_inter.compute(dener, dforce_tmp, dvirial, dcoord, dtype, dbox); diff --git a/source/ipi/tests/test_driver.py b/source/ipi/tests/test_driver.py index 78edeac977..1b2e1dd951 100644 --- a/source/ipi/tests/test_driver.py +++ b/source/ipi/tests/test_driver.py @@ -53,7 +53,7 @@ def write_input(self, atoms, **kwargs): atoms.write(self.xyz_file, format="xyz") -class TestDeepPotALargeBoxNoPBC(unittest.TestCase): +class TestDPIPI(unittest.TestCase): # copy from test_deeppot_a.py @classmethod def setUpClass(cls): @@ -193,8 +193,8 @@ def test_ase_unix(self): cell=self.box.reshape((3, 3)), calculator=calc, ) - ee = water.get_potential_energy() - ff = water.get_forces() + ee = water.get_potential_energy() + ff = water.get_forces() nframes = 1 np.testing.assert_almost_equal( ff.ravel(), self.expected_f.ravel(), default_places @@ -213,8 +213,38 @@ def test_ase_nounix(self): cell=self.box.reshape((3, 3)), calculator=calc, ) - ee = water.get_potential_energy() - ff = water.get_forces() + ee = water.get_potential_energy() + ff = water.get_forces() + nframes = 1 + np.testing.assert_almost_equal( + ff.ravel(), self.expected_f.ravel(), default_places + ) + expected_se = np.sum(self.expected_e.reshape([nframes, -1]), axis=1) + np.testing.assert_almost_equal(ee.ravel(), expected_se.ravel(), default_places) + + def test_normalize_coords(self): + # coordinate nomarlization should happen inside the interface + cell = self.box.reshape((3, 3)) + coord = self.coords.reshape((-1, 3)) + # random unwrap coords + coord[0] += np.array([3, 0, 0]) @ cell + coord[1] += np.array([0, -3, 0]) @ cell + coord[2] += np.array([0, 0, 3]) @ cell + coord[3] += np.array([-3, 0, 0]) @ cell + coord[4] += np.array([0, 3, 0]) @ cell + coord[5] += np.array([0, 0, -3]) @ cell + with SocketIOCalculator( + DPiPICalculator(self.model_file, use_unix=False), + log=sys.stdout, + ) as calc: + water = Atoms( + "OHHOHH", + positions=coord, + cell=cell, + calculator=calc, + ) + ee = water.get_potential_energy() + ff = water.get_forces() nframes = 1 np.testing.assert_almost_equal( ff.ravel(), self.expected_f.ravel(), default_places