Skip to content

Commit

Permalink
Refactor: delete GlobalV::CURRENT_SPIN in code (#4301)
Browse files Browse the repository at this point in the history
Co-authored-by: dyzheng <zhengdy@bjaisi.com>
  • Loading branch information
dyzheng and dyzheng committed Jun 4, 2024
1 parent f280fa5 commit 7c5a670
Show file tree
Hide file tree
Showing 28 changed files with 82 additions and 108 deletions.
1 change: 0 additions & 1 deletion source/module_base/global_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ double XC_TEMPERATURE = 0.0;
int NSPIN = 1; // LDA
bool TWO_EFERMI = 0; // two fermi energy, exist only magnetization is fixed.
double nupdown = 0.0;
int CURRENT_SPIN = 0;
int CURRENT_K = 0;
int CAL_FORCE = 0; // if cal_force >1, means do the grid integration 'cal_force' times.
double FORCE_THR = 1.0e-3;
Expand Down
1 change: 0 additions & 1 deletion source/module_base/global_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ extern double XC_TEMPERATURE;
extern int NSPIN; // 7
extern bool TWO_EFERMI; // 7.5 two fermi energy, exist if nupdown isn't zero.
extern double nupdown;
extern int CURRENT_SPIN; // 8
extern int CURRENT_K; // 8

extern int CAL_FORCE; // 8.1
Expand Down
3 changes: 2 additions & 1 deletion source/module_elecstate/elecstate_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ void ElecStateLCAO<double>::print_psi(const psi::Psi<double>& psi_in, const int

// output but not do "2d-to-grid" conversion
double** wfc_grid = nullptr;
const int ik = psi_in.get_current_k();
#ifdef __MPI
this->lowf->wfc_2d_to_grid(istep, out_wfc_flag, psi_in.get_pointer(), wfc_grid, this->ekb, this->wg);
this->lowf->wfc_2d_to_grid(istep, out_wfc_flag, psi_in.get_pointer(), wfc_grid, ik, this->ekb, this->wg);
#endif
return;
}
Expand Down
2 changes: 1 addition & 1 deletion source/module_elecstate/elecstate_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void ElecState::print_band(const int& ik, const int& printe, const int& iter)
if (printe > 0 && ((iter + 1) % printe == 0))
{
GlobalV::ofs_running << std::setprecision(6);
GlobalV::ofs_running << " Energy (eV) & Occupations for spin=" << GlobalV::CURRENT_SPIN + 1
GlobalV::ofs_running << " Energy (eV) & Occupations for spin=" << this->klist->isk[ik]+1
<< " K-point=" << ik + 1 << std::endl;
GlobalV::ofs_running << std::setiosflags(std::ios::showpoint);
for (int ib = 0; ib < GlobalV::NBANDS; ib++)
Expand Down
1 change: 0 additions & 1 deletion source/module_elecstate/test/elecstate_print_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ TEST_F(ElecStatePrintTest, PrintEigenvalueS4)
TEST_F(ElecStatePrintTest, PrintBand)
{
GlobalV::NSPIN = 1;
GlobalV::CURRENT_SPIN = 0;
GlobalV::NBANDS = 2;
GlobalV::MY_RANK = 0;
GlobalV::ofs_running.open("test.dat", std::ios::out);
Expand Down
5 changes: 2 additions & 3 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_gamma_vl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ void Force_LCAO<double>::cal_fvl_dphi(
svl_dphi.zero_out();
for(int is=0; is<GlobalV::NSPIN; ++is)
{
GlobalV::CURRENT_SPIN = is;
const double* vr_eff1 = pot_in->get_effective_v(GlobalV::CURRENT_SPIN);
const double* vr_eff1 = pot_in->get_effective_v(is);
const double* vofk_eff1 = nullptr;
if(XC_Functional::get_func_type()==3 || XC_Functional::get_func_type()==5)
{
vofk_eff1 = pot_in->get_effective_vofk(GlobalV::CURRENT_SPIN);
vofk_eff1 = pot_in->get_effective_vofk(is);
}

if(XC_Functional::get_func_type()==3 || XC_Functional::get_func_type()==5)
Expand Down
6 changes: 2 additions & 4 deletions source/module_hamilt_lcao/hamilt_lcaodft/fvl_dphi_k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ void Force_LCAO<std::complex<double>>::cal_fvl_dphi(const bool isforce,

for (int is = 0; is < GlobalV::NSPIN; ++is)
{
GlobalV::CURRENT_SPIN = is;

const double* vr_eff1 = pot_in->get_effective_v(GlobalV::CURRENT_SPIN);
const double* vr_eff1 = pot_in->get_effective_v(is);
const double* vofk_eff1 = nullptr;
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
{
vofk_eff1 = pot_in->get_effective_vofk(GlobalV::CURRENT_SPIN);
vofk_eff1 = pot_in->get_effective_vofk(is);
}

//--------------------------------
Expand Down
6 changes: 3 additions & 3 deletions source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ void HamiltLCAO<TK, TR>::updateHk(const int ik)
if (GlobalV::NSPIN == 2)
{
// if Veff is added and current_spin is changed, refresh HR
if(GlobalV::VL_IN_H && this->kv->isk[ik] != GlobalV::CURRENT_SPIN)
if(GlobalV::VL_IN_H && this->kv->isk[ik] != this->current_spin)
{
// change data pointer of HR
this->hR->allocate(this->hRS2.data()+this->hRS2.size()/2*this->kv->isk[ik], 0);
Expand All @@ -481,7 +481,7 @@ void HamiltLCAO<TK, TR>::updateHk(const int ik)
dynamic_cast<hamilt::OperatorLCAO<TK, TR>*>(this->ops)->set_hr_done(false);
}
}
GlobalV::CURRENT_SPIN = this->kv->isk[ik];
this->current_spin = this->kv->isk[ik];
}
this->getOperator()->init(ik);
ModuleBase::timer::tick("HamiltLCAO", "updateHk");
Expand All @@ -495,7 +495,7 @@ void HamiltLCAO<TK, TR>::refresh()
if(GlobalV::NSPIN == 2)
{
this->refresh_times = 1;
GlobalV::CURRENT_SPIN = 0;
this->current_spin = 0;
if(this->hR->get_nnr() != this->hRS2.size()/2)
{
// operator has changed, resize hRS2
Expand Down
3 changes: 3 additions & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class HamiltLCAO : public Hamilt<TK>
std::vector<TR> hRS2;
int refresh_times = 1;

/// current_spin for NSPIN=2, 0: hamiltonian for spin up, 1: hamiltonian for spin down
int current_spin = 0;

// sk and hk will be refactored to HamiltLCAO later
//std::vector<TK> sk;
//std::vector<TK> hk;
Expand Down
13 changes: 7 additions & 6 deletions source/module_hamilt_lcao/hamilt_lcaodft/local_orbital_wfc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ void Local_Orbital_wfc::wfc_2d_to_grid(const int istep,
const int out_wfc_lcao,
const double* wfc_2d,
double** wfc_grid,
const int ik,
const ModuleBase::matrix& ekb,
const ModuleBase::matrix& wg)
{
Expand Down Expand Up @@ -294,33 +295,33 @@ void Local_Orbital_wfc::wfc_2d_to_grid(const int istep,
{
if (out_wfc_lcao == 1)
{
ss << GlobalV::global_out_dir << "LOWF_GAMMA_S" << GlobalV::CURRENT_SPIN + 1 << ".txt";
ss << GlobalV::global_out_dir << "LOWF_GAMMA_S" << ik + 1 << ".txt";
}
else if (out_wfc_lcao == 2)
{
ss << GlobalV::global_out_dir << "LOWF_GAMMA_S" << GlobalV::CURRENT_SPIN + 1 << ".dat";
ss << GlobalV::global_out_dir << "LOWF_GAMMA_S" << ik + 1 << ".dat";
}
}
else
{
if (out_wfc_lcao == 1)
{
ss << GlobalV::global_out_dir << istep << "_"
<< "LOWF_GAMMA_S" << GlobalV::CURRENT_SPIN + 1 << ".txt";
<< "LOWF_GAMMA_S" << ik + 1 << ".txt";
}
else if (out_wfc_lcao == 2)
{
ss << GlobalV::global_out_dir << istep << "_"
<< "LOWF_GAMMA_S" << GlobalV::CURRENT_SPIN + 1 << ".dat";
<< "LOWF_GAMMA_S" << ik + 1 << ".dat";
}
}
if (out_wfc_lcao == 1)
{
ModuleIO::write_wfc_nao(ss.str(), ctot, ekb, wg);
ModuleIO::write_wfc_nao(ss.str(), ctot, ik, ekb, wg);
}
else if (out_wfc_lcao == 2)
{
ModuleIO::write_wfc_nao(ss.str(), ctot, ekb, wg, true);
ModuleIO::write_wfc_nao(ss.str(), ctot, ik, ekb, wg, true);
}
for (int i = 0; i < GlobalV::NBANDS; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Local_Orbital_wfc
const int out_wfc_lcao,
const double* wfc_2d,
double** wfc_grid,
const int ik,
const ModuleBase::matrix& ekb,
const ModuleBase::matrix& wg);
void wfc_2d_to_grid(const int istep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ void Veff<OperatorLCAO<TK, TR>>::initialize_HR(const UnitCell* ucell_in,
ModuleBase::TITLE("Veff", "initialize_HR");
ModuleBase::timer::tick("Veff", "initialize_HR");

this->nspin = GlobalV::NSPIN;

for (int iat1 = 0; iat1 < ucell_in->nat; iat1++)
{
auto tau1 = ucell_in->get_tau(iat1);
Expand Down Expand Up @@ -64,15 +66,15 @@ void Veff<OperatorLCAO<TK, TR>>::contributeHR()
//(1) prepare data for this k point.
// copy the local potential from array.
//-----------------------------------------
double* vr_eff1 = this->pot->get_effective_v(GlobalV::CURRENT_SPIN);
double* vofk_eff1 = this->pot->get_effective_vofk(GlobalV::CURRENT_SPIN);
double* vr_eff1 = this->pot->get_effective_v(this->current_spin);
double* vofk_eff1 = this->pot->get_effective_vofk(this->current_spin);

//--------------------------------------------
//(2) check if we need to calculate
// pvpR = < phi0 | v(spin) | phiR> for a new spin.
//--------------------------------------------
// GlobalV::ofs_running << " (spin change)" << std::endl;
this->GK->reset_spin(GlobalV::CURRENT_SPIN);
this->GK->reset_spin(this->current_spin);

// if you change the place of the following code,
// rememeber to delete the #include
Expand All @@ -90,7 +92,7 @@ void Veff<OperatorLCAO<TK, TR>>::contributeHR()

// added by zhengdy-soc, for non-collinear case
// integral 4 times, is there any method to simplify?
if (GlobalV::NSPIN == 4)
if (this->nspin == 4)
{
for (int is = 1; is < 4; is++)
{
Expand All @@ -114,6 +116,8 @@ void Veff<OperatorLCAO<TK, TR>>::contributeHR()
}
this->GK->transfer_pvpR(this->hR,this->ucell,GlobalC::ORB,this->gd);

if(this->nspin == 2) this->current_spin = 1 - this->current_spin;

ModuleBase::timer::tick("Veff", "contributeHR");
return;
}
Expand All @@ -129,8 +133,8 @@ void Veff<OperatorLCAO<double, double>>::contributeHR(void)
//(1) prepare data for this k point.
// copy the local potential from array.
//-----------------------------------------
const double* vr_eff1 = this->pot->get_effective_v(GlobalV::CURRENT_SPIN);
const double* vofk_eff1 = this->pot->get_effective_vofk(GlobalV::CURRENT_SPIN);
const double* vr_eff1 = this->pot->get_effective_v(this->current_spin);
const double* vofk_eff1 = this->pot->get_effective_vofk(this->current_spin);

//--------------------------------------------
// (3) folding matrix,
Expand All @@ -150,6 +154,9 @@ void Veff<OperatorLCAO<double, double>>::contributeHR(void)
this->GG->transfer_pvpR(this->hR,this->ucell,GlobalC::ORB);

this->new_e_iteration = false;

if(this->nspin == 2) this->current_spin = 1 - this->current_spin;

ModuleBase::timer::tick("Veff", "contributeHR");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class Veff<OperatorLCAO<TK, TR>> : public OperatorLCAO<TK, TR>

elecstate::Potential* pot = nullptr;

int nspin = 1;
int current_spin = 0;

/**
* @brief initialize HR, search the nearest neighbor atoms
* HContainer is used to store the electronic kinetic matrix with specific <I,J,R> atom-pairs
Expand Down
5 changes: 2 additions & 3 deletions source/module_hamilt_pw/hamilt_pwdft/forces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,7 @@ void Forces<FPTYPE, Device>::cal_force_nl(ModuleBase::matrix& forcenl,

for (int ik = 0; ik < wfc_basis->nks; ik++)
{
if (GlobalV::NSPIN == 2)
GlobalV::CURRENT_SPIN = p_kv->isk[ik];
const int current_spin = p_kv->isk[ik];
const int nbasis = wfc_basis->npwk[ik];
// generate vkb
if (GlobalC::ppcell.nkb > 0)
Expand Down Expand Up @@ -1125,7 +1124,7 @@ void Forces<FPTYPE, Device>::cal_force_nl(ModuleBase::matrix& forcenl,
nbands_occ,
wg_nc,
GlobalC::ucell.ntype,
GlobalV::CURRENT_SPIN,
current_spin,
GlobalC::ppcell.deeq.getBound2(),
GlobalC::ppcell.deeq.getBound3(),
GlobalC::ppcell.deeq.getBound4(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ __global__ void cal_force_nl(
FPTYPE ekb_now = d_ekb[ik * wg_nc + ib];
for (int ia = 0; ia < atom_na[it]; ia++) {
for (int ip = threadIdx.x; ip < Nprojs; ip += blockDim.x) {
// FPTYPE ps = GlobalC::ppcell.deeq[GlobalV::CURRENT_SPIN, iat, ip, ip];
// FPTYPE ps = GlobalC::ppcell.deeq[spin, iat, ip, ip];
FPTYPE ps = deeq[((spin * deeq_2 + iat) * deeq_3 + ip) * deeq_4 + ip]
- ekb_now * qq_nt[it * deeq_3 * deeq_4 + ip * deeq_4 + ip];
const int inkb = sum + ip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ __global__ void cal_force_nl(
FPTYPE ekb_now = d_ekb[ik * wg_nc + ib];
for (int ia = 0; ia < atom_na[it]; ia++) {
for (int ip = threadIdx.x; ip < Nprojs; ip += blockDim.x) {
// FPTYPE ps = GlobalC::ppcell.deeq[GlobalV::CURRENT_SPIN, iat, ip, ip];
// FPTYPE ps = GlobalC::ppcell.deeq[spin, iat, ip, ip];
FPTYPE ps = deeq[((spin * deeq_2 + iat) * deeq_3 + ip) * deeq_4 + ip]
- ekb_now * qq_nt[it * deeq_3 * deeq_4 + ip * deeq_4 + ip];
const int inkb = sum + ip;
Expand Down
5 changes: 2 additions & 3 deletions source/module_hamilt_pw/hamilt_pwdft/stress_func_nl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ void Stress_Func<FPTYPE, Device>::stress_nl(ModuleBase::matrix& sigma,

for (int ik = 0; ik < p_kv->get_nks(); ik++)
{
if (GlobalV::NSPIN == 2)
GlobalV::CURRENT_SPIN = p_kv->isk[ik];
const int current_spin = p_kv->isk[ik];
const int npw = p_kv->ngk[ik];
// generate vkb
if (GlobalC::ppcell.nkb > 0)
Expand Down Expand Up @@ -246,7 +245,7 @@ void Stress_Func<FPTYPE, Device>::stress_nl(ModuleBase::matrix& sigma,
nkb,
nbands_occ,
GlobalC::ucell.ntype,
GlobalV::CURRENT_SPIN,
current_spin,
wg_nc,
ik,
GlobalC::ppcell.deeq.getBound2(),
Expand Down
7 changes: 3 additions & 4 deletions source/module_hamilt_pw/hamilt_stodft/sto_forces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ void Sto_Forces::cal_sto_force_nl(ModuleBase::matrix& forcenl,
// dbecp: conj( -iG * <Beta(nkb,npw)|psi(nbnd,npw)> )
ModuleBase::ComplexArray dbecp( 3, nbandstot, nkb);
ModuleBase::ComplexMatrix becp( nbandstot, nkb);
if (GlobalV::NSPIN == 2)
GlobalV::CURRENT_SPIN = p_kv->isk[ik];
const int current_spin = p_kv->isk[ik];

// generate vkb
if (GlobalC::ppcell.nkb > 0)
Expand Down Expand Up @@ -292,7 +291,7 @@ void Sto_Forces::cal_sto_force_nl(ModuleBase::matrix& forcenl,
{
for (int ip=0; ip<Nprojs; ip++)
{
double ps = GlobalC::ppcell.deeq( GlobalV::CURRENT_SPIN, iat, ip, ip) ;
double ps = GlobalC::ppcell.deeq( current_spin, iat, ip, ip) ;
const int inkb = sum + ip;
//out<<"\n ps = "<<ps;

Expand All @@ -316,7 +315,7 @@ void Sto_Forces::cal_sto_force_nl(ModuleBase::matrix& forcenl,
//if ( ip != ip2 )
//{
const int jnkb = sum + ip2;
double ps = GlobalC::ppcell.deeq( GlobalV::CURRENT_SPIN, iat, ip2, ip) ;
double ps = GlobalC::ppcell.deeq( current_spin, iat, ip2, ip) ;
for (int ipol=0; ipol<3; ipol++)
{
const double dbb = 2.0 * ( conj( dbecp( ipol, ib, inkb) ) * becp( ib, jnkb) ).real();
Expand Down
5 changes: 0 additions & 5 deletions source/module_hamilt_pw/hamilt_stodft/sto_iter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,6 @@ void Stochastic_Iter::calHsqrtchi(Stochastic_WF& stowf)
if(this->pkv->get_nks() > 1)
{

if(GlobalV::NSPIN==2)
{
GlobalV::CURRENT_SPIN = this->pkv->isk[ik];
}

if(GlobalC::ppcell.nkb > 0 && (GlobalV::BASIS_TYPE=="pw" || GlobalV::BASIS_TYPE=="lcao_in_pw")) //xiaohui add 2013-09-02. Attention...
{
GlobalC::ppcell.getvnl(ik, GlobalC::ppcell.vkb);
Expand Down
5 changes: 2 additions & 3 deletions source/module_hamilt_pw/hamilt_stodft/sto_stress_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ void Sto_Stress_PW::sto_stress_nl(ModuleBase::matrix& sigma,
ModuleBase::ComplexMatrix dbecp(nbandstot, nkb);
ModuleBase::ComplexMatrix becp(nbandstot, nkb);

if (GlobalV::NSPIN == 2)
GlobalV::CURRENT_SPIN = p_kv->isk[ik];
const int current_spin = p_kv->isk[ik];
// generate vkb
if (GlobalC::ppcell.nkb > 0)
{
Expand Down Expand Up @@ -413,7 +412,7 @@ void Sto_Stress_PW::sto_stress_nl(ModuleBase::matrix& sigma,
{
for (int ip = 0; ip < Nprojs; ++ip)
{
double ps = GlobalC::ppcell.deeq(GlobalV::CURRENT_SPIN, iat, ip, ip);
double ps = GlobalC::ppcell.deeq(current_spin, iat, ip, ip);
const int inkb = sum + ip;
// out<<"\n ps = "<<ps;

Expand Down
1 change: 0 additions & 1 deletion source/module_io/input_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ void Input_Conv::Convert(void)
GlobalV::DFT_FUNCTIONAL = INPUT.dft_functional;
GlobalV::XC_TEMPERATURE = INPUT.xc_temperature;
GlobalV::NSPIN = INPUT.nspin;
GlobalV::CURRENT_SPIN = 0;

GlobalV::CAL_FORCE = INPUT.cal_force;
GlobalV::FORCE_THR = INPUT.force_thr;
Expand Down
2 changes: 1 addition & 1 deletion source/module_io/istate_envelope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void IState_Envelope::begin(const psi::Psi<double>* psid,

psid->fix_k(is);
#ifdef __MPI
lowf.wfc_2d_to_grid(-1, 0, psid->get_pointer(), wfc_gamma_grid[is], this->pes->ekb, this->pes->wg);
lowf.wfc_2d_to_grid(-1, 0, psid->get_pointer(), wfc_gamma_grid[is], is, this->pes->ekb, this->pes->wg);
#else
for (int i = 0;i < nbands;++i)
{
Expand Down
1 change: 0 additions & 1 deletion source/module_io/test/input_conv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ TEST_F(InputConvTest, Conv)
EXPECT_EQ(GlobalV::DFT_FUNCTIONAL,"hse");
EXPECT_DOUBLE_EQ(GlobalV::XC_TEMPERATURE,0.0);
EXPECT_EQ(GlobalV::NSPIN,1);
EXPECT_EQ(GlobalV::CURRENT_SPIN,0);
EXPECT_EQ(GlobalV::CAL_FORCE,0);
EXPECT_NEAR(GlobalV::FORCE_THR,0.001,0.0000001);
EXPECT_DOUBLE_EQ(GlobalV::STRESS_THR, 0.01);
Expand Down
Loading

0 comments on commit 7c5a670

Please sign in to comment.