Skip to content

Commit 794f7de

Browse files
committed
Fix: allocate DiagH only once
1 parent b5598ce commit 794f7de

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

source/module_hsolver/diagh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "module_base/complexmatrix.h"
55
#include "module_hamilt/hamilt.h"
66
#include "module_psi/psi.h"
7+
#include "string"
78

89
namespace hsolver
910
{
@@ -12,6 +13,7 @@ class DiagH
1213
{
1314
public:
1415
// virtual void init()=0;
16+
std::string method = "none";
1517

1618
virtual void diag(hamilt::Hamilt *phm_in, psi::Psi<std::complex<double>> &psi, double *eigenvalue_in) = 0;
1719

source/module_hsolver/hsolver_lcao.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,43 @@ void HSolverLCAO::solveTemplate(hamilt::Hamilt* pHamilt, psi::Psi<T>& psi, elecs
1111
{
1212
// select the method of diagonalization
1313
if (this->method == "genelpa")
14-
pdiagh = new DiagoElpa();
14+
{
15+
if (pdiagh != nullptr)
16+
{
17+
if (pdiagh->method != this->method)
18+
{
19+
delete[] pdiagh;
20+
pdiagh = new DiagoElpa();
21+
pdiagh->method = this->method;
22+
}
23+
}
24+
else
25+
{
26+
pdiagh = new DiagoElpa();
27+
pdiagh->method = this->method;
28+
}
29+
}
1530
else if (this->method == "scalapack_gvx")
16-
pdiagh = new DiagoBlas();
31+
{
32+
if (pdiagh != nullptr)
33+
{
34+
if (pdiagh->method != this->method)
35+
{
36+
delete[] pdiagh;
37+
pdiagh = new DiagoBlas();
38+
pdiagh->method = this->method;
39+
}
40+
}
41+
else
42+
{
43+
pdiagh = new DiagoBlas();
44+
pdiagh->method = this->method;
45+
}
46+
}
1747
else
48+
{
1849
ModuleBase::WARNING_QUIT("HSolverLCAO::solve", "This method of DiagH is not supported!");
50+
}
1951

2052
/// Loop over k points for solve Hamiltonian to charge density
2153
for (int ik = 0; ik < psi.get_nk(); ++ik)

source/module_hsolver/hsolver_pw.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,43 @@ void HSolverPW::solve(hamilt::Hamilt* pHamilt, psi::Psi<std::complex<double>>& p
2727

2828
// select the method of diagonalization
2929
if (this->method == "cg")
30-
pdiagh = new DiagoCG(&(GlobalC::hm.hpw), precondition.data());
30+
{
31+
if(pdiagh!=nullptr)
32+
{
33+
if(pdiagh->method != this->method)
34+
{
35+
delete[] pdiagh;
36+
pdiagh = new DiagoCG(&(GlobalC::hm.hpw), precondition.data());
37+
pdiagh->method = this->method;
38+
}
39+
}
40+
else
41+
{
42+
pdiagh = new DiagoCG(&(GlobalC::hm.hpw), precondition.data());
43+
pdiagh->method = this->method;
44+
}
45+
}
3146
else if (this->method == "david")
32-
pdiagh = new DiagoDavid(&(GlobalC::hm.hpw), precondition.data());
47+
{
48+
if (pdiagh != nullptr)
49+
{
50+
if (pdiagh->method != this->method)
51+
{
52+
delete[] pdiagh;
53+
pdiagh = new DiagoDavid(&(GlobalC::hm.hpw), precondition.data());
54+
pdiagh->method = this->method;
55+
}
56+
}
57+
else
58+
{
59+
pdiagh = new DiagoDavid(&(GlobalC::hm.hpw), precondition.data());
60+
pdiagh->method = this->method;
61+
}
62+
}
3363
else
64+
{
3465
ModuleBase::WARNING_QUIT("HSolverPW::solve", "This method of DiagH is not supported!");
66+
}
3567

3668
/// Loop over k points for solve Hamiltonian to charge density
3769
for (int ik = 0; ik < psi.get_nk(); ++ik)

0 commit comments

Comments
 (0)