Skip to content

Commit f0c760a

Browse files
committed
refactor(xc): pass spin controls explicitly to force and stress helpers
Address review feedback by removing PARAM reads from cal_force_cc, stress_cc and stress_gga and updating PW, LCAO, SDFT and OFDFT callers. Name the legacy gga_grad test input explicitly. Production parameter values and numerical formulas are unchanged; no INPUT documentation changes are required. Validation: Sai DSPRHBM job 1196207 rebuilt abacus_basic_para and four XC test executables. Five CTest entries passed, including serial and MPI2 NCGGA finite differences. CUDA validation remains for CI.
1 parent 46eea5a commit f0c760a

12 files changed

Lines changed: 96 additions & 49 deletions

File tree

source/source_hamilt/module_xc/test/test_xc3.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,16 @@ class XCTest_GRADCORR : public XCTest
9090

9191
XC_Functional::set_xc_type("PBE");
9292

93+
const int gga_grad = 0;
9394
double hybrid_alpha = 0.0;
9495
double hse_omega = 0.0;
95-
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,false,nspin1,domag,domag_z,0, hybrid_alpha, hse_omega);
96-
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,true,nspin1,domag,domag_z,0, hybrid_alpha, hse_omega);
96+
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,false,nspin1,domag,domag_z,gga_grad, hybrid_alpha, hse_omega);
97+
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,true,nspin1,domag,domag_z,gga_grad, hybrid_alpha, hse_omega);
9798

98-
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,false,nspin2,domag,domag_z,0, hybrid_alpha, hse_omega);
99-
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,true,nspin2,domag,domag_z,0, hybrid_alpha, hse_omega);
99+
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,false,nspin2,domag,domag_z,gga_grad, hybrid_alpha, hse_omega);
100+
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,true,nspin2,domag,domag_z,gga_grad, hybrid_alpha, hse_omega);
100101

101-
XC_Functional::gradcorr(et4,vt4,v4,&chr,&rhopw,&ucell,stress4,false,nspin4,domag_true,domag_z,0, hybrid_alpha, hse_omega);
102+
XC_Functional::gradcorr(et4,vt4,v4,&chr,&rhopw,&ucell,stress4,false,nspin4,domag_true,domag_z,gga_grad, hybrid_alpha, hse_omega);
102103
}
103104
};
104105

source/source_lcao/force_stress_lcao.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -921,14 +921,17 @@ void Force_Stress_LCAO<T>::calForcePwPart(UnitCell& ucell,
921921
const pseudopot_cell_vl& locpp,
922922
const Structure_Factor& sf)
923923
{
924+
const auto& xc_input = PARAM.inp;
925+
const auto& xc_spin = PARAM.globalv;
924926
ModuleBase::TITLE("Force_Stress_LCAO", "calForcePwPart");
925927
#ifdef __CUDA
926928
if(PARAM.inp.device == "gpu")
927929
{
928930
Forces<double, base_device::DEVICE_GPU> f_pw(nat);
929931
f_pw.cal_force_loc(ucell, fvl_dvl, rhopw, locpp.vloc, chr);
930932
f_pw.cal_force_ew(ucell, fewalds, rhopw, &sf);
931-
f_pw.cal_force_cc(fcc, rhopw, chr, locpp.numeric, ucell);
933+
f_pw.cal_force_cc(fcc, rhopw, chr, locpp.numeric, ucell,
934+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad);
932935
f_pw.cal_force_scc(fscc, rhopw, vnew, vnew_exist, locpp.numeric, ucell);
933936
}
934937
else
@@ -937,7 +940,8 @@ void Force_Stress_LCAO<T>::calForcePwPart(UnitCell& ucell,
937940
Forces<double, base_device::DEVICE_CPU> f_pw(nat);
938941
f_pw.cal_force_loc(ucell, fvl_dvl, rhopw, locpp.vloc, chr);
939942
f_pw.cal_force_ew(ucell, fewalds, rhopw, &sf);
940-
f_pw.cal_force_cc(fcc, rhopw, chr, locpp.numeric, ucell);
943+
f_pw.cal_force_cc(fcc, rhopw, chr, locpp.numeric, ucell,
944+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad);
941945
f_pw.cal_force_scc(fscc, rhopw, vnew, vnew_exist, locpp.numeric, ucell);
942946
}
943947

@@ -1027,6 +1031,8 @@ void Force_Stress_LCAO<T>::calStressPwPart(UnitCell& ucell,
10271031
const pseudopot_cell_vl& locpp,
10281032
const Structure_Factor& sf)
10291033
{
1034+
const auto& xc_input = PARAM.inp;
1035+
const auto& xc_spin = PARAM.globalv;
10301036
ModuleBase::TITLE("Force_Stress_LCAO", "calStressPwPart");
10311037

10321038
// local pseudopotential stress:
@@ -1039,15 +1045,17 @@ void Force_Stress_LCAO<T>::calStressPwPart(UnitCell& ucell,
10391045
sc_pw.stress_ewa(ucell, sigmaewa, rhopw, 0); // remain problem
10401046

10411047
// stress due to core correlation.
1042-
sc_pw.stress_cc(sigmacc, rhopw, ucell, &sf, 0, locpp.numeric, chr);
1048+
sc_pw.stress_cc(sigmacc, rhopw, ucell, &sf, 0, locpp.numeric, chr,
1049+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad, xc_spin.gamma_only_pw);
10431050

10441051
// stress due to self-consistent charge.
10451052
for (int i = 0; i < 3; i++)
10461053
{
10471054
sigmaxc(i, i) = -etxc / ucell.omega;
10481055
}
10491056
// Exchange-correlation for PBE
1050-
sc_pw.stress_gga(ucell, sigmaxc, rhopw, chr);
1057+
sc_pw.stress_gga(ucell, sigmaxc, rhopw, chr,
1058+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad);
10511059

10521060
return;
10531061
}

source/source_pw/module_ofdft/of_stress_pw.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ void OF_Stress_PW::cal_stress(ModuleBase::matrix& sigmatot,
1616
Structure_Factor* p_sf,
1717
K_Vectors* p_kv)
1818
{
19+
const auto& xc_input = PARAM.inp;
20+
const auto& xc_spin = PARAM.globalv;
1921
ModuleBase::TITLE("OF_Stress_PW", "cal_stress");
2022
ModuleBase::timer::start("OF_Stress_PW", "cal_stress");
2123

@@ -74,13 +76,15 @@ void OF_Stress_PW::cal_stress(ModuleBase::matrix& sigmatot,
7476
{
7577
sigmaxc(i, i) = -(pelec->f_en.etxc - pelec->f_en.vtxc) / ucell.omega;
7678
}
77-
stress_gga(ucell,sigmaxc, this->rhopw, pelec->charge);
79+
stress_gga(ucell,sigmaxc, this->rhopw, pelec->charge,
80+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad);
7881

7982
// local contribution
8083
stress_loc(ucell,sigmaloc, this->rhopw, locpp.vloc, p_sf, true, pelec->charge);
8184

8285
// nlcc
83-
stress_cc(sigmaxcc, this->rhopw, ucell, p_sf, true, locpp.numeric, pelec->charge);
86+
stress_cc(sigmaxcc, this->rhopw, ucell, p_sf, true, locpp.numeric, pelec->charge,
87+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad, xc_spin.gamma_only_pw);
8488

8589
// vdW term prepared before SCF for this ionic configuration.
8690
if (vdw_result != nullptr)

source/source_pw/module_pwdft/force_pw.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void Forces<FPTYPE, Device>::cal_force(UnitCell& ucell,
4141
ModulePW::PW_Basis_K* wfc_basis,
4242
const psi::Psi<std::complex<FPTYPE>, Device>* psi_in)
4343
{
44+
const auto& xc_input = PARAM.inp;
45+
const auto& xc_spin = PARAM.globalv;
4446
ModuleBase::timer::start("Forces", "cal_force");
4547
ModuleBase::TITLE("Forces", "init");
4648
this->device = base_device::get_device_type(this->ctx);
@@ -83,7 +85,8 @@ void Forces<FPTYPE, Device>::cal_force(UnitCell& ucell,
8385
}
8486

8587
// non-linear core correction
86-
Forces::cal_force_cc(forcecc, rho_basis, chr, locpp->numeric, ucell);
88+
Forces::cal_force_cc(forcecc, rho_basis, chr, locpp->numeric, ucell,
89+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad);
8790

8891
// force due to core charge
8992
this->cal_force_scc(forcescc, rho_basis, elec.vnew, elec.vnew_exist, locpp->numeric, ucell);

source/source_pw/module_pwdft/force_pw.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ class Forces
7777
const ModulePW::PW_Basis* const rho_basis,
7878
const Charge* const chr,
7979
const bool* numeric,
80-
UnitCell& ucell_in);
80+
UnitCell& ucell_in,
81+
const int nspin,
82+
const bool domag,
83+
const bool domag_z,
84+
const int gga_grad);
8185
/**
8286
* @brief This routine computes the atomic force of non-local pseudopotential
8387
* F^{NL}_i = \sum_{n,k}f_{nk}\sum_I \sum_{lm,l'm'}D_{l,l'}^{I} [

source/source_pw/module_pwdft/force_pw_cc.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "force_pw.h"
22
#include "stress_func.h"
33
#include "source_base/parallel_reduce.h"
4-
#include "source_io/module_parameter/parameter.h"
54
// new
65
#include "source_base/complexmatrix.h"
76
#include "source_base/libm/libm.h"
@@ -32,9 +31,12 @@ void Forces<FPTYPE, Device>::cal_force_cc(ModuleBase::matrix& forcecc,
3231
const ModulePW::PW_Basis* const rho_basis,
3332
const Charge* const chr,
3433
const bool* numeric,
35-
UnitCell& ucell_in)
34+
UnitCell& ucell_in,
35+
const int nspin,
36+
const bool domag,
37+
const bool domag_z,
38+
const int gga_grad)
3639
{
37-
const Parameter& parameters = PARAM;
3840
ModuleBase::TITLE("Forces", "cal_force_cc");
3941
// recalculate the exchange-correlation potential.
4042
ModuleBase::timer::start("Forces", "cal_force_cc");
@@ -54,7 +56,7 @@ void Forces<FPTYPE, Device>::cal_force_cc(ModuleBase::matrix& forcecc,
5456
return;
5557
}
5658

57-
ModuleBase::matrix v(parameters.inp.nspin, rho_basis->nrxx);
59+
ModuleBase::matrix v(nspin, rho_basis->nrxx);
5860

5961
const double hybrid_alpha = XC_Functional::get_hybrid_alpha();
6062
#ifdef __EXX
@@ -67,7 +69,7 @@ void Forces<FPTYPE, Device>::cal_force_cc(ModuleBase::matrix& forcecc,
6769
#ifdef __LIBXC
6870
const auto etxc_vtxc_v
6971
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rho_basis->nrxx, ucell_in.omega, ucell_in.tpiba, chr,
70-
parameters.inp.nspin, hybrid_alpha, hse_omega);
72+
nspin, hybrid_alpha, hse_omega);
7173

7274
// etxc = std::get<0>(etxc_vtxc_v);
7375
// vtxc = std::get<1>(etxc_vtxc_v);
@@ -78,12 +80,12 @@ void Forces<FPTYPE, Device>::cal_force_cc(ModuleBase::matrix& forcecc,
7880
}
7981
else
8082
{
81-
unitcell::cal_ux(ucell_in, parameters.inp.nspin);
83+
unitcell::cal_ux(ucell_in, nspin);
8284
const auto etxc_vtxc_v = XC_Functional::v_xc(rho_basis->nrxx, chr, &ucell_in,
83-
parameters.inp.nspin,
84-
parameters.globalv.domag,
85-
parameters.globalv.domag_z,
86-
parameters.inp.gga_grad,
85+
nspin,
86+
domag,
87+
domag_z,
88+
gga_grad,
8789
hybrid_alpha,
8890
hse_omega);
8991

@@ -94,7 +96,7 @@ void Forces<FPTYPE, Device>::cal_force_cc(ModuleBase::matrix& forcecc,
9496

9597
const ModuleBase::matrix vxc = v;
9698
std::complex<double>* psiv = new std::complex<double>[rho_basis->nmaxgr];
97-
if (parameters.inp.nspin == 1 || parameters.inp.nspin == 4)
99+
if (nspin == 1 || nspin == 4)
98100
{
99101
#ifdef _OPENMP
100102
#pragma omp parallel for schedule(static, 1024)
@@ -370,4 +372,4 @@ void Forces<FPTYPE, Device>::deriv_drhoc
370372
template class Forces<double, base_device::DEVICE_CPU>;
371373
#if ((defined __CUDA) || (defined __ROCM))
372374
template class Forces<double, base_device::DEVICE_GPU>;
373-
#endif
375+
#endif

source/source_pw/module_pwdft/stress_cc.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "stress_func.h"
22
#include "source_base/parallel_reduce.h"
33
#include "source_hamilt/module_xc/xc_functional.h"
4-
#include "source_io/module_parameter/parameter.h"
54
#include "source_base/math_integral.h"
65
#include "source_base/timer.h"
76
#include "source_cell/cal_ux.h"
@@ -15,19 +14,23 @@
1514
template <typename FPTYPE, typename Device>
1615
void Stress_Func<FPTYPE, Device>::stress_cc(ModuleBase::matrix& sigma,
1716
ModulePW::PW_Basis* rho_basis,
18-
UnitCell& ucell,
17+
UnitCell& ucell,
1918
const Structure_Factor* p_sf,
2019
const bool is_pw,
21-
const bool *numeric,
22-
const Charge* const chr)
20+
const bool *numeric,
21+
const Charge* const chr,
22+
const int nspin,
23+
const bool domag,
24+
const bool domag_z,
25+
const int gga_grad,
26+
const bool gamma_only_pw)
2327
{
24-
const Parameter& parameters = PARAM;
2528
ModuleBase::TITLE("Stress","stress_cc");
2629
ModuleBase::timer::start("Stress","stress_cc");
2730

2831
FPTYPE fact=1.0;
2932

30-
if(is_pw&&parameters.globalv.gamma_only_pw)
33+
if(is_pw&&gamma_only_pw)
3134
{
3235
fact = 2.0; //is_pw:PW basis, gamma_only need to FPTYPE.
3336
}
@@ -63,7 +66,7 @@ void Stress_Func<FPTYPE, Device>::stress_cc(ModuleBase::matrix& sigma,
6366
#ifdef __LIBXC
6467
const auto etxc_vtxc_v
6568
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rho_basis->nrxx, ucell.omega, ucell.tpiba, chr,
66-
parameters.inp.nspin, hybrid_alpha, hse_omega);
69+
nspin, hybrid_alpha, hse_omega);
6770

6871
// etxc = std::get<0>(etxc_vtxc_v);
6972
// vtxc = std::get<1>(etxc_vtxc_v);
@@ -74,12 +77,12 @@ void Stress_Func<FPTYPE, Device>::stress_cc(ModuleBase::matrix& sigma,
7477
}
7578
else
7679
{
77-
unitcell::cal_ux(ucell, parameters.inp.nspin);
80+
unitcell::cal_ux(ucell, nspin);
7881
const auto etxc_vtxc_v = XC_Functional::v_xc(rho_basis->nrxx, chr, &ucell,
79-
parameters.inp.nspin,
80-
parameters.globalv.domag,
81-
parameters.globalv.domag_z,
82-
parameters.inp.gga_grad,
82+
nspin,
83+
domag,
84+
domag_z,
85+
gga_grad,
8386
hybrid_alpha,
8487
hse_omega);
8588
// etxc = std::get<0>(etxc_vtxc_v); // may delete?
@@ -89,7 +92,7 @@ void Stress_Func<FPTYPE, Device>::stress_cc(ModuleBase::matrix& sigma,
8992

9093
std::complex<FPTYPE>* psic = new std::complex<FPTYPE>[rho_basis->nmaxgr];
9194

92-
if(parameters.inp.nspin==1||parameters.inp.nspin==4)
95+
if(nspin==1||nspin==4)
9396
{
9497
#ifdef _OPENMP
9598
#pragma omp parallel for schedule(static, 1024)

source/source_pw/module_pwdft/stress_func.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ class Stress_Func
117117
const Structure_Factor* p_sf,
118118
const bool is_pw,
119119
const bool *numeric,
120-
const Charge* const chr); // nonlinear core correction stress in PW or LCAO basis
120+
const Charge* const chr,
121+
const int nspin,
122+
const bool domag,
123+
const bool domag_z,
124+
const int gga_grad,
125+
const bool gamma_only_pw); // nonlinear core correction stress in PW or LCAO basis
121126

122127
void deriv_drhoc(const bool& numeric,
123128
const double& omega,
@@ -134,7 +139,11 @@ class Stress_Func
134139
void stress_gga(const UnitCell& ucell,
135140
ModuleBase::matrix& sigma,
136141
ModulePW::PW_Basis* rho_basis,
137-
const Charge* const chr); // gga part in both PW and LCAO basis
142+
const Charge* const chr,
143+
const int nspin,
144+
const bool domag,
145+
const bool domag_z,
146+
const int gga_grad); // gga part in both PW and LCAO basis
138147
void stress_mgga(const UnitCell& ucell,
139148
ModuleBase::matrix& sigma,
140149
const ModuleBase::matrix& wg,

source/source_pw/module_pwdft/stress_gga.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#include "stress_func.h"
22
#include "source_base/parallel_reduce.h"
33
#include "source_hamilt/module_xc/xc_functional.h"
4-
#include "source_io/module_parameter/parameter.h"
54

65
//calculate the GGA stress correction in PW and LCAO
76
template <typename FPTYPE, typename Device>
87
void Stress_Func<FPTYPE, Device>::stress_gga(const UnitCell& ucell,
98
ModuleBase::matrix& sigma,
109
ModulePW::PW_Basis* rho_basis,
11-
const Charge* const chr)
10+
const Charge* const chr,
11+
const int nspin,
12+
const bool domag,
13+
const bool domag_z,
14+
const int gga_grad)
1215
{
13-
const Parameter& parameters = PARAM;
1416
ModuleBase::TITLE("Stress","stress_gga");
1517
ModuleBase::timer::start("Stress","stress_gga");
1618

@@ -32,7 +34,7 @@ void Stress_Func<FPTYPE, Device>::stress_gga(const UnitCell& ucell,
3234
XC_Functional::gradcorr(
3335
dum1, dum2, dum3, chr, rho_basis, &ucell,
3436
stress_gga, is_stress,
35-
parameters.inp.nspin, parameters.globalv.domag, parameters.globalv.domag_z, parameters.inp.gga_grad,
37+
nspin, domag, domag_z, gga_grad,
3638
hybrid_alpha, hse_omega);
3739

3840
for(int l = 0;l< 3;l++)

source/source_pw/module_pwdft/stress_pw.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void Stress_PW<FPTYPE, Device>::cal_stress(ModuleBase::matrix& sigmatot,
2323
const General_Exx_Info& exx_info,
2424
const psi::Psi <std::complex<FPTYPE>, Device>* d_psi_in)
2525
{
26+
const auto& xc_input = PARAM.inp;
27+
const auto& xc_spin = PARAM.globalv;
2628
ModuleBase::TITLE("Stress_PW", "cal_stress");
2729
ModuleBase::timer::start("Stress_PW", "cal_stress");
2830

@@ -91,7 +93,8 @@ void Stress_PW<FPTYPE, Device>::cal_stress(ModuleBase::matrix& sigmatot,
9193
{
9294
sigmaxc(i, i) = -(pelec->f_en.etxc - pelec->f_en.vtxc) / ucell.omega;
9395
}
94-
this->stress_gga(ucell, sigmaxc, rho_basis, pelec->charge);
96+
this->stress_gga(ucell, sigmaxc, rho_basis, pelec->charge,
97+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad);
9598
if (XC_Functional::get_ked_flag())
9699
{
97100
this->stress_mgga(ucell,
@@ -108,7 +111,8 @@ void Stress_PW<FPTYPE, Device>::cal_stress(ModuleBase::matrix& sigmatot,
108111
this->stress_loc(ucell, sigmaloc, rho_basis, locpp.vloc, p_sf, 1, pelec->charge);
109112

110113
// nlcc
111-
this->stress_cc(sigmaxcc, rho_basis, ucell, p_sf, 1, locpp.numeric, pelec->charge);
114+
this->stress_cc(sigmaxcc, rho_basis, ucell, p_sf, 1, locpp.numeric, pelec->charge,
115+
xc_input.nspin, xc_spin.domag, xc_spin.domag_z, xc_input.gga_grad, xc_spin.gamma_only_pw);
112116

113117
// nonlocal
114118
this->stress_nl(sigmanl, this->pelec->wg, this->pelec->ekb, p_sf, p_kv, p_symm, wfc_basis, d_psi_in, nlpp, ucell);

0 commit comments

Comments
 (0)