Skip to content

Commit 02b07c6

Browse files
authored
Refactor: Move the print of H(k)&S(k) and wavefunctions to after_scf. (#5682)
* Refactor: move writing H(k)&S(k) from update_pot to after_scf. * Fix the print condition of out_mat_hs for tddft * Add override to avoid compile warning.
1 parent e555490 commit 02b07c6

File tree

8 files changed

+110
-162
lines changed

8 files changed

+110
-162
lines changed

source/module_esolver/esolver_gets.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,9 @@ void ESolver_GetS::runner(UnitCell& ucell, const int istep)
143143
ModuleBase::timer::tick("ESolver_GetS", "runner");
144144
}
145145

146+
void ESolver_GetS::after_all_runners(UnitCell& ucell) {};
147+
double ESolver_GetS::cal_energy() {};
148+
void ESolver_GetS::cal_force(UnitCell& ucell, ModuleBase::matrix& force) {};
149+
void ESolver_GetS::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) {};
150+
146151
} // namespace ModuleESolver

source/module_esolver/esolver_gets.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ class ESolver_GetS : public ESolver_KS<std::complex<double>>
1818

1919
void before_all_runners(UnitCell& ucell, const Input_para& inp) override;
2020

21-
void after_all_runners(UnitCell& ucell){};
21+
void after_all_runners(UnitCell& ucell) override;
2222

2323
void runner(UnitCell& ucell, const int istep) override;
2424

2525
//! calculate total energy of a given system
26-
double cal_energy() {};
26+
double cal_energy() override;
2727

2828
//! calcualte forces for the atoms in the given cell
29-
void cal_force(UnitCell& ucell, ModuleBase::matrix& force) {};
29+
void cal_force(UnitCell& ucell, ModuleBase::matrix& force) override;
3030

3131
//! calcualte stress of given cell
32-
void cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) {};
32+
void cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) override;
3333

3434
protected:
3535
// 2d block - cyclic distribution info

source/module_esolver/esolver_ks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,15 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
475475
return;
476476
};
477477

478+
template <typename T, typename Device>
479+
void ESolver_KS<T, Device>::before_scf(UnitCell& ucell, const int istep)
480+
{
481+
ModuleBase::TITLE("ESolver_KS", "before_scf");
482+
483+
//! 1) call before_scf() of ESolver_FP
484+
ESolver_FP::before_scf(ucell, istep);
485+
}
486+
478487
template <typename T, typename Device>
479488
void ESolver_KS<T, Device>::iter_init(UnitCell& ucell, const int istep, const int iter)
480489
{

source/module_esolver/esolver_ks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ESolver_KS : public ESolver_FP
3737

3838
protected:
3939
//! Something to do before SCF iterations.
40-
virtual void before_scf(UnitCell& ucell, const int istep) {};
40+
virtual void before_scf(UnitCell& ucell, const int istep) override;
4141

4242
//! Something to do before hamilt2density function in each iter loop.
4343
virtual void iter_init(UnitCell& ucell, const int istep, const int iter);

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 87 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -757,86 +757,13 @@ void ESolver_KS_LCAO<TK, TR>::hamilt2density_single(UnitCell& ucell, int istep,
757757
//------------------------------------------------------------------------------
758758
//! the 12th function of ESolver_KS_LCAO: update_pot
759759
//! mohan add 2024-05-11
760-
//! 1) print Hamiltonian and Overlap matrix (why related to update_pot()?)
761-
//! 2) print wavefunctions (why related to update_pot()?)
762-
//! 3) print potential
760+
//! 1) print potential
763761
//------------------------------------------------------------------------------
764762
template <typename TK, typename TR>
765763
void ESolver_KS_LCAO<TK, TR>::update_pot(UnitCell& ucell, const int istep, const int iter)
766764
{
767765
ModuleBase::TITLE("ESolver_KS_LCAO", "update_pot");
768766

769-
// 1) print Hamiltonian and Overlap matrix
770-
if (this->conv_esolver || iter == PARAM.inp.scf_nmax)
771-
{
772-
if (!PARAM.globalv.gamma_only_local && (PARAM.inp.out_mat_hs[0] || PARAM.inp.deepks_v_delta))
773-
{
774-
this->GK.renew(true);
775-
}
776-
for (int ik = 0; ik < this->kv.get_nks(); ++ik)
777-
{
778-
if (PARAM.inp.out_mat_hs[0] || PARAM.inp.deepks_v_delta)
779-
{
780-
this->p_hamilt->updateHk(ik);
781-
}
782-
bool bit = false; // LiuXh, 2017-03-21
783-
// if set bit = true, there would be error in soc-multi-core
784-
// calculation, noted by zhengdy-soc
785-
if (this->psi != nullptr && (istep % PARAM.inp.out_interval == 0))
786-
{
787-
hamilt::MatrixBlock<TK> h_mat;
788-
hamilt::MatrixBlock<TK> s_mat;
789-
790-
this->p_hamilt->matrix(h_mat, s_mat);
791-
792-
if (PARAM.inp.out_mat_hs[0])
793-
{
794-
ModuleIO::save_mat(istep,
795-
h_mat.p,
796-
PARAM.globalv.nlocal,
797-
bit,
798-
PARAM.inp.out_mat_hs[1],
799-
1,
800-
PARAM.inp.out_app_flag,
801-
"H",
802-
"data-" + std::to_string(ik),
803-
this->pv,
804-
GlobalV::DRANK);
805-
ModuleIO::save_mat(istep,
806-
s_mat.p,
807-
PARAM.globalv.nlocal,
808-
bit,
809-
PARAM.inp.out_mat_hs[1],
810-
1,
811-
PARAM.inp.out_app_flag,
812-
"S",
813-
"data-" + std::to_string(ik),
814-
this->pv,
815-
GlobalV::DRANK);
816-
}
817-
#ifdef __DEEPKS
818-
if (PARAM.inp.deepks_out_labels && PARAM.inp.deepks_v_delta)
819-
{
820-
DeePKS_domain::save_h_mat(h_mat.p, this->pv.nloc);
821-
}
822-
#endif
823-
}
824-
}
825-
}
826-
827-
// 2) print wavefunctions
828-
if (elecstate::ElecStateLCAO<TK>::out_wfc_lcao && (this->conv_esolver || iter == PARAM.inp.scf_nmax)
829-
&& (istep % PARAM.inp.out_interval == 0))
830-
{
831-
ModuleIO::write_wfc_nao(elecstate::ElecStateLCAO<TK>::out_wfc_lcao,
832-
this->psi[0],
833-
this->pelec->ekb,
834-
this->pelec->wg,
835-
this->pelec->klist->kvec_c,
836-
this->pv,
837-
istep);
838-
}
839-
840767
if (!this->conv_esolver)
841768
{
842769
elecstate::cal_ux(ucell);
@@ -962,7 +889,9 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
962889
//! 1) call after_scf() of ESolver_KS
963890
//! 2) write density matrix for sparse matrix
964891
//! 4) write density matrix
965-
//! 6) write Exx matrix
892+
//! 5) write Exx matrix
893+
//! 6) write Hamiltonian and Overlap matrix
894+
//! 7) write wavefunctions
966895
//! 11) write deepks information
967896
//! 12) write rpa information
968897
//! 13) write HR in npz format
@@ -982,10 +911,10 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
982911
this->pelec->cal_tau(*(this->psi));
983912
}
984913

985-
//! 5) call after_scf() of ESolver_KS
914+
//! 2) call after_scf() of ESolver_KS
986915
ESolver_KS<TK>::after_scf(ucell, istep);
987916

988-
//! 6) write density matrix for sparse matrix
917+
//! 3) write density matrix for sparse matrix
989918
ModuleIO::write_dmr(dynamic_cast<const elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM()->get_DMR_vector(),
990919
this->pv,
991920
PARAM.inp.out_dm1,
@@ -995,7 +924,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
995924
&ucell.nat,
996925
istep);
997926

998-
//! 7) write density matrix
927+
//! 4) write density matrix
999928
if (PARAM.inp.out_dm)
1000929
{
1001930
std::vector<double> efermis(PARAM.inp.nspin == 2 ? 2 : 1);
@@ -1012,7 +941,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
1012941
}
1013942

1014943
#ifdef __EXX
1015-
//! 8) write Hexx matrix for NSCF (see `out_chg` in docs/advanced/input_files/input-main.md)
944+
//! 5) write Hexx matrix for NSCF (see `out_chg` in docs/advanced/input_files/input-main.md)
1016945
if (PARAM.inp.calculation != "nscf")
1017946
{
1018947
if (GlobalC::exx_info.info_global.cal_exx && PARAM.inp.out_chg[0]
@@ -1031,7 +960,74 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
1031960
}
1032961
#endif
1033962

1034-
//! 9) Write DeePKS information
963+
// 6) write Hamiltonian and Overlap matrix
964+
if (!PARAM.globalv.gamma_only_local && (PARAM.inp.out_mat_hs[0] || PARAM.inp.deepks_v_delta))
965+
{
966+
this->GK.renew(true);
967+
}
968+
for (int ik = 0; ik < this->kv.get_nks(); ++ik)
969+
{
970+
if (PARAM.inp.out_mat_hs[0] || PARAM.inp.deepks_v_delta)
971+
{
972+
this->p_hamilt->updateHk(ik);
973+
}
974+
bool bit = false; // LiuXh, 2017-03-21
975+
// if set bit = true, there would be error in soc-multi-core
976+
// calculation, noted by zhengdy-soc
977+
if (this->psi != nullptr && (istep % PARAM.inp.out_interval == 0))
978+
{
979+
hamilt::MatrixBlock<TK> h_mat;
980+
hamilt::MatrixBlock<TK> s_mat;
981+
982+
this->p_hamilt->matrix(h_mat, s_mat);
983+
984+
if (PARAM.inp.out_mat_hs[0])
985+
{
986+
ModuleIO::save_mat(istep,
987+
h_mat.p,
988+
PARAM.globalv.nlocal,
989+
bit,
990+
PARAM.inp.out_mat_hs[1],
991+
1,
992+
PARAM.inp.out_app_flag,
993+
"H",
994+
"data-" + std::to_string(ik),
995+
this->pv,
996+
GlobalV::DRANK);
997+
ModuleIO::save_mat(istep,
998+
s_mat.p,
999+
PARAM.globalv.nlocal,
1000+
bit,
1001+
PARAM.inp.out_mat_hs[1],
1002+
1,
1003+
PARAM.inp.out_app_flag,
1004+
"S",
1005+
"data-" + std::to_string(ik),
1006+
this->pv,
1007+
GlobalV::DRANK);
1008+
}
1009+
#ifdef __DEEPKS
1010+
if (PARAM.inp.deepks_out_labels && PARAM.inp.deepks_v_delta)
1011+
{
1012+
DeePKS_domain::save_h_mat(h_mat.p, this->pv.nloc);
1013+
}
1014+
#endif
1015+
}
1016+
}
1017+
1018+
// 7) write wavefunctions
1019+
if (elecstate::ElecStateLCAO<TK>::out_wfc_lcao && (istep % PARAM.inp.out_interval == 0))
1020+
{
1021+
ModuleIO::write_wfc_nao(elecstate::ElecStateLCAO<TK>::out_wfc_lcao,
1022+
this->psi[0],
1023+
this->pelec->ekb,
1024+
this->pelec->wg,
1025+
this->pelec->klist->kvec_c,
1026+
this->pv,
1027+
istep);
1028+
}
1029+
1030+
//! 8) Write DeePKS information
10351031
#ifdef __DEEPKS
10361032
std::shared_ptr<LCAO_Deepks> ld_shared_ptr(&GlobalC::ld, [](LCAO_Deepks*) {});
10371033
LCAO_Deepks_Interface LDI = LCAO_Deepks_Interface(ld_shared_ptr);
@@ -1053,7 +1049,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
10531049
ModuleBase::timer::tick("ESolver_KS_LCAO", "out_deepks_labels");
10541050
#endif
10551051

1056-
//! 10) Perform RDMFT calculations
1052+
//! 9) Perform RDMFT calculations
10571053
/******** test RDMFT *********/
10581054
if ( PARAM.inp.rdmft == true ) // rdmft, added by jghan, 2024-10-17
10591055
{
@@ -1079,7 +1075,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
10791075

10801076

10811077
#ifdef __EXX
1082-
// 11) Write RPA information.
1078+
// 10) Write RPA information.
10831079
if (PARAM.inp.rpa)
10841080
{
10851081
// ModuleRPA::DFT_RPA_interface
@@ -1094,7 +1090,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
10941090
}
10951091
#endif
10961092

1097-
// 12) write HR in npz format.
1093+
// 11) write HR in npz format.
10981094
if (PARAM.inp.out_hr_npz)
10991095
{
11001096
this->p_hamilt->updateHk(0); // first k point, up spin
@@ -1113,7 +1109,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
11131109
}
11141110
}
11151111

1116-
// 13) write density matrix in the 'npz' format.
1112+
// 12) write density matrix in the 'npz' format.
11171113
if (PARAM.inp.out_dm_npz)
11181114
{
11191115
const elecstate::DensityMatrix<TK, double>* dm
@@ -1128,7 +1124,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
11281124
}
11291125
}
11301126

1131-
//! 14) Print out information every 'out_interval' steps.
1127+
//! 13) Print out information every 'out_interval' steps.
11321128
if (PARAM.inp.calculation != "md" || istep % PARAM.inp.out_interval == 0)
11331129
{
11341130
//! Print out sparse matrix
@@ -1154,7 +1150,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
11541150
}
11551151
}
11561152

1157-
//! 15) Print out atomic magnetization only when 'spin_constraint' is on.
1153+
//! 14) Print out atomic magnetization only when 'spin_constraint' is on.
11581154
if (PARAM.inp.sc_mag_switch)
11591155
{
11601156
spinconstrain::SpinConstrain<TK>& sc = spinconstrain::SpinConstrain<TK>::getScInstance();
@@ -1163,14 +1159,14 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
11631159
sc.print_Mag_Force(GlobalV::ofs_running);
11641160
}
11651161

1166-
//! 16) Clean up RA.
1162+
//! 15) Clean up RA.
11671163
//! this should be last function and put it in the end, mohan request 2024-11-28
11681164
if (!PARAM.inp.cal_force && !PARAM.inp.cal_stress)
11691165
{
11701166
RA.delete_grid();
11711167
}
11721168

1173-
//! 17) Print out quasi-orbitals.
1169+
//! 16) Print out quasi-orbitals.
11741170
if (PARAM.inp.qo_switch)
11751171
{
11761172
toQO tqo(PARAM.inp.qo_basis, PARAM.inp.qo_strategy, PARAM.inp.qo_thr, PARAM.inp.qo_screening_coeff);
@@ -1185,7 +1181,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
11851181
tqo.calculate();
11861182
}
11871183

1188-
//! 18) Print out kinetic matrix.
1184+
//! 17) Print out kinetic matrix.
11891185
if (PARAM.inp.out_mat_tk[0])
11901186
{
11911187
hamilt::HS_Matrix_K<TK> hsk(&pv, true);
@@ -1220,7 +1216,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
12201216
delete ekinetic;
12211217
}
12221218

1223-
//! 19) Wannier 90 function, added by jingan in 2018.11.7
1219+
//! 18) Wannier 90 function, added by jingan in 2018.11.7
12241220
if (PARAM.inp.calculation == "nscf" && PARAM.inp.towannier90)
12251221
{
12261222
std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Wave function to Wannier90");
@@ -1259,7 +1255,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep)
12591255
std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wave function to Wannier90");
12601256
}
12611257

1262-
//! 20) berry phase calculations, added by jingan
1258+
//! 19) berry phase calculations, added by jingan
12631259
if (PARAM.inp.calculation == "nscf" &&
12641260
berryphase::berry_phase_flag &&
12651261
ModuleSymmetry::Symmetry::symm_flag != 1)

0 commit comments

Comments
 (0)