From 5e75b689f7c687b46960a6c217ae16b47eb41551 Mon Sep 17 00:00:00 2001 From: Critsium Date: Fri, 24 Jul 2026 14:15:28 +0800 Subject: [PATCH] Refactor: remove source_lcao dependency from source_hsolver HSolverLCAO::solve computed the charge density directly via the source_lcao free function LCAO_domain::dm2rho, making source_hsolver depend on source_lcao. Delegate the charge-density calculation through a new thin ElecStateLCAO::dmToRho wrapper (source_estate, which already depends on source_lcao). This mirrors the existing pexsi branch (_pes->dm2rho) and the plane-wave path (ElecStatePW::psiToRho), and removes the only direct source_lcao include from source_hsolver. No functional change. Co-Authored-By: Claude Opus 4.8 --- source/source_estate/elecstate_lcao.cpp | 10 ++++++++++ source/source_estate/elecstate_lcao.h | 17 +++++++++++++++-- source/source_hsolver/hsolver_lcao.cpp | 6 +++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/source/source_estate/elecstate_lcao.cpp b/source/source_estate/elecstate_lcao.cpp index 2040ee769e1..0753e0338e1 100644 --- a/source/source_estate/elecstate_lcao.cpp +++ b/source/source_estate/elecstate_lcao.cpp @@ -7,6 +7,7 @@ #include "source_io/module_parameter/parameter.h" #include "source_lcao/module_gint/gint_interface.h" +#include "source_lcao/rho_tau_lcao.h" #include @@ -84,6 +85,15 @@ void ElecStateLCAO>::dm2rho(std::vector +void ElecStateLCAO::dmToRho(std::vector*>& dmr, + int nspin, + Charge* chr, + bool skip_charge) +{ + LCAO_domain::dm2rho(dmr, nspin, chr, skip_charge); +} + template class ElecStateLCAO; // Gamma_only case template class ElecStateLCAO>; // multi-k case diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index cef6653d600..fa28c5bb804 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -39,10 +39,23 @@ class ElecStateLCAO : public ElecState * @param pexsi_EDM: pointers of energy-weighed density matrix (EDMK) calculated by pexsi, needed by MD, will be * stored in DensityMatrix::pexsi_EDM */ - void dm2rho(std::vector pexsi_DM, - std::vector pexsi_EDM, + void dm2rho(std::vector pexsi_DM, + std::vector pexsi_EDM, DensityMatrix* dm); + /** + * @brief calculate electronic charge density from the density matrix (DMR) + * + * Thin wrapper over LCAO_domain::dm2rho so that HSolverLCAO delegates the + * charge-density calculation through the ElecState interface, mirroring the + * plane-wave path (ElecStatePW::psiToRho) and the pexsi branch above. This + * keeps the source_lcao dependency out of source_hsolver. + */ + void dmToRho(std::vector*>& dmr, + int nspin, + Charge* chr, + bool skip_charge = false); + }; template diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index a210b9fe81c..99a8a4d3a56 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -35,8 +35,6 @@ #include "source_hsolver/parallel_k2d.h" #include "source_io/module_parameter/parameter.h" -#include "source_lcao/rho_tau_lcao.h" // mohan add 20251024 - namespace hsolver { @@ -103,7 +101,9 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, if (!skip_charge) { // compute charge density from density matrix, mohan update 20251024 - LCAO_domain::dm2rho(dm.get_DMR_vector(), nspin, &chr); + // delegate to ElecStateLCAO to keep the source_lcao dependency out of + // source_hsolver (mirrors the pexsi branch below and the PW psiToRho path) + dynamic_cast*>(pes)->dmToRho(dm.get_DMR_vector(), nspin, &chr); } else {