Skip to content

Commit c6ae012

Browse files
committed
Refactor: 1. added nscf() in ESolver_KS_PW, 2. replace GlobalC::wf.psi with psi in ESolver
1 parent 71e31d7 commit c6ae012

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+598
-431
lines changed

source/module_elecstate/test/updaterhok_pw_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ TEST_F(EState,RhoPW)
299299
GlobalC::CHR.allocate(GlobalV::NSPIN, GlobalC::pw.nrxx, GlobalC::pw.ngmc);
300300
// GlobalC::pot.allocate(GlobalC::pw.nrxx);
301301
// we need to supply NBANDS here
302-
GlobalC::wf.allocate(GlobalC::kv.nks);
302+
psi::Psi<std::complex<double>>* psi = GlobalC::wf.allocate(GlobalC::kv.nks);
303303
// std::cout<<"npwx "<<GlobalC::wf.npwx<<std::endl;
304304
GlobalC::UFFT.allocate();
305305

306306
//====== read wavefunction ==========================================
307307
std::stringstream ssw;
308308
ssw <<GlobalV::global_out_dir<< "WAVEFUNC";
309309
// we need to supply out_wfc_pw here
310-
read_wfc2(ssw.str(), GlobalC::wf.psi[0], GlobalC::pw.gcar);
310+
read_wfc2(ssw.str(), psi[0], GlobalC::pw.gcar);
311311

312312
// copy data from old wf.evc to new evc(an object of Psi)
313313
evc.resize(GlobalC::kv.nks,GlobalV::NBANDS,GlobalC::wf.npwx);
@@ -316,9 +316,10 @@ TEST_F(EState,RhoPW)
316316
{
317317
for(int j=0;j<GlobalC::wf.npwx;j++)
318318
{
319-
evc(i,j)=GlobalC::wf.psi[0](i,j);
319+
evc(i,j) = psi[0](i,j);
320320
}
321321
}
322+
delete psi;
322323
// using class ElecStatePW to calculate rho
323324
elecstate::MockElecStatePW* kk;
324325
kk = new elecstate::MockElecStatePW(&GlobalC::pw,&GlobalC::CHR,GlobalV::NBANDS);

source/module_elecstate/test/updaterhok_pw_test.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,17 @@ void Use_FFT::allocate()
132132
return;
133133
}
134134

135-
void wavefunc::allocate(const int nks)
135+
psi::Psi<complex<double>>* wavefunc::allocate(const int nks)
136136
{
137137
this->npwx = GlobalC::pw.setupIndGk(this->igk, GlobalC::kv.ngk);
138138
this->wg.create(nks,GlobalV::NBANDS);
139139
this->ekb = new double*[nks];
140-
this->psi = new psi::Psi<std::complex<double>>(nks, GlobalV::NBANDS,npwx, nullptr);
140+
psi::Psi<std::complex<double>>* psi = new psi::Psi<std::complex<double>>(nks, GlobalV::NBANDS,npwx, nullptr);
141141
for (int ik=0;ik<nks;ik++)
142142
{
143143
this->ekb[ik] = new double[GlobalV::NBANDS];
144144
}
145-
return;
145+
return psi;
146146
}
147147

148148
bool Charge::read_rho(const int &is, const std::string &fn, double* rho) //add by dwan

source/module_esolver/esolver.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "src_lcao/local_orbital_charge.h"
1111
#include "src_lcao/local_orbital_wfc.h"
1212
#include "src_lcao/LCAO_hamilt.h"
13+
#include "module_psi/psi.h"
1314
//--------------\temporary----------------------------
1415
//------It should be moved as fast as possible------
1516

@@ -24,7 +25,14 @@ namespace ModuleESolver
2425
ESolver() {
2526
classname = "ESolver";
2627
}
27-
virtual ~ESolver() {};
28+
29+
virtual ~ESolver()
30+
{
31+
if(this->psi != nullptr)
32+
{
33+
delete psi;
34+
}
35+
}
2836

2937
//virtual void Init(Input_EnSolver &inp, matrix &lattice_v)=0
3038
virtual void Init(Input& inp, UnitCell_pseudo& cell) = 0;
@@ -36,6 +44,9 @@ namespace ModuleESolver
3644

3745
virtual void Run(int istep, UnitCell_pseudo& cell) = 0;
3846

47+
// this is the interface of non-self-consistant calculation
48+
virtual void nscf() {};
49+
3950
//Deal with exx and other calculation than scf/md/relax:
4051
// such as nscf, istate-charge or envelope
4152
virtual void othercalculation(const int istep) {};
@@ -52,6 +63,9 @@ namespace ModuleESolver
5263
//get iterstep used in current scf
5364
virtual int getniter() { return 0; }
5465
string classname;
66+
67+
//wavefunction coefficients
68+
psi::Psi<std::complex<double>>* psi=nullptr;
5569
};
5670

5771
void init_esolver(ESolver*& p_esolver, const string use_esol);

source/module_esolver/esolver_ks.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "module_hsolver/hsolver.h"
77
#include "module_hamilt/hamilt.h"
88
#include "module_elecstate/elecstate.h"
9+
#include "module_psi/psi.h"
910
// #include "estates.h"
1011
// #include "h2e.h"
1112
namespace ModuleESolver
@@ -28,7 +29,11 @@ namespace ModuleESolver
2829

2930
// calculate electron density from a specific Hamiltonian
3031
virtual void hamilt2density(const int istep, const int iter, const double ethr);
31-
// get
32+
33+
// calculate electron states from a specific Hamiltonian
34+
virtual void hamilt2estates(const double ethr){};
35+
36+
// get current step of Ionic simulation
3237
virtual int getniter() override;
3338

3439
protected:

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 149 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
#include "module_hamilt/hamilt_pw.h"
3232
#include "module_hsolver/diago_iter_assist.h"
3333

34+
#include "src_io/write_wfc_realspace.h"
35+
#include "src_io/winput.h"
36+
#include "src_io/numerical_descriptor.h"
37+
#include "src_io/numerical_basis.h"
38+
#include "src_io/to_wannier90.h"
39+
#include "src_io/berryphase.h"
40+
3441
namespace ModuleESolver
3542
{
3643

@@ -107,7 +114,7 @@ namespace ModuleESolver
107114
GlobalC::CHR.allocate(GlobalV::NSPIN, GlobalC::pw.nrxx, GlobalC::pw.ngmc);
108115
GlobalC::pot.allocate(GlobalC::pw.nrxx);
109116

110-
GlobalC::wf.allocate(GlobalC::kv.nks);
117+
this->psi = GlobalC::wf.allocate(GlobalC::kv.nks);
111118

112119
// cout<<GlobalC::pw.nrxx<<endl;
113120
// cout<<"before ufft allocate"<<endl;
@@ -158,7 +165,7 @@ namespace ModuleESolver
158165
if (GlobalV::NBANDS != 0 || GlobalV::CALCULATION.substr(0,3) != "sto")
159166
// qianrui add temporarily. In the future, wfcinit() should be compatible with cases when NBANDS=0
160167
{
161-
GlobalC::wf.wfcinit();
168+
GlobalC::wf.wfcinit(this->psi);
162169
}
163170

164171
#ifdef __LCAO
@@ -286,7 +293,7 @@ namespace ModuleESolver
286293
}
287294

288295
hsolver::DiagoIterAssist::PW_DIAG_THR = ethr;
289-
this->phsol->solve(this->phami, GlobalC::wf.psi[0], this->pelec);
296+
this->phsol->solve(this->phami, this->psi[0], this->pelec);
290297

291298
// transform energy for print
292299
GlobalC::en.eband = this->pelec->eband;
@@ -402,7 +409,7 @@ namespace ModuleESolver
402409
//WF_io::write_wfc( ssw.str(), GlobalC::wf.evc );
403410
// mohan update 2011-02-21
404411
//qianrui update 2020-10-17
405-
WF_io::write_wfc2(ssw.str(), GlobalC::wf.psi[0], GlobalC::pw.gcar);
412+
WF_io::write_wfc2(ssw.str(), this->psi[0], GlobalC::pw.gcar);
406413
//ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running,"write wave functions into file WAVEFUNC.dat");
407414
}
408415

@@ -414,9 +421,6 @@ namespace ModuleESolver
414421

415422
void ESolver_KS_PW::afterscf()
416423
{
417-
//temporary transform psi to evc
418-
// psi back to evc
419-
//GlobalC::wf.psi_transform_evc();
420424
for(int ik=0; ik<this->pelec->ekb.nr; ++ik)
421425
{
422426
for(int ib=0; ib<this->pelec->ekb.nc; ++ib)
@@ -595,17 +599,153 @@ namespace ModuleESolver
595599
void ESolver_KS_PW::cal_Force(ModuleBase::matrix& force)
596600
{
597601
Forces ff;
598-
ff.init(force);
602+
ff.init(force, this->psi);
599603
}
604+
600605
void ESolver_KS_PW::cal_Stress(ModuleBase::matrix& stress)
601606
{
602607
Stress_PW ss;
603-
ss.cal_stress(stress);
608+
ss.cal_stress(stress, this->psi);
604609
}
610+
605611
void ESolver_KS_PW::postprocess()
606612
{
607613
// compute density of states
608614
GlobalC::en.perform_dos_pw();
615+
616+
// caoyu add 2020-11-24, mohan updat 2021-01-03
617+
if(GlobalV::BASIS_TYPE=="pw" && GlobalV::deepks_out_labels)
618+
{
619+
Numerical_Descriptor nc;
620+
nc.output_descriptor(this->psi[0], INPUT.deepks_descriptor_lmax);
621+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running,"GENERATE DESCRIPTOR FOR DEEPKS");
622+
}
623+
624+
if(GlobalV::BASIS_TYPE=="pw" && winput::out_spillage) //xiaohui add 2013-09-01
625+
{
626+
//std::cout << "\n Output Spillage Information : " << std::endl;
627+
// calculate spillage value.
628+
#ifdef __LCAO
629+
if ( winput::out_spillage == 3)
630+
{
631+
GlobalV::BASIS_TYPE="pw";
632+
std::cout << " NLOCAL = " << GlobalV::NLOCAL << std::endl;
633+
634+
for (int ik=0; ik<GlobalC::kv.nks; ik++)
635+
{
636+
GlobalC::wf.wanf2[ik].create(GlobalV::NLOCAL, GlobalC::wf.npwx);
637+
if(GlobalV::BASIS_TYPE=="pw")
638+
{
639+
std::cout << " ik=" << ik + 1 << std::endl;
640+
641+
GlobalV::BASIS_TYPE="lcao_in_pw";
642+
GlobalC::wf.LCAO_in_pw_k(ik, GlobalC::wf.wanf2[ik]);
643+
GlobalV::BASIS_TYPE="pw";
644+
}
645+
}
646+
647+
//Spillage sp;
648+
//sp.get_both(GlobalV::NBANDS, GlobalV::NLOCAL, GlobalC::wf.wanf2, GlobalC::wf.evc);
649+
}
650+
#endif
651+
652+
// output overlap
653+
if ( winput::out_spillage <= 2 )
654+
{
655+
Numerical_Basis numerical_basis;
656+
numerical_basis.output_overlap(this->psi[0]);
657+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running,"BASIS OVERLAP (Q and S) GENERATION.");
658+
}
659+
}
660+
661+
if(GlobalC::wf.out_wfc_r == 1) // Peize Lin add 2021.11.21
662+
{
663+
Write_Wfc_Realspace::write_wfc_realspace_1(this->psi[0], "wfc_realspace", true);
664+
}
665+
}
666+
667+
void ESolver_KS_PW::hamilt2estates(const double ethr)
668+
{
669+
if(this->phsol != nullptr)
670+
{
671+
hsolver::DiagoIterAssist::need_subspace = false;
672+
hsolver::DiagoIterAssist::PW_DIAG_THR = ethr;
673+
this->phsol->solve(this->phami, this->psi[0], this->pelec, true);
674+
}
675+
else
676+
{
677+
ModuleBase::WARNING_QUIT("ESolver_KS_PW", "HSolver has not been initialed!");
678+
}
679+
}
680+
681+
void ESolver_KS_PW::nscf()
682+
{
683+
ModuleBase::TITLE("ESolver_KS_PW","nscf");
684+
ModuleBase::timer::tick("ESolver_KS_PW","nscf");
685+
686+
this->beforescf(1);
687+
//========================================
688+
// diagonalization of the KS hamiltonian
689+
// =======================================
690+
set_ethr(1, 1);
691+
692+
this->hamilt2estates(this->diag_ethr);
693+
694+
for(int ik=0; ik<this->pelec->ekb.nr; ++ik)
695+
{
696+
for(int ib=0; ib<this->pelec->ekb.nc; ++ib)
697+
{
698+
GlobalC::wf.ekb[ik][ib] = this->pelec->ekb(ik, ib);
699+
}
700+
}
701+
702+
GlobalV::ofs_running << "\n End of Band Structure Calculation \n" << std::endl;
703+
704+
705+
for (int ik = 0; ik < GlobalC::kv.nks; ik++)
706+
{
707+
if (GlobalV::NSPIN==2)
708+
{
709+
if (ik == 0) GlobalV::ofs_running << " spin up :" << std::endl;
710+
if (ik == ( GlobalC::kv.nks / 2)) GlobalV::ofs_running << " spin down :" << std::endl;
711+
}
712+
//out.printV3(GlobalV::ofs_running, GlobalC::kv.kvec_c[ik]);
713+
714+
GlobalV::ofs_running << " k-points" << ik+1
715+
<< "(" << GlobalC::kv.nkstot << "): "
716+
<< GlobalC::kv.kvec_c[ik].x
717+
<< " " << GlobalC::kv.kvec_c[ik].y
718+
<< " " << GlobalC::kv.kvec_c[ik].z << std::endl;
719+
720+
for (int ib = 0; ib < GlobalV::NBANDS; ib++)
721+
{
722+
GlobalV::ofs_running << " spin" << GlobalC::kv.isk[ik]+1
723+
<< "_final_band " << ib+1
724+
<< " " << this->pelec->ekb(ik, ib) * ModuleBase::Ry_to_eV
725+
<< " " << GlobalC::wf.wg(ik, ib)*GlobalC::kv.nks << std::endl;
726+
}
727+
GlobalV::ofs_running << std::endl;
728+
}
729+
730+
// add by jingan in 2018.11.7
731+
if(INPUT.towannier90)
732+
{
733+
toWannier90 myWannier(GlobalC::kv.nkstot,GlobalC::ucell.G);
734+
myWannier.init_wannier(this->psi);
735+
}
736+
737+
//=======================================================
738+
// Do a Berry phase polarization calculation if required
739+
//=======================================================
740+
741+
if (berryphase::berry_phase_flag && ModuleSymmetry::Symmetry::symm_flag == 0)
742+
{
743+
berryphase bp;
744+
bp.Macroscopic_polarization(this->psi);
745+
}
746+
747+
ModuleBase::timer::tick("ESolver_KS_PW","nscf");
748+
return;
609749
}
610750

611751
}

source/module_esolver/esolver_ks_pw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace ModuleESolver
1919
void cal_Force(ModuleBase::matrix& force) override;
2020
void cal_Stress(ModuleBase::matrix& stress) override;
2121
virtual void hamilt2density(const int istep, const int iter, const double ethr) override;
22+
virtual void hamilt2estates(const double ethr) override;
23+
virtual void nscf() override;
2224
void postprocess() override;
2325

2426
protected:

0 commit comments

Comments
 (0)