Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions source/source_estate/module_pot/pot_xc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "source_base/timer.h"
#include "source_hamilt/module_xc/xc_functional.h"
#include "source_io/module_parameter/parameter.h"

#ifdef USE_LIBXC
#include "source_hamilt/module_xc/libxc_abacus.h"
Expand All @@ -24,7 +25,8 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
{
#ifdef USE_LIBXC
const std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> etxc_vtxc_v
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), nrxx_current, ucell->omega, ucell->tpiba, chg);
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), nrxx_current, ucell->omega, ucell->tpiba, chg,
PARAM.inp.nspin);
*(this->etxc_) = std::get<0>(etxc_vtxc_v);
*(this->vtxc_) = std::get<1>(etxc_vtxc_v);
v_eff += std::get<2>(etxc_vtxc_v);
Expand All @@ -36,7 +38,10 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
else
{
const std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v
= XC_Functional::v_xc(nrxx_current, chg, ucell);
= XC_Functional::v_xc(nrxx_current, chg, ucell,
PARAM.inp.nspin,
PARAM.globalv.domag,
PARAM.globalv.domag_z);
*(this->etxc_) = std::get<0>(etxc_vtxc_v);
*(this->vtxc_) = std::get<1>(etxc_vtxc_v);
v_eff += std::get<2>(etxc_vtxc_v);
Expand Down
11 changes: 9 additions & 2 deletions source/source_estate/module_pot/pot_xc_fdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "pot_xc_fdm.h"
#include "source_hamilt/module_xc/xc_functional.h"
#include "source_io/module_parameter/parameter.h"

namespace elecstate
{
Expand All @@ -20,7 +21,10 @@ PotXC_FDM::PotXC_FDM(
this->fixed_mode = false;

const std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v_0
= XC_Functional::v_xc(this->chg_0->nrxx, this->chg_0, ucell);
= XC_Functional::v_xc(this->chg_0->nrxx, this->chg_0, ucell,
PARAM.inp.nspin,
PARAM.globalv.domag,
PARAM.globalv.domag_z);
this->v_xc_0 = std::get<2>(etxc_vtxc_v_0);
}

Expand All @@ -47,7 +51,10 @@ void PotXC_FDM::cal_v_eff(
}

const std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v_01
= XC_Functional::v_xc(chg_01.nrxx, &chg_01, ucell);
= XC_Functional::v_xc(chg_01.nrxx, &chg_01, ucell,
PARAM.inp.nspin,
PARAM.globalv.domag,
PARAM.globalv.domag_z);
const ModuleBase::matrix &v_xc_01 = std::get<2>(etxc_vtxc_v_01);

v_eff += v_xc_01 - this->v_xc_0;
Expand Down
8 changes: 6 additions & 2 deletions source/source_hamilt/module_xc/libxc_abacus.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ namespace XC_Functional_Libxc
const double &omega, // volume of cell
const double tpiba,
const Charge* const chr, // charge density
const std::map<int, double>* scaling_factor = nullptr); // added by jghan, 2024-10-10
const std::map<int, double>* scaling_factor = nullptr, // added by jghan, 2024-10-10
const int nspin = 1,
const bool domag = false,
const bool domag_z = false);

// for mGGA functional
extern std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> v_xc_meta(
const std::vector<int> &func_id,
const int &nrxx, // number of real-space grid
const double &omega, // volume of cell
const double tpiba,
const Charge* const chr);
const Charge* const chr,
const int nspin = 1);


//-------------------
Expand Down
20 changes: 11 additions & 9 deletions source/source_hamilt/module_xc/libxc_pot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
const double &omega, // volume of cell
const double tpiba,
const Charge* const chr,
const std::map<int, double>* scaling_factor)
const std::map<int, double>* scaling_factor,
const int nspin_in,
const bool domag,
const bool domag_z)
{
ModuleBase::TITLE("XC_Functional_Libxc","v_xc_libxc");
ModuleBase::timer::start("XC_Functional_Libxc","v_xc_libxc");

const int nspin =
(PARAM.inp.nspin == 1 || ( PARAM.inp.nspin ==4 && !PARAM.globalv.domag && !PARAM.globalv.domag_z))
(nspin_in == 1 || ( nspin_in ==4 && !domag && !domag_z))
? 1 : 2;

//----------------------------------------------------------
Expand Down Expand Up @@ -56,7 +59,7 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
// converting rho
std::vector<double> rho;
std::vector<double> amag;
if(1==nspin || 2==PARAM.inp.nspin)
if(1==nspin || 2==nspin_in)
{
rho = XC_Functional_Libxc::convert_rho(nspin, nrxx, chr);
}
Expand Down Expand Up @@ -167,7 +170,7 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
v += std::get<1>(vtxc_v) * factor;
} // end for( xc_func_type &func : funcs )

if(4==PARAM.inp.nspin)
if(4==nspin_in)
{
v = XC_Functional_Libxc::convert_v_nspin4(nrxx, chr, amag, v);
}
Expand Down Expand Up @@ -207,7 +210,8 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
const int &nrxx, // number of real-space grid
const double &omega, // volume of cell
const double tpiba,
const Charge* const chr)
const Charge* const chr,
const int nspin)
{
ModuleBase::TITLE("XC_Functional_Libxc","v_xc_meta");
ModuleBase::timer::start("XC_Functional_Libxc","v_xc_meta");
Expand All @@ -217,17 +221,15 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
//output of the subroutine
double etxc = 0.0;
double vtxc = 0.0;
ModuleBase::matrix v(PARAM.inp.nspin,nrxx);
ModuleBase::matrix vofk(PARAM.inp.nspin,nrxx);
ModuleBase::matrix v(nspin,nrxx);
ModuleBase::matrix vofk(nspin,nrxx);

//----------------------------------------------------------
// xc_func_type is defined in Libxc package
// to understand the usage of xc_func_type,
// use can check on website, for example:
// https://www.tddft.org/programs/libxc/manual/libxc-5.1.x/
//----------------------------------------------------------

const int nspin = PARAM.inp.nspin;
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
/* func_id = */ func_id,
/* xc_polarized = */ (1==nspin) ? XC_UNPOLARIZED:XC_POLARIZED);
Expand Down
19 changes: 6 additions & 13 deletions source/source_hamilt/module_xc/test/test_xc3.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "gtest/gtest.h"
#include "xctest.h"
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
#include "../xc_functional.h"
#include "../exx_info.h"
#include "xc3_mock.h"
Expand Down Expand Up @@ -82,19 +79,15 @@ class XCTest_GRADCORR : public XCTest
v4.create(4,5);
v4.zero_out();

XC_Functional::set_xc_type("PBE");
XC_Functional::set_xc_type("PBE", 1);

PARAM.input.nspin = 1;
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,false);
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,true);
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,false,1,false,false);
XC_Functional::gradcorr(et1,vt1,v1,&chr,&rhopw,&ucell,stress1,true,1,false,false);

PARAM.input.nspin = 2;
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,false);
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,true);
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,false,2,false,false);
XC_Functional::gradcorr(et2,vt2,v2,&chr,&rhopw,&ucell,stress2,true,2,false,false);

PARAM.input.nspin = 4;
PARAM.sys.domag = true;
XC_Functional::gradcorr(et4,vt4,v4,&chr,&rhopw,&ucell,stress4,false);
XC_Functional::gradcorr(et4,vt4,v4,&chr,&rhopw,&ucell,stress4,false,4,true,false);
}
};

Expand Down
27 changes: 9 additions & 18 deletions source/source_hamilt/module_xc/test/test_xc5.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "../xc_functional.h"
#include "../libxc_abacus.h"
#include "gtest/gtest.h"
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
#include "xctest.h"
#include "../exx_info.h"
#include "xc3_mock.h"
Expand Down Expand Up @@ -73,18 +70,16 @@ class XCTest_VXC : public XCTest
rhopw.gcar[i]= 1;
}

XC_Functional::set_xc_type("PBE");
XC_Functional::set_xc_type("PBE", 1);

PARAM.input.nspin = 1;
std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,1,false,false);
et1 = std::get<0>(etxc_vtxc_v);
vt1 = std::get<1>(etxc_vtxc_v);
v1 = std::get<2>(etxc_vtxc_v);

PARAM.input.nspin = 2;
etxc_vtxc_v
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,2,false,false);
et2 = std::get<0>(etxc_vtxc_v);
vt2 = std::get<1>(etxc_vtxc_v);
v2 = std::get<2>(etxc_vtxc_v);
Expand Down Expand Up @@ -171,18 +166,16 @@ class XCTest_VXC_Libxc : public XCTest
rhopw.gcar[i]= 1;
}

XC_Functional::set_xc_type("GGA_X_PBE+GGA_C_PBE");
XC_Functional::set_xc_type("GGA_X_PBE+GGA_C_PBE", 1);

PARAM.input.nspin = 1;
std::tuple<double, double, ModuleBase::matrix> etxc_vtxc_v
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,1,false,false);
et1 = std::get<0>(etxc_vtxc_v);
vt1 = std::get<1>(etxc_vtxc_v);
v1 = std::get<2>(etxc_vtxc_v);

PARAM.input.nspin = 2;
etxc_vtxc_v
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell);
= XC_Functional::v_xc(rhopw.nrxx,&chr,&ucell,2,false,false);
et2 = std::get<0>(etxc_vtxc_v);
vt2 = std::get<1>(etxc_vtxc_v);
v2 = std::get<2>(etxc_vtxc_v);
Expand Down Expand Up @@ -279,19 +272,17 @@ class XCTest_VXC_meta : public XCTest
chr.kin_r[1][3] = 0.51340429824;
chr.kin_r[1][4] = 0.51141731056;

XC_Functional::set_xc_type("SCAN");
XC_Functional::set_xc_type("SCAN", 1);

PARAM.input.nspin = 1;
std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> etxc_vtxc_v
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr);
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr,1);
et1 = std::get<0>(etxc_vtxc_v);
vt1 = std::get<1>(etxc_vtxc_v);
v1 = std::get<2>(etxc_vtxc_v);
vtau1 = std::get<3>(etxc_vtxc_v);

PARAM.input.nspin = 2;
etxc_vtxc_v
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr);
= XC_Functional_Libxc::v_xc_meta(XC_Functional::get_func_id(), rhopw.nrxx,ucell.omega,ucell.tpiba,&chr,2);
et2 = std::get<0>(etxc_vtxc_v);
vt2 = std::get<1>(etxc_vtxc_v);
v2 = std::get<2>(etxc_vtxc_v);
Expand Down
10 changes: 1 addition & 9 deletions source/source_hamilt/module_xc/test/xctest.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
#ifndef XCTEST_H
#define XCTEST_H
#include "gtest/gtest.h"
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
class XCTest: public testing::Test
{
public:
XCTest()
{
PARAM.input.basis_type = "";
PARAM.input.cal_force = 0;
PARAM.input.cal_stress = 0;
}
XCTest() {}
};
#endif
10 changes: 6 additions & 4 deletions source/source_hamilt/module_xc/xc_functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ method. */

// The setting values of functional id according to the index in LIBXC
// for detail, refer to https://www.tddft.org/programs/libxc/functionals/
void XC_Functional::set_xc_type(const std::string xc_func_in)
void XC_Functional::set_xc_type(const std::string xc_func_in,
const int nspin /*= 1*/,
const std::string basis_type /*= "pw"*/)
{
ModuleBase::TITLE("XC_Functional", "set_xc_type");
//Note : due to the separation of gcx_spin and gcc_spin,
Expand Down Expand Up @@ -301,17 +303,17 @@ void XC_Functional::set_xc_type(const std::string xc_func_in)
std::cerr << "\n OPTX untested please test,";
}

// if((func_type == 4 || func_type == 5) && PARAM.inp.basis_type == "pw")
// if((func_type == 4 || func_type == 5) && basis_type == "pw")
// {
// ModuleBase::WARNING_QUIT("set_xc_type","hybrid functional not realized for planewave yet");
// }
if((func_type == 3 || func_type == 5) && PARAM.inp.nspin==4)
if((func_type == 3 || func_type == 5) && nspin==4)
{
ModuleBase::WARNING_QUIT("set_xc_type","meta-GGA has not been implemented for nspin = 4 yet");
}

#ifndef __EXX
if((func_type == 4 || func_type == 5) && PARAM.inp.basis_type == "lcao")
if((func_type == 4 || func_type == 5) && basis_type == "lcao")
{
ModuleBase::WARNING_QUIT("set_xc_type","compile with libri to use hybrid functional in lcao basis");
}
Expand Down
14 changes: 11 additions & 3 deletions source/source_hamilt/module_xc/xc_functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class XC_Functional
static std::tuple<double, double, ModuleBase::matrix> v_xc(
const int &nrxx, // number of real-space grid
const Charge* const chr,
const UnitCell *ucell); // charge density
const UnitCell *ucell, // charge density
const int nspin,
const bool domag = false,
const bool domag_z = false);

//-------------------
// xc_functional.cpp
Expand All @@ -67,7 +70,9 @@ class XC_Functional
return func_type;
};

static void set_xc_type(const std::string xc_func_in);
static void set_xc_type(const std::string xc_func_in,
const int nspin = 1,
const std::string basis_type = "pw");

// For hybrid functional
static void set_hybrid_alpha(const double alpha_in);
Expand Down Expand Up @@ -206,7 +211,10 @@ class XC_Functional
ModulePW::PW_Basis* rhopw,
const UnitCell* ucell,
std::vector<double>& stress_gga,
const bool is_stress = false);
const bool is_stress = false,
const int nspin = 1,
const bool domag = false,
const bool domag_z = false);
template <typename T, typename Device,
typename Real = typename GetTypeReal<T>::type>

Expand Down
Loading
Loading