Skip to content
85 changes: 85 additions & 0 deletions source/source_estate/module_charge/chg_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,89 @@ void set_rho_core(const UnitCell& ucell,
ModuleBase::timer::end("module_charge", "set_rho_core");
}

double core_charge_form_factor(int mesh,
const double* r,
const double* rab,
const double* rho_atc,
double omega,
double k,
std::vector<double>& scratch)
Comment thread
mohanchen marked this conversation as resolved.
Resolved
{
// radial FT: rho_core~(k) = 4pi/Omega * Int r^2 rho_atc(r) j0(kr) dr;
// scratch is caller-owned and refilled here (no per-call allocation)
ModuleBase::Sphbes::Spherical_Bessel(mesh, r, k, 0, scratch.data());
for (int ir = 0; ir < mesh; ++ir)
{
scratch[ir] = r[ir] * r[ir] * rho_atc[ir] * scratch[ir];
}
double rhocg = 0.0;
ModuleBase::Integral::Simpson_Integral(mesh, scratch.data(), rab, rhocg);
return ModuleBase::FOUR_PI / omega * rhocg;
}

void set_drho_core(const UnitCell& ucell,
int iat,
int dir,
const ModuleBase::Vector3<double>& q_cart,
const ModulePW::PW_Basis& rhopw,
std::vector<std::complex<double>>& drhoc_g)
{
ModuleBase::TITLE("module_charge", "set_drho_core");
ModuleBase::timer::start("module_charge", "set_drho_core");
const int npw = rhopw.npw;
drhoc_g.clear();
if (npw == 0 || iat < 0 || iat >= ucell.nat || dir < 0 || dir > 2)
{
ModuleBase::timer::end("module_charge", "set_drho_core");
return;
}
const int it = ucell.iat2it[iat];
if (!ucell.atoms[it].ncpp.nlcc)
{
ModuleBase::timer::end("module_charge", "set_drho_core");
return;
}

// tau in cartesian bohr (lat0-scaled: tau is in direct units of latvec)
const ModuleBase::Vector3<double> tau_cart = ucell.atoms[it].tau[ucell.iat2ia[iat]] * ucell.lat0;
const pseudo& p = ucell.atoms[it].ncpp;

drhoc_g.assign(npw, std::complex<double>(0.0, 0.0));
// one scratch buffer reused across the G loop (the kernel allocates
// nothing per call)
std::vector<double> scratch(p.msh);

for (int ig = 0; ig < npw; ++ig)
{
// w is in the scaled gcar units; K = tpiba * w is the physical
// 1/bohr wavevector
const ModuleBase::Vector3<double> w = rhopw.gcar[ig] + q_cart;
const double w2 = w * w;
// skip the |K| = 0 shell (dropped by compute_drho, zero density there)
if (w2 < 1.0e-12)
{
continue;
}
const double k_phys = std::sqrt(w2) * ucell.tpiba;
const double rhocg = core_charge_form_factor(p.msh, p.r.data(), p.rab.data(),
p.rho_atc.data(), ucell.omega, k_phys, scratch);

// structure factor phase: exp(-i (G+q) . tau_iat). The sign is
// load-bearing: exp(+i(G+q)tau) mirrors the core response to the
// -tau site (for wurtzite B the mirror lands exactly on the N
// sites of the other layer), which bn_cubic cannot expose (B at
// the origin makes both conventions identical) but which showed
// up element-by-element in the bn_hex matrix comparison against
// QE dyn.G. QE addcore: e^{-i G tau} * e^{-i q tau}.
const double phase = ModuleBase::TWO_PI * (w * tau_cart) / ucell.lat0;
const std::complex<double> gtau(std::cos(phase), -std::sin(phase));

// direction derivative: -i * tpiba * (G+q)_dir
const double wdir = ucell.tpiba * w[dir];

drhoc_g[ig] = std::complex<double>(0.0, -wdir) * rhocg * gtau;
}
ModuleBase::timer::end("module_charge", "set_drho_core");
}

} // namespace module_charge
41 changes: 41 additions & 0 deletions source/source_estate/module_charge/chg_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
// from Charge members or globals.

#include <complex>
#include <vector>

class UnitCell;
namespace ModuleBase
{
class ComplexMatrix;
template <class T>
class Vector3;
}
namespace ModulePW
{
Expand All @@ -32,6 +35,44 @@ void set_rho_core(const UnitCell& ucell,
std::complex<double>* rhog_core,
const ModulePW::PW_Basis& rhopw);

// First-order core-density response (the q-shifted derivative of the
// core charge, QE addcore convention). Conventions: q_cart is in the
// same scaled Cartesian units as rhopw.gcar (q_cart = q_frac * ucell.G),
// so the physical wavevector is K = tpiba * (gcar + q_cart) in 1/bohr;
// with tau_cart = lat0 * tau (bohr), for the displacement of atom iat
// along direction dir,
// drhoc_g(G) = -i K_dir * rho_atc~(|K|) * exp(-i K.tau_cart)
// where rho_atc~ is the numeric Simpson radial transform
// 4pi/Omega Int r^2 rho_atc(r) j0(|K| r) dr.
// Unlike set_rho_core this evaluates per G (|K| differs at every ig, so
// the gg_uniq shell cache cannot be reused) and carries the
// single-atom structure-factor phase. Empty output for a non-nlcc atom
// or an out-of-range (iat, dir); the |K| = 0 harmonic is left zero.
// Consumed by the DFPT first-order density (DFPT_Rho::compute_drhoc_g
// delegates here).
void set_drho_core(const UnitCell& ucell,
int iat,
int dir,
const ModuleBase::Vector3<double>& q_cart,
const ModulePW::PW_Basis& rhopw,
std::vector<std::complex<double>>& drhoc_g);

// Core-charge form factor at a single physical wavevector k (1/bohr):
// 4pi/Omega * Int r^2 rho_atc(r) j0(k r) dr (numeric Simpson).
// The shared radial kernel of every per-point core-charge evaluation
// (the first-order response above and the DFPT dynmatcc second-order
// route); the ground-state set_rho_core instead shells it through the
// cached, multithreaded non_linear_core_correction. scratch is a
// caller-owned buffer of size mesh reused across calls (the kernel
// allocates nothing per G).
double core_charge_form_factor(int mesh,
const double* r,
const double* rab,
const double* rho_atc,
double omega,
double k,
std::vector<double>& scratch);

// Sum the (spin-resolved) charge density over the real-space grid and
// convert it to a total charge using the cell volume and grid size.
// rho[is][ir] is the density for spin channel is. nspin0 is the number of
Expand Down
39 changes: 39 additions & 0 deletions source/source_estate/module_charge/unittests/test_chg_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "source_estate/module_charge/chg_tools.h"

#include <algorithm>
#include <cmath>
#include <vector>

// chg_tools.cpp references UnitCell (set_rho_core), so the test binary links
Expand All @@ -28,6 +29,10 @@ Magnetism::~Magnetism()
* by omega / nxyz to obtain the electron number
* - check_rho: nspin == 1/4 total-density check, nspin == 2 spin-up/down
* checks, mismatch warning path and negative-channel abort path
* - core_charge_form_factor: the shared per-point radial kernel against
* the closed-form Gaussian transform -- this pins the ABSOLUTE radial
* normalization (4pi/Omega and the Simpson mesh convention), which the
* DFPT localization/phase regressions do not constrain independently
*/

class ChgToolsTest : public ::testing::Test
Expand Down Expand Up @@ -106,3 +111,37 @@ TEST_F(ChgToolsTest, CheckRhoUnsupportedNspinNoop)
// original Charge::check_rho
module_charge::check_rho(rho.data(), 3, nrxx, omega, nxyz, nelec);
}

TEST_F(ChgToolsTest, CoreChargeFormFactorMatchesAnalyticGaussian)
{
// rho_atc(r) = exp(-alpha r^2) on a uniform radial grid; the closed
// form of its spherical transform is
// Int r^2 exp(-alpha r^2) j0(k r) dr = sqrt(pi)/(4 alpha^{3/2}) * exp(-k^2/(4 alpha))
// so the form factor is 4pi/omega times that. Checks K = 0 and several
// finite K values with ONE scratch buffer reused across the calls.
const double pi = 3.14159265358979323846;
const double alpha = 0.5;
const double dr = 0.005;
const int mesh = 2001; // r_max = 10: exp(-50) ~ 2e-22, truncated safely
std::vector<double> r(mesh);
std::vector<double> rab(mesh, dr);
std::vector<double> rho_atc(mesh);
for (int i = 0; i < mesh; ++i)
{
r[i] = i * dr;
rho_atc[i] = std::exp(-alpha * r[i] * r[i]);
Comment thread
mohanchen marked this conversation as resolved.
Resolved
}
const double omega = 137.0; // arbitrary cell volume for the 4pi/omega scale
const double gaussian_norm = std::sqrt(pi) / (4.0 * std::pow(alpha, 1.5));

std::vector<double> scratch(mesh); // one buffer reused across the K values
for (const double k : {0.0, 0.5, 1.0, 2.0, 4.0})
{
const double radial = gaussian_norm * std::exp(-k * k / (4.0 * alpha));
const double expect = 4.0 * pi / omega * radial;
const double got = module_charge::core_charge_form_factor(mesh, r.data(), rab.data(), rho_atc.data(), omega, k, scratch);
// the Simpson sum on this grid converges to ~1e-11 relative; the
// tolerance leaves architectural margin
EXPECT_NEAR(got, expect, 1e-9 * std::abs(expect)) << "k = " << k;
}
}
18 changes: 6 additions & 12 deletions source/source_pw/module_dfpt/dfpt_phon_elec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include "dfpt_pert.h"
#include "dfpt_pw_data.h"
#include "dfpt_rho.h"
#include "source_base/math_integral.h"
#include "source_base/math_sphbes.h"
#include "source_estate/module_charge/chg_tools.h"
#include "source_base/parallel_reduce.h"

#include <algorithm>
Expand Down Expand Up @@ -355,8 +354,9 @@ void DFPT_Phon::accum_nlcc_diag(int atom_idx, int q_idx)
const int mesh = p.msh;
const ModuleBase::Vector3<double> tau_cart
= ucell_->atoms[it].tau[ucell_->iat2ia[atom_idx]] * ucell_->lat0;
std::vector<double> aux(mesh);
std::vector<std::complex<double>> w_g(npw);
// one radial scratch reused across the six tensor-component G passes
std::vector<double> scratch(mesh);
std::vector<std::complex<double>> w_r(nrxx);
double d3[3][3] = {{0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}};
const double fac = ucell_->omega / static_cast<double>(pw_rho_->nxyz);
Expand All @@ -374,15 +374,9 @@ void DFPT_Phon::accum_nlcc_diag(int atom_idx, int q_idx)
{
continue;
}
const double gphys = std::sqrt(g2) * ucell_->tpiba;
ModuleBase::Sphbes::Spherical_Bessel(mesh, p.r.data(), gphys, 0, aux.data());
double rhocg = 0.0;
for (int ir = 0; ir < mesh; ++ir)
{
aux[ir] = p.r[ir] * p.r[ir] * p.rho_atc[ir] * aux[ir];
}
ModuleBase::Integral::Simpson_Integral(mesh, aux.data(), p.rab.data(), rhocg);
rhocg *= ModuleBase::FOUR_PI / ucell_->omega;
const double k_phys = std::sqrt(g2) * ucell_->tpiba;
const double rhocg = module_charge::core_charge_form_factor(
mesh, p.r.data(), p.rab.data(), p.rho_atc.data(), ucell_->omega, k_phys, scratch);

const double phase = ModuleBase::TWO_PI * (gv * tau_cart) / ucell_->lat0;
// AGENTS rule 14: named locals instead of computed ctor args
Expand Down
64 changes: 10 additions & 54 deletions source/source_pw/module_dfpt/dfpt_rho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "source_base/module_mixing/plain_mixing.h"
#include "source_base/module_mixing/broyden_mixing.h"
#include "source_base/parallel_reduce.h"
#include "source_base/math_integral.h"
#include "source_base/math_sphbes.h"
#include "source_estate/module_charge/chg_tools.h"

#include <algorithm>
Expand Down Expand Up @@ -324,61 +322,19 @@ void DFPT_Rho::compute_drhoc_g(const UnitCell& ucell,
const ModuleBase::Vector3<double>& q_cart,
std::vector<std::complex<double>>& drhoc_g) const
{
const int npw = pw_rho_ ? pw_rho_->npw : 0;
drhoc_g.clear();
if (pw_rho_ == nullptr || npw == 0 || iat < 0 || iat >= ucell.nat || dir < 0 || dir > 2)
{
return;
}
const int it = ucell.iat2it[iat];
if (!ucell.atoms[it].ncpp.nlcc)
ModuleBase::TITLE("DFPT_Rho", "compute_drhoc_g");
ModuleBase::timer::start("DFPT_Rho", "compute_drhoc_g");
// the q-shifted core-density response lives in module_charge
// (set_drho_core) next to its ground-state sibling set_rho_core;
// this is a thin delegate keeping the DFPT_Rho call surface stable
if (pw_rho_ == nullptr)
{
drhoc_g.clear();
ModuleBase::timer::end("DFPT_Rho", "compute_drhoc_g");
return;
}

// tau in cartesian bohr (lat0-scaled: tau is in direct units of latvec)
const ModuleBase::Vector3<double> tau_cart = ucell.atoms[it].tau[ucell.iat2ia[iat]] * ucell.lat0;
const pseudo& p = ucell.atoms[it].ncpp;
const int mesh = p.msh;

drhoc_g.assign(npw, std::complex<double>(0.0, 0.0));
std::vector<double> aux(mesh);

for (int ig = 0; ig < npw; ++ig)
{
const ModuleBase::Vector3<double> w = pw_rho_->gcar[ig] + q_cart;
const double w2 = w * w;
// skip the |G+q| = 0 shell (dropped by compute_drho, zero density there)
if (w2 < 1.0e-12)
{
continue;
}
const double g = std::sqrt(w2) * ucell.tpiba; // physical 1/bohr
// radial FT: rho_core~(|G+q|) = 4pi/Omega * Int r^2 rho_atc(r) j0(gr) dr
ModuleBase::Sphbes::Spherical_Bessel(mesh, p.r.data(), g, 0, aux.data());
double rhocg = 0.0;
for (int ir = 0; ir < mesh; ++ir)
{
aux[ir] = p.r[ir] * p.r[ir] * p.rho_atc[ir] * aux[ir];
}
ModuleBase::Integral::Simpson_Integral(mesh, aux.data(), p.rab.data(), rhocg);
rhocg *= ModuleBase::FOUR_PI / ucell.omega;

// structure factor phase: exp(-i (G+q) . tau_iat). The sign is
// load-bearing: exp(+i(G+q)tau) mirrors the core response to the
// -tau site (for wurtzite B the mirror lands exactly on the N
// sites of the other layer), which bn_cubic cannot expose (B at
// the origin makes both conventions identical) but which showed
// up element-by-element in the bn_hex matrix comparison against
// QE dyn.G. QE addcore: e^{-i G tau} * e^{-i q tau}.
const double phase = ModuleBase::TWO_PI * (w * tau_cart) / ucell.lat0;
const std::complex<double> gtau(std::cos(phase), -std::sin(phase));

// direction derivative: -i * tpiba * (G+q)_dir
const double wdir = ucell.tpiba * w[dir];

drhoc_g[ig] = std::complex<double>(0.0, -wdir) * rhocg * gtau;
}
module_charge::set_drho_core(ucell, iat, dir, q_cart, *pw_rho_, drhoc_g);
ModuleBase::timer::end("DFPT_Rho", "compute_drhoc_g");
}

void DFPT_Rho::cal_docc(const psi::Psi<std::complex<double>>& psi,
Expand Down
1 change: 1 addition & 0 deletions source/source_pw/module_dfpt/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ addtest(
../dfpt_kq_basis.cpp
../dfpt_stern.cpp
../dfpt_rho.cpp
../../../source_estate/module_charge/chg_tools.cpp
../dfpt_phon.cpp
../dfpt_phon_ewald.cpp
../dfpt_phon_elec.cpp
Expand Down
3 changes: 3 additions & 0 deletions source/source_pw/module_dfpt/test_serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ addtest(
LIBS parameter dfpt_planewave_serial device base symmetry
SOURCES dfpt_rho_serial_test.cpp
../dfpt_rho.cpp
../../../source_estate/module_charge/chg_tools.cpp
../dfpt_pw_data.cpp
../dfpt_kq_basis.cpp
../../../source_cell/qlist.cpp
Expand All @@ -68,6 +69,7 @@ addtest(
../dfpt_phon_ewald.cpp
../dfpt_phon_elec.cpp
../dfpt_rho.cpp
../../../source_estate/module_charge/chg_tools.cpp
../dfpt_pert.cpp
../dfpt_pert_vkb.cpp
../dfpt_pert_nl.cpp
Expand Down Expand Up @@ -129,6 +131,7 @@ if(ENABLE_LIBXC)
SOURCES dfpt_xc_analytic_serial_test.cpp
../dfpt_xc_analytic.cpp
../dfpt_rho.cpp
../../../source_estate/module_charge/chg_tools.cpp
../dfpt_pw_data.cpp
../dfpt_kq_basis.cpp
../../../source_cell/qlist.cpp
Expand Down
Loading