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
12 changes: 7 additions & 5 deletions source/source_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ void ESolver_KS::hamilt2rho(
// example wavefunctions uses 20 processors while density uses 10.
if (PARAM.globalv.ks_run)
{
drho = module_charge::cal_drho(
&this->chr, this->inp_->nelec, *this->pw_rho, p_chgmix->get_mixing_config(), ucell.omega, ucell.tpiba);
const MixingConfig &mix_cfg = p_chgmix->get_mixing_config();
// Taoni fix 20261204: Real-space residuals use the density grid; reciprocal residuals use the smooth basis.
const ModulePW::PW_Basis &residual_pw = mix_cfg.scf_thr_type == 2 ? *this->chr.rhopw : *this->pw_rho;
drho = module_charge::cal_drho(&this->chr, this->inp_->nelec, residual_pw, mix_cfg, ucell.omega, ucell.tpiba);
hsolver_error = 0.0;
if (iter == 1 && this->inp_->calculation != "nscf")
{
Expand All @@ -163,8 +165,7 @@ void ESolver_KS::hamilt2rho(
this->hamilt2rho_single(ucell, istep, iter, diag_ethr);

drho = module_charge::cal_drho(
&this->chr, this->inp_->nelec, *this->pw_rho, p_chgmix->get_mixing_config(), ucell.omega,
ucell.tpiba);
&this->chr, this->inp_->nelec, residual_pw, mix_cfg, ucell.omega, ucell.tpiba);

hsolver_error = hsolver::cal_hsolve_error(this->inp_->basis_type, policy, diag_ethr, this->inp_->nelec);
}
Expand Down Expand Up @@ -408,8 +409,9 @@ void ESolver_KS::iter_finish(
double dkin = 0.0; // for meta-GGA
if (XC_Functional::get_ked_flag())
{
// Taoni fix 20261204: Kinetic-energy-density residuals always use the grid storing kin_r and kin_r_save.
dkin = module_charge::cal_dkin(
&this->chr, this->inp_->nelec, *this->pw_rho, p_chgmix->get_mixing_config(), ucell.omega);
&this->chr, this->inp_->nelec, *this->chr.rhopw, p_chgmix->get_mixing_config(), ucell.omega);
}

// Iter finish
Expand Down
10 changes: 8 additions & 2 deletions source/source_estate/elecstate_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void ElecStatePW<T, Device>::init_rho_data()
this->kin_r = reinterpret_cast<Real**>(this->charge->kin_r);
}
}
// FFT work buffers follow the wavefunction grid, including when the density grid is denser.
resmem_complex_op()(this->wfcr, this->basis->nmaxgr, "ElecSPW::wfcr");
resmem_complex_op()(this->wfcr_another_spin, this->basis->nrxx, "ElecSPW::wfcr_a");
this->init_rho = true;
Expand Down Expand Up @@ -174,6 +175,10 @@ void ElecStatePW<T, Device>::psiToRho(const psi::Psi<T, Device>& psi)
}
}

if (this->charge->meta_gga)
{
this->tau_to_dense();
}
this->add_usrho(psi);
this->parallelK();
ModuleBase::timer::end("ElecStatePW", "psiToRho");
Expand Down Expand Up @@ -275,7 +280,7 @@ void ElecStatePW<T, Device>::rhoBandK(const psi::Psi<T, Device>& psi)
{
for (int j = 0; j < 3; j++)
{
setmem_complex_op()(this->wfcr, 0, this->charge->nrxx);
setmem_complex_op()(this->wfcr, 0, this->basis->nmaxgr);

meta_op()(this->ctx,
ik,
Expand All @@ -290,7 +295,8 @@ void ElecStatePW<T, Device>::rhoBandK(const psi::Psi<T, Device>& psi)

this->basis->recip_to_real(this->ctx, this->wfcr, this->wfcr, ik);

elecstate_pw_op()(this->ctx, current_spin, this->charge->nrxx, this->charge->nrxx, w1, this->kin_r, this->wfcr);
// Taoni fix 20261204: Derivatives use the smooth grid; kin_r retains the dense-grid spin stride.
elecstate_pw_op()(this->ctx, current_spin, this->basis->nrxx, this->charge->nrxx, w1, this->kin_r, this->wfcr);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions source/source_estate/elecstate_pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class ElecStatePW : public ElecState
//! calcualte rho for each k
void rhoBandK(const psi::Psi<T, Device>& psi);

//! Interpolate kinetic-energy density from the smooth grid to the density grid.
void tau_to_dense();

//! add to the charge density in reciprocal space the part which is due to the US augmentation.
void add_usrho(const psi::Psi<T, Device>& psi);

Expand Down
24 changes: 22 additions & 2 deletions source/source_estate/elecstate_pw_cal_tau.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void ElecStatePW<T, Device>::cal_tau(const psi::Psi<T, Device>& psi)
// kinetic energy density
for (int j = 0; j < 3; j++)
{
setmem_complex_op()(this->wfcr, 0, this->charge->nrxx);
setmem_complex_op()(this->wfcr, 0, this->basis->nmaxgr);

meta_op()(this->ctx,
ik,
Expand All @@ -47,7 +47,8 @@ void ElecStatePW<T, Device>::cal_tau(const psi::Psi<T, Device>& psi)

this->basis->recip_to_real(this->ctx, this->wfcr, this->wfcr, ik);

elecstate_pw_op()(this->ctx, current_spin, this->charge->nrxx, this->charge->nrxx, w1, this->kin_r, this->wfcr);
// Derivatives use the smooth grid; kin_r retains the dense-grid spin stride.
elecstate_pw_op()(this->ctx, current_spin, this->basis->nrxx, this->charge->nrxx, w1, this->kin_r, this->wfcr);
}
}
}
Expand All @@ -56,6 +57,7 @@ void ElecStatePW<T, Device>::cal_tau(const psi::Psi<T, Device>& psi)
castmem_var_d2h_op()(this->charge->kin_r[ii], this->kin_r[ii], this->charge->nrxx);
}
}
this->tau_to_dense();
#ifdef __MPI
module_charge::kin_r_mpi(*this->charge,
GlobalV::KPAR,
Expand All @@ -66,6 +68,24 @@ void ElecStatePW<T, Device>::cal_tau(const psi::Psi<T, Device>& psi)
ModuleBase::TITLE("ElecStatePW", "cal_tau");
}

// Taoni add 20261204: Fix meta-GGA double grid
template <typename T, typename Device>
void ElecStatePW<T, Device>::tau_to_dense()
{
if (this->rhopw_smooth == this->charge->rhopw)
{
return;
}

// Dense reciprocal coefficients start with the smooth basis in the same local order.
std::vector<std::complex<double>> tau_g(this->charge->rhopw->npw, std::complex<double>(0.0, 0.0));
for (int is = 0; is < this->charge->nspin; ++is)
{
this->rhopw_smooth->real2recip(this->charge->kin_r[is], tau_g.data());
this->charge->rhopw->recip2real(tau_g.data(), this->charge->kin_r[is]);
}
}

template class ElecStatePW<std::complex<float>, base_device::DEVICE_CPU>;
template class ElecStatePW<std::complex<double>, base_device::DEVICE_CPU>;
#if ((defined __CUDA) || (defined __ROCM))
Expand Down
4 changes: 2 additions & 2 deletions source/source_estate/module_charge/chg_drho.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace module_charge
*
* @param chr charge object supplying rho/rho_save (and rhog buffers for the reciprocal case)
* @param nelec number of electrons, used to normalize the real-space residual
* @param rhopw plane-wave basis supplying the real/reciprocal grid sizes
* @param rhopw smooth basis for the reciprocal metric, or chr->rhopw for the real-space metric
* @param cfg mixing config (nspin, scf_thr_type and magnetization flags select the loops)
* @param omega cell volume, used to normalize the real-space residual
* @param tpiba 2*pi/lattice constant, used by the reciprocal metric
Expand All @@ -42,7 +42,7 @@ double cal_drho(Charge* chr,
*
* @param chr charge object supplying kin_r/kin_r_save
* @param nelec number of electrons, used to normalize the residual
* @param rhopw plane-wave basis supplying the real-space grid size
* @param rhopw chr->rhopw, supplying the kinetic-energy-density grid sizes
* @param cfg mixing config (nspin and magnetization flags select the loops)
* @param omega cell volume, used to normalize the residual
* @return pooled residual value
Expand Down
11 changes: 8 additions & 3 deletions source/source_estate/module_charge/chg_mix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ void Charge_Mixing::init_mixing()
ModuleBase::WARNING_QUIT("Charge_Mixing",
"set_mixing must be called before init_mixing");
}
if (this->cfg_.scf_thr_type != 1 && this->rhodpw == nullptr)
{
ModuleBase::WARNING_QUIT("Charge_Mixing", "dense grid must be set before real-space mixing");
}

// (re)construct mixing object
if (this->cfg_.mixing_mode == "broyden")
Expand Down Expand Up @@ -160,13 +164,14 @@ void Charge_Mixing::init_mixing()
}
else
{
// Real-space mixing histories follow the dense-grid layout of Charge.
if (this->cfg_.nspin == 4 && this->cfg_.mixing_angle > 0 )
{
this->mixing->init_mixing_data(this->rho_mdata, this->rhopw->nrxx * 2, sizeof(double));
this->mixing->init_mixing_data(this->rho_mdata, this->rhodpw->nrxx * 2, sizeof(double));
}
else
{
this->mixing->init_mixing_data(this->rho_mdata, this->rhopw->nrxx * this->cfg_.nspin, sizeof(double));
this->mixing->init_mixing_data(this->rho_mdata, this->rhodpw->nrxx * this->cfg_.nspin, sizeof(double));
}
}

Expand All @@ -181,7 +186,7 @@ void Charge_Mixing::init_mixing()
}
else
{
this->mixing->init_mixing_data(this->tau_mdata, this->rhopw->nrxx * this->cfg_.nspin, sizeof(double));
this->mixing->init_mixing_data(this->tau_mdata, this->rhodpw->nrxx * this->cfg_.nspin, sizeof(double));
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/source_estate/module_charge/chg_mix.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class Charge_Mixing
std::vector<double> _drho_history; ///< Residual history for oscillation detection, sized to the active SCF limit.

ModulePW::PW_Basis* rhopw = nullptr; ///< smooth grid
ModulePW::PW_Basis* rhodpw = nullptr; ///< dense grid, same as rhopw for ncpp.
ModulePW::PW_Basis* rhodpw = nullptr; ///< dense grid, same as rhopw when double grids are disabled.

/// Runtime override set by close_kerker_gg0(): short-circuits the
/// Kerker screening lambdas in mix_rho_recip/mix_rho_real so the
Expand Down
9 changes: 4 additions & 5 deletions source/source_estate/module_charge/chg_mix_rho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ void Charge_Mixing::mix_rho_real(Charge* chr)
ModuleBase::timer::start("Charge_Mixing", "mix_rho_real");

const int nspin = cfg_.nspin;
const int nrxx = this->rhodpw->nrxx;
assert(nspin==1 || nspin==2 || nspin==4);

double* rhor_in=nullptr;
Expand All @@ -267,11 +268,12 @@ void Charge_Mixing::mix_rho_real(Charge* chr)
{
return;
}
module_charge::kerker_screen_real(this->cfg_, this->rhopw, *this->tpiba, p);
// FFT screening must use the same dense grid as the real-space residual buffer.
module_charge::kerker_screen_real(this->cfg_, this->rhodpw, *this->tpiba, p);
};
std::function<double(double*, double*)> inner_product = [this](double* rho1, double* rho2)
{
return module_charge::inner_product_real(rho1, rho2, *this->rhopw, this->cfg_);
return module_charge::inner_product_real(rho1, rho2, *this->rhodpw, this->cfg_);
};

if (nspin == 1)
Expand All @@ -285,7 +287,6 @@ void Charge_Mixing::mix_rho_real(Charge* chr)
else if (nspin == 2)
{
// magnetic density
const int nrxx = this->rhopw->nrxx;
std::vector<double> rho_mag(nrxx * nspin);
std::vector<double> rho_mag_save(nrxx * nspin);
module_charge::detail::pack_rho_mag(rho_mag.data(), chr->rho[0], chr->rho[1], nrxx);
Expand All @@ -305,7 +306,6 @@ void Charge_Mixing::mix_rho_real(Charge* chr)
{
rhor_in = chr->rho_save[0];
rhor_out = chr->rho[0];
const int nrxx = this->rhopw->nrxx;
std::function<void(double*, const double*, const double*)> twobeta_mix
= module_charge::detail::make_twobeta_mix<double>(4 * nrxx, nrxx, this->cfg_.mixing_beta, this->cfg_.mixing_beta_mag);
this->mixing->push_data(this->rho_mdata, rhor_in, rhor_out, screen, twobeta_mix, true);
Expand All @@ -315,7 +315,6 @@ void Charge_Mixing::mix_rho_real(Charge* chr)
else if (nspin == 4 && cfg_.mixing_angle > 0)
{
// real-space version of the {rho, |m|} broyden mixing
const int nrxx = this->rhopw->nrxx;
std::vector<double> rho_magabs(nrxx * 2);
std::vector<double> rho_magabs_save(nrxx * 2);
for (int ir = 0; ir < nrxx; ir++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ if(ENABLE_MPI)
TARGET MODULE_CHARGE_mix_rho
LIBS parameter base psi device planewave_serial cell_info
SOURCES test_chg_mix_rho.cpp
../../../source_cell/magnetism.cpp
../chg_mix.cpp
../chg_mix_rho.cpp
../chg_dmr.cpp
Expand Down
Loading
Loading