Skip to content

Commit e5ed740

Browse files
committed
Add optional DFT-D4 support
1 parent 91c0c03 commit e5ed740

22 files changed

Lines changed: 575 additions & 19 deletions

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ option(USE_ELPA "Enable ELPA for LCAO" ON)
3434
option(ENABLE_LIBRI "Enable LibRI for hybrid functional" OFF)
3535
option(ENABLE_LIBCOMM "Enable LibComm" OFF)
3636
option(ENABLE_PEXSI "Enable PEXSI for LCAO" OFF)
37+
option(ENABLE_DFTD4 "Enable DFT-D4 dispersion correction" OFF)
3738

3839
option(BUILD_TESTING "Build unittests" OFF)
3940
option(DEBUG_INFO "Print message to debug" OFF)
@@ -236,6 +237,18 @@ if(ENABLE_COVERAGE)
236237
add_coverage(${ABACUS_BIN_NAME})
237238
endif()
238239

240+
if(ENABLE_DFTD4)
241+
# DFTD4 requires enabling C and Fortran to work
242+
enable_language(C)
243+
enable_language(Fortran)
244+
# Avoid custom-lapack fallback when resolving DFT-D4 dependencies
245+
find_package(BLAS REQUIRED)
246+
find_package(LAPACK REQUIRED)
247+
find_package(dftd4 4.2.0 REQUIRED)
248+
if(NOT TARGET dftd4::dftd4)
249+
message(FATAL_ERROR "DFT-D4 was found, but target dftd4::dftd4 is missing.")
250+
endif()
251+
endif()
239252

240253
macro(set_if_higher VARIABLE VALUE)
241254
if(${VARIABLE} LESS ${VALUE})

source/source_estate/elecstate_print.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ void print_etot(const Magnetism& magnet,
249249
titles.push_back("E_vdwD3");
250250
energies_Ry.push_back(elec.f_en.evdw);
251251
}
252+
else if (vdw_method == "d4")
253+
{
254+
titles.push_back("E_vdwD4");
255+
energies_Ry.push_back(elec.f_en.evdw);
256+
}
252257

253258
// mohan add 20251108
254259
if (PARAM.inp.dft_plus_u)
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
add_library(
2-
vdw
3-
OBJECT
4-
vdwd2_parameters.cpp
5-
vdwd3_parameters_tab.cpp
6-
vdwd3_parameters.cpp
7-
vdwd2.cpp
8-
vdwd3.cpp
9-
vdwd3_autoset_xcname.cpp
10-
vdwd3_autoset_xcparam.cpp
11-
vdw.cpp
1+
set(vdw_sources
2+
vdwd2_parameters.cpp
3+
vdwd3_parameters_tab.cpp
4+
vdwd3_parameters.cpp
5+
vdwd2.cpp
6+
vdwd3.cpp
7+
vdwd3_autoset_xcname.cpp
8+
vdwd3_autoset_xcparam.cpp
9+
vdw.cpp
1210
)
1311

12+
if(ENABLE_DFTD4)
13+
list(APPEND vdw_sources vdwd4.cpp)
14+
endif()
15+
16+
add_library(vdw OBJECT ${vdw_sources})
17+
18+
if(ENABLE_DFTD4)
19+
target_link_libraries(vdw PUBLIC dftd4::dftd4)
20+
target_compile_definitions(vdw PUBLIC __DFTD4)
21+
endif()
22+
1423
if(ENABLE_COVERAGE)
1524
add_coverage(vdw)
1625
endif()
@@ -19,4 +28,4 @@ if(BUILD_TESTING)
1928
if(ENABLE_MPI)
2029
add_subdirectory(test)
2130
endif()
22-
endif()
31+
endif()

source/source_hamilt/module_vdw/vdw.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "vdwd3.h"
77
#include "source_base/tool_quit.h"
88

9+
#ifdef __DFTD4
10+
#include "vdwd4.h"
11+
#endif
12+
913
std::string parse_xcname(const std::string &xc_input,
1014
const std::vector<std::string> &xc_psp)
1115
{
@@ -70,6 +74,28 @@ std::unique_ptr<Vdw> make_vdw(const UnitCell &ucell,
7074
vdw_ptr->parameter().initial_parameters(parse_xcname(input.dft_functional, xc_psp), input, plog);
7175
return vdw_ptr;
7276
}
77+
else if (input.vdw_method == "d4")
78+
{
79+
#ifdef __DFTD4
80+
std::vector<std::string> xc_psp(ucell.ntype);
81+
for (int it = 0; it < ucell.ntype; it++)
82+
{
83+
xc_psp[it] = ucell.atoms[it].ncpp.xc_func;
84+
}
85+
86+
std::string xc_name = input.vdw_d4_xc;
87+
if (xc_name == "default")
88+
{
89+
xc_name = parse_xcname(input.dft_functional, xc_psp);
90+
}
91+
92+
return vdw::make_unique<Vdwd4>(ucell, xc_name, input);
93+
#else
94+
ModuleBase::WARNING_QUIT("ModuleHamiltGeneral::ModuleVDW::make_vdw",
95+
"DFT-D4 support was not enabled at build time. "
96+
"Rebuild ABACUS with -DENABLE_DFTD4=ON.");
97+
#endif
98+
}
7399
else if (input.vdw_method != "none")
74100
{
75101
ModuleBase::WARNING_QUIT("ModuleHamiltGeneral::ModuleVDW::make_vdw",

0 commit comments

Comments
 (0)