Skip to content

Commit 860efe6

Browse files
committed
correct the use of const map
1 parent 3ece2a1 commit 860efe6

File tree

7 files changed

+76
-119
lines changed

7 files changed

+76
-119
lines changed

source/module_hamilt_general/module_xc/xc_functional_libxc.cpp

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,49 +146,70 @@ XC_Functional_Libxc::set_xc_type_libxc(const std::string& xc_func_in)
146146
return std::make_pair(func_type, func_id);
147147
}
148148

149-
const std::map<int, std::vector<double>> in_built_xc_func_ext_params = {
150-
// finite temperature XC functionals
151-
// convert the temperature from Rydberg to Hartree
152-
{XC_LDA_XC_KSDT, {PARAM.inp.xc_temperature * 0.5}},
153-
{XC_LDA_XC_CORRKSDT, {PARAM.inp.xc_temperature * 0.5}},
154-
{XC_LDA_XC_GDSMFB, {PARAM.inp.xc_temperature * 0.5}},
155-
// hybrid functionals
149+
const std::vector<double> in_built_xc_func_ext_params(const int id)
150+
{
151+
const std::map<int, std::vector<double>> mymap = {
152+
// finite temperature XC functionals
153+
{XC_LDA_XC_KSDT, {PARAM.inp.xc_temperature * 0.5}},
154+
{XC_LDA_XC_CORRKSDT, {PARAM.inp.xc_temperature * 0.5}},
155+
{XC_LDA_XC_GDSMFB, {PARAM.inp.xc_temperature * 0.5}},
156+
// hybrid functionals
156157
#ifdef __EXX
157-
{XC_HYB_GGA_XC_PBEH, {GlobalC::exx_info.info_global.hybrid_alpha,
158-
GlobalC::exx_info.info_global.hse_omega,
159-
GlobalC::exx_info.info_global.hse_omega}},
160-
{XC_HYB_GGA_XC_HSE06, {GlobalC::exx_info.info_global.hybrid_alpha,
161-
GlobalC::exx_info.info_global.hse_omega,
162-
GlobalC::exx_info.info_global.hse_omega}},
163-
// short-range of B88_X
164-
{XC_GGA_X_ITYH, {PARAM.inp.exx_hse_omega}},
165-
// short-range of LYP_C
166-
{XC_GGA_C_LYPR, {0.04918, 0.132, 0.2533, 0.349,
167-
0.35/2.29, 2.0/2.29, PARAM.inp.exx_hse_omega}},
158+
{XC_HYB_GGA_XC_PBEH, {GlobalC::exx_info.info_global.hybrid_alpha,
159+
GlobalC::exx_info.info_global.hse_omega,
160+
GlobalC::exx_info.info_global.hse_omega}},
161+
{XC_HYB_GGA_XC_HSE06, {GlobalC::exx_info.info_global.hybrid_alpha,
162+
GlobalC::exx_info.info_global.hse_omega,
163+
GlobalC::exx_info.info_global.hse_omega}},
164+
// short-range of B88_X
165+
{XC_GGA_X_ITYH, {PARAM.inp.exx_hse_omega}},
166+
// short-range of LYP_C
167+
{XC_GGA_C_LYPR, {0.04918, 0.132, 0.2533, 0.349,
168+
0.35/2.29, 2.0/2.29, PARAM.inp.exx_hse_omega}},
168169
#endif
169-
};
170+
};
171+
auto it = mymap.find(id);
172+
return (it != mymap.end()) ? it->second : std::vector<double>{};
173+
}
174+
175+
const std::vector<double> external_xc_func_ext_params(const int id)
176+
{
177+
const std::map<int, std::vector<double>> mymap = {
178+
{
179+
PARAM.inp.xc_exch_ext_param[0],
180+
std::vector<double>(PARAM.inp.xc_exch_ext_param.begin()+1,
181+
PARAM.inp.xc_exch_ext_param.end())
182+
},
183+
{
184+
PARAM.inp.xc_corr_ext_param[0],
185+
std::vector<double>(PARAM.inp.xc_corr_ext_param.begin()+1,
186+
PARAM.inp.xc_corr_ext_param.end())
187+
}
188+
};
189+
auto it = mymap.find(id);
190+
return (it != mymap.end()) ? it->second : std::vector<double>{};
191+
}
170192

171193
std::vector<xc_func_type>
172194
XC_Functional_Libxc::init_func(const std::vector<int> &func_id,
173-
const int xc_polarized,
174-
const std::map<int, std::vector<double>> &external_xc_func_ext_params)
195+
const int xc_polarized)
175196
{
176197
std::vector<xc_func_type> funcs;
177-
for(int id : func_id)
198+
for (int id : func_id)
178199
{
179200
funcs.push_back({}); // create placeholder
180201
xc_func_init(&funcs.back(), id, xc_polarized); // instantiate the XC term
181202

182203
// search for extended parameters, external overwrites in-built if
183204
// the same functional id is found in both maps
184-
auto it_in_built = in_built_xc_func_ext_params.find(id);
185-
auto it_external = external_xc_func_ext_params.find(id);
205+
const std::vector<double> in_built_ext_params = in_built_xc_func_ext_params(id);
206+
const std::vector<double> external_ext_params = external_xc_func_ext_params(id);
186207

187208
const double* xc_func_ext_params =
188-
(it_external != external_xc_func_ext_params.end()) ?
189-
it_external->second.data() :
190-
(it_in_built != in_built_xc_func_ext_params.end()) ?
191-
it_in_built->second.data() : nullptr;
209+
(!external_ext_params.empty()) ? external_ext_params.data() :
210+
(!in_built_ext_params.empty()) ? in_built_ext_params.data() :
211+
nullptr; // nullptr if no extended parameters are found
212+
192213
// if there are no extended parameters, do nothing
193214
if(xc_func_ext_params != nullptr)
194215
{

source/module_hamilt_general/module_xc/xc_functional_libxc.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,24 @@ namespace XC_Functional_Libxc
2727
// for example, "XC_LDA_X+XC_LDA_C_PZ"
2828
extern std::pair<int,std::vector<int>> set_xc_type_libxc(const std::string& xc_func_in);
2929

30-
// converts func_id into corresponding xc_func_type vector
31-
extern
32-
std::vector<xc_func_type>
33-
init_func(const std::vector<int> &func_id,
34-
const int xc_polarized,
35-
const std::map<int, std::vector<double>> &external_xc_func_ext_params = {});
30+
/**
31+
* @brief instantiate the XC functional by its ID, and set the extended parameters if provided.
32+
*
33+
* @param func_id libxc ID of functional, see https://libxc.gitlab.io/functionals/ for details
34+
* @param xc_polarized 0: unpolarized, 1: spin-polarized
35+
* @return std::vector<xc_func_type>
36+
*
37+
* @note the functionality of this method is extended by supporting the user-defined
38+
* extended parameters of xc. However, there are several functionals' extended
39+
* parameters are pre-defined in the code, which herein we call those are
40+
* "in-built" parameters. If the same functional ID is found in both in-built
41+
* and external parameters, the external parameters will overwrite the in-built ones.
42+
* The external parameters can be passed here by keywords xc_exch_ext_param and
43+
* xc_corr_ext_param in the input file. The expected format would be an XC ID
44+
* followed by a list of parameters.
45+
*/
46+
extern std::vector<xc_func_type> init_func(const std::vector<int> &func_id,
47+
const int xc_polarized);
3648

3749
extern void finish_func(std::vector<xc_func_type> &funcs);
3850

source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,7 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
3737

3838
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
3939
/* func_id = */ func_id,
40-
/* xc_polarized = */ (1==nspin) ? XC_UNPOLARIZED:XC_POLARIZED,
41-
/* external_xc_func_ext_params = */
42-
std::map<int, std::vector<double>>({
43-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
44-
PARAM.inp.xc_exch_ext_param.begin()+1,
45-
PARAM.inp.xc_exch_ext_param.end()
46-
)},
47-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
48-
PARAM.inp.xc_corr_ext_param.begin()+1,
49-
PARAM.inp.xc_corr_ext_param.end()
50-
)}
51-
}));
40+
/* xc_polarized = */ (1==nspin) ? XC_UNPOLARIZED : XC_POLARIZED);
5241

5342
const bool is_gga = [&funcs]()
5443
{
@@ -207,18 +196,7 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
207196
const int nspin = PARAM.inp.nspin;
208197
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
209198
/* func_id = */ func_id,
210-
/* xc_polarized = */ (1==nspin) ? XC_UNPOLARIZED:XC_POLARIZED,
211-
/* external_xc_func_ext_params = */
212-
std::map<int, std::vector<double>>({
213-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
214-
PARAM.inp.xc_exch_ext_param.begin()+1,
215-
PARAM.inp.xc_exch_ext_param.end()
216-
)},
217-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
218-
PARAM.inp.xc_corr_ext_param.begin()+1,
219-
PARAM.inp.xc_corr_ext_param.end()
220-
)}
221-
}));
199+
/* xc_polarized = */ (1==nspin) ? XC_UNPOLARIZED:XC_POLARIZED);
222200

223201
const std::vector<double> rho = XC_Functional_Libxc::convert_rho(nspin, nrxx, chr);
224202
const std::vector<std::vector<ModuleBase::Vector3<double>>> gdr

source/module_hamilt_general/module_xc/xc_functional_libxc_wrapper_gcxc.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,7 @@ void XC_Functional_Libxc::gcxc_libxc(
2222

2323
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
2424
/* func_id = */ func_id,
25-
/* xc_polarized = */ XC_UNPOLARIZED,
26-
/* external_xc_func_ext_params = */
27-
std::map<int, std::vector<double>>({
28-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
29-
PARAM.inp.xc_exch_ext_param.begin()+1,
30-
PARAM.inp.xc_exch_ext_param.end()
31-
)},
32-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
33-
PARAM.inp.xc_corr_ext_param.begin()+1,
34-
PARAM.inp.xc_corr_ext_param.end()
35-
)}
36-
}));
25+
/* xc_polarized = */ XC_UNPOLARIZED);
3726
for(xc_func_type &func : funcs)
3827
{
3928
double s,v1,v2;
@@ -60,18 +49,7 @@ void XC_Functional_Libxc::gcxc_spin_libxc(
6049

6150
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
6251
/* func_id = */ func_id,
63-
/* xc_polarized = */ XC_POLARIZED,
64-
/* external_xc_func_ext_params = */
65-
std::map<int, std::vector<double>>({
66-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
67-
PARAM.inp.xc_exch_ext_param.begin()+1,
68-
PARAM.inp.xc_exch_ext_param.end()
69-
)},
70-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
71-
PARAM.inp.xc_corr_ext_param.begin()+1,
72-
PARAM.inp.xc_corr_ext_param.end()
73-
)}
74-
}));
52+
/* xc_polarized = */ XC_POLARIZED);
7553

7654
for(xc_func_type &func : funcs)
7755
{

source/module_hamilt_general/module_xc/xc_functional_libxc_wrapper_tauxc.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,7 @@ void XC_Functional_Libxc::tau_xc(
2020
lapl_rho = grho;
2121
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
2222
/* func_id = */ func_id,
23-
/* xc_polarized = */ XC_UNPOLARIZED,
24-
/* external_xc_func_ext_params = */
25-
std::map<int, std::vector<double>>({
26-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
27-
PARAM.inp.xc_exch_ext_param.begin()+1,
28-
PARAM.inp.xc_exch_ext_param.end()
29-
)},
30-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
31-
PARAM.inp.xc_corr_ext_param.begin()+1,
32-
PARAM.inp.xc_corr_ext_param.end()
33-
)}
34-
}));
23+
/* xc_polarized = */ XC_UNPOLARIZED);
3524

3625
sxc = 0.0; v1xc = 0.0; v2xc = 0.0; v3xc = 0.0;
3726

@@ -76,18 +65,7 @@ void XC_Functional_Libxc::tau_xc_spin(
7665

7766
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
7867
/* func_id = */ func_id,
79-
/* xc_polarized = */ XC_POLARIZED,
80-
/* external_xc_func_ext_params = */
81-
std::map<int, std::vector<double>>({
82-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
83-
PARAM.inp.xc_exch_ext_param.begin()+1,
84-
PARAM.inp.xc_exch_ext_param.end()
85-
)},
86-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
87-
PARAM.inp.xc_corr_ext_param.begin()+1,
88-
PARAM.inp.xc_corr_ext_param.end()
89-
)}
90-
}));
68+
/* xc_polarized = */ XC_POLARIZED);
9169

9270
for(xc_func_type &func : funcs)
9371
{

source/module_hamilt_general/module_xc/xc_functional_libxc_wrapper_xc.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ void XC_Functional_Libxc::xc_spin_libxc(
1313

1414
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
1515
/* func_id = */ func_id,
16-
/* xc_polarized = */ XC_POLARIZED,
17-
/* external_xc_func_ext_params = */
18-
std::map<int, std::vector<double>>({
19-
{PARAM.inp.xc_exch_ext_param[0], std::vector<double>(
20-
PARAM.inp.xc_exch_ext_param.begin()+1,
21-
PARAM.inp.xc_exch_ext_param.end()
22-
)},
23-
{PARAM.inp.xc_corr_ext_param[0], std::vector<double>(
24-
PARAM.inp.xc_corr_ext_param.begin()+1,
25-
PARAM.inp.xc_corr_ext_param.end()
26-
)}
27-
}));
16+
/* xc_polarized = */ XC_POLARIZED);
2817

2918
for(xc_func_type &func : funcs)
3019
{

source/module_io/read_input_item_exx_dftu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void ReadInput::item_exx()
4848
const double exx_hybrid_alpha_value = std::stod(para.input.exx_hybrid_alpha);
4949
if (exx_hybrid_alpha_value < 0 || exx_hybrid_alpha_value > 1)
5050
{
51-
ModuleBase::WARNING_QUIT("ReadInput", "must 0 <= exx_hybrid_alpha <= 1");
51+
ModuleBase::WARNING_QUIT("ReadInput",
52+
"The Hartree-Fock fraction (exx_hybrid_alpha) can only be in range [0, 1]");
5253
}
5354
};
5455
this->add_item(item);

0 commit comments

Comments
 (0)