Skip to content

Commit 71e31d7

Browse files
committed
Refactor: add allocation of HSolver in esolver_ks_lcao and fix bug of DiagoDavid parameter pass
1 parent 7876f92 commit 71e31d7

File tree

5 files changed

+109
-45
lines changed

5 files changed

+109
-45
lines changed

source/module_esolver/esolver_ks_lcao_elec.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#endif
2727
#include "../src_pw/H_Ewald_pw.h"
2828

29+
#include "module_hamilt/hamilt_lcao.h"
30+
#include "module_elecstate/elecstate_lcao.h"
31+
#include "module_hsolver/hsolver_lcao.h"
32+
2933
namespace ModuleESolver
3034
{
3135

@@ -188,6 +192,75 @@ namespace ModuleESolver
188192
srho.begin(is, GlobalC::CHR, GlobalC::pw, GlobalC::Pgrid, GlobalC::symm);
189193
}
190194

195+
//init Psi, HSolver, ElecState, Hamilt
196+
if(this->phsol != nullptr)
197+
{
198+
if(this->phsol->classname != "HSolverLCAO")
199+
{
200+
delete this->phsol;
201+
this->phsol = nullptr;
202+
}
203+
}
204+
else
205+
{
206+
this->phsol = new hsolver::HSolverLCAO();
207+
this->phsol->method = GlobalV::KS_SOLVER;
208+
}
209+
if(this->pelec != nullptr)
210+
{
211+
if(this->pelec->classname != "ElecStateLCAO")
212+
{
213+
delete this->pelec;
214+
this->pelec = nullptr;
215+
}
216+
}
217+
else
218+
{
219+
this->pelec = new elecstate::ElecStateLCAO(
220+
(Charge*)(&(GlobalC::CHR)),
221+
&(GlobalC::kv),
222+
GlobalC::kv.nks,
223+
GlobalV::NBANDS,
224+
&(this->LOC),
225+
&(this->UHM),
226+
&(this->LOWF));
227+
}
228+
if(this->phami != nullptr)
229+
{
230+
if(this->phami->classname != "HamiltLCAO")
231+
{
232+
delete this->phami;
233+
this->phami = nullptr;
234+
}
235+
}
236+
else
237+
{
238+
// three cases for hamilt class
239+
if(GlobalV::GAMMA_ONLY_LOCAL)
240+
{
241+
this->phami = new hamilt::HamiltLCAO<double, double>(
242+
&(this->UHM.GG),
243+
&(this->UHM.genH),
244+
&(this->LM) );
245+
}
246+
// non-collinear spin case would not use the second template now,
247+
// would add this feature in the future
248+
/*else if(GlobalV::NSPIN==4)
249+
{
250+
this->phami = new hamilt::HamiltLCAO<std::complex<double>, std::complex<double>>(
251+
&(this->UHM.GK),
252+
&(this->UHM.genH),
253+
&(this->LM) );
254+
}*/
255+
else
256+
{
257+
this->phami = new hamilt::HamiltLCAO<std::complex<double>, double>(
258+
&(this->UHM.GK),
259+
&(this->UHM.genH),
260+
&(this->LM) );
261+
}
262+
}
263+
191264
ModuleBase::timer::tick("ESolver_KS_LCAO", "beforescf");
192265
return;
193266
}

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,37 +199,43 @@ namespace ModuleESolver
199199
hsolver::DiagoIterAssist::PW_DIAG_NMAX = GlobalV::PW_DIAG_NMAX;
200200
hsolver::DiagoIterAssist::PW_DIAG_THR = GlobalV::PW_DIAG_THR;
201201
const PW_Basis* pbas = &(GlobalC::pw);
202-
if(this->phsol == nullptr)
202+
if(this->phsol != nullptr)
203203
{
204-
this->phsol = new hsolver::HSolverPW(pbas);
204+
if(this->phsol->classname != "HSolverPW")
205+
{
206+
delete this->phsol;
207+
this->phsol = nullptr;
208+
}
205209
}
206-
else if(this->phsol->classname != "HSolverPW")
210+
else
207211
{
208-
delete[] this->phsol;
209212
this->phsol = new hsolver::HSolverPW(pbas);
213+
this->phsol->method = GlobalV::KS_SOLVER;
210214
}
211-
this->phsol->method = GlobalV::KS_SOLVER;
212-
if(this->pelec == nullptr)
215+
if(this->pelec != nullptr)
213216
{
214-
this->pelec = new elecstate::ElecStatePW( pbas, (Charge*)(&(GlobalC::CHR)), GlobalV::NBANDS);
217+
if(this->pelec->classname != "ElecStatePW")
218+
{
219+
delete this->pelec;
220+
this->pelec = nullptr;
221+
}
215222
}
216-
else if(this->pelec->classname != "ElecStatePW")
223+
else
217224
{
218-
delete[] this->pelec;
219225
this->pelec = new elecstate::ElecStatePW( pbas, (Charge*)(&(GlobalC::CHR)), GlobalV::NBANDS);
220226
}
221-
Hamilt_PW* hpw = &(GlobalC::hm.hpw);
222-
if(this->phami == nullptr)
227+
if(this->phami != nullptr)
223228
{
224-
this->phami = new hamilt::HamiltPW(hpw);
229+
if(this->phami->classname != "HamiltPW")
230+
{
231+
delete this->phami;
232+
this->phami = nullptr;
233+
}
225234
}
226-
else if(this->phami->classname != "HamiltPW")
235+
else
227236
{
228-
delete[] this->phami;
229-
this->phami = new hamilt::HamiltPW(hpw);
237+
this->phami = new hamilt::HamiltPW(&(GlobalC::hm.hpw));
230238
}
231-
//initial psi
232-
//GlobalC::wf.evc_transform_psi();
233239
}
234240

235241
void ESolver_KS_PW::eachiterinit(const int istep, const int iter)

source/module_hamilt/hamilt_lcao.cpp

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace hamilt
1111
template class HamiltLCAO<double, double>;
1212
// case for nspin<4, multi-k-points
1313
template class HamiltLCAO<std::complex<double>, double>;
14+
// case for nspin == 4, non-collinear spin case
15+
//template class HamiltLCAO<std::complex<double>, std::complex<double>>;
1416

1517
template <typename T, typename T1> void HamiltLCAO<T, T1>::constructFixedReal()
1618
{
@@ -83,8 +85,9 @@ template <typename T, typename T1> void HamiltLCAO<T, T1>::getMatrix(MatrixBlock
8385
}
8486

8587
// case for nspin==4
86-
/*void HamiltLCAO<std::complex<double>, std::complex<double>>::matrix(MatrixBlock<std::complex<double>> hk_in,
87-
MatrixBlock<std::complex<double>> sk_in)const
88+
/*template <>
89+
void HamiltLCAO<std::complex<double>, std::complex<double>>::matrix(MatrixBlock<std::complex<double>> &hk_in,
90+
MatrixBlock<std::complex<double>> &sk_in)
8891
{
8992
this->getMatrix(hk_in, sk_in);
9093
}*/
@@ -102,41 +105,16 @@ template <> void HamiltLCAO<double, double>::matrix(MatrixBlock<double> &hk_in,
102105
this->getMatrix(hk_in, sk_in);
103106
}
104107

105-
// nspin==4 case not supported yet
106-
/*void HamiltLCAO<std::complex<double>, std::complex<double>>::constructHamilt(const int iter, const MatrixBlock<double>
107-
rho)
108-
{
109-
this->constructFixedReal();
110-
this->constructUpdateReal();
111-
}*/
112108
template <typename T, typename T1>
113109
void HamiltLCAO<T, T1>::constructHamilt(const int iter, const MatrixBlock<double> rho)
114110
{
115111
this->constructFixedReal();
116112
this->constructUpdateReal();
117113
}
118-
/*template<>
119-
void HamiltLCAO<double, double>::constructHamilt(const int iter, const MatrixBlock<double> rho)
120-
{
121-
this->constructFixedReal();
122-
this->constructUpdateReal();
123-
}*/
124-
125-
/*void HamiltLCAO<std::complex<double>, std::complex<double>>::updateHk(const int ik)
126-
{
127-
this->hk_fixed_mock(ik);
128-
this->hk_update_mock(ik);
129-
};*/
130114
template <typename T, typename T1> void HamiltLCAO<T, T1>::updateHk(const int ik)
131115
{
132116
this->hk_fixed_mock(ik);
133117
this->hk_update_mock(ik);
134118
}
135-
/*template<>
136-
void HamiltLCAO<double, double>::updateHk(const int ik)
137-
{
138-
this->hk_fixed_mock(ik);
139-
this->hk_update_mock(ik);
140-
}*/
141119

142120
} // namespace hamilt

source/module_hamilt/hamilt_lcao.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ template <typename T> class LocalMatrix
3838
template <typename T, typename T1> class HamiltLCAO : public Hamilt
3939
{
4040
public:
41-
HamiltLCAO(Gint_Gamma* GG_in, Gint_k* GK_in, LCAO_gen_fixedH* genH_in, LCAO_Matrix* LM_in)
41+
HamiltLCAO(Gint_Gamma* GG_in, LCAO_gen_fixedH* genH_in, LCAO_Matrix* LM_in)
4242
{
4343
this->GG = GG_in;
44+
this->genH = genH_in;
45+
this->LM = LM_in;
46+
this->classname = "HamiltLCAO";
47+
}
48+
HamiltLCAO(Gint_k* GK_in, LCAO_gen_fixedH* genH_in, LCAO_Matrix* LM_in)
49+
{
4450
this->GK = GK_in;
4551
this->genH = genH_in;
4652
this->LM = LM_in;

source/module_hsolver/hsolver_pw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void HSolverPW::solve(hamilt::Hamilt* pHamilt, psi::Psi<std::complex<double>>& p
4545
}
4646
else if (this->method == "dav")
4747
{
48+
DiagoDavid::PW_DIAG_NDIM = GlobalV::PW_DIAG_NDIM;
4849
if (pdiagh != nullptr)
4950
{
5051
if (pdiagh->method != this->method)

0 commit comments

Comments
 (0)