Skip to content

Commit 9839f9c

Browse files
mohanchenabacus_fixer
andauthored
Refactor tests of xc module (#7475)
* refactor(xc): Decouple XC_Functional from global PARAM by using function parameters for nspin, domag, domag_z * feat(xc): Decouple XC_Functional from global PARAM object This commit decouples XC_Functional and related libxc functions from the global PARAM object by adding explicit parameters: 1. Modified XC_Functional::v_xc() to accept nspin, domag, domag_z parameters instead of accessing PARAM 2. Modified XC_Functional::gradcorr() to accept nspin, domag, domag_z parameters 3. Modified XC_Functional::set_xc_type() to accept nspin parameter 4. Modified XC_Functional_Libxc::v_xc_libxc() to accept nspin, domag, domag_z parameters 5. Modified XC_Functional_Libxc::v_xc_meta() to accept nspin parameter Updated all call sites in: - source_estate/module_pot/pot_xc.cpp - source_estate/module_pot/pot_xc_fdm.cpp - source_pw/module_pwdft/forces_cc.cpp - source_pw/module_pwdft/stress_cc.cpp - source_hamilt/module_xc/xc_pot.cpp - source_hamilt/module_xc/libxc_pot.cpp - source_hamilt/module_xc/libxc_abacus.h Updated test files to remove PARAM dependencies: - test_xc3.cpp: Removed PARAM include and initialization - test_xc5.cpp: Removed PARAM include and initialization, updated v_xc, v_xc_meta calls - xctest.h: Removed PARAM include and initialization Default parameter values are provided for backward compatibility. * update tests * Refactor: Remove PARAM references from XC functional interface and improve parameter passing Summary of changes: 1. Modified libxc_abacus.h: - Removed default value (=1) for nspin parameter in v_xc_libxc - Removed default value (=nullptr) for scaling_factor parameter in v_xc_libxc 2. Modified xc_functional.h: - Changed set_xc_type signature to only accept const std::string xc_func_in - Removed nspin and basis_type parameters from set_xc_type - Removed default parameters from v_xc and gradcorr 3. Modified xc_functional.cpp: - Updated set_xc_type implementation to match new signature - Removed meta-GGA nspin=4 check from set_xc_type (moved to gradcorr) - Removed hybrid functional LCAO check from set_xc_type (no longer needed) - Updated internal set_xc_type calls to use single parameter 4. Modified xc_grad.cpp: - Added meta-GGA nspin=4 check at the beginning of gradcorr function - This is the correct place for runtime validation since nspin is known here 5. Modified xc_pot.cpp: - Updated v_xc_libxc call to pass parameters correctly 6. Modified libxc_pot.cpp: - Updated v_xc_libxc implementation to match new signature 7. Modified stress_gga.cpp: - Changed direct 'true' argument to named variable 'is_stress' for gradcorr call - Improved code readability and maintainability 8. Updated all set_xc_type call sites (12 files): - test_xc.cpp, test_xc1.cpp, test_xc2.cpp, test_xc3.cpp, test_xc5.cpp - xc_functional.cpp, esolver_double_xc.cpp, esolver_ks_lcaopw.cpp - esolver_fp.cpp, Exx_LRI_interface.hpp, xc_kernel.cpp, exx_helper.cpp - All calls now pass only the xc_func_in parameter 9. Updated test_xc3.cpp and test_xc5.cpp: - Added named variables (nspin1, nspin2, nspin4, domag, domag_z, domag_true) - Removed direct numeric literals in function calls Rationale: - Separated functional setup (set_xc_type) from runtime configuration (nspin, basis_type) - Runtime validation checks moved to actual computation functions (gradcorr) - Parameter passing improved with meaningful variable names instead of magic numbers - Code readability and maintainability enhanced - Preparation for future improvements to XC functional interface * update --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 445c55f commit 9839f9c

14 files changed

Lines changed: 140 additions & 91 deletions

File tree

source/source_estate/module_pot/pot_xc.cpp

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

33
#include "source_base/timer.h"
44
#include "source_hamilt/module_xc/xc_functional.h"
5+
#include "source_io/module_parameter/parameter.h"
56

67
#ifdef USE_LIBXC
78
#include "source_hamilt/module_xc/libxc_abacus.h"
@@ -24,7 +25,8 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
2425
{
2526
#ifdef USE_LIBXC
2627
const std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> etxc_vtxc_v
27-
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), nrxx_current, ucell->omega, ucell->tpiba, chg);
28+
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), nrxx_current, ucell->omega, ucell->tpiba, chg,
29+
PARAM.inp.nspin);
2830
*(this->etxc_) = std::get<0>(etxc_vtxc_v);
2931
*(this->vtxc_) = std::get<1>(etxc_vtxc_v);
3032
v_eff += std::get<2>(etxc_vtxc_v);
@@ -36,7 +38,10 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
3638
else
3739
{
3840
const std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v
39-
= XC_Functional::v_xc(nrxx_current, chg, ucell);
41+
= XC_Functional::v_xc(nrxx_current, chg, ucell,
42+
PARAM.inp.nspin,
43+
PARAM.globalv.domag,
44+
PARAM.globalv.domag_z);
4045
*(this->etxc_) = std::get<0>(etxc_vtxc_v);
4146
*(this->vtxc_) = std::get<1>(etxc_vtxc_v);
4247
v_eff += std::get<2>(etxc_vtxc_v);

source/source_estate/module_pot/pot_xc_fdm.cpp

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

66
#include "pot_xc_fdm.h"
77
#include "source_hamilt/module_xc/xc_functional.h"
8+
#include "source_io/module_parameter/parameter.h"
89

910
namespace elecstate
1011
{
@@ -20,7 +21,10 @@ PotXC_FDM::PotXC_FDM(
2021
this->fixed_mode = false;
2122

2223
const std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v_0
23-
= XC_Functional::v_xc(this->chg_0->nrxx, this->chg_0, ucell);
24+
= XC_Functional::v_xc(this->chg_0->nrxx, this->chg_0, ucell,
25+
PARAM.inp.nspin,
26+
PARAM.globalv.domag,
27+
PARAM.globalv.domag_z);
2428
this->v_xc_0 = std::get<2>(etxc_vtxc_v_0);
2529
}
2630

@@ -47,7 +51,10 @@ void PotXC_FDM::cal_v_eff(
4751
}
4852

4953
const std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v_01
50-
= XC_Functional::v_xc(chg_01.nrxx, &chg_01, ucell);
54+
= XC_Functional::v_xc(chg_01.nrxx, &chg_01, ucell,
55+
PARAM.inp.nspin,
56+
PARAM.globalv.domag,
57+
PARAM.globalv.domag_z);
5158
const ModuleBase::matrix &v_xc_01 = std::get<2>(etxc_vtxc_v_01);
5259

5360
v_eff += v_xc_01 - this->v_xc_0;

source/source_hamilt/module_xc/libxc_abacus.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ namespace XC_Functional_Libxc
6060
const double &omega, // volume of cell
6161
const double tpiba,
6262
const Charge* const chr, // charge density
63-
const std::map<int, double>* scaling_factor = nullptr); // added by jghan, 2024-10-10
63+
const int nspin,
64+
const bool domag,
65+
const bool domag_z,
66+
const std::map<int, double>* scaling_factor);
6467

6568
// for mGGA functional
6669
extern std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> v_xc_meta(
6770
const std::vector<int> &func_id,
6871
const int &nrxx, // number of real-space grid
6972
const double &omega, // volume of cell
7073
const double tpiba,
71-
const Charge* const chr);
74+
const Charge* const chr,
75+
const int nspin);
7276

7377

7478
//-------------------

source/source_hamilt/module_xc/libxc_pot.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
1919
const double &omega, // volume of cell
2020
const double tpiba,
2121
const Charge* const chr,
22+
const int nspin_in,
23+
const bool domag,
24+
const bool domag_z,
2225
const std::map<int, double>* scaling_factor)
2326
{
2427
ModuleBase::TITLE("XC_Functional_Libxc","v_xc_libxc");
2528
ModuleBase::timer::start("XC_Functional_Libxc","v_xc_libxc");
2629

2730
const int nspin =
28-
(PARAM.inp.nspin == 1 || ( PARAM.inp.nspin ==4 && !PARAM.globalv.domag && !PARAM.globalv.domag_z))
31+
(nspin_in == 1 || ( nspin_in ==4 && !domag && !domag_z))
2932
? 1 : 2;
3033

3134
//----------------------------------------------------------
@@ -56,7 +59,7 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
5659
// converting rho
5760
std::vector<double> rho;
5861
std::vector<double> amag;
59-
if(1==nspin || 2==PARAM.inp.nspin)
62+
if(1==nspin || 2==nspin_in)
6063
{
6164
rho = XC_Functional_Libxc::convert_rho(nspin, nrxx, chr);
6265
}
@@ -167,7 +170,7 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
167170
v += std::get<1>(vtxc_v) * factor;
168171
} // end for( xc_func_type &func : funcs )
169172

170-
if(4==PARAM.inp.nspin)
173+
if(4==nspin_in)
171174
{
172175
v = XC_Functional_Libxc::convert_v_nspin4(nrxx, chr, amag, v);
173176
}
@@ -207,7 +210,8 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
207210
const int &nrxx, // number of real-space grid
208211
const double &omega, // volume of cell
209212
const double tpiba,
210-
const Charge* const chr)
213+
const Charge* const chr,
214+
const int nspin)
211215
{
212216
ModuleBase::TITLE("XC_Functional_Libxc","v_xc_meta");
213217
ModuleBase::timer::start("XC_Functional_Libxc","v_xc_meta");
@@ -217,17 +221,15 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
217221
//output of the subroutine
218222
double etxc = 0.0;
219223
double vtxc = 0.0;
220-
ModuleBase::matrix v(PARAM.inp.nspin,nrxx);
221-
ModuleBase::matrix vofk(PARAM.inp.nspin,nrxx);
224+
ModuleBase::matrix v(nspin,nrxx);
225+
ModuleBase::matrix vofk(nspin,nrxx);
222226

223227
//----------------------------------------------------------
224228
// xc_func_type is defined in Libxc package
225229
// to understand the usage of xc_func_type,
226230
// use can check on website, for example:
227231
// https://www.tddft.org/programs/libxc/manual/libxc-5.1.x/
228232
//----------------------------------------------------------
229-
230-
const int nspin = PARAM.inp.nspin;
231233
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
232234
/* func_id = */ func_id,
233235
/* xc_polarized = */ (1==nspin) ? XC_UNPOLARIZED:XC_POLARIZED);

source/source_hamilt/module_xc/test/test_xc3.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "gtest/gtest.h"
22
#include "xctest.h"
3-
#define private public
4-
#include "source_io/module_parameter/parameter.h"
5-
#undef private
63
#include "../xc_functional.h"
74
#include "../exx_info.h"
85
#include "xc3_mock.h"
@@ -37,6 +34,14 @@ class XCTest_GRADCORR : public XCTest
3734

3835
void SetUp()
3936
{
37+
// Define variables for parameters
38+
int nspin1 = 1;
39+
int nspin2 = 2;
40+
int nspin4 = 4;
41+
bool domag = false;
42+
bool domag_z = false;
43+
bool domag_true = true;
44+
4045
ModulePW::PW_Basis rhopw;
4146
UnitCell ucell;
4247
Charge chr;
@@ -84,17 +89,13 @@ class XCTest_GRADCORR : public XCTest
8489

8590
XC_Functional::set_xc_type("PBE");
8691

87-
PARAM.input.nspin = 1;
88-
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,false);
89-
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,true);
92+
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,false,nspin1,domag,domag_z);
93+
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,true,nspin1,domag,domag_z);
9094

91-
PARAM.input.nspin = 2;
92-
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,false);
93-
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,true);
95+
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,false,nspin2,domag,domag_z);
96+
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,true,nspin2,domag,domag_z);
9497

95-
PARAM.input.nspin = 4;
96-
PARAM.sys.domag = true;
97-
XC_Functional::gradcorr(et4,vt4,v4,&chr,&rhopw,&ucell,stress4,false);
98+
XC_Functional::gradcorr(et4,vt4,v4,&chr,&rhopw,&ucell,stress4,false,nspin4,domag_true,domag_z);
9899
}
99100
};
100101

source/source_hamilt/module_xc/test/test_xc5.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "../xc_functional.h"
22
#include "../libxc_abacus.h"
33
#include "gtest/gtest.h"
4-
#define private public
5-
#include "source_io/module_parameter/parameter.h"
6-
#undef private
74
#include "xctest.h"
85
#include "../exx_info.h"
96
#include "xc3_mock.h"
@@ -32,6 +29,12 @@ class XCTest_VXC : public XCTest
3229

3330
void SetUp()
3431
{
32+
// Define variables for parameters
33+
int nspin1 = 1;
34+
int nspin2 = 2;
35+
bool domag = false;
36+
bool domag_z = false;
37+
3538
ModulePW::PW_Basis rhopw;
3639
UnitCell ucell;
3740
Charge chr;
@@ -75,16 +78,14 @@ class XCTest_VXC : public XCTest
7578

7679
XC_Functional::set_xc_type("PBE");
7780

78-
PARAM.input.nspin = 1;
7981
std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v
80-
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
82+
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,nspin1,domag,domag_z);
8183
et1 = std::get<0>(etxc_vtxc_v);
8284
vt1 = std::get<1>(etxc_vtxc_v);
8385
v1 = std::get<2>(etxc_vtxc_v);
8486

85-
PARAM.input.nspin = 2;
8687
etxc_vtxc_v
87-
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
88+
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,nspin2,domag,domag_z);
8889
et2 = std::get<0>(etxc_vtxc_v);
8990
vt2 = std::get<1>(etxc_vtxc_v);
9091
v2 = std::get<2>(etxc_vtxc_v);
@@ -130,6 +131,12 @@ class XCTest_VXC_Libxc : public XCTest
130131

131132
void SetUp()
132133
{
134+
// Define variables for parameters
135+
int nspin1 = 1;
136+
int nspin2 = 2;
137+
bool domag = false;
138+
bool domag_z = false;
139+
133140
ModulePW::PW_Basis rhopw;
134141
UnitCell ucell;
135142
Charge chr;
@@ -173,16 +180,14 @@ class XCTest_VXC_Libxc : public XCTest
173180

174181
XC_Functional::set_xc_type("GGA_X_PBE+GGA_C_PBE");
175182

176-
PARAM.input.nspin = 1;
177183
std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v
178-
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
184+
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,nspin1,domag,domag_z);
179185
et1 = std::get<0>(etxc_vtxc_v);
180186
vt1 = std::get<1>(etxc_vtxc_v);
181187
v1 = std::get<2>(etxc_vtxc_v);
182188

183-
PARAM.input.nspin = 2;
184189
etxc_vtxc_v
185-
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
190+
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,nspin2,domag,domag_z);
186191
et2 = std::get<0>(etxc_vtxc_v);
187192
vt2 = std::get<1>(etxc_vtxc_v);
188193
v2 = std::get<2>(etxc_vtxc_v);
@@ -228,6 +233,10 @@ class XCTest_VXC_meta : public XCTest
228233

229234
void SetUp()
230235
{
236+
// Define variables for parameters
237+
int nspin1 = 1;
238+
int nspin2 = 2;
239+
231240
ModulePW::PW_Basis rhopw;
232241
UnitCell ucell;
233242
Charge chr;
@@ -281,17 +290,15 @@ class XCTest_VXC_meta : public XCTest
281290

282291
XC_Functional::set_xc_type("SCAN");
283292

284-
PARAM.input.nspin = 1;
285293
std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> etxc_vtxc_v
286-
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr);
294+
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr,nspin1);
287295
et1 = std::get<0>(etxc_vtxc_v);
288296
vt1 = std::get<1>(etxc_vtxc_v);
289297
v1 = std::get<2>(etxc_vtxc_v);
290298
vtau1 = std::get<3>(etxc_vtxc_v);
291299

292-
PARAM.input.nspin = 2;
293300
etxc_vtxc_v
294-
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr);
301+
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr,nspin2);
295302
et2 = std::get<0>(etxc_vtxc_v);
296303
vt2 = std::get<1>(etxc_vtxc_v);
297304
v2 = std::get<2>(etxc_vtxc_v);
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
#ifndef XCTEST_H
22
#define XCTEST_H
33
#include "gtest/gtest.h"
4-
#define private public
5-
#include "source_io/module_parameter/parameter.h"
6-
#undef private
74
class XCTest: public testing::Test
85
{
96
public:
10-
XCTest()
11-
{
12-
PARAM.input.basis_type = "";
13-
PARAM.input.cal_force = 0;
14-
PARAM.input.cal_stress = 0;
15-
}
7+
XCTest() {}
168
};
179
#endif

source/source_hamilt/module_xc/xc_functional.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,18 @@ void XC_Functional::set_xc_type(const std::string xc_func_in)
301301
std::cerr << "\n OPTX untested please test,";
302302
}
303303

304-
// if((func_type == 4 || func_type == 5) && PARAM.inp.basis_type == "pw")
304+
// if((func_type == 4 || func_type == 5) && basis_type == "pw")
305305
// {
306306
// ModuleBase::WARNING_QUIT("set_xc_type","hybrid functional not realized for planewave yet");
307307
// }
308-
if((func_type == 3 || func_type == 5) && PARAM.inp.nspin==4)
309-
{
310-
ModuleBase::WARNING_QUIT("set_xc_type","meta-GGA has not been implemented for nspin = 4 yet");
311-
}
312308

313-
#ifndef __EXX
314-
if((func_type == 4 || func_type == 5) && PARAM.inp.basis_type == "lcao")
315-
{
316-
ModuleBase::WARNING_QUIT("set_xc_type","compile with libri to use hybrid functional in lcao basis");
317-
}
318-
#endif
309+
// Hybrid functional is now supported for both PW and LCAO basis
310+
// #ifndef __EXX
311+
// if((func_type == 4 || func_type == 5) && basis_type == "lcao")
312+
// {
313+
// ModuleBase::WARNING_QUIT("set_xc_type","compile with libri to use hybrid functional in lcao basis");
314+
// }
315+
// #endif
319316

320317
#ifndef USE_LIBXC
321318
if(xc_func == "SCAN" || xc_func == "HSE" || xc_func == "SCAN0"

source/source_hamilt/module_xc/xc_functional.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class XC_Functional
4747
static std::tuple<double, double, ModuleBase::matrix> v_xc(
4848
const int &nrxx, // number of real-space grid
4949
const Charge* const chr,
50-
const UnitCell *ucell); // charge density
50+
const UnitCell *ucell, // charge density
51+
const int nspin,
52+
const bool domag,
53+
const bool domag_z);
5154

5255
//-------------------
5356
// xc_functional.cpp
@@ -206,7 +209,11 @@ class XC_Functional
206209
ModulePW::PW_Basis* rhopw,
207210
const UnitCell* ucell,
208211
std::vector<double>& stress_gga,
209-
const bool is_stress = false);
212+
const bool is_stress,
213+
const int nspin,
214+
const bool domag,
215+
const bool domag_z);
216+
210217
template <typename T, typename Device,
211218
typename Real = typename GetTypeReal<T>::type>
212219

0 commit comments

Comments
 (0)