Skip to content

Commit d9d49b9

Browse files
committed
Fix: deconstructor of Operator for PW base
1 parent b86feda commit d9d49b9

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

source/module_hamilt/ks_pw/ekinetic_pw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Ekinetic : public T
1616
const int gk2_dim_in
1717
);
1818

19+
virtual ~Ekinetic(){};
20+
1921
virtual void act
2022
(
2123
const psi::Psi<std::complex<double>> *psi_in,

source/module_hamilt/ks_pw/meta_pw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Meta : public T
1919
ModulePW::PW_Basis_K* wfcpw
2020
);
2121

22+
virtual ~Meta(){};
23+
2224
virtual void act
2325
(
2426
const psi::Psi<std::complex<double>> *psi_in,

source/module_hamilt/ks_pw/nonlocal_pw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Nonlocal : public T
2020
const UnitCell_pseudo* ucell_in
2121
);
2222

23+
virtual ~Nonlocal(){};
24+
2325
virtual void init(const int ik_in)override;
2426

2527
virtual void act

source/module_hamilt/ks_pw/operator_pw.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace hamilt
99
class OperatorPW : public Operator
1010
{
1111
public:
12+
virtual ~OperatorPW(){};
1213

1314
//in PW code, different operators donate hPsi independently
1415
//run this->act function for the first operator and run all act() for other nodes in chain table
@@ -29,11 +30,17 @@ class OperatorPW : public Operator
2930
node = (OperatorPW*)(node->next_op);
3031
}
3132

33+
//during recursive call of hPsi, delete the input psi
34+
if(this->recursive) delete std::get<0>(input);
35+
3236
ModuleBase::timer::tick("OperatorPW", "hPsi");
3337

3438
return hpsi_info(this->hpsi, psi::Range(1, 0, 0, n_npwx/std::get<0>(input)->npol));
3539
}
3640

41+
//main function which should be modified in Operator for PW base
42+
//do operation : |hpsi_choosed> = V|psi_choosed>
43+
//V is the target operator act on choosed psi, the consequence should be added to choosed hpsi
3744
virtual void act
3845
(
3946
const psi::Psi<std::complex<double>> *psi_in,

source/module_hamilt/ks_pw/veff_pw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Veff : public T
1818
ModulePW::PW_Basis_K* wfcpw_in
1919
);
2020

21+
virtual ~Veff(){};
22+
2123
virtual void act
2224
(
2325
const psi::Psi<std::complex<double>> *psi_in,

source/module_hamilt/operator.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@ class Operator
1212
{
1313
public:
1414
Operator(){};
15-
~Operator()
15+
virtual ~Operator()
1616
{
1717
if(this->hpsi != nullptr) delete this->hpsi;
18-
this->release();
1918
Operator* last = this->next_op;
2019
if(last != nullptr)
2120
{
22-
last->release();
2321
Operator* node_delete = last;
2422
last = last->next_op;
2523
node_delete->next_op = nullptr;
2624
delete node_delete;
2725
}
2826
}
29-
virtual void release(){return;}
3027

3128
typedef std::tuple<const psi::Psi<std::complex<double>>*, const psi::Range> hpsi_info;
3229
virtual hpsi_info hPsi(const hpsi_info& input)const {return hpsi_info(nullptr, 0);}
@@ -63,6 +60,8 @@ class Operator
6360
protected:
6461
int ik = 0;
6562

63+
mutable bool recursive = false;
64+
6665
//calculation type, only different type can be in main chain table
6766
int cal_type = 0;
6867
Operator* next_op = nullptr;
@@ -73,10 +72,21 @@ class Operator
7372
std::complex<double>* get_hpsi(const hpsi_info& info)const
7473
{
7574
const int nbands_range = (std::get<1>(info).range_2 - std::get<1>(info).range_1 + 1);
76-
if(this->hpsi != nullptr)
75+
//recursive call of hPsi, hpsi inputs as new psi,
76+
//create a new hpsi and delete old hpsi later
77+
if(this->hpsi != std::get<0>(info) )
78+
{
79+
this->recursive = false;
80+
if(this->hpsi != nullptr)
81+
{
82+
delete this->hpsi;
83+
}
84+
}
85+
else
7786
{
78-
delete this->hpsi;
87+
this->recursive = true;
7988
}
89+
//create a new hpsi
8090
this->hpsi = new psi::Psi<std::complex<double>>(std::get<0>(info)[0], 1, nbands_range);
8191

8292
std::complex<double>* pointer_hpsi = this->hpsi->get_pointer();

0 commit comments

Comments
 (0)