From 415b55f57189e64214e4b7e269b278f9304444f0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 15:30:30 +0800 Subject: [PATCH 01/60] update small places in charge_mixing_residual.cpp --- .../module_charge/charge_mixing_residual.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_residual.cpp b/source/source_estate/module_charge/charge_mixing_residual.cpp index 9e9c5e0131..8862ede35e 100644 --- a/source/source_estate/module_charge/charge_mixing_residual.cpp +++ b/source/source_estate/module_charge/charge_mixing_residual.cpp @@ -9,11 +9,13 @@ double Charge_Mixing::get_drho(Charge* chr, const double nelec) { ModuleBase::TITLE("Charge_Mixing", "get_drho"); ModuleBase::timer::tick("Charge_Mixing", "get_drho"); + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); double drho = 0.0; if (PARAM.inp.scf_thr_type == 1) { - for (int is = 0; is < PARAM.inp.nspin; ++is) + for (int is = 0; is < nspin; ++is) { ModuleBase::GlobalFunc::NOTE("Perform FFT on rho(r) to obtain rho(G)."); chr->rhopw->real2recip(chr->rho[is], chr->rhog[is]); @@ -23,15 +25,16 @@ double Charge_Mixing::get_drho(Charge* chr, const double nelec) } ModuleBase::GlobalFunc::NOTE("Calculate the charge difference between rho(G) and rho_save(G)"); - std::vector> drhog(PARAM.inp.nspin * this->rhopw->npw); + std::vector> drhog(nspin * this->rhopw->npw); #ifdef _OPENMP #pragma omp parallel for collapse(2) schedule(static, 512) #endif - for (int is = 0; is < PARAM.inp.nspin; ++is) + for (int is = 0; is < nspin; ++is) { + const int is_idx = is * this->rhopw->npw; for (int ig = 0; ig < this->rhopw->npw; ig++) { - drhog[is * rhopw->npw + ig] = chr->rhog[is][ig] - chr->rhog_save[is][ig]; + drhog[is_idx + ig] = chr->rhog[is][ig] - chr->rhog_save[is][ig]; } } @@ -42,7 +45,7 @@ double Charge_Mixing::get_drho(Charge* chr, const double nelec) { // Note: Maybe it is wrong. // The inner_product_real function (L1-norm) is different from that (L2-norm) in mixing. - for (int is = 0; is < PARAM.inp.nspin; is++) + for (int is = 0; is < nspin; is++) { if (is != 0 && is != 3 && PARAM.globalv.domag_z) { From 1156f2161b08b0bbca8314755c5f37f7fe359116 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 15:43:03 +0800 Subject: [PATCH 02/60] update charge_mixing_preconditioner --- .../charge_mixing_preconditioner.cpp | 90 +++++++++++++------ 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_preconditioner.cpp b/source/source_estate/module_charge/charge_mixing_preconditioner.cpp index 804705be99..165b967ec8 100644 --- a/source/source_estate/module_charge/charge_mixing_preconditioner.cpp +++ b/source/source_estate/module_charge/charge_mixing_preconditioner.cpp @@ -6,21 +6,32 @@ void Charge_Mixing::Kerker_screen_recip(std::complex* drhog) { - if (this->mixing_gg0 <= 0.0 || this->mixing_beta <= 0.1) { - return; -} + ModuleBase::TITLE("Charge_Mixing", "Kerker_screen_recip"); + + if (this->mixing_gg0 <= 0.0 || this->mixing_beta <= 0.1) + { + return; + } + + ModuleBase::timer::tick("Charge_Mixing", "Kerker_screen_recip"); + + const int nspin = PARAM.inp.nspin; + double fac = 0.0; double gg0 = 0.0; double amin = 0.0; /// consider a resize for mixing_angle - int resize_tmp = 1; - if (PARAM.inp.nspin == 4 && this->mixing_angle > 0) { resize_tmp = 2; -} + int resize_tmp = 1; + if (nspin == 4 && this->mixing_angle > 0) + { + resize_tmp = 2; + } /// implement Kerker for density and magnetization separately - for (int is = 0; is < PARAM.inp.nspin / resize_tmp; ++is) + for (int is = 0; is < nspin / resize_tmp; ++is) { + const int is_idx = is * this->rhopw->npw; /// new mixing method only support nspin=2 not nspin=4 if (is >= 1) { @@ -29,10 +40,10 @@ void Charge_Mixing::Kerker_screen_recip(std::complex* drhog) #ifdef __DEBUG assert(is == 1); // make sure break works #endif - double is_mag = PARAM.inp.nspin - 1; + double is_mag = nspin - 1; //for (int ig = 0; ig < this->rhopw->npw * is_mag; ig++) //{ - // drhog[is * this->rhopw->npw + ig] *= 1; + // drhog[is_idx + ig] *= 1; //} break; } @@ -49,29 +60,45 @@ void Charge_Mixing::Kerker_screen_recip(std::complex* drhog) #ifdef _OPENMP #pragma omp parallel for schedule(static, 512) #endif + const double gg0_amin = this->mixing_gg0_min / amin; + for (int ig = 0; ig < this->rhopw->npw; ++ig) { double gg = this->rhopw->gg[ig]; - double filter_g = std::max(gg / (gg + gg0), this->mixing_gg0_min / amin); - drhog[is * this->rhopw->npw + ig] *= filter_g; + double filter_g = std::max(gg / (gg + gg0), gg0_min); + drhog[is_idx + ig] *= filter_g; } } + + ModuleBase::timer::tick("Charge_Mixing", "Kerker_screen_recip"); return; } void Charge_Mixing::Kerker_screen_real(double* drhor) { - if (this->mixing_gg0 <= 0.0001 || this->mixing_beta <= 0.1) { - return; -} - /// consider a resize for mixing_angle + ModuleBase::TITLE("Charge_Mixing", "Kerker_screen_real"); + + if (this->mixing_gg0 <= 0.0001 || this->mixing_beta <= 0.1) + { + return; + } + + ModuleBase::timer::tick("Charge_Mixing", "Kerker_screen_real"); + + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); + + /// consider a resize for mixing_angle int resize_tmp = 1; - if (PARAM.inp.nspin == 4 && this->mixing_angle > 0) { resize_tmp = 2; -} + if (nspin == 4 && this->mixing_angle > 0) + { + resize_tmp = 2; + } - std::vector> drhog(this->rhopw->npw * PARAM.inp.nspin / resize_tmp); - std::vector drhor_filter(this->rhopw->nrxx * PARAM.inp.nspin / resize_tmp); - for (int is = 0; is < PARAM.inp.nspin / resize_tmp; ++is) + std::vector> drhog(this->rhopw->npw * nspin / resize_tmp); + std::vector drhor_filter(this->rhopw->nrxx * nspin / resize_tmp); + + for (int is = 0; is < nspin / resize_tmp; ++is) { // Note after this process some G which is higher than Gmax will be filtered. // Thus we cannot use Kerker_screen_recip(drhog.data()) directly after it. @@ -82,7 +109,7 @@ void Charge_Mixing::Kerker_screen_real(double* drhor) double gg0 = 0.0; double amin = 0.0; - for (int is = 0; is < PARAM.inp.nspin / resize_tmp; is++) + for (int is = 0; is < nspin / resize_tmp; is++) { if (is >= 1) @@ -92,8 +119,8 @@ void Charge_Mixing::Kerker_screen_real(double* drhor) #ifdef __DEBUG assert(is == 1); /// make sure break works #endif - double is_mag = PARAM.inp.nspin - 1; - if (PARAM.inp.nspin == 4 && this->mixing_angle > 0) { is_mag = 1; + double is_mag = nspin - 1; + if (nspin == 4 && this->mixing_angle > 0) { is_mag = 1; } for (int ig = 0; ig < this->rhopw->npw * is_mag; ig++) { @@ -114,21 +141,25 @@ void Charge_Mixing::Kerker_screen_real(double* drhor) #ifdef _OPENMP #pragma omp parallel for schedule(static, 512) #endif + + const int is_idx = is * this->rhopw->npw; + const double gg0_amin = this->mixing_gg0_min / amin; + for (int ig = 0; ig < this->rhopw->npw; ig++) { double gg = this->rhopw->gg[ig]; // I have not decided how to handle gg=0 part, will be changed in future //if (gg == 0) //{ - // drhog[is * this->rhopw->npw + ig] *= 0; + // drhog[is_idx + ig] *= 0; // continue; //} - double filter_g = std::max(gg / (gg + gg0), this->mixing_gg0_min / amin); - drhog[is * this->rhopw->npw + ig] *= (1 - filter_g); + double filter_g = std::max(gg / (gg + gg0), gg0_amin); + drhog[is_idx + ig] *= (1 - filter_g); } } /// inverse FT - for (int is = 0; is < PARAM.inp.nspin / resize_tmp; ++is) + for (int is = 0; is < nspin / resize_tmp; ++is) { this->rhopw->recip2real(drhog.data() + is * this->rhopw->npw, drhor_filter.data() + is * this->rhopw->nrxx); } @@ -136,8 +167,11 @@ void Charge_Mixing::Kerker_screen_real(double* drhor) #ifdef _OPENMP #pragma omp parallel for schedule(static, 512) #endif - for (int ir = 0; ir < this->rhopw->nrxx * PARAM.inp.nspin / resize_tmp; ir++) + for (int ir = 0; ir < this->rhopw->nrxx * nspin / resize_tmp; ir++) { drhor[ir] -= drhor_filter[ir]; } + + ModuleBase::timer::tick("Charge_Mixing", "Kerker_screen_real"); + return; } From 8d8803b5ee9d25289fa04d9fa7f504738930f90b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 15:50:20 +0800 Subject: [PATCH 03/60] add timers and remove some PARAM.inp.nspin --- .../module_charge/charge_mixing_rho.cpp | 103 +++++++++++------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_rho.cpp b/source/source_estate/module_charge/charge_mixing_rho.cpp index 6a965c5da2..38cd679f94 100644 --- a/source/source_estate/module_charge/charge_mixing_rho.cpp +++ b/source/source_estate/module_charge/charge_mixing_rho.cpp @@ -5,6 +5,12 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) { + ModuleBase::TITLE("Charge_Mixing", "mix_rho_recip"); + ModuleBase::timer::tick("Charge_Mixing", "mix_rho_recip"); + + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); + std::complex* rhog_in = nullptr; std::complex* rhog_out = nullptr; // for smooth part @@ -26,7 +32,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) = std::bind(&Charge_Mixing::inner_product_recip_hartree, this, std::placeholders::_1, std::placeholders::_2); // DIIS Mixing Only for smooth part, while high_frequency part is mixed by plain mixing method. - if (PARAM.inp.nspin == 1) + if (nspin == 1) { rhog_in = rhogs_in; rhog_out = rhogs_out; @@ -35,17 +41,17 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) this->mixing->cal_coef(this->rho_mdata, inner_product); this->mixing->mix_data(this->rho_mdata, rhog_out); } - else if (PARAM.inp.nspin == 2) + else if (nspin == 2) { // magnetic density std::complex *rhog_mag = nullptr; std::complex *rhog_mag_save = nullptr; const int npw = this->rhopw->npw; // allocate rhog_mag[is*ngmc] and rhog_mag_save[is*ngmc] - rhog_mag = new std::complex[npw * PARAM.inp.nspin]; - rhog_mag_save = new std::complex[npw * PARAM.inp.nspin]; - ModuleBase::GlobalFunc::ZEROS(rhog_mag, npw * PARAM.inp.nspin); - ModuleBase::GlobalFunc::ZEROS(rhog_mag_save, npw * PARAM.inp.nspin); + rhog_mag = new std::complex[npw * nspin]; + rhog_mag_save = new std::complex[npw * nspin]; + ModuleBase::GlobalFunc::ZEROS(rhog_mag, npw * nspin); + ModuleBase::GlobalFunc::ZEROS(rhog_mag_save, npw * nspin); // get rhog_mag[is*ngmc] and rhog_mag_save[is*ngmc] for (int ig = 0; ig < npw; ig++) { @@ -84,7 +90,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) this->mixing->cal_coef(this->rho_mdata, inner_product); this->mixing->mix_data(this->rho_mdata, rhog_out); // get rhog[is][ngmc] from rhog_mag[is*ngmc] - for (int is = 0; is < PARAM.inp.nspin; is++) + for (int is = 0; is < nspin; is++) { ModuleBase::GlobalFunc::ZEROS(chr->rhog[is], npw); } @@ -106,7 +112,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } } } - else if (PARAM.inp.nspin == 4 && PARAM.inp.mixing_angle <= 0) + else if (nspin == 4 && PARAM.inp.mixing_angle <= 0) { // normal broyden mixing for {rho, mx, my, mz} rhog_in = rhogs_in; @@ -135,7 +141,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) this->mixing->cal_coef(this->rho_mdata, inner_product); this->mixing->mix_data(this->rho_mdata, rhog_out); } - else if (PARAM.inp.nspin == 4 && PARAM.inp.mixing_angle > 0) + else if (nspin == 4 && PARAM.inp.mixing_angle > 0) { // special broyden mixing for {rho, |m|} proposed by J. Phys. Soc. Jpn. 82 (2013) 114706 // here only consider the case of mixing_angle = 1, which mean only change |m| and keep angle fixed @@ -154,9 +160,13 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) for (int ir = 0; ir < nrxx; ir++) { // |m| for rho - rho_magabs[ir] = std::sqrt(chr->rho[1][ir] * chr->rho[1][ir] + chr->rho[2][ir] * chr->rho[2][ir] + chr->rho[3][ir] * chr->rho[3][ir]); + rho_magabs[ir] = std::sqrt(chr->rho[1][ir] * chr->rho[1][ir] + + chr->rho[2][ir] * chr->rho[2][ir] + + chr->rho[3][ir] * chr->rho[3][ir]); // |m| for rho_save - rho_magabs_save[ir] = std::sqrt(chr->rho_save[1][ir] * chr->rho_save[1][ir] + chr->rho_save[2][ir] * chr->rho_save[2][ir] + chr->rho_save[3][ir] * chr->rho_save[3][ir]); + rho_magabs_save[ir] = std::sqrt(chr->rho_save[1][ir] * chr->rho_save[1][ir] + + chr->rho_save[2][ir] * chr->rho_save[2][ir] + + chr->rho_save[3][ir] * chr->rho_save[3][ir]); } // allocate memory for rhog_magabs and rhog_magabs_save const int npw = this->rhopw->npw; @@ -203,10 +213,14 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) // use new |m| and angle to update {mx, my, mz} for (int ig = 0; ig < npw; ig++) { - chr->rhog[0][ig] = rhog_magabs[ig]; // rhog - double norm = std::sqrt(chr->rho[1][ig] * chr->rho[1][ig] + chr->rho[2][ig] * chr->rho[2][ig] + chr->rho[3][ig] * chr->rho[3][ig]); - if (std::abs(norm) < 1e-10) { continue; -} + chr->rhog[0][ig] = rhog_magabs[ig]; // rhog + double norm = std::sqrt(chr->rho[1][ig] * chr->rho[1][ig] + + chr->rho[2][ig] * chr->rho[2][ig] + + chr->rho[3][ig] * chr->rho[3][ig]); + if (std::abs(norm) < 1e-10) + { + continue; + } double rescale_tmp = rho_magabs[npw + ig] / norm; chr->rho[1][ig] *= rescale_tmp; chr->rho[2][ig] *= rescale_tmp; @@ -222,7 +236,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) if ( PARAM.globalv.double_grid) { // plain mixing for high_frequencies - const int ndimhf = (this->rhodpw->npw - this->rhopw->npw) * PARAM.inp.nspin; + const int ndimhf = (this->rhodpw->npw - this->rhopw->npw) * nspin; this->mixing_highf->plain_mix(rhoghf_out, rhoghf_in, rhoghf_out, ndimhf, nullptr); // combine smooth part and high_frequency part @@ -231,7 +245,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } // rhog to rho - if (PARAM.inp.nspin == 4 && PARAM.inp.mixing_angle > 0) + if (nspin == 4 && PARAM.inp.mixing_angle > 0) { // only tranfer rhog[0] // do not support double_grid, use rhopw directly @@ -239,7 +253,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } else { - for (int is = 0; is < PARAM.inp.nspin; is++) + for (int is = 0; is < nspin; is++) { // use rhodpw for double_grid // rhodpw is the same as rhopw for ! PARAM.globalv.double_grid @@ -249,10 +263,10 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) // For kinetic energy density if ((XC_Functional::get_ked_flag()) && mixing_tau) { - std::vector> kin_g(PARAM.inp.nspin * rhodpw->npw); - std::vector> kin_g_save(PARAM.inp.nspin * rhodpw->npw); + std::vector> kin_g(nspin * rhodpw->npw); + std::vector> kin_g_save(nspin * rhodpw->npw); // FFT to get kin_g and kin_g_save - for (int is = 0; is < PARAM.inp.nspin; ++is) + for (int is = 0; is < nspin; ++is) { rhodpw->real2recip(chr->kin_r[is], &kin_g[is * rhodpw->npw]); rhodpw->real2recip(chr->kin_r_save[is], &kin_g_save[is * rhodpw->npw]); @@ -277,7 +291,7 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) if ( PARAM.globalv.double_grid) { // simple mixing for high_frequencies - const int ndimhf = (this->rhodpw->npw - this->rhopw->npw) * PARAM.inp.nspin; + const int ndimhf = (this->rhodpw->npw - this->rhopw->npw) * nspin; this->mixing_highf->plain_mix(taughf_out, taughf_in, taughf_out, ndimhf, nullptr); // combine smooth part and high_frequency part @@ -286,22 +300,28 @@ void Charge_Mixing::mix_rho_recip(Charge* chr) } // kin_g to kin_r - for (int is = 0; is < PARAM.inp.nspin; is++) + for (int is = 0; is < nspin; is++) { rhodpw->recip2real(&kin_g[is * rhodpw->npw], chr->kin_r[is]); } } - + ModuleBase::timer::tick("Charge_Mixing", "mix_rho_recip"); return; } void Charge_Mixing::mix_rho_real(Charge* chr) { + ModuleBase::TITLE("Charge_Mixing", "mix_rho_real"); + ModuleBase::timer::tick("Charge_Mixing", "mix_rho_real"); + + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); + double* rhor_in=nullptr; double* rhor_out=nullptr; - if (PARAM.inp.nspin == 1) + if (nspin == 1) { rhor_in = chr->rho_save[0]; rhor_out = chr->rho[0]; @@ -312,17 +332,17 @@ void Charge_Mixing::mix_rho_real(Charge* chr) this->mixing->cal_coef(this->rho_mdata, inner_product); this->mixing->mix_data(this->rho_mdata, rhor_out); } - else if (PARAM.inp.nspin == 2) + else if (nspin == 2) { // magnetic density double *rho_mag = nullptr; double *rho_mag_save = nullptr; const int nrxx = this->rhopw->nrxx; // allocate rho_mag[is*nnrx] and rho_mag_save[is*nnrx] - rho_mag = new double[nrxx * PARAM.inp.nspin]; - rho_mag_save = new double[nrxx * PARAM.inp.nspin]; - ModuleBase::GlobalFunc::ZEROS(rho_mag, nrxx * PARAM.inp.nspin); - ModuleBase::GlobalFunc::ZEROS(rho_mag_save, nrxx * PARAM.inp.nspin); + rho_mag = new double[nrxx * nspin]; + rho_mag_save = new double[nrxx * nspin]; + ModuleBase::GlobalFunc::ZEROS(rho_mag, nrxx * nspin); + ModuleBase::GlobalFunc::ZEROS(rho_mag_save, nrxx * nspin); // get rho_mag[is*nnrx] and rho_mag_save[is*nnrx] for (int ir = 0; ir < nrxx; ir++) { @@ -362,7 +382,7 @@ void Charge_Mixing::mix_rho_real(Charge* chr) this->mixing->cal_coef(this->rho_mdata, inner_product); this->mixing->mix_data(this->rho_mdata, rhor_out); // get new rho[is][nrxx] from rho_mag[is*nrxx] - for (int is = 0; is < PARAM.inp.nspin; is++) + for (int is = 0; is < nspin; is++) { ModuleBase::GlobalFunc::ZEROS(chr->rho[is], nrxx); //ModuleBase::GlobalFunc::ZEROS(rho_save[is], nrxx); @@ -376,7 +396,7 @@ void Charge_Mixing::mix_rho_real(Charge* chr) delete[] rho_mag; delete[] rho_mag_save; } - else if (PARAM.inp.nspin == 4 && PARAM.inp.mixing_angle <= 0) + else if (nspin == 4 && PARAM.inp.mixing_angle <= 0) { // normal broyden mixing for {rho, mx, my, mz} rhor_in = chr->rho_save[0]; @@ -407,7 +427,7 @@ void Charge_Mixing::mix_rho_real(Charge* chr) this->mixing->cal_coef(this->rho_mdata, inner_product); this->mixing->mix_data(this->rho_mdata, rhor_out); } - else if (PARAM.inp.nspin == 4 && PARAM.inp.mixing_angle > 0) + else if (nspin == 4 && PARAM.inp.mixing_angle > 0) { // special broyden mixing for {rho, |m|} proposed by J. Phys. Soc. Jpn. 82 (2013) 114706 // here only consider the case of mixing_angle = 1, which mean only change |m| and keep angle fixed @@ -494,6 +514,8 @@ void Charge_Mixing::mix_rho_real(Charge* chr) this->mixing->mix_data(this->tau_mdata, taur_out); } + ModuleBase::timer::tick("Charge_Mixing", "mix_rho_real"); + return; } @@ -502,10 +524,13 @@ void Charge_Mixing::mix_rho(Charge* chr) ModuleBase::TITLE("Charge_Mixing", "mix_rho"); ModuleBase::timer::tick("Charge_Mixing", "mix_rho"); + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); + // the charge before mixing. const int nrxx = chr->rhopw->nrxx; - std::vector rho123(PARAM.inp.nspin * nrxx); - for (int is = 0; is < PARAM.inp.nspin; ++is) + std::vector rho123(nspin * nrxx); + for (int is = 0; is < nspin; ++is) { if (is == 0 || is == 3 || !PARAM.globalv.domag_z) { @@ -522,8 +547,8 @@ void Charge_Mixing::mix_rho(Charge* chr) std::vector kin_r123; if ((XC_Functional::get_ked_flag()) && mixing_tau) { - kin_r123.resize(PARAM.inp.nspin * nrxx); - for (int is = 0; is < PARAM.inp.nspin; ++is) + kin_r123.resize(nspin * nrxx); + for (int is = 0; is < nspin; ++is) { double* kin_r123_is = kin_r123.data() + is * nrxx; #ifdef _OPENMP @@ -548,7 +573,7 @@ void Charge_Mixing::mix_rho(Charge* chr) // mohan add 2012-06-05 // rho_save is the charge before mixing - for (int is = 0; is < PARAM.inp.nspin; ++is) + for (int is = 0; is < nspin; ++is) { if (is == 0 || is == 3 || !PARAM.globalv.domag_z) { @@ -565,7 +590,7 @@ void Charge_Mixing::mix_rho(Charge* chr) if ((XC_Functional::get_ked_flag()) && mixing_tau) { - for (int is = 0; is < PARAM.inp.nspin; ++is) + for (int is = 0; is < nspin; ++is) { double* kin_r123_is = kin_r123.data() + is * nrxx; #ifdef _OPENMP From 91b9ceb20c220b092a075e361aea585b7b751f61 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 15:59:41 +0800 Subject: [PATCH 04/60] fix problems --- .../module_charge/charge_mixing_preconditioner.cpp | 14 +++++++------- .../module_charge/charge_mixing_residual.cpp | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/source/source_estate/module_charge/charge_mixing_preconditioner.cpp b/source/source_estate/module_charge/charge_mixing_preconditioner.cpp index 165b967ec8..00b5d42038 100644 --- a/source/source_estate/module_charge/charge_mixing_preconditioner.cpp +++ b/source/source_estate/module_charge/charge_mixing_preconditioner.cpp @@ -57,15 +57,16 @@ void Charge_Mixing::Kerker_screen_recip(std::complex* drhog) } gg0 = std::pow(fac * 0.529177 / *this->tpiba, 2); + + const double gg0_amin = this->mixing_gg0_min / amin; + #ifdef _OPENMP #pragma omp parallel for schedule(static, 512) #endif - const double gg0_amin = this->mixing_gg0_min / amin; - for (int ig = 0; ig < this->rhopw->npw; ++ig) { double gg = this->rhopw->gg[ig]; - double filter_g = std::max(gg / (gg + gg0), gg0_min); + double filter_g = std::max(gg / (gg + gg0), gg0_amin); drhog[is_idx + ig] *= filter_g; } } @@ -138,13 +139,12 @@ void Charge_Mixing::Kerker_screen_real(double* drhor) } gg0 = std::pow(fac * 0.529177 / *this->tpiba, 2); -#ifdef _OPENMP -#pragma omp parallel for schedule(static, 512) -#endif const int is_idx = is * this->rhopw->npw; const double gg0_amin = this->mixing_gg0_min / amin; - +#ifdef _OPENMP +#pragma omp parallel for schedule(static, 512) +#endif for (int ig = 0; ig < this->rhopw->npw; ig++) { double gg = this->rhopw->gg[ig]; diff --git a/source/source_estate/module_charge/charge_mixing_residual.cpp b/source/source_estate/module_charge/charge_mixing_residual.cpp index 8862ede35e..bbc7c7b6be 100644 --- a/source/source_estate/module_charge/charge_mixing_residual.cpp +++ b/source/source_estate/module_charge/charge_mixing_residual.cpp @@ -31,10 +31,9 @@ double Charge_Mixing::get_drho(Charge* chr, const double nelec) #endif for (int is = 0; is < nspin; ++is) { - const int is_idx = is * this->rhopw->npw; for (int ig = 0; ig < this->rhopw->npw; ig++) { - drhog[is_idx + ig] = chr->rhog[is][ig] - chr->rhog_save[is][ig]; + drhog[is * this->rhopw->npw + ig] = chr->rhog[is][ig] - chr->rhog_save[is][ig]; } } From a7f629459de6162a1252278c9dc9f74a48eff8d1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 16:09:03 +0800 Subject: [PATCH 05/60] add timers --- .../source_cell/module_symmetry/symm_rho.cpp | 4 ++ .../module_charge/symmetry_rho.cpp | 57 ++++++++++++------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/source/source_cell/module_symmetry/symm_rho.cpp b/source/source_cell/module_symmetry/symm_rho.cpp index 754279ffbc..28ae30d5b4 100644 --- a/source/source_cell/module_symmetry/symm_rho.cpp +++ b/source/source_cell/module_symmetry/symm_rho.cpp @@ -9,6 +9,10 @@ void Symmetry::rho_symmetry( double *rho, { ModuleBase::timer::tick("Symmetry","rho_symmetry"); + assert(nr1>0); + assert(nr2>0); + assert(nr3>0); + // allocate flag for each FFT grid. bool* symflag = new bool[nr1 * nr2 * nr3]; for (int i=0; ireal2recip(CHR.rho[spin_now], CHR.rhog[spin_now]); - psymmg(CHR.rhog[spin_now], rho_basis, symm); // need to modify - rho_basis->recip2real(CHR.rhog[spin_now], CHR.rho[spin_now]); - if (XC_Functional::get_ked_flag() || CHR.cal_elf) + rho_basis->real2recip(chr.rho[spin_now], chr.rhog[spin_now]); + + psymmg(chr.rhog[spin_now], rho_basis, symm); // need to modify + + rho_basis->recip2real(chr.rhog[spin_now], chr.rho[spin_now]); + + if (XC_Functional::get_ked_flag() || chr.cal_elf) { // Use std::vector to manage kin_g instead of raw pointer - std::vector> kin_g(CHR.ngmc); - rho_basis->real2recip(CHR.kin_r[spin_now], kin_g.data()); + std::vector> kin_g(chr.ngmc); + rho_basis->real2recip(chr.kin_r[spin_now], kin_g.data()); psymmg(kin_g.data(), rho_basis, symm); - rho_basis->recip2real(kin_g.data(), CHR.kin_r[spin_now]); - } + rho_basis->recip2real(kin_g.data(), chr.kin_r[spin_now]); } + + ModuleBase::timer::tick("Symmetry_rho","begin"); return; } @@ -59,6 +67,10 @@ void Symmetry_rho::begin(const int& spin_now, { return; } + + ModuleBase::TITLE("Symmetry_rho", "begin"); + ModuleBase::timer::tick("Symmetry_rho","begin"); + // both parallel and serial // if(symm.nrot==symm.nrotk) //pure point-group, do rho_symm in real space // { @@ -81,6 +93,8 @@ void Symmetry_rho::begin(const int& spin_now, rho_basis->recip2real(kin_g.data(), kin_r[spin_now]); } } + + ModuleBase::timer::tick("Symmetry_rho","begin"); return; } @@ -89,8 +103,11 @@ void Symmetry_rho::psymm(double* rho_part, Parallel_Grid& Pgrid, ModuleSymmetry::Symmetry& symm) const { + ModuleBase::TITLE("Symmetry_rho", "psymm"); + ModuleBase::timer::tick("Symmetry_rho","psymm"); + #ifdef __MPI - // (1) reduce all rho from the first pool. + // reduce all rho from the first pool. std::vector rhotot; if (GlobalV::MY_RANK == 0) { @@ -99,7 +116,6 @@ void Symmetry_rho::psymm(double* rho_part, } Pgrid.reduce(rhotot.data(), rho_part, false); - // (2) if (GlobalV::MY_RANK == 0) { symm.rho_symmetry(rhotot.data(), rho_basis->nx, rho_basis->ny, rho_basis->nz); @@ -126,8 +142,9 @@ void Symmetry_rho::psymm(double* rho_part, #ifdef __MPI } - // (3) -Pgrid.bcast(rhotot.data(), rho_part, GlobalV::MY_RANK); + Pgrid.bcast(rhotot.data(), rho_part, GlobalV::MY_RANK); #endif + + ModuleBase::timer::tick("Symmetry_rho","psymm"); return; -} \ No newline at end of file +} From 9f957336051341586992c1251afe3b2a846ffb79 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 16:34:51 +0800 Subject: [PATCH 06/60] small fix --- source/source_esolver/esolver_of_tool.cpp | 2 +- .../module_pot/potential_new.cpp | 56 +++++++++++-------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/source/source_esolver/esolver_of_tool.cpp b/source/source_esolver/esolver_of_tool.cpp index a71b128cf3..50c0ff6561 100644 --- a/source/source_esolver/esolver_of_tool.cpp +++ b/source/source_esolver/esolver_of_tool.cpp @@ -220,7 +220,7 @@ double ESolver_OF::cal_mu(double* pphi, double* pdEdphi, double nelec) * @brief Rotate and renormalize the direction |d>, * make it orthogonal to phi ( = 0), and = nelec */ -void ESolver_OF::adjust_direction() +void ESolver_OF::adjust_direction(void) { // filter the high frequency term in direction if of_full_pw = false if (!PARAM.inp.of_full_pw) diff --git a/source/source_estate/module_pot/potential_new.cpp b/source/source_estate/module_pot/potential_new.cpp index 3d958bc87e..b1968339aa 100644 --- a/source/source_estate/module_pot/potential_new.cpp +++ b/source/source_estate/module_pot/potential_new.cpp @@ -23,7 +23,8 @@ Potential::Potential(const ModulePW::PW_Basis* rho_basis_in, double* etxc_in, double* vtxc_in, VSep* vsep_cell_in) - : ucell_(ucell_in), vloc_(vloc_in), structure_factors_(structure_factors_in), solvent_(solvent_in), vsep_cell(vsep_cell_in), etxc_(etxc_in), + : ucell_(ucell_in), vloc_(vloc_in), structure_factors_(structure_factors_in), + solvent_(solvent_in), vsep_cell(vsep_cell_in), etxc_(etxc_in), vtxc_(vtxc_in) { this->rho_basis_ = rho_basis_in; @@ -81,7 +82,6 @@ void Potential::pot_register(const std::vector& components_list) { PotBase* tmp = this->get_pot_type(comp); this->components.push_back(tmp); - // GlobalV::ofs_running << "Successful completion of Potential's registration : " << comp << std::endl; } // after register, reset fixed_done to false @@ -93,8 +93,13 @@ void Potential::pot_register(const std::vector& components_list) void Potential::allocate() { ModuleBase::TITLE("Potential", "allocate"); - int nrxx = this->rho_basis_->nrxx; - int nrxx_smooth = this->rho_basis_smooth_->nrxx; + + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); + + const int nrxx = this->rho_basis_->nrxx; + const int nrxx_smooth = this->rho_basis_smooth_->nrxx; + if (nrxx == 0) { return; @@ -107,39 +112,39 @@ void Potential::allocate() this->v_effective_fixed.resize(nrxx); ModuleBase::Memory::record("Pot::veff_fix", sizeof(double) * nrxx); - this->v_effective.create(PARAM.inp.nspin, nrxx); - ModuleBase::Memory::record("Pot::veff", sizeof(double) * PARAM.inp.nspin * nrxx); + this->v_effective.create(nspin, nrxx); + ModuleBase::Memory::record("Pot::veff", sizeof(double) * nspin * nrxx); - this->veff_smooth.create(PARAM.inp.nspin, nrxx_smooth); - ModuleBase::Memory::record("Pot::veff_smooth", sizeof(double) * PARAM.inp.nspin * nrxx_smooth); + this->veff_smooth.create(nspin, nrxx_smooth); + ModuleBase::Memory::record("Pot::veff_smooth", sizeof(double) * nspin * nrxx_smooth); if (XC_Functional::get_ked_flag()) { - this->vofk_effective.create(PARAM.inp.nspin, nrxx); - ModuleBase::Memory::record("Pot::vofk", sizeof(double) * PARAM.inp.nspin * nrxx); + this->vofk_effective.create(nspin, nrxx); + ModuleBase::Memory::record("Pot::vofk", sizeof(double) * nspin * nrxx); - this->vofk_smooth.create(PARAM.inp.nspin, nrxx_smooth); - ModuleBase::Memory::record("Pot::vofk_smooth", sizeof(double) * PARAM.inp.nspin * nrxx_smooth); + this->vofk_smooth.create(nspin, nrxx_smooth); + ModuleBase::Memory::record("Pot::vofk_smooth", sizeof(double) * nspin * nrxx_smooth); } if (use_gpu_) { if (PARAM.globalv.has_float_data) { - resmem_sd_op()(s_veff_smooth, PARAM.inp.nspin * nrxx_smooth); - resmem_sd_op()(s_vofk_smooth, PARAM.inp.nspin * nrxx_smooth); + resmem_sd_op()(s_veff_smooth, nspin * nrxx_smooth); + resmem_sd_op()(s_vofk_smooth, nspin * nrxx_smooth); } if (PARAM.globalv.has_double_data) { - resmem_dd_op()(d_veff_smooth, PARAM.inp.nspin * nrxx_smooth); - resmem_dd_op()(d_vofk_smooth, PARAM.inp.nspin * nrxx_smooth); + resmem_dd_op()(d_veff_smooth, nspin * nrxx_smooth); + resmem_dd_op()(d_vofk_smooth, nspin * nrxx_smooth); } } else { if (PARAM.globalv.has_float_data) { - resmem_sh_op()(s_veff_smooth, PARAM.inp.nspin * nrxx_smooth, "POT::sveff_smooth"); - resmem_sh_op()(s_vofk_smooth, PARAM.inp.nspin * nrxx_smooth, "POT::svofk_smooth"); + resmem_sh_op()(s_veff_smooth, nspin * nrxx_smooth, "POT::sveff_smooth"); + resmem_sh_op()(s_vofk_smooth, nspin * nrxx_smooth, "POT::svofk_smooth"); } if (PARAM.globalv.has_double_data) { @@ -273,20 +278,23 @@ void Potential::get_vnew(const Charge* chg, ModuleBase::matrix& vnew) return; } -void Potential::interpolate_vrs() +void Potential::interpolate_vrs(void) { ModuleBase::TITLE("Potential", "interpolate_vrs"); ModuleBase::timer::tick("Potential", "interpolate_vrs"); - if ( PARAM.globalv.double_grid) + const int nspin = PARAM.inp.nspin; + assert(nspin==1 || nspin==2 || nspin==4); + + if (PARAM.globalv.double_grid) { if (rho_basis_->gamma_only != rho_basis_smooth_->gamma_only) { ModuleBase::WARNING_QUIT("Potential::interpolate_vrs", "gamma_only is not consistent"); } - ModuleBase::ComplexMatrix vrs(PARAM.inp.nspin, rho_basis_->npw); - for (int is = 0; is < PARAM.inp.nspin; is++) + ModuleBase::ComplexMatrix vrs(nspin, rho_basis_->npw); + for (int is = 0; is < nspin; is++) { rho_basis_->real2recip(&v_effective(is, 0), &vrs(is, 0)); rho_basis_smooth_->recip2real(&vrs(is, 0), &veff_smooth(is, 0)); @@ -294,8 +302,8 @@ void Potential::interpolate_vrs() if (XC_Functional::get_ked_flag()) { - ModuleBase::ComplexMatrix vrs_ofk(PARAM.inp.nspin, rho_basis_->npw); - for (int is = 0; is < PARAM.inp.nspin; is++) + ModuleBase::ComplexMatrix vrs_ofk(nspin, rho_basis_->npw); + for (int is = 0; is < nspin; is++) { rho_basis_->real2recip(&vofk_effective(is, 0), &vrs_ofk(is, 0)); rho_basis_smooth_->recip2real(&vrs_ofk(is, 0), &vofk_smooth(is, 0)); From f7f156dffb062f6a74ccde1f7f7def25ae753ce0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 26 Sep 2025 16:41:40 +0800 Subject: [PATCH 07/60] fix a potential memory leak --- source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp b/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp index 95d076040e..5eedae9259 100644 --- a/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp +++ b/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp @@ -180,6 +180,7 @@ class PW_BASIS_K_GPU_TEST : public ::testing::Test } void TearDown() override { + delete[] kvec_d; // mohan add 20250926 delete[] h_rhog; delete[] h_rhogout; delete[] h_rhor; From 94e578026ad485e2fb3251d4873295702772a6f3 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 10:17:55 +0800 Subject: [PATCH 08/60] fix bug --- source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp b/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp index 5eedae9259..9abaefab44 100644 --- a/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp +++ b/source/source_basis/module_pw/test_gpu/pw_basis_C2C.cpp @@ -44,9 +44,6 @@ class PW_BASIS_K_GPU_TEST : public ::testing::Test int distribution_type = 1; bool xprime = false; const int nks = 1; - ModuleBase::Vector3* kvec_d; - kvec_d = new ModuleBase::Vector3[nks]; - kvec_d[0].set(0, 0, 0); // init const int mypool = 0; const int key = 1; @@ -180,7 +177,6 @@ class PW_BASIS_K_GPU_TEST : public ::testing::Test } void TearDown() override { - delete[] kvec_d; // mohan add 20250926 delete[] h_rhog; delete[] h_rhogout; delete[] h_rhor; From 8616ac3934b3a62ca406bbd0f6fa400227662e8d Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 13:52:37 +0800 Subject: [PATCH 09/60] add ctrl_output_pw files but cannot run now --- source/source_esolver/esolver_ks_pw.cpp | 305 +------------------- source/source_io/ctrl_output_pw.cpp | 356 ++++++++++++++++++++++++ source/source_io/ctrl_output_pw.h | 19 ++ 3 files changed, 380 insertions(+), 300 deletions(-) create mode 100644 source/source_io/ctrl_output_pw.cpp create mode 100644 source/source_io/ctrl_output_pw.h diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 2c342bfa13..ac522b495c 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -634,54 +634,6 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } - //---------------------------------------------------------- - // 3) Print out electronic wavefunctions in pw basis - // we only print information every few ionic steps - //---------------------------------------------------------- - - // if istep_in = -1, istep will not appear in file name - // if iter_in = -1, iter will not appear in file name - int istep_in = -1; - int iter_in = -1; - bool out_wfc_flag = false; - if (PARAM.inp.out_freq_ion>0) // default value of out_freq_ion is 0 - { - if (istep % PARAM.inp.out_freq_ion == 0) - { - if(iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver) - { - istep_in = istep; - iter_in = iter; - out_wfc_flag = true; - } - } - } - else if(iter == PARAM.inp.scf_nmax || conv_esolver) - { - out_wfc_flag = true; - } - - - if (out_wfc_flag) - { - ModuleIO::write_wfc_pw(istep_in, iter_in, - GlobalV::KPAR, - GlobalV::MY_POOL, - GlobalV::MY_RANK, - PARAM.inp.nbands, - PARAM.inp.nspin, - PARAM.globalv.npol, - GlobalV::RANK_IN_POOL, - GlobalV::NPROC_IN_POOL, - PARAM.inp.out_wfc_pw, - PARAM.inp.ecutwfc, - PARAM.globalv.global_out_dir, - this->psi[0], - this->kv, - this->pw_wfc, - GlobalV::ofs_running); - } - //---------------------------------------------------------- // 4) check if oscillate for delta_spin method //---------------------------------------------------------- @@ -699,6 +651,8 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } } + + ctrl_iter_pw(); } template @@ -733,144 +687,7 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const this->psi[0].size()); } - //---------------------------------------------------------- - //! 4) Compute density of states (DOS) - //---------------------------------------------------------- - if (PARAM.inp.out_dos) - { - bool out_dos_tmp = false; - - int istep_in = -1; - - // default value of out_freq_ion is 0 - if(PARAM.inp.out_freq_ion==0) - { - out_dos_tmp = true; - } - else if (PARAM.inp.out_freq_ion>0) - { - if (istep % PARAM.inp.out_freq_ion == 0) - { - out_dos_tmp = true; - istep_in=istep; - } - else - { - out_dos_tmp = false; - } - } - else - { - out_dos_tmp = false; - } - - // the above is only valid for KSDFT, not SDFT - // this part needs update in the near future - if (PARAM.inp.esolver_type == "sdft") - { - out_dos_tmp = false; - } - - if(out_dos_tmp) - { - ModuleIO::write_dos_pw(ucell, - this->pelec->ekb, - this->pelec->wg, - this->kv, - PARAM.inp.nbands, - istep_in, - this->pelec->eferm, - PARAM.inp.dos_edelta_ev, - PARAM.inp.dos_scale, - PARAM.inp.dos_sigma, - GlobalV::ofs_running); - } - } - - //------------------------------------------------------------------ - // 5) calculate band-decomposed (partial) charge density in pw basis - //------------------------------------------------------------------ - if (PARAM.inp.out_pchg.size() > 0) - { - if (this->__kspw_psi != nullptr && PARAM.inp.precision == "single") - { - delete reinterpret_cast, Device>*>(this->__kspw_psi); - } - - // Refresh __kspw_psi - this->__kspw_psi = PARAM.inp.precision == "single" - ? new psi::Psi, Device>(this->kspw_psi[0]) - : reinterpret_cast, Device>*>(this->kspw_psi); - - ModuleIO::get_pchg_pw(PARAM.inp.out_pchg, - this->kspw_psi->get_nbands(), - PARAM.inp.nspin, - this->pw_rhod->nxyz, - this->chr.ngmc, - &ucell, - this->__kspw_psi, - this->pw_rhod, - this->pw_wfc, - this->ctx, - this->Pgrid, - PARAM.globalv.global_out_dir, - PARAM.inp.if_separate_k, - this->kv, - GlobalV::KPAR, - GlobalV::MY_POOL, - &this->chr); - } - - //------------------------------------------------------------------ - //! 6) calculate Wannier functions in pw basis - //------------------------------------------------------------------ - if (PARAM.inp.calculation == "nscf" && PARAM.inp.towannier90) - { - std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Wannier functions calculation"); - toWannier90_PW wan(PARAM.inp.out_wannier_mmn, - PARAM.inp.out_wannier_amn, - PARAM.inp.out_wannier_unk, - PARAM.inp.out_wannier_eig, - PARAM.inp.out_wannier_wvfn_formatted, - PARAM.inp.nnkpfile, - PARAM.inp.wannier_spin); - wan.set_tpiba_omega(ucell.tpiba, ucell.omega); - wan.calculate(ucell, this->pelec->ekb, this->pw_wfc, this->pw_big, this->kv, this->psi); - std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wannier functions calculation"); - } - - //------------------------------------------------------------------ - //! 7) calculate Berry phase polarization in pw basis - //------------------------------------------------------------------ - if (PARAM.inp.calculation == "nscf" && berryphase::berry_phase_flag && ModuleSymmetry::Symmetry::symm_flag != 1) - { - std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Berry phase polarization"); - berryphase bp; - bp.Macroscopic_polarization(ucell, this->pw_wfc->npwk_max, this->psi, this->pw_rho, this->pw_wfc, this->kv); - std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Berry phase polarization"); - } - - //------------------------------------------------------------------ - // 8) write spin constrian results in pw basis - // spin constrain calculations, write atomic magnetization and magnetic force. - //------------------------------------------------------------------ - if (PARAM.inp.sc_mag_switch) - { - spinconstrain::SpinConstrain>& sc - = spinconstrain::SpinConstrain>::getScInstance(); - sc.cal_mi_pw(); - sc.print_Mag_Force(GlobalV::ofs_running); - } - - //------------------------------------------------------------------ - // 9) write onsite occupations for charge and magnetizations - //------------------------------------------------------------------ - if (PARAM.inp.onsite_radius > 0) - { // float type has not been implemented - auto* onsite_p = projectors::OnsiteProjector::get_instance(); - onsite_p->cal_occupations(reinterpret_cast, Device>*>(this->kspw_psi), - this->pelec->wg); - } + ModuleIO::ctrl_scf_pw(); ModuleBase::timer::tick("ESolver_KS_PW", "after_scf"); } @@ -954,121 +771,9 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) //---------------------------------------------------------- ESolver_KS::after_all_runners(ucell); - //---------------------------------------------------------- - //! 2) Compute LDOS - //---------------------------------------------------------- - if (PARAM.inp.out_ldos[0]) - { - ModuleIO::cal_ldos_pw(reinterpret_cast>*>(this->pelec), - this->psi[0], - this->Pgrid, - ucell); - } - - //---------------------------------------------------------- - //! 3) Calculate the spillage value, - //! which are used to generate numerical atomic orbitals - //---------------------------------------------------------- - if (PARAM.inp.basis_type == "pw" && PARAM.inp.out_spillage) - { - // ! Print out overlap matrices - if (PARAM.inp.out_spillage <= 2) - { - for (int i = 0; i < PARAM.inp.bessel_nao_rcuts.size(); i++) - { - if (GlobalV::MY_RANK == 0) - { - std::cout << "update value: bessel_nao_rcut <- " << std::fixed << PARAM.inp.bessel_nao_rcuts[i] - << " a.u." << std::endl; - } - Numerical_Basis numerical_basis; - numerical_basis.output_overlap(this->psi[0], this->sf, this->kv, this->pw_wfc, ucell, i); - } - ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "BASIS OVERLAP (Q and S) GENERATION."); - } - } - - //---------------------------------------------------------- - //! 4) Print out electronic wave functions in real space - //---------------------------------------------------------- - if (PARAM.inp.out_wfc_norm.size() > 0 || PARAM.inp.out_wfc_re_im.size() > 0) - { - if (this->__kspw_psi != nullptr && PARAM.inp.precision == "single") - { - delete reinterpret_cast, Device>*>(this->__kspw_psi); - } - - // Refresh __kspw_psi - this->__kspw_psi = PARAM.inp.precision == "single" - ? new psi::Psi, Device>(this->kspw_psi[0]) - : reinterpret_cast, Device>*>(this->kspw_psi); - - ModuleIO::get_wf_pw(PARAM.inp.out_wfc_norm, - PARAM.inp.out_wfc_re_im, - this->kspw_psi->get_nbands(), - PARAM.inp.nspin, - this->pw_rhod->nxyz, - &ucell, - this->__kspw_psi, - this->pw_wfc, - this->ctx, - this->Pgrid, - PARAM.globalv.global_out_dir, - this->kv, - GlobalV::KPAR, - GlobalV::MY_POOL); - } - - //---------------------------------------------------------- - //! 5) Use Kubo-Greenwood method to compute conductivities - //---------------------------------------------------------- - if (PARAM.inp.cal_cond) - { - EleCond elec_cond(&ucell, &this->kv, this->pelec, this->pw_wfc, this->kspw_psi, &this->ppcell); - elec_cond.KG(PARAM.inp.cond_smear, - PARAM.inp.cond_fwhm, - PARAM.inp.cond_wcut, - PARAM.inp.cond_dw, - PARAM.inp.cond_dt, - PARAM.inp.cond_nonlocal, - this->pelec->wg); - } + ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_big, this->pw_rhod, + this->chr, this->solvent, this->Pgrid, istep); -#ifdef __MLALGO - //---------------------------------------------------------- - //! 7) generate training data for ML-KEDF - //---------------------------------------------------------- - if (PARAM.inp.of_ml_gene_data == 1) - { - this->pelec->pot->update_from_charge(&this->chr, &ucell); - - ModuleIO::Write_MLKEDF_Descriptors write_mlkedf_desc; - write_mlkedf_desc.cal_tool->set_para(this->chr.nrxx, - PARAM.inp.nelec, - PARAM.inp.of_tf_weight, - PARAM.inp.of_vw_weight, - PARAM.inp.of_ml_chi_p, - PARAM.inp.of_ml_chi_q, - PARAM.inp.of_ml_chi_xi, - PARAM.inp.of_ml_chi_pnl, - PARAM.inp.of_ml_chi_qnl, - PARAM.inp.of_ml_nkernel, - PARAM.inp.of_ml_kernel, - PARAM.inp.of_ml_kernel_scaling, - PARAM.inp.of_ml_yukawa_alpha, - PARAM.inp.of_ml_kernel_file, - ucell.omega, - this->pw_rho); - - write_mlkedf_desc.generateTrainData_KS(PARAM.globalv.global_mlkedf_descriptor_dir, - this->kspw_psi, - this->pelec, - this->pw_wfc, - this->pw_rho, - ucell, - this->pelec->pot->get_effective_v(0)); - } -#endif } template class ESolver_KS_PW, base_device::DEVICE_CPU>; diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp new file mode 100644 index 0000000000..6563d3e6c8 --- /dev/null +++ b/source/source_io/ctrl_output_pw.cpp @@ -0,0 +1,356 @@ +/* +#include "source_io/ctrl_output_fp.h" // use ctrl_output_fp() +#include "source_estate/module_charge/symmetry_rho.h" // use Symmetry_rho +#include "source_io/write_elecstat_pot.h" // use write_elecstat_pot +#include "source_io/write_elf.h" +#include "cube_io.h" // use write_vdata_palgrid +#include "source_hamilt/module_xc/xc_functional.h" // use XC_Functional + +#ifdef USE_LIBXC +#include "source_io/write_libxc_r.h" +#endif +*/ + +namespace ModuleIO +{ + +void ctrl_iter_pw() +{ + ModuleBase::TITLE("ModuleIO", "ctrl_iter_pw"); + ModuleBase::timer::tick("ModuleIO", "ctrl_iter_pw"); + //---------------------------------------------------------- + // 3) Print out electronic wavefunctions in pw basis + // we only print information every few ionic steps + //---------------------------------------------------------- + + // if istep_in = -1, istep will not appear in file name + // if iter_in = -1, iter will not appear in file name + int istep_in = -1; + int iter_in = -1; + bool out_wfc_flag = false; + if (PARAM.inp.out_freq_ion>0) // default value of out_freq_ion is 0 + { + if (istep % PARAM.inp.out_freq_ion == 0) + { + if(iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver) + { + istep_in = istep; + iter_in = iter; + out_wfc_flag = true; + } + } + } + else if(iter == PARAM.inp.scf_nmax || conv_esolver) + { + out_wfc_flag = true; + } + + if (out_wfc_flag) + { + ModuleIO::write_wfc_pw(istep_in, iter_in, + GlobalV::KPAR, + GlobalV::MY_POOL, + GlobalV::MY_RANK, + PARAM.inp.nbands, + PARAM.inp.nspin, + PARAM.globalv.npol, + GlobalV::RANK_IN_POOL, + GlobalV::NPROC_IN_POOL, + PARAM.inp.out_wfc_pw, + PARAM.inp.ecutwfc, + PARAM.globalv.global_out_dir, + this->psi[0], + this->kv, + this->pw_wfc, + GlobalV::ofs_running); + } + + ModuleBase::timer::tick("ModuleIO", "ctrl_iter_pw"); + return; +} + + +void ctrl_scf_pw() +{ + ModuleBase::TITLE("ModuleIO", "ctrl_scf_pw"); + ModuleBase::timer::tick("ModuleIO", "ctrl_scf_pw"); + + //---------------------------------------------------------- + //! 4) Compute density of states (DOS) + //---------------------------------------------------------- + if (PARAM.inp.out_dos) + { + bool out_dos_tmp = false; + + int istep_in = -1; + + // default value of out_freq_ion is 0 + if(PARAM.inp.out_freq_ion==0) + { + out_dos_tmp = true; + } + else if (PARAM.inp.out_freq_ion>0) + { + if (istep % PARAM.inp.out_freq_ion == 0) + { + out_dos_tmp = true; + istep_in=istep; + } + else + { + out_dos_tmp = false; + } + } + else + { + out_dos_tmp = false; + } + + // the above is only valid for KSDFT, not SDFT + // this part needs update in the near future + if (PARAM.inp.esolver_type == "sdft") + { + out_dos_tmp = false; + } + + if(out_dos_tmp) + { + ModuleIO::write_dos_pw(ucell, + this->pelec->ekb, + this->pelec->wg, + this->kv, + PARAM.inp.nbands, + istep_in, + this->pelec->eferm, + PARAM.inp.dos_edelta_ev, + PARAM.inp.dos_scale, + PARAM.inp.dos_sigma, + GlobalV::ofs_running); + } + } + + + //------------------------------------------------------------------ + // 5) calculate band-decomposed (partial) charge density in pw basis + //------------------------------------------------------------------ + if (PARAM.inp.out_pchg.size() > 0) + { + if (this->__kspw_psi != nullptr && PARAM.inp.precision == "single") + { + delete reinterpret_cast, Device>*>(this->__kspw_psi); + } + + // Refresh __kspw_psi + this->__kspw_psi = PARAM.inp.precision == "single" + ? new psi::Psi, Device>(this->kspw_psi[0]) + : reinterpret_cast, Device>*>(this->kspw_psi); + + ModuleIO::get_pchg_pw(PARAM.inp.out_pchg, + this->kspw_psi->get_nbands(), + PARAM.inp.nspin, + this->pw_rhod->nxyz, + this->chr.ngmc, + &ucell, + this->__kspw_psi, + this->pw_rhod, + this->pw_wfc, + this->ctx, + this->Pgrid, + PARAM.globalv.global_out_dir, + PARAM.inp.if_separate_k, + this->kv, + GlobalV::KPAR, + GlobalV::MY_POOL, + &this->chr); + } + + + //------------------------------------------------------------------ + //! 6) calculate Wannier functions in pw basis + //------------------------------------------------------------------ + if (PARAM.inp.calculation == "nscf" && PARAM.inp.towannier90) + { + std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Wannier functions calculation"); + toWannier90_PW wan(PARAM.inp.out_wannier_mmn, + PARAM.inp.out_wannier_amn, + PARAM.inp.out_wannier_unk, + PARAM.inp.out_wannier_eig, + PARAM.inp.out_wannier_wvfn_formatted, + PARAM.inp.nnkpfile, + PARAM.inp.wannier_spin); + wan.set_tpiba_omega(ucell.tpiba, ucell.omega); + wan.calculate(ucell, this->pelec->ekb, this->pw_wfc, this->pw_big, this->kv, this->psi); + std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wannier functions calculation"); + } + + + //------------------------------------------------------------------ + //! 7) calculate Berry phase polarization in pw basis + //------------------------------------------------------------------ + if (PARAM.inp.calculation == "nscf" && berryphase::berry_phase_flag && ModuleSymmetry::Symmetry::symm_flag != 1) + { + std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Berry phase polarization"); + berryphase bp; + bp.Macroscopic_polarization(ucell, this->pw_wfc->npwk_max, this->psi, this->pw_rho, this->pw_wfc, this->kv); + std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Berry phase polarization"); + } + + //------------------------------------------------------------------ + // 8) write spin constrian results in pw basis + // spin constrain calculations, write atomic magnetization and magnetic force. + //------------------------------------------------------------------ + if (PARAM.inp.sc_mag_switch) + { + spinconstrain::SpinConstrain>& sc + = spinconstrain::SpinConstrain>::getScInstance(); + sc.cal_mi_pw(); + sc.print_Mag_Force(GlobalV::ofs_running); + } + + //------------------------------------------------------------------ + // 9) write onsite occupations for charge and magnetizations + //------------------------------------------------------------------ + if (PARAM.inp.onsite_radius > 0) + { // float type has not been implemented + auto* onsite_p = projectors::OnsiteProjector::get_instance(); + onsite_p->cal_occupations(reinterpret_cast, Device>*>(this->kspw_psi), + this->pelec->wg); + } + + ModuleBase::timer::tick("ModuleIO", "ctrl_scf_pw"); + return; +} + + +void ctrl_runner_pw(UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_Big* pw_big, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + surchem &solvent, + Parallel_Grid ¶_grid, + const int istep) +{ + ModuleBase::TITLE("ModuleIO", "ctrl_runner_pw"); + ModuleBase::timer::tick("ModuleIO", "ctrl_runner_pw"); + + //---------------------------------------------------------- + //! 1) Compute LDOS + //---------------------------------------------------------- + if (PARAM.inp.out_ldos[0]) + { + ModuleIO::cal_ldos_pw(reinterpret_cast>*>(this->pelec), + this->psi[0], + this->Pgrid, + ucell); + } + + //---------------------------------------------------------- + //! 2) Calculate the spillage value, + //! which are used to generate numerical atomic orbitals + //---------------------------------------------------------- + if (PARAM.inp.basis_type == "pw" && PARAM.inp.out_spillage) + { + // ! Print out overlap matrices + if (PARAM.inp.out_spillage <= 2) + { + for (int i = 0; i < PARAM.inp.bessel_nao_rcuts.size(); i++) + { + if (GlobalV::MY_RANK == 0) + { + std::cout << "update value: bessel_nao_rcut <- " << std::fixed << PARAM.inp.bessel_nao_rcuts[i] + << " a.u." << std::endl; + } + Numerical_Basis numerical_basis; + numerical_basis.output_overlap(this->psi[0], this->sf, this->kv, this->pw_wfc, ucell, i); + } + ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "BASIS OVERLAP (Q and S) GENERATION."); + } + } + + //---------------------------------------------------------- + //! 3) Print out electronic wave functions in real space + //---------------------------------------------------------- + if (PARAM.inp.out_wfc_norm.size() > 0 || PARAM.inp.out_wfc_re_im.size() > 0) + { + if (this->__kspw_psi != nullptr && PARAM.inp.precision == "single") + { + delete reinterpret_cast, Device>*>(this->__kspw_psi); + } + + // Refresh __kspw_psi + this->__kspw_psi = PARAM.inp.precision == "single" + ? new psi::Psi, Device>(this->kspw_psi[0]) + : reinterpret_cast, Device>*>(this->kspw_psi); + + ModuleIO::get_wf_pw(PARAM.inp.out_wfc_norm, + PARAM.inp.out_wfc_re_im, + this->kspw_psi->get_nbands(), + PARAM.inp.nspin, + this->pw_rhod->nxyz, + &ucell, + this->__kspw_psi, + this->pw_wfc, + this->ctx, + this->Pgrid, + PARAM.globalv.global_out_dir, + this->kv, + GlobalV::KPAR, + GlobalV::MY_POOL); + } + + //---------------------------------------------------------- + //! 4) Use Kubo-Greenwood method to compute conductivities + //---------------------------------------------------------- + if (PARAM.inp.cal_cond) + { + EleCond elec_cond(&ucell, &this->kv, this->pelec, this->pw_wfc, this->kspw_psi, &this->ppcell); + elec_cond.KG(PARAM.inp.cond_smear, + PARAM.inp.cond_fwhm, + PARAM.inp.cond_wcut, + PARAM.inp.cond_dw, + PARAM.inp.cond_dt, + PARAM.inp.cond_nonlocal, + this->pelec->wg); + } + +#ifdef __MLALGO + //---------------------------------------------------------- + //! 7) generate training data for ML-KEDF + //---------------------------------------------------------- + if (PARAM.inp.of_ml_gene_data == 1) + { + this->pelec->pot->update_from_charge(&this->chr, &ucell); + + ModuleIO::Write_MLKEDF_Descriptors write_mlkedf_desc; + write_mlkedf_desc.cal_tool->set_para(this->chr.nrxx, + PARAM.inp.nelec, + PARAM.inp.of_tf_weight, + PARAM.inp.of_vw_weight, + PARAM.inp.of_ml_chi_p, + PARAM.inp.of_ml_chi_q, + PARAM.inp.of_ml_chi_xi, + PARAM.inp.of_ml_chi_pnl, + PARAM.inp.of_ml_chi_qnl, + PARAM.inp.of_ml_nkernel, + PARAM.inp.of_ml_kernel, + PARAM.inp.of_ml_kernel_scaling, + PARAM.inp.of_ml_yukawa_alpha, + PARAM.inp.of_ml_kernel_file, + ucell.omega, + this->pw_rho); + + write_mlkedf_desc.generateTrainData_KS(PARAM.globalv.global_mlkedf_descriptor_dir, + this->kspw_psi, + this->pelec, + this->pw_wfc, + this->pw_rho, + ucell, + this->pelec->pot->get_effective_v(0)); + } +#endif + + ModuleBase::timer::tick("ModuleIO", "ctrl_runner_pw"); +} + +} // End ModuleIO diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h new file mode 100644 index 0000000000..8dc8520766 --- /dev/null +++ b/source/source_io/ctrl_output_pw.h @@ -0,0 +1,19 @@ +#ifndef CTRL_OUTPUT_PW_H +#define CTRL_OUTPUT_PW_H + +#include "source_estate/elecstate_lcao.h" + +namespace ModuleIO +{ + + void ctrl_output_pw(UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_Big* pw_big, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + surchem &solvent, + Parallel_Grid ¶_grid, + const int istep); + +} +#endif From bbe4c3f12301fd1b94d77ac77d11097ab59a30ee Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 14:26:35 +0800 Subject: [PATCH 10/60] add some interfaces in ctrl_output_pw --- source/Makefile.Objects | 1 + source/source_io/CMakeLists.txt | 1 + source/source_io/ctrl_output_pw.cpp | 238 +++++++++++++++------------- source/source_io/ctrl_output_pw.h | 29 +++- 4 files changed, 151 insertions(+), 118 deletions(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index db5585dd3a..8d4d9933a1 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -580,6 +580,7 @@ OBJS_IO=input_conv.o\ output_mat_sparse.o\ ctrl_output_lcao.o\ ctrl_output_fp.o\ + ctrl_output_pw.o\ para_json.o\ abacusjson.o\ general_info.o\ diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index ffa7598cd9..e511ffb35c 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -1,6 +1,7 @@ list(APPEND objects input_conv.cpp ctrl_output_fp.cpp + ctrl_output_pw.cpp bessel_basis.cpp cal_test.cpp cal_dos.cpp diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 6563d3e6c8..806b586c65 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -14,7 +14,14 @@ namespace ModuleIO { -void ctrl_iter_pw() +template +void ctrl_iter_pw(const int istep, + const int iter, + const double &conv_esolver, + psi::Psi, base_device::DEVICE_CPU>* psi, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const Input_para& inp) { ModuleBase::TITLE("ModuleIO", "ctrl_iter_pw"); ModuleBase::timer::tick("ModuleIO", "ctrl_iter_pw"); @@ -28,11 +35,11 @@ void ctrl_iter_pw() int istep_in = -1; int iter_in = -1; bool out_wfc_flag = false; - if (PARAM.inp.out_freq_ion>0) // default value of out_freq_ion is 0 + if (inp.out_freq_ion>0) // default value of out_freq_ion is 0 { - if (istep % PARAM.inp.out_freq_ion == 0) + if (istep % inp.out_freq_ion == 0) { - if(iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver) + if(iter % inp.out_freq_elec == 0 || iter == inp.scf_nmax || conv_esolver) { istep_in = istep; iter_in = iter; @@ -40,7 +47,7 @@ void ctrl_iter_pw() } } } - else if(iter == PARAM.inp.scf_nmax || conv_esolver) + else if(iter == inp.scf_nmax || conv_esolver) { out_wfc_flag = true; } @@ -51,17 +58,17 @@ void ctrl_iter_pw() GlobalV::KPAR, GlobalV::MY_POOL, GlobalV::MY_RANK, - PARAM.inp.nbands, - PARAM.inp.nspin, + inp.nbands, + inp.nspin, PARAM.globalv.npol, GlobalV::RANK_IN_POOL, GlobalV::NPROC_IN_POOL, - PARAM.inp.out_wfc_pw, - PARAM.inp.ecutwfc, + inp.out_wfc_pw, + inp.ecutwfc, PARAM.globalv.global_out_dir, - this->psi[0], - this->kv, - this->pw_wfc, + psi[0], + kv, + pw_wfc, GlobalV::ofs_running); } @@ -70,7 +77,14 @@ void ctrl_iter_pw() } -void ctrl_scf_pw() +void ctrl_scf_pw(elecstate::ElecState* pelec, + const Charge &chr, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rhod, + psi::Psi* kspw_psi, + psi::Psi, Device>* __kspw_psi, + const Input_para& inp) { ModuleBase::TITLE("ModuleIO", "ctrl_scf_pw"); ModuleBase::timer::tick("ModuleIO", "ctrl_scf_pw"); @@ -78,20 +92,20 @@ void ctrl_scf_pw() //---------------------------------------------------------- //! 4) Compute density of states (DOS) //---------------------------------------------------------- - if (PARAM.inp.out_dos) + if (inp.out_dos) { bool out_dos_tmp = false; int istep_in = -1; // default value of out_freq_ion is 0 - if(PARAM.inp.out_freq_ion==0) + if(inp.out_freq_ion==0) { out_dos_tmp = true; } - else if (PARAM.inp.out_freq_ion>0) + else if (inp.out_freq_ion>0) { - if (istep % PARAM.inp.out_freq_ion == 0) + if (istep % inp.out_freq_ion == 0) { out_dos_tmp = true; istep_in=istep; @@ -108,7 +122,7 @@ void ctrl_scf_pw() // the above is only valid for KSDFT, not SDFT // this part needs update in the near future - if (PARAM.inp.esolver_type == "sdft") + if (inp.esolver_type == "sdft") { out_dos_tmp = false; } @@ -116,15 +130,15 @@ void ctrl_scf_pw() if(out_dos_tmp) { ModuleIO::write_dos_pw(ucell, - this->pelec->ekb, - this->pelec->wg, - this->kv, - PARAM.inp.nbands, + pelec->ekb, + pelec->wg, + kv, + inp.nbands, istep_in, - this->pelec->eferm, - PARAM.inp.dos_edelta_ev, - PARAM.inp.dos_scale, - PARAM.inp.dos_sigma, + pelec->eferm, + inp.dos_edelta_ev, + inp.dos_scale, + inp.dos_sigma, GlobalV::ofs_running); } } @@ -133,53 +147,53 @@ void ctrl_scf_pw() //------------------------------------------------------------------ // 5) calculate band-decomposed (partial) charge density in pw basis //------------------------------------------------------------------ - if (PARAM.inp.out_pchg.size() > 0) + if (inp.out_pchg.size() > 0) { - if (this->__kspw_psi != nullptr && PARAM.inp.precision == "single") + if (__kspw_psi != nullptr && inp.precision == "single") { - delete reinterpret_cast, Device>*>(this->__kspw_psi); + delete reinterpret_cast, Device>*>(__kspw_psi); } // Refresh __kspw_psi - this->__kspw_psi = PARAM.inp.precision == "single" - ? new psi::Psi, Device>(this->kspw_psi[0]) - : reinterpret_cast, Device>*>(this->kspw_psi); - - ModuleIO::get_pchg_pw(PARAM.inp.out_pchg, - this->kspw_psi->get_nbands(), - PARAM.inp.nspin, - this->pw_rhod->nxyz, - this->chr.ngmc, + __kspw_psi = inp.precision == "single" + ? new psi::Psi, Device>(kspw_psi[0]) + : reinterpret_cast, Device>*>(kspw_psi); + + ModuleIO::get_pchg_pw(inp.out_pchg, + kspw_psi->get_nbands(), + inp.nspin, + pw_rhod->nxyz, + chr.ngmc, &ucell, - this->__kspw_psi, - this->pw_rhod, - this->pw_wfc, + __kspw_psi, + pw_rhod, + pw_wfc, this->ctx, this->Pgrid, PARAM.globalv.global_out_dir, - PARAM.inp.if_separate_k, - this->kv, + inp.if_separate_k, + kv, GlobalV::KPAR, GlobalV::MY_POOL, - &this->chr); + &chr); } //------------------------------------------------------------------ //! 6) calculate Wannier functions in pw basis //------------------------------------------------------------------ - if (PARAM.inp.calculation == "nscf" && PARAM.inp.towannier90) + if (inp.calculation == "nscf" && inp.towannier90) { std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Wannier functions calculation"); - toWannier90_PW wan(PARAM.inp.out_wannier_mmn, - PARAM.inp.out_wannier_amn, - PARAM.inp.out_wannier_unk, - PARAM.inp.out_wannier_eig, - PARAM.inp.out_wannier_wvfn_formatted, - PARAM.inp.nnkpfile, - PARAM.inp.wannier_spin); + toWannier90_PW wan(inp.out_wannier_mmn, + inp.out_wannier_amn, + inp.out_wannier_unk, + inp.out_wannier_eig, + inp.out_wannier_wvfn_formatted, + inp.nnkpfile, + inp.wannier_spin); wan.set_tpiba_omega(ucell.tpiba, ucell.omega); - wan.calculate(ucell, this->pelec->ekb, this->pw_wfc, this->pw_big, this->kv, this->psi); + wan.calculate(ucell, pelec->ekb, this->pw_wfc, this->pw_big, kv, this->psi); std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wannier functions calculation"); } @@ -187,11 +201,11 @@ void ctrl_scf_pw() //------------------------------------------------------------------ //! 7) calculate Berry phase polarization in pw basis //------------------------------------------------------------------ - if (PARAM.inp.calculation == "nscf" && berryphase::berry_phase_flag && ModuleSymmetry::Symmetry::symm_flag != 1) + if (inp.calculation == "nscf" && berryphase::berry_phase_flag && ModuleSymmetry::Symmetry::symm_flag != 1) { std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Berry phase polarization"); berryphase bp; - bp.Macroscopic_polarization(ucell, this->pw_wfc->npwk_max, this->psi, this->pw_rho, this->pw_wfc, this->kv); + bp.Macroscopic_polarization(ucell, this->pw_wfc->npwk_max, this->psi, this->pw_rho, this->pw_wfc, kv); std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Berry phase polarization"); } @@ -199,7 +213,7 @@ void ctrl_scf_pw() // 8) write spin constrian results in pw basis // spin constrain calculations, write atomic magnetization and magnetic force. //------------------------------------------------------------------ - if (PARAM.inp.sc_mag_switch) + if (inp.sc_mag_switch) { spinconstrain::SpinConstrain>& sc = spinconstrain::SpinConstrain>::getScInstance(); @@ -210,11 +224,11 @@ void ctrl_scf_pw() //------------------------------------------------------------------ // 9) write onsite occupations for charge and magnetizations //------------------------------------------------------------------ - if (PARAM.inp.onsite_radius > 0) + if (inp.onsite_radius > 0) { // float type has not been implemented auto* onsite_p = projectors::OnsiteProjector::get_instance(); onsite_p->cal_occupations(reinterpret_cast, Device>*>(this->kspw_psi), - this->pelec->wg); + pelec->wg); } ModuleBase::timer::tick("ModuleIO", "ctrl_scf_pw"); @@ -224,12 +238,16 @@ void ctrl_scf_pw() void ctrl_runner_pw(UnitCell& ucell, elecstate::ElecState* pelec, - ModulePW::PW_Basis_Big* pw_big, + ModulePW::PW_Basis_K* pw_wfc, + ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, - Charge &chr, - surchem &solvent, + Charge &chr, + psi::Psi* kspw_psi, + psi::Psi, Device>* __kspw_psi, + surchem &solvent, Parallel_Grid ¶_grid, - const int istep) + const int istep, + const Input_para& inp); { ModuleBase::TITLE("ModuleIO", "ctrl_runner_pw"); ModuleBase::timer::tick("ModuleIO", "ctrl_runner_pw"); @@ -237,9 +255,9 @@ void ctrl_runner_pw(UnitCell& ucell, //---------------------------------------------------------- //! 1) Compute LDOS //---------------------------------------------------------- - if (PARAM.inp.out_ldos[0]) + if (inp.out_ldos[0]) { - ModuleIO::cal_ldos_pw(reinterpret_cast>*>(this->pelec), + ModuleIO::cal_ldos_pw(reinterpret_cast>*>(pelec), this->psi[0], this->Pgrid, ucell); @@ -249,20 +267,20 @@ void ctrl_runner_pw(UnitCell& ucell, //! 2) Calculate the spillage value, //! which are used to generate numerical atomic orbitals //---------------------------------------------------------- - if (PARAM.inp.basis_type == "pw" && PARAM.inp.out_spillage) + if (inp.basis_type == "pw" && inp.out_spillage) { // ! Print out overlap matrices - if (PARAM.inp.out_spillage <= 2) + if (inp.out_spillage <= 2) { - for (int i = 0; i < PARAM.inp.bessel_nao_rcuts.size(); i++) + for (int i = 0; i < inp.bessel_nao_rcuts.size(); i++) { if (GlobalV::MY_RANK == 0) { - std::cout << "update value: bessel_nao_rcut <- " << std::fixed << PARAM.inp.bessel_nao_rcuts[i] + std::cout << "update value: bessel_nao_rcut <- " << std::fixed << inp.bessel_nao_rcuts[i] << " a.u." << std::endl; } Numerical_Basis numerical_basis; - numerical_basis.output_overlap(this->psi[0], this->sf, this->kv, this->pw_wfc, ucell, i); + numerical_basis.output_overlap(this->psi[0], this->sf, kv, pw_wfc, ucell, i); } ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "BASIS OVERLAP (Q and S) GENERATION."); } @@ -271,30 +289,30 @@ void ctrl_runner_pw(UnitCell& ucell, //---------------------------------------------------------- //! 3) Print out electronic wave functions in real space //---------------------------------------------------------- - if (PARAM.inp.out_wfc_norm.size() > 0 || PARAM.inp.out_wfc_re_im.size() > 0) + if (inp.out_wfc_norm.size() > 0 || inp.out_wfc_re_im.size() > 0) { - if (this->__kspw_psi != nullptr && PARAM.inp.precision == "single") + if (__kspw_psi != nullptr && inp.precision == "single") { - delete reinterpret_cast, Device>*>(this->__kspw_psi); + delete reinterpret_cast, Device>*>(__kspw_psi); } // Refresh __kspw_psi - this->__kspw_psi = PARAM.inp.precision == "single" + __kspw_psi = inp.precision == "single" ? new psi::Psi, Device>(this->kspw_psi[0]) : reinterpret_cast, Device>*>(this->kspw_psi); - ModuleIO::get_wf_pw(PARAM.inp.out_wfc_norm, - PARAM.inp.out_wfc_re_im, + ModuleIO::get_wf_pw(inp.out_wfc_norm, + inp.out_wfc_re_im, this->kspw_psi->get_nbands(), - PARAM.inp.nspin, - this->pw_rhod->nxyz, + inp.nspin, + pw_rhod->nxyz, &ucell, - this->__kspw_psi, - this->pw_wfc, + __kspw_psi, + pw_wfc, this->ctx, this->Pgrid, PARAM.globalv.global_out_dir, - this->kv, + kv, GlobalV::KPAR, GlobalV::MY_POOL); } @@ -302,51 +320,51 @@ void ctrl_runner_pw(UnitCell& ucell, //---------------------------------------------------------- //! 4) Use Kubo-Greenwood method to compute conductivities //---------------------------------------------------------- - if (PARAM.inp.cal_cond) + if (inp.cal_cond) { - EleCond elec_cond(&ucell, &this->kv, this->pelec, this->pw_wfc, this->kspw_psi, &this->ppcell); - elec_cond.KG(PARAM.inp.cond_smear, - PARAM.inp.cond_fwhm, - PARAM.inp.cond_wcut, - PARAM.inp.cond_dw, - PARAM.inp.cond_dt, - PARAM.inp.cond_nonlocal, - this->pelec->wg); + EleCond elec_cond(&ucell, &kv, pelec, pw_wfc, this->kspw_psi, &this->ppcell); + elec_cond.KG(inp.cond_smear, + inp.cond_fwhm, + inp.cond_wcut, + inp.cond_dw, + inp.cond_dt, + inp.cond_nonlocal, + pelec->wg); } #ifdef __MLALGO //---------------------------------------------------------- //! 7) generate training data for ML-KEDF //---------------------------------------------------------- - if (PARAM.inp.of_ml_gene_data == 1) + if (inp.of_ml_gene_data == 1) { - this->pelec->pot->update_from_charge(&this->chr, &ucell); + pelec->pot->update_from_charge(&chr, &ucell); ModuleIO::Write_MLKEDF_Descriptors write_mlkedf_desc; - write_mlkedf_desc.cal_tool->set_para(this->chr.nrxx, - PARAM.inp.nelec, - PARAM.inp.of_tf_weight, - PARAM.inp.of_vw_weight, - PARAM.inp.of_ml_chi_p, - PARAM.inp.of_ml_chi_q, - PARAM.inp.of_ml_chi_xi, - PARAM.inp.of_ml_chi_pnl, - PARAM.inp.of_ml_chi_qnl, - PARAM.inp.of_ml_nkernel, - PARAM.inp.of_ml_kernel, - PARAM.inp.of_ml_kernel_scaling, - PARAM.inp.of_ml_yukawa_alpha, - PARAM.inp.of_ml_kernel_file, + write_mlkedf_desc.cal_tool->set_para(chr.nrxx, + inp.nelec, + inp.of_tf_weight, + inp.of_vw_weight, + inp.of_ml_chi_p, + inp.of_ml_chi_q, + inp.of_ml_chi_xi, + inp.of_ml_chi_pnl, + inp.of_ml_chi_qnl, + inp.of_ml_nkernel, + inp.of_ml_kernel, + inp.of_ml_kernel_scaling, + inp.of_ml_yukawa_alpha, + inp.of_ml_kernel_file, ucell.omega, - this->pw_rho); + pw_rho); write_mlkedf_desc.generateTrainData_KS(PARAM.globalv.global_mlkedf_descriptor_dir, this->kspw_psi, - this->pelec, - this->pw_wfc, - this->pw_rho, + pelec, + pw_wfc, + pw_rho, ucell, - this->pelec->pot->get_effective_v(0)); + pelec->pot->get_effective_v(0)); } #endif diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 8dc8520766..1502cd5f03 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -6,14 +6,27 @@ namespace ModuleIO { - void ctrl_output_pw(UnitCell& ucell, - elecstate::ElecState* pelec, - ModulePW::PW_Basis_Big* pw_big, - ModulePW::PW_Basis* pw_rhod, - Charge &chr, - surchem &solvent, - Parallel_Grid ¶_grid, - const int istep); +template +void ctrl_iter_pw(const int istep, + const int iter, + const double &conv_esolver, + psi::Psi, base_device::DEVICE_CPU>* psi, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const Input_para& inp); + +template +void ctrl_scf_pw(elecstate::ElecState* pelec); + +template +void ctrl_runner_pw(UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_Big* pw_big, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + surchem &solvent, + Parallel_Grid ¶_grid, + const int istep); } #endif From 77ea8006c7e13bbb7aca7b74c7095459946c42b9 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 15:15:49 +0800 Subject: [PATCH 11/60] keep fixing bugs --- source/source_esolver/esolver_ks_pw.cpp | 41 +++++---- source/source_io/ctrl_output_pw.cpp | 114 +++++++++++++++++++----- source/source_io/ctrl_output_pw.h | 37 ++++++-- 3 files changed, 145 insertions(+), 47 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index ac522b495c..dc2e1cfc47 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -14,30 +14,30 @@ #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/hsolver_pw.h" #include "source_hsolver/kernels/dngvd_op.h" -#include "source_io/berryphase.h" -#include "source_io/cal_ldos.h" -#include "source_io/get_pchg_pw.h" -#include "source_io/get_wf_pw.h" +//#include "source_io/berryphase.h" +//#include "source_io/cal_ldos.h" +//#include "source_io/get_pchg_pw.h" +//#include "source_io/get_wf_pw.h" #include "source_io/module_parameter/parameter.h" -#include "source_io/numerical_basis.h" -#include "source_io/numerical_descriptor.h" -#include "source_io/to_wannier90_pw.h" -#include "source_io/write_dos_pw.h" -#include "source_io/write_wfc_pw.h" +//#include "source_io/numerical_basis.h" +//#include "source_io/numerical_descriptor.h" +//#include "source_io/to_wannier90_pw.h" +//#include "source_io/write_dos_pw.h" +//#include "source_io/write_wfc_pw.h" #include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/VSep_in_pw.h" #include "source_pw/module_pwdft/elecond.h" #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/hamilt_pw.h" -#include "source_pw/module_pwdft/onsite_projector.h" +//#include "source_pw/module_pwdft/onsite_projector.h" #include "source_pw/module_pwdft/stress_pw.h" #include -#ifdef __MLALGO -#include "source_io/write_mlkedf_descriptors.h" -#endif +//#ifdef __MLALGO +//#include "source_io/write_mlkedf_descriptors.h" +//#endif #include #include @@ -48,6 +48,8 @@ #include +#include "source_io/ctrl_output_pw.h" // mohan add 20250927 + namespace ModuleESolver { @@ -652,7 +654,8 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } - ctrl_iter_pw(); + ModuleIO::ctrl_iter_pw(istep, iter, conv_esolver, this->psi, + this->kv, this->pw_wfc, PARAM.inp); } template @@ -687,7 +690,9 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const this->psi[0].size()); } - ModuleIO::ctrl_scf_pw(); + ModuleIO::ctrl_scf_pw(this->pelec, this->chr, this->kv, this->pw_wfc, + this->pw_rho, this->pw_rhod, this->pw_big, this->psi, this->kspw_psi, + this->__kspw_psi, this->ctx, this->Pgrid, PARAM.inp); ModuleBase::timer::tick("ESolver_KS_PW", "after_scf"); } @@ -771,8 +776,10 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) //---------------------------------------------------------- ESolver_KS::after_all_runners(ucell); - ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_big, this->pw_rhod, - this->chr, this->solvent, this->Pgrid, istep); + ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, + this->pw_rho, this->pw_rhod, this->chr, this->psi, + this->kspw_psi, this->__kspw_psi, this->sf, + this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); } diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 806b586c65..7d1aa48869 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -1,3 +1,8 @@ +#include "source_io/ctrl_output_pw.h" + +#include "source_io/write_wfc_pw.h" // use write_wfc_pw +#include "source_pw/module_pwdft/onsite_projector.h" // use projector + /* #include "source_io/ctrl_output_fp.h" // use ctrl_output_fp() #include "source_estate/module_charge/symmetry_rho.h" // use Symmetry_rho @@ -14,7 +19,6 @@ namespace ModuleIO { -template void ctrl_iter_pw(const int istep, const int iter, const double &conv_esolver, @@ -77,13 +81,19 @@ void ctrl_iter_pw(const int istep, } +template void ctrl_scf_pw(elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, + const ModulePW::PW_Basis_Big *pw_big, + psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, + const Device* ctx, + const Parallel_Grid ¶_grid, const Input_para& inp) { ModuleBase::TITLE("ModuleIO", "ctrl_scf_pw"); @@ -121,7 +131,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, } // the above is only valid for KSDFT, not SDFT - // this part needs update in the near future + // Needs update in the near future if (inp.esolver_type == "sdft") { out_dos_tmp = false; @@ -168,8 +178,8 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, __kspw_psi, pw_rhod, pw_wfc, - this->ctx, - this->Pgrid, + ctx, + para_grid, PARAM.globalv.global_out_dir, inp.if_separate_k, kv, @@ -193,7 +203,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, inp.nnkpfile, inp.wannier_spin); wan.set_tpiba_omega(ucell.tpiba, ucell.omega); - wan.calculate(ucell, pelec->ekb, this->pw_wfc, this->pw_big, kv, this->psi); + wan.calculate(ucell, pelec->ekb, pw_wfc, pw_big, kv, psi); std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wannier functions calculation"); } @@ -205,7 +215,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, { std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Berry phase polarization"); berryphase bp; - bp.Macroscopic_polarization(ucell, this->pw_wfc->npwk_max, this->psi, this->pw_rho, this->pw_wfc, kv); + bp.Macroscopic_polarization(ucell, pw_wfc->npwk_max, psi, pw_rho, pw_wfc, kv); std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Berry phase polarization"); } @@ -227,7 +237,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, if (inp.onsite_radius > 0) { // float type has not been implemented auto* onsite_p = projectors::OnsiteProjector::get_instance(); - onsite_p->cal_occupations(reinterpret_cast, Device>*>(this->kspw_psi), + onsite_p->cal_occupations(reinterpret_cast, Device>*>(kspw_psi), pelec->wg); } @@ -235,19 +245,22 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, return; } - +template void ctrl_runner_pw(UnitCell& ucell, elecstate::ElecState* pelec, ModulePW::PW_Basis_K* pw_wfc, ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, + psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, + Structure_Factor &sf, + pseudopot_cell_vnl &ppcell, surchem &solvent, + const Device* ctx, Parallel_Grid ¶_grid, - const int istep, - const Input_para& inp); + const Input_para& inp) { ModuleBase::TITLE("ModuleIO", "ctrl_runner_pw"); ModuleBase::timer::tick("ModuleIO", "ctrl_runner_pw"); @@ -258,9 +271,7 @@ void ctrl_runner_pw(UnitCell& ucell, if (inp.out_ldos[0]) { ModuleIO::cal_ldos_pw(reinterpret_cast>*>(pelec), - this->psi[0], - this->Pgrid, - ucell); + psi[0], para_grid, ucell); } //---------------------------------------------------------- @@ -280,7 +291,7 @@ void ctrl_runner_pw(UnitCell& ucell, << " a.u." << std::endl; } Numerical_Basis numerical_basis; - numerical_basis.output_overlap(this->psi[0], this->sf, kv, pw_wfc, ucell, i); + numerical_basis.output_overlap(psi[0], sf, kv, pw_wfc, ucell, i); } ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "BASIS OVERLAP (Q and S) GENERATION."); } @@ -298,19 +309,19 @@ void ctrl_runner_pw(UnitCell& ucell, // Refresh __kspw_psi __kspw_psi = inp.precision == "single" - ? new psi::Psi, Device>(this->kspw_psi[0]) - : reinterpret_cast, Device>*>(this->kspw_psi); + ? new psi::Psi, Device>(kspw_psi[0]) + : reinterpret_cast, Device>*>(kspw_psi); ModuleIO::get_wf_pw(inp.out_wfc_norm, inp.out_wfc_re_im, - this->kspw_psi->get_nbands(), + kspw_psi->get_nbands(), inp.nspin, pw_rhod->nxyz, &ucell, __kspw_psi, pw_wfc, - this->ctx, - this->Pgrid, + ctx, + para_grid, PARAM.globalv.global_out_dir, kv, GlobalV::KPAR, @@ -322,7 +333,7 @@ void ctrl_runner_pw(UnitCell& ucell, //---------------------------------------------------------- if (inp.cal_cond) { - EleCond elec_cond(&ucell, &kv, pelec, pw_wfc, this->kspw_psi, &this->ppcell); + EleCond elec_cond(&ucell, &kv, pelec, pw_wfc, kspw_psi, &ppcell); elec_cond.KG(inp.cond_smear, inp.cond_fwhm, inp.cond_wcut, @@ -359,7 +370,7 @@ void ctrl_runner_pw(UnitCell& ucell, pw_rho); write_mlkedf_desc.generateTrainData_KS(PARAM.globalv.global_mlkedf_descriptor_dir, - this->kspw_psi, + kspw_psi, pelec, pw_wfc, pw_rho, @@ -371,4 +382,65 @@ void ctrl_runner_pw(UnitCell& ucell, ModuleBase::timer::tick("ModuleIO", "ctrl_runner_pw"); } +template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( + elecstate::ElecState* pelec, + const Charge &chr, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rho, + const ModulePW::PW_Basis *pw_rhod, + const ModulePW::PW_Basis_Big *pw_big, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, + const base_device::DEVICE_CPU* ctx, + const Parallel_Grid ¶_grid, + const Input_para& inp); + +template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( + elecstate::ElecState* pelec, + const Charge &chr, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rho, + const ModulePW::PW_Basis *pw_rhod, + const ModulePW::PW_Basis_Big *pw_big, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, + const base_device::DEVICE_CPU* ctx, + const Parallel_Grid ¶_grid, + const Input_para& inp); + +template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( + elecstate::ElecState* pelec, + const Charge &chr, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rho, + const ModulePW::PW_Basis *pw_rhod, + const ModulePW::PW_Basis_Big *pw_big, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, + const base_device::DEVICE_CPU* ctx, + const Parallel_Grid ¶_grid, + const Input_para& inp); + +template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( + elecstate::ElecState* pelec, + const Charge &chr, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rho, + const ModulePW::PW_Basis *pw_rhod, + const ModulePW::PW_Basis_Big *pw_big, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, + const base_device::DEVICE_CPU* ctx, + const Parallel_Grid ¶_grid, + const Input_para& inp); + + } // End ModuleIO diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 1502cd5f03..010749c805 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -1,12 +1,12 @@ #ifndef CTRL_OUTPUT_PW_H #define CTRL_OUTPUT_PW_H -#include "source_estate/elecstate_lcao.h" +#include "source_psi/psi_init.h" // about psi +#include "source_estate/elecstate_lcao.h" // use pelec namespace ModuleIO { -template void ctrl_iter_pw(const int istep, const int iter, const double &conv_esolver, @@ -15,18 +15,37 @@ void ctrl_iter_pw(const int istep, const ModulePW::PW_Basis_K *pw_wfc, const Input_para& inp); -template -void ctrl_scf_pw(elecstate::ElecState* pelec); +template +void ctrl_scf_pw(elecstate::ElecState* pelec, + const Charge &chr, + const K_Vectors &kv, + const ModulePW::PW_Basis_K *pw_wfc, + const ModulePW::PW_Basis *pw_rho, + const ModulePW::PW_Basis *pw_rhod, + const ModulePW::PW_Basis_Big *pw_big, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi* kspw_psi, + psi::Psi, Device>* __kspw_psi, + const Device* ctx, + const Parallel_Grid ¶_grid, + const Input_para& inp); -template +template void ctrl_runner_pw(UnitCell& ucell, elecstate::ElecState* pelec, - ModulePW::PW_Basis_Big* pw_big, - ModulePW::PW_Basis* pw_rhod, + ModulePW::PW_Basis_K* pw_wfc, + ModulePW::PW_Basis* pw_rho, + ModulePW::PW_Basis* pw_rhod, Charge &chr, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi* kspw_psi, + psi::Psi, Device>* __kspw_psi, + Structure_Factor &sf, + pseudopot_cell_vnl &ppcell, surchem &solvent, - Parallel_Grid ¶_grid, - const int istep); + const Device* ctx, + Parallel_Grid ¶_grid, + const Input_para& inp); } #endif From 5364e44503ddc019d7e2e4e74ba43f09259aedf2 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 16:17:19 +0800 Subject: [PATCH 12/60] successfully compile the codes --- source/source_base/module_device/device.h | 12 +- source/source_esolver/esolver_ks_pw.cpp | 33 ++--- source/source_io/ctrl_output_pw.cpp | 161 +++++++++++++++++----- source/source_io/ctrl_output_pw.h | 12 +- 4 files changed, 146 insertions(+), 72 deletions(-) diff --git a/source/source_base/module_device/device.h b/source/source_base/module_device/device.h index a073bdab91..1b3498cc60 100644 --- a/source/source_base/module_device/device.h +++ b/source/source_base/module_device/device.h @@ -11,16 +11,6 @@ namespace base_device { -// struct CPU; -// struct GPU; - -// enum AbacusDevice_t -// { -// UnKnown, -// CpuDevice, -// GpuDevice -// }; - template base_device::AbacusDevice_t get_device_type(const Device* dev); @@ -122,4 +112,4 @@ static __inline__ __device__ double atomicAdd(double* address, double val) } #endif -#endif // MODULE_DEVICE_H_ \ No newline at end of file +#endif // MODULE_DEVICE_H_ diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index dc2e1cfc47..425dc2ef23 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -1,6 +1,6 @@ #include "esolver_ks_pw.h" -#include "source_base/formatter.h" +//#include "source_base/formatter.h" #include "source_base/global_variable.h" #include "source_base/kernels/math_kernel_op.h" #include "source_base/memory.h" @@ -14,23 +14,14 @@ #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/hsolver_pw.h" #include "source_hsolver/kernels/dngvd_op.h" -//#include "source_io/berryphase.h" -//#include "source_io/cal_ldos.h" -//#include "source_io/get_pchg_pw.h" -//#include "source_io/get_wf_pw.h" #include "source_io/module_parameter/parameter.h" -//#include "source_io/numerical_basis.h" -//#include "source_io/numerical_descriptor.h" -//#include "source_io/to_wannier90_pw.h" -//#include "source_io/write_dos_pw.h" -//#include "source_io/write_wfc_pw.h" #include "source_lcao/module_deltaspin/spin_constrain.h" +#include "source_pw/module_pwdft/onsite_projector.h" #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/VSep_in_pw.h" -#include "source_pw/module_pwdft/elecond.h" +//#include "source_pw/module_pwdft/elecond.h" #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/hamilt_pw.h" -//#include "source_pw/module_pwdft/onsite_projector.h" #include "source_pw/module_pwdft/stress_pw.h" #include @@ -654,8 +645,8 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } - ModuleIO::ctrl_iter_pw(istep, iter, conv_esolver, this->psi, - this->kv, this->pw_wfc, PARAM.inp); +// ModuleIO::ctrl_iter_pw(istep, iter, conv_esolver, this->psi, + // this->kv, this->pw_wfc, PARAM.inp); } template @@ -690,9 +681,9 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const this->psi[0].size()); } - ModuleIO::ctrl_scf_pw(this->pelec, this->chr, this->kv, this->pw_wfc, - this->pw_rho, this->pw_rhod, this->pw_big, this->psi, this->kspw_psi, - this->__kspw_psi, this->ctx, this->Pgrid, PARAM.inp); +// ModuleIO::ctrl_scf_pw(istep, ucell, this->pelec, this->chr, this->kv, this->pw_wfc, + // this->pw_rho, this->pw_rhod, this->pw_big, this->psi, this->kspw_psi, + // this->__kspw_psi, /* this->ctx,*/ this->Pgrid, PARAM.inp); ModuleBase::timer::tick("ESolver_KS_PW", "after_scf"); } @@ -776,10 +767,10 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) //---------------------------------------------------------- ESolver_KS::after_all_runners(ucell); - ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, - this->pw_rho, this->pw_rhod, this->chr, this->psi, - this->kspw_psi, this->__kspw_psi, this->sf, - this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); + //ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, + // this->pw_rho, this->pw_rhod, this->chr, this->psi, + // this->kspw_psi, this->__kspw_psi, this->sf, + // this->ppcell, this->solvent, /* this->ctx,*/ this->Pgrid, PARAM.inp); } diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 7d1aa48869..65ab3c2583 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -1,25 +1,20 @@ #include "source_io/ctrl_output_pw.h" #include "source_io/write_wfc_pw.h" // use write_wfc_pw +#include "source_io/write_dos_pw.h" // use write_dos_pw +#include "source_io/to_wannier90_pw.h" // wannier90 interface #include "source_pw/module_pwdft/onsite_projector.h" // use projector - -/* -#include "source_io/ctrl_output_fp.h" // use ctrl_output_fp() -#include "source_estate/module_charge/symmetry_rho.h" // use Symmetry_rho -#include "source_io/write_elecstat_pot.h" // use write_elecstat_pot -#include "source_io/write_elf.h" -#include "cube_io.h" // use write_vdata_palgrid -#include "source_hamilt/module_xc/xc_functional.h" // use XC_Functional - -#ifdef USE_LIBXC -#include "source_io/write_libxc_r.h" -#endif -*/ - -namespace ModuleIO -{ - -void ctrl_iter_pw(const int istep, +#include "source_io/numerical_basis.h" +#include "source_io/numerical_descriptor.h" +#include "source_io/cal_ldos.h" +#include "source_io/berryphase.h" +#include "source_lcao/module_deltaspin/spin_constrain.h" +#include "source_base/formatter.h" +#include "source_io/get_pchg_pw.h" +#include "source_io/get_wf_pw.h" +#include "source_pw/module_pwdft/elecond.h" + +void ModuleIO::ctrl_iter_pw(const int istep, const int iter, const double &conv_esolver, psi::Psi, base_device::DEVICE_CPU>* psi, @@ -82,7 +77,9 @@ void ctrl_iter_pw(const int istep, template -void ctrl_scf_pw(elecstate::ElecState* pelec, +void ModuleIO::ctrl_scf_pw(const int istep, + const UnitCell& ucell, + elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, const ModulePW::PW_Basis_K *pw_wfc, @@ -92,7 +89,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, - const Device* ctx, +// const Device* ctx, const Parallel_Grid ¶_grid, const Input_para& inp) { @@ -169,8 +166,11 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, ? new psi::Psi, Device>(kspw_psi[0]) : reinterpret_cast, Device>*>(kspw_psi); + const int nbands = kspw_psi->get_nbands(); + +/* ModuleIO::get_pchg_pw(inp.out_pchg, - kspw_psi->get_nbands(), + nbands, inp.nspin, pw_rhod->nxyz, chr.ngmc, @@ -186,6 +186,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, GlobalV::KPAR, GlobalV::MY_POOL, &chr); +*/ } @@ -236,9 +237,11 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, //------------------------------------------------------------------ if (inp.onsite_radius > 0) { // float type has not been implemented +/* auto* onsite_p = projectors::OnsiteProjector::get_instance(); onsite_p->cal_occupations(reinterpret_cast, Device>*>(kspw_psi), pelec->wg); +*/ } ModuleBase::timer::tick("ModuleIO", "ctrl_scf_pw"); @@ -246,19 +249,20 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, } template -void ctrl_runner_pw(UnitCell& ucell, +void ModuleIO::ctrl_runner_pw(UnitCell& ucell, elecstate::ElecState* pelec, ModulePW::PW_Basis_K* pw_wfc, ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, + const K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, - const Device* ctx, +// const Device* ctx, Parallel_Grid ¶_grid, const Input_para& inp) { @@ -312,6 +316,7 @@ void ctrl_runner_pw(UnitCell& ucell, ? new psi::Psi, Device>(kspw_psi[0]) : reinterpret_cast, Device>*>(kspw_psi); +/* ModuleIO::get_wf_pw(inp.out_wfc_norm, inp.out_wfc_re_im, kspw_psi->get_nbands(), @@ -326,6 +331,7 @@ void ctrl_runner_pw(UnitCell& ucell, kv, GlobalV::KPAR, GlobalV::MY_POOL); +*/ } //---------------------------------------------------------- @@ -333,6 +339,7 @@ void ctrl_runner_pw(UnitCell& ucell, //---------------------------------------------------------- if (inp.cal_cond) { +/* EleCond elec_cond(&ucell, &kv, pelec, pw_wfc, kspw_psi, &ppcell); elec_cond.KG(inp.cond_smear, inp.cond_fwhm, @@ -341,6 +348,7 @@ void ctrl_runner_pw(UnitCell& ucell, inp.cond_dt, inp.cond_nonlocal, pelec->wg); +*/ } #ifdef __MLALGO @@ -383,6 +391,8 @@ void ctrl_runner_pw(UnitCell& ucell, } template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( + const int nstep, + const UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -391,13 +401,15 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, psi::Psi, base_device::DEVICE_CPU>* psi, - psi::Psi, base_device::DEVICE_CPU>* kspw_psi, - psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, - const base_device::DEVICE_CPU* ctx, + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device +// const base_device::DEVICE_CPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( + const int nstep, + const UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -406,13 +418,16 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CP const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, psi::Psi, base_device::DEVICE_CPU>* psi, - psi::Psi, base_device::DEVICE_CPU>* kspw_psi, - psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, - const base_device::DEVICE_CPU* ctx, + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device +// const base_device::DEVICE_CPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); +#if ((defined __CUDA) || (defined __ROCM)) template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( + const int nstep, + const UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -421,13 +436,15 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, psi::Psi, base_device::DEVICE_CPU>* psi, - psi::Psi, base_device::DEVICE_GPU>* kspw_psi, - psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, - const base_device::DEVICE_CPU* ctx, + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device +// const base_device::DEVICE_CPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( + const int nstep, + const UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -436,11 +453,83 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GP const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, psi::Psi, base_device::DEVICE_CPU>* psi, - psi::Psi, base_device::DEVICE_GPU>* kspw_psi, - psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, - const base_device::DEVICE_CPU* ctx, + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device + //const base_device::DEVICE_CPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); +#endif + +template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_CPU>( + UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_K* pw_wfc, + ModulePW::PW_Basis* pw_rho, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + const K_Vectors &kv, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device + Structure_Factor &sf, + pseudopot_cell_vnl &ppcell, + surchem &solvent, +// const Device* ctx, + Parallel_Grid ¶_grid, + const Input_para& inp); + +template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_CPU>( + UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_K* pw_wfc, + ModulePW::PW_Basis* pw_rho, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + const K_Vectors &kv, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device + Structure_Factor &sf, + pseudopot_cell_vnl &ppcell, + surchem &solvent, +// const Device* ctx, + Parallel_Grid ¶_grid, + const Input_para& inp); +#if ((defined __CUDA) || (defined __ROCM)) +template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_GPU>( + UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_K* pw_wfc, + ModulePW::PW_Basis* pw_rho, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + const K_Vectors &kv, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device + Structure_Factor &sf, + pseudopot_cell_vnl &ppcell, + surchem &solvent, +// const Device* ctx, + Parallel_Grid ¶_grid, + const Input_para& inp); -} // End ModuleIO +template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_GPU>( + UnitCell& ucell, + elecstate::ElecState* pelec, + ModulePW::PW_Basis_K* pw_wfc, + ModulePW::PW_Basis* pw_rho, + ModulePW::PW_Basis* pw_rhod, + Charge &chr, + const K_Vectors &kv, + psi::Psi, base_device::DEVICE_CPU>* psi, + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device + psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device + Structure_Factor &sf, + pseudopot_cell_vnl &ppcell, + surchem &solvent, +// const Device* ctx, + Parallel_Grid ¶_grid, + const Input_para& inp); +#endif diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 010749c805..492b3be6f8 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -1,7 +1,8 @@ #ifndef CTRL_OUTPUT_PW_H #define CTRL_OUTPUT_PW_H -#include "source_psi/psi_init.h" // about psi +#include "source_base/module_device/device.h" // use Device +#include "source_psi/psi.h" // define psi #include "source_estate/elecstate_lcao.h" // use pelec namespace ModuleIO @@ -16,7 +17,9 @@ void ctrl_iter_pw(const int istep, const Input_para& inp); template -void ctrl_scf_pw(elecstate::ElecState* pelec, +void ctrl_scf_pw(const int istep, + const UnitCell& ucell, + elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, const ModulePW::PW_Basis_K *pw_wfc, @@ -26,7 +29,7 @@ void ctrl_scf_pw(elecstate::ElecState* pelec, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, - const Device* ctx, +// const Device* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); @@ -37,13 +40,14 @@ void ctrl_runner_pw(UnitCell& ucell, ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, + const K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, - const Device* ctx, +// const Device* ctx, Parallel_Grid ¶_grid, const Input_para& inp); From 55349809cf3961e9108ea67a7db80b4d89a087b0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 16:31:43 +0800 Subject: [PATCH 13/60] finally I understand Devicegit add ../source/source_base/module_device/device.h ../source/source_io/ctrl_output_pw.cpp ../source/source_io/ctrl_output_pw.h! --- source/source_base/module_device/device.h | 1 - source/source_io/ctrl_output_pw.cpp | 6 +++--- source/source_io/ctrl_output_pw.h | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/source_base/module_device/device.h b/source/source_base/module_device/device.h index 1b3498cc60..7b8dd0c6ae 100644 --- a/source/source_base/module_device/device.h +++ b/source/source_base/module_device/device.h @@ -63,7 +63,6 @@ int get_node_rank_with_mpi_shared(const MPI_Comm mpi_comm = MPI_COMM_WORLD); int stringCmp(const void* a, const void* b); #ifdef __CUDA - int set_device_by_rank(const MPI_Comm mpi_comm = MPI_COMM_WORLD); #endif diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 65ab3c2583..387330d9f2 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -89,7 +89,7 @@ void ModuleIO::ctrl_scf_pw(const int istep, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, -// const Device* ctx, + const Device* ctx, const Parallel_Grid ¶_grid, const Input_para& inp) { @@ -403,7 +403,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device -// const base_device::DEVICE_CPU* ctx, + const base_device::DEVICE_CPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); @@ -420,7 +420,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CP psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device -// const base_device::DEVICE_CPU* ctx, + const base_device::DEVICE_CPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 492b3be6f8..2713be3719 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -29,7 +29,7 @@ void ctrl_scf_pw(const int istep, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, -// const Device* ctx, + const Device* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); From 9a1c9fdb602696e7ed9f0dc7c391717ef116b21f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 16:36:31 +0800 Subject: [PATCH 14/60] update function variables --- source/source_io/ctrl_output_pw.cpp | 22 +++++++++++++++------- source/source_io/ctrl_output_pw.h | 9 ++++++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 387330d9f2..f69b4c5748 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -262,7 +262,7 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, -// const Device* ctx, + const Device* ctx, Parallel_Grid ¶_grid, const Input_para& inp) { @@ -390,6 +390,7 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, ModuleBase::timer::tick("ModuleIO", "ctrl_runner_pw"); } +// complex + CPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( const int nstep, const UnitCell& ucell, @@ -407,6 +408,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU const Parallel_Grid ¶_grid, const Input_para& inp); +// complex + CPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( const int nstep, const UnitCell& ucell, @@ -425,6 +427,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CP const Input_para& inp); #if ((defined __CUDA) || (defined __ROCM)) +// complex + GPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( const int nstep, const UnitCell& ucell, @@ -438,10 +441,11 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device -// const base_device::DEVICE_CPU* ctx, + const base_device::DEVICE_GPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); +// complex + GPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( const int nstep, const UnitCell& ucell, @@ -455,11 +459,12 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GP psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device - //const base_device::DEVICE_CPU* ctx, + const base_device::DEVICE_GPU* ctx, const Parallel_Grid ¶_grid, const Input_para& inp); #endif +// complex + CPU template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_CPU>( UnitCell& ucell, elecstate::ElecState* pelec, @@ -474,10 +479,11 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_ Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, -// const Device* ctx, + const base_device::DEVICE_CPU* ctx, Parallel_Grid ¶_grid, const Input_para& inp); +// complex + CPU template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_CPU>( UnitCell& ucell, elecstate::ElecState* pelec, @@ -492,11 +498,12 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, -// const Device* ctx, + const base_device::DEVICE_CPU* ctx, Parallel_Grid ¶_grid, const Input_para& inp); #if ((defined __CUDA) || (defined __ROCM)) +// complex + GPU template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_GPU>( UnitCell& ucell, elecstate::ElecState* pelec, @@ -511,10 +518,11 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_ Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, -// const Device* ctx, + const base_device::DEVICE_GPU* ctx, Parallel_Grid ¶_grid, const Input_para& inp); +// complex + GPU template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_GPU>( UnitCell& ucell, elecstate::ElecState* pelec, @@ -529,7 +537,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, -// const Device* ctx, + const base_device::DEVICE_GPU* ctx, Parallel_Grid ¶_grid, const Input_para& inp); #endif diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 2713be3719..987ca2a92f 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -2,12 +2,13 @@ #define CTRL_OUTPUT_PW_H #include "source_base/module_device/device.h" // use Device -#include "source_psi/psi.h" // define psi -#include "source_estate/elecstate_lcao.h" // use pelec +#include "source_psi/psi.h" // define psi +#include "source_estate/elecstate_lcao.h" // use pelec namespace ModuleIO { +// print out information in 'iter_finish' in ESolver_KS_PW void ctrl_iter_pw(const int istep, const int iter, const double &conv_esolver, @@ -16,6 +17,7 @@ void ctrl_iter_pw(const int istep, const ModulePW::PW_Basis_K *pw_wfc, const Input_para& inp); +// print out information in 'after_scf' in ESolver_KS_PW template void ctrl_scf_pw(const int istep, const UnitCell& ucell, @@ -33,6 +35,7 @@ void ctrl_scf_pw(const int istep, const Parallel_Grid ¶_grid, const Input_para& inp); +// print out information in 'after_all_runners' in ESolver_KS_PW template void ctrl_runner_pw(UnitCell& ucell, elecstate::ElecState* pelec, @@ -47,7 +50,7 @@ void ctrl_runner_pw(UnitCell& ucell, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, -// const Device* ctx, + const Device* ctx, Parallel_Grid ¶_grid, const Input_para& inp); From 40fa9f081014ec2f8d88fa7c6157cdae385b0b70 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 16:49:40 +0800 Subject: [PATCH 15/60] one step further --- source/source_io/ctrl_output_pw.cpp | 15 +++++++-------- source/source_io/ctrl_output_pw.h | 2 +- source/source_io/get_pchg_pw.h | 3 ++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index f69b4c5748..4f6f04b50f 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -78,7 +78,7 @@ void ModuleIO::ctrl_iter_pw(const int istep, template void ModuleIO::ctrl_scf_pw(const int istep, - const UnitCell& ucell, + UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -167,13 +167,13 @@ void ModuleIO::ctrl_scf_pw(const int istep, : reinterpret_cast, Device>*>(kspw_psi); const int nbands = kspw_psi->get_nbands(); + const int ngmc = chr.ngmc; -/* ModuleIO::get_pchg_pw(inp.out_pchg, nbands, inp.nspin, pw_rhod->nxyz, - chr.ngmc, + ngmc, &ucell, __kspw_psi, pw_rhod, @@ -186,7 +186,6 @@ void ModuleIO::ctrl_scf_pw(const int istep, GlobalV::KPAR, GlobalV::MY_POOL, &chr); -*/ } @@ -393,7 +392,7 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, // complex + CPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( const int nstep, - const UnitCell& ucell, + UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -411,7 +410,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU // complex + CPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU>( const int nstep, - const UnitCell& ucell, + UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -430,7 +429,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CP // complex + GPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( const int nstep, - const UnitCell& ucell, + UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, @@ -448,7 +447,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU // complex + GPU template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU>( const int nstep, - const UnitCell& ucell, + UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 987ca2a92f..2cf9cc41ae 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -20,7 +20,7 @@ void ctrl_iter_pw(const int istep, // print out information in 'after_scf' in ESolver_KS_PW template void ctrl_scf_pw(const int istep, - const UnitCell& ucell, + UnitCell& ucell, elecstate::ElecState* pelec, const Charge &chr, const K_Vectors &kv, diff --git a/source/source_io/get_pchg_pw.h b/source/source_io/get_pchg_pw.h index 7c2d14fe6a..2a61c77aa3 100644 --- a/source/source_io/get_pchg_pw.h +++ b/source/source_io/get_pchg_pw.h @@ -1,7 +1,8 @@ #ifndef GET_PCHG_PW_H #define GET_PCHG_PW_H -#include "cube_io.h" +#include "source_io/cube_io.h" +#include "source_estate/module_charge/symmetry_rho.h" namespace ModuleIO { From 155c18d051e5c1fd4e7526f25b6c1abd2d32d28f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 17:01:27 +0800 Subject: [PATCH 16/60] move on --- source/source_io/ctrl_output_pw.cpp | 17 ++++++----------- source/source_io/ctrl_output_pw.h | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 4f6f04b50f..67d143a274 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -236,11 +236,9 @@ void ModuleIO::ctrl_scf_pw(const int istep, //------------------------------------------------------------------ if (inp.onsite_radius > 0) { // float type has not been implemented -/* auto* onsite_p = projectors::OnsiteProjector::get_instance(); onsite_p->cal_occupations(reinterpret_cast, Device>*>(kspw_psi), pelec->wg); -*/ } ModuleBase::timer::tick("ModuleIO", "ctrl_scf_pw"); @@ -254,7 +252,7 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, - const K_Vectors &kv, + K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, @@ -315,7 +313,6 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, ? new psi::Psi, Device>(kspw_psi[0]) : reinterpret_cast, Device>*>(kspw_psi); -/* ModuleIO::get_wf_pw(inp.out_wfc_norm, inp.out_wfc_re_im, kspw_psi->get_nbands(), @@ -330,7 +327,6 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, kv, GlobalV::KPAR, GlobalV::MY_POOL); -*/ } //---------------------------------------------------------- @@ -338,7 +334,7 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, //---------------------------------------------------------- if (inp.cal_cond) { -/* + using Real = typename GetTypeReal::type; EleCond elec_cond(&ucell, &kv, pelec, pw_wfc, kspw_psi, &ppcell); elec_cond.KG(inp.cond_smear, inp.cond_fwhm, @@ -347,7 +343,6 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, inp.cond_dt, inp.cond_nonlocal, pelec->wg); -*/ } #ifdef __MLALGO @@ -471,7 +466,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_ ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, - const K_Vectors &kv, + K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device @@ -490,7 +485,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, - const K_Vectors &kv, + K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_CPU>* __kspw_psi, // Device @@ -510,7 +505,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_ ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, - const K_Vectors &kv, + K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device @@ -529,7 +524,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, - const K_Vectors &kv, + K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // T and Device psi::Psi, base_device::DEVICE_GPU>* __kspw_psi, // Device diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index 2cf9cc41ae..87fea245b0 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -43,7 +43,7 @@ void ctrl_runner_pw(UnitCell& ucell, ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis* pw_rhod, Charge &chr, - const K_Vectors &kv, + K_Vectors &kv, psi::Psi, base_device::DEVICE_CPU>* psi, psi::Psi* kspw_psi, psi::Psi, Device>* __kspw_psi, From 9c1c37672cff1387ae28dd52ae23b1b463829c42 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 27 Sep 2025 17:13:21 +0800 Subject: [PATCH 17/60] update codes, done! --- source/source_esolver/esolver_ks_pw.cpp | 54 ++++++++----------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 425dc2ef23..daba4282c5 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -1,6 +1,5 @@ #include "esolver_ks_pw.h" -//#include "source_base/formatter.h" #include "source_base/global_variable.h" #include "source_base/kernels/math_kernel_op.h" #include "source_base/memory.h" @@ -19,17 +18,12 @@ #include "source_pw/module_pwdft/onsite_projector.h" #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/VSep_in_pw.h" -//#include "source_pw/module_pwdft/elecond.h" #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/hamilt_pw.h" #include "source_pw/module_pwdft/stress_pw.h" #include -//#ifdef __MLALGO -//#include "source_io/write_mlkedf_descriptors.h" -//#endif - #include #include @@ -55,7 +49,6 @@ ESolver_KS_PW::ESolver_KS_PW() template ESolver_KS_PW::~ESolver_KS_PW() { - // delete Hamilt this->deallocate_hamilt(); @@ -495,6 +488,7 @@ void ESolver_KS_PW::hamilt2rho_single(UnitCell& ucell, const int iste skip_solve = true; } } + if (!skip_solve) { hsolver::HSolverPW hsolver_pw_obj(this->pw_wfc, @@ -645,8 +639,8 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } -// ModuleIO::ctrl_iter_pw(istep, iter, conv_esolver, this->psi, - // this->kv, this->pw_wfc, PARAM.inp); + ModuleIO::ctrl_iter_pw(istep, iter, conv_esolver, this->psi, + this->kv, this->pw_wfc, PARAM.inp); } template @@ -681,9 +675,9 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const this->psi[0].size()); } -// ModuleIO::ctrl_scf_pw(istep, ucell, this->pelec, this->chr, this->kv, this->pw_wfc, - // this->pw_rho, this->pw_rhod, this->pw_big, this->psi, this->kspw_psi, - // this->__kspw_psi, /* this->ctx,*/ this->Pgrid, PARAM.inp); + ModuleIO::ctrl_scf_pw(istep, ucell, this->pelec, this->chr, this->kv, this->pw_wfc, + this->pw_rho, this->pw_rhod, this->pw_big, this->psi, this->kspw_psi, + this->__kspw_psi, this->ctx, this->Pgrid, PARAM.inp); ModuleBase::timer::tick("ESolver_KS_PW", "after_scf"); } @@ -710,18 +704,9 @@ void ESolver_KS_PW::cal_force(UnitCell& ucell, ModuleBase::matrix& fo : reinterpret_cast, Device>*>(this->kspw_psi); // Calculate forces - ff.cal_force(ucell, - force, - *this->pelec, - this->pw_rhod, - &ucell.symm, - &this->sf, - this->solvent, - &this->locpp, - &this->ppcell, - &this->kv, - this->pw_wfc, - this->__kspw_psi); + ff.cal_force(ucell, force, *this->pelec, this->pw_rhod, &ucell.symm, + &this->sf, this->solvent, &this->locpp, &this->ppcell, + &this->kv, this->pw_wfc, this->__kspw_psi); } template @@ -738,16 +723,9 @@ void ESolver_KS_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& s this->__kspw_psi = PARAM.inp.precision == "single" ? new psi::Psi, Device>(this->kspw_psi[0]) : reinterpret_cast, Device>*>(this->kspw_psi); - ss.cal_stress(stress, - ucell, - this->locpp, - this->ppcell, - this->pw_rhod, - &ucell.symm, - &this->sf, - &this->kv, - this->pw_wfc, - this->__kspw_psi); + + ss.cal_stress(stress, ucell, this->locpp, this->ppcell, this->pw_rhod, + &ucell.symm, &this->sf, &this->kv, this->pw_wfc, this->__kspw_psi); // external stress double unit_transform = 0.0; @@ -767,10 +745,10 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) //---------------------------------------------------------- ESolver_KS::after_all_runners(ucell); - //ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, - // this->pw_rho, this->pw_rhod, this->chr, this->psi, - // this->kspw_psi, this->__kspw_psi, this->sf, - // this->ppcell, this->solvent, /* this->ctx,*/ this->Pgrid, PARAM.inp); + ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, + this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, + this->kspw_psi, this->__kspw_psi, this->sf, + this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); } From e6623f1d2dae1df4654a39a5cc622471580ff1a0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 28 Sep 2025 21:34:17 +0800 Subject: [PATCH 18/60] fix bug --- source/source_io/ctrl_output_pw.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index 67d143a274..a8c588996a 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -14,6 +14,10 @@ #include "source_io/get_wf_pw.h" #include "source_pw/module_pwdft/elecond.h" +#ifdef __MLALGO +#include "source_io/write_mlkedf_descriptors.h" +#endif + void ModuleIO::ctrl_iter_pw(const int istep, const int iter, const double &conv_esolver, From 2cc87f9bed00531175058f69935f8e32ebcfcb20 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 29 Sep 2025 16:56:35 +0800 Subject: [PATCH 19/60] add setup_pot --- source/Makefile.Objects | 1 + source/source_esolver/esolver_ks_pw.cpp | 102 ++---------- source/source_pw/module_pwdft/CMakeLists.txt | 1 + source/source_pw/module_pwdft/forces_scc.cpp | 154 +++++++++++-------- source/source_pw/module_pwdft/setup_pot.cpp | 120 +++++++++++++++ source/source_pw/module_pwdft/setup_pot.h | 22 +++ 6 files changed, 246 insertions(+), 154 deletions(-) create mode 100644 source/source_pw/module_pwdft/setup_pot.cpp create mode 100644 source/source_pw/module_pwdft/setup_pot.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 8d4d9933a1..dc638d4199 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -712,6 +712,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ charge_mixing_rho.o\ charge_mixing_uspp.o\ fp_energy.o\ + setup_pot.o\ forces.o\ forces_us.o\ forces_nl.o\ diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index daba4282c5..72e37fb03d 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -18,8 +18,9 @@ #include "source_pw/module_pwdft/onsite_projector.h" #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/VSep_in_pw.h" -#include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/hamilt_pw.h" + +#include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/stress_pw.h" #include @@ -33,6 +34,7 @@ #include +#include "source/source_pw/module_pwdft/setup_pot.h" // mohan add 20250929 #include "source_io/ctrl_output_pw.h" // mohan add 20250927 namespace ModuleESolver @@ -251,96 +253,14 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) this->allocate_hamilt(ucell); //---------------------------------------------------------- - //! 4) DFT-1/2 calculations, sep potential need to generate before effective potential calculation - //---------------------------------------------------------- - if (PARAM.inp.dfthalf_type > 0) - { - this->vsep_cell->generate_vsep_r(this->pw_rhod[0], this->sf.strucFac, ucell.sep_cell); - } - - //---------------------------------------------------------- - //! 5) Renew local pseudopotential - //---------------------------------------------------------- - this->pelec - ->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm, (void*)this->pw_wfc); - - //---------------------------------------------------------- - //! 6) Symmetrize the charge density (rho) - //---------------------------------------------------------- - - //! Symmetry_rho should behind init_scf, because charge should be - //! initialized first. liuyu comment: Symmetry_rho should be - //! located between init_rho and v_of_rho? - Symmetry_rho srho; - for (int is = 0; is < PARAM.inp.nspin; is++) - { - srho.begin(is, this->chr, this->pw_rhod, ucell.symm); - } - - //---------------------------------------------------------- - //! 7) Calculate the effective potential with rho + // 4) setup potentials (local, non-local, sc, +U, DFT-1/2) //---------------------------------------------------------- - //! liuyu move here 2023-10-09 - //! D in uspp need vloc, thus behind init_scf() - //! calculate the effective coefficient matrix - //! for non-local pseudopotential projectors - ModuleBase::matrix veff = this->pelec->pot->get_effective_v(); - - this->ppcell.cal_effective_D(veff, this->pw_rhod, ucell); - - //---------------------------------------------------------- - //! 8) Onsite projectors - //---------------------------------------------------------- - if (PARAM.inp.onsite_radius > 0) - { - auto* onsite_p = projectors::OnsiteProjector::get_instance(); - onsite_p->init(PARAM.inp.orbital_dir, - &ucell, - *(this->kspw_psi), - this->kv, - *(this->pw_wfc), - this->sf, - PARAM.inp.onsite_radius, - PARAM.globalv.nqx, - PARAM.globalv.dq, - this->pelec->wg, - this->pelec->ekb); - } - - //---------------------------------------------------------- - //! 9) Spin-constrained algorithms - //---------------------------------------------------------- - if (PARAM.inp.sc_mag_switch) - { - spinconstrain::SpinConstrain>& sc - = spinconstrain::SpinConstrain>::getScInstance(); - sc.init_sc(PARAM.inp.sc_thr, - PARAM.inp.nsc, - PARAM.inp.nsc_min, - PARAM.inp.alpha_trial, - PARAM.inp.sccut, - PARAM.inp.sc_drop_thr, - ucell, - nullptr, - PARAM.inp.nspin, - this->kv, - this->p_hamilt, - this->kspw_psi, - this->pelec, - this->pw_wfc); - } - - //---------------------------------------------------------- - //! 10) DFT+U algorithm - //---------------------------------------------------------- - if (PARAM.inp.dft_plus_u) - { - auto* dftu = ModuleDFTU::DFTU::get_instance(); - dftu->init(ucell, nullptr, this->kv.get_nks()); - } + setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, + this->chr, this->locpp, this->ppcell, this->vsep_cell, + this->kspw_psi, this->pw_wfc, this->pw_rhod, inp); //---------------------------------------------------------- - //! 10) Initialize wave functions + //! 5) Initialize wave functions //---------------------------------------------------------- if (!this->already_initpsi) { @@ -349,10 +269,10 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) } //---------------------------------------------------------- - //! 11) Exx calculations + //! 6) Exx calculations //---------------------------------------------------------- - if (PARAM.inp.calculation == "scf" || PARAM.inp.calculation == "relax" || PARAM.inp.calculation == "cell-relax" - || PARAM.inp.calculation == "md") + if (PARAM.inp.calculation == "scf" || PARAM.inp.calculation == "relax" + || PARAM.inp.calculation == "cell-relax" || PARAM.inp.calculation == "md") { if (GlobalC::exx_info.info_global.cal_exx && PARAM.inp.basis_type == "pw") { diff --git a/source/source_pw/module_pwdft/CMakeLists.txt b/source/source_pw/module_pwdft/CMakeLists.txt index 8f49801c48..be44682dab 100644 --- a/source/source_pw/module_pwdft/CMakeLists.txt +++ b/source/source_pw/module_pwdft/CMakeLists.txt @@ -12,6 +12,7 @@ list(APPEND objects operator_pw/op_exx_pw.cpp operator_pw/exx_pw_ace.cpp operator_pw/exx_pw_pot.cpp + setup_pot.cpp forces_nl.cpp forces_cc.cpp forces_scc.cpp diff --git a/source/source_pw/module_pwdft/forces_scc.cpp b/source/source_pw/module_pwdft/forces_scc.cpp index 9279a0bbea..1484166b4e 100644 --- a/source/source_pw/module_pwdft/forces_scc.cpp +++ b/source/source_pw/module_pwdft/forces_scc.cpp @@ -75,63 +75,65 @@ void Forces::cal_force_scc(ModuleBase::matrix& forcescc, int igg0 = 0; const int ig0 = rho_basis->ig_gge0; - if (rho_basis->gg_uniq[0] < 1.0e-8) { - igg0 = 1; -} + if (rho_basis->gg_uniq[0] < 1.0e-8) + { + igg0 = 1; + } double fact = 2.0; - for (int nt = 0; nt < ucell_in.ntype; nt++) { - // Here we compute the G.ne.0 term - const int mesh = ucell_in.atoms[nt].ncpp.msh; - this->deriv_drhoc_scc(numeric, - mesh, - ucell_in.atoms[nt].ncpp.r.data(), - ucell_in.atoms[nt].ncpp.rab.data(), - ucell_in.atoms[nt].ncpp.rho_at.data(), - rhocgnt.data(), - rho_basis, - ucell_in); - int iat = 0; - for (int it = 0; it < ucell_in.ntype; it++) { - for (int ia = 0; ia < ucell_in.atoms[it].na; ia++) { - if (nt == it) { - const ModuleBase::Vector3 pos - = ucell_in.atoms[it].tau[ia]; - double &force0 = forcescc(iat, 0), - &force1 = forcescc(iat, 1), - &force2 = forcescc(iat, 2); + for (int nt = 0; nt < ucell_in.ntype; nt++) + { + // Here we compute the G.ne.0 term + const int mesh = ucell_in.atoms[nt].ncpp.msh; + this->deriv_drhoc_scc(numeric, + mesh, + ucell_in.atoms[nt].ncpp.r.data(), + ucell_in.atoms[nt].ncpp.rab.data(), + ucell_in.atoms[nt].ncpp.rho_at.data(), + rhocgnt.data(), + rho_basis, + ucell_in); + int iat = 0; + for (int it = 0; it < ucell_in.ntype; it++) { + for (int ia = 0; ia < ucell_in.atoms[it].na; ia++) { + if (nt == it) { + const ModuleBase::Vector3 pos + = ucell_in.atoms[it].tau[ia]; + double &force0 = forcescc(iat, 0), + &force1 = forcescc(iat, 1), + &force2 = forcescc(iat, 2); #ifdef _OPENMP #pragma omp parallel for reduction(+ : force0) reduction(+ : force1) reduction(+ : force2) #endif - for (int ig = 0; ig < rho_basis->npw; ++ig) { - if (ig == ig0) { - continue; -} - const ModuleBase::Vector3 gv - = rho_basis->gcar[ig]; - const double rhocgntigg - = rhocgnt[rho_basis->ig2igg[ig]]; - const double arg = ModuleBase::TWO_PI * (gv * pos); - double sinp, cosp; - ModuleBase::libm::sincos(arg, &sinp, &cosp); - const std::complex cpm - = std::complex(sinp, cosp) * conj(psic[ig]); - - force0 += fact * rhocgntigg * ucell_in.tpiba - * gv.x * cpm.real(); - force1 += fact * rhocgntigg * ucell_in.tpiba - * gv.y * cpm.real(); - force2 += fact * rhocgntigg * ucell_in.tpiba - * gv.z * cpm.real(); - } - } - iat++; - } - } - } + for (int ig = 0; ig < rho_basis->npw; ++ig) { + if (ig == ig0) { + continue; + } + const ModuleBase::Vector3 gv + = rho_basis->gcar[ig]; + const double rhocgntigg + = rhocgnt[rho_basis->ig2igg[ig]]; + const double arg = ModuleBase::TWO_PI * (gv * pos); + double sinp, cosp; + ModuleBase::libm::sincos(arg, &sinp, &cosp); + const std::complex cpm + = std::complex(sinp, cosp) * conj(psic[ig]); + + force0 += fact * rhocgntigg * ucell_in.tpiba + * gv.x * cpm.real(); + force1 += fact * rhocgntigg * ucell_in.tpiba + * gv.y * cpm.real(); + force2 += fact * rhocgntigg * ucell_in.tpiba + * gv.z * cpm.real(); + } + } + iat++; + } + } + } - Parallel_Reduce::reduce_pool(forcescc.c, forcescc.nr * forcescc.nc); + Parallel_Reduce::reduce_pool(forcescc.c, forcescc.nr * forcescc.nc); ModuleBase::timer::tick("Forces", "cal_force_scc"); return; @@ -163,10 +165,12 @@ void Forces::deriv_drhoc_scc(const bool& numeric, /// /// G=0 term /// - if (rho_basis->gg_uniq[0] < 1.0e-8) { - drhocg[0] = 0.0; + if (rho_basis->gg_uniq[0] < 1.0e-8) + { + drhocg[0] = 0.0; igl0 = 1; - } else { + } else + { igl0 = 0; } @@ -178,17 +182,19 @@ void Forces::deriv_drhoc_scc(const bool& numeric, #ifdef _OPENMP #pragma omp parallel for #endif - for (int igl = igl0; igl < rho_basis->ngg; igl++) { - gx_arr[igl] = sqrt(rho_basis->gg_uniq[igl]) * ucell_in.tpiba; - } + for (int igl = igl0; igl < rho_basis->ngg; igl++) + { + gx_arr[igl] = sqrt(rho_basis->gg_uniq[igl]) * ucell_in.tpiba; + } double *r_d = nullptr; double *rhoc_d = nullptr; double *rab_d = nullptr; double *aux_d = nullptr; double *drhocg_d = nullptr; - if (this->device == base_device::GpuDevice) { - resmem_var_op()(r_d, mesh); + if (this->device == base_device::GpuDevice) + { + resmem_var_op()(r_d, mesh); resmem_var_op()(rhoc_d, mesh); resmem_var_op()(rab_d, mesh); @@ -204,14 +210,36 @@ void Forces::deriv_drhoc_scc(const bool& numeric, syncmem_var_h2d_op()(rhoc_d, rhoc, mesh); } - if(this->device == base_device::GpuDevice) { + if(this->device == base_device::GpuDevice) + { hamilt::cal_stress_drhoc_aux_op()( - r_d,rhoc_d,gx_arr_d+igl0,rab_d,drhocg_d+igl0,mesh,igl0,rho_basis->ngg-igl0,ucell_in.omega,2); + r_d, + rhoc_d, + gx_arr_d+igl0, + rab_d, + drhocg_d+igl0, + mesh, + igl0, + rho_basis->ngg-igl0, + ucell_in.omega, + 2); + syncmem_var_d2h_op()(drhocg+igl0, drhocg_d+igl0, rho_basis->ngg-igl0); - } else { + } + else + { hamilt::cal_stress_drhoc_aux_op()( - r,rhoc,gx_arr.data()+igl0,rab,drhocg+igl0,mesh,igl0,rho_basis->ngg-igl0,ucell_in.omega,2); + r, + rhoc, + gx_arr.data()+igl0, + rab, + drhocg+igl0, + mesh, + igl0, + rho_basis->ngg-igl0, + ucell_in.omega, + 2); } @@ -226,4 +254,4 @@ void Forces::deriv_drhoc_scc(const bool& numeric, template class Forces; #if ((defined __CUDA) || (defined __ROCM)) template class Forces; -#endif \ No newline at end of file +#endif diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp new file mode 100644 index 0000000000..e25d5c9f0d --- /dev/null +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -0,0 +1,120 @@ +#include "source_pw/module_pwdft/setup_pot.h" + +#include "source_estate/module_charge/symmetry_rho.h" +#include "source_lcao/module_deltaspin/spin_constrain.h" +#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_lcao/module_dftu/dftu.h" +#include "source_pw/module_pwdft/VSep_in_pw.h" + +void setup_pot(const int istep, + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi* kspw_psi, // electronic wave functions + const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp) // input parameters +{ + //---------------------------------------------------------- + //! 1) Renew local pseudopotential + //---------------------------------------------------------- + pelec->init_scf(istep, + ucell, + para_grid, + sf, + locpp.numeric, + ucell.symm, + (void*)pw_wfc); + + //---------------------------------------------------------- + //! 2) Symmetrize the charge density (rho) + //---------------------------------------------------------- + + //! Symmetry_rho should behind init_scf, because charge should be + //! initialized first. liuyu comment: Symmetry_rho should be + //! located between init_rho and v_of_rho? + Symmetry_rho srho; + for (int is = 0; is < inp.nspin; is++) + { + srho.begin(is, chr, pw_rhod, ucell.symm); + } + + //---------------------------------------------------------- + //! 3) Calculate the effective potential with rho + //---------------------------------------------------------- + //! liuyu move here 2023-10-09 + //! D in uspp need vloc, thus behind init_scf() + //! calculate the effective coefficient matrix + //! for non-local pseudopotential projectors + ModuleBase::matrix veff = pelec->pot->get_effective_v(); + + ppcell.cal_effective_D(veff, pw_rhod, ucell); + + //---------------------------------------------------------- + //! 4) Onsite projectors + //---------------------------------------------------------- + if (PARAM.inp.onsite_radius > 0) + { + auto* onsite_p = projectors::OnsiteProjector::get_instance(); + onsite_p->init(PARAM.inp.orbital_dir, + &ucell, + *(kspw_psi), + this->kv, + *(pw_wfc), + this->sf, + PARAM.inp.onsite_radius, + PARAM.globalv.nqx, + PARAM.globalv.dq, + this->pelec->wg, + this->pelec->ekb); + } + + //---------------------------------------------------------- + //! 5) Spin-constrained algorithms + //---------------------------------------------------------- + if (PARAM.inp.sc_mag_switch) + { + spinconstrain::SpinConstrain>& sc + = spinconstrain::SpinConstrain>::getScInstance(); + sc.init_sc(PARAM.inp.sc_thr, + PARAM.inp.nsc, + PARAM.inp.nsc_min, + PARAM.inp.alpha_trial, + PARAM.inp.sccut, + PARAM.inp.sc_drop_thr, + ucell, + nullptr, + PARAM.inp.nspin, + this->kv, + this->p_hamilt, + this->kspw_psi, + this->pelec, + this->pw_wfc); + } + + //---------------------------------------------------------- + //! 6) DFT+U algorithm + //---------------------------------------------------------- + if (PARAM.inp.dft_plus_u) + { + auto* dftu = ModuleDFTU::DFTU::get_instance(); + dftu->init(ucell, nullptr, this->kv.get_nks()); + } + + //---------------------------------------------------------- + //! 7) DFT-1/2 calculations, sep potential need to generate + // before effective potential calculation + //---------------------------------------------------------- + if (PARAM.inp.dfthalf_type > 0) + { + this->vsep_cell->generate_vsep_r(this->pw_rhod[0], this->sf.strucFac, ucell.sep_cell); + } + + return; +} diff --git a/source/source_pw/module_pwdft/setup_pot.h b/source/source_pw/module_pwdft/setup_pot.h new file mode 100644 index 0000000000..926e911d58 --- /dev/null +++ b/source/source_pw/module_pwdft/setup_pot.h @@ -0,0 +1,22 @@ +#ifndef SETUP_POT_H +#define SETUP_POT_H + + +void setup_pot(const int istep, + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi* kspw_psi, // electronic wave functions + const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + + + +#endif From 5d2fc355d9c6e52364b121ec2c87c3f03de52c0b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 30 Sep 2025 12:29:25 +0800 Subject: [PATCH 20/60] fix bugs --- source/source_esolver/esolver_ks_pw.cpp | 4 +-- source/source_pw/module_pwdft/setup_pot.cpp | 34 +++++++++++++++++++++ source/source_pw/module_pwdft/setup_pot.h | 6 ++++ source/source_pw/module_pwdft/stress_pw.h | 4 +-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 72e37fb03d..4788131a6c 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -34,7 +34,7 @@ #include -#include "source/source_pw/module_pwdft/setup_pot.h" // mohan add 20250929 +#include "source_pw/module_pwdft/setup_pot.h" // mohan add 20250929 #include "source_io/ctrl_output_pw.h" // mohan add 20250927 namespace ModuleESolver @@ -257,7 +257,7 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) //---------------------------------------------------------- setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, this->chr, this->locpp, this->ppcell, this->vsep_cell, - this->kspw_psi, this->pw_wfc, this->pw_rhod, inp); + this->kspw_psi, this->pw_wfc, this->pw_rhod, PARAM.inp); //---------------------------------------------------------- //! 5) Initialize wave functions diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index e25d5c9f0d..e6764b8035 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -6,6 +6,7 @@ #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/VSep_in_pw.h" +template void setup_pot(const int istep, UnitCell& ucell, // unitcell const K_Vectors &kv, // kpoints @@ -118,3 +119,36 @@ void setup_pot(const int istep, return; } + +template void setup_pot, base_device::DEVICE_CPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_devcie::DEVICE_CPU>* kspw_psi, // electronic wave functions + const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + + +template void setup_pot, base_device::DEVICE_CPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_devcie::DEVICE_CPU>* kspw_psi, // electronic wave functions + const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters diff --git a/source/source_pw/module_pwdft/setup_pot.h b/source/source_pw/module_pwdft/setup_pot.h index 926e911d58..63047864c1 100644 --- a/source/source_pw/module_pwdft/setup_pot.h +++ b/source/source_pw/module_pwdft/setup_pot.h @@ -1,7 +1,13 @@ #ifndef SETUP_POT_H #define SETUP_POT_H +#include "source_cell/unitcell.h" +#include "source_cell/klist.h" +#include "source_pw/module_pwdft/structure_factor.h" +#include "source_estate/elecstate.h" +#include "source_pw/module_pwdft/VL_in_pw.h" +template void setup_pot(const int istep, UnitCell& ucell, // unitcell const K_Vectors &kv, // kpoints diff --git a/source/source_pw/module_pwdft/stress_pw.h b/source/source_pw/module_pwdft/stress_pw.h index 027bc56039..a9ac9decb5 100644 --- a/source/source_pw/module_pwdft/stress_pw.h +++ b/source/source_pw/module_pwdft/stress_pw.h @@ -1,5 +1,5 @@ -#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_source_pw_HAMILT_PWDFT_STRESS_PW_H -#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_source_pw_HAMILT_PWDFT_STRESS_PW_H +#ifndef STRESS_PW_H +#define STRESS_PW_H #include "source_estate/elecstate.h" #include "source_pw/module_pwdft/VL_in_pw.h" From 632b1c6517b8b2ae8accb19ae291969e09b1e528 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 30 Sep 2025 23:00:34 +0800 Subject: [PATCH 21/60] update esolver_ks_pw, add setup_pot --- source/source_esolver/esolver_ks_pw.cpp | 4 +- .../source_lcao/module_deltaspin/init_sc.cpp | 4 +- .../module_deltaspin/spin_constrain.cpp | 4 +- .../module_deltaspin/spin_constrain.h | 4 +- source/source_pw/module_pwdft/setup_pot.cpp | 43 ++++++++++--------- source/source_pw/module_pwdft/setup_pot.h | 10 ++++- 6 files changed, 40 insertions(+), 29 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 4788131a6c..8c7734d2da 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -255,9 +255,9 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) //---------------------------------------------------------- // 4) setup potentials (local, non-local, sc, +U, DFT-1/2) //---------------------------------------------------------- - setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, + pw::setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, this->chr, this->locpp, this->ppcell, this->vsep_cell, - this->kspw_psi, this->pw_wfc, this->pw_rhod, PARAM.inp); + this->kspw_psi, this->p_hamilt, this->pw_wfc, this->pw_rhod, PARAM.inp); //---------------------------------------------------------- //! 5) Initialize wave functions diff --git a/source/source_lcao/module_deltaspin/init_sc.cpp b/source/source_lcao/module_deltaspin/init_sc.cpp index fbba82a839..ab192fe133 100644 --- a/source/source_lcao/module_deltaspin/init_sc.cpp +++ b/source/source_lcao/module_deltaspin/init_sc.cpp @@ -11,7 +11,7 @@ void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, const UnitCell& ucell, Parallel_Orbitals* ParaV_in, int nspin_in, - K_Vectors& kv_in, + const K_Vectors& kv_in, void* p_hamilt_in, void* psi_in, elecstate::ElecState* pelec_in, @@ -34,4 +34,4 @@ void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, } template class spinconstrain::SpinConstrain>; -template class spinconstrain::SpinConstrain; \ No newline at end of file +template class spinconstrain::SpinConstrain; diff --git a/source/source_lcao/module_deltaspin/spin_constrain.cpp b/source/source_lcao/module_deltaspin/spin_constrain.cpp index 26cf70cea5..6213f528f7 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.cpp +++ b/source/source_lcao/module_deltaspin/spin_constrain.cpp @@ -487,7 +487,7 @@ double SpinConstrain::get_sc_drop_thr() } template -void SpinConstrain::set_solver_parameters(K_Vectors& kv_in, +void SpinConstrain::set_solver_parameters(const K_Vectors& kv_in, void* p_hamilt_in, void* psi_in, elecstate::ElecState* pelec_in) @@ -603,4 +603,4 @@ void SpinConstrain::print_Mag_Force(std::ofstream& ofs_running) template class SpinConstrain>; template class SpinConstrain; -} // namespace spinconstrain \ No newline at end of file +} // namespace spinconstrain diff --git a/source/source_lcao/module_deltaspin/spin_constrain.h b/source/source_lcao/module_deltaspin/spin_constrain.h index 8f510db57c..bcc4ed9ec1 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.h +++ b/source/source_lcao/module_deltaspin/spin_constrain.h @@ -36,7 +36,7 @@ class SpinConstrain const UnitCell& ucell, Parallel_Orbitals* ParaV_in, int nspin_in, - K_Vectors& kv_in, + const K_Vectors& kv_in, void* p_hamilt_in, void* psi_in, elecstate::ElecState* pelec_in, @@ -201,7 +201,7 @@ class SpinConstrain /// @brief set orbital parallel info void set_ParaV(Parallel_Orbitals* ParaV_in); /// @brief set parameters for solver - void set_solver_parameters(K_Vectors& kv_in, + void set_solver_parameters(const K_Vectors& kv_in, void* p_hamilt_in, void* psi_in, elecstate::ElecState* pelec_in); diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index e6764b8035..1ebf4cc5eb 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -7,7 +7,7 @@ #include "source_pw/module_pwdft/VSep_in_pw.h" template -void setup_pot(const int istep, +void pw::setup_pot(const int istep, UnitCell& ucell, // unitcell const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors @@ -18,7 +18,8 @@ void setup_pot(const int istep, pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method psi::Psi* kspw_psi, // electronic wave functions - const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + hamilt::Hamilt* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp) // input parameters { @@ -28,7 +29,7 @@ void setup_pot(const int istep, pelec->init_scf(istep, ucell, para_grid, - sf, + sf.strucFac, locpp.numeric, ucell.symm, (void*)pw_wfc); @@ -66,14 +67,14 @@ void setup_pot(const int istep, onsite_p->init(PARAM.inp.orbital_dir, &ucell, *(kspw_psi), - this->kv, + kv, *(pw_wfc), - this->sf, + sf, PARAM.inp.onsite_radius, PARAM.globalv.nqx, PARAM.globalv.dq, - this->pelec->wg, - this->pelec->ekb); + pelec->wg, + pelec->ekb); } //---------------------------------------------------------- @@ -92,11 +93,11 @@ void setup_pot(const int istep, ucell, nullptr, PARAM.inp.nspin, - this->kv, - this->p_hamilt, - this->kspw_psi, - this->pelec, - this->pw_wfc); + kv, + p_hamilt, + kspw_psi, + pelec, + pw_wfc); } //---------------------------------------------------------- @@ -105,7 +106,7 @@ void setup_pot(const int istep, if (PARAM.inp.dft_plus_u) { auto* dftu = ModuleDFTU::DFTU::get_instance(); - dftu->init(ucell, nullptr, this->kv.get_nks()); + dftu->init(ucell, nullptr, kv.get_nks()); } //---------------------------------------------------------- @@ -114,13 +115,13 @@ void setup_pot(const int istep, //---------------------------------------------------------- if (PARAM.inp.dfthalf_type > 0) { - this->vsep_cell->generate_vsep_r(this->pw_rhod[0], this->sf.strucFac, ucell.sep_cell); + vsep_cell->generate_vsep_r(pw_rhod[0], sf.strucFac, ucell.sep_cell); } return; } -template void setup_pot, base_device::DEVICE_CPU>( +template void pw::setup_pot, base_device::DEVICE_CPU>( const int istep, // ionic step UnitCell& ucell, // unitcell const K_Vectors &kv, // kpoints @@ -131,13 +132,14 @@ template void setup_pot, base_device::DEVICE_CPU>( pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method - psi::Psi, base_devcie::DEVICE_CPU>* kspw_psi, // electronic wave functions - const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters -template void setup_pot, base_device::DEVICE_CPU>( +template void pw::setup_pot, base_device::DEVICE_CPU>( const int istep, // ionic step UnitCell& ucell, // unitcell const K_Vectors &kv, // kpoints @@ -148,7 +150,8 @@ template void setup_pot, base_device::DEVICE_CPU>( pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method - psi::Psi, base_devcie::DEVICE_CPU>* kspw_psi, // electronic wave functions - const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters diff --git a/source/source_pw/module_pwdft/setup_pot.h b/source/source_pw/module_pwdft/setup_pot.h index 63047864c1..b89e33c21e 100644 --- a/source/source_pw/module_pwdft/setup_pot.h +++ b/source/source_pw/module_pwdft/setup_pot.h @@ -1,11 +1,16 @@ #ifndef SETUP_POT_H #define SETUP_POT_H +#include "source_base/module_device/device.h" // use Device #include "source_cell/unitcell.h" #include "source_cell/klist.h" #include "source_pw/module_pwdft/structure_factor.h" #include "source_estate/elecstate.h" #include "source_pw/module_pwdft/VL_in_pw.h" +#include "source_hamilt/hamilt.h" + +namespace pw +{ template void setup_pot(const int istep, @@ -19,10 +24,13 @@ void setup_pot(const int istep, pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method psi::Psi* kspw_psi, // electronic wave functions - const ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + hamilt::Hamilt* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters +} + #endif From 0e70265ca71768e88301be1fdb3049b2275c3684 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 30 Sep 2025 23:14:09 +0800 Subject: [PATCH 22/60] update format of esolver_ks_pw.cpp --- source/source_esolver/esolver_ks_pw.cpp | 49 ++++++------------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 8c7734d2da..8b3802b7dd 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -8,8 +8,8 @@ #include "source_estate/elecstate_pw_sdft.h" #include "source_estate/elecstate_tools.h" #include "source_estate/module_charge/symmetry_rho.h" -#include "source_hamilt/module_ewald/H_Ewald_pw.h" -#include "source_hamilt/module_vdw/vdw.h" +//#include "source_hamilt/module_ewald/H_Ewald_pw.h" +//#include "source_hamilt/module_vdw/vdw.h" #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/hsolver_pw.h" #include "source_hsolver/kernels/dngvd_op.h" @@ -23,7 +23,7 @@ #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/stress_pw.h" -#include +//#include #include #include @@ -107,24 +107,14 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p { //! SDFT only supports double precision currently this->pelec = new elecstate::ElecStatePW_SDFT, Device>(this->pw_wfc, - &(this->chr), - &(this->kv), - &ucell, - &(this->ppcell), - this->pw_rhod, - this->pw_rho, - this->pw_big); + &(this->chr), &(this->kv), &ucell, &(this->ppcell), + this->pw_rhod, this->pw_rho, this->pw_big); } else { this->pelec = new elecstate::ElecStatePW(this->pw_wfc, - &(this->chr), - &(this->kv), - &ucell, - &this->ppcell, - this->pw_rhod, - this->pw_rho, - this->pw_big); + &(this->chr), &(this->kv), &ucell, &this->ppcell, + this->pw_rhod, this->pw_rho, this->pw_big); } } @@ -141,19 +131,12 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p this->vsep_cell->init_vsep(*this->pw_rhod, ucell.sep_cell); } - //! 4) initialize the potential. if (this->pelec->pot == nullptr) { this->pelec->pot = new elecstate::Potential(this->pw_rhod, - this->pw_rho, - &ucell, - &this->locpp.vloc, - &(this->sf), - &(this->solvent), - &(this->pelec->f_en.etxc), - &(this->pelec->f_en.vtxc), - this->vsep_cell); + this->pw_rho, &ucell, &this->locpp.vloc, &(this->sf), + &(this->solvent), &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc), this->vsep_cell); } //! 5) Initalize local pseudopotential @@ -569,25 +552,18 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const ModuleBase::TITLE("ESolver_KS_PW", "after_scf"); ModuleBase::timer::tick("ESolver_KS_PW", "after_scf"); - //------------------------------------------------------------------ - // 1) since ESolver_KS::psi is hidden by ESolver_KS_PW::psi, + // since ESolver_KS::psi is hidden by ESolver_KS_PW::psi, // we need to copy the data from ESolver_KS::psi to ESolver_KS_PW::psi. // This part needs to be removed when we have a better design. // sunliang 2025-04-10 - //------------------------------------------------------------------ if (PARAM.inp.out_elf[0] > 0) { this->ESolver_KS::psi = new psi::Psi(this->psi[0]); } - //------------------------------------------------------------------ - // 2) call after_scf() of ESolver_KS - //------------------------------------------------------------------ ESolver_KS::after_scf(ucell, istep, conv_esolver); - //------------------------------------------------------------------ - // 3) transfer data from GPU to CPU in pw basis - //------------------------------------------------------------------ + // transfer data from GPU to CPU in pw basis if (this->device == base_device::GpuDevice) { castmem_2d_d2h_op()(this->psi[0].get_pointer() - this->psi[0].get_psi_bias(), @@ -660,9 +636,6 @@ void ESolver_KS_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& s template void ESolver_KS_PW::after_all_runners(UnitCell& ucell) { - //---------------------------------------------------------- - //! 1) after_all_runners in ESolver_KS - //---------------------------------------------------------- ESolver_KS::after_all_runners(ucell); ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, From e0eed861bb5e453707b44452549ab3c2df22ae5e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 4 Oct 2025 10:05:31 +0800 Subject: [PATCH 23/60] update setup_pot for GPU version --- source/source_pw/module_pwdft/setup_pot.cpp | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index 1ebf4cc5eb..edf101ce2a 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -155,3 +155,41 @@ template void pw::setup_pot, base_device::DEVICE_CPU>( ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters + +#if ((defined __CUDA) || (defined __ROCM)) + +template void pw::setup_pot, base_device::DEVICE_GPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + +template void pw::setup_pot, base_device::DEVICE_GPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + +#endif From 6f1657e2d6336e80a0a149c08ea027d09f88cc16 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 09:49:52 +0800 Subject: [PATCH 24/60] update esolver_ks_pw.cpp --- source/source_esolver/esolver_ks_pw.cpp | 118 ++++++++---------------- 1 file changed, 40 insertions(+), 78 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 8b3802b7dd..5a176dd57e 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -1,18 +1,13 @@ #include "esolver_ks_pw.h" -#include "source_base/global_variable.h" -#include "source_base/kernels/math_kernel_op.h" -#include "source_base/memory.h" #include "source_estate/cal_ux.h" #include "source_estate/elecstate_pw.h" #include "source_estate/elecstate_pw_sdft.h" #include "source_estate/elecstate_tools.h" #include "source_estate/module_charge/symmetry_rho.h" -//#include "source_hamilt/module_ewald/H_Ewald_pw.h" -//#include "source_hamilt/module_vdw/vdw.h" + #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/hsolver_pw.h" -#include "source_hsolver/kernels/dngvd_op.h" #include "source_io/module_parameter/parameter.h" #include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_pw/module_pwdft/onsite_projector.h" @@ -23,10 +18,12 @@ #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/stress_pw.h" -//#include - -#include -#include +//#include "source_base/global_variable.h" +//#include "source_base/kernels/math_kernel_op.h" +//#include "source_base/memory.h" +//#include "source_hsolver/kernels/dngvd_op.h" +//#include +//#include #ifdef __DSP #include "source_base/kernels/dsp/dsp_connector.h" @@ -97,10 +94,10 @@ void ESolver_KS_PW::deallocate_hamilt() template void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_para& inp) { - // 1) call before_all_runners() of ESolver_KS + //! Call before_all_runners() of ESolver_KS ESolver_KS::before_all_runners(ucell, inp); - // 2) initialize ElecState, set pelec pointer + //! Initialize ElecState, set pelec pointer if (this->pelec == nullptr) { if (inp.esolver_type == "sdft") @@ -118,20 +115,20 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p } } - //! set the cell volume variable in pelec + //! Set the cell volume variable in pelec this->pelec->omega = ucell.omega; - //! 3) inititlize the charge density. + //! Inititlize the charge density. this->chr.allocate(inp.nspin); - // 3.5) initialize DFT-1/2 + //! Initialize DFT-1/2 if (PARAM.inp.dfthalf_type > 0) { this->vsep_cell = new VSep; this->vsep_cell->init_vsep(*this->pw_rhod, ucell.sep_cell); } - //! 4) initialize the potential. + //! Initialize the potential. if (this->pelec->pot == nullptr) { this->pelec->pot = new elecstate::Potential(this->pw_rhod, @@ -139,25 +136,19 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p &(this->solvent), &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc), this->vsep_cell); } - //! 5) Initalize local pseudopotential + //! Initalize local pseudopotential this->locpp.init_vloc(ucell, this->pw_rhod); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); - //! 6) Initalize non-local pseudopotential + //! Initalize non-local pseudopotential this->ppcell.init(ucell, &this->sf, this->pw_wfc); this->ppcell.init_vnl(ucell, this->pw_rhod); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); - //! 7) Allocate and initialize psi + //! Allocate and initialize psi this->p_psi_init = new psi::PSIInit(inp.init_wfc, - inp.ks_solver, - inp.basis_type, - GlobalV::MY_RANK, - ucell, - this->sf, - this->kv, - this->ppcell, - *this->pw_wfc); + inp.ks_solver, inp.basis_type, GlobalV::MY_RANK, ucell, + this->sf, this->kv, this->ppcell, *this->pw_wfc); allocate_psi(this->psi, this->kv.get_nks(), this->kv.ngk, PARAM.globalv.nbands_l, this->pw_wfc->npwk_max); @@ -169,7 +160,7 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS"); - //! 8) setup occupations + //! Setup occupations if (inp.ocp) { elecstate::fixed_weights(inp.ocp_kb, @@ -180,7 +171,7 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p this->pelec->skip_weights); } - // 9) initialize exx pw + //! Initialize exx pw if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" || inp.calculation == "md") { @@ -203,14 +194,10 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) ModuleBase::TITLE("ESolver_KS_PW", "before_scf"); ModuleBase::timer::tick("ESolver_KS_PW", "before_scf"); - //---------------------------------------------------------- - //! 1) Call before_scf() of ESolver_KS - //---------------------------------------------------------- + //! Call before_scf() of ESolver_KS ESolver_KS::before_scf(ucell, istep); - //---------------------------------------------------------- - //! 2) Init variables (cell changed) - //---------------------------------------------------------- + //! Init variables (once the cell has changed) if (ucell.cell_parameter_updated) { this->ppcell.rescale_vnl(ucell.omega); @@ -225,35 +212,27 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) this->p_psi_init->prepare_init(PARAM.inp.pw_seed); } - //---------------------------------------------------------- - //! 3) init Hamiltonian (cell changed) - //---------------------------------------------------------- + //! Init Hamiltonian (cell changed) //! Operators in HamiltPW should be reallocated once cell changed //! delete Hamilt if not first scf this->deallocate_hamilt(); - // allocate HamiltPW + //! Allocate HamiltPW this->allocate_hamilt(ucell); - //---------------------------------------------------------- - // 4) setup potentials (local, non-local, sc, +U, DFT-1/2) - //---------------------------------------------------------- + //! Setup potentials (local, non-local, sc, +U, DFT-1/2) pw::setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, this->chr, this->locpp, this->ppcell, this->vsep_cell, this->kspw_psi, this->p_hamilt, this->pw_wfc, this->pw_rhod, PARAM.inp); - //---------------------------------------------------------- - //! 5) Initialize wave functions - //---------------------------------------------------------- + //! Initialize wave functions if (!this->already_initpsi) { this->p_psi_init->initialize_psi(this->psi, this->kspw_psi, this->p_hamilt, GlobalV::ofs_running); this->already_initpsi = true; } - //---------------------------------------------------------- - //! 6) Exx calculations - //---------------------------------------------------------- + //! Exx calculations if (PARAM.inp.calculation == "scf" || PARAM.inp.calculation == "relax" || PARAM.inp.calculation == "cell-relax" || PARAM.inp.calculation == "md") { @@ -271,7 +250,7 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) template void ESolver_KS_PW::iter_init(UnitCell& ucell, const int istep, const int iter) { - // call iter_init() of ESolver_KS + // Call iter_init() of ESolver_KS ESolver_KS::iter_init(ucell, istep, iter); if (iter == 1) @@ -280,10 +259,7 @@ void ESolver_KS_PW::iter_init(UnitCell& ucell, const int istep, const this->p_chgmix->mixing_restart_step = PARAM.inp.scf_nmax + 1; } - //---------------------------------------------------------- - // for mixing restart - // the details should move somewhere else, 2025-04-13 - //---------------------------------------------------------- + // For mixing restart if (iter == this->p_chgmix->mixing_restart_step && PARAM.inp.mixing_restart > 0.0) { this->p_chgmix->init_mixing(); @@ -322,16 +298,12 @@ void ESolver_KS_PW::iter_init(UnitCell& ucell, const int istep, const } } - //---------------------------------------------------------- // mohan move harris functional to here, 2012-06-05 // use 'rho(in)' and 'v_h and v_xc'(in) - //---------------------------------------------------------- this->pelec->f_en.deband_harris = this->pelec->cal_delta_eband(ucell); - //---------------------------------------------------------- // update local occupations for DFT+U // should before lambda loop in DeltaSpin - //---------------------------------------------------------- if (PARAM.inp.dft_plus_u && (iter != 1 || istep != 0)) { auto* dftu = ModuleDFTU::DFTU::get_instance(); @@ -450,38 +422,28 @@ void ESolver_KS_PW::update_pot(UnitCell& ucell, const int istep, cons template void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) { - //---------------------------------------------------------- // Related to EXX - //---------------------------------------------------------- if (GlobalC::exx_info.info_global.cal_exx && !exx_helper.op_exx->first_iter) { this->pelec->set_exx(exx_helper.cal_exx_energy(kspw_psi)); } - //---------------------------------------------------------- - // deband is calculated from "output" charge density calculated - // in sum_band - // need 'rho(out)' and 'vr (v_h(in) and v_xc(in))' - //---------------------------------------------------------- + // deband is calculated from "output" charge density this->pelec->f_en.deband = this->pelec->cal_delta_eband(ucell); - //---------------------------------------------------------- - // 1) Call iter_finish() of ESolver_KS - //---------------------------------------------------------- + // Call iter_finish() of ESolver_KS ESolver_KS::iter_finish(ucell, istep, iter, conv_esolver); - //---------------------------------------------------------- - // 2) Update USPP-related quantities // D in USPP needs vloc, thus needs update when veff updated // calculate the effective coefficient matrix for non-local // pp projectors, liuyu 2023-10-24 - //---------------------------------------------------------- if (PARAM.globalv.use_uspp) { ModuleBase::matrix veff = this->pelec->pot->get_effective_v(); this->ppcell.cal_effective_D(veff, this->pw_rhod, ucell); } + // Related to EXX if (GlobalC::exx_info.info_global.cal_exx) { if (GlobalC::exx_info.info_global.separate_loop) @@ -524,17 +486,15 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } - //---------------------------------------------------------- - // 4) check if oscillate for delta_spin method - //---------------------------------------------------------- + // check if oscillate for delta_spin method if (PARAM.inp.sc_mag_switch) { spinconstrain::SpinConstrain>& sc = spinconstrain::SpinConstrain>::getScInstance(); if (!sc.higher_mag_prec) { - sc.higher_mag_prec - = this->p_chgmix->if_scf_oscillate(iter, this->drho, PARAM.inp.sc_os_ndim, PARAM.inp.scf_os_thr); + sc.higher_mag_prec = this->p_chgmix->if_scf_oscillate(iter, + this->drho, PARAM.inp.sc_os_ndim, PARAM.inp.scf_os_thr); if (sc.higher_mag_prec) { // if oscillate, increase the precision of magnetization and do mixing_restart in next iteration this->p_chgmix->mixing_restart_step = iter + 1; @@ -542,6 +502,7 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int } } + // the output quantities ModuleIO::ctrl_iter_pw(istep, iter, conv_esolver, this->psi, this->kv, this->pw_wfc, PARAM.inp); } @@ -552,18 +513,18 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const ModuleBase::TITLE("ESolver_KS_PW", "after_scf"); ModuleBase::timer::tick("ESolver_KS_PW", "after_scf"); - // since ESolver_KS::psi is hidden by ESolver_KS_PW::psi, + // Since ESolver_KS::psi is hidden by ESolver_KS_PW::psi, // we need to copy the data from ESolver_KS::psi to ESolver_KS_PW::psi. - // This part needs to be removed when we have a better design. // sunliang 2025-04-10 if (PARAM.inp.out_elf[0] > 0) { this->ESolver_KS::psi = new psi::Psi(this->psi[0]); } + // Call 'after_scf' of ESolver_KS ESolver_KS::after_scf(ucell, istep, conv_esolver); - // transfer data from GPU to CPU in pw basis + // Transfer data from GPU to CPU in pw basis if (this->device == base_device::GpuDevice) { castmem_2d_d2h_op()(this->psi[0].get_pointer() - this->psi[0].get_psi_bias(), @@ -571,6 +532,7 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const this->psi[0].size()); } + // Output quantities ModuleIO::ctrl_scf_pw(istep, ucell, this->pelec, this->chr, this->kv, this->pw_wfc, this->pw_rho, this->pw_rhod, this->pw_big, this->psi, this->kspw_psi, this->__kspw_psi, this->ctx, this->Pgrid, PARAM.inp); From 28ce5d6c73fbc78de07f51cc2f4c70c21ad9e091 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 10:32:41 +0800 Subject: [PATCH 25/60] add setup_estate_pw in source_estate --- source/Makefile.Objects | 3 +- source/source_esolver/esolver_ks_pw.cpp | 76 +-------- source/source_estate/CMakeLists.txt | 1 + source/source_estate/setup_estate_pw.cpp | 169 ++++++++++++++++++++ source/source_estate/setup_estate_pw.h | 36 +++++ source/source_pw/module_pwdft/setup_pot.cpp | 1 + 6 files changed, 216 insertions(+), 70 deletions(-) create mode 100644 source/source_estate/setup_estate_pw.cpp create mode 100644 source/source_estate/setup_estate_pw.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index dc638d4199..f0f0903310 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -238,6 +238,7 @@ OBJS_ELECSTAT=elecstate.o\ gatefield.o\ potential_new.o\ potential_types.o\ + pot_sep.o\ pot_local.o\ pot_local_paw.o\ H_Hartree_pw.o\ @@ -248,7 +249,7 @@ OBJS_ELECSTAT=elecstate.o\ cal_nelec_nband.o\ read_pseudo.o\ cal_wfc.o\ - pot_sep.o\ + setup_estate_pw.cpp\ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\ elecstate_lcao_cal_tau.o\ diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 5a176dd57e..826cb9fe2d 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -32,6 +32,7 @@ #include #include "source_pw/module_pwdft/setup_pot.h" // mohan add 20250929 +#include "source_estate/setup_estate_pw.h" // mohan add 20251005 #include "source_io/ctrl_output_pw.h" // mohan add 20250927 namespace ModuleESolver @@ -51,16 +52,6 @@ ESolver_KS_PW::~ESolver_KS_PW() // delete Hamilt this->deallocate_hamilt(); - if (this->vsep_cell != nullptr) - { - delete this->vsep_cell; - } - - if (this->pelec != nullptr) - { - delete reinterpret_cast*>(this->pelec); - this->pelec = nullptr; - } if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { @@ -97,53 +88,10 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p //! Call before_all_runners() of ESolver_KS ESolver_KS::before_all_runners(ucell, inp); - //! Initialize ElecState, set pelec pointer - if (this->pelec == nullptr) - { - if (inp.esolver_type == "sdft") - { - //! SDFT only supports double precision currently - this->pelec = new elecstate::ElecStatePW_SDFT, Device>(this->pw_wfc, - &(this->chr), &(this->kv), &ucell, &(this->ppcell), - this->pw_rhod, this->pw_rho, this->pw_big); - } - else - { - this->pelec = new elecstate::ElecStatePW(this->pw_wfc, - &(this->chr), &(this->kv), &ucell, &this->ppcell, - this->pw_rhod, this->pw_rho, this->pw_big); - } - } - - //! Set the cell volume variable in pelec - this->pelec->omega = ucell.omega; - - //! Inititlize the charge density. - this->chr.allocate(inp.nspin); - - //! Initialize DFT-1/2 - if (PARAM.inp.dfthalf_type > 0) - { - this->vsep_cell = new VSep; - this->vsep_cell->init_vsep(*this->pw_rhod, ucell.sep_cell); - } - - //! Initialize the potential. - if (this->pelec->pot == nullptr) - { - this->pelec->pot = new elecstate::Potential(this->pw_rhod, - this->pw_rho, &ucell, &this->locpp.vloc, &(this->sf), - &(this->solvent), &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc), this->vsep_cell); - } - - //! Initalize local pseudopotential - this->locpp.init_vloc(ucell, this->pw_rhod); - ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); - - //! Initalize non-local pseudopotential - this->ppcell.init(ucell, &this->sf, this->pw_wfc); - this->ppcell.init_vnl(ucell, this->pw_rhod); - ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); + //! setup and allocation for pelec, charge density, potentials, etc. + elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, + this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, + this->pw_rhod, this->pw_big, this->solvent, inp); //! Allocate and initialize psi this->p_psi_init = new psi::PSIInit(inp.init_wfc, @@ -160,17 +108,6 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS"); - //! Setup occupations - if (inp.ocp) - { - elecstate::fixed_weights(inp.ocp_kb, - inp.nbands, - inp.nelec, - this->pelec->klist, - this->pelec->wg, - this->pelec->skip_weights); - } - //! Initialize exx pw if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" || inp.calculation == "md") @@ -604,7 +541,8 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, this->kspw_psi, this->__kspw_psi, this->sf, this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); - + + elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); } template class ESolver_KS_PW, base_device::DEVICE_CPU>; diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index 651c602822..e22dbfc3b0 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -37,6 +37,7 @@ list(APPEND objects cal_nelec_nband.cpp read_pseudo.cpp cal_wfc.cpp + setup_estate_pw.cpp ) if(ENABLE_LCAO) diff --git a/source/source_estate/setup_estate_pw.cpp b/source/source_estate/setup_estate_pw.cpp new file mode 100644 index 0000000000..4023183787 --- /dev/null +++ b/source/source_estate/setup_estate_pw.cpp @@ -0,0 +1,169 @@ +#include "source_estate/setup_estate_pw.h" + +template +void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rho, // pw for rho + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + surchem &solvent, // solvent + const Input_para& inp) // input parameters +{ + ModuleBase::TITLE("elecstate", "setup_estate_pw"); + + //! Initialize ElecState, set pelec pointer + if (pelec == nullptr) + { + if (inp.esolver_type == "sdft") + { + //! SDFT only supports double precision currently + pelec = new elecstate::ElecStatePW_SDFT, Device>(pw_wfc, + &chr, &kv, &ucell, &ppcell, + pw_rhod, pw_rho, pw_big); + } + else + { + pelec = new elecstate::ElecStatePW(pw_wfc, + &chr, &kv, &ucell, &ppcell, + pw_rhod, pw_rho, pw_big); + } + } + + //! Set the cell volume variable in pelec + pelec->omega = ucell.omega; + + //! Inititlize the charge density. + chr.allocate(inp.nspin); + + //! Initialize DFT-1/2 + if (PARAM.inp.dfthalf_type > 0) + { + vsep_cell = new VSep; + vsep_cell->init_vsep(*pw_rhod, ucell.sep_cell); + } + + //! Initialize the potential. + if (pelec->pot == nullptr) + { + pelec->pot = new elecstate::Potential(pw_rhod, + pw_rho, &ucell, &locpp.vloc, &sf, + &solvent, &(pelec->f_en.etxc), &(pelec->f_en.vtxc), vsep_cell); + } + + //! Initalize local pseudopotential + locpp.init_vloc(ucell, pw_rhod); + ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); + + //! Initalize non-local pseudopotential + ppcell.init(ucell, &sf, pw_wfc); + ppcell.init_vnl(ucell, pw_rhod); + ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); + + //! Setup occupations + if (inp.ocp) + { + elecstate::fixed_weights(inp.ocp_kb, + inp.nbands, + inp.nelec, + pelec->klist, + pelec->wg, + pelec->skip_weights); + } + + return; +} + + +void elecstate::teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell) +{ + ModuleBase::TITLE("elecstate", "teardown_estate_pw"); + + if (vsep_cell != nullptr) + { + delete vsep_cell; + } + + if (pelec != nullptr) + { + delete reinterpret_cast*>(pelec); + pelec = nullptr; + } +} + + +template void elecstate::setup_estate_pw, base_device::DEVICE_CPU>( + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rho, // pw for rho + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + surchem &solvent, // solvent + const Input_para& inp); // input parameters + +template void elecstate::setup_estate_pw, base_device::DEVICE_CPU>( + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rho, // pw for rho + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + surchem &solvent, // solvent + const Input_para& inp); // input parameters + + + +#if ((defined __CUDA) || (defined __ROCM)) + +template void elecstate::setup_estate_pw, base_device::DEVICE_GPU>( + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rho, // pw for rho + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + surchem &solvent, // solvent + const Input_para& inp); // input parameters + +template void elecstate::setup_estate_pw, base_device::DEVICE_GPU>( + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rho, // pw for rho + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + surchem &solvent, // solvent + const Input_para& inp); // input parameters + +#endif diff --git a/source/source_estate/setup_estate_pw.h b/source/source_estate/setup_estate_pw.h new file mode 100644 index 0000000000..e31ba502e5 --- /dev/null +++ b/source/source_estate/setup_estate_pw.h @@ -0,0 +1,36 @@ +#ifndef SETUP_ESTATE_PW_H +#define SETUP_ESTATE_PW_H + +#include "source_base/module_device/device.h" // use Device +#include "source_cell/unitcell.h" +#include "source_cell/klist.h" +#include "source_pw/module_pwdft/structure_factor.h" +#include "source_estate/elecstate.h" +#include "source_pw/module_pwdft/VL_in_pw.h" +#include "source_pw/module_pwdft/VSep_in_pw.h" + +namespace elecstate +{ + +template +void setup_estate_pw(UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rho, // pw for rho + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + surchem &solvent, // solvent + const Input_para& inp); // input parameters + +void teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell); + +} + + +#endif diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index edf101ce2a..b65d25cdb4 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -23,6 +23,7 @@ void pw::setup_pot(const int istep, const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp) // input parameters { + ModuleBase::TITLE("pw", "setup_pot"); //---------------------------------------------------------- //! 1) Renew local pseudopotential //---------------------------------------------------------- From af3955d968edf56a22802fdc4e24e86ff5c3dff9 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 10:53:39 +0800 Subject: [PATCH 26/60] fix some bugs --- source/source_esolver/esolver_ks_pw.cpp | 5 +- source/source_estate/setup_estate_pw.cpp | 66 +++++++++++++++--------- source/source_estate/setup_estate_pw.h | 11 ++-- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 826cb9fe2d..95d4ce88f2 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -3,7 +3,6 @@ #include "source_estate/cal_ux.h" #include "source_estate/elecstate_pw.h" #include "source_estate/elecstate_pw_sdft.h" -#include "source_estate/elecstate_tools.h" #include "source_estate/module_charge/symmetry_rho.h" #include "source_hsolver/diago_iter_assist.h" @@ -89,7 +88,7 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ESolver_KS::before_all_runners(ucell, inp); //! setup and allocation for pelec, charge density, potentials, etc. - elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, + elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, this->pw_rhod, this->pw_big, this->solvent, inp); @@ -542,7 +541,7 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) this->kspw_psi, this->__kspw_psi, this->sf, this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); - elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); + elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); } template class ESolver_KS_PW, base_device::DEVICE_CPU>; diff --git a/source/source_estate/setup_estate_pw.cpp b/source/source_estate/setup_estate_pw.cpp index 4023183787..8042334471 100644 --- a/source/source_estate/setup_estate_pw.cpp +++ b/source/source_estate/setup_estate_pw.cpp @@ -1,18 +1,21 @@ #include "source_estate/setup_estate_pw.h" +#include "source_estate/elecstate_pw.h" // init of pelec +#include "source_estate/elecstate_pw_sdft.h" // init of pelec for sdft +#include "source_estate/elecstate_tools.h" // occupations template void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors elecstate::ElecState *pelec, // pointer of electrons - const Charge &chr, // charge density + Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rho, // pw for rho - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + ModulePW::PW_Basis *pw_rho, // pw for rho + ModulePW::PW_Basis *pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp) // input parameters { @@ -81,6 +84,7 @@ void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell } +template void elecstate::teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell) { ModuleBase::TITLE("elecstate", "teardown_estate_pw"); @@ -100,70 +104,82 @@ void elecstate::teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell) template void elecstate::setup_estate_pw, base_device::DEVICE_CPU>( UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors elecstate::ElecState *pelec, // pointer of electrons - const Charge &chr, // charge density + Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rho, // pw for rho - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + ModulePW::PW_Basis *pw_rho, // pw for rho + ModulePW::PW_Basis *pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp); // input parameters template void elecstate::setup_estate_pw, base_device::DEVICE_CPU>( UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors elecstate::ElecState *pelec, // pointer of electrons - const Charge &chr, // charge density + Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rho, // pw for rho - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + ModulePW::PW_Basis *pw_rho, // pw for rho + ModulePW::PW_Basis *pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp); // input parameters +template void elecstate::teardown_estate_pw, base_device::DEVICE_CPU>( + elecstate::ElecState *pelec, VSep* vsep_cell); + +template void elecstate::teardown_estate_pw, base_device::DEVICE_CPU>( + elecstate::ElecState *pelec, VSep* vsep_cell); + #if ((defined __CUDA) || (defined __ROCM)) template void elecstate::setup_estate_pw, base_device::DEVICE_GPU>( UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors elecstate::ElecState *pelec, // pointer of electrons - const Charge &chr, // charge density + Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rho, // pw for rho - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + ModulePW::PW_Basis *pw_rho, // pw for rho + ModulePW::PW_Basis *pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp); // input parameters template void elecstate::setup_estate_pw, base_device::DEVICE_GPU>( UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors elecstate::ElecState *pelec, // pointer of electrons - const Charge &chr, // charge density + Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rho, // pw for rho - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + ModulePW::PW_Basis *pw_rho, // pw for rho + ModulePW::PW_Basis *pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp); // input parameters +template void elecstate::teardown_estate_pw, base_device::DEVICE_GPU>( + elecstate::ElecState *pelec, VSep* vsep_cell); + +template void elecstate::teardown_estate_pw, base_device::DEVICE_GPU>( + elecstate::ElecState *pelec, VSep* vsep_cell); + #endif diff --git a/source/source_estate/setup_estate_pw.h b/source/source_estate/setup_estate_pw.h index e31ba502e5..ee4839e978 100644 --- a/source/source_estate/setup_estate_pw.h +++ b/source/source_estate/setup_estate_pw.h @@ -14,20 +14,21 @@ namespace elecstate template void setup_estate_pw(UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors elecstate::ElecState *pelec, // pointer of electrons - const Charge &chr, // charge density + Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials VSep* vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rho, // pw for rho - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const ModulePW::PW_Basis_Big* pw_big, // pw for big grid + ModulePW::PW_Basis *pw_rho, // pw for rho + ModulePW::PW_Basis *pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp); // input parameters +template void teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell); } From 8030dd0570e073200d73432387744c8871f54402 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 10:56:23 +0800 Subject: [PATCH 27/60] small update --- source/source_esolver/esolver_ks_pw.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 95d4ce88f2..ddfebfde03 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -2,7 +2,6 @@ #include "source_estate/cal_ux.h" #include "source_estate/elecstate_pw.h" -#include "source_estate/elecstate_pw_sdft.h" #include "source_estate/module_charge/symmetry_rho.h" #include "source_hsolver/diago_iter_assist.h" @@ -17,6 +16,7 @@ #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/stress_pw.h" +//#include "source_estate/elecstate_pw_sdft.h" //#include "source_base/global_variable.h" //#include "source_base/kernels/math_kernel_op.h" //#include "source_base/memory.h" @@ -51,7 +51,6 @@ ESolver_KS_PW::~ESolver_KS_PW() // delete Hamilt this->deallocate_hamilt(); - if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { delete this->kspw_psi; From 266555cc4c83cb93834d936c1cf5e5feeb4dc7d0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 11:06:15 +0800 Subject: [PATCH 28/60] move teardown to deconstructor of ESolver --- source/source_esolver/esolver_ks_pw.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 5ad514b5bb..2cbf0fb272 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -53,6 +53,8 @@ ESolver_KS_PW::~ESolver_KS_PW() // delete Hamilt this->deallocate_hamilt(); + elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); + if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { delete this->kspw_psi; @@ -542,7 +544,6 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) this->kspw_psi, this->__kspw_psi, this->sf, this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); - elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); } template class ESolver_KS_PW, base_device::DEVICE_CPU>; From cb1a21ae06a188450302f61bd19f259487363c59 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 12:31:24 +0800 Subject: [PATCH 29/60] fix error in passing pointers --- source/source_esolver/esolver_ks_pw.cpp | 87 ++++++++++++++++++++++-- source/source_estate/setup_estate_pw.cpp | 25 +++++-- source/source_estate/setup_estate_pw.h | 2 +- 3 files changed, 102 insertions(+), 12 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 2cbf0fb272..45e1b4dedb 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -3,6 +3,7 @@ #include "source_estate/cal_ux.h" #include "source_estate/elecstate_pw.h" #include "source_estate/module_charge/symmetry_rho.h" +#include "source_estate/elecstate_tools.h" // occupations #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/hsolver_pw.h" @@ -18,7 +19,7 @@ #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/stress_pw.h" -//#include "source_estate/elecstate_pw_sdft.h" +#include "source_estate/elecstate_pw_sdft.h" //#include "source_base/global_variable.h" //#include "source_base/kernels/math_kernel_op.h" //#include "source_base/memory.h" @@ -55,6 +56,20 @@ ESolver_KS_PW::~ESolver_KS_PW() elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); +/* + if (vsep_cell != nullptr) + { + delete vsep_cell; + } + + if (this->pelec != nullptr) + { + delete reinterpret_cast*>(this->pelec); + this->pelec = nullptr; + } +*/ + + if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { delete this->kspw_psi; @@ -91,9 +106,60 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ESolver_KS::before_all_runners(ucell, inp); //! setup and allocation for pelec, charge density, potentials, etc. - elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, - this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, - this->pw_rhod, this->pw_big, this->solvent, inp); +// elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, + // this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, + // this->pw_rhod, this->pw_big, this->solvent, inp); + + //! Initialize ElecState, set pelec pointer + if (this->pelec == nullptr) + { + if (inp.esolver_type == "sdft") + { + //! SDFT only supports double precision currently + this->pelec = new elecstate::ElecStatePW_SDFT, Device>(this->pw_wfc, + &this->chr, &this->kv, &ucell, &this->ppcell, + this->pw_rhod, this->pw_rho, this->pw_big); + } + else + { + this->pelec = new elecstate::ElecStatePW(this->pw_wfc, + &this->chr, &this->kv, &ucell, &this->ppcell, + this->pw_rhod, this->pw_rho, this->pw_big); + } + } + + + //! Set the cell volume variable in this->pelec + this->pelec->omega = ucell.omega; + + //! Inititlize the charge density. + this->chr.allocate(inp.nspin); + + //! Initialize DFT-1/2 + if (PARAM.inp.dfthalf_type > 0) + { + vsep_cell = new VSep; + vsep_cell->init_vsep(*this->pw_rhod, ucell.sep_cell); + } + + //! Initialize the potential. + if (this->pelec->pot == nullptr) + { + this->pelec->pot = new elecstate::Potential(this->pw_rhod, + this->pw_rho, &ucell, &this->locpp.vloc, &this->sf, + &this->solvent, &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc), vsep_cell); + } + + //! Initalize local pseudopotential + this->locpp.init_vloc(ucell, this->pw_rhod); + ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); + + //! Initalize non-local pseudopotential + this->ppcell.init(ucell, &this->sf, this->pw_wfc); + this->ppcell.init_vnl(ucell, this->pw_rhod); + ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); + + //! Allocate and initialize psi this->p_psi_init = new psi::PSIInit(inp.init_wfc, @@ -110,6 +176,19 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS"); + //! Setup occupations + if (inp.ocp) + { + elecstate::fixed_weights(inp.ocp_kb, + inp.nbands, + inp.nelec, + this->pelec->klist, + this->pelec->wg, + this->pelec->skip_weights); + } + + + //! Initialize exx pw if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" || inp.calculation == "md") diff --git a/source/source_estate/setup_estate_pw.cpp b/source/source_estate/setup_estate_pw.cpp index 8042334471..81800bd79c 100644 --- a/source/source_estate/setup_estate_pw.cpp +++ b/source/source_estate/setup_estate_pw.cpp @@ -69,6 +69,7 @@ void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell ppcell.init_vnl(ucell, pw_rhod); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); +/* //! Setup occupations if (inp.ocp) { @@ -79,13 +80,14 @@ void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell pelec->wg, pelec->skip_weights); } +*/ return; } template -void elecstate::teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell) +void elecstate::teardown_estate_pw(elecstate::ElecState* &pelec, VSep* &vsep_cell) { ModuleBase::TITLE("elecstate", "teardown_estate_pw"); @@ -94,10 +96,19 @@ void elecstate::teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell) delete vsep_cell; } + // mohan update 20251005 to increase the security level if (pelec != nullptr) { - delete reinterpret_cast*>(pelec); - pelec = nullptr; + auto* pw_elec = dynamic_cast*>(pelec); + if (pw_elec) + { + delete pw_elec; + pelec = nullptr; + } + else + { + ModuleBase::WARNING_QUIT("elecstate::teardown_estate_pw", "Invalid ElecState type"); + } } } @@ -136,10 +147,10 @@ template void elecstate::setup_estate_pw, base_device::DEVI template void elecstate::teardown_estate_pw, base_device::DEVICE_CPU>( - elecstate::ElecState *pelec, VSep* vsep_cell); + elecstate::ElecState* &pelec, VSep* &vsep_cell); template void elecstate::teardown_estate_pw, base_device::DEVICE_CPU>( - elecstate::ElecState *pelec, VSep* vsep_cell); + elecstate::ElecState* &pelec, VSep* &vsep_cell); #if ((defined __CUDA) || (defined __ROCM)) @@ -177,9 +188,9 @@ template void elecstate::setup_estate_pw, base_device::DEVI const Input_para& inp); // input parameters template void elecstate::teardown_estate_pw, base_device::DEVICE_GPU>( - elecstate::ElecState *pelec, VSep* vsep_cell); + elecstate::ElecState* &pelec, VSep* &vsep_cell); template void elecstate::teardown_estate_pw, base_device::DEVICE_GPU>( - elecstate::ElecState *pelec, VSep* vsep_cell); + elecstate::ElecState* &pelec, VSep* &vsep_cell); #endif diff --git a/source/source_estate/setup_estate_pw.h b/source/source_estate/setup_estate_pw.h index ee4839e978..005f2fc066 100644 --- a/source/source_estate/setup_estate_pw.h +++ b/source/source_estate/setup_estate_pw.h @@ -29,7 +29,7 @@ void setup_estate_pw(UnitCell& ucell, // unitcell const Input_para& inp); // input parameters template -void teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell); +void teardown_estate_pw(elecstate::ElecState* &pelec, VSep* &vsep_cell); } From 9a4a462705fe69c2a82c42b377ab3c03b7ec4605 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 12:32:21 +0800 Subject: [PATCH 30/60] move teardown function to the correct place --- source/source_esolver/esolver_ks_pw.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 45e1b4dedb..3e99c84d61 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -54,21 +54,6 @@ ESolver_KS_PW::~ESolver_KS_PW() // delete Hamilt this->deallocate_hamilt(); - elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); - -/* - if (vsep_cell != nullptr) - { - delete vsep_cell; - } - - if (this->pelec != nullptr) - { - delete reinterpret_cast*>(this->pelec); - this->pelec = nullptr; - } -*/ - if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { @@ -622,6 +607,8 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, this->kspw_psi, this->__kspw_psi, this->sf, this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); + + elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); } From 4ffe19aff60f54aec614ce0e5c46570c71dd02b1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 12:37:07 +0800 Subject: [PATCH 31/60] fix bugs --- source/source_esolver/esolver_ks_pw.cpp | 12 +++++----- source/source_estate/setup_estate_pw.cpp | 28 +++++++++++------------- source/source_estate/setup_estate_pw.h | 10 ++++----- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 3e99c84d61..de37b4000a 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -91,10 +91,11 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ESolver_KS::before_all_runners(ucell, inp); //! setup and allocation for pelec, charge density, potentials, etc. -// elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, - // this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, - // this->pw_rhod, this->pw_big, this->solvent, inp); + elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr, + this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, + this->pw_rhod, this->pw_big, this->solvent, inp); +/* //! Initialize ElecState, set pelec pointer if (this->pelec == nullptr) { @@ -143,6 +144,7 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p this->ppcell.init(ucell, &this->sf, this->pw_wfc); this->ppcell.init_vnl(ucell, this->pw_rhod); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); +*/ @@ -161,6 +163,7 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS"); +/* //! Setup occupations if (inp.ocp) { @@ -171,8 +174,7 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p this->pelec->wg, this->pelec->skip_weights); } - - +*/ //! Initialize exx pw if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" diff --git a/source/source_estate/setup_estate_pw.cpp b/source/source_estate/setup_estate_pw.cpp index 81800bd79c..51bd14e8b1 100644 --- a/source/source_estate/setup_estate_pw.cpp +++ b/source/source_estate/setup_estate_pw.cpp @@ -7,14 +7,14 @@ template void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons + elecstate::ElecState* &pelec, // pointer of electrons Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - ModulePW::PW_Basis *pw_rho, // pw for rho - ModulePW::PW_Basis *pw_rhod, // pw for rhod + VSep* &vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K* pw_wfc, // pw for wfc + ModulePW::PW_Basis* pw_rho, // pw for rho + ModulePW::PW_Basis* pw_rhod, // pw for rhod ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp) // input parameters @@ -69,7 +69,6 @@ void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell ppcell.init_vnl(ucell, pw_rhod); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); -/* //! Setup occupations if (inp.ocp) { @@ -80,7 +79,6 @@ void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell pelec->wg, pelec->skip_weights); } -*/ return; } @@ -117,11 +115,11 @@ template void elecstate::setup_estate_pw, base_device::DEVIC UnitCell& ucell, // unitcell K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons + elecstate::ElecState* &pelec, // pointer of electrons Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method + VSep* &vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc ModulePW::PW_Basis *pw_rho, // pw for rho ModulePW::PW_Basis *pw_rhod, // pw for rhod @@ -133,11 +131,11 @@ template void elecstate::setup_estate_pw, base_device::DEVI UnitCell& ucell, // unitcell K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons + elecstate::ElecState* &pelec, // pointer of electrons Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method + VSep* &vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc ModulePW::PW_Basis *pw_rho, // pw for rho ModulePW::PW_Basis *pw_rhod, // pw for rhod @@ -159,11 +157,11 @@ template void elecstate::setup_estate_pw, base_device::DEVIC UnitCell& ucell, // unitcell K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons + elecstate::ElecState* &pelec, // pointer of electrons Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method + VSep* &vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc ModulePW::PW_Basis *pw_rho, // pw for rho ModulePW::PW_Basis *pw_rhod, // pw for rhod @@ -175,11 +173,11 @@ template void elecstate::setup_estate_pw, base_device::DEVI UnitCell& ucell, // unitcell K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons + elecstate::ElecState* &pelec, // pointer of electrons Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method + VSep* &vsep_cell, // U-1/2 method ModulePW::PW_Basis_K *pw_wfc, // pw for wfc ModulePW::PW_Basis *pw_rho, // pw for rho ModulePW::PW_Basis *pw_rhod, // pw for rhod diff --git a/source/source_estate/setup_estate_pw.h b/source/source_estate/setup_estate_pw.h index 005f2fc066..44864ad588 100644 --- a/source/source_estate/setup_estate_pw.h +++ b/source/source_estate/setup_estate_pw.h @@ -16,14 +16,14 @@ template void setup_estate_pw(UnitCell& ucell, // unitcell K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons + elecstate::ElecState* &pelec, // pointer of electrons Charge &chr, // charge density pseudopot_cell_vl &locpp, // local pseudopotentials pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - ModulePW::PW_Basis *pw_rho, // pw for rho - ModulePW::PW_Basis *pw_rhod, // pw for rhod + VSep* &vsep_cell, // U-1/2 method + ModulePW::PW_Basis_K* pw_wfc, // pw for wfc + ModulePW::PW_Basis* pw_rho, // pw for rho + ModulePW::PW_Basis* pw_rhod, // pw for rhod ModulePW::PW_Basis_Big* pw_big, // pw for big grid surchem &solvent, // solvent const Input_para& inp); // input parameters From c3c81749030b200bb0ca5eb6bc89429556590110 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 13:04:50 +0800 Subject: [PATCH 32/60] update --- source/Makefile.Objects | 2 +- source/source_esolver/esolver_fp.cpp | 2 +- source/source_esolver/esolver_ks_pw.cpp | 76 ------------------------- 3 files changed, 2 insertions(+), 78 deletions(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 6f0b375d8f..c24ccd157b 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -249,7 +249,7 @@ OBJS_ELECSTAT=elecstate.o\ cal_nelec_nband.o\ read_pseudo.o\ cal_wfc.o\ - setup_estate_pw.cpp\ + setup_estate_pw.o\ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\ elecstate_lcao_cal_tau.o\ diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index 9d8326d2a3..d860b74673 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -34,7 +34,7 @@ ESolver_FP::~ESolver_FP() } if (PARAM.globalv.double_grid) { - delete pw_rhod; + delete this->pw_rhod; } delete this->pelec; } diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index de37b4000a..00d00d87bb 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -3,7 +3,6 @@ #include "source_estate/cal_ux.h" #include "source_estate/elecstate_pw.h" #include "source_estate/module_charge/symmetry_rho.h" -#include "source_estate/elecstate_tools.h" // occupations #include "source_hsolver/diago_iter_assist.h" #include "source_hsolver/hsolver_pw.h" @@ -19,14 +18,6 @@ #include "source_pw/module_pwdft/forces.h" #include "source_pw/module_pwdft/stress_pw.h" -#include "source_estate/elecstate_pw_sdft.h" -//#include "source_base/global_variable.h" -//#include "source_base/kernels/math_kernel_op.h" -//#include "source_base/memory.h" -//#include "source_hsolver/kernels/dngvd_op.h" -//#include -//#include - #ifdef __DSP #include "source_base/kernels/dsp/dsp_connector.h" #endif @@ -54,7 +45,6 @@ ESolver_KS_PW::~ESolver_KS_PW() // delete Hamilt this->deallocate_hamilt(); - if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { delete this->kspw_psi; @@ -95,59 +85,6 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho, this->pw_rhod, this->pw_big, this->solvent, inp); -/* - //! Initialize ElecState, set pelec pointer - if (this->pelec == nullptr) - { - if (inp.esolver_type == "sdft") - { - //! SDFT only supports double precision currently - this->pelec = new elecstate::ElecStatePW_SDFT, Device>(this->pw_wfc, - &this->chr, &this->kv, &ucell, &this->ppcell, - this->pw_rhod, this->pw_rho, this->pw_big); - } - else - { - this->pelec = new elecstate::ElecStatePW(this->pw_wfc, - &this->chr, &this->kv, &ucell, &this->ppcell, - this->pw_rhod, this->pw_rho, this->pw_big); - } - } - - - //! Set the cell volume variable in this->pelec - this->pelec->omega = ucell.omega; - - //! Inititlize the charge density. - this->chr.allocate(inp.nspin); - - //! Initialize DFT-1/2 - if (PARAM.inp.dfthalf_type > 0) - { - vsep_cell = new VSep; - vsep_cell->init_vsep(*this->pw_rhod, ucell.sep_cell); - } - - //! Initialize the potential. - if (this->pelec->pot == nullptr) - { - this->pelec->pot = new elecstate::Potential(this->pw_rhod, - this->pw_rho, &ucell, &this->locpp.vloc, &this->sf, - &this->solvent, &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc), vsep_cell); - } - - //! Initalize local pseudopotential - this->locpp.init_vloc(ucell, this->pw_rhod); - ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); - - //! Initalize non-local pseudopotential - this->ppcell.init(ucell, &this->sf, this->pw_wfc); - this->ppcell.init_vnl(ucell, this->pw_rhod); - ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL"); -*/ - - - //! Allocate and initialize psi this->p_psi_init = new psi::PSIInit(inp.init_wfc, inp.ks_solver, inp.basis_type, GlobalV::MY_RANK, ucell, @@ -163,19 +100,6 @@ void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_p ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS"); -/* - //! Setup occupations - if (inp.ocp) - { - elecstate::fixed_weights(inp.ocp_kb, - inp.nbands, - inp.nelec, - this->pelec->klist, - this->pelec->wg, - this->pelec->skip_weights); - } -*/ - //! Initialize exx pw if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" || inp.calculation == "md") From 94c17c3396229149e0f7f6cc7515ec7f856d71e3 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 13:15:07 +0800 Subject: [PATCH 33/60] add two functions in module_pwdft --- source/source_pw/module_pwdft/setup_pwrho.cpp | 206 ++++++++++++++++++ source/source_pw/module_pwdft/setup_pwrho.h | 36 +++ 2 files changed, 242 insertions(+) create mode 100644 source/source_pw/module_pwdft/setup_pwrho.cpp create mode 100644 source/source_pw/module_pwdft/setup_pwrho.h diff --git a/source/source_pw/module_pwdft/setup_pwrho.cpp b/source/source_pw/module_pwdft/setup_pwrho.cpp new file mode 100644 index 0000000000..f1a526bed5 --- /dev/null +++ b/source/source_pw/module_pwdft/setup_pwrho.cpp @@ -0,0 +1,206 @@ +#include "source_pw/module_pwdft/setup_pwrho.h" + +#include "source_estate/module_charge/symmetry_rho.h" +#include "source_lcao/module_deltaspin/spin_constrain.h" +#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_lcao/module_dftu/dftu.h" +#include "source_pw/module_pwdft/VSep_in_pw.h" + +template +void pw::setup_pot(const int istep, + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi* kspw_psi, // electronic wave functions + hamilt::Hamilt* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp) // input parameters * +{ + ModuleBase::TITLE("pw", "setup_pwrho"); + + std::string fft_device = inp.device; + std::string fft_precision = inp.precision; + + // LCAO basis doesn't support GPU acceleration on FFT currently + if(inp.basis_type == "lcao") + { + fft_device = "cpu"; + } + + // single, double, or mixing precision calculations + if ((inp.precision=="single") || (inp.precision=="mixing")) + { + fft_precision = "mixing"; + } + else if (inp.precision=="double") + { + fft_precision = "double"; + } + + // for GPU +#if (not defined(__ENABLE_FLOAT_FFTW) and (defined(__CUDA) || defined(__RCOM))) + if (fft_device == "gpu") + { + fft_precision = "double"; + } +#endif + + // initialize pw_rho + pw_rho = new ModulePW::PW_Basis_Big(fft_device, fft_precision); + pw_rho_flag = true; + + // initialize pw_rhod + if (PARAM.globalv.double_grid) + { + pw_rhod = new ModulePW::PW_Basis_Big(fft_device, fft_precision); + } + else + { + pw_rhod = pw_rho; + } + + // initialize pw_big + pw_big = static_cast(pw_rhod); + pw_big->setbxyz(inp.bx, inp.by, inp.bz); + + // setup the structure factors + sf.set(pw_rhod, inp.nbspline); + + //! read pseudopotentials, move somewhere else??? mohan note 20251005 + elecstate::read_pseudo(GlobalV::ofs_running, ucell); + + //! initialie the plane wave basis for rho +#ifdef __MPI + this->pw_rho->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); +#endif + + //! for OFDFT calculations + if (this->classname == "ESolver_OF" || inp.of_ml_gene_data == 1) + { + this->pw_rho->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); + } + + //! initialize the FFT grid + if (inp.nx * inp.ny * inp.nz == 0) + { + this->pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, 4.0 * inp.ecutwfc); + } + else + { + this->pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.nx, inp.ny, inp.nz); + } + + this->pw_rho->initparameters(false, 4.0 * inp.ecutwfc); + this->pw_rho->fft_bundle.initfftmode(inp.fft_mode); + this->pw_rho->setuptransform(); + this->pw_rho->collect_local_pw(); + this->pw_rho->collect_uniqgg(); + + //! initialize the double grid (for uspp) if necessary + if ( PARAM.globalv.double_grid) + { + ModulePW::PW_Basis_Sup* pw_rhod_sup = static_cast(pw_rhod); +#ifdef __MPI + this->pw_rhod->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); +#endif + if (this->classname == "ESolver_OF") + { + this->pw_rhod->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); + } + if (inp.ndx * inp.ndy * inp.ndz == 0) + { + this->pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ecutrho); + } + else + { + this->pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ndx, inp.ndy, inp.ndz); + } + this->pw_rhod->initparameters(false, inp.ecutrho); + this->pw_rhod->fft_bundle.initfftmode(inp.fft_mode); + pw_rhod_sup->setuptransform(this->pw_rho); + this->pw_rhod->collect_local_pw(); + this->pw_rhod->collect_uniqgg(); + } + + return; +} + +template void pw::setup_pot, base_device::DEVICE_CPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + + +template void pw::setup_pot, base_device::DEVICE_CPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + +#if ((defined __CUDA) || (defined __ROCM)) + +template void pw::setup_pot, base_device::DEVICE_GPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + +template void pw::setup_pot, base_device::DEVICE_GPU>( + const int istep, // ionic step + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions + hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + +#endif diff --git a/source/source_pw/module_pwdft/setup_pwrho.h b/source/source_pw/module_pwdft/setup_pwrho.h new file mode 100644 index 0000000000..9146d6303c --- /dev/null +++ b/source/source_pw/module_pwdft/setup_pwrho.h @@ -0,0 +1,36 @@ +#ifndef SETUP_PWRHO_H +#define SETUP_PWRHO_H + +#include "source_base/module_device/device.h" // use Device +#include "source_cell/unitcell.h" +#include "source_cell/klist.h" +#include "source_pw/module_pwdft/structure_factor.h" +#include "source_estate/elecstate.h" +#include "source_pw/module_pwdft/VL_in_pw.h" +#include "source_hamilt/hamilt.h" + +namespace pw +{ + +template +void setup_pot(const int istep, + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints + Structure_Factor &sf, // structure factors + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + VSep* vsep_cell, // U-1/2 method + psi::Psi* kspw_psi, // electronic wave functions + hamilt::Hamilt* p_hamilt, // hamiltonian + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters + +} + + + +#endif From 8aaf32d01396a3ad51b8b10744a649f4789c0f55 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 14:58:11 +0800 Subject: [PATCH 34/60] add setup_pwrho file, and update esolver_fp --- source/Makefile.Objects | 1 + source/source_esolver/esolver_fp.cpp | 109 ++----------- source/source_esolver/esolver_fp.h | 2 +- source/source_pw/module_pwdft/CMakeLists.txt | 1 + source/source_pw/module_pwdft/setup_pwrho.cpp | 153 ++++-------------- source/source_pw/module_pwdft/setup_pwrho.h | 33 ++-- 6 files changed, 62 insertions(+), 237 deletions(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index c24ccd157b..537ad1a4dd 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -716,6 +716,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ charge_mixing_uspp.o\ fp_energy.o\ setup_pot.o\ + setup_pwrho.o\ forces.o\ forces_us.o\ forces_nl.o\ diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index d860b74673..3494911acf 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -1,12 +1,12 @@ #include "esolver_fp.h" -#include "source_base/global_variable.h" +//#include "source_base/global_variable.h" #include "source_estate/cal_ux.h" #include "source_estate/module_charge/symmetry_rho.h" #include "source_estate/read_pseudo.h" #include "source_hamilt/module_ewald/H_Ewald_pw.h" #include "source_hamilt/module_vdw/vdw.h" -#include "source_pw/module_pwdft/global.h" +//#include "source_pw/module_pwdft/global.h" #include "source_io/cif_io.h" #include "source_io/cube_io.h" // use write_vdata_palgrid #include "source_io/json_output/init_info.h" @@ -15,9 +15,12 @@ #include "source_io/print_info.h" #include "source_io/rhog_io.h" #include "source_io/module_parameter/parameter.h" -#include "source_cell/k_vector_utils.h" +//#include "source_cell/k_vector_utils.h" #include "source_io/ctrl_output_fp.h" +#include "source_pw/module_pwdft/setup_pwrho.h" // mohan 20251005 +#include "source_hamilt/module_xc/xc_functional.h" // mohan 20251005 + namespace ModuleESolver { @@ -42,102 +45,24 @@ ESolver_FP::~ESolver_FP() void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp) { ModuleBase::TITLE("ESolver_FP", "before_all_runners"); - std::string fft_device = inp.device; - std::string fft_precison = inp.precision; - // LCAO basis doesn't support GPU acceleration on FFT currently - if(inp.basis_type == "lcao") - { - fft_device = "cpu"; - } - if ((inp.precision=="single") || (inp.precision=="mixing")) - { - fft_precison = "mixing"; - } - else if (inp.precision=="double") - { - fft_precison = "double"; - } - #if (not defined(__ENABLE_FLOAT_FFTW) and (defined(__CUDA) || defined(__RCOM))) - if (fft_device == "gpu") - { - fft_precison = "double"; - } - #endif - pw_rho = new ModulePW::PW_Basis_Big(fft_device, fft_precison); - pw_rho_flag = true; - if (PARAM.globalv.double_grid) - { - pw_rhod = new ModulePW::PW_Basis_Big(fft_device, fft_precison); - } - else - { - pw_rhod = pw_rho; - } - pw_big = static_cast(pw_rhod); - pw_big->setbxyz(inp.bx, inp.by, inp.bz); - sf.set(pw_rhod, inp.nbspline); - //! 1) read pseudopotentials + //! read pseudopotentials elecstate::read_pseudo(GlobalV::ofs_running, ucell); - //! 2) initialie the plane wave basis for rho -#ifdef __MPI - this->pw_rho->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); -#endif - if (this->classname == "ESolver_OF" || inp.of_ml_gene_data == 1) - { - this->pw_rho->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); - } - - if (inp.nx * inp.ny * inp.nz == 0) - { - this->pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, 4.0 * inp.ecutwfc); - } - else - { - this->pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.nx, inp.ny, inp.nz); - } + // setup pw_rho, pw_rhod, pw_big, sf, and read_pseudopotentials + pw::setup_pwrho(ucell, PARAM.globalv.double_grid, this->pw_rho_flag, + this->pw_rho, this->pw_rhod, this->pw_big, + this->classname, inp); - this->pw_rho->initparameters(false, 4.0 * inp.ecutwfc); - this->pw_rho->fft_bundle.initfftmode(inp.fft_mode); - this->pw_rho->setuptransform(); - this->pw_rho->collect_local_pw(); - this->pw_rho->collect_uniqgg(); + // setup the structure factors + this->sf.set(this->pw_rhod, inp.nbspline); - //! 3) initialize the double grid (for uspp) if necessary - if ( PARAM.globalv.double_grid) - { - ModulePW::PW_Basis_Sup* pw_rhod_sup = static_cast(pw_rhod); -#ifdef __MPI - this->pw_rhod->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); -#endif - if (this->classname == "ESolver_OF") - { - this->pw_rhod->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); - } - if (inp.ndx * inp.ndy * inp.ndz == 0) - { - this->pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ecutrho); - } - else - { - this->pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ndx, inp.ndy, inp.ndz); - } - this->pw_rhod->initparameters(false, inp.ecutrho); - this->pw_rhod->fft_bundle.initfftmode(inp.fft_mode); - pw_rhod_sup->setuptransform(this->pw_rho); - this->pw_rhod->collect_local_pw(); - this->pw_rhod->collect_uniqgg(); - } ModuleIO::CifParser::write(PARAM.globalv.global_out_dir + "STRU.cif", ucell, "# Generated by ABACUS ModuleIO::CifParser", "data_?"); - //! 4) print some information - ModuleIO::print_rhofft(this->pw_rhod, this->pw_rho, this->pw_big, GlobalV::ofs_running); - - //! 5) initialize the charge extrapolation method if necessary + //! initialize the charge extrapolation method if necessary this->CE.Init_CE(inp.nspin, ucell.nat, this->pw_rhod->nrxx, inp.chg_extrap); return; @@ -233,16 +158,10 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) this->pelec->f_en.ewald_energy = H_Ewald_pw::compute_ewald(ucell, this->pw_rhod, this->sf.strucFac); } - //---------------------------------------------------------- //! set direction of magnetism, used in non-collinear case - //---------------------------------------------------------- elecstate::cal_ux(ucell); - - - //---------------------------------------------------------- //! output the initial charge density - //---------------------------------------------------------- const int nspin = PARAM.inp.nspin; if (PARAM.inp.out_chg[0] == 2) { diff --git a/source/source_esolver/esolver_fp.h b/source/source_esolver/esolver_fp.h index 7d4f34736e..8e4741129a 100644 --- a/source/source_esolver/esolver_fp.h +++ b/source/source_esolver/esolver_fp.h @@ -82,7 +82,7 @@ class ESolver_FP: public ESolver //! solvent model surchem solvent; - int pw_rho_flag = false; ///< flag for pw_rho, 0: not initialized, 1: initialized + bool pw_rho_flag = false; ///< flag for pw_rho, 0: not initialized, 1: initialized //! the start time of scf iteration #ifdef __MPI diff --git a/source/source_pw/module_pwdft/CMakeLists.txt b/source/source_pw/module_pwdft/CMakeLists.txt index be44682dab..fdfe37828a 100644 --- a/source/source_pw/module_pwdft/CMakeLists.txt +++ b/source/source_pw/module_pwdft/CMakeLists.txt @@ -13,6 +13,7 @@ list(APPEND objects operator_pw/exx_pw_ace.cpp operator_pw/exx_pw_pot.cpp setup_pot.cpp + setup_pwrho.cpp forces_nl.cpp forces_cc.cpp forces_scc.cpp diff --git a/source/source_pw/module_pwdft/setup_pwrho.cpp b/source/source_pw/module_pwdft/setup_pwrho.cpp index f1a526bed5..e50910cd4f 100644 --- a/source/source_pw/module_pwdft/setup_pwrho.cpp +++ b/source/source_pw/module_pwdft/setup_pwrho.cpp @@ -1,26 +1,15 @@ #include "source_pw/module_pwdft/setup_pwrho.h" +#include "source_io/print_info.h" // use print_rhofft +#include "source_base/parallel_comm.h" // use POOL_WORLD -#include "source_estate/module_charge/symmetry_rho.h" -#include "source_lcao/module_deltaspin/spin_constrain.h" -#include "source_pw/module_pwdft/onsite_projector.h" -#include "source_lcao/module_dftu/dftu.h" -#include "source_pw/module_pwdft/VSep_in_pw.h" - -template -void pw::setup_pot(const int istep, +void pw::setup_pwrho( UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints - Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - psi::Psi* kspw_psi, // electronic wave functions - hamilt::Hamilt* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const bool double_grid, // for USPP + bool &pw_rho_flag, // flag for allocation of pw_rho + ModulePW::PW_Basis* &pw_rho, // pw for rhod + ModulePW::PW_Basis* &pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* &pw_big, // pw for rhod + const std::string &classname, const Input_para& inp) // input parameters * { ModuleBase::TITLE("pw", "setup_pwrho"); @@ -57,7 +46,7 @@ void pw::setup_pot(const int istep, pw_rho_flag = true; // initialize pw_rhod - if (PARAM.globalv.double_grid) + if (double_grid) { pw_rhod = new ModulePW::PW_Basis_Big(fft_device, fft_precision); } @@ -70,137 +59,61 @@ void pw::setup_pot(const int istep, pw_big = static_cast(pw_rhod); pw_big->setbxyz(inp.bx, inp.by, inp.bz); - // setup the structure factors - sf.set(pw_rhod, inp.nbspline); - - //! read pseudopotentials, move somewhere else??? mohan note 20251005 - elecstate::read_pseudo(GlobalV::ofs_running, ucell); - //! initialie the plane wave basis for rho #ifdef __MPI - this->pw_rho->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); + pw_rho->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); #endif //! for OFDFT calculations - if (this->classname == "ESolver_OF" || inp.of_ml_gene_data == 1) + if (classname == "ESolver_OF" || inp.of_ml_gene_data == 1) { - this->pw_rho->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); + pw_rho->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); } //! initialize the FFT grid if (inp.nx * inp.ny * inp.nz == 0) { - this->pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, 4.0 * inp.ecutwfc); + pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, 4.0 * inp.ecutwfc); } else { - this->pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.nx, inp.ny, inp.nz); + pw_rho->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.nx, inp.ny, inp.nz); } - this->pw_rho->initparameters(false, 4.0 * inp.ecutwfc); - this->pw_rho->fft_bundle.initfftmode(inp.fft_mode); - this->pw_rho->setuptransform(); - this->pw_rho->collect_local_pw(); - this->pw_rho->collect_uniqgg(); + pw_rho->initparameters(false, 4.0 * inp.ecutwfc); + pw_rho->fft_bundle.initfftmode(inp.fft_mode); + pw_rho->setuptransform(); + pw_rho->collect_local_pw(); + pw_rho->collect_uniqgg(); //! initialize the double grid (for uspp) if necessary - if ( PARAM.globalv.double_grid) + if (double_grid) { ModulePW::PW_Basis_Sup* pw_rhod_sup = static_cast(pw_rhod); #ifdef __MPI - this->pw_rhod->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); + pw_rhod->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); #endif - if (this->classname == "ESolver_OF") + if (classname == "ESolver_OF") { - this->pw_rhod->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); + pw_rhod->setfullpw(inp.of_full_pw, inp.of_full_pw_dim); } if (inp.ndx * inp.ndy * inp.ndz == 0) { - this->pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ecutrho); + pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ecutrho); } else { - this->pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ndx, inp.ndy, inp.ndz); + pw_rhod->initgrids(inp.ref_cell_factor * ucell.lat0, ucell.latvec, inp.ndx, inp.ndy, inp.ndz); } - this->pw_rhod->initparameters(false, inp.ecutrho); - this->pw_rhod->fft_bundle.initfftmode(inp.fft_mode); - pw_rhod_sup->setuptransform(this->pw_rho); - this->pw_rhod->collect_local_pw(); - this->pw_rhod->collect_uniqgg(); + pw_rhod->initparameters(false, inp.ecutrho); + pw_rhod->fft_bundle.initfftmode(inp.fft_mode); + pw_rhod_sup->setuptransform(pw_rho); + pw_rhod->collect_local_pw(); + pw_rhod->collect_uniqgg(); } + ModuleIO::print_rhofft(pw_rhod, pw_rho, pw_big, GlobalV::ofs_running); + return; } -template void pw::setup_pot, base_device::DEVICE_CPU>( - const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints - Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters - - -template void pw::setup_pot, base_device::DEVICE_CPU>( - const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints - Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters - -#if ((defined __CUDA) || (defined __ROCM)) - -template void pw::setup_pot, base_device::DEVICE_GPU>( - const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints - Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters - -template void pw::setup_pot, base_device::DEVICE_GPU>( - const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints - Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters - -#endif diff --git a/source/source_pw/module_pwdft/setup_pwrho.h b/source/source_pw/module_pwdft/setup_pwrho.h index 9146d6303c..7b4e532e6f 100644 --- a/source/source_pw/module_pwdft/setup_pwrho.h +++ b/source/source_pw/module_pwdft/setup_pwrho.h @@ -2,32 +2,23 @@ #define SETUP_PWRHO_H #include "source_base/module_device/device.h" // use Device -#include "source_cell/unitcell.h" -#include "source_cell/klist.h" -#include "source_pw/module_pwdft/structure_factor.h" -#include "source_estate/elecstate.h" -#include "source_pw/module_pwdft/VL_in_pw.h" -#include "source_hamilt/hamilt.h" +#include "source_cell/unitcell.h" // use UnitCell +#include "source_pw/module_pwdft/structure_factor.h" // use Structure_Factor +#include "source_basis/module_pw/pw_basis.h" // use PW_Basis +#include "source_io/module_parameter/input_parameter.h" // use Input_para namespace pw { -template -void setup_pot(const int istep, +void setup_pwrho( UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints - Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials - VSep* vsep_cell, // U-1/2 method - psi::Psi* kspw_psi, // electronic wave functions - hamilt::Hamilt* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters + const bool double_grid, // for USPP + bool &pw_rho_flag, // flag for allocation of pw_rho + ModulePW::PW_Basis* &pw_rho, // pw for rhod + ModulePW::PW_Basis* &pw_rhod, // pw for rhod + ModulePW::PW_Basis_Big* &pw_big, // pw for rhod + const std::string &classname, + const Input_para& inp); // input parameters * } From 3268459a158ea3ce1cdb73f1e84781a8669c8c42 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 15:12:40 +0800 Subject: [PATCH 35/60] update esolver_fp --- source/source_esolver/esolver_fp.cpp | 22 ++++++---------------- source/source_esolver/esolver_fp.h | 2 -- source/source_esolver/esolver_ks.cpp | 1 - 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index 3494911acf..2e26b79fee 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -1,12 +1,10 @@ #include "esolver_fp.h" -//#include "source_base/global_variable.h" #include "source_estate/cal_ux.h" #include "source_estate/module_charge/symmetry_rho.h" #include "source_estate/read_pseudo.h" #include "source_hamilt/module_ewald/H_Ewald_pw.h" #include "source_hamilt/module_vdw/vdw.h" -//#include "source_pw/module_pwdft/global.h" #include "source_io/cif_io.h" #include "source_io/cube_io.h" // use write_vdata_palgrid #include "source_io/json_output/init_info.h" @@ -15,7 +13,6 @@ #include "source_io/print_info.h" #include "source_io/rhog_io.h" #include "source_io/module_parameter/parameter.h" -//#include "source_cell/k_vector_utils.h" #include "source_io/ctrl_output_fp.h" #include "source_pw/module_pwdft/setup_pwrho.h" // mohan 20251005 @@ -73,16 +70,16 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso { ModuleBase::TITLE("ESolver_FP", "after_scf"); - // 1) output convergence information + //! Output convergence information ModuleIO::output_convergence_after_scf(conv_esolver, this->pelec->f_en.etot); - // 2) write fermi energy + //! Write Fermi energy ModuleIO::output_efermi(conv_esolver, this->pelec->eferm.ef); - // 3) update delta_rho for charge extrapolation + //! Update delta_rho for charge extrapolation CE.update_delta_rho(ucell, &(this->chr), &(this->sf)); - // 4) print out charge density, potential, elf, etc. + //! print out charge density, potential, elf, etc. ModuleIO::ctrl_output_fp(ucell, this->pelec, this->pw_big, this->pw_rhod, this->chr, this->solvent, this->Pgrid, istep); @@ -127,9 +124,7 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS"); } - //---------------------------------------------------------- // charge extrapolation - //---------------------------------------------------------- if (ucell.ionic_position_updated) { this->CE.update_all_dis(ucell); @@ -141,18 +136,14 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) GlobalV::ofs_warning); } - //---------------------------------------------------------- //! calculate D2 or D3 vdW - //---------------------------------------------------------- auto vdw_solver = vdw::make_vdw(ucell, PARAM.inp, &(GlobalV::ofs_running)); if (vdw_solver != nullptr) { this->pelec->f_en.evdw = vdw_solver->get_energy(); } - //---------------------------------------------------------- //! calculate ewald energy - //---------------------------------------------------------- if (!PARAM.inp.test_skip_ewald) { this->pelec->f_en.ewald_energy = H_Ewald_pw::compute_ewald(ucell, this->pw_rhod, this->sf.strucFac); @@ -190,9 +181,7 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) } } - //---------------------------------------------------------- //! output total local potential of the initial charge density - //---------------------------------------------------------- if (PARAM.inp.out_pot == 3) { for (int is = 0; is < nspin; is++) @@ -227,7 +216,7 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) { - //! output charge density + //! output charge density in G-space, or if available, kinetic energy density in G-space if (PARAM.inp.out_chg[0] != -1) { if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver) @@ -271,6 +260,7 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& void ESolver_FP::after_all_runners(UnitCell& ucell) { + // print out the final total energy GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; diff --git a/source/source_esolver/esolver_fp.h b/source/source_esolver/esolver_fp.h index 8e4741129a..b2bb8f065e 100644 --- a/source/source_esolver/esolver_fp.h +++ b/source/source_esolver/esolver_fp.h @@ -47,9 +47,7 @@ class ESolver_FP: public ESolver virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver); - //! ------------------------------------------------------------------------------ //! These pointers will be deleted in the free_pointers() function every ion step. - //! ------------------------------------------------------------------------------ elecstate::ElecState* pelec = nullptr; ///< Electronic states //! K points in Brillouin zone diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index ba919399e7..a92fd11fb9 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -26,7 +26,6 @@ #include "source_io/json_output/output_info.h" - namespace ModuleESolver { From 38093a15de9fab2d788ef1c28f93d6d589158988 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 15:59:53 +0800 Subject: [PATCH 36/60] add teardown function in setup_pwrho --- source/source_esolver/esolver_fp.cpp | 12 +++-------- source/source_pw/module_pwdft/setup_pwrho.cpp | 20 +++++++++++++++++++ source/source_pw/module_pwdft/setup_pwrho.h | 6 ++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index 2e26b79fee..766d47ed79 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -27,15 +27,6 @@ ESolver_FP::ESolver_FP() ESolver_FP::~ESolver_FP() { - if (pw_rho_flag == true) - { - delete this->pw_rho; - this->pw_rho_flag = false; - } - if (PARAM.globalv.double_grid) - { - delete this->pw_rhod; - } delete this->pelec; } @@ -265,6 +256,9 @@ void ESolver_FP::after_all_runners(UnitCell& ucell) GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; GlobalV::ofs_running << " --------------------------------------------\n\n" << std::endl; + + // mohan add 20251005 + pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); } } // namespace ModuleESolver diff --git a/source/source_pw/module_pwdft/setup_pwrho.cpp b/source/source_pw/module_pwdft/setup_pwrho.cpp index e50910cd4f..10585f6131 100644 --- a/source/source_pw/module_pwdft/setup_pwrho.cpp +++ b/source/source_pw/module_pwdft/setup_pwrho.cpp @@ -117,3 +117,23 @@ void pw::setup_pwrho( return; } + +void pw::teardown_pwrho(bool &pw_rho_flag, + const bool double_grid, + ModulePW::PW_Basis* &pw_rho, // pw for rhod + ModulePW::PW_Basis* &pw_rhod) // pw for rhod +{ + if (pw_rho_flag == true) + { + delete pw_rho; + pw_rho_flag = false; + } + + if (double_grid) + { + delete pw_rhod; + } + + return; +} + diff --git a/source/source_pw/module_pwdft/setup_pwrho.h b/source/source_pw/module_pwdft/setup_pwrho.h index 7b4e532e6f..820edb9d2d 100644 --- a/source/source_pw/module_pwdft/setup_pwrho.h +++ b/source/source_pw/module_pwdft/setup_pwrho.h @@ -20,6 +20,12 @@ void setup_pwrho( const std::string &classname, const Input_para& inp); // input parameters * + +void teardown_pwrho(bool &pw_rho_flag, + const bool double_grid, + ModulePW::PW_Basis* &pw_rho, // pw for rhod + ModulePW::PW_Basis* &pw_rhod); // pw for rhod + } From 440b690711551e0e39138d752da8bd0a2dddfb98 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 16:27:13 +0800 Subject: [PATCH 37/60] update esolver --- source/source_esolver/esolver.h | 3 +++ source/source_esolver/esolver_fp.cpp | 10 +++++++--- source/source_esolver/esolver_ks.cpp | 5 ++++- source/source_esolver/esolver_ks_lcao.cpp | 3 +++ source/source_esolver/esolver_ks_lcao_tddft.cpp | 5 ++++- source/source_esolver/esolver_ks_lcaopw.cpp | 3 +++ source/source_esolver/esolver_ks_pw.cpp | 5 ++++- source/source_esolver/esolver_of.cpp | 5 ++++- source/source_esolver/esolver_sdft_pw.cpp | 3 +++ source/source_pw/module_pwdft/setup_pwrho.cpp | 4 +++- 10 files changed, 38 insertions(+), 8 deletions(-) diff --git a/source/source_esolver/esolver.h b/source/source_esolver/esolver.h index 58238dcb1b..6716ea0c96 100644 --- a/source/source_esolver/esolver.h +++ b/source/source_esolver/esolver.h @@ -17,6 +17,9 @@ class ESolver virtual ~ESolver() { + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** } //! initialize the energy solver by using input parameters and cell modules diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index 766d47ed79..f6aff301bd 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -27,7 +27,13 @@ ESolver_FP::ESolver_FP() ESolver_FP::~ESolver_FP() { - delete this->pelec; + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** + // mohan add 20251005 + pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); + + delete this->pelec; } void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp) @@ -257,8 +263,6 @@ void ESolver_FP::after_all_runners(UnitCell& ucell) GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; GlobalV::ofs_running << " --------------------------------------------\n\n" << std::endl; - // mohan add 20251005 - pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); } } // namespace ModuleESolver diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index a92fd11fb9..b477c89656 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -38,7 +38,10 @@ ESolver_KS::ESolver_KS() template ESolver_KS::~ESolver_KS() { - delete this->psi; + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** + delete this->psi; delete this->pw_wfc; delete this->p_hamilt; delete this->p_chgmix; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index e7c212f829..b7ef11ed55 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -97,6 +97,9 @@ ESolver_KS_LCAO::ESolver_KS_LCAO() template ESolver_KS_LCAO::~ESolver_KS_LCAO() { + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** } template diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index 857da73913..c68ff0955b 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -57,7 +57,10 @@ ESolver_KS_LCAO_TDDFT::ESolver_KS_LCAO_TDDFT() template ESolver_KS_LCAO_TDDFT::~ESolver_KS_LCAO_TDDFT() { - delete psi_laststep; + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** + delete psi_laststep; if (Hk_laststep != nullptr) { for (int ik = 0; ik < this->kv.get_nks(); ++ik) diff --git a/source/source_esolver/esolver_ks_lcaopw.cpp b/source/source_esolver/esolver_ks_lcaopw.cpp index 1b0b7dae67..8597fa6847 100644 --- a/source/source_esolver/esolver_ks_lcaopw.cpp +++ b/source/source_esolver/esolver_ks_lcaopw.cpp @@ -51,6 +51,9 @@ namespace ModuleESolver template ESolver_KS_LIP::~ESolver_KS_LIP() { + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** delete this->psi_local; // delete Hamilt this->deallocate_hamilt(); diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 00d00d87bb..6bb78b5c25 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -42,7 +42,10 @@ ESolver_KS_PW::ESolver_KS_PW() template ESolver_KS_PW::~ESolver_KS_PW() { - // delete Hamilt + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** + // delete Hamilt this->deallocate_hamilt(); if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 45f26babcb..6a53152030 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -25,7 +25,10 @@ ESolver_OF::ESolver_OF() ESolver_OF::~ESolver_OF() { - delete psi_; + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** + delete psi_; delete[] this->pphi_; for (int i = 0; i < PARAM.inp.nspin; ++i) diff --git a/source/source_esolver/esolver_sdft_pw.cpp b/source/source_esolver/esolver_sdft_pw.cpp index 42fde3ebe5..170147ba06 100644 --- a/source/source_esolver/esolver_sdft_pw.cpp +++ b/source/source_esolver/esolver_sdft_pw.cpp @@ -27,6 +27,9 @@ ESolver_SDFT_PW::ESolver_SDFT_PW() template ESolver_SDFT_PW::~ESolver_SDFT_PW() { + //**************************************************** + // do not add any codes in this deconstructor funcion + //**************************************************** } template diff --git a/source/source_pw/module_pwdft/setup_pwrho.cpp b/source/source_pw/module_pwdft/setup_pwrho.cpp index 10585f6131..e08ee71d23 100644 --- a/source/source_pw/module_pwdft/setup_pwrho.cpp +++ b/source/source_pw/module_pwdft/setup_pwrho.cpp @@ -126,12 +126,14 @@ void pw::teardown_pwrho(bool &pw_rho_flag, if (pw_rho_flag == true) { delete pw_rho; + pw_rho = nullptr; pw_rho_flag = false; } - if (double_grid) + if (double_grid == true) { delete pw_rhod; + pw_rhod = nullptr; } return; From d672bca02af960b697f07179fecf14fce697d71a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 16:48:15 +0800 Subject: [PATCH 38/60] move the after_all_runners() function of the base class to the end --- source/source_esolver/esolver_gets.cpp | 5 ++++- source/source_esolver/esolver_ks.cpp | 2 +- source/source_esolver/esolver_ks_lcao.cpp | 4 +++- source/source_esolver/esolver_ks_lcaopw.cpp | 5 ++++- source/source_esolver/esolver_ks_pw.cpp | 4 +++- source/source_esolver/esolver_of.cpp | 1 + source/source_esolver/esolver_sdft_pw.cpp | 13 +++++++------ 7 files changed, 23 insertions(+), 11 deletions(-) diff --git a/source/source_esolver/esolver_gets.cpp b/source/source_esolver/esolver_gets.cpp index 300fd532b2..0bd78e9a73 100644 --- a/source/source_esolver/esolver_gets.cpp +++ b/source/source_esolver/esolver_gets.cpp @@ -154,11 +154,14 @@ void ESolver_GetS::runner(UnitCell& ucell, const int istep) ModuleBase::timer::tick("ESolver_GetS", "runner"); } -void ESolver_GetS::after_all_runners(UnitCell& ucell) {}; +void ESolver_GetS::after_all_runners(UnitCell& ucell) +{}; + double ESolver_GetS::cal_energy() { return 0.0; }; + void ESolver_GetS::cal_force(UnitCell& ucell, ModuleBase::matrix& force) {}; void ESolver_GetS::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) {}; diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index b477c89656..eda9803e2d 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -606,7 +606,7 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo template void ESolver_KS::after_all_runners(UnitCell& ucell) { - // 1) write Etot information + //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index b7ef11ed55..1a09ab7dd4 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -410,7 +410,6 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) ModuleBase::TITLE("ESolver_KS_LCAO", "after_all_runners"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); - ESolver_KS::after_all_runners(ucell); const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; @@ -512,6 +511,9 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) ); } + //! Ensure the function of base class is called last + ESolver_KS::after_all_runners(ucell); + ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); } diff --git a/source/source_esolver/esolver_ks_lcaopw.cpp b/source/source_esolver/esolver_ks_lcaopw.cpp index 8597fa6847..2a52d15c35 100644 --- a/source/source_esolver/esolver_ks_lcaopw.cpp +++ b/source/source_esolver/esolver_ks_lcaopw.cpp @@ -236,7 +236,6 @@ namespace ModuleESolver template void ESolver_KS_LIP::after_all_runners(UnitCell& ucell) { - ESolver_KS_PW::after_all_runners(ucell); #ifdef __LCAO if (PARAM.inp.out_mat_xc) @@ -262,6 +261,10 @@ namespace ModuleESolver ); } #endif + + //! Ensure the function of base class is called last + ESolver_KS_PW::after_all_runners(ucell); + } template class ESolver_KS_LIP>; template class ESolver_KS_LIP>; diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 6bb78b5c25..94bf3d4f8b 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -530,7 +530,6 @@ void ESolver_KS_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& s template void ESolver_KS_PW::after_all_runners(UnitCell& ucell) { - ESolver_KS::after_all_runners(ucell); ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, @@ -538,6 +537,9 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); + + //! Ensure the function of base class is called last + ESolver_KS::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 6a53152030..027ba518bd 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -540,6 +540,7 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso */ void ESolver_OF::after_all_runners(UnitCell& ucell) { + //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_sdft_pw.cpp b/source/source_esolver/esolver_sdft_pw.cpp index 170147ba06..9f9ff5429e 100644 --- a/source/source_esolver/esolver_sdft_pw.cpp +++ b/source/source_esolver/esolver_sdft_pw.cpp @@ -259,16 +259,13 @@ void ESolver_SDFT_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& template void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) { - // 1) write down etot and eigenvalues (for MDFT) information - ESolver_FP::after_all_runners(ucell); - - // 2) release memory + // release memory if (this->method_sto == 2) { stowf.clean_chiallorder(); // release lots of memories } - // 3) write down DOS + // write down DOS if (PARAM.inp.out_dos) { if(!std::is_same>::value || !std::is_same::value) @@ -294,7 +291,7 @@ void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) sto_dos.caldos(PARAM.inp.dos_sigma, PARAM.inp.dos_edelta_ev, PARAM.inp.npart_sto); } - // 4) sKG cost memory, and it should be placed at the end of the program + // sKG cost memory, and it should be placed near the end of the program if (PARAM.inp.cal_cond) { Sto_EleCond sto_elecond(&ucell, @@ -315,6 +312,10 @@ void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) PARAM.inp.cond_nonlocal, PARAM.inp.npart_sto); } + + // write down etot and eigenvalues (for MDFT) information + ESolver_FP::after_all_runners(ucell); + } From 16ba5d2f51ee3a055a47a10a2138033205707e13 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 17:13:40 +0800 Subject: [PATCH 39/60] add TITLE for after_all_runners --- source/source_esolver/esolver_dp.cpp | 1 + source/source_esolver/esolver_fp.cpp | 5 +++-- source/source_esolver/esolver_ks.cpp | 1 + source/source_esolver/esolver_ks_lcao.cpp | 1 - source/source_esolver/esolver_ks_pw.cpp | 1 + source/source_esolver/esolver_lj.cpp | 1 + source/source_esolver/esolver_of.cpp | 2 ++ source/source_esolver/esolver_sdft_pw.cpp | 2 ++ 8 files changed, 11 insertions(+), 3 deletions(-) diff --git a/source/source_esolver/esolver_dp.cpp b/source/source_esolver/esolver_dp.cpp index 50ab8b0e78..a943be2914 100644 --- a/source/source_esolver/esolver_dp.cpp +++ b/source/source_esolver/esolver_dp.cpp @@ -148,6 +148,7 @@ void ESolver_DP::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) void ESolver_DP::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_DP", "after_all_runners"); GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << dp_potential * ModuleBase::Ry_to_eV << " eV" << std::endl; diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index f6aff301bd..d6e55b1fad 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -30,8 +30,6 @@ ESolver_FP::~ESolver_FP() //**************************************************** // do not add any codes in this deconstructor funcion //**************************************************** - // mohan add 20251005 - pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); delete this->pelec; } @@ -257,12 +255,15 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& void ESolver_FP::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_FP", "after_all_runners"); // print out the final total energy GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; GlobalV::ofs_running << " --------------------------------------------\n\n" << std::endl; + // mohan add 20251005, delete pw_rho and pw_rhod, ensure following functions will not use these + pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); } } // namespace ModuleESolver diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index eda9803e2d..0bc0835f19 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -606,6 +606,7 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo template void ESolver_KS::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_KS", "after_all_runners"); //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 1a09ab7dd4..806bce1f7c 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -410,7 +410,6 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) ModuleBase::TITLE("ESolver_KS_LCAO", "after_all_runners"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); - const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; // 1) write projected band structure diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 94bf3d4f8b..b169eba91c 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -530,6 +530,7 @@ void ESolver_KS_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& s template void ESolver_KS_PW::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_KS_PW", "after_all_runners"); ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, diff --git a/source/source_esolver/esolver_lj.cpp b/source/source_esolver/esolver_lj.cpp index 3f1dd95f23..2a73c68aa7 100644 --- a/source/source_esolver/esolver_lj.cpp +++ b/source/source_esolver/esolver_lj.cpp @@ -120,6 +120,7 @@ void ESolver_LJ::runner(UnitCell& ucell, const int istep) void ESolver_LJ::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_LJ", "after_all_runners"); GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << lj_potential * ModuleBase::Ry_to_eV << " eV" << std::endl; diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 027ba518bd..cd136f8428 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -540,6 +540,8 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso */ void ESolver_OF::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_OF", "after_all_runners"); + //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_sdft_pw.cpp b/source/source_esolver/esolver_sdft_pw.cpp index 9f9ff5429e..48c5096d36 100644 --- a/source/source_esolver/esolver_sdft_pw.cpp +++ b/source/source_esolver/esolver_sdft_pw.cpp @@ -259,6 +259,8 @@ void ESolver_SDFT_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& template void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) { + ModuleBase::TITLE("ESolver_SDFT_PW", "after_all_runners"); + // release memory if (this->method_sto == 2) { From 8516477d0ab3078fa2613578f8c2f2dd6fcbb84f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 18:02:12 +0800 Subject: [PATCH 40/60] update esolver_fp, move teardown to desconstructor --- source/source_esolver/esolver_fp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index d6e55b1fad..4666d95ecb 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -30,6 +30,8 @@ ESolver_FP::~ESolver_FP() //**************************************************** // do not add any codes in this deconstructor funcion //**************************************************** + // mohan add 20251005, delete pw_rho and pw_rhod, ensure following functions will not use these + pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); delete this->pelec; } @@ -262,8 +264,6 @@ void ESolver_FP::after_all_runners(UnitCell& ucell) GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; GlobalV::ofs_running << " --------------------------------------------\n\n" << std::endl; - // mohan add 20251005, delete pw_rho and pw_rhod, ensure following functions will not use these - pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); } } // namespace ModuleESolver From 9713afb2984d1a9a736484840f5ace80f3d3129e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 18:11:44 +0800 Subject: [PATCH 41/60] Revert "update esolver_fp, move teardown to desconstructor" This reverts commit 8516477d0ab3078fa2613578f8c2f2dd6fcbb84f. --- source/source_esolver/esolver_fp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index 4666d95ecb..d6e55b1fad 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -30,8 +30,6 @@ ESolver_FP::~ESolver_FP() //**************************************************** // do not add any codes in this deconstructor funcion //**************************************************** - // mohan add 20251005, delete pw_rho and pw_rhod, ensure following functions will not use these - pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); delete this->pelec; } @@ -264,6 +262,8 @@ void ESolver_FP::after_all_runners(UnitCell& ucell) GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; GlobalV::ofs_running << " --------------------------------------------\n\n" << std::endl; + // mohan add 20251005, delete pw_rho and pw_rhod, ensure following functions will not use these + pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); } } // namespace ModuleESolver From de9d3de9feaf791e9599f07256fbf4319a03bdf4 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 18:12:11 +0800 Subject: [PATCH 42/60] Revert "add TITLE for after_all_runners" This reverts commit 16ba5d2f51ee3a055a47a10a2138033205707e13. --- source/source_esolver/esolver_dp.cpp | 1 - source/source_esolver/esolver_fp.cpp | 5 ++--- source/source_esolver/esolver_ks.cpp | 1 - source/source_esolver/esolver_ks_lcao.cpp | 1 + source/source_esolver/esolver_ks_pw.cpp | 1 - source/source_esolver/esolver_lj.cpp | 1 - source/source_esolver/esolver_of.cpp | 2 -- source/source_esolver/esolver_sdft_pw.cpp | 2 -- 8 files changed, 3 insertions(+), 11 deletions(-) diff --git a/source/source_esolver/esolver_dp.cpp b/source/source_esolver/esolver_dp.cpp index a943be2914..50ab8b0e78 100644 --- a/source/source_esolver/esolver_dp.cpp +++ b/source/source_esolver/esolver_dp.cpp @@ -148,7 +148,6 @@ void ESolver_DP::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) void ESolver_DP::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_DP", "after_all_runners"); GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << dp_potential * ModuleBase::Ry_to_eV << " eV" << std::endl; diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index d6e55b1fad..f6aff301bd 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -30,6 +30,8 @@ ESolver_FP::~ESolver_FP() //**************************************************** // do not add any codes in this deconstructor funcion //**************************************************** + // mohan add 20251005 + pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); delete this->pelec; } @@ -255,15 +257,12 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& void ESolver_FP::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_FP", "after_all_runners"); // print out the final total energy GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl; GlobalV::ofs_running << " --------------------------------------------\n\n" << std::endl; - // mohan add 20251005, delete pw_rho and pw_rhod, ensure following functions will not use these - pw::teardown_pwrho(this->pw_rho_flag, PARAM.globalv.double_grid, this->pw_rho, this->pw_rhod); } } // namespace ModuleESolver diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 0bc0835f19..eda9803e2d 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -606,7 +606,6 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo template void ESolver_KS::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_KS", "after_all_runners"); //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 806bce1f7c..1a09ab7dd4 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -410,6 +410,7 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) ModuleBase::TITLE("ESolver_KS_LCAO", "after_all_runners"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); + const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; // 1) write projected band structure diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index b169eba91c..94bf3d4f8b 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -530,7 +530,6 @@ void ESolver_KS_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& s template void ESolver_KS_PW::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_KS_PW", "after_all_runners"); ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, diff --git a/source/source_esolver/esolver_lj.cpp b/source/source_esolver/esolver_lj.cpp index 2a73c68aa7..3f1dd95f23 100644 --- a/source/source_esolver/esolver_lj.cpp +++ b/source/source_esolver/esolver_lj.cpp @@ -120,7 +120,6 @@ void ESolver_LJ::runner(UnitCell& ucell, const int istep) void ESolver_LJ::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_LJ", "after_all_runners"); GlobalV::ofs_running << "\n --------------------------------------------" << std::endl; GlobalV::ofs_running << std::setprecision(16); GlobalV::ofs_running << " !FINAL_ETOT_IS " << lj_potential * ModuleBase::Ry_to_eV << " eV" << std::endl; diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index cd136f8428..027ba518bd 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -540,8 +540,6 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso */ void ESolver_OF::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_OF", "after_all_runners"); - //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_sdft_pw.cpp b/source/source_esolver/esolver_sdft_pw.cpp index 48c5096d36..9f9ff5429e 100644 --- a/source/source_esolver/esolver_sdft_pw.cpp +++ b/source/source_esolver/esolver_sdft_pw.cpp @@ -259,8 +259,6 @@ void ESolver_SDFT_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& template void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) { - ModuleBase::TITLE("ESolver_SDFT_PW", "after_all_runners"); - // release memory if (this->method_sto == 2) { From 2163ef5e965152b2d15e45162e844e0317639d1e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 5 Oct 2025 18:13:07 +0800 Subject: [PATCH 43/60] Revert "move the after_all_runners() function of the base class to the end" This reverts commit d672bca02af960b697f07179fecf14fce697d71a. --- source/source_esolver/esolver_gets.cpp | 5 +---- source/source_esolver/esolver_ks.cpp | 2 +- source/source_esolver/esolver_ks_lcao.cpp | 4 +--- source/source_esolver/esolver_ks_lcaopw.cpp | 5 +---- source/source_esolver/esolver_ks_pw.cpp | 4 +--- source/source_esolver/esolver_of.cpp | 1 - source/source_esolver/esolver_sdft_pw.cpp | 13 ++++++------- 7 files changed, 11 insertions(+), 23 deletions(-) diff --git a/source/source_esolver/esolver_gets.cpp b/source/source_esolver/esolver_gets.cpp index 0bd78e9a73..300fd532b2 100644 --- a/source/source_esolver/esolver_gets.cpp +++ b/source/source_esolver/esolver_gets.cpp @@ -154,14 +154,11 @@ void ESolver_GetS::runner(UnitCell& ucell, const int istep) ModuleBase::timer::tick("ESolver_GetS", "runner"); } -void ESolver_GetS::after_all_runners(UnitCell& ucell) -{}; - +void ESolver_GetS::after_all_runners(UnitCell& ucell) {}; double ESolver_GetS::cal_energy() { return 0.0; }; - void ESolver_GetS::cal_force(UnitCell& ucell, ModuleBase::matrix& force) {}; void ESolver_GetS::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) {}; diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index eda9803e2d..b477c89656 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -606,7 +606,7 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo template void ESolver_KS::after_all_runners(UnitCell& ucell) { - //! Ensure the function of base class is called last + // 1) write Etot information ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 1a09ab7dd4..b7ef11ed55 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -410,6 +410,7 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) ModuleBase::TITLE("ESolver_KS_LCAO", "after_all_runners"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); + ESolver_KS::after_all_runners(ucell); const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; @@ -511,9 +512,6 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) ); } - //! Ensure the function of base class is called last - ESolver_KS::after_all_runners(ucell); - ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); } diff --git a/source/source_esolver/esolver_ks_lcaopw.cpp b/source/source_esolver/esolver_ks_lcaopw.cpp index 2a52d15c35..8597fa6847 100644 --- a/source/source_esolver/esolver_ks_lcaopw.cpp +++ b/source/source_esolver/esolver_ks_lcaopw.cpp @@ -236,6 +236,7 @@ namespace ModuleESolver template void ESolver_KS_LIP::after_all_runners(UnitCell& ucell) { + ESolver_KS_PW::after_all_runners(ucell); #ifdef __LCAO if (PARAM.inp.out_mat_xc) @@ -261,10 +262,6 @@ namespace ModuleESolver ); } #endif - - //! Ensure the function of base class is called last - ESolver_KS_PW::after_all_runners(ucell); - } template class ESolver_KS_LIP>; template class ESolver_KS_LIP>; diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 94bf3d4f8b..6bb78b5c25 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -530,6 +530,7 @@ void ESolver_KS_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& s template void ESolver_KS_PW::after_all_runners(UnitCell& ucell) { + ESolver_KS::after_all_runners(ucell); ModuleIO::ctrl_runner_pw(ucell, this->pelec, this->pw_wfc, this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi, @@ -537,9 +538,6 @@ void ESolver_KS_PW::after_all_runners(UnitCell& ucell) this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp); elecstate::teardown_estate_pw(this->pelec, this->vsep_cell); - - //! Ensure the function of base class is called last - ESolver_KS::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 027ba518bd..6a53152030 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -540,7 +540,6 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso */ void ESolver_OF::after_all_runners(UnitCell& ucell) { - //! Ensure the function of base class is called last ESolver_FP::after_all_runners(ucell); } diff --git a/source/source_esolver/esolver_sdft_pw.cpp b/source/source_esolver/esolver_sdft_pw.cpp index 9f9ff5429e..170147ba06 100644 --- a/source/source_esolver/esolver_sdft_pw.cpp +++ b/source/source_esolver/esolver_sdft_pw.cpp @@ -259,13 +259,16 @@ void ESolver_SDFT_PW::cal_stress(UnitCell& ucell, ModuleBase::matrix& template void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) { - // release memory + // 1) write down etot and eigenvalues (for MDFT) information + ESolver_FP::after_all_runners(ucell); + + // 2) release memory if (this->method_sto == 2) { stowf.clean_chiallorder(); // release lots of memories } - // write down DOS + // 3) write down DOS if (PARAM.inp.out_dos) { if(!std::is_same>::value || !std::is_same::value) @@ -291,7 +294,7 @@ void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) sto_dos.caldos(PARAM.inp.dos_sigma, PARAM.inp.dos_edelta_ev, PARAM.inp.npart_sto); } - // sKG cost memory, and it should be placed near the end of the program + // 4) sKG cost memory, and it should be placed at the end of the program if (PARAM.inp.cal_cond) { Sto_EleCond sto_elecond(&ucell, @@ -312,10 +315,6 @@ void ESolver_SDFT_PW::after_all_runners(UnitCell& ucell) PARAM.inp.cond_nonlocal, PARAM.inp.npart_sto); } - - // write down etot and eigenvalues (for MDFT) information - ESolver_FP::after_all_runners(ucell); - } From 143e9c1796095151855f569abbe3b6650be268ee Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 10:17:17 +0800 Subject: [PATCH 44/60] update esolver, move chargemixing to module_charge --- source/Makefile.Objects | 1 + source/source_esolver/esolver_ks.cpp | 8 +-- source/source_esolver/esolver_ks.h | 21 ++----- source/source_esolver/esolver_ks_pw.cpp | 49 ++-------------- source/source_estate/CMakeLists.txt | 1 + .../source_estate/module_charge/chgmixing.cpp | 57 +++++++++++++++++++ .../source_estate/module_charge/chgmixing.h | 17 ++++++ source/source_pw/module_pwdft/setup_pwrho.h | 1 - 8 files changed, 87 insertions(+), 68 deletions(-) create mode 100644 source/source_estate/module_charge/chgmixing.cpp create mode 100644 source/source_estate/module_charge/chgmixing.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 537ad1a4dd..7510075da5 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -704,6 +704,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ VL_in_pw.o\ VNL_in_pw.o\ VNL_grad_pw.o\ + chgmixing.o\ charge.o\ charge_init.o\ charge_mpi.o\ diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index b477c89656..ba54f10e29 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -1,7 +1,5 @@ #include "esolver_ks.h" - -// To setup plane wave for electronic wave functions -#include "pw_setup.h" +#include "pw_setup.h" // setup plane wave #include "source_base/timer.h" #include "source_base/global_variable.h" @@ -152,10 +150,6 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para this->sf.setup_structure_factor(&ucell, Pgrid, this->pw_rhod); } -//------------------------------------------------------------------------------ -//! the 5th function of ESolver_KS: hamilt2rho_single -//! mohan add 2024-05-11 -//------------------------------------------------------------------------------ template void ESolver_KS::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) { diff --git a/source/source_esolver/esolver_ks.h b/source/source_esolver/esolver_ks.h index 974351a4a7..d54794dd89 100644 --- a/source/source_esolver/esolver_ks.h +++ b/source/source_esolver/esolver_ks.h @@ -1,21 +1,12 @@ #ifndef ESOLVER_KS_H #define ESOLVER_KS_H -#include -//#include - -// for first-principles esolver -#include "esolver_fp.h" -// for plane wave basis set -#include "source_basis/module_pw/pw_basis_k.h" -// for k-points in Brillouin zone -#include "source_cell/klist.h" -// for charge mixing -#include "source_estate/module_charge/charge_mixing.h" -// for electronic wave functions -#include "source_psi/psi.h" -// for Hamiltonian -#include "source_hamilt/hamilt.h" +#include "esolver_fp.h" // first-principles esolver +#include "source_basis/module_pw/pw_basis_k.h" // use plane wave +#include "source_cell/klist.h" // use k-points in Brillouin zone +#include "source_estate/module_charge/charge_mixing.h" // use charge mixing +#include "source_psi/psi.h" // use electronic wave functions +#include "source_hamilt/hamilt.h" // use Hamiltonian namespace ModuleESolver { diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 6bb78b5c25..19205647d2 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -22,11 +22,12 @@ #include "source_base/kernels/dsp/dsp_connector.h" #endif -#include +//#include #include "source_pw/module_pwdft/setup_pot.h" // mohan add 20250929 #include "source_estate/setup_estate_pw.h" // mohan add 20251005 #include "source_io/ctrl_output_pw.h" // mohan add 20250927 +#include "source_estate/module_charge/chgmixing.h" // use charge mixing, mohan add 20251006 namespace ModuleESolver { @@ -185,50 +186,8 @@ void ESolver_KS_PW::iter_init(UnitCell& ucell, const int istep, const // Call iter_init() of ESolver_KS ESolver_KS::iter_init(ucell, istep, iter); - if (iter == 1) - { - this->p_chgmix->init_mixing(); - this->p_chgmix->mixing_restart_step = PARAM.inp.scf_nmax + 1; - } - - // For mixing restart - if (iter == this->p_chgmix->mixing_restart_step && PARAM.inp.mixing_restart > 0.0) - { - this->p_chgmix->init_mixing(); - this->p_chgmix->mixing_restart_count++; - - if (PARAM.inp.dft_plus_u) - { - auto* dftu = ModuleDFTU::DFTU::get_instance(); - if (dftu->uramping > 0.01 && !dftu->u_converged()) - { - this->p_chgmix->mixing_restart_step = PARAM.inp.scf_nmax + 1; - } - if (dftu->uramping > 0.01) - { - bool do_uramping = true; - if (PARAM.inp.sc_mag_switch) - { - spinconstrain::SpinConstrain>& sc - = spinconstrain::SpinConstrain>::getScInstance(); - if (!sc.mag_converged()) // skip uramping if mag not converged - { - do_uramping = false; - } - } - if (do_uramping) - { - dftu->uramping_update(); // update U by uramping if uramping > 0.01 - std::cout << " U-Ramping! Current U = "; - for (int i = 0; i < dftu->U0.size(); i++) - { - std::cout << dftu->U[i] * ModuleBase::Ry_to_eV << " "; - } - std::cout << " eV " << std::endl; - } - } - } - } + // perform charge mixing + module_charge::chgmixing(iter, this->p_chgmix, PARAM.inp); // mohan move harris functional to here, 2012-06-05 // use 'rho(in)' and 'v_h and v_xc'(in) diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index e22dbfc3b0..f1bd096009 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND objects module_pot/potential_new.cpp module_pot/potential_types.cpp module_pot/pot_sep.cpp + module_charge/chgmixing.cpp module_charge/charge.cpp module_charge/charge_init.cpp module_charge/charge_mpi.cpp diff --git a/source/source_estate/module_charge/chgmixing.cpp b/source/source_estate/module_charge/chgmixing.cpp new file mode 100644 index 0000000000..0be2b8e72c --- /dev/null +++ b/source/source_estate/module_charge/chgmixing.cpp @@ -0,0 +1,57 @@ +#include "source_estate/module_charge/chgmixing.h" +#include "source_lcao/module_dftu/dftu.h" +#include "source_lcao/module_deltaspin/spin_constrain.h" + +void module_charge::chgmixing(const int iter, // scf iteration number + Charge_Mixing* p_chgmix, // charge mixing class + const Input_para& inp) // input parameters +{ + ModuleBase::TITLE("module_charge", "setup_chgmixing"); + + if (iter == 1) + { + p_chgmix->init_mixing(); + p_chgmix->mixing_restart_step = inp.scf_nmax + 1; + } + + // For mixing restart + if (iter == p_chgmix->mixing_restart_step && inp.mixing_restart > 0.0) + { + p_chgmix->init_mixing(); + p_chgmix->mixing_restart_count++; + + if (inp.dft_plus_u) + { + auto* dftu = ModuleDFTU::DFTU::get_instance(); + if (dftu->uramping > 0.01 && !dftu->u_converged()) + { + p_chgmix->mixing_restart_step = inp.scf_nmax + 1; + } + if (dftu->uramping > 0.01) + { + bool do_uramping = true; + if (inp.sc_mag_switch) + { + spinconstrain::SpinConstrain>& sc + = spinconstrain::SpinConstrain>::getScInstance(); + if (!sc.mag_converged()) // skip uramping if mag not converged + { + do_uramping = false; + } + } + if (do_uramping) + { + dftu->uramping_update(); // update U by uramping if uramping > 0.01 + std::cout << " U-Ramping! Current U = "; + for (int i = 0; i < dftu->U0.size(); i++) + { + std::cout << dftu->U[i] * ModuleBase::Ry_to_eV << " "; + } + std::cout << " eV " << std::endl; + } + } + } + } + + return; +} diff --git a/source/source_estate/module_charge/chgmixing.h b/source/source_estate/module_charge/chgmixing.h new file mode 100644 index 0000000000..f1214d187e --- /dev/null +++ b/source/source_estate/module_charge/chgmixing.h @@ -0,0 +1,17 @@ +#ifndef CHGMIXING_H +#define CHGMIXING_H + +#include "source_estate/module_charge/charge_mixing.h" +#include "source_io/module_parameter/input_parameter.h" // use Input_para + +namespace module_charge +{ + +void chgmixing(const int iter, + Charge_Mixing* p_chgmix, + const Input_para& inp); // input parameters + +} + + +#endif diff --git a/source/source_pw/module_pwdft/setup_pwrho.h b/source/source_pw/module_pwdft/setup_pwrho.h index 820edb9d2d..4e11848d62 100644 --- a/source/source_pw/module_pwdft/setup_pwrho.h +++ b/source/source_pw/module_pwdft/setup_pwrho.h @@ -1,7 +1,6 @@ #ifndef SETUP_PWRHO_H #define SETUP_PWRHO_H -#include "source_base/module_device/device.h" // use Device #include "source_cell/unitcell.h" // use UnitCell #include "source_pw/module_pwdft/structure_factor.h" // use Structure_Factor #include "source_basis/module_pw/pw_basis.h" // use PW_Basis From 4d89de7a352f79e813507c8aba2ee4002ee7bff0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 11:03:45 +0800 Subject: [PATCH 45/60] update dm pointer in esolver_ks_lcao --- source/source_esolver/esolver_ks.cpp | 13 ---- source/source_esolver/esolver_ks_lcao.cpp | 12 ++-- source/source_esolver/esolver_ks_pw.cpp | 2 +- .../source_estate/module_charge/chgmixing.cpp | 62 ++++++++++++++++++- .../source_estate/module_charge/chgmixing.h | 4 +- 5 files changed, 70 insertions(+), 23 deletions(-) diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index ba54f10e29..a00abc02e6 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -242,24 +242,16 @@ void ESolver_KS::runner(UnitCell& ucell, const int istep) this->scf_nmax_flag=true; } - //---------------------------------------------------------------- // 3) initialization of SCF iterations - //---------------------------------------------------------------- this->iter_init(ucell, istep, iter); - //---------------------------------------------------------------- // 4) use Hamiltonian to obtain charge density - //---------------------------------------------------------------- this->hamilt2rho(ucell, istep, iter, diag_ethr); - //---------------------------------------------------------------- // 5) finish scf iterations - //---------------------------------------------------------------- this->iter_finish(ucell, istep, iter, conv_esolver); - //---------------------------------------------------------------- // 6) check convergence - //---------------------------------------------------------------- if (conv_esolver || this->oscillate_esolver) { this->niter = iter; @@ -271,9 +263,7 @@ void ESolver_KS::runner(UnitCell& ucell, const int istep) } } // end scf iterations - //---------------------------------------------------------------- // 7) after scf - //---------------------------------------------------------------- this->after_scf(ucell, istep, conv_esolver); ModuleBase::timer::tick(this->classname, "runner"); @@ -592,9 +582,6 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo this->kv); } } - - - } template diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index b7ef11ed55..a03e6e56f6 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -523,6 +523,9 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const // call iter_init() of ESolver_KS ESolver_KS::iter_init(ucell, istep, iter); + elecstate::DensityMatrix* dm + = dynamic_cast*>(this->pelec)->get_DM(); + if (iter == 1) { this->p_chgmix->mix_reset(); // init mixing @@ -565,8 +568,6 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const if (PARAM.inp.mixing_dmr) // for mixing_dmr { // allocate memory for dmr_mdata - const elecstate::DensityMatrix* dm - = dynamic_cast*>(this->pelec)->get_DM(); int nnr_tmp = dm->get_DMR_pointer(1)->get_nnr(); this->p_chgmix->allocate_mixing_dmr(nnr_tmp); } @@ -652,7 +653,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { this->exd->exx_eachiterinit(istep, ucell, - *dynamic_cast*>(this->pelec)->get_DM(), + *dm, this->kv, iter); } @@ -660,7 +661,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { this->exc->exx_eachiterinit(istep, ucell, - *dynamic_cast*>(this->pelec)->get_DM(), + *dm, this->kv, iter); } @@ -671,7 +672,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { if (istep != 0 || iter != 1) { - GlobalC::dftu.set_dmr(dynamic_cast*>(this->pelec)->get_DM()); + GlobalC::dftu.set_dmr(dm); } // Calculate U and J if Yukawa potential is used GlobalC::dftu.cal_slater_UJ(ucell, this->chr.rho, this->pw_rho->nrxx); @@ -702,7 +703,6 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const // save density matrix DMR for mixing if (PARAM.inp.mixing_restart > 0 && PARAM.inp.mixing_dmr && this->p_chgmix->mixing_restart_count > 0) { - elecstate::DensityMatrix* dm = dynamic_cast*>(this->pelec)->get_DM(); dm->save_DMR(); } } diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 19205647d2..97d4f61533 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -187,7 +187,7 @@ void ESolver_KS_PW::iter_init(UnitCell& ucell, const int istep, const ESolver_KS::iter_init(ucell, istep, iter); // perform charge mixing - module_charge::chgmixing(iter, this->p_chgmix, PARAM.inp); + module_charge::chgmixing_ks_pw(iter, this->p_chgmix, PARAM.inp); // mohan move harris functional to here, 2012-06-05 // use 'rho(in)' and 'v_h and v_xc'(in) diff --git a/source/source_estate/module_charge/chgmixing.cpp b/source/source_estate/module_charge/chgmixing.cpp index 0be2b8e72c..806c7baf25 100644 --- a/source/source_estate/module_charge/chgmixing.cpp +++ b/source/source_estate/module_charge/chgmixing.cpp @@ -2,11 +2,11 @@ #include "source_lcao/module_dftu/dftu.h" #include "source_lcao/module_deltaspin/spin_constrain.h" -void module_charge::chgmixing(const int iter, // scf iteration number +void module_charge::chgmixing_ks_pw(const int iter, // scf iteration number Charge_Mixing* p_chgmix, // charge mixing class const Input_para& inp) // input parameters { - ModuleBase::TITLE("module_charge", "setup_chgmixing"); + ModuleBase::TITLE("module_charge", "chgmixing_ks_pw"); if (iter == 1) { @@ -55,3 +55,61 @@ void module_charge::chgmixing(const int iter, // scf iteration number return; } + +/* +void module_charge::chgmixing_ks_lcao(const int iter, // scf iteration number + Charge_Mixing* p_chgmix, // charge mixing class + const Input_para& inp) // input parameters +{ + ModuleBase::TITLE("module_charge", "chgmixing_ks_lcao"); + + if (iter == 1) + { + p_chgmix->mix_reset(); // init mixing + p_chgmix->mixing_restart_step = inp.scf_nmax + 1; + p_chgmix->mixing_restart_count = 0; + // this output will be removed once the feeature is stable + if (GlobalC::dftu.uramping > 0.01) + { + std::cout << " U-Ramping! Current U = "; + for (int i = 0; i < GlobalC::dftu.U0.size(); i++) + { + std::cout << GlobalC::dftu.U[i] * ModuleBase::Ry_to_eV << " "; + } + std::cout << " eV " << std::endl; + } + } + + // for mixing restart + if (iter == p_chgmix->mixing_restart_step && inp.mixing_restart > 0.0) + { + p_chgmix->init_mixing(); + p_chgmix->mixing_restart_count++; + if (inp.dft_plus_u) + { + GlobalC::dftu.uramping_update(); // update U by uramping if uramping > 0.01 + if (GlobalC::dftu.uramping > 0.01) + { + std::cout << " U-Ramping! Current U = "; + for (int i = 0; i < GlobalC::dftu.U0.size(); i++) + { + std::cout << GlobalC::dftu.U[i] * ModuleBase::Ry_to_eV << " "; + } + std::cout << " eV " << std::endl; + } + if (GlobalC::dftu.uramping > 0.01 && !GlobalC::dftu.u_converged()) + { + p_chgmix->mixing_restart_step = inp.scf_nmax + 1; + } + } + if (inp.mixing_dmr) // for mixing_dmr + { + // allocate memory for dmr_mdata + const elecstate::DensityMatrix* dm + = dynamic_cast*>(this->pelec)->get_DM(); + int nnr_tmp = dm->get_DMR_pointer(1)->get_nnr(); + p_chgmix->allocate_mixing_dmr(nnr_tmp); + } + } +} +*/ diff --git a/source/source_estate/module_charge/chgmixing.h b/source/source_estate/module_charge/chgmixing.h index f1214d187e..9ef453d643 100644 --- a/source/source_estate/module_charge/chgmixing.h +++ b/source/source_estate/module_charge/chgmixing.h @@ -7,10 +7,12 @@ namespace module_charge { -void chgmixing(const int iter, +void chgmixing_ks_pw(const int iter, Charge_Mixing* p_chgmix, const Input_para& inp); // input parameters +//void chgmixing_ks_lcao(); + } From b5b66d6716be702ad5df26d4172a484d0b560408 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 11:20:21 +0800 Subject: [PATCH 46/60] reduce the charge mixing code in esolver_ks_lcao --- source/source_esolver/esolver_ks_lcao.cpp | 51 ++----------------- .../source_estate/module_charge/chgmixing.cpp | 8 +-- .../source_estate/module_charge/chgmixing.h | 5 +- 3 files changed, 9 insertions(+), 55 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index a03e6e56f6..73d0a830ce 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -63,10 +63,10 @@ // test RDMFT #include "source_lcao/module_rdmft/rdmft.h" - #include "source_lcao/module_gint/temp_gint/gint_info.h" -#include +#include "source_estate/module_charge/chgmixing.h" // use charge mixing, mohan add 20251006 + namespace ModuleESolver { @@ -526,52 +526,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const elecstate::DensityMatrix* dm = dynamic_cast*>(this->pelec)->get_DM(); - if (iter == 1) - { - this->p_chgmix->mix_reset(); // init mixing - this->p_chgmix->mixing_restart_step = PARAM.inp.scf_nmax + 1; - this->p_chgmix->mixing_restart_count = 0; - // this output will be removed once the feeature is stable - if (GlobalC::dftu.uramping > 0.01) - { - std::cout << " U-Ramping! Current U = "; - for (int i = 0; i < GlobalC::dftu.U0.size(); i++) - { - std::cout << GlobalC::dftu.U[i] * ModuleBase::Ry_to_eV << " "; - } - std::cout << " eV " << std::endl; - } - } - - // for mixing restart - if (iter == this->p_chgmix->mixing_restart_step && PARAM.inp.mixing_restart > 0.0) - { - this->p_chgmix->init_mixing(); - this->p_chgmix->mixing_restart_count++; - if (PARAM.inp.dft_plus_u) - { - GlobalC::dftu.uramping_update(); // update U by uramping if uramping > 0.01 - if (GlobalC::dftu.uramping > 0.01) - { - std::cout << " U-Ramping! Current U = "; - for (int i = 0; i < GlobalC::dftu.U0.size(); i++) - { - std::cout << GlobalC::dftu.U[i] * ModuleBase::Ry_to_eV << " "; - } - std::cout << " eV " << std::endl; - } - if (GlobalC::dftu.uramping > 0.01 && !GlobalC::dftu.u_converged()) - { - this->p_chgmix->mixing_restart_step = PARAM.inp.scf_nmax + 1; - } - } - if (PARAM.inp.mixing_dmr) // for mixing_dmr - { - // allocate memory for dmr_mdata - int nnr_tmp = dm->get_DMR_pointer(1)->get_nnr(); - this->p_chgmix->allocate_mixing_dmr(nnr_tmp); - } - } + module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp); // mohan update 2012-06-05 this->pelec->f_en.deband_harris = this->pelec->cal_delta_eband(ucell); diff --git a/source/source_estate/module_charge/chgmixing.cpp b/source/source_estate/module_charge/chgmixing.cpp index 806c7baf25..87496ced87 100644 --- a/source/source_estate/module_charge/chgmixing.cpp +++ b/source/source_estate/module_charge/chgmixing.cpp @@ -56,9 +56,9 @@ void module_charge::chgmixing_ks_pw(const int iter, // scf iteration number return; } -/* void module_charge::chgmixing_ks_lcao(const int iter, // scf iteration number Charge_Mixing* p_chgmix, // charge mixing class + const int nnr, // dimension of density matrix const Input_para& inp) // input parameters { ModuleBase::TITLE("module_charge", "chgmixing_ks_lcao"); @@ -105,11 +105,7 @@ void module_charge::chgmixing_ks_lcao(const int iter, // scf iteration number if (inp.mixing_dmr) // for mixing_dmr { // allocate memory for dmr_mdata - const elecstate::DensityMatrix* dm - = dynamic_cast*>(this->pelec)->get_DM(); - int nnr_tmp = dm->get_DMR_pointer(1)->get_nnr(); - p_chgmix->allocate_mixing_dmr(nnr_tmp); + p_chgmix->allocate_mixing_dmr(nnr); } } } -*/ diff --git a/source/source_estate/module_charge/chgmixing.h b/source/source_estate/module_charge/chgmixing.h index 9ef453d643..2e962ed8e5 100644 --- a/source/source_estate/module_charge/chgmixing.h +++ b/source/source_estate/module_charge/chgmixing.h @@ -11,7 +11,10 @@ void chgmixing_ks_pw(const int iter, Charge_Mixing* p_chgmix, const Input_para& inp); // input parameters -//void chgmixing_ks_lcao(); +void chgmixing_ks_lcao(const int iter, // scf iteration number + Charge_Mixing* p_chgmix, // charge mixing class + const int nnr, // dimension of density matrix + const Input_para& inp); // input parameters } From e3bff8f917edb30bee8eb563d0f05b8e2b69ceae Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 11:40:35 +0800 Subject: [PATCH 47/60] reduce the lines of esolver_ks_lcao --- source/source_esolver/esolver_ks_lcao.cpp | 188 ++++++---------------- 1 file changed, 48 insertions(+), 140 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 73d0a830ce..76f8f6bf88 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -112,32 +112,19 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa ESolver_KS::before_all_runners(ucell, inp); // 2) init ElecState - // autoset nbands in ElecState before basis_init (for Psi 2d division) + // autoset nbands in ElecState before init_basis (for Psi 2d division) if (this->pelec == nullptr) { // TK stands for double and std::complex? - this->pelec = new elecstate::ElecStateLCAO(&(this->chr), // use which parameter? - &(this->kv), - this->kv.get_nks(), - &(this->GG), - &(this->GK), - this->pw_rho, - this->pw_big); - } - - // 3) init LCAO basis - // reading the localized orbitals/projectors - // construct the interpolation tables. - LCAO_domain::init_basis_lcao(this->pv, - inp.onsite_radius, - inp.lcao_ecut, - inp.lcao_dk, - inp.lcao_dr, - inp.lcao_rmax, - ucell, - two_center_bundle_, - orb_); + this->pelec = new elecstate::ElecStateLCAO(&(this->chr), &(this->kv), + this->kv.get_nks(), &(this->GG), &(this->GK), this->pw_rho, this->pw_big); + } + + // 3) read the LCAO orbitals/projectors and construct the interpolation tables. + LCAO_domain::init_basis_lcao(this->pv, inp.onsite_radius, inp.lcao_ecut, + inp.lcao_dk, inp.lcao_dr, inp.lcao_rmax, ucell, two_center_bundle_, orb_); + // 4) setup EXX calculations if (PARAM.inp.calculation == "gen_opt_abfs") { #ifdef __EXX @@ -149,7 +136,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa return; } - // 4) initialize electronic wave function psi + // 5) initialize electronic wave function psi if (this->psi == nullptr) { int nsk = 0; @@ -177,7 +164,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa this->psi = new psi::Psi(nsk, ncol, this->pv.nrow, this->kv.ngk, true); } - // 5) read psi from file + // 6) read psi from file if (inp.init_wfc == "file" && inp.esolver_type != "tddft") { if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, @@ -192,12 +179,11 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } } - // 6) initialize the density matrix - // DensityMatrix is allocated here, DMK is also initialized here - // DMR is not initialized here, it will be constructed in each before_scf + // 7) initialize the density matrix + // DMK are allocated here, but DMR is constructed in before_scf() dynamic_cast*>(this->pelec)->init_DM(&this->kv, &(this->pv), inp.nspin); - // 7) initialize exact exchange calculations + // 8) initialize exact exchange calculations #ifdef __EXX if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" || inp.calculation == "md") @@ -224,35 +210,30 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } #endif - // 8) initialize DFT+U + // 9) initialize DFT+U if (inp.dft_plus_u) { auto* dftu = ModuleDFTU::DFTU::get_instance(); dftu->init(ucell, &this->pv, this->kv.get_nks(), &orb_); } - // 9) initialize local pseudopotentials + // 10) initialize local pseudopotentials this->locpp.init_vloc(ucell, this->pw_rho); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); - // 10) inititlize the charge density + // 11) inititlize the charge density this->chr.allocate(inp.nspin); this->pelec->omega = ucell.omega; - // 11) initialize the potential + // 12) initialize the potential if (this->pelec->pot == nullptr) { - this->pelec->pot = new elecstate::Potential(this->pw_rhod, - this->pw_rho, - &ucell, - &(this->locpp.vloc), - &(this->sf), - &(this->solvent), - &(this->pelec->f_en.etxc), - &(this->pelec->f_en.vtxc)); + this->pelec->pot = new elecstate::Potential(this->pw_rhod, this->pw_rho, + &ucell, &(this->locpp.vloc), &(this->sf), &(this->solvent), + &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc)); } - // 12) initialize deepks + // 13) initialize deepks #ifdef __MLALGO LCAO_domain::DeePKS_init(ucell, pv, this->kv.get_nks(), orb_, this->ld, GlobalV::ofs_running); if (inp.deepks_scf) @@ -260,31 +241,21 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa // load the DeePKS model from deep neural network DeePKS_domain::load_model(inp.deepks_model, ld.model_deepks); // read pdm from file for NSCF or SCF-restart, do it only once in whole calculation - DeePKS_domain::read_pdm((inp.init_chg == "file"), - inp.deepks_equiv, - ld.init_pdm, - ucell.nat, - orb_.Alpha[0].getTotal_nchi() * ucell.nat, - ld.lmaxd, - ld.inl2l, - *orb_.Alpha, - ld.pdm); + DeePKS_domain::read_pdm((inp.init_chg == "file"), inp.deepks_equiv, + ld.init_pdm, ucell.nat, orb_.Alpha[0].getTotal_nchi() * ucell.nat, + ld.lmaxd, ld.inl2l, *orb_.Alpha, ld.pdm); } #endif - // 13) set occupations + // 14) set occupations // tddft does not need to set occupations in the first scf if (inp.ocp && inp.esolver_type != "tddft") { - elecstate::fixed_weights(inp.ocp_kb, - inp.nbands, - inp.nelec, - this->pelec->klist, - this->pelec->wg, - this->pelec->skip_weights); + elecstate::fixed_weights(inp.ocp_kb, inp.nbands, inp.nelec, + this->pelec->klist, this->pelec->wg, this->pelec->skip_weights); } - // 14) if kpar is not divisible by nks, print a warning + // 15) if kpar is not divisible by nks, print a warning if (PARAM.globalv.kpar_lcao > 1) { if (this->kv.get_nks() % PARAM.globalv.kpar_lcao != 0) @@ -305,20 +276,12 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } } - // 15) initialize rdmft, added by jghan + // 16) initialize rdmft, added by jghan if (inp.rdmft == true) { - rdmft_solver.init(this->GG, - this->GK, - this->pv, - ucell, - this->gd, - this->kv, - *(this->pelec), - this->orb_, - two_center_bundle_, - inp.dft_functional, - inp.rdmft_power_alpha); + rdmft_solver.init(this->GG, this->GK, this->pv, ucell, + this->gd, this->kv, *(this->pelec), this->orb_, + two_center_bundle_, inp.dft_functional, inp.rdmft_power_alpha); } ModuleBase::timer::tick("ESolver_KS_LCAO", "before_all_runners"); @@ -568,28 +531,6 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const this->pelec->psiToRho(*this->psi); this->pelec->skip_weights = false; - // calculate the local potential(rho) again. - // the grid integration will do in later grid integration. - - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // a puzzle remains here. - // if I don't renew potential, - // The scf_thr is very small. - // OneElectron, Hartree and - // Exc energy are all correct - // except the band energy. - // - // solved by mohan 2010-09-10 - // there are there rho here: - // rho1: formed by read in orbitals. - // rho2: atomic rho, used to construct H - // rho3: generated by after diagonalize - // here converged because rho3 and rho1 - // are very close. - // so be careful here, make sure - // rho1 and rho2 are the same rho. - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - elecstate::cal_ux(ucell); //! update the potentials by using new electron charge density @@ -606,19 +547,11 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { if (GlobalC::exx_info.info_ri.real_number) { - this->exd->exx_eachiterinit(istep, - ucell, - *dm, - this->kv, - iter); + this->exd->exx_eachiterinit(istep, ucell, *dm, this->kv, iter); } else { - this->exc->exx_eachiterinit(istep, - ucell, - *dm, - this->kv, - iter); + this->exc->exx_eachiterinit(istep, ucell, *dm, this->kv, iter); } } #endif @@ -747,8 +680,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { ModuleBase::TITLE("ESolver_KS_LCAO", "iter_finish"); - // 1) calculate the local occupation number matrix and energy correction - // in DFT+U + // 1) calculate the local occupation number matrix and energy correction in DFT+U if (PARAM.inp.dft_plus_u) { // only old DFT+U method should calculated energy correction in esolver, @@ -801,7 +733,8 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { if (PARAM.inp.mixing_restart > 0 && this->p_chgmix->mixing_restart_count > 0 && PARAM.inp.mixing_dmr) { - elecstate::DensityMatrix* dm = dynamic_cast*>(this->pelec)->get_DM(); + elecstate::DensityMatrix* dm = + dynamic_cast*>(this->pelec)->get_DM(); this->p_chgmix->mix_dmr(dm); } } @@ -821,24 +754,11 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { if (GlobalC::exx_info.info_global.cal_exx) { - GlobalC::exx_info.info_ri.real_number ? this->exd->exx_iter_finish(this->kv, - ucell, - *this->p_hamilt, - *this->pelec, - *this->p_chgmix, - this->scf_ene_thr, - iter, - istep, - conv_esolver) - : this->exc->exx_iter_finish(this->kv, - ucell, - *this->p_hamilt, - *this->pelec, - *this->p_chgmix, - this->scf_ene_thr, - iter, - istep, - conv_esolver); + GlobalC::exx_info.info_ri.real_number ? + this->exd->exx_iter_finish(this->kv, ucell, *this->p_hamilt, *this->pelec, + *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver) : + this->exc->exx_iter_finish(this->kv, ucell, *this->p_hamilt, *this->pelec, + *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver); } } #endif @@ -859,23 +779,11 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& std::shared_ptr> ld_shared_ptr(&ld, [](LCAO_Deepks*) {}); LCAO_Deepks_Interface deepks_interface(ld_shared_ptr); - deepks_interface.out_deepks_labels(this->pelec->f_en.etot, - this->kv.get_nks(), - ucell.nat, - PARAM.globalv.nlocal, - this->pelec->ekb, - this->kv.kvec_d, - ucell, - orb_, - this->gd, - &(this->pv), - *(this->psi), - dynamic_cast*>(this->pelec)->get_DM(), - p_ham_deepks, - iter, - conv_esolver, - GlobalV::MY_RANK, - GlobalV::ofs_running); + deepks_interface.out_deepks_labels(this->pelec->f_en.etot, this->kv.get_nks(), + ucell.nat, PARAM.globalv.nlocal, this->pelec->ekb, this->kv.kvec_d, + ucell, orb_, this->gd, &(this->pv), *(this->psi), + dynamic_cast*>(this->pelec)->get_DM(), + p_ham_deepks, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); } } #endif From 3100997437061477d3f406136d061e48a190240a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 12:02:34 +0800 Subject: [PATCH 48/60] update dm vector --- source/source_esolver/esolver_ks_lcao.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 76f8f6bf88..1aa7e10161 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -680,6 +680,9 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { ModuleBase::TITLE("ESolver_KS_LCAO", "iter_finish"); + const std::vector>& dm_vec + = dynamic_cast*>(this->pelec)->get_DM()->get_DMK_vector(); + // 1) calculate the local occupation number matrix and energy correction in DFT+U if (PARAM.inp.dft_plus_u) { @@ -689,11 +692,9 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { if (GlobalC::dftu.omc != 2) { - const std::vector>& tmp_dm - = dynamic_cast*>(this->pelec)->get_DM()->get_DMK_vector(); ModuleDFTU::dftu_cal_occup_m(iter, ucell, - tmp_dm, + dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), this->p_hamilt); @@ -707,11 +708,8 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& #ifdef __MLALGO if (PARAM.inp.deepks_scf) { - const std::vector>& dm - = dynamic_cast*>(this->pelec)->get_DM()->get_DMK_vector(); - - ld.dpks_cal_e_delta_band(dm, this->kv.get_nks()); - DeePKS_domain::update_dmr(this->kv.kvec_d, dm, ucell, orb_, this->pv, this->gd, ld.dm_r); + ld.dpks_cal_e_delta_band(dm_vec, this->kv.get_nks()); + DeePKS_domain::update_dmr(this->kv.kvec_d, dm_vec, ucell, orb_, this->pv, this->gd, ld.dm_r); this->pelec->f_en.edeepks_scf = ld.E_delta - ld.e_delta_band; this->pelec->f_en.edeepks_delta = ld.E_delta; } From ca5bf7844debc13f04b31c2bf220ae8ab4ce2385 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 12:11:33 +0800 Subject: [PATCH 49/60] change ctrl_output_lcao to ctrl_scf_lcao --- source/Makefile.Objects | 2 +- source/source_esolver/lcao_after_scf.cpp | 11 ++++++++--- source/source_io/CMakeLists.txt | 2 +- .../{ctrl_output_lcao.cpp => ctrl_scf_lcao.cpp} | 16 ++++++++-------- .../{ctrl_output_lcao.h => ctrl_scf_lcao.h} | 6 +++--- 5 files changed, 21 insertions(+), 16 deletions(-) rename source/source_io/{ctrl_output_lcao.cpp => ctrl_scf_lcao.cpp} (96%) rename source/source_io/{ctrl_output_lcao.h => ctrl_scf_lcao.h} (94%) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 7510075da5..edde8c32d7 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -581,7 +581,7 @@ OBJS_IO=input_conv.o\ write_libxc_r.o\ output_log.o\ output_mat_sparse.o\ - ctrl_output_lcao.o\ + ctrl_scf_lcao.o\ ctrl_output_fp.o\ ctrl_output_pw.o\ para_json.o\ diff --git a/source/source_esolver/lcao_after_scf.cpp b/source/source_esolver/lcao_after_scf.cpp index 30a4cd6997..b1f5c4e6b8 100644 --- a/source/source_esolver/lcao_after_scf.cpp +++ b/source/source_esolver/lcao_after_scf.cpp @@ -1,5 +1,5 @@ #include "esolver_ks_lcao.h" -#include "source_io/ctrl_output_lcao.h" +#include "source_io/ctrl_scf_lcao.h" namespace ModuleESolver { @@ -23,7 +23,12 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const if(!estate) { - ModuleBase::WARNING_QUIT("ModuleIO::ctrl_output_lcao","pelec does not exist"); + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::after_scf","pelec does not exist"); + } + + if(!hamilt_lcao) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::after_scf","p_hamilt does not exist"); } @@ -49,7 +54,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const if (out_flag) { - ModuleIO::ctrl_output_lcao(ucell, + ModuleIO::ctrl_scf_lcao(ucell, PARAM.inp, this->kv, estate, diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index e511ffb35c..f3b6816dd3 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -83,7 +83,7 @@ if(ENABLE_LCAO) single_R_io.cpp cal_r_overlap_R.cpp output_mat_sparse.cpp - ctrl_output_lcao.cpp + ctrl_scf_lcao.cpp ) endif() diff --git a/source/source_io/ctrl_output_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp similarity index 96% rename from source/source_io/ctrl_output_lcao.cpp rename to source/source_io/ctrl_scf_lcao.cpp index 5722265307..c981e631e3 100644 --- a/source/source_io/ctrl_output_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -1,7 +1,7 @@ #include #include "source_estate/elecstate_lcao.h" // use elecstate::ElecState -#include "source_io/ctrl_output_lcao.h" // use ctrl_output_lcao() +#include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() #include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO #include "source_hamilt/hamilt.h" // use Hamilt @@ -34,7 +34,7 @@ namespace ModuleIO { template -void ctrl_output_lcao(UnitCell& ucell, +void ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, @@ -60,8 +60,8 @@ void ctrl_output_lcao(UnitCell& ucell, #endif const int istep) { - ModuleBase::TITLE("ModuleIO", "ctrl_output_lcao"); - ModuleBase::timer::tick("ModuleIO", "ctrl_output_lcao"); + ModuleBase::TITLE("ModuleIO", "ctrl_scf_lcao"); + ModuleBase::timer::tick("ModuleIO", "ctrl_scf_lcao"); const bool out_app_flag = inp.out_app_flag; const bool gamma_only = PARAM.globalv.gamma_only_local; @@ -434,14 +434,14 @@ void ctrl_output_lcao(UnitCell& ucell, } - ModuleBase::timer::tick("ModuleIO", "ctrl_output_lcao"); + ModuleBase::timer::tick("ModuleIO", "ctrl_scf_lcao"); } } // End ModuleIO // For gamma only -template void ModuleIO::ctrl_output_lcao(UnitCell& ucell, +template void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, @@ -468,7 +468,7 @@ template void ModuleIO::ctrl_output_lcao(UnitCell& ucell, const int istep); // For multiple k-points -template void ModuleIO::ctrl_output_lcao, double>(UnitCell& ucell, +template void ModuleIO::ctrl_scf_lcao, double>(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO>* pelec, @@ -494,7 +494,7 @@ template void ModuleIO::ctrl_output_lcao, double>(UnitCell& #endif const int istep); -template void ModuleIO::ctrl_output_lcao, std::complex>(UnitCell& ucell, +template void ModuleIO::ctrl_scf_lcao, std::complex>(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO>* pelec, diff --git a/source/source_io/ctrl_output_lcao.h b/source/source_io/ctrl_scf_lcao.h similarity index 94% rename from source/source_io/ctrl_output_lcao.h rename to source/source_io/ctrl_scf_lcao.h index a223e5381d..8e3dcf4d29 100644 --- a/source/source_io/ctrl_output_lcao.h +++ b/source/source_io/ctrl_scf_lcao.h @@ -1,5 +1,5 @@ -#ifndef CTRL_OUTPUT_LCAO_H -#define CTRL_OUTPUT_LCAO_H +#ifndef CTRL_SCF_LCAO_H +#define CTRL_SCF_LCAO_H #include @@ -21,7 +21,7 @@ namespace ModuleIO { // in principle, we need to add const for all of the variables, mohan note 2025-06-05 template - void ctrl_output_lcao(UnitCell& ucell, + void ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, From f37dd62d5ddd61157658f2bc4a07cc16d9e93c0b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 13:14:08 +0800 Subject: [PATCH 50/60] add ctrl_runner_lcao, but cannot run --- source/source_esolver/esolver_ks_lcao.cpp | 119 ++++-------------- source/source_esolver/lcao_after_scf.cpp | 22 +--- source/source_io/ctrl_runner_lcao.cpp | 139 ++++++++++++++++++++++ source/source_io/ctrl_runner_lcao.h | 45 +++++++ 4 files changed, 215 insertions(+), 110 deletions(-) create mode 100644 source/source_io/ctrl_runner_lcao.cpp create mode 100644 source/source_io/ctrl_runner_lcao.h diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 1aa7e10161..95c0f791bd 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -377,103 +377,34 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; - // 1) write projected band structure - if (PARAM.inp.out_proj_band) - { - ModuleIO::write_proj_band_lcao(this->psi, this->pv, this->pelec, this->kv, ucell, this->p_hamilt); - } - - // 2) out ldos - if (PARAM.inp.out_ldos[0]) - { - ModuleIO::Cal_ldos::cal_ldos_lcao(reinterpret_cast*>(this->pelec), - this->psi[0], - this->Pgrid, - ucell); - } - - // 3) print out exchange-correlation potential - if (PARAM.inp.out_mat_xc) - { - ModuleIO::write_Vxc(PARAM.inp.nspin, - PARAM.globalv.nlocal, - GlobalV::DRANK, - &this->pv, - *this->psi, - ucell, - this->sf, - this->solvent, - *this->pw_rho, - *this->pw_rhod, - this->locpp.vloc, - this->chr, - this->GG, - this->GK, - this->kv, - orb_.cutoffs(), - this->pelec->wg, - this->gd -#ifdef __EXX - , - this->exd ? &this->exd->get_Hexxs() : nullptr, - this->exc ? &this->exc->get_Hexxs() : nullptr -#endif - ); - } - - if (PARAM.inp.out_mat_xc2) - { - ModuleIO::write_Vxc_R(PARAM.inp.nspin, - &this->pv, - ucell, - this->sf, - this->solvent, - *this->pw_rho, - *this->pw_rhod, - this->locpp.vloc, - this->chr, - this->GG, - this->GK, - this->kv, - orb_.cutoffs(), - this->gd -#ifdef __EXX - , - this->exd ? &this->exd->get_Hexxs() : nullptr, - this->exc ? &this->exc->get_Hexxs() : nullptr + auto* estate = dynamic_cast*>(this->pelec); + auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); + + if(!estate) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::after_all_runners","pelec does not exist"); + } + + if(!hamilt_lcao) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::after_all_runners","p_hamilt does not exist"); + } + + ModuleIO::ctrl_runner_lcao(ucell, + PARAM.inp, this->kv, estate, this->pv, + this->gd, this->psi, hamilt_lcao, + this->two_center_bundle_, this->GK, + this->orb_, this->pw_wfc, this->pw_rho, + this->GridT, this->pw_big, this->sf, + this->rdmft_solver, +#ifdef __MLALGO + this->ld, #endif - ); - } - - // write eband terms - if (PARAM.inp.out_eband_terms) - { - ModuleIO::write_eband_terms(PARAM.inp.nspin, - PARAM.globalv.nlocal, - GlobalV::DRANK, - &this->pv, - *this->psi, - ucell, - this->sf, - this->solvent, - *this->pw_rho, - *this->pw_rhod, - this->locpp.vloc, - this->chr, - this->GG, - this->GK, - this->kv, - this->pelec->wg, - this->gd, - orb_.cutoffs(), - this->two_center_bundle_ #ifdef __EXX - , - this->exd ? &this->exd->get_Hexxs() : nullptr, - this->exc ? &this->exc->get_Hexxs() : nullptr + *this->exd, + *this->exc, #endif - ); - } + istep); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); } diff --git a/source/source_esolver/lcao_after_scf.cpp b/source/source_esolver/lcao_after_scf.cpp index b1f5c4e6b8..bf819f8a75 100644 --- a/source/source_esolver/lcao_after_scf.cpp +++ b/source/source_esolver/lcao_after_scf.cpp @@ -55,22 +55,12 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const if (out_flag) { ModuleIO::ctrl_scf_lcao(ucell, - PARAM.inp, - this->kv, - estate, - this->pv, - this->gd, - this->psi, - hamilt_lcao, - this->two_center_bundle_, - this->GK, - this->orb_, - this->pw_wfc, - this->pw_rho, - this->GridT, - this->pw_big, - this->sf, - this->rdmft_solver, + PARAM.inp, this->kv, estate, this->pv, + this->gd, this->psi, hamilt_lcao, + this->two_center_bundle_, this->GK, + this->orb_, this->pw_wfc, this->pw_rho, + this->GridT, this->pw_big, this->sf, + this->rdmft_solver, #ifdef __MLALGO this->ld, #endif diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp new file mode 100644 index 0000000000..d5f0de6a26 --- /dev/null +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -0,0 +1,139 @@ +#include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() + +#include "source_estate/elecstate_lcao.h" // use elecstate::ElecState +#include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO + +// functions +#ifdef __EXX +#include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes +#include "source_lcao/module_ri/RPA_LRI.h" // use RPA code +#endif + +namespace ModuleIO +{ + +template +void ctrl_runner_lcao(UnitCell& ucell, // unitcell + Input_para &inp, // input + K_Vectors &kv, // k-point + elecstate::ElecStateLCAO* pelec,// electronic info + Parallel_Orbitals &pv, // orbital info + Parallel_Grid &pgrid, // grid info + Grid_Driver &gd, // search for adjacent atoms + psi::Psi* psi, // wave function + Charge &chr, // charge density + hamilt::HamiltLCAO* p_hamilt, // hamiltonian + TwoCenterBundle &two_center_bundle, // use two-center integration + Gint_Gamma &gg, // gint for Gamma-only + Gint_k &gk, // gint for multi k-points + LCAO_Orbitals &orb, // LCAO orbitals + ModulePW::PW_Basis* pw_rho, // charge density + ModulePW::PW_Basis* pw_rhod, // dense charge density + Structure_Factor &sf, // structure factor + ModuleBase::matrix &vloc, // local pseudopotential +#ifdef __EXX + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, +#endif + surchem &solvent) // solvent model +{ + ModuleBase::TITLE("ModuleIO", "ctrl_runner_lcao"); + ModuleBase::timer::tick("ModuleIO", "ctrl_runner_lcao"); + + // 1) write projected band structure + if (inp.out_proj_band) + { + ModuleIO::write_proj_band_lcao(psi, pv, pelec, kv, ucell, p_hamilt); + } + + // 2) out ldos + if (inp.out_ldos[0]) + { + ModuleIO::Cal_ldos::cal_ldos_lcao(estate, psi[0], pgrid, ucell); + } + + // 3) print out exchange-correlation potential + if (inp.out_mat_xc) + { + ModuleIO::write_Vxc(inp.nspin, + PARAM.globalv.nlocal, + GlobalV::DRANK, + &pv, + *psi, + ucell, + sf, + solvent, + *pw_rho, + *pw_rhod, + vloc, + chr, + gg, + gk, + kv, + orb.cutoffs(), + pelec->wg, + gd +#ifdef __EXX + , + exd ? &exd->get_Hexxs() : nullptr, + exc ? &exc->get_Hexxs() : nullptr +#endif + ); + } + + if (inp.out_mat_xc2) + { + ModuleIO::write_Vxc_R(inp.nspin, + &pv, + ucell, + sf, + solvent, + *pw_rho, + *pw_rhod, + vloc, + chr, + gg, + gk, + kv, + orb.cutoffs(), + gd +#ifdef __EXX + , + exd ? &exd->get_Hexxs() : nullptr, + exc ? &exc->get_Hexxs() : nullptr +#endif + ); + } + + + // write eband terms + if (inp.out_eband_terms) + { + ModuleIO::write_eband_terms(inp.nspin, + PARAM.globalv.nlocal, + GlobalV::DRANK, + &pv, + *psi, + ucell, + sf, + solvent, + *pw_rho, + *pw_rhod, + vloc, + chr, + gg, + gk, + kv, + pelec->wg, + gd, + orb_.cutoffs(), + two_center_bundle_ +#ifdef __EXX + , + exd ? &exd->get_Hexxs() : nullptr, + exc ? &exc->get_Hexxs() : nullptr +#endif + ); + } + +} diff --git a/source/source_io/ctrl_runner_lcao.h b/source/source_io/ctrl_runner_lcao.h new file mode 100644 index 0000000000..5e819f9105 --- /dev/null +++ b/source/source_io/ctrl_runner_lcao.h @@ -0,0 +1,45 @@ +#ifndef CTRL_RUNNER_LCAO_H +#define CTRL_RUNNER_LCAO_H + +#include "source_cell/unitcell.h" // use UnitCell +#include "source_cell/klist.h" // use K_Vectors +#include "source_estate/elecstate_lcao.h" // use elecstate::ElecStateLCAO +#include "source_psi/psi.h" // use Psi +#include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO +#include "source_basis/module_nao/two_center_bundle.h" // use TwoCenterBundle +#include "source_lcao/module_gint/gint_k.h" // use Gint_k +#ifdef __EXX +#include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes +#endif + +namespace ModuleIO +{ + +template +void ctrl_runner_lcao(UnitCell& ucell, // unitcell + Input_para &inp, // input + K_Vectors &kv, // k-point + elecstate::ElecStateLCAO* pelec,// electronic info + Parallel_Orbitals &pv, // orbital info + Parallel_Grid &pgrid, // grid info + Grid_Driver &gd, // search for adjacent atoms + psi::Psi* psi, // wave function + Charge &chr, // charge density + hamilt::HamiltLCAO* p_hamilt, // hamiltonian + TwoCenterBundle &two_center_bundle, // use two-center integration + Gint_Gamma &gg, // gint for Gamma-only + Gint_k &gk, // gint for multi k-points + LCAO_Orbitals &orb, // LCAO orbitals + ModulePW::PW_Basis* pw_rho, // charge density + ModulePW::PW_Basis* pw_rhod, // dense charge density + Structure_Factor &sf, // structure factor + ModuleBase::matrix &vloc, // local pseudopotential +#ifdef __EXX + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, +#endif + surchem &solvent); // solvent model + +} + +#endif From bf5ccbc07ec8208fc41d1f501b5c08a7424a5bb3 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 13:37:09 +0800 Subject: [PATCH 51/60] add ctrl_runner_lcao successfully --- source/Makefile.Objects | 1 + source/source_esolver/esolver_ks_lcao.cpp | 22 ++---- source/source_io/CMakeLists.txt | 1 + source/source_io/ctrl_runner_lcao.cpp | 93 +++++++++++++++++++++-- source/source_io/ctrl_runner_lcao.h | 2 +- 5 files changed, 98 insertions(+), 21 deletions(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index edde8c32d7..53fdb3f2d6 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -582,6 +582,7 @@ OBJS_IO=input_conv.o\ output_log.o\ output_mat_sparse.o\ ctrl_scf_lcao.o\ + ctrl_runner_lcao.o\ ctrl_output_fp.o\ ctrl_output_pw.o\ para_json.o\ diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 95c0f791bd..ce8ff09c28 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -1,6 +1,5 @@ #include "esolver_ks_lcao.h" -#include "source_io/write_proj_band_lcao.h" // projcted band structure #include "source_base/formatter.h" #include "source_base/global_variable.h" @@ -11,7 +10,6 @@ #include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_lcao/module_dftu/dftu.h" #include "source_io/berryphase.h" -#include "source_io/cal_ldos.h" #include "source_io/cube_io.h" //#include "source_io/io_npz.h" #include "source_io/output_dmk.h" @@ -29,9 +27,6 @@ // be careful of hpp, there may be multiple definitions of functions, 20250302, mohan #include "source_lcao/hs_matrix_k.hpp" -#include "source_io/write_eband_terms.hpp" -#include "source_io/write_vxc.hpp" -#include "source_io/write_vxc_r.hpp" #include "source_base/global_function.h" #include "source_cell/module_neighbor/sltk_grid_driver.h" @@ -66,6 +61,7 @@ #include "source_lcao/module_gint/temp_gint/gint_info.h" #include "source_estate/module_charge/chgmixing.h" // use charge mixing, mohan add 20251006 +#include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() namespace ModuleESolver @@ -391,20 +387,16 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) } ModuleIO::ctrl_runner_lcao(ucell, - PARAM.inp, this->kv, estate, this->pv, - this->gd, this->psi, hamilt_lcao, - this->two_center_bundle_, this->GK, - this->orb_, this->pw_wfc, this->pw_rho, - this->GridT, this->pw_big, this->sf, - this->rdmft_solver, -#ifdef __MLALGO - this->ld, -#endif + PARAM.inp, this->kv, estate, this->pv, this->Pgrid, + this->gd, this->psi, this->chr, hamilt_lcao, + this->two_center_bundle_, this->GG, this->GK, + this->orb_, this->pw_rho, this->pw_rhod, + this->sf, this->locpp.vloc, #ifdef __EXX *this->exd, *this->exc, #endif - istep); + this->solvent); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); } diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index f3b6816dd3..20f85f4b24 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -84,6 +84,7 @@ if(ENABLE_LCAO) cal_r_overlap_R.cpp output_mat_sparse.cpp ctrl_scf_lcao.cpp + ctrl_runner_lcao.cpp ) endif() diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index d5f0de6a26..7a7981d03b 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -3,6 +3,12 @@ #include "source_estate/elecstate_lcao.h" // use elecstate::ElecState #include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO +#include "source_io/write_proj_band_lcao.h" // projcted band structure +#include "source_io/cal_ldos.h" // cal LDOS +#include "source_io/write_eband_terms.hpp" +#include "source_io/write_vxc.hpp" +#include "source_io/write_vxc_r.hpp" + // functions #ifdef __EXX #include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes @@ -14,7 +20,7 @@ namespace ModuleIO template void ctrl_runner_lcao(UnitCell& ucell, // unitcell - Input_para &inp, // input + const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info Parallel_Orbitals &pv, // orbital info @@ -49,7 +55,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell // 2) out ldos if (inp.out_ldos[0]) { - ModuleIO::Cal_ldos::cal_ldos_lcao(estate, psi[0], pgrid, ucell); + ModuleIO::Cal_ldos::cal_ldos_lcao(pelec, psi[0], pgrid, ucell); } // 3) print out exchange-correlation potential @@ -126,14 +132,91 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell kv, pelec->wg, gd, - orb_.cutoffs(), - two_center_bundle_ + orb.cutoffs(), + two_center_bundle #ifdef __EXX , exd ? &exd->get_Hexxs() : nullptr, exc ? &exc->get_Hexxs() : nullptr #endif - ); + ); } } + + + + +template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, // unitcell + const Input_para &inp, // input + K_Vectors &kv, // k-point + elecstate::ElecStateLCAO* pelec,// electronic info + Parallel_Orbitals &pv, // orbital info + Parallel_Grid &pgrid, // grid info + Grid_Driver &gd, // search for adjacent atoms + psi::Psi* psi, // wave function + Charge &chr, // charge density + hamilt::HamiltLCAO* p_hamilt, // hamiltonian + TwoCenterBundle &two_center_bundle, // use two-center integration + Gint_Gamma &gg, // gint for Gamma-only + Gint_k &gk, // gint for multi k-points + LCAO_Orbitals &orb, // LCAO orbitals + ModulePW::PW_Basis* pw_rho, // charge density + ModulePW::PW_Basis* pw_rhod, // dense charge density + Structure_Factor &sf, // structure factor + ModuleBase::matrix &vloc, // local pseudopotential +#ifdef __EXX + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, +#endif + surchem &solvent); // solvent model + +template void ctrl_runner_lcao, double>(UnitCell& ucell, // unitcell + const Input_para &inp, // input + K_Vectors &kv, // k-point + elecstate::ElecStateLCAO>* pelec,// electronic info + Parallel_Orbitals &pv, // orbital info + Parallel_Grid &pgrid, // grid info + Grid_Driver &gd, // search for adjacent atoms + psi::Psi>* psi, // wave function + Charge &chr, // charge density + hamilt::HamiltLCAO, double>* p_hamilt, // hamiltonian + TwoCenterBundle &two_center_bundle, // use two-center integration + Gint_Gamma &gg, // gint for Gamma-only + Gint_k &gk, // gint for multi k-points + LCAO_Orbitals &orb, // LCAO orbitals + ModulePW::PW_Basis* pw_rho, // charge density + ModulePW::PW_Basis* pw_rhod, // dense charge density + Structure_Factor &sf, // structure factor + ModuleBase::matrix &vloc, // local pseudopotential +#ifdef __EXX + Exx_LRI_Interface, double>& exd, + Exx_LRI_Interface, std::complex>& exc, +#endif + surchem &solvent); // solvent model + +template void ctrl_runner_lcao, std::complex>(UnitCell& ucell, // unitcell + const Input_para &inp, // input + K_Vectors &kv, // k-point + elecstate::ElecStateLCAO>* pelec,// electronic info + Parallel_Orbitals &pv, // orbital info + Parallel_Grid &pgrid, // grid info + Grid_Driver &gd, // search for adjacent atoms + psi::Psi>* psi, // wave function + Charge &chr, // charge density + hamilt::HamiltLCAO, std::complex>* p_hamilt, // hamiltonian + TwoCenterBundle &two_center_bundle, // use two-center integration + Gint_Gamma &gg, // gint for Gamma-only + Gint_k &gk, // gint for multi k-points + LCAO_Orbitals &orb, // LCAO orbitals + ModulePW::PW_Basis* pw_rho, // charge density + ModulePW::PW_Basis* pw_rhod, // dense charge density + Structure_Factor &sf, // structure factor + ModuleBase::matrix &vloc, // local pseudopotential +#ifdef __EXX + Exx_LRI_Interface, double>& exd, + Exx_LRI_Interface, std::complex>& exc, +#endif + surchem &solvent); // solvent model + +} // end namespace diff --git a/source/source_io/ctrl_runner_lcao.h b/source/source_io/ctrl_runner_lcao.h index 5e819f9105..f2996277cd 100644 --- a/source/source_io/ctrl_runner_lcao.h +++ b/source/source_io/ctrl_runner_lcao.h @@ -17,7 +17,7 @@ namespace ModuleIO template void ctrl_runner_lcao(UnitCell& ucell, // unitcell - Input_para &inp, // input + const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info Parallel_Orbitals &pv, // orbital info From df610556c89c1faf066275dab83302e6face8fec Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 13:39:21 +0800 Subject: [PATCH 52/60] add ctrl_iter_lcao files --- source/source_io/ctrl_iter_lcao.cpp | 39 +++++++++++++++++++++++ source/source_io/ctrl_iter_lcao.h | 48 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 source/source_io/ctrl_iter_lcao.cpp create mode 100644 source/source_io/ctrl_iter_lcao.h diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp new file mode 100644 index 0000000000..0643926740 --- /dev/null +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -0,0 +1,39 @@ +#include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() + +namespace ModuleIO +{ + +template +void ctrl_iter_lcao(UnitCell& ucell, + const Input_para& inp, + K_Vectors& kv, + elecstate::ElecStateLCAO* pelec, + Parallel_Orbitals& pv, + Grid_Driver& gd, + psi::Psi* psi, + hamilt::HamiltLCAO* p_hamilt, + TwoCenterBundle &two_center_bundle, + Gint_k &gk, + LCAO_Orbitals &orb, + const ModulePW::PW_Basis_K* pw_wfc, // for berryphase + const ModulePW::PW_Basis* pw_rho, // for berryphase + Grid_Technique >, // for berryphase + const ModulePW::PW_Basis_Big* pw_big, // for Wannier90 + const Structure_Factor& sf, // for Wannier90 + rdmft::RDMFT &rdmft_solver, // for RDMFT +#ifdef __MLALGO + LCAO_Deepks& ld, +#endif +#ifdef __EXX + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, +#endif + const int istep) +{ + ModuleBase::TITLE("ModuleIO", "ctrl_iter_lcao"); + ModuleBase::timer::tick("ModuleIO", "ctrl_iter_lcao"); + + + + ModuleBase::timer::tick("ModuleIO", "ctrl_iter_lcao"); +} diff --git a/source/source_io/ctrl_iter_lcao.h b/source/source_io/ctrl_iter_lcao.h new file mode 100644 index 0000000000..f104d48635 --- /dev/null +++ b/source/source_io/ctrl_iter_lcao.h @@ -0,0 +1,48 @@ +#ifndef CTRL_ITER_LCAO_H +#define CTRL_ITER_LCAO_H + +#include "source_cell/unitcell.h" // use UnitCell +#include "source_cell/klist.h" // use K_Vectors +#include "source_estate/elecstate_lcao.h" // use elecstate::ElecStateLCAO +#include "source_psi/psi.h" // use Psi +#include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO +#include "source_basis/module_nao/two_center_bundle.h" // use TwoCenterBundle +#include "source_lcao/module_gint/gint_k.h" // use Gint_k +#include "source_basis/module_pw/pw_basis_k.h" // use ModulePW::PW_Basis_K and ModulePW::PW_Basis +#include "source_pw/module_pwdft/structure_factor.h" // use Structure_Factor +#include "source_lcao/module_rdmft/rdmft.h" // use RDMFT codes +#ifdef __EXX +#include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes +#endif + +namespace ModuleIO +{ + // in principle, we need to add const for all of the variables, mohan note 2025-06-05 + template + void ctrl_iter_lcao(UnitCell& ucell, + const Input_para& inp, + K_Vectors& kv, + elecstate::ElecStateLCAO* pelec, + Parallel_Orbitals& pv, + Grid_Driver& gd, + psi::Psi* psi, + hamilt::HamiltLCAO* p_hamilt, + TwoCenterBundle &two_center_bundle, + Gint_k &gk, + LCAO_Orbitals &orb, + const ModulePW::PW_Basis_K* pw_wfc, // for berryphase + const ModulePW::PW_Basis* pw_rho, // for berryphase + Grid_Technique >, // for berryphase + const ModulePW::PW_Basis_Big* pw_big, // for Wannier90 + const Structure_Factor& sf, // for Wannier90 + rdmft::RDMFT &rdmft_solver, // for RDMFT +#ifdef __MLALGO + LCAO_Deepks& ld, +#endif +#ifdef __EXX + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, +#endif + const int istep); +} +#endif From 025395335996bc6b4eb27dbed5d1d8d5954391f0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 14:11:45 +0800 Subject: [PATCH 53/60] restore esolver_ks_lcao iter_finish --- source/source_esolver/esolver_ks_lcao.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index ce8ff09c28..8a034246fc 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -606,6 +606,9 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& const std::vector>& dm_vec = dynamic_cast*>(this->pelec)->get_DM()->get_DMK_vector(); + // control the output related to the finished iteration +// ModuleIO::ctrl_iter_lcao(); + // 1) calculate the local occupation number matrix and energy correction in DFT+U if (PARAM.inp.dft_plus_u) { @@ -645,7 +648,10 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& sc.cal_mi_lcao(iter); } - // 4) call iter_finish() of ESolver_KS + // call iter_finish() of ESolver_KS, where band gap is printed, + // eig and occ are printed, magnetization is calculated, + // charge mixing is performed, potential is updated, + // HF and kS energies are computed, meta-GGA, Jason and restart ESolver_KS::iter_finish(ucell, istep, iter, conv_esolver); // 5) mix density matrix if mixing_restart + mixing_dmr + not first From 9a2822cdd0d132115cb2acbe82e2d6ea7977f8d0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 15:55:20 +0800 Subject: [PATCH 54/60] fix bugs --- source/source_esolver/esolver_ks_lcao.cpp | 4 ++-- source/source_io/ctrl_runner_lcao.cpp | 24 +++++++++++------------ source/source_io/ctrl_runner_lcao.h | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 8a034246fc..40d9631527 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -393,8 +393,8 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) this->orb_, this->pw_rho, this->pw_rhod, this->sf, this->locpp.vloc, #ifdef __EXX - *this->exd, - *this->exc, + this->exd, + this->exc, #endif this->solvent); diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index 7a7981d03b..3cd4459643 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -10,10 +10,10 @@ #include "source_io/write_vxc_r.hpp" // functions -#ifdef __EXX -#include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes -#include "source_lcao/module_ri/RPA_LRI.h" // use RPA code -#endif +//#ifdef __EXX +//#include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes +//#include "source_lcao/module_ri/RPA_LRI.h" // use RPA code +//#endif namespace ModuleIO { @@ -38,8 +38,8 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential #ifdef __EXX - Exx_LRI_Interface& exd, - Exx_LRI_Interface>& exc, + std::shared_ptr> exd, + std::shared_ptr>> exc, #endif surchem &solvent) // solvent model { @@ -166,8 +166,8 @@ template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, / Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential #ifdef __EXX - Exx_LRI_Interface& exd, - Exx_LRI_Interface>& exc, + std::shared_ptr> exd, + std::shared_ptr>> exc, #endif surchem &solvent); // solvent model @@ -190,8 +190,8 @@ template void ctrl_runner_lcao, double>(UnitCell& ucell, Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential #ifdef __EXX - Exx_LRI_Interface, double>& exd, - Exx_LRI_Interface, std::complex>& exc, + std::shared_ptr, double>> exd, + std::shared_ptr, std::complex>> exc, #endif surchem &solvent); // solvent model @@ -214,8 +214,8 @@ template void ctrl_runner_lcao, std::complex>(UnitC Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential #ifdef __EXX - Exx_LRI_Interface, double>& exd, - Exx_LRI_Interface, std::complex>& exc, + std::shared_ptr, double>> exd, + std::shared_ptr, std::complex>> exc, #endif surchem &solvent); // solvent model diff --git a/source/source_io/ctrl_runner_lcao.h b/source/source_io/ctrl_runner_lcao.h index f2996277cd..20318bba94 100644 --- a/source/source_io/ctrl_runner_lcao.h +++ b/source/source_io/ctrl_runner_lcao.h @@ -35,8 +35,8 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential #ifdef __EXX - Exx_LRI_Interface& exd, - Exx_LRI_Interface>& exc, + std::shared_ptr> exd, + std::shared_ptr>> exc, #endif surchem &solvent); // solvent model From cf73e306487e259f733737e2862d62f4f3538372 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 16:47:22 +0800 Subject: [PATCH 55/60] update ctrl_iter_lcao --- source/source_esolver/esolver_ks_lcao.cpp | 51 ++----------------- source/source_io/ctrl_iter_lcao.cpp | 62 ++++++++++++++++++----- source/source_io/ctrl_runner_lcao.cpp | 6 --- 3 files changed, 53 insertions(+), 66 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 40d9631527..a91614d670 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -606,8 +606,6 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& const std::vector>& dm_vec = dynamic_cast*>(this->pelec)->get_DM()->get_DMK_vector(); - // control the output related to the finished iteration -// ModuleIO::ctrl_iter_lcao(); // 1) calculate the local occupation number matrix and energy correction in DFT+U if (PARAM.inp.dft_plus_u) @@ -654,7 +652,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& // HF and kS energies are computed, meta-GGA, Jason and restart ESolver_KS::iter_finish(ucell, istep, iter, conv_esolver); - // 5) mix density matrix if mixing_restart + mixing_dmr + not first + // mix density matrix if mixing_restart + mixing_dmr + not first // mixing_restart at every iter except the last iter if(iter != PARAM.inp.scf_nmax && !conv_esolver) { @@ -665,55 +663,16 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& this->p_chgmix->mix_dmr(dm); } } - // 6) save charge density - // Peize Lin add 2020.04.04 - if (GlobalC::restart.info_save.save_charge) - { - for (int is = 0; is < PARAM.inp.nspin; ++is) - { - GlobalC::restart.save_disk("charge", is, this->chr.nrxx, this->chr.rho[is]); - } - } - -#ifdef __EXX - // 7) save exx matrix - if (PARAM.inp.calculation != "nscf") - { - if (GlobalC::exx_info.info_global.cal_exx) - { - GlobalC::exx_info.info_ri.real_number ? - this->exd->exx_iter_finish(this->kv, ucell, *this->p_hamilt, *this->pelec, - *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver) : - this->exc->exx_iter_finish(this->kv, ucell, *this->p_hamilt, *this->pelec, - *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver); - } - } -#endif - // 8) use the converged occupation matrix for next MD/Relax SCF calculation + // use the converged occupation matrix for next MD/Relax SCF calculation if (PARAM.inp.dft_plus_u && conv_esolver) { GlobalC::dftu.initialed_locale = true; } - // 9) for deepks, output labels during electronic steps (after conv_esolver is renewed) -#ifdef __MLALGO - if (PARAM.inp.deepks_out_labels >0 && PARAM.inp.deepks_out_freq_elec) - { - if (iter % PARAM.inp.deepks_out_freq_elec == 0 ) - { - hamilt::HamiltLCAO* p_ham_deepks = dynamic_cast*>(this->p_hamilt); - std::shared_ptr> ld_shared_ptr(&ld, [](LCAO_Deepks*) {}); - LCAO_Deepks_Interface deepks_interface(ld_shared_ptr); - - deepks_interface.out_deepks_labels(this->pelec->f_en.etot, this->kv.get_nks(), - ucell.nat, PARAM.globalv.nlocal, this->pelec->ekb, this->kv.kvec_d, - ucell, orb_, this->gd, &(this->pv), *(this->psi), - dynamic_cast*>(this->pelec)->get_DM(), - p_ham_deepks, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); - } - } -#endif + // control the output related to the finished iteration + ModuleIO::ctrl_iter_lcao(); + } template class ESolver_KS_LCAO; diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index 0643926740..03259a582c 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -6,21 +6,13 @@ namespace ModuleIO template void ctrl_iter_lcao(UnitCell& ucell, const Input_para& inp, - K_Vectors& kv, - elecstate::ElecStateLCAO* pelec, - Parallel_Orbitals& pv, - Grid_Driver& gd, - psi::Psi* psi, + K_Vectors& kv, // k points * + elecstate::ElecStateLCAO* pelec, // electronic info * + Parallel_Orbitals& pv, // parallel orbital info * + Grid_Driver& gd, // adjacent atom info * + psi::Psi* psi, // wave functions * hamilt::HamiltLCAO* p_hamilt, - TwoCenterBundle &two_center_bundle, - Gint_k &gk, - LCAO_Orbitals &orb, - const ModulePW::PW_Basis_K* pw_wfc, // for berryphase - const ModulePW::PW_Basis* pw_rho, // for berryphase - Grid_Technique >, // for berryphase - const ModulePW::PW_Basis_Big* pw_big, // for Wannier90 - const Structure_Factor& sf, // for Wannier90 - rdmft::RDMFT &rdmft_solver, // for RDMFT + LCAO_Orbitals &orb, // orbital info * #ifdef __MLALGO LCAO_Deepks& ld, #endif @@ -33,7 +25,49 @@ void ctrl_iter_lcao(UnitCell& ucell, ModuleBase::TITLE("ModuleIO", "ctrl_iter_lcao"); ModuleBase::timer::tick("ModuleIO", "ctrl_iter_lcao"); + // save charge density + // Peize Lin add 2020.04.04 + if (GlobalC::restart.info_save.save_charge) + { + for (int is = 0; is < PARAM.inp.nspin; ++is) + { + GlobalC::restart.save_disk("charge", is, this->chr.nrxx, this->chr.rho[is]); + } + } +#ifdef __EXX + // save exx matrix + if (PARAM.inp.calculation != "nscf") + { + if (GlobalC::exx_info.info_global.cal_exx) + { + GlobalC::exx_info.info_ri.real_number ? + exd->exx_iter_finish(kv, ucell, *this->p_hamilt, *pelec, + *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver) : + exc->exx_iter_finish(kv, ucell, *this->p_hamilt, *pelec, + *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver); + } + } +#endif + + // for deepks, output labels during electronic steps (after conv_esolver is renewed) +#ifdef __MLALGO + if (PARAM.inp.deepks_out_labels >0 && PARAM.inp.deepks_out_freq_elec) + { + if (iter % PARAM.inp.deepks_out_freq_elec == 0 ) + { + hamilt::HamiltLCAO* p_ham_deepks = dynamic_cast*>(this->p_hamilt); + std::shared_ptr> ld_shared_ptr(&ld, [](LCAO_Deepks*) {}); + LCAO_Deepks_Interface deepks_interface(ld_shared_ptr); + + deepks_interface.out_deepks_labels(pelec->f_en.etot, kv.get_nks(), + ucell.nat, PARAM.globalv.nlocal, pelec->ekb, kv.kvec_d, + ucell, orb, gd, &pv, *psi, + dynamic_cast*>(pelec)->get_DM(), + p_ham_deepks, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); + } + } +#endif ModuleBase::timer::tick("ModuleIO", "ctrl_iter_lcao"); } diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index 3cd4459643..0365205ba4 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -9,12 +9,6 @@ #include "source_io/write_vxc.hpp" #include "source_io/write_vxc_r.hpp" -// functions -//#ifdef __EXX -//#include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes -//#include "source_lcao/module_ri/RPA_LRI.h" // use RPA code -//#endif - namespace ModuleIO { From 60d344051ada5429843f286308822f44d3b3fad2 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 17:56:52 +0800 Subject: [PATCH 56/60] upate, compile successfully --- source/Makefile.Objects | 1 + source/source_esolver/esolver_ks_lcao.cpp | 59 ++++++++---- source/source_io/CMakeLists.txt | 1 + source/source_io/ctrl_iter_lcao.cpp | 111 ++++++++++++++++++---- source/source_io/ctrl_iter_lcao.h | 50 +++++----- source/source_io/ctrl_runner_lcao.cpp | 3 + 6 files changed, 161 insertions(+), 64 deletions(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 53fdb3f2d6..d43b91ee3d 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -583,6 +583,7 @@ OBJS_IO=input_conv.o\ output_mat_sparse.o\ ctrl_scf_lcao.o\ ctrl_runner_lcao.o\ + ctrl_iter_lcao.o\ ctrl_output_fp.o\ ctrl_output_pw.o\ para_json.o\ diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index a91614d670..456aafee7a 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -1,7 +1,6 @@ #include "esolver_ks_lcao.h" - -#include "source_base/formatter.h" +//#include "source_base/formatter.h" #include "source_base/global_variable.h" #include "source_base/tool_title.h" #include "source_estate/elecstate_tools.h" @@ -9,19 +8,19 @@ #include "source_estate/module_dm/cal_dm_psi.h" #include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_lcao/module_dftu/dftu.h" -#include "source_io/berryphase.h" +//#include "source_io/berryphase.h" #include "source_io/cube_io.h" //#include "source_io/io_npz.h" -#include "source_io/output_dmk.h" +//#include "source_io/output_dmk.h" #include "source_io/output_log.h" -#include "source_io/output_mat_sparse.h" -#include "source_io/output_mulliken.h" -#include "source_io/output_sk.h" +//#include "source_io/output_mat_sparse.h" +//#include "source_io/output_mulliken.h" +//#include "source_io/output_sk.h" #include "source_io/read_wfc_nao.h" -#include "source_io/to_qo.h" -#include "source_io/to_wannier90_lcao.h" -#include "source_io/to_wannier90_lcao_in_pw.h" -#include "source_io/write_HS.h" +//#include "source_io/to_qo.h" +//#include "source_io/to_wannier90_lcao.h" +//#include "source_io/to_wannier90_lcao_in_pw.h" +//#include "source_io/write_HS.h" #include "source_io/write_elecstat_pot.h" #include "source_io/module_parameter/parameter.h" @@ -62,6 +61,7 @@ #include "source_estate/module_charge/chgmixing.h" // use charge mixing, mohan add 20251006 #include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() +#include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() namespace ModuleESolver @@ -603,8 +603,20 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { ModuleBase::TITLE("ESolver_KS_LCAO", "iter_finish"); - const std::vector>& dm_vec - = dynamic_cast*>(this->pelec)->get_DM()->get_DMK_vector(); + auto* estate = dynamic_cast*>(this->pelec); + auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); + + if(!estate) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_finish","pelec does not exist"); + } + + if(!hamilt_lcao) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_finish","p_hamilt does not exist"); + } + + const std::vector>& dm_vec = estate->get_DM()->get_DMK_vector(); // 1) calculate the local occupation number matrix and energy correction in DFT+U @@ -621,7 +633,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), - this->p_hamilt); + hamilt_lcao); } GlobalC::dftu.cal_energy_correction(ucell, istep); } @@ -634,8 +646,8 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { ld.dpks_cal_e_delta_band(dm_vec, this->kv.get_nks()); DeePKS_domain::update_dmr(this->kv.kvec_d, dm_vec, ucell, orb_, this->pv, this->gd, ld.dm_r); - this->pelec->f_en.edeepks_scf = ld.E_delta - ld.e_delta_band; - this->pelec->f_en.edeepks_delta = ld.E_delta; + estate->f_en.edeepks_scf = ld.E_delta - ld.e_delta_band; + estate->f_en.edeepks_delta = ld.E_delta; } #endif @@ -658,8 +670,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { if (PARAM.inp.mixing_restart > 0 && this->p_chgmix->mixing_restart_count > 0 && PARAM.inp.mixing_dmr) { - elecstate::DensityMatrix* dm = - dynamic_cast*>(this->pelec)->get_DM(); + elecstate::DensityMatrix* dm = estate->get_DM(); this->p_chgmix->mix_dmr(dm); } } @@ -671,7 +682,17 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // control the output related to the finished iteration - ModuleIO::ctrl_iter_lcao(); + ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, estate, + this->pv, this->gd, this->psi, this->chr, this->p_chgmix, + hamilt_lcao, this->orb_, +#ifdef __MLALGO + this->ld, +#endif +#ifdef __EXX + *this->exd, + *this->exc, +#endif + iter, istep, conv_esolver, this->scf_ene_thr); } diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index 20f85f4b24..850a124402 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -85,6 +85,7 @@ if(ENABLE_LCAO) output_mat_sparse.cpp ctrl_scf_lcao.cpp ctrl_runner_lcao.cpp + ctrl_iter_lcao.cpp ) endif() diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index 03259a582c..8766d0b8c3 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -4,14 +4,16 @@ namespace ModuleIO { template -void ctrl_iter_lcao(UnitCell& ucell, - const Input_para& inp, +void ctrl_iter_lcao(UnitCell& ucell, // unit cell * + const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO* pelec, // electronic info * Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi* psi, // wave functions * - hamilt::HamiltLCAO* p_hamilt, + Charge &chr, // charge density * + Charge_Mixing* p_chgmix, // charge mixing * + hamilt::HamiltLCAO* p_hamilt, // hamiltonian * LCAO_Orbitals &orb, // orbital info * #ifdef __MLALGO LCAO_Deepks& ld, @@ -20,7 +22,10 @@ void ctrl_iter_lcao(UnitCell& ucell, Exx_LRI_Interface& exd, Exx_LRI_Interface>& exc, #endif - const int istep) + int &iter, + const int istep, + bool &conv_esolver, + const double &scf_ene_thr) { ModuleBase::TITLE("ModuleIO", "ctrl_iter_lcao"); ModuleBase::timer::tick("ModuleIO", "ctrl_iter_lcao"); @@ -29,45 +34,117 @@ void ctrl_iter_lcao(UnitCell& ucell, // Peize Lin add 2020.04.04 if (GlobalC::restart.info_save.save_charge) { - for (int is = 0; is < PARAM.inp.nspin; ++is) + for (int is = 0; is < inp.nspin; ++is) { - GlobalC::restart.save_disk("charge", is, this->chr.nrxx, this->chr.rho[is]); + GlobalC::restart.save_disk("charge", is, chr.nrxx, chr.rho[is]); } } #ifdef __EXX // save exx matrix - if (PARAM.inp.calculation != "nscf") + if (inp.calculation != "nscf") { if (GlobalC::exx_info.info_global.cal_exx) { GlobalC::exx_info.info_ri.real_number ? - exd->exx_iter_finish(kv, ucell, *this->p_hamilt, *pelec, - *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver) : - exc->exx_iter_finish(kv, ucell, *this->p_hamilt, *pelec, - *this->p_chgmix, this->scf_ene_thr, iter, istep, conv_esolver); + exd.exx_iter_finish(kv, ucell, *p_hamilt, *pelec, + *p_chgmix, scf_ene_thr, iter, istep, conv_esolver) : + exc.exx_iter_finish(kv, ucell, *p_hamilt, *pelec, + *p_chgmix, scf_ene_thr, iter, istep, conv_esolver); } } #endif // for deepks, output labels during electronic steps (after conv_esolver is renewed) #ifdef __MLALGO - if (PARAM.inp.deepks_out_labels >0 && PARAM.inp.deepks_out_freq_elec) + if (inp.deepks_out_labels >0 && inp.deepks_out_freq_elec) { - if (iter % PARAM.inp.deepks_out_freq_elec == 0 ) + if (iter % inp.deepks_out_freq_elec == 0 ) { - hamilt::HamiltLCAO* p_ham_deepks = dynamic_cast*>(this->p_hamilt); std::shared_ptr> ld_shared_ptr(&ld, [](LCAO_Deepks*) {}); LCAO_Deepks_Interface deepks_interface(ld_shared_ptr); deepks_interface.out_deepks_labels(pelec->f_en.etot, kv.get_nks(), ucell.nat, PARAM.globalv.nlocal, pelec->ekb, kv.kvec_d, - ucell, orb, gd, &pv, *psi, - dynamic_cast*>(pelec)->get_DM(), - p_ham_deepks, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); + ucell, orb, gd, &pv, *psi, estate->get_DM(), + p_hamilt, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); } } #endif ModuleBase::timer::tick("ModuleIO", "ctrl_iter_lcao"); } + +// TK: double TR: double +template void ctrl_iter_lcao(UnitCell& ucell, // unit cell * + const Input_para& inp, // input parameters * + K_Vectors& kv, // k points * + elecstate::ElecStateLCAO* pelec, // electronic info * + Parallel_Orbitals& pv, // parallel orbital info * + Grid_Driver& gd, // adjacent atom info * + psi::Psi* psi, // wave functions * + Charge &chr, // charge density * + Charge_Mixing* p_chgmix, // charge mixing * + hamilt::HamiltLCAO* p_hamilt, // hamiltonian * + LCAO_Orbitals &orb, // orbital info * +#ifdef __MLALGO + LCAO_Deepks& ld, +#endif +#ifdef __EXX + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, +#endif + int &iter, + const int istep, + bool &conv_esolver, + const double &scf_ene_thr); + +// TK: complex TR: double +template void ctrl_iter_lcao, double>(UnitCell& ucell, // unit cell * + const Input_para& inp, // input parameters * + K_Vectors& kv, // k points * + elecstate::ElecStateLCAO>* pelec, // electronic info * + Parallel_Orbitals& pv, // parallel orbital info * + Grid_Driver& gd, // adjacent atom info * + psi::Psi>* psi, // wave functions * + Charge &chr, // charge density * + Charge_Mixing* p_chgmix, // charge mixing * + hamilt::HamiltLCAO, double>* p_hamilt, // hamiltonian * + LCAO_Orbitals &orb, // orbital info * +#ifdef __MLALGO + LCAO_Deepks>& ld, +#endif +#ifdef __EXX + Exx_LRI_Interface, double>& exd, + Exx_LRI_Interface, std::complex>& exc, +#endif + int &iter, + const int istep, + bool &conv_esolver, + const double &scf_ene_thr); + +// TK: complex TR: complex +template void ctrl_iter_lcao, std::complex>(UnitCell& ucell, // unit cell * + const Input_para& inp, // input parameters * + K_Vectors& kv, // k points * + elecstate::ElecStateLCAO>* pelec, // electronic info * + Parallel_Orbitals& pv, // parallel orbital info * + Grid_Driver& gd, // adjacent atom info * + psi::Psi>* psi, // wave functions * + Charge &chr, // charge density * + Charge_Mixing* p_chgmix, // charge mixing * + hamilt::HamiltLCAO, std::complex>* p_hamilt, // hamiltonian * + LCAO_Orbitals &orb, // orbital info * +#ifdef __MLALGO + LCAO_Deepks>& ld, +#endif +#ifdef __EXX + Exx_LRI_Interface, double>& exd, + Exx_LRI_Interface, std::complex>& exc, +#endif + int &iter, + const int istep, + bool &conv_esolver, + const double &scf_ene_thr); + +} // end ModuleIO diff --git a/source/source_io/ctrl_iter_lcao.h b/source/source_io/ctrl_iter_lcao.h index f104d48635..4e750e798d 100644 --- a/source/source_io/ctrl_iter_lcao.h +++ b/source/source_io/ctrl_iter_lcao.h @@ -5,44 +5,38 @@ #include "source_cell/klist.h" // use K_Vectors #include "source_estate/elecstate_lcao.h" // use elecstate::ElecStateLCAO #include "source_psi/psi.h" // use Psi +#include "source_estate/module_charge/charge.h" // use charge #include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO -#include "source_basis/module_nao/two_center_bundle.h" // use TwoCenterBundle -#include "source_lcao/module_gint/gint_k.h" // use Gint_k -#include "source_basis/module_pw/pw_basis_k.h" // use ModulePW::PW_Basis_K and ModulePW::PW_Basis -#include "source_pw/module_pwdft/structure_factor.h" // use Structure_Factor -#include "source_lcao/module_rdmft/rdmft.h" // use RDMFT codes #ifdef __EXX #include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes #endif namespace ModuleIO { - // in principle, we need to add const for all of the variables, mohan note 2025-06-05 - template - void ctrl_iter_lcao(UnitCell& ucell, - const Input_para& inp, - K_Vectors& kv, - elecstate::ElecStateLCAO* pelec, - Parallel_Orbitals& pv, - Grid_Driver& gd, - psi::Psi* psi, - hamilt::HamiltLCAO* p_hamilt, - TwoCenterBundle &two_center_bundle, - Gint_k &gk, - LCAO_Orbitals &orb, - const ModulePW::PW_Basis_K* pw_wfc, // for berryphase - const ModulePW::PW_Basis* pw_rho, // for berryphase - Grid_Technique >, // for berryphase - const ModulePW::PW_Basis_Big* pw_big, // for Wannier90 - const Structure_Factor& sf, // for Wannier90 - rdmft::RDMFT &rdmft_solver, // for RDMFT + +template +void ctrl_iter_lcao(UnitCell& ucell, // unit cell * + const Input_para& inp, // input parameters * + K_Vectors& kv, // k points * + elecstate::ElecStateLCAO* pelec, // electronic info * + Parallel_Orbitals& pv, // parallel orbital info * + Grid_Driver& gd, // adjacent atom info * + psi::Psi* psi, // wave functions * + Charge &chr, // charge density * + Charge_Mixing* p_chgmix, // charge mixing * + hamilt::HamiltLCAO* p_hamilt, // hamiltonian * + LCAO_Orbitals &orb, // orbital info * #ifdef __MLALGO - LCAO_Deepks& ld, + LCAO_Deepks& ld, #endif #ifdef __EXX - Exx_LRI_Interface& exd, - Exx_LRI_Interface>& exc, + Exx_LRI_Interface& exd, + Exx_LRI_Interface>& exc, #endif - const int istep); + int &iter, + const int istep, + bool &conv_esolver, + const double &scf_ene_thr); + } #endif diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index 0365205ba4..d92f08ff3d 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -141,6 +141,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell +// TK: double TR: double template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point @@ -165,6 +166,7 @@ template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, / #endif surchem &solvent); // solvent model +// TK: complex TR: double template void ctrl_runner_lcao, double>(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point @@ -189,6 +191,7 @@ template void ctrl_runner_lcao, double>(UnitCell& ucell, #endif surchem &solvent); // solvent model +// TK: complex TR: complex template void ctrl_runner_lcao, std::complex>(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point From 75762d249904091506e4a940864b4022d058654b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 18:00:17 +0800 Subject: [PATCH 57/60] fix bug --- source/source_io/ctrl_iter_lcao.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/source_io/ctrl_iter_lcao.h b/source/source_io/ctrl_iter_lcao.h index 4e750e798d..7b6dc32b47 100644 --- a/source/source_io/ctrl_iter_lcao.h +++ b/source/source_io/ctrl_iter_lcao.h @@ -6,6 +6,7 @@ #include "source_estate/elecstate_lcao.h" // use elecstate::ElecStateLCAO #include "source_psi/psi.h" // use Psi #include "source_estate/module_charge/charge.h" // use charge +#include "source_estate/module_charge/charge_mixing.h" // use charge mixing #include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO #ifdef __EXX #include "source_lcao/module_ri/Exx_LRI_interface.h" // use EXX codes From 53e115d1d3589f4e3b14152796d498b7148e66fe Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 18:05:59 +0800 Subject: [PATCH 58/60] fix bug, add GlobalC using global.h --- source/source_io/ctrl_iter_lcao.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index 8766d0b8c3..62e1a453fe 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -1,4 +1,5 @@ #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() +#include "source_pw/module_pwdft/global.h" // use GlobalC::restart namespace ModuleIO { From 86716731fecb296d03d7e193949b6eadd4b32f91 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 6 Oct 2025 18:14:40 +0800 Subject: [PATCH 59/60] fix deepks compile --- source/source_io/ctrl_iter_lcao.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index 62e1a453fe..ae919ad0ae 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -1,6 +1,11 @@ #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() #include "source_pw/module_pwdft/global.h" // use GlobalC::restart +#ifdef __MLALGO +#include "source_lcao/module_deepks/LCAO_deepks.h" +#include "source_lcao/module_deepks/LCAO_deepks_interface.h" +#endif + namespace ModuleIO { From 1465b2b876fc23446784f9eaa373a95740af86a9 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 7 Oct 2025 17:45:37 +0800 Subject: [PATCH 60/60] update estate --- source/source_io/ctrl_iter_lcao.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index ae919ad0ae..d14ba00f63 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -72,7 +72,7 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * deepks_interface.out_deepks_labels(pelec->f_en.etot, kv.get_nks(), ucell.nat, PARAM.globalv.nlocal, pelec->ekb, kv.kvec_d, - ucell, orb, gd, &pv, *psi, estate->get_DM(), + ucell, orb, gd, &pv, *psi, pelec->get_DM(), p_hamilt, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); } }