Skip to content

Commit 374204e

Browse files
author
abacus_fixer
committed
refactor(module_dftu): thread ucell explicitly, drop Plus_U::ucell member
Remove the Plus_U private member `const UnitCell* ucell` and its `get_ucell()` accessor. The 4 read sites now receive ucell as an explicit parameter: - DFTU_LCAO::pot_uterm_{complex,real}: add `const UnitCell& ucell` param, passed through the legacy OperatorDFTU operator which now holds its own `ucell` pointer set once in the constructor. - Plus_U::cal_eff_pot_mat_R_{double,complex_double}: add `const UnitCell&` param; their only callers (sparse_format::cal_HR_dftu{,_soc}) are updated to thread ucell through. Construction sites updated to pass ucell: - hamilt_lcao.cpp (HamiltLCAO ctor, 2 sites) - write_vxc.hpp (vdftu_op_ao local, 1 site) Also fix a stale doxygen on pot_uterm_* (the Plus_U::pot_onsite_* wrappers were removed in the previous commit; the comment now reads "Calls DFTU_LCAO::pot_onsite_*"). Pure refactor, no behavior change. No INPUT parameter affected. Verification: - make -j4 (full build): 100%, linked abacus_basic_para - python3 tools/03_code_analysis/agent_governance_check.py --staged: no findings - grep: no residual Plus_U::ucell / get_ucell() references Signed-off-by: Abacus Agent <abacus@example.com>
1 parent 49f82d0 commit 374204e

10 files changed

Lines changed: 46 additions & 30 deletions

File tree

source/source_io/module_hs/write_vxc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void write_Vxc(const int nspin,
209209
&vxcs_R_ao[0],ucell,/*for paraV*/ kv, Hexxd, Hexxc, &exx_info, hamilt::Add_Hexx_Type::k);
210210
std::vector<std::vector<double>> e_orb_exx; // orbital energy (EXX)
211211
#endif
212-
hamilt::OperatorDFTU<hamilt::OperatorLCAO<TK, TR>> vdftu_op_ao(&vxc_k_ao, kv.kvec_d, nullptr, nullptr, kv.isk, PARAM.globalv.npol);
212+
hamilt::OperatorDFTU<hamilt::OperatorLCAO<TK, TR>> vdftu_op_ao(&vxc_k_ao, kv.kvec_d, nullptr, ucell, nullptr, kv.isk, PARAM.globalv.npol);
213213

214214
// 4. calculate and write the MO-matrix Exc
215215
Parallel_2D p2d;

source/source_lcao/hamilt_lcao.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ HamiltLCAO<TK, TR>::HamiltLCAO(const UnitCell& ucell,
226226
{
227227
plus_u = new OperatorDFTU<OperatorLCAO<TK, TR>>(this->hsk,
228228
this->kv->kvec_d,
229-
this->hR,
230-
p_dftu,
231-
this->kv->isk,
232-
PARAM.globalv.npol);
229+
this->hR,
230+
ucell,
231+
p_dftu,
232+
this->kv->isk,
233+
PARAM.globalv.npol);
233234
}
234235
else
235236
{
@@ -383,8 +384,9 @@ HamiltLCAO<TK, TR>::HamiltLCAO(const UnitCell& ucell,
383384
{
384385
plus_u = new OperatorDFTU<OperatorLCAO<TK, TR>>(this->hsk,
385386
this->kv->kvec_d,
386-
this->hR,
387-
p_dftu,
387+
this->hR,
388+
ucell,
389+
p_dftu,
388390
this->kv->isk,
389391
PARAM.globalv.npol);
390392
}

source/source_lcao/module_dftu/dftu_hamilt.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace DFTU_LCAO {
1010

1111
void pot_uterm_complex(Plus_U& dftu,
12+
const UnitCell& ucell,
1213
const Parallel_Orbitals* pv,
1314
const int ik,
1415
std::complex<double>* pot_uterm,
@@ -39,7 +40,7 @@ void pot_uterm_complex(Plus_U& dftu,
3940
const std::complex<double> zero = 0.0;
4041

4142
std::vector<std::complex<double>> pot_onsite(pv->nloc);
42-
DFTU_LCAO::pot_onsite_complex(dftu, dftu.get_ucell(), pv, spin, true, &pot_onsite[0], npol);
43+
DFTU_LCAO::pot_onsite_complex(dftu, ucell, pv, spin, true, &pot_onsite[0], npol);
4344

4445
#ifdef __MPI
4546
ScalapackConnector::gemm(transN, transN,
@@ -69,6 +70,7 @@ void pot_uterm_complex(Plus_U& dftu,
6970
}
7071

7172
void pot_uterm_real(Plus_U& dftu,
73+
const UnitCell& ucell,
7274
const Parallel_Orbitals* pv,
7375
const int ik,
7476
double* pot_uterm,
@@ -96,7 +98,7 @@ void pot_uterm_real(Plus_U& dftu,
9698
double alpha = 1.0, beta = 0.0, half = 0.5, one = 1.0;
9799

98100
std::vector<double> pot_onsite(pv->nloc);
99-
DFTU_LCAO::pot_onsite_real(dftu, dftu.get_ucell(), pv, spin, true, &pot_onsite[0], npol);
101+
DFTU_LCAO::pot_onsite_real(dftu, ucell, pv, spin, true, &pot_onsite[0], npol);
100102

101103
#ifdef __MPI
102104
ScalapackConnector::gemm(transN, transN,
@@ -125,14 +127,14 @@ void pot_uterm_real(Plus_U& dftu,
125127

126128
} // namespace DFTU_LCAO
127129

128-
void Plus_U::cal_eff_pot_mat_R_double(const Parallel_Orbitals* pv, const int ispin, double* SR, double* HR, const int npol)
130+
void Plus_U::cal_eff_pot_mat_R_double(const UnitCell& ucell, const Parallel_Orbitals* pv, const int ispin, double* SR, double* HR, const int npol)
129131
{
130132
const char transN = 'N', transT = 'T';
131133
const int one_int = 1;
132134
const double alpha = 1.0, beta = 0.0, one = 1.0, half = 0.5;
133135

134136
std::vector<double> pot_onsite(pv->nloc);
135-
DFTU_LCAO::pot_onsite_real(*this, *this->ucell, pv, ispin, true, &pot_onsite[0], npol);
137+
DFTU_LCAO::pot_onsite_real(*this, ucell, pv, ispin, true, &pot_onsite[0], npol);
136138

137139
#ifdef __MPI
138140
ScalapackConnector::gemm(transN, transN,
@@ -155,14 +157,14 @@ void Plus_U::cal_eff_pot_mat_R_double(const Parallel_Orbitals* pv, const int isp
155157
return;
156158
}
157159

158-
void Plus_U::cal_eff_pot_mat_R_complex_double(const Parallel_Orbitals* pv, const int ispin, std::complex<double>* SR, std::complex<double>* HR, const int npol)
160+
void Plus_U::cal_eff_pot_mat_R_complex_double(const UnitCell& ucell, const Parallel_Orbitals* pv, const int ispin, std::complex<double>* SR, std::complex<double>* HR, const int npol)
159161
{
160162
const char transN = 'N', transT = 'T';
161163
const int one_int = 1;
162164
const std::complex<double> zero = 0.0, one = 1.0, half = 0.5;
163165

164166
std::vector<std::complex<double>> pot_onsite(pv->nloc);
165-
DFTU_LCAO::pot_onsite_complex(*this, *this->ucell, pv, ispin, true, &pot_onsite[0], npol);
167+
DFTU_LCAO::pot_onsite_complex(*this, ucell, pv, ispin, true, &pot_onsite[0], npol);
166168

167169
#ifdef __MPI
168170
ScalapackConnector::gemm(transN, transN,

source/source_lcao/module_dftu/dftu_hamilt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
class Plus_U;
88
class Parallel_Orbitals;
9+
class UnitCell;
910

1011
#ifdef __LCAO
1112
namespace DFTU_LCAO {
1213

1314
/// @brief Compute the LCAO-basis U-term effective potential matrix (complex).
14-
/// Wraps Plus_U::pot_onsite_complex plus the S-projection GEMM.
15+
/// Calls DFTU_LCAO::pot_onsite_complex plus the S-projection GEMM.
1516
void pot_uterm_complex(Plus_U& dftu,
17+
const UnitCell& ucell,
1618
const Parallel_Orbitals* pv,
1719
const int ik,
1820
std::complex<double>* pot_uterm,
@@ -21,8 +23,9 @@ void pot_uterm_complex(Plus_U& dftu,
2123
const int npol);
2224

2325
/// @brief Compute the LCAO-basis U-term effective potential matrix (real).
24-
/// Wraps Plus_U::pot_onsite_real plus the S-projection GEMM.
26+
/// Calls DFTU_LCAO::pot_onsite_real plus the S-projection GEMM.
2527
void pot_uterm_real(Plus_U& dftu,
28+
const UnitCell& ucell,
2629
const Parallel_Orbitals* pv,
2730
const int ik,
2831
double* pot_uterm,

source/source_lcao/module_dftu/dftu_lcao.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ void Plus_U::init(UnitCell& cell,
4949
{
5050
orb_cutoff_ = orb->cutoffs();
5151
}
52-
ucell = &cell;
5352
#endif
5453

5554
if (pv != nullptr)

source/source_lcao/module_dftu/dftu_lcao.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ class Plus_U : public Plus_U_Base
6363
// For calculating contribution to Hamiltonian matrices
6464
//=============================================================
6565
public:
66-
void cal_eff_pot_mat_R_double(const Parallel_Orbitals* pv, const int ispin, double* SR, double* HR, const int npol);
67-
68-
void cal_eff_pot_mat_R_complex_double(const Parallel_Orbitals* pv,
66+
void cal_eff_pot_mat_R_double(const UnitCell& ucell,
67+
const Parallel_Orbitals* pv,
68+
const int ispin,
69+
double* SR,
70+
double* HR,
71+
const int npol);
72+
73+
void cal_eff_pot_mat_R_complex_double(const UnitCell& ucell,
74+
const Parallel_Orbitals* pv,
6975
const int ispin,
7076
std::complex<double>* SR,
7177
std::complex<double>* HR,
@@ -92,12 +98,8 @@ class Plus_U : public Plus_U_Base
9298
const std::vector<double>& get_orb_cutoff() const { return orb_cutoff_; }
9399
double get_yukawa_lambda() const { return yukawa_lambda; }
94100
const LCAO_Orbitals* get_ptr_orb() const { return ptr_orb_; }
95-
/// @brief unit cell backing the DFT+U calculation; set once in init().
96-
/// Freed callers source it from here instead of holding a separate pointer.
97-
const UnitCell& get_ucell() const { return *ucell; }
98101

99102
private:
100-
const UnitCell* ucell = nullptr;
101103
const elecstate::DensityMatrix<double, double>* dm_in_dftu_d = nullptr;
102104
const elecstate::DensityMatrix<std::complex<double>, double>* dm_in_dftu_cd = nullptr;
103105
#endif

source/source_lcao/module_dftu/dftu_lcao_op_legacy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void OperatorDFTU<OperatorLCAO<double, double>>::contributeHk(int ik)
2727
// Effective potential of DFT+U is added to total Hamiltonian here; Quxin adds on 20201029
2828
std::vector<double> pot_uterm(this->hsk->get_pv()->nloc);
2929

30-
DFTU_LCAO::pot_uterm_real(*this->dftu, this->hsk->get_pv(), ik, &pot_uterm[0], isk, this->hsk->get_sk(), this->npol);
30+
DFTU_LCAO::pot_uterm_real(*this->dftu, *this->ucell, this->hsk->get_pv(), ik, &pot_uterm[0], isk, this->hsk->get_sk(), this->npol);
3131

3232
double* hk = this->hsk->get_hk();
3333

@@ -48,7 +48,7 @@ void OperatorDFTU<OperatorLCAO<std::complex<double>, double>>::contributeHk(int
4848
// Effective potential of DFT+U is added to total Hamiltonian here; Quxin adds on 20201029
4949
std::vector<std::complex<double>> pot_uterm(this->hsk->get_pv()->nloc);
5050

51-
DFTU_LCAO::pot_uterm_complex(*this->dftu, this->hsk->get_pv(), ik, &pot_uterm[0], isk, this->hsk->get_sk(), this->npol);
51+
DFTU_LCAO::pot_uterm_complex(*this->dftu, *this->ucell, this->hsk->get_pv(), ik, &pot_uterm[0], isk, this->hsk->get_sk(), this->npol);
5252

5353
std::complex<double>* hk = this->hsk->get_hk();
5454

@@ -68,7 +68,7 @@ void OperatorDFTU<OperatorLCAO<std::complex<double>, std::complex<double>>>::con
6868
// Effective potential of DFT+U is added to total Hamiltonian here; Quxin adds on 20201029
6969
std::vector<std::complex<double>> pot_uterm(this->hsk->get_pv()->nloc);
7070

71-
DFTU_LCAO::pot_uterm_complex(*this->dftu, this->hsk->get_pv(), ik, &pot_uterm[0], isk, this->hsk->get_sk(), this->npol);
71+
DFTU_LCAO::pot_uterm_complex(*this->dftu, *this->ucell, this->hsk->get_pv(), ik, &pot_uterm[0], isk, this->hsk->get_sk(), this->npol);
7272

7373
std::complex<double>* hk = this->hsk->get_hk();
7474
for (int irc = 0; irc < this->hsk->get_pv()->nloc; irc++)

source/source_lcao/module_dftu/dftu_lcao_op_legacy.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class OperatorDFTU<OperatorLCAO<TK, TR>> : public OperatorLCAO<TK, TR>
2424
OperatorDFTU<OperatorLCAO<TK, TR>>(HS_Matrix_K<TK>* hsk_in,
2525
const std::vector<ModuleBase::Vector3<double>>& kvec_d_in,
2626
hamilt::HContainer<TR>* hR_in,
27+
const UnitCell& ucell_in,
2728
Plus_U* dftu_in,
2829
const std::vector<int>& isk_in,
2930
const int npol_in)
3031
: isk(isk_in), npol(npol_in), OperatorLCAO<TK, TR>(hsk_in, kvec_d_in, hR_in)
3132
{
3233
this->cal_type = calculation_type::lcao_dftu;
3334
this->dftu = dftu_in;
35+
this->ucell = &ucell_in;
3436
}
3537

3638
virtual void contributeHR() override;
@@ -41,6 +43,8 @@ class OperatorDFTU<OperatorLCAO<TK, TR>> : public OperatorLCAO<TK, TR>
4143

4244
Plus_U *dftu;
4345

46+
const UnitCell* ucell = nullptr;
47+
4448
bool HR_fixed_done = false;
4549

4650
const std::vector<int>& isk;

source/source_lcao/spar_u.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
void sparse_format::cal_HR_dftu(
77
Plus_U &dftu, // mohan add 2025-11-07
8+
const UnitCell &ucell,
89
const Parallel_Orbitals &pv,
910
std::set<Abfs::Vector3_Order<int>> &all_R_coor,
1011
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, double>>> &SR_sparse,
@@ -73,7 +74,7 @@ void sparse_format::cal_HR_dftu(
7374
}
7475
}
7576

76-
dftu.cal_eff_pot_mat_R_double(&pv, current_spin, SR_tmp, HR_tmp, PARAM.globalv.npol);
77+
dftu.cal_eff_pot_mat_R_double(ucell, &pv, current_spin, SR_tmp, HR_tmp, PARAM.globalv.npol);
7778

7879
for (int i = 0; i < PARAM.globalv.nlocal; ++i)
7980
{
@@ -126,6 +127,7 @@ void sparse_format::cal_HR_dftu(
126127

127128
void sparse_format::cal_HR_dftu_soc(
128129
Plus_U &dftu, // mohan add 2025-11-07
130+
const UnitCell &ucell,
129131
const Parallel_Orbitals &pv,
130132
std::set<Abfs::Vector3_Order<int>> &all_R_coor,
131133
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, std::complex<double>>>> &SR_soc_sparse,
@@ -192,7 +194,7 @@ void sparse_format::cal_HR_dftu_soc(
192194
}
193195
}
194196

195-
dftu.cal_eff_pot_mat_R_complex_double(&pv, current_spin, SR_soc_tmp, HR_soc_tmp, PARAM.globalv.npol);
197+
dftu.cal_eff_pot_mat_R_complex_double(ucell, &pv, current_spin, SR_soc_tmp, HR_soc_tmp, PARAM.globalv.npol);
196198

197199
for (int i = 0; i < PARAM.globalv.nlocal; ++i)
198200
{

source/source_lcao/spar_u.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ namespace sparse_format
99

1010
void cal_HR_dftu(
1111
Plus_U &dftu, // mohan add 2025-11-07
12+
const UnitCell &ucell,
1213
const Parallel_Orbitals &pv,
1314
std::set<Abfs::Vector3_Order<int>> &all_R_coor,
1415
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, double>>> &SR_sparse,
1516
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, double>>> *HR_sparse,
16-
const int &current_spin,
17+
const int &current_spin,
1718
const double &sparse_thr);
1819

1920
void cal_HR_dftu_soc(
2021
Plus_U &dftu, // mohan add 2025-11-07
22+
const UnitCell &ucell,
2123
const Parallel_Orbitals &pv,
2224
std::set<Abfs::Vector3_Order<int>> &all_R_coor,
2325
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, std::complex<double>>>> &SR_soc_sparse,
2426
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, std::complex<double>>>> &HR_soc_sparse,
25-
const int &current_spin,
27+
const int &current_spin,
2628
const double &sparse_thr);
2729

2830
}

0 commit comments

Comments
 (0)