Skip to content

Commit 63c58fc

Browse files
author
dyzheng
committed
Fix: init_chg dm with mismatched sparsity
1 parent 3e41893 commit 63c58fc

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(UnitCell& ucell, const Input_pa
7575

7676
LCAO_domain::set_psi_occ_dm_chg<TK>(this->kv, this->psi, this->pv, this->pelec,
7777
this->dmat, this->chr, inp);
78-
79-
if(inp.init_chg == "dm")
80-
{
81-
//! 4.1) init density matrix from file
82-
std::string dmfile = PARAM.globalv.global_readin_dir + "/dmrs1_nao.csr";
83-
LCAO_domain::init_dm_from_file<TK>(dmfile, this->dmat, ucell, &(this->pv));
84-
}
8578

8679
LCAO_domain::set_pot<TK>(ucell, this->kv, this->sf, *this->pw_rho, *this->pw_rhod,
8780
this->pelec, this->orb_, this->pv, this->locpp, this->dftu,
@@ -167,35 +160,42 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(UnitCell& ucell, const int istep)
167160
// 11) set xc type before the first cal of xc in pelec->init_scf, Peize Lin add 2016-12-03
168161
this->exx_nao.before_scf(ucell, this->kv, orb_, this->p_chgmix, istep, PARAM.inp);
169162

170-
// 12.1) if init_chg = "dm", then calculate rho from readin DMR before init_scf
171-
if(PARAM.inp.init_chg == "dm")
172-
{
173-
LCAO_domain::dm2rho(this->dmat.dm->get_DMR_vector(), PARAM.inp.nspin, this->pelec->charge, true);
174-
}
175-
// 12.2) init_scf, should be before_scf? mohan add 2025-03-10
176-
this->pelec->init_scf(ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
177-
178-
// 13) initalize DM(R), which has the same size with Hamiltonian(R)
163+
// 12) initalize DM(R), which has the same size with Hamiltonian(R)
179164
auto* hamilt_lcao = dynamic_cast<hamilt::HamiltLCAO<TK, TR>*>(this->p_hamilt);
165+
180166
if(!hamilt_lcao)
181167
{
182168
ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","p_hamilt does not exist");
183169
}
184-
if(PARAM.inp.init_chg != "dm") this->dmat.dm->init_DMR(*hamilt_lcao->getHR());
170+
this->dmat.dm->init_DMR(*hamilt_lcao->getHR());
171+
172+
// 13.1) calculate or readin the density matrix DMR
173+
if(istep == 0 && PARAM.inp.init_chg == "dm")//if the first scf step, readin DMR from file,
174+
{
175+
//! 13.1.1) init density matrix from file
176+
std::string dmfile = PARAM.globalv.global_readin_dir + "/dmrs1_nao.csr";
177+
LCAO_domain::init_dm_from_file<TK>(dmfile, this->dmat, ucell, &(this->pv));
178+
}
179+
else if(istep > 0) //if not, use the DMR calculated from last step
180+
{
181+
// 13.1.2) two cases are considered:
182+
// 1. DMK in DensityMatrix is not empty (istep > 0), then DMR is initialized by DMK
183+
// 2. DMK in DensityMatrix is empty (istep == 0), then DMR is initialized by zeros
184+
this->dmat.dm->cal_DMR();
185+
}
186+
// 13.2 if init_chg = "dm", then calculate rho from readin DMR before init_scf
187+
if(PARAM.inp.init_chg == "dm")
188+
{
189+
LCAO_domain::dm2rho(this->dmat.dm->get_DMR_vector(), PARAM.inp.nspin, this->pelec->charge, true);
190+
}
191+
// 13.2) init_scf, should be before_scf? mohan add 2025-03-10
192+
this->pelec->init_scf(ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
185193

186194
#ifdef __MLALGO
187195
// 14) initialize DM2(R) of DeePKS, the DM2(R) is different from DM(R)
188196
this->deepks.ld.init_DMR(ucell, orb_, this->pv, this->gd);
189197
#endif
190198

191-
// 15) two cases are considered:
192-
// 1. DMK in DensityMatrix is not empty (istep > 0), then DMR is initialized by DMK
193-
// 2. DMK in DensityMatrix is empty (istep == 0), then DMR is initialized by zeros
194-
if (istep > 0)
195-
{
196-
this->dmat.dm->cal_DMR();
197-
}
198-
199199
// 16) the electron charge density should be symmetrized,
200200
Symmetry_rho srho;
201201
for (int is = 0; is < PARAM.inp.nspin; is++)

source/source_lcao/LCAO_set.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,14 @@ void LCAO_domain::init_dm_from_file(
100100
const Parallel_Orbitals* pv)
101101
{
102102
ModuleBase::TITLE("LCAO_domain::init_dm_from_file", "init_dm_from_file");
103-
hamilt::HContainer<double>* dm_container = new hamilt::HContainer<double>(pv);
104-
dmat.dm->init_DMR(dm_container[0]);
103+
hamilt::HContainer<double>* dm_container = dmat.dm->get_DMR_vector()[0];
105104
hamilt::Read_HContainer<double> reader_dm(
106105
dmat.dm->get_DMR_vector()[0],
107106
dmfile,
108107
PARAM.globalv.nlocal,
109108
&ucell
110109
);
111110
reader_dm.read();
112-
delete dm_container;
113111
return;
114112
}
115113

0 commit comments

Comments
 (0)