Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions source/module_elecstate/potentials/pot_xc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
//----------------------------------------------------------
// calculate the exchange-correlation potential
//----------------------------------------------------------

if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
{
#ifdef USE_LIBXC
Expand All @@ -44,4 +43,4 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
ModuleBase::timer::tick("PotXC", "cal_v_eff");
}

} // namespace elecstate
} // namespace elecstate
5 changes: 5 additions & 0 deletions source/module_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ void ESolver_KS<T, Device>::before_all_runners(UnitCell& ucell, const Input_para
//! 4) it has been established that
// xc_func is same for all elements, therefore
// only the first one if used

// A little hacking of the code here. Adding the
// following line to set up the density threshold
// for DFT calculations.
XC_Functional::set_xc_ingred_thrs(PARAM.inp.xc_dens_thr);
if (PARAM.inp.use_paw)
{
XC_Functional::set_xc_type(PARAM.inp.dft_functional);
Expand Down
42 changes: 40 additions & 2 deletions source/module_hamilt_general/module_xc/xc_functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,59 @@ XC_Functional::XC_Functional(){}

XC_Functional::~XC_Functional(){}

//double XC_Functional::dens_threshold;
//double XC_Functional::zeta_threshold;
//double XC_Functional::grho_threshold;
//double XC_Functional::tau_threshold;

double XC_Functional::dens_threshold = 1.0e-10;
double XC_Functional::zeta_threshold = dens_threshold;
double XC_Functional::grho_threshold = dens_threshold;
double XC_Functional::tau_threshold = dens_threshold;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, I have on major and two minor comments here.
the major one is, your change indicate all these four thresholds are correlated. Is this always true? if not, you can use the "dens_threshold" as default for the latter three.
the other two minor are:

  1. for you abbreviate the "density" as "dens", you can also use "thr" instead of the full name "threshold". The use of "thr" can refer to QE, like conv_thr, force_thr, etc.
  2. you name variables with some inconsistency. You denote the density as "dens" but use physical symbol name like "tau", "zeta", "grho" in the following. For it is under the context of "XC", so it is okay to use "rho" instead, say, "rho_thr", "grho_thr", "zeta_thr" and "tau_thr".


std::vector<int> XC_Functional::func_id(1);
int XC_Functional::func_type = 0;
bool XC_Functional::use_libxc = true;
double XC_Functional::hybrid_alpha = 0.25;

void XC_Functional::set_hybrid_alpha(const double alpha_in)
void XC_Functional::set_xc_ingred_thrs(double _thr_in)
{
hybrid_alpha = alpha_in;
XC_Functional::dens_threshold = _thr_in;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void XC_Functional::set_xc_ingred_thrs(double _thr_in)
{
hybrid_alpha = alpha_in;
XC_Functional::dens_threshold = _thr_in;
void XC_Functional::set_xc_ingred_thrs(const double& thr)
{
XC_Functional::dens_threshold = thr;

XC_Functional::zeta_threshold = dens_threshold;
XC_Functional::grho_threshold = dens_threshold*1.0e-4;
XC_Functional::tau_threshold = dens_threshold*1.0e-4;
}

double XC_Functional::get_dens_threshold()
{
return dens_threshold;
}

double XC_Functional::get_grho_threshold()
{
return grho_threshold;
}

double XC_Functional::get_zeta_threshold()
{
return zeta_threshold;
}

double XC_Functional::get_tau_threshold()
{
return tau_threshold;
}

double XC_Functional::get_hybrid_alpha()
{
return hybrid_alpha;
}

void XC_Functional::set_hybrid_alpha(const double alpha_in)
{
hybrid_alpha = alpha_in;
}

int XC_Functional::get_func_type()
{
return func_type;
Expand Down
11 changes: 11 additions & 0 deletions source/module_hamilt_general/module_xc/xc_functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class XC_Functional
static int get_func_type();
static void set_xc_type(const std::string xc_func_in);

static void set_xc_ingred_thrs(double _thr_in);
static double get_dens_threshold();
static double get_grho_threshold();
static double get_zeta_threshold();
static double get_tau_threshold();

// For hybrid functional
static void set_hybrid_alpha(const double alpha_in);
static double get_hybrid_alpha();
Expand All @@ -77,6 +83,11 @@ class XC_Functional
static int func_type; //0:none, 1:lda, 2:gga, 3:mgga, 4:hybrid lda/gga, 5:hybrid mgga
static bool use_libxc;


static double dens_threshold;
static double grho_threshold;
static double zeta_threshold;
static double tau_threshold;
//exx_hybrid_alpha for mixing exx in hybrid functional:
static double hybrid_alpha;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ void XC_Functional::gradcorr(double &etxc, double &vtxc, ModuleBase::matrix &v,

}

const double epsr = 1.0e-6;
const double epsg = 1.0e-10;
const double epsr = dens_threshold;
const double epsg = grho_threshold;

double vtxcgc = 0.0;
double etxcgc = 0.0;
Expand Down Expand Up @@ -791,4 +791,4 @@ template void XC_Functional::grad_wfc<std::complex<double>, base_device::DEVICE_
const ModulePW::PW_Basis_K* wfc_basis,
const std::complex<double>* rhog,
std::complex<double>* grad);
#endif // __CUDA || __ROCM
#endif // __CUDA || __ROCM
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ ModuleBase::matrix XC_Functional_Libxc::convert_v_nspin4(
const ModuleBase::matrix &v)
{
assert(PARAM.inp.nspin==4);
constexpr double vanishing_charge = 1.0e-10;
const double vanishing_charge = XC_Functional::get_dens_threshold();
ModuleBase::matrix v_nspin4(PARAM.inp.nspin, nrxx);
for( int ir=0; ir<nrxx; ++ir )
v_nspin4(0,ir) = 0.5 * (v(0,ir)+v(1,ir));
Expand All @@ -287,4 +287,4 @@ ModuleBase::matrix XC_Functional_Libxc::convert_v_nspin4(
return v_nspin4;
}

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( /
double vtxc = 0.0;
ModuleBase::matrix v(nspin,nrxx);

// std::cout<< "hello from v_xc_libxc, threshold = " << XC_Functional::get_dens_threshold() << " " << XC_Functional::get_grho_threshold()<< std::endl;

for( xc_func_type &func : funcs )
{
// jiyy add for threshold
constexpr double rho_threshold = 1E-6;
constexpr double grho_threshold = 1E-10;
const double rho_threshold = XC_Functional::get_dens_threshold();
const double grho_threshold = XC_Functional::get_grho_threshold();

xc_func_set_dens_threshold(&func, rho_threshold);

Expand Down Expand Up @@ -207,9 +209,9 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
std::vector<double> vtau ( nrxx * nspin );
std::vector<double> vlapl ( nrxx * nspin );

constexpr double rho_th = 1e-8;
constexpr double grho_th = 1e-12;
constexpr double tau_th = 1e-8;
const double rho_th = XC_Functional::get_dens_threshold();
const double grho_th = XC_Functional::get_grho_threshold();
const double tau_th = XC_Functional::get_tau_threshold();
// sgn for threshold mask
std::vector<double> sgn( nrxx * nspin);
#ifdef _OPENMP
Expand Down Expand Up @@ -392,4 +394,4 @@ std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> XC_Functional_Li
return std::make_tuple( etxc, vtxc, std::move(v), std::move(vofk) );
}

#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifdef USE_LIBXC

#include "xc_functional_libxc.h"
#include "xc_functional.h"

#include <xc.h>
#include <array>
Expand All @@ -12,9 +13,12 @@ void XC_Functional_Libxc::gcxc_libxc(
{
sxc = v1xc = v2xc = 0.0;

constexpr double small = 1.e-6;
constexpr double smallg = 1.e-10;
if (rho <= small || grho < smallg)
// constexpr double small = 1.e-6;
// constexpr double smallg = 1.e-10;

// std::cout << "hello from XC_Functional_Libxc, threshold = " << XC_Functional::get_dens_threshold() << std::endl;

if (rho <= XC_Functional::get_dens_threshold() || grho < XC_Functional::get_grho_threshold())
{
return;
}
Expand Down Expand Up @@ -49,8 +53,9 @@ void XC_Functional_Libxc::gcxc_spin_libxc(
{
if( func.info->family == XC_FAMILY_GGA || func.info->family == XC_FAMILY_HYB_GGA)
{
constexpr double rho_threshold = 1E-6;
constexpr double grho_threshold = 1E-10;
// std::cout << "hello libxc gcxc_spin_libxc, threshold: " << XC_Functional::get_dens_threshold() << std::endl;
const double rho_threshold = XC_Functional::get_dens_threshold();
const double grho_threshold = XC_Functional::get_grho_threshold();
std::array<double,2> sgn = {1.0, 1.0};
if(func.info->kind==XC_CORRELATION)
{
Expand All @@ -76,4 +81,4 @@ void XC_Functional_Libxc::gcxc_spin_libxc(
XC_Functional_Libxc::finish_func(funcs);
}

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifdef USE_LIBXC

#include "xc_functional_libxc.h"
#include "xc_functional.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include <array>

Expand Down Expand Up @@ -67,8 +68,8 @@ void XC_Functional_Libxc::tau_xc_spin(
{
if( func.info->family == XC_FAMILY_MGGA || func.info->family == XC_FAMILY_HYB_MGGA)
{
constexpr double rho_threshold = 1E-6;
constexpr double grho_threshold = 1E-10;
const double rho_threshold = XC_Functional::get_dens_threshold();
const double grho_threshold = XC_Functional::get_grho_threshold();
std::array<double,2> sgn = {1.0, 1.0};
if(func.info->kind==XC_CORRELATION)
{
Expand Down Expand Up @@ -98,4 +99,4 @@ void XC_Functional_Libxc::tau_xc_spin(
XC_Functional_Libxc::finish_func(funcs);
}

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::tuple<double,double,ModuleBase::matrix> XC_Functional::v_xc(
// in Rydeberg unit, so * 2.0.
double e2 = 2.0;

double vanishing_charge = 1.0e-10;
double vanishing_charge = dens_threshold;

if (PARAM.inp.nspin == 1 || ( PARAM.inp.nspin ==4 && !PARAM.globalv.domag && !PARAM.globalv.domag_z))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ void XC_Functional::gcxc(const double &rho, const double &grho, double &sxc,
// USE kinds
// implicit none
// real rho, grho, sx, sc, v1x, v2x, v1c, v2c;
const double small = 1.e-6;
const double smallg = 1.e-10;
double s,v1,v2;
sxc = v1xc = v2xc = 0.0;

if (rho <= small || grho < smallg)
std::cout << "threshold for densities " << dens_threshold << " " << rho << " " << grho<< std::endl;
if (rho <= dens_threshold || grho < grho_threshold)
{
return;
}
Expand Down Expand Up @@ -127,7 +125,7 @@ void XC_Functional::gcx_spin(double rhoup, double rhodw, double grhoup2, double
// derivatives of exchange wr. grho

// parameter :
double small = 1.e-10;
double small = dens_threshold;
double sxup, sxdw;
int iflag;

Expand All @@ -139,6 +137,7 @@ void XC_Functional::gcx_spin(double rhoup, double rhodw, double grhoup2, double
v1xdw = 0.00; v2xdw = 0.00;
sxup = 0.00; sxdw = 0.00;


if (rho <= small)
{
return;
Expand Down Expand Up @@ -245,14 +244,17 @@ void XC_Functional::gcc_spin(double rho, double &zeta, double grho, double &sc,
// derivatives of correlation wr. grho

// parameter :
double small = 1.0e-10;
double epsr = 1.0e-6;
double small = dens_threshold;
double epsr = zeta_threshold;

double x;

sc = 0.00;
v1cup = 0.00; v1cdw = 0.00;
v2c = 0.00;

// std::cout << "hello from gcc_spin, threshold = " << dens_threshold << " " << grho_threshold << std::endl;

if (std::abs(zeta) - 1.0 > small || rho <= small || sqrt(std::abs(grho)) <= small)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ void XC_Functional::xc_spin(const double &rho, const double &zeta,
vxcdw += vdw;
}
return;
}
}
13 changes: 13 additions & 0 deletions source/module_io/read_input_item_elec_stru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,19 @@ void ReadInput::item_elec_stru()
};
this->add_item(item);
}
{
Input_Item item("xc_dens_thr");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item.annotation = "threshold for density and other dft functional ingredients";
read_sync_double(input.xc_dens_thr);
item.check_value = [] (const Input_Item& item, const Parameter& para){
if (para.input.xc_dens_thr <= 0.0)
{
ModuleBase::WARNING_QUIT("ReadInput", "xc_dens_thr should be positive");
}
std::cout<< "resetting xc_dens_thr " << para.input.xc_dens_thr<< std::endl;
};
this->add_item(item);
}
{
Input_Item item("scf_ene_thr");
item.annotation = "total energy error threshold";
Expand Down
1 change: 1 addition & 0 deletions source/module_parameter/input_parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ struct Input_para
bool gamma_only = false; ///< for plane wave.
int scf_nmax = 100; ///< number of max elec iter
double scf_thr = -1.0; ///< \sum |rhog_out - rhog_in |^2
double xc_dens_thr = 1.0e-10; ///< threshold for density and other DFT ingredients to toss away in DFT calculation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will xc_rho_thr be better?

double scf_ene_thr = -1.0; ///< energy threshold for scf convergence, in eV
int scf_thr_type = -1; ///< type of the criterion of scf_thr, 1: reci drho, 2: real drho
bool final_scf = false; ///< whether to do final scf
Expand Down
Loading