Skip to content

Commit 5ed2b2a

Browse files
authored
Refactor:Modify Weights and Ebands Functions in the elecstate Module (deepmodeling#5985)
* change the calEband * change calculate_weights * change fixed_weights * update file * fix bud
1 parent 3950276 commit 5ed2b2a

24 files changed

+413
-275
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ OBJS_ELECSTAT=elecstate.o\
222222
elecstate_pw_sdft.o\
223223
elecstate_pw_cal_tau.o\
224224
elecstate_op.o\
225+
elecstate_tools.o\
225226
efield.o\
226227
gatefield.o\
227228
potential_new.o\

source/module_elecstate/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ list(APPEND objects
44
elecstate_energy.cpp
55
elecstate_exx.cpp
66
elecstate_print.cpp
7+
elecstate_tools.cpp
78
elecstate_pw.cpp
89
elecstate_pw_sdft.cpp
910
elecstate_pw_cal_tau.cpp

source/module_elecstate/elecstate.cpp

Lines changed: 0 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,6 @@ const double* ElecState::getRho(int spin) const
1515
return &(this->charge->rho[spin][0]);
1616
}
1717

18-
void ElecState::fixed_weights(const std::vector<double>& ocp_kb, const int& nbands, const double& nelec)
19-
{
20-
assert(nbands > 0);
21-
assert(nelec > 0.0);
22-
23-
const double ne_thr = 1.0e-5;
24-
25-
const int num = this->klist->get_nks() * nbands;
26-
if (num != ocp_kb.size())
27-
{
28-
ModuleBase::WARNING_QUIT("ElecState::fixed_weights",
29-
"size of occupation array is wrong , please check ocp_set");
30-
}
31-
32-
double num_elec = 0.0;
33-
for (int i = 0; i < ocp_kb.size(); ++i)
34-
{
35-
num_elec += ocp_kb[i];
36-
}
37-
38-
if (std::abs(num_elec - nelec) > ne_thr)
39-
{
40-
ModuleBase::WARNING_QUIT("ElecState::fixed_weights",
41-
"total number of occupations is wrong , please check ocp_set");
42-
}
43-
44-
for (int ik = 0; ik < this->wg.nr; ++ik)
45-
{
46-
for (int ib = 0; ib < this->wg.nc; ++ib)
47-
{
48-
this->wg(ik, ib) = ocp_kb[ik * this->wg.nc + ib];
49-
}
50-
}
51-
this->skip_weights = true;
52-
53-
return;
54-
}
5518

5619

5720
void ElecState::init_nelec_spin()
@@ -64,143 +27,6 @@ void ElecState::init_nelec_spin()
6427
}
6528
}
6629

67-
68-
void ElecState::calculate_weights()
69-
{
70-
ModuleBase::TITLE("ElecState", "calculate_weights");
71-
if (this->skip_weights)
72-
{
73-
return;
74-
}
75-
76-
const int nbands = this->ekb.nc;
77-
const int nks = this->ekb.nr;
78-
79-
if (!Occupy::use_gaussian_broadening && !Occupy::fixed_occupations)
80-
{
81-
if (PARAM.globalv.two_fermi)
82-
{
83-
Occupy::iweights(nks,
84-
this->klist->wk,
85-
nbands,
86-
this->nelec_spin[0],
87-
this->ekb,
88-
this->eferm.ef_up,
89-
this->wg,
90-
0,
91-
this->klist->isk);
92-
Occupy::iweights(nks,
93-
this->klist->wk,
94-
nbands,
95-
this->nelec_spin[1],
96-
this->ekb,
97-
this->eferm.ef_dw,
98-
this->wg,
99-
1,
100-
this->klist->isk);
101-
// ef = ( ef_up + ef_dw ) / 2.0_dp need??? mohan add 2012-04-16
102-
}
103-
else
104-
{
105-
// -1 means don't need to consider spin.
106-
Occupy::iweights(nks,
107-
this->klist->wk,
108-
nbands,
109-
PARAM.inp.nelec,
110-
this->ekb,
111-
this->eferm.ef,
112-
this->wg,
113-
-1,
114-
this->klist->isk);
115-
}
116-
}
117-
else if (Occupy::use_gaussian_broadening)
118-
{
119-
if (PARAM.globalv.two_fermi)
120-
{
121-
double demet_up = 0.0;
122-
double demet_dw = 0.0;
123-
Occupy::gweights(nks,
124-
this->klist->wk,
125-
nbands,
126-
this->nelec_spin[0],
127-
Occupy::gaussian_parameter,
128-
Occupy::gaussian_type,
129-
this->ekb,
130-
this->eferm.ef_up,
131-
demet_up,
132-
this->wg,
133-
0,
134-
this->klist->isk);
135-
Occupy::gweights(nks,
136-
this->klist->wk,
137-
nbands,
138-
this->nelec_spin[1],
139-
Occupy::gaussian_parameter,
140-
Occupy::gaussian_type,
141-
this->ekb,
142-
this->eferm.ef_dw,
143-
demet_dw,
144-
this->wg,
145-
1,
146-
this->klist->isk);
147-
this->f_en.demet = demet_up + demet_dw;
148-
}
149-
else
150-
{
151-
// -1 means is no related to spin.
152-
Occupy::gweights(nks,
153-
this->klist->wk,
154-
nbands,
155-
PARAM.inp.nelec,
156-
Occupy::gaussian_parameter,
157-
Occupy::gaussian_type,
158-
this->ekb,
159-
this->eferm.ef,
160-
this->f_en.demet,
161-
this->wg,
162-
-1,
163-
this->klist->isk);
164-
}
165-
#ifdef __MPI
166-
const int npool = GlobalV::KPAR * PARAM.inp.bndpar;
167-
Parallel_Reduce::reduce_double_allpool(npool, GlobalV::NPROC_IN_POOL, this->f_en.demet);
168-
#endif
169-
}
170-
else if (Occupy::fixed_occupations)
171-
{
172-
ModuleBase::WARNING_QUIT("calculate_weights", "other occupations, not implemented");
173-
}
174-
175-
return;
176-
}
177-
178-
179-
void ElecState::calEBand()
180-
{
181-
ModuleBase::TITLE("ElecState", "calEBand");
182-
// calculate ebands using wg and ekb
183-
double eband = 0.0;
184-
#ifdef _OPENMP
185-
#pragma omp parallel for collapse(2) reduction(+ : eband)
186-
#endif
187-
for (int ik = 0; ik < this->ekb.nr; ++ik)
188-
{
189-
for (int ibnd = 0; ibnd < this->ekb.nc; ibnd++)
190-
{
191-
eband += this->ekb(ik, ibnd) * this->wg(ik, ibnd);
192-
}
193-
}
194-
this->f_en.eband = eband;
195-
196-
#ifdef __MPI
197-
const int npool = GlobalV::KPAR * PARAM.inp.bndpar;
198-
Parallel_Reduce::reduce_double_allpool(npool, GlobalV::NPROC_IN_POOL, this->f_en.eband);
199-
#endif
200-
return;
201-
}
202-
203-
20430
void ElecState::init_scf(const int istep,
20531
const UnitCell& ucell,
20632
const Parallel_Grid& pgrid,

source/module_elecstate/elecstate.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ class ElecState
7272
return;
7373
}
7474

75-
// calculate wg from ekb
76-
virtual void calculate_weights();
75+
7776

7877
// use occupied weights from INPUT and skip calculate_weights
7978
// mohan updated on 2024-06-08
80-
void fixed_weights(const std::vector<double>& ocp_kb, const int& nbands, const double& nelec);
79+
8180

8281
// if nupdown is not 0(TWO_EFERMI case),
8382
// nelec_spin will be fixed and weights will be constrained
@@ -167,11 +166,11 @@ class ElecState
167166
ModuleBase::matrix wg; ///< occupation weight for each k-point and band
168167

169168
public:
170-
// calculate ebands for all k points and all occupied bands
171-
void calEBand();
172169

173170
bool skip_weights = false;
174171
};
172+
173+
175174

176175
} // namespace elecstate
177176
#endif

0 commit comments

Comments
 (0)