Skip to content

Commit 43e4e78

Browse files
authored
Fix DFT-D4 calculations for charged systems (#7532)
1 parent 5231beb commit 43e4e78

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

source/source_hamilt/module_vdw/test/vdw_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,9 @@ class vdwd4Test: public testing::Test
623623
{0.3, 0.25, 0.25}
624624
}}}};
625625
construct_ucell(structure,ucell);
626+
ucell.atoms[0].ncpp.zv = 4.0;
626627

628+
input.nelec = 8.0;
627629
input.vdw_method = "d4";
628630
input.vdw_d4_xc = "pbe";
629631
input.vdw_d4_model = "d4";
@@ -646,6 +648,15 @@ TEST_F(vdwd4Test, D4GetEnergy)
646648
EXPECT_NEAR(ene, -0.04998837990336073, 1E-10);
647649
}
648650

651+
TEST_F(vdwd4Test, D4GetEnergyForChargedSystem)
652+
{
653+
input.nelec = 7.0;
654+
655+
auto vdw_solver = vdw::make_vdw(ucell, input);
656+
const double ene = vdw_solver->get_energy();
657+
EXPECT_NEAR(ene, -0.04359451765256733, 1E-10);
658+
}
659+
649660
TEST_F(vdwd4Test, D4GetForce)
650661
{
651662
auto vdw_solver = vdw::make_vdw(ucell, input);

source/source_hamilt/module_vdw/vdwd4.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ Vdwd4::Vdwd4(const UnitCell& unit_in, const std::string& xc_name, const Input_pa
8686
cutoff_disp2_ = cutoff_to_bohr(input.vdw_cutoff_radius, input.vdw_radius_unit);
8787
cutoff_disp3_ = std::min(40.0, cutoff_disp2_);
8888
cutoff_cn_ = length_to_bohr(input.vdw_cn_thr, input.vdw_cn_thr_unit);
89+
90+
double valence_charge = 0.0;
91+
for (int it = 0; it < ucell_.ntype; ++it)
92+
{
93+
valence_charge += ucell_.atoms[it].ncpp.zv * ucell_.atoms[it].na;
94+
}
95+
total_charge_ = valence_charge - input.nelec;
8996
}
9097

9198
void Vdwd4::build_structure(std::vector<int>& numbers,
@@ -163,7 +170,7 @@ void Vdwd4::compute(double& energy_ha,
163170
ucell_.nat,
164171
numbers.data(),
165172
positions.data(),
166-
nullptr,
173+
&total_charge_,
167174
lattice.data(),
168175
periodic.data());
169176
check_dftd4_error(error, "dftd4_new_structure");

source/source_hamilt/module_vdw/vdwd4.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Vdwd4 : public Vdw
2323
double cutoff_disp2_ = 0.0; // Bohr, two-body dispersion cutoff
2424
double cutoff_disp3_ = 0.0; // Bohr, three-body ATM cutoff
2525
double cutoff_cn_ = 0.0; // Bohr, coordination-number cutoff
26+
double total_charge_ = 0.0; // e, total system charge (sum zv*na - nelec)
2627

2728
bool has_force_cache_ = false;
2829
bool has_stress_cache_ = false;

0 commit comments

Comments
 (0)