Skip to content

Commit 1c2a933

Browse files
authored
Reducing the size of esolver_ks_lcao (#6697)
* remove some codes in esolver_ks_lcao and add LCAO_set.cpp * add LCAO_set.h and LCAO_set.cpp * finish updating esolver * update LCAO_set, fix ref of pointer of psi * update esolver
1 parent c3eb4e6 commit 1c2a933

File tree

5 files changed

+222
-56
lines changed

5 files changed

+222
-56
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ OBJS_LCAO=evolve_elec.o\
634634
spar_hsr.o\
635635
spar_st.o\
636636
spar_u.o\
637+
LCAO_set.o\
637638
LCAO_set_fs.o\
638639
LCAO_set_st.o\
639640
LCAO_nl_mu.o\

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
#include "source_io/ctrl_runner_lcao.h" // use ctrl_runner_lcao()
1818
#include "source_io/ctrl_iter_lcao.h" // use ctrl_iter_lcao()
1919
#include "source_io/ctrl_scf_lcao.h" // use ctrl_scf_lcao()
20-
#include "source_psi/setup_psi.h" // mohan add 20251019
21-
#include "source_io/read_wfc_nao.h"
2220
#include "source_io/print_info.h"
2321
#include "source_lcao/rho_tau_lcao.h" // mohan add 20251024
22+
#include "source_lcao/LCAO_set.h" // mohan add 20251111
23+
24+
25+
// tmp
26+
#include "source_psi/setup_psi.h" // use Setup_Psi
27+
#include "source_io/read_wfc_nao.h" // use read_wfc_nao
28+
#include "source_estate/elecstate_tools.h" // use fixed_weights
29+
2430

2531
namespace ModuleESolver
2632
{
@@ -74,62 +80,17 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(UnitCell& ucell, const Input_pa
7480
return;
7581
}
7682

77-
// 5) init electronic wave function psi
78-
Setup_Psi<TK>::allocate_psi(this->psi, this->kv, this->pv, inp);
79-
80-
//! read psi from file
81-
if (inp.init_wfc == "file" && inp.esolver_type != "tddft")
82-
{
83-
if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir,
84-
this->pv, *this->psi, this->pelec->ekb, this->pelec->wg, this->kv.ik2iktot,
85-
this->kv.get_nkstot(), inp.nspin))
86-
{
87-
ModuleBase::WARNING_QUIT("ESolver_KS_LCAO", "read electronic wave functions failed");
88-
}
89-
}
90-
83+
LCAO_domain::set_psi_occ_dm_chg<TK>(this->kv, this->psi, this->pv, this->pelec,
84+
this->dmat, this->chr, inp);
9185

92-
// 7) init DMK, but DMR is constructed in before_scf()
93-
this->dmat.allocate_dm(&this->kv, &this->pv, inp.nspin);
86+
LCAO_domain::set_pot<TK>(ucell, this->kv, this->sf, *this->pw_rho, *this->pw_rhod,
87+
this->pelec, this->orb_, this->pv, this->locpp, this->dftu,
88+
this->solvent, this->exx_nao, this->deepks, inp);
9489

95-
// 8) init exact exchange calculations
96-
this->exx_nao.before_runner(ucell, this->kv, this->orb_, this->pv, inp);
97-
98-
// 9) initialize DFT+U
99-
if (inp.dft_plus_u)
100-
{
101-
this->dftu.init(ucell, &this->pv, this->kv.get_nks(), &orb_);
102-
}
103-
104-
// 10) init local pseudopotentials
105-
this->locpp.init_vloc(ucell, this->pw_rho);
106-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL");
107-
108-
// 11) init charge density
109-
this->chr.allocate(inp.nspin);
110-
111-
// 12) init potentials
112-
if (this->pelec->pot == nullptr)
113-
{
114-
this->pelec->pot = new elecstate::Potential(this->pw_rhod, this->pw_rho,
115-
&ucell, &(this->locpp.vloc), &(this->sf), &(this->solvent),
116-
&(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc));
117-
}
118-
119-
// 13) init deepks
120-
this->deepks.before_runner(ucell, this->kv.get_nks(), this->orb_, this->pv, inp);
121-
122-
// 14) set occupations, tddft does not need to set occupations in the first scf
123-
if (inp.ocp && inp.esolver_type != "tddft")
124-
{
125-
elecstate::fixed_weights(inp.ocp_kb, inp.nbands, inp.nelec,
126-
this->pelec->klist, this->pelec->wg, this->pelec->skip_weights);
127-
}
128-
129-
// 15) if kpar is not divisible by nks, print a warning
90+
//! if kpar is not divisible by nks, print a warning
13091
ModuleIO::print_kpar(this->kv.get_nks(), PARAM.globalv.kpar_lcao);
13192

132-
// 16) init rdmft, added by jghan
93+
//! init rdmft, added by jghan
13394
if (inp.rdmft == true)
13495
{
13596
rdmft_solver.init(this->pv, ucell,
@@ -310,8 +271,6 @@ void ESolver_KS_LCAO<TK, TR>::after_all_runners(UnitCell& ucell)
310271

311272
ESolver_KS<TK>::after_all_runners(ucell);
312273

313-
const int nspin0 = (PARAM.inp.nspin == 2) ? 2 : 1;
314-
315274
auto* hamilt_lcao = dynamic_cast<hamilt::HamiltLCAO<TK, TR>*>(this->p_hamilt);
316275
if(!hamilt_lcao)
317276
{

source/source_lcao/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ if(ENABLE_LCAO)
3333
spar_hsr.cpp
3434
spar_st.cpp
3535
spar_u.cpp
36+
LCAO_set.cpp
3637
LCAO_set_fs.cpp
3738
LCAO_set_st.cpp
3839
LCAO_nl_mu.cpp

source/source_lcao/LCAO_set.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include "source_lcao/LCAO_set.h"
2+
#include "source_io/module_parameter/parameter.h"
3+
#include "source_psi/setup_psi.h" // use Setup_Psi
4+
#include "source_io/read_wfc_nao.h" // use read_wfc_nao
5+
#include "source_estate/elecstate_tools.h" // use fixed_weights
6+
7+
template <typename TK>
8+
void LCAO_domain::set_psi_occ_dm_chg(
9+
const K_Vectors &kv, // k-points
10+
psi::Psi<TK>* &psi, // coefficients of NAO basis
11+
const Parallel_Orbitals &pv, // parallel scheme of NAO basis
12+
elecstate::ElecState* pelec, // eigen values and weights
13+
LCAO_domain::Setup_DM<TK> &dmat, // density matrix
14+
Charge &chr, // charge density
15+
const Input_para &inp) // input parameters
16+
{
17+
18+
//! 1) init electronic wave function psi
19+
Setup_Psi<TK>::allocate_psi(psi, kv, pv, inp);
20+
21+
//! 2) read psi from file
22+
if (inp.init_wfc == "file" && inp.esolver_type != "tddft")
23+
{
24+
if (!ModuleIO::read_wfc_nao(PARAM.globalv.global_readin_dir,
25+
pv, *psi, pelec->ekb, pelec->wg, kv.ik2iktot,
26+
kv.get_nkstot(), inp.nspin))
27+
{
28+
ModuleBase::WARNING_QUIT("set_psi_occ_dm_chg", "read electronic wave functions failed");
29+
}
30+
}
31+
32+
//! 3) set occupations, tddft does not need to set occupations in the first scf
33+
if (inp.ocp && inp.esolver_type != "tddft")
34+
{
35+
elecstate::fixed_weights(inp.ocp_kb, inp.nbands, inp.nelec,
36+
&kv, pelec->wg, pelec->skip_weights);
37+
}
38+
39+
//! 4) init DMK, but DMR is constructed in before_scf()
40+
dmat.allocate_dm(&kv, &pv, inp.nspin);
41+
42+
//! 5) init charge density
43+
chr.allocate(inp.nspin);
44+
45+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "CHARGE");
46+
47+
return;
48+
}
49+
50+
51+
template <typename TK>
52+
void LCAO_domain::set_pot(
53+
UnitCell &ucell, // not const because of dftu
54+
K_Vectors &kv, // not const due to exx
55+
Structure_Factor& sf, // will be modified in potential
56+
const ModulePW::PW_Basis &pw_rho,
57+
const ModulePW::PW_Basis &pw_rhod,
58+
elecstate::ElecState* pelec,
59+
const LCAO_Orbitals& orb,
60+
Parallel_Orbitals &pv, // not const due to deepks
61+
pseudopot_cell_vl &locpp,
62+
Plus_U &dftu,
63+
surchem& solvent,
64+
Exx_NAO<TK> &exx_nao,
65+
Setup_DeePKS<TK> &deepks,
66+
const Input_para &inp)
67+
{
68+
//! 1) init local pseudopotentials
69+
locpp.init_vloc(ucell, &pw_rho);
70+
71+
//! 2) init potentials
72+
if (pelec->pot == nullptr)
73+
{
74+
// where is the pot deleted?
75+
pelec->pot = new elecstate::Potential(&pw_rhod, &pw_rho,
76+
&ucell, &locpp.vloc, &sf, &solvent,
77+
&(pelec->f_en.etxc), &(pelec->f_en.vtxc));
78+
}
79+
80+
//! 3) initialize DFT+U
81+
if (inp.dft_plus_u)
82+
{
83+
dftu.init(ucell, &pv, kv.get_nks(), &orb);
84+
}
85+
86+
//! 4) init exact exchange calculations
87+
exx_nao.before_runner(ucell, kv, orb, pv, inp);
88+
89+
//! 5) init deepks
90+
deepks.before_runner(ucell, kv.get_nks(), orb, pv, inp);
91+
92+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "POTENTIALS");
93+
94+
return;
95+
}
96+
97+
98+
99+
template void LCAO_domain::set_psi_occ_dm_chg<double>(
100+
const K_Vectors &kv, // k-points
101+
psi::Psi<double>* &psi, // coefficients of NAO basis
102+
const Parallel_Orbitals &pv, // parallel scheme of NAO basis
103+
elecstate::ElecState* pelec, // eigen values and weights
104+
LCAO_domain::Setup_DM<double> &dmat, // density matrix
105+
Charge &chr, // charge density
106+
const Input_para &inp);
107+
108+
template void LCAO_domain::set_psi_occ_dm_chg<std::complex<double>>(
109+
const K_Vectors &kv, // k-points
110+
psi::Psi<std::complex<double>>* &psi, // coefficients of NAO basis
111+
const Parallel_Orbitals &pv, // parallel scheme of NAO basis
112+
elecstate::ElecState* pelec, // eigen values and weights
113+
LCAO_domain::Setup_DM<std::complex<double>> &dmat, // density matrix
114+
Charge &chr, // charge density
115+
const Input_para &inp);
116+
117+
template void LCAO_domain::set_pot<double>(
118+
UnitCell &ucell,
119+
K_Vectors &kv,
120+
Structure_Factor& sf,
121+
const ModulePW::PW_Basis &pw_rho,
122+
const ModulePW::PW_Basis &pw_rhod,
123+
elecstate::ElecState* pelec,
124+
const LCAO_Orbitals& orb,
125+
Parallel_Orbitals &pv,
126+
pseudopot_cell_vl &locpp,
127+
Plus_U &dftu,
128+
surchem& solvent,
129+
Exx_NAO<double> &exx_nao,
130+
Setup_DeePKS<double> &deepks,
131+
const Input_para &inp);
132+
133+
template void LCAO_domain::set_pot<std::complex<double>>(
134+
UnitCell &ucell,
135+
K_Vectors &kv,
136+
Structure_Factor& sf,
137+
const ModulePW::PW_Basis &pw_rho,
138+
const ModulePW::PW_Basis &pw_rhod,
139+
elecstate::ElecState* pelec,
140+
const LCAO_Orbitals& orb,
141+
Parallel_Orbitals &pv,
142+
pseudopot_cell_vl &locpp,
143+
Plus_U &dftu,
144+
surchem& solvent,
145+
Exx_NAO<std::complex<double>> &exx_nao,
146+
Setup_DeePKS<std::complex<double>> &deepks,
147+
const Input_para &inp);

source/source_lcao/LCAO_set.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef LCAO_SET_H
2+
#define LCAO_SET_H
3+
4+
#include "source_cell/unitcell.h"
5+
#include "source_cell/klist.h"
6+
#include "source_psi/psi.h"
7+
#include "source_estate/elecstate.h"
8+
#include "source_lcao/setup_dm.h"
9+
#include "source_pw/module_pwdft/structure_factor.h"
10+
#include "source_basis/module_pw/pw_basis.h"
11+
#include "source_hamilt/module_surchem/surchem.h"
12+
#include "source_pw/module_pwdft/VL_in_pw.h"
13+
#include "source_lcao/module_deepks/LCAO_deepks.h"
14+
#include "source_lcao/module_dftu/dftu.h"
15+
#include "source_lcao/setup_exx.h"
16+
#include "source_lcao/setup_deepks.h"
17+
18+
namespace LCAO_domain
19+
{
20+
21+
/**
22+
* @brief set up wave functions, occupation numbers,
23+
* density matrix and charge density
24+
*/
25+
template <typename TK>
26+
void set_psi_occ_dm_chg(
27+
const K_Vectors &kv, // k-points
28+
psi::Psi<TK>* &psi, // coefficients of NAO basis
29+
const Parallel_Orbitals &pv, // parallel scheme of NAO basis
30+
elecstate::ElecState* pelec, // eigen values and weights
31+
LCAO_domain::Setup_DM<TK> &dmat, // density matrix
32+
Charge &chr, // charge density
33+
const Input_para& inp); // input parameters
34+
35+
/**
36+
* @brief set up potentials, including local pseudopotentials,
37+
* +U potential, solvent potential, exx potential and deepks potential
38+
*/
39+
template <typename TK>
40+
void set_pot(
41+
UnitCell &ucell,
42+
K_Vectors &kv,
43+
Structure_Factor& sf,
44+
const ModulePW::PW_Basis &pw_rho,
45+
const ModulePW::PW_Basis &pw_rhod,
46+
elecstate::ElecState* pelec,
47+
const LCAO_Orbitals& orb,
48+
Parallel_Orbitals &pv,
49+
pseudopot_cell_vl &locpp,
50+
Plus_U &dftu,
51+
surchem& solvent,
52+
Exx_NAO<TK> &exx_nao,
53+
Setup_DeePKS<TK> &deepks,
54+
const Input_para &inp);
55+
56+
} // end namespace
57+
58+
#endif

0 commit comments

Comments
 (0)