Skip to content

Commit 0f569ff

Browse files
authored
fix wrong nscf energy of sDFT (#5618)
add nscf energy tests
1 parent 3d64446 commit 0f569ff

File tree

20 files changed

+66
-37
lines changed

20 files changed

+66
-37
lines changed

source/module_elecstate/elecstate_pw.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ void ElecStatePW<T, Device>::parallelK()
135135
{
136136
#ifdef __MPI
137137
this->charge->rho_mpi();
138-
if(PARAM.inp.esolver_type == "sdft") //qinarui add it 2021-7-21
139-
{
140-
this->f_en.eband /= GlobalV::NPROC_IN_POOL;
141-
MPI_Allreduce(MPI_IN_PLACE, &this->f_en.eband, 1, MPI_DOUBLE, MPI_SUM, STO_WORLD);
142-
}
143138
#endif
144139
}
145140

source/module_esolver/esolver_ks.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,16 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
442442
this->diag_ethr = PARAM.inp.pw_diag_thr;
443443
for (int iter = 1; iter <= this->maxniter; ++iter)
444444
{
445-
// 6) initialization of SCF iterations
445+
// 5) initialization of SCF iterations
446446
this->iter_init(ucell, istep, iter);
447447

448+
// 6) use Hamiltonian to obtain charge density
448449
this->hamilt2density(ucell, istep, iter, diag_ethr);
449450

450-
// 10) finish scf iterations
451+
// 7) finish scf iterations
451452
this->iter_finish(ucell, istep, iter);
452453

453-
// 13) check convergence
454+
// 8) check convergence
454455
if (this->conv_esolver || this->oscillate_esolver)
455456
{
456457
this->niter = iter;
@@ -462,7 +463,7 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
462463
}
463464
} // end scf iterations
464465

465-
// 15) after scf
466+
// 9) after scf
466467
ModuleBase::timer::tick(this->classname, "after_scf");
467468
this->after_scf(ucell, istep);
468469
ModuleBase::timer::tick(this->classname, "after_scf");

source/module_hamilt_pw/hamilt_stodft/sto_iter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,13 @@ void Stochastic_Iter<T, Device>::calHsqrtchi(Stochastic_WF<T, Device>& stowf)
472472
}
473473

474474
template <typename T, typename Device>
475-
void Stochastic_Iter<T, Device>::sum_stoband(Stochastic_WF<T, Device>& stowf,
475+
void Stochastic_Iter<T, Device>::sum_stoeband(Stochastic_WF<T, Device>& stowf,
476476
elecstate::ElecStatePW<T, Device>* pes,
477477
hamilt::Hamilt<T, Device>* pHamilt,
478478
ModulePW::PW_Basis_K* wfc_basis)
479479
{
480-
ModuleBase::TITLE("Stochastic_Iter", "sum_stoband");
481-
ModuleBase::timer::tick("Stochastic_Iter", "sum_stoband");
480+
ModuleBase::TITLE("Stochastic_Iter", "sum_stoeband");
481+
ModuleBase::timer::tick("Stochastic_Iter", "sum_stoeband");
482482
const int npwx = wfc_basis->npwk_max;
483483
const int norder = p_che->norder;
484484

@@ -556,7 +556,7 @@ void Stochastic_Iter<T, Device>::sum_stoband(Stochastic_WF<T, Device>& stowf,
556556
MPI_Allreduce(MPI_IN_PLACE, &sto_eband, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
557557
#endif
558558
pes->f_en.eband += sto_eband;
559-
ModuleBase::timer::tick("Stochastic_Iter", "sum_stoband");
559+
ModuleBase::timer::tick("Stochastic_Iter", "sum_stoeband");
560560
}
561561

562562
template <typename T, typename Device>

source/module_hamilt_pw/hamilt_stodft/sto_iter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Stochastic_Iter
5252
* @param pHamilt hamiltonian
5353
* @param wfc_basis wfc pw basis
5454
*/
55-
void sum_stoband(Stochastic_WF<T, Device>& stowf,
55+
void sum_stoeband(Stochastic_WF<T, Device>& stowf,
5656
elecstate::ElecStatePW<T, Device>* pes,
5757
hamilt::Hamilt<T, Device>* pHamilt,
5858
ModulePW::PW_Basis_K* wfc_basis);

source/module_hsolver/hsolver_pw_sdft.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,39 @@ void HSolverPW_SDFT<T, Device>::solve(hamilt::Hamilt<T, Device>* pHamilt,
108108
// prepare sqrt{f(\hat{H})}|\chi> to calculate density, force and stress
109109
stoiter.calHsqrtchi(stowf);
110110

111+
// calculate eband = \sum_{ik,ib} w(ik)f(ik,ib)e_{ikib}, demet = -TS
111112
elecstate::ElecStatePW<T, Device>* pes_pw = static_cast<elecstate::ElecStatePW<T, Device>*>(pes);
112113
if (GlobalV::MY_STOGROUP == 0)
113114
{
114115
pes_pw->calEBand();
115116
}
117+
if (nbands > 0)
118+
{
119+
#ifdef __MPI
120+
pes->f_en.eband /= GlobalV::NPROC_IN_POOL;
121+
MPI_Allreduce(MPI_IN_PLACE, &pes->f_en.eband, 1, MPI_DOUBLE, MPI_SUM, STO_WORLD);
122+
MPI_Bcast(&pes->f_en.eband, 1, MPI_DOUBLE, 0, PARAPW_WORLD);
123+
#endif
124+
}
125+
stoiter.sum_stoeband(stowf, pes_pw, pHamilt, wfc_basis);
126+
127+
128+
129+
// for nscf, skip charge
116130
if (skip_charge)
117131
{
118132
ModuleBase::timer::tick("HSolverPW_SDFT", "solve");
119133
return;
120134
}
135+
121136
//(5) calculate new charge density
122137
// calculate KS rho.
123138
pes_pw->init_rho_data();
124139
if (nbands > 0)
125140
{
126141
pes_pw->psiToRho(psi);
127-
#ifdef __MPI
128-
MPI_Bcast(&pes->f_en.eband, 1, MPI_DOUBLE, 0, PARAPW_WORLD);
129-
#endif
130142
}
131-
132143
// calculate stochastic rho
133-
stoiter.sum_stoband(stowf, pes_pw, pHamilt, wfc_basis);
134144
stoiter.cal_storho(stowf, pes_pw, wfc_basis);
135145

136146
// will do rho symmetry and energy calculation in esolver

source/module_hsolver/test/test_hsolver_sdft.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void Stochastic_Iter<T, Device>::calHsqrtchi(Stochastic_WF<T, Device>& stowf)
141141
}
142142

143143
template <typename T, typename Device>
144-
void Stochastic_Iter<T, Device>::sum_stoband(Stochastic_WF<T, Device>& stowf,
144+
void Stochastic_Iter<T, Device>::sum_stoeband(Stochastic_WF<T, Device>& stowf,
145145
elecstate::ElecStatePW<T, Device>* pes,
146146
hamilt::Hamilt<T, Device>* pHamilt,
147147
ModulePW::PW_Basis_K* wfc_basis)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
etotref -206.9728084455294
2+
etotperatomref -103.4864042228
13
totaldosref 1190.44
24
CompareBand_pass 0
3-
totaltimeref 0.33861
5+
totaltimeref 0.38
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
etotref -206.9727001566756
2+
etotperatomref -103.4863500783
13
CompareBand_pass 0
2-
totaltimeref 0.28457
4+
totaltimeref 0.23
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
etotref -206.9727320695148
2+
etotperatomref -103.4863660348
13
totaldosref 1190.44
2-
totaltimeref
4+
totaltimeref 0.26
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
etotref -308.0061503011041
2+
etotperatomref -154.0030751506
13
CompareAMN_pass 0
24
CompareMMN_pass 0
35
CompareEIG_pass 0
4-
totaltimeref 9.3341
6+
totaltimeref 2.15

0 commit comments

Comments
 (0)