Skip to content

Commit

Permalink
Fix: multiple memory leak and array access out of bound (#3997)
Browse files Browse the repository at this point in the history
* fix gint memory leak

* fix tddft dealloc mismatch

* fix dr vector overflow

* fix mpi broadcast type mismatch

* fix bool mpi type
  • Loading branch information
caic99 committed Apr 17, 2024
1 parent ef53ec1 commit 64f4ccd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 93 deletions.
4 changes: 2 additions & 2 deletions source/module_basis/module_nao/projgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void projgen(const int l,
assert(rcut < r[nr - 1]);
assert(std::is_sorted(r, r + nr));

std::vector<double> dr(nr - 1);
std::vector<double> dr(nr);
std::adjacent_difference(r, r + nr, dr.begin());

// lower_bound returns the first element that is equal or larger than rcut
Expand Down Expand Up @@ -82,7 +82,7 @@ void smoothgen(const int nr, const double* r, const double* chi, const double rc
assert(rcut < r[nr - 1]);
assert(std::is_sorted(r, r + nr));

std::vector<double> dr(nr - 1);
std::vector<double> dr(nr);
std::adjacent_difference(r, r + nr, dr.begin());

// lower_bound returns the first element that is equal or larger than rcut
Expand Down
82 changes: 41 additions & 41 deletions source/module_esolver/esolver_ks_lcao_tddft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ ESolver_KS_LCAO_TDDFT::~ESolver_KS_LCAO_TDDFT()
{
for (int ik = 0; ik < kv.nks; ++ik)
{
delete Hk_laststep[ik];
delete[] Hk_laststep[ik];
}
delete Hk_laststep;
delete[] Hk_laststep;
}
if (Sk_laststep != nullptr)
{
for (int ik = 0; ik < kv.nks; ++ik)
{
delete Sk_laststep[ik];
delete[] Sk_laststep[ik];
}
delete Sk_laststep;
delete[] Sk_laststep;
}
}

Expand Down Expand Up @@ -131,8 +131,8 @@ void ESolver_KS_LCAO_TDDFT::init(Input& inp, UnitCell& ucell)
}

void ESolver_KS_LCAO_TDDFT::hamilt2density(
int istep,
int iter,
int istep,
int iter,
double ethr)
{

Expand Down Expand Up @@ -185,8 +185,8 @@ void ESolver_KS_LCAO_TDDFT::hamilt2density(
if (this->psi != nullptr)
{
this->phsol->solve(
this->p_hamilt,
this->psi[0],
this->p_hamilt,
this->psi[0],
this->pelec_td,
GlobalV::KS_SOLVER);
}
Expand All @@ -211,11 +211,11 @@ void ESolver_KS_LCAO_TDDFT::hamilt2density(
for (int ib = 0; ib < GlobalV::NBANDS; ib++)
{
std::setprecision(6);
GlobalV::ofs_running << ik + 1
<< " "
<< ib + 1
<< " "
<< this->pelec_td->wg(ik, ib)
GlobalV::ofs_running << ik + 1
<< " "
<< ib + 1
<< " "
<< this->pelec_td->wg(ik, ib)
<< std::endl;
}
}
Expand All @@ -239,10 +239,10 @@ void ESolver_KS_LCAO_TDDFT::hamilt2density(
Symmetry_rho srho;
for (int is = 0; is < GlobalV::NSPIN; is++)
{
srho.begin(is,
*(pelec->charge),
pw_rho,
GlobalC::Pgrid,
srho.begin(is,
*(pelec->charge),
pw_rho,
GlobalC::Pgrid,
GlobalC::ucell.symm);
}
}
Expand Down Expand Up @@ -280,28 +280,28 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
this->p_hamilt->matrix(h_mat, s_mat);
if (hsolver::HSolverLCAO<std::complex<double>>::out_mat_hs[0])
{
ModuleIO::save_mat(istep,
h_mat.p,
GlobalV::NLOCAL,
ModuleIO::save_mat(istep,
h_mat.p,
GlobalV::NLOCAL,
bit,
hsolver::HSolverLCAO<std::complex<double>>::out_mat_hs[1],
1,
GlobalV::out_app_flag,
"H",
"data-" + std::to_string(ik),
*this->LOWF.ParaV,
1,
GlobalV::out_app_flag,
"H",
"data-" + std::to_string(ik),
*this->LOWF.ParaV,
GlobalV::DRANK);

ModuleIO::save_mat(istep,
h_mat.p,
GlobalV::NLOCAL,
bit,
ModuleIO::save_mat(istep,
h_mat.p,
GlobalV::NLOCAL,
bit,
hsolver::HSolverLCAO<std::complex<double>>::out_mat_hs[1],
1,
GlobalV::out_app_flag,
"S",
"data-" + std::to_string(ik),
*this->LOWF.ParaV,
1,
GlobalV::out_app_flag,
"S",
"data-" + std::to_string(ik),
*this->LOWF.ParaV,
GlobalV::DRANK);
}
}
Expand All @@ -312,7 +312,7 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
{
if (elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_lcao)
{
elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_flag
elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_flag
= elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_lcao;
}
for (int ik = 0; ik < kv.nks; ik++)
Expand Down Expand Up @@ -400,7 +400,7 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
}

// calculate energy density matrix for tddft
if (istep >= (wf.init_wfc == "file" ? 0 : 2)
if (istep >= (wf.init_wfc == "file" ? 0 : 2)
&& module_tddft::Evolve_elec::td_edm == 0)
{
this->cal_edm_tddft();
Expand Down Expand Up @@ -447,7 +447,7 @@ void ESolver_KS_LCAO_TDDFT::after_scf(const int istep)
}
if(module_tddft::Evolve_elec::out_current == 1)
{
elecstate::DensityMatrix<std::complex<double>, double>* tmp_DM =
elecstate::DensityMatrix<std::complex<double>, double>* tmp_DM =
dynamic_cast<elecstate::ElecStateLCAO<std::complex<double>>*>(this->pelec)->get_DM();
ModuleIO::write_current(istep,
this->psi,
Expand All @@ -466,20 +466,20 @@ void ESolver_KS_LCAO_TDDFT::after_scf(const int istep)
void ESolver_KS_LCAO_TDDFT::cal_edm_tddft(void)
{
// mohan add 2024-03-27
const int nlocal = GlobalV::NLOCAL;
const int nlocal = GlobalV::NLOCAL;
assert(nlocal>=0);

//this->LOC.edm_k_tddft.resize(kv.nks);
dynamic_cast<elecstate::ElecStateLCAO<std::complex<double>>*>(this->pelec)->get_DM()->EDMK.resize(kv.nks);
for (int ik = 0; ik < kv.nks; ++ik)
{
std::complex<double>* tmp_dmk =
std::complex<double>* tmp_dmk =
dynamic_cast<elecstate::ElecStateLCAO<std::complex<double>>*>(this->pelec)->get_DM()->get_DMK_pointer(ik);

ModuleBase::ComplexMatrix& tmp_edmk =
ModuleBase::ComplexMatrix& tmp_edmk =
dynamic_cast<elecstate::ElecStateLCAO<std::complex<double>>*>(this->pelec)->get_DM()->EDMK[ik];

const Parallel_Orbitals* tmp_pv =
const Parallel_Orbitals* tmp_pv =
dynamic_cast<elecstate::ElecStateLCAO<std::complex<double>>*>(this->pelec)->get_DM()->get_paraV_pointer();

#ifdef __MPI
Expand Down

0 comments on commit 64f4ccd

Please sign in to comment.