Skip to content

Commit c3eb4e6

Browse files
Remove GlobalC::dftu (#6694)
* add update_pot in source_estate * split update_pot in rt-TDDFT into two functions, one is the original update_pot, which is replaced by update_pot function in estate, the other is save2 function used in after_iter * fix bugs * update * update esolver_ks * small updates * small bug fixed * update E_bandgap to E_gap(k) * delete rho_restart file * change setup_parameters to print_parameters * move setup_pw.cpp to setup_pwwfc.cpp in module_pwdft * update esolver, small things * delete some old_gint codes in esolver * fix bugs, update outputs * fix bug in tests * change efermi to Efermi in fp_energy.h * fix bugs * fix exd and exc in ctrl_runner_lcao * fix cmake * fix bug * delete lcao_after_scf and lcao_before_scf * modify codes including read_wfc_nao to delete pelec, add setup_psi_lcao codes * fix bugs * delete grid integral in FORCE_STRESS * move read psi to esolver, not in psi * fix system bug * fix bug in read_wfc_nao.h * change PARAM.inp to inp * fix bugs * delete omega in pelec, remove some deepks codes to setup_deepks.cpp * remove exx redundant parameters in Hamilt_LCAO * Fix circular dependencies of header files. * fix bugs in MLALGO * refactor exx lcao * add rho_tau_lcao files * fix bug in setup_deepks * move rho_tau_lcao to source_lcao * remove old psiToRho function in elecstate_lcao and replace with new rho_tau_lcao * update estate * fix bug in ELF * fix bug * move DM outside of pelec * deal with init_dm functions * remove pelec in dftu, replace with dmk_d(ouble) and dmk_c(omplex) * fix cal_ldos with DM * update delta_spin * update delta spin run_lambda_loop * the big issue to remove dm may be caused by cal_mw_from_lambda * remove some DM * keep updating DM * refactor some DM in double_xc * now all functions can be compiled * fix it * update again * add and compile setup_dm * update fix bugs * simplify dm * one more fix * now I can compile the code * fix some bugs in EXX version * fix FORCE_STRESS * fix 2 * fix 3 * fix 4 * fix 5 * improve esolver_ks_lcao * improve again * comment out psi2rho in init_dm * clean esolver_ks_lcao * update structure_factor * update sf * update * fix sf * fix * fix structure factor * update DFTU, remove GlobalC of DFTU * remove some GlobalC * remove GlobalC, rename ModuleDFTU::DFTU class to Plus_U * keep updating DFTU * fix +U forces and stress * fix dm bug in PEXSI * fix PEXSI * Update edm.cpp * fix edm * Update hsolver_lcao.cpp * Update hsolver_lcao.cpp * keep removing dftu * update dftu, remove GlobalC * keep removing GlobalC::DFTU * remove all GlobalC::dftu * fix gpu +U * fix tests related to removal of GlobalC::dftu * fix again * fix bug that ignoring pass dftu pointer * update +U * add output of DFT+U energy term --------- Co-authored-by: Levi Zhou <[email protected]>
1 parent feb20e8 commit c3eb4e6

Some content is hidden

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

69 files changed

+929
-653
lines changed

source/source_esolver/esolver_double_xc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ void ESolver_DoubleXC<TK, TR>::before_scf(UnitCell& ucell, const int istep)
146146
this->kv,
147147
this->two_center_bundle_,
148148
this->orb_,
149-
this->dmat_base.dm,
149+
this->dmat_base.dm,
150+
&this->dftu,
150151
this->deepks,
151152
istep,
152153
this->exx_nao);
@@ -396,6 +397,7 @@ void ESolver_DoubleXC<TK, TR>::cal_force(UnitCell& ucell, ModuleBase::matrix& fo
396397
this->kv,
397398
this->pw_rho,
398399
this->solvent,
400+
this->dftu,
399401
this->deepks,
400402
this->exx_nao,
401403
&ucell.symm);

source/source_esolver/esolver_gets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ESolver_GetS : public ESolver_KS<std::complex<double>>
3737

3838
TwoCenterBundle two_center_bundle_;
3939

40-
// temporary introduced during removing GlobalC::ORB
40+
// temporary introduced
4141
LCAO_Orbitals orb_;
4242
};
4343
} // namespace ModuleESolver

source/source_esolver/esolver_ks.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "source_estate/elecstate_print.h" // print_etot
1616
#include "source_io/print_info.h" // print_parameters
1717
#include "source_psi/setup_psi.h" // mohan add 20251009
18+
#include "source_lcao/module_dftu/dftu.h" // mohan add 2025-11-07
1819

1920
namespace ModuleESolver
2021
{
@@ -281,9 +282,19 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
281282
this->pelec->nelec_spin.data());
282283

283284
// 2.2) charge mixing
285+
// SCF will continue if U is not converged for uramping calculation
286+
bool converged_u = true;
287+
// to avoid unnecessary dependence on dft+u, refactor is needed
288+
#ifdef __LCAO
289+
if (PARAM.inp.dft_plus_u)
290+
{
291+
converged_u = this->dftu.u_converged();
292+
}
293+
#endif
294+
284295
module_charge::chgmixing_ks(iter, ucell, this->pelec, this->chr, this->p_chgmix,
285296
this->pw_rhod->nrxx, this->drho, this->oscillate_esolver, conv_esolver, hsolver_error,
286-
this->scf_thr, this->scf_ene_thr, PARAM.inp);
297+
this->scf_thr, this->scf_ene_thr, converged_u, PARAM.inp);
287298

288299
// 2.3) Update potentials (should be done every SF iter)
289300
elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver);

source/source_esolver/esolver_ks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "source_estate/module_charge/charge_mixing.h" // use charge mixing
88
#include "source_psi/psi.h" // use electronic wave functions
99
#include "source_hamilt/hamilt.h" // use Hamiltonian
10+
#include "source_lcao/module_dftu/dftu.h" // mohan add 20251107
1011

1112
namespace ModuleESolver
1213
{
@@ -61,6 +62,9 @@ class ESolver_KS : public ESolver_FP
6162
//! Electronic wavefunctions
6263
psi::Psi<T>* psi = nullptr;
6364

65+
//! DFT+U method, mohan add 2025-11-07
66+
Plus_U dftu;
67+
6468
std::string basisname; //! esolver_ks_lcao.cpp
6569
double esolver_KS_ne = 0.0; //! number of electrons
6670
double diag_ethr; //! the threshold for diagonalization

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "source_lcao/hs_matrix_k.hpp" // there may be multiple definitions if using hpp
55
#include "source_estate/module_charge/symmetry_rho.h"
66
#include "source_lcao/LCAO_domain.h" // need DeePKS_init
7-
#include "source_lcao/module_dftu/dftu.h"
87
#include "source_lcao/FORCE_STRESS.h"
98
#include "source_estate/elecstate_lcao.h"
109
#include "source_lcao/hamilt_lcao.h"
@@ -99,8 +98,7 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(UnitCell& ucell, const Input_pa
9998
// 9) initialize DFT+U
10099
if (inp.dft_plus_u)
101100
{
102-
auto* dftu = ModuleDFTU::DFTU::get_instance();
103-
dftu->init(ucell, &this->pv, this->kv.get_nks(), &orb_);
101+
this->dftu.init(ucell, &this->pv, this->kv.get_nks(), &orb_);
104102
}
105103

106104
// 10) init local pseudopotentials
@@ -189,7 +187,7 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(UnitCell& ucell, const int istep)
189187
{
190188
this->p_hamilt = new hamilt::HamiltLCAO<TK, TR>(
191189
ucell, this->gd, &this->pv, this->pelec->pot, this->kv,
192-
two_center_bundle_, orb_, this->dmat.dm, this->deepks, istep, exx_nao);
190+
two_center_bundle_, orb_, this->dmat.dm, &this->dftu, this->deepks, istep, exx_nao);
193191
}
194192

195193
// 9) for each ionic step, the overlap <phi|alpha> must be rebuilt
@@ -274,7 +272,7 @@ void ESolver_KS_LCAO<TK, TR>::cal_force(UnitCell& ucell, ModuleBase::matrix& for
274272
this->gd, this->pv, this->pelec, this->dmat, this->psi,
275273
two_center_bundle_, orb_, force, this->scs,
276274
this->locpp, this->sf, this->kv,
277-
this->pw_rho, this->solvent, this->deepks,
275+
this->pw_rho, this->solvent, this->dftu, this->deepks,
278276
this->exx_nao, &ucell.symm);
279277

280278
// delete RA after cal_force
@@ -338,7 +336,8 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(UnitCell& ucell, const int istep, const
338336
// call iter_init() of ESolver_KS
339337
ESolver_KS<TK>::iter_init(ucell, istep, iter);
340338

341-
module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, this->dmat.dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp);
339+
module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, this->dftu,
340+
this->dmat.dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp);
342341

343342
// mohan update 2012-06-05
344343
this->pelec->f_en.deband_harris = this->pelec->cal_delta_eband(ucell);
@@ -377,10 +376,10 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(UnitCell& ucell, const int istep, const
377376
{
378377
if (istep != 0 || iter != 1)
379378
{
380-
GlobalC::dftu.set_dmr(this->dmat.dm);
379+
this->dftu.set_dmr(this->dmat.dm);
381380
}
382381
// Calculate U and J if Yukawa potential is used
383-
GlobalC::dftu.cal_slater_UJ(ucell, this->chr.rho, this->pw_rho->nrxx);
382+
this->dftu.cal_slater_UJ(ucell, this->chr.rho, this->pw_rho->nrxx);
384383
}
385384

386385
#ifdef __MLALGO
@@ -489,18 +488,18 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
489488
// 1) calculate the local occupation number matrix and energy correction in DFT+U
490489
if (PARAM.inp.dft_plus_u)
491490
{
492-
// only old DFT+U method should calculated energy correction in esolver,
493-
// new DFT+U method will calculate energy in calculating Hamiltonian
491+
// old DFT+U method calculates energy correction in esolver,
492+
// new DFT+U method calculates energy in Hamiltonian
494493
if (PARAM.inp.dft_plus_u == 2)
495494
{
496-
if (GlobalC::dftu.omc != 2)
495+
if (this->dftu.omc != 2)
497496
{
498-
ModuleDFTU::dftu_cal_occup_m(iter, ucell, dm_vec, this->kv,
499-
this->p_chgmix->get_mixing_beta(), hamilt_lcao);
497+
dftu_cal_occup_m(iter, ucell, dm_vec, this->kv,
498+
this->p_chgmix->get_mixing_beta(), hamilt_lcao, this->dftu);
500499
}
501-
GlobalC::dftu.cal_energy_correction(ucell, istep);
500+
this->dftu.cal_energy_correction(ucell, istep);
502501
}
503-
GlobalC::dftu.output(ucell);
502+
this->dftu.output(ucell);
504503
}
505504

506505
// 2) for deepks, calculate delta_e, output labels during electronic steps
@@ -532,7 +531,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
532531
// use the converged occupation matrix for next MD/Relax SCF calculation
533532
if (PARAM.inp.dft_plus_u && conv_esolver)
534533
{
535-
GlobalC::dftu.initialed_locale = true;
534+
this->dftu.initialed_locale = true;
536535
}
537536

538537
// control the output related to the finished iteration
@@ -566,7 +565,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep, const
566565
//! 2) output of lcao every few ionic steps
567566
ModuleIO::ctrl_scf_lcao<TK, TR>(ucell,
568567
PARAM.inp, this->kv, this->pelec, this->dmat.dm, this->pv,
569-
this->gd, this->psi, hamilt_lcao, this->two_center_bundle_,
568+
this->gd, this->psi, hamilt_lcao, this->dftu, this->two_center_bundle_,
570569
this->orb_, this->pw_wfc, this->pw_rho, this->pw_big, this->sf,
571570
this->rdmft_solver, this->deepks, this->exx_nao,
572571
this->conv_esolver, this->scf_nmax_flag, istep);

source/source_esolver/esolver_ks_lcao.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,34 @@ class ESolver_KS_LCAO : public ESolver_KS<TK>
6969
//! GintInfo: used to store some basic infomation about module_gint
7070
std::unique_ptr<ModuleGint::GintInfo> gint_info_;
7171

72+
//! NAO: store related information
73+
LCAO_Orbitals orb_;
74+
7275
//! NAO orbitals: two-center integrations
7376
TwoCenterBundle two_center_bundle_;
7477

7578
//! Add density matrix class, mohan add 2025-10-30
7679
LCAO_domain::Setup_DM<TK> dmat;
7780

81+
82+
// For deepks method, mohan add 2025-10-08
83+
Setup_DeePKS<TK> deepks;
84+
85+
// For exact-exchange energy, mohan add 2025-10-08
86+
Exx_NAO<TK> exx_nao;
87+
7888
//! For RDMFT calculations, added by jghan, 2024-03-16
7989
rdmft::RDMFT<TK, TR> rdmft_solver;
8090

81-
//! NAO: store related information
82-
LCAO_Orbitals orb_;
91+
//! For linear-response TDDFT
92+
friend class LR::ESolver_LR<double, double>;
93+
friend class LR::ESolver_LR<std::complex<double>, double>;
8394

8495
// Temporarily store the stress to unify the interface with PW,
8596
// because it's hard to seperate force and stress calculation in LCAO.
8697
ModuleBase::matrix scs;
8798
bool have_force = false;
8899

89-
// deepks method, mohan add 2025-10-08
90-
Setup_DeePKS<TK> deepks;
91-
92-
// exact-exchange energy, mohan add 2025-10-08
93-
Exx_NAO<TK> exx_nao;
94-
95-
friend class LR::ESolver_LR<double, double>;
96-
friend class LR::ESolver_LR<std::complex<double>, double>;
97-
98100

99101
public:
100102
const Record_adj & get_RA() const { return RA; }

source/source_esolver/esolver_ks_pw.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ ESolver_KS_PW<T, Device>::~ESolver_KS_PW()
5555
template <typename T, typename Device>
5656
void ESolver_KS_PW<T, Device>::allocate_hamilt(const UnitCell& ucell)
5757
{
58-
this->p_hamilt = new hamilt::HamiltPW<T, Device>(this->pelec->pot, this->pw_wfc, &this->kv, &this->ppcell, &ucell);
58+
this->p_hamilt = new hamilt::HamiltPW<T, Device>(
59+
this->pelec->pot,
60+
this->pw_wfc,
61+
&this->kv,
62+
&this->ppcell,
63+
&this->dftu,
64+
&ucell);
5965
}
6066

6167
template <typename T, typename Device>
@@ -133,8 +139,10 @@ void ESolver_KS_PW<T, Device>::before_scf(UnitCell& ucell, const int istep)
133139
this->allocate_hamilt(ucell);
134140

135141
//! Setup potentials (local, non-local, sc, +U, DFT-1/2)
142+
// note: init DFT+U is done here for pw basis for every scf iteration, however,
143+
// init DFT+U is done in "before_all_runners" in LCAO basis. This should be refactored, mohan note 2025-11-06
136144
pw::setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid,
137-
this->chr, this->locpp, this->ppcell, this->vsep_cell,
145+
this->chr, this->locpp, this->ppcell, this->dftu, this->vsep_cell,
138146
this->stp.psi_t, this->p_hamilt, this->pw_wfc, this->pw_rhod, PARAM.inp);
139147

140148
// setup psi (electronic wave functions)
@@ -162,7 +170,7 @@ void ESolver_KS_PW<T, Device>::iter_init(UnitCell& ucell, const int istep, const
162170
ESolver_KS<T, Device>::iter_init(ucell, istep, iter);
163171

164172
// 2) perform charge mixing for KSDFT using pw basis
165-
module_charge::chgmixing_ks_pw(iter, this->p_chgmix, PARAM.inp);
173+
module_charge::chgmixing_ks_pw(iter, this->p_chgmix, this->dftu, PARAM.inp);
166174

167175
// 3) mohan move harris functional here, 2012-06-05
168176
// use 'rho(in)' and 'v_h and v_xc'(in)
@@ -172,14 +180,13 @@ void ESolver_KS_PW<T, Device>::iter_init(UnitCell& ucell, const int istep, const
172180
// should before lambda loop in DeltaSpin
173181
if (PARAM.inp.dft_plus_u && (iter != 1 || istep != 0))
174182
{
175-
auto* dftu = ModuleDFTU::DFTU::get_instance();
176183
// only old DFT+U method should calculate energy correction in esolver,
177184
// new DFT+U method will calculate energy when evaluating the Hamiltonian
178-
if (dftu->omc != 2)
185+
if (this->dftu.omc != 2)
179186
{
180-
dftu->cal_occ_pw(iter, this->stp.psi_t, this->pelec->wg, ucell, PARAM.inp.mixing_beta);
187+
this->dftu.cal_occ_pw(iter, this->stp.psi_t, this->pelec->wg, ucell, PARAM.inp.mixing_beta);
181188
}
182-
dftu->output(ucell);
189+
this->dftu.output(ucell);
183190
}
184191
}
185192

@@ -389,7 +396,7 @@ void ESolver_KS_PW<T, Device>::cal_force(UnitCell& ucell, ModuleBase::matrix& fo
389396

390397
// Calculate forces
391398
ff.cal_force(ucell, force, *this->pelec, this->pw_rhod, &ucell.symm,
392-
&this->sf, this->solvent, &this->locpp, &this->ppcell,
399+
&this->sf, this->solvent, &this->dftu, &this->locpp, &this->ppcell,
393400
&this->kv, this->pw_wfc, this->stp.psi_d);
394401
}
395402

@@ -401,7 +408,7 @@ void ESolver_KS_PW<T, Device>::cal_stress(UnitCell& ucell, ModuleBase::matrix& s
401408
// mohan add 2025-10-12
402409
this->stp.update_psi_d();
403410

404-
ss.cal_stress(stress, ucell, this->locpp, this->ppcell, this->pw_rhod,
411+
ss.cal_stress(stress, ucell, this->dftu, this->locpp, this->ppcell, this->pw_rhod,
405412
&ucell.symm, &this->sf, &this->kv, this->pw_wfc, this->stp.psi_d);
406413

407414
// external stress

source/source_esolver/esolver_of.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,10 @@ double ESolver_OF::cal_energy()
575575
void ESolver_OF::cal_force(UnitCell& ucell, ModuleBase::matrix& force)
576576
{
577577
Forces<double> ff(ucell.nat);
578-
ff.cal_force(ucell, force, *pelec, this->pw_rho, &ucell.symm, &sf, this->solvent, &this->locpp);
578+
579+
// here nullptr is for DFT+U, which may cause bugs, mohan note 2025-11-07
580+
// solvent can be used? mohan ask 2025-11-07
581+
ff.cal_force(ucell, force, *pelec, this->pw_rho, &ucell.symm, &sf, this->solvent, nullptr, &this->locpp);
579582
}
580583

581584
/**

source/source_esolver/lcao_others.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ void ESolver_KS_LCAO<TK, TR>::others(UnitCell& ucell, const int istep)
182182
two_center_bundle_,
183183
orb_,
184184
this->dmat.dm,
185+
&this->dftu,
185186
this->deepks,
186187
istep,
187188
this->exx_nao);

source/source_estate/elecstate_energy_terms.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "source_estate/module_pot/gatefield.h"
55
#include "source_lcao/module_deepks/LCAO_deepks.h"
66
#include "source_lcao/module_deltaspin/spin_constrain.h"
7-
#include "source_lcao/module_dftu/dftu.h"
7+
#include "source_lcao/module_dftu/dftu.h" // mohan add 2025-11-06
88

99
namespace elecstate
1010
{
@@ -36,7 +36,7 @@ double ElecState::get_solvent_model_Acav()
3636

3737
double ElecState::get_dftu_energy()
3838
{
39-
return GlobalC::dftu.get_energy();
39+
return Plus_U::get_energy();
4040
}
4141

4242
double ElecState::get_local_pp_energy()
@@ -52,4 +52,4 @@ double ElecState::get_local_pp_energy()
5252
return local_pseudopot_energy;
5353
}
5454

55-
} // namespace elecstate
55+
} // namespace elecstate

0 commit comments

Comments
 (0)