Skip to content
Merged
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
19 changes: 11 additions & 8 deletions source/source_estate/module_pot/pot_surchem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ class PotSurChem : public PotBase
}
}

// Passing an explicit output matrix makes the lifetime and allocation explicit and avoids hidden allocations.
void cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix& v_eff) override
{
if (!this->allocated)
{
this->surchem_->allocate(this->rho_basis_->nrxx, v_eff.nr);
this->allocated = true;
}

v_eff += this->surchem_->v_correction(*ucell,
*chg->pgrid,
const_cast<ModulePW::PW_Basis*>(this->rho_basis_),
v_eff.nr,
chg->rho,
this->vlocal,
this->structure_factors_);
ModuleBase::matrix v_sol_correction(v_eff.nr, this->rho_basis_->nrxx);
this->surchem_->v_correction(*ucell,
*chg->pgrid,
const_cast<ModulePW::PW_Basis*>(this->rho_basis_),
v_eff.nr,
chg->rho,
this->vlocal,
this->structure_factors_,
v_sol_correction);
v_eff += v_sol_correction;
}

private:
Expand Down
32 changes: 19 additions & 13 deletions source/source_hamilt/module_surchem/H_correction_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include "source_base/timer.h"
#include "source_hamilt/module_xc/xc_functional.h"
#include "surchem.h"

ModuleBase::matrix surchem::v_correction(const UnitCell& cell,
const Parallel_Grid& pgrid,
const ModulePW::PW_Basis* rho_basis,
const int& nspin,
const double* const* const rho,
const double* vlocal,
Structure_Factor* sf)
// Changing the interface to use an explicit output parameter clarifies lifetime management and avoids hidden allocations.
void surchem::v_correction(const UnitCell& cell,
const Parallel_Grid& pgrid,
const ModulePW::PW_Basis* rho_basis,
const int& nspin,
const double* const* const rho,
const double* vlocal,
Structure_Factor* sf,
ModuleBase::matrix& v)
{
ModuleBase::TITLE("surchem", "v_cor");
ModuleBase::timer::tick("surchem", "v_cor");
Expand Down Expand Up @@ -46,10 +47,15 @@ ModuleBase::matrix surchem::v_correction(const UnitCell& cell,

cal_pseudo(cell, pgrid, rho_basis, porter_g, ps_totn, sf);

ModuleBase::matrix v(nspin, rho_basis->nrxx);
// ModuleBase::matrix v(nspin, rho_basis->nrxx);
if (v.nr != nspin || v.nc != rho_basis->nrxx)
{
v.create(nspin, rho_basis->nrxx);
}
ModuleBase::GlobalFunc::ZEROS(v.c, nspin * rho_basis->nrxx);

v += cal_vel(cell, rho_basis, total_n, ps_totn, nspin);
v += cal_vcav(cell, rho_basis, ps_totn, nspin);
cal_vel(cell, rho_basis, total_n, ps_totn, nspin, v);
cal_vcav(cell, rho_basis, ps_totn, nspin, v);

delete[] porter;
delete[] porter_g;
Expand All @@ -58,5 +64,5 @@ ModuleBase::matrix surchem::v_correction(const UnitCell& cell,
delete[] total_n;

ModuleBase::timer::tick("surchem", "v_cor");
return v;
}
return;
}
4 changes: 2 additions & 2 deletions source/source_hamilt/module_surchem/cal_pseudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ void surchem::gauss_charge(const UnitCell& cell,
std::complex<double>* N,
Structure_Factor* sf)
{
sf->setup(&cell, pgrid, rho_basis); // this is strange, should be removed to other places, mohan add 2025-11-04

//sf->setup(&cell, pgrid, rho_basis); // this is strange, should be removed to other places, mohan add 2025-11-04
//sf here was only needed in the test.
const int ig0 = rho_basis->ig_gge0; // G=0 index
for (int it = 0; it < cell.ntype; it++)
{
Expand Down
38 changes: 22 additions & 16 deletions source/source_hamilt/module_surchem/cal_vcav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ void lapl_rho(const double& tpiba2,
// the formula is : rho(r)^prime = \int iG * rho(G)e^{iGr} dG
for (int ig = 0; ig < rho_basis->npw; ig++) {
gdrtmpg[ig] = rhog[ig];
}
}

for(int i = 0 ; i < 3 ; ++i)
{
// calculate the charge density gradient in reciprocal space.
// calculate the charge density gradient in reciprocal space.
for (int ig = 0; ig < rho_basis->npw; ig++) {
aux[ig] = gdrtmpg[ig] * pow(rho_basis->gcar[ig][i], 2);
}
}

// bring the gdr from G --> R
rho_basis->recip2real(aux, aux);

// remember to multily 2pi/a0, which belongs to G vectors.
for (int ir = 0; ir < rho_basis->nrxx; ir++) {
lapn[ir] -= aux[ir].real() * tpiba2;
}

}
}

delete[] gdrtmpg;
delete[] aux;
return;
}

// calculates first derivative of the shape function in realspace
Expand Down Expand Up @@ -66,6 +67,7 @@ void surchem::createcavity(const UnitCell& ucell,
{
ModuleBase::Vector3<double> *nablan = new ModuleBase::Vector3<double>[rho_basis->nrxx];
ModuleBase::GlobalFunc::ZEROS(nablan, rho_basis->nrxx);

double *nablan_2 = new double[rho_basis->nrxx];
double *sqrt_nablan_2 = new double[rho_basis->nrxx];
double *lapn = new double[rho_basis->nrxx];
Expand All @@ -77,14 +79,14 @@ void surchem::createcavity(const UnitCell& ucell,
// nabla n
XC_Functional::grad_rho(ps_totn, nablan, rho_basis, ucell.tpiba);

// |\nabla n |^2 = nablan_2
// |\nabla n |^2 = nablan_2
for (int ir = 0; ir < rho_basis->nrxx; ir++)
{
nablan_2[ir] = pow(nablan[ir].x, 2) + pow(nablan[ir].y, 2) + pow(nablan[ir].z, 2);
}

// Laplacian of n
lapl_rho(ucell.tpiba2,ps_totn, lapn, rho_basis);
lapl_rho(ucell.tpiba2, ps_totn, lapn, rho_basis);

//-------------------------------------------------------------
// add -Lap(n)/|\nabla n| to vwork and copy \sqrt(|\nabla n|^2)
Expand Down Expand Up @@ -162,10 +164,13 @@ void surchem::createcavity(const UnitCell& ucell,
delete[] ggn;
}

ModuleBase::matrix surchem::cal_vcav(const UnitCell& ucell,
const ModulePW::PW_Basis* rho_basis,
std::complex<double>* ps_totn,
int nspin)
//The interface is changed to use an explicit output parameter to
//clarify lifetime management and avoid hidden allocations.
void surchem::cal_vcav(const UnitCell& ucell,
const ModulePW::PW_Basis* rho_basis,
std::complex<double>* ps_totn,
int nspin,
ModuleBase::matrix& v)
{
ModuleBase::TITLE("surchem", "cal_vcav");
ModuleBase::timer::tick("surchem", "cal_vcav");
Expand All @@ -175,12 +180,12 @@ ModuleBase::matrix surchem::cal_vcav(const UnitCell& ucell,

createcavity(ucell, rho_basis, ps_totn, tmp_Vcav);

ModuleBase::GlobalFunc::ZEROS(Vcav.c, nspin * rho_basis->nrxx);
if (nspin == 4)
{
for (int ir = 0; ir < rho_basis->nrxx; ir++)
{
Vcav(0, ir) += tmp_Vcav[ir];
Vcav(0, ir) = tmp_Vcav[ir];
v(0, ir) += Vcav(0, ir);
}
}
else
Expand All @@ -189,12 +194,13 @@ ModuleBase::matrix surchem::cal_vcav(const UnitCell& ucell,
{
for (int ir = 0; ir < rho_basis->nrxx; ir++)
{
Vcav(is, ir) += tmp_Vcav[ir];
Vcav(is, ir) = tmp_Vcav[ir];
v(is, ir) += Vcav(is, ir);
}
}
}

delete[] tmp_Vcav;
ModuleBase::timer::tick("surchem", "cal_vcav");
return Vcav;
return;
}
19 changes: 12 additions & 7 deletions source/source_hamilt/module_surchem/cal_vel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ void eps_pot(const double* PS_TOTN_real,
delete[] phisq;
}

ModuleBase::matrix surchem::cal_vel(const UnitCell& cell,
const ModulePW::PW_Basis* rho_basis,
std::complex<double>* TOTN,
std::complex<double>* PS_TOTN,
int nspin)
//The interface is changed to use an explicit output parameter to
//clarify lifetime management and avoid hidden allocations.
void surchem::cal_vel(const UnitCell& cell,
const ModulePW::PW_Basis* rho_basis,
std::complex<double>* TOTN,
std::complex<double>* PS_TOTN,
int nspin,
ModuleBase::matrix& v)
{
ModuleBase::TITLE("surchem", "cal_vel");
ModuleBase::timer::tick("surchem", "cal_vel");
Expand Down Expand Up @@ -134,6 +137,7 @@ ModuleBase::matrix surchem::cal_vel(const UnitCell& cell,
for (int ir = 0; ir < rho_basis->nrxx; ir++)
{
Vel(0, ir) += tmp_Vel[ir];
v(0, ir) += Vel(0, ir);
}
}
else
Expand All @@ -143,6 +147,7 @@ ModuleBase::matrix surchem::cal_vel(const UnitCell& cell,
for (int ir = 0; ir < rho_basis->nrxx; ir++)
{
Vel(is, ir) += tmp_Vel[ir];
v(is, ir) += Vel(is, ir);
}
}
}
Expand All @@ -158,5 +163,5 @@ ModuleBase::matrix surchem::cal_vel(const UnitCell& cell,
delete[] phi_tilda_R0;

ModuleBase::timer::tick("surchem", "cal_vel");
return Vel;
}
return;
}
Loading
Loading