From 242184cdbc2fa0b59f46fdacd7c1701704ae3222 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 16 Oct 2025 14:47:46 +0800 Subject: [PATCH 01/81] add update_pot in source_estate --- source/source_esolver/esolver_ks.cpp | 3 ++- source/source_esolver/esolver_ks_lcao.cpp | 16 ---------------- source/source_esolver/esolver_ks_pw.cpp | 21 ++------------------- source/source_estate/update_pot.cpp | 20 ++++++++++++++++++++ source/source_estate/update_pot.h | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 source/source_estate/update_pot.cpp create mode 100644 source/source_estate/update_pot.h diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index a4e8126ad0..d98571d541 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -23,6 +23,7 @@ #include "source_io/json_output/init_info.h" #include "source_io/json_output/output_info.h" +#include "source_estate/update_pot.h" // mohan add 20251016 namespace ModuleESolver { @@ -374,7 +375,7 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i if (this->scf_ene_thr > 0.0) { // calculate energy of output charge density - this->update_pot(ucell, istep, iter, conv_esolver); + elecstate::update_pot(ucell, istep, iter, conv_esolver); this->pelec->cal_energies(2); // 2 means Kohn-Sham functional // now, etot_old is the energy of input density, while etot is the energy of output density this->pelec->f_en.etot_delta = this->pelec->f_en.etot - this->pelec->f_en.etot_old; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index b7601da811..c7171d0de7 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -446,22 +446,6 @@ void ESolver_KS_LCAO::hamilt2rho_single(UnitCell& ucell, int istep, int this->pelec->f_en.deband = this->pelec->cal_delta_eband(ucell); } -template -void ESolver_KS_LCAO::update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) -{ - ModuleBase::TITLE("ESolver_KS_LCAO", "update_pot"); - - if (!conv_esolver) - { - elecstate::cal_ux(ucell); - this->pelec->pot->update_from_charge(&this->chr, &ucell); - this->pelec->f_en.descf = this->pelec->cal_delta_escf(); - } - else - { - this->pelec->cal_converged(); - } -} template void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index d2e2612070..3fd406d086 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -28,6 +28,7 @@ #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 +#include "source_estate/update_pot.h" // mohan add 20251016 namespace ModuleESolver { @@ -268,24 +269,6 @@ void ESolver_KS_PW::hamilt2rho_single(UnitCell& ucell, const int iste ModuleBase::timer::tick("ESolver_KS_PW", "hamilt2rho_single"); } -// Temporary, it should be rewritten with Hamilt class. -template -void ESolver_KS_PW::update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) -{ - if (!conv_esolver) - { - elecstate::cal_ux(ucell); - this->pelec->pot->update_from_charge(&this->chr, &ucell); - this->pelec->f_en.descf = this->pelec->cal_delta_escf(); -#ifdef __MPI - MPI_Bcast(&(this->pelec->f_en.descf), 1, MPI_DOUBLE, 0, BP_WORLD); -#endif - } - else - { - this->pelec->cal_converged(); - } -} template void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) @@ -343,7 +326,7 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int << std::endl; exx_helper.op_exx->first_iter = false; XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); - update_pot(ucell, istep, iter, conv_esolver); + elecstate::update_pot(ucell, istep, iter, conv_esolver); exx_helper.iter_inc(); } } diff --git a/source/source_estate/update_pot.cpp b/source/source_estate/update_pot.cpp new file mode 100644 index 0000000000..4f4c6791f7 --- /dev/null +++ b/source/source_estate/update_pot.cpp @@ -0,0 +1,20 @@ +#include "source_estate/update_pot.h" + +void elecstate::update_pot(UnitCell& ucell, // unitcell + elecstate::ElecState* &pelec, // pointer of electrons + Charge &chr) // charge density +{ + if (!conv_esolver) + { + elecstate::cal_ux(ucell); + pelec->pot->update_from_charge(&chr, &ucell); + pelec->f_en.descf = pelec->cal_delta_escf(); +#ifdef __MPI + MPI_Bcast(&(pelec->f_en.descf), 1, MPI_DOUBLE, 0, BP_WORLD); +#endif + } + else + { + pelec->cal_converged(); + } +} diff --git a/source/source_estate/update_pot.h b/source/source_estate/update_pot.h new file mode 100644 index 0000000000..33eca12f7c --- /dev/null +++ b/source/source_estate/update_pot.h @@ -0,0 +1,16 @@ +#ifndef UPDATE_POT_H +#define UPDATE_POT_H + +#include "source_cell/unitcell.h" +#include "source_estate/elecstate.h" + +namespace elecstate +{ + +void update_pot(UnitCell& ucell, // unitcell + elecstate::ElecState* &pelec, // pointer of electrons + Charge &chr); // charge density +} + + +#endif From 9441396323cd0405e9d84d63e7dda55879e8645b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 17 Oct 2025 16:59:50 +0800 Subject: [PATCH 02/81] split update_pot in rt-TDDFT into two functions, one is the original update_pot, which is replaced by update_pot function in estate, the other is save2 function used in after_iter --- source/Makefile.Objects | 1 + source/source_esolver/esolver_ks.cpp | 4 ++-- source/source_esolver/esolver_ks.h | 3 --- source/source_esolver/esolver_ks_lcao.h | 2 -- source/source_esolver/esolver_ks_lcao_tddft.cpp | 6 +++++- source/source_esolver/esolver_ks_lcao_tddft.h | 3 ++- source/source_esolver/esolver_ks_pw.cpp | 4 ++-- source/source_esolver/esolver_ks_pw.h | 2 -- source/source_esolver/rho_restart.cpp | 2 ++ source/source_estate/CMakeLists.txt | 1 + source/source_estate/update_pot.cpp | 6 ++++-- source/source_estate/update_pot.h | 5 +++-- 12 files changed, 22 insertions(+), 17 deletions(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index b05e69f8f9..e612261c6e 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -250,6 +250,7 @@ OBJS_ELECSTAT=elecstate.o\ read_pseudo.o\ cal_wfc.o\ setup_estate_pw.o\ + update_pot.o\ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\ elecstate_lcao_cal_tau.o\ diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index d98571d541..d9544209d3 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -375,7 +375,7 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i if (this->scf_ene_thr > 0.0) { // calculate energy of output charge density - elecstate::update_pot(ucell, istep, iter, conv_esolver); + elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver); this->pelec->cal_energies(2); // 2 means Kohn-Sham functional // now, etot_old is the energy of input density, while etot is the energy of output density this->pelec->f_en.etot_delta = this->pelec->f_en.etot - this->pelec->f_en.etot_old; @@ -433,7 +433,7 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i #endif // 4) Update potentials (should be done every SF iter) - this->update_pot(ucell, istep, iter, conv_esolver); + elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver); // 5) calculate energies // 1 means Harris-Foulkes functional diff --git a/source/source_esolver/esolver_ks.h b/source/source_esolver/esolver_ks.h index d54794dd89..3f271dc4cc 100644 --- a/source/source_esolver/esolver_ks.h +++ b/source/source_esolver/esolver_ks.h @@ -46,9 +46,6 @@ class ESolver_KS : public ESolver_FP //! Something to do after SCF iterations when SCF is converged or comes to the max iter step. virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) override; - //! It should be replaced by a function in Hamilt Class - virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver){}; - //! Hamiltonian hamilt::Hamilt* p_hamilt = nullptr; diff --git a/source/source_esolver/esolver_ks_lcao.h b/source/source_esolver/esolver_ks_lcao.h index 13359f7c8e..67ca7a0482 100644 --- a/source/source_esolver/esolver_ks_lcao.h +++ b/source/source_esolver/esolver_ks_lcao.h @@ -52,8 +52,6 @@ class ESolver_KS_LCAO : public ESolver_KS virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; - virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) override; - virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override; virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) override; diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index c68ff0955b..20f1feddce 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -336,16 +336,19 @@ void ESolver_KS_LCAO_TDDFT::iter_finish( GlobalV::ofs_running << std::endl; } + this->save2(ucell, istep, iter, conv_esolver); + ESolver_KS_LCAO, TR>::iter_finish(ucell, istep, iter, conv_esolver); } template -void ESolver_KS_LCAO_TDDFT::update_pot(UnitCell& ucell, +void ESolver_KS_LCAO_TDDFT::save2(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) { // Calculate new potential according to new Charge Density +/* if (!conv_esolver) { elecstate::cal_ux(ucell); @@ -356,6 +359,7 @@ void ESolver_KS_LCAO_TDDFT::update_pot(UnitCell& ucell, { this->pelec->cal_converged(); } +*/ const int nloc = this->pv.nloc; const int ncol_nbands = this->pv.ncol_bands; diff --git a/source/source_esolver/esolver_ks_lcao_tddft.h b/source/source_esolver/esolver_ks_lcao_tddft.h index fc0a62e41d..fe3f55cf29 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.h +++ b/source/source_esolver/esolver_ks_lcao_tddft.h @@ -64,7 +64,8 @@ class ESolver_KS_LCAO_TDDFT : public ESolver_KS_LCAO, TR> virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; - virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) override; + // mohan change update_pot to save2, 2025-10-17 + virtual void save2(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver); virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override; diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 3fd406d086..a8e5d553b7 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -326,8 +326,8 @@ void ESolver_KS_PW::iter_finish(UnitCell& ucell, const int istep, int << std::endl; exx_helper.op_exx->first_iter = false; XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); - elecstate::update_pot(ucell, istep, iter, conv_esolver); - exx_helper.iter_inc(); + elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver); + exx_helper.iter_inc(); } } } diff --git a/source/source_esolver/esolver_ks_pw.h b/source/source_esolver/esolver_ks_pw.h index acdd7083ee..09fbefc175 100644 --- a/source/source_esolver/esolver_ks_pw.h +++ b/source/source_esolver/esolver_ks_pw.h @@ -41,8 +41,6 @@ class ESolver_KS_PW : public ESolver_KS virtual void iter_init(UnitCell& ucell, const int istep, const int iter) override; - virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) override; - virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override; virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) override; diff --git a/source/source_esolver/rho_restart.cpp b/source/source_esolver/rho_restart.cpp index bd7497f34b..3a8c4b40b5 100644 --- a/source/source_esolver/rho_restart.cpp +++ b/source/source_esolver/rho_restart.cpp @@ -1,5 +1,6 @@ #include "rho_restart.h" +/* void ModuleESolver::rho_restart(const Input_para& inp, const UnitCell& ucell, const elecstate::ElecState& elec, @@ -145,3 +146,4 @@ void ModuleESolver::rho_restart(const Input_para& inp, #endif } +*/ diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index a8380ade4e..8a54110e55 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -39,6 +39,7 @@ list(APPEND objects read_pseudo.cpp cal_wfc.cpp setup_estate_pw.cpp + update_pot.cpp ) if(ENABLE_LCAO) diff --git a/source/source_estate/update_pot.cpp b/source/source_estate/update_pot.cpp index 4f4c6791f7..1cc51da836 100644 --- a/source/source_estate/update_pot.cpp +++ b/source/source_estate/update_pot.cpp @@ -1,8 +1,10 @@ #include "source_estate/update_pot.h" -void elecstate::update_pot(UnitCell& ucell, // unitcell +void elecstate::update_pot(const UnitCell& ucell, // unitcell elecstate::ElecState* &pelec, // pointer of electrons - Charge &chr) // charge density + const Charge &chr, + const bool conv_esolver + ) // charge density { if (!conv_esolver) { diff --git a/source/source_estate/update_pot.h b/source/source_estate/update_pot.h index 33eca12f7c..27c55bf582 100644 --- a/source/source_estate/update_pot.h +++ b/source/source_estate/update_pot.h @@ -7,9 +7,10 @@ namespace elecstate { -void update_pot(UnitCell& ucell, // unitcell +void update_pot(const UnitCell& ucell, // unitcell elecstate::ElecState* &pelec, // pointer of electrons - Charge &chr); // charge density + const Charge &chr, + const bool conv_esolver); // charge density } From dc200841cf7166f985e1e61c7109005f08cb04fe Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 17 Oct 2025 20:56:48 +0800 Subject: [PATCH 03/81] fix bugs --- source/source_estate/update_pot.cpp | 3 ++- source/source_estate/update_pot.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/source_estate/update_pot.cpp b/source/source_estate/update_pot.cpp index 1cc51da836..8a49f66e0c 100644 --- a/source/source_estate/update_pot.cpp +++ b/source/source_estate/update_pot.cpp @@ -1,6 +1,7 @@ #include "source_estate/update_pot.h" +#include "source_estate/cal_ux.h" -void elecstate::update_pot(const UnitCell& ucell, // unitcell +void elecstate::update_pot(UnitCell& ucell, // unitcell elecstate::ElecState* &pelec, // pointer of electrons const Charge &chr, const bool conv_esolver diff --git a/source/source_estate/update_pot.h b/source/source_estate/update_pot.h index 27c55bf582..cce4c42428 100644 --- a/source/source_estate/update_pot.h +++ b/source/source_estate/update_pot.h @@ -7,7 +7,7 @@ namespace elecstate { -void update_pot(const UnitCell& ucell, // unitcell +void update_pot(UnitCell& ucell, // unitcell elecstate::ElecState* &pelec, // pointer of electrons const Charge &chr, const bool conv_esolver); // charge density From 7829228d9a1f125351fe7fa466d8d3c81094d678 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 17 Oct 2025 22:54:53 +0800 Subject: [PATCH 04/81] update --- source/source_esolver/esolver_ks_lcao_tddft.cpp | 3 ++- source/source_esolver/esolver_ks_lcao_tddft.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index 20f1feddce..3072579164 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -336,9 +336,10 @@ void ESolver_KS_LCAO_TDDFT::iter_finish( GlobalV::ofs_running << std::endl; } + ESolver_KS_LCAO, TR>::iter_finish(ucell, istep, iter, conv_esolver); + this->save2(ucell, istep, iter, conv_esolver); - ESolver_KS_LCAO, TR>::iter_finish(ucell, istep, iter, conv_esolver); } template diff --git a/source/source_esolver/esolver_ks_lcao_tddft.h b/source/source_esolver/esolver_ks_lcao_tddft.h index fe3f55cf29..75bd287591 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.h +++ b/source/source_esolver/esolver_ks_lcao_tddft.h @@ -65,7 +65,7 @@ class ESolver_KS_LCAO_TDDFT : public ESolver_KS_LCAO, TR> virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; // mohan change update_pot to save2, 2025-10-17 - virtual void save2(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver); + void save2(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver); virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override; From e842ff458f5d6410ce0c2d71c6103479880e68a8 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 11:52:35 +0800 Subject: [PATCH 05/81] update esolver_ks --- source/source_esolver/esolver_ks.cpp | 192 ++++-------------- .../source_estate/module_charge/chgmixing.cpp | 115 +++++++++++ .../source_estate/module_charge/chgmixing.h | 19 +- 3 files changed, 172 insertions(+), 154 deletions(-) diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index d9544209d3..a9c6891384 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -24,14 +24,13 @@ #include "source_io/json_output/output_info.h" #include "source_estate/update_pot.h" // mohan add 20251016 +#include "source_estate/module_charge/chgmixing.h" // mohan add 20251018 namespace ModuleESolver { template -ESolver_KS::ESolver_KS() -{ -} +ESolver_KS::ESolver_KS(){} template @@ -274,31 +273,15 @@ void ESolver_KS::iter_init(UnitCell& ucell, const int istep, const in if (PARAM.inp.esolver_type == "ksdft") { - diag_ethr = hsolver::set_diagethr_ks(PARAM.inp.basis_type, - PARAM.inp.esolver_type, - PARAM.inp.calculation, - PARAM.inp.init_chg, - PARAM.inp.precision, - istep, - iter, - drho, - PARAM.inp.pw_diag_thr, - diag_ethr, - PARAM.inp.nelec); + diag_ethr = hsolver::set_diagethr_ks(PARAM.inp.basis_type, PARAM.inp.esolver_type, + PARAM.inp.calculation, PARAM.inp.init_chg, PARAM.inp.precision, istep, iter, + drho, PARAM.inp.pw_diag_thr, diag_ethr, PARAM.inp.nelec); } else if (PARAM.inp.esolver_type == "sdft") { - diag_ethr = hsolver::set_diagethr_sdft(PARAM.inp.basis_type, - PARAM.inp.esolver_type, - PARAM.inp.calculation, - PARAM.inp.init_chg, - istep, - iter, - drho, - PARAM.inp.pw_diag_thr, - diag_ethr, - PARAM.inp.nbands, - esolver_KS_ne); + diag_ethr = hsolver::set_diagethr_sdft(PARAM.inp.basis_type, PARAM.inp.esolver_type, + PARAM.inp.calculation, PARAM.inp.init_chg, istep, iter, drho, + PARAM.inp.pw_diag_thr, diag_ethr, PARAM.inp.nbands, esolver_KS_ne); } // save input charge density (rho) @@ -309,9 +292,7 @@ template void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver) { - //---------------------------------------------------------------- - // 1) print out band gap - //---------------------------------------------------------------- + // 1.1) print out band gap if (!PARAM.globalv.two_fermi) { this->pelec->cal_bandgap(); @@ -321,121 +302,28 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i this->pelec->cal_bandgap_updw(); } + // 1.2) print out eigenvalues and occupations if(iter % PARAM.inp.out_freq_elec == 0) { - //---------------------------------------------------------------- - // 2) print out eigenvalues and occupations - //---------------------------------------------------------------- if (PARAM.inp.out_band[0] || iter == PARAM.inp.scf_nmax || conv_esolver) { ModuleIO::write_eig_iter(this->pelec->ekb,this->pelec->wg,*this->pelec->klist); } } - //---------------------------------------------------------------- - // 2) compute magnetization, only for LSDA(spin==2) - //---------------------------------------------------------------- + // 2.1) compute magnetization, only for spin==2 ucell.magnet.compute_mag(ucell.omega, this->chr.nrxx, this->chr.nxyz, this->chr.rho, this->pelec->nelec_spin.data()); - //---------------------------------------------------------------- - // 3) charge mixing - //---------------------------------------------------------------- - if (PARAM.globalv.ks_run) - { - // mixing will restart at this->p_chgmix->mixing_restart steps - if (drho <= PARAM.inp.mixing_restart && PARAM.inp.mixing_restart > 0.0 - && this->p_chgmix->mixing_restart_step > iter) - { - this->p_chgmix->mixing_restart_step = iter + 1; - } - - if (PARAM.inp.scf_os_stop) // if oscillation is detected, SCF will stop - { - this->oscillate_esolver - = this->p_chgmix->if_scf_oscillate(iter, drho, PARAM.inp.scf_os_ndim, PARAM.inp.scf_os_thr); - } - - // drho will be 0 at this->p_chgmix->mixing_restart step, which is - // not ground state - bool not_restart_step = !(iter == this->p_chgmix->mixing_restart_step && PARAM.inp.mixing_restart > 0.0); - // SCF will continue if U is not converged for uramping calculation - bool is_U_converged = true; - // to avoid unnecessary dependence on dft+u, refactor is needed -#ifdef __LCAO - if (PARAM.inp.dft_plus_u) - { - is_U_converged = GlobalC::dftu.u_converged(); - } -#endif - - conv_esolver = (drho < this->scf_thr && not_restart_step && is_U_converged); - - // add energy threshold for SCF convergence - if (this->scf_ene_thr > 0.0) - { - // calculate energy of output charge density - elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver); - this->pelec->cal_energies(2); // 2 means Kohn-Sham functional - // now, etot_old is the energy of input density, while etot is the energy of output density - this->pelec->f_en.etot_delta = this->pelec->f_en.etot - this->pelec->f_en.etot_old; - // output etot_delta - GlobalV::ofs_running << " DeltaE_womix = " << this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV << " eV" - << std::endl; - if (iter > 1 && conv_esolver == 1) // only check when density is converged - { - // update the convergence flag - conv_esolver - = (std::abs(this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < this->scf_ene_thr); - } - } - - // If drho < hsolver_error in the first iter or drho < scf_thr, we - // do not change rho. - if (drho < hsolver_error || conv_esolver || PARAM.inp.calculation == "nscf") - { - if (drho < hsolver_error) - { - GlobalV::ofs_warning << " drho < hsolver_error, keep " - "charge density unchanged." - << std::endl; - } - } - else - { - //----------charge mixing--------------- - // mixing will restart after this->p_chgmix->mixing_restart - // steps - if (PARAM.inp.mixing_restart > 0 && iter == this->p_chgmix->mixing_restart_step - 1 - && drho <= PARAM.inp.mixing_restart) - { - // do not mix charge density - } - else - { - p_chgmix->mix_rho(&this->chr); // update chr->rho by mixing - } - if (PARAM.inp.scf_thr_type == 2) - { - this->chr.renormalize_rho(); // renormalize rho in R-space would - // induce a error in K-space - } - //----------charge mixing done----------- - } - } - -#ifdef __MPI - MPI_Bcast(&drho, 1, MPI_DOUBLE, 0, BP_WORLD); + // 2.2) charge mixing + module_charge::chgmixing_ks(iter, ucell, this->pelec, this->chr, this->p_chgmix, + this->pw_rhod->nrxx, this->drho, this->oscillate_esolver, conv_esolver, hsolver_error, + this->scf_thr, this->scf_ene_thr, PARAM.inp); - // change MPI_DOUBLE to MPI_C_BOOL, mohan 2025-04-13 - MPI_Bcast(&conv_esolver, 1, MPI_C_BOOL, 0, BP_WORLD); - MPI_Bcast(this->chr.rho[0], this->pw_rhod->nrxx, MPI_DOUBLE, 0, BP_WORLD); -#endif - - // 4) Update potentials (should be done every SF iter) + // 2.3) Update potentials (should be done every SF iter) elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver); - // 5) calculate energies + // 3.1) calculate energies // 1 means Harris-Foulkes functional // 2 means Kohn-Sham functional this->pelec->cal_energies(1); @@ -448,10 +336,27 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i this->pelec->f_en.etot_delta = this->pelec->f_en.etot - this->pelec->f_en.etot_old; this->pelec->f_en.etot_old = this->pelec->f_en.etot; + // 4) get meta-GGA related parameters + double dkin = 0.0; // for meta-GGA + if (XC_Functional::get_ked_flag()) + { + dkin = p_chgmix->get_dkin(&this->chr, PARAM.inp.nelec); + } + + // SCF restart information + if (PARAM.inp.mixing_restart > 0 + && iter == this->p_chgmix->mixing_restart_step - 1 + && iter != PARAM.inp.scf_nmax) + { + this->p_chgmix->mixing_restart_last = iter; + std::cout << " SCF restart after this step!" << std::endl; + } + + // Iter finish + ESolver_FP::iter_finish(ucell, istep, iter, conv_esolver); + - //---------------------------------------------------------------- - // 6) time and meta-GGA - //---------------------------------------------------------------- + // the end, print time #ifdef __MPI double duration = (double)(MPI_Wtime() - iter_time); #else @@ -460,38 +365,19 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i / static_cast(1e6); #endif - // get mtaGGA related parameters - double dkin = 0.0; // for meta-GGA - if (XC_Functional::get_ked_flag()) - { - dkin = p_chgmix->get_dkin(&this->chr, PARAM.inp.nelec); - } - - // pint energy + // print energies elecstate::print_etot(ucell.magnet, *pelec,conv_esolver, iter, drho, dkin, duration, diag_ethr); #ifdef __RAPIDJSON - // 7) add Json of scf mag + // add Json of scf mag Json::add_output_scf_mag(ucell.magnet.tot_mag, ucell.magnet.abs_mag, this->pelec->f_en.etot * ModuleBase::Ry_to_eV, this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV, drho, duration); #endif //__RAPIDJSON - - // 7) SCF restart information - if (PARAM.inp.mixing_restart > 0 - && iter == this->p_chgmix->mixing_restart_step - 1 - && iter != PARAM.inp.scf_nmax) - { - this->p_chgmix->mixing_restart_last = iter; - std::cout << " SCF restart after this step!" << std::endl; - } - - // 8) Iter finish - ESolver_FP::iter_finish(ucell, istep, iter, conv_esolver); } //! Something to do after SCF iterations when SCF is converged or comes to the max iter step. diff --git a/source/source_estate/module_charge/chgmixing.cpp b/source/source_estate/module_charge/chgmixing.cpp index 87496ced87..21f8a2ab32 100644 --- a/source/source_estate/module_charge/chgmixing.cpp +++ b/source/source_estate/module_charge/chgmixing.cpp @@ -1,7 +1,122 @@ #include "source_estate/module_charge/chgmixing.h" +#include "source_estate/update_pot.h" #include "source_lcao/module_dftu/dftu.h" #include "source_lcao/module_deltaspin/spin_constrain.h" +void module_charge::chgmixing_ks(const int iter, // scf iteration number + UnitCell& ucell, + elecstate::ElecState* pelec, + Charge &chr, // charge density + Charge_Mixing* p_chgmix, // charge mixing class + const int nrxx, // charge density + double &drho, // charge density deviation + bool &oscillate_esolver, // whether the esolver has oscillation of charge density + bool &conv_esolver, + const double &hsolver_error, + const double &scf_thr, + const double &scf_ene_thr, + const Input_para& inp) // input parameters +{ + + if (PARAM.globalv.ks_run) + { + // mixing will restart at p_chgmix->mixing_restart steps + if (drho <= inp.mixing_restart && inp.mixing_restart > 0.0 + && p_chgmix->mixing_restart_step > iter) + { + p_chgmix->mixing_restart_step = iter + 1; + } + + if (inp.scf_os_stop) // if oscillation is detected, SCF will stop + { + oscillate_esolver = p_chgmix->if_scf_oscillate(iter, drho, + inp.scf_os_ndim, inp.scf_os_thr); + } + + // drho will be 0 at p_chgmix->mixing_restart step, which is + // not ground state + bool not_restart_step = !(iter == p_chgmix->mixing_restart_step && inp.mixing_restart > 0.0); + // SCF will continue if U is not converged for uramping calculation + bool is_U_converged = true; + // to avoid unnecessary dependence on dft+u, refactor is needed +#ifdef __LCAO + if (inp.dft_plus_u) + { + is_U_converged = GlobalC::dftu.u_converged(); + } +#endif + + conv_esolver = (drho < scf_thr && not_restart_step && is_U_converged); + + // add energy threshold for SCF convergence + if (scf_ene_thr > 0.0) + { + // calculate energy of output charge density + elecstate::update_pot(ucell, pelec, chr, conv_esolver); + pelec->cal_energies(2); // 2 means Kohn-Sham functional + // now, etot_old is the energy of input density, while etot is the energy of output density + pelec->f_en.etot_delta = pelec->f_en.etot - pelec->f_en.etot_old; + // output etot_delta + GlobalV::ofs_running << " DeltaE_womix = " << pelec->f_en.etot_delta * ModuleBase::Ry_to_eV << " eV" + << std::endl; + if (iter > 1 && conv_esolver == 1) // only check when density is converged + { + // update the convergence flag + conv_esolver + = (std::abs(pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < scf_ene_thr); + } + } + + + + // If drho < hsolver_error in the first iter or drho < scf_thr, we + // do not change rho. + if (drho < hsolver_error || conv_esolver || inp.calculation == "nscf") + { + if (drho < hsolver_error) + { + GlobalV::ofs_warning << " drho < hsolver_error, keep " + "charge density unchanged." + << std::endl; + } + } + else + { + //----------charge mixing--------------- + // mixing will restart after p_chgmix->mixing_restart + // steps + if (inp.mixing_restart > 0 && iter == p_chgmix->mixing_restart_step - 1 + && drho <= inp.mixing_restart) + { + // do not mix charge density + } + else + { + p_chgmix->mix_rho(&chr); // update chr->rho by mixing + } + if (inp.scf_thr_type == 2) + { + chr.renormalize_rho(); // renormalize rho in R-space would + // induce a error in K-space + } + //----------charge mixing done----------- + } + } + +#ifdef __MPI + MPI_Bcast(&drho, 1, MPI_DOUBLE, 0, BP_WORLD); + + // change MPI_DOUBLE to MPI_C_BOOL, mohan 2025-04-13 + MPI_Bcast(&conv_esolver, 1, MPI_C_BOOL, 0, BP_WORLD); + + assert(nrxx>=0); // mohan add 2025-10-18 + MPI_Bcast(chr.rho[0], nrxx, MPI_DOUBLE, 0, BP_WORLD); +#endif + +} + + + 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 diff --git a/source/source_estate/module_charge/chgmixing.h b/source/source_estate/module_charge/chgmixing.h index 2e962ed8e5..3be8660013 100644 --- a/source/source_estate/module_charge/chgmixing.h +++ b/source/source_estate/module_charge/chgmixing.h @@ -1,12 +1,29 @@ #ifndef CHGMIXING_H #define CHGMIXING_H -#include "source_estate/module_charge/charge_mixing.h" +#include "source_estate/elecstate.h" // use pelec +#include "source_estate/module_charge/charge.h" // use chr +#include "source_estate/module_charge/charge_mixing.h" // use p_chgmix #include "source_io/module_parameter/input_parameter.h" // use Input_para +#include "source_cell/unitcell.h" namespace module_charge { +void chgmixing_ks(const int iter, // scf iteration number + UnitCell& ucell, + elecstate::ElecState* pelec, + Charge &chr, // charge density + Charge_Mixing* p_chgmix, // charge mixing class + const int nrxx, // charge density + double &drho, // charge density deviation + bool &oscillate_esolver, // whether the esolver has oscillation of charge density + bool &conv_esolver, + const double &hsolver_error, + const double &scf_thr, + const double &scf_ene_thr, + const Input_para& inp); // input parameters + void chgmixing_ks_pw(const int iter, Charge_Mixing* p_chgmix, const Input_para& inp); // input parameters From 5ade8bb118e14758b0a9a1570f1fbdf1fc363ce1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 12:02:46 +0800 Subject: [PATCH 06/81] small updates --- source/source_esolver/esolver_ks.cpp | 27 +++++-------------- .../source_estate/module_charge/chgmixing.cpp | 11 ++++++++ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index a9c6891384..1718f4b342 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -324,10 +324,8 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i elecstate::update_pot(ucell, this->pelec, this->chr, conv_esolver); // 3.1) calculate energies - // 1 means Harris-Foulkes functional - // 2 means Kohn-Sham functional - this->pelec->cal_energies(1); - this->pelec->cal_energies(2); + this->pelec->cal_energies(1); // Harris-Foulkes functional + this->pelec->cal_energies(2); // Kohn-Sham functional if (iter == 1) { @@ -343,15 +341,6 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i dkin = p_chgmix->get_dkin(&this->chr, PARAM.inp.nelec); } - // SCF restart information - if (PARAM.inp.mixing_restart > 0 - && iter == this->p_chgmix->mixing_restart_step - 1 - && iter != PARAM.inp.scf_nmax) - { - this->p_chgmix->mixing_restart_last = iter; - std::cout << " SCF restart after this step!" << std::endl; - } - // Iter finish ESolver_FP::iter_finish(ucell, istep, iter, conv_esolver); @@ -366,7 +355,7 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i #endif // print energies - elecstate::print_etot(ucell.magnet, *pelec,conv_esolver, iter, drho, + elecstate::print_etot(ucell.magnet, *pelec, conv_esolver, iter, drho, dkin, duration, diag_ethr); @@ -420,13 +409,9 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo ss << ".txt"; const double eshift = 0.0; - ModuleIO::nscf_band(is, - ss.str(), - PARAM.inp.nbands, - eshift, - PARAM.inp.out_band[1], // precision - this->pelec->ekb, - this->kv); + ModuleIO::nscf_band(is, ss.str(), PARAM.inp.nbands, + eshift, PARAM.inp.out_band[1], // precision + this->pelec->ekb, this->kv); } } } diff --git a/source/source_estate/module_charge/chgmixing.cpp b/source/source_estate/module_charge/chgmixing.cpp index 21f8a2ab32..4505774952 100644 --- a/source/source_estate/module_charge/chgmixing.cpp +++ b/source/source_estate/module_charge/chgmixing.cpp @@ -113,6 +113,17 @@ void module_charge::chgmixing_ks(const int iter, // scf iteration number MPI_Bcast(chr.rho[0], nrxx, MPI_DOUBLE, 0, BP_WORLD); #endif + // mohan move the following code here, 2025-10-18 + // SCF restart information + if (PARAM.inp.mixing_restart > 0 + && iter == this->p_chgmix->mixing_restart_step - 1 + && iter != PARAM.inp.scf_nmax) + { + this->p_chgmix->mixing_restart_last = iter; + std::cout << " SCF restart after this step!" << std::endl; + } + + return; } From d04e97d051774e187dfdd0db3995c1a125c0d5c1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 12:04:46 +0800 Subject: [PATCH 07/81] small bug fixed --- source/source_estate/module_charge/chgmixing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_estate/module_charge/chgmixing.cpp b/source/source_estate/module_charge/chgmixing.cpp index 4505774952..59467fc010 100644 --- a/source/source_estate/module_charge/chgmixing.cpp +++ b/source/source_estate/module_charge/chgmixing.cpp @@ -116,10 +116,10 @@ void module_charge::chgmixing_ks(const int iter, // scf iteration number // mohan move the following code here, 2025-10-18 // SCF restart information if (PARAM.inp.mixing_restart > 0 - && iter == this->p_chgmix->mixing_restart_step - 1 + && iter == p_chgmix->mixing_restart_step - 1 && iter != PARAM.inp.scf_nmax) { - this->p_chgmix->mixing_restart_last = iter; + p_chgmix->mixing_restart_last = iter; std::cout << " SCF restart after this step!" << std::endl; } From b8237aadab364f8ff5e891f777c78656775c1213 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 12:29:49 +0800 Subject: [PATCH 08/81] update E_bandgap to E_gap(k) --- source/source_estate/elecstate_print.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/source_estate/elecstate_print.cpp b/source/source_estate/elecstate_print.cpp index a04efc7563..4a5918f75e 100644 --- a/source/source_estate/elecstate_print.cpp +++ b/source/source_estate/elecstate_print.cpp @@ -278,14 +278,14 @@ void print_etot(const Magnetism& magnet, // print out the band gap if needed if (!PARAM.globalv.two_fermi) { - titles.push_back("E_bandgap"); + titles.push_back("E_gap(k)"); // gap of given k-points energies_Ry.push_back(elec.bandgap); } else { - titles.push_back("E_bandgap_up"); + titles.push_back("E_gap_up(k)"); energies_Ry.push_back(elec.bandgap_up); - titles.push_back("E_bandgap_dw"); + titles.push_back("E_gap_dw(k)"); energies_Ry.push_back(elec.bandgap_dw); } energies_eV.resize(energies_Ry.size()); From 266fbc22540a0ef75d87856e24f05d7eb461b0fb Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 14:31:59 +0800 Subject: [PATCH 09/81] delete rho_restart file --- source/source_esolver/rho_restart.cpp | 149 -------------------------- 1 file changed, 149 deletions(-) delete mode 100644 source/source_esolver/rho_restart.cpp diff --git a/source/source_esolver/rho_restart.cpp b/source/source_esolver/rho_restart.cpp deleted file mode 100644 index 3a8c4b40b5..0000000000 --- a/source/source_esolver/rho_restart.cpp +++ /dev/null @@ -1,149 +0,0 @@ -#include "rho_restart.h" - -/* -void ModuleESolver::rho_restart(const Input_para& inp, - const UnitCell& ucell, - const elecstate::ElecState& elec, - const int nrxx, // originally written as pw_rhod->nrxx - const int iter, // SCF iteration index - const double& scf_thr, - const double& scf_ene_thr, - double& drho, // not sure how this is changed in this function - Charge_Mixing& chr_mix, - Charge &chr, - bool &conv_esolver, - bool &oscillate_esolver) -{ - ModuleBase::TITLE("ModuleESolver", "rho_restart"); - - // mixing will restart once if drho is smaller than inp.mixing_restart - const double restart_thr = inp.mixing_restart; - - // ks_run means this is KSDFT - if (PARAM.globalv.ks_run) - { - //-------------------------------------------------------- - // step1: determine mixing_restart_step - //-------------------------------------------------------- - // charge mixing restarts at chgmix.mixing_restart steps - if (drho <= restart_thr - && restart_thr > 0.0 - && chgmix.mixing_restart_step > iter) - { - chgmix.mixing_restart_step = iter + 1; - } - - - //-------------------------------------------------------- - // step2: determine density oscillation - //-------------------------------------------------------- - // if density oscillation is detected, SCF will stop - if (inp.scf_os_stop) - { - oscillate_esolver = chgmix.if_scf_oscillate(iter, - drho, - inp.scf_os_ndim, - inp.scf_os_thr); - } - - //-------------------------------------------------------- - // step3: determine convergence of SCF: conv_esolver - //-------------------------------------------------------- - // drho will be 0 at the chgmix.mixing_restart step, - // which is not ground state - bool is_mixing_restart_step = (iter == chgmix.mixing_restart_step); - bool is_restart_thr_positive = (restart_thr > 0.0); - bool is_restart_condition_met = is_mixing_restart_step && is_restart_thr_positive; - bool not_restart_step =!is_restart_condition_met; - - // SCF will continue if U is not converged for uramping calculation - bool is_U_converged = true; - - // to avoid unnecessary dependence on dft+u, refactor is needed -#ifdef __LCAO - if (inp.dft_plus_u) - { - is_U_converged = GlobalC::dftu.u_converged(); - } -#endif - - conv_esolver = (drho < scf_thr && not_restart_step && is_U_converged); - - //-------------------------------------------------------- - // step4: determine conv_esolver if energy threshold is - // used in SCF - //-------------------------------------------------------- - if (scf_ene_thr > 0.0) - { - // calculate energy of output charge density - this->update_pot(ucell, istep, iter, conv_esolver); - - // '2' means Kohn-Sham functional - elec.cal_energies(2); - - // now, etot_old is the energy of input density, while etot is the energy of output density - elec.f_en.etot_delta = elec.f_en.etot - elec.f_en.etot_old; - - // output etot_delta - GlobalV::ofs_running << " DeltaE_womix = " - << elec.f_en.etot_delta * ModuleBase::Ry_to_eV << " eV" - << std::endl; - - // only check when density is converged - if (iter > 1 && conv_esolver == 1) - { - // update the convergence flag - conv_esolver - = (std::abs(elec.f_en.etot_delta * ModuleBase::Ry_to_eV) < scf_ene_thr); - } - } - - //-------------------------------------------------------- - // If drho < hsolver_error in the first iter or - // drho < scf_thr, we do nothing and do not change rho. - //-------------------------------------------------------- - if (drho < hsolver_error - || conv_esolver // SCF has been converged - || inp.calculation == "nscf") // nscf calculations, do not change rho - { - if (drho < hsolver_error) - { - GlobalV::ofs_warning << " drho < hsolver_error, keep " - "charge density unchanged." - << std::endl; - } - } - else - { - // mixing will restart after chgmix.mixing_restart steps - if (restart_thr > 0.0 - && iter == chgmix.mixing_restart_step - 1 - && drho <= restart_thr) - { - // do not mix charge density - } - else - { - // mix charge density (rho) - chgmix.mix_rho(&chr); - } - - // renormalize rho in R-space would induce error in G space - if (inp.scf_thr_type == 2) - { - chr.renormalize_rho(); - } - } - } - -#ifdef __MPI - // bcast drho in BP_WORLD (Band parallel world) - MPI_Bcast(&drho, 1, MPI_DOUBLE, 0, BP_WORLD); - - // be careful! conv_esolver is bool, not double !! Maybe a bug 20250302 by mohan - MPI_Bcast(&conv_esolver, 1, MPI_DOUBLE, 0, BP_WORLD); - MPI_Bcast(chr.rho[0], nrxx, MPI_DOUBLE, 0, BP_WORLD); -#endif - -} -*/ From 11d942721ef0793446dedad3d69a38c89da29158 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 16:49:18 +0800 Subject: [PATCH 10/81] change setup_parameters to print_parameters --- source/source_esolver/esolver_gets.cpp | 2 +- source/source_esolver/esolver_ks.cpp | 15 ++++--- source/source_esolver/esolver_of.cpp | 2 +- .../module_charge/charge_mixing.cpp | 15 ++++++- source/source_io/print_info.cpp | 45 ++++++++++--------- source/source_io/print_info.h | 6 ++- .../module_lr/esolver_lrtd_lcao.cpp | 2 +- 7 files changed, 53 insertions(+), 34 deletions(-) diff --git a/source/source_esolver/esolver_gets.cpp b/source/source_esolver/esolver_gets.cpp index 300fd532b2..d503876212 100644 --- a/source/source_esolver/esolver_gets.cpp +++ b/source/source_esolver/esolver_gets.cpp @@ -43,7 +43,7 @@ void ESolver_GetS::before_all_runners(UnitCell& ucell, const Input_para& inp) this->kv.set(ucell, ucell.symm, inp.kpoint_file, inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS"); - ModuleIO::setup_parameters(ucell, this->kv); + ModuleIO::print_parameters(ucell, this->kv, inp); // 2) init ElecState // autoset nbands in ElecState, it should before basis_init (for Psi 2d division) diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 1718f4b342..f83ff2cd4e 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -59,11 +59,11 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para classname = "ESolver_KS"; basisname = ""; - scf_thr = inp.scf_thr; - scf_ene_thr = inp.scf_ene_thr; - maxniter = inp.scf_nmax; - niter = maxniter; - drho = 0.0; + this->scf_thr = inp.scf_thr; + this->scf_ene_thr = inp.scf_ene_thr; + this->maxniter = inp.scf_nmax; + this->niter = maxniter; + this->drho = 0.0; std::string fft_device = inp.device; @@ -82,8 +82,9 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para #endif pw_wfc = new ModulePW::PW_Basis_K_Big(fft_device, fft_precision); - ModulePW::PW_Basis_K_Big* tmp = static_cast(pw_wfc); + // for LCAO calculations, we need to set bx, by, and bz + ModulePW::PW_Basis_K_Big* tmp = static_cast(pw_wfc); tmp->setbxyz(inp.bx, inp.by, inp.bz); //! 4) setup charge mixing @@ -117,7 +118,7 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS"); //! 9) print information - ModuleIO::setup_parameters(ucell, this->kv); + ModuleIO::print_parameters(ucell, this->kv, inp); //! 10) setup plane wave for electronic wave functions ModuleESolver::pw_setup(inp, ucell, *this->pw_rho, this->kv, *this->pw_wfc); diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 1b0fed366f..598ba93bc6 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -93,7 +93,7 @@ void ESolver_OF::before_all_runners(UnitCell& ucell, const Input_para& inp) // print information // mohan add 2021-01-30 - ModuleIO::setup_parameters(ucell, kv); + ModuleIO::print_parameters(ucell, kv, inp); // initialize the real-space uniform grid for FFT and parallel // distribution of plane waves diff --git a/source/source_estate/module_charge/charge_mixing.cpp b/source/source_estate/module_charge/charge_mixing.cpp index a236e3a76d..67e887a344 100644 --- a/source/source_estate/module_charge/charge_mixing.cpp +++ b/source/source_estate/module_charge/charge_mixing.cpp @@ -8,12 +8,23 @@ Charge_Mixing::Charge_Mixing() { + this->mixing = nullptr; + this->mixing_highf = nullptr; } Charge_Mixing::~Charge_Mixing() { - delete this->mixing; - delete this->mixing_highf; + if(this->mixing != nullptr) + { + delete this->mixing; + this->mixing = nullptr; + } + + if(this->mixing_highf != nullptr) + { + delete this->mixing_highf; + this->mixing_highf = nullptr; + } } void Charge_Mixing::set_mixing(const std::string& mixing_mode_in, diff --git a/source/source_io/print_info.cpp b/source/source_io/print_info.cpp index 4f46083030..528b65b86c 100644 --- a/source/source_io/print_info.cpp +++ b/source/source_io/print_info.cpp @@ -7,36 +7,39 @@ namespace ModuleIO { -void setup_parameters(UnitCell& ucell, K_Vectors& kv) +void print_parameters( + const UnitCell& ucell, + K_Vectors& kv, + const Input_para& inp) { - ModuleBase::TITLE("ModuleIO", "setup_parameters"); - - if(PARAM.inp.calculation=="scf" - || PARAM.inp.calculation=="relax" - || PARAM.inp.calculation=="cell-relax" - || PARAM.inp.calculation=="nscf" - || PARAM.inp.calculation=="get_pchg" - || PARAM.inp.calculation=="get_wf" - || PARAM.inp.calculation=="md") + ModuleBase::TITLE("ModuleIO", "print_parameters"); + + if(inp.calculation=="scf" + || inp.calculation=="relax" + || inp.calculation=="cell-relax" + || inp.calculation=="nscf" + || inp.calculation=="get_pchg" + || inp.calculation=="get_wf" + || inp.calculation=="md") { std::cout << " ---------------------------------------------------------" << std::endl; - if(PARAM.inp.calculation=="scf") + if(inp.calculation=="scf") { std::cout << " Self-consistent calculations for electrons" << std::endl; } - else if(PARAM.inp.calculation=="test") + else if(inp.calculation=="test") { std::cout << " Test run" << std::endl; } - if(PARAM.inp.calculation=="relax") + if(inp.calculation=="relax") { std::cout << " Ion relaxation calculations" << std::endl; } - if(PARAM.inp.calculation=="cell-relax") + if(inp.calculation=="cell-relax") { std::cout << " Cell relaxation calculations" << std::endl; } - if(PARAM.inp.calculation=="md") + if(inp.calculation=="md") { std::cout << " Molecular Dynamics simulations" << std::endl; @@ -79,12 +82,12 @@ void setup_parameters(UnitCell& ucell, K_Vectors& kv) << std::setw(12) << "PROCESSORS" << std::setw(12) << "THREADS"; - const bool orbinfo = (PARAM.inp.basis_type=="lcao" || PARAM.inp.basis_type=="lcao_in_pw" - || (PARAM.inp.basis_type=="pw" && PARAM.inp.init_wfc.substr(0, 3) == "nao")); + const bool orbinfo = (inp.basis_type=="lcao" || inp.basis_type=="lcao_in_pw" + || (inp.basis_type=="pw" && inp.init_wfc.substr(0, 3) == "nao")); if (orbinfo) { std::cout << std::setw(12) << "NBASE"; } std::cout << std::endl; - std::cout << " " << std::setw(8) << PARAM.inp.nspin; + std::cout << " " << std::setw(8) << inp.nspin; if(PARAM.globalv.gamma_only_local) { @@ -105,15 +108,15 @@ void setup_parameters(UnitCell& ucell, K_Vectors& kv) std::cout << " ---------------------------------------------------------" << std::endl; - if(PARAM.inp.basis_type == "lcao") + if(inp.basis_type == "lcao") { std::cout << " Use Systematically Improvable Atomic bases" << std::endl; } - else if(PARAM.inp.basis_type == "lcao_in_pw") + else if(inp.basis_type == "lcao_in_pw") { std::cout << " Expand Atomic bases into plane waves" << std::endl; } - else if(PARAM.inp.basis_type == "pw") + else if(inp.basis_type == "pw") { std::cout << " Use plane wave basis" << std::endl; } diff --git a/source/source_io/print_info.h b/source/source_io/print_info.h index 78672601c1..6c94d29530 100644 --- a/source/source_io/print_info.h +++ b/source/source_io/print_info.h @@ -15,7 +15,11 @@ namespace ModuleIO { // print out to screen about the readin parameters -void setup_parameters(UnitCell& ucell, K_Vectors& kv); +void print_parameters( + const UnitCell& ucell, + K_Vectors& kv, + const Input_para& inp); + void print_time(time_t& time_start, time_t& time_finish); void print_screen(const int& stress_step, const int& force_step, const int& istep); //! Print charge density using FFT diff --git a/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp b/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp index 6608acb150..3bc1dd4434 100644 --- a/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp +++ b/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp @@ -319,7 +319,7 @@ LR::ESolver_LR::ESolver_LR(const Input_para& inp, UnitCell& ucell) : inpu } this->kv.set(ucell,ucell.symm, PARAM.inp.kpoint_file, PARAM.inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS"); - ModuleIO::setup_parameters(ucell, this->kv); + ModuleIO::print_parameters(ucell, this->kv, inp); this->parameter_check(); From 4998c0c8cd3804debf5b17d7d6d60a26743a871a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 17:22:57 +0800 Subject: [PATCH 11/81] move setup_pw.cpp to setup_pwwfc.cpp in module_pwdft --- source/Makefile.Objects | 2 +- source/source_esolver/CMakeLists.txt | 1 - source/source_esolver/esolver_ks.cpp | 52 ++++-------- source/source_esolver/pw_setup.cpp | 52 ------------ source/source_esolver/pw_setup.h | 26 ------ source/source_pw/module_pwdft/CMakeLists.txt | 1 + source/source_pw/module_pwdft/setup_pwwfc.cpp | 80 +++++++++++++++++++ source/source_pw/module_pwdft/setup_pwwfc.h | 23 ++++++ 8 files changed, 121 insertions(+), 116 deletions(-) delete mode 100644 source/source_esolver/pw_setup.cpp delete mode 100644 source/source_esolver/pw_setup.h create mode 100644 source/source_pw/module_pwdft/setup_pwwfc.cpp create mode 100644 source/source_pw/module_pwdft/setup_pwwfc.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index e612261c6e..4d328386d9 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -274,7 +274,6 @@ OBJS_ESOLVER=esolver.o\ esolver_of_tool.o\ esolver_of_interface.o\ pw_others.o\ - pw_setup.o\ OBJS_ESOLVER_LCAO=esolver_ks_lcao.o\ esolver_ks_lcao_tddft.o\ @@ -725,6 +724,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ fp_energy.o\ setup_pot.o\ setup_pwrho.o\ + setup_pwwfc.o\ forces.o\ forces_us.o\ forces_nl.o\ diff --git a/source/source_esolver/CMakeLists.txt b/source/source_esolver/CMakeLists.txt index 939581df88..a16b9bd6a2 100644 --- a/source/source_esolver/CMakeLists.txt +++ b/source/source_esolver/CMakeLists.txt @@ -13,7 +13,6 @@ list(APPEND objects esolver_of_interface.cpp esolver_of_tool.cpp pw_others.cpp - pw_setup.cpp ) if(ENABLE_LCAO) list(APPEND objects diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index f83ff2cd4e..33d0db398c 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -1,5 +1,4 @@ #include "esolver_ks.h" -#include "pw_setup.h" // setup plane wave #include "source_base/timer.h" #include "source_base/global_variable.h" @@ -25,6 +24,7 @@ #include "source_estate/update_pot.h" // mohan add 20251016 #include "source_estate/module_charge/chgmixing.h" // mohan add 20251018 +#include "source_pw/module_pwdft/setup_pwwfc.h" // mohan add 20251018 namespace ModuleESolver { @@ -40,10 +40,12 @@ ESolver_KS::~ESolver_KS() // do not add any codes in this deconstructor funcion //**************************************************** delete this->psi; - delete this->pw_wfc; delete this->p_hamilt; delete this->p_chgmix; this->ppcell.release_memory(); + + // mohan add 2025-10-18, should be put int clean() function + pw::teardown_pwwfc(this->pw_wfc); } @@ -65,69 +67,47 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para this->niter = maxniter; this->drho = 0.0; - std::string fft_device = inp.device; - - //! 3) setup pw_wfc - // currently LCAO doesn't support GPU acceleration of FFT - if(inp.basis_type == "lcao") - { - fft_device = "cpu"; - } - std::string fft_precision = inp.precision; -#ifdef __ENABLE_FLOAT_FFTW - if (inp.cal_cond && inp.esolver_type == "sdft") - { - fft_precision = "mixing"; - } -#endif - - pw_wfc = new ModulePW::PW_Basis_K_Big(fft_device, fft_precision); - - // for LCAO calculations, we need to set bx, by, and bz - ModulePW::PW_Basis_K_Big* tmp = static_cast(pw_wfc); - tmp->setbxyz(inp.bx, inp.by, inp.bz); + // cell_factor + this->ppcell.cell_factor = inp.cell_factor; - //! 4) setup charge mixing + //! 3) setup charge mixing p_chgmix = new Charge_Mixing(); p_chgmix->set_rhopw(this->pw_rho, this->pw_rhod); - // cell_factor - this->ppcell.cell_factor = inp.cell_factor; - ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL"); - //! 5) setup Exc for the first element '0', because all elements have same exc + //! 4) setup Exc for the first element '0', because all elements have same exc XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); - //! 6) setup the charge mixing parameters + //! 5) setup the charge mixing parameters p_chgmix->set_mixing(inp.mixing_mode, inp.mixing_beta, inp.mixing_ndim, inp.mixing_gg0, inp.mixing_tau, inp.mixing_beta_mag, inp.mixing_gg0_mag, inp.mixing_gg0_min, inp.mixing_angle, inp.mixing_dmr, ucell.omega, ucell.tpiba); p_chgmix->init_mixing(); - //! 7) symmetry analysis should be performed every time the cell is changed + //! 6) symmetry analysis should be performed every time the cell is changed if (ModuleSymmetry::Symmetry::symm_flag == 1) { ucell.symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SYMMETRY"); } - //! 8) Setup the k points according to symmetry. + //! 7) Setup the k points according to symmetry. this->kv.set(ucell,ucell.symm, inp.kpoint_file, inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS"); - //! 9) print information + //! 8) print information ModuleIO::print_parameters(ucell, this->kv, inp); - //! 10) setup plane wave for electronic wave functions - ModuleESolver::pw_setup(inp, ucell, *this->pw_rho, this->kv, *this->pw_wfc); + //! 9) setup plane wave for electronic wave functions + pw::setup_pwwfc(inp, ucell, *this->pw_rho, this->kv, this->pw_wfc); - //! 11) parallel of FFT grid + //! 10) parallel of FFT grid Pgrid.init(this->pw_rhod->nx, this->pw_rhod->ny, this->pw_rhod->nz, this->pw_rhod->nplane, this->pw_rhod->nrxx, pw_big->nbz, pw_big->bz); - //! 12) calculate the structure factor + //! 11) calculate the structure factor this->sf.setup_structure_factor(&ucell, Pgrid, this->pw_rhod); } diff --git a/source/source_esolver/pw_setup.cpp b/source/source_esolver/pw_setup.cpp deleted file mode 100644 index 14fb66347e..0000000000 --- a/source/source_esolver/pw_setup.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "pw_setup.h" - -// To get POOL_WORLD -#include "source_base/parallel_comm.h" -// Print information -#include "source_io/print_info.h" - -void ModuleESolver::pw_setup(const Input_para& inp, - const UnitCell& ucell, - const ModulePW::PW_Basis& pw_rho, - K_Vectors& kv, - ModulePW::PW_Basis_K& pw_wfc) -{ - ModuleBase::TITLE("ModuleESolver", "pw_setup"); - - //! new plane wave basis, fft grids, etc. -#ifdef __MPI - pw_wfc.initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); -#endif - - pw_wfc.initgrids(inp.ref_cell_factor * ucell.lat0, - ucell.latvec, - pw_rho.nx, - pw_rho.ny, - pw_rho.nz); - - pw_wfc.initparameters(false, inp.ecutwfc, kv.get_nks(), kv.kvec_d.data()); - -#ifdef __MPI - if (inp.pw_seed > 0) - { - MPI_Allreduce(MPI_IN_PLACE, &pw_wfc.ggecut, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); - } - // qianrui add 2021-8-13 to make different kpar parameters can get the same - // results -#endif - - pw_wfc.fft_bundle.initfftmode(inp.fft_mode); - pw_wfc.setuptransform(); - - //! initialize the number of plane waves for each k point - for (int ik = 0; ik < kv.get_nks(); ++ik) - { - kv.ngk[ik] = pw_wfc.npwk[ik]; - } - - pw_wfc.collect_local_pw(inp.erf_ecut, inp.erf_height, inp.erf_sigma); - - ModuleIO::print_wfcfft(inp, pw_wfc, GlobalV::ofs_running); - -} - diff --git a/source/source_esolver/pw_setup.h b/source/source_esolver/pw_setup.h deleted file mode 100644 index 8f1cd86af4..0000000000 --- a/source/source_esolver/pw_setup.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef PW_SETUP_H -#define PW_SETUP_H - -//! Input parameters -#include "source_io/module_parameter/parameter.h" -//! Unit cell information -#include "source_cell/unitcell.h" -//! Plane wave basis -#include "source_basis/module_pw/pw_basis.h" -//! K points in Brillouin zone -#include "source_cell/klist.h" -//! Plane wave basis set for k points -#include "source_basis/module_pw/pw_basis_k.h" - -namespace ModuleESolver -{ - -void pw_setup(const Input_para& inp, - const UnitCell& ucell, - const ModulePW::PW_Basis& pw_rho, - K_Vectors& kv, - ModulePW::PW_Basis_K& pw_wfc); - -} - -#endif diff --git a/source/source_pw/module_pwdft/CMakeLists.txt b/source/source_pw/module_pwdft/CMakeLists.txt index fdfe37828a..f958fdc75e 100644 --- a/source/source_pw/module_pwdft/CMakeLists.txt +++ b/source/source_pw/module_pwdft/CMakeLists.txt @@ -14,6 +14,7 @@ list(APPEND objects operator_pw/exx_pw_pot.cpp setup_pot.cpp setup_pwrho.cpp + setup_pwwfc.cpp forces_nl.cpp forces_cc.cpp forces_scc.cpp diff --git a/source/source_pw/module_pwdft/setup_pwwfc.cpp b/source/source_pw/module_pwdft/setup_pwwfc.cpp new file mode 100644 index 0000000000..759178638c --- /dev/null +++ b/source/source_pw/module_pwdft/setup_pwwfc.cpp @@ -0,0 +1,80 @@ +#include "source_pw/module_pwdft/setup_pwwfc.h" // pw_wfc +#include "source_base/parallel_comm.h" // POOL_WORLD +#include "source_io/print_info.h" // print information + +void pw::teardown_pwwfc(ModulePW::PW_Basis_K* &pw_wfc) +{ + delete pw_wfc; +} + +void pw::setup_pwwfc(const Input_para& inp, + const UnitCell& ucell, + const ModulePW::PW_Basis& pw_rho, + K_Vectors& kv, + ModulePW::PW_Basis_K* &pw_wfc) +{ + ModuleBase::TITLE("pw", "pw_setup"); + + std::string fft_device = inp.device; + + //! setup pw_wfc + // currently LCAO doesn't support GPU acceleration of FFT + if(inp.basis_type == "lcao") + { + fft_device = "cpu"; + } + std::string fft_precision = inp.precision; +#ifdef __ENABLE_FLOAT_FFTW + if (inp.cal_cond && inp.esolver_type == "sdft") + { + fft_precision = "mixing"; + } +#endif + + pw_wfc = new ModulePW::PW_Basis_K_Big(fft_device, fft_precision); + + + // for LCAO calculations, we need to set bx, by, and bz + ModulePW::PW_Basis_K_Big* tmp = static_cast(pw_wfc); + tmp->setbxyz(inp.bx, inp.by, inp.bz); + + + + //! new plane wave basis, fft grids, etc. +#ifdef __MPI + pw_wfc->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD); +#endif + + pw_wfc->initgrids(inp.ref_cell_factor * ucell.lat0, + ucell.latvec, + pw_rho.nx, + pw_rho.ny, + pw_rho.nz); + + pw_wfc->initparameters(false, inp.ecutwfc, kv.get_nks(), kv.kvec_d.data()); + +#ifdef __MPI + if (inp.pw_seed > 0) + { + MPI_Allreduce(MPI_IN_PLACE, &pw_wfc->ggecut, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + } + // qianrui add 2021-8-13 to make different kpar parameters can get the same + // results +#endif + + pw_wfc->fft_bundle.initfftmode(inp.fft_mode); + pw_wfc->setuptransform(); + + //! initialize the number of plane waves for each k point + for (int ik = 0; ik < kv.get_nks(); ++ik) + { + kv.ngk[ik] = pw_wfc->npwk[ik]; + } + + pw_wfc->collect_local_pw(inp.erf_ecut, inp.erf_height, inp.erf_sigma); + + ModuleIO::print_wfcfft(inp, *pw_wfc, GlobalV::ofs_running); + + return; +} + diff --git a/source/source_pw/module_pwdft/setup_pwwfc.h b/source/source_pw/module_pwdft/setup_pwwfc.h new file mode 100644 index 0000000000..db39e28941 --- /dev/null +++ b/source/source_pw/module_pwdft/setup_pwwfc.h @@ -0,0 +1,23 @@ +#ifndef SETUP_PWWFC_H +#define SETUP_PWWFC_H + +#include "source_io/module_parameter/parameter.h" // input parameters +#include "source_cell/unitcell.h" // cell information +#include "source_cell/klist.h" // k-points +#include "source_basis/module_pw/pw_basis.h" // pw_rho +#include "source_basis/module_pw/pw_basis_k.h" // pw_wfc + +namespace pw +{ + +void teardown_pwwfc(ModulePW::PW_Basis_K* &pw_wfc); + +void setup_pwwfc(const Input_para& inp, + const UnitCell& ucell, + const ModulePW::PW_Basis& pw_rho, + K_Vectors& kv, + ModulePW::PW_Basis_K* &pw_wfc); + +} + +#endif From e31c3aafb1c0419714309bc595873be4240d4838 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 17:38:55 +0800 Subject: [PATCH 12/81] update esolver, small things --- source/source_esolver/esolver_ks_pw.cpp | 25 +++++++------------------ source/source_esolver/pw_others.cpp | 21 +++------------------ 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index a8e5d553b7..840e8968c0 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -22,8 +22,6 @@ #include "source_base/kernels/dsp/dsp_connector.h" #endif -//#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 @@ -52,7 +50,6 @@ ESolver_KS_PW::~ESolver_KS_PW() // mohan add 2025-10-12 this->stp.clean(); - } template @@ -140,7 +137,7 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) this->chr, this->locpp, this->ppcell, this->vsep_cell, this->stp.psi_t, this->p_hamilt, this->pw_wfc, this->pw_rhod, PARAM.inp); - + // setup psi (electronic wave functions) this->stp.init(this->p_hamilt); //! Exx calculations @@ -161,17 +158,17 @@ 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 + // 1) Call iter_init() of ESolver_KS ESolver_KS::iter_init(ucell, istep, iter); - // perform charge mixing + // 2) perform charge mixing for KSDFT using pw basis module_charge::chgmixing_ks_pw(iter, this->p_chgmix, PARAM.inp); - // mohan move harris functional to here, 2012-06-05 + // 3) mohan move harris functional 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 + // 4) update local occupations for DFT+U // should before lambda loop in DeltaSpin if (PARAM.inp.dft_plus_u && (iter != 1 || istep != 0)) { @@ -200,7 +197,6 @@ void ESolver_KS_PW::hamilt2rho_single(UnitCell& ucell, const int iste // be careful that istep start from 0 and iter start from 1 // if (iter == 1) hsolver::DiagoIterAssist::need_subspace = ((istep == 0 || istep == 1) && iter == 1) ? false : true; - hsolver::DiagoIterAssist::SCF_ITER = iter; hsolver::DiagoIterAssist::PW_DIAG_THR = ethr; @@ -248,15 +244,8 @@ void ESolver_KS_PW::hamilt2rho_single(UnitCell& ucell, const int iste hsolver::DiagoIterAssist::need_subspace, PARAM.inp.use_k_continuity); - hsolver_pw_obj.solve(this->p_hamilt, - this->stp.psi_t[0], - this->pelec, - this->pelec->ekb.c, - GlobalV::RANK_IN_POOL, - GlobalV::NPROC_IN_POOL, - skip_charge, - ucell.tpiba, - ucell.nat); + hsolver_pw_obj.solve(this->p_hamilt, this->stp.psi_t[0], this->pelec, this->pelec->ekb.c, + GlobalV::RANK_IN_POOL, GlobalV::NPROC_IN_POOL, skip_charge, ucell.tpiba, ucell.nat); } // symmetrize the charge density diff --git a/source/source_esolver/pw_others.cpp b/source/source_esolver/pw_others.cpp index 09550f075b..1542494400 100644 --- a/source/source_esolver/pw_others.cpp +++ b/source/source_esolver/pw_others.cpp @@ -1,5 +1,6 @@ #include "esolver_ks_pw.h" +/* #include "source_base/global_variable.h" #include "source_pw/module_pwdft/elecond.h" #include "source_io/input_conv.h" @@ -13,29 +14,13 @@ #include "source_hamilt/module_ewald/H_Ewald_pw.h" #include "source_pw/module_pwdft/global.h" #include "source_io/print_info.h" -//-----force------------------- -#include "source_pw/module_pwdft/forces.h" -//-----stress------------------ -#include "source_pw/module_pwdft/stress_pw.h" -//--------------------------------------------------- +*/ + #include "source_base/memory.h" #include "source_base/module_device/device.h" -#include "source_estate/elecstate_pw.h" -#include "source_hamilt/module_vdw/vdw.h" -#include "source_pw/module_pwdft/hamilt_pw.h" -#include "source_hsolver/diago_iter_assist.h" -#include "source_hsolver/hsolver_pw.h" -#include "source_hsolver/kernels/hegvd_op.h" -#include "source_base/kernels/math_kernel_op.h" -#include "source_io/berryphase.h" #include "source_io/numerical_basis.h" #include "source_io/numerical_descriptor.h" -#include "source_io/to_wannier90_pw.h" -#include "source_io/write_elecstat_pot.h" -#include "source_io/module_parameter/parameter.h" -#include -#include #include "source_base/formatter.h" // mohan add 2025-03-06 From 94292369d77e29798964faa814b66a301e45dbec Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 19:53:35 +0800 Subject: [PATCH 13/81] delete some old_gint codes in esolver --- source/source_esolver/esolver_ks_lcao.h | 2 - source/source_esolver/lcao_before_scf.cpp | 65 ++--------------------- source/source_esolver/lcao_others.cpp | 44 --------------- source/source_esolver/pw_others.cpp | 17 ------ 4 files changed, 5 insertions(+), 123 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.h b/source/source_esolver/esolver_ks_lcao.h index 67ca7a0482..6867521b5c 100644 --- a/source/source_esolver/esolver_ks_lcao.h +++ b/source/source_esolver/esolver_ks_lcao.h @@ -76,10 +76,8 @@ class ESolver_KS_LCAO : public ESolver_KS //! Grid integration: used to store some basic information Grid_Technique GridT; -#ifndef __OLD_GINT //! GintInfo: used to store some basic infomation about module_gint std::unique_ptr gint_info_; -#endif //! NAO orbitals: two-center integrations TwoCenterBundle two_center_bundle_; diff --git a/source/source_esolver/lcao_before_scf.cpp b/source/source_esolver/lcao_before_scf.cpp index cf09a63943..46025b9db6 100644 --- a/source/source_esolver/lcao_before_scf.cpp +++ b/source/source_esolver/lcao_before_scf.cpp @@ -41,70 +41,15 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) this->gd, ucell, search_radius, PARAM.inp.test_atom_input); //! 4) initialize NAO basis set -#ifdef __OLD_GINT - double dr_uniform = 0.001; - std::vector rcuts; - std::vector> psi_u; - std::vector> dpsi_u; - std::vector> d2psi_u; - - Gint_Tools::init_orb(dr_uniform, rcuts, ucell, orb_, psi_u, dpsi_u, d2psi_u); - - //! 5) set periodic boundary conditions - this->GridT.set_pbc_grid(this->pw_rho->nx, - this->pw_rho->ny, - this->pw_rho->nz, - this->pw_big->bx, - this->pw_big->by, - this->pw_big->bz, - this->pw_big->nbx, - this->pw_big->nby, - this->pw_big->nbz, - this->pw_big->nbxx, - this->pw_big->nbzp_start, - this->pw_big->nbzp, - this->pw_rho->ny, - this->pw_rho->nplane, - this->pw_rho->startz_current, - ucell, - this->gd, - dr_uniform, - rcuts, - psi_u, - dpsi_u, - d2psi_u, - PARAM.inp.nstream); - - psi_u.clear(); - psi_u.shrink_to_fit(); - dpsi_u.clear(); - dpsi_u.shrink_to_fit(); - d2psi_u.clear(); - d2psi_u.shrink_to_fit(); - LCAO_domain::grid_prepare(this->GridT, this->GG, this->GK, ucell, orb_, *this->pw_rho, *this->pw_big); - - //! 6) prepare grid integral -#else // here new is a unique pointer, which will be deleted automatically gint_info_.reset( new ModuleGint::GintInfo( - this->pw_big->nbx, - this->pw_big->nby, - this->pw_big->nbz, - this->pw_rho->nx, - this->pw_rho->ny, - this->pw_rho->nz, - 0, - 0, - this->pw_big->nbzp_start, - this->pw_big->nbx, - this->pw_big->nby, - this->pw_big->nbzp, - orb_.Phi, - ucell, - this->gd)); + this->pw_big->nbx, this->pw_big->nby, this->pw_big->nbz, + this->pw_rho->nx, this->pw_rho->ny, this->pw_rho->nz, + 0, 0, this->pw_big->nbzp_start, + this->pw_big->nbx, this->pw_big->nby, this->pw_big->nbzp, + orb_.Phi, ucell, this->gd)); ModuleGint::Gint::set_gint_info(gint_info_.get()); -#endif // 7) For each atom, calculate the adjacent atoms in different cells // and allocate the space for H(R) and S(R). diff --git a/source/source_esolver/lcao_others.cpp b/source/source_esolver/lcao_others.cpp index 98cababdd2..dea6d52b88 100644 --- a/source/source_esolver/lcao_others.cpp +++ b/source/source_esolver/lcao_others.cpp @@ -8,8 +8,6 @@ #include "source_io/berryphase.h" #include "source_io/get_pchg_lcao.h" #include "source_io/get_wf_lcao.h" -#include "source_io/to_wannier90_lcao.h" -#include "source_io/to_wannier90_lcao_in_pw.h" #include "source_io/write_HS_R.h" #include "source_io/module_parameter/parameter.h" #include "source_base/timer.h" @@ -92,47 +90,6 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) PARAM.inp.test_atom_input); // (3) Periodic condition search for each grid. -#ifdef __OLD_GINT - double dr_uniform = 0.001; - std::vector rcuts; - std::vector> psi_u; - std::vector> dpsi_u; - std::vector> d2psi_u; - - Gint_Tools::init_orb(dr_uniform, rcuts, ucell, orb_, psi_u, dpsi_u, d2psi_u); - this->GridT.set_pbc_grid(this->pw_rho->nx, - this->pw_rho->ny, - this->pw_rho->nz, - this->pw_big->bx, - this->pw_big->by, - this->pw_big->bz, - this->pw_big->nbx, - this->pw_big->nby, - this->pw_big->nbz, - this->pw_big->nbxx, - this->pw_big->nbzp_start, - this->pw_big->nbzp, - this->pw_rho->ny, - this->pw_rho->nplane, - this->pw_rho->startz_current, - ucell, - this->gd, - dr_uniform, - rcuts, - psi_u, - dpsi_u, - d2psi_u, - PARAM.inp.nstream); - - psi_u.clear(); - psi_u.shrink_to_fit(); - dpsi_u.clear(); - dpsi_u.shrink_to_fit(); - d2psi_u.clear(); - d2psi_u.shrink_to_fit(); - // prepare grid in Gint - LCAO_domain::grid_prepare(this->GridT, this->GG, this->GK, ucell, orb_, *this->pw_rho, *this->pw_big); -#else gint_info_.reset( new ModuleGint::GintInfo( this->pw_big->nbx, @@ -151,7 +108,6 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) ucell, this->gd)); ModuleGint::Gint::set_gint_info(gint_info_.get()); -#endif // (2)For each atom, calculate the adjacent atoms in different cells // and allocate the space for H(R) and S(R). diff --git a/source/source_esolver/pw_others.cpp b/source/source_esolver/pw_others.cpp index 1542494400..38c3c927b0 100644 --- a/source/source_esolver/pw_others.cpp +++ b/source/source_esolver/pw_others.cpp @@ -1,21 +1,4 @@ #include "esolver_ks_pw.h" - -/* -#include "source_base/global_variable.h" -#include "source_pw/module_pwdft/elecond.h" -#include "source_io/input_conv.h" -#include "source_io/output_log.h" - -#include - -//--------------temporary---------------------------- -#include "source_estate/module_charge/symmetry_rho.h" -#include "source_estate/occupy.h" -#include "source_hamilt/module_ewald/H_Ewald_pw.h" -#include "source_pw/module_pwdft/global.h" -#include "source_io/print_info.h" -*/ - #include "source_base/memory.h" #include "source_base/module_device/device.h" #include "source_io/numerical_basis.h" From 78829e98ac87d8b695dd83341352fddf9528b205 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 20:48:53 +0800 Subject: [PATCH 14/81] fix bugs, update outputs --- source/Makefile.Objects | 2 +- source/source_esolver/esolver_ks.cpp | 38 +++------------- source/source_io/CMakeLists.txt | 2 +- .../{nscf_band.cpp => write_bands.cpp} | 44 ++++++++++++++++--- .../source_io/{nscf_band.h => write_bands.h} | 14 ++++-- 5 files changed, 59 insertions(+), 41 deletions(-) rename source/source_io/{nscf_band.cpp => write_bands.cpp} (80%) rename source/source_io/{nscf_band.h => write_bands.h} (74%) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 4d328386d9..fdb165c3c3 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -546,7 +546,7 @@ OBJS_IO=input_conv.o\ cal_test.o\ write_dos_pw.o\ nscf_fermi_surf.o\ - nscf_band.o\ + write_bands.o\ cal_dos.o\ cal_pdos_gamma.o\ cal_pdos_multik.o\ diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 33d0db398c..89aa970cad 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -12,8 +12,7 @@ #include "source_hsolver/hsolver.h" #include "source_io/cube_io.h" -// for NSCF calculations of band structures -#include "source_io/nscf_band.h" +#include "source_io/write_bands.h" // for output log information #include "source_io/output_log.h" #include "source_io/print_info.h" @@ -284,13 +283,13 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i } // 1.2) print out eigenvalues and occupations - if(iter % PARAM.inp.out_freq_elec == 0) - { - if (PARAM.inp.out_band[0] || iter == PARAM.inp.scf_nmax || conv_esolver) + if (PARAM.inp.out_band[0]) + { + if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver) { ModuleIO::write_eig_iter(this->pelec->ekb,this->pelec->wg,*this->pelec->klist); } - } + } // 2.1) compute magnetization, only for spin==2 ucell.magnet.compute_mag(ucell.omega, this->chr.nrxx, this->chr.nxyz, this->chr.rho, @@ -369,32 +368,9 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo // 3) write eigenvalues and occupations to eig_occ.txt ModuleIO::write_eig_file(this->pelec->ekb, this->pelec->wg, this->kv, istep); - // 3) write band information to band.txt - if (PARAM.inp.out_band[0]) - { - const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; - for (int is = 0; is < nspin0; is++) - { - std::stringstream ss; - ss << PARAM.globalv.global_out_dir << "band"; + // 4) write band information to band.txt + ModuleIO::write_bands(PARAM.inp, this->pelec->ekb, this->kv); - if(nspin0==1) - { - // do nothing - } - else if(nspin0==2) - { - ss << "s" << is + 1; - } - - ss << ".txt"; - - const double eshift = 0.0; - ModuleIO::nscf_band(is, ss.str(), PARAM.inp.nbands, - eshift, PARAM.inp.out_band[1], // precision - this->pelec->ekb, this->kv); - } - } } template diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index 850a124402..8b1f6e9b89 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -9,7 +9,7 @@ list(APPEND objects cal_mlkedf_descriptors.cpp cif_io.cpp write_dos_pw.cpp - nscf_band.cpp + write_bands.cpp nscf_fermi_surf.cpp write_eig_occ.cpp numerical_basis.cpp diff --git a/source/source_io/nscf_band.cpp b/source/source_io/write_bands.cpp similarity index 80% rename from source/source_io/nscf_band.cpp rename to source/source_io/write_bands.cpp index b23c135f5d..067d50d1f1 100644 --- a/source/source_io/nscf_band.cpp +++ b/source/source_io/write_bands.cpp @@ -1,4 +1,4 @@ -#include "nscf_band.h" +#include "write_bands.h" #include "source_base/global_function.h" #include "source_base/global_variable.h" #include "source_base/timer.h" @@ -10,7 +10,41 @@ #include #endif -void ModuleIO::nscf_band( + +void ModuleIO::write_bands(const Input_para& inp, + const ModuleBase::matrix& ekb, + const K_Vectors& kv) +{ + // write band information to band.txt + if (inp.out_band[0]) + { + const int nspin0 = (inp.nspin == 2) ? 2 : 1; + for (int is = 0; is < nspin0; is++) + { + std::stringstream ss; + ss << PARAM.globalv.global_out_dir << "band"; + + if(nspin0==1) + { + // do nothing + } + else if(nspin0==2) + { + ss << "s" << is + 1; + } + + ss << ".txt"; + + const double eshift = 0.0; + nscf_bands(is, ss.str(), inp.nbands, eshift, + inp.out_band[1], // precision + ekb, kv); + } + } +} + + +void ModuleIO::nscf_bands( const int &is, const std::string &eig_file, const int &nband, @@ -19,8 +53,8 @@ void ModuleIO::nscf_band( const ModuleBase::matrix& ekb, const K_Vectors& kv) { - ModuleBase::TITLE("ModuleIO","nscf_band"); - ModuleBase::timer::tick("ModuleIO", "nscf_band"); + ModuleBase::TITLE("ModuleIO","nscf_bands"); + ModuleBase::timer::tick("ModuleIO", "nscf_bands"); assert(precision>0); @@ -34,7 +68,7 @@ void ModuleIO::nscf_band( GlobalV::ofs_running << "\n"; */ - GlobalV::ofs_running << " Write eigenvalues data for plot to file: " << eig_file << std::endl; + GlobalV::ofs_running << " Write eigenvalues to file: " << eig_file << std::endl; // number of k points without spin; // nspin = 1,2, nkstot = nkstot_np * nspin; diff --git a/source/source_io/nscf_band.h b/source/source_io/write_bands.h similarity index 74% rename from source/source_io/nscf_band.h rename to source/source_io/write_bands.h index e458a0aba6..bc1c6942b9 100644 --- a/source/source_io/nscf_band.h +++ b/source/source_io/write_bands.h @@ -1,11 +1,19 @@ -#ifndef NSCF_BAND_H -#define NSCF_BAND_H +#ifndef WRITE_BANDS_H +#define WRITE_BANDS_H + +#include "source_io/module_parameter/input_parameter.h" #include "source_base/matrix.h" #include "source_cell/klist.h" #include "source_cell/parallel_kpoints.h" namespace ModuleIO { + + +void write_bands(const Input_para& inp, + const ModuleBase::matrix& ekb, + const K_Vectors& kv); + /** * @brief calculate the band structure * @@ -17,7 +25,7 @@ namespace ModuleIO * @param ekb eigenvalues of k points and bands * @param kv klist */ -void nscf_band(const int& is, +void nscf_bands(const int& is, const std::string &eig_file, const int& nband, const double& fermie, From 5463571b483968f194352d458f02caba5c24a6d4 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 21:07:51 +0800 Subject: [PATCH 15/81] fix bug in tests --- source/source_io/test/print_info_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/source_io/test/print_info_test.cpp b/source/source_io/test/print_info_test.cpp index af10f8fe3d..cdb64dfc4e 100644 --- a/source/source_io/test/print_info_test.cpp +++ b/source/source_io/test/print_info_test.cpp @@ -33,7 +33,7 @@ bool berryphase::berry_phase_flag=false; /** * - Tested Functions: - * - setup_parameters() + * - print_parameters() * - setup calculation parameters */ @@ -73,7 +73,7 @@ TEST_F(PrintInfoTest, SetupParameters) PARAM.sys.gamma_only_local = false; PARAM.input.calculation = cal_type[i]; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ModuleIO::setup_parameters(*ucell, *kv)); + EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv)); output = testing::internal::GetCapturedStdout(); if(PARAM.input.calculation == "scf") { @@ -96,7 +96,7 @@ TEST_F(PrintInfoTest, SetupParameters) { PARAM.input.mdp.md_type = md_types[j]; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ModuleIO::setup_parameters(*ucell, *kv)); + EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv)); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output,testing::HasSubstr("Molecular Dynamics simulations")); if (PARAM.mdp.md_type == "fire") @@ -131,7 +131,7 @@ TEST_F(PrintInfoTest, SetupParameters) { PARAM.input.basis_type = basis_type[i]; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ModuleIO::setup_parameters(*ucell, *kv)); + EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv)); output = testing::internal::GetCapturedStdout(); if(PARAM.input.basis_type == "lcao") { From 6f095fb0126539b808f986be5aa30a1e5cf5dea1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 22:48:50 +0800 Subject: [PATCH 16/81] change efermi to Efermi in fp_energy.h --- source/Makefile.Objects | 1 + source/source_esolver/esolver_fp.cpp | 62 +---------- source/source_esolver/esolver_ks.cpp | 17 +-- source/source_estate/elecstate.h | 2 +- source/source_estate/elecstate_tools.cpp | 4 +- source/source_estate/elecstate_tools.h | 4 +- source/source_estate/fp_energy.cpp | 8 +- source/source_estate/fp_energy.h | 2 +- source/source_estate/module_charge/charge.h | 29 ++--- .../module_charge/charge_init.cpp | 2 +- source/source_io/CMakeLists.txt | 1 + source/source_io/cal_dos.cpp | 2 +- source/source_io/cal_dos.h | 2 +- source/source_io/write_dos_lcao.cpp | 6 +- source/source_io/write_dos_lcao.h | 4 +- source/source_io/write_dos_pw.cpp | 2 +- source/source_io/write_dos_pw.h | 2 +- source/source_io/write_init.cpp | 103 ++++++++++++++++++ source/source_io/write_init.h | 31 ++++++ 19 files changed, 184 insertions(+), 100 deletions(-) create mode 100644 source/source_io/write_init.cpp create mode 100644 source/source_io/write_init.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index fdb165c3c3..a55c6206be 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -579,6 +579,7 @@ OBJS_IO=input_conv.o\ write_elecstat_pot.o\ write_elf.o\ write_dipole.o\ + write_init.o\ td_current_io.o\ write_libxc_r.o\ output_log.o\ diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index 2084f589dc..efe6990192 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -13,10 +13,11 @@ #include "source_io/print_info.h" #include "source_io/rhog_io.h" #include "source_io/module_parameter/parameter.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 +#include "source_io/ctrl_output_fp.h" +#include "source_io/write_init.h" // write_chg_init, write_pot_init namespace ModuleESolver { @@ -146,63 +147,8 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) elecstate::cal_ux(ucell); //! output the initial charge density - const int nspin = PARAM.inp.nspin; - if (PARAM.inp.out_chg[0] == 2) - { - for (int is = 0; is < nspin; is++) - { - std::stringstream ss; - ss << PARAM.globalv.global_out_dir << "chg"; - - if(nspin==1) - { - ss << "ini.cube"; - } - else if(nspin==2 || nspin==4) - { - ss << "s" << is + 1 << "ini.cube"; - } - - ModuleIO::write_vdata_palgrid(this->Pgrid, - this->chr.rho[is], - is, - nspin, - istep, - ss.str(), - this->pelec->eferm.ef, - &(ucell)); - } - } - - //! output total local potential of the initial charge density - if (PARAM.inp.out_pot == 3) - { - for (int is = 0; is < nspin; is++) - { - std::stringstream ss; - ss << PARAM.globalv.global_out_dir << "pot"; - - if(nspin==1) - { - ss << "ini.cube"; - } - else if(nspin==2 || nspin==4) - { - ss << "s" << is + 1 << "ini.cube"; - } - - ModuleIO::write_vdata_palgrid(this->Pgrid, - this->pelec->pot->get_effective_v(is), - is, - nspin, - istep, - ss.str(), - 0.0, // efermi - &(ucell), - 11, // precsion - 0); // out_fermi - } - } + ModuleIO::write_chg_init(ucell, this->Pgrid, this->chr, this->pelec->eferm, istep, PARAM.inp); + ModuleIO::write_pot_init(ucell, this->Pgrid, this->pelec, istep, PARAM.inp); return; } diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 89aa970cad..477f33dcd2 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -1,5 +1,6 @@ #include "esolver_ks.h" +/* #include "source_base/timer.h" #include "source_base/global_variable.h" #include "source_pw/module_pwdft/global.h" @@ -7,16 +8,9 @@ #include "source_lcao/module_dftu/dftu.h" #include "source_cell/cal_atoms_info.h" -#include "source_estate/elecstate_print.h" -#include "source_hamilt/module_xc/xc_functional.h" -#include "source_hsolver/hsolver.h" #include "source_io/cube_io.h" +*/ -#include "source_io/write_bands.h" -// for output log information -#include "source_io/output_log.h" -#include "source_io/print_info.h" -#include "source_io/write_eig_occ.h" // for jason output information #include "source_io/json_output/init_info.h" #include "source_io/json_output/output_info.h" @@ -24,6 +18,13 @@ #include "source_estate/update_pot.h" // mohan add 20251016 #include "source_estate/module_charge/chgmixing.h" // mohan add 20251018 #include "source_pw/module_pwdft/setup_pwwfc.h" // mohan add 20251018 +#include "source_hsolver/hsolver.h" +#include "source_io/write_eig_occ.h" +#include "source_io/write_bands.h" +#include "source_hamilt/module_xc/xc_functional.h" +#include "source_io/output_log.h" // use write_head +#include "source_estate/elecstate_print.h" // print_etot +#include "source_io/print_info.h" // print_parameters namespace ModuleESolver { diff --git a/source/source_estate/elecstate.h b/source/source_estate/elecstate.h index 7b1ff8fbcc..a59d2bf4c3 100644 --- a/source/source_estate/elecstate.h +++ b/source/source_estate/elecstate.h @@ -150,7 +150,7 @@ class ElecState double get_local_pp_energy(); fenergy f_en; ///< energies contribute to the total free energy - efermi eferm; ///< fermi energies + Efermi eferm; ///< fermi energies // below defines the bandgap: diff --git a/source/source_estate/elecstate_tools.cpp b/source/source_estate/elecstate_tools.cpp index 446007a296..4a4c24e3cf 100644 --- a/source/source_estate/elecstate_tools.cpp +++ b/source/source_estate/elecstate_tools.cpp @@ -29,7 +29,7 @@ namespace elecstate void calculate_weights(const ModuleBase::matrix& ekb, ModuleBase::matrix& wg, const K_Vectors* klist, - efermi& eferm, + Efermi& eferm, fenergy& f_en, std::vector& nelec_spin, const bool skip_weights=false) @@ -179,4 +179,4 @@ namespace elecstate skip_weights = true; } -} \ No newline at end of file +} diff --git a/source/source_estate/elecstate_tools.h b/source/source_estate/elecstate_tools.h index 6d144ab91b..1486c41452 100644 --- a/source/source_estate/elecstate_tools.h +++ b/source/source_estate/elecstate_tools.h @@ -10,7 +10,7 @@ void calEBand(const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, fener void calculate_weights(const ModuleBase::matrix& ekb, ModuleBase::matrix& wg, const K_Vectors* klist, - efermi& eferm, + Efermi& eferm, fenergy& f_en, std::vector& nelec_spin, const bool skip_weights); @@ -23,4 +23,4 @@ void fixed_weights(const std::vector& ocp_kb, bool& skip_weights); } // namespace elecstate -#endif \ No newline at end of file +#endif diff --git a/source/source_estate/fp_energy.cpp b/source/source_estate/fp_energy.cpp index b84d9a4a52..8c11a3fac1 100644 --- a/source/source_estate/fp_energy.cpp +++ b/source/source_estate/fp_energy.cpp @@ -61,10 +61,10 @@ void fenergy::print_all() const std::cout << " total= " << etot << std::endl; } -/// @brief set efermi of a specific spin +/// @brief set Efermi of a specific spin /// @param is SPIN /// @param ef_in fermi(is) -void efermi::set_efval(const int& is, const double& ef_in) +void Efermi::set_efval(const int& is, const double& ef_in) { if (!two_efermi) { @@ -88,7 +88,7 @@ void efermi::set_efval(const int& is, const double& ef_in) /// @brief get the value of fermi of a specific spin /// @param is SPIN /// @return value of fermi(is) -double efermi::get_efval(const int& is) const +double Efermi::get_efval(const int& is) const { if (!two_efermi) { @@ -111,7 +111,7 @@ double efermi::get_efval(const int& is) const /// @brief get all fermi energies for all spins /// @return all fermi energies for all spins -std::vector efermi::get_all_ef() const +std::vector Efermi::get_all_ef() const { if (two_efermi) { diff --git a/source/source_estate/fp_energy.h b/source/source_estate/fp_energy.h index 78a3085ed2..4d6ff36d5f 100644 --- a/source/source_estate/fp_energy.h +++ b/source/source_estate/fp_energy.h @@ -59,7 +59,7 @@ struct fenergy * @struct efermi * @brief Fermi energies */ -struct efermi +struct Efermi { double ef = 0.0; ///< Fermi energy double ef_up = 0.0; ///< spin up Fermi energy diff --git a/source/source_estate/module_charge/charge.h b/source/source_estate/module_charge/charge.h index e943e662a9..479eb8dc66 100644 --- a/source/source_estate/module_charge/charge.h +++ b/source/source_estate/module_charge/charge.h @@ -13,13 +13,12 @@ //a forward declaration of UnitCell class UnitCell; -//========================================================== // Electron Charge Density -//========================================================== class Charge { public: + Charge(); ~Charge(); @@ -43,8 +42,8 @@ class Charge double **kin_r = nullptr; // kinetic energy density in real space, for meta-GGA double **kin_r_save = nullptr; // kinetic energy density in real space, for meta-GGA - // wenfei 2021-07-28 const Parallel_Grid* pgrid = nullptr; + private: //temporary @@ -56,6 +55,7 @@ class Charge double *_space_kin_r_save = nullptr; public: + double **nhat = nullptr; //compensation charge for PAW double **nhat_save = nullptr; //compensation charge for PAW // wenfei 2023-09-05 @@ -77,7 +77,7 @@ class Charge * @param klist [in] k points list if needed * @param wfcpw [in] PW basis for wave function if needed */ - void init_rho(elecstate::efermi& eferm_iout, + void init_rho(elecstate::Efermi& eferm_iout, const UnitCell& ucell, const Parallel_Grid& pgrid, const ModuleBase::ComplexMatrix& strucFac, @@ -96,6 +96,7 @@ class Charge void set_rho_core(const UnitCell& ucell, const ModuleBase::ComplexMatrix& structure_factor, const bool* numeric); + void set_rho_core_paw(); void renormalize_rho(); @@ -119,14 +120,13 @@ class Charge double cal_rho2ne(const double *rho_in) const; - void check_rho(); // to check whether the charge density is normal + void check_rho(); // to check whether the charge density is normal - void init_final_scf(); //LiuXh add 20180619 + void init_final_scf(); //LiuXh add 20180619 public: /** * @brief init some arrays for mpi_inter_pools, rho_mpi - * */ void init_chgmpi(); @@ -136,14 +136,14 @@ class Charge */ void rho_mpi(); - /** - * @brief Reduce among different pools + /** + * @brief Reduce among different pools * If NPROC_IN_POOLs are all the same, use GlobalV::KP_WORLD * else, gather rho in a POOL, and then reduce among different POOLs - * - * @param array_rho f(rho): an array [nrxx] - */ - void reduce_diff_pools(double* array_rho) const; + * + * @param array_rho f(rho): an array [nrxx] + */ + void reduce_diff_pools(double* array_rho) const; void set_omega(double* omega_in){this->omega_ = omega_in;}; @@ -154,6 +154,7 @@ class Charge int nspin=0; // number of spins ModulePW::PW_Basis* rhopw = nullptr;// When double_grid is used, rhopw = rhodpw (dense grid) bool cal_elf = false; // whether to calculate electron localization function (ELF) + private: void destroy(); // free arrays liuyu 2023-03-12 @@ -163,8 +164,8 @@ class Charge bool allocate_rho; bool allocate_rho_final_scf; // LiuXh add 20180606 + #ifdef __MPI - private: int *rec = nullptr; //The number of elements each process should receive into the receive buffer. int *dis = nullptr; //The displacement (relative to recvbuf) for each process in the receive buffer. #endif diff --git a/source/source_estate/module_charge/charge_init.cpp b/source/source_estate/module_charge/charge_init.cpp index 5294a4d376..b3a034c3d2 100644 --- a/source/source_estate/module_charge/charge_init.cpp +++ b/source/source_estate/module_charge/charge_init.cpp @@ -18,7 +18,7 @@ #include "source_io/rhog_io.h" #include "source_io/read_wf2rho_pw.h" -void Charge::init_rho(elecstate::efermi& eferm_iout, +void Charge::init_rho(elecstate::Efermi& eferm_iout, const UnitCell& ucell, const Parallel_Grid& pgrid, const ModuleBase::ComplexMatrix& strucFac, diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index 8b1f6e9b89..34a9e7092a 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -30,6 +30,7 @@ list(APPEND objects write_elecstat_pot.cpp write_elf.cpp write_dipole.cpp + write_init.cpp write_mlkedf_descriptors.cpp td_current_io.cpp write_libxc_r.cpp diff --git a/source/source_io/cal_dos.cpp b/source/source_io/cal_dos.cpp index 6c109f06d2..6eb53aac8f 100644 --- a/source/source_io/cal_dos.cpp +++ b/source/source_io/cal_dos.cpp @@ -7,7 +7,7 @@ #include "source_io/module_parameter/parameter.h" void ModuleIO::prepare_dos(std::ofstream& ofs_running, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const ModuleBase::matrix& ekb, const int nks, const int nbands, diff --git a/source/source_io/cal_dos.h b/source/source_io/cal_dos.h index 569d038df2..c56891f6db 100644 --- a/source/source_io/cal_dos.h +++ b/source/source_io/cal_dos.h @@ -9,7 +9,7 @@ namespace ModuleIO { void prepare_dos(std::ofstream& ofs_running, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const ModuleBase::matrix& ekb, const int nks, const int nbands, diff --git a/source/source_io/write_dos_lcao.cpp b/source/source_io/write_dos_lcao.cpp index eae8e231c6..228207d7e2 100644 --- a/source/source_io/write_dos_lcao.cpp +++ b/source/source_io/write_dos_lcao.cpp @@ -16,7 +16,7 @@ void write_dos_lcao( const UnitCell& ucell, const K_Vectors& kv, const int nbands, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, const double& dos_edelta_ev, @@ -113,7 +113,7 @@ template void write_dos_lcao( const UnitCell& ucell, const K_Vectors& kv, const int nbands, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, const double& dos_edelta_ev, @@ -131,7 +131,7 @@ template void write_dos_lcao( const UnitCell& ucell, const K_Vectors& kv, const int nbands, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, const double& dos_edelta_ev, diff --git a/source/source_io/write_dos_lcao.h b/source/source_io/write_dos_lcao.h index 73d21a22a4..88b186f3f9 100644 --- a/source/source_io/write_dos_lcao.h +++ b/source/source_io/write_dos_lcao.h @@ -6,7 +6,7 @@ #include "source_psi/psi.h" // use psi::Psi #include "source_hamilt/hamilt.h" // use hamilt::Hamilt #include "source_basis/module_ao/parallel_orbitals.h" // use Parallel_Orbitals -#include "source_estate/fp_energy.h" // use elecstate::efermi +#include "source_estate/fp_energy.h" // use elecstate::Efermi namespace ModuleIO @@ -22,7 +22,7 @@ namespace ModuleIO const UnitCell& ucell, // Unit cell information const K_Vectors& kv, // k-point information in Brillouin zone const int nbands, // Number of bands - const elecstate::efermi &energy_fermi, // Fermi energy + const elecstate::Efermi &energy_fermi, // Fermi energy const ModuleBase::matrix& ekb, // Eigenvalues per k-point and band const ModuleBase::matrix& wg, // Weights of eigenvalues const double& dos_edelta_ev, // Delta energy diff --git a/source/source_io/write_dos_pw.cpp b/source/source_io/write_dos_pw.cpp index 7f5d2be88d..b766358584 100644 --- a/source/source_io/write_dos_pw.cpp +++ b/source/source_io/write_dos_pw.cpp @@ -11,7 +11,7 @@ void ModuleIO::write_dos_pw( const K_Vectors& kv, const int nbands, const int istep_in, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const double& dos_edelta_ev, const double& dos_scale, const double& bcoeff, diff --git a/source/source_io/write_dos_pw.h b/source/source_io/write_dos_pw.h index 7b50c0ac57..44f507d84e 100644 --- a/source/source_io/write_dos_pw.h +++ b/source/source_io/write_dos_pw.h @@ -16,7 +16,7 @@ namespace ModuleIO const K_Vectors& kv, const int nbands, const int istep_in, - const elecstate::efermi &energy_fermi, + const elecstate::Efermi &energy_fermi, const double &dos_edelta_ev, const double &dos_scale, const double &bcoeff, diff --git a/source/source_io/write_init.cpp b/source/source_io/write_init.cpp new file mode 100644 index 0000000000..5d1e6b8322 --- /dev/null +++ b/source/source_io/write_init.cpp @@ -0,0 +1,103 @@ +#include "source_io/write_init.h" +#include "source_io/cube_io.h" + +void write_chg_init( + const UnitCell& ucell, + const Parallel_Grid ¶_grid, + const Charge &chr, + const elecstate::Efermi &efermi, + const int istep, + const Input_para& inp) +{ + const int nspin = inp.nspin; + assert(nspin == 1 || nspin ==2 || nspin == 4); + + if (inp.out_chg[0] == 2) + { + for (int is = 0; is < nspin; is++) + { + std::stringstream ss; + ss << PARAM.globalv.global_out_dir << "chg"; + + if(nspin==1) + { + ss << "ini.cube"; + } + else if(nspin==2 || nspin==4) + { + ss << "s" << is + 1 << "ini.cube"; + } + + // mohan add 2025-10-18 + double fermi_energy = 0.0; + if(nspin == 1 || nspin ==4) + { + fermi_energy = efermi.ef; + } + else if(nspin == 2) + { + if(is==0) + { + fermi_energy = efermi.ef_up; + } + else if(is==1) + { + fermi_energy = efermi.ef_dw; + } + } + + ModuleIO::write_vdata_palgrid(para_grid, + chr.rho[is], + is, + nspin, + istep, + ss.str(), + fermi_energy, + &(ucell)); + } + } + return; +} + + +void write_pot_init( + const UnitCell& ucell, + const Parallel_Grid ¶_grid, + elecstate::ElecState *pelec, + const int istep, + const Input_para& inp) +{ + //! output total local potential of the initial charge density + const int nspin = inp.nspin; + assert(nspin == 1 || nspin ==2 || nspin == 4); + + if (inp.out_pot == 3) + { + for (int is = 0; is < nspin; is++) + { + std::stringstream ss; + ss << PARAM.globalv.global_out_dir << "pot"; + + if(nspin==1) + { + ss << "ini.cube"; + } + else if(nspin==2 || nspin==4) + { + ss << "s" << is + 1 << "ini.cube"; + } + + ModuleIO::write_vdata_palgrid(para_grid, + pelec->pot->get_effective_v(is), + is, + nspin, + istep, + ss.str(), + 0.0, // efermi + &(ucell), + 11, // precsion + 0); // out_fermi + } + } + +} diff --git a/source/source_io/write_init.h b/source/source_io/write_init.h new file mode 100644 index 0000000000..08184931e9 --- /dev/null +++ b/source/source_io/write_init.h @@ -0,0 +1,31 @@ +#ifndef WRITE_INIT_H +#define WRITE_INIT_H + +#include "source_io/module_parameter/input_parameter.h" // use inp +#include "source_cell/parallel_kpoints.h" // use para_grid +#include "source_estate/module_charge/charge.h" // use chg +#include "source_estate/fp_energy.h" // use efermi +#include "source_estate/elecstate.h" // use pelec + +namespace ModuleIO +{ + +void write_chg_init( + const UnitCell& ucell, + const Parallel_Grid ¶_grid, + const Charge &chr, + const elecstate::Efermi &efermi, + const int istep, + const Input_para& inp); + +void write_pot_init( + const UnitCell& ucell, + const Parallel_Grid ¶_grid, + elecstate::ElecState *pelec, + const int istep, + const Input_para& inp); + +} + + +#endif From 6b4d7c85e22c04592fddf8a35a0274059c55b135 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 23:05:03 +0800 Subject: [PATCH 17/81] fix bugs --- source/source_esolver/esolver_fp.cpp | 4 ++-- source/source_esolver/esolver_ks.cpp | 11 ----------- source/source_estate/test/elecstate_base_test.cpp | 4 ++-- .../source_estate/test/elecstate_fp_energy_test.cpp | 8 ++++---- source/source_estate/test/elecstate_pw_test.cpp | 2 +- source/source_io/test/print_info_test.cpp | 6 +++--- source/source_io/test/write_dos_pw_test.cpp | 4 ++-- source/source_io/write_init.cpp | 4 ++-- 8 files changed, 16 insertions(+), 27 deletions(-) diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index efe6990192..b098068e14 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -146,9 +146,9 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) //! set direction of magnetism, used in non-collinear case elecstate::cal_ux(ucell); - //! output the initial charge density + //! output the initial charge density and potential ModuleIO::write_chg_init(ucell, this->Pgrid, this->chr, this->pelec->eferm, istep, PARAM.inp); - ModuleIO::write_pot_init(ucell, this->Pgrid, this->pelec, istep, PARAM.inp); +// ModuleIO::write_pot_init(ucell, this->Pgrid, this->pelec, istep, PARAM.inp); return; } diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 477f33dcd2..0383a43196 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -1,16 +1,5 @@ #include "esolver_ks.h" -/* -#include "source_base/timer.h" -#include "source_base/global_variable.h" -#include "source_pw/module_pwdft/global.h" -#include "source_io/module_parameter/parameter.h" -#include "source_lcao/module_dftu/dftu.h" - -#include "source_cell/cal_atoms_info.h" -#include "source_io/cube_io.h" -*/ - // for jason output information #include "source_io/json_output/init_info.h" #include "source_io/json_output/output_info.h" diff --git a/source/source_estate/test/elecstate_base_test.cpp b/source/source_estate/test/elecstate_base_test.cpp index ced491cf8b..24ec4a5fff 100644 --- a/source/source_estate/test/elecstate_base_test.cpp +++ b/source/source_estate/test/elecstate_base_test.cpp @@ -83,7 +83,7 @@ void Charge::set_rho_core(const UnitCell& ucell, ModuleBase::ComplexMatrix const void Charge::set_rho_core_paw() { } -void Charge::init_rho(elecstate::efermi&, +void Charge::init_rho(elecstate::Efermi&, const UnitCell&, const Parallel_Grid&, ModuleBase::ComplexMatrix const&, @@ -266,7 +266,7 @@ TEST_F(ElecStateTest, InitSCF) Charge* charge = new Charge; elecstate->charge = charge; elecstate->pot = new elecstate::Potential; - elecstate::efermi efermi; + elecstate::Efermi efermi; int istep = 0; ModuleBase::ComplexMatrix strucfac; elecstate->eferm = efermi; diff --git a/source/source_estate/test/elecstate_fp_energy_test.cpp b/source/source_estate/test/elecstate_fp_energy_test.cpp index 36f775d9af..66ef941f37 100644 --- a/source/source_estate/test/elecstate_fp_energy_test.cpp +++ b/source/source_estate/test/elecstate_fp_energy_test.cpp @@ -14,14 +14,14 @@ * - fenergy::calculate_harris() * - fenergy::clear_all() * - fenergy::print_all() - * - efermi::set_efval() - * - efermi::get_efval() + * - Efermi::set_efval() + * - Efermi::get_efval() */ class fenergy : public ::testing::Test { protected: elecstate::fenergy f_en; - elecstate::efermi eferm; + elecstate::Efermi eferm; }; TEST_F(fenergy, calculate_etot) @@ -92,4 +92,4 @@ TEST_F(fenergy, eferm_get_efval) EXPECT_EXIT(double tmpp = eferm.get_efval(2);, ::testing::ExitedWithCode(1), ""); std::string output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("Please check NSPIN when TWO_EFERMI is true")); -} \ No newline at end of file +} diff --git a/source/source_estate/test/elecstate_pw_test.cpp b/source/source_estate/test/elecstate_pw_test.cpp index 4f211ad923..4128d12812 100644 --- a/source/source_estate/test/elecstate_pw_test.cpp +++ b/source/source_estate/test/elecstate_pw_test.cpp @@ -133,7 +133,7 @@ void Charge::set_rho_core(const UnitCell& ucell, ModuleBase::ComplexMatrix const void Charge::set_rho_core_paw() { } -void Charge::init_rho(elecstate::efermi&, +void Charge::init_rho(elecstate::Efermi&, const UnitCell&, const Parallel_Grid&, ModuleBase::ComplexMatrix const&, diff --git a/source/source_io/test/print_info_test.cpp b/source/source_io/test/print_info_test.cpp index cdb64dfc4e..bc5d90bf1c 100644 --- a/source/source_io/test/print_info_test.cpp +++ b/source/source_io/test/print_info_test.cpp @@ -73,7 +73,7 @@ TEST_F(PrintInfoTest, SetupParameters) PARAM.sys.gamma_only_local = false; PARAM.input.calculation = cal_type[i]; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv)); + EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv, PARAM.input)); output = testing::internal::GetCapturedStdout(); if(PARAM.input.calculation == "scf") { @@ -96,7 +96,7 @@ TEST_F(PrintInfoTest, SetupParameters) { PARAM.input.mdp.md_type = md_types[j]; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv)); + EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv, PARAM.input)); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output,testing::HasSubstr("Molecular Dynamics simulations")); if (PARAM.mdp.md_type == "fire") @@ -131,7 +131,7 @@ TEST_F(PrintInfoTest, SetupParameters) { PARAM.input.basis_type = basis_type[i]; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv)); + EXPECT_NO_THROW(ModuleIO::print_parameters(*ucell, *kv, PARAM.input)); output = testing::internal::GetCapturedStdout(); if(PARAM.input.basis_type == "lcao") { diff --git a/source/source_io/test/write_dos_pw_test.cpp b/source/source_io/test/write_dos_pw_test.cpp index e6a26759df..a7dcd74202 100644 --- a/source/source_io/test/write_dos_pw_test.cpp +++ b/source/source_io/test/write_dos_pw_test.cpp @@ -65,7 +65,7 @@ TEST_F(DosPWTest,Dos1) PARAM.input.nbands = dosp.nbands; // initialize the Fermi energy - elecstate::efermi fermi_energy; + elecstate::Efermi fermi_energy; std::ofstream ofs("write_dos_pw.log"); @@ -130,7 +130,7 @@ TEST_F(DosPWTest,Dos2) PARAM.input.nbands = dosp.nbands; // initialize the Fermi energy - elecstate::efermi fermi_energy; + elecstate::Efermi fermi_energy; std::ofstream ofs("write_dos_pw.log"); diff --git a/source/source_io/write_init.cpp b/source/source_io/write_init.cpp index 5d1e6b8322..391422bf67 100644 --- a/source/source_io/write_init.cpp +++ b/source/source_io/write_init.cpp @@ -1,7 +1,7 @@ #include "source_io/write_init.h" #include "source_io/cube_io.h" -void write_chg_init( +void ModuleIO::write_chg_init( const UnitCell& ucell, const Parallel_Grid ¶_grid, const Charge &chr, @@ -60,7 +60,7 @@ void write_chg_init( } -void write_pot_init( +void ModuleIO::write_pot_init( const UnitCell& ucell, const Parallel_Grid ¶_grid, elecstate::ElecState *pelec, From 010a5757fee44d5b2d343ae8dda1aa381ce7f719 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 23:12:23 +0800 Subject: [PATCH 18/81] fix exd and exc in ctrl_runner_lcao --- source/source_esolver/esolver_ks_lcao.cpp | 9 +------ source/source_io/ctrl_runner_lcao.cpp | 32 +++++++---------------- source/source_io/ctrl_runner_lcao.h | 13 +++------ 3 files changed, 15 insertions(+), 39 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index c7171d0de7..7e29a4a1f5 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -286,12 +286,7 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) 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->exx_nao.exd, - this->exx_nao.exc, -#endif - this->solvent); + this->sf, this->locpp.vloc, this->exx_nao, this->solvent); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners"); } @@ -305,9 +300,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const ESolver_KS::iter_init(ucell, istep, iter); // cast pointers - auto* estate = dynamic_cast*>(this->pelec); - if(!estate) { ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_init","pelec does not exist"); diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index d92f08ff3d..c6e2c0e447 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -31,10 +31,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell ModulePW::PW_Basis* pw_rhod, // dense charge density Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential -#ifdef __EXX - std::shared_ptr> exd, - std::shared_ptr>> exc, -#endif + Exx_NAO &exx_nao, surchem &solvent) // solvent model { ModuleBase::TITLE("ModuleIO", "ctrl_runner_lcao"); @@ -75,8 +72,8 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell gd #ifdef __EXX , - exd ? &exd->get_Hexxs() : nullptr, - exc ? &exc->get_Hexxs() : nullptr + exx_nao.exd ? &exx_nao.exd->get_Hexxs() : nullptr, + exx_nao.exc ? &exx_nao.exc->get_Hexxs() : nullptr #endif ); } @@ -99,8 +96,8 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell gd #ifdef __EXX , - exd ? &exd->get_Hexxs() : nullptr, - exc ? &exc->get_Hexxs() : nullptr + exx_nao.exd ? &exx_nao.exd->get_Hexxs() : nullptr, + exx_nao.exc ? &exx_nao.exc->get_Hexxs() : nullptr #endif ); } @@ -130,8 +127,8 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell two_center_bundle #ifdef __EXX , - exd ? &exd->get_Hexxs() : nullptr, - exc ? &exc->get_Hexxs() : nullptr + exx_nao.exd ? &exx_nao.exd->get_Hexxs() : nullptr, + exx_nao.exc ? &exx_nao.exc->get_Hexxs() : nullptr #endif ); } @@ -160,10 +157,7 @@ template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, / ModulePW::PW_Basis* pw_rhod, // dense charge density Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential -#ifdef __EXX - std::shared_ptr> exd, - std::shared_ptr>> exc, -#endif + Exx_NAO &exx_nao, surchem &solvent); // solvent model // TK: complex TR: double @@ -185,10 +179,7 @@ template void ctrl_runner_lcao, double>(UnitCell& ucell, ModulePW::PW_Basis* pw_rhod, // dense charge density Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential -#ifdef __EXX - std::shared_ptr, double>> exd, - std::shared_ptr, std::complex>> exc, -#endif + Exx_NAO> &exx_nao, surchem &solvent); // solvent model // TK: complex TR: complex @@ -210,10 +201,7 @@ template void ctrl_runner_lcao, std::complex>(UnitC ModulePW::PW_Basis* pw_rhod, // dense charge density Structure_Factor &sf, // structure factor ModuleBase::matrix &vloc, // local pseudopotential -#ifdef __EXX - std::shared_ptr, double>> exd, - std::shared_ptr, std::complex>> exc, -#endif + Exx_NAO> &exx_nao, 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 20318bba94..34eae3d26a 100644 --- a/source/source_io/ctrl_runner_lcao.h +++ b/source/source_io/ctrl_runner_lcao.h @@ -8,9 +8,7 @@ #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 +#include "source_lcao/setup_exx.h" // for exx, mohan add 20251018 namespace ModuleIO { @@ -33,12 +31,9 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell 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 - std::shared_ptr> exd, - std::shared_ptr>> exc, -#endif - surchem &solvent); // solvent model + ModuleBase::matrix &vloc, // local pseudopotential + Exx_NAO &exx_nao, + surchem &solvent); // solvent model } From 3f23f156fc5ccf7d1dea3f5f32998037a9db5a46 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 23:22:32 +0800 Subject: [PATCH 19/81] fix cmake --- source/source_io/test_serial/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/source_io/test_serial/CMakeLists.txt b/source/source_io/test_serial/CMakeLists.txt index 8196852c16..57a591d597 100644 --- a/source/source_io/test_serial/CMakeLists.txt +++ b/source/source_io/test_serial/CMakeLists.txt @@ -51,13 +51,13 @@ AddTest( ) AddTest( - TARGET MODULE_IO_nscf_band + TARGET MODULE_IO_write_bands LIBS parameter ${math_libs} base device - SOURCES nscf_band_test.cpp ../nscf_band.cpp + SOURCES write_bands_test.cpp ../write_bands.cpp ) AddTest( TARGET MODULE_IO_system_variable_test LIBS parameter ${math_libs} base device io_input_serial SOURCES io_system_variable_test.cpp -) \ No newline at end of file +) From cc834e9a746e3713cb4efe223531ab28c2a1b4a9 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 18 Oct 2025 23:26:43 +0800 Subject: [PATCH 20/81] fix bug --- .../{nscf_band_test.cpp => write_bands_test.cpp} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename source/source_io/test_serial/{nscf_band_test.cpp => write_bands_test.cpp} (91%) diff --git a/source/source_io/test_serial/nscf_band_test.cpp b/source/source_io/test_serial/write_bands_test.cpp similarity index 91% rename from source/source_io/test_serial/nscf_band_test.cpp rename to source/source_io/test_serial/write_bands_test.cpp index 08c4a338f2..e7e82684da 100644 --- a/source/source_io/test_serial/nscf_band_test.cpp +++ b/source/source_io/test_serial/write_bands_test.cpp @@ -1,17 +1,17 @@ #include "gtest/gtest.h" #include "gmock/gmock.h" -#include "source_io/nscf_band.h" +#include "source_io/write_bands.h" #include "source_cell/parallel_kpoints.h" #include "source_cell/klist.h" /************************************************ - * unit test of nscf_band + * unit test of ns ***********************************************/ /** * - Tested Functions: - * - nscf_band() + * - nscf_bands() * - output band structure in nscf calculation */ @@ -61,14 +61,14 @@ class BandTest : public ::testing::Test K_Vectors* kv; }; -TEST_F(BandTest, nscf_band) +TEST_F(BandTest, nscf_bands) { kv->para_k.nks_pool.resize(1); kv->para_k.nks_pool[0] = nks; kv->para_k.nkstot_np = nks; kv->para_k.nks_np = nks; // Call the function to be tested - ModuleIO::nscf_band(is, out_band_dir, nband, fermie, 8, ekb, *kv); + ModuleIO::nscf_bands(is, out_band_dir, nband, fermie, 8, ekb, *kv); // Check the output file std::ifstream ifs(out_band_dir); From 803468f5a78ff49a6a73599dc09b3daf4fd95840 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 11:09:04 +0800 Subject: [PATCH 21/81] delete lcao_after_scf and lcao_before_scf --- source/Makefile.Objects | 2 - source/source_esolver/CMakeLists.txt | 2 - source/source_esolver/esolver_ks.h | 1 - source/source_esolver/esolver_ks_lcao.cpp | 186 ++++++++++++++++++++++ source/source_esolver/lcao_after_scf.cpp | 83 ---------- source/source_esolver/lcao_before_scf.cpp | 162 ------------------- source/source_io/ctrl_scf_lcao.cpp | 49 +++++- source/source_io/ctrl_scf_lcao.h | 2 + source/source_io/write_init.cpp | 3 + 9 files changed, 232 insertions(+), 258 deletions(-) delete mode 100644 source/source_esolver/lcao_after_scf.cpp delete mode 100644 source/source_esolver/lcao_before_scf.cpp diff --git a/source/Makefile.Objects b/source/Makefile.Objects index a55c6206be..bee1dfa9a8 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -277,8 +277,6 @@ OBJS_ESOLVER=esolver.o\ OBJS_ESOLVER_LCAO=esolver_ks_lcao.o\ esolver_ks_lcao_tddft.o\ - lcao_before_scf.o\ - lcao_after_scf.o\ esolver_gets.o\ lcao_others.o\ esolver_dm2rho.o\ diff --git a/source/source_esolver/CMakeLists.txt b/source/source_esolver/CMakeLists.txt index a16b9bd6a2..787982d555 100644 --- a/source/source_esolver/CMakeLists.txt +++ b/source/source_esolver/CMakeLists.txt @@ -18,8 +18,6 @@ if(ENABLE_LCAO) list(APPEND objects esolver_ks_lcao.cpp esolver_ks_lcao_tddft.cpp - lcao_before_scf.cpp - lcao_after_scf.cpp esolver_gets.cpp lcao_others.cpp esolver_dm2rho.cpp diff --git a/source/source_esolver/esolver_ks.h b/source/source_esolver/esolver_ks.h index 3f271dc4cc..bd47446a68 100644 --- a/source/source_esolver/esolver_ks.h +++ b/source/source_esolver/esolver_ks.h @@ -71,7 +71,6 @@ class ESolver_KS : public ESolver_FP int maxniter; //! maximum iter steps for scf int niter; //! iter steps actually used in scf bool oscillate_esolver = false; // whether esolver is oscillated - bool scf_nmax_flag = false; // whether scf has reached nmax, mohan add 20250921 }; } // namespace ModuleESolver diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 7e29a4a1f5..35ca76b2af 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -19,6 +19,7 @@ #include "source_estate/module_dm/setup_dm.h" // setup dm from electronic wave functions #include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() +#include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() namespace ModuleESolver { @@ -183,6 +184,147 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa return; } + +template +void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) +{ + ModuleBase::TITLE("ESolver_KS_LCAO", "before_scf"); + ModuleBase::timer::tick("ESolver_KS_LCAO", "before_scf"); + + //! 1) call before_scf() of ESolver_KS. + ESolver_KS::before_scf(ucell, istep); + + auto* estate = dynamic_cast*>(this->pelec); + if(!estate) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","pelec does not exist"); + } + + //! 2) find search radius + double search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, + PARAM.inp.out_level, orb_.get_rcutmax_Phi(), ucell.infoNL.get_rcutmax_Beta(), + PARAM.globalv.gamma_only_local); + + //! 3) use search_radius to search adj atoms + atom_arrange::search(PARAM.globalv.search_pbc, GlobalV::ofs_running, + this->gd, ucell, search_radius, PARAM.inp.test_atom_input); + + //! 4) initialize NAO basis set + // here new is a unique pointer, which will be deleted automatically + gint_info_.reset( + new ModuleGint::GintInfo( + this->pw_big->nbx, this->pw_big->nby, this->pw_big->nbz, + this->pw_rho->nx, this->pw_rho->ny, this->pw_rho->nz, + 0, 0, this->pw_big->nbzp_start, + this->pw_big->nbx, this->pw_big->nby, this->pw_big->nbzp, + orb_.Phi, ucell, this->gd)); + ModuleGint::Gint::set_gint_info(gint_info_.get()); + + // 7) For each atom, calculate the adjacent atoms in different cells + // and allocate the space for H(R) and S(R). + // If k point is used here, allocate HlocR after atom_arrange. + this->RA.for_2d(ucell, this->gd, this->pv, PARAM.globalv.gamma_only_local, orb_.cutoffs()); + + // 8) initialize the Hamiltonian operators + // if atom moves, then delete old pointer and add a new one + if (this->p_hamilt != nullptr) + { + delete this->p_hamilt; + this->p_hamilt = nullptr; + } + if (this->p_hamilt == nullptr) + { + elecstate::DensityMatrix* DM = estate->get_DM(); + + this->p_hamilt = new hamilt::HamiltLCAO( + PARAM.globalv.gamma_only_local ? &(this->GG) : nullptr, + PARAM.globalv.gamma_only_local ? nullptr : &(this->GK), + ucell, this->gd, &this->pv, this->pelec->pot, this->kv, + two_center_bundle_, orb_, DM, this->deepks +#ifdef __EXX + , + istep, + GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->two_level_step : &this->exx_nao.exc->two_level_step, + GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->get_Hexxs() : nullptr, + GlobalC::exx_info.info_ri.real_number ? nullptr : &this->exx_nao.exc->get_Hexxs() +#endif + ); + } + + // 9) for each ionic step, the overlap must be rebuilt + // since it depends on ionic positions + this->deepks.build_overlap(ucell, orb_, pv, gd, *(two_center_bundle_.overlap_orb_alpha), PARAM.inp); + + // 10) prepare sc calculation + 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, &(this->pv), + PARAM.inp.nspin, this->kv, this->p_hamilt, this->psi, this->pelec); + } + + // 11) set xc type before the first cal of xc in pelec->init_scf, Peize Lin add 2016-12-03 +#ifdef __EXX + if (PARAM.inp.calculation != "nscf") + { + if (GlobalC::exx_info.info_ri.real_number) + { + this->exx_nao.exd->exx_beforescf(istep, this->kv, *this->p_chgmix, ucell, orb_); + } + else + { + this->exx_nao.exc->exx_beforescf(istep, this->kv, *this->p_chgmix, ucell, orb_); + } + } +#endif + + // 12) init_scf, should be before_scf? mohan add 2025-03-10 + this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm); + + // 13) initalize DMR + // DMR should be same size with Hamiltonian(R) + auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); + if(!hamilt_lcao) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","p_hamilt does not exist"); + } + estate->get_DM()->init_DMR(*hamilt_lcao->getHR()); + +#ifdef __MLALGO + // 14) initialize DMR of DeePKS + this->deepks.ld.init_DMR(ucell, orb_, this->pv, this->gd); +#endif + + // 15) two cases are considered: + // 1. DMK in DensityMatrix is not empty (istep > 0), then DMR is initialized by DMK + // 2. DMK in DensityMatrix is empty (istep == 0), then DMR is initialized by zeros + if (istep > 0) + { + estate->get_DM()->cal_DMR(); + } + + // 16) the electron charge density should be symmetrized, + Symmetry_rho srho; + for (int is = 0; is < PARAM.inp.nspin; is++) + { + srho.begin(is, this->chr, this->pw_rho, ucell.symm); + } + + // 17) why we need to set this sentence? mohan add 2025-03-10 + this->p_hamilt->non_first_scf = istep; + + // 18) update of RDMFT, added by jghan + if (PARAM.inp.rdmft == true) + { + rdmft_solver.update_ion(ucell, *(this->pw_rho), this->locpp.vloc, this->sf.strucFac); + } + + ModuleBase::timer::tick("ESolver_KS_LCAO", "before_scf"); + return; +} + + template double ESolver_KS_LCAO::cal_energy() { @@ -526,6 +668,50 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } +template +void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) +{ + ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf"); + ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); + + //! 1) call after_scf() of ESolver_KS + ESolver_KS::after_scf(ucell, istep, conv_esolver); + + auto* estate = dynamic_cast*>(this->pelec); + auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); + + if(!estate) + { + 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"); + } + + //! 2) output of lcao every few ionic steps + 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, this->deepks, this->exx_nao, + this->conv_esolver, this->scf_nmax_flag, + istep); + + + //! 3) Clean up RA, which is used to serach for adjacent atoms + if (!PARAM.inp.cal_force && !PARAM.inp.cal_stress) + { + RA.delete_grid(); + } + + ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); +} + + template class ESolver_KS_LCAO; template class ESolver_KS_LCAO, double>; template class ESolver_KS_LCAO, std::complex>; diff --git a/source/source_esolver/lcao_after_scf.cpp b/source/source_esolver/lcao_after_scf.cpp deleted file mode 100644 index 4f01542466..0000000000 --- a/source/source_esolver/lcao_after_scf.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "esolver_ks_lcao.h" -#include "source_io/ctrl_scf_lcao.h" - -namespace ModuleESolver -{ - -template -void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) -{ - ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf"); - ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); - - //------------------------------------------------------------------ - //! 1) call after_scf() of ESolver_KS - //------------------------------------------------------------------ - ESolver_KS::after_scf(ucell, istep, conv_esolver); - - //------------------------------------------------------------------ - //! 2) output of lcao every few ionic steps - //------------------------------------------------------------------ - auto* estate = dynamic_cast*>(this->pelec); - auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); - - if(!estate) - { - 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"); - } - - - //***** - // 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_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) - { - istep_in = istep; - out_flag = true; - } - } - else if(conv_esolver || this->scf_nmax_flag) // mohan add scf_nmax_flag on 20250921 - { - out_flag = true; - } - //***** - - 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, - this->deepks, - this->exx_nao, - istep); - } - - //------------------------------------------------------------------ - //! 3) Clean up RA, which is used to serach for adjacent atoms - //------------------------------------------------------------------ - if (!PARAM.inp.cal_force && !PARAM.inp.cal_stress) - { - RA.delete_grid(); - } - - ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); -} - -template class ESolver_KS_LCAO; -template class ESolver_KS_LCAO, double>; -template class ESolver_KS_LCAO, std::complex>; -} // namespace ModuleESolver diff --git a/source/source_esolver/lcao_before_scf.cpp b/source/source_esolver/lcao_before_scf.cpp deleted file mode 100644 index 46025b9db6..0000000000 --- a/source/source_esolver/lcao_before_scf.cpp +++ /dev/null @@ -1,162 +0,0 @@ -#include "source_estate/module_charge/symmetry_rho.h" -#include "source_esolver/esolver_ks_lcao.h" -#include "source_lcao/hamilt_lcao.h" -#include "source_lcao/module_dftu/dftu.h" -#include "source_cell/module_neighbor/sltk_atom_arrange.h" -#include "source_cell/module_neighbor/sltk_grid_driver.h" -#include "source_io/module_parameter/parameter.h" -#include "source_estate/elecstate_tools.h" -#include "source_estate/elecstate_lcao.h" -#include "source_lcao/LCAO_domain.h" -#include "source_lcao/module_operator_lcao/op_exx_lcao.h" -#include "source_lcao/module_operator_lcao/operator_lcao.h" -#include "source_lcao/module_deltaspin/spin_constrain.h" - -namespace ModuleESolver -{ - -template -void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) -{ - ModuleBase::TITLE("ESolver_KS_LCAO", "before_scf"); - ModuleBase::timer::tick("ESolver_KS_LCAO", "before_scf"); - - //! 1) call before_scf() of ESolver_KS. - ESolver_KS::before_scf(ucell, istep); - - auto* estate = dynamic_cast*>(this->pelec); - if(!estate) - { - ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","pelec does not exist"); - } - - - //! 2) find search radius - double search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, - PARAM.inp.out_level, orb_.get_rcutmax_Phi(), ucell.infoNL.get_rcutmax_Beta(), - PARAM.globalv.gamma_only_local); - - //! 3) use search_radius to search adj atoms - atom_arrange::search(PARAM.globalv.search_pbc, GlobalV::ofs_running, - this->gd, ucell, search_radius, PARAM.inp.test_atom_input); - - //! 4) initialize NAO basis set - // here new is a unique pointer, which will be deleted automatically - gint_info_.reset( - new ModuleGint::GintInfo( - this->pw_big->nbx, this->pw_big->nby, this->pw_big->nbz, - this->pw_rho->nx, this->pw_rho->ny, this->pw_rho->nz, - 0, 0, this->pw_big->nbzp_start, - this->pw_big->nbx, this->pw_big->nby, this->pw_big->nbzp, - orb_.Phi, ucell, this->gd)); - ModuleGint::Gint::set_gint_info(gint_info_.get()); - - // 7) For each atom, calculate the adjacent atoms in different cells - // and allocate the space for H(R) and S(R). - // If k point is used here, allocate HlocR after atom_arrange. - this->RA.for_2d(ucell, this->gd, this->pv, PARAM.globalv.gamma_only_local, orb_.cutoffs()); - - // 8) initialize the Hamiltonian operators - // if atom moves, then delete old pointer and add a new one - if (this->p_hamilt != nullptr) - { - delete this->p_hamilt; - this->p_hamilt = nullptr; - } - if (this->p_hamilt == nullptr) - { - elecstate::DensityMatrix* DM = estate->get_DM(); - - this->p_hamilt = new hamilt::HamiltLCAO( - PARAM.globalv.gamma_only_local ? &(this->GG) : nullptr, - PARAM.globalv.gamma_only_local ? nullptr : &(this->GK), - ucell, this->gd, &this->pv, this->pelec->pot, this->kv, - two_center_bundle_, orb_, DM, this->deepks -#ifdef __EXX - , - istep, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->two_level_step : &this->exx_nao.exc->two_level_step, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->get_Hexxs() : nullptr, - GlobalC::exx_info.info_ri.real_number ? nullptr : &this->exx_nao.exc->get_Hexxs() -#endif - ); - } - - - // 9) for each ionic step, the overlap must be rebuilt - // since it depends on ionic positions - this->deepks.build_overlap(ucell, orb_, pv, gd, *(two_center_bundle_.overlap_orb_alpha), PARAM.inp); - - // 10) prepare sc calculation - 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, &(this->pv), - PARAM.inp.nspin, this->kv, this->p_hamilt, this->psi, this->pelec); - } - - // 11) set xc type before the first cal of xc in pelec->init_scf, Peize Lin add 2016-12-03 -#ifdef __EXX - if (PARAM.inp.calculation != "nscf") - { - if (GlobalC::exx_info.info_ri.real_number) - { - this->exx_nao.exd->exx_beforescf(istep, this->kv, *this->p_chgmix, ucell, orb_); - } - else - { - this->exx_nao.exc->exx_beforescf(istep, this->kv, *this->p_chgmix, ucell, orb_); - } - } -#endif - - // 12) init_scf, should be before_scf? mohan add 2025-03-10 - this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm); - - // 13) initalize DMR - // DMR should be same size with Hamiltonian(R) - auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); - if(!hamilt_lcao) - { - ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","p_hamilt does not exist"); - } - estate->get_DM()->init_DMR(*hamilt_lcao->getHR()); - -#ifdef __MLALGO - // 14) initialize DMR of DeePKS - this->deepks.ld.init_DMR(ucell, orb_, this->pv, this->gd); -#endif - - // 15) two cases are considered: - // 1. DMK in DensityMatrix is not empty (istep > 0), then DMR is initialized by DMK - // 2. DMK in DensityMatrix is empty (istep == 0), then DMR is initialized by zeros - if (istep > 0) - { - estate->get_DM()->cal_DMR(); - } - - // 16) the electron charge density should be symmetrized, - Symmetry_rho srho; - for (int is = 0; is < PARAM.inp.nspin; is++) - { - srho.begin(is, this->chr, this->pw_rho, ucell.symm); - } - - // 17) why we need to set this sentence? mohan add 2025-03-10 - this->p_hamilt->non_first_scf = istep; - - // 18) update of RDMFT, added by jghan - if (PARAM.inp.rdmft == true) - { - rdmft_solver.update_ion(ucell, *(this->pw_rho), this->locpp.vloc, this->sf.strucFac); - } - - ModuleBase::timer::tick("ESolver_KS_LCAO", "before_scf"); - return; -} - -template class ESolver_KS_LCAO; -template class ESolver_KS_LCAO, double>; -template class ESolver_KS_LCAO, std::complex>; -} // namespace ModuleESolver diff --git a/source/source_io/ctrl_scf_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp index baf9257ca4..7ab02002c2 100644 --- a/source/source_io/ctrl_scf_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -30,11 +30,9 @@ #include "source_lcao/module_rdmft/rdmft.h" // use RDMFT codes #include "source_io/to_qo.h" // use toQO -namespace ModuleIO -{ template -void ctrl_scf_lcao(UnitCell& ucell, +void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, @@ -53,11 +51,39 @@ void ctrl_scf_lcao(UnitCell& ucell, rdmft::RDMFT &rdmft_solver, // for RDMFT Setup_DeePKS &deepks, Exx_NAO &exx_nao, + const bool conv_esolver, + const bool scf_nmax_flag, const int istep) { ModuleBase::TITLE("ModuleIO", "ctrl_scf_lcao"); ModuleBase::timer::tick("ModuleIO", "ctrl_scf_lcao"); + //***** + // 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_flag = false; + if (inp.out_freq_ion>0) // default value of out_freq_ion is 0 + { + if (istep % inp.out_freq_ion == 0) + { + istep_in = istep; + out_flag = true; + } + } + else if(conv_esolver || scf_nmax_flag) // mohan add scf_nmax_flag on 20250921 + { + out_flag = true; + } + + if(!out_flag) + { + return; + } + + //***** + const bool out_app_flag = inp.out_app_flag; const bool gamma_only = PARAM.globalv.gamma_only_local; const int nspin = inp.nspin; @@ -432,11 +458,11 @@ void ctrl_scf_lcao(UnitCell& ucell, ModuleBase::timer::tick("ModuleIO", "ctrl_scf_lcao"); } -} // End ModuleIO // For gamma only -template void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, +template void ModuleIO::ctrl_scf_lcao( + UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, @@ -455,10 +481,13 @@ template void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, rdmft::RDMFT &rdmft_solver, // for RDMFT Setup_DeePKS &deepks, Exx_NAO &exx_nao, + const bool conv_esolver, + const bool scf_nmax_flag, const int istep); // For multiple k-points -template void ModuleIO::ctrl_scf_lcao, double>(UnitCell& ucell, +template void ModuleIO::ctrl_scf_lcao, double>( + UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO>* pelec, @@ -477,9 +506,12 @@ template void ModuleIO::ctrl_scf_lcao, double>(UnitCell& uc rdmft::RDMFT, double> &rdmft_solver, // for RDMFT Setup_DeePKS> &deepks, Exx_NAO> &exx_nao, + const bool conv_esolver, + const bool scf_nmax_flag, const int istep); -template void ModuleIO::ctrl_scf_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, @@ -498,5 +530,6 @@ template void ModuleIO::ctrl_scf_lcao, std::complex rdmft::RDMFT, std::complex> &rdmft_solver, // for RDMFT Setup_DeePKS> &deepks, Exx_NAO> &exx_nao, + const bool conv_esolver, + const bool scf_nmax_flag, const int istep); - diff --git a/source/source_io/ctrl_scf_lcao.h b/source/source_io/ctrl_scf_lcao.h index c67ea465b0..ee1dcfdada 100644 --- a/source/source_io/ctrl_scf_lcao.h +++ b/source/source_io/ctrl_scf_lcao.h @@ -40,6 +40,8 @@ namespace ModuleIO rdmft::RDMFT &rdmft_solver, // for RDMFT Setup_DeePKS &deepks, Exx_NAO &exx_nao, + const bool conv_esolver, + const bool scf_nmax_flag, const int istep); } #endif diff --git a/source/source_io/write_init.cpp b/source/source_io/write_init.cpp index 391422bf67..8881647023 100644 --- a/source/source_io/write_init.cpp +++ b/source/source_io/write_init.cpp @@ -1,6 +1,9 @@ #include "source_io/write_init.h" #include "source_io/cube_io.h" +#include +#include + void ModuleIO::write_chg_init( const UnitCell& ucell, const Parallel_Grid ¶_grid, From 63eab5556f5930e8a9e248696d25859db53bf8ee Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 11:45:03 +0800 Subject: [PATCH 22/81] modify codes including read_wfc_nao to delete pelec, add setup_psi_lcao codes --- source/source_esolver/esolver_ks_lcao.cpp | 39 +-------------- source/source_io/read_wfc_nao.cpp | 19 +++++--- source/source_io/read_wfc_nao.h | 8 +-- source/source_io/td_current_io.h | 2 +- source/source_io/to_wannier90_lcao_in_pw.h | 4 +- source/source_psi/setup_psi.cpp | 4 +- source/source_psi/setup_psi_lcao.cpp | 57 ++++++++++++++++++++++ source/source_psi/setup_psi_lcao.h | 23 +++++++++ 8 files changed, 102 insertions(+), 54 deletions(-) create mode 100644 source/source_psi/setup_psi_lcao.cpp create mode 100644 source/source_psi/setup_psi_lcao.h diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 35ca76b2af..5faa333ae2 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -20,6 +20,7 @@ #include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() #include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() +#include "source_psi/setup_psi_lcao.h" // mohan add 20251019 namespace ModuleESolver { @@ -74,43 +75,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } // 5) init electronic wave function psi - if (this->psi == nullptr) - { - int nsk = 0; - int ncol = 0; - if (PARAM.globalv.gamma_only_local) - { - nsk = inp.nspin; - ncol = this->pv.ncol_bands; - if (inp.ks_solver == "genelpa" || inp.ks_solver == "elpa" || inp.ks_solver == "lapack" - || inp.ks_solver == "pexsi" || inp.ks_solver == "cusolver" - || inp.ks_solver == "cusolvermp") - { - ncol = this->pv.ncol; - } - } - else - { - nsk = this->kv.get_nks(); -#ifdef __MPI - ncol = this->pv.ncol_bands; -#else - ncol = inp.nbands; -#endif - } - this->psi = new psi::Psi(nsk, ncol, this->pv.nrow, this->kv.ngk, true); - } - - // 6) read psi from file - if (inp.init_wfc == "file" && inp.esolver_type != "tddft") - { - if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, - this->pv, *(this->psi), this->pelec, this->pelec->klist->ik2iktot, - this->pelec->klist->get_nkstot(), inp.nspin)) - { - ModuleBase::WARNING_QUIT("ESolver_KS_LCAO", "read electronic wave functions failed"); - } - } + Setup_Psi_LCAO::before_runner(); // 7) init DMK, but DMR is constructed in before_scf() dynamic_cast*>(this->pelec)->init_DM(&this->kv, &(this->pv), inp.nspin); diff --git a/source/source_io/read_wfc_nao.cpp b/source/source_io/read_wfc_nao.cpp index 9383e32905..f29bbeb609 100644 --- a/source/source_io/read_wfc_nao.cpp +++ b/source/source_io/read_wfc_nao.cpp @@ -26,7 +26,8 @@ bool ModuleIO::read_wfc_nao( const std::string& global_readin_dir, const Parallel_Orbitals& ParaV, psi::Psi& psid, - elecstate::ElecState* const pelec, + ModuleBase::matrix& ekb, + ModuleBase::matrix& wg, const std::vector &ik2iktot, const int nkstot, const int nspin, @@ -36,7 +37,7 @@ bool ModuleIO::read_wfc_nao( ModuleBase::TITLE("ModuleIO", "read_wfc_nao"); ModuleBase::timer::tick("ModuleIO", "read_wfc_nao"); - const int nk = pelec->ekb.nr; + const int nk = ekb.nr; const bool gamma_only = std::is_same::value; const int out_type = 1; // only support .txt file now @@ -119,8 +120,8 @@ bool ModuleIO::read_wfc_nao( const int ib_read = std::max(i - skip_band, 0); int ib = 0; ModuleBase::GlobalFunc::READ_VALUE(ifs, ib); - ModuleBase::GlobalFunc::READ_VALUE(ifs, pelec->ekb(ik, ib_read)); - ModuleBase::GlobalFunc::READ_VALUE(ifs, pelec->wg(ik, ib_read)); + ModuleBase::GlobalFunc::READ_VALUE(ifs, ekb(ik, ib_read)); + ModuleBase::GlobalFunc::READ_VALUE(ifs, wg(ik, ib_read)); if (i+1 != ib) { error_message << "The band index read in from file do not match the global parameter band index!\n"; @@ -194,8 +195,8 @@ bool ModuleIO::read_wfc_nao( 1, const_cast(ParaV.desc_wfc), pv_glb.blacs_ctxt); - Parallel_Common::bcast_double(&(pelec->ekb(ik, 0)), nbands); - Parallel_Common::bcast_double(&(pelec->wg(ik, 0)), nbands); + Parallel_Common::bcast_double(&ekb(ik, 0), nbands); + Parallel_Common::bcast_double(&wg(ik, 0), nbands); #else BlasConnector::copy(nbands*nlocal, ctot.data(), 1, psid.get_pointer(), 1); #endif @@ -207,7 +208,8 @@ bool ModuleIO::read_wfc_nao( template bool ModuleIO::read_wfc_nao(const std::string& global_readin_dir, const Parallel_Orbitals& ParaV, psi::Psi& psid, - elecstate::ElecState* const pelec, + ModuleBase::matrix& ekb, + ModuleBase::matrix& wg, const std::vector &ik2iktot, const int nkstot, const int nspin, @@ -217,7 +219,8 @@ template bool ModuleIO::read_wfc_nao(const std::string& global_readin_di template bool ModuleIO::read_wfc_nao>(const std::string& global_readin_dir, const Parallel_Orbitals& ParaV, psi::Psi>& psid, - elecstate::ElecState* const pelec, + ModuleBase::matrix& ekb, + ModuleBase::matrix& wg, const std::vector &ik2iktot, const int nkstot, const int nspin, diff --git a/source/source_io/read_wfc_nao.h b/source/source_io/read_wfc_nao.h index ad59e4f4f3..18aa7b4787 100644 --- a/source/source_io/read_wfc_nao.h +++ b/source/source_io/read_wfc_nao.h @@ -1,9 +1,8 @@ -#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_IO_READ_WFC_NAO_H -#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_IO_READ_WFC_NAO_H +#ifndef READ_WFC_NAO_H +#define READ_WFC_NAO_H #include "source_basis/module_ao/parallel_orbitals.h" #include "source_psi/psi.h" -#include "source_estate/elecstate.h" // mohan add 2010-09-09 namespace ModuleIO @@ -40,7 +39,8 @@ bool read_wfc_nao( const std::string& global_readin_dir, const Parallel_Orbitals& ParaV, psi::Psi& psid, - elecstate::ElecState* const pelec, + ModuleBase::matrix& ekb, + ModuleBase::matrix& wg, const std::vector &ik2iktot, const int nkstot, const int nspin, diff --git a/source/source_io/td_current_io.h b/source/source_io/td_current_io.h index 7872858c72..5158d1de26 100644 --- a/source/source_io/td_current_io.h +++ b/source/source_io/td_current_io.h @@ -50,4 +50,4 @@ void cal_tmp_DM(const UnitCell& ucell, #endif // __LCAO } // namespace ModuleIO -#endif // W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_IO_TD_CURRENT_IO_H +#endif diff --git a/source/source_io/to_wannier90_lcao_in_pw.h b/source/source_io/to_wannier90_lcao_in_pw.h index 94a5e87483..d7a728a209 100644 --- a/source/source_io/to_wannier90_lcao_in_pw.h +++ b/source/source_io/to_wannier90_lcao_in_pw.h @@ -1,5 +1,5 @@ -#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_IO_TO_WANNIER90_LCAO_IN_PW_H -#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_IO_TO_WANNIER90_LCAO_IN_PW_H +#ifndef TO_WANNIER90_LCAO_IN_PW_H +#define TO_WANNIER90_LCAO_IN_PW_H #include "source_base/abfs-vector3_order.h" #include "source_base/complexmatrix.h" diff --git a/source/source_psi/setup_psi.cpp b/source/source_psi/setup_psi.cpp index 84ef806bd9..afdebfd8ba 100644 --- a/source/source_psi/setup_psi.cpp +++ b/source/source_psi/setup_psi.cpp @@ -1,6 +1,6 @@ #include "source_psi/setup_psi.h" -#include "source_lcao/setup_deepks.h" -#include "source_lcao/LCAO_domain.h" +//#include "source_lcao/setup_deepks.h" +//#include "source_lcao/LCAO_domain.h" #include "source_io/module_parameter/parameter.h" // use parameter template diff --git a/source/source_psi/setup_psi_lcao.cpp b/source/source_psi/setup_psi_lcao.cpp new file mode 100644 index 0000000000..b3a34cc13b --- /dev/null +++ b/source/source_psi/setup_psi_lcao.cpp @@ -0,0 +1,57 @@ +#include "source_psi/setup_psi_lcao.h" + +template +Setup_Psi_LCAO::Setup_Psi_LCAO(){} + +template +Setup_Psi_LCAO::~Setup_Psi_LCAO(){} + +template +void Setup_Psi_LCAO::before_runner( + psi::Psi* &psi, + matrix &ekb, + matrix &wg, + const K_Vectors &kv, + const Parallel_Orbitals ¶_orb, + const Input_para &inp) +{ + // 5) init electronic wave function psi + if (this->psi == nullptr) + { + int nsk = 0; + int ncol = 0; + if (PARAM.globalv.gamma_only_local) + { + nsk = inp.nspin; + ncol = para_orb.ncol_bands; + if (inp.ks_solver == "genelpa" || inp.ks_solver == "elpa" || inp.ks_solver == "lapack" + || inp.ks_solver == "pexsi" || inp.ks_solver == "cusolver" + || inp.ks_solver == "cusolvermp") + { + ncol = para_orb.ncol; + } + } + else + { + nsk = kv.get_nks(); +#ifdef __MPI + ncol = para_orb.ncol_bands; +#else + ncol = inp.nbands; +#endif + } + this->psi = new psi::Psi(nsk, ncol, para_orb.nrow, kv.ngk, true); + } + + // 6) read psi from file + if (inp.init_wfc == "file" && inp.esolver_type != "tddft") + { + if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, + para_orb, *psi, ekb, wg, kv.ik2iktot, + kv.get_nkstot(), inp.nspin)) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO", "read electronic wave functions failed"); + } + } +} + diff --git a/source/source_psi/setup_psi_lcao.h b/source/source_psi/setup_psi_lcao.h new file mode 100644 index 0000000000..5e9ac3949d --- /dev/null +++ b/source/source_psi/setup_psi_lcao.h @@ -0,0 +1,23 @@ +#ifndef SETUP_PSI_LCAO_H +#define SETUP_PSI_LCAO_H + +#include "source_psi/psi_init.h" +#include "source_cell/klist.h" +#include "source_io/module_parameter/input_parameter.h" + +template +class Setup_Psi_LCAO +{ + public: + + Setup_Psi_LCAO(); + ~Setup_Psi_LCAO(); + + static void before_runner( + const K_Vectors &kv, + const Input_para &inp); + +}; + + +#endif From f39bc508bc3b3e58ec1f0564bfc6fc1fcf61557b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 15:49:41 +0800 Subject: [PATCH 23/81] fix bugs --- source/Makefile.Objects | 2 +- source/source_esolver/esolver_ks_lcao.cpp | 4 +- .../source_esolver/esolver_ks_lcao_tddft.cpp | 7 +- source/source_esolver/esolver_ks_pw.h | 4 +- source/source_esolver/lcao_others.cpp | 3 +- source/source_io/CMakeLists.txt | 1 - source/source_io/ctrl_output_pw.cpp | 20 +- source/source_io/ctrl_output_pw.h | 6 +- source/source_io/read_wfc_lcao.cpp | 492 ------------------ source/source_io/read_wfc_lcao.h | 87 ---- source/source_io/read_wfc_nao.cpp | 2 + source/source_io/read_wfc_nao.h | 1 + source/source_io/test/read_wfc_nao_test.cpp | 32 +- .../module_lr/esolver_lrtd_lcao.cpp | 3 +- source/source_psi/CMakeLists.txt | 1 + source/source_psi/setup_psi.cpp | 145 +++--- source/source_psi/setup_psi.h | 61 +-- source/source_psi/setup_psi_lcao.cpp | 57 -- source/source_psi/setup_psi_lcao.h | 23 - source/source_psi/setup_psi_pw.cpp | 103 ++++ source/source_psi/setup_psi_pw.h | 74 +++ 21 files changed, 287 insertions(+), 841 deletions(-) delete mode 100644 source/source_io/read_wfc_lcao.cpp delete mode 100644 source/source_io/read_wfc_lcao.h delete mode 100644 source/source_psi/setup_psi_lcao.cpp delete mode 100644 source/source_psi/setup_psi_lcao.h create mode 100644 source/source_psi/setup_psi_pw.cpp create mode 100644 source/source_psi/setup_psi_pw.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index bee1dfa9a8..8b185f2011 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -623,7 +623,6 @@ OBJS_IO_LCAO=cal_r_overlap_R.o\ write_dmk.o\ unk_overlap_lcao.o\ read_wfc_nao.o\ - read_wfc_lcao.o\ write_wfc_nao.o\ write_HS_sparse.o\ single_R_io.o\ @@ -765,6 +764,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ of_stress_pw.o\ symmetry_rho.o\ symmetry_rhog.o\ + setup_psi_pw.o\ setup_psi.o\ psi_init.o\ elecond.o\ diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 5faa333ae2..3e3bcee6c0 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -20,7 +20,7 @@ #include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() #include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() -#include "source_psi/setup_psi_lcao.h" // mohan add 20251019 +#include "source_psi/setup_psi.h" // mohan add 20251019 namespace ModuleESolver { @@ -75,7 +75,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } // 5) init electronic wave function psi - Setup_Psi_LCAO::before_runner(); + Setup_Psi::allocate_psi(this->psi, this->pelec->ekb, this->pelec->wg, this->kv, this->pv, PARAM.inp); // 7) init DMK, but DMR is constructed in before_scf() dynamic_cast*>(this->pelec)->init_DM(&this->kv, &(this->pv), inp.nspin); diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index 3072579164..7c047774b4 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -102,9 +102,10 @@ void ESolver_KS_LCAO_TDDFT::before_all_runners(UnitCell& ucell, cons if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, this->pv, *(this->psi), - this->pelec, - this->pelec->klist->ik2iktot, - this->pelec->klist->get_nkstot(), + this->pelec->ekb, + this->pelec->wg, + this->kv.ik2iktot, + this->kv.get_nkstot(), PARAM.inp.nspin, 0, TD_info::estep_shift)) diff --git a/source/source_esolver/esolver_ks_pw.h b/source/source_esolver/esolver_ks_pw.h index 09fbefc175..27eee6c564 100644 --- a/source/source_esolver/esolver_ks_pw.h +++ b/source/source_esolver/esolver_ks_pw.h @@ -1,7 +1,7 @@ #ifndef ESOLVER_KS_PW_H #define ESOLVER_KS_PW_H #include "./esolver_ks.h" -#include "source_psi/setup_psi.h" // mohan add 20251012 +#include "source_psi/setup_psi_pw.h" // mohan add 20251012 #include "source_pw/module_pwdft/VSep_in_pw.h" #include "source_pw/module_pwdft/global.h" #include "source_pw/module_pwdft/module_exx_helper/exx_helper.h" @@ -53,7 +53,7 @@ class ESolver_KS_PW : public ESolver_KS virtual void deallocate_hamilt(); // Electronic wave function psi - Setup_Psi stp; + Setup_Psi_pw stp; // DFT-1/2 method VSep* vsep_cell = nullptr; diff --git a/source/source_esolver/lcao_others.cpp b/source/source_esolver/lcao_others.cpp index dea6d52b88..1d4891954b 100644 --- a/source/source_esolver/lcao_others.cpp +++ b/source/source_esolver/lcao_others.cpp @@ -155,7 +155,8 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, this->pv, *(this->psi), - this->pelec, + this->pelec->ekb, + this->pelec->wg, this->pelec->klist->ik2iktot, this->pelec->klist->get_nkstot(), PARAM.inp.nspin)) diff --git a/source/source_io/CMakeLists.txt b/source/source_io/CMakeLists.txt index 34a9e7092a..39e32a9fb0 100644 --- a/source/source_io/CMakeLists.txt +++ b/source/source_io/CMakeLists.txt @@ -61,7 +61,6 @@ if(ENABLE_LCAO) get_pchg_lcao.cpp get_wf_lcao.cpp read_wfc_nao.cpp - read_wfc_lcao.cpp write_wfc_nao.cpp write_dmk.cpp write_dmr.cpp diff --git a/source/source_io/ctrl_output_pw.cpp b/source/source_io/ctrl_output_pw.cpp index a67d949ade..34d57caa01 100644 --- a/source/source_io/ctrl_output_pw.cpp +++ b/source/source_io/ctrl_output_pw.cpp @@ -90,7 +90,7 @@ void ModuleIO::ctrl_scf_pw(const int istep, const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, - Setup_Psi &stp, + Setup_Psi_pw &stp, const Device* ctx, const base_device::AbacusDevice_t &device, const Parallel_Grid ¶_grid, @@ -252,7 +252,7 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell, ModulePW::PW_Basis* pw_rhod, Charge &chr, K_Vectors &kv, - Setup_Psi &stp, + Setup_Psi_pw &stp, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, @@ -384,7 +384,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CPU const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, - Setup_Psi, base_device::DEVICE_CPU> &stp, + Setup_Psi_pw, base_device::DEVICE_CPU> &stp, const base_device::DEVICE_CPU* ctx, const base_device::AbacusDevice_t &device, const Parallel_Grid ¶_grid, @@ -401,7 +401,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_CP const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, - Setup_Psi, base_device::DEVICE_CPU> &stp, + Setup_Psi_pw, base_device::DEVICE_CPU> &stp, const base_device::DEVICE_CPU* ctx, const base_device::AbacusDevice_t &device, const Parallel_Grid ¶_grid, @@ -419,7 +419,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GPU const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, - Setup_Psi, base_device::DEVICE_GPU> &stp, + Setup_Psi_pw, base_device::DEVICE_GPU> &stp, const base_device::DEVICE_GPU* ctx, const base_device::AbacusDevice_t &device, const Parallel_Grid ¶_grid, @@ -436,7 +436,7 @@ template void ModuleIO::ctrl_scf_pw, base_device::DEVICE_GP const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, - Setup_Psi, base_device::DEVICE_GPU> &stp, + Setup_Psi_pw, base_device::DEVICE_GPU> &stp, const base_device::DEVICE_GPU* ctx, const base_device::AbacusDevice_t &device, const Parallel_Grid ¶_grid, @@ -452,7 +452,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_ ModulePW::PW_Basis* pw_rhod, Charge &chr, K_Vectors &kv, - Setup_Psi, base_device::DEVICE_CPU> &stp, + Setup_Psi_pw, base_device::DEVICE_CPU> &stp, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, @@ -469,7 +469,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE ModulePW::PW_Basis* pw_rhod, Charge &chr, K_Vectors &kv, - Setup_Psi, base_device::DEVICE_CPU> &stp, + Setup_Psi_pw, base_device::DEVICE_CPU> &stp, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, @@ -487,7 +487,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE_ ModulePW::PW_Basis* pw_rhod, Charge &chr, K_Vectors &kv, - Setup_Psi, base_device::DEVICE_GPU> &stp, + Setup_Psi_pw, base_device::DEVICE_GPU> &stp, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, @@ -504,7 +504,7 @@ template void ModuleIO::ctrl_runner_pw, base_device::DEVICE ModulePW::PW_Basis* pw_rhod, Charge &chr, K_Vectors &kv, - Setup_Psi, base_device::DEVICE_GPU> &stp, + Setup_Psi_pw, base_device::DEVICE_GPU> &stp, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, diff --git a/source/source_io/ctrl_output_pw.h b/source/source_io/ctrl_output_pw.h index b10870c9d1..798629c55e 100644 --- a/source/source_io/ctrl_output_pw.h +++ b/source/source_io/ctrl_output_pw.h @@ -4,7 +4,7 @@ #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/setup_psi.h" // use Setup_Psi class +#include "source_psi/setup_psi_pw.h" // use Setup_Psi class namespace ModuleIO { @@ -29,7 +29,7 @@ void ctrl_scf_pw(const int istep, const ModulePW::PW_Basis *pw_rho, const ModulePW::PW_Basis *pw_rhod, const ModulePW::PW_Basis_Big *pw_big, - Setup_Psi &stp, + Setup_Psi_pw &stp, const Device* ctx, const base_device::AbacusDevice_t &device, // mohan add 2025-10-15 const Parallel_Grid ¶_grid, @@ -44,7 +44,7 @@ void ctrl_runner_pw(UnitCell& ucell, ModulePW::PW_Basis* pw_rhod, Charge &chr, K_Vectors &kv, - Setup_Psi &stp, + Setup_Psi_pw &stp, Structure_Factor &sf, pseudopot_cell_vnl &ppcell, surchem &solvent, diff --git a/source/source_io/read_wfc_lcao.cpp b/source/source_io/read_wfc_lcao.cpp deleted file mode 100644 index 7738979aaa..0000000000 --- a/source/source_io/read_wfc_lcao.cpp +++ /dev/null @@ -1,492 +0,0 @@ -#include "source_io/read_wfc_lcao.h" - -#include "source_base/formatter.h" -#include "source_base/tool_quit.h" - -#include -#include -#include -#include - -#ifdef __MPI -#include "source_base/parallel_common.h" -#endif - -/* -template -void ModuleIO::read_wfc_lcao(const std::string& file, - int& ik, - ModuleBase::Vector3& kvec_c, - int& nbands, - int& nbasis, - std::vector>& lowf, - std::vector& ekb, - std::vector& occ, - double& wk) //<[out] wavefunction coefficients -{ - // assert the T must be double or float - std::ifstream ifs(file.c_str()); - if (!ifs) - { - ModuleBase::WARNING_QUIT("ModuleIO::read_wfc_lcao", "open file failed: " + file); - } - // will use line-by-line parse - std::string line; - bool read_kvec = false; - int iband = 0; - int ilocal = 0; - - wk = 1.0; - while (std::getline(ifs, line)) - { - // remove leading and trailing whitespaces - line = std::regex_replace(line, std::regex("^ +| +$|( ) +"), "$1"); - if (FmtCore::endswith(line, "(index of k points)")) - { - std::vector result = FmtCore::split(line); - ik = std::stoi(result[0]); - read_kvec = true; - continue; - } - if (read_kvec) - { - const std::vector result = FmtCore::split(line); - kvec_c.x = std::stod(result[0]); - kvec_c.y = std::stod(result[1]); - kvec_c.z = std::stod(result[2]); - read_kvec = false; - continue; - } - if (FmtCore::endswith(line, "(number of bands)")) - { - std::vector result = FmtCore::split(line); - nbands = std::stoi(result[0]); - ekb.resize(nbands, 0.0); // initialize ekb - occ.resize(nbands, 0.0); // initialize occ - } - else if (FmtCore::endswith(line, "(number of orbitals)")) - { - std::vector result = FmtCore::split(line); - nbasis = std::stoi(result[0]); - lowf.resize(nbands * nbasis, std::complex(0.0, 0.0)); // initialize lowf - } - else if (FmtCore::endswith(line, "(band)")) - { - std::vector result = FmtCore::split(line); - assert((ilocal == 0) || (ilocal == nbasis)); - iband = std::stoi(result[0]) - 1; - ilocal = 0; // reset ilocal - } - else if (FmtCore::endswith(line, "(Ry)")) - { - std::vector result = FmtCore::split(line); - ekb[iband] = std::stod(result[0]); - } - else if (FmtCore::endswith(line, "(Occupations)")) - { - std::vector result = FmtCore::split(line); - occ[iband] = std::stod(result[0]); - assert(ilocal == 0); - } - else // read wavefunction coefficients - { - const std::vector result = FmtCore::split(line); - // for the case the complex number is written as a b - for (int i = 0; i < result.size(); i += 2) - { - lowf[iband * nbasis + ilocal] = std::complex(std::stod(result[i]), std::stod(result[i + 1])); - ilocal += 1; - } - } - } - assert(lowf.size() == nbands * nbasis); - assert(iband == nbands - 1); - assert(ilocal == nbasis); -} - -// instantiate the template function -template void ModuleIO::read_wfc_lcao(const std::string& file, - int& ik, - ModuleBase::Vector3& kvec_c, - int& nbands, - int& nbasis, - std::vector>& lowf, - std::vector& ekb, - std::vector& occ, - double& wk); - -template void ModuleIO::read_wfc_lcao(const std::string& file, - int& ik, - ModuleBase::Vector3& kvec_c, - int& nbands, - int& nbasis, - std::vector>& lowf, - std::vector& ekb, - std::vector& occ, - double& wk); - -template -void ModuleIO::read_wfc_lcao(const std::string& file, - int& ik, - ModuleBase::Vector3& kvec_c, - int& nbands, - int& nbasis, - std::vector& lowf, - std::vector& ekb, - std::vector& occ, - double& wk) -{ - std::ifstream ifs(file.c_str()); - if (!ifs) - { - ModuleBase::WARNING_QUIT("ModuleIO::read_wfc_lcao", "open file failed: " + file); - } - // will use line-by-line parse - std::string line; - bool read_kvec = false; - int iband = 0; - int ilocal = 0; - - ik = 0; - kvec_c = ModuleBase::Vector3(0.0, 0.0, 0.0); - wk = 1.0; - while (std::getline(ifs, line)) - { - // remove leading and trailing whitespaces - line = std::regex_replace(line, std::regex("^ +| +$|( ) +"), "$1"); - if (read_kvec) - { - const std::vector result = FmtCore::split(line); - kvec_c.x = std::stod(result[0]); - kvec_c.y = std::stod(result[1]); - kvec_c.z = std::stod(result[2]); - read_kvec = false; - continue; - } - if (FmtCore::endswith(line, "(number of bands)")) - { - std::vector result = FmtCore::split(line); - nbands = std::stoi(result[0]); - ekb.resize(nbands, 0.0); // initialize ekb - occ.resize(nbands, 0.0); // initialize occ - } - else if (FmtCore::endswith(line, "(number of orbitals)")) - { - std::vector result = FmtCore::split(line); - nbasis = std::stoi(result[0]); - lowf.resize(nbands * nbasis, 0.0); // initialize lowf - } - else if (FmtCore::endswith(line, "(band)")) - { - std::vector result = FmtCore::split(line); - assert((ilocal == 0) || (ilocal == nbasis)); - iband = std::stoi(result[0]) - 1; - ilocal = 0; // reset ilocal - } - else if (FmtCore::endswith(line, "(Ry)")) - { - std::vector result = FmtCore::split(line); - ekb[iband] = std::stod(result[0]); - } - else if (FmtCore::endswith(line, "(Occupations)")) - { - std::vector result = FmtCore::split(line); - occ[iband] = std::stod(result[0]); - assert(ilocal == 0); - } - else // read wavefunction coefficients - { - const std::vector result = FmtCore::split(line); - for (const auto& token: result) - { - lowf[iband * nbasis + ilocal] = static_cast(std::stod(token)); - ilocal += 1; - } - } - } - assert(lowf.size() == nbands * nbasis); - assert(iband == nbands - 1); - assert(ilocal == nbasis); -} - -// instantiate the template function -template void ModuleIO::read_wfc_lcao(const std::string& file, - int& ik, - ModuleBase::Vector3& kvec_c, - int& nbands, - int& nbasis, - std::vector& lowf, - std::vector& ekb, - std::vector& occ, - double& wk); - -template void ModuleIO::read_wfc_lcao(const std::string& file, - int& ik, - ModuleBase::Vector3& kvec_c, - int& nbands, - int& nbasis, - std::vector& lowf, - std::vector& ekb, - std::vector& occ, - double& wk); - -#ifdef __MPI -template -void ModuleIO::restart_from_file(const std::string& out_dir, // hard-code the file name to be WFC_NAO_K*.txt? - const Parallel_2D& p2d, - const int& nks, - int& nbands, - int& nbasis, - std::vector& lowf_loc, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk) -{ - // reset vectors - lowf_loc.clear(); - ekb.clear(); - occ.clear(); - kvec_c.clear(); - wk.clear(); - const bool gamma_only = std::is_same::value || std::is_same::value; - const bool multi_k = std::is_same>::value || std::is_same>::value; - assert(gamma_only || multi_k); - const std::string file_prefix = gamma_only ? "WFC_GAMMA" : "WFC_NAO_K"; - // MPI-related variables init - int iproc=0; - - MPI_Comm_rank(p2d.comm(), &iproc); - // then start - int nbands_ = -1, nbasis_ = -1; - - // in LCAO, nks == nkstot - for (int ik = 0; ik < nks; ik++) - { - // check existence of file - const std::string file = out_dir + "/" + file_prefix + std::to_string(ik + 1) + ".txt"; - std::ifstream ifs(file); - if (!ifs) - { - ModuleBase::WARNING_QUIT("Module_IO::restart_from_file", "open file failed: " + file); - } - - std::vector lowf_glb; - std::vector lowf_loc_k; - std::vector ekb_; - std::vector occ_; - ModuleBase::Vector3 kvec; - double wk_ = 0.0; - - if (iproc == 0) // only one rank is needed to read the global lowf, ekb, ... - { - int ik_ = 0; - read_wfc_lcao(file, ik_, kvec, nbands, nbasis, lowf_glb, ekb_, occ_, wk_); - - assert(ik_ == ik + 1); // check the consistency of ik - assert(nbands == nbands_ || nbands_ == -1); // check the consistency of nbands - assert(nbasis == nbasis_ || nbasis_ == -1); // check the consistency of nbasis - - nbands_ = (nbands_ == -1) ? nbands : nbands_; - nbasis_ = (nbasis_ == -1) ? nbasis : nbasis_; - - ekb.insert(ekb.end(), ekb_.begin(), ekb_.end()); - occ.insert(occ.end(), occ_.begin(), occ_.end()); - wk.push_back(wk_); - kvec_c.push_back(kvec); - } - - MPI_Barrier(p2d.comm()); // wait for finishing the reading task - - // scatter the lowf_glb to lowf_loc - Parallel_2D p2d_glb; - Parallel_Common::bcast_int(nbands); - Parallel_Common::bcast_int(nbasis); - - p2d_glb.init(nbasis, nbands, std::max(nbasis, nbands), p2d.comm()); // in the same comm world - lowf_loc_k.resize(p2d.nrow * p2d.ncol); - - Cpxgemr2d(nbasis, - nbands, - lowf_glb.data(), - 1, - 1, - const_cast(p2d_glb.desc), - lowf_loc_k.data(), - 1, - 1, - const_cast(p2d.desc), - p2d_glb.blacs_ctxt); - // append to the global lowf_loc - lowf_loc.insert(lowf_loc.end(), lowf_loc_k.begin(), lowf_loc_k.end()); - } - assert(lowf_loc.size() == nks * p2d.nrow * p2d.ncol); - // still something to broadcast: ekb, occ, wk and kvec. - if (iproc != 0) - { - ekb.resize(nks * nbands, 0.0); - occ.resize(nks * nbands, 0.0); - wk.resize(nks, 0.0); - kvec_c.resize(nks); - } - Parallel_Common::bcast_double(ekb.data(), nks * nbands); - Parallel_Common::bcast_double(occ.data(), nks * nbands); - Parallel_Common::bcast_double(wk.data(), nks); - // Vector3 is not a trivial datatype, need to be broadcasted element by element - for (int ik = 0; ik < nks; ik++) - { - Parallel_Common::bcast_double(kvec_c[ik].x); - Parallel_Common::bcast_double(kvec_c[ik].y); - Parallel_Common::bcast_double(kvec_c[ik].z); - } -} - - -// instantiate the template function -template void ModuleIO::restart_from_file(const std::string& out_dir, - const Parallel_2D& p2d, - const int& nks, - int& nbands, - int& nbasis, - std::vector& lowf_loc, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); - -template void ModuleIO::restart_from_file(const std::string& out_dir, - const Parallel_2D& p2d, - const int& nks, - int& nbands, - int& nbasis, - std::vector& lowf_loc, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); - -template void ModuleIO::restart_from_file(const std::string& out_dir, - const Parallel_2D& p2d, - const int& nks, - int& nbands, - int& nbasis, - std::vector>& lowf_loc, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); - -template void ModuleIO::restart_from_file(const std::string& out_dir, - const Parallel_2D& p2d, - const int& nks, - int& nbands, - int& nbasis, - std::vector>& lowf_loc, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); -#endif - -template -void ModuleIO::restart_from_file(const std::string& out_dir, // hard-code the file name to be WFC_NAO_K*.txt? - const int& nks, - int& nbands, - int& nbasis, - std::vector& lowf, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk) -{ - // reset vectors - lowf.clear(); - ekb.clear(); - occ.clear(); - kvec_c.clear(); - wk.clear(); - const bool gamma_only = std::is_same::value || std::is_same::value; - const bool multi_k = std::is_same>::value || std::is_same>::value; - assert(gamma_only || multi_k); - const std::string file_prefix = gamma_only ? "WFC_GAMMA" : "WFC_NAO_K"; - int nbands_ = -1, nbasis_ = -1; - for (int ik = 0; ik < nks; ik++) - { - // check existence of file - const std::string file = out_dir + "/" + file_prefix + std::to_string(ik + 1) + ".txt"; - const std::ifstream ifs(file); - if (!ifs) - { - ModuleBase::WARNING_QUIT("restart_from_file", "open file failed: " + file); - } - - std::vector lowf_; - std::vector ekb_; - std::vector occ_; - ModuleBase::Vector3 kvec_; - double wk_=0.0; - int ik_=0; - - read_wfc_lcao(file, ik_, kvec_, nbands, nbasis, lowf_, ekb_, occ_, wk_); - - assert(nbands == nbands_ || nbands_ == -1); // check the consistency of nbands - assert(nbasis == nbasis_ || nbasis_ == -1); // check the consistency of nbasis - - nbands_ = (nbands_ == -1) ? nbands : nbands_; - nbasis_ = (nbasis_ == -1) ? nbasis : nbasis_; - - assert(ik_ == ik + 1); // check the consistency of ik - - // append to the global lowf_loc - lowf.insert(lowf.end(), lowf_.begin(), lowf_.end()); - ekb.insert(ekb.end(), ekb_.begin(), ekb_.end()); - occ.insert(occ.end(), occ_.begin(), occ_.end()); - wk.push_back(wk_); - kvec_c.push_back(kvec_); - } - assert(ekb.size() == nks * nbands); - assert(occ.size() == nks * nbands); - assert(lowf.size() == nks * nbands * nbasis); -} - -// instantiate the template function -template void ModuleIO::restart_from_file(const std::string& out_dir, - const int& nks, - int& nbands, - int& nbasis, - std::vector& lowf, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); - -template void ModuleIO::restart_from_file(const std::string& out_dir, - const int& nks, - int& nbands, - int& nbasis, - std::vector& lowf, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); - -template void ModuleIO::restart_from_file(const std::string& out_dir, - const int& nks, - int& nbands, - int& nbasis, - std::vector>& lowf, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); - -template void ModuleIO::restart_from_file(const std::string& out_dir, - const int& nks, - int& nbands, - int& nbasis, - std::vector>& lowf, - std::vector& ekb, - std::vector& occ, - std::vector>& kvec_c, - std::vector& wk); -*/ diff --git a/source/source_io/read_wfc_lcao.h b/source/source_io/read_wfc_lcao.h deleted file mode 100644 index f8f5933310..0000000000 --- a/source/source_io/read_wfc_lcao.h +++ /dev/null @@ -1,87 +0,0 @@ -#ifndef READ_WFC_LCAO_H -#define READ_WFC_LCAO_H - -#include "source_base/vector3.h" -#include -#include -#include - -#ifdef __MPI -#include "source_base/module_external/scalapack_connector.h" -#include "source_base/parallel_2d.h" -#endif - -/** - * @brief This class has two functions: restart psi from the previous calculation, and write psi to the disk. - */ - -namespace ModuleIO -{ -// only when you know why you need the T, you can write function with template parameter, -// otherwise, you should overload the function for different types -// For example in this case, ONLY wfc support to be std::complex and std::complex, -// not ekb, occ, wk and kvec_c. -/** - * @brief Read the wavefunction coefficients from the file (for complex wavefunction coefficients) - * - * @tparam T - * @param file [in] file name - * @param ik [out] the index of k points - * @param kvec_c [out] the k vector in Cartesian coordinates - * @param nbands [out] the number of bands - * @param nbasis [out] the number of orbitals - * @param lowf [out] wavefunction coefficients - * @param ekb [out] eigenvalues - * @param occ [out] occupations - * @param wk [out] weight of k points - */ -/* -template -void read_wfc_lcao(const std::string& file, int& ik, ModuleBase::Vector3& kvec_c, int& nbands, int& nbasis, - std::vector>& lowf, std::vector& ekb, std::vector& occ, - double& wk); - -template -void read_wfc_lcao(const std::string& file, int& ik, ModuleBase::Vector3& kvec_c, int& nbands, int& nbasis, - std::vector& lowf, std::vector& ekb, std::vector& occ, double& wk); -*/ -// the two functions above will return nbands, nbasis, lowf, ekb, occ and wk. -// the lowf is actually lowf_glb, which means the global matrix (ScaLAPACK convention), need to distribute -// to the local matrix (2D-block-cyclic parallel distribution) in the following function. - -//#ifdef __MPI -/** - * @brief Restart the wavefunction coefficients from the file (MPI 2D-BCD version) - * - * @tparam T: datatype of the wavefunction coefficients, can be double, float, std::complex or - * std::complex - * @param out_dir [in] the directory where the wavefunction coefficients are stored - * @param p2d [in] the 2D parallel distribution - * @param nks [in] the number of k points - * @param nbands [out] the number of bands - * @param nbasis [out] the number of orbitals - * @param lowf_loc [out] the local wavefunction coefficients, can be used to construct psi, see constructor No.8 - * @param ekb [out] the eigenvalues - * @param occ [out] the occupations - * @param kvec_c [out] the k vectors in Cartesian coordinates - * @param wk [out] the weight of k points - * - * @warning Cpxgemr2d not implemented yet - */ -/* -template -void restart_from_file(const std::string& out_dir, // hard-code the file name to be LOWF_K_*.txt? - const Parallel_2D& p2d, const int& nks, int& nbands, int& nbasis, std::vector& lowf_loc, - std::vector& ekb, std::vector& occ, - std::vector>& kvec_c, std::vector& wk); -#endif - -// serial version, can always present -template -void restart_from_file(const std::string& out_dir, // hard-code the file name to be LOWF_K_*.txt? - const int& nks, int& nbands, int& nbasis, std::vector& lowf, std::vector& ekb, - std::vector& occ, std::vector>& kvec_c, - std::vector& wk); -*/ -} -#endif diff --git a/source/source_io/read_wfc_nao.cpp b/source/source_io/read_wfc_nao.cpp index f29bbeb609..80f2b49f9b 100644 --- a/source/source_io/read_wfc_nao.cpp +++ b/source/source_io/read_wfc_nao.cpp @@ -7,6 +7,8 @@ #include "write_wfc_nao.h" #include "source_base/module_external/scalapack_connector.h" #include "source_io/filename.h" +#include "source_base/tool_title.h" // use title +#include "source_base/global_function.h" // use READ_VALUE void ModuleIO::read_wfc_nao_one_data(std::ifstream& ifs, double& data) { diff --git a/source/source_io/read_wfc_nao.h b/source/source_io/read_wfc_nao.h index 18aa7b4787..882e380f0a 100644 --- a/source/source_io/read_wfc_nao.h +++ b/source/source_io/read_wfc_nao.h @@ -3,6 +3,7 @@ #include "source_basis/module_ao/parallel_orbitals.h" #include "source_psi/psi.h" +#include "source_base/matrix.h" // mohan add 2010-09-09 namespace ModuleIO diff --git a/source/source_io/test/read_wfc_nao_test.cpp b/source/source_io/test/read_wfc_nao_test.cpp index 57917400d8..c1b31cc99b 100644 --- a/source/source_io/test/read_wfc_nao_test.cpp +++ b/source/source_io/test/read_wfc_nao_test.cpp @@ -7,14 +7,6 @@ #include "source_basis/module_ao/parallel_orbitals.h" #include "source_io/write_wfc_nao.h" -//define a mock derived class of class ElecState - -namespace elecstate -{ - const double* ElecState::getRho(int spin) const{return &(this->eferm.ef);}//just for mock -} - - namespace ModuleIO { // mock filename_output @@ -78,19 +70,20 @@ TEST_F(ReadWfcNaoTest,ReadWfcNao) #endif psi::Psi psid; - elecstate::ElecState pelec; - pelec.ekb.create(nks,nbands); - pelec.wg.create(nks,nbands); + ModuleBase::matrix ekb; + ModuleBase::matrix wg; + ekb.create(nks,nbands); + wg.create(nks,nbands); std::vector ik2iktot = {0}; const int nkstot = 1; // Act ModuleIO::read_wfc_nao(PARAM.sys.global_readin_dir, ParaV, psid, - &(pelec), ik2iktot, nkstot, nspin); + ekb, wg, ik2iktot, nkstot, nspin); // Assert - EXPECT_NEAR(pelec.ekb(0,1),0.31482195194888534794941393,1e-5); - EXPECT_NEAR(pelec.wg(0,1),0.0,1e-5); + EXPECT_NEAR(ekb(0,1),0.31482195194888534794941393,1e-5); + EXPECT_NEAR(wg(0,1),0.0,1e-5); if (my_rank == 0) { EXPECT_NEAR(psid(0,0,0),5.3759239842e-01,1e-5); @@ -123,19 +116,20 @@ TEST_F(ReadWfcNaoTest, ReadWfcNaoPart) #endif psi::Psi psid; - elecstate::ElecState pelec; - pelec.ekb.create(nks, nbands); - pelec.wg.create(nks, nbands); + ModuleBase::matrix ekb; + ModuleBase::matrix wg; + ekb.create(nks, nbands); + wg.create(nks, nbands); std::vector ik2iktot = {0}; const int nkstot = 1; // Act ModuleIO::read_wfc_nao(PARAM.sys.global_readin_dir, ParaV, psid, - &(pelec), ik2iktot, nkstot, nspin, skip_band, nstep); + ekb, wg, ik2iktot, nkstot, nspin, skip_band, nstep); // Assert - EXPECT_NEAR(pelec.ekb(0, 1), 7.4141254894954844445464914e-01, 1e-5); + EXPECT_NEAR(ekb(0, 1), 7.4141254894954844445464914e-01, 1e-5); if (my_rank == 0) { EXPECT_NEAR(psid(0, 0, 0), 1.8587183851, 1e-5); diff --git a/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp b/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp index 3bc1dd4434..90bcab0c47 100644 --- a/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp +++ b/source/source_lcao/module_lr/esolver_lrtd_lcao.cpp @@ -752,7 +752,8 @@ void LR::ESolver_LR::read_ks_wfc() #endif } else if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, this->paraMat_, *this->psi_ks, - this->pelec, + this->pelec->ekb, + this->pelec->wg, this->pelec->klist->ik2iktot, this->pelec->klist->get_nkstot(), /*skip_bands=*/this->nocc_max - this->nocc_in)) { diff --git a/source/source_psi/CMakeLists.txt b/source/source_psi/CMakeLists.txt index f871d2feee..54d636728f 100644 --- a/source/source_psi/CMakeLists.txt +++ b/source/source_psi/CMakeLists.txt @@ -7,6 +7,7 @@ add_library( add_library( psi_overall_init OBJECT + setup_psi_pw.cpp setup_psi.cpp psi_init.cpp ) diff --git a/source/source_psi/setup_psi.cpp b/source/source_psi/setup_psi.cpp index afdebfd8ba..650a949a19 100644 --- a/source/source_psi/setup_psi.cpp +++ b/source/source_psi/setup_psi.cpp @@ -1,105 +1,76 @@ #include "source_psi/setup_psi.h" -//#include "source_lcao/setup_deepks.h" -//#include "source_lcao/LCAO_domain.h" #include "source_io/module_parameter/parameter.h" // use parameter - -template -Setup_Psi::Setup_Psi(){} - -template -Setup_Psi::~Setup_Psi(){} - -template -void Setup_Psi::before_runner( - const UnitCell &ucell, +#include "source_io/read_wfc_nao.h" + +template +Setup_Psi::Setup_Psi(){} + +template +Setup_Psi::~Setup_Psi(){} + +// the size of psi may change during scf (in the future we can support such +// calculations: first SZP or DZ, and then DZP calculations. +// In that case, psi may change its size multiple times during SCF +template +void Setup_Psi::allocate_psi( + psi::Psi* &psi, + ModuleBase::matrix &ekb, + ModuleBase::matrix &wg, const K_Vectors &kv, - const Structure_Factor &sf, - const ModulePW::PW_Basis_K &pw_wfc, - const pseudopot_cell_vnl &ppcell, + const Parallel_Orbitals ¶_orb, const Input_para &inp) { - //! Allocate and initialize psi - this->p_psi_init = new psi::PSIInit(inp.init_wfc, - inp.ks_solver, inp.basis_type, GlobalV::MY_RANK, ucell, - sf, kv, ppcell, pw_wfc); - - //! Allocate memory for cpu version of psi - allocate_psi(this->psi_cpu, kv.get_nks(), kv.ngk, PARAM.globalv.nbands_l, pw_wfc.npwk_max); - - this->p_psi_init->prepare_init(inp.pw_seed); - - //! If GPU or single precision, allocate a new psi (psi_t). - //! otherwise, transform psi_cpu to psi_t - this->psi_t = inp.device == "gpu" || inp.precision == "single" - ? new psi::Psi(this->psi_cpu[0]) - : reinterpret_cast*>(this->psi_cpu); -} - - -template -void Setup_Psi::update_psi_d() -{ - if (this->psi_d != nullptr && PARAM.inp.precision == "single") + // init electronic wave function psi + if (psi == nullptr) { - delete reinterpret_cast, Device>*>(this->psi_d); + int nsk = 0; + int ncol = 0; + if (PARAM.globalv.gamma_only_local) + { + nsk = inp.nspin; + ncol = para_orb.ncol_bands; + if (inp.ks_solver == "genelpa" || inp.ks_solver == "elpa" || inp.ks_solver == "lapack" + || inp.ks_solver == "pexsi" || inp.ks_solver == "cusolver" + || inp.ks_solver == "cusolvermp") + { + ncol = para_orb.ncol; + } + } + else + { + nsk = kv.get_nks(); +#ifdef __MPI + ncol = para_orb.ncol_bands; +#else + ncol = inp.nbands; +#endif + } + psi = new psi::Psi(nsk, ncol, para_orb.nrow, kv.ngk, true); } - // Refresh this->psi_d - this->psi_d = PARAM.inp.precision == "single" - ? new psi::Psi, Device>(this->psi_t[0]) - : reinterpret_cast, Device>*>(this->psi_t); -} - -template -void Setup_Psi::init(hamilt::Hamilt* p_hamilt) -{ - //! Initialize wave functions - if (!this->already_initpsi) + //------------------------------ + //! read psi from file + //------------------------------ + if (inp.init_wfc == "file" && inp.esolver_type != "tddft") { - this->p_psi_init->initialize_psi(this->psi_cpu, this->psi_t, p_hamilt, GlobalV::ofs_running); - this->already_initpsi = true; + if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, + para_orb, *psi, ekb, wg, kv.ik2iktot, + kv.get_nkstot(), inp.nspin)) + { + ModuleBase::WARNING_QUIT("Setup_Psi", "read electronic wave functions failed"); + } } } -// Transfer data from GPU to CPU in pw basis -template -void Setup_Psi::copy_d2h(const base_device::AbacusDevice_t &device) +template +void Setup_Psi::deallocate_psi(psi::Psi* &psi) { - if (device == base_device::GpuDevice) - { - castmem_2d_d2h_op()(this->psi_cpu[0].get_pointer() - this->psi_cpu[0].get_psi_bias(), - this->psi_t[0].get_pointer() - this->psi_t[0].get_psi_bias(), - this->psi_cpu[0].size()); - } - else + if(psi!=nullptr) { - // do nothing + delete psi; } - return; } - - -template -void Setup_Psi::clean() -{ - if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") - { - delete this->psi_t; - } - if (PARAM.inp.precision == "single") - { - delete this->psi_d; - } - - delete this->psi_cpu; - delete this->p_psi_init; -} - -template class Setup_Psi, base_device::DEVICE_CPU>; -template class Setup_Psi, base_device::DEVICE_CPU>; -#if ((defined __CUDA) || (defined __ROCM)) -template class Setup_Psi, base_device::DEVICE_GPU>; -template class Setup_Psi, base_device::DEVICE_GPU>; -#endif +template class Setup_Psi; +template class Setup_Psi>; diff --git a/source/source_psi/setup_psi.h b/source/source_psi/setup_psi.h index 40a68a5020..a4306a0f30 100644 --- a/source/source_psi/setup_psi.h +++ b/source/source_psi/setup_psi.h @@ -2,16 +2,12 @@ #define SETUP_PSI_H #include "source_psi/psi_init.h" -#include "source_cell/unitcell.h" #include "source_cell/klist.h" -#include "source_pw/module_pwdft/structure_factor.h" -#include "source_basis/module_pw/pw_basis_k.h" -#include "source_pw/module_pwdft/VNL_in_pw.h" #include "source_io/module_parameter/input_parameter.h" -#include "source_base/module_device/device.h" -#include "source_hamilt/hamilt.h" +#include "source_basis/module_ao/parallel_orbitals.h" // use para_orb +#include "source_base/matrix.h" // use matrix -template +template class Setup_Psi { public: @@ -19,54 +15,15 @@ class Setup_Psi Setup_Psi(); ~Setup_Psi(); - //------------ - // variables - // psi_cpu, complex on cpu - // psi_t, complex on cpu/gpu - // psi_d, complex on cpu/gpu - //------------ - - // originally, this term is psi - // for PW, we have psi_cpu - psi::Psi, base_device::DEVICE_CPU>* psi_cpu = nullptr; - - // originally, this term is kspw_psi - // if CPU, kspw_psi = psi, otherwise, kspw_psi has a new copy - psi::Psi* psi_t = nullptr; - - // originally, this term is __kspw_psi - psi::Psi, Device>* psi_d = nullptr; - - // psi_initializer controller - psi::PSIInit* p_psi_init = nullptr; - - bool already_initpsi = false; - - //------------ - // functions - //------------ - - void before_runner( - const UnitCell &ucell, + static void allocate_psi( + psi::Psi* &psi, + ModuleBase::matrix &ekb, + ModuleBase::matrix &wg, const K_Vectors &kv, - const Structure_Factor &sf, - const ModulePW::PW_Basis_K &pw_wfc, - const pseudopot_cell_vnl &ppcell, + const Parallel_Orbitals ¶_orb, const Input_para &inp); - void init(hamilt::Hamilt* p_hamilt); - - void update_psi_d(); - - // Transfer data from device to host in pw basis - void copy_d2h(const base_device::AbacusDevice_t &device); - - void clean(); - - private: - - using castmem_2d_d2h_op - = base_device::memory::cast_memory_op, T, base_device::DEVICE_CPU, Device>; + static void deallocate_psi(psi::Psi* &psi); }; diff --git a/source/source_psi/setup_psi_lcao.cpp b/source/source_psi/setup_psi_lcao.cpp deleted file mode 100644 index b3a34cc13b..0000000000 --- a/source/source_psi/setup_psi_lcao.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "source_psi/setup_psi_lcao.h" - -template -Setup_Psi_LCAO::Setup_Psi_LCAO(){} - -template -Setup_Psi_LCAO::~Setup_Psi_LCAO(){} - -template -void Setup_Psi_LCAO::before_runner( - psi::Psi* &psi, - matrix &ekb, - matrix &wg, - const K_Vectors &kv, - const Parallel_Orbitals ¶_orb, - const Input_para &inp) -{ - // 5) init electronic wave function psi - if (this->psi == nullptr) - { - int nsk = 0; - int ncol = 0; - if (PARAM.globalv.gamma_only_local) - { - nsk = inp.nspin; - ncol = para_orb.ncol_bands; - if (inp.ks_solver == "genelpa" || inp.ks_solver == "elpa" || inp.ks_solver == "lapack" - || inp.ks_solver == "pexsi" || inp.ks_solver == "cusolver" - || inp.ks_solver == "cusolvermp") - { - ncol = para_orb.ncol; - } - } - else - { - nsk = kv.get_nks(); -#ifdef __MPI - ncol = para_orb.ncol_bands; -#else - ncol = inp.nbands; -#endif - } - this->psi = new psi::Psi(nsk, ncol, para_orb.nrow, kv.ngk, true); - } - - // 6) read psi from file - if (inp.init_wfc == "file" && inp.esolver_type != "tddft") - { - if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, - para_orb, *psi, ekb, wg, kv.ik2iktot, - kv.get_nkstot(), inp.nspin)) - { - ModuleBase::WARNING_QUIT("ESolver_KS_LCAO", "read electronic wave functions failed"); - } - } -} - diff --git a/source/source_psi/setup_psi_lcao.h b/source/source_psi/setup_psi_lcao.h deleted file mode 100644 index 5e9ac3949d..0000000000 --- a/source/source_psi/setup_psi_lcao.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef SETUP_PSI_LCAO_H -#define SETUP_PSI_LCAO_H - -#include "source_psi/psi_init.h" -#include "source_cell/klist.h" -#include "source_io/module_parameter/input_parameter.h" - -template -class Setup_Psi_LCAO -{ - public: - - Setup_Psi_LCAO(); - ~Setup_Psi_LCAO(); - - static void before_runner( - const K_Vectors &kv, - const Input_para &inp); - -}; - - -#endif diff --git a/source/source_psi/setup_psi_pw.cpp b/source/source_psi/setup_psi_pw.cpp new file mode 100644 index 0000000000..f5f0ae6005 --- /dev/null +++ b/source/source_psi/setup_psi_pw.cpp @@ -0,0 +1,103 @@ +#include "source_psi/setup_psi_pw.h" +#include "source_io/module_parameter/parameter.h" // use parameter + +template +Setup_Psi_pw::Setup_Psi_pw(){} + +template +Setup_Psi_pw::~Setup_Psi_pw(){} + +template +void Setup_Psi_pw::before_runner( + const UnitCell &ucell, + const K_Vectors &kv, + const Structure_Factor &sf, + const ModulePW::PW_Basis_K &pw_wfc, + const pseudopot_cell_vnl &ppcell, + const Input_para &inp) +{ + //! Allocate and initialize psi + this->p_psi_init = new psi::PSIInit(inp.init_wfc, + inp.ks_solver, inp.basis_type, GlobalV::MY_RANK, ucell, + sf, kv, ppcell, pw_wfc); + + //! Allocate memory for cpu version of psi + allocate_psi(this->psi_cpu, kv.get_nks(), kv.ngk, PARAM.globalv.nbands_l, pw_wfc.npwk_max); + + this->p_psi_init->prepare_init(inp.pw_seed); + + //! If GPU or single precision, allocate a new psi (psi_t). + //! otherwise, transform psi_cpu to psi_t + this->psi_t = inp.device == "gpu" || inp.precision == "single" + ? new psi::Psi(this->psi_cpu[0]) + : reinterpret_cast*>(this->psi_cpu); +} + + +template +void Setup_Psi_pw::update_psi_d() +{ + if (this->psi_d != nullptr && PARAM.inp.precision == "single") + { + delete reinterpret_cast, Device>*>(this->psi_d); + } + + // Refresh this->psi_d + this->psi_d = PARAM.inp.precision == "single" + ? new psi::Psi, Device>(this->psi_t[0]) + : reinterpret_cast, Device>*>(this->psi_t); +} + +template +void Setup_Psi_pw::init(hamilt::Hamilt* p_hamilt) +{ + //! Initialize wave functions + if (!this->already_initpsi) + { + this->p_psi_init->initialize_psi(this->psi_cpu, this->psi_t, p_hamilt, GlobalV::ofs_running); + this->already_initpsi = true; + } +} + + +// Transfer data from GPU to CPU in pw basis +template +void Setup_Psi_pw::copy_d2h(const base_device::AbacusDevice_t &device) +{ + if (device == base_device::GpuDevice) + { + castmem_2d_d2h_op()(this->psi_cpu[0].get_pointer() - this->psi_cpu[0].get_psi_bias(), + this->psi_t[0].get_pointer() - this->psi_t[0].get_psi_bias(), + this->psi_cpu[0].size()); + } + else + { + // do nothing + } + return; +} + + + +template +void Setup_Psi_pw::clean() +{ + if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") + { + delete this->psi_t; + } + if (PARAM.inp.precision == "single") + { + delete this->psi_d; + } + + delete this->psi_cpu; + delete this->p_psi_init; +} + +template class Setup_Psi_pw, base_device::DEVICE_CPU>; +template class Setup_Psi_pw, base_device::DEVICE_CPU>; +#if ((defined __CUDA) || (defined __ROCM)) +template class Setup_Psi_pw, base_device::DEVICE_GPU>; +template class Setup_Psi_pw, base_device::DEVICE_GPU>; +#endif diff --git a/source/source_psi/setup_psi_pw.h b/source/source_psi/setup_psi_pw.h new file mode 100644 index 0000000000..99407f67e1 --- /dev/null +++ b/source/source_psi/setup_psi_pw.h @@ -0,0 +1,74 @@ +#ifndef SETUP_PSI_PW_H +#define SETUP_PSI_PW_H + +#include "source_psi/psi_init.h" +#include "source_cell/unitcell.h" +#include "source_cell/klist.h" +#include "source_pw/module_pwdft/structure_factor.h" +#include "source_basis/module_pw/pw_basis_k.h" +#include "source_pw/module_pwdft/VNL_in_pw.h" +#include "source_io/module_parameter/input_parameter.h" +#include "source_base/module_device/device.h" +#include "source_hamilt/hamilt.h" + +template +class Setup_Psi_pw +{ + public: + + Setup_Psi_pw(); + ~Setup_Psi_pw(); + + //------------ + // variables + // psi_cpu, complex on cpu + // psi_t, complex on cpu/gpu + // psi_d, complex on cpu/gpu + //------------ + + // originally, this term is psi + // for PW, we have psi_cpu + psi::Psi, base_device::DEVICE_CPU>* psi_cpu = nullptr; + + // originally, this term is kspw_psi + // if CPU, kspw_psi = psi, otherwise, kspw_psi has a new copy + psi::Psi* psi_t = nullptr; + + // originally, this term is __kspw_psi + psi::Psi, Device>* psi_d = nullptr; + + // psi_initializer controller + psi::PSIInit* p_psi_init = nullptr; + + bool already_initpsi = false; + + //------------ + // functions + //------------ + + void before_runner( + const UnitCell &ucell, + const K_Vectors &kv, + const Structure_Factor &sf, + const ModulePW::PW_Basis_K &pw_wfc, + const pseudopot_cell_vnl &ppcell, + const Input_para &inp); + + void init(hamilt::Hamilt* p_hamilt); + + void update_psi_d(); + + // Transfer data from device to host in pw basis + void copy_d2h(const base_device::AbacusDevice_t &device); + + void clean(); + + private: + + using castmem_2d_d2h_op + = base_device::memory::cast_memory_op, T, base_device::DEVICE_CPU, Device>; + +}; + + +#endif From 367db31f9c1df5bc381a4147bdf14adfd572c87e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 16:31:46 +0800 Subject: [PATCH 24/81] delete grid integral in FORCE_STRESS --- source/source_esolver/esolver_double_xc.cpp | 2 -- source/source_esolver/esolver_ks.cpp | 4 ++- source/source_esolver/esolver_ks_lcao.cpp | 30 ++++------------ source/source_io/read_wfc_nao.cpp | 38 +++++++++++++++++++++ source/source_lcao/FORCE.h | 19 ----------- source/source_lcao/FORCE_STRESS.cpp | 15 +++----- source/source_lcao/FORCE_STRESS.h | 6 ---- source/source_lcao/FORCE_gamma.cpp | 2 -- source/source_lcao/FORCE_k.cpp | 3 +- source/source_lcao/pulay_fs.h | 10 ------ source/source_lcao/pulay_fs_gint.hpp | 21 ------------ source/source_lcao/pulay_fs_temp.hpp | 5 +++ source/source_psi/setup_psi.cpp | 2 ++ 13 files changed, 60 insertions(+), 97 deletions(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index db48bc0a8f..2658444bec 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -399,8 +399,6 @@ void ESolver_DoubleXC::cal_force(UnitCell& ucell, ModuleBase::matrix& fo this->pv, this->pelec_base, this->psi, - this->GG, // mohan add 2024-04-01 - this->GK, // mohan add 2024-04-01 this->two_center_bundle_, this->orb_, force_base, diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 0383a43196..a326b23802 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -14,6 +14,7 @@ #include "source_io/output_log.h" // use write_head #include "source_estate/elecstate_print.h" // print_etot #include "source_io/print_info.h" // print_parameters +#include "source_psi/setup_psi.h" // mohan add 20251009 namespace ModuleESolver { @@ -28,7 +29,8 @@ ESolver_KS::~ESolver_KS() //**************************************************** // do not add any codes in this deconstructor funcion //**************************************************** - delete this->psi; + Setup_Psi::deallocate_psi(this->psi); + delete this->p_hamilt; delete this->p_chgmix; this->ppcell.release_memory(); diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 3e3bcee6c0..875f43d8b6 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -308,29 +308,13 @@ void ESolver_KS_LCAO::cal_force(UnitCell& ucell, ModuleBase::matrix& for deepks.dpks_out_type = "tot"; // for deepks method - fsl.getForceStress(ucell, - PARAM.inp.cal_force, - PARAM.inp.cal_stress, - PARAM.inp.test_force, - PARAM.inp.test_stress, - this->gd, - this->pv, - this->pelec, - this->psi, - this->GG, // mohan add 2024-04-01 - this->GK, // mohan add 2024-04-01 - two_center_bundle_, - orb_, - force, - this->scs, - this->locpp, - this->sf, - this->kv, - this->pw_rho, - this->solvent, - this->deepks, - this->exx_nao, - &ucell.symm); + fsl.getForceStress(ucell, PARAM.inp.cal_force, PARAM.inp.cal_stress, + PARAM.inp.test_force, PARAM.inp.test_stress, + this->gd, this->pv, this->pelec, this->psi, + two_center_bundle_, orb_, force, this->scs, + this->locpp, this->sf, this->kv, + this->pw_rho, this->solvent, this->deepks, + this->exx_nao, &ucell.symm); // delete RA after cal_force this->RA.delete_grid(); diff --git a/source/source_io/read_wfc_nao.cpp b/source/source_io/read_wfc_nao.cpp index 80f2b49f9b..984e6298ac 100644 --- a/source/source_io/read_wfc_nao.cpp +++ b/source/source_io/read_wfc_nao.cpp @@ -10,6 +10,12 @@ #include "source_base/tool_title.h" // use title #include "source_base/global_function.h" // use READ_VALUE +// mohan add 2025-10-19 +void ModuleIO::read_wfc_nao_one_data(std::ifstream& ifs, float& data) +{ + ifs >> data; +} + void ModuleIO::read_wfc_nao_one_data(std::ifstream& ifs, double& data) { ifs >> data; @@ -23,6 +29,14 @@ void ModuleIO::read_wfc_nao_one_data(std::ifstream& ifs, std::complex& d data = std::complex(a, b); } +void ModuleIO::read_wfc_nao_one_data(std::ifstream& ifs, std::complex& data) +{ + float a = 0.0; + float b = 0.0; + ifs >> a >> b; + data = std::complex(a, b); +} + template bool ModuleIO::read_wfc_nao( const std::string& global_readin_dir, @@ -218,6 +232,18 @@ template bool ModuleIO::read_wfc_nao(const std::string& global_readin_di const int istep, const int skip_band); +// mohan add 2025-10-19 +template bool ModuleIO::read_wfc_nao(const std::string& global_readin_dir, + const Parallel_Orbitals& ParaV, + psi::Psi& psid, + ModuleBase::matrix& ekb, + ModuleBase::matrix& wg, + const std::vector &ik2iktot, + const int nkstot, + const int nspin, + const int istep, + const int skip_band); + template bool ModuleIO::read_wfc_nao>(const std::string& global_readin_dir, const Parallel_Orbitals& ParaV, psi::Psi>& psid, @@ -228,3 +254,15 @@ template bool ModuleIO::read_wfc_nao>(const std::string& gl const int nspin, const int istep, const int skip_band); + +// mohan add 2025-10-19 +template bool ModuleIO::read_wfc_nao>(const std::string& global_readin_dir, + const Parallel_Orbitals& ParaV, + psi::Psi>& psid, + ModuleBase::matrix& ekb, + ModuleBase::matrix& wg, + const std::vector &ik2iktot, + const int nkstot, + const int nspin, + const int istep, + const int skip_band); diff --git a/source/source_lcao/FORCE.h b/source/source_lcao/FORCE.h index e659863b56..ee16afd8b0 100644 --- a/source/source_lcao/FORCE.h +++ b/source/source_lcao/FORCE.h @@ -10,26 +10,9 @@ #include "source_estate/module_pot/potential_new.h" #include "source_lcao/force_stress_arrays.h" #include "source_lcao/module_deepks/LCAO_deepks.h" -#include "source_lcao/module_gint/gint_gamma.h" -#include "source_lcao/module_gint/gint_k.h" #include "source_psi/psi.h" #include "source_lcao/setup_deepks.h" -#ifndef TGINT_H -#define TGINT_H -template -struct TGint; -template <> -struct TGint -{ - using type = Gint_Gamma; -}; -template <> -struct TGint> -{ - using type = Gint_k; -}; -#endif template class Force_Stress_LCAO; @@ -67,7 +50,6 @@ class Force_LCAO ModuleBase::matrix& fvnl_dalpha, ModuleBase::matrix& svnl_dalpha, Setup_DeePKS& deepks, - typename TGint::type& gint, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, const Parallel_Orbitals& pv, @@ -127,7 +109,6 @@ class Force_LCAO void cal_fvl_dphi(const bool isforce, const bool isstress, const elecstate::Potential* pot_in, - typename TGint::type& gint, ModuleBase::matrix& fvl_dphi, ModuleBase::matrix& svl_dphi); diff --git a/source/source_lcao/FORCE_STRESS.cpp b/source/source_lcao/FORCE_STRESS.cpp index 043c45a2fa..af51e04ab2 100644 --- a/source/source_lcao/FORCE_STRESS.cpp +++ b/source/source_lcao/FORCE_STRESS.cpp @@ -40,8 +40,6 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, Parallel_Orbitals& pv, const elecstate::ElecState* pelec, const psi::Psi* psi, - Gint_Gamma& gint_gamma, // mohan add 2024-04-01 - Gint_k& gint_k, // mohan add 2024-04-01 const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, ModuleBase::matrix& fcs, @@ -137,8 +135,8 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, this->integral_part(PARAM.globalv.gamma_only_local, isforce, isstress, ucell, gd, fsr, pelec, psi, foverlap, ftvnl_dphi, fvnl_dbeta, fvl_dphi, soverlap, stvnl_dphi, svnl_dbeta, - svl_dphi, fvnl_dalpha, svnl_dalpha, deepks, gint_gamma, - gint_k, two_center_bundle, orb, pv, kv); + svl_dphi, fvnl_dalpha, svnl_dalpha, deepks, + two_center_bundle, orb, pv, kv); // calculate force and stress for Nonlocal part if (PARAM.inp.nspin == 1 || PARAM.inp.nspin == 2) @@ -732,8 +730,6 @@ void Force_Stress_LCAO::integral_part(const bool isGammaOnly, ModuleBase::matrix& fvnl_dalpha, ModuleBase::matrix& svnl_dalpha, Setup_DeePKS& deepks, - Gint_Gamma& gint_gamma, // mohan add 2024-04-01 - Gint_k& gint_k, // mohan add 2024-04-01 const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, const Parallel_Orbitals& pv, @@ -743,8 +739,7 @@ void Force_Stress_LCAO::integral_part(const bool isGammaOnly, flk.ftable(isforce, isstress, fsr, ucell, gd, psi, pelec, foverlap, ftvnl_dphi, fvnl_dbeta, fvl_dphi, soverlap, stvnl_dphi, svnl_dbeta, svl_dphi, - fvnl_dalpha, svnl_dalpha, deepks, gint_gamma, - two_center_bundle, orb, pv); + fvnl_dalpha, svnl_dalpha, deepks, two_center_bundle, orb, pv); return; } @@ -768,8 +763,6 @@ void Force_Stress_LCAO>::integral_part(const bool isGammaOn ModuleBase::matrix& fvnl_dalpha, ModuleBase::matrix& svnl_dalpha, Setup_DeePKS>& deepks, - Gint_Gamma& gint_gamma, - Gint_k& gint_k, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, const Parallel_Orbitals& pv, @@ -778,7 +771,7 @@ void Force_Stress_LCAO>::integral_part(const bool isGammaOn flk.ftable(isforce, isstress, fsr, ucell, gd, psi, pelec, foverlap, ftvnl_dphi, fvnl_dbeta, fvl_dphi, soverlap, stvnl_dphi, svnl_dbeta, svl_dphi, - fvnl_dalpha, svnl_dalpha, deepks, gint_k, + fvnl_dalpha, svnl_dalpha, deepks, two_center_bundle, orb, pv, &kv, this->RA); return; } diff --git a/source/source_lcao/FORCE_STRESS.h b/source/source_lcao/FORCE_STRESS.h index 5ef6648621..dd7d464b94 100644 --- a/source/source_lcao/FORCE_STRESS.h +++ b/source/source_lcao/FORCE_STRESS.h @@ -14,8 +14,6 @@ #include "source_lcao/module_ri/Exx_LRI_interface.h" #endif #include "force_stress_arrays.h" -#include "source_lcao/module_gint/gint_gamma.h" -#include "source_lcao/module_gint/gint_k.h" #include "source_lcao/setup_exx.h" // for exx, mohan add 20251008 #include "source_lcao/setup_deepks.h" // for deepks, mohan add 20251010 @@ -41,8 +39,6 @@ class Force_Stress_LCAO Parallel_Orbitals& pv, const elecstate::ElecState* pelec, const psi::Psi* psi, - Gint_Gamma& gint_gamma, // mohan add 2024-04-01 - Gint_k& gint_k, // mohan add 2024-04-01 const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, ModuleBase::matrix& fcs, @@ -96,8 +92,6 @@ class Force_Stress_LCAO ModuleBase::matrix& fvnl_dalpha, ModuleBase::matrix& svnl_dalpha, Setup_DeePKS& deepks, - Gint_Gamma& gint_gamma, - Gint_k& gint_k, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, const Parallel_Orbitals& pv, diff --git a/source/source_lcao/FORCE_gamma.cpp b/source/source_lcao/FORCE_gamma.cpp index ffd17393bb..763a3e3321 100644 --- a/source/source_lcao/FORCE_gamma.cpp +++ b/source/source_lcao/FORCE_gamma.cpp @@ -170,7 +170,6 @@ void Force_LCAO::ftable(const bool isforce, ModuleBase::matrix& fvnl_dalpha, ModuleBase::matrix& svnl_dalpha, Setup_DeePKS& deepks, - TGint::type& gint, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, const Parallel_Orbitals& pv, @@ -220,7 +219,6 @@ void Force_LCAO::ftable(const bool isforce, *dm, ucell, pelec->pot, - gint, isforce, isstress, false /*reset dm to gint*/); diff --git a/source/source_lcao/FORCE_k.cpp b/source/source_lcao/FORCE_k.cpp index ba5b58c89c..1e19a53a20 100644 --- a/source/source_lcao/FORCE_k.cpp +++ b/source/source_lcao/FORCE_k.cpp @@ -207,7 +207,6 @@ void Force_LCAO>::ftable(const bool isforce, ModuleBase::matrix& fvnl_dalpha, ModuleBase::matrix& svnl_dalpha, Setup_DeePKS>& deepks, - TGint>::type& gint, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, const Parallel_Orbitals& pv, @@ -247,7 +246,7 @@ void Force_LCAO>::ftable(const bool isforce, // doing on the real space grid. // vl_dphi PulayForceStress::cal_pulay_fs(fvl_dphi, svl_dphi, *dm, ucell, - pelec->pot, gint, isforce, isstress, + pelec->pot, isforce, isstress, false /*reset dm to gint*/); #ifdef __MLALGO diff --git a/source/source_lcao/pulay_fs.h b/source/source_lcao/pulay_fs.h index 78094449fc..ea25e9186a 100644 --- a/source/source_lcao/pulay_fs.h +++ b/source/source_lcao/pulay_fs.h @@ -1,18 +1,9 @@ #pragma once #include "source_basis/module_nao/two_center_bundle.h" #include "source_estate/module_dm/density_matrix.h" -#include "source_lcao/module_gint/gint_gamma.h" -#include "source_lcao/module_gint/gint_k.h" #include "source_estate/module_pot/potential_new.h" #include "source_cell/unitcell.h" #include "source_lcao/stress_tools.h" -#ifndef TGINT_H -#define TGINT_H -template -struct TGint; -template <> struct TGint { using type = Gint_Gamma; }; -template <> struct TGint> { using type = Gint_k; }; -#endif /// calculate the abstract formulas: /// $Tr[D*dH/dx]$ (force) and $1/V Tr[D*(dH/dx_a*x_b)]$ (stress) @@ -60,7 +51,6 @@ namespace PulayForceStress const elecstate::DensityMatrix& dm, ///< [in] density matrix or energy density matrix const UnitCell& ucell, ///< [in] unit cell const elecstate::Potential* pot, ///< [in] potential on grid - typename TGint::type& gint, ///< [in] Gint object const bool& isforce, const bool& isstress, const bool& set_dmr_gint = true); diff --git a/source/source_lcao/pulay_fs_gint.hpp b/source/source_lcao/pulay_fs_gint.hpp index f8bdb37d48..f097bf8a93 100644 --- a/source/source_lcao/pulay_fs_gint.hpp +++ b/source/source_lcao/pulay_fs_gint.hpp @@ -13,32 +13,12 @@ namespace PulayForceStress const elecstate::DensityMatrix& dm, ///< [in] density matrix const UnitCell& ucell, ///< [in] unit cell const elecstate::Potential* pot, ///< [in] potential on grid - typename TGint::type& gint, const bool& isforce, const bool& isstress, const bool& set_dmr_gint) { const int nspin = PARAM.inp.nspin; -#ifdef __OLD_GINT - if (set_dmr_gint) { gint.transfer_DM2DtoGrid(dm.get_DMR_vector()); } // 2d block to grid - for (int is = 0; is < nspin; ++is) - { - const double* vr_eff1 = pot->get_effective_v(is); - const double* vofk_eff1 = nullptr; - if (XC_Functional::get_ked_flag()) - { - vofk_eff1 = pot->get_effective_vofk(is); - Gint_inout inout(is, vr_eff1, vofk_eff1, isforce, isstress, &f, &s, Gint_Tools::job_type::force_meta); - gint.cal_gint(&inout); - } - else - { - Gint_inout inout(is, vr_eff1, isforce, isstress, &f, &s, Gint_Tools::job_type::force); - gint.cal_gint(&inout); - } - } -#else std::vector vr_eff(nspin, nullptr); std::vector vofk_eff(nspin, nullptr); if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5) @@ -58,7 +38,6 @@ namespace PulayForceStress } ModuleGint::cal_gint_fvl(nspin, vr_eff, dm.get_DMR_vector(), isforce, isstress, &f, &s); } -#endif if (isstress) { StressTools::stress_fill(-1.0, ucell.omega, s); } } diff --git a/source/source_lcao/pulay_fs_temp.hpp b/source/source_lcao/pulay_fs_temp.hpp index fb5b728a87..8d60d47255 100644 --- a/source/source_lcao/pulay_fs_temp.hpp +++ b/source/source_lcao/pulay_fs_temp.hpp @@ -2,6 +2,11 @@ #include "pulay_fs.h" #include "source_base/timer.h" #include "source_io/module_parameter/parameter.h" +#ifdef _OPENMP +#include +#endif + + namespace PulayForceStress { // common kernel diff --git a/source/source_psi/setup_psi.cpp b/source/source_psi/setup_psi.cpp index 650a949a19..97da42c45b 100644 --- a/source/source_psi/setup_psi.cpp +++ b/source/source_psi/setup_psi.cpp @@ -74,3 +74,5 @@ void Setup_Psi::deallocate_psi(psi::Psi* &psi) template class Setup_Psi; template class Setup_Psi>; +template class Setup_Psi; +template class Setup_Psi>; From d8bd20a84b00ff1beab3008e14d2a0315a743bdd Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 16:50:54 +0800 Subject: [PATCH 25/81] move read psi to esolver, not in psi --- source/source_esolver/esolver_ks_lcao.cpp | 15 ++++++++++++++- source/source_psi/setup_psi.cpp | 16 ---------------- source/source_psi/setup_psi.h | 3 --- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 875f43d8b6..d7a5286186 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -21,6 +21,7 @@ #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() #include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() #include "source_psi/setup_psi.h" // mohan add 20251019 +#include "source_io/read_wfc_nao.h" namespace ModuleESolver { @@ -75,7 +76,19 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } // 5) init electronic wave function psi - Setup_Psi::allocate_psi(this->psi, this->pelec->ekb, this->pelec->wg, this->kv, this->pv, PARAM.inp); + Setup_Psi::allocate_psi(this->psi, this->kv, this->pv, PARAM.inp); + + //! read psi from file + if (inp.init_wfc == "file" && inp.esolver_type != "tddft") + { + if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, + this->pv, *this->psi, this->pelec->ekb, this->pelec->wg, this->kv.ik2iktot, + this->kv.get_nkstot(), PARAM.inp.nspin)) + { + ModuleBase::WARNING_QUIT("ESolver_KS_LCAO", "read electronic wave functions failed"); + } + } + // 7) init DMK, but DMR is constructed in before_scf() dynamic_cast*>(this->pelec)->init_DM(&this->kv, &(this->pv), inp.nspin); diff --git a/source/source_psi/setup_psi.cpp b/source/source_psi/setup_psi.cpp index 97da42c45b..ba658a02de 100644 --- a/source/source_psi/setup_psi.cpp +++ b/source/source_psi/setup_psi.cpp @@ -1,6 +1,5 @@ #include "source_psi/setup_psi.h" #include "source_io/module_parameter/parameter.h" // use parameter -#include "source_io/read_wfc_nao.h" template Setup_Psi::Setup_Psi(){} @@ -14,8 +13,6 @@ Setup_Psi::~Setup_Psi(){} template void Setup_Psi::allocate_psi( psi::Psi* &psi, - ModuleBase::matrix &ekb, - ModuleBase::matrix &wg, const K_Vectors &kv, const Parallel_Orbitals ¶_orb, const Input_para &inp) @@ -47,19 +44,6 @@ void Setup_Psi::allocate_psi( } psi = new psi::Psi(nsk, ncol, para_orb.nrow, kv.ngk, true); } - - //------------------------------ - //! read psi from file - //------------------------------ - if (inp.init_wfc == "file" && inp.esolver_type != "tddft") - { - if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, - para_orb, *psi, ekb, wg, kv.ik2iktot, - kv.get_nkstot(), inp.nspin)) - { - ModuleBase::WARNING_QUIT("Setup_Psi", "read electronic wave functions failed"); - } - } } diff --git a/source/source_psi/setup_psi.h b/source/source_psi/setup_psi.h index a4306a0f30..d73ddb0659 100644 --- a/source/source_psi/setup_psi.h +++ b/source/source_psi/setup_psi.h @@ -5,7 +5,6 @@ #include "source_cell/klist.h" #include "source_io/module_parameter/input_parameter.h" #include "source_basis/module_ao/parallel_orbitals.h" // use para_orb -#include "source_base/matrix.h" // use matrix template class Setup_Psi @@ -17,8 +16,6 @@ class Setup_Psi static void allocate_psi( psi::Psi* &psi, - ModuleBase::matrix &ekb, - ModuleBase::matrix &wg, const K_Vectors &kv, const Parallel_Orbitals ¶_orb, const Input_para &inp); From 66445ebbbed96a2190ff78aba37363ad734ed488 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 16:56:44 +0800 Subject: [PATCH 26/81] fix system bug --- source/source_lcao/module_deepks/deepks_basic.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/source_lcao/module_deepks/deepks_basic.cpp b/source/source_lcao/module_deepks/deepks_basic.cpp index 53f09b8d0e..844b1705d5 100644 --- a/source/source_lcao/module_deepks/deepks_basic.cpp +++ b/source/source_lcao/module_deepks/deepks_basic.cpp @@ -8,6 +8,7 @@ #include "source_base/atom_in.h" #include "source_base/timer.h" #include "source_io/module_parameter/parameter.h" +#include // use system command // d(Descriptor) / d(projected density matrix) // Dimension is different for each inl, so there's a vector of tensors From 73965ea75062459691b233e57625a88d0814f272 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 19 Oct 2025 17:12:00 +0800 Subject: [PATCH 27/81] fix bug in read_wfc_nao.h --- source/source_io/read_wfc_nao.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/source_io/read_wfc_nao.h b/source/source_io/read_wfc_nao.h index 882e380f0a..4abf06ae00 100644 --- a/source/source_io/read_wfc_nao.h +++ b/source/source_io/read_wfc_nao.h @@ -14,6 +14,7 @@ namespace ModuleIO * @param ifs The input file stream to read from. * @param data The variable to store the read data value. */ +void read_wfc_nao_one_data(std::ifstream& ifs, float& data); void read_wfc_nao_one_data(std::ifstream& ifs, double& data); /** @@ -22,6 +23,7 @@ void read_wfc_nao_one_data(std::ifstream& ifs, double& data); * @param ifs The input file stream to read from. * @param data The variable to store the read complex data value. */ +void read_wfc_nao_one_data(std::ifstream& ifs, std::complex& data); void read_wfc_nao_one_data(std::ifstream& ifs, std::complex& data); /** From d521c449c6e43b1eba30114c4f82ff20c6a776ed Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 22 Oct 2025 15:38:41 +0800 Subject: [PATCH 28/81] change PARAM.inp to inp --- source/source_esolver/esolver_ks_lcao.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index d7a5286186..c42ffb0fb5 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -64,7 +64,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa 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") + if (inp.calculation == "gen_opt_abfs") { #ifdef __EXX Exx_Opt_Orb exx_opt_orb; @@ -76,14 +76,14 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } // 5) init electronic wave function psi - Setup_Psi::allocate_psi(this->psi, this->kv, this->pv, PARAM.inp); + Setup_Psi::allocate_psi(this->psi, this->kv, this->pv, inp); //! read psi from file if (inp.init_wfc == "file" && inp.esolver_type != "tddft") { if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir, this->pv, *this->psi, this->pelec->ekb, this->pelec->wg, this->kv.ik2iktot, - this->kv.get_nkstot(), PARAM.inp.nspin)) + this->kv.get_nkstot(), inp.nspin)) { ModuleBase::WARNING_QUIT("ESolver_KS_LCAO", "read electronic wave functions failed"); } @@ -94,7 +94,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa dynamic_cast*>(this->pelec)->init_DM(&this->kv, &(this->pv), inp.nspin); // 8) init exact exchange calculations - this->exx_nao.before_runner(ucell, this->kv, this->orb_, this->pv, PARAM.inp); + this->exx_nao.before_runner(ucell, this->kv, this->orb_, this->pv, inp); // 9) initialize DFT+U if (inp.dft_plus_u) @@ -120,7 +120,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } // 13) init deepks - this->deepks.before_runner(ucell, this->kv.get_nks(), this->orb_, this->pv, PARAM.inp); + this->deepks.before_runner(ucell, this->kv.get_nks(), this->orb_, this->pv, inp); // 14) set occupations, tddft does not need to set occupations in the first scf if (inp.ocp && inp.esolver_type != "tddft") From 5130f4bd25e967b463c0103f3ef416990b646bc4 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 22 Oct 2025 16:14:24 +0800 Subject: [PATCH 29/81] fix bugs --- source/source_esolver/esolver_ks_lcao.cpp | 23 +++---------------- source/source_io/print_info.cpp | 28 +++++++++++++++++++++++ source/source_io/print_info.h | 14 ++++++------ 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index c42ffb0fb5..5b7797169f 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -22,6 +22,7 @@ #include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() #include "source_psi/setup_psi.h" // mohan add 20251019 #include "source_io/read_wfc_nao.h" +#include "source_io/print_info.h" namespace ModuleESolver { @@ -130,25 +131,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa } // 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) - { - ModuleBase::WARNING("ESolver_KS_LCAO::before_all_runners", "nks is not divisible by kpar."); - std::cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - std::cout << " Warning: nks (" << this->kv.get_nks() << ") is not divisible by kpar (" - << PARAM.globalv.kpar_lcao << ")." << std::endl; - std::cout << " This may lead to poor load balance. It is strongly suggested to" << std::endl; - std::cout << " set nks to be divisible by kpar, but if this is really what" << std::endl; - std::cout << " you want, please ignore this warning." << std::endl; - std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%\n"; - } - } + ModuleIO::print_kpar(this->kv.get_nks(), PARAM.globalv.kpar_lcao); // 16) init rdmft, added by jghan if (inp.rdmft == true) @@ -667,7 +650,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const //! 3) Clean up RA, which is used to serach for adjacent atoms if (!PARAM.inp.cal_force && !PARAM.inp.cal_stress) { - RA.delete_grid(); + this->RA.delete_grid(); } ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); diff --git a/source/source_io/print_info.cpp b/source/source_io/print_info.cpp index 528b65b86c..7db14b4638 100644 --- a/source/source_io/print_info.cpp +++ b/source/source_io/print_info.cpp @@ -387,4 +387,32 @@ void print_screen(const int& stress_step, const int& force_step, const int& iste GlobalV::ofs_running << " ================================================================" << std::endl; } + +void print_kpar(const int &nks, const int &kpar_lcao) +{ + assert(nks>0); + assert(kpar_lcao>0); + + // 15) if kpar is not divisible by nks, print a warning + if (kpar_lcao > 1) + { + if (nks % kpar_lcao != 0) + { + ModuleBase::WARNING("ModuleIO::print_kpar", "nks is not divisible by kpar."); + std::cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%" + << std::endl; + std::cout << " Warning: nks (" << nks << ") is not divisible by kpar (" + << kpar_lcao << ")." << std::endl; + std::cout << " This may lead to poor load balance. It is strongly suggested to" << std::endl; + std::cout << " set nks to be divisible by kpar, but if this is really what" << std::endl; + std::cout << " you want, please ignore this warning." << std::endl; + std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%\n"; + } + } +} + } // namespace ModuleIO diff --git a/source/source_io/print_info.h b/source/source_io/print_info.h index 6c94d29530..fcdb90704d 100644 --- a/source/source_io/print_info.h +++ b/source/source_io/print_info.h @@ -1,9 +1,5 @@ -//========================================================== -// AUTHOR : mohan -// DATE : 2021-01-30 -//========================================================== -#ifndef PRINT_INFO -#define PRINT_INFO +#ifndef PRINT_INFO_H +#define PRINT_INFO_H #include "source_base/timer.h" #include "source_basis/module_pw/pw_basis_k.h" @@ -13,7 +9,6 @@ namespace ModuleIO { - // print out to screen about the readin parameters void print_parameters( const UnitCell& ucell, @@ -21,14 +16,19 @@ void print_parameters( const Input_para& inp); void print_time(time_t& time_start, time_t& time_finish); + void print_screen(const int& stress_step, const int& force_step, const int& istep); + //! Print charge density using FFT void print_rhofft(ModulePW::PW_Basis* pw_rhod, ModulePW::PW_Basis* pw_rho, ModulePW::PW_Basis_Big* pw_big, std::ofstream& ofs); + void print_wfcfft(const Input_para& inp, ModulePW::PW_Basis_K& pw_wfc, std::ofstream& ofs); +void print_kpar(const int &nks, const int &kpar_lcao); + } // namespace ModuleIO #endif From 10c4d9c35a1312899467a9561249cfa41f4cb1fe Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 22 Oct 2025 20:18:13 +0800 Subject: [PATCH 30/81] delete omega in pelec, remove some deepks codes to setup_deepks.cpp --- source/source_esolver/esolver_double_xc.cpp | 2 -- source/source_esolver/esolver_fp.cpp | 2 -- source/source_esolver/esolver_ks_lcao.cpp | 11 +---------- source/source_esolver/esolver_of.cpp | 4 ++-- source/source_esolver/esolver_of_tool.cpp | 1 - source/source_estate/elecstate.h | 1 - source/source_estate/elecstate_energy.cpp | 4 ++-- source/source_estate/magnetism.cpp | 11 +++++++---- source/source_estate/setup_estate_pw.cpp | 3 --- source/source_lcao/setup_deepks.cpp | 20 ++++++++++++++++++++ source/source_lcao/setup_deepks.h | 8 ++++++++ 11 files changed, 40 insertions(+), 27 deletions(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index 2658444bec..50a52e6d8b 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -90,7 +90,6 @@ void ESolver_DoubleXC::before_all_runners(UnitCell& ucell, const Input_p // 10) inititlize the charge density this->chr_base.allocate(PARAM.inp.nspin); - this->pelec_base->omega = ucell.omega; // 11) initialize the potential if (this->pelec_base->pot == nullptr) @@ -116,7 +115,6 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) ESolver_KS_LCAO::before_scf(ucell, istep); - this->pelec_base->omega = ucell.omega; //---------------------------------------------------------- //! calculate D2 or D3 vdW //---------------------------------------------------------- diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index b098068e14..ef2258de36 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -108,8 +108,6 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep) this->locpp.init_vloc(ucell, this->pw_rhod); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL"); - this->pelec->omega = ucell.omega; - // perform symmetry analysis if (ModuleSymmetry::Symmetry::symm_flag == 1) { diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 5b7797169f..a0f1d1a89d 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -110,7 +110,6 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa // 11) init charge density this->chr.allocate(inp.nspin); - this->pelec->omega = ucell.omega; // 12) init potentials if (this->pelec->pot == nullptr) @@ -565,15 +564,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // 2) for deepks, calculate delta_e, output labels during electronic steps -#ifdef __MLALGO - if (PARAM.inp.deepks_scf) - { - this->deepks.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, this->deepks.ld.dm_r); - estate->f_en.edeepks_scf = this->deepks.ld.E_delta - this->deepks.ld.e_delta_band; - estate->f_en.edeepks_delta = this->deepks.ld.E_delta; - } -#endif + this->deepks.delta_e(this->kv, this->pv, this->gd, dm_vec, PARAM.inp); // 3) for delta spin if (PARAM.inp.sc_mag_switch) diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 598ba93bc6..6785c64a1f 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -273,7 +273,7 @@ void ESolver_OF::before_opt(const int istep, UnitCell& ucell) { // Here we initialize rho to be uniform, // because the rho got by pot.init_pot -> Charge::atomic_rho may contain minus elements. - this->chr.rho[is][ibs] = this->nelec_[is] / this->pelec->omega; + this->chr.rho[is][ibs] = this->nelec_[is] / ucell.omega; this->pphi_[is][ibs] = sqrt(this->chr.rho[is][ibs]); } } @@ -587,7 +587,7 @@ void ESolver_OF::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) { ModuleBase::matrix kinetic_stress_; kinetic_stress_.create(3, 3); - this->kedf_manager_->get_stress(this->pelec->omega, + this->kedf_manager_->get_stress(ucell.omega, this->chr.rho, this->pphi_, this->pw_rho, diff --git a/source/source_esolver/esolver_of_tool.cpp b/source/source_esolver/esolver_of_tool.cpp index 50c0ff6561..6e7ebbd7fb 100644 --- a/source/source_esolver/esolver_of_tool.cpp +++ b/source/source_esolver/esolver_of_tool.cpp @@ -22,7 +22,6 @@ void ESolver_OF::init_elecstate(UnitCell& ucell) this->pelec = new elecstate::ElecState((Charge*)(&chr), this->pw_rho, pw_big); this->chr.allocate(PARAM.inp.nspin); } - this->pelec->omega = ucell.omega; delete this->pelec->pot; this->pelec->pot = new elecstate::Potential(this->pw_rhod, diff --git a/source/source_estate/elecstate.h b/source/source_estate/elecstate.h index a59d2bf4c3..3ca6a28e1c 100644 --- a/source/source_estate/elecstate.h +++ b/source/source_estate/elecstate.h @@ -114,7 +114,6 @@ class ElecState std::string classname = "elecstate"; int iter = 0; ///< scf iteration - double omega = 0.0; ///< volume Potential* pot = nullptr; ///< pointer to potential Charge* charge = nullptr; ///< pointer to charge density const K_Vectors* klist = nullptr; ///< pointer to k points lists diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 1e24bbf89a..b6079df0aa 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -174,7 +174,7 @@ double ElecState::cal_delta_eband(const UnitCell& ucell) const deband0 = deband_aux; #endif - deband0 *= this->omega / this->charge->rhopw->nxyz; + deband0 *= ucell.omega / this->charge->rhopw->nxyz; // \int rho(r) v_{exx}(r) dr = 2 E_{exx}[rho] deband0 -= 2 * this->f_en.exx; // Peize Lin add 2017-10-16 @@ -246,7 +246,7 @@ double ElecState::cal_delta_escf() const assert(this->charge->rhopw->nxyz > 0); - descf *= this->omega / this->charge->rhopw->nxyz; + descf *= this->charge->rhopw->omega / this->charge->rhopw->nxyz; return descf; } diff --git a/source/source_estate/magnetism.cpp b/source/source_estate/magnetism.cpp index 3d42ce6062..a31bec1f79 100644 --- a/source/source_estate/magnetism.cpp +++ b/source/source_estate/magnetism.cpp @@ -23,8 +23,11 @@ void Magnetism::compute_mag(const double& omega, const double* const * rho, double* nelec_spin) { + assert(omega>0.0); assert(nxyz>0); + const double fac = omega / nxyz; + if (PARAM.inp.nspin==2) { this->tot_mag = 0.00; @@ -40,8 +43,8 @@ void Magnetism::compute_mag(const double& omega, Parallel_Reduce::reduce_pool(this->tot_mag); Parallel_Reduce::reduce_pool(this->abs_mag); #endif - this->tot_mag *= omega / nxyz; - this->abs_mag *= omega / nxyz; + this->tot_mag *= fac; + this->abs_mag *= fac; ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"Total magnetism (Bohr mag/cell)",this->tot_mag); ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"Absolute magnetism (Bohr mag/cell)",this->abs_mag); @@ -82,7 +85,7 @@ void Magnetism::compute_mag(const double& omega, #endif for(int i=0;i<3;i++) { - this->tot_mag_nc[i] *= omega/ nxyz; + this->tot_mag_nc[i] *= fac; // mohan add 2025-06-21 if( std::abs(this->tot_mag_nc[i]) < 1.0e-16) { @@ -90,7 +93,7 @@ void Magnetism::compute_mag(const double& omega, } } - this->abs_mag *= omega/ nxyz; + this->abs_mag *= fac; // mohan update 2025-06-21 ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"Total magnetism (Bohr mag/cell)", diff --git a/source/source_estate/setup_estate_pw.cpp b/source/source_estate/setup_estate_pw.cpp index 51bd14e8b1..d384b3a171 100644 --- a/source/source_estate/setup_estate_pw.cpp +++ b/source/source_estate/setup_estate_pw.cpp @@ -39,9 +39,6 @@ void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell } } - //! Set the cell volume variable in pelec - pelec->omega = ucell.omega; - //! Inititlize the charge density. chr.allocate(inp.nspin); diff --git a/source/source_lcao/setup_deepks.cpp b/source/source_lcao/setup_deepks.cpp index 460ce7bf76..0e421c7bc8 100644 --- a/source/source_lcao/setup_deepks.cpp +++ b/source/source_lcao/setup_deepks.cpp @@ -61,6 +61,26 @@ void Setup_DeePKS::before_runner(const UnitCell& ucell, // unitcell #endif } +template +void Setup_DeePKS::delta_e( + const K_Vectors &kv, + const Parallel_Orbitals &pv, // parallel orbitals + const Grid_Driver &gd, + const std::vector>& dm_vec, + const Input_para &inp) +{ +#ifdef __MLALGO + if (inp.deepks_scf) + { + this->ld.dpks_cal_e_delta_band(dm_vec, kv.get_nks()); + DeePKS_domain::update_dmr(kv.kvec_d, dm_vec, ucell, orb_, pv, gd, this->ld.dm_r); + estate->f_en.edeepks_scf = this->ld.E_delta - this->ld.e_delta_band; + estate->f_en.edeepks_delta = this->ld.E_delta; + } +#endif +} + + template void Setup_DeePKS::write_forces( diff --git a/source/source_lcao/setup_deepks.h b/source/source_lcao/setup_deepks.h index 5e9f05ee33..5df5ad5a2d 100644 --- a/source/source_lcao/setup_deepks.h +++ b/source/source_lcao/setup_deepks.h @@ -7,6 +7,7 @@ #include "source_basis/module_ao/ORB_read.h" // orb #include "source_basis/module_nao/two_center_integrator.h" // overlap_orb_alpha #include "source_cell/module_neighbor/sltk_grid_driver.h" // grid driver +#include "source_cell/klist.h" // k-points #ifdef __MLALGO #include "source_lcao/module_deepks/LCAO_deepks.h" // deepks @@ -42,6 +43,13 @@ class Setup_DeePKS TwoCenterIntegrator &overlap_orb_alpha, const Input_para &inp); + void delta_e( + const K_Vectors &kv, + const Parallel_Orbitals &pv, // parallel orbitals + const Grid_Driver &gd, + const std::vector>& dm_vec, + const Input_para &inp); + void write_forces( const ModuleBase::matrix &fcs, const ModuleBase::matrix &fvnl_dalpha, From a969207d1560eebb11e32fd9888ddd03355d9c8d Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 22 Oct 2025 20:42:17 +0800 Subject: [PATCH 31/81] remove exx redundant parameters in Hamilt_LCAO --- source/source_esolver/esolver_double_xc.cpp | 14 +++-------- source/source_esolver/esolver_ks_lcao.cpp | 19 +++----------- source/source_esolver/lcao_others.cpp | 14 +++-------- source/source_lcao/hamilt_lcao.cpp | 28 ++++++++++++++------- source/source_lcao/hamilt_lcao.h | 14 ++++------- 5 files changed, 35 insertions(+), 54 deletions(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index 50a52e6d8b..f5576e6299 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -153,16 +153,10 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) this->two_center_bundle_, this->orb_, DM, - this->deepks -#ifdef __EXX - , - istep, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->two_level_step : &this->exx_nao.exc->two_level_step, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->get_Hexxs() : nullptr, - GlobalC::exx_info.info_ri.real_number ? nullptr : &this->exx_nao.exc->get_Hexxs() -#endif - ); - } + this->deepks, + istep, + this->exx_nao); + } XC_Functional::set_xc_type(PARAM.inp.deepks_out_base); this->pelec_base->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm); diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index a0f1d1a89d..dc8e0e41b3 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -3,7 +3,6 @@ #include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_io/read_wfc_nao.h" #include "source_lcao/hs_matrix_k.hpp" // there may be multiple definitions if using hpp -#include "source_estate/cal_ux.h" #include "source_estate/module_charge/symmetry_rho.h" #include "source_lcao/LCAO_domain.h" // need DeePKS_init #include "source_lcao/module_dftu/dftu.h" @@ -200,15 +199,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) PARAM.globalv.gamma_only_local ? &(this->GG) : nullptr, PARAM.globalv.gamma_only_local ? nullptr : &(this->GK), ucell, this->gd, &this->pv, this->pelec->pot, this->kv, - two_center_bundle_, orb_, DM, this->deepks -#ifdef __EXX - , - istep, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->two_level_step : &this->exx_nao.exc->two_level_step, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->get_Hexxs() : nullptr, - GlobalC::exx_info.info_ri.real_number ? nullptr : &this->exx_nao.exc->get_Hexxs() -#endif - ); + two_center_bundle_, orb_, DM, this->deepks, istep, exx_nao); } // 9) for each ionic step, the overlap must be rebuilt @@ -319,10 +310,6 @@ void ESolver_KS_LCAO::cal_force(UnitCell& ucell, ModuleBase::matrix& for ModuleBase::timer::tick("ESolver_KS_LCAO", "cal_force"); } -//------------------------------------------------------------------------------ -//! the 7th function of ESolver_KS_LCAO: cal_stress -//! mohan add 2024-05-11 -//------------------------------------------------------------------------------ template void ESolver_KS_LCAO::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) { @@ -407,7 +394,8 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { // the following steps are only needed in the first outer exx loop exx_two_level_step - = GlobalC::exx_info.info_ri.real_number ? this->exx_nao.exd->two_level_step : this->exx_nao.exc->two_level_step; + = GlobalC::exx_info.info_ri.real_number ? + this->exx_nao.exd->two_level_step : this->exx_nao.exc->two_level_step; } #endif elecstate::setup_dm(ucell, estate, this->psi, this->chr, iter, exx_two_level_step); @@ -647,7 +635,6 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); } - template class ESolver_KS_LCAO; template class ESolver_KS_LCAO, double>; template class ESolver_KS_LCAO, std::complex>; diff --git a/source/source_esolver/lcao_others.cpp b/source/source_esolver/lcao_others.cpp index 1d4891954b..61a018a2de 100644 --- a/source/source_esolver/lcao_others.cpp +++ b/source/source_esolver/lcao_others.cpp @@ -185,16 +185,10 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) two_center_bundle_, orb_, DM, - this->deepks -#ifdef __EXX - , - istep, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->two_level_step : &this->exx_nao.exc->two_level_step, - GlobalC::exx_info.info_ri.real_number ? &this->exx_nao.exd->get_Hexxs() : nullptr, - GlobalC::exx_info.info_ri.real_number ? nullptr : &this->exx_nao.exc->get_Hexxs() -#endif - ); - } + this->deepks, + istep, + this->exx_nao); + } // for each ionic step, the overlap must be rebuilt diff --git a/source/source_lcao/hamilt_lcao.cpp b/source/source_lcao/hamilt_lcao.cpp index 389b9812cf..904ffa4396 100644 --- a/source/source_lcao/hamilt_lcao.cpp +++ b/source/source_lcao/hamilt_lcao.cpp @@ -41,6 +41,7 @@ #include "module_operator_lcao/td_pot_hybrid.h" #include "module_operator_lcao/veff_lcao.h" + namespace hamilt { @@ -80,15 +81,9 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in, - Setup_DeePKS &deepks -#ifdef __EXX - , - const int istep, - int* exx_two_level_step, - std::vector>>>* Hexxd, - std::vector>>>>* Hexxc -#endif -) + Setup_DeePKS &deepks, + const int istep, + Exx_NAO &exx_nao) { this->classname = "HamiltLCAO"; @@ -415,6 +410,21 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, #ifdef __EXX if (GlobalC::exx_info.info_global.cal_exx) { + int* exx_two_level_step = nullptr; + std::vector>>>* Hexxd = nullptr; + std::vector>>>>* Hexxc = nullptr; + + if(GlobalC::exx_info.info_ri.real_number) + { + exx_two_level_step = exx_nao.exd->two_level_step; + Hexxd = exx_nao.exd->get_Hexxs(); + } + else + { + exx_two_level_step = exx_nao.exc->two_level_step; + Hexxc = exx_nao.exc->get_Hexxs(); + } + // Peize Lin add 2016-12-03 // set xc type before the first cal of xc in pelec->init_scf // and calculate Cs, Vs diff --git a/source/source_lcao/hamilt_lcao.h b/source/source_lcao/hamilt_lcao.h index 7e888ded3d..3d62330446 100644 --- a/source/source_lcao/hamilt_lcao.h +++ b/source/source_lcao/hamilt_lcao.h @@ -19,6 +19,8 @@ #ifdef __EXX #include "source_lcao/module_ri/Exx_LRI.h" #endif + +#include "source_lcao/setup_exx.h" // for exx, mohan add 20251022 namespace hamilt { @@ -49,15 +51,9 @@ class HamiltLCAO : public Hamilt const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in, - Setup_DeePKS &deepks -#ifdef __EXX - , - const int istep, - int* exx_two_level_step = nullptr, - std::vector>>>* Hexxd = nullptr, - std::vector>>>>* Hexxc = nullptr -#endif - ); + Setup_DeePKS &deepks, + const int istep, + Exx_NAO &exx_nao); /** * @brief Constructor of vacuum Operators, only HR and SR will be initialed as empty HContainer From 9691f3d16cdc0e17628899111ad826812e04bcc6 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 22 Oct 2025 22:28:48 +0800 Subject: [PATCH 32/81] Fix circular dependencies of header files. --- source/source_lcao/hamilt_lcao.cpp | 8 +++--- source/source_lcao/hamilt_lcao.h | 1 + .../module_ri/Exx_LRI_interface.hpp | 28 ------------------- 3 files changed, 5 insertions(+), 32 deletions(-) diff --git a/source/source_lcao/hamilt_lcao.cpp b/source/source_lcao/hamilt_lcao.cpp index 904ffa4396..0e23d6683c 100644 --- a/source/source_lcao/hamilt_lcao.cpp +++ b/source/source_lcao/hamilt_lcao.cpp @@ -416,13 +416,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if(GlobalC::exx_info.info_ri.real_number) { - exx_two_level_step = exx_nao.exd->two_level_step; - Hexxd = exx_nao.exd->get_Hexxs(); + exx_two_level_step = &exx_nao.exd->two_level_step; + Hexxd = &exx_nao.exd->get_Hexxs(); } else { - exx_two_level_step = exx_nao.exc->two_level_step; - Hexxc = exx_nao.exc->get_Hexxs(); + exx_two_level_step = &exx_nao.exc->two_level_step; + Hexxc = &exx_nao.exc->get_Hexxs(); } // Peize Lin add 2016-12-03 diff --git a/source/source_lcao/hamilt_lcao.h b/source/source_lcao/hamilt_lcao.h index 3d62330446..45f77b2c84 100644 --- a/source/source_lcao/hamilt_lcao.h +++ b/source/source_lcao/hamilt_lcao.h @@ -21,6 +21,7 @@ #endif #include "source_lcao/setup_exx.h" // for exx, mohan add 20251022 + namespace hamilt { diff --git a/source/source_lcao/module_ri/Exx_LRI_interface.hpp b/source/source_lcao/module_ri/Exx_LRI_interface.hpp index f7807b8ef6..89db3e5417 100644 --- a/source/source_lcao/module_ri/Exx_LRI_interface.hpp +++ b/source/source_lcao/module_ri/Exx_LRI_interface.hpp @@ -4,7 +4,6 @@ #include "Exx_LRI_interface.h" #include "source_lcao/module_ri/exx_abfs-jle.h" -#include "source_lcao/hamilt_lcao.h" #include "source_lcao/module_operator_lcao/op_exx_lcao.h" #include "source_base/parallel_common.h" #include "source_base/formatter.h" @@ -17,33 +16,6 @@ #include #include -/* -template -void Exx_LRI_Interface::write_Hexxs_cereal(const std::string& file_name) const -{ - ModuleBase::TITLE("Exx_LRI_Interface", "write_Hexxs_cereal"); - ModuleBase::timer::tick("Exx_LRI_Interface", "write_Hexxs_cereal"); - std::ofstream ofs(file_name + "_" + std::to_string(GlobalV::MY_RANK), std::ofstream::binary); - cereal::BinaryOutputArchive oar(ofs); - oar(this->exx_ptr->Hexxs); - ModuleBase::timer::tick("Exx_LRI_Interface", "write_Hexxs_cereal"); -} - -template -void Exx_LRI_Interface::read_Hexxs_cereal(const std::string& file_name) -{ - ModuleBase::TITLE("Exx_LRI_Interface", "read_Hexxs_cereal"); - ModuleBase::timer::tick("Exx_LRI_Interface", "read_Hexxs_cereal"); - const std::string file_name_rank = file_name + "_" + std::to_string(GlobalV::MY_RANK); - std::ifstream ifs(file_name_rank, std::ofstream::binary); - if(!ifs.is_open()) - { ModuleBase::WARNING_QUIT("Exx_LRI_Interface", file_name_rank+" not found."); } - cereal::BinaryInputArchive iar(ifs); - iar(this->exx_ptr->Hexxs); - ModuleBase::timer::tick("Exx_LRI_Interface", "read_Hexxs_cereal"); -} -*/ - template void Exx_LRI_Interface::init(const MPI_Comm &mpi_comm, const UnitCell &ucell, From b997a539b4f34676b44c572381e3427f9ed6b16a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 22 Oct 2025 23:08:23 +0800 Subject: [PATCH 33/81] fix bugs in MLALGO --- source/source_esolver/esolver_ks_lcao.cpp | 2 +- source/source_lcao/setup_deepks.cpp | 5 +++-- source/source_lcao/setup_deepks.h | 5 +++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 9af8ccf127..f7578b4255 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -550,7 +550,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // 2) for deepks, calculate delta_e, output labels during electronic steps - this->deepks.delta_e(this->kv, this->pv, this->gd, dm_vec, PARAM.inp); + this->deepks.delta_e(this->kv, this->pv, this->gd, dm_vec, this->pelec->f_en, PARAM.inp); // 3) for delta spin if (PARAM.inp.sc_mag_switch) diff --git a/source/source_lcao/setup_deepks.cpp b/source/source_lcao/setup_deepks.cpp index 0e421c7bc8..ea2308c57b 100644 --- a/source/source_lcao/setup_deepks.cpp +++ b/source/source_lcao/setup_deepks.cpp @@ -67,6 +67,7 @@ void Setup_DeePKS::delta_e( const Parallel_Orbitals &pv, // parallel orbitals const Grid_Driver &gd, const std::vector>& dm_vec, + elecstate::fenergy &f_en, const Input_para &inp) { #ifdef __MLALGO @@ -74,8 +75,8 @@ void Setup_DeePKS::delta_e( { this->ld.dpks_cal_e_delta_band(dm_vec, kv.get_nks()); DeePKS_domain::update_dmr(kv.kvec_d, dm_vec, ucell, orb_, pv, gd, this->ld.dm_r); - estate->f_en.edeepks_scf = this->ld.E_delta - this->ld.e_delta_band; - estate->f_en.edeepks_delta = this->ld.E_delta; + f_en.edeepks_scf = this->ld.E_delta - this->ld.e_delta_band; + f_en.edeepks_delta = this->ld.E_delta; } #endif } diff --git a/source/source_lcao/setup_deepks.h b/source/source_lcao/setup_deepks.h index 5df5ad5a2d..25470520ee 100644 --- a/source/source_lcao/setup_deepks.h +++ b/source/source_lcao/setup_deepks.h @@ -8,6 +8,10 @@ #include "source_basis/module_nao/two_center_integrator.h" // overlap_orb_alpha #include "source_cell/module_neighbor/sltk_grid_driver.h" // grid driver #include "source_cell/klist.h" // k-points +#include "source_cell/unitcell.h" // use unitcell +#include "source_basis/module_ao/ORB_read.h" // LCAO_Orbitals +#include "source_estate/fp_energy.h" // fp energy + #ifdef __MLALGO #include "source_lcao/module_deepks/LCAO_deepks.h" // deepks @@ -48,6 +52,7 @@ class Setup_DeePKS const Parallel_Orbitals &pv, // parallel orbitals const Grid_Driver &gd, const std::vector>& dm_vec, + elecstate::fenergy &f_en, const Input_para &inp); void write_forces( From 244acfa16d1e07ef15b1fcbb0cf9a34b108f3ed2 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 23 Oct 2025 08:16:37 +0800 Subject: [PATCH 34/81] refactor exx lcao --- source/source_esolver/esolver_ks_lcao.cpp | 15 +----------- source/source_lcao/setup_exx.cpp | 29 +++++++++++++++++++++++ source/source_lcao/setup_exx.h | 9 +++++++ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index f7578b4255..278d34e446 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_estate/elecstate_tools.h" #include "source_lcao/module_deltaspin/spin_constrain.h" -#include "source_io/read_wfc_nao.h" #include "source_lcao/hs_matrix_k.hpp" // there may be multiple definitions if using hpp #include "source_estate/module_charge/symmetry_rho.h" #include "source_lcao/LCAO_domain.h" // need DeePKS_init @@ -214,19 +213,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) } // 11) set xc type before the first cal of xc in pelec->init_scf, Peize Lin add 2016-12-03 -#ifdef __EXX - if (PARAM.inp.calculation != "nscf") - { - if (GlobalC::exx_info.info_ri.real_number) - { - this->exx_nao.exd->exx_beforescf(istep, this->kv, *this->p_chgmix, ucell, orb_); - } - else - { - this->exx_nao.exc->exx_beforescf(istep, this->kv, *this->p_chgmix, ucell, orb_); - } - } -#endif + this->exx_nao.before_scf(ucell, this->kv, orb_, this->p_chgmix, istep, PARAM.inp); // 12) init_scf, should be before_scf? mohan add 2025-03-10 this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm); diff --git a/source/source_lcao/setup_exx.cpp b/source/source_lcao/setup_exx.cpp index aeabfaac36..f552ed06df 100644 --- a/source/source_lcao/setup_exx.cpp +++ b/source/source_lcao/setup_exx.cpp @@ -62,5 +62,34 @@ void Exx_NAO::before_runner( #endif } +template +void Exx_NAO::before_scf( + const UnitCell &ucell, // unitcell + const K_Vectors &kv, + const LCAO_Orbitals &orb, // orbital info + Charge_Mixing* p_chgmix, + const int istep, + const Input_para& inp) +{ +#ifdef __EXX + if (PARAM.inp.calculation != "nscf") + { + if (GlobalC::exx_info.info_ri.real_number) + { + this->exd->exx_beforescf(istep, kv, *p_chgmix, ucell, orb); + } + else + { + this->exc->exx_beforescf(istep, kv, *p_chgmix, ucell, orb); + } + } + else + { + // do nothing + } +#endif +} + + template class Exx_NAO; template class Exx_NAO>; diff --git a/source/source_lcao/setup_exx.h b/source/source_lcao/setup_exx.h index 33154ba8e6..9805ff8c91 100644 --- a/source/source_lcao/setup_exx.h +++ b/source/source_lcao/setup_exx.h @@ -6,6 +6,7 @@ #include "source_io/module_parameter/input_parameter.h" // Input_para #include "source_basis/module_ao/parallel_orbitals.h" // parallel orbitals #include "source_basis/module_ao/ORB_read.h" // orb +#include "source_estate/module_charge/charge_mixing.h" // use charge mixing // for EXX #ifdef __EXX @@ -35,6 +36,14 @@ class Exx_NAO const Parallel_Orbitals &pv, // parallel orbitals const Input_para& inp); + void before_scf( + const UnitCell &ucell, // unitcell + const K_Vectors &kv, + const LCAO_Orbitals &orb, // orbital info + Charge_Mixing* p_chgmix, + const int istep, + const Input_para& inp); + }; From a49c4092333bcdde38d1776a7abe4db0bf5f10fc Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 23 Oct 2025 09:22:07 +0800 Subject: [PATCH 35/81] add rho_tau_lcao files --- source/Makefile.Objects | 3 +- source/source_estate/CMakeLists.txt | 1 + .../source_estate/module_dm/density_matrix.h | 5 ++- source/source_estate/rho_tau_lcao.cpp | 45 +++++++++++++++++++ source/source_estate/rho_tau_lcao.h | 16 +++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 source/source_estate/rho_tau_lcao.cpp create mode 100644 source/source_estate/rho_tau_lcao.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 8353a0f754..78baeb45f1 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -228,11 +228,12 @@ OBJS_ELECSTAT=elecstate.o\ elecstate_energy.o\ elecstate_exx.o\ elecstate_print.o\ + elecstate_tools.o\ elecstate_pw.o\ elecstate_pw_sdft.o\ elecstate_pw_cal_tau.o\ + rho_tau_lcao.o\ elecstate_op.o\ - elecstate_tools.o\ efield.o\ gatefield.o\ potential_new.o\ diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index 8a54110e55..85d74e589c 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -8,6 +8,7 @@ list(APPEND objects elecstate_pw.cpp elecstate_pw_sdft.cpp elecstate_pw_cal_tau.cpp + rho_tau_lcao.cpp module_pot/gatefield.cpp module_pot/efield.cpp module_pot/H_Hartree_pw.cpp diff --git a/source/source_estate/module_dm/density_matrix.h b/source/source_estate/module_dm/density_matrix.h index 5716340e07..605e7b6acd 100644 --- a/source/source_estate/module_dm/density_matrix.h +++ b/source/source_estate/module_dm/density_matrix.h @@ -51,7 +51,10 @@ class DensityMatrix * @param nk number of k-points, not always equal to K_Vectors::get_nks()/nspin_dm. * it will be set to kvec_d.size() if the value is invalid */ - DensityMatrix(const Parallel_Orbitals* _paraV, const int nspin, const std::vector>& kvec_d, const int nk); + DensityMatrix(const Parallel_Orbitals* _paraV, + const int nspin, + const std::vector>& kvec_d, + const int nk); /** * @brief Constructor of class DensityMatrix for gamma-only calculation, where kvector is not required diff --git a/source/source_estate/rho_tau_lcao.cpp b/source/source_estate/rho_tau_lcao.cpp new file mode 100644 index 0000000000..8731eaff03 --- /dev/null +++ b/source/source_estate/rho_tau_lcao.cpp @@ -0,0 +1,45 @@ +#include "rho_tau_lcao.h" +#include "source_estate/module_dm/density_matrix.h" // use density matrix + +namespace elecstate +{ + +// gamma only case +void psi2tau_lcao(std::vector*> &dmr, + const int nspin, + Charge* chg) +{ + if (XC_Functional::get_ked_flag()) + { + for (int is = 0; is < nspin; is++) + { + ModuleBase::GlobalFunc::ZEROS(chg->kin_r[is], chg->nrxx); + } + ModuleGint::cal_gint_tau(dmr, nspin, chg->kin_r); + } +} + +// multi-k case +void psi2rho_lcao(std::vector*> &dmr, + const int nspin, + Charge* chg) +{ + ModuleBase::TITLE("elecstate", "psi2rho_lcao"); + ModuleBase::timer::tick("elecstate", "psi2rho_lcao"); + + for (int is = 0; is < nspin; is++) + { + ModuleBase::GlobalFunc::ZEROS(chg->rho[is], chg->nrxx); + } + + ModuleGint::cal_gint_rho(dmr, inp.nspin, chg->rho); + + charge->renormalize_rho(); + + // symmetrize of charge density should be here, mohan 20251023 + + ModuleBase::timer::tick("elecstate", "psi2rho_lcao"); + return; +} + +}// end namespace diff --git a/source/source_estate/rho_tau_lcao.h b/source/source_estate/rho_tau_lcao.h new file mode 100644 index 0000000000..faf77f277e --- /dev/null +++ b/source/source_estate/rho_tau_lcao.h @@ -0,0 +1,16 @@ +#ifndef RHO_TAU_LCAO_H +#define RHO_TAU_LCAO_H + +// generate charge density from different basis or methods +namespace elecstate +{ + // interface for HSolver to calculate rho from Psi + void psi2rho_lcao(const psi::Psi>& psi, + std::vector*> &dmr, + const int nspin, + Charge* chg); + + +} + +#endif From d309cbaf2900e312a7145407f7609c4a61b2b983 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 24 Oct 2025 08:07:55 +0800 Subject: [PATCH 36/81] fix bug in setup_deepks --- source/source_esolver/esolver_ks_lcao.cpp | 2 +- source/source_lcao/setup_deepks.cpp | 6 ++++-- source/source_lcao/setup_deepks.h | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 278d34e446..894d16b63b 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -537,7 +537,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // 2) for deepks, calculate delta_e, output labels during electronic steps - this->deepks.delta_e(this->kv, this->pv, this->gd, dm_vec, this->pelec->f_en, PARAM.inp); + this->deepks.delta_e(ucell, this->kv, this->orb_, this->pv, this->gd, dm_vec, this->pelec->f_en, PARAM.inp); // 3) for delta spin if (PARAM.inp.sc_mag_switch) diff --git a/source/source_lcao/setup_deepks.cpp b/source/source_lcao/setup_deepks.cpp index ea2308c57b..baa776f0a2 100644 --- a/source/source_lcao/setup_deepks.cpp +++ b/source/source_lcao/setup_deepks.cpp @@ -63,7 +63,9 @@ void Setup_DeePKS::before_runner(const UnitCell& ucell, // unitcell template void Setup_DeePKS::delta_e( - const K_Vectors &kv, + const UnitCell& ucell, + const K_Vectors &kv, + const LCAO_Orbitals& orb, const Parallel_Orbitals &pv, // parallel orbitals const Grid_Driver &gd, const std::vector>& dm_vec, @@ -74,7 +76,7 @@ void Setup_DeePKS::delta_e( if (inp.deepks_scf) { this->ld.dpks_cal_e_delta_band(dm_vec, kv.get_nks()); - DeePKS_domain::update_dmr(kv.kvec_d, dm_vec, ucell, orb_, pv, gd, this->ld.dm_r); + DeePKS_domain::update_dmr(kv.kvec_d, dm_vec, ucell, orb, pv, gd, this->ld.dm_r); f_en.edeepks_scf = this->ld.E_delta - this->ld.e_delta_band; f_en.edeepks_delta = this->ld.E_delta; } diff --git a/source/source_lcao/setup_deepks.h b/source/source_lcao/setup_deepks.h index 25470520ee..f4a5ad5f42 100644 --- a/source/source_lcao/setup_deepks.h +++ b/source/source_lcao/setup_deepks.h @@ -48,7 +48,9 @@ class Setup_DeePKS const Input_para &inp); void delta_e( + const UnitCell& ucell, const K_Vectors &kv, + const LCAO_Orbitals& orb, const Parallel_Orbitals &pv, // parallel orbitals const Grid_Driver &gd, const std::vector>& dm_vec, From b8e0d854d9a692d92820b78d4117fd577b4238ea Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 24 Oct 2025 08:34:09 +0800 Subject: [PATCH 37/81] move rho_tau_lcao to source_lcao --- source/Makefile.Objects | 2 +- source/source_estate/CMakeLists.txt | 1 - source/source_estate/rho_tau_lcao.cpp | 45 ------------------ source/source_estate/rho_tau_lcao.h | 16 ------- source/source_lcao/CMakeLists.txt | 1 + .../module_hcontainer/hcontainer.h | 6 +-- source/source_lcao/rho_tau_lcao.cpp | 47 +++++++++++++++++++ source/source_lcao/rho_tau_lcao.h | 20 ++++++++ 8 files changed, 72 insertions(+), 66 deletions(-) delete mode 100644 source/source_estate/rho_tau_lcao.cpp delete mode 100644 source/source_estate/rho_tau_lcao.h create mode 100644 source/source_lcao/rho_tau_lcao.cpp create mode 100644 source/source_lcao/rho_tau_lcao.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 78baeb45f1..acb66ac49d 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -232,7 +232,6 @@ OBJS_ELECSTAT=elecstate.o\ elecstate_pw.o\ elecstate_pw_sdft.o\ elecstate_pw_cal_tau.o\ - rho_tau_lcao.o\ elecstate_op.o\ efield.o\ gatefield.o\ @@ -644,6 +643,7 @@ OBJS_LCAO=evolve_elec.o\ LCAO_init_basis.o\ setup_exx.o\ setup_deepks.o\ + rho_tau_lcao.o\ center2_orb.o\ center2_orb-orb11.o\ center2_orb-orb21.o\ diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index 85d74e589c..8a54110e55 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -8,7 +8,6 @@ list(APPEND objects elecstate_pw.cpp elecstate_pw_sdft.cpp elecstate_pw_cal_tau.cpp - rho_tau_lcao.cpp module_pot/gatefield.cpp module_pot/efield.cpp module_pot/H_Hartree_pw.cpp diff --git a/source/source_estate/rho_tau_lcao.cpp b/source/source_estate/rho_tau_lcao.cpp deleted file mode 100644 index 8731eaff03..0000000000 --- a/source/source_estate/rho_tau_lcao.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "rho_tau_lcao.h" -#include "source_estate/module_dm/density_matrix.h" // use density matrix - -namespace elecstate -{ - -// gamma only case -void psi2tau_lcao(std::vector*> &dmr, - const int nspin, - Charge* chg) -{ - if (XC_Functional::get_ked_flag()) - { - for (int is = 0; is < nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(chg->kin_r[is], chg->nrxx); - } - ModuleGint::cal_gint_tau(dmr, nspin, chg->kin_r); - } -} - -// multi-k case -void psi2rho_lcao(std::vector*> &dmr, - const int nspin, - Charge* chg) -{ - ModuleBase::TITLE("elecstate", "psi2rho_lcao"); - ModuleBase::timer::tick("elecstate", "psi2rho_lcao"); - - for (int is = 0; is < nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(chg->rho[is], chg->nrxx); - } - - ModuleGint::cal_gint_rho(dmr, inp.nspin, chg->rho); - - charge->renormalize_rho(); - - // symmetrize of charge density should be here, mohan 20251023 - - ModuleBase::timer::tick("elecstate", "psi2rho_lcao"); - return; -} - -}// end namespace diff --git a/source/source_estate/rho_tau_lcao.h b/source/source_estate/rho_tau_lcao.h deleted file mode 100644 index faf77f277e..0000000000 --- a/source/source_estate/rho_tau_lcao.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef RHO_TAU_LCAO_H -#define RHO_TAU_LCAO_H - -// generate charge density from different basis or methods -namespace elecstate -{ - // interface for HSolver to calculate rho from Psi - void psi2rho_lcao(const psi::Psi>& psi, - std::vector*> &dmr, - const int nspin, - Charge* chg); - - -} - -#endif diff --git a/source/source_lcao/CMakeLists.txt b/source/source_lcao/CMakeLists.txt index 118e877239..dbd2113bfb 100644 --- a/source/source_lcao/CMakeLists.txt +++ b/source/source_lcao/CMakeLists.txt @@ -42,6 +42,7 @@ if(ENABLE_LCAO) LCAO_init_basis.cpp setup_exx.cpp setup_deepks.cpp + rho_tau_lcao.cpp record_adj.cpp center2_orb.cpp center2_orb-orb11.cpp diff --git a/source/source_lcao/module_hcontainer/hcontainer.h b/source/source_lcao/module_hcontainer/hcontainer.h index 7592273fdb..92baed942a 100644 --- a/source/source_lcao/module_hcontainer/hcontainer.h +++ b/source/source_lcao/module_hcontainer/hcontainer.h @@ -1,5 +1,5 @@ -#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_HAMILT_LCAO_MODULE_HCONTAINER_HCONTAINER_H -#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_HAMILT_LCAO_MODULE_HCONTAINER_HCONTAINER_H +#ifndef HCONTAINER_H +#define HCONTAINER_H #include "atom_pair.h" #include "source_base/vector3.h" @@ -507,4 +507,4 @@ class HContainer } // namespace hamilt -#endif \ No newline at end of file +#endif diff --git a/source/source_lcao/rho_tau_lcao.cpp b/source/source_lcao/rho_tau_lcao.cpp new file mode 100644 index 0000000000..39bc344425 --- /dev/null +++ b/source/source_lcao/rho_tau_lcao.cpp @@ -0,0 +1,47 @@ +#include "rho_tau_lcao.h" +#include "source_hamilt/module_xc/xc_functional.h" +#include "source_lcao/module_gint/gint_interface.h" + +void LCAO_domain::dm2rho(std::vector*> &dmr, + const int nspin, + Charge* chg) +{ + ModuleBase::TITLE("LCAO_domain", "dm2rho"); + ModuleBase::timer::tick("LCAO_domain", "dm2rho"); + + for (int is = 0; is < nspin; is++) + { + ModuleBase::GlobalFunc::ZEROS(chg->rho[is], chg->nrxx); + } + + ModuleGint::cal_gint_rho(dmr, nspin, chg->rho); + + chg->renormalize_rho(); + + // symmetrize of charge density should be here, mohan 20251023 + + ModuleBase::timer::tick("LCAO_domain", "dm2rho"); + return; +} + + +void LCAO_domain::dm2tau(std::vector*> &dmr, + const int nspin, + Charge* chg) +{ + ModuleBase::TITLE("LCAO_domain", "dm2tau"); + ModuleBase::timer::tick("LCAO_domain", "dm2tau"); + + if (XC_Functional::get_ked_flag()) + { + for (int is = 0; is < nspin; is++) + { + ModuleBase::GlobalFunc::ZEROS(chg->kin_r[is], chg->nrxx); + } + ModuleGint::cal_gint_tau(dmr, nspin, chg->kin_r); + } + + ModuleBase::timer::tick("LCAO_domain", "dm2tau"); +} + + diff --git a/source/source_lcao/rho_tau_lcao.h b/source/source_lcao/rho_tau_lcao.h new file mode 100644 index 0000000000..59de61cbc0 --- /dev/null +++ b/source/source_lcao/rho_tau_lcao.h @@ -0,0 +1,20 @@ +#ifndef RHO_TAU_LCAO_H +#define RHO_TAU_LCAO_H + +#include "source_lcao/module_hcontainer/hcontainer.h" +#include "source_estate/module_charge/charge.h" + +// generate charge density from different basis or methods +namespace LCAO_domain +{ + void dm2rho(std::vector*> &dmr, + const int nspin, + Charge* chg); + + void dm2tau(std::vector*> &dmr, + const int nspin, + Charge* chg); + +} + +#endif From e493841cc2d50eae427c0c4cccb5d0f4c2c18009 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 24 Oct 2025 09:14:39 +0800 Subject: [PATCH 38/81] remove old psiToRho function in elecstate_lcao and replace with new rho_tau_lcao --- source/source_esolver/esolver_dm2rho.cpp | 14 +++- source/source_esolver/esolver_ks_lcao.cpp | 2 +- .../source_esolver/esolver_ks_lcao_tddft.cpp | 9 ++- source/source_estate/elecstate_lcao.cpp | 70 ++----------------- source/source_estate/elecstate_lcao.h | 20 +----- .../source_estate/elecstate_lcao_cal_tau.cpp | 5 +- source/source_hsolver/hsolver_lcao.cpp | 11 +-- source/source_hsolver/hsolver_lcao.h | 8 ++- .../module_deltaspin/cal_mw_from_lambda.cpp | 3 +- source/source_lcao/rho_tau_lcao.cpp | 19 ++--- source/source_lcao/rho_tau_lcao.h | 4 +- 11 files changed, 53 insertions(+), 112 deletions(-) diff --git a/source/source_esolver/esolver_dm2rho.cpp b/source/source_esolver/esolver_dm2rho.cpp index 07b458206e..291da30f1b 100644 --- a/source/source_esolver/esolver_dm2rho.cpp +++ b/source/source_esolver/esolver_dm2rho.cpp @@ -10,6 +10,7 @@ #include "source_io/cube_io.h" #include "source_io/io_npz.h" #include "source_io/print_info.h" +#include "source_lcao/rho_tau_lcao.h" // mohan add 2025-10-24 namespace ModuleESolver { @@ -45,9 +46,16 @@ void ESolver_DM2rho::runner(UnitCell& ucell, const int istep) ESolver_KS_LCAO::before_scf(ucell, istep); + auto* estate = dynamic_cast*>(this->pelec); + + if(!estate) + { + ModuleBase::WARNING_QUIT("ESolver_DM2rho::after_scf","pelec does not exist"); + } + // file name of DM std::string zipname = "output_DM0.npz"; - elecstate::DensityMatrix* dm = dynamic_cast*>(this->pelec)->get_DM(); + elecstate::DensityMatrix* dm = estate->get_DM(); // read DM from file ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(dm->get_DMR_pointer(1))); @@ -59,7 +67,9 @@ void ESolver_DM2rho::runner(UnitCell& ucell, const int istep) ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(dm->get_DMR_pointer(2))); } - this->pelec->psiToRho(*this->psi); + // it's dangrous to design psiToRho function like this, mohan note 20251024 + // this->pelec->psiToRho(*this->psi); + LCAO_domain::dm2rho(estate->DM->get_DMR_vector(), PARAM.inp.nspin, &this->chr); int nspin0 = PARAM.inp.nspin == 2 ? 2 : 1; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 894d16b63b..c8ef7f44c5 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -469,7 +469,7 @@ void ESolver_KS_LCAO::hamilt2rho_single(UnitCell& ucell, int istep, int if (!skip_solve) { hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), PARAM.inp.ks_solver); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, skip_charge); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, this->chr, PARAM.inp.nspin, skip_charge); } // 4) EXX diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index 7c047774b4..a5ecf82fa8 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -31,10 +31,9 @@ #include "source_lcao/hamilt_lcao.h" #include "source_psi/psi.h" -//-----force& stress------------------- #include "source_lcao/FORCE_STRESS.h" +#include "source_lcao/rho_tau_lcao.h" // mohan add 2025-10-24 -//--------------------------------------------------- namespace ModuleESolver { @@ -293,7 +292,7 @@ void ESolver_KS_LCAO_TDDFT::hamilt2rho_single(UnitCell& ucell, { bool skip_charge = PARAM.inp.calculation == "nscf" ? true : false; hsolver::HSolverLCAO> hsolver_lcao_obj(&this->pv, PARAM.inp.ks_solver); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, skip_charge); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, this->chr, PARAM.inp.nspin, skip_charge); } } @@ -574,8 +573,8 @@ void ESolver_KS_LCAO_TDDFT::weight_dm_rho(const UnitCell& ucell) _pes->DM->cal_DMR(); } - // get the real-space charge density - this->pelec->psiToRho(this->psi[0]); + // get the real-space charge density, mohan add 2025-10-24 + LCAO_domain::dm2rho(_pes->DM->get_DMR_vector(), PARAM.inp.nspin, &this->chr); } template class ESolver_KS_LCAO_TDDFT; diff --git a/source/source_estate/elecstate_lcao.cpp b/source/source_estate/elecstate_lcao.cpp index fe3bb11758..44500941d6 100644 --- a/source/source_estate/elecstate_lcao.cpp +++ b/source/source_estate/elecstate_lcao.cpp @@ -15,68 +15,6 @@ namespace elecstate { -// multi-k case -template <> -void ElecStateLCAO>::psiToRho(const psi::Psi>& psi) -{ - ModuleBase::TITLE("ElecStateLCAO", "psiToRho"); - ModuleBase::timer::tick("ElecStateLCAO", "psiToRho"); - - for (int is = 0; is < PARAM.inp.nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(this->charge->rho[is], - this->charge->nrxx); // mohan 2009-11-10 - } - - //------------------------------------------------------------ - // calculate the charge density on real space grid. - //------------------------------------------------------------ - - ModuleBase::GlobalFunc::NOTE("Calculate the charge on real space grid!"); - ModuleGint::cal_gint_rho(this->DM->get_DMR_vector(), PARAM.inp.nspin, this->charge->rho); - - if (XC_Functional::get_ked_flag()) - { - this->cal_tau(psi); - } - - this->charge->renormalize_rho(); - - ModuleBase::timer::tick("ElecStateLCAO", "psiToRho"); - return; -} - -// Gamma_only case -template <> -void ElecStateLCAO::psiToRho(const psi::Psi& psi) -{ - ModuleBase::TITLE("ElecStateLCAO", "psiToRho"); - ModuleBase::timer::tick("ElecStateLCAO", "psiToRho"); - - for (int is = 0; is < PARAM.inp.nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(this->charge->rho[is], - this->charge->nrxx); // mohan 2009-11-10 - } - - //------------------------------------------------------------ - // calculate the charge density on real space grid. - //------------------------------------------------------------ - ModuleBase::GlobalFunc::NOTE("Calculate the charge on real space grid!"); - - ModuleGint::cal_gint_rho(this->DM->get_DMR_vector(), PARAM.inp.nspin, this->charge->rho); - - if (XC_Functional::get_ked_flag()) - { - this->cal_tau(psi); - } - - this->charge->renormalize_rho(); - - ModuleBase::timer::tick("ElecStateLCAO", "psiToRho"); - return; -} - template void ElecStateLCAO::init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) { @@ -101,9 +39,9 @@ double ElecStateLCAO>::get_spin_constrain_energy() #ifdef __PEXSI template <> -void ElecStateLCAO::dmToRho(std::vector pexsi_DM, std::vector pexsi_EDM) +void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, std::vector pexsi_EDM) { - ModuleBase::timer::tick("ElecStateLCAO", "dmToRho"); + ModuleBase::timer::tick("ElecStateLCAO", "dm2Rho"); int nspin = PARAM.inp.nspin; if (PARAM.inp.nspin == 4) @@ -138,12 +76,12 @@ void ElecStateLCAO::dmToRho(std::vector pexsi_DM, std::vectorcharge->renormalize_rho(); - ModuleBase::timer::tick("ElecStateLCAO", "dmToRho"); + ModuleBase::timer::tick("ElecStateLCAO", "dm2Rho"); return; } template <> -void ElecStateLCAO>::dmToRho(std::vector*> pexsi_DM, +void ElecStateLCAO>::dm2Rho(std::vector*> pexsi_DM, std::vector*> pexsi_EDM) { ModuleBase::WARNING_QUIT("ElecStateLCAO", "pexsi is not completed for multi-k case"); diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index 7c19f2c39b..0b1d6cd9b7 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -33,15 +33,6 @@ class ElecStateLCAO : public ElecState } } - // void init(Charge* chg_in):charge(chg_in){} override; - - // interface for HSolver to calculate rho from Psi - virtual void psiToRho(const psi::Psi& psi) override; - // virtual void psiToRho(const psi::Psi& psi) override; - // return current electronic density rho, as a input for constructing Hamiltonian - // const double* getRho(int spin) const override; - virtual void cal_tau(const psi::Psi& psi) override; - // update charge density for next scf step // void getNewRho() override; @@ -65,20 +56,11 @@ class ElecStateLCAO : public ElecState * @param pexsi_EDM: pointers of energy-weighed density matrix (EDMK) calculated by pexsi, needed by MD, will be * stored in DensityMatrix::pexsi_EDM */ - void dmToRho(std::vector pexsi_DM, std::vector pexsi_EDM); + void dm2Rho(std::vector pexsi_DM, std::vector pexsi_EDM); #endif DensityMatrix* DM = nullptr; - protected: - // calculate electronic charge density on grid points or density matrix in real space - // the consequence charge density rho saved into rho_out, preparing for charge mixing. - // void updateRhoK(const psi::Psi>& psi) ;//override; - // sum over all pools for rho and ebands - // void parallelK(); - // calcualte rho for each k - // void rhoBandK(const psi::Psi>& psi); - }; template diff --git a/source/source_estate/elecstate_lcao_cal_tau.cpp b/source/source_estate/elecstate_lcao_cal_tau.cpp index a2a4210002..7e0544c2d0 100644 --- a/source/source_estate/elecstate_lcao_cal_tau.cpp +++ b/source/source_estate/elecstate_lcao_cal_tau.cpp @@ -6,6 +6,7 @@ namespace elecstate { +/* // calculate the kinetic energy density tau, multi-k case template <> void ElecStateLCAO>::cal_tau(const psi::Psi>& psi) @@ -36,4 +37,6 @@ void ElecStateLCAO::cal_tau(const psi::Psi& psi) ModuleBase::timer::tick("ElecStateLCAO", "cal_tau"); return; } -} \ No newline at end of file +*/ + +} diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index a9d940216b..091bf15771 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -34,6 +34,8 @@ #include "source_hsolver/parallel_k2d.h" #include "source_io/module_parameter/parameter.h" +#include "source_lcao/rho_tau_lcao.h" // mohan add 20251024 + namespace hsolver { @@ -41,6 +43,8 @@ template void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, psi::Psi& psi, elecstate::ElecState* pes, + Charge &chr, + const int nspin, const bool skip_charge) { ModuleBase::TITLE("HSolverLCAO", "solve"); @@ -97,9 +101,8 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, if (!skip_charge) { - // used in scf calculation - // calculate charge by eigenpairs(eigenvalues and eigenvectors) - pes->psiToRho(psi); + // compute charge density from density matrix, mohan update 20251024 + LCAO_domain::dm2rho(_pes_lcao->DM->get_DMR_vector(), nspin, &chr); } else { @@ -492,4 +495,4 @@ void HSolverLCAO::parakSolve_cusolver(hamilt::Hamilt* pHamilt, template class HSolverLCAO; template class HSolverLCAO>; -} // namespace hsolver \ No newline at end of file +} // namespace hsolver diff --git a/source/source_hsolver/hsolver_lcao.h b/source/source_hsolver/hsolver_lcao.h index a1905fbcac..67ac1fe32a 100644 --- a/source/source_hsolver/hsolver_lcao.h +++ b/source/source_hsolver/hsolver_lcao.h @@ -5,6 +5,8 @@ #include "source_hamilt/hamilt.h" #include "source_basis/module_ao/parallel_orbitals.h" +#include "source_estate/module_charge/charge.h" // mohan add 20251024 + namespace hsolver { @@ -17,7 +19,9 @@ class HSolverLCAO void solve(hamilt::Hamilt* pHamilt, psi::Psi& psi, elecstate::ElecState* pes, - const bool skip_charge); + Charge &chr, // charge density + const int nspin, + const bool skip_charge); private: void hamiltSolvePsiK(hamilt::Hamilt* hm, psi::Psi& psi, double* eigenvalue); // for kpar_lcao == 1 @@ -36,4 +40,4 @@ class HSolverLCAO } // namespace hsolver -#endif \ No newline at end of file +#endif diff --git a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp index 3a83289cb7..515a89ab9c 100644 --- a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp @@ -151,7 +151,8 @@ void spinconstrain::SpinConstrain>::cal_mw_from_lambda(int ->update_lambda(); } // diagonalization without update charge - hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, true); + // mohan add two parameters charge and nspin, 2025-10-24 + hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, *this->pelec->charge, PARAM.inp.nspin, true); elecstate::calculate_weights(this->pelec->ekb, this->pelec->wg, this->pelec->klist, diff --git a/source/source_lcao/rho_tau_lcao.cpp b/source/source_lcao/rho_tau_lcao.cpp index 39bc344425..2397ad8230 100644 --- a/source/source_lcao/rho_tau_lcao.cpp +++ b/source/source_lcao/rho_tau_lcao.cpp @@ -4,19 +4,22 @@ void LCAO_domain::dm2rho(std::vector*> &dmr, const int nspin, - Charge* chg) + Charge* chr) { ModuleBase::TITLE("LCAO_domain", "dm2rho"); ModuleBase::timer::tick("LCAO_domain", "dm2rho"); for (int is = 0; is < nspin; is++) { - ModuleBase::GlobalFunc::ZEROS(chg->rho[is], chg->nrxx); + ModuleBase::GlobalFunc::ZEROS(chr->rho[is], chr->nrxx); } - ModuleGint::cal_gint_rho(dmr, nspin, chg->rho); + ModuleGint::cal_gint_rho(dmr, nspin, chr->rho); - chg->renormalize_rho(); + chr->renormalize_rho(); + + // should be moved somewhere else, mohan 20251024 + dm2tau(dmr, nspin, chr); // symmetrize of charge density should be here, mohan 20251023 @@ -27,7 +30,7 @@ void LCAO_domain::dm2rho(std::vector*> &dmr, void LCAO_domain::dm2tau(std::vector*> &dmr, const int nspin, - Charge* chg) + Charge* chr) { ModuleBase::TITLE("LCAO_domain", "dm2tau"); ModuleBase::timer::tick("LCAO_domain", "dm2tau"); @@ -36,12 +39,10 @@ void LCAO_domain::dm2tau(std::vector*> &dmr, { for (int is = 0; is < nspin; is++) { - ModuleBase::GlobalFunc::ZEROS(chg->kin_r[is], chg->nrxx); + ModuleBase::GlobalFunc::ZEROS(chr->kin_r[is], chr->nrxx); } - ModuleGint::cal_gint_tau(dmr, nspin, chg->kin_r); + ModuleGint::cal_gint_tau(dmr, nspin, chr->kin_r); } ModuleBase::timer::tick("LCAO_domain", "dm2tau"); } - - diff --git a/source/source_lcao/rho_tau_lcao.h b/source/source_lcao/rho_tau_lcao.h index 59de61cbc0..d0ee2d10ae 100644 --- a/source/source_lcao/rho_tau_lcao.h +++ b/source/source_lcao/rho_tau_lcao.h @@ -9,11 +9,11 @@ namespace LCAO_domain { void dm2rho(std::vector*> &dmr, const int nspin, - Charge* chg); + Charge* chr); void dm2tau(std::vector*> &dmr, const int nspin, - Charge* chg); + Charge* chr); } From 64f1757d43c3578e8df369c11945d9e0ef700cea Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 24 Oct 2025 09:37:41 +0800 Subject: [PATCH 39/81] update estate --- source/Makefile.Objects | 1 - source/source_estate/CMakeLists.txt | 1 - source/source_estate/elecstate.cpp | 4 +- source/source_estate/elecstate.h | 6 +-- source/source_estate/elecstate_lcao.h | 4 +- .../source_estate/elecstate_lcao_cal_tau.cpp | 42 ------------------- source/source_estate/elecstate_pw.cpp | 4 +- source/source_estate/elecstate_pw.h | 2 +- source/source_estate/elecstate_pw_sdft.h | 6 +-- 9 files changed, 13 insertions(+), 57 deletions(-) delete mode 100644 source/source_estate/elecstate_lcao_cal_tau.cpp diff --git a/source/Makefile.Objects b/source/Makefile.Objects index acb66ac49d..12d3637d79 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -252,7 +252,6 @@ OBJS_ELECSTAT=elecstate.o\ update_pot.o\ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\ - elecstate_lcao_cal_tau.o\ setup_dm.o\ density_matrix.o\ density_matrix_io.o\ diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index 8a54110e55..55167e6c64 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -45,7 +45,6 @@ list(APPEND objects if(ENABLE_LCAO) list(APPEND objects elecstate_lcao.cpp - elecstate_lcao_cal_tau.cpp module_pot/H_TDDFT_pw.cpp module_dm/setup_dm.cpp module_dm/density_matrix.cpp diff --git a/source/source_estate/elecstate.cpp b/source/source_estate/elecstate.cpp index 72ac36f06b..58fdf7344a 100644 --- a/source/source_estate/elecstate.cpp +++ b/source/source_estate/elecstate.cpp @@ -54,13 +54,13 @@ void ElecState::init_scf(const int istep, } -void ElecState::init_ks(Charge* chg_in, // pointer for class Charge +void ElecState::init_ks(Charge* chr_in, // pointer for class Charge const K_Vectors* klist_in, int nk_in, ModulePW::PW_Basis* rhopw_in, const ModulePW::PW_Basis_Big* bigpw_in) { - this->charge = chg_in; + this->charge = chr_in; this->klist = klist_in; this->charge->set_rhopw(rhopw_in); this->bigpw = bigpw_in; diff --git a/source/source_estate/elecstate.h b/source/source_estate/elecstate.h index 3ca6a28e1c..4ac3cb8f8a 100644 --- a/source/source_estate/elecstate.h +++ b/source/source_estate/elecstate.h @@ -17,9 +17,9 @@ class ElecState ElecState() { } - ElecState(Charge* charge_in, ModulePW::PW_Basis* rhopw_in, ModulePW::PW_Basis_Big* bigpw_in) + ElecState(Charge* chr_in, ModulePW::PW_Basis* rhopw_in, ModulePW::PW_Basis_Big* bigpw_in) { - this->charge = charge_in; + this->charge = chr_in; this->charge->set_rhopw(rhopw_in); this->bigpw = bigpw_in; this->eferm.two_efermi = PARAM.globalv.two_fermi; @@ -32,7 +32,7 @@ class ElecState this->pot = nullptr; } } - void init_ks(Charge* chg_in, // pointer for class Charge + void init_ks(Charge* chr_in, // pointer for class Charge const K_Vectors* klist_in, int nk_in, // number of k points ModulePW::PW_Basis* rhopw_in, diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index 0b1d6cd9b7..cb3f8169d3 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -15,13 +15,13 @@ class ElecStateLCAO : public ElecState ElecStateLCAO() { } // will be called by ElecStateLCAO_TDDFT - ElecStateLCAO(Charge* chg_in, + ElecStateLCAO(Charge* chr_in, const K_Vectors* klist_in, int nks_in, ModulePW::PW_Basis* rhopw_in, ModulePW::PW_Basis_Big* bigpw_in) { - init_ks(chg_in, klist_in, nks_in, rhopw_in, bigpw_in); + init_ks(chr_in, klist_in, nks_in, rhopw_in, bigpw_in); this->classname = "ElecStateLCAO"; } diff --git a/source/source_estate/elecstate_lcao_cal_tau.cpp b/source/source_estate/elecstate_lcao_cal_tau.cpp deleted file mode 100644 index 7e0544c2d0..0000000000 --- a/source/source_estate/elecstate_lcao_cal_tau.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "elecstate_lcao.h" -#include "source_lcao/module_gint/gint_interface.h" - -#include "source_base/timer.h" - -namespace elecstate -{ - -/* -// calculate the kinetic energy density tau, multi-k case -template <> -void ElecStateLCAO>::cal_tau(const psi::Psi>& psi) -{ - ModuleBase::timer::tick("ElecStateLCAO", "cal_tau"); - - for (int is = 0; is < PARAM.inp.nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[is], this->charge->nrxx); - } - ModuleGint::cal_gint_tau(this->DM->get_DMR_vector(), PARAM.inp.nspin, this->charge->kin_r); - ModuleBase::timer::tick("ElecStateLCAO", "cal_tau"); - return; -} - -// calculate the kinetic energy density tau, gamma-only case -template <> -void ElecStateLCAO::cal_tau(const psi::Psi& psi) -{ - ModuleBase::timer::tick("ElecStateLCAO", "cal_tau"); - - for (int is = 0; is < PARAM.inp.nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[is], this->charge->nrxx); - } - ModuleGint::cal_gint_tau(this->DM->get_DMR_vector(), PARAM.inp.nspin, this->charge->kin_r); - - ModuleBase::timer::tick("ElecStateLCAO", "cal_tau"); - return; -} -*/ - -} diff --git a/source/source_estate/elecstate_pw.cpp b/source/source_estate/elecstate_pw.cpp index caffd1211b..247227149e 100644 --- a/source/source_estate/elecstate_pw.cpp +++ b/source/source_estate/elecstate_pw.cpp @@ -13,7 +13,7 @@ namespace elecstate { template ElecStatePW::ElecStatePW(ModulePW::PW_Basis_K* wfc_basis_in, - Charge* chg_in, + Charge* chr_in, K_Vectors* pkv_in, UnitCell* ucell_in, pseudopot_cell_vnl* ppcell_in, @@ -26,7 +26,7 @@ ElecStatePW::ElecStatePW(ModulePW::PW_Basis_K* wfc_basis_in, this->rhopw_smooth = rhopw_in; this->ppcell = ppcell_in; this->ucell = ucell_in; - this->init_ks(chg_in, pkv_in, pkv_in->get_nks(), rhodpw_in, bigpw_in); + this->init_ks(chr_in, pkv_in, pkv_in->get_nks(), rhodpw_in, bigpw_in); } template diff --git a/source/source_estate/elecstate_pw.h b/source/source_estate/elecstate_pw.h index c3d7568dc4..10b54676e6 100644 --- a/source/source_estate/elecstate_pw.h +++ b/source/source_estate/elecstate_pw.h @@ -20,7 +20,7 @@ class ElecStatePW : public ElecState public: ElecStatePW(ModulePW::PW_Basis_K* wfc_basis_in, - Charge* chg_in, + Charge* chr_in, K_Vectors* pkv_in, UnitCell* ucell_in, pseudopot_cell_vnl* ppcell_in, diff --git a/source/source_estate/elecstate_pw_sdft.h b/source/source_estate/elecstate_pw_sdft.h index fe9ab81834..506cabdd89 100644 --- a/source/source_estate/elecstate_pw_sdft.h +++ b/source/source_estate/elecstate_pw_sdft.h @@ -8,7 +8,7 @@ class ElecStatePW_SDFT : public ElecStatePW { public: ElecStatePW_SDFT(ModulePW::PW_Basis_K* wfc_basis_in, - Charge* chg_in, + Charge* chr_in, K_Vectors* pkv_in, UnitCell* ucell_in, pseudopot_cell_vnl* ppcell_in, @@ -16,7 +16,7 @@ class ElecStatePW_SDFT : public ElecStatePW ModulePW::PW_Basis* rhopw_in, ModulePW::PW_Basis_Big* bigpw_in) : ElecStatePW(wfc_basis_in, chg_in, pkv_in, ucell_in, ppcell_in, rhodpw_in, rhopw_in, bigpw_in) + Device>(wfc_basis_in, chr_in, pkv_in, ucell_in, ppcell_in, rhodpw_in, rhopw_in, bigpw_in) { this->classname = "ElecStatePW_SDFT"; } @@ -27,4 +27,4 @@ class ElecStatePW_SDFT : public ElecStatePW using castmem_var_d2h_op = base_device::memory::cast_memory_op; }; } // namespace elecstate -#endif \ No newline at end of file +#endif From 8e5677f79bc731d229491baca285314fdb4a841c Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 24 Oct 2025 10:28:21 +0800 Subject: [PATCH 40/81] fix bug in ELF --- source/Makefile.Objects | 2 +- source/source_esolver/esolver_ks_lcao.cpp | 4 +- source/source_esolver/esolver_ks_pw.cpp | 1 + source/source_estate/CMakeLists.txt | 2 +- source/source_estate/elecstate_lcao.cpp | 2 +- source/source_estate/elecstate_lcao.h | 2 +- .../module_dm/{setup_dm.cpp => init_dm.cpp} | 10 ++--- .../module_dm/{setup_dm.h => init_dm.h} | 6 +-- source/source_hsolver/hsolver_lcao.cpp | 2 +- source/source_io/ctrl_scf_lcao.cpp | 13 ++++-- source/source_lcao/rho_tau_lcao.cpp | 18 ++++---- source/source_lcao/rho_tau_lcao.h | 1 - source/source_lcao/setup_dm.cpp | 28 ++++++++++++ source/source_lcao/setup_dm.h | 44 +++++++++++++++++++ 14 files changed, 107 insertions(+), 28 deletions(-) rename source/source_estate/module_dm/{setup_dm.cpp => init_dm.cpp} (85%) rename source/source_estate/module_dm/{setup_dm.h => init_dm.h} (86%) create mode 100644 source/source_lcao/setup_dm.cpp create mode 100644 source/source_lcao/setup_dm.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 12d3637d79..ceeedd0f15 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -252,7 +252,7 @@ OBJS_ELECSTAT=elecstate.o\ update_pot.o\ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\ - setup_dm.o\ + init_dm.o\ density_matrix.o\ density_matrix_io.o\ cal_dm_psi.o\ diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index c8ef7f44c5..653b1c3497 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -14,7 +14,7 @@ #endif #include "source_lcao/module_rdmft/rdmft.h" #include "source_estate/module_charge/chgmixing.h" // use charge mixing, mohan add 20251006 -#include "source_estate/module_dm/setup_dm.h" // setup dm from electronic wave functions +#include "source_estate/module_dm/init_dm.h" // init dm from electronic wave functions #include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao() #include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao() #include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao() @@ -383,7 +383,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const this->exx_nao.exd->two_level_step : this->exx_nao.exc->two_level_step; } #endif - elecstate::setup_dm(ucell, estate, this->psi, this->chr, iter, exx_two_level_step); + elecstate::init_dm(ucell, estate, this->psi, this->chr, iter, exx_two_level_step); } #ifdef __EXX diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 840e8968c0..f1886eb004 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -359,6 +359,7 @@ void ESolver_KS_PW::after_scf(UnitCell& ucell, const int istep, const if (PARAM.inp.out_elf[0] > 0) { this->ESolver_KS::psi = new psi::Psi(this->stp.psi_cpu[0]); + this->pelec->cal_tau(*(this->psi)); } // Call 'after_scf' of ESolver_KS diff --git a/source/source_estate/CMakeLists.txt b/source/source_estate/CMakeLists.txt index 55167e6c64..bd7be0e782 100644 --- a/source/source_estate/CMakeLists.txt +++ b/source/source_estate/CMakeLists.txt @@ -46,7 +46,7 @@ if(ENABLE_LCAO) list(APPEND objects elecstate_lcao.cpp module_pot/H_TDDFT_pw.cpp - module_dm/setup_dm.cpp + module_dm/init_dm.cpp module_dm/density_matrix.cpp module_dm/density_matrix_io.cpp module_dm/cal_dm_psi.cpp diff --git a/source/source_estate/elecstate_lcao.cpp b/source/source_estate/elecstate_lcao.cpp index 44500941d6..b0518d817e 100644 --- a/source/source_estate/elecstate_lcao.cpp +++ b/source/source_estate/elecstate_lcao.cpp @@ -81,7 +81,7 @@ void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, std::vector -void ElecStateLCAO>::dm2Rho(std::vector*> pexsi_DM, +void ElecStateLCAO>::dm2rho(std::vector*> pexsi_DM, std::vector*> pexsi_EDM) { ModuleBase::WARNING_QUIT("ElecStateLCAO", "pexsi is not completed for multi-k case"); diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index cb3f8169d3..e9bb3077a5 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -56,7 +56,7 @@ class ElecStateLCAO : public ElecState * @param pexsi_EDM: pointers of energy-weighed density matrix (EDMK) calculated by pexsi, needed by MD, will be * stored in DensityMatrix::pexsi_EDM */ - void dm2Rho(std::vector pexsi_DM, std::vector pexsi_EDM); + void dm2rho(std::vector pexsi_DM, std::vector pexsi_EDM); #endif DensityMatrix* DM = nullptr; diff --git a/source/source_estate/module_dm/setup_dm.cpp b/source/source_estate/module_dm/init_dm.cpp similarity index 85% rename from source/source_estate/module_dm/setup_dm.cpp rename to source/source_estate/module_dm/init_dm.cpp index beb611b0dc..1913914226 100644 --- a/source/source_estate/module_dm/setup_dm.cpp +++ b/source/source_estate/module_dm/init_dm.cpp @@ -1,17 +1,17 @@ -#include "source_estate/module_dm/setup_dm.h" +#include "source_estate/module_dm/init_dm.h" #include "source_estate/module_dm/cal_dm_psi.h" #include "source_estate/elecstate_tools.h" #include "source_estate/cal_ux.h" template -void elecstate::setup_dm(UnitCell& ucell, +void elecstate::init_dm(UnitCell& ucell, elecstate::ElecStateLCAO* pelec, psi::Psi* psi, Charge &chr, const int iter, const int exx_two_level_step) { - ModuleBase::TITLE("elecstate", "setup_dm"); + ModuleBase::TITLE("elecstate", "init_dm"); if (iter == 1 && exx_two_level_step == 0) { @@ -49,14 +49,14 @@ void elecstate::setup_dm(UnitCell& ucell, } -template void elecstate::setup_dm(UnitCell& ucell, +template void elecstate::init_dm(UnitCell& ucell, elecstate::ElecStateLCAO* pelec, psi::Psi* psi, Charge &chr, const int iter, const int exx_two_level_step); -template void elecstate::setup_dm>(UnitCell& ucell, +template void elecstate::init_dm>(UnitCell& ucell, elecstate::ElecStateLCAO>* pelec, psi::Psi>* psi, Charge &chr, diff --git a/source/source_estate/module_dm/setup_dm.h b/source/source_estate/module_dm/init_dm.h similarity index 86% rename from source/source_estate/module_dm/setup_dm.h rename to source/source_estate/module_dm/init_dm.h index 73a9f8b00f..38b6263eca 100644 --- a/source/source_estate/module_dm/setup_dm.h +++ b/source/source_estate/module_dm/init_dm.h @@ -1,5 +1,5 @@ -#ifndef SETUP_DM_H -#define SETUP_DM_H +#ifndef INIT_DM_H +#define INIT_DM_H #include "source_cell/unitcell.h" // use unitcell #include "source_estate/elecstate_lcao.h"// use ElecStateLCAO @@ -10,7 +10,7 @@ namespace elecstate { template -void setup_dm(UnitCell& ucell, +void init_dm(UnitCell& ucell, ElecStateLCAO* pelec, psi::Psi* psi, Charge &chr, diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index 091bf15771..bee55de5c5 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -124,7 +124,7 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, auto _pes = dynamic_cast*>(pes); pes->f_en.eband = pe.totalFreeEnergy; // maybe eferm could be dealt with in the future - _pes->dmToRho(pe.DM, pe.EDM); + _pes->dm2rho(pe.DM, pe.EDM); #endif } diff --git a/source/source_io/ctrl_scf_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp index 9fd14afbd6..2e127139b0 100644 --- a/source/source_io/ctrl_scf_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -29,6 +29,7 @@ #endif #include "source_lcao/module_rdmft/rdmft.h" // use RDMFT codes #include "source_io/to_qo.h" // use toQO +#include "source_lcao/rho_tau_lcao.h" // mohan add 2025-10-24 template @@ -368,10 +369,16 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wave function to Wannier90"); } + // 14) calculate the kinetic energy density tau + // mohan add 2025-10-24 + if (inp.out_elf[0] > 0) + { + LCAO_domain::dm2tau(pelec->DM->get_DMR_vector(), inp.nspin, pelec->charge); + } #ifdef __EXX //------------------------------------------------------------------ - //! 14) Output Hexx matrix in LCAO basis + //! 15) Output Hexx matrix in LCAO basis // (see `out_chg` in docs/advanced/input_files/input-main.md) //------------------------------------------------------------------ if (inp.out_chg[0]) @@ -392,7 +399,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, } //------------------------------------------------------------------ - //! 15) Write RPA information in LCAO basis + //! 16) Write RPA information in LCAO basis //------------------------------------------------------------------ if (inp.rpa) { @@ -408,7 +415,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, #endif //------------------------------------------------------------------ - //! 16) Perform RDMFT calculations, added by jghan, 2024-10-17 + //! 17) Perform RDMFT calculations, added by jghan, 2024-10-17 //------------------------------------------------------------------ if (inp.rdmft == true) { diff --git a/source/source_lcao/rho_tau_lcao.cpp b/source/source_lcao/rho_tau_lcao.cpp index 2397ad8230..7477f72077 100644 --- a/source/source_lcao/rho_tau_lcao.cpp +++ b/source/source_lcao/rho_tau_lcao.cpp @@ -19,7 +19,10 @@ void LCAO_domain::dm2rho(std::vector*> &dmr, chr->renormalize_rho(); // should be moved somewhere else, mohan 20251024 - dm2tau(dmr, nspin, chr); + if (XC_Functional::get_ked_flag()) + { + dm2tau(dmr, nspin, chr); + } // symmetrize of charge density should be here, mohan 20251023 @@ -35,14 +38,11 @@ void LCAO_domain::dm2tau(std::vector*> &dmr, ModuleBase::TITLE("LCAO_domain", "dm2tau"); ModuleBase::timer::tick("LCAO_domain", "dm2tau"); - if (XC_Functional::get_ked_flag()) - { - for (int is = 0; is < nspin; is++) - { - ModuleBase::GlobalFunc::ZEROS(chr->kin_r[is], chr->nrxx); - } - ModuleGint::cal_gint_tau(dmr, nspin, chr->kin_r); - } + for (int is = 0; is < nspin; is++) + { + ModuleBase::GlobalFunc::ZEROS(chr->kin_r[is], chr->nrxx); + } + ModuleGint::cal_gint_tau(dmr, nspin, chr->kin_r); ModuleBase::timer::tick("LCAO_domain", "dm2tau"); } diff --git a/source/source_lcao/rho_tau_lcao.h b/source/source_lcao/rho_tau_lcao.h index d0ee2d10ae..ccedba2b70 100644 --- a/source/source_lcao/rho_tau_lcao.h +++ b/source/source_lcao/rho_tau_lcao.h @@ -14,7 +14,6 @@ namespace LCAO_domain void dm2tau(std::vector*> &dmr, const int nspin, Charge* chr); - } #endif diff --git a/source/source_lcao/setup_dm.cpp b/source/source_lcao/setup_dm.cpp new file mode 100644 index 0000000000..231c8b0615 --- /dev/null +++ b/source/source_lcao/setup_dm.cpp @@ -0,0 +1,28 @@ +#include "source_lcao/setup_dm.h" + +#include "cal_dm.h" +#include "source_base/timer.h" +#include "source_estate/module_dm/cal_dm_psi.h" +#include "source_hamilt/module_xc/xc_functional.h" +#include "source_lcao/module_deltaspin/spin_constrain.h" +#include "source_pw/module_pwdft/global.h" +#include "source_io/module_parameter/parameter.h" + +#include "source_lcao/module_gint/gint_interface.h" + +#include + +namespace LCAO_domain +{ + +template +void Setup_DM::init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) +{ + const int nspin_dm = nspin == 2 ? 2 : 1; + this->dm = new DensityMatrix(paraV, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); +} + +template class Setup_DM; // Gamma_only case +template class Setup_DM>; // multi-k case + +} // namespace elecstate diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h new file mode 100644 index 0000000000..0e4da43497 --- /dev/null +++ b/source/source_lcao/setup_dm.h @@ -0,0 +1,44 @@ +#ifndef SETUP_DM_H +#define SETUP_DM_H + +#include "source_estate/module_dm/density_matrix.h" + +#include + +namespace LCAO_DOMAIN +{ +template +class Setup_DM +{ + public: + + Setup_DM() + { + } // will be called by ElecStateLCAO_TDDFT + + ~Setup_DM() + { + if (this->DM != nullptr) + { + delete this->dm; + } + } + + // initial density matrix + void init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); + + DensityMatrix* get_dm() const + { + return const_cast*>(this->dm); + } + + private: + + DensityMatrix* dm = nullptr; + +}; + + +} // namespace elecstate + +#endif From 6559a6108dd2d5df60223c6881c74d4fa864e137 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 24 Oct 2025 20:10:45 +0800 Subject: [PATCH 41/81] fix bug --- source/source_esolver/esolver_ks.cpp | 2 ++ source/source_esolver/esolver_ks_lcao.cpp | 13 ++++++++++--- source/source_io/ctrl_scf_lcao.cpp | 8 ++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index a326b23802..f85fd53f43 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -347,12 +347,14 @@ void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bo { ModuleBase::TITLE("ESolver_KS", "after_scf"); +/* // 1) calculate the kinetic energy density tau if (PARAM.inp.out_elf[0] > 0) { assert(this->psi != nullptr); this->pelec->cal_tau(*(this->psi)); } +*/ // 2) call after_scf() of ESolver_FP ESolver_FP::after_scf(ucell, istep, conv_esolver); diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 653b1c3497..47382550b4 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -21,6 +21,7 @@ #include "source_psi/setup_psi.h" // mohan add 20251019 #include "source_io/read_wfc_nao.h" #include "source_io/print_info.h" +#include "source_lcao/rho_tau_lcao.h" // mohan add 20251024 namespace ModuleESolver { @@ -583,9 +584,6 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); - //! 1) call after_scf() of ESolver_KS - ESolver_KS::after_scf(ucell, istep, conv_esolver); - auto* estate = dynamic_cast*>(this->pelec); auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); @@ -599,6 +597,15 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::after_scf","p_hamilt does not exist"); } + if (PARAM.inp.out_elf[0] > 0) + { + LCAO_domain::dm2tau(estate->DM->get_DMR_vector(), PARAM.inp.nspin, estate->charge); + } + + //! 1) call after_scf() of ESolver_KS + ESolver_KS::after_scf(ucell, istep, conv_esolver); + + //! 2) output of lcao every few ionic steps ModuleIO::ctrl_scf_lcao(ucell, PARAM.inp, this->kv, estate, this->pv, diff --git a/source/source_io/ctrl_scf_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp index 2e127139b0..c859cf3f74 100644 --- a/source/source_io/ctrl_scf_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -371,10 +371,10 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, // 14) calculate the kinetic energy density tau // mohan add 2025-10-24 - if (inp.out_elf[0] > 0) - { - LCAO_domain::dm2tau(pelec->DM->get_DMR_vector(), inp.nspin, pelec->charge); - } +// if (inp.out_elf[0] > 0) +// { +// LCAO_domain::dm2tau(pelec->DM->get_DMR_vector(), inp.nspin, pelec->charge); +// } #ifdef __EXX //------------------------------------------------------------------ From 449d1eb91c12116c346772f489e2daf3cde1b941 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 30 Oct 2025 21:10:17 +0800 Subject: [PATCH 42/81] move DM outside of pelec --- source/source_esolver/esolver_ks_lcao.cpp | 12 +++++------- source/source_esolver/esolver_ks_lcao.h | 4 ++++ source/source_esolver/esolver_ks_lcao_tddft.cpp | 2 +- source/source_estate/elecstate_lcao.cpp | 6 ------ source/source_estate/elecstate_lcao.h | 12 ------------ source/source_estate/module_dm/cal_edm_tddft.cpp | 10 ++++------ source/source_estate/module_dm/cal_edm_tddft.h | 4 ++-- source/source_lcao/setup_dm.cpp | 2 +- source/source_lcao/setup_dm.h | 2 +- 9 files changed, 18 insertions(+), 36 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 47382550b4..b8ad993c12 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -91,7 +91,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa // 7) init DMK, but DMR is constructed in before_scf() - dynamic_cast*>(this->pelec)->init_DM(&this->kv, &(this->pv), inp.nspin); + this->dmat.init_dm(&this->kv, &(this->pv), inp.nspin); // 8) init exact exchange calculations this->exx_nao.before_runner(ucell, this->kv, this->orb_, this->pv, inp); @@ -193,11 +193,9 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) } if (this->p_hamilt == nullptr) { - elecstate::DensityMatrix* DM = estate->get_DM(); - this->p_hamilt = new hamilt::HamiltLCAO( ucell, this->gd, &this->pv, this->pelec->pot, this->kv, - two_center_bundle_, orb_, DM, this->deepks, istep, exx_nao); + two_center_bundle_, orb_, this->dmat.get_dm(), this->deepks, istep, exx_nao); } // 9) for each ionic step, the overlap must be rebuilt @@ -226,7 +224,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) { ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","p_hamilt does not exist"); } - estate->get_DM()->init_DMR(*hamilt_lcao->getHR()); + this->dmat.get_dm()->init_DMR(*hamilt_lcao->getHR()); #ifdef __MLALGO // 14) initialize DMR of DeePKS @@ -238,7 +236,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) // 2. DMK in DensityMatrix is empty (istep == 0), then DMR is initialized by zeros if (istep > 0) { - estate->get_DM()->cal_DMR(); + this->dmat.get_dm()->cal_DMR(); } // 16) the electron charge density should be symmetrized, @@ -365,7 +363,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_init","pelec does not exist"); } - elecstate::DensityMatrix* dm = estate->get_DM(); + elecstate::DensityMatrix* dm = this->dmat.get_dm(); module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp); diff --git a/source/source_esolver/esolver_ks_lcao.h b/source/source_esolver/esolver_ks_lcao.h index 16fb6aa8df..4309090ba5 100644 --- a/source/source_esolver/esolver_ks_lcao.h +++ b/source/source_esolver/esolver_ks_lcao.h @@ -9,6 +9,7 @@ #include "source_lcao/setup_deepks.h" // for deepks, mohan add 20251008 #include "source_lcao/setup_exx.h" // for exx, mohan add 20251008 #include "source_lcao/module_rdmft/rdmft.h" // rdmft +#include "source_lcao/setup_dm.h" // mohan add 2025-10-30 #include @@ -71,6 +72,9 @@ class ESolver_KS_LCAO : public ESolver_KS //! NAO orbitals: two-center integrations TwoCenterBundle two_center_bundle_; + //! Add density matrix class, mohan add 2025-10-30 + Setup_DM dmat; + //! For RDMFT calculations, added by jghan, 2024-03-16 rdmft::RDMFT rdmft_solver; diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index a5ecf82fa8..a70d13bd02 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -467,7 +467,7 @@ void ESolver_KS_LCAO_TDDFT::save2(UnitCell& ucell, // calculate energy density matrix for tddft if (istep >= (PARAM.inp.init_wfc == "file" ? 0 : 1) && PARAM.inp.td_edm == 0) { - elecstate::cal_edm_tddft(this->pv, this->pelec, this->kv, this->p_hamilt); + elecstate::cal_edm_tddft(this->pv, this->dmat, this->kv, this->p_hamilt); } } diff --git a/source/source_estate/elecstate_lcao.cpp b/source/source_estate/elecstate_lcao.cpp index b0518d817e..14562fca87 100644 --- a/source/source_estate/elecstate_lcao.cpp +++ b/source/source_estate/elecstate_lcao.cpp @@ -15,12 +15,6 @@ namespace elecstate { -template -void ElecStateLCAO::init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) -{ - const int nspin_dm = nspin == 2 ? 2 : 1; - this->DM = new DensityMatrix(paraV, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); -} template <> double ElecStateLCAO::get_spin_constrain_energy() diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index e9bb3077a5..5d2d9ac7f6 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -27,21 +27,11 @@ class ElecStateLCAO : public ElecState virtual ~ElecStateLCAO() { - if (this->DM != nullptr) - { - delete this->DM; - } } // update charge density for next scf step // void getNewRho() override; - // initial density matrix - void init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); - DensityMatrix* get_DM() const - { - return const_cast*>(this->DM); - } static int out_wfc_lcao; static bool need_psi_grid; @@ -59,8 +49,6 @@ class ElecStateLCAO : public ElecState void dm2rho(std::vector pexsi_DM, std::vector pexsi_EDM); #endif - DensityMatrix* DM = nullptr; - }; template diff --git a/source/source_estate/module_dm/cal_edm_tddft.cpp b/source/source_estate/module_dm/cal_edm_tddft.cpp index 9a90ee4cf7..c2435c9882 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.cpp +++ b/source/source_estate/module_dm/cal_edm_tddft.cpp @@ -6,7 +6,7 @@ namespace elecstate { // use the original formula (Hamiltonian matrix) to calculate energy density matrix void cal_edm_tddft(Parallel_Orbitals& pv, - elecstate::ElecState* pelec, + Setup_DM &dmat, K_Vectors& kv, hamilt::Hamilt>* p_hamilt) { @@ -14,15 +14,13 @@ void cal_edm_tddft(Parallel_Orbitals& pv, const int nlocal = PARAM.globalv.nlocal; assert(nlocal >= 0); - auto _pelec = dynamic_cast>*>(pelec); - - _pelec->get_DM()->EDMK.resize(kv.get_nks()); + dmat.get_dm()->EDMK.resize(kv.get_nks()); for (int ik = 0; ik < kv.get_nks(); ++ik) { p_hamilt->updateHk(ik); - std::complex* tmp_dmk = _pelec->get_DM()->get_DMK_pointer(ik); - ModuleBase::ComplexMatrix& tmp_edmk = _pelec->get_DM()->EDMK[ik]; + std::complex* tmp_dmk = dmat.get_dm()->get_DMK_pointer(ik); + ModuleBase::ComplexMatrix& tmp_edmk = dmat.get_dm()->EDMK[ik]; #ifdef __MPI diff --git a/source/source_estate/module_dm/cal_edm_tddft.h b/source/source_estate/module_dm/cal_edm_tddft.h index 49de0550b5..a3a48822b5 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.h +++ b/source/source_estate/module_dm/cal_edm_tddft.h @@ -3,13 +3,13 @@ #include "source_basis/module_ao/parallel_orbitals.h" #include "source_cell/klist.h" -#include "source_estate/elecstate_lcao.h" #include "source_hamilt/hamilt.h" +#include "source_lcao/setup_dm.h" namespace elecstate { void cal_edm_tddft(Parallel_Orbitals& pv, - elecstate::ElecState* pelec, + Setup_DM &dmat, K_Vectors& kv, hamilt::Hamilt>* p_hamilt); } // namespace elecstate diff --git a/source/source_lcao/setup_dm.cpp b/source/source_lcao/setup_dm.cpp index 231c8b0615..dc03015a5d 100644 --- a/source/source_lcao/setup_dm.cpp +++ b/source/source_lcao/setup_dm.cpp @@ -16,7 +16,7 @@ namespace LCAO_domain { template -void Setup_DM::init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) +void Setup_DM::init_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) { const int nspin_dm = nspin == 2 ? 2 : 1; this->dm = new DensityMatrix(paraV, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h index 0e4da43497..ef2a79b01b 100644 --- a/source/source_lcao/setup_dm.h +++ b/source/source_lcao/setup_dm.h @@ -25,7 +25,7 @@ class Setup_DM } // initial density matrix - void init_DM(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); + void init_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); DensityMatrix* get_dm() const { From e01f84ad54cb238ecea0d550af3ad09de8111d60 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 31 Oct 2025 11:33:32 +0800 Subject: [PATCH 43/81] deal with init_dm functions --- source/source_esolver/esolver_ks_lcao.cpp | 4 ++-- source/source_estate/module_dm/cal_edm_tddft.cpp | 4 +++- source/source_estate/module_dm/cal_edm_tddft.h | 2 +- source/source_estate/module_dm/init_dm.cpp | 7 +++++-- source/source_estate/module_dm/init_dm.h | 2 ++ source/source_lcao/setup_dm.cpp | 5 +++-- source/source_lcao/setup_dm.h | 10 +++++----- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index b8ad993c12..c6cdf68c3c 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -91,7 +91,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa // 7) init DMK, but DMR is constructed in before_scf() - this->dmat.init_dm(&this->kv, &(this->pv), inp.nspin); + this->dmat.allocate_dm(&this->kv, &(this->pv), inp.nspin); // 8) init exact exchange calculations this->exx_nao.before_runner(ucell, this->kv, this->orb_, this->pv, inp); @@ -382,7 +382,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const this->exx_nao.exd->two_level_step : this->exx_nao.exc->two_level_step; } #endif - elecstate::init_dm(ucell, estate, this->psi, this->chr, iter, exx_two_level_step); + elecstate::init_dm(ucell, estate, this->dmat, this->psi, this->chr, iter, exx_two_level_step); } #ifdef __EXX diff --git a/source/source_estate/module_dm/cal_edm_tddft.cpp b/source/source_estate/module_dm/cal_edm_tddft.cpp index c2435c9882..333f916706 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.cpp +++ b/source/source_estate/module_dm/cal_edm_tddft.cpp @@ -2,11 +2,13 @@ #include "source_base/module_external/lapack_connector.h" #include "source_base/module_external/scalapack_connector.h" + +#include "source_io/module_parameter/parameter.h" // use PARAM.globalv namespace elecstate { // use the original formula (Hamiltonian matrix) to calculate energy density matrix void cal_edm_tddft(Parallel_Orbitals& pv, - Setup_DM &dmat, + LCAO_DOMAIN::Setup_DM> &dmat, K_Vectors& kv, hamilt::Hamilt>* p_hamilt) { diff --git a/source/source_estate/module_dm/cal_edm_tddft.h b/source/source_estate/module_dm/cal_edm_tddft.h index a3a48822b5..f107509a95 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.h +++ b/source/source_estate/module_dm/cal_edm_tddft.h @@ -9,7 +9,7 @@ namespace elecstate { void cal_edm_tddft(Parallel_Orbitals& pv, - Setup_DM &dmat, + LCAO_DOMAIN::Setup_DM> &dmat, K_Vectors& kv, hamilt::Hamilt>* p_hamilt); } // namespace elecstate diff --git a/source/source_estate/module_dm/init_dm.cpp b/source/source_estate/module_dm/init_dm.cpp index 1913914226..94dd441697 100644 --- a/source/source_estate/module_dm/init_dm.cpp +++ b/source/source_estate/module_dm/init_dm.cpp @@ -6,6 +6,7 @@ template void elecstate::init_dm(UnitCell& ucell, elecstate::ElecStateLCAO* pelec, + LCAO_DOMAIN::Setup_DM &dmat, psi::Psi* psi, Charge &chr, const int iter, @@ -30,8 +31,8 @@ void elecstate::init_dm(UnitCell& ucell, pelec->skip_weights); elecstate::calEBand(pelec->ekb, pelec->wg, pelec->f_en); - elecstate::cal_dm_psi(pelec->DM->get_paraV_pointer(), pelec->wg, *psi, *(pelec->DM)); - pelec->DM->cal_DMR(); + elecstate::cal_dm_psi(dmat.get_dm()->get_paraV_pointer(), pelec->wg, *psi, *dmat.get_dm()); + dmat.get_dm()->cal_DMR(); pelec->psiToRho(*psi); pelec->skip_weights = false; @@ -51,6 +52,7 @@ void elecstate::init_dm(UnitCell& ucell, template void elecstate::init_dm(UnitCell& ucell, elecstate::ElecStateLCAO* pelec, + LCAO_DOMAIN::Setup_DM &dmat, psi::Psi* psi, Charge &chr, const int iter, @@ -58,6 +60,7 @@ template void elecstate::init_dm(UnitCell& ucell, template void elecstate::init_dm>(UnitCell& ucell, elecstate::ElecStateLCAO>* pelec, + LCAO_DOMAIN::Setup_DM> &dmat, psi::Psi>* psi, Charge &chr, const int iter, diff --git a/source/source_estate/module_dm/init_dm.h b/source/source_estate/module_dm/init_dm.h index 38b6263eca..4470c897b8 100644 --- a/source/source_estate/module_dm/init_dm.h +++ b/source/source_estate/module_dm/init_dm.h @@ -5,6 +5,7 @@ #include "source_estate/elecstate_lcao.h"// use ElecStateLCAO #include "source_psi/psi.h" // use electronic wave functions #include "source_estate/module_charge/charge.h" // use charge +#include "source_lcao/setup_dm.h" // define Setup_DM namespace elecstate { @@ -12,6 +13,7 @@ namespace elecstate template void init_dm(UnitCell& ucell, ElecStateLCAO* pelec, + LCAO_DOMAIN::Setup_DM &dmat, psi::Psi* psi, Charge &chr, const int iter, diff --git a/source/source_lcao/setup_dm.cpp b/source/source_lcao/setup_dm.cpp index dc03015a5d..8724bd10fc 100644 --- a/source/source_lcao/setup_dm.cpp +++ b/source/source_lcao/setup_dm.cpp @@ -15,11 +15,12 @@ namespace LCAO_domain { +// change init_dm to allocate_dm, mohan 2025-10-31 template -void Setup_DM::init_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) +void Setup_DM::allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) { const int nspin_dm = nspin == 2 ? 2 : 1; - this->dm = new DensityMatrix(paraV, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); + this->dm = new elecstate::DensityMatrix(paraV, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); } template class Setup_DM; // Gamma_only case diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h index ef2a79b01b..1fd6c7d4ec 100644 --- a/source/source_lcao/setup_dm.h +++ b/source/source_lcao/setup_dm.h @@ -24,17 +24,17 @@ class Setup_DM } } - // initial density matrix - void init_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); + // allocate density matrix + void allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); - DensityMatrix* get_dm() const + elecstate::DensityMatrix* get_dm() const { - return const_cast*>(this->dm); + return const_cast*>(this->dm); } private: - DensityMatrix* dm = nullptr; + elecstate::DensityMatrix* dm = nullptr; }; From 7a6f8391aa508e1a7d12a51237c1d348b268b3e1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 2 Nov 2025 16:49:56 +0800 Subject: [PATCH 44/81] remove pelec in dftu, replace with dmk_d(ouble) and dmk_c(omplex) --- source/source_lcao/module_dftu/dftu.h | 7 ++- source/source_lcao/module_dftu/dftu_force.cpp | 63 ++++--------------- 2 files changed, 17 insertions(+), 53 deletions(-) diff --git a/source/source_lcao/module_dftu/dftu.h b/source/source_lcao/module_dftu/dftu.h index b269acc665..899d73a5f1 100644 --- a/source/source_lcao/module_dftu/dftu.h +++ b/source/source_lcao/module_dftu/dftu.h @@ -7,7 +7,7 @@ #ifdef __LCAO #include "source_estate/module_charge/charge_mixing.h" #include "source_hamilt/hamilt.h" -#include "source_estate/elecstate.h" +// #include "source_estate/elecstate.h" // mohan update 2025-11-02 #include "source_lcao/module_hcontainer/hcontainer.h" #include "source_estate/module_dm/density_matrix.h" #include "source_lcao/force_stress_arrays.h" // mohan add 2024-06-15 @@ -200,8 +200,9 @@ class DFTU public: void force_stress(const UnitCell& ucell, const Grid_Driver& gd, - const elecstate::ElecState* pelec, - const Parallel_Orbitals& pv, + const std::vector>& dmk_d, // mohan modify 2025-11-02 + const std::vector>>& dmk_c, // dmat.get_dm()->get_DMK_vector(); + const Parallel_Orbitals& pv, ForceStressArrays& fsr, ModuleBase::matrix& force_dftu, ModuleBase::matrix& stress_dftu, diff --git a/source/source_lcao/module_dftu/dftu_force.cpp b/source/source_lcao/module_dftu/dftu_force.cpp index 5f08b8b545..1d33761cf4 100644 --- a/source/source_lcao/module_dftu/dftu_force.cpp +++ b/source/source_lcao/module_dftu/dftu_force.cpp @@ -1,8 +1,5 @@ -//========================================================== -// Author:Xin Qu #include "source_io/module_parameter/parameter.h" -// DATE : 2019-12-10 -//========================================================== + #ifdef __LCAO #include "dftu.h" #include "source_base/constants.h" @@ -24,6 +21,7 @@ #include #include + extern "C" { // I'm not sure what's happenig here, but the interface in scalapack_connecter.h @@ -74,7 +72,8 @@ namespace ModuleDFTU void DFTU::force_stress(const UnitCell& ucell, const Grid_Driver& gd, - const elecstate::ElecState* pelec, + const std::vector>& dmk_d, // mohan modify 2025-11-02 + const std::vector>>& dmk_c, // dmat.get_dm()->get_DMK_vector(); const Parallel_Orbitals& pv, ForceStressArrays& fsr, // mohan add 2024-06-16 ModuleBase::matrix& force_dftu, @@ -114,29 +113,12 @@ void DFTU::force_stress(const UnitCell& ucell, this->cal_VU_pot_mat_real(spin, false, VU); - const std::vector>& dmk - = dynamic_cast*>(pelec)->get_DM()->get_DMK_vector(); - #ifdef __MPI - pdgemm_(&transT, - &transN, - &nlocal, - &nlocal, - &nlocal, - &alpha, - dmk[spin].data(), - &one_int, - &one_int, - pv.desc, - VU, - &one_int, - &one_int, - pv.desc, - &beta, - &rho_VU[0], - &one_int, - &one_int, - pv.desc); + pdgemm_(&transT, &transN, &nlocal, &nlocal, &nlocal, + &alpha, dmk_d[spin].data(), &one_int, &one_int, + pv.desc, VU, &one_int, &one_int, + pv.desc, &beta, &rho_VU[0], + &one_int, &one_int, pv.desc); #endif delete[] VU; @@ -178,31 +160,12 @@ void DFTU::force_stress(const UnitCell& ucell, this->cal_VU_pot_mat_complex(spin, false, VU); - const std::vector>>& dmk - = dynamic_cast>*>(pelec) - ->get_DM() - ->get_DMK_vector(); #ifdef __MPI - pzgemm_(&transT, - &transN, - &nlocal, - &nlocal, - &nlocal, - &alpha, - dmk[ik].data(), - &one_int, - &one_int, - pv.desc, - VU, - &one_int, - &one_int, - pv.desc, - &beta, - &rho_VU[0], - &one_int, - &one_int, - pv.desc); + pzgemm_(&transT, &transN, &nlocal, &nlocal, &nlocal, + &alpha, dmk_c[ik].data(), &one_int, &one_int, + pv.desc, VU, &one_int, &one_int, pv.desc, &beta, + &rho_VU[0], &one_int, &one_int, pv.desc); #endif delete[] VU; From 7d052a8aa7e690052e5539602d8300dff8dc8d7c Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 2 Nov 2025 17:05:17 +0800 Subject: [PATCH 45/81] fix cal_ldos with DM --- source/source_io/cal_ldos.cpp | 42 ++++++++++++++++++++--------------- source/source_io/cal_ldos.h | 23 ++++++++++++++----- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/source/source_io/cal_ldos.cpp b/source/source_io/cal_ldos.cpp index f12fc9e391..4db365f545 100644 --- a/source/source_io/cal_ldos.cpp +++ b/source/source_io/cal_ldos.cpp @@ -12,10 +12,16 @@ namespace ModuleIO #ifdef __LCAO template -void Cal_ldos::cal_ldos_lcao(const elecstate::ElecStateLCAO* pelec, - const psi::Psi& psi, - const Parallel_Grid& pgrid, - const UnitCell& ucell) +void Cal_ldos::cal_ldos_lcao( + const Efermi &eferm, // mohan add 2025-11-02 + const Charge &chr, // mohan add add 2025-11-02 + const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const K_Vectors &kv, // k points, mohan add 2025-11-02 + const matrix &ekb, // mohan add 2025-11-02 + const matrix &wg, // mohan add 2025-11-02 + const psi::Psi& psi, + const Parallel_Grid& pgrid, + const UnitCell& ucell) { for (int ie = 0; ie < PARAM.inp.stm_bias[2]; ie++) { @@ -25,38 +31,38 @@ void Cal_ldos::cal_ldos_lcao(const elecstate::ElecStateLCAO* pelec, const double emax = en > 0 ? en : 0; // calculate weight (for bands not in the range, weight is zero) - ModuleBase::matrix weight(pelec->ekb.nr, pelec->ekb.nc); - for (int ik = 0; ik < pelec->ekb.nr; ++ik) + ModuleBase::matrix weight(ekb.nr, ekb.nc); + for (int ik = 0; ik < ekb.nr; ++ik) { - const double efermi = pelec->eferm.get_efval(pelec->klist->isk[ik]); + const double efermi = eferm.get_efval(kv.isk[ik]); - for (int ib = 0; ib < pelec->ekb.nc; ib++) + for (int ib = 0; ib < ekb.nc; ib++) { - const double eigenval = (pelec->ekb(ik, ib) - efermi) * ModuleBase::Ry_to_eV; + const double eigenval = (ekb(ik, ib) - efermi) * ModuleBase::Ry_to_eV; if (eigenval >= emin && eigenval <= emax) { - weight(ik, ib) = en > 0 ? pelec->klist->wk[ik] - pelec->wg(ik, ib) : pelec->wg(ik, ib); + weight(ik, ib) = en > 0 ? kv.wk[ik] - wg(ik, ib) : wg(ik, ib); } } } // calculate dm-like for ldos const int nspin_dm = PARAM.inp.nspin == 2 ? 2 : 1; - elecstate::DensityMatrix dm_ldos(pelec->DM->get_paraV_pointer(), + elecstate::DensityMatrix dm_ldos(dmat.get_dm()->get_paraV_pointer(), nspin_dm, - pelec->klist->kvec_d, - pelec->klist->get_nks() / nspin_dm); + kv.kvec_d, + kv.get_nks() / nspin_dm); - elecstate::cal_dm_psi(pelec->DM->get_paraV_pointer(), weight, psi, dm_ldos); - dm_ldos.init_DMR(*(pelec->DM->get_DMR_pointer(1))); + elecstate::cal_dm_psi(dmat.get_dm()->get_paraV_pointer(), weight, psi, dm_ldos); + dm_ldos.init_DMR(*(dmat.get_dm()->get_DMR_pointer(1))); dm_ldos.cal_DMR(); // allocate ldos space - std::vector ldos_space(PARAM.inp.nspin * pelec->charge->nrxx); + std::vector ldos_space(PARAM.inp.nspin * chr.nrxx); double** ldos = new double*[PARAM.inp.nspin]; for (int is = 0; is < PARAM.inp.nspin; ++is) { - ldos[is] = &ldos_space[is * pelec->charge->nrxx]; + ldos[is] = &ldos_space[is * chr.nrxx]; } // calculate ldos @@ -66,7 +72,7 @@ void Cal_ldos::cal_ldos_lcao(const elecstate::ElecStateLCAO* pelec, // ldos[0] += ldos[1] for nspin_dm == 2 if (nspin_dm == 2) { - BlasConnector::axpy(pelec->charge->nrxx, 1.0, ldos[1], 1, ldos[0], 1); + BlasConnector::axpy(chr.nrxx, 1.0, ldos[1], 1, ldos[0], 1); } // write ldos to cube file diff --git a/source/source_io/cal_ldos.h b/source/source_io/cal_ldos.h index a18ac0f1d0..75e5efd1ee 100644 --- a/source/source_io/cal_ldos.h +++ b/source/source_io/cal_ldos.h @@ -4,6 +4,12 @@ #include "source_estate/elecstate_lcao.h" #include "source_estate/elecstate_pw.h" +#include "source_estate/fp_energy.h" // eferm +#include "source_estate/module_charge/charge.h" // chr +#include "source_lcao/setup_dm.h" // Setup_DM +#include "source_cell/klist.h" // K_Vectors +#include "source_base/matrix.h" // matrix + namespace ModuleIO { template @@ -13,10 +19,17 @@ class Cal_ldos Cal_ldos(){}; ~Cal_ldos(){}; - static void cal_ldos_lcao(const elecstate::ElecStateLCAO* pelec, - const psi::Psi& psi, - const Parallel_Grid& pgrid, - const UnitCell& ucell); + static void cal_ldos_lcao( + const Efermi &eferm, // mohan add 2025-11-02 + const Charge &chr, // mohan add add 2025-11-02 + const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const K_Vectors &kv, // k points, mohan add 2025-11-02 + const matrix &ekb, // mohan add 2025-11-02 + const matrix &wg, // mohan add 2025-11-02 + const psi::Psi& psi, + const Parallel_Grid& pgrid, + const UnitCell& ucell); + }; // namespace Cal_ldos void cal_ldos_pw(const elecstate::ElecStatePW>* pelec, @@ -72,4 +85,4 @@ void trilinear_interpolate(const std::vector>& points, } // namespace ModuleIO -#endif // CAL_LDOS_H \ No newline at end of file +#endif // CAL_LDOS_H From 26ce47d32ecfe6af90a26725e3c671a73966ac3f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 2 Nov 2025 17:17:05 +0800 Subject: [PATCH 46/81] update delta_spin --- .../module_deltaspin/cal_mw_from_lambda.cpp | 11 +++++++---- source/source_lcao/module_deltaspin/spin_constrain.h | 6 +++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp index 515a89ab9c..6320f6e123 100644 --- a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp @@ -128,7 +128,10 @@ void spinconstrain::SpinConstrain>::calculate_delta_hcc(std } template <> -void spinconstrain::SpinConstrain>::cal_mw_from_lambda(int i_step, const ModuleBase::Vector3* delta_lambda) +void spinconstrain::SpinConstrain>::cal_mw_from_lambda( + int i_step, + elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 + const ModuleBase::Vector3* delta_lambda) { ModuleBase::TITLE("spinconstrain::SpinConstrain", "cal_mw_from_lambda"); ModuleBase::timer::tick("spinconstrain::SpinConstrain", "cal_mw_from_lambda"); @@ -161,9 +164,9 @@ void spinconstrain::SpinConstrain>::cal_mw_from_lambda(int this->pelec->nelec_spin, this->pelec->skip_weights); elecstate::calEBand(this->pelec->ekb,this->pelec->wg,this->pelec->f_en); - elecstate::ElecStateLCAO>* pelec_lcao - = dynamic_cast>*>(this->pelec); - elecstate::cal_dm_psi(this->ParaV, pelec_lcao->wg, *psi_t, *(pelec_lcao->get_DM())); + + elecstate::cal_dm_psi(this->ParaV, pelec_lcao->wg, *psi_t, dm); + pelec_lcao->get_DM()->cal_DMR(); this->cal_mi_lcao(i_step); } diff --git a/source/source_lcao/module_deltaspin/spin_constrain.h b/source/source_lcao/module_deltaspin/spin_constrain.h index bcc4ed9ec1..fd00f9f443 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.h +++ b/source/source_lcao/module_deltaspin/spin_constrain.h @@ -14,6 +14,8 @@ #include "source_hamilt/operator.h" #include "source_estate/elecstate.h" +#include "source_estate/module_dm/density_matrix.h" // mohan add 2025-11-02 + namespace spinconstrain { @@ -49,7 +51,9 @@ class SpinConstrain void cal_mi_pw(); - void cal_mw_from_lambda(int i_step, const ModuleBase::Vector3* delta_lambda = nullptr); + void cal_mw_from_lambda(int i_step, + elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 + const ModuleBase::Vector3* delta_lambda = nullptr); /** * @brief calculate the energy of \sum_i \lambda_i * Mi From 20c5889c34a58bd07d4c8c177d3fccb340fc630f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 08:48:12 +0800 Subject: [PATCH 47/81] update delta spin run_lambda_loop --- source/source_hsolver/hsolver_lcao.cpp | 22 +++++++------- source/source_hsolver/hsolver_lcao.h | 16 +++++----- source/source_io/cal_ldos.cpp | 6 ++-- source/source_io/cal_ldos.h | 6 ++-- .../module_deltaspin/cal_mw_from_lambda.cpp | 9 ++++-- .../module_deltaspin/lambda_loop.cpp | 30 +++++++++++++++++-- .../module_deltaspin/spin_constrain.h | 10 ++++++- .../module_deltaspin/template_helpers.cpp | 10 +++++-- 8 files changed, 76 insertions(+), 33 deletions(-) diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index bee55de5c5..87eec7475e 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -39,11 +39,12 @@ namespace hsolver { -template -void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, - psi::Psi& psi, - elecstate::ElecState* pes, - Charge &chr, +template +void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, + psi::Psi& psi, + elecstate::ElecState* pes, + elecstate::DensityMatrix& dm, // mohan add 2025-11-03 + Charge &chr, const int nspin, const bool skip_charge) { @@ -94,15 +95,14 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, pes->nelec_spin, pes->skip_weights); - auto _pes_lcao = dynamic_cast*>(pes); - elecstate::calEBand(_pes_lcao->ekb, _pes_lcao->wg, _pes_lcao->f_en); - elecstate::cal_dm_psi(_pes_lcao->DM->get_paraV_pointer(), _pes_lcao->wg, psi, *(_pes_lcao->DM)); - _pes_lcao->DM->cal_DMR(); + elecstate::calEBand(pes->ekb, pes->wg, pes->f_en); + elecstate::cal_dm_psi(dm.get_paraV_pointer(), pes->wg, psi, dm); + dm.cal_DMR(); if (!skip_charge) { // compute charge density from density matrix, mohan update 20251024 - LCAO_domain::dm2rho(_pes_lcao->DM->get_DMR_vector(), nspin, &chr); + LCAO_domain::dm2rho(dm.get_DMR_vector(), nspin, &chr); } else { @@ -124,7 +124,7 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, auto _pes = dynamic_cast*>(pes); pes->f_en.eband = pe.totalFreeEnergy; // maybe eferm could be dealt with in the future - _pes->dm2rho(pe.DM, pe.EDM); + _pes->dm2rho(dm, pe.EDM); #endif } diff --git a/source/source_hsolver/hsolver_lcao.h b/source/source_hsolver/hsolver_lcao.h index 67ac1fe32a..eebab34208 100644 --- a/source/source_hsolver/hsolver_lcao.h +++ b/source/source_hsolver/hsolver_lcao.h @@ -6,31 +6,33 @@ #include "source_basis/module_ao/parallel_orbitals.h" #include "source_estate/module_charge/charge.h" // mohan add 20251024 +#include "source_estate/module_dm/density_matrix.h" // mohan add 20251103 namespace hsolver { -template +template class HSolverLCAO { public: HSolverLCAO(const Parallel_Orbitals* ParaV_in, std::string method_in) : ParaV(ParaV_in), method(method_in) {}; - void solve(hamilt::Hamilt* pHamilt, - psi::Psi& psi, + void solve(hamilt::Hamilt* pHamilt, + psi::Psi& psi, elecstate::ElecState* pes, + elecstate::DensityMatrix& dm, // mohan add 2025-11-03 Charge &chr, // charge density const int nspin, const bool skip_charge); private: - void hamiltSolvePsiK(hamilt::Hamilt* hm, psi::Psi& psi, double* eigenvalue); // for kpar_lcao == 1 + void hamiltSolvePsiK(hamilt::Hamilt* hm, psi::Psi& psi, double* eigenvalue); // for kpar_lcao == 1 - void parakSolve(hamilt::Hamilt* pHamilt, psi::Psi& psi, elecstate::ElecState* pes, int kpar); // for kpar_lcao > 1 + void parakSolve(hamilt::Hamilt* pHamilt, psi::Psi& psi, elecstate::ElecState* pes, int kpar); // for kpar_lcao > 1 // The solving algorithm using cusolver is different from others, so a separate function is needed - void parakSolve_cusolver(hamilt::Hamilt* pHamilt, - psi::Psi& psi, + void parakSolve_cusolver(hamilt::Hamilt* pHamilt, + psi::Psi& psi, elecstate::ElecState* pes); const Parallel_Orbitals* ParaV; diff --git a/source/source_io/cal_ldos.cpp b/source/source_io/cal_ldos.cpp index 4db365f545..f6f0cf3fd6 100644 --- a/source/source_io/cal_ldos.cpp +++ b/source/source_io/cal_ldos.cpp @@ -13,12 +13,12 @@ namespace ModuleIO #ifdef __LCAO template void Cal_ldos::cal_ldos_lcao( - const Efermi &eferm, // mohan add 2025-11-02 + const elecstate::Efermi &eferm, // mohan add 2025-11-02 const Charge &chr, // mohan add add 2025-11-02 const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 const K_Vectors &kv, // k points, mohan add 2025-11-02 - const matrix &ekb, // mohan add 2025-11-02 - const matrix &wg, // mohan add 2025-11-02 + const ModuleBase::matrix &ekb, // mohan add 2025-11-02 + const ModuleBase::matrix &wg, // mohan add 2025-11-02 const psi::Psi& psi, const Parallel_Grid& pgrid, const UnitCell& ucell) diff --git a/source/source_io/cal_ldos.h b/source/source_io/cal_ldos.h index 75e5efd1ee..0aa59871c2 100644 --- a/source/source_io/cal_ldos.h +++ b/source/source_io/cal_ldos.h @@ -20,12 +20,12 @@ class Cal_ldos ~Cal_ldos(){}; static void cal_ldos_lcao( - const Efermi &eferm, // mohan add 2025-11-02 + const elecstate::Efermi &eferm, // mohan add 2025-11-02 const Charge &chr, // mohan add add 2025-11-02 const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 const K_Vectors &kv, // k points, mohan add 2025-11-02 - const matrix &ekb, // mohan add 2025-11-02 - const matrix &wg, // mohan add 2025-11-02 + const ModuleBase::matrix &ekb, // mohan add 2025-11-02 + const ModuleBase::matrix &wg, // mohan add 2025-11-02 const psi::Psi& psi, const Parallel_Grid& pgrid, const UnitCell& ucell); diff --git a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp index 6320f6e123..db94f4ce93 100644 --- a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp @@ -130,7 +130,9 @@ void spinconstrain::SpinConstrain>::calculate_delta_hcc(std template <> void spinconstrain::SpinConstrain>::cal_mw_from_lambda( int i_step, +#ifdef __LCAO elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 +#endif const ModuleBase::Vector3* delta_lambda) { ModuleBase::TITLE("spinconstrain::SpinConstrain", "cal_mw_from_lambda"); @@ -155,7 +157,7 @@ void spinconstrain::SpinConstrain>::cal_mw_from_lambda( } // diagonalization without update charge // mohan add two parameters charge and nspin, 2025-10-24 - hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, *this->pelec->charge, PARAM.inp.nspin, true); + hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, dm, *this->pelec->charge, PARAM.inp.nspin, true); elecstate::calculate_weights(this->pelec->ekb, this->pelec->wg, this->pelec->klist, @@ -165,9 +167,10 @@ void spinconstrain::SpinConstrain>::cal_mw_from_lambda( this->pelec->skip_weights); elecstate::calEBand(this->pelec->ekb,this->pelec->wg,this->pelec->f_en); - elecstate::cal_dm_psi(this->ParaV, pelec_lcao->wg, *psi_t, dm); + elecstate::cal_dm_psi(this->ParaV, this->pelec->wg, *psi_t, dm); + + dm.cal_DMR(); - pelec_lcao->get_DM()->cal_DMR(); this->cal_mi_lcao(i_step); } else diff --git a/source/source_lcao/module_deltaspin/lambda_loop.cpp b/source/source_lcao/module_deltaspin/lambda_loop.cpp index ac4fe88c9a..9571f70e36 100644 --- a/source/source_lcao/module_deltaspin/lambda_loop.cpp +++ b/source/source_lcao/module_deltaspin/lambda_loop.cpp @@ -99,7 +99,12 @@ void spinconstrain::SpinConstrain>::run_lambda_loop(int out template <> -void spinconstrain::SpinConstrain>::run_lambda_loop(int outer_step, bool rerun) +void spinconstrain::SpinConstrain>::run_lambda_loop( + int outer_step, +#ifdef __LCAO + elecstate::DensityMatrix, double>& dm, // mohan add 2025-11-03 +#endif + bool rerun) { // init controlling parameters int nat = this->get_nat(); @@ -139,7 +144,12 @@ void spinconstrain::SpinConstrain>::run_lambda_loop(int out double duration = 0.0; if (i_step == -1) { + +#ifdef __LCAO + this->cal_mw_from_lambda(i_step, dm); +#else this->cal_mw_from_lambda(i_step); +#endif spin = this->Mi_; where_fill_scalar_else_2d(this->constrain_, 0, zero, this->lambda_, initial_lambda); print_2d("initial lambda (eV/uB): ", initial_lambda, this->nspin_, ModuleBase::Ry_to_eV); @@ -151,7 +161,13 @@ void spinconstrain::SpinConstrain>::run_lambda_loop(int out { where_fill_scalar_else_2d(this->constrain_, 0, zero, delta_lambda, delta_lambda); add_scalar_multiply_2d(initial_lambda, delta_lambda, one, this->lambda_); - this->cal_mw_from_lambda(i_step, delta_lambda.data()); + +#ifdef __LCAO + this->cal_mw_from_lambda(i_step, dm); +#else + this->cal_mw_from_lambda(i_step); +#endif + new_spin = this->Mi_; bool GradLessThanBound = this->check_gradient_decay(new_spin, spin, delta_lambda, dnu_last_step); if (i_step >= this->nsc_min_ && GradLessThanBound) @@ -222,7 +238,12 @@ void spinconstrain::SpinConstrain>::run_lambda_loop(int out if(rms_error > this->current_sc_thr_ * 10 && rerun == true && this->higher_mag_prec == true) { std::cout<<"Error: RMS error is too large, rerun the loop"<run_lambda_loop(outer_step, dm, false); +#else this->run_lambda_loop(outer_step, false); +#endif + } } break; @@ -246,7 +267,12 @@ void spinconstrain::SpinConstrain>::run_lambda_loop(int out where_fill_scalar_else_2d(this->constrain_, 0, zero, delta_lambda, delta_lambda); add_scalar_multiply_2d(initial_lambda, delta_lambda, one, this->lambda_); + +#ifdef __LCAO + this->cal_mw_from_lambda(i_step, dm, delta_lambda.data()); +#else this->cal_mw_from_lambda(i_step, delta_lambda.data()); +#endif spin_plus = this->Mi_; diff --git a/source/source_lcao/module_deltaspin/spin_constrain.h b/source/source_lcao/module_deltaspin/spin_constrain.h index fd00f9f443..b809bc3598 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.h +++ b/source/source_lcao/module_deltaspin/spin_constrain.h @@ -14,7 +14,9 @@ #include "source_hamilt/operator.h" #include "source_estate/elecstate.h" +#ifdef __LCAO #include "source_estate/module_dm/density_matrix.h" // mohan add 2025-11-02 +#endif namespace spinconstrain { @@ -52,7 +54,9 @@ class SpinConstrain void cal_mi_pw(); void cal_mw_from_lambda(int i_step, +#ifdef __LCAO elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 +#endif const ModuleBase::Vector3* delta_lambda = nullptr); /** @@ -64,7 +68,11 @@ class SpinConstrain double get_escon(); - void run_lambda_loop(int outer_step, bool rerun = true); + void run_lambda_loop(int outer_step, +#ifdef __LCAO + elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-03 +#endif + bool rerun = true); /// @brief update the charge density for LCAO base with new lambda /// update the charge density and psi for PW base with new lambda diff --git a/source/source_lcao/module_deltaspin/template_helpers.cpp b/source/source_lcao/module_deltaspin/template_helpers.cpp index 437ecec662..77cd02a2bc 100644 --- a/source/source_lcao/module_deltaspin/template_helpers.cpp +++ b/source/source_lcao/module_deltaspin/template_helpers.cpp @@ -1,7 +1,9 @@ #include "spin_constrain.h" template <> -void spinconstrain::SpinConstrain::cal_mw_from_lambda(int i_step, const ModuleBase::Vector3* delta_lambda) +void spinconstrain::SpinConstrain::cal_mw_from_lambda(int i_step, + elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 + const ModuleBase::Vector3* delta_lambda) { } @@ -11,7 +13,9 @@ void spinconstrain::SpinConstrain::cal_mi_lcao(const int& step, bool pri } template <> -void spinconstrain::SpinConstrain::run_lambda_loop(int outer_step, bool rerun) +void spinconstrain::SpinConstrain::run_lambda_loop(int outer_step, + elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-03 + bool rerun) { } @@ -60,4 +64,4 @@ bool spinconstrain::SpinConstrain::check_gradient_decay( bool print) { return false; -} \ No newline at end of file +} From 49301a35dfe8f0807b0e29f89465bd6531fbdec9 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 08:53:17 +0800 Subject: [PATCH 48/81] the big issue to remove dm may be caused by cal_mw_from_lambda --- .../module_deltaspin/cal_mw_from_lambda.cpp | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp index db94f4ce93..a6f272afc8 100644 --- a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp @@ -437,27 +437,27 @@ void spinconstrain::SpinConstrain>::update_psi_charge(const if(pw_solve) { - hsolver::HSolverPW, base_device::DEVICE_CPU> hsolver_pw_obj(this->pw_wfc_, - PARAM.inp.calculation, - PARAM.inp.basis_type, - PARAM.inp.ks_solver, - false, - PARAM.globalv.use_uspp, - PARAM.inp.nspin, - hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::SCF_ITER, - hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::PW_DIAG_NMAX, - hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::PW_DIAG_THR, - hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::need_subspace); + hsolver::HSolverPW, base_device::DEVICE_CPU> hsolver_pw_obj(this->pw_wfc_, + PARAM.inp.calculation, + PARAM.inp.basis_type, + PARAM.inp.ks_solver, + false, + PARAM.globalv.use_uspp, + PARAM.inp.nspin, + hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::SCF_ITER, + hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::PW_DIAG_NMAX, + hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::PW_DIAG_THR, + hsolver::DiagoIterAssist, base_device::DEVICE_CPU>::need_subspace); - hsolver_pw_obj.solve(hamilt_t, - psi_t[0], - this->pelec, - this->pelec->ekb.c, - GlobalV::RANK_IN_POOL, - GlobalV::NPROC_IN_POOL, - false, - this->tpiba, - this->get_nat()); + hsolver_pw_obj.solve(hamilt_t, + psi_t[0], + this->pelec, + this->pelec->ekb.c, + GlobalV::RANK_IN_POOL, + GlobalV::NPROC_IN_POOL, + false, + this->tpiba, + this->get_nat()); } else {// update charge density only @@ -467,17 +467,17 @@ void spinconstrain::SpinConstrain>::update_psi_charge(const #if ((defined __CUDA) || (defined __ROCM)) else { - base_device::DEVICE_GPU* ctx = {}; - base_device::DEVICE_CPU* cpu_ctx = {}; - psi::Psi, base_device::DEVICE_GPU>* psi_t = static_cast, base_device::DEVICE_GPU>*>(this->psi); - hamilt::Hamilt, base_device::DEVICE_GPU>* hamilt_t = static_cast, base_device::DEVICE_GPU>*>(this->p_hamilt); - auto* onsite_p = projectors::OnsiteProjector::get_instance(); - nbands = psi_t->get_nbands(); - npol = psi_t->get_npol(); - nkb = onsite_p->get_tot_nproj(); - nk = psi_t->get_nk(); - nh_iat = &onsite_p->get_nh(0); - size_becp = nbands * nkb * npol; + base_device::DEVICE_GPU* ctx = {}; + base_device::DEVICE_CPU* cpu_ctx = {}; + psi::Psi, base_device::DEVICE_GPU>* psi_t = static_cast, base_device::DEVICE_GPU>*>(this->psi); + hamilt::Hamilt, base_device::DEVICE_GPU>* hamilt_t = static_cast, base_device::DEVICE_GPU>*>(this->p_hamilt); + auto* onsite_p = projectors::OnsiteProjector::get_instance(); + nbands = psi_t->get_nbands(); + npol = psi_t->get_npol(); + nkb = onsite_p->get_tot_nproj(); + nk = psi_t->get_nk(); + nh_iat = &onsite_p->get_nh(0); + size_becp = nbands * nkb * npol; std::complex* h_tmp = nullptr; std::complex* s_tmp = nullptr; From ba7d6e6a2360ac65f6c0f969232ec66801ebcda2 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 13:21:41 +0800 Subject: [PATCH 49/81] remove some DM --- source/source_esolver/esolver_dm2rho.cpp | 11 ++----- source/source_esolver/esolver_ks_lcao.cpp | 9 +++--- source/source_esolver/esolver_ks_lcao.h | 2 +- .../source_esolver/esolver_ks_lcao_tddft.cpp | 15 +++++----- source/source_esolver/lcao_others.cpp | 3 +- .../module_deltaspin/cal_mw_from_lambda.cpp | 9 ++---- .../source_lcao/module_deltaspin/init_sc.cpp | 30 +++++++++++-------- .../module_deltaspin/lambda_loop.cpp | 20 ------------- .../module_deltaspin/spin_constrain.h | 19 +++++++----- .../module_deltaspin/template_helpers.cpp | 2 -- source/source_lcao/setup_dm.h | 2 +- 11 files changed, 49 insertions(+), 73 deletions(-) diff --git a/source/source_esolver/esolver_dm2rho.cpp b/source/source_esolver/esolver_dm2rho.cpp index 4c78c5db53..5f03717c68 100644 --- a/source/source_esolver/esolver_dm2rho.cpp +++ b/source/source_esolver/esolver_dm2rho.cpp @@ -46,16 +46,9 @@ void ESolver_DM2rho::runner(UnitCell& ucell, const int istep) ESolver_KS_LCAO::before_scf(ucell, istep); - auto* estate = dynamic_cast*>(this->pelec); - - if(!estate) - { - ModuleBase::WARNING_QUIT("ESolver_DM2rho::after_scf","pelec does not exist"); - } - // file name of DM std::string zipname = "output_DM0.npz"; - elecstate::DensityMatrix* dm = estate->get_DM(); + elecstate::DensityMatrix* dm = this->dmat.get_dm(); // read DM from file ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(dm->get_DMR_pointer(1))); @@ -69,7 +62,7 @@ void ESolver_DM2rho::runner(UnitCell& ucell, const int istep) // it's dangerous to design psiToRho function like this, mohan note 20251024 // this->pelec->psiToRho(*this->psi); - LCAO_domain::dm2rho(estate->DM->get_DMR_vector(), PARAM.inp.nspin, &this->chr); + LCAO_domain::dm2rho(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, &this->chr); int nspin0 = PARAM.inp.nspin == 2 ? 2 : 1; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index c6cdf68c3c..95beb088cd 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -208,7 +208,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) 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, &(this->pv), - PARAM.inp.nspin, this->kv, this->p_hamilt, this->psi, this->pelec); + PARAM.inp.nspin, this->kv, this->p_hamilt, this->psi, this->dmat.get_dm(), this->pelec); } // 11) set xc type before the first cal of xc in pelec->init_scf, Peize Lin add 2016-12-03 @@ -468,7 +468,8 @@ void ESolver_KS_LCAO::hamilt2rho_single(UnitCell& ucell, int istep, int if (!skip_solve) { hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), PARAM.inp.ks_solver); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, this->chr, PARAM.inp.nspin, skip_charge); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.get_dm(), + this->chr, PARAM.inp.nspin, skip_charge); } // 4) EXX @@ -557,7 +558,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 = estate->get_DM(); + elecstate::DensityMatrix* dm = this->dmat.get_dm(); this->p_chgmix->mix_dmr(dm); } } @@ -597,7 +598,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const if (PARAM.inp.out_elf[0] > 0) { - LCAO_domain::dm2tau(estate->DM->get_DMR_vector(), PARAM.inp.nspin, estate->charge); + LCAO_domain::dm2tau(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, estate->charge); } //! 1) call after_scf() of ESolver_KS diff --git a/source/source_esolver/esolver_ks_lcao.h b/source/source_esolver/esolver_ks_lcao.h index 4309090ba5..3cc9a7ae27 100644 --- a/source/source_esolver/esolver_ks_lcao.h +++ b/source/source_esolver/esolver_ks_lcao.h @@ -73,7 +73,7 @@ class ESolver_KS_LCAO : public ESolver_KS TwoCenterBundle two_center_bundle_; //! Add density matrix class, mohan add 2025-10-30 - Setup_DM dmat; + LCAO_DOMAIN::Setup_DM dmat; //! For RDMFT calculations, added by jghan, 2024-03-16 rdmft::RDMFT rdmft_solver; diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index a70d13bd02..86457a328b 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -292,7 +292,8 @@ void ESolver_KS_LCAO_TDDFT::hamilt2rho_single(UnitCell& ucell, { bool skip_charge = PARAM.inp.calculation == "nscf" ? true : false; hsolver::HSolverLCAO> hsolver_lcao_obj(&this->pv, PARAM.inp.ks_solver); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, this->chr, PARAM.inp.nspin, skip_charge); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.get_dm(), + this->chr, PARAM.inp.nspin, skip_charge); } } @@ -497,7 +498,8 @@ void ESolver_KS_LCAO_TDDFT::after_scf(UnitCell& ucell, const int ist } } elecstate::DensityMatrix, double>* tmp_DM - = dynamic_cast>*>(this->pelec)->get_DM(); + = this->dmat.get_dm(); + // (2) write current information if(TD_info::out_current) { @@ -562,19 +564,18 @@ void ESolver_KS_LCAO_TDDFT::weight_dm_rho(const UnitCell& ucell) // calculate the density matrix ModuleBase::GlobalFunc::NOTE("Calculate the density matrix."); - auto _pes = dynamic_cast>*>(this->pelec); - elecstate::cal_dm_psi(_pes->DM->get_paraV_pointer(), _pes->wg, this->psi[0], *(_pes->DM)); + elecstate::cal_dm_psi(this->dmat.get_dm()->get_paraV_pointer(), this->pelec->wg, this->psi[0], *this->dmat.get_dm()); if(PARAM.inp.td_stype == 2) { - _pes->DM->cal_DMR_td(ucell, TD_info::cart_At); + this->dmat.get_dm()->cal_DMR_td(ucell, TD_info::cart_At); } else { - _pes->DM->cal_DMR(); + this->dmat.get_dm()->cal_DMR(); } // get the real-space charge density, mohan add 2025-10-24 - LCAO_domain::dm2rho(_pes->DM->get_DMR_vector(), PARAM.inp.nspin, &this->chr); + LCAO_domain::dm2rho(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, &this->chr); } template class ESolver_KS_LCAO_TDDFT; diff --git a/source/source_esolver/lcao_others.cpp b/source/source_esolver/lcao_others.cpp index a31dc28a9c..2ee2a020f6 100644 --- a/source/source_esolver/lcao_others.cpp +++ b/source/source_esolver/lcao_others.cpp @@ -173,7 +173,6 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) } if (this->p_hamilt == nullptr) { - elecstate::DensityMatrix* DM = dynamic_cast*>(this->pelec)->get_DM(); this->p_hamilt = new hamilt::HamiltLCAO( ucell, this->gd, @@ -182,7 +181,7 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) this->kv, two_center_bundle_, orb_, - DM, + this->dmat.get_dm(), this->deepks, istep, this->exx_nao); diff --git a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp index a6f272afc8..e8bb81db23 100644 --- a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp @@ -130,9 +130,6 @@ void spinconstrain::SpinConstrain>::calculate_delta_hcc(std template <> void spinconstrain::SpinConstrain>::cal_mw_from_lambda( int i_step, -#ifdef __LCAO - elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 -#endif const ModuleBase::Vector3* delta_lambda) { ModuleBase::TITLE("spinconstrain::SpinConstrain", "cal_mw_from_lambda"); @@ -157,7 +154,7 @@ void spinconstrain::SpinConstrain>::cal_mw_from_lambda( } // diagonalization without update charge // mohan add two parameters charge and nspin, 2025-10-24 - hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, dm, *this->pelec->charge, PARAM.inp.nspin, true); + hsolver_t.solve(hamilt_t, psi_t[0], this->pelec, *this->dm_, *this->pelec->charge, PARAM.inp.nspin, true); elecstate::calculate_weights(this->pelec->ekb, this->pelec->wg, this->pelec->klist, @@ -167,9 +164,9 @@ void spinconstrain::SpinConstrain>::cal_mw_from_lambda( this->pelec->skip_weights); elecstate::calEBand(this->pelec->ekb,this->pelec->wg,this->pelec->f_en); - elecstate::cal_dm_psi(this->ParaV, this->pelec->wg, *psi_t, dm); + elecstate::cal_dm_psi(this->ParaV, this->pelec->wg, *psi_t, *this->dm_); - dm.cal_DMR(); + this->dm_->cal_DMR(); this->cal_mi_lcao(i_step); } diff --git a/source/source_lcao/module_deltaspin/init_sc.cpp b/source/source_lcao/module_deltaspin/init_sc.cpp index ab192fe133..d62e66700d 100644 --- a/source/source_lcao/module_deltaspin/init_sc.cpp +++ b/source/source_lcao/module_deltaspin/init_sc.cpp @@ -3,19 +3,22 @@ // init sc template void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, - int nsc_in, - int nsc_min_in, - double alpha_trial_in, - double sccut_in, - double sc_drop_thr_in, - const UnitCell& ucell, - Parallel_Orbitals* ParaV_in, - int nspin_in, - const K_Vectors& kv_in, - void* p_hamilt_in, - void* psi_in, - elecstate::ElecState* pelec_in, - ModulePW::PW_Basis_K* pw_wfc_in) + int nsc_in, + int nsc_min_in, + double alpha_trial_in, + double sccut_in, + double sc_drop_thr_in, + const UnitCell& ucell, + Parallel_Orbitals* ParaV_in, + int nspin_in, + const K_Vectors& kv_in, + void* p_hamilt_in, + void* psi_in, +#ifdef __LCAO + elecstate::DensityMatrix, double>* dm_in, // mohan add 2025-11-03 +#endif + elecstate::ElecState* pelec_in, + ModulePW::PW_Basis_K* pw_wfc_in) { this->set_input_parameters(sc_thr_in, nsc_in, nsc_min_in, alpha_trial_in, sccut_in, sc_drop_thr_in); this->set_atomCounts(ucell.get_atom_Counts()); @@ -31,6 +34,7 @@ void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, this->set_decay_grad(); if(ParaV_in != nullptr) this->set_ParaV(ParaV_in); this->set_solver_parameters(kv_in, p_hamilt_in, psi_in, pelec_in); + this->dm_ = dm_in; // mohan add 2025-11-03 } template class spinconstrain::SpinConstrain>; diff --git a/source/source_lcao/module_deltaspin/lambda_loop.cpp b/source/source_lcao/module_deltaspin/lambda_loop.cpp index 9571f70e36..8b87f7eedd 100644 --- a/source/source_lcao/module_deltaspin/lambda_loop.cpp +++ b/source/source_lcao/module_deltaspin/lambda_loop.cpp @@ -101,9 +101,6 @@ void spinconstrain::SpinConstrain>::run_lambda_loop(int out template <> void spinconstrain::SpinConstrain>::run_lambda_loop( int outer_step, -#ifdef __LCAO - elecstate::DensityMatrix, double>& dm, // mohan add 2025-11-03 -#endif bool rerun) { // init controlling parameters @@ -145,11 +142,7 @@ void spinconstrain::SpinConstrain>::run_lambda_loop( if (i_step == -1) { -#ifdef __LCAO - this->cal_mw_from_lambda(i_step, dm); -#else this->cal_mw_from_lambda(i_step); -#endif spin = this->Mi_; where_fill_scalar_else_2d(this->constrain_, 0, zero, this->lambda_, initial_lambda); print_2d("initial lambda (eV/uB): ", initial_lambda, this->nspin_, ModuleBase::Ry_to_eV); @@ -162,11 +155,7 @@ void spinconstrain::SpinConstrain>::run_lambda_loop( where_fill_scalar_else_2d(this->constrain_, 0, zero, delta_lambda, delta_lambda); add_scalar_multiply_2d(initial_lambda, delta_lambda, one, this->lambda_); -#ifdef __LCAO - this->cal_mw_from_lambda(i_step, dm); -#else this->cal_mw_from_lambda(i_step); -#endif new_spin = this->Mi_; bool GradLessThanBound = this->check_gradient_decay(new_spin, spin, delta_lambda, dnu_last_step); @@ -238,12 +227,7 @@ void spinconstrain::SpinConstrain>::run_lambda_loop( if(rms_error > this->current_sc_thr_ * 10 && rerun == true && this->higher_mag_prec == true) { std::cout<<"Error: RMS error is too large, rerun the loop"<run_lambda_loop(outer_step, dm, false); -#else this->run_lambda_loop(outer_step, false); -#endif - } } break; @@ -268,11 +252,7 @@ void spinconstrain::SpinConstrain>::run_lambda_loop( where_fill_scalar_else_2d(this->constrain_, 0, zero, delta_lambda, delta_lambda); add_scalar_multiply_2d(initial_lambda, delta_lambda, one, this->lambda_); -#ifdef __LCAO - this->cal_mw_from_lambda(i_step, dm, delta_lambda.data()); -#else this->cal_mw_from_lambda(i_step, delta_lambda.data()); -#endif spin_plus = this->Mi_; diff --git a/source/source_lcao/module_deltaspin/spin_constrain.h b/source/source_lcao/module_deltaspin/spin_constrain.h index b809bc3598..f5490a8b0b 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.h +++ b/source/source_lcao/module_deltaspin/spin_constrain.h @@ -43,7 +43,10 @@ class SpinConstrain const K_Vectors& kv_in, void* p_hamilt_in, void* psi_in, - elecstate::ElecState* pelec_in, +#ifdef __LCAO + elecstate::DensityMatrix, double> *dm_in, // mohan add 2025-11-02 +#endif + elecstate::ElecState* pelec_in, ModulePW::PW_Basis_K* pw_wfc_in = nullptr); /// @brief calculate the magnetization of each atom with real space projection method for LCAO base @@ -54,9 +57,6 @@ class SpinConstrain void cal_mi_pw(); void cal_mw_from_lambda(int i_step, -#ifdef __LCAO - elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 -#endif const ModuleBase::Vector3* delta_lambda = nullptr); /** @@ -69,16 +69,16 @@ class SpinConstrain double get_escon(); void run_lambda_loop(int outer_step, -#ifdef __LCAO - elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-03 -#endif bool rerun = true); /// @brief update the charge density for LCAO base with new lambda /// update the charge density and psi for PW base with new lambda void update_psi_charge(const ModuleBase::Vector3* delta_lambda, bool pw_solve = true); - void calculate_delta_hcc(std::complex* h_tmp, const std::complex* becp_k, const ModuleBase::Vector3* delta_lambda, const int nbands, const int nkb, const int* nh_iat); + void calculate_delta_hcc(std::complex* h_tmp, + const std::complex* becp_k, + const ModuleBase::Vector3* delta_lambda, + const int nbands, const int nkb, const int* nh_iat); /// lambda loop helper functions bool check_rms_stop(int outer_step, int i_step, double rms_error, double duration, double total_duration); @@ -121,6 +121,9 @@ class SpinConstrain void* psi = nullptr; elecstate::ElecState* pelec = nullptr; ModulePW::PW_Basis_K* pw_wfc_ = nullptr; +#ifdef __LCAO + elecstate::DensityMatrix, double>* dm_; +#endif double tpiba = 0.0; /// save ucell.tpiba const double meV_to_Ry = 7.349864435130999e-05; K_Vectors kv_; diff --git a/source/source_lcao/module_deltaspin/template_helpers.cpp b/source/source_lcao/module_deltaspin/template_helpers.cpp index 77cd02a2bc..83e5f17f75 100644 --- a/source/source_lcao/module_deltaspin/template_helpers.cpp +++ b/source/source_lcao/module_deltaspin/template_helpers.cpp @@ -2,7 +2,6 @@ template <> void spinconstrain::SpinConstrain::cal_mw_from_lambda(int i_step, - elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-02 const ModuleBase::Vector3* delta_lambda) { } @@ -14,7 +13,6 @@ void spinconstrain::SpinConstrain::cal_mi_lcao(const int& step, bool pri template <> void spinconstrain::SpinConstrain::run_lambda_loop(int outer_step, - elecstate::DensityMatrix, double> &dm, // mohan add 2025-11-03 bool rerun) { } diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h index 1fd6c7d4ec..58d7808f53 100644 --- a/source/source_lcao/setup_dm.h +++ b/source/source_lcao/setup_dm.h @@ -18,7 +18,7 @@ class Setup_DM ~Setup_DM() { - if (this->DM != nullptr) + if (this->dm != nullptr) { delete this->dm; } From 6ff99f007a2b14579f2dcb9e64d609052b064139 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 13:31:28 +0800 Subject: [PATCH 50/81] keep updating DM --- source/source_esolver/esolver_ks_lcao.cpp | 2 +- source/source_io/ctrl_iter_lcao.cpp | 6 +++++- source/source_io/ctrl_iter_lcao.h | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 95beb088cd..53da5cb262 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -598,7 +598,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const if (PARAM.inp.out_elf[0] > 0) { - LCAO_domain::dm2tau(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, estate->charge); + LCAO_domain::dm2tau(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, this->pelec->charge); } //! 1) call after_scf() of ESolver_KS diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index d0b0c1bd0a..7bc9299438 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -14,6 +14,7 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO* pelec, // electronic info * + elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi* psi, // wave functions * @@ -68,7 +69,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, pelec->get_DM(), + ucell, orb, gd, &pv, *psi, &dm, p_hamilt, iter, conv_esolver, GlobalV::MY_RANK, GlobalV::ofs_running); } } @@ -82,6 +83,7 @@ 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 * + elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi* psi, // wave functions * @@ -101,6 +103,7 @@ template void ctrl_iter_lcao, double>(UnitCell& ucell, // u const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO>* pelec, // electronic info * + elecstate::DensityMatrix, double>& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi>* psi, // wave functions * @@ -120,6 +123,7 @@ template void ctrl_iter_lcao, std::complex>(UnitCel const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO>* pelec, // electronic info * + elecstate::DensityMatrix, std::complex>& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi>* psi, // wave functions * diff --git a/source/source_io/ctrl_iter_lcao.h b/source/source_io/ctrl_iter_lcao.h index c94afac122..7fa33f2f96 100644 --- a/source/source_io/ctrl_iter_lcao.h +++ b/source/source_io/ctrl_iter_lcao.h @@ -19,6 +19,7 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO* pelec, // electronic info * + elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi* psi, // wave functions * From 6c0fbe36d26396e65b397620111f4b434df026d7 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 14:16:11 +0800 Subject: [PATCH 51/81] refactor some DM in double_xc --- source/source_esolver/esolver_double_xc.cpp | 53 +++--- source/source_esolver/esolver_double_xc.h | 3 + source/source_esolver/esolver_ks_lcao.cpp | 10 +- .../source_lcao/module_deltaspin/init_sc.cpp | 6 +- .../module_deltaspin/spin_constrain.cpp | 170 +++++++++--------- .../module_deltaspin/spin_constrain.h | 16 +- 6 files changed, 127 insertions(+), 131 deletions(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index 814c675b8c..cb328c278a 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -84,7 +84,7 @@ void ESolver_DoubleXC::before_all_runners(UnitCell& ucell, const Input_p } // 6) initialize the density matrix - dynamic_cast*>(this->pelec_base)->init_DM(&this->kv, &(this->pv), PARAM.inp.nspin); + this->dmat_base.allocate_dm(&this->kv, &(this->pv), PARAM.inp.nspin); // 10) inititlize the charge density this->chr_base.allocate(PARAM.inp.nspin); @@ -138,8 +138,6 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) } if (this->p_hamilt_base == nullptr) { - elecstate::DensityMatrix* DM = dynamic_cast*>(this->pelec_base)->get_DM(); - this->p_hamilt_base = new hamilt::HamiltLCAO( ucell, this->gd, @@ -148,7 +146,7 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) this->kv, this->two_center_bundle_, this->orb_, - DM, + this->dmat_base.get_dm(), this->deepks, istep, this->exx_nao); @@ -159,13 +157,11 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); // DMR should be same size with Hamiltonian(R) - dynamic_cast*>(this->pelec_base) - ->get_DM() - ->init_DMR(*(dynamic_cast*>(this->p_hamilt_base)->getHR())); + this->dmat_base.get_dm()->init_DMR(*(dynamic_cast*>(this->p_hamilt_base)->getHR())); if (istep > 0) { - dynamic_cast*>(this->pelec_base)->get_DM()->cal_DMR(); + this->dmat_base.get_dm()->cal_DMR(); } ModuleBase::timer::tick("ESolver_DoubleXC", "before_scf"); @@ -226,23 +222,23 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int std::shared_ptr> ld_shared_ptr(&this->deepks.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, - this->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, + this->orb_, + this->gd, + &(this->pv), + *(this->psi), + this->dmat_base.get_dm(), + p_ham_deepks, + iter, + conv_esolver, + GlobalV::MY_RANK, + GlobalV::ofs_running); #endif // restore to density after charge mixing @@ -352,9 +348,12 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int auto _pes_lcao = dynamic_cast*>(this->pelec); for (int ik = 0; ik < nks; ik++) { - _pes_lcao_base->get_DM()->set_DMK_pointer(ik, _pes_lcao->get_DM()->get_DMK_pointer(ik)); +// mohan update 2025-11-03 + this->dmat_base.get_dm()->set_DMK_pointer(ik, this->dmat.get_dm()->get_DMK_pointer(ik)); +// _pes_lcao_base->get_DM()->set_DMK_pointer(ik, _pes_lcao->get_DM()->get_DMK_pointer(ik)); } - _pes_lcao_base->get_DM()->cal_DMR(); + this->dmat_base.get_dm()->cal_DMR(); +// _pes_lcao_base->get_DM()->cal_DMR(); _pes_lcao_base->ekb = _pes_lcao->ekb; _pes_lcao_base->wg = _pes_lcao->wg; } diff --git a/source/source_esolver/esolver_double_xc.h b/source/source_esolver/esolver_double_xc.h index 591411c82d..8e6d85e4b7 100644 --- a/source/source_esolver/esolver_double_xc.h +++ b/source/source_esolver/esolver_double_xc.h @@ -32,6 +32,9 @@ class ESolver_DoubleXC : public ESolver_KS_LCAO //! Electronic states elecstate::ElecState* pelec_base = nullptr; + //! Density Matrix, mohan add 2025-11-03 + LCAO_DOMAIN::Setup_DM dmat_base; + //! Electorn charge density Charge chr_base; }; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 53da5cb262..5d06ff0b78 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -153,12 +153,6 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) //! 1) call before_scf() of ESolver_KS. ESolver_KS::before_scf(ucell, istep); - auto* estate = dynamic_cast*>(this->pelec); - if(!estate) - { - ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","pelec does not exist"); - } - //! 2) find search radius double search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, PARAM.inp.out_level, orb_.get_rcutmax_Phi(), ucell.infoNL.get_rcutmax_Beta(), @@ -517,7 +511,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_finish","p_hamilt does not exist"); } - const std::vector>& dm_vec = estate->get_DM()->get_DMK_vector(); + const std::vector>& dm_vec = this->dmat.get_dm()->get_DMK_vector(); // 1) calculate the local occupation number matrix and energy correction in DFT+U if (PARAM.inp.dft_plus_u) @@ -528,7 +522,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { if (GlobalC::dftu.omc != 2) { - ModuleDFTU::dftu_cal_occup_m(iter, ucell, dm_vec, this->kv, + ModuleDFTU::dftu_cal_occup_m(iter, ucell, *dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), hamilt_lcao); } GlobalC::dftu.cal_energy_correction(ucell, istep); diff --git a/source/source_lcao/module_deltaspin/init_sc.cpp b/source/source_lcao/module_deltaspin/init_sc.cpp index d62e66700d..9b2ee0483f 100644 --- a/source/source_lcao/module_deltaspin/init_sc.cpp +++ b/source/source_lcao/module_deltaspin/init_sc.cpp @@ -1,8 +1,8 @@ #include "spin_constrain.h" // init sc -template -void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, +template +void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, int nsc_in, int nsc_min_in, double alpha_trial_in, @@ -15,7 +15,7 @@ void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, void* p_hamilt_in, void* psi_in, #ifdef __LCAO - elecstate::DensityMatrix, double>* dm_in, // mohan add 2025-11-03 + elecstate::DensityMatrix* dm_in, // mohan add 2025-11-03 #endif elecstate::ElecState* pelec_in, ModulePW::PW_Basis_K* pw_wfc_in) diff --git a/source/source_lcao/module_deltaspin/spin_constrain.cpp b/source/source_lcao/module_deltaspin/spin_constrain.cpp index 6213f528f7..6b49487975 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.cpp +++ b/source/source_lcao/module_deltaspin/spin_constrain.cpp @@ -8,15 +8,15 @@ namespace spinconstrain { -template -SpinConstrain& SpinConstrain::getScInstance() +template +SpinConstrain& SpinConstrain::getScInstance() { - static SpinConstrain instance; // Guaranteed to be created and destroyed only once + static SpinConstrain instance; // Guaranteed to be created and destroyed only once return instance; } -template -double SpinConstrain::cal_escon() +template +double SpinConstrain::cal_escon() { this->escon_ = 0.0; if (!this->is_Mi_converged) @@ -33,30 +33,30 @@ double SpinConstrain::cal_escon() return this->escon_; } -template -double SpinConstrain::get_escon() +template +double SpinConstrain::get_escon() { return this->escon_; } // set atomCounts -template -void SpinConstrain::set_atomCounts(const std::map& atomCounts_in) +template +void SpinConstrain::set_atomCounts(const std::map& atomCounts_in) { this->atomCounts.clear(); this->atomCounts = atomCounts_in; } // get atomCounts -template -const std::map& SpinConstrain::get_atomCounts() const +template +const std::map& SpinConstrain::get_atomCounts() const { return this->atomCounts; } /// set nspin -template -void SpinConstrain::set_nspin(int nspin_in) +template +void SpinConstrain::set_nspin(int nspin_in) { if (nspin_in != 4 && nspin_in != 2) { @@ -66,14 +66,14 @@ void SpinConstrain::set_nspin(int nspin_in) } /// get nspin -template -int SpinConstrain::get_nspin() +template +int SpinConstrain::get_nspin() { return this->nspin_; } -template -int SpinConstrain::get_nat() +template +int SpinConstrain::get_nat() { int nat = 0; for (std::map::iterator it = this->atomCounts.begin(); it != this->atomCounts.end(); ++it) @@ -83,14 +83,14 @@ int SpinConstrain::get_nat() return nat; } -template -int SpinConstrain::get_ntype() +template +int SpinConstrain::get_ntype() { return this->atomCounts.size(); } -template -void SpinConstrain::check_atomCounts() +template +void SpinConstrain::check_atomCounts() { if (!this->atomCounts.size()) { @@ -116,8 +116,8 @@ void SpinConstrain::check_atomCounts() } // get iat -template -int SpinConstrain::get_iat(int itype, int atom_index) +template +int SpinConstrain::get_iat(int itype, int atom_index) { if (itype < 0 || itype >= this->get_ntype()) { @@ -141,38 +141,38 @@ int SpinConstrain::get_iat(int itype, int atom_index) } // set orbitalCounts -template -void SpinConstrain::set_orbitalCounts(const std::map& orbitalCounts_in) +template +void SpinConstrain::set_orbitalCounts(const std::map& orbitalCounts_in) { this->orbitalCounts.clear(); this->orbitalCounts = orbitalCounts_in; } // get orbitalCounts -template -const std::map& SpinConstrain::get_orbitalCounts() const +template +const std::map& SpinConstrain::get_orbitalCounts() const { return this->orbitalCounts; } // set lnchiCounts -template -void SpinConstrain::set_lnchiCounts(const std::map>& lnchiCounts_in) +template +void SpinConstrain::set_lnchiCounts(const std::map>& lnchiCounts_in) { this->lnchiCounts.clear(); this->lnchiCounts = lnchiCounts_in; } // get lnchiCounts -template -const std::map>& SpinConstrain::get_lnchiCounts() const +template +const std::map>& SpinConstrain::get_lnchiCounts() const { return this->lnchiCounts; } // set sc_lambda from ScData -template -void SpinConstrain::set_sc_lambda() +template +void SpinConstrain::set_sc_lambda() { this->check_atomCounts(); int nat = this->get_nat(); @@ -194,8 +194,8 @@ void SpinConstrain::set_sc_lambda() } // set target_mag from ScData -template -void SpinConstrain::set_target_mag() +template +void SpinConstrain::set_target_mag() { this->check_atomCounts(); int nat = this->get_nat(); @@ -234,8 +234,8 @@ void SpinConstrain::set_target_mag() } // set constrain from ScData -template -void SpinConstrain::set_constrain() +template +void SpinConstrain::set_constrain() { this->check_atomCounts(); int nat = this->get_nat(); @@ -266,8 +266,8 @@ void SpinConstrain::set_constrain() } // set sc_lambda from variable -template -void SpinConstrain::set_sc_lambda(const ModuleBase::Vector3* lambda_in, int nat_in) +template +void SpinConstrain::set_sc_lambda(const ModuleBase::Vector3* lambda_in, int nat_in) { this->check_atomCounts(); int nat = this->get_nat(); @@ -283,8 +283,8 @@ void SpinConstrain::set_sc_lambda(const ModuleBase::Vector3* lam } // set target_mag from variable -template -void SpinConstrain::set_target_mag(const ModuleBase::Vector3* target_mag_in, int nat_in) +template +void SpinConstrain::set_target_mag(const ModuleBase::Vector3* target_mag_in, int nat_in) { this->check_atomCounts(); int nat = this->get_nat(); @@ -299,8 +299,8 @@ void SpinConstrain::set_target_mag(const ModuleBase::Vector3* ta } } -template -void SpinConstrain::set_target_mag(const std::vector>& target_mag_in) +template +void SpinConstrain::set_target_mag(const std::vector>& target_mag_in) { int nat = this->get_nat(); assert(target_mag_in.size() == nat); @@ -324,8 +324,8 @@ void SpinConstrain::set_target_mag(const std::vector -void SpinConstrain::set_constrain(const ModuleBase::Vector3* constrain_in, int nat_in) +template +void SpinConstrain::set_constrain(const ModuleBase::Vector3* constrain_in, int nat_in) { this->check_atomCounts(); int nat = this->get_nat(); @@ -340,28 +340,28 @@ void SpinConstrain::set_constrain(const ModuleBase::Vector3* constr } } -template -const std::vector>& SpinConstrain::get_sc_lambda() const +template +const std::vector>& SpinConstrain::get_sc_lambda() const { return this->lambda_; } -template -const std::vector>& SpinConstrain::get_target_mag() const +template +const std::vector>& SpinConstrain::get_target_mag() const { return this->target_mag_; } /// get_constrain -template -const std::vector>& SpinConstrain::get_constrain() const +template +const std::vector>& SpinConstrain::get_constrain() const { return this->constrain_; } /// zero atomic magnetic moment -template -void SpinConstrain::zero_Mi() +template +void SpinConstrain::zero_Mi() { this->check_atomCounts(); int nat = this->get_nat(); @@ -377,15 +377,15 @@ void SpinConstrain::zero_Mi() /// get grad_decay /// this function can only be called by the root process because only /// root process reads the ScDecayGrad from json file -template -double SpinConstrain::get_decay_grad(int itype) +template +double SpinConstrain::get_decay_grad(int itype) { return this->ScDecayGrad[itype]; } /// set grad_decy -template -void SpinConstrain::set_decay_grad() +template +void SpinConstrain::set_decay_grad() { this->check_atomCounts(); int ntype = this->get_ntype(); @@ -397,15 +397,15 @@ void SpinConstrain::set_decay_grad() } /// get decay_grad -template -const std::vector& SpinConstrain::get_decay_grad() +template +const std::vector& SpinConstrain::get_decay_grad() { return this->decay_grad_; } /// set grad_decy from variable -template -void SpinConstrain::set_decay_grad(const double* decay_grad_in, int ntype_in) +template +void SpinConstrain::set_decay_grad(const double* decay_grad_in, int ntype_in) { this->check_atomCounts(); int ntype = this->get_ntype(); @@ -421,8 +421,8 @@ void SpinConstrain::set_decay_grad(const double* decay_grad_in, int ntyp } /// @brief set input parameters -template -void SpinConstrain::set_input_parameters(double sc_thr_in, +template +void SpinConstrain::set_input_parameters(double sc_thr_in, int nsc_in, int nsc_min_in, double alpha_trial_in, @@ -438,56 +438,56 @@ void SpinConstrain::set_input_parameters(double sc_thr_in, } /// get sc_thr -template -double SpinConstrain::get_sc_thr() +template +double SpinConstrain::get_sc_thr() { return this->sc_thr_; } /// get nsc -template -int SpinConstrain::get_nsc() +template +int SpinConstrain::get_nsc() { return this->nsc_; } /// get nsc_min -template -int SpinConstrain::get_nsc_min() +template +int SpinConstrain::get_nsc_min() { return this->nsc_min_; } /// get alpha_trial -template -double SpinConstrain::get_alpha_trial() +template +double SpinConstrain::get_alpha_trial() { return this->alpha_trial_; } /// get sccut -template -double SpinConstrain::get_sccut() +template +double SpinConstrain::get_sccut() { return this->restrict_current_; } /// set sc_drop_thr -template -void SpinConstrain::set_sc_drop_thr(double sc_drop_thr_in) +template +void SpinConstrain::set_sc_drop_thr(double sc_drop_thr_in) { this->sc_drop_thr_ = sc_drop_thr_in; } /// get sc_drop_thr -template -double SpinConstrain::get_sc_drop_thr() +template +double SpinConstrain::get_sc_drop_thr() { return this->sc_drop_thr_; } -template -void SpinConstrain::set_solver_parameters(const K_Vectors& kv_in, +template +void SpinConstrain::set_solver_parameters(const K_Vectors& kv_in, void* p_hamilt_in, void* psi_in, elecstate::ElecState* pelec_in) @@ -499,8 +499,8 @@ void SpinConstrain::set_solver_parameters(const K_Vectors& kv_in, } /// @brief set ParaV -template -void SpinConstrain::set_ParaV(Parallel_Orbitals* ParaV_in) +template +void SpinConstrain::set_ParaV(Parallel_Orbitals* ParaV_in) { this->ParaV = ParaV_in; int nloc = this->ParaV->nloc; @@ -511,8 +511,8 @@ void SpinConstrain::set_ParaV(Parallel_Orbitals* ParaV_in) } /// print Mi -template -void SpinConstrain::print_Mi(std::ofstream& ofs_running) +template +void SpinConstrain::print_Mi(std::ofstream& ofs_running) { this->check_atomCounts(); int nat = this->get_nat(); @@ -556,8 +556,8 @@ void SpinConstrain::print_Mi(std::ofstream& ofs_running) } /// print magnetic force (defined as \frac{\delta{L}}/{\delta{Mi}} = -lambda[iat]) -template -void SpinConstrain::print_Mag_Force(std::ofstream& ofs_running) +template +void SpinConstrain::print_Mag_Force(std::ofstream& ofs_running) { this->check_atomCounts(); int nat = this->get_nat(); diff --git a/source/source_lcao/module_deltaspin/spin_constrain.h b/source/source_lcao/module_deltaspin/spin_constrain.h index f5490a8b0b..f4868aa93a 100644 --- a/source/source_lcao/module_deltaspin/spin_constrain.h +++ b/source/source_lcao/module_deltaspin/spin_constrain.h @@ -23,7 +23,7 @@ namespace spinconstrain struct ScAtomData; -template +template class SpinConstrain { public: @@ -44,7 +44,7 @@ class SpinConstrain void* p_hamilt_in, void* psi_in, #ifdef __LCAO - elecstate::DensityMatrix, double> *dm_in, // mohan add 2025-11-02 + elecstate::DensityMatrix *dm_in, // mohan add 2025-11-02 #endif elecstate::ElecState* pelec_in, ModulePW::PW_Basis_K* pw_wfc_in = nullptr); @@ -122,7 +122,7 @@ class SpinConstrain elecstate::ElecState* pelec = nullptr; ModulePW::PW_Basis_K* pw_wfc_ = nullptr; #ifdef __LCAO - elecstate::DensityMatrix, double>* dm_; + elecstate::DensityMatrix* dm_; #endif double tpiba = 0.0; /// save ucell.tpiba const double meV_to_Ry = 7.349864435130999e-05; @@ -255,20 +255,20 @@ class SpinConstrain public: /// @brief save operator for spin-constrained DFT /// @param op_in the base pointer of operator, actual type should be DeltaSpin>* - void set_operator(hamilt::Operator* op_in); + void set_operator(hamilt::Operator* op_in); /// @brief set is_Mi_converged void set_mag_converged(bool is_Mi_converged_in){this->is_Mi_converged = is_Mi_converged_in;} /// @brief get is_Mi_converged bool mag_converged() const {return this->is_Mi_converged;} private: /// operator for spin-constrained DFT, used for calculating current atomic magnetic moment - hamilt::Operator* p_operator = nullptr; + hamilt::Operator* p_operator = nullptr; /// @brief if atomic magnetic moment is converged bool is_Mi_converged = false; - FPTYPE* sub_h_save; - FPTYPE* sub_s_save; - FPTYPE* becp_save; + TK* sub_h_save; + TK* sub_s_save; + TK* becp_save; }; From ea848fe021be4217836d4132f3af54548037aa02 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 22:07:20 +0800 Subject: [PATCH 52/81] now all functions can be compiled --- source/source_esolver/esolver_ks_lcao.cpp | 7 +++---- source/source_esolver/lcao_others.cpp | 1 + source/source_io/ctrl_iter_lcao.cpp | 4 ++-- source/source_io/ctrl_iter_lcao.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 5d06ff0b78..2bb9fbed20 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -522,7 +522,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { if (GlobalC::dftu.omc != 2) { - ModuleDFTU::dftu_cal_occup_m(iter, ucell, *dm_vec, this->kv, + ModuleDFTU::dftu_cal_occup_m(iter, ucell, dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), hamilt_lcao); } GlobalC::dftu.cal_energy_correction(ucell, istep); @@ -552,8 +552,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 = this->dmat.get_dm(); - this->p_chgmix->mix_dmr(dm); + this->p_chgmix->mix_dmr(this->dmat.get_dm()); } } @@ -564,7 +563,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // control the output related to the finished iteration - ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, estate, + ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, estate, *this->dmat.get_dm(), this->pv, this->gd, this->psi, this->chr, this->p_chgmix, hamilt_lcao, this->orb_, this->deepks, this->exx_nao, iter, istep, conv_esolver, this->scf_ene_thr); diff --git a/source/source_esolver/lcao_others.cpp b/source/source_esolver/lcao_others.cpp index 2ee2a020f6..155d62710a 100644 --- a/source/source_esolver/lcao_others.cpp +++ b/source/source_esolver/lcao_others.cpp @@ -207,6 +207,7 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) this->kv, this->p_hamilt, this->psi, + this->dmat.get_dm(), this->pelec); } diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index 7bc9299438..fecfe93197 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -14,7 +14,7 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO* pelec, // electronic info * - elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 + elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi* psi, // wave functions * @@ -123,7 +123,7 @@ template void ctrl_iter_lcao, std::complex>(UnitCel const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO>* pelec, // electronic info * - elecstate::DensityMatrix, std::complex>& dm, // density matrix, mohan add 2025-11-03 + elecstate::DensityMatrix, double>& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi>* psi, // wave functions * diff --git a/source/source_io/ctrl_iter_lcao.h b/source/source_io/ctrl_iter_lcao.h index 7fa33f2f96..05c02d10b5 100644 --- a/source/source_io/ctrl_iter_lcao.h +++ b/source/source_io/ctrl_iter_lcao.h @@ -19,7 +19,7 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * const Input_para& inp, // input parameters * K_Vectors& kv, // k points * elecstate::ElecStateLCAO* pelec, // electronic info * - elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 + elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * psi::Psi* psi, // wave functions * From b53865f7653513e9253877cc0741f4def8b513ef Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 22:19:32 +0800 Subject: [PATCH 53/81] fix it --- source/source_esolver/esolver_ks_lcao.cpp | 2 +- source/source_io/ctrl_runner_lcao.cpp | 7 ++++++- source/source_io/ctrl_runner_lcao.h | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 2bb9fbed20..9c1a48a35e 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -333,7 +333,7 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) } ModuleIO::ctrl_runner_lcao(ucell, - PARAM.inp, this->kv, estate, this->pv, this->Pgrid, + PARAM.inp, this->kv, estate, this->dmat, this->pv, this->Pgrid, this->gd, this->psi, this->chr, hamilt_lcao, this->two_center_bundle_, this->orb_, this->pw_rho, this->pw_rhod, diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index 9cf550830c..7db992dce8 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -17,6 +17,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info + const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms @@ -44,7 +45,8 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell // 2) out ldos if (inp.out_ldos[0]) { - ModuleIO::Cal_ldos::cal_ldos_lcao(pelec, psi[0], pgrid, ucell); + ModuleIO::Cal_ldos::cal_ldos_lcao(pelec->eferm, chr, dmat, kv, + pelec->ekb, pelec->wg, psi[0], pgrid, ucell); } // 3) print out exchange-correlation potential @@ -135,6 +137,7 @@ template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, / const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info + const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms @@ -155,6 +158,7 @@ template void ctrl_runner_lcao, double>(UnitCell& ucell, const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO>* pelec,// electronic info + const LCAO_DOMAIN::Setup_DM> &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms @@ -175,6 +179,7 @@ template void ctrl_runner_lcao, std::complex>(UnitC const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO>* pelec,// electronic info + const LCAO_DOMAIN::Setup_DM> &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms diff --git a/source/source_io/ctrl_runner_lcao.h b/source/source_io/ctrl_runner_lcao.h index 2b57c1800f..9d41498399 100644 --- a/source/source_io/ctrl_runner_lcao.h +++ b/source/source_io/ctrl_runner_lcao.h @@ -8,6 +8,7 @@ #include "source_lcao/hamilt_lcao.h" // use hamilt::HamiltLCAO #include "source_basis/module_nao/two_center_bundle.h" // use TwoCenterBundle #include "source_lcao/setup_exx.h" // for exx, mohan add 20251018 +#include "source_lcao/setup_dm.h" // for density matrix, mohan add 20251103 namespace ModuleIO { @@ -17,6 +18,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info + const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms From 236b8a8ad1e504ba4baaec798844e9a2e7d31da1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 22:36:31 +0800 Subject: [PATCH 54/81] update again --- source/source_esolver/esolver_double_xc.cpp | 2 +- source/source_esolver/esolver_double_xc.h | 2 +- source/source_esolver/esolver_ks_lcao.cpp | 2 +- source/source_esolver/esolver_ks_lcao.h | 2 +- source/source_estate/module_dm/cal_edm_tddft.cpp | 2 +- source/source_estate/module_dm/cal_edm_tddft.h | 2 +- source/source_estate/module_dm/init_dm.cpp | 6 +++--- source/source_estate/module_dm/init_dm.h | 2 +- source/source_io/cal_ldos.cpp | 2 +- source/source_io/cal_ldos.h | 2 +- source/source_io/ctrl_runner_lcao.cpp | 8 ++++---- source/source_io/ctrl_runner_lcao.h | 2 +- source/source_lcao/setup_dm.cpp | 4 ++-- source/source_lcao/setup_dm.h | 4 ++-- source/source_pw/module_pwdft/setup_pot.cpp | 5 ++++- 15 files changed, 25 insertions(+), 22 deletions(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index cb328c278a..48c43df740 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -84,7 +84,7 @@ void ESolver_DoubleXC::before_all_runners(UnitCell& ucell, const Input_p } // 6) initialize the density matrix - this->dmat_base.allocate_dm(&this->kv, &(this->pv), PARAM.inp.nspin); + this->dmat_base.allocate_dm(&this->kv, &this->pv, PARAM.inp.nspin); // 10) inititlize the charge density this->chr_base.allocate(PARAM.inp.nspin); diff --git a/source/source_esolver/esolver_double_xc.h b/source/source_esolver/esolver_double_xc.h index 8e6d85e4b7..bff9e28bc7 100644 --- a/source/source_esolver/esolver_double_xc.h +++ b/source/source_esolver/esolver_double_xc.h @@ -33,7 +33,7 @@ class ESolver_DoubleXC : public ESolver_KS_LCAO elecstate::ElecState* pelec_base = nullptr; //! Density Matrix, mohan add 2025-11-03 - LCAO_DOMAIN::Setup_DM dmat_base; + LCAO_domain::Setup_DM dmat_base; //! Electorn charge density Charge chr_base; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 9c1a48a35e..29c2375aab 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -91,7 +91,7 @@ void ESolver_KS_LCAO::before_all_runners(UnitCell& ucell, const Input_pa // 7) init DMK, but DMR is constructed in before_scf() - this->dmat.allocate_dm(&this->kv, &(this->pv), inp.nspin); + this->dmat.allocate_dm(&this->kv, &this->pv, inp.nspin); // 8) init exact exchange calculations this->exx_nao.before_runner(ucell, this->kv, this->orb_, this->pv, inp); diff --git a/source/source_esolver/esolver_ks_lcao.h b/source/source_esolver/esolver_ks_lcao.h index 3cc9a7ae27..2bcf0c672c 100644 --- a/source/source_esolver/esolver_ks_lcao.h +++ b/source/source_esolver/esolver_ks_lcao.h @@ -73,7 +73,7 @@ class ESolver_KS_LCAO : public ESolver_KS TwoCenterBundle two_center_bundle_; //! Add density matrix class, mohan add 2025-10-30 - LCAO_DOMAIN::Setup_DM dmat; + LCAO_domain::Setup_DM dmat; //! For RDMFT calculations, added by jghan, 2024-03-16 rdmft::RDMFT rdmft_solver; diff --git a/source/source_estate/module_dm/cal_edm_tddft.cpp b/source/source_estate/module_dm/cal_edm_tddft.cpp index 333f916706..419261a158 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.cpp +++ b/source/source_estate/module_dm/cal_edm_tddft.cpp @@ -8,7 +8,7 @@ namespace elecstate { // use the original formula (Hamiltonian matrix) to calculate energy density matrix void cal_edm_tddft(Parallel_Orbitals& pv, - LCAO_DOMAIN::Setup_DM> &dmat, + LCAO_domain::Setup_DM> &dmat, K_Vectors& kv, hamilt::Hamilt>* p_hamilt) { diff --git a/source/source_estate/module_dm/cal_edm_tddft.h b/source/source_estate/module_dm/cal_edm_tddft.h index f107509a95..5ceec1bdaa 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.h +++ b/source/source_estate/module_dm/cal_edm_tddft.h @@ -9,7 +9,7 @@ namespace elecstate { void cal_edm_tddft(Parallel_Orbitals& pv, - LCAO_DOMAIN::Setup_DM> &dmat, + LCAO_domain::Setup_DM> &dmat, K_Vectors& kv, hamilt::Hamilt>* p_hamilt); } // namespace elecstate diff --git a/source/source_estate/module_dm/init_dm.cpp b/source/source_estate/module_dm/init_dm.cpp index 94dd441697..9307776f53 100644 --- a/source/source_estate/module_dm/init_dm.cpp +++ b/source/source_estate/module_dm/init_dm.cpp @@ -6,7 +6,7 @@ template void elecstate::init_dm(UnitCell& ucell, elecstate::ElecStateLCAO* pelec, - LCAO_DOMAIN::Setup_DM &dmat, + LCAO_domain::Setup_DM &dmat, psi::Psi* psi, Charge &chr, const int iter, @@ -52,7 +52,7 @@ void elecstate::init_dm(UnitCell& ucell, template void elecstate::init_dm(UnitCell& ucell, elecstate::ElecStateLCAO* pelec, - LCAO_DOMAIN::Setup_DM &dmat, + LCAO_domain::Setup_DM &dmat, psi::Psi* psi, Charge &chr, const int iter, @@ -60,7 +60,7 @@ template void elecstate::init_dm(UnitCell& ucell, template void elecstate::init_dm>(UnitCell& ucell, elecstate::ElecStateLCAO>* pelec, - LCAO_DOMAIN::Setup_DM> &dmat, + LCAO_domain::Setup_DM> &dmat, psi::Psi>* psi, Charge &chr, const int iter, diff --git a/source/source_estate/module_dm/init_dm.h b/source/source_estate/module_dm/init_dm.h index 4470c897b8..75bc7aeeed 100644 --- a/source/source_estate/module_dm/init_dm.h +++ b/source/source_estate/module_dm/init_dm.h @@ -13,7 +13,7 @@ namespace elecstate template void init_dm(UnitCell& ucell, ElecStateLCAO* pelec, - LCAO_DOMAIN::Setup_DM &dmat, + LCAO_domain::Setup_DM &dmat, psi::Psi* psi, Charge &chr, const int iter, diff --git a/source/source_io/cal_ldos.cpp b/source/source_io/cal_ldos.cpp index f6f0cf3fd6..7339037810 100644 --- a/source/source_io/cal_ldos.cpp +++ b/source/source_io/cal_ldos.cpp @@ -15,7 +15,7 @@ template void Cal_ldos::cal_ldos_lcao( const elecstate::Efermi &eferm, // mohan add 2025-11-02 const Charge &chr, // mohan add add 2025-11-02 - const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 const K_Vectors &kv, // k points, mohan add 2025-11-02 const ModuleBase::matrix &ekb, // mohan add 2025-11-02 const ModuleBase::matrix &wg, // mohan add 2025-11-02 diff --git a/source/source_io/cal_ldos.h b/source/source_io/cal_ldos.h index 0aa59871c2..8a16046b76 100644 --- a/source/source_io/cal_ldos.h +++ b/source/source_io/cal_ldos.h @@ -22,7 +22,7 @@ class Cal_ldos static void cal_ldos_lcao( const elecstate::Efermi &eferm, // mohan add 2025-11-02 const Charge &chr, // mohan add add 2025-11-02 - const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 const K_Vectors &kv, // k points, mohan add 2025-11-02 const ModuleBase::matrix &ekb, // mohan add 2025-11-02 const ModuleBase::matrix &wg, // mohan add 2025-11-02 diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index 7db992dce8..ab5c4e1280 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -17,7 +17,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info - const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms @@ -137,7 +137,7 @@ template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, / const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info - const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms @@ -158,7 +158,7 @@ template void ctrl_runner_lcao, double>(UnitCell& ucell, const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO>* pelec,// electronic info - const LCAO_DOMAIN::Setup_DM> &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM> &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms @@ -179,7 +179,7 @@ template void ctrl_runner_lcao, std::complex>(UnitC const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO>* pelec,// electronic info - const LCAO_DOMAIN::Setup_DM> &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM> &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms diff --git a/source/source_io/ctrl_runner_lcao.h b/source/source_io/ctrl_runner_lcao.h index 9d41498399..0ea631080f 100644 --- a/source/source_io/ctrl_runner_lcao.h +++ b/source/source_io/ctrl_runner_lcao.h @@ -18,7 +18,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point elecstate::ElecStateLCAO* pelec,// electronic info - const LCAO_DOMAIN::Setup_DM &dmat, // mohan add 2025-11-02 + const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info Grid_Driver &gd, // search for adjacent atoms diff --git a/source/source_lcao/setup_dm.cpp b/source/source_lcao/setup_dm.cpp index 8724bd10fc..cc993dab0c 100644 --- a/source/source_lcao/setup_dm.cpp +++ b/source/source_lcao/setup_dm.cpp @@ -17,10 +17,10 @@ namespace LCAO_domain // change init_dm to allocate_dm, mohan 2025-10-31 template -void Setup_DM::allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin) +void Setup_DM::allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* pv, const int nspin) { const int nspin_dm = nspin == 2 ? 2 : 1; - this->dm = new elecstate::DensityMatrix(paraV, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); + this->dm = new elecstate::DensityMatrix(pv, nspin_dm, kv->kvec_d, kv->get_nks() / nspin_dm); } template class Setup_DM; // Gamma_only case diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h index 58d7808f53..0817039227 100644 --- a/source/source_lcao/setup_dm.h +++ b/source/source_lcao/setup_dm.h @@ -5,7 +5,7 @@ #include -namespace LCAO_DOMAIN +namespace LCAO_domain { template class Setup_DM @@ -25,7 +25,7 @@ class Setup_DM } // allocate density matrix - void allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* paraV, const int nspin); + void allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* pv, const int nspin); elecstate::DensityMatrix* get_dm() const { diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index 1eef712283..fb20b547e8 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -102,11 +102,14 @@ void pw::setup_pot(const int istep, PARAM.inp.sccut, PARAM.inp.sc_drop_thr, ucell, - nullptr, + nullptr, // parallel orbitals PARAM.inp.nspin, kv, p_hamilt, kspw_psi, +#ifdef __LCAO + nullptr, // density matrix, not useful in LCAO, mohan note 2025-11-03 +#endif pelec, pw_wfc); } From b6bedf5e14280ee7c934a04c55bd11d4ee5d626a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 22:40:25 +0800 Subject: [PATCH 55/81] add and compile setup_dm --- source/Makefile.Objects | 1 + source/source_lcao/CMakeLists.txt | 1 + source/source_lcao/setup_dm.cpp | 2 +- source/source_lcao/setup_dm.h | 2 ++ 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index ceeedd0f15..5e785d1ed6 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -642,6 +642,7 @@ OBJS_LCAO=evolve_elec.o\ LCAO_init_basis.o\ setup_exx.o\ setup_deepks.o\ + setup_dm.o\ rho_tau_lcao.o\ center2_orb.o\ center2_orb-orb11.o\ diff --git a/source/source_lcao/CMakeLists.txt b/source/source_lcao/CMakeLists.txt index dbd2113bfb..5199625c41 100644 --- a/source/source_lcao/CMakeLists.txt +++ b/source/source_lcao/CMakeLists.txt @@ -42,6 +42,7 @@ if(ENABLE_LCAO) LCAO_init_basis.cpp setup_exx.cpp setup_deepks.cpp + setup_dm.cpp rho_tau_lcao.cpp record_adj.cpp center2_orb.cpp diff --git a/source/source_lcao/setup_dm.cpp b/source/source_lcao/setup_dm.cpp index cc993dab0c..a91b43ab96 100644 --- a/source/source_lcao/setup_dm.cpp +++ b/source/source_lcao/setup_dm.cpp @@ -1,6 +1,6 @@ #include "source_lcao/setup_dm.h" -#include "cal_dm.h" +#include "source_estate/cal_dm.h" #include "source_base/timer.h" #include "source_estate/module_dm/cal_dm_psi.h" #include "source_hamilt/module_xc/xc_functional.h" diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h index 0817039227..7c7d7eb203 100644 --- a/source/source_lcao/setup_dm.h +++ b/source/source_lcao/setup_dm.h @@ -1,6 +1,8 @@ #ifndef SETUP_DM_H #define SETUP_DM_H +#include "source_cell/klist.h" +#include "source_basis/module_ao/parallel_orbitals.h" #include "source_estate/module_dm/density_matrix.h" #include From 2abcc9a806b60bf73b611b1c546b8d049856f129 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 3 Nov 2025 23:10:06 +0800 Subject: [PATCH 56/81] update fix bugs --- source/source_esolver/esolver_double_xc.cpp | 1 + source/source_esolver/esolver_ks_lcao.cpp | 2 +- source/source_lcao/FORCE_STRESS.cpp | 21 +++++++++++++++---- source/source_lcao/FORCE_STRESS.h | 2 ++ source/source_lcao/module_dftu/dftu.h | 4 ++-- source/source_lcao/module_dftu/dftu_force.cpp | 8 +++---- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index 48c43df740..e523ad41ef 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -385,6 +385,7 @@ void ESolver_DoubleXC::cal_force(UnitCell& ucell, ModuleBase::matrix& fo this->gd, this->pv, this->pelec_base, + this->dmat_base, // mohan add 2025-11-03 this->psi, this->two_center_bundle_, this->orb_, diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 29c2375aab..7518a2f7fa 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -274,7 +274,7 @@ void ESolver_KS_LCAO::cal_force(UnitCell& ucell, ModuleBase::matrix& for fsl.getForceStress(ucell, PARAM.inp.cal_force, PARAM.inp.cal_stress, PARAM.inp.test_force, PARAM.inp.test_stress, - this->gd, this->pv, this->pelec, this->psi, + this->gd, this->pv, this->pelec, this->dmat, this->psi, two_center_bundle_, orb_, force, this->scs, this->locpp, this->sf, this->kv, this->pw_rho, this->solvent, this->deepks, diff --git a/source/source_lcao/FORCE_STRESS.cpp b/source/source_lcao/FORCE_STRESS.cpp index af51e04ab2..21864a0243 100644 --- a/source/source_lcao/FORCE_STRESS.cpp +++ b/source/source_lcao/FORCE_STRESS.cpp @@ -39,6 +39,7 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, const Grid_Driver& gd, Parallel_Orbitals& pv, const elecstate::ElecState* pelec, + LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-03 const psi::Psi* psi, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, @@ -144,7 +145,7 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, hamilt::NonlocalNew> tmp_nonlocal(nullptr, kv.kvec_d, nullptr, &ucell, orb.cutoffs(), &gd, two_center_bundle.overlap_orb_beta.get()); - const auto* dm_p = dynamic_cast*>(pelec)->get_DM(); + const auto* dm_p = dmat.get_dm(); if (PARAM.inp.nspin == 2) { @@ -166,7 +167,7 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, // calculate temporary complex DMR for nonlocal force&stress // In fact, only SOC part need the imaginary part of DMR for correct force&stress - const auto* dm_p = dynamic_cast>*>(pelec)->get_DM(); + const auto* dm_p = dmat.get_dm(); hamilt::HContainer> tmp_dmr(dm_p->get_DMR_pointer(1)->get_paraV()); std::vector ijrs = dm_p->get_DMR_pointer(1)->get_ijr_info(); tmp_dmr.insert_ijrs(&ijrs); @@ -248,7 +249,19 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, } if (PARAM.inp.dft_plus_u == 2) { - GlobalC::dftu.force_stress(ucell, gd, pelec, pv, fsr, force_dftu, stress_dftu, kv); + // GlobalC::dftu.force_stress(ucell, gd, pelec, pv, fsr, force_dftu, stress_dftu, kv); + // mohan modify 2025-11-03 + auto dm_pointer = dmat.get_dm(); + if(PARAM.globalv.gamma_only_local) + { + // T is double + GlobalC::dftu.force_stress(ucell, gd, dm_pointer, nullptr, pv, fsr, force_dftu, stress_dftu, kv); + } + else + { + // T is complex + GlobalC::dftu.force_stress(ucell, gd, nullptr, dm_pointer, pv, fsr, force_dftu, stress_dftu, kv); + } } else { @@ -287,7 +300,7 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, two_center_bundle.overlap_orb_onsite.get(), orb.cutoffs()); - const auto* dm_p = dynamic_cast>*>(pelec)->get_DM(); + const auto* dm_p = dmat.get_dm(); if (PARAM.inp.nspin == 2) { const_cast, double>*>(dm_p)->switch_dmr(2); diff --git a/source/source_lcao/FORCE_STRESS.h b/source/source_lcao/FORCE_STRESS.h index dd7d464b94..be5cec951d 100644 --- a/source/source_lcao/FORCE_STRESS.h +++ b/source/source_lcao/FORCE_STRESS.h @@ -16,6 +16,7 @@ #include "force_stress_arrays.h" #include "source_lcao/setup_exx.h" // for exx, mohan add 20251008 #include "source_lcao/setup_deepks.h" // for deepks, mohan add 20251010 +#include "source_lcao/setup_dm.h" // mohan add 2025-11-03 template @@ -38,6 +39,7 @@ class Force_Stress_LCAO const Grid_Driver& gd, Parallel_Orbitals& pv, const elecstate::ElecState* pelec, + LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-03 const psi::Psi* psi, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, diff --git a/source/source_lcao/module_dftu/dftu.h b/source/source_lcao/module_dftu/dftu.h index 899d73a5f1..457debc8bd 100644 --- a/source/source_lcao/module_dftu/dftu.h +++ b/source/source_lcao/module_dftu/dftu.h @@ -200,8 +200,8 @@ class DFTU public: void force_stress(const UnitCell& ucell, const Grid_Driver& gd, - const std::vector>& dmk_d, // mohan modify 2025-11-02 - const std::vector>>& dmk_c, // dmat.get_dm()->get_DMK_vector(); + std::vector>* dmk_d, // mohan modify 2025-11-02 + std::vector>>* dmk_c, // dmat.get_dm()->get_DMK_vector(); const Parallel_Orbitals& pv, ForceStressArrays& fsr, ModuleBase::matrix& force_dftu, diff --git a/source/source_lcao/module_dftu/dftu_force.cpp b/source/source_lcao/module_dftu/dftu_force.cpp index 1d33761cf4..5d76611470 100644 --- a/source/source_lcao/module_dftu/dftu_force.cpp +++ b/source/source_lcao/module_dftu/dftu_force.cpp @@ -72,8 +72,8 @@ namespace ModuleDFTU void DFTU::force_stress(const UnitCell& ucell, const Grid_Driver& gd, - const std::vector>& dmk_d, // mohan modify 2025-11-02 - const std::vector>>& dmk_c, // dmat.get_dm()->get_DMK_vector(); + std::vector>* dmk_d, // mohan modify 2025-11-02 + std::vector>>* dmk_c, // dmat.get_dm()->get_DMK_vector(); const Parallel_Orbitals& pv, ForceStressArrays& fsr, // mohan add 2024-06-16 ModuleBase::matrix& force_dftu, @@ -115,7 +115,7 @@ void DFTU::force_stress(const UnitCell& ucell, #ifdef __MPI pdgemm_(&transT, &transN, &nlocal, &nlocal, &nlocal, - &alpha, dmk_d[spin].data(), &one_int, &one_int, + &alpha, (*dmk_d)[spin].data(), &one_int, &one_int, // important to add () outside *dmk_d, mohan note 20251103 pv.desc, VU, &one_int, &one_int, pv.desc, &beta, &rho_VU[0], &one_int, &one_int, pv.desc); @@ -163,7 +163,7 @@ void DFTU::force_stress(const UnitCell& ucell, #ifdef __MPI pzgemm_(&transT, &transN, &nlocal, &nlocal, &nlocal, - &alpha, dmk_c[ik].data(), &one_int, &one_int, + &alpha, (*dmk_c)[ik].data(), &one_int, &one_int, // important to add (), 20251103 pv.desc, VU, &one_int, &one_int, pv.desc, &beta, &rho_VU[0], &one_int, &one_int, pv.desc); #endif From a9b11040654ed66932c94a62bb39b16161780d2a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 09:26:22 +0800 Subject: [PATCH 57/81] simplify dm --- source/source_esolver/esolver_dm2rho.cpp | 7 +- source/source_esolver/esolver_double_xc.cpp | 12 +- source/source_esolver/esolver_ks_lcao.cpp | 38 +++--- .../source_esolver/esolver_ks_lcao_tddft.cpp | 16 +-- source/source_esolver/lcao_others.cpp | 4 +- .../source_estate/module_dm/cal_edm_tddft.cpp | 6 +- source/source_estate/module_dm/init_dm.cpp | 4 +- source/source_io/cal_ldos.cpp | 6 +- source/source_io/ctrl_scf_lcao.cpp | 14 +- source/source_io/ctrl_scf_lcao.h | 2 + source/source_lcao/FORCE.h | 5 +- source/source_lcao/FORCE_STRESS.cpp | 127 +++++++++--------- source/source_lcao/FORCE_STRESS.h | 5 +- source/source_lcao/FORCE_gamma.cpp | 11 +- source/source_lcao/FORCE_k.cpp | 48 ++++--- .../module_deltaspin/.spin_constrain.h.swp | Bin 0 -> 16384 bytes .../source_lcao/module_deltaspin/cal_mw.cpp | 12 +- source/source_lcao/setup_dm.h | 7 - 18 files changed, 155 insertions(+), 169 deletions(-) create mode 100644 source/source_lcao/module_deltaspin/.spin_constrain.h.swp diff --git a/source/source_esolver/esolver_dm2rho.cpp b/source/source_esolver/esolver_dm2rho.cpp index 5f03717c68..3f1b482dcb 100644 --- a/source/source_esolver/esolver_dm2rho.cpp +++ b/source/source_esolver/esolver_dm2rho.cpp @@ -48,21 +48,20 @@ void ESolver_DM2rho::runner(UnitCell& ucell, const int istep) // file name of DM std::string zipname = "output_DM0.npz"; - elecstate::DensityMatrix* dm = this->dmat.get_dm(); // read DM from file - ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(dm->get_DMR_pointer(1))); + ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(this->dmat.dm.get_DMR_pointer(1))); // if nspin=2, need extra reading if (PARAM.inp.nspin == 2) { zipname = "output_DM1.npz"; - ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(dm->get_DMR_pointer(2))); + ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(this->dmat.dm.get_DMR_pointer(2))); } // it's dangerous to design psiToRho function like this, mohan note 20251024 // this->pelec->psiToRho(*this->psi); - LCAO_domain::dm2rho(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, &this->chr); + LCAO_domain::dm2rho(this->dmat.dm.get_DMR_vector(), PARAM.inp.nspin, &this->chr); int nspin0 = PARAM.inp.nspin == 2 ? 2 : 1; diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index e523ad41ef..7ae8dee5dc 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -146,7 +146,7 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) this->kv, this->two_center_bundle_, this->orb_, - this->dmat_base.get_dm(), + &this->dmat_base.dm, this->deepks, istep, this->exx_nao); @@ -157,11 +157,11 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); // DMR should be same size with Hamiltonian(R) - this->dmat_base.get_dm()->init_DMR(*(dynamic_cast*>(this->p_hamilt_base)->getHR())); + this->dmat_base.dm.init_DMR(*(dynamic_cast*>(this->p_hamilt_base)->getHR())); if (istep > 0) { - this->dmat_base.get_dm()->cal_DMR(); + this->dmat_base.dm.cal_DMR(); } ModuleBase::timer::tick("ESolver_DoubleXC", "before_scf"); @@ -233,7 +233,7 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int this->gd, &(this->pv), *(this->psi), - this->dmat_base.get_dm(), + &this->dmat_base.dm, p_ham_deepks, iter, conv_esolver, @@ -349,10 +349,10 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int for (int ik = 0; ik < nks; ik++) { // mohan update 2025-11-03 - this->dmat_base.get_dm()->set_DMK_pointer(ik, this->dmat.get_dm()->get_DMK_pointer(ik)); + this->dmat_base.dm.set_DMK_pointer(ik, this->dmat.dm.get_DMK_pointer(ik)); // _pes_lcao_base->get_DM()->set_DMK_pointer(ik, _pes_lcao->get_DM()->get_DMK_pointer(ik)); } - this->dmat_base.get_dm()->cal_DMR(); + this->dmat_base.dm.cal_DMR(); // _pes_lcao_base->get_DM()->cal_DMR(); _pes_lcao_base->ekb = _pes_lcao->ekb; _pes_lcao_base->wg = _pes_lcao->wg; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 7518a2f7fa..464d1d0856 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -189,7 +189,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) { this->p_hamilt = new hamilt::HamiltLCAO( ucell, this->gd, &this->pv, this->pelec->pot, this->kv, - two_center_bundle_, orb_, this->dmat.get_dm(), this->deepks, istep, exx_nao); + two_center_bundle_, orb_, this->dmat.dm, this->deepks, istep, exx_nao); } // 9) for each ionic step, the overlap must be rebuilt @@ -202,7 +202,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) 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, &(this->pv), - PARAM.inp.nspin, this->kv, this->p_hamilt, this->psi, this->dmat.get_dm(), this->pelec); + PARAM.inp.nspin, this->kv, this->p_hamilt, this->psi, this->dmat.dm, this->pelec); } // 11) set xc type before the first cal of xc in pelec->init_scf, Peize Lin add 2016-12-03 @@ -218,7 +218,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) { ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::before_scf","p_hamilt does not exist"); } - this->dmat.get_dm()->init_DMR(*hamilt_lcao->getHR()); + this->dmat.dm->init_DMR(*hamilt_lcao->getHR()); #ifdef __MLALGO // 14) initialize DMR of DeePKS @@ -230,7 +230,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) // 2. DMK in DensityMatrix is empty (istep == 0), then DMR is initialized by zeros if (istep > 0) { - this->dmat.get_dm()->cal_DMR(); + this->dmat.dm->cal_DMR(); } // 16) the electron charge density should be symmetrized, @@ -357,9 +357,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_init","pelec does not exist"); } - elecstate::DensityMatrix* dm = this->dmat.get_dm(); - - module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp); + module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, this->dmat.dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp); // mohan update 2012-06-05 estate->f_en.deband_harris = estate->cal_delta_eband(ucell); @@ -385,11 +383,11 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { if (GlobalC::exx_info.info_ri.real_number) { - this->exx_nao.exd->exx_eachiterinit(istep, ucell, *dm, this->kv, iter); + this->exx_nao.exd->exx_eachiterinit(istep, ucell, this->dmat.dm, this->kv, iter); } else { - this->exx_nao.exc->exx_eachiterinit(istep, ucell, *dm, this->kv, iter); + this->exx_nao.exc->exx_eachiterinit(istep, ucell, this->dmat.dm, this->kv, iter); } } #endif @@ -398,7 +396,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { if (istep != 0 || iter != 1) { - GlobalC::dftu.set_dmr(dm); + GlobalC::dftu.set_dmr(this->dmat.dm); } // Calculate U and J if Yukawa potential is used GlobalC::dftu.cal_slater_UJ(ucell, this->chr.rho, this->pw_rho->nrxx); @@ -424,7 +422,7 @@ 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) { - dm->save_DMR(); + this->dmat.dm->save_DMR(); } } @@ -462,7 +460,7 @@ void ESolver_KS_LCAO::hamilt2rho_single(UnitCell& ucell, int istep, int if (!skip_solve) { hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), PARAM.inp.ks_solver); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.get_dm(), + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.dm, this->chr, PARAM.inp.nspin, skip_charge); } @@ -511,7 +509,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_finish","p_hamilt does not exist"); } - const std::vector>& dm_vec = this->dmat.get_dm()->get_DMK_vector(); + const std::vector>& dm_vec = this->dmat.dm->get_DMK_vector(); // 1) calculate the local occupation number matrix and energy correction in DFT+U if (PARAM.inp.dft_plus_u) @@ -552,7 +550,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) { - this->p_chgmix->mix_dmr(this->dmat.get_dm()); + this->p_chgmix->mix_dmr(this->dmat.dm); } } @@ -563,7 +561,7 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // control the output related to the finished iteration - ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, estate, *this->dmat.get_dm(), + ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, estate, *this->dmat.dm, this->pv, this->gd, this->psi, this->chr, this->p_chgmix, hamilt_lcao, this->orb_, this->deepks, this->exx_nao, iter, istep, conv_esolver, this->scf_ene_thr); @@ -576,14 +574,8 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); - auto* estate = dynamic_cast*>(this->pelec); auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); - if(!estate) - { - 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"); @@ -591,7 +583,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const if (PARAM.inp.out_elf[0] > 0) { - LCAO_domain::dm2tau(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, this->pelec->charge); + LCAO_domain::dm2tau(this->dmat.dm->get_DMR_vector(), PARAM.inp.nspin, this->pelec->charge); } //! 1) call after_scf() of ESolver_KS @@ -600,7 +592,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const //! 2) output of lcao every few ionic steps ModuleIO::ctrl_scf_lcao(ucell, - PARAM.inp, this->kv, estate, this->pv, + PARAM.inp, this->kv, this->pelec, this->dmat.dm, this->pv, this->gd, this->psi, hamilt_lcao, this->two_center_bundle_, this->orb_, this->pw_wfc, this->pw_rho, diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index 86457a328b..995ec35b80 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -292,7 +292,7 @@ void ESolver_KS_LCAO_TDDFT::hamilt2rho_single(UnitCell& ucell, { bool skip_charge = PARAM.inp.calculation == "nscf" ? true : false; hsolver::HSolverLCAO> hsolver_lcao_obj(&this->pv, PARAM.inp.ks_solver); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.get_dm(), + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, *this->dmat.dm, this->chr, PARAM.inp.nspin, skip_charge); } } @@ -497,8 +497,6 @@ void ESolver_KS_LCAO_TDDFT::after_scf(UnitCell& ucell, const int ist ss_dipole.str()); } } - elecstate::DensityMatrix, double>* tmp_DM - = this->dmat.get_dm(); // (2) write current information if(TD_info::out_current) @@ -511,7 +509,7 @@ void ESolver_KS_LCAO_TDDFT::after_scf(UnitCell& ucell, const int ist this->pelec, this->kv, this->two_center_bundle_.overlap_orb.get(), - tmp_DM->get_paraV_pointer(), + this->dmat.dm->get_paraV_pointer(), this->orb_, this->velocity_mat, this->RA); @@ -524,7 +522,7 @@ void ESolver_KS_LCAO_TDDFT::after_scf(UnitCell& ucell, const int ist this->pelec, this->kv, this->two_center_bundle_.overlap_orb.get(), - tmp_DM->get_paraV_pointer(), + this->dmat.dm->get_paraV_pointer(), this->orb_, this->velocity_mat, this->RA); @@ -564,18 +562,18 @@ void ESolver_KS_LCAO_TDDFT::weight_dm_rho(const UnitCell& ucell) // calculate the density matrix ModuleBase::GlobalFunc::NOTE("Calculate the density matrix."); - elecstate::cal_dm_psi(this->dmat.get_dm()->get_paraV_pointer(), this->pelec->wg, this->psi[0], *this->dmat.get_dm()); + elecstate::cal_dm_psi(this->dmat.dm->get_paraV_pointer(), this->pelec->wg, this->psi[0], *this->dmat.dm); if(PARAM.inp.td_stype == 2) { - this->dmat.get_dm()->cal_DMR_td(ucell, TD_info::cart_At); + this->dmat.dm->cal_DMR_td(ucell, TD_info::cart_At); } else { - this->dmat.get_dm()->cal_DMR(); + this->dmat.dm->cal_DMR(); } // get the real-space charge density, mohan add 2025-10-24 - LCAO_domain::dm2rho(this->dmat.get_dm()->get_DMR_vector(), PARAM.inp.nspin, &this->chr); + LCAO_domain::dm2rho(this->dmat.dm->get_DMR_vector(), PARAM.inp.nspin, &this->chr); } template class ESolver_KS_LCAO_TDDFT; diff --git a/source/source_esolver/lcao_others.cpp b/source/source_esolver/lcao_others.cpp index 155d62710a..9c413836d4 100644 --- a/source/source_esolver/lcao_others.cpp +++ b/source/source_esolver/lcao_others.cpp @@ -181,7 +181,7 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) this->kv, two_center_bundle_, orb_, - this->dmat.get_dm(), + this->dmat.dm, this->deepks, istep, this->exx_nao); @@ -207,7 +207,7 @@ void ESolver_KS_LCAO::others(UnitCell& ucell, const int istep) this->kv, this->p_hamilt, this->psi, - this->dmat.get_dm(), + this->dmat.dm, this->pelec); } diff --git a/source/source_estate/module_dm/cal_edm_tddft.cpp b/source/source_estate/module_dm/cal_edm_tddft.cpp index 419261a158..809d76e1e4 100644 --- a/source/source_estate/module_dm/cal_edm_tddft.cpp +++ b/source/source_estate/module_dm/cal_edm_tddft.cpp @@ -16,13 +16,13 @@ void cal_edm_tddft(Parallel_Orbitals& pv, const int nlocal = PARAM.globalv.nlocal; assert(nlocal >= 0); - dmat.get_dm()->EDMK.resize(kv.get_nks()); + dmat.dm->EDMK.resize(kv.get_nks()); for (int ik = 0; ik < kv.get_nks(); ++ik) { p_hamilt->updateHk(ik); - std::complex* tmp_dmk = dmat.get_dm()->get_DMK_pointer(ik); - ModuleBase::ComplexMatrix& tmp_edmk = dmat.get_dm()->EDMK[ik]; + std::complex* tmp_dmk = dmat.dm->get_DMK_pointer(ik); + ModuleBase::ComplexMatrix& tmp_edmk = dmat.dm->EDMK[ik]; #ifdef __MPI diff --git a/source/source_estate/module_dm/init_dm.cpp b/source/source_estate/module_dm/init_dm.cpp index 9307776f53..0b5d6e86b6 100644 --- a/source/source_estate/module_dm/init_dm.cpp +++ b/source/source_estate/module_dm/init_dm.cpp @@ -31,8 +31,8 @@ void elecstate::init_dm(UnitCell& ucell, pelec->skip_weights); elecstate::calEBand(pelec->ekb, pelec->wg, pelec->f_en); - elecstate::cal_dm_psi(dmat.get_dm()->get_paraV_pointer(), pelec->wg, *psi, *dmat.get_dm()); - dmat.get_dm()->cal_DMR(); + elecstate::cal_dm_psi(dmat.dm->get_paraV_pointer(), pelec->wg, *psi, *dmat.dm); + dmat.dm->cal_DMR(); pelec->psiToRho(*psi); pelec->skip_weights = false; diff --git a/source/source_io/cal_ldos.cpp b/source/source_io/cal_ldos.cpp index 7339037810..009337c254 100644 --- a/source/source_io/cal_ldos.cpp +++ b/source/source_io/cal_ldos.cpp @@ -48,13 +48,13 @@ void Cal_ldos::cal_ldos_lcao( // calculate dm-like for ldos const int nspin_dm = PARAM.inp.nspin == 2 ? 2 : 1; - elecstate::DensityMatrix dm_ldos(dmat.get_dm()->get_paraV_pointer(), + elecstate::DensityMatrix dm_ldos(dmat.dm->get_paraV_pointer(), nspin_dm, kv.kvec_d, kv.get_nks() / nspin_dm); - elecstate::cal_dm_psi(dmat.get_dm()->get_paraV_pointer(), weight, psi, dm_ldos); - dm_ldos.init_DMR(*(dmat.get_dm()->get_DMR_pointer(1))); + elecstate::cal_dm_psi(dmat.dm->get_paraV_pointer(), weight, psi, dm_ldos); + dm_ldos.init_DMR(*(dmat.dm->get_DMR_pointer(1))); dm_ldos.cal_DMR(); // allocate ldos space diff --git a/source/source_io/ctrl_scf_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp index c859cf3f74..77344632d8 100644 --- a/source/source_io/ctrl_scf_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -37,6 +37,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, + elecstate::DensityMatrix* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, psi::Psi* psi, @@ -115,11 +116,9 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, //------------------------------------------------------------------ if(inp.out_dmr[0]) { - const auto& dmr_vector = pelec->get_DM()->get_DMR_vector(); - const int precision = inp.out_dmr[1]; - ModuleIO::write_dmr(dmr_vector, precision, pv, out_app_flag, + ModuleIO::write_dmr(dm->get_DMR_vector(), precision, pv, out_app_flag, ucell.get_iat2iwt(), ucell.nat, istep); } @@ -135,7 +134,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, } const int precision = inp.out_dmk[1]; - ModuleIO::write_dmk(pelec->get_DM()->get_DMK_vector(), + ModuleIO::write_dmk(dm->get_DMK_vector(), precision, efermis, &(ucell), pv, istep); } @@ -196,7 +195,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, gd, &pv, *psi, - pelec->get_DM(), + dm, p_ham_deepks, -1, // -1 when called in after scf true, // no used when after scf @@ -404,7 +403,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, if (inp.rpa) { RPA_LRI rpa_lri_double(GlobalC::exx_info.info_ri); - rpa_lri_double.cal_postSCF_exx(*dynamic_cast*>(pelec)->get_DM(), + rpa_lri_double.cal_postSCF_exx(*dm, MPI_COMM_WORLD, ucell, kv, @@ -470,6 +469,7 @@ template void ModuleIO::ctrl_scf_lcao( const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, + elecstate::DensityMatrix* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, psi::Psi* psi, @@ -493,6 +493,7 @@ template void ModuleIO::ctrl_scf_lcao, double>( const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO>* pelec, + elecstate::DensityMatrix,double>* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, psi::Psi>* psi, @@ -515,6 +516,7 @@ template void ModuleIO::ctrl_scf_lcao, std::complex const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO>* pelec, + elecstate::DensityMatrix,double>* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, psi::Psi>* psi, diff --git a/source/source_io/ctrl_scf_lcao.h b/source/source_io/ctrl_scf_lcao.h index 98ee5e18a5..9eac6600d4 100644 --- a/source/source_io/ctrl_scf_lcao.h +++ b/source/source_io/ctrl_scf_lcao.h @@ -15,6 +15,7 @@ #include "source_lcao/setup_deepks.h" // for deepks, mohan add 20251008 #include "source_lcao/setup_exx.h" // for exx, mohan add 20251008 +#include "source_estate/module_dm/density_matrix.h" // mohan add 2025-11-04 namespace ModuleIO { @@ -24,6 +25,7 @@ namespace ModuleIO const Input_para& inp, K_Vectors& kv, elecstate::ElecStateLCAO* pelec, + elecstate::DensityMatrix *dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, psi::Psi* psi, diff --git a/source/source_lcao/FORCE.h b/source/source_lcao/FORCE.h index 5eba250181..a80dd02f25 100644 --- a/source/source_lcao/FORCE.h +++ b/source/source_lcao/FORCE.h @@ -37,8 +37,9 @@ class Force_LCAO const UnitCell& ucell, const Grid_Driver& gd, const psi::Psi* psi, - const elecstate::ElecState* pelec, - ModuleBase::matrix& foverlap, + const elecstate::ElecState* pelec, + const elecstate::DensityMatrix* dm, // mohan add 2025-11-04 + ModuleBase::matrix& foverlap, ModuleBase::matrix& ftvnl_dphi, ModuleBase::matrix& fvnl_dbeta, ModuleBase::matrix& fvl_dphi, diff --git a/source/source_lcao/FORCE_STRESS.cpp b/source/source_lcao/FORCE_STRESS.cpp index 21864a0243..e03708a527 100644 --- a/source/source_lcao/FORCE_STRESS.cpp +++ b/source/source_lcao/FORCE_STRESS.cpp @@ -134,7 +134,7 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, //! atomic forces from integration (4 terms) this->integral_part(PARAM.globalv.gamma_only_local, isforce, isstress, - ucell, gd, fsr, pelec, psi, foverlap, ftvnl_dphi, + ucell, gd, fsr, pelec, dmat.dm, psi, foverlap, ftvnl_dphi, // add dmat.dm, mohan 20251104 fvnl_dbeta, fvl_dphi, soverlap, stvnl_dphi, svnl_dbeta, svl_dphi, fvnl_dalpha, svnl_dalpha, deepks, two_center_bundle, orb, pv, kv); @@ -145,18 +145,16 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, hamilt::NonlocalNew> tmp_nonlocal(nullptr, kv.kvec_d, nullptr, &ucell, orb.cutoffs(), &gd, two_center_bundle.overlap_orb_beta.get()); - const auto* dm_p = dmat.get_dm(); - if (PARAM.inp.nspin == 2) { - const_cast*>(dm_p)->switch_dmr(1); + dmat.dm->switch_dmr(1); } - const hamilt::HContainer* dmr = dm_p->get_DMR_pointer(1); + const hamilt::HContainer* dmr = dmat.dm->get_DMR_pointer(1); tmp_nonlocal.cal_force_stress(isforce, isstress, dmr, fvnl_dbeta, svnl_dbeta); if (PARAM.inp.nspin == 2) { - const_cast*>(dm_p)->switch_dmr(0); + dmat.dm->switch_dmr(0); } } else if (PARAM.inp.nspin == 4) @@ -167,12 +165,11 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, // calculate temporary complex DMR for nonlocal force&stress // In fact, only SOC part need the imaginary part of DMR for correct force&stress - const auto* dm_p = dmat.get_dm(); - hamilt::HContainer> tmp_dmr(dm_p->get_DMR_pointer(1)->get_paraV()); - std::vector ijrs = dm_p->get_DMR_pointer(1)->get_ijr_info(); + hamilt::HContainer> tmp_dmr(dmat.dm->get_DMR_pointer(1)->get_paraV()); + std::vector ijrs = dmat.dm->get_DMR_pointer(1)->get_ijr_info(); tmp_dmr.insert_ijrs(&ijrs); tmp_dmr.allocate(); - dm_p->cal_DMR_full(&tmp_dmr); + dmat.dm->cal_DMR_full(&tmp_dmr); tmp_nonlocal.cal_force_stress(isforce, isstress, &tmp_dmr, fvnl_dbeta, svnl_dbeta); } @@ -251,17 +248,20 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, { // GlobalC::dftu.force_stress(ucell, gd, pelec, pv, fsr, force_dftu, stress_dftu, kv); // mohan modify 2025-11-03 - auto dm_pointer = dmat.get_dm(); + std::vector>* dmk_d = nullptr; + std::vector>>* dmk_c = nullptr; + auto& dmk_tmp = dmat.dm->get_DMK_vector(); if(PARAM.globalv.gamma_only_local) { // T is double - GlobalC::dftu.force_stress(ucell, gd, dm_pointer, nullptr, pv, fsr, force_dftu, stress_dftu, kv); + //dmk_d = &dmk_tmp; } else { // T is complex - GlobalC::dftu.force_stress(ucell, gd, nullptr, dm_pointer, pv, fsr, force_dftu, stress_dftu, kv); + //dmk_c = &dmk_tmp; } + GlobalC::dftu.force_stress(ucell, gd, dmk_d, dmk_c, pv, fsr, force_dftu, stress_dftu, kv); } else { @@ -300,16 +300,15 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, two_center_bundle.overlap_orb_onsite.get(), orb.cutoffs()); - const auto* dm_p = dmat.get_dm(); if (PARAM.inp.nspin == 2) { - const_cast, double>*>(dm_p)->switch_dmr(2); + dmat.dm->switch_dmr(2); } - const hamilt::HContainer* dmr = dm_p->get_DMR_pointer(1); + const hamilt::HContainer* dmr = dmat.dm->get_DMR_pointer(1); tmp_dspin.cal_force_stress(isforce, isstress, dmr, force_dspin, stress_dspin); if (PARAM.inp.nspin == 2) { - const_cast, double>*>(dm_p)->switch_dmr(0); + dmat.dm->switch_dmr(0); } } @@ -725,31 +724,32 @@ void Force_Stress_LCAO::calForcePwPart(UnitCell& ucell, // overlap, kinetic, nonlocal pseudopotential, Local potential terms in force and stress template <> void Force_Stress_LCAO::integral_part(const bool isGammaOnly, - const bool isforce, - const bool isstress, - const UnitCell& ucell, - const Grid_Driver& gd, - ForceStressArrays& fsr, // mohan add 2024-06-15 - const elecstate::ElecState* pelec, - const psi::Psi* psi, - ModuleBase::matrix& foverlap, - ModuleBase::matrix& ftvnl_dphi, - ModuleBase::matrix& fvnl_dbeta, - ModuleBase::matrix& fvl_dphi, - ModuleBase::matrix& soverlap, - ModuleBase::matrix& stvnl_dphi, - ModuleBase::matrix& svnl_dbeta, - ModuleBase::matrix& svl_dphi, - ModuleBase::matrix& fvnl_dalpha, - ModuleBase::matrix& svnl_dalpha, - Setup_DeePKS& deepks, - const TwoCenterBundle& two_center_bundle, - const LCAO_Orbitals& orb, - const Parallel_Orbitals& pv, - const K_Vectors& kv) + const bool isforce, + const bool isstress, + const UnitCell& ucell, + const Grid_Driver& gd, + ForceStressArrays& fsr, // mohan add 2024-06-15 + const elecstate::ElecState* pelec, + const elecstate::DensityMatrix* dm, // mohan add 2025-11-04 + const psi::Psi* psi, + ModuleBase::matrix& foverlap, + ModuleBase::matrix& ftvnl_dphi, + ModuleBase::matrix& fvnl_dbeta, + ModuleBase::matrix& fvl_dphi, + ModuleBase::matrix& soverlap, + ModuleBase::matrix& stvnl_dphi, + ModuleBase::matrix& svnl_dbeta, + ModuleBase::matrix& svl_dphi, + ModuleBase::matrix& fvnl_dalpha, + ModuleBase::matrix& svnl_dalpha, + Setup_DeePKS& deepks, + const TwoCenterBundle& two_center_bundle, + const LCAO_Orbitals& orb, + const Parallel_Orbitals& pv, + const K_Vectors& kv) { - flk.ftable(isforce, isstress, fsr, ucell, gd, psi, pelec, + flk.ftable(isforce, isstress, fsr, ucell, gd, psi, pelec, dm, foverlap, ftvnl_dphi, fvnl_dbeta, fvl_dphi, soverlap, stvnl_dphi, svnl_dbeta, svl_dphi, fvnl_dalpha, svnl_dalpha, deepks, two_center_bundle, orb, pv); @@ -758,30 +758,31 @@ void Force_Stress_LCAO::integral_part(const bool isGammaOnly, template <> void Force_Stress_LCAO>::integral_part(const bool isGammaOnly, - const bool isforce, - const bool isstress, - const UnitCell& ucell, - const Grid_Driver& gd, - ForceStressArrays& fsr, // mohan add 2024-06-15 - const elecstate::ElecState* pelec, - const psi::Psi>* psi, - ModuleBase::matrix& foverlap, - ModuleBase::matrix& ftvnl_dphi, - ModuleBase::matrix& fvnl_dbeta, - ModuleBase::matrix& fvl_dphi, - ModuleBase::matrix& soverlap, - ModuleBase::matrix& stvnl_dphi, - ModuleBase::matrix& svnl_dbeta, - ModuleBase::matrix& svl_dphi, - ModuleBase::matrix& fvnl_dalpha, - ModuleBase::matrix& svnl_dalpha, - Setup_DeePKS>& deepks, - const TwoCenterBundle& two_center_bundle, - const LCAO_Orbitals& orb, - const Parallel_Orbitals& pv, - const K_Vectors& kv) + const bool isforce, + const bool isstress, + const UnitCell& ucell, + const Grid_Driver& gd, + ForceStressArrays& fsr, // mohan add 2024-06-15 + const elecstate::ElecState* pelec, + const elecstate::DensityMatrix, double>* dm, // mohan add 2025-11-04 + const psi::Psi>* psi, + ModuleBase::matrix& foverlap, + ModuleBase::matrix& ftvnl_dphi, + ModuleBase::matrix& fvnl_dbeta, + ModuleBase::matrix& fvl_dphi, + ModuleBase::matrix& soverlap, + ModuleBase::matrix& stvnl_dphi, + ModuleBase::matrix& svnl_dbeta, + ModuleBase::matrix& svl_dphi, + ModuleBase::matrix& fvnl_dalpha, + ModuleBase::matrix& svnl_dalpha, + Setup_DeePKS>& deepks, + const TwoCenterBundle& two_center_bundle, + const LCAO_Orbitals& orb, + const Parallel_Orbitals& pv, + const K_Vectors& kv) { - flk.ftable(isforce, isstress, fsr, ucell, gd, psi, pelec, + flk.ftable(isforce, isstress, fsr, ucell, gd, psi, pelec, dm, foverlap, ftvnl_dphi, fvnl_dbeta, fvl_dphi, soverlap, stvnl_dphi, svnl_dbeta, svl_dphi, fvnl_dalpha, svnl_dalpha, deepks, diff --git a/source/source_lcao/FORCE_STRESS.h b/source/source_lcao/FORCE_STRESS.h index be5cec951d..188b1d20c0 100644 --- a/source/source_lcao/FORCE_STRESS.h +++ b/source/source_lcao/FORCE_STRESS.h @@ -81,8 +81,9 @@ class Force_Stress_LCAO const UnitCell& ucell, const Grid_Driver& gd, ForceStressArrays& fsr, // mohan add 2024-06-15 - const elecstate::ElecState* pelec, - const psi::Psi* psi, + const elecstate::ElecState* pelec, + const elecstate::DensityMatrix* dm, // mohan add 2025-11-04 + const psi::Psi* psi, ModuleBase::matrix& foverlap, ModuleBase::matrix& ftvnl_dphi, ModuleBase::matrix& fvnl_dbeta, diff --git a/source/source_lcao/FORCE_gamma.cpp b/source/source_lcao/FORCE_gamma.cpp index 763a3e3321..fb9249c4b0 100644 --- a/source/source_lcao/FORCE_gamma.cpp +++ b/source/source_lcao/FORCE_gamma.cpp @@ -158,9 +158,10 @@ void Force_LCAO::ftable(const bool isforce, const UnitCell& ucell, const Grid_Driver& gd, const psi::Psi* psi, - const elecstate::ElecState* pelec, - ModuleBase::matrix& foverlap, - ModuleBase::matrix& ftvnl_dphi, + const elecstate::ElecState* pelec, + const elecstate::DensityMatrix* dm, // mohan add 2025-11-04 + ModuleBase::matrix& foverlap, + ModuleBase::matrix& ftvnl_dphi, ModuleBase::matrix& fvnl_dbeta, ModuleBase::matrix& fvl_dphi, ModuleBase::matrix& soverlap, @@ -179,10 +180,6 @@ void Force_LCAO::ftable(const bool isforce, ModuleBase::TITLE("Forces", "ftable"); ModuleBase::timer::tick("Forces", "ftable"); - // get DM - const elecstate::DensityMatrix* dm - = dynamic_cast*>(pelec)->get_DM(); - this->ParaV = dm->get_paraV_pointer(); // allocate DSloc_x, DSloc_y, DSloc_z diff --git a/source/source_lcao/FORCE_k.cpp b/source/source_lcao/FORCE_k.cpp index 1e19a53a20..7a774481b7 100644 --- a/source/source_lcao/FORCE_k.cpp +++ b/source/source_lcao/FORCE_k.cpp @@ -190,35 +190,33 @@ void Force_LCAO>::finish_ftable(ForceStressArrays& fsr) // be called in Force_LCAO::start_force_calculation template <> void Force_LCAO>::ftable(const bool isforce, - const bool isstress, - ForceStressArrays& fsr, // mohan add 2024-06-15 - const UnitCell& ucell, - const Grid_Driver& gd, - const psi::Psi>* psi, - const elecstate::ElecState* pelec, - ModuleBase::matrix& foverlap, - ModuleBase::matrix& ftvnl_dphi, - ModuleBase::matrix& fvnl_dbeta, - ModuleBase::matrix& fvl_dphi, - ModuleBase::matrix& soverlap, - ModuleBase::matrix& stvnl_dphi, - ModuleBase::matrix& svnl_dbeta, - ModuleBase::matrix& svl_dphi, - ModuleBase::matrix& fvnl_dalpha, - ModuleBase::matrix& svnl_dalpha, - Setup_DeePKS>& deepks, - const TwoCenterBundle& two_center_bundle, - const LCAO_Orbitals& orb, - const Parallel_Orbitals& pv, - const K_Vectors* kv, - Record_adj* ra) + const bool isstress, + ForceStressArrays& fsr, // mohan add 2024-06-15 + const UnitCell& ucell, + const Grid_Driver& gd, + const psi::Psi>* psi, + const elecstate::ElecState* pelec, + const elecstate::DensityMatrix, double>* dm, // mohan add 2025-11-04 + ModuleBase::matrix& foverlap, + ModuleBase::matrix& ftvnl_dphi, + ModuleBase::matrix& fvnl_dbeta, + ModuleBase::matrix& fvl_dphi, + ModuleBase::matrix& soverlap, + ModuleBase::matrix& stvnl_dphi, + ModuleBase::matrix& svnl_dbeta, + ModuleBase::matrix& svl_dphi, + ModuleBase::matrix& fvnl_dalpha, + ModuleBase::matrix& svnl_dalpha, + Setup_DeePKS>& deepks, + const TwoCenterBundle& two_center_bundle, + const LCAO_Orbitals& orb, + const Parallel_Orbitals& pv, + const K_Vectors* kv, + Record_adj* ra) { ModuleBase::TITLE("Forces", "ftable"); ModuleBase::timer::tick("Forces", "ftable"); - elecstate::DensityMatrix , double>* dm - = dynamic_cast>*>(pelec)->get_DM(); - this->allocate(ucell, gd, pv, diff --git a/source/source_lcao/module_deltaspin/.spin_constrain.h.swp b/source/source_lcao/module_deltaspin/.spin_constrain.h.swp new file mode 100644 index 0000000000000000000000000000000000000000..1814ebab47c82516ba738d9c5b3b260bdf448780 GIT binary patch literal 16384 zcmeHNTa08y89s7fxeA)74_fTT$!uq4doCc_JKL~33rLn>$DLs}F}TO-bE>C{IXBK_ zhGkq1|N_YUvyPSHZDARA%;jW(H9@Mh~HmT=k)3B)4M~A zAu;D9-*k6f{;K-wufP8PpVOH+de;)WWzS6(p6e{@?BW%2_S4s2e)^M^<%c~UQ27O( z#r?~xH_y&opp|7ktOPb6kUWSp+2kGWWXZJeG?Ork9brCf&*5Ry58ccYwky1pCy@-A z^v8C>AW35`gFU?qj#f9Q8K@Z;WZ-IReoi0MT{~}NH(Y=3V0HbgW}s%EW}s%EW}s%E zW}s%EW}s%^|C)g`y4X5}6<%akdB6Gm$k6Be=J~2AUmvQ!*!;fFeExc<-u$dTY6fZs zY6fZsY6fZsY6fZsY6fZsY6fZsY6fZsJ_H81YgyN0+*c{!$N&HH`2US-E$e0A_rOcQ z^T1DlM*#us2N>|?HJ0^T;77nCz}Em9_%d)Q@b9ZF>pkGF!0W(YfLDN*ffs;hfv17* z0Z#xN_zExwd<^*4RhIP{a2j|5SO;zgJ_WpcrDdH1eg-@VJPPasJ`Y?6ocpL{{Q(Go zdElQrEbAA*3&69$Gr$(G7ia-D0dHSnS&sv^0$&7n0Xu=KfGdI5FUNX;Gr%Lje&A1+ zS=Jwc=YW&IoxnA~)xevVq73*t-~#Vmf_Vdf2EGF<0&gLC@?F3M4gl{W8FLyq1*`$f zzzpzl;0E9l;5F76*XWd?lxhn;9*cA>xvHh)~S?1zs zdz3VmOJhn}q3C+3Sm1O>M=842zwL4|!4kn^r#EHvX<|fWX$m}wyc4W63XNY{YV0R= zl7`VFjw}l^94`D6(G(6!%B1;jjs&$8aU90^kj%|ubw-PVG)$q@d2w-awJtC(3?tSP zUL<1H%>uRR#IknmfCke$R%+0o!vi;&DwnKxN>2p2ODw?|7Xn*GE?B zrKom7Kk~$b`B`nSj&LG-eTso7WzLGjWi+toI9eq}diLzp2X)03XOW9DX-A2)ogRWs!7HIw!J(R5xxE*376>9$O4pRdtddK&^9xJ)M6rcDtufofH#8(8o>oQE`v z12(f~W*f4I^6{by?NHkw0!%^EZ^od_!S&~{4en)hQ(@}HY*%&-yX?Ls^KHrQ)BA*P z4YnlFv)O2r56iZ-2%8dQo)@LDI*_?i+y18AjYHq;kJ_62#1|?|?EBJ=HqEB=J4^M- zq|s)B z^6A3U5@;mWQjDvwLFRX0Q_zv5tt{SA+=y@>@~-->93=fb1FPerb}osyBQV?Weq8Gi z2)85cg|0eW#mOMX1|GdV%g0)+c2NzZk<<_{(8ZHzmm;!Z7y#>p->$16- zxtphFXQyZ8tX;C}!hf)BT81?+eQo?OJdBsVAP1^R4^V0MxG%l5s)aUnhivT&B@=9Y zqpBI{Nub=LicyxKFPFvdXoY`jU@WB}FgI!TAZ!SFP8r4wl61?ZlZZ#vhm<9RO+pmsCC^lutRW zR!#3F!|oltV^!^;wvc3KqO2nwx>ZHoB^@SPPFz=ZBlCg|o^)E)mgRUnN!SV+FXpd5 zTdBZRNnYkcdLj}5_XS%$)V3_RFqxCG6=y$A>_MJ#*zYZi>h;l~A&ZG3y3u;P3x!_h z3bs?fi47}H8ehDG?KjPB2J=puy{ETxn~Ds*t`=RKFvuEhq;?0yhC~AXa}0SOXfs-w~^y0e%2H0KAR3{HK5i8~}C$p8zfbC|*Ac zoB>`0ehfSY2;eT@2H+jU=)VNM348Vg@DAeh7lG5j5nw0q7UJ_W zz;A$`15X3r1|A0<1HJ`30^9=31D^%11O5R%e+N7dJO!KrIIsxZ2HXnl1a1U~`%8iP z`LGz!c93k^$N{lVQt?;|dXmy7h2xx(>z^tomoE%K8LiiMSDW=`HNK7ZaQmZ5n2QJZj@~D1%Tj)S;X0ceN8M*I)yOC@!cZG>PCyQg6MD^S-9VGQO$TjV+m{g>g@Z@e2ux}M{kBQC7tn59hx|isH z;$qeJ)p}z+v zNKwTqAW`9W6e>TAlrxt%#kiAZhz;qav7{Gfo(nBx2Pt7euGhk0v-RYkE6;xQNW0w_ zCXw{B5PCi*FL&>h0OMrlxgp!AfW4PY4+0eOa%i?kPBgOvpmLsxqxe?tTXujeyB(iH zM}*n4y6^Eh)m!bND>~U4YE_7+$t#IS^I6$5fQ*Qr{_+7_7C`A-9=aO-y#*kge0fD%T96D7{f$MRwr$T=U2Yr6+^4$h)sN2Pr#! zCvw%p1gXyAXhrApF80GU94bgtaT_Kw81&)aHpT|+n;eJ+*~G+dC@GSwxO8a&c(8`0 z5Vi=Rqgvz85vU{1g84_CF@NOYmMpaX z_nuA)k`Ga;vWSv_UdZB%wnyP`%hGi=+`d$$dXep>DVcj=xVb4FZd%c7cn(+yuCL*S tx>U7gV;{5T5*2;FWNfFLsl*wR>l*551}X7@K}OPSL!F4N{061g{{bzJH7WoA literal 0 HcmV?d00001 diff --git a/source/source_lcao/module_deltaspin/cal_mw.cpp b/source/source_lcao/module_deltaspin/cal_mw.cpp index 7f6a49e207..becdd82e7c 100644 --- a/source/source_lcao/module_deltaspin/cal_mw.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw.cpp @@ -19,14 +19,16 @@ void spinconstrain::SpinConstrain>::cal_mi_lcao(const int& ModuleBase::timer::tick("spinconstrain::SpinConstrain", "cal_mi_lcao"); // calculate MW from lambda in real space projection method this->zero_Mi(); - const hamilt::HContainer* dmr - = static_cast>*>(this->pelec)->get_DM()->get_DMR_pointer(1); + const hamilt::HContainer* dmr = this->dm_->get_DMR_pointer(1); std::vector moments; if(PARAM.inp.nspin==2) { - static_cast>*>(this->pelec)->get_DM()->switch_dmr(2); + this->dm_->switch_dmr(2); + moments = static_cast, double>>*>(this->p_operator)->cal_moment(dmr, this->get_constrain()); - static_cast>*>(this->pelec)->get_DM()->switch_dmr(0); + + this->dm_->switch_dmr(0); + for(int iat=0;iatMi_.size();iat++) { this->Mi_[iat].x = 0.0; @@ -167,4 +169,4 @@ void spinconstrain::SpinConstrain::set_operator( hamilt::Operator* op_in) { this->p_operator = op_in; -} \ No newline at end of file +} diff --git a/source/source_lcao/setup_dm.h b/source/source_lcao/setup_dm.h index 7c7d7eb203..672a50c878 100644 --- a/source/source_lcao/setup_dm.h +++ b/source/source_lcao/setup_dm.h @@ -29,13 +29,6 @@ class Setup_DM // allocate density matrix void allocate_dm(const K_Vectors* kv, const Parallel_Orbitals* pv, const int nspin); - elecstate::DensityMatrix* get_dm() const - { - return const_cast*>(this->dm); - } - - private: - elecstate::DensityMatrix* dm = nullptr; }; From 531b647c1396cb1af014d0d31997ecbdf091d56e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 09:39:41 +0800 Subject: [PATCH 58/81] one more fix --- source/source_io/ctrl_scf_lcao.cpp | 1 + source/source_io/output_mulliken.h | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/source/source_io/ctrl_scf_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp index 77344632d8..2d28b1bc2e 100644 --- a/source/source_io/ctrl_scf_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -300,6 +300,7 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, p_hamilt, kv, pelec, + dm, // mohan add 2025-11-04 two_center_bundle, orb, ucell, diff --git a/source/source_io/output_mulliken.h b/source/source_io/output_mulliken.h index e20cea9e04..d072a7c837 100644 --- a/source/source_io/output_mulliken.h +++ b/source/source_io/output_mulliken.h @@ -9,6 +9,7 @@ #include "source_io/output_sk.h" #include "source_base/formatter.h" #include "source_lcao/module_operator_lcao/dspin_lcao.h" +#include "source_estate/module_dm/density_matrix.h" // mohan add 2025-11-04 #include #include @@ -90,6 +91,7 @@ void cal_mag(Parallel_Orbitals* pv, hamilt::Hamilt* p_ham, K_Vectors& kv, elecstate::ElecState* pelec, + elecstate::DensityMatrix* dm, // mohan add 2025-11-04 const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, UnitCell& ucell, @@ -103,10 +105,7 @@ void cal_mag(Parallel_Orbitals* pv, auto cell_index = CellIndex(ucell.get_atomLabels(), ucell.get_atomCounts(), ucell.get_lnchiCounts(), PARAM.inp.nspin); auto out_s_k = ModuleIO::Output_Sk(p_ham, pv, PARAM.inp.nspin, kv.get_nks()); - auto out_dm_k = ModuleIO::Output_DMK(dynamic_cast*>(pelec)->get_DM(), - pv, - PARAM.inp.nspin, - kv.get_nks()); + auto out_dm_k = ModuleIO::Output_DMK(dm, pv, PARAM.inp.nspin, kv.get_nks()); auto mulp = ModuleIO::Output_Mulliken(&(out_s_k), &(out_dm_k), pv, &cell_index, kv.isk, PARAM.inp.nspin); auto atom_chg = mulp.get_atom_chg(); @@ -127,8 +126,7 @@ void cal_mag(Parallel_Orbitals* pv, { std::vector> atom_mag(ucell.nat, std::vector(PARAM.inp.nspin, 0.0)); std::vector> constrain(ucell.nat, ModuleBase::Vector3(1, 1, 1)); - const hamilt::HContainer* dmr - = dynamic_cast*>(pelec)->get_DM()->get_DMR_pointer(1); + const hamilt::HContainer* dmr = dm->get_DMR_pointer(1); std::vector moments; std::vector mag_x(ucell.nat, 0.0); std::vector mag_y(ucell.nat, 0.0); @@ -144,9 +142,9 @@ void cal_mag(Parallel_Orbitals* pv, &gd, two_center_bundle.overlap_orb_onsite.get(), orb.cutoffs()); - dynamic_cast*>(pelec)->get_DM()->switch_dmr(2); + dm->switch_dmr(2); moments = sc_lambda->cal_moment(dmr, constrain); - dynamic_cast*>(pelec)->get_DM()->switch_dmr(0); + dm->switch_dmr(0); delete sc_lambda; //const std::vector title = {"Total Magnetism (uB)", ""}; //const std::vector fmts = {"%-26s", "%20.10f"}; From bbfea9a37da513af9faa43aec4d39d136551382d Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 12:05:38 +0800 Subject: [PATCH 59/81] now I can compile the code --- source/source_esolver/esolver_dm2rho.cpp | 6 ++-- source/source_esolver/esolver_double_xc.cpp | 12 +++---- source/source_esolver/esolver_ks_lcao.cpp | 11 +++++- source/source_lcao/FORCE_STRESS.cpp | 13 ++----- source/source_lcao/FORCE_STRESS.h | 33 ++++++++++++++++++ .../module_deltaspin/.spin_constrain.h.swp | Bin 16384 -> 0 bytes 6 files changed, 54 insertions(+), 21 deletions(-) delete mode 100644 source/source_lcao/module_deltaspin/.spin_constrain.h.swp diff --git a/source/source_esolver/esolver_dm2rho.cpp b/source/source_esolver/esolver_dm2rho.cpp index 3f1b482dcb..84e6da5141 100644 --- a/source/source_esolver/esolver_dm2rho.cpp +++ b/source/source_esolver/esolver_dm2rho.cpp @@ -50,18 +50,18 @@ void ESolver_DM2rho::runner(UnitCell& ucell, const int istep) std::string zipname = "output_DM0.npz"; // read DM from file - ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(this->dmat.dm.get_DMR_pointer(1))); + ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(this->dmat.dm->get_DMR_pointer(1))); // if nspin=2, need extra reading if (PARAM.inp.nspin == 2) { zipname = "output_DM1.npz"; - ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(this->dmat.dm.get_DMR_pointer(2))); + ModuleIO::read_mat_npz(&(this->pv), ucell, zipname, *(this->dmat.dm->get_DMR_pointer(2))); } // it's dangerous to design psiToRho function like this, mohan note 20251024 // this->pelec->psiToRho(*this->psi); - LCAO_domain::dm2rho(this->dmat.dm.get_DMR_vector(), PARAM.inp.nspin, &this->chr); + LCAO_domain::dm2rho(this->dmat.dm->get_DMR_vector(), PARAM.inp.nspin, &this->chr); int nspin0 = PARAM.inp.nspin == 2 ? 2 : 1; diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index 7ae8dee5dc..66536efc36 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -146,7 +146,7 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) this->kv, this->two_center_bundle_, this->orb_, - &this->dmat_base.dm, + this->dmat_base.dm, this->deepks, istep, this->exx_nao); @@ -157,11 +157,11 @@ void ESolver_DoubleXC::before_scf(UnitCell& ucell, const int istep) XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); // DMR should be same size with Hamiltonian(R) - this->dmat_base.dm.init_DMR(*(dynamic_cast*>(this->p_hamilt_base)->getHR())); + this->dmat_base.dm->init_DMR(*(dynamic_cast*>(this->p_hamilt_base)->getHR())); if (istep > 0) { - this->dmat_base.dm.cal_DMR(); + this->dmat_base.dm->cal_DMR(); } ModuleBase::timer::tick("ESolver_DoubleXC", "before_scf"); @@ -233,7 +233,7 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int this->gd, &(this->pv), *(this->psi), - &this->dmat_base.dm, + this->dmat_base.dm, p_ham_deepks, iter, conv_esolver, @@ -349,10 +349,10 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int for (int ik = 0; ik < nks; ik++) { // mohan update 2025-11-03 - this->dmat_base.dm.set_DMK_pointer(ik, this->dmat.dm.get_DMK_pointer(ik)); + this->dmat_base.dm->set_DMK_pointer(ik, this->dmat.dm->get_DMK_pointer(ik)); // _pes_lcao_base->get_DM()->set_DMK_pointer(ik, _pes_lcao->get_DM()->get_DMK_pointer(ik)); } - this->dmat_base.dm.cal_DMR(); + this->dmat_base.dm->cal_DMR(); // _pes_lcao_base->get_DM()->cal_DMR(); _pes_lcao_base->ekb = _pes_lcao->ekb; _pes_lcao_base->wg = _pes_lcao->wg; diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 464d1d0856..91b6ccb2c5 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -574,8 +574,14 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); + auto* estate = dynamic_cast*>(this->pelec); auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); + if(!estate) + { + 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"); @@ -591,8 +597,11 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const //! 2) output of lcao every few ionic steps + + + ModuleIO::ctrl_scf_lcao(ucell, - PARAM.inp, this->kv, this->pelec, this->dmat.dm, this->pv, + PARAM.inp, this->kv, estate, this->dmat.dm, this->pv, this->gd, this->psi, hamilt_lcao, this->two_center_bundle_, this->orb_, this->pw_wfc, this->pw_rho, diff --git a/source/source_lcao/FORCE_STRESS.cpp b/source/source_lcao/FORCE_STRESS.cpp index e03708a527..e885eb9559 100644 --- a/source/source_lcao/FORCE_STRESS.cpp +++ b/source/source_lcao/FORCE_STRESS.cpp @@ -250,17 +250,8 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, // mohan modify 2025-11-03 std::vector>* dmk_d = nullptr; std::vector>>* dmk_c = nullptr; - auto& dmk_tmp = dmat.dm->get_DMK_vector(); - if(PARAM.globalv.gamma_only_local) - { - // T is double - //dmk_d = &dmk_tmp; - } - else - { - // T is complex - //dmk_c = &dmk_tmp; - } + // add a new template function + assign_dmk_ptr(dmat.dm, dmk_d, dmk_c, PARAM.globalv.gamma_only_local); GlobalC::dftu.force_stress(ucell, gd, dmk_d, dmk_c, pv, fsr, force_dftu, stress_dftu, kv); } else diff --git a/source/source_lcao/FORCE_STRESS.h b/source/source_lcao/FORCE_STRESS.h index 188b1d20c0..8a387e42cb 100644 --- a/source/source_lcao/FORCE_STRESS.h +++ b/source/source_lcao/FORCE_STRESS.h @@ -118,4 +118,37 @@ class Force_Stress_LCAO template double Force_Stress_LCAO::force_invalid_threshold_ev = 0.00; +// only for DFT+U, mohan add 2025-11-04 +template +void assign_dmk_ptr( + elecstate::DensityMatrix* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +); + +template <> +void assign_dmk_ptr( + elecstate::DensityMatrix* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +) { + auto& dmk_tmp = dm->get_DMK_vector(); + dmk_d = &dmk_tmp; + dmk_c = nullptr; +} + +template <> +void assign_dmk_ptr>( + elecstate::DensityMatrix,double>* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +) { + auto& dmk_tmp = dm->get_DMK_vector(); + dmk_c = &dmk_tmp; + dmk_d = nullptr; +} + #endif diff --git a/source/source_lcao/module_deltaspin/.spin_constrain.h.swp b/source/source_lcao/module_deltaspin/.spin_constrain.h.swp deleted file mode 100644 index 1814ebab47c82516ba738d9c5b3b260bdf448780..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeHNTa08y89s7fxeA)74_fTT$!uq4doCc_JKL~33rLn>$DLs}F}TO-bE>C{IXBK_ zhGkq1|N_YUvyPSHZDARA%;jW(H9@Mh~HmT=k)3B)4M~A zAu;D9-*k6f{;K-wufP8PpVOH+de;)WWzS6(p6e{@?BW%2_S4s2e)^M^<%c~UQ27O( z#r?~xH_y&opp|7ktOPb6kUWSp+2kGWWXZJeG?Ork9brCf&*5Ry58ccYwky1pCy@-A z^v8C>AW35`gFU?qj#f9Q8K@Z;WZ-IReoi0MT{~}NH(Y=3V0HbgW}s%EW}s%EW}s%E zW}s%EW}s%^|C)g`y4X5}6<%akdB6Gm$k6Be=J~2AUmvQ!*!;fFeExc<-u$dTY6fZs zY6fZsY6fZsY6fZsY6fZsY6fZsY6fZsJ_H81YgyN0+*c{!$N&HH`2US-E$e0A_rOcQ z^T1DlM*#us2N>|?HJ0^T;77nCz}Em9_%d)Q@b9ZF>pkGF!0W(YfLDN*ffs;hfv17* z0Z#xN_zExwd<^*4RhIP{a2j|5SO;zgJ_WpcrDdH1eg-@VJPPasJ`Y?6ocpL{{Q(Go zdElQrEbAA*3&69$Gr$(G7ia-D0dHSnS&sv^0$&7n0Xu=KfGdI5FUNX;Gr%Lje&A1+ zS=Jwc=YW&IoxnA~)xevVq73*t-~#Vmf_Vdf2EGF<0&gLC@?F3M4gl{W8FLyq1*`$f zzzpzl;0E9l;5F76*XWd?lxhn;9*cA>xvHh)~S?1zs zdz3VmOJhn}q3C+3Sm1O>M=842zwL4|!4kn^r#EHvX<|fWX$m}wyc4W63XNY{YV0R= zl7`VFjw}l^94`D6(G(6!%B1;jjs&$8aU90^kj%|ubw-PVG)$q@d2w-awJtC(3?tSP zUL<1H%>uRR#IknmfCke$R%+0o!vi;&DwnKxN>2p2ODw?|7Xn*GE?B zrKom7Kk~$b`B`nSj&LG-eTso7WzLGjWi+toI9eq}diLzp2X)03XOW9DX-A2)ogRWs!7HIw!J(R5xxE*376>9$O4pRdtddK&^9xJ)M6rcDtufofH#8(8o>oQE`v z12(f~W*f4I^6{by?NHkw0!%^EZ^od_!S&~{4en)hQ(@}HY*%&-yX?Ls^KHrQ)BA*P z4YnlFv)O2r56iZ-2%8dQo)@LDI*_?i+y18AjYHq;kJ_62#1|?|?EBJ=HqEB=J4^M- zq|s)B z^6A3U5@;mWQjDvwLFRX0Q_zv5tt{SA+=y@>@~-->93=fb1FPerb}osyBQV?Weq8Gi z2)85cg|0eW#mOMX1|GdV%g0)+c2NzZk<<_{(8ZHzmm;!Z7y#>p->$16- zxtphFXQyZ8tX;C}!hf)BT81?+eQo?OJdBsVAP1^R4^V0MxG%l5s)aUnhivT&B@=9Y zqpBI{Nub=LicyxKFPFvdXoY`jU@WB}FgI!TAZ!SFP8r4wl61?ZlZZ#vhm<9RO+pmsCC^lutRW zR!#3F!|oltV^!^;wvc3KqO2nwx>ZHoB^@SPPFz=ZBlCg|o^)E)mgRUnN!SV+FXpd5 zTdBZRNnYkcdLj}5_XS%$)V3_RFqxCG6=y$A>_MJ#*zYZi>h;l~A&ZG3y3u;P3x!_h z3bs?fi47}H8ehDG?KjPB2J=puy{ETxn~Ds*t`=RKFvuEhq;?0yhC~AXa}0SOXfs-w~^y0e%2H0KAR3{HK5i8~}C$p8zfbC|*Ac zoB>`0ehfSY2;eT@2H+jU=)VNM348Vg@DAeh7lG5j5nw0q7UJ_W zz;A$`15X3r1|A0<1HJ`30^9=31D^%11O5R%e+N7dJO!KrIIsxZ2HXnl1a1U~`%8iP z`LGz!c93k^$N{lVQt?;|dXmy7h2xx(>z^tomoE%K8LiiMSDW=`HNK7ZaQmZ5n2QJZj@~D1%Tj)S;X0ceN8M*I)yOC@!cZG>PCyQg6MD^S-9VGQO$TjV+m{g>g@Z@e2ux}M{kBQC7tn59hx|isH z;$qeJ)p}z+v zNKwTqAW`9W6e>TAlrxt%#kiAZhz;qav7{Gfo(nBx2Pt7euGhk0v-RYkE6;xQNW0w_ zCXw{B5PCi*FL&>h0OMrlxgp!AfW4PY4+0eOa%i?kPBgOvpmLsxqxe?tTXujeyB(iH zM}*n4y6^Eh)m!bND>~U4YE_7+$t#IS^I6$5fQ*Qr{_+7_7C`A-9=aO-y#*kge0fD%T96D7{f$MRwr$T=U2Yr6+^4$h)sN2Pr#! zCvw%p1gXyAXhrApF80GU94bgtaT_Kw81&)aHpT|+n;eJ+*~G+dC@GSwxO8a&c(8`0 z5Vi=Rqgvz85vU{1g84_CF@NOYmMpaX z_nuA)k`Ga;vWSv_UdZB%wnyP`%hGi=+`d$$dXep>DVcj=xVb4FZd%c7cn(+yuCL*S tx>U7gV;{5T5*2;FWNfFLsl*wR>l*551}X7@K}OPSL!F4N{061g{{bzJH7WoA From fc7bc7ccf230c074d0a55f9e7feb80651cf319df Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 13:36:00 +0800 Subject: [PATCH 60/81] fix some bugs in EXX version --- source/source_esolver/esolver_ks_lcao.cpp | 4 ++-- source/source_io/ctrl_iter_lcao.cpp | 4 ++-- source/source_io/get_wf_lcao.cpp | 1 + .../source_lcao/module_ri/Exx_LRI_interface.h | 4 +++- .../module_ri/Exx_LRI_interface.hpp | 22 ++++++++++--------- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 91b6ccb2c5..b7b023234f 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -383,11 +383,11 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const { if (GlobalC::exx_info.info_ri.real_number) { - this->exx_nao.exd->exx_eachiterinit(istep, ucell, this->dmat.dm, this->kv, iter); + this->exx_nao.exd->exx_eachiterinit(istep, ucell, *this->dmat.dm, this->kv, iter); } else { - this->exx_nao.exc->exx_eachiterinit(istep, ucell, this->dmat.dm, this->kv, iter); + this->exx_nao.exc->exx_eachiterinit(istep, ucell, *this->dmat.dm, this->kv, iter); } } #endif diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index fecfe93197..295146280b 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -49,9 +49,9 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * if (GlobalC::exx_info.info_global.cal_exx) { GlobalC::exx_info.info_ri.real_number ? - exx_nao.exd->exx_iter_finish(kv, ucell, *p_hamilt, *pelec, + exx_nao.exd->exx_iter_finish(kv, ucell, *p_hamilt, *pelec, &dm, *p_chgmix, scf_ene_thr, iter, istep, conv_esolver) : - exx_nao.exc->exx_iter_finish(kv, ucell, *p_hamilt, *pelec, + exx_nao.exc->exx_iter_finish(kv, ucell, *p_hamilt, *pelec, &dm, *p_chgmix, scf_ene_thr, iter, istep, conv_esolver); } } diff --git a/source/source_io/get_wf_lcao.cpp b/source/source_io/get_wf_lcao.cpp index 7d6cd0d15c..6cafa72d60 100644 --- a/source/source_io/get_wf_lcao.cpp +++ b/source/source_io/get_wf_lcao.cpp @@ -480,6 +480,7 @@ template int Get_wf_lcao::set_wfc_grid(const int naroc[2], const double* in, double** out, const std::vector& trace_lo); + template int Get_wf_lcao::set_wfc_grid(const int naroc[2], const int nb, const int dim0, diff --git a/source/source_lcao/module_ri/Exx_LRI_interface.h b/source/source_lcao/module_ri/Exx_LRI_interface.h index ff76dbbc7d..15302677b5 100644 --- a/source/source_lcao/module_ri/Exx_LRI_interface.h +++ b/source/source_lcao/module_ri/Exx_LRI_interface.h @@ -4,6 +4,7 @@ #include "Exx_LRI.h" #include "source_lcao/module_ri/Mix_DMk_2D.h" #include "source_lcao/module_ri/module_exx_symmetry/symmetry_rotation.h" +#include "source_estate/module_dm/density_matrix.h" // mohan add 2025-11-04 #include class LCAO_Matrix; @@ -96,7 +97,8 @@ class Exx_LRI_Interface void exx_iter_finish(const K_Vectors& kv, const UnitCell& ucell, hamilt::Hamilt& hamilt, - elecstate::ElecState& elec, + elecstate::ElecState& elec, + elecstate::DensityMatrix* dm, // mohan add 2025-11-04 Charge_Mixing& chgmix, const double& scf_ene_thr, int& iter, diff --git a/source/source_lcao/module_ri/Exx_LRI_interface.hpp b/source/source_lcao/module_ri/Exx_LRI_interface.hpp index 89db3e5417..802f02a4f8 100644 --- a/source/source_lcao/module_ri/Exx_LRI_interface.hpp +++ b/source/source_lcao/module_ri/Exx_LRI_interface.hpp @@ -262,14 +262,15 @@ void Exx_LRI_Interface::exx_hamilt2rho(elecstate::ElecState& elec, con template void Exx_LRI_Interface::exx_iter_finish(const K_Vectors& kv, - const UnitCell& ucell, - hamilt::Hamilt& hamilt, - elecstate::ElecState& elec, - Charge_Mixing& chgmix, - const double& scf_ene_thr, - int& iter, - const int istep, - bool& conv_esolver) + const UnitCell& ucell, + hamilt::Hamilt& hamilt, + elecstate::ElecState& elec, + elecstate::DensityMatrix* dm, // mohan add 2025-11-04 + Charge_Mixing& chgmix, + const double& scf_ene_thr, + int& iter, + const int istep, + bool& conv_esolver) { ModuleBase::TITLE("Exx_LRI_Interface","exx_iter_finish"); if (GlobalC::restart.info_save.save_H && (this->two_level_step > 0 || istep > 0) @@ -312,11 +313,12 @@ void Exx_LRI_Interface::exx_iter_finish(const K_Vectors& kv, { chgmix.close_kerker_gg0(); } - this->dm_last_step = dynamic_cast*>(&elec)->get_DM(); +// mohan update 2025-11-04 + this->dm_last_step = dm; conv_esolver = this->exx_after_converge( ucell, hamilt, - *dynamic_cast*>(&elec)->get_DM(), + *dm, kv, PARAM.inp.nspin, iter, From 1af42b8d0d0b2f56e75bc094d569d90a6abc3b0b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 15:31:52 +0800 Subject: [PATCH 61/81] fix FORCE_STRESS --- source/source_lcao/FORCE_STRESS.cpp | 26 ++++++++++++++++++++++++++ source/source_lcao/FORCE_STRESS.h | 24 ------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/source/source_lcao/FORCE_STRESS.cpp b/source/source_lcao/FORCE_STRESS.cpp index e885eb9559..d5ab8cb0c6 100644 --- a/source/source_lcao/FORCE_STRESS.cpp +++ b/source/source_lcao/FORCE_STRESS.cpp @@ -852,3 +852,29 @@ void Force_Stress_LCAO::forceSymmetry(const UnitCell& ucell, ModuleBase::matr template class Force_Stress_LCAO; template class Force_Stress_LCAO>; + +template <> +void assign_dmk_ptr( + elecstate::DensityMatrix* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +) { + auto& dmk_tmp = dm->get_DMK_vector(); + dmk_d = &dmk_tmp; + dmk_c = nullptr; +} + +template <> +void assign_dmk_ptr>( + elecstate::DensityMatrix,double>* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +) { + auto& dmk_tmp = dm->get_DMK_vector(); + dmk_c = &dmk_tmp; + dmk_d = nullptr; +} + + diff --git a/source/source_lcao/FORCE_STRESS.h b/source/source_lcao/FORCE_STRESS.h index 8a387e42cb..b62dd1460b 100644 --- a/source/source_lcao/FORCE_STRESS.h +++ b/source/source_lcao/FORCE_STRESS.h @@ -127,28 +127,4 @@ void assign_dmk_ptr( bool gamma_only_local ); -template <> -void assign_dmk_ptr( - elecstate::DensityMatrix* dm, - std::vector>*& dmk_d, - std::vector>>*& dmk_c, - bool gamma_only_local -) { - auto& dmk_tmp = dm->get_DMK_vector(); - dmk_d = &dmk_tmp; - dmk_c = nullptr; -} - -template <> -void assign_dmk_ptr>( - elecstate::DensityMatrix,double>* dm, - std::vector>*& dmk_d, - std::vector>>*& dmk_c, - bool gamma_only_local -) { - auto& dmk_tmp = dm->get_DMK_vector(); - dmk_c = &dmk_tmp; - dmk_d = nullptr; -} - #endif From 93fd786460b2108342e0d20e232ee49937a498ca Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 16:00:31 +0800 Subject: [PATCH 62/81] fix 2 --- source/source_lcao/module_deltaspin/init_sc.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/source_lcao/module_deltaspin/init_sc.cpp b/source/source_lcao/module_deltaspin/init_sc.cpp index 9b2ee0483f..ac56047173 100644 --- a/source/source_lcao/module_deltaspin/init_sc.cpp +++ b/source/source_lcao/module_deltaspin/init_sc.cpp @@ -34,7 +34,9 @@ void spinconstrain::SpinConstrain::init_sc(double sc_thr_in, this->set_decay_grad(); if(ParaV_in != nullptr) this->set_ParaV(ParaV_in); this->set_solver_parameters(kv_in, p_hamilt_in, psi_in, pelec_in); +#ifdef __LCAO this->dm_ = dm_in; // mohan add 2025-11-03 +#endif } template class spinconstrain::SpinConstrain>; From 1b4f8b6cb9684a6d8303949e9670417d733c91dd Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 16:16:51 +0800 Subject: [PATCH 63/81] fix 3 --- source/source_lcao/FORCE_STRESS.cpp | 54 +++++++++++++++-------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/source/source_lcao/FORCE_STRESS.cpp b/source/source_lcao/FORCE_STRESS.cpp index d5ab8cb0c6..54fe92ed9a 100644 --- a/source/source_lcao/FORCE_STRESS.cpp +++ b/source/source_lcao/FORCE_STRESS.cpp @@ -22,6 +22,34 @@ #include "source_lcao/module_operator_lcao/dspin_lcao.h" #include "source_lcao/module_operator_lcao/nonlocal_new.h" + +// mohan add 2025-11-04 +template <> +void assign_dmk_ptr( + elecstate::DensityMatrix* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +) { + auto& dmk_tmp = dm->get_DMK_vector(); + dmk_d = &dmk_tmp; + dmk_c = nullptr; +} + +template <> +void assign_dmk_ptr>( + elecstate::DensityMatrix,double>* dm, + std::vector>*& dmk_d, + std::vector>>*& dmk_c, + bool gamma_only_local +) { + auto& dmk_tmp = dm->get_DMK_vector(); + dmk_c = &dmk_tmp; + dmk_d = nullptr; +} + + + template Force_Stress_LCAO::Force_Stress_LCAO(Record_adj& ra, const int nat_in) : RA(&ra), nat(nat_in) { @@ -852,29 +880,3 @@ void Force_Stress_LCAO::forceSymmetry(const UnitCell& ucell, ModuleBase::matr template class Force_Stress_LCAO; template class Force_Stress_LCAO>; - -template <> -void assign_dmk_ptr( - elecstate::DensityMatrix* dm, - std::vector>*& dmk_d, - std::vector>>*& dmk_c, - bool gamma_only_local -) { - auto& dmk_tmp = dm->get_DMK_vector(); - dmk_d = &dmk_tmp; - dmk_c = nullptr; -} - -template <> -void assign_dmk_ptr>( - elecstate::DensityMatrix,double>* dm, - std::vector>*& dmk_d, - std::vector>>*& dmk_c, - bool gamma_only_local -) { - auto& dmk_tmp = dm->get_DMK_vector(); - dmk_c = &dmk_tmp; - dmk_d = nullptr; -} - - From 600421ce34c0b6d2d5c1ca79d3460cd3c0bfd967 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 19:31:28 +0800 Subject: [PATCH 64/81] fix 4 --- source/source_esolver/esolver_ks_lcao_tddft.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao_tddft.cpp b/source/source_esolver/esolver_ks_lcao_tddft.cpp index d293d33093..d608082ddd 100644 --- a/source/source_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/source_esolver/esolver_ks_lcao_tddft.cpp @@ -408,7 +408,7 @@ void ESolver_KS_LCAO_TDDFT::store_h_s_psi(UnitCell& ucell, BlasConnector::copy(len_HS_ik, h_mat_g.p.get(), 1, - this->Hk_laststep.data>() + ik * len_HS_ik, + this->Hk_laststep.template data>() + ik * len_HS_ik, 1); // Collect S matrix @@ -416,7 +416,7 @@ void ESolver_KS_LCAO_TDDFT::store_h_s_psi(UnitCell& ucell, BlasConnector::copy(len_HS_ik, s_mat_g.p.get(), 1, - this->Sk_laststep.data>() + ik * len_HS_ik, + this->Sk_laststep.template data>() + ik * len_HS_ik, 1); #endif } @@ -425,12 +425,12 @@ void ESolver_KS_LCAO_TDDFT::store_h_s_psi(UnitCell& ucell, BlasConnector::copy(len_HS_ik, h_mat.p, 1, - this->Hk_laststep.data>() + ik * len_HS_ik, + this->Hk_laststep.template data>() + ik * len_HS_ik, 1); BlasConnector::copy(len_HS_ik, s_mat.p, 1, - this->Sk_laststep.data>() + ik * len_HS_ik, + this->Sk_laststep.template data>() + ik * len_HS_ik, 1); } // end use_tensor } // end ik From 8bd559a27f3a1e83d7d2776daa8436cb98fa868d Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 20:15:12 +0800 Subject: [PATCH 65/81] fix 5 --- source/source_esolver/esolver_double_xc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_esolver/esolver_double_xc.cpp b/source/source_esolver/esolver_double_xc.cpp index 66536efc36..082271ee2e 100644 --- a/source/source_esolver/esolver_double_xc.cpp +++ b/source/source_esolver/esolver_double_xc.cpp @@ -233,7 +233,7 @@ void ESolver_DoubleXC::iter_finish(UnitCell& ucell, const int istep, int this->gd, &(this->pv), *(this->psi), - this->dmat_base.dm, + this->dmat.dm, p_ham_deepks, iter, conv_esolver, From f3d99861640507d8c8636d7bb8190efefeffec4a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 20:24:27 +0800 Subject: [PATCH 66/81] improve esolver_ks_lcao --- source/source_esolver/esolver_ks_lcao.cpp | 11 +---------- source/source_io/ctrl_scf_lcao.cpp | 11 +++++------ source/source_io/ctrl_scf_lcao.h | 4 ++-- source/source_io/output_mulliken.h | 1 - 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index b7b023234f..f25353cb6f 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -574,14 +574,8 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf"); ModuleBase::timer::tick("ESolver_KS_LCAO", "after_scf"); - auto* estate = dynamic_cast*>(this->pelec); auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); - if(!estate) - { - 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"); @@ -597,11 +591,8 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const //! 2) output of lcao every few ionic steps - - - ModuleIO::ctrl_scf_lcao(ucell, - PARAM.inp, this->kv, estate, this->dmat.dm, this->pv, + PARAM.inp, this->kv, this->pelec, this->dmat.dm, this->pv, this->gd, this->psi, hamilt_lcao, this->two_center_bundle_, this->orb_, this->pw_wfc, this->pw_rho, diff --git a/source/source_io/ctrl_scf_lcao.cpp b/source/source_io/ctrl_scf_lcao.cpp index 2d28b1bc2e..f66fd2720b 100644 --- a/source/source_io/ctrl_scf_lcao.cpp +++ b/source/source_io/ctrl_scf_lcao.cpp @@ -36,8 +36,8 @@ template void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, - elecstate::ElecStateLCAO* pelec, - elecstate::DensityMatrix* dm, // mohan add 2025-11-04 + elecstate::ElecState* pelec, + elecstate::DensityMatrix* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, psi::Psi* psi, @@ -299,7 +299,6 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell, ModuleIO::cal_mag(&pv, p_hamilt, kv, - pelec, dm, // mohan add 2025-11-04 two_center_bundle, orb, @@ -469,7 +468,7 @@ template void ModuleIO::ctrl_scf_lcao( UnitCell& ucell, const Input_para& inp, K_Vectors& kv, - elecstate::ElecStateLCAO* pelec, + elecstate::ElecState* pelec, elecstate::DensityMatrix* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, @@ -493,7 +492,7 @@ template void ModuleIO::ctrl_scf_lcao, double>( UnitCell& ucell, const Input_para& inp, K_Vectors& kv, - elecstate::ElecStateLCAO>* pelec, + elecstate::ElecState* pelec, elecstate::DensityMatrix,double>* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, @@ -516,7 +515,7 @@ template void ModuleIO::ctrl_scf_lcao, std::complex UnitCell& ucell, const Input_para& inp, K_Vectors& kv, - elecstate::ElecStateLCAO>* pelec, + elecstate::ElecState* pelec, elecstate::DensityMatrix,double>* dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, diff --git a/source/source_io/ctrl_scf_lcao.h b/source/source_io/ctrl_scf_lcao.h index 9eac6600d4..9dd381cd2a 100644 --- a/source/source_io/ctrl_scf_lcao.h +++ b/source/source_io/ctrl_scf_lcao.h @@ -5,7 +5,7 @@ #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_estate/elecstate.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 @@ -24,7 +24,7 @@ namespace ModuleIO void ctrl_scf_lcao(UnitCell& ucell, const Input_para& inp, K_Vectors& kv, - elecstate::ElecStateLCAO* pelec, + elecstate::ElecState* pelec, elecstate::DensityMatrix *dm, // mohan add 2025-11-04 Parallel_Orbitals& pv, Grid_Driver& gd, diff --git a/source/source_io/output_mulliken.h b/source/source_io/output_mulliken.h index d072a7c837..42c3cdc550 100644 --- a/source/source_io/output_mulliken.h +++ b/source/source_io/output_mulliken.h @@ -90,7 +90,6 @@ template void cal_mag(Parallel_Orbitals* pv, hamilt::Hamilt* p_ham, K_Vectors& kv, - elecstate::ElecState* pelec, elecstate::DensityMatrix* dm, // mohan add 2025-11-04 const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, From 8789c9fc41299eaba29148b1c237fd0fab464342 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 20:51:58 +0800 Subject: [PATCH 67/81] improve again --- source/source_esolver/esolver_ks_lcao.cpp | 20 ++++---------------- source/source_io/ctrl_iter_lcao.cpp | 8 ++++---- source/source_io/ctrl_iter_lcao.h | 2 +- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index f25353cb6f..2df5eb6e34 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -496,14 +496,8 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& { ModuleBase::TITLE("ESolver_KS_LCAO", "iter_finish"); - 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"); @@ -561,11 +555,10 @@ void ESolver_KS_LCAO::iter_finish(UnitCell& ucell, const int istep, int& } // control the output related to the finished iteration - ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, estate, *this->dmat.dm, + ModuleIO::ctrl_iter_lcao(ucell, PARAM.inp, this->kv, this->pelec, *this->dmat.dm, this->pv, this->gd, this->psi, this->chr, this->p_chgmix, hamilt_lcao, this->orb_, this->deepks, this->exx_nao, iter, istep, conv_esolver, this->scf_ene_thr); - } template @@ -589,18 +582,13 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const //! 1) call after_scf() of ESolver_KS ESolver_KS::after_scf(ucell, istep, conv_esolver); - //! 2) output of lcao every few ionic steps ModuleIO::ctrl_scf_lcao(ucell, PARAM.inp, this->kv, this->pelec, this->dmat.dm, this->pv, - this->gd, this->psi, hamilt_lcao, - this->two_center_bundle_, - this->orb_, this->pw_wfc, this->pw_rho, - this->pw_big, this->sf, + this->gd, this->psi, hamilt_lcao, this->two_center_bundle_, + this->orb_, this->pw_wfc, this->pw_rho, this->pw_big, this->sf, this->rdmft_solver, this->deepks, this->exx_nao, - this->conv_esolver, this->scf_nmax_flag, - istep); - + this->conv_esolver, this->scf_nmax_flag, istep); //! 3) Clean up RA, which is used to serach for adjacent atoms if (!PARAM.inp.cal_force && !PARAM.inp.cal_stress) diff --git a/source/source_io/ctrl_iter_lcao.cpp b/source/source_io/ctrl_iter_lcao.cpp index 295146280b..c772d162e6 100644 --- a/source/source_io/ctrl_iter_lcao.cpp +++ b/source/source_io/ctrl_iter_lcao.cpp @@ -13,7 +13,7 @@ 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 * + elecstate::ElecState* pelec, // electronic info * elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * @@ -82,7 +82,7 @@ void ctrl_iter_lcao(UnitCell& ucell, // unit cell * 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 * + elecstate::ElecState* pelec, // electronic info * elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * @@ -102,7 +102,7 @@ template void ctrl_iter_lcao(UnitCell& ucell, // unit cell * 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 * + elecstate::ElecState* pelec, // electronic info * elecstate::DensityMatrix, double>& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * @@ -122,7 +122,7 @@ template void ctrl_iter_lcao, double>(UnitCell& ucell, // u 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 * + elecstate::ElecState* pelec, // electronic info * elecstate::DensityMatrix, double>& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * diff --git a/source/source_io/ctrl_iter_lcao.h b/source/source_io/ctrl_iter_lcao.h index 05c02d10b5..6297d4f1d0 100644 --- a/source/source_io/ctrl_iter_lcao.h +++ b/source/source_io/ctrl_iter_lcao.h @@ -18,7 +18,7 @@ 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 * + elecstate::ElecState* pelec, // electronic info * elecstate::DensityMatrix& dm, // density matrix, mohan add 2025-11-03 Parallel_Orbitals& pv, // parallel orbital info * Grid_Driver& gd, // adjacent atom info * From 4d2cf41643335ba69b77837c2cf01b0b947c3a28 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 21:05:47 +0800 Subject: [PATCH 68/81] comment out psi2rho in init_dm --- source/source_estate/module_dm/init_dm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_estate/module_dm/init_dm.cpp b/source/source_estate/module_dm/init_dm.cpp index 0b5d6e86b6..113c71e1bc 100644 --- a/source/source_estate/module_dm/init_dm.cpp +++ b/source/source_estate/module_dm/init_dm.cpp @@ -34,7 +34,7 @@ void elecstate::init_dm(UnitCell& ucell, elecstate::cal_dm_psi(dmat.dm->get_paraV_pointer(), pelec->wg, *psi, *dmat.dm); dmat.dm->cal_DMR(); - pelec->psiToRho(*psi); +// pelec->psiToRho(*psi); // I found this sentence is useless, mohan add 2025-11-04 pelec->skip_weights = false; elecstate::cal_ux(ucell); From aef0c12e349105579ffeb802cc35b36b2a632eeb Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 21:24:49 +0800 Subject: [PATCH 69/81] clean esolver_ks_lcao --- source/source_esolver/esolver_ks_lcao.cpp | 31 +++++----------------- source/source_estate/module_dm/init_dm.cpp | 6 ++--- source/source_estate/module_dm/init_dm.h | 4 +-- source/source_io/ctrl_runner_lcao.cpp | 8 +++--- source/source_io/ctrl_runner_lcao.h | 4 +-- 5 files changed, 17 insertions(+), 36 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index 2df5eb6e34..b3f83c42b8 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -211,8 +211,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) // 12) init_scf, should be before_scf? mohan add 2025-03-10 this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm); - // 13) initalize DMR - // DMR should be same size with Hamiltonian(R) + // 13) initalize DM(R), which has the same size with Hamiltonian(R) auto* hamilt_lcao = dynamic_cast*>(this->p_hamilt); if(!hamilt_lcao) { @@ -221,7 +220,7 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) this->dmat.dm->init_DMR(*hamilt_lcao->getHR()); #ifdef __MLALGO - // 14) initialize DMR of DeePKS + // 14) initialize DM2(R) of DeePKS, the DM2(R) is different from DM(R) this->deepks.ld.init_DMR(ucell, orb_, this->pv, this->gd); #endif @@ -257,8 +256,6 @@ void ESolver_KS_LCAO::before_scf(UnitCell& ucell, const int istep) template double ESolver_KS_LCAO::cal_energy() { - ModuleBase::TITLE("ESolver_KS_LCAO", "cal_energy"); - return this->pelec->f_en.etot; } @@ -294,15 +291,13 @@ void ESolver_KS_LCAO::cal_stress(UnitCell& ucell, ModuleBase::matrix& st ModuleBase::TITLE("ESolver_KS_LCAO", "cal_stress"); ModuleBase::timer::tick("ESolver_KS_LCAO", "cal_stress"); - // if the users do not want to calculate forces but want stress, - // we call cal_force if (!this->have_force) { ModuleBase::matrix fcs; this->cal_force(ucell, fcs); } - // the 'scs' stress has already been calculated in 'cal_force' + // the stress has been calculated in 'cal_force' stress = this->scs; this->have_force = false; @@ -319,21 +314,14 @@ void ESolver_KS_LCAO::after_all_runners(UnitCell& ucell) const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1; - 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->dmat, this->pv, this->Pgrid, + PARAM.inp, this->kv, this->pelec, this->dmat, this->pv, this->Pgrid, this->gd, this->psi, this->chr, hamilt_lcao, this->two_center_bundle_, this->orb_, this->pw_rho, this->pw_rhod, @@ -350,17 +338,10 @@ 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); - // cast pointers - auto* estate = dynamic_cast*>(this->pelec); - if(!estate) - { - ModuleBase::WARNING_QUIT("ESolver_KS_LCAO::iter_init","pelec does not exist"); - } - module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, this->dmat.dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp); // mohan update 2012-06-05 - estate->f_en.deband_harris = estate->cal_delta_eband(ucell); + this->pelec->f_en.deband_harris = this->pelec->cal_delta_eband(ucell); if (istep == 0 && PARAM.inp.init_wfc == "file") { @@ -374,7 +355,7 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const this->exx_nao.exd->two_level_step : this->exx_nao.exc->two_level_step; } #endif - elecstate::init_dm(ucell, estate, this->dmat, this->psi, this->chr, iter, exx_two_level_step); + elecstate::init_dm(ucell, this->pelec, this->dmat, this->psi, this->chr, iter, exx_two_level_step); } #ifdef __EXX diff --git a/source/source_estate/module_dm/init_dm.cpp b/source/source_estate/module_dm/init_dm.cpp index 113c71e1bc..669360ee1e 100644 --- a/source/source_estate/module_dm/init_dm.cpp +++ b/source/source_estate/module_dm/init_dm.cpp @@ -5,7 +5,7 @@ template void elecstate::init_dm(UnitCell& ucell, - elecstate::ElecStateLCAO* pelec, + elecstate::ElecState* pelec, LCAO_domain::Setup_DM &dmat, psi::Psi* psi, Charge &chr, @@ -51,7 +51,7 @@ void elecstate::init_dm(UnitCell& ucell, template void elecstate::init_dm(UnitCell& ucell, - elecstate::ElecStateLCAO* pelec, + elecstate::ElecState* pelec, LCAO_domain::Setup_DM &dmat, psi::Psi* psi, Charge &chr, @@ -59,7 +59,7 @@ template void elecstate::init_dm(UnitCell& ucell, const int exx_two_level_step); template void elecstate::init_dm>(UnitCell& ucell, - elecstate::ElecStateLCAO>* pelec, + elecstate::ElecState* pelec, LCAO_domain::Setup_DM> &dmat, psi::Psi>* psi, Charge &chr, diff --git a/source/source_estate/module_dm/init_dm.h b/source/source_estate/module_dm/init_dm.h index 75bc7aeeed..2fd969638d 100644 --- a/source/source_estate/module_dm/init_dm.h +++ b/source/source_estate/module_dm/init_dm.h @@ -2,7 +2,7 @@ #define INIT_DM_H #include "source_cell/unitcell.h" // use unitcell -#include "source_estate/elecstate_lcao.h"// use ElecStateLCAO +#include "source_estate/elecstate.h"// use ElecState #include "source_psi/psi.h" // use electronic wave functions #include "source_estate/module_charge/charge.h" // use charge #include "source_lcao/setup_dm.h" // define Setup_DM @@ -12,7 +12,7 @@ namespace elecstate template void init_dm(UnitCell& ucell, - ElecStateLCAO* pelec, + ElecState* pelec, LCAO_domain::Setup_DM &dmat, psi::Psi* psi, Charge &chr, diff --git a/source/source_io/ctrl_runner_lcao.cpp b/source/source_io/ctrl_runner_lcao.cpp index ab5c4e1280..9a83e9f3d2 100644 --- a/source/source_io/ctrl_runner_lcao.cpp +++ b/source/source_io/ctrl_runner_lcao.cpp @@ -16,7 +16,7 @@ template void ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point - elecstate::ElecStateLCAO* pelec,// electronic info + elecstate::ElecState* pelec,// electronic info const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info @@ -136,7 +136,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point - elecstate::ElecStateLCAO* pelec,// electronic info + elecstate::ElecState* pelec,// electronic info const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info @@ -157,7 +157,7 @@ template void ModuleIO::ctrl_runner_lcao(UnitCell& ucell, / template void ctrl_runner_lcao, double>(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point - elecstate::ElecStateLCAO>* pelec,// electronic info + elecstate::ElecState* pelec,// electronic info const LCAO_domain::Setup_DM> &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info @@ -178,7 +178,7 @@ template void ctrl_runner_lcao, double>(UnitCell& ucell, 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 + elecstate::ElecState* pelec,// electronic info const LCAO_domain::Setup_DM> &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info diff --git a/source/source_io/ctrl_runner_lcao.h b/source/source_io/ctrl_runner_lcao.h index 0ea631080f..95c4fe4658 100644 --- a/source/source_io/ctrl_runner_lcao.h +++ b/source/source_io/ctrl_runner_lcao.h @@ -3,7 +3,7 @@ #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_estate/elecstate.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 @@ -17,7 +17,7 @@ template void ctrl_runner_lcao(UnitCell& ucell, // unitcell const Input_para &inp, // input K_Vectors &kv, // k-point - elecstate::ElecStateLCAO* pelec,// electronic info + elecstate::ElecState* pelec,// electronic info const LCAO_domain::Setup_DM &dmat, // mohan add 2025-11-02 Parallel_Orbitals &pv, // orbital info Parallel_Grid &pgrid, // grid info From 0ba139b08e6f1282536ff4b3f28e9a210d7a687e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 22:03:09 +0800 Subject: [PATCH 70/81] update structure_factor --- source/source_esolver/esolver_fp.cpp | 4 +-- source/source_esolver/esolver_ks.cpp | 2 +- source/source_esolver/esolver_of.cpp | 2 +- .../module_charge/charge_extra.cpp | 6 ++-- .../module_surchem/cal_pseudo.cpp | 3 +- .../module_pwdft/structure_factor.cpp | 32 ++++++++++++------- .../source_pw/module_pwdft/structure_factor.h | 25 +++++++++++---- 7 files changed, 47 insertions(+), 27 deletions(-) diff --git a/source/source_esolver/esolver_fp.cpp b/source/source_esolver/esolver_fp.cpp index ef2258de36..cca74c26a5 100644 --- a/source/source_esolver/esolver_fp.cpp +++ b/source/source_esolver/esolver_fp.cpp @@ -54,9 +54,7 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp) // write geometry file ModuleIO::CifParser::write(PARAM.globalv.global_out_dir + "STRU.cif", - ucell, - "# Generated by ABACUS ModuleIO::CifParser", - "data_?"); + ucell, "# Generated by ABACUS ModuleIO::CifParser", "data_?"); // init charge extrapolation this->CE.Init_CE(inp.nspin, ucell.nat, this->pw_rhod->nrxx, inp.chg_extrap); diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index f85fd53f43..776a9dc3bd 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -99,7 +99,7 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para this->pw_rhod->nplane, this->pw_rhod->nrxx, pw_big->nbz, pw_big->bz); //! 11) calculate the structure factor - this->sf.setup_structure_factor(&ucell, Pgrid, this->pw_rhod); + this->sf.setup(&ucell, Pgrid, this->pw_rhod); } template diff --git a/source/source_esolver/esolver_of.cpp b/source/source_esolver/esolver_of.cpp index 6785c64a1f..e3fad24322 100644 --- a/source/source_esolver/esolver_of.cpp +++ b/source/source_esolver/esolver_of.cpp @@ -105,7 +105,7 @@ void ESolver_OF::before_all_runners(UnitCell& ucell, const Input_para& inp) pw_big->nbz, pw_big->bz); // mohan add 2010-07-22, update 2011-05-04 // Calculate Structure factor - sf.setup_structure_factor(&ucell, Pgrid, pw_rho); + sf.setup(&ucell, Pgrid, pw_rho); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS"); // initialize local pseudopotential diff --git a/source/source_estate/module_charge/charge_extra.cpp b/source/source_estate/module_charge/charge_extra.cpp index 345a34b723..dd7bea8344 100644 --- a/source/source_estate/module_charge/charge_extra.cpp +++ b/source/source_estate/module_charge/charge_extra.cpp @@ -107,7 +107,7 @@ void Charge_Extra::extrapolate_charge( rho_extr = std::min(istep, pot_order); if(rho_extr == 0) { - sf->setup_structure_factor(&ucell, *Pgrid, chr->rhopw); + sf->setup(&ucell, *Pgrid, chr->rhopw); ofs_running << " charge density from previous step !" << std::endl; return; } @@ -169,7 +169,7 @@ void Charge_Extra::extrapolate_charge( } } - sf->setup_structure_factor(&ucell, *Pgrid, chr->rhopw); + sf->setup(&ucell, *Pgrid, chr->rhopw); double** rho_atom = new double*[this->nspin]; for (int is = 0; is < this->nspin; is++) { @@ -314,4 +314,4 @@ void Charge_Extra::update_delta_rho(const UnitCell& ucell, const Charge* chr, co } delete[] rho_atom; return; -} \ No newline at end of file +} diff --git a/source/source_hamilt/module_surchem/cal_pseudo.cpp b/source/source_hamilt/module_surchem/cal_pseudo.cpp index 1e0a64c5b5..9f707654d0 100644 --- a/source/source_hamilt/module_surchem/cal_pseudo.cpp +++ b/source/source_hamilt/module_surchem/cal_pseudo.cpp @@ -9,7 +9,8 @@ void surchem::gauss_charge(const UnitCell& cell, std::complex* N, Structure_Factor* sf) { - sf->setup_structure_factor(&cell, pgrid, rho_basis); // call strucFac(ntype,ngmc) + sf->setup(&cell, pgrid, rho_basis); // this is strange, should be removed to other places, mohan add 2025-11-04 + const int ig0 = rho_basis->ig_gge0; // G=0 index for (int it = 0; it < cell.ntype; it++) { diff --git a/source/source_pw/module_pwdft/structure_factor.cpp b/source/source_pw/module_pwdft/structure_factor.cpp index b97bdee944..d49cc75360 100644 --- a/source/source_pw/module_pwdft/structure_factor.cpp +++ b/source/source_pw/module_pwdft/structure_factor.cpp @@ -46,7 +46,7 @@ Structure_Factor::~Structure_Factor() // called in input.cpp void Structure_Factor::set(const ModulePW::PW_Basis* rho_basis_in, const int& nbspline_in) { - ModuleBase::TITLE("PW_Basis","set"); + ModuleBase::TITLE("Structure_Factor","set"); this->rho_basis = rho_basis_in; this->nbspline = nbspline_in; return; @@ -54,10 +54,11 @@ void Structure_Factor::set(const ModulePW::PW_Basis* rho_basis_in, const int& nb // Peize Lin optimize and add OpenMP 2021.04.01 // Calculate structure factor -void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const Parallel_Grid& pgrid, const ModulePW::PW_Basis* rho_basis) +void Structure_Factor::setup(const UnitCell* Ucell, const Parallel_Grid& pgrid, const ModulePW::PW_Basis* rho_basis) { - ModuleBase::TITLE("PW_Basis","setup_structure_factor"); - ModuleBase::timer::tick("PW_Basis","setup_struc_factor"); + ModuleBase::TITLE("Structure_Factor","setup"); + ModuleBase::timer::tick("Structure_Factor","setup"); + const std::complex ci_tpi = ModuleBase::NEG_IMAG_UNIT * ModuleBase::TWO_PI; this->ucell = Ucell; this->strucFac.create(Ucell->ntype, rho_basis->npw); @@ -66,9 +67,15 @@ void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const Paral // std::string outstr; // outstr = PARAM.globalv.global_out_dir + "strucFac.dat"; // std::ofstream ofs( outstr.c_str() ) ; - bool usebspline; - if(nbspline > 0) { usebspline = true; - } else { usebspline = false;} + bool usebspline; + if(nbspline > 0) + { + usebspline = true; + } + else + { + usebspline = false; + } if(usebspline) { @@ -100,12 +107,15 @@ void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const Paral // ofs.close(); - int i,j; //ng; + int i=0; + int j=0; + this->eigts1.create(Ucell->nat, 2*rho_basis->nx + 1); this->eigts2.create(Ucell->nat, 2*rho_basis->ny + 1); this->eigts3.create(Ucell->nat, 2*rho_basis->nz + 1); - ModuleBase::Memory::record("SF::eigts123",sizeof(std::complex) * (Ucell->nat*2 * (rho_basis->nx + rho_basis->ny + rho_basis->nz) + 3)); + ModuleBase::Memory::record("SF::eigts123",sizeof(std::complex) + * (Ucell->nat*2 * (rho_basis->nx + rho_basis->ny + rho_basis->nz) + 3)); ModuleBase::Vector3 gtau; int inat = 0; @@ -177,7 +187,7 @@ void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const Paral this->z_eigts3 = this->eigts3.c; // There's no need to delete double precision pointers while in a CPU environment. } - ModuleBase::timer::tick("PW_Basis","setup_struc_factor"); + ModuleBase::timer::tick("Structure_Factor","setup"); return; } @@ -379,4 +389,4 @@ template <> std::complex * Structure_Factor::get_eigts3_data() const { return this->z_eigts3; -} \ No newline at end of file +} diff --git a/source/source_pw/module_pwdft/structure_factor.h b/source/source_pw/module_pwdft/structure_factor.h index 36e9185400..6de0a5b0e3 100644 --- a/source/source_pw/module_pwdft/structure_factor.h +++ b/source/source_pw/module_pwdft/structure_factor.h @@ -1,5 +1,5 @@ -#ifndef PLANEWAVE_H -#define PLANEWAVE_H +#ifndef STRUCTURE_FACTOR_H +#define STRUCTURE_FACTOR_H #include "source_base/complexmatrix.h" #include "source_basis/module_pw/pw_basis_k.h" @@ -23,9 +23,10 @@ class Structure_Factor // structure factor (ntype, ngmc) ModuleBase::ComplexMatrix strucFac; - void setup_structure_factor(const UnitCell* Ucell, - const Parallel_Grid& pgrid, - const ModulePW::PW_Basis* rho_basis); // Calculate structure factors + + void setup(const UnitCell* Ucell, + const Parallel_Grid& pgrid, + const ModulePW::PW_Basis* rho_basis); // Calculate structure factors /// calculate structure factors through Cardinal B-spline interpolation void bspline_sf( @@ -33,6 +34,7 @@ class Structure_Factor const UnitCell* Ucell, const Parallel_Grid& pgrid, const ModulePW::PW_Basis* rho_basis); + void bsplinecoef(std::complex *b1, std::complex *b2, std::complex *b3, const int nx, const int ny, const int nz, const int norder); @@ -51,7 +53,9 @@ class Structure_Factor // sf with k points std::complex* get_sk(const int ik, const int it, const int ia, const ModulePW::PW_Basis_K* wfc_basis) const; template + void get_sk(Device* ctx, const int ik, const ModulePW::PW_Basis_K* wfc_basis, std::complex* sk) const; + std::complex* get_skq(int ik, int it, int ia, @@ -59,9 +63,16 @@ class Structure_Factor ModuleBase::Vector3 q); private: + const UnitCell* ucell=nullptr; - std::complex * c_eigts1 = nullptr, * c_eigts2 = nullptr, * c_eigts3 = nullptr; - std::complex * z_eigts1 = nullptr, * z_eigts2 = nullptr, * z_eigts3 = nullptr; + std::complex * c_eigts1 = nullptr; + std::complex * c_eigts2 = nullptr; + std::complex * c_eigts3 = nullptr; + + std::complex * z_eigts1 = nullptr; + std::complex * z_eigts2 = nullptr; + std::complex * z_eigts3 = nullptr; + const ModulePW::PW_Basis* rho_basis = nullptr; std::string device = "cpu"; }; From 3f77d9796098b8c3d01dd6448e990eea1000ed55 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 4 Nov 2025 22:04:41 +0800 Subject: [PATCH 71/81] update sf --- source/source_pw/module_pwdft/structure_factor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_pw/module_pwdft/structure_factor.cpp b/source/source_pw/module_pwdft/structure_factor.cpp index d49cc75360..2823d11e36 100644 --- a/source/source_pw/module_pwdft/structure_factor.cpp +++ b/source/source_pw/module_pwdft/structure_factor.cpp @@ -308,7 +308,7 @@ void Structure_Factor::bspline_sf(const int norder, return; } -void Structure_Factor:: bsplinecoef(std::complex *b1, std::complex *b2, std::complex *b3, +void Structure_Factor::bsplinecoef(std::complex *b1, std::complex *b2, std::complex *b3, const int nx, const int ny, const int nz, const int norder) { const std::complex ci_tpi = ModuleBase::NEG_IMAG_UNIT * ModuleBase::TWO_PI; From ed6423176ab8d6fe2aada0562585ad4249a2a84a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 5 Nov 2025 08:41:12 +0800 Subject: [PATCH 72/81] update --- source/source_esolver/esolver_ks.cpp | 15 ++++----------- source/source_relax/test/relax_test.h | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 776a9dc3bd..2818152b22 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -45,7 +45,7 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para { ModuleBase::TITLE("ESolver_KS", "before_all_runners"); - //! 1) init "before_all_runniers" in ESolver_FP + //! 1) setup "before_all_runniers" in ESolver_FP ESolver_FP::before_all_runners(ucell, inp); //! 2) setup some parameters @@ -67,7 +67,7 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL"); - //! 4) setup Exc for the first element '0', because all elements have same exc + //! 4) setup Exc for the first element '0' (all elements have same exc) XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); //! 5) setup the charge mixing parameters @@ -84,7 +84,7 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SYMMETRY"); } - //! 7) Setup the k points according to symmetry. + //! 7) setup k points in the Brillouin zone according to symmetry. this->kv.set(ucell,ucell.symm, inp.kpoint_file, inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS"); @@ -104,14 +104,7 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para template void ESolver_KS::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) -{ - ModuleBase::timer::tick(this->classname, "hamilt2rho_single"); - // Temporarily, before HSolver is constructed, it should be overrided by - // LCAO, PW, SDFT and TDDFT. - // After HSolver is constructed, LCAO, PW, SDFT should delete their own - // hamilt2rho_single() and use: - ModuleBase::timer::tick(this->classname, "hamilt2rho_single"); -} +{} template void ESolver_KS::hamilt2rho(UnitCell& ucell, const int istep, const int iter, const double ethr) diff --git a/source/source_relax/test/relax_test.h b/source/source_relax/test/relax_test.h index d3c6ce5d2f..fa787a909a 100644 --- a/source/source_relax/test/relax_test.h +++ b/source/source_relax/test/relax_test.h @@ -26,4 +26,4 @@ void ModuleSymmetry::Symmetry::symmetrize_mat3(ModuleBase::matrix& sigma, const void ModuleSymmetry::Symmetry::symmetrize_vec3_nat(double* v)const {}; Structure_Factor::Structure_Factor() {}; Structure_Factor::~Structure_Factor(){}; -void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const Parallel_Grid&, const ModulePW::PW_Basis* rho_basis){}; +void Structure_Factor::setup(const UnitCell* Ucell, const Parallel_Grid&, const ModulePW::PW_Basis* rho_basis){}; From 1d71aa7176f3c05448715bec02d4a4834414657e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 5 Nov 2025 08:46:42 +0800 Subject: [PATCH 73/81] fix sf --- source/source_psi/test/psi_initializer_unit_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_psi/test/psi_initializer_unit_test.cpp b/source/source_psi/test/psi_initializer_unit_test.cpp index 44d3a6eb9c..0bfad3d371 100644 --- a/source/source_psi/test/psi_initializer_unit_test.cpp +++ b/source/source_psi/test/psi_initializer_unit_test.cpp @@ -80,7 +80,7 @@ InfoNonlocal::~InfoNonlocal() {} #endif Structure_Factor::Structure_Factor() {} Structure_Factor::~Structure_Factor() {} -void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const Parallel_Grid&, const ModulePW::PW_Basis* rho_basis) {} +void Structure_Factor::setup(const UnitCell* Ucell, const Parallel_Grid&, const ModulePW::PW_Basis* rho_basis) {} std::complex* Structure_Factor::get_sk(int ik, int it, int ia, ModulePW::PW_Basis_K const*wfc_basis) const { int npw = wfc_basis->npwk[ik]; @@ -546,4 +546,4 @@ int main(int argc, char** argv) #endif return result; -} \ No newline at end of file +} From 8029f27d9eef5962f807135988ead11b111fe387 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 5 Nov 2025 08:50:52 +0800 Subject: [PATCH 74/81] fix --- source/source_estate/test/charge_extra_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_estate/test/charge_extra_test.cpp b/source/source_estate/test/charge_extra_test.cpp index 255c2fbe78..c2951c1b5a 100644 --- a/source/source_estate/test/charge_extra_test.cpp +++ b/source/source_estate/test/charge_extra_test.cpp @@ -89,7 +89,7 @@ Structure_Factor::Structure_Factor() Structure_Factor::~Structure_Factor() { } -void Structure_Factor::setup_structure_factor(const UnitCell*, const Parallel_Grid&, const ModulePW::PW_Basis*) +void Structure_Factor::setup(const UnitCell*, const Parallel_Grid&, const ModulePW::PW_Basis*) { } From 3fe9aed801868f9f3caec1d3cadaf37209211474 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 5 Nov 2025 08:56:09 +0800 Subject: [PATCH 75/81] fix structure factor --- .../source_pw/module_pwdft/test/structure_factor_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/source_pw/module_pwdft/test/structure_factor_test.cpp b/source/source_pw/module_pwdft/test/structure_factor_test.cpp index c0f5078a72..0a1866a3f4 100644 --- a/source/source_pw/module_pwdft/test/structure_factor_test.cpp +++ b/source/source_pw/module_pwdft/test/structure_factor_test.cpp @@ -75,7 +75,7 @@ TEST_F(StructureFactorTest, set) TEST_F(StructureFactorTest, setup_structure_factor_double) { rho_basis->npw = 10; - SF.setup_structure_factor(ucell,*pgrid,rho_basis); + SF.setup(ucell,*pgrid,rho_basis); for (int i=0;i< ucell->nat * (2 * rho_basis->nx + 1);i++) { @@ -100,7 +100,7 @@ TEST_F(StructureFactorTest, setup_structure_factor_float) { PARAM.sys.has_float_data = true; rho_basis->npw = 10; - SF.setup_structure_factor(ucell,*pgrid,rho_basis); + SF.setup(ucell,*pgrid,rho_basis); for (int i=0;i< ucell->nat * (2 * rho_basis->nx + 1);i++) { @@ -125,4 +125,4 @@ int main() { testing::InitGoogleTest(); return RUN_ALL_TESTS(); -} \ No newline at end of file +} From 7d29c430c22153f2533011a6a6c3fe41ebc4a2a0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 6 Nov 2025 22:04:33 +0800 Subject: [PATCH 76/81] fix dm bug in PEXSI --- source/source_estate/elecstate_lcao.cpp | 28 +++++++++++++------------ source/source_estate/elecstate_lcao.h | 6 +++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/source/source_estate/elecstate_lcao.cpp b/source/source_estate/elecstate_lcao.cpp index 14562fca87..a9578b228a 100644 --- a/source/source_estate/elecstate_lcao.cpp +++ b/source/source_estate/elecstate_lcao.cpp @@ -1,6 +1,5 @@ -#include "elecstate_lcao.h" - -#include "cal_dm.h" +#include "source_estate/elecstate_lcao.h" +#include "source_estate/cal_dm.h" #include "source_base/timer.h" #include "source_estate/module_dm/cal_dm_psi.h" #include "source_hamilt/module_xc/xc_functional.h" @@ -31,9 +30,10 @@ double ElecStateLCAO>::get_spin_constrain_energy() return sc.cal_escon(); } -#ifdef __PEXSI template <> -void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, std::vector pexsi_EDM) +void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, + std::vector pexsi_EDM, + DensityMatrix* dm) { ModuleBase::timer::tick("ElecStateLCAO", "dm2Rho"); @@ -43,13 +43,15 @@ void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, std::vectorget_DM()->pexsi_EDM = pexsi_EDM; +#ifdef __PEXSI + dm->pexsi_EDM = pexsi_EDM; +#endif for (int is = 0; is < nspin; is++) { - this->DM->set_DMK_pointer(is, pexsi_DM[is]); + dm->set_DMK_pointer(is, pexsi_DM[is]); } - DM->cal_DMR(); + dm->cal_DMR(); for (int is = 0; is < PARAM.inp.nspin; is++) { @@ -58,14 +60,14 @@ void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, std::vectorDM->get_DMR_vector(), PARAM.inp.nspin, this->charge->rho); + ModuleGint::cal_gint_rho(dm->get_DMR_vector(), PARAM.inp.nspin, this->charge->rho); if (XC_Functional::get_ked_flag()) { for (int is = 0; is < PARAM.inp.nspin; is++) { ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[0], this->charge->nrxx); } - ModuleGint::cal_gint_tau(this->DM->get_DMR_vector(), PARAM.inp.nspin, this->charge->kin_r); + ModuleGint::cal_gint_tau(dm->get_DMR_vector(), PARAM.inp.nspin, this->charge->kin_r); } this->charge->renormalize_rho(); @@ -75,13 +77,13 @@ void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, std::vector -void ElecStateLCAO>::dm2rho(std::vector*> pexsi_DM, - std::vector*> pexsi_EDM) +void ElecStateLCAO>::dm2Rho(std::vector*> pexsi_DM, + std::vector*> pexsi_EDM, + DensityMatrix, double>* dm) { ModuleBase::WARNING_QUIT("ElecStateLCAO", "pexsi is not completed for multi-k case"); } -#endif template class ElecStateLCAO; // Gamma_only case template class ElecStateLCAO>; // multi-k case diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index 5d2d9ac7f6..eafc1e0a0d 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -37,7 +37,6 @@ class ElecStateLCAO : public ElecState double get_spin_constrain_energy() override; -#ifdef __PEXSI // use for pexsi /** @@ -46,8 +45,9 @@ class ElecStateLCAO : public ElecState * @param pexsi_EDM: pointers of energy-weighed density matrix (EDMK) calculated by pexsi, needed by MD, will be * stored in DensityMatrix::pexsi_EDM */ - void dm2rho(std::vector pexsi_DM, std::vector pexsi_EDM); -#endif + void dm2Rho(std::vector pexsi_DM, + std::vector pexsi_EDM, + DensityMatrix* dm); }; From 01a83f2a8315f4f2ee5eab9802345c7dfb9fcef0 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 7 Nov 2025 08:14:20 +0800 Subject: [PATCH 77/81] fix PEXSI --- source/source_estate/elecstate_lcao.cpp | 8 ++++---- source/source_estate/elecstate_lcao.h | 2 +- source/source_hsolver/hsolver_lcao.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/source_estate/elecstate_lcao.cpp b/source/source_estate/elecstate_lcao.cpp index a9578b228a..7bd32542ae 100644 --- a/source/source_estate/elecstate_lcao.cpp +++ b/source/source_estate/elecstate_lcao.cpp @@ -31,11 +31,11 @@ double ElecStateLCAO>::get_spin_constrain_energy() } template <> -void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, +void ElecStateLCAO::dm2rho(std::vector pexsi_DM, std::vector pexsi_EDM, DensityMatrix* dm) { - ModuleBase::timer::tick("ElecStateLCAO", "dm2Rho"); + ModuleBase::timer::tick("ElecStateLCAO", "dm2rho"); int nspin = PARAM.inp.nspin; if (PARAM.inp.nspin == 4) @@ -72,12 +72,12 @@ void ElecStateLCAO::dm2Rho(std::vector pexsi_DM, this->charge->renormalize_rho(); - ModuleBase::timer::tick("ElecStateLCAO", "dm2Rho"); + ModuleBase::timer::tick("ElecStateLCAO", "dm2rho"); return; } template <> -void ElecStateLCAO>::dm2Rho(std::vector*> pexsi_DM, +void ElecStateLCAO>::dm2rho(std::vector*> pexsi_DM, std::vector*> pexsi_EDM, DensityMatrix, double>* dm) { diff --git a/source/source_estate/elecstate_lcao.h b/source/source_estate/elecstate_lcao.h index eafc1e0a0d..3cf06641e3 100644 --- a/source/source_estate/elecstate_lcao.h +++ b/source/source_estate/elecstate_lcao.h @@ -45,7 +45,7 @@ class ElecStateLCAO : public ElecState * @param pexsi_EDM: pointers of energy-weighed density matrix (EDMK) calculated by pexsi, needed by MD, will be * stored in DensityMatrix::pexsi_EDM */ - void dm2Rho(std::vector pexsi_DM, + void dm2rho(std::vector pexsi_DM, std::vector pexsi_EDM, DensityMatrix* dm); diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index 87eec7475e..214a0de591 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -124,7 +124,7 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, auto _pes = dynamic_cast*>(pes); pes->f_en.eband = pe.totalFreeEnergy; // maybe eferm could be dealt with in the future - _pes->dm2rho(dm, pe.EDM); + _pes->dm2rho(pe.DM, pe.EDM, dm); #endif } From 5020b40e72c8b86aa383a16e738847bb9cc3860c Mon Sep 17 00:00:00 2001 From: Levi Zhou <31941107+ZhouXY-PKU@users.noreply.github.com> Date: Fri, 7 Nov 2025 10:52:25 +0800 Subject: [PATCH 78/81] Update edm.cpp --- source/source_lcao/edm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_lcao/edm.cpp b/source/source_lcao/edm.cpp index 2a02afcaaf..79d6058de5 100644 --- a/source/source_lcao/edm.cpp +++ b/source/source_lcao/edm.cpp @@ -31,10 +31,10 @@ elecstate::DensityMatrix Force_LCAO::cal_edm(const elecs #ifdef __PEXSI if (PARAM.inp.ks_solver == "pexsi") { - auto pes = dynamic_cast*>(pelec); + // auto pes = dynamic_cast*>(pelec); for (int ik = 0; ik < nspin; ik++) { - edm.set_DMK_pointer(ik, pes->get_DM()->pexsi_EDM[ik]); + edm.set_DMK_pointer(ik, pelec->dm->pexsi_EDM[ik]); } } From c4d309de4a5f0f08b324fbd0961ab413a0b5cd57 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 7 Nov 2025 11:02:05 +0800 Subject: [PATCH 79/81] fix edm --- source/source_lcao/edm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_lcao/edm.cpp b/source/source_lcao/edm.cpp index 2a02afcaaf..3f2d545e10 100644 --- a/source/source_lcao/edm.cpp +++ b/source/source_lcao/edm.cpp @@ -34,7 +34,7 @@ elecstate::DensityMatrix Force_LCAO::cal_edm(const elecs auto pes = dynamic_cast*>(pelec); for (int ik = 0; ik < nspin; ik++) { - edm.set_DMK_pointer(ik, pes->get_DM()->pexsi_EDM[ik]); + edm.set_DMK_pointer(ik, dm.pexsi_EDM[ik]); } } From 884d47d1d21543070125b1f65c96c1f8bbf60b48 Mon Sep 17 00:00:00 2001 From: Levi Zhou <31941107+ZhouXY-PKU@users.noreply.github.com> Date: Fri, 7 Nov 2025 11:04:29 +0800 Subject: [PATCH 80/81] Update hsolver_lcao.cpp --- source/source_hsolver/hsolver_lcao.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index 214a0de591..8a2d3f6d6b 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -112,7 +112,7 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, else if (this->method == "pexsi") { #ifdef __PEXSI // other purification methods should follow this routine - DiagoPexsi pe(ParaV); + DiagoPexsi pe(ParaV); for (int ik = 0; ik < psi.get_nk(); ++ik) { /// update H(k) for each k point @@ -121,7 +121,7 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, // solve eigenvector and eigenvalue for H(k) pe.diag(pHamilt, psi, nullptr); } - auto _pes = dynamic_cast*>(pes); + auto _pes = dynamic_cast*>(pes); pes->f_en.eband = pe.totalFreeEnergy; // maybe eferm could be dealt with in the future _pes->dm2rho(pe.DM, pe.EDM, dm); From c010f37ebc5501c0d73c84b6b231f6dc30eb5fe2 Mon Sep 17 00:00:00 2001 From: Levi Zhou <31941107+ZhouXY-PKU@users.noreply.github.com> Date: Fri, 7 Nov 2025 11:32:28 +0800 Subject: [PATCH 81/81] Update hsolver_lcao.cpp --- source/source_hsolver/hsolver_lcao.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/source_hsolver/hsolver_lcao.cpp b/source/source_hsolver/hsolver_lcao.cpp index 8a2d3f6d6b..1c4c5a5d61 100644 --- a/source/source_hsolver/hsolver_lcao.cpp +++ b/source/source_hsolver/hsolver_lcao.cpp @@ -124,7 +124,7 @@ void HSolverLCAO::solve(hamilt::Hamilt* pHamilt, auto _pes = dynamic_cast*>(pes); pes->f_en.eband = pe.totalFreeEnergy; // maybe eferm could be dealt with in the future - _pes->dm2rho(pe.DM, pe.EDM, dm); + _pes->dm2rho(pe.DM, pe.EDM, &dm); #endif }