Skip to content

Commit 68e76c5

Browse files
authored
Merge branch 'develop' into 2026-08-28-2
2 parents 8d77bd9 + 370e024 commit 68e76c5

43 files changed

Lines changed: 1781 additions & 1212 deletions

Some content is hidden

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

AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ rules. Read the complete governance document before making or reviewing changes:
9898
ask the developer whether to write them in; be cautious and skip unclear
9999
or unverified lessons.
100100

101+
## Refactoring Patterns
102+
103+
- Member -> free function: inventory `this->` reads; pass as params (const
104+
for config, ref for mutable state); move only when body is `this`-free;
105+
keep thin wrapper; compile each step.
106+
101107
## Local Commands
102108

103109
```bash

source/Makefile.Objects

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ OBJS_DFTU=dftu.o\
818818
dftu_yukawa.o\
819819
dftu_folding.o\
820820
dftu_tools.o\
821-
dftu_occup.o\
821+
dftu_lcao_occ.o\
822822
dftu_hamilt.o\
823823
setup_dftu_pw.o
824824

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
506506
const std::vector<std::vector<TK>>& dm_vec = this->dmat.dm->get_DMK_vector();
507507

508508
// 1) calculate the local occupation number matrix and energy correction in DFT+U
509-
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);
509+
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, PARAM.globalv.gamma_only_local);
510510

511511
// mohan add 2025-11: push DFT+U energy from Plus_U instance to ElecState.
512512
// Covers both dft_plus_u==1 (new method, energy accumulated by DFTU::contributeHR

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/force_stress_lcao.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ void Force_Stress_LCAO<T>::getForceStress(UnitCell& ucell,
457457
std::vector<std::vector<double>>* dmk_d = nullptr;
458458
std::vector<std::vector<std::complex<double>>>* dmk_c = nullptr;
459459
assign_dmk_ptr<T>(dmat.dm, dmk_d, dmk_c, PARAM.globalv.gamma_only_local);
460-
DFTU_LCAO::force_stress(dftu, ucell, gd, dmk_d, dmk_c, pv, fsr_dftu, force_u, stress_u, kv, PARAM.globalv.npol);
460+
DFTU_LCAO::force_stress(dftu, isforce, isstress, ucell, gd, dmk_d, dmk_c, pv, fsr_dftu, force_u, stress_u, kv, PARAM.globalv.npol, PARAM.globalv.gamma_only_local);
461461
}
462462
else
463463
{

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/lcao_set.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ void LCAO_domain::set_pot(
8989
PARAM.globalv.global_out_dir,
9090
inp.init_chg,
9191
pv.get_global_row_size(),
92-
PARAM.globalv.gamma_only_local,
9392
inp.ks_solver,
94-
inp.cal_force,
95-
inp.cal_stress,
9693
inp.device,
9794
inp.kpar,
9895
PARAM.globalv.hubbard_u,

source/source_lcao/module_dftu/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ list(APPEND objects
33
dftu_force.cpp
44
dftu_yukawa.cpp
55
dftu_folding.cpp
6-
dftu_tools.cpp
7-
dftu_occup.cpp
6+
dftu_lcao_pots.cpp
7+
dftu_lcao_occ.cpp
8+
dftu_lcao_energy.cpp
89
dftu_hamilt.cpp
910
)
1011

source/source_lcao/module_dftu/dftu_force.cpp

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#include "dftu_force.h"
33
#include "dftu_folding.h"
44
#include "dftu_lcao.h"
5+
#include "dftu_lcao_pots.h"
56
#include "source_base/global_function.h"
67
#include "source_base/module_external/scalapack_connector.h"
78
#include "source_base/parallel_reduce.h"
9+
#include "source_io/module_parameter/parameter.h"
810
#include "source_base/timer.h"
911

1012
#include <complex>
@@ -14,6 +16,8 @@
1416
namespace DFTU_LCAO {
1517

1618
void force_stress(Plus_U& dftu,
19+
const bool cal_force,
20+
const bool cal_stress,
1721
const UnitCell& ucell,
1822
const Grid_Driver& gd,
1923
std::vector<std::vector<double>>* dmk_d,
@@ -23,7 +27,8 @@ void force_stress(Plus_U& dftu,
2327
ModuleBase::matrix& force_dftu,
2428
ModuleBase::matrix& stress_dftu,
2529
const K_Vectors& kv,
26-
const int npol)
30+
const int npol,
31+
const bool gamma_only_local)
2732
{
2833
ModuleBase::TITLE("DFTU_LCAO", "force_stress");
2934
ModuleBase::timer::start("DFTU_LCAO", "force_stress");
@@ -35,16 +40,16 @@ void force_stress(Plus_U& dftu,
3540
// fsr_dftu is created without allocation), we fail early with a clear
3641
// message instead of letting pdgemm_ dereference nullptr and crash.
3742
// See force_stress_lcao.cpp for the historical background.
38-
if (dftu.is_gamma_only_local())
43+
if (gamma_only_local)
3944
{
40-
if (dftu.is_cal_force()
45+
if (cal_force
4146
&& (fsr.DSloc_x == nullptr || fsr.DSloc_y == nullptr || fsr.DSloc_z == nullptr))
4247
{
4348
ModuleBase::WARNING_QUIT("DFTU_LCAO::force_stress",
4449
"fsr.DSloc_x/y/z are nullptr in gamma_only path; the caller must allocate and fill them. "
4550
"See notes in source/source_lcao/force_stress_lcao.cpp.");
4651
}
47-
if (dftu.is_cal_stress()
52+
if (cal_stress
4853
&& (fsr.DSloc_x == nullptr || fsr.DSloc_y == nullptr || fsr.DSloc_z == nullptr
4954
|| fsr.DH_r == nullptr))
5055
{
@@ -56,14 +61,14 @@ void force_stress(Plus_U& dftu,
5661
}
5762
else
5863
{
59-
if (dftu.is_cal_force()
64+
if (cal_force
6065
&& (fsr.DSloc_Rx == nullptr || fsr.DSloc_Ry == nullptr || fsr.DSloc_Rz == nullptr))
6166
{
6267
ModuleBase::WARNING_QUIT("DFTU_LCAO::force_stress",
6368
"fsr.DSloc_Rx/Ry/Rz are nullptr in multik path; the caller must allocate and fill them. "
6469
"See notes in source/source_lcao/force_stress_lcao.cpp.");
6570
}
66-
if (dftu.is_cal_stress()
71+
if (cal_stress
6772
&& (fsr.DSloc_Rx == nullptr || fsr.DSloc_Ry == nullptr || fsr.DSloc_Rz == nullptr
6873
|| fsr.DH_r == nullptr))
6974
{
@@ -79,26 +84,26 @@ void force_stress(Plus_U& dftu,
7984
// explicit ic * pv.nrow + ir indices. All ks_solvers accepted by INPUT
8085
// validation are column-major today; abort loudly instead of silently
8186
// producing wrong forces/stresses if that assumption ever changes.
82-
if ((dftu.is_cal_force() || dftu.is_cal_stress())
83-
&& !ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(dftu.get_ks_solver()))
87+
if ((cal_force || cal_stress)
88+
&& !ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver))
8489
{
8590
ModuleBase::WARNING_QUIT("DFTU_LCAO::force_stress",
8691
"non column-major ks_solver is not supported for DFT+U force/stress; "
8792
"the folded matrix layout assumption would be violated");
8893
}
8994

90-
const int nlocal = dftu.get_nlocal();
95+
const int nlocal = pv.get_global_row_size();
9196

92-
if (dftu.is_cal_force())
97+
if (cal_force)
9398
{
9499
force_dftu.zero_out();
95100
}
96-
if (dftu.is_cal_stress())
101+
if (cal_stress)
97102
{
98103
stress_dftu.zero_out();
99104
}
100105

101-
if (dftu.is_gamma_only_local())
106+
if (gamma_only_local)
102107
{
103108
const char transN = 'N';
104109
const char transT = 'T';
@@ -115,7 +120,7 @@ void force_stress(Plus_U& dftu,
115120

116121
double* pot_onsite = new double[pv.nloc];
117122

118-
dftu.pot_onsite_real(spin, false, pot_onsite, npol);
123+
DFTU_LCAO::pot_onsite_real(dftu, ucell, &pv, spin, false, pot_onsite, npol);
119124

120125
#ifdef __MPI
121126
ScalapackConnector::gemm(transT, transN, nlocal, nlocal, nlocal,
@@ -127,18 +132,18 @@ void force_stress(Plus_U& dftu,
127132

128133
delete[] pot_onsite;
129134

130-
if (dftu.is_cal_force())
135+
if (cal_force)
131136
{
132-
cal_force_gamma(dftu.get_nlocal(), dftu.get_npol(),
137+
cal_force_gamma(nlocal, npol,
133138
dftu.get_orbital_corr_vec(), dftu.get_iatlnmipol2iwt(),
134139
ucell, &rho_pot_onsite[0], pv,
135140
fsr.DSloc_x, fsr.DSloc_y, fsr.DSloc_z, force_dftu);
136141
}
137142

138-
if (dftu.is_cal_stress())
143+
if (cal_stress)
139144
{
140-
cal_stress_gamma(dftu.get_nlocal(), dftu.get_npol(),
141-
dftu.get_ks_solver(), dftu.get_orb_cutoff(),
145+
cal_stress_gamma(nlocal, npol,
146+
PARAM.inp.ks_solver, dftu.get_orb_cutoff(),
142147
ucell, pv, &gd,
143148
fsr.DSloc_x, fsr.DSloc_y, fsr.DSloc_z, fsr.DH_r,
144149
&rho_pot_onsite[0], stress_dftu);
@@ -161,7 +166,7 @@ void force_stress(Plus_U& dftu,
161166

162167
std::complex<double>* pot_onsite = new std::complex<double>[pv.nloc];
163168

164-
dftu.pot_onsite_complex(spin, false, pot_onsite, npol);
169+
DFTU_LCAO::pot_onsite_complex(dftu, ucell, &pv, spin, false, pot_onsite, npol);
165170

166171

167172
#ifdef __MPI
@@ -173,28 +178,28 @@ void force_stress(Plus_U& dftu,
173178

174179
delete[] pot_onsite;
175180

176-
if (dftu.is_cal_force())
181+
if (cal_force)
177182
{
178-
cal_force_k(dftu.get_nlocal(), dftu.get_npol(),
179-
dftu.get_ks_solver(), dftu.get_orb_cutoff(),
183+
cal_force_k(nlocal, npol,
184+
PARAM.inp.ks_solver, dftu.get_orb_cutoff(),
180185
dftu.get_orbital_corr_vec(), dftu.get_iatlnmipol2iwt(),
181186
ucell, gd, fsr, pv, ik, &rho_pot_onsite[0], force_dftu, kv.kvec_d[ik]);
182187
}
183-
if (dftu.is_cal_stress())
188+
if (cal_stress)
184189
{
185-
cal_stress_k(dftu.get_nlocal(), dftu.get_npol(),
186-
dftu.get_ks_solver(), dftu.get_orb_cutoff(),
190+
cal_stress_k(nlocal, npol,
191+
PARAM.inp.ks_solver, dftu.get_orb_cutoff(),
187192
ucell, gd, fsr, pv, ik, &rho_pot_onsite[0], stress_dftu, kv.kvec_d[ik]);
188193
}
189194
} // ik
190195
}
191196

192-
if (dftu.is_cal_force())
197+
if (cal_force)
193198
{
194199
Parallel_Reduce::reduce_pool(force_dftu.c, force_dftu.nr * force_dftu.nc);
195200
}
196201

197-
if (dftu.is_cal_stress())
202+
if (cal_stress)
198203
{
199204
Parallel_Reduce::reduce_pool(stress_dftu.c, stress_dftu.nr * stress_dftu.nc);
200205

source/source_lcao/module_dftu/dftu_force.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ namespace DFTU_LCAO {
3030
/// Takes Plus_U& because it calls dftu.pot_onsite_real/complex,
3131
/// which are still members of Plus_U (defined in dftu_tools.cpp).
3232
void force_stress(Plus_U& dftu,
33+
const bool cal_force,
34+
const bool cal_stress,
3335
const UnitCell& ucell,
3436
const Grid_Driver& gd,
3537
std::vector<std::vector<double>>* dmk_d,
@@ -39,7 +41,8 @@ void force_stress(Plus_U& dftu,
3941
ModuleBase::matrix& force_dftu,
4042
ModuleBase::matrix& stress_dftu,
4143
const K_Vectors& kv,
42-
const int npol);
44+
const int npol,
45+
const bool gamma_only_local);
4346

4447
/// @brief Force contribution at a k-point (multik path).
4548
void cal_force_k(int nlocal,

0 commit comments

Comments
 (0)