Skip to content

Commit 2d3bf57

Browse files
Fix: potential segmentation fault in DFTU force&stress (#5439)
* Fix: potential segmentation fault in DFTU force&stress * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7da86cd commit 2d3bf57

File tree

1 file changed

+21
-15
lines changed
  • source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao

1 file changed

+21
-15
lines changed

source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/dftu_lcao.cpp

100755100644
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::initialize_HR(Grid_Driver* Grid
4747
ModuleBase::TITLE("DFTU", "initialize_HR");
4848
ModuleBase::timer::tick("DFTU", "initialize_HR");
4949

50-
auto* paraV = this->hR->get_paraV();// get parallel orbitals from HR
51-
// TODO: if paraV is nullptr, AtomPair can not use paraV for constructor, I will repair it in the future.
52-
5350
this->adjs_all.clear();
5451
this->adjs_all.reserve(this->ucell->nat);
5552
for (int iat0 = 0; iat0 < ucell->nat; iat0++)
@@ -58,8 +55,9 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::initialize_HR(Grid_Driver* Grid
5855
int T0, I0;
5956
ucell->iat2iait(iat0, &I0, &T0);
6057
const int target_L = this->dftu->orbital_corr[T0];
61-
if (target_L == -1)
58+
if (target_L == -1) {
6259
continue;
60+
}
6361

6462
AdjacentAtomInfo adjs;
6563
GridD->Find_atom(*ucell, tau0, T0, I0, &adjs);
@@ -92,8 +90,9 @@ template <typename TK, typename TR>
9290
void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_nlm_all(const Parallel_Orbitals* paraV)
9391
{
9492
ModuleBase::TITLE("DFTU", "cal_nlm_all");
95-
if (this->precal_nlm_done)
93+
if (this->precal_nlm_done) {
9694
return;
95+
}
9796
ModuleBase::timer::tick("DFTU", "cal_nlm_all");
9897
nlm_tot.resize(this->ucell->nat);
9998
const int npol = this->ucell->get_npol();
@@ -104,8 +103,9 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_nlm_all(const Parallel_Orbi
104103
int T0, I0;
105104
ucell->iat2iait(iat0, &I0, &T0);
106105
const int target_L = this->dftu->orbital_corr[T0];
107-
if (target_L == -1)
106+
if (target_L == -1) {
108107
continue;
108+
}
109109
const int tlp1 = 2 * target_L + 1;
110110
AdjacentAtomInfo& adjs = this->adjs_all[atom_index++];
111111

@@ -144,7 +144,7 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_nlm_all(const Parallel_Orbi
144144
const int M1 = (m1 % 2 == 0) ? -m1 / 2 : (m1 + 1) / 2;
145145

146146
ModuleBase::Vector3<double> dtau = tau0 - tau1;
147-
intor_->snap(T1, L1, N1, M1, T0, dtau * this->ucell->lat0, 0 /*cal_deri*/, nlm);
147+
intor_->snap(T1, L1, N1, M1, T0, dtau * this->ucell->lat0, false /*cal_deri*/, nlm);
148148
// select the elements of nlm with target_L
149149
for (int iw = 0; iw < this->ucell->atoms[T0].nw; iw++)
150150
{
@@ -178,8 +178,9 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
178178
else
179179
{
180180
// will update this->dftu->locale and this->dftu->EU
181-
if (this->current_spin == 0)
181+
if (this->current_spin == 0) {
182182
this->dftu->EU = 0.0;
183+
}
183184
}
184185
ModuleBase::timer::tick("DFTU", "contributeHR");
185186

@@ -196,8 +197,9 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
196197
int T0, I0;
197198
ucell->iat2iait(iat0, &I0, &T0);
198199
const int target_L = this->dftu->orbital_corr[T0];
199-
if (target_L == -1)
200+
if (target_L == -1) {
200201
continue;
202+
}
201203
const int tlp1 = 2 * target_L + 1;
202204
AdjacentAtomInfo& adjs = this->adjs_all[atom_index++];
203205

@@ -241,8 +243,9 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
241243
// save occ to dftu
242244
for (int i = 0; i < occ.size(); i++)
243245
{
244-
if (this->nspin == 1)
246+
if (this->nspin == 1) {
245247
occ[i] *= 0.5;
248+
}
246249
this->dftu->locale[iat0][target_L][0][this->current_spin].c[i] = occ[i];
247250
}
248251
}
@@ -297,17 +300,20 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
297300
}
298301

299302
// energy correction for NSPIN=1
300-
if (this->nspin == 1)
303+
if (this->nspin == 1) {
301304
this->dftu->EU *= 2.0;
305+
}
302306
// for readin onsite_dm, set initialed_locale to false to avoid using readin locale in next iteration
303-
if (this->current_spin == this->nspin - 1 || this->nspin == 4)
307+
if (this->current_spin == this->nspin - 1 || this->nspin == 4) {
304308
this->dftu->initialed_locale = false;
309+
}
305310

306311
// update this->current_spin: only nspin=2 iterate change it between 0 and 1
307312
// the key point is only nspin=2 calculate spin-up and spin-down separately,
308313
// and won't calculate spin-up twice without spin-down
309-
if (this->nspin == 2)
314+
if (this->nspin == 2) {
310315
this->current_spin = 1 - this->current_spin;
316+
}
311317

312318
ModuleBase::timer::tick("DFTU", "contributeHR");
313319
}
@@ -488,7 +494,7 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_v_of_u(const std::vector<do
488494
{
489495
// calculate the local matrix
490496
int spin_fold = occ.size() / m_size / m_size;
491-
if (spin_fold < 4)
497+
if (spin_fold < 4) {
492498
for (int is = 0; is < spin_fold; ++is)
493499
{
494500
int start = is * m_size * m_size;
@@ -501,7 +507,7 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_v_of_u(const std::vector<do
501507
}
502508
}
503509
}
504-
else
510+
} else
505511
{
506512
for (int m1 = 0; m1 < m_size; m1++)
507513
{

0 commit comments

Comments
 (0)