Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Removed GlobalVs related to module_io/istate_envelope.cpp #3801

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions source/module_esolver/esolver_ks_lcao_elec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,13 @@ void ESolver_KS_LCAO<TK, TR>::othercalculation(const int istep)
this->UHM.GG,
INPUT.out_wfc_pw,
this->wf.out_wfc_r,
this->kv);
this->kv,
GlobalV::nelec,
GlobalV::NBANDS_ISTATE,
GlobalV::NBANDS,
GlobalV::NSPIN,
GlobalV::NLOCAL,
GlobalV::global_out_dir);
else
IEP.begin(this->psi,
this->pw_rho,
Expand All @@ -418,7 +424,13 @@ void ESolver_KS_LCAO<TK, TR>::othercalculation(const int istep)
this->UHM.GK,
INPUT.out_wfc_pw,
this->wf.out_wfc_r,
this->kv);
this->kv,
GlobalV::nelec,
GlobalV::NBANDS_ISTATE,
GlobalV::NBANDS,
GlobalV::NSPIN,
GlobalV::NLOCAL,
GlobalV::global_out_dir);
}
else
{
Expand Down
96 changes: 54 additions & 42 deletions source/module_io/istate_envelope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
Gint_Gamma& gg,
int& out_wfc_pw,
int& out_wfc_r,
const K_Vectors& kv)
const K_Vectors& kv,
const double nelec,
const int nbands_istate,
const int nbands,
const int nspin,
const int nlocal,
const std::string& global_out_dir)
{
ModuleBase::TITLE("IState_Envelope", "begin");

Expand All @@ -32,11 +38,11 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
// if ucell is odd, it's correct,
// if ucell is even, it's also correct.
// +1.0e-8 in case like (2.999999999+1)/2
int fermi_band = static_cast<int>((GlobalV::nelec + 1) / 2 + 1.0e-8);
int bands_below = GlobalV::NBANDS_ISTATE;
int bands_above = GlobalV::NBANDS_ISTATE;
int fermi_band = static_cast<int>((nelec + 1) / 2 + 1.0e-8);
int bands_below = nbands_istate;
int bands_above = nbands_istate;

std::cout << " number of electrons = " << GlobalV::nelec << std::endl;
std::cout << " number of electrons = " << nelec << std::endl;
std::cout << " number of occupied bands = " << fermi_band << std::endl;
std::cout << " plot band decomposed charge density below fermi surface with "
<< bands_below << " bands." << std::endl;
Expand All @@ -53,9 +59,9 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
// get the charge density.

// (2.3) output the charge density in .cub format.
this->bands_picked = new bool[GlobalV::NBANDS];
ModuleBase::GlobalFunc::ZEROS(bands_picked, GlobalV::NBANDS);
for (int ib = 0; ib < GlobalV::NBANDS; ib++)
this->bands_picked = new bool[nbands];
ModuleBase::GlobalFunc::ZEROS(bands_picked, nbands);
for (int ib = 0; ib < nbands; ib++)
{
if (ib >= fermi_band - bands_below)
{
Expand All @@ -67,11 +73,11 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
}

//allocate grid wavefunction for gamma_only
std::vector<double**> wfc_gamma_grid(GlobalV::NSPIN);
for (int is = 0; is < GlobalV::NSPIN; ++is)
std::vector<double**> wfc_gamma_grid(nspin);
for (int is = 0; is < nspin; ++is)
{
wfc_gamma_grid[is] = new double* [GlobalV::NBANDS];
for (int ib = 0;ib < GlobalV::NBANDS; ++ib)
wfc_gamma_grid[is] = new double* [nbands];
for (int ib = 0;ib < nbands; ++ib)
wfc_gamma_grid[is][ib] = new double[gg.gridt->lgd];
}

Expand All @@ -80,15 +86,15 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,

if (out_wfc_pw || out_wfc_r)
{
pw_wfc_g.resize(1, GlobalV::NBANDS, kv.ngk[0]);
pw_wfc_g.resize(1, nbands, kv.ngk[0]);
}


for (int ib = 0; ib < GlobalV::NBANDS; ib++)
for (int ib = 0; ib < nbands; ib++)
{
if (bands_picked[ib])
{
for (int is = 0; is < GlobalV::NSPIN; ++is)
for (int is = 0; is < nspin; ++is)
{
std::cout << " Perform envelope function for band " << ib + 1 << std::endl;
ModuleBase::GlobalFunc::ZEROS(pes->charge->rho[is], wfcpw->nrxx);
Expand All @@ -97,9 +103,9 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
#ifdef __MPI
lowf.wfc_2d_to_grid(-1, 0, psid->get_pointer(), wfc_gamma_grid[is], this->pes->ekb, this->pes->wg);
#else
for (int i = 0;i < GlobalV::NBANDS;++i)
for (int i = 0;i < nbands;++i)
{
for (int j = 0;j < GlobalV::NLOCAL;++j)
for (int j = 0;j < nlocal;++j)
wfc_gamma_grid[is][i][j] = psid[0](i, j);
}
#endif
Expand All @@ -108,7 +114,7 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,

pes->charge->save_rho_before_sum_band(); //xiaohui add 2014-12-09
std::stringstream ss;
ss << GlobalV::global_out_dir << "BAND" << ib + 1 << "_s_" << is + 1 << "_ENV.cube";
ss << global_out_dir << "BAND" << ib + 1 << "_s_" << is + 1 << "_ENV.cube";
const double ef_tmp = this->pes->eferm.get_efval(is);
ModuleIO::write_rho(
#ifdef __MPI
Expand All @@ -119,7 +125,7 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
#endif
pes->charge->rho_save[is],
is,
GlobalV::NSPIN,
nspin,
0,
ss.str(),
rhopw->nx,
Expand All @@ -130,7 +136,7 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
3);

if (out_wfc_pw || out_wfc_r) //only for gamma_only now
this->set_pw_wfc(wfcpw, 0, ib, GlobalV::NSPIN,
this->set_pw_wfc(wfcpw, 0, ib, nspin,
pes->charge->rho_save, pw_wfc_g);
}
}
Expand All @@ -139,9 +145,9 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
if (out_wfc_pw)
{
std::stringstream ssw;
ssw << GlobalV::global_out_dir << "WAVEFUNC";
ssw << global_out_dir << "WAVEFUNC";
std::cout << " write G-space wavefunction into \"" <<
GlobalV::global_out_dir << "/" << ssw.str() << "\" files." << std::endl;
global_out_dir << "/" << ssw.str() << "\" files." << std::endl;
ModuleIO::write_wfc_pw(ssw.str(), pw_wfc_g, kv, wfcpw);
}
if (out_wfc_r)
Expand All @@ -150,9 +156,9 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,
}

delete[] bands_picked;
for (int is = 0; is < GlobalV::NSPIN; ++is)
for (int is = 0; is < nspin; ++is)
{
for (int ib = 0;ib < GlobalV::NBANDS; ++ib)
for (int ib = 0;ib < nbands; ++ib)
delete[] wfc_gamma_grid[is][ib];
delete[] wfc_gamma_grid[is];
}
Expand All @@ -167,7 +173,13 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
Gint_k& gk,
int& out_wf,
int& out_wf_r,
const K_Vectors& kv)
const K_Vectors& kv,
const double nelec,
const int nbands_istate,
const int nbands,
const int nspin,
const int nlocal,
const std::string& global_out_dir)
{
ModuleBase::TITLE("IState_Envelope", "begin");

Expand All @@ -179,11 +191,11 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
// if ucell is even, it's also correct.
// +1.0e-8 in case like (2.999999999+1)/2
// if NSPIN=4, each band only one electron, fermi_band should be nelec
int fermi_band = GlobalV::NSPIN<4 ? static_cast<int>((GlobalV::nelec + 1) / 2 + 1.0e-8) : GlobalV::nelec;
int bands_below = GlobalV::NBANDS_ISTATE;
int bands_above = GlobalV::NBANDS_ISTATE;
int fermi_band = nspin<4 ? static_cast<int>((nelec + 1) / 2 + 1.0e-8) : nelec;
int bands_below = nbands_istate;
int bands_above = nbands_istate;

std::cout << " number of electrons = " << GlobalV::nelec << std::endl;
std::cout << " number of electrons = " << nelec << std::endl;
std::cout << " number of occupied bands = " << fermi_band << std::endl;
std::cout << " plot band decomposed charge density below fermi surface with "
<< bands_below << " bands." << std::endl;
Expand All @@ -197,9 +209,9 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
// get the charge density.

// (2.3) output the charge density in .cub format.
this->bands_picked = new bool[GlobalV::NBANDS];
ModuleBase::GlobalFunc::ZEROS(bands_picked, GlobalV::NBANDS);
for (int ib = 0; ib < GlobalV::NBANDS; ib++)
this->bands_picked = new bool[nbands];
ModuleBase::GlobalFunc::ZEROS(bands_picked, nbands);
for (int ib = 0; ib < nbands; ib++)
{
if (ib >= fermi_band - bands_below)
{
Expand All @@ -215,14 +227,14 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,

if (out_wf || out_wf_r)
{
pw_wfc_g.resize(kv.nks, GlobalV::NBANDS, wfcpw->npwk_max);
pw_wfc_g.resize(kv.nks, nbands, wfcpw->npwk_max);
}

for (int ib = 0; ib < GlobalV::NBANDS; ib++)
for (int ib = 0; ib < nbands; ib++)
{
if (bands_picked[ib])
{
const int nspin0 = (GlobalV::NSPIN == 2) ? 2 : 1;
const int nspin0 = (nspin == 2) ? 2 : 1;
for (int ik = 0; ik < kv.nks; ++ik) //the loop of nspin0 is included
{
const int ispin = kv.isk[ik];
Expand All @@ -241,17 +253,17 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
this->pes->wg,
kv.kvec_c);
#else
for (int i = 0;i < GlobalV::NBANDS;++i)
for (int i = 0;i < nbands;++i)
{
for (int j = 0;j < GlobalV::NLOCAL;++j)
for (int j = 0;j < nlocal;++j)
lowf.wfc_k_grid[ik][i][j] = psi[0](i, j);
}
#endif
//deal with NSPIN=4
gk.cal_env_k(ik, lowf.wfc_k_grid[ik][ib], pes->charge->rho[ispin], kv.kvec_c, kv.kvec_d);

std::stringstream ss;
ss << GlobalV::global_out_dir << "BAND" << ib + 1 << "_k_" << ik / nspin0 + 1 << "_s_" << ispin + 1 << "_ENV.cube";
ss << global_out_dir << "BAND" << ib + 1 << "_k_" << ik / nspin0 + 1 << "_s_" << ispin + 1 << "_ENV.cube";
const double ef_tmp = this->pes->eferm.get_efval(ispin);
ModuleIO::write_rho(
#ifdef __MPI
Expand All @@ -262,7 +274,7 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
#endif
pes->charge->rho[ispin],
ispin,
GlobalV::NSPIN,
nspin,
0,
ss.str(),
rhopw->nx,
Expand All @@ -275,7 +287,7 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
if (out_wf || out_wf_r) //only for gamma_only now
{
pw_wfc_g.fix_k(ik);
this->set_pw_wfc(wfcpw, ik, ib, GlobalV::NSPIN,
this->set_pw_wfc(wfcpw, ik, ib, nspin,
pes->charge->rho, pw_wfc_g);
}
}
Expand All @@ -287,9 +299,9 @@ void IState_Envelope::begin(const psi::Psi<std::complex<double>>* psi,
if (out_wf)
{
std::stringstream ssw;
ssw << GlobalV::global_out_dir << "WAVEFUNC";
ssw << global_out_dir << "WAVEFUNC";
std::cout << " write G-space wavefunction into \"" <<
GlobalV::global_out_dir << "/" << ssw.str() << "\" files." << std::endl;
global_out_dir << "/" << ssw.str() << "\" files." << std::endl;
ModuleIO::write_wfc_pw(ssw.str(), pw_wfc_g, kv, wfcpw);
}
if (out_wf_r)
Expand Down
32 changes: 28 additions & 4 deletions source/module_io/istate_envelope.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ class IState_Envelope
Gint_Gamma& gg,
int& out_wfc_pw,
int& out_wfc_r,
const K_Vectors& kv);
const K_Vectors& kv,
const double nelec,
const int nbands_istate,
const int nbands,
const int nspin,
const int nlocal,
const std::string& global_out_dir);


/// tmp, delete after Gint is refactored.
Expand All @@ -36,7 +42,13 @@ class IState_Envelope
Gint_k& gg,
int& out_wfc_pw,
int& out_wfc_r,
const K_Vectors& kv) {
const K_Vectors& kv,
const double nelec,
const int nbands_istate,
const int nbands,
const int nspin,
const int nlocal,
const std::string& global_out_dir) {
throw std::logic_error("gint_k should use with complex psi.");
};
/// for multi-k
Expand All @@ -48,7 +60,13 @@ class IState_Envelope
Gint_k& gk,
int& out_wfc_pw,
int& out_wfc_r,
const K_Vectors& kv);
const K_Vectors& kv,
const double nelec,
const int nbands_istate,
const int nbands,
const int nspin,
const int nlocal,
const std::string& global_out_dir);

/// tmp, delete after Gint is refactored.
void begin(const psi::Psi<std::complex<double>>* psi,
Expand All @@ -59,7 +77,13 @@ class IState_Envelope
Gint_Gamma& gk,
int& out_wfc_pw,
int& out_wfc_r,
const K_Vectors& kv) {
const K_Vectors& kv,
const double nelec,
const int nbands_istate,
const int nbands,
const int nspin,
const int nlocal,
const std::string& global_out_dir) {
throw std::logic_error("gint_gamma should use with real psi.");
};

Expand Down