Skip to content

Commit 31ea057

Browse files
author
dyzheng
committed
Fix(deltaspin): fix cal_escon segfault, cal_pre_HR memory leak, and current_spin propagation
Bug fixes: - cal_escon(): add lambda_/Mi_ empty check to prevent segfault when uninitialized (also preserves is_Mi_converged guard for behavioral compatibility) - cal_pre_HR(): delete old pre_hr allocations before reallocating to prevent memory leak when DeltaSpin is reinitialized - operator_lcao.cpp: propagate current_spin to next operator in chain so all operators share the same spin state set by HamiltLCAO::updateHk Test refs regenerated for 17_DS_DFTU enabled cases.
1 parent bb2cb4b commit 31ea057

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

source/source_lcao/module_deltaspin/spin_constrain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ template <typename TK>
4545
double SpinConstrain<TK>::cal_escon()
4646
{
4747
this->escon_ = 0.0;
48-
if (!this->is_Mi_converged)
48+
if (this->lambda_.empty() || this->Mi_.empty() || !this->is_Mi_converged)
4949
{
5050
return this->escon_;
5151
}

source/source_lcao/module_operator_lcao/dspin_lcao.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ void hamilt::DeltaSpin<hamilt::OperatorLCAO<TK, TR>>::cal_pre_HR()
224224
}
225225
this->paraV = this->hR->get_paraV();
226226
ModuleBase::timer::start("DeltaSpin", "cal_pre_HR");
227+
for (auto& hr : this->pre_hr)
228+
{
229+
delete hr;
230+
}
227231
this->pre_hr.clear();
228232
this->pre_hr.resize(this->ucell->nat, nullptr);
229233

source/source_lcao/module_operator_lcao/operator_lcao.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ void OperatorLCAO<TK, TR>::init(const int ik_in) {
7979
this->hR->set_zero();
8080
}
8181
}
82+
if (this->next_op != nullptr) {
83+
dynamic_cast<OperatorLCAO<TK, TR>*>(this->next_op)->current_spin = this->current_spin;
84+
}
8285
switch (this->cal_type) {
8386
case calculation_type::lcao_overlap: {
8487
// cal_type=lcao_overlap refer to overlap matrix operators, which are

0 commit comments

Comments
 (0)