From 482006e045694c0d9f97ccb146c6c5c5817e47a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conrad=20H=C3=BCbler?= Date: Mon, 9 Jan 2023 11:09:16 +0100 Subject: [PATCH 1/5] fix compile error, add cmake and functions for interfacing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Conrad Hübler --- CMakeLists.txt | 66 +++++++++++++++++++++++++++++++++++++++++ include/dftd_cblas.h | 4 +-- include/dftd_geometry.h | 47 +++++++++++++++++++++++++++++ src/program_dftd.cpp | 4 +-- 4 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..59b7565 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.18) +project(cpp-d4) + +find_package(LAPACKE REQUIRED) +find_package(CBLAS REQUIRED) + + +IF(CMAKE_COMPILER_IS_GNUCXX) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-reorder -pedantic \ + -Wextra -Wcast-align -Wcast-qual -Wchar-subscripts \ + -Wcomment -Wdisabled-optimization \ + -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security\ + -Wformat-y2k -Wimport -Winit-self -Winline -Winvalid-pch\ + -Wunsafe-loop-optimizations -Wmissing-braces\ + -Wmissing-field-initializers -Wmissing-format-attribute \ + -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses\ + -Wpointer-arith -Wredundant-decls -Wsequence-point\ + -Wsign-compare -Wstack-protector -Wstrict-aliasing\ + -Wstrict-aliasing=2 -Wswitch -Wsuggest-override\ + -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code\ + -Wunused -Wunused-function -Wunused-label -Wunused-parameter\ + -Wunused-value -Wunused-variable -Wvariadic-macros\ + -Wvolatile-register-var -Wwrite-strings -Wdeprecated-declarations\ + -Wno-error=unused-local-typedefs -Wno-error=enum-compare -Wno-narrowing -Werror=return-type -g") +set (GCC ON) +if(WIN32) # Check if we are on Windows +else() + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic") +endif(WIN32) +ENDIF(CMAKE_COMPILER_IS_GNUCXX) +include_directories(${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR} ) + + +set(cpp_d4_SRC + src/dftd_cutoff.cpp + src/dftd_damping.cpp + src/dftd_dispersion.cpp + src/dftd_eeq.cpp + src/dftd_model.cpp + src/dftd_ncoord.cpp + src/dftd_readxyz.cpp + ) + +add_library(libcpp_d4 ${cpp_d4_SRC}) + +add_executable(cpp-d4 + src/program_dftd.cpp +) + +add_executable(cpp-d4_test + test/main.cpp + test/test_grad.cpp + test/molecules.cpp + test/test_disp.cpp + test/test_ncoord.cpp + test/test_param.cpp + test/util.cpp + +) +target_link_libraries(cpp-d4 libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) +target_link_libraries(cpp-d4_test libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) + +if(WIN32) # Check if we are on Windows +else() + target_link_libraries(cpp-d4 dl ) +endif(WIN32) diff --git a/include/dftd_cblas.h b/include/dftd_cblas.h index 2188575..de16726 100644 --- a/include/dftd_cblas.h +++ b/include/dftd_cblas.h @@ -19,10 +19,10 @@ #include "dftd_matrix.h" -extern "C" { +//extern "C" { #include "cblas.h" #include "lapacke.h" -} +//} namespace dftd4 { diff --git a/include/dftd_geometry.h b/include/dftd_geometry.h index b2487cf..940bcd9 100644 --- a/include/dftd_geometry.h +++ b/include/dftd_geometry.h @@ -42,6 +42,53 @@ class TMolecule { xyz.Delete(); at.Delete(); } + + /* set structure by array of coords (double), atom types (int) and number of atoms */ + /* Geometry has to be in bohrs */ + void SetGeometry(const double* coord, const int* attype, int number) + { + GetMemory(number); + for(int i = 0; i < NAtoms; ++i) + { + at(i) = attype[i]; + xyz(i, 0) = coord[3*i + 0]; + xyz(i, 1) = coord[3*i + 1]; + xyz(i, 2) = coord[3*i + 2]; + } + } + + /* update the geometry */ + /* Geometry has to be in bohrs */ + void UpdateGeometry(const double* coord) + { + for(int i = 0; i < NAtoms; ++i) + { + xyz(i, 0) = coord[3*i + 0]; + xyz(i, 1) = coord[3*i + 1]; + xyz(i, 2) = coord[3*i + 2]; + } + } + + /* update the position of a single atom (index, coord and element) */ + /* Geometry has to be in bohrs */ + void UpdateAtom(int index, double x, double y, double z, int element = -1) + { + //std::cout << x << " " << y << " " << z << std::endl; + if(element != -1) + at(index) = element; + xyz(index, 0) = x; + xyz(index, 1) = y; + xyz(index, 2) = z; + } + + void Print() const + { + for(int i = 0; i < NAtoms; ++i) + { + std::cout << at(i) << " " << xyz(i, 0) << " " << xyz(i, 1) << " " << xyz(i, 2) << std::endl; + + } + } }; } // namespace dftd4 diff --git a/src/program_dftd.cpp b/src/program_dftd.cpp index c6e399e..531b362 100644 --- a/src/program_dftd.cpp +++ b/src/program_dftd.cpp @@ -153,9 +153,9 @@ int main(int argc, char **argv) { dftd4::TCutoff cutoff; // molecular information - dftd4::TMolInfo dat = dftd4::TMolInfo(charge); + //dftd4::TMolInfo dat = dftd4::TMolInfo(charge); - info = dftd4::get_dispersion(dat, mol, par, cutoff, energy, nullptr); + info = dftd4::get_dispersion(mol, charge, par, cutoff, energy, nullptr); if (info != 0) return EXIT_FAILURE; std::cout << "Dispersion energy: " << energy << " Eh\n"; From 5f715930e76feaa7a54d935b5042242a6f6eebe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conrad=20H=C3=BCbler?= Date: Thu, 12 Jan 2023 20:37:43 +0100 Subject: [PATCH 2/5] find cblas and lapacke without cmake files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Conrad Hübler --- CMakeLists.txt | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59b7565..afda72f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,11 @@ cmake_minimum_required(VERSION 3.18) project(cpp-d4) +set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) -find_package(LAPACKE REQUIRED) -find_package(CBLAS REQUIRED) +if(NOT DEFINED LIBS_DIR AND NOT DEFINED INCLUDE_DIR) + find_package(LAPACKE REQUIRED) + find_package(CBLAS CONFIG REQUIRED) +endif(NOT DEFINED LIBS_DIR AND NOT DEFINED INCLUDE_DIR) IF(CMAKE_COMPILER_IS_GNUCXX) @@ -29,6 +32,9 @@ else() endif(WIN32) ENDIF(CMAKE_COMPILER_IS_GNUCXX) include_directories(${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR} ) +if(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) + include_directories(${INCLUDE_DIR}) +endif(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) set(cpp_d4_SRC @@ -55,10 +61,18 @@ add_executable(cpp-d4_test test/test_ncoord.cpp test/test_param.cpp test/util.cpp - ) -target_link_libraries(cpp-d4 libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) -target_link_libraries(cpp-d4_test libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) + +if(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) + link_directories(${LIBS_DIR}) + + target_link_libraries(cpp-d4 libcpp_d4 cblas lapacke pthread) + target_link_libraries(cpp-d4_test libcpp_d4 cblas lapacke pthread) +else() + target_link_libraries(cpp-d4 libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) + target_link_libraries(cpp-d4_test libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) +endif(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) + if(WIN32) # Check if we are on Windows else() From 493f99eaa9e3e76be572b51518f54dec05e22b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conrad=20H=C3=BCbler?= Date: Thu, 12 Jan 2023 21:04:25 +0100 Subject: [PATCH 3/5] adopt cmake file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Conrad Hübler --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index afda72f..38e3c80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,9 @@ endif(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) set(cpp_d4_SRC + src/damping/atm.cpp + src/damping/rational.cpp + src/dftd_cutoff.cpp src/dftd_damping.cpp src/dftd_dispersion.cpp From 9e35b5f906d92978c81c96348c717df0e3a08d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conrad=20H=C3=BCbler?= Date: Thu, 19 Jan 2023 23:14:46 +0100 Subject: [PATCH 4/5] update Readme and CMakeLists.txt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Conrad Hübler --- CMakeLists.txt | 23 ++++++++++++--------- README.md | 44 ++++++++++++++++++++++++++++++++++++++++- include/dftd_geometry.h | 1 - 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 38e3c80..fb92db5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.18) project(cpp-d4) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) -if(NOT DEFINED LIBS_DIR AND NOT DEFINED INCLUDE_DIR) +if(NOT DEFINED D4LIBS_DIR AND NOT DEFINED D4INCLUDE_DIR) find_package(LAPACKE REQUIRED) find_package(CBLAS CONFIG REQUIRED) -endif(NOT DEFINED LIBS_DIR AND NOT DEFINED INCLUDE_DIR) +endif(NOT DEFINED D4LIBS_DIR AND NOT DEFINED D4INCLUDE_DIR) IF(CMAKE_COMPILER_IS_GNUCXX) @@ -31,16 +31,16 @@ else() SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic") endif(WIN32) ENDIF(CMAKE_COMPILER_IS_GNUCXX) + include_directories(${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR} ) -if(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) - include_directories(${INCLUDE_DIR}) -endif(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) +if(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) + include_directories(${D4INCLUDE_DIR}) +endif(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) set(cpp_d4_SRC src/damping/atm.cpp src/damping/rational.cpp - src/dftd_cutoff.cpp src/dftd_damping.cpp src/dftd_dispersion.cpp @@ -66,16 +66,21 @@ add_executable(cpp-d4_test test/util.cpp ) -if(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) - link_directories(${LIBS_DIR}) +if(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) + link_directories(${D4LIBS_DIR}) target_link_libraries(cpp-d4 libcpp_d4 cblas lapacke pthread) target_link_libraries(cpp-d4_test libcpp_d4 cblas lapacke pthread) else() target_link_libraries(cpp-d4 libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) target_link_libraries(cpp-d4_test libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} pthread) -endif(DEFINED LIBS_DIR AND DEFINED INCLUDE_DIR) +endif(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) +include(CTest) +add_test(NAME disp COMMAND cpp-d4_test disp) +add_test(NAME grad COMMAND cpp-d4_test grad) +add_test(NAME ncoord COMMAND cpp-d4_test ncoord) +add_test(NAME param COMMAND cpp-d4_test param) if(WIN32) # Check if we are on Windows else() diff --git a/README.md b/README.md index 9acae29..d0e9243 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,11 @@ This project is a port of the [`dftd4`](https://github.com/dftd4/dftd4) project to C++ and provides the D4(EEQ)-ATM method. -## Building This Project +## Requirements +cpp-d4 depends on cblas and lapacke, the C implementation of blas and lapack. + +## Building This Project This project is build with `meson`, to setup and perform a build run: ```bash @@ -21,6 +24,45 @@ Run the test suite with: meson test -C _build --print-errorlogs ``` +In addition to `meson`, one can use CMake to build cpp-d4 and include it into external projects (see for example [https://github.com/conradhuebler/curcuma](https://github.com/conradhuebler/curcuma)). + +```bash +cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. +make +``` +The tests can be started with + +```bash +make test +``` +In case, cblas and lapacke can not be found using cmake (which for example happens on Ubuntu), one can specify one include and library path for both dependencies. + +```bash +cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. -DD4LIBS_DIR=/usr/lib/ -DD4INCLUDE_DIR=/usr/include/ +``` + +```bash +cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. -DD4LIBS_DIR=$(find /usr -name 'cblas.so' |head -n1 |sed 's/cblas.so//g') -DD4INCLUDE_DIR=$(find /usr -name 'cblas.h' |head -n1 |sed 's/cblas.h//g') +``` + +Using CMake it is then easy to include cpp-d4 in an own project. + +```cmake +if(NOT DEFINED D4LIBS_DIR AND NOT DEFINED D4INCLUDE_DIR) + find_package(LAPACKE REQUIRED) + find_package(CBLAS CONFIG REQUIRED) +else() + include_directories(${INCLUDE_DIR}) +endif(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) + +add_subdirectory(cpp-d4) + +if(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) + target_link_libraries(yourproject PUBLIC libcpp_d4 cblas lapacke ) +else() + target_link_libraries(yourproject PUBLIC libcpp_d4 ${LAPACKE_LIBRARIES} ${CBLAS_LIBRARIES} ) +endif(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) +``` ## Citations - E. Caldeweyher, C. Bannwarth and S. Grimme, _J. Chem. Phys._, **2017**, 147, 034112. DOI: [10.1063/1.4993215](https://dx.doi.org/10.1063/1.4993215) diff --git a/include/dftd_geometry.h b/include/dftd_geometry.h index d179f6f..7d7dab6 100644 --- a/include/dftd_geometry.h +++ b/include/dftd_geometry.h @@ -86,7 +86,6 @@ class TMolecule { for(int i = 0; i < NAtoms; ++i) { std::cout << at(i) << " " << xyz(i, 0) << " " << xyz(i, 1) << " " << xyz(i, 2) << std::endl; - } } }; From 95882c7203e3b847528d0746a92b184f4cef4dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conrad=20H=C3=BCbler?= Date: Thu, 19 Jan 2023 23:21:45 +0100 Subject: [PATCH 5/5] update CMakeLists.txt for upstream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Conrad Hübler --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb92db5..5c3a85e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ add_executable(cpp-d4_test test/test_grad.cpp test/molecules.cpp test/test_disp.cpp + test/test_disp2.cpp test/test_ncoord.cpp test/test_param.cpp test/util.cpp @@ -77,11 +78,14 @@ else() endif(DEFINED D4LIBS_DIR AND DEFINED D4INCLUDE_DIR) include(CTest) + +add_test(NAME disp2 COMMAND cpp-d4_test disp) add_test(NAME disp COMMAND cpp-d4_test disp) add_test(NAME grad COMMAND cpp-d4_test grad) add_test(NAME ncoord COMMAND cpp-d4_test ncoord) add_test(NAME param COMMAND cpp-d4_test param) + if(WIN32) # Check if we are on Windows else() target_link_libraries(cpp-d4 dl )