Skip to content

Commit 0aa8d8b

Browse files
mohanchenabacus_fixer
andauthored
Refactor DFT+U: rename and de-static Plus_U members, step 3 (deepmodeling#7859)
* Refactor DFT+U: rename and de-static Plus_U members - Rename U -> u_current, U0 -> u_target, omc -> occ_mat_ctrl, Yukawa -> use_yukawa - Rename accessors: get_hubbard_u -> get_u_current, get_hubbard_u0 -> get_u_target - Remove static qualifier from all 9 Plus_U_Base members (u_current, u_target, occ_mat_ctrl, use_yukawa, energy_u, orbital_corr, uramping, mixing_dftu, nspin) - Convert static accessors (get_energy, set_energy, get_u_current, etc.) and is_mixing_enabled/enable_mixing to non-static member functions - Extend Plus_U_Base::init_base() and Plus_U::init() signatures to accept hubbard_u, uramping, occ_mat_ctrl, mixing_dftu; move initialization logic out of input_conv.cpp into init_base() so members are set after the Plus_U instance is constructed (no longer relies on static state) - Remove redundant Plus_U assignments from input_conv.cpp; drop its include of dftu.h (dependencies made explicit at call sites) - Introduce ElecState::set_dftu_energy() / dftu_energy_ member so get_dftu_energy() no longer depends on Plus_U::get_energy() static state; ESolver_KS_PW and ESolver_KS_LAO push the energy after DFT+U update - Replace PARAM.inp.* with inp.* in setup_pot.cpp (inp is already a parameter) - Rename Input_para::omc -> occ_mat_ctrl (INPUT keyword 'omc' preserved for backward compatibility) Verification: pending (compilation and tests to be run by user) * fix bug * fix bugs * add an example to test the old DFT+U method with only scf energy, not force and stress * update CASES_CPU.txt * delete useless members * Move LCAO-only DFT+U members from Plus_U_Base to Plus_U Seven members (yukawa_lambda, npol, nlocal, gamma_only_local, ks_solver, cal_force, cal_stress) were only read by the LCAO subclass (Plus_U) and its component files under source_lcao/module_dftu/. They are now declared in Plus_U and assigned in Plus_U::init, so Plus_U_Base no longer carries LCAO-specific mutable workflow state. Accordingly shorten Plus_U_Base::init_base signature by six parameters (yukawa_lambda, nlocal, gamma_only_local, ks_solver, cal_force, cal_stress); update the two call sites (Plus_U::init in dftu.cpp, PW path in setup_pot.cpp). Plus_U::init keeps its original signature so external callers (lcao_set.cpp) are unaffected. * Inline DFTU template header into dftu_lcao.h dftu.hpp only provided the empty primary template for hamilt::DFTU. Move it into dftu_lcao.h (the sole includer), guarded by __DFTUTEMPLATE, matching the convention used by sibling LCAO operators (nonlocal.h, ekinetic.h, meta_lcao.h, veff_lcao.h, etc.) that inline the primary template instead of using a separate .hpp file. This also aligns with AGENTS.md rule 4 (avoid .hpp implementation headers). * Rename DFT+U LCAO files to reflect roles Three LCAO DFT+U files had confusing names that hid which was the main implementation, the new operator, and the legacy operator. Rename for clarity: - dftu.h/.cpp -> dftu_lcao.h/.cpp : Plus_U class (LCAO DFT+U physics, subclass of Plus_U_Base in dftu_base.h). The name now mirrors dftu_base.h. - dftu_lcao.h/.cpp -> dftu_lcao_op.h/.cpp : hamilt::DFTU operator (new method, dft_plus_u==1, onsite_radius projections). Operators now carry the _op suffix. - op_dftu_lcao.h/.cpp -> dftu_lcao_op_legacy.h/.cpp : hamilt::OperatorDFTU operator (old method, dft_plus_u==2, NAO first zeta projections, test-only). Renamed to mark it as legacy and group it with the new operator. Class names are unchanged in this step (Plus_U, hamilt::DFTU, hamilt::OperatorDFTU); only filenames and #include paths change. Updated all ~40 includers across source_lcao, source_pw, source_esolver, source_estate, source_io, plus the module and test CMakeLists (the dftu_lcao_test links the new-operator cpp ../dftu_lcao_op.cpp, not the Plus_U cpp). * refactor: eliminate reverse dependency of dftu_lcao.h from PW/base layers Replace Plus_U (LCAO-derived class) with Plus_U_Base (PW-layer base class) in all non-LCAO interfaces. All DFT+U calls in PW/estate/test layers only use base-class members, so the derived-type reference was unnecessary. Changes: - Remove #include "source_lcao/module_dftu/dftu_lcao.h" from PW/base headers - Use #include "source_pw/module_pwdft/dftu_base.h" instead - Replace Plus_U&/Plus_U* with Plus_U_Base&/Plus_U_Base* in: * source_pw/module_pwdft: force_pw, hamilt_pw, onsite_proj, op_pw_proj, stress_func, stress_onsite, stress_pw (6 headers + 7 impls) * source_estate/module_charge: chgmixing (PW + LCAO mixing functions) * source_pw/module_stodft/test: test_hamilt_sto, test_sto_tool * source_hsolver/test: diago_mock.h (3 explicit specializations) Plus_U (derived) instances held by callers (e.g. ESolver_KS::dftu) continue to work via implicit pointer/reference upcast to Plus_U_Base. No behavioral change. --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 8925fdc commit 0aa8d8b

73 files changed

Lines changed: 1769 additions & 1637 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

source/source_esolver/esolver_ks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "source_hamilt/module_xc/xc_functional.h"
1414
#include "source_io/module_output/output_log.h" // use write_head
1515
#include "source_estate/elecstate_print.h" // print_etot
16-
#include "source_lcao/module_dftu/dftu.h" // mohan add 2025-11-07
16+
#include "source_lcao/module_dftu/dftu_lcao.h" // mohan add 2025-11-07
1717
#include "source_hamilt/module_xc/general_exx_info.h" // for init_general_exx_info
1818

1919
namespace ModuleESolver

source/source_esolver/esolver_ks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "source_hamilt/hamilt.h" // use Hamiltonian
99
#include "source_hamilt/hamilt_base.h" // use Hamiltonian base class
1010
#include "source_hamilt/module_xc/general_exx_info.h" // ESolver owns General_Exx_Info value
11-
#include "source_lcao/module_dftu/dftu.h" // mohan add 20251107
11+
#include "source_lcao/module_dftu/dftu_lcao.h" // mohan add 20251107
1212
#include "source_pw/module_pwdft/vnl_pw.h"
1313

1414
namespace ModuleESolver

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
508508
// 1) calculate the local occupation number matrix and energy correction in DFT+U
509509
finish_dftu_lcao<TK>(iter, conv_esolver, this->inp_->dft_plus_u, this->inp_->out_chg[0], &(this->dftu), ucell, dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), hamilt_lcao, PARAM.globalv.global_out_dir, this->inp_->nspin, PARAM.globalv.npol);
510510

511+
// mohan add 2025-11: push DFT+U energy from Plus_U instance to ElecState.
512+
// Covers both dft_plus_u==1 (new method, energy accumulated by DFTU::contributeHR
513+
// via cal_v_of_u) and dft_plus_u==2 (old method, energy from cal_energy_correction).
514+
if (this->inp_->dft_plus_u)
515+
{
516+
this->pelec->set_dftu_energy(this->dftu.get_energy());
517+
}
518+
511519
// 2) for deepks, calculate delta_e, output labels during electronic steps
512520
this->deepks.delta_e(ucell, this->kv, this->orb_, this->pv, this->gd, dm_vec, this->pelec->f_en, *this->inp_);
513521

source/source_esolver/esolver_ks_pw.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ void ESolver_KS_PW<T, Device>::iter_init(UnitCell& ucell, const int istep, const
210210
ucell,
211211
this->p_chgmix,
212212
this->kv.isk.data());
213+
214+
// mohan add 2025-11: push DFT+U energy from Plus_U instance to ElecState
215+
if (this->inp_->dft_plus_u)
216+
{
217+
this->pelec->set_dftu_energy(this->dftu.get_energy());
218+
}
213219
}
214220

215221
// Temporary, it should be replaced by hsolver later.

source/source_esolver/lcao_others.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "source_cell/cal_ux.h"
33
#include "source_estate/module_charge/symm_rho.h"
44
#include "source_lcao/hamilt_lcao.h"
5-
#include "source_lcao/module_dftu/dftu.h"
5+
#include "source_lcao/module_dftu/dftu_lcao.h"
66
#include "source_hamilt/module_gint/gint.h"
77
#include "source_base/formatter.h"
88
#include "source_base/timer.h"

source/source_estate/elecstate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ElecState
131131
}
132132

133133
double get_dftu_energy();
134+
void set_dftu_energy(double e) { dftu_energy_ = e; }
134135
double get_local_pp_energy();
135136

136137
fenergy f_en; ///< energies contribute to the total free energy
@@ -148,6 +149,9 @@ class ElecState
148149
public:
149150

150151
bool skip_weights = false;
152+
153+
private:
154+
double dftu_energy_ = 0.0; ///< DFT+U energy, set by ESolver via set_dftu_energy()
151155
};
152156

153157
/**

source/source_estate/estate_e_terms.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "source_estate/module_pot/h_hartree_pw.h"
44
#include "source_estate/module_pot/efield.h"
55
#include "source_estate/module_pot/gatefield.h"
6-
#include "source_lcao/module_dftu/dftu.h" // mohan add 2025-11-06
76

87
namespace elecstate
98
{
@@ -35,7 +34,7 @@ double ElecState::get_solvent_model_Acav()
3534

3635
double ElecState::get_dftu_energy()
3736
{
38-
return Plus_U::get_energy();
37+
return dftu_energy_;
3938
}
4039

4140
double ElecState::get_local_pp_energy()

source/source_estate/module_charge/chgmixing.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "source_estate/module_charge/chgmixing.h"
22
#include "source_estate/update_pot.h"
3-
#include "source_lcao/module_dftu/dftu.h"
3+
#include "source_lcao/module_dftu/dftu_lcao.h"
44
#include "source_lcao/module_deltaspin/spin_constrain.h"
55

66
void module_charge::chgmixing_ks(const int iter, // scf iteration number
@@ -119,7 +119,7 @@ void module_charge::chgmixing_ks(const int iter, // scf iteration number
119119

120120
void module_charge::chgmixing_ks_pw(const int iter, // scf iteration number
121121
Charge_Mixing* p_chgmix, // charge mixing class
122-
Plus_U &dftu, // mohan add 2025-11-06
122+
Plus_U_Base& dftu,
123123
const Input_para& inp) // input parameters
124124
{
125125
ModuleBase::TITLE("module_charge", "chgmixing_ks_pw");
@@ -167,7 +167,7 @@ void module_charge::chgmixing_ks_pw(const int iter, // scf iteration number
167167
std::cout << " U-Ramping! Current U = ";
168168
for (int i = 0; i < dftu.get_num_u_types(); i++)
169169
{
170-
std::cout << dftu.get_hubbard_u(i) * ModuleBase::Ry_to_eV << " ";
170+
std::cout << dftu.get_u_current(i) * ModuleBase::Ry_to_eV << " ";
171171
}
172172
std::cout << " eV " << std::endl;
173173
}
@@ -180,7 +180,7 @@ void module_charge::chgmixing_ks_pw(const int iter, // scf iteration number
180180

181181
void module_charge::chgmixing_ks_lcao(const int iter, // scf iteration number
182182
Charge_Mixing* p_chgmix, // charge mixing class
183-
Plus_U &dftu, // mohan add 2025-11-06
183+
Plus_U_Base& dftu,
184184
const int nnr, // dimension of density matrix
185185
const Input_para& inp) // input parameters
186186
{
@@ -202,7 +202,7 @@ void module_charge::chgmixing_ks_lcao(const int iter, // scf iteration number
202202
std::cout << " U-Ramping! Current U = ";
203203
for (int i = 0; i < dftu.get_num_u_types(); i++)
204204
{
205-
std::cout << dftu.get_hubbard_u(i) * ModuleBase::Ry_to_eV << " ";
205+
std::cout << dftu.get_u_current(i) * ModuleBase::Ry_to_eV << " ";
206206
}
207207
std::cout << " eV " << std::endl;
208208
}
@@ -221,7 +221,7 @@ void module_charge::chgmixing_ks_lcao(const int iter, // scf iteration number
221221
std::cout << " U-Ramping! Current U = ";
222222
for (int i = 0; i < dftu.get_num_u_types(); i++)
223223
{
224-
std::cout << dftu.get_hubbard_u(i) * ModuleBase::Ry_to_eV << " ";
224+
std::cout << dftu.get_u_current(i) * ModuleBase::Ry_to_eV << " ";
225225
}
226226
std::cout << " eV " << std::endl;
227227
}

source/source_estate/module_charge/chgmixing.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "source_estate/module_charge/charge_mixing.h" // use p_chgmix
77
#include "source_io/module_parameter/input_parameter.h" // use Input_para
88
#include "source_cell/unitcell.h"
9-
#include "source_lcao/module_dftu/dftu.h" // mohan add 2025-11-06
9+
#include "source_pw/module_pwdft/dftu_base.h"
1010

1111
namespace module_charge
1212
{
@@ -28,12 +28,12 @@ void chgmixing_ks(const int iter, // scf iteration number
2828

2929
void chgmixing_ks_pw(const int iter,
3030
Charge_Mixing* p_chgmix,
31-
Plus_U &dftu, // mohan add 2025-11-06
31+
Plus_U_Base& dftu,
3232
const Input_para& inp); // input parameters
3333

3434
void chgmixing_ks_lcao(const int iter, // scf iteration number
3535
Charge_Mixing* p_chgmix, // charge mixing class
36-
Plus_U &dftu, // mohan add 2025-11-06
36+
Plus_U_Base& dftu,
3737
const int nnr, // dimension of density matrix
3838
const Input_para& inp); // input parameters
3939

source/source_hsolver/test/diago_mock.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ template<> hamilt::HamiltPW<double>::HamiltPW(
577577
ModulePW::PW_Basis_K* wfc_basis,
578578
K_Vectors* pkv,
579579
pseudopot_cell_vnl* ppcell,
580-
Plus_U* p_dftu, // mohan add 20251108
580+
Plus_U_Base* p_dftu,
581581
const UnitCell* ucell,
582582
const General_Exx_Info* exx_info)
583583
{
@@ -599,7 +599,7 @@ template<> hamilt::HamiltPW<std::complex<double>>::HamiltPW(
599599
ModulePW::PW_Basis_K* wfc_basis,
600600
K_Vectors* pkv,
601601
pseudopot_cell_vnl* ppcell,
602-
Plus_U* p_dftu, // mohan add 20251108
602+
Plus_U_Base* p_dftu,
603603
const UnitCell* ucell,
604604
const General_Exx_Info* exx_info)
605605
{
@@ -621,7 +621,7 @@ template<> hamilt::HamiltPW<std::complex<float>>::HamiltPW(
621621
ModulePW::PW_Basis_K* wfc_basis,
622622
K_Vectors* pkv,
623623
pseudopot_cell_vnl* ppcell,
624-
Plus_U* p_dftu, // mohan add 20251108
624+
Plus_U_Base* p_dftu,
625625
const UnitCell* ucell,
626626
const General_Exx_Info* exx_info)
627627
{

0 commit comments

Comments
 (0)