Skip to content

Commit 34f2e8b

Browse files
committed
support the dft_functional value without the XC_ header
1 parent 3f7c55c commit 34f2e8b

File tree

10 files changed

+77
-32
lines changed

10 files changed

+77
-32
lines changed

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void ESolver_KS_PW<T, Device>::before_scf(const int istep)
210210
//----------------------------------------------------------
211211
// about vdw, jiyy add vdwd3 and linpz add vdwd2
212212
//----------------------------------------------------------
213-
auto vdw_solver = vdw::make_vdw(GlobalC::ucell, PARAM.inp);
213+
auto vdw_solver = vdw::make_vdw(GlobalC::ucell, PARAM.inp, &(GlobalV::ofs_running));
214214
if (vdw_solver != nullptr)
215215
{
216216
this->pelec->f_en.evdw = vdw_solver->get_energy();

source/module_esolver/lcao_before_scf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(const int istep)
198198
//----------------------------------------------------------
199199
// about vdw, jiyy add vdwd3 and linpz add vdwd2
200200
//----------------------------------------------------------
201-
auto vdw_solver = vdw::make_vdw(GlobalC::ucell, PARAM.inp);
201+
auto vdw_solver = vdw::make_vdw(GlobalC::ucell, PARAM.inp, &(GlobalV::ofs_running));
202202
if (vdw_solver != nullptr)
203203
{
204204
this->pelec->f_en.evdw = vdw_solver->get_energy();

source/module_hamilt_general/module_vdw/dftd3_xc_name.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,14 @@ namespace DFTD3 {
344344
void _xcname_libxc_xplusc(const std::string& xcpattern, std::string& xname)
345345
{
346346
std::vector<std::string> xc_words = FmtCore::split(xcpattern, "+");
347+
std::for_each(xc_words.begin(), xc_words.end(), [](std::string& s) {
348+
s = (FmtCore::startswith(s, "XC_")? s: "XC_" + s); }); // add XC_ if not present
347349
assert(xc_words.size() == 2);
348350

349351
std::vector<std::string> words = FmtCore::split(xc_words[0], "_");
350-
const std::string key = (words[2] == "X")? xcpattern: xc_words[1] + "+" + xc_words[0];
351-
352+
const std::string key = (words[2] == "X")?
353+
xc_words[0] + "+" + xc_words[1]: xc_words[1] + "+" + xc_words[0];
354+
352355
if (xcname_libxc_xplusc_.find(key) != xcname_libxc_xplusc_.end()) {
353356
xname = xcname_libxc_xplusc_.at(key);
354357
} else {
@@ -359,9 +362,11 @@ namespace DFTD3 {
359362

360363
void _xcname_libxc_xc(const std::string& xcpattern, std::string& xname)
361364
{
362-
std::vector<std::string> words = FmtCore::split(xcpattern, "_");
363-
if (xcname_libxc_xc_.find(xcpattern) != xcname_libxc_xc_.end()) {
364-
xname = xcname_libxc_xc_.at(xcpattern);
365+
// add XC_ if not present
366+
const std::string key = FmtCore::startswith(xcpattern, "XC_")? xcpattern: "XC_" + xcpattern;
367+
368+
if (xcname_libxc_xc_.find(key) != xcname_libxc_xc_.end()) {
369+
xname = xcname_libxc_xc_.at(key);
365370
} else {
366371
ModuleBase::WARNING_QUIT("ModuleHamiltGeneral::ModuleVDW::DFTD3::xcname_libxc_xc",
367372
"XC's LibXC-notation on `" + xcpattern + "` not recognized");
@@ -380,9 +385,9 @@ namespace DFTD3 {
380385
std::string _xcname(const std::string& xcpattern)
381386
{
382387
std::string xcname = xcpattern;
383-
const std::regex pattern("XC_(LDA|GGA|MGGA|HYB|HYB_LDA|HYB_GGA|HYB_MGGA)_(X|C|XC|K)_(.*)");
388+
const std::regex pattern("(LDA|GGA|MGGA|HYB|HYB_LDA|HYB_GGA|HYB_MGGA)_(X|C|XC|K)_(.*)");
384389
// as long as there is piece in xcpattern that can match, we can search for the corresponding name
385-
if (std::regex_match(xcpattern, pattern)) {
390+
if (std::regex_search(xcpattern, pattern)) {
386391
_xcname_libxc(xcpattern, xcname);
387392
}
388393
return xcname;

source/module_hamilt_general/module_vdw/dftd3_xc_param.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ namespace DFTD3 {
416416
"XC (`" + xc + "`)'s DFT-D3M(0) parameters not found");
417417
}
418418
}
419-
else // zero
419+
else if (method == "zero")
420420
{
421421
if (zero.find(xc_lowercase) != zero.end())
422422
{
@@ -428,6 +428,11 @@ namespace DFTD3 {
428428
"XC (`" + xc + "`)'s DFT-D3(0) parameters not found");
429429
}
430430
}
431+
else // should not reach here
432+
{
433+
ModuleBase::WARNING_QUIT("ModuleHamiltGeneral::ModuleVDW::DFTD3::_search",
434+
"Unknown DFT-D3 method: " + method);
435+
}
431436
}
432437

433438
std::string _lowercase(const std::string& s)
@@ -469,7 +474,8 @@ namespace DFTD3 {
469474
double& s6,
470475
double& s8,
471476
double& a1,
472-
double& a2)
477+
double& a2,
478+
std::ofstream* plog = nullptr)
473479
{
474480
const std::map<std::string, std::string> param_map = {
475481
{"d3_bj", "bj"}, {"d3_0", "zero"}, {"d3_bjm", "bjm"}, {"d3_0m", "zerom"},
@@ -493,12 +499,16 @@ namespace DFTD3 {
493499
s8 = (s8_in == "default") ? param[3] : std::stod(s8_in);
494500
a1 = (a1_in == "default") ? param[2] : std::stod(a1_in);
495501
a2 = (a2_in == "default") ? param[5] : std::stod(a2_in);
496-
// param = {s6, s8, a1, a2};
497-
// FmtTable vdwd3tab({"Parameters", "Original", "Autoset"}, 4, {"%10s", "%10s", "%10.4f"});
498-
// const std::vector<std::string> items = {"s6", "s8", "a1", "a2"};
499-
// vdwd3tab << items << flag << param;
500-
// std::cout << "DFT-D3 Dispersion correction parameters autoset\n" << vdwd3tab.str()
501-
// << std::flush;
502+
if (plog != nullptr) // logging the autoset
503+
{
504+
param = {s6, s8, a1, a2};
505+
FmtTable vdwd3tab({"Parameters", "Original", "Autoset"}, 4, {"%10s", "%10s", "%10.4f"});
506+
const std::vector<std::string> items = {"s6", "s8", "a1", "a2"};
507+
vdwd3tab << items << flag << param;
508+
(*plog) << "\nDFT-D3 Dispersion correction parameters autoset\n" << vdwd3tab.str()
509+
<< "XC functional: " << xc_in << std::endl;
510+
}
511+
502512
}
503513
}
504514
}

source/module_hamilt_general/module_vdw/vdw.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@
66
namespace vdw
77
{
88

9-
std::unique_ptr<Vdw> make_vdw(const UnitCell &ucell, const Input_para &input)
9+
std::unique_ptr<Vdw> make_vdw(const UnitCell &ucell,
10+
const Input_para &input,
11+
std::ofstream* plog)
1012
{
11-
if (ucell.nat < 2 && input.vdw_method != "none")
12-
{
13-
ModuleBase::WARNING("VDW", "Only one atom in this system, and will not do the calculation of VDW");
14-
return nullptr;
15-
}
16-
else if (input.vdw_method == "d2")
13+
// if (ucell.nat < 2 && input.vdw_method != "none")
14+
// {
15+
// ModuleBase::WARNING("VDW", "Only one atom in this system, and will not do the calculation of VDW");
16+
// return nullptr;
17+
// }
18+
if (input.vdw_method == "d2")
1719
{
1820
std::unique_ptr<Vdwd2> vdw_ptr = make_unique<Vdwd2>(ucell);
19-
vdw_ptr->parameter().initial_parameters(input);
21+
vdw_ptr->parameter().initial_parameters(input, plog);
2022
vdw_ptr->parameter().initset(ucell);
2123
return vdw_ptr;
2224
}
2325
else if (input.vdw_method == "d3_0" || input.vdw_method == "d3_bj")
2426
{
2527
std::unique_ptr<Vdwd3> vdw_ptr = make_unique<Vdwd3>(ucell);
26-
vdw_ptr->parameter().initial_parameters(input);
28+
vdw_ptr->parameter().initial_parameters(input, plog);
2729
return vdw_ptr;
2830
}
2931
else
3032
{
33+
ModuleBase::WARNING_QUIT("ModuleHamiltGeneral::ModuleVDW::make_vdw",
34+
"Unrecognized Van der Waals correction method: " + input.vdw_method);
3135
return nullptr;
3236
}
3337
}

source/module_hamilt_general/module_vdw/vdw.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ class Vdw
4848
virtual void cal_stress() = 0;
4949
};
5050

51-
std::unique_ptr<Vdw> make_vdw(const UnitCell &ucell, const Input_para &input);
51+
/**
52+
* @brief make vdw correction object
53+
*
54+
* @param ucell UnitCell instance
55+
* @param input Parameter instance
56+
* @param plog optional, for logging the parameter setting process
57+
* @return std::unique_ptr<Vdw>
58+
*/
59+
std::unique_ptr<Vdw> make_vdw(const UnitCell &ucell,
60+
const Input_para &input,
61+
std::ofstream* plog = nullptr);
5262

5363
} // namespace vdw
5464

source/module_hamilt_general/module_vdw/vdwd2_parameters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace vdw
1010
{
1111

12-
void Vdwd2Parameters::initial_parameters(const Input_para &input)
12+
void Vdwd2Parameters::initial_parameters(const Input_para &input, std::ofstream* plog)
1313
{
1414
scaling_ = std::stod(input.vdw_s6);
1515
damping_ = input.vdw_d;

source/module_hamilt_general/module_vdw/vdwd2_parameters.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ class Vdwd2Parameters : public VdwParameters
3131
void R0_input(const std::string &file, const std::string &unit);
3232

3333
void initset(const UnitCell &ucell); // init sets of vdwd2 once this correction is called
34-
void initial_parameters(const Input_para &input); // initial parameters of Vdwd2 with INPUT file
34+
35+
/**
36+
* @brief initial parameters of Vdwd2 with INPUT file
37+
*
38+
* @param input Parameter instance
39+
* @param plog optional, for logging the parameter setting process (not implemented)
40+
*/
41+
void initial_parameters(const Input_para &input,
42+
std::ofstream* plog = nullptr);
3543

3644
inline const std::map<std::string, double> C6() const { return C6_; }
3745
inline const std::map<std::string, double> R0() const { return R0_; }

source/module_hamilt_general/module_vdw/vdwd3_parameters.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace vdw
1212
{
1313

14-
void Vdwd3Parameters::initial_parameters(const Input_para &input)
14+
void Vdwd3Parameters::initial_parameters(const Input_para &input, std::ofstream* plog)
1515
{
1616
// initialize the dftd3 parameters
1717
mxc_.resize(max_elem_, 1);
@@ -26,7 +26,8 @@ void Vdwd3Parameters::initial_parameters(const Input_para &input)
2626

2727
DFTD3::dftd3_params(input.dft_functional, input.vdw_method,
2828
input.vdw_s6, input.vdw_s8, input.vdw_a1, input.vdw_a2,
29-
s6_, s18_, rs6_, rs18_); /* rs6: a1, rs18: a2 */
29+
s6_, s18_, rs6_, rs18_, /* rs6: a1, rs18: a2 */
30+
plog);
3031
abc_ = input.vdw_abc;
3132
version_ = input.vdw_method;
3233
model_ = input.vdw_cutoff_type;

source/module_hamilt_general/module_vdw/vdwd3_parameters.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ class Vdwd3Parameters : public VdwParameters
2121

2222
~Vdwd3Parameters() = default;
2323

24-
void initial_parameters(const Input_para &input);
24+
/**
25+
* @brief initialize the parameter by either input (from user setting) or autoset by dft XC
26+
*
27+
* @param input Parameter instance
28+
* @param plog optional, for logging the parameter setting process
29+
*/
30+
void initial_parameters(const Input_para &input,
31+
std::ofstream* plog = nullptr); // for logging the parameter autoset
2532

2633
inline const std::string &version() const { return version_; }
2734

0 commit comments

Comments
 (0)