Skip to content

Commit 8cb9681

Browse files
author
dyzheng
committed
Fix: refactor for short esolver_ks_lcao.cpp
1 parent f9b1a7e commit 8cb9681

File tree

3 files changed

+124
-25
lines changed

3 files changed

+124
-25
lines changed

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,10 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(UnitCell& ucell, const int istep)
181181
}
182182
if(PARAM.inp.init_chg == "hr")
183183
{
184-
//! 13.1.2) init HR from file
185-
if (PARAM.inp.nspin == 2)
186-
{
187-
// nspin=2: load spin-up into first half of hRS2, spin-down into second half
188-
const std::string hrfile_up = PARAM.globalv.global_readin_dir + "/hrs1_nao.csr";
189-
LCAO_domain::init_hr_from_file<TR>(hrfile_up, hamilt_lcao->getHR(), ucell, &(this->pv));
190-
191-
// switch hR data pointer to spin-down half, then read hrs2
192-
auto& hRS2 = hamilt_lcao->getHRS2();
193-
hamilt_lcao->getHR()->allocate(hRS2.data() + hRS2.size() / 2, 0);
194-
const std::string hrfile_down = PARAM.globalv.global_readin_dir + "/hrs2_nao.csr";
195-
LCAO_domain::init_hr_from_file<TR>(hrfile_down, hamilt_lcao->getHR(), ucell, &(this->pv));
196-
197-
// restore hR to spin-up half (refresh(false) will also do this, but be explicit)
198-
hamilt_lcao->getHR()->allocate(hRS2.data(), 0);
199-
}
200-
else
201-
{
202-
const std::string hrfile = PARAM.globalv.global_readin_dir + "/hrs1_nao.csr";
203-
LCAO_domain::init_hr_from_file<TR>(hrfile, hamilt_lcao->getHR(), ucell, &(this->pv));
204-
}
205-
this->p_hamilt->refresh(false);
206-
hsolver::HSolverLCAO<TK> hsolver_lcao_obj(&(this->pv), PARAM.inp.ks_solver);
207-
hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.dm,
208-
this->chr, PARAM.inp.nspin, 0);
184+
//! 13.1.2) init charge density from Hamiltonian matrix file
185+
LCAO_domain::init_chg_hr<TK, TR>(PARAM.globalv.global_readin_dir, PARAM.inp.nspin,
186+
this->p_hamilt, ucell, &(this->pv), this->psi[0], this->pelec, *this->dmat.dm,
187+
this->chr, PARAM.inp.ks_solver);
209188
}
210189
}
211190
else //if not, use the DMR calculated from last step

source/source_lcao/LCAO_set.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "source_estate/elecstate_tools.h" // use fixed_weights
66
#include "source_lcao/module_hcontainer/read_hcontainer.h"
77
#include "source_lcao/rho_tau_lcao.h" // use dm2rho
8+
#include "source_lcao/hamilt_lcao.h" // use HamiltLCAO for init_chg_hr
9+
#include "source_hsolver/hsolver_lcao.h" // use HSolverLCAO for init_chg_hr
810

911
template <typename TK>
1012
void LCAO_domain::set_psi_occ_dm_chg(
@@ -157,6 +159,57 @@ void LCAO_domain::init_hr_from_file(
157159
return;
158160
}
159161

162+
template <typename TK, typename TR>
163+
void LCAO_domain::init_chg_hr(
164+
const std::string& readin_dir,
165+
const int nspin,
166+
hamilt::Hamilt<TK>* p_hamilt,
167+
const UnitCell& ucell,
168+
const Parallel_Orbitals* pv,
169+
psi::Psi<TK>& psi,
170+
elecstate::ElecState* pelec,
171+
elecstate::DensityMatrix<TK, double>& dm,
172+
Charge& chr,
173+
const std::string& ks_solver)
174+
{
175+
ModuleBase::TITLE("LCAO_domain", "init_chg_hr");
176+
177+
auto* hamilt_lcao = dynamic_cast<hamilt::HamiltLCAO<TK, TR>*>(p_hamilt);
178+
if (!hamilt_lcao)
179+
{
180+
ModuleBase::WARNING_QUIT("LCAO_domain::init_chg_hr", "p_hamilt is not HamiltLCAO");
181+
}
182+
183+
// Step 1: Read HR from file(s)
184+
if (nspin == 2)
185+
{
186+
// nspin=2: load spin-up into first half of hRS2, spin-down into second half
187+
const std::string hrfile_up = readin_dir + "/hrs1_nao.csr";
188+
LCAO_domain::init_hr_from_file<TR>(hrfile_up, hamilt_lcao->getHR(), ucell, pv);
189+
190+
// switch hR data pointer to spin-down half, then read hrs2
191+
auto& hRS2 = hamilt_lcao->getHRS2();
192+
hamilt_lcao->getHR()->allocate(hRS2.data() + hRS2.size() / 2, 0);
193+
const std::string hrfile_down = readin_dir + "/hrs2_nao.csr";
194+
LCAO_domain::init_hr_from_file<TR>(hrfile_down, hamilt_lcao->getHR(), ucell, pv);
195+
196+
// restore hR to spin-up half
197+
hamilt_lcao->getHR()->allocate(hRS2.data(), 0);
198+
}
199+
else
200+
{
201+
const std::string hrfile = readin_dir + "/hrs1_nao.csr";
202+
LCAO_domain::init_hr_from_file<TR>(hrfile, hamilt_lcao->getHR(), ucell, pv);
203+
}
204+
205+
// Step 2: Mark HR as loaded from file (skip operator recalculation)
206+
p_hamilt->refresh(false);
207+
208+
// Step 3: Diagonalize to get wavefunctions and charge density
209+
hsolver::HSolverLCAO<TK> hsolver_lcao_obj(pv, ks_solver);
210+
hsolver_lcao_obj.solve(p_hamilt, psi, pelec, dm, chr, nspin, 0);
211+
}
212+
160213

161214

162215
template void LCAO_domain::set_psi_occ_dm_chg<double>(
@@ -247,3 +300,37 @@ template void LCAO_domain::init_hr_from_file<std::complex<double>>(
247300
hamilt::HContainer<std::complex<double>>* hmat,
248301
const UnitCell& ucell,
249302
const Parallel_Orbitals* pv);
303+
304+
template void LCAO_domain::init_chg_hr<double, double>(
305+
const std::string& readin_dir,
306+
const int nspin,
307+
hamilt::Hamilt<double>* p_hamilt,
308+
const UnitCell& ucell,
309+
const Parallel_Orbitals* pv,
310+
psi::Psi<double>& psi,
311+
elecstate::ElecState* pelec,
312+
elecstate::DensityMatrix<double, double>& dm,
313+
Charge& chr,
314+
const std::string& ks_solver);
315+
template void LCAO_domain::init_chg_hr<std::complex<double>, double>(
316+
const std::string& readin_dir,
317+
const int nspin,
318+
hamilt::Hamilt<std::complex<double>>* p_hamilt,
319+
const UnitCell& ucell,
320+
const Parallel_Orbitals* pv,
321+
psi::Psi<std::complex<double>>& psi,
322+
elecstate::ElecState* pelec,
323+
elecstate::DensityMatrix<std::complex<double>, double>& dm,
324+
Charge& chr,
325+
const std::string& ks_solver);
326+
template void LCAO_domain::init_chg_hr<std::complex<double>, std::complex<double>>(
327+
const std::string& readin_dir,
328+
const int nspin,
329+
hamilt::Hamilt<std::complex<double>>* p_hamilt,
330+
const UnitCell& ucell,
331+
const Parallel_Orbitals* pv,
332+
psi::Psi<std::complex<double>>& psi,
333+
elecstate::ElecState* pelec,
334+
elecstate::DensityMatrix<std::complex<double>, double>& dm,
335+
Charge& chr,
336+
const std::string& ks_solver);

source/source_lcao/LCAO_set.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "source_cell/klist.h"
66
#include "source_psi/psi.h"
77
#include "source_estate/elecstate.h"
8+
#include "source_estate/module_dm/density_matrix.h"
9+
#include "source_hamilt/hamilt.h"
810
#include "source_lcao/setup_dm.h"
911
#include "source_pw/module_pwdft/structure_factor.h"
1012
#include "source_basis/module_pw/pw_basis.h"
@@ -94,6 +96,37 @@ void init_hr_from_file(
9496
hamilt::HContainer<TK>* hmat,
9597
const UnitCell& ucell,
9698
const Parallel_Orbitals* pv);
99+
100+
/**
101+
* @brief initialize charge density from Hamiltonian matrix file (init_chg=hr)
102+
* Reads HR from file(s), diagonalizes to get wavefunctions, then computes charge density.
103+
* For nspin=2, reads both hrs1_nao.csr (spin-up) and hrs2_nao.csr (spin-down)
104+
* into the two halves of HamiltLCAO::hRS2.
105+
* @tparam TK k-space type (double or complex<double>)
106+
* @tparam TR real-space type (double)
107+
* @param readin_dir directory containing hrs*_nao.csr files
108+
* @param nspin number of spin components
109+
* @param p_hamilt pointer to Hamilt base class (will be dynamic_cast to HamiltLCAO)
110+
* @param ucell unit cell
111+
* @param pv parallel orbitals
112+
* @param psi wave function object
113+
* @param pelec electronic state
114+
* @param dm density matrix
115+
* @param chr charge density
116+
* @param ks_solver solver method name
117+
*/
118+
template <typename TK, typename TR>
119+
void init_chg_hr(
120+
const std::string& readin_dir,
121+
const int nspin,
122+
hamilt::Hamilt<TK>* p_hamilt,
123+
const UnitCell& ucell,
124+
const Parallel_Orbitals* pv,
125+
psi::Psi<TK>& psi,
126+
elecstate::ElecState* pelec,
127+
elecstate::DensityMatrix<TK, double>& dm,
128+
Charge& chr,
129+
const std::string& ks_solver);
97130
} // end namespace
98131

99132
#endif

0 commit comments

Comments
 (0)