Skip to content

Commit 79e054e

Browse files
authored
Fix: enable ecutrho/ecutwfc > 4 for ncpp (#5765)
* Fix: enable ecutrho/ecutwfc > 4 for ncpp * Tests: add integrated tests * Fix: init rhog * Fix: enable combination of ncpp and uspp * Tests: add tests * Tests: update result.ref * Tests: update tests
1 parent c3e78f7 commit 79e054e

File tree

13 files changed

+140
-37
lines changed

13 files changed

+140
-37
lines changed

source/module_elecstate/elecstate_pw.cpp

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,55 @@ ElecStatePW<T, Device>::ElecStatePW(ModulePW::PW_Basis_K* wfc_basis_in,
3131
template<typename T, typename Device>
3232
ElecStatePW<T, Device>::~ElecStatePW()
3333
{
34-
if (base_device::get_device_type<Device>(this->ctx) == base_device::GpuDevice)
34+
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single")
3535
{
3636
delmem_var_op()(this->ctx, this->rho_data);
37+
delete[] this->rho;
38+
39+
if (PARAM.globalv.double_grid || PARAM.globalv.use_uspp)
40+
{
41+
delmem_complex_op()(this->ctx, this->rhog_data);
42+
delete[] this->rhog;
43+
}
3744
if (get_xc_func_type() == 3 || PARAM.inp.out_elf[0] > 0)
3845
{
3946
delmem_var_op()(this->ctx, this->kin_r_data);
47+
delete[] this->kin_r;
4048
}
4149
}
42-
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") {
43-
delete[] this->rho;
44-
delete[] this->kin_r;
50+
if (PARAM.globalv.use_uspp)
51+
{
52+
delmem_var_op()(this->ctx, this->becsum);
4553
}
46-
delmem_var_op()(this->ctx, becsum);
4754
delmem_complex_op()(this->ctx, this->wfcr);
4855
delmem_complex_op()(this->ctx, this->wfcr_another_spin);
4956
}
5057

5158
template<typename T, typename Device>
5259
void ElecStatePW<T, Device>::init_rho_data()
5360
{
54-
if(this->init_rho) {
61+
if (this->init_rho)
62+
{
5563
return;
5664
}
57-
58-
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") {
65+
66+
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single")
67+
{
5968
this->rho = new Real*[this->charge->nspin];
6069
resmem_var_op()(this->ctx, this->rho_data, this->charge->nspin * this->charge->nrxx);
61-
for (int ii = 0; ii < this->charge->nspin; ii++) {
70+
for (int ii = 0; ii < this->charge->nspin; ii++)
71+
{
6272
this->rho[ii] = this->rho_data + ii * this->charge->nrxx;
6373
}
74+
if (PARAM.globalv.double_grid || PARAM.globalv.use_uspp)
75+
{
76+
this->rhog = new T*[this->charge->nspin];
77+
resmem_complex_op()(this->ctx, this->rhog_data, this->charge->nspin * this->charge->rhopw->npw);
78+
for (int ii = 0; ii < this->charge->nspin; ii++)
79+
{
80+
this->rhog[ii] = this->rhog_data + ii * this->charge->rhopw->npw;
81+
}
82+
}
6483
if (get_xc_func_type() == 3 || PARAM.inp.out_elf[0] > 0)
6584
{
6685
this->kin_r = new Real*[this->charge->nspin];
@@ -70,8 +89,13 @@ void ElecStatePW<T, Device>::init_rho_data()
7089
}
7190
}
7291
}
73-
else {
92+
else
93+
{
7494
this->rho = reinterpret_cast<Real **>(this->charge->rho);
95+
if (PARAM.globalv.double_grid || PARAM.globalv.use_uspp)
96+
{
97+
this->rhog = reinterpret_cast<T**>(this->charge->rhog);
98+
}
7599
if (get_xc_func_type() == 3 || PARAM.inp.out_elf[0] > 0)
76100
{
77101
this->kin_r = reinterpret_cast<Real **>(this->charge->kin_r);
@@ -100,19 +124,24 @@ void ElecStatePW<T, Device>::psiToRho(const psi::Psi<T, Device>& psi)
100124
// ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[is], this->charge->nrxx);
101125
setmem_var_op()(this->ctx, this->kin_r[is], 0, this->charge->nrxx);
102126
}
103-
}
127+
if (PARAM.globalv.double_grid || PARAM.globalv.use_uspp)
128+
{
129+
setmem_complex_op()(this->ctx, this->rhog[is], 0, this->charge->rhopw->npw);
130+
}
131+
}
104132

105133
for (int ik = 0; ik < psi.get_nk(); ++ik)
106134
{
107135
psi.fix_k(ik);
108136
this->updateRhoK(psi);
109137
}
110-
if (PARAM.globalv.use_uspp)
138+
139+
this->add_usrho(psi);
140+
141+
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single")
111142
{
112-
this->add_usrho(psi);
113-
}
114-
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") {
115-
for (int ii = 0; ii < PARAM.inp.nspin; ii++) {
143+
for (int ii = 0; ii < PARAM.inp.nspin; ii++)
144+
{
116145
castmem_var_d2h_op()(cpu_ctx, this->ctx, this->charge->rho[ii], this->rho[ii], this->charge->nrxx);
117146
if (get_xc_func_type() == 3)
118147
{
@@ -397,32 +426,39 @@ void ElecStatePW<T, Device>::cal_becsum(const psi::Psi<T, Device>& psi)
397426
template <typename T, typename Device>
398427
void ElecStatePW<T, Device>::add_usrho(const psi::Psi<T, Device>& psi)
399428
{
400-
this->cal_becsum(psi);
429+
if (PARAM.globalv.use_uspp)
430+
{
431+
this->cal_becsum(psi);
432+
}
401433

402434
// transform soft charge to recip space using smooth grids
403-
T* rhog = nullptr;
404-
resmem_complex_op()(this->ctx, rhog, this->charge->rhopw->npw * PARAM.inp.nspin, "ElecState<PW>::rhog");
405-
setmem_complex_op()(this->ctx, rhog, 0, this->charge->rhopw->npw * PARAM.inp.nspin);
406-
for (int is = 0; is < PARAM.inp.nspin; is++)
435+
if (PARAM.globalv.double_grid || PARAM.globalv.use_uspp)
407436
{
408-
this->rhopw_smooth->real2recip(this->rho[is], &rhog[is * this->charge->rhopw->npw]);
437+
for (int is = 0; is < PARAM.inp.nspin; is++)
438+
{
439+
this->rhopw_smooth->real2recip(this->rho[is], this->rhog[is]);
440+
}
409441
}
410442

411443
// \sum_lm Q_lm(r) \sum_i <psi_i|beta_l><beta_m|psi_i> w_i
412444
// add to the charge density in reciprocal space the part which is due to the US augmentation.
413-
this->addusdens_g(becsum, rhog);
445+
if (PARAM.globalv.use_uspp)
446+
{
447+
this->addusdens_g(becsum, rhog);
448+
}
414449

415450
// transform back to real space using dense grids
416-
for (int is = 0; is < PARAM.inp.nspin; is++)
451+
if (PARAM.globalv.double_grid || PARAM.globalv.use_uspp)
417452
{
418-
this->charge->rhopw->recip2real(&rhog[is * this->charge->rhopw->npw], this->rho[is]);
453+
for (int is = 0; is < PARAM.inp.nspin; is++)
454+
{
455+
this->charge->rhopw->recip2real(this->rhog[is], this->rho[is]);
456+
}
419457
}
420-
421-
delmem_complex_op()(this->ctx, rhog);
422458
}
423459

424460
template <typename T, typename Device>
425-
void ElecStatePW<T, Device>::addusdens_g(const Real* becsum, T* rhog)
461+
void ElecStatePW<T, Device>::addusdens_g(const Real* becsum, T** rhog)
426462
{
427463
const T one{1, 0};
428464
const T zero{0, 0};
@@ -506,7 +542,7 @@ void ElecStatePW<T, Device>::addusdens_g(const Real* becsum, T* rhog)
506542
this->ppcell->radial_fft_q(this->ctx, npw, ih, jh, it, qmod, ylmk0, qgm);
507543
for (int ig = 0; ig < npw; ig++)
508544
{
509-
rhog[is * npw + ig] += qgm[ig] * aux2[ijh * npw + ig];
545+
rhog[is][ig] += qgm[ig] * aux2[ijh * npw + ig];
510546
}
511547
ijh++;
512548
}

source/module_elecstate/elecstate_pw.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ class ElecStatePW : public ElecState
4242

4343
//! init rho_data and kin_r_data
4444
void init_rho_data();
45-
Real** rho = nullptr;
46-
Real** kin_r = nullptr; //[Device] [spin][nrxx] rho and kin_r
45+
Real** rho = nullptr; // [Device] [spin][nrxx] rho
46+
T** rhog = nullptr; // [Device] [spin][nrxx] rhog
47+
Real** kin_r = nullptr; // [Device] [spin][nrxx] kin_r
4748

4849
protected:
4950

@@ -70,15 +71,16 @@ class ElecStatePW : public ElecState
7071

7172
//! Non-local pseudopotentials
7273
//! \sum_lm Q_lm(r) \sum_i <psi_i|beta_l><beta_m|psi_i> w_i
73-
void addusdens_g(const Real* becsum, T* rhog);
74+
void addusdens_g(const Real* becsum, T** rhog);
7475

7576
Device * ctx = {};
7677

7778
bool init_rho = false;
7879

7980
mutable T* vkb = nullptr;
8081

81-
Real* rho_data = nullptr;
82+
Real* rho_data = nullptr;
83+
T* rhog_data = nullptr;
8284
Real* kin_r_data = nullptr;
8385
T* wfcr = nullptr;
8486
T* wfcr_another_spin = nullptr;

source/module_hamilt_pw/hamilt_pwdft/VNL_in_pw.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,8 @@ void pseudopot_cell_vnl::newq(const ModuleBase::matrix& veff, const ModulePW::PW
15421542
fact = 2.0;
15431543
}
15441544

1545+
deeq.zero_out();
1546+
15451547
const int npw = rho_basis->npw;
15461548
ModuleBase::matrix ylmk0(lmaxq * lmaxq, npw);
15471549
ModuleBase::YlmReal::Ylm_Real(lmaxq * lmaxq, npw, rho_basis->gcar, ylmk0);

tests/integrate/101_PW_upf201_Al_pseudopots/INPUT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pseudo_dir ../../PP_ORB
99

1010
#Parameters (2.Iteration)
1111
ecutwfc 30
12+
ecutrho 160
1213
scf_thr 1e-9
1314
scf_nmax 100
1415

tests/integrate/101_PW_upf201_Al_pseudopots/README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ This test for:
33
*upf201 pseudopotential
44
*mixing_type broyden-kerker
55
*mixing_beta 0.7
6+
*ecutwfc 30
7+
*ecutrho 160
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test upf201 pseudopotential, symmetry=on
1+
test upf201 pseudopotential with ecutrho/ecutwfc > 4, symmetry=on
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
etotref -57.66876692615671
2-
etotperatomref -57.6687669262
1+
etotref -57.66876684738178
2+
etotperatomref -57.6687668474
33
pointgroupref O_h
44
spacegroupref O_h
55
nksibzref 3
6-
totaltimeref
6+
totaltimeref 0.29
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
INPUT_PARAMETERS
2+
#Parameters (1.General)
3+
suffix autotest
4+
calculation scf
5+
symmetry 0
6+
latname fcc
7+
pseudo_dir ../../PP_ORB
8+
9+
#Parameters (2.Iteration)
10+
ecutwfc 20
11+
ecutrho 160
12+
scf_thr 1e-9
13+
scf_nmax 100
14+
15+
#Parameters (3.Basis)
16+
basis_type pw
17+
18+
#Parameters (4.Smearing)
19+
smearing_method gaussian
20+
smearing_sigma 0.002
21+
22+
#parameters (5.Mixing)
23+
mixing_type pulay
24+
mixing_beta 0.4
25+
26+
pseudo_mesh 1
27+
pseudo_rcut 10
28+
29+
cal_force 1
30+
cal_stress 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
K_POINTS
2+
0
3+
Gamma
4+
2 2 2 0 0 0
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ATOMIC_SPECIES
2+
H 1.008 H_ONCV_PBE-1.0.upf
3+
Cl 35.453 Cl.pbe-nl-rrkjus_psl.1.0.0.UPF
4+
5+
LATTICE_CONSTANT
6+
7
7+
8+
ATOMIC_POSITIONS
9+
Direct
10+
11+
H #label
12+
0 #magnetism
13+
1 #number of atoms
14+
0.00 0.00 0.00
15+
16+
Cl #label
17+
0 #magnetism
18+
1 #number of atoms
19+
0.51 0.00 0.00

0 commit comments

Comments
 (0)