Skip to content

Commit e4dde18

Browse files
authored
Merge pull request #1108 from deepmodeling/HSolver
Refactor: Operator for PW base, accelerated davidson diagonalization method using new Operator for a example
2 parents c9d86ef + b86feda commit e4dde18

26 files changed

+1112
-558
lines changed

source/module_base/global_function.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "global_variable.h"
1919
#include "global_function-func_each_2.h" // Peize Lin add 2016-09-07
2020

21+
#include "blas_connector.h"
22+
2123
namespace ModuleBase
2224
{
2325
namespace GlobalFunc
@@ -167,6 +169,23 @@ static inline void DCOPY( const T &a, T &b, const int &dim)
167169
for (int i=0; i<dim; ++i) b[i] = a[i];
168170
}
169171

172+
template<typename T>
173+
inline void COPYARRAY(const T* a, T* b, const long dim);
174+
175+
template<>
176+
inline void COPYARRAY(const std::complex<double>* a, std::complex<double>* b, const long dim)
177+
{
178+
const int one = 1;
179+
zcopy_(&dim, a, &one, b, &one);
180+
}
181+
182+
template<>
183+
inline void COPYARRAY(const double* a, double* b, const long dim)
184+
{
185+
const int one = 1;
186+
dcopy_(&dim, a, &one, b, &one);
187+
}
188+
170189
void BLOCK_HERE( const std::string &description );
171190

172191
//==========================================================
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
add_subdirectory(ks_pw)
2+
13
add_library(
24
hamilt
35
OBJECT
46
hamilt_pw.cpp
5-
ekinetic_pw.cpp
6-
veff_pw.cpp
7-
nonlocal_pw.cpp
8-
meta_pw.cpp
97
hamilt_lcao.cpp
8+
ks_pw/ekinetic_pw.cpp
9+
ks_pw/veff_pw.cpp
10+
ks_pw/nonlocal_pw.cpp
11+
ks_pw/meta_pw.cpp
1012
)

source/module_hamilt/ekinetic_pw.cpp

Lines changed: 0 additions & 74 deletions
This file was deleted.

source/module_hamilt/ekinetic_pw.h

Lines changed: 0 additions & 37 deletions
This file was deleted.

source/module_hamilt/hamilt.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ class Hamilt
3333

3434
int non_first_scf=0;
3535

36-
protected:
37-
// array, save operations from each operators
38-
std::vector<Operator*> ops;
36+
// first node operator, add operations from each operators
37+
Operator* ops = nullptr;
3938
};
4039

4140
} // namespace hamilt

source/module_hamilt/hamilt_pw.cpp

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,111 +7,98 @@
77
#include "src_parallel/parallel_reduce.h"
88
#include "src_pw/global.h"
99

10-
#include "veff_pw.h"
11-
#include "ekinetic_pw.h"
12-
#include "meta_pw.h"
13-
#include "nonlocal_pw.h"
10+
#include "ks_pw/veff_pw.h"
11+
#include "ks_pw/ekinetic_pw.h"
12+
#include "ks_pw/meta_pw.h"
13+
#include "ks_pw/nonlocal_pw.h"
1414

1515
namespace hamilt
1616
{
1717

1818
HamiltPW::HamiltPW()
1919
{
2020
this->classname = "HamiltPW";
21-
this->ops.resize(0);
22-
const int npwx = GlobalC::wf.npwx;
23-
const int npol = GlobalV::NPOL;
24-
const double tpiba = GlobalC::ucell.tpiba;
2521
const double tpiba2 = GlobalC::ucell.tpiba2;
26-
const int* ngk = GlobalC::kv.ngk.data();
22+
const double tpiba = GlobalC::ucell.tpiba;
2723
const int* isk = GlobalC::kv.isk.data();
2824
const double* gk2 = GlobalC::wfcpw->gk2;
2925

3026
if (GlobalV::T_IN_H)
3127
{
32-
Operator* ekinetic = new EkineticPW(
33-
npwx,
34-
npol,
28+
Operator* ekinetic = new Ekinetic<OperatorPW>(
3529
tpiba2,
36-
ngk,
37-
gk2
30+
gk2,
31+
GlobalC::wfcpw->npwk_max
3832
);
39-
this->ops.push_back(ekinetic);
33+
if(this->ops == nullptr)
34+
{
35+
this->ops = ekinetic;
36+
}
37+
else
38+
{
39+
this->ops->add(ekinetic);
40+
}
4041
}
4142
if (GlobalV::VL_IN_H)
4243
{
43-
Operator* veff = new VeffPW(
44-
npwx,
45-
npol,
46-
ngk,
44+
Operator* veff = new Veff<OperatorPW>(
4745
isk,
4846
&(GlobalC::pot.vr_eff),
4947
GlobalC::wfcpw
5048
);
51-
this->ops.push_back(veff);
49+
if(this->ops == nullptr)
50+
{
51+
this->ops = veff;
52+
}
53+
else
54+
{
55+
this->ops->add(veff);
56+
}
5257
}
5358
if (GlobalV::VNL_IN_H)
5459
{
55-
Operator* nonlocal = new NonlocalPW(
56-
npwx,
57-
npol,
58-
ngk,
60+
Operator* nonlocal = new Nonlocal<OperatorPW>(
5961
isk,
6062
&GlobalC::ppcell,
6163
&GlobalC::ucell
6264
);
63-
this->ops.push_back(nonlocal);
65+
if(this->ops == nullptr)
66+
{
67+
this->ops = nonlocal;
68+
}
69+
else
70+
{
71+
this->ops->add(nonlocal);
72+
}
6473
}
65-
Operator* meta = new MetaPW(
66-
npwx,
67-
npol,
74+
Operator* meta = new Meta<OperatorPW>(
6875
tpiba,
69-
ngk,
7076
isk,
7177
&GlobalC::pot.vofk,
7278
GlobalC::wfcpw
7379
);
74-
this->ops.push_back(meta);
80+
if(this->ops == nullptr)
81+
{
82+
this->ops = meta;
83+
}
84+
else
85+
{
86+
this->ops->add(meta);
87+
}
7588
}
7689

7790
HamiltPW::~HamiltPW()
7891
{
7992
int index = 0;
80-
if (GlobalV::T_IN_H)
81-
{
82-
delete (EkineticPW*)this->ops[index++];
83-
}
84-
if (GlobalV::VL_IN_H)
85-
{
86-
delete (VeffPW*)this->ops[index++];
87-
}
88-
if (GlobalV::VNL_IN_H)
89-
{
90-
delete (NonlocalPW*)this->ops[index++];
91-
}
92-
delete (MetaPW*)this->ops[index++];
93+
delete this->ops;
9394
}
9495

9596
void HamiltPW::updateHk(const int ik)
9697
{
9798
ModuleBase::TITLE("HamiltPW","updateHk");
9899

99-
for(int iter = 0; iter < this->ops.size(); ++iter)
100-
{
101-
this->ops[iter]->init(ik);
102-
}
103-
104-
return;
105-
}
100+
this->ops->init(ik);
106101

107-
void HamiltPW::hPsi(const std::complex<double> *psi_in, std::complex<double> *hpsi, const size_t size) const
108-
{
109-
ModuleBase::timer::tick("HamiltPW", "h_psi");
110-
for(int iter = 0; iter < this->ops.size(); ++iter)
111-
{
112-
this->ops[iter]->act(psi_in, hpsi, size);
113-
}
114-
ModuleBase::timer::tick("HamiltPW", "h_psi");
115102
return;
116103
}
117104

@@ -122,10 +109,7 @@ void HamiltPW::sPsi
122109
size_t size
123110
) const
124111
{
125-
for (size_t i=0; i<size; i++)
126-
{
127-
spsi[i] = psi[i];
128-
}
112+
ModuleBase::GlobalFunc::COPYARRAY(psi, spsi, size);
129113
return;
130114
}
131115

source/module_hamilt/hamilt_pw.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class HamiltPW : public Hamilt
1616
void updateHk(const int ik) override;
1717

1818
// core function: for solving eigenvalues of Hamiltonian with iterative method
19-
virtual void hPsi(const std::complex<double> *psi_in, std::complex<double> *hpsi, const size_t size) const override;
2019
virtual void sPsi(const std::complex<double> *psi_in, std::complex<double> *spsi, const size_t size) const override;
2120

2221
private:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_library(
2+
operator_ks_pw
3+
OBJECT
4+
ekinetic_pw.cpp
5+
veff_pw.cpp
6+
nonlocal_pw.cpp
7+
meta_pw.cpp
8+
)

0 commit comments

Comments
 (0)