Skip to content

Commit 3b58709

Browse files
authored
refactor the call of initialization of charge density (#6744)
* 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 * delete useless head files * update esolver * change effective to eff for short * add dm2rho in init_dm * delete useless calculate_weights * fix a potential bug, the bcast of descf * move init_rho to KS:before_all_runners * fix the input parametes of init_rho * small update of esolver_of.cpp * fix tests * update eff in ML_KEDF * fix a potential bug in allocate() function in charge.cpp * move set_rhopw from elecstate to before charge.allocate * fix bug about two allocations of charge class * fix a bug in OFDFT * move the common parts in esolver_ks and esolver_of to esolver_fp * small updates of fp and ks esolvers * update double xc * fix bug about allocation of charge density
1 parent f671ce1 commit 3b58709

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+309
-425
lines changed

source/Makefile.Objects

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ OBJS_ELECSTAT=elecstate.o\
239239
potential_types.o\
240240
pot_sep.o\
241241
pot_local.o\
242-
pot_local_paw.o\
243242
H_Hartree_pw.o\
244243
H_TDDFT_pw.o\
245244
pot_xc.o\

source/source_esolver/esolver_double_xc.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void ESolver_DoubleXC<TK, TR>::before_all_runners(UnitCell& ucell, const Input_p
5151
this->pelec_base = new elecstate::ElecStateLCAO<TK>(&(this->chr_base), // use which parameter?
5252
&(this->kv),
5353
this->kv.get_nks(),
54-
this->pw_rho,
5554
this->pw_big);
5655
}
5756

@@ -87,7 +86,10 @@ void ESolver_DoubleXC<TK, TR>::before_all_runners(UnitCell& ucell, const Input_p
8786
this->dmat_base.allocate_dm(&this->kv, &this->pv, PARAM.inp.nspin);
8887

8988
// 10) inititlize the charge density
90-
this->chr_base.allocate(PARAM.inp.nspin);
89+
this->chr_base.set_rhopw(this->pw_rhod); // mohan add 20251130
90+
this->chr_base.allocate(PARAM.inp.nspin);
91+
this->chr_base.init_rho(ucell, this->Pgrid, this->sf.strucFac, ucell.symm, &this->kv);
92+
this->chr_base.check_rho();
9193

9294
// 11) initialize the potential
9395
if (this->pelec_base->pot == nullptr)

source/source_esolver/esolver_fp.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,56 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp)
4141
{
4242
ModuleBase::TITLE("ESolver_FP", "before_all_runners");
4343

44-
//! read pseudopotentials
44+
//! 1) read pseudopotentials
4545
elecstate::read_pseudo(GlobalV::ofs_running, ucell);
4646

47-
// setup pw_rho, pw_rhod, pw_big, sf, and read_pseudopotentials
47+
//! 2) setup pw_rho, pw_rhod, pw_big, sf, and read_pseudopotentials
4848
pw::setup_pwrho(ucell, PARAM.globalv.double_grid, this->pw_rho_flag,
49-
this->pw_rho, this->pw_rhod, this->pw_big,
50-
this->classname, inp);
49+
this->pw_rho, this->pw_rhod, this->pw_big, this->classname, inp);
5150

52-
// setup structure factors
51+
//! 3) setup structure factors
5352
this->sf.set(this->pw_rhod, inp.nbspline);
5453

55-
// write geometry file
54+
//! 4) write geometry file
5655
ModuleIO::CifParser::write(PARAM.globalv.global_out_dir + "STRU.cif",
5756
ucell, "# Generated by ABACUS ModuleIO::CifParser", "data_?");
5857

59-
// init charge extrapolation
58+
//! 5) init charge extrapolation
6059
this->CE.Init_CE(inp.nspin, ucell.nat, this->pw_rhod->nrxx, inp.chg_extrap);
6160

61+
//! 6) symmetry analysis should be performed every time the cell is changed
62+
if (ModuleSymmetry::Symmetry::symm_flag == 1)
63+
{
64+
ucell.symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running);
65+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SYMMETRY");
66+
}
67+
68+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL");
69+
70+
//! 7) setup k points in the Brillouin zone according to symmetry.
71+
this->kv.set(ucell,ucell.symm, inp.kpoint_file, inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running);
72+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
73+
74+
//! 8) print information
75+
ModuleIO::print_parameters(ucell, this->kv, inp);
76+
77+
//! 9) parallel of FFT grid
78+
this->Pgrid.init(this->pw_rhod->nx, this->pw_rhod->ny, this->pw_rhod->nz,
79+
this->pw_rhod->nplane, this->pw_rhod->nrxx, pw_big->nbz, pw_big->bz);
80+
81+
//! 10) calculate the structure factor
82+
this->sf.setup(&ucell, Pgrid, this->pw_rhod);
83+
84+
//! 11) setup the xc functional
85+
XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func);
86+
GlobalV::ofs_running<<XC_Functional::output_info()<<std::endl;
87+
88+
//! 11) initialize the charge density, we need to first set xc_type,
89+
// then we can call chr.allocate()
90+
this->chr.set_rhopw(this->pw_rhod); // mohan add 20251130
91+
this->chr.allocate(inp.nspin); // mohan move this from setup_estate_pw, 20251128
92+
93+
6294
return;
6395
}
6496

source/source_esolver/esolver_gets.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void ESolver_GetS::before_all_runners(UnitCell& ucell, const Input_para& inp)
5353
this->pelec = new elecstate::ElecStateLCAO<std::complex<double>>(&(this->chr), // use which parameter?
5454
&(this->kv),
5555
this->kv.get_nks(),
56-
this->pw_rho,
5756
this->pw_big);
5857
}
5958

source/source_esolver/esolver_ks.cpp

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,46 +62,26 @@ void ESolver_KS<T, Device>::before_all_runners(UnitCell& ucell, const Input_para
6262
// cell_factor
6363
this->ppcell.cell_factor = inp.cell_factor;
6464

65-
//! 3) setup charge mixing
65+
//! 3) setup Exc for the first element '0' (all elements have same exc)
66+
// XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func);
67+
// GlobalV::ofs_running<<XC_Functional::output_info()<<std::endl;
68+
69+
//! 4) setup charge mixing
6670
p_chgmix = new Charge_Mixing();
6771
p_chgmix->set_rhopw(this->pw_rho, this->pw_rhod);
68-
69-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL");
70-
71-
//! 4) setup Exc for the first element '0' (all elements have same exc)
72-
XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func);
73-
GlobalV::ofs_running<<XC_Functional::output_info()<<std::endl;
74-
75-
//! 5) setup the charge mixing parameters
7672
p_chgmix->set_mixing(inp.mixing_mode, inp.mixing_beta, inp.mixing_ndim,
7773
inp.mixing_gg0, inp.mixing_tau, inp.mixing_beta_mag, inp.mixing_gg0_mag,
7874
inp.mixing_gg0_min, inp.mixing_angle, inp.mixing_dmr, ucell.omega, ucell.tpiba);
79-
8075
p_chgmix->init_mixing();
8176

82-
//! 6) symmetry analysis should be performed every time the cell is changed
83-
if (ModuleSymmetry::Symmetry::symm_flag == 1)
84-
{
85-
ucell.symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running);
86-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SYMMETRY");
87-
}
88-
89-
//! 7) setup k points in the Brillouin zone according to symmetry.
90-
this->kv.set(ucell,ucell.symm, inp.kpoint_file, inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running);
91-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
92-
93-
//! 8) print information
94-
ModuleIO::print_parameters(ucell, this->kv, inp);
95-
96-
//! 9) setup plane wave for electronic wave functions
77+
//! 5) setup plane wave for electronic wave functions
9778
pw::setup_pwwfc(inp, ucell, *this->pw_rho, this->kv, this->pw_wfc);
9879

99-
//! 10) parallel of FFT grid
100-
Pgrid.init(this->pw_rhod->nx, this->pw_rhod->ny, this->pw_rhod->nz,
101-
this->pw_rhod->nplane, this->pw_rhod->nrxx, pw_big->nbz, pw_big->bz);
102-
103-
//! 11) calculate the structure factor
104-
this->sf.setup(&ucell, Pgrid, this->pw_rhod);
80+
//! 6) read in charge density, mohan add 2025-11-28
81+
//! Inititlize the charge density.
82+
this->chr.init_rho(ucell, this->Pgrid, this->sf.strucFac, ucell.symm, &this->kv, this->pw_wfc);
83+
this->chr.check_rho(); // check the rho
84+
10585
}
10686

10787
template <typename T, typename Device>
@@ -115,20 +95,14 @@ void ESolver_KS<T, Device>::hamilt2rho(UnitCell& ucell, const int istep, const i
11595
this->hamilt2rho_single(ucell, istep, iter, diag_ethr);
11696

11797
// 2) for MPI: STOGROUP? need to rewrite
118-
//<Temporary> It may be changed when more clever parallel algorithm is
119-
// put forward.
120-
// When parallel algorithm for bands are adopted. Density will only be
121-
// treated in the first group.
122-
//(Different ranks should have abtained the same, but small differences
123-
// always exist in practice.)
98+
//<Temporary> It may be changed when more clever parallel algorithm is put forward.
99+
// When parallel algorithm for bands are adopted. Density will only be treated in the first group.
100+
//(Different ranks should have abtained the same, but small differences always exist in practice.)
124101
// Maybe in the future, density and wavefunctions should use different
125102
// parallel algorithms, in which they do not occupy all processors, for
126103
// example wavefunctions uses 20 processors while density uses 10.
127104
if (PARAM.globalv.ks_run)
128105
{
129-
// double drho = this->estate.caldr2();
130-
// EState should be used after it is constructed.
131-
132106
drho = p_chgmix->get_drho(&this->chr, PARAM.inp.nelec);
133107
hsolver_error = 0.0;
134108
if (iter == 1 && PARAM.inp.calculation != "nscf")
@@ -140,23 +114,16 @@ void ESolver_KS<T, Device>::hamilt2rho(UnitCell& ucell, const int istep, const i
140114
// so a more precise HSolver should be executed.
141115
if (hsolver_error > drho)
142116
{
143-
diag_ethr = hsolver::reset_diag_ethr(GlobalV::ofs_running,
144-
PARAM.inp.basis_type,
145-
PARAM.inp.esolver_type,
146-
PARAM.inp.precision,
147-
hsolver_error,
148-
drho,
149-
diag_ethr,
150-
PARAM.inp.nelec);
117+
diag_ethr = hsolver::reset_diag_ethr(GlobalV::ofs_running, PARAM.inp.basis_type,
118+
PARAM.inp.esolver_type, PARAM.inp.precision, hsolver_error,
119+
drho, diag_ethr, PARAM.inp.nelec);
151120

152121
this->hamilt2rho_single(ucell, istep, iter, diag_ethr);
153122

154123
drho = p_chgmix->get_drho(&this->chr, PARAM.inp.nelec);
155124

156125
hsolver_error = hsolver::cal_hsolve_error(PARAM.inp.basis_type,
157-
PARAM.inp.esolver_type,
158-
diag_ethr,
159-
PARAM.inp.nelec);
126+
PARAM.inp.esolver_type, diag_ethr, PARAM.inp.nelec);
160127
}
161128
}
162129
}
@@ -168,22 +135,17 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
168135
ModuleBase::TITLE("ESolver_KS", "runner");
169136
ModuleBase::timer::tick(this->classname, "runner");
170137

171-
//----------------------------------------------------------------
172138
// 1) before_scf (electronic iteration loops)
173-
//----------------------------------------------------------------
174139
this->before_scf(ucell, istep);
175140
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT SCF");
176141

177-
//----------------------------------------------------------------
178142
// 2) SCF iterations
179-
//----------------------------------------------------------------
180143
bool conv_esolver = false;
181144
this->niter = this->maxniter;
182145
this->diag_ethr = PARAM.inp.pw_diag_thr;
183146
this->scf_nmax_flag = false; // mohan add 2025-09-21
184147
for (int iter = 1; iter <= this->maxniter; ++iter)
185148
{
186-
// mohan add 2025-09-21
187149
if(iter == this->maxniter)
188150
{
189151
this->scf_nmax_flag=true;

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@
2121
#include "source_lcao/rho_tau_lcao.h" // mohan add 20251024
2222
#include "source_lcao/LCAO_set.h" // mohan add 20251111
2323

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-
30-
3124
namespace ModuleESolver
3225
{
3326

@@ -61,7 +54,7 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(UnitCell& ucell, const Input_pa
6154
{
6255
// TK stands for double and std::complex<double>?
6356
this->pelec = new elecstate::ElecStateLCAO<TK>(&(this->chr), &(this->kv),
64-
this->kv.get_nks(), this->pw_rho, this->pw_big);
57+
this->kv.get_nks(), this->pw_big);
6558
}
6659

6760
// 3) read LCAO orbitals/projectors and construct the interpolation tables.
@@ -458,8 +451,13 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
458451
}
459452
this->dftu.cal_energy_correction(ucell, istep);
460453
}
461-
this->dftu.output(ucell);
462-
}
454+
this->dftu.output(ucell);
455+
// use the converged occupation matrix for next MD/Relax SCF calculation
456+
if (conv_esolver)
457+
{
458+
this->dftu.initialed_locale = true;
459+
}
460+
}
463461

464462
// 2) for deepks, calculate delta_e, output labels during electronic steps
465463
this->deepks.delta_e(ucell, this->kv, this->orb_, this->pv, this->gd, dm_vec, this->pelec->f_en, PARAM.inp);
@@ -487,12 +485,6 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
487485
}
488486
}
489487

490-
// use the converged occupation matrix for next MD/Relax SCF calculation
491-
if (PARAM.inp.dft_plus_u && conv_esolver)
492-
{
493-
this->dftu.initialed_locale = true;
494-
}
495-
496488
// control the output related to the finished iteration
497489
ModuleIO::ctrl_iter_lcao<TK, TR>(ucell, PARAM.inp, this->kv, this->pelec, *this->dmat.dm,
498490
this->pv, this->gd, this->psi, this->chr, this->p_chgmix,

source/source_esolver/esolver_ks_pw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_p
8080
//! Call before_all_runners() of ESolver_KS
8181
ESolver_KS<T, Device>::before_all_runners(ucell, inp);
8282

83-
//! setup and allocation for pelec, charge density, potentials, etc.
83+
//! setup and allocation for pelec, potentials, etc.
8484
elecstate::setup_estate_pw<T, Device>(ucell, this->kv, this->sf, this->pelec, this->chr,
8585
this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho,
8686
this->pw_rhod, this->pw_big, this->solvent, inp);
@@ -286,7 +286,7 @@ void ESolver_KS_PW<T, Device>::iter_finish(UnitCell& ucell, const int istep, int
286286
// pp projectors, liuyu 2023-10-24
287287
if (PARAM.globalv.use_uspp)
288288
{
289-
ModuleBase::matrix veff = this->pelec->pot->get_effective_v();
289+
ModuleBase::matrix veff = this->pelec->pot->get_eff_v();
290290
this->ppcell.cal_effective_D(veff, this->pw_rhod, ucell);
291291
}
292292

source/source_esolver/esolver_of.cpp

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,15 @@ void ESolver_OF::before_all_runners(UnitCell& ucell, const Input_para& inp)
7373

7474
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL");
7575

76-
XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func);
76+
// XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func);
7777
int func_type = XC_Functional::get_func_type();
7878
if (func_type > 2)
7979
{
8080
ModuleBase::WARNING_QUIT("esolver_of", "meta-GGA and Hybrid functionals are not supported by OFDFT.");
8181
}
8282

83-
// symmetry analysis should be performed every time the cell is changed
84-
if (ModuleSymmetry::Symmetry::symm_flag == 1)
85-
{
86-
ucell.symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running);
87-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SYMMETRY");
88-
}
89-
90-
// Setup the k points according to symmetry.
91-
kv.set(ucell,ucell.symm, inp.kpoint_file, inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running);
92-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
93-
94-
// print information
95-
// mohan add 2021-01-30
96-
ModuleIO::print_parameters(ucell, kv, inp);
97-
98-
// initialize the real-space uniform grid for FFT and parallel
99-
// distribution of plane waves
100-
Pgrid.init(pw_rho->nx,
101-
pw_rho->ny,
102-
pw_rho->nz,
103-
pw_rho->nplane,
104-
pw_rho->nrxx,
105-
pw_big->nbz,
106-
pw_big->bz); // mohan add 2010-07-22, update 2011-05-04
107-
// Calculate Structure factor
108-
sf.setup(&ucell, Pgrid, pw_rho);
109-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS");
83+
this->chr.init_rho(ucell, this->Pgrid, this->sf.strucFac, ucell.symm, &this->kv);
84+
this->chr.check_rho(); // check the rho
11085

11186
// initialize local pseudopotential
11287
this->locpp.init_vloc(ucell,pw_rho);
@@ -122,7 +97,7 @@ void ESolver_OF::before_all_runners(UnitCell& ucell, const Input_para& inp)
12297
// liuyu move here 2023-10-09
12398
// D in uspp need vloc, thus behind init_scf()
12499
// calculate the effective coefficient matrix for non-local pseudopotential projectors
125-
ModuleBase::matrix veff = this->pelec->pot->get_effective_v();
100+
ModuleBase::matrix veff = this->pelec->pot->get_eff_v();
126101

127102
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT POTENTIAL");
128103

@@ -214,6 +189,8 @@ void ESolver_OF::before_opt(const int istep, UnitCell& ucell)
214189
//! 1) call before_scf() of ESolver_FP
215190
ESolver_FP::before_scf(ucell, istep);
216191

192+
193+
217194
if (ucell.cell_parameter_updated)
218195
{
219196
this->dV_ = ucell.omega / this->pw_rho->nxyz;
@@ -317,10 +294,10 @@ void ESolver_OF::update_potential(UnitCell& ucell)
317294
this->kedf_manager_->get_potential(this->chr.rho,
318295
this->pphi_,
319296
this->pw_rho,
320-
this->pelec->pot->get_effective_v()); // KEDF potential
297+
this->pelec->pot->get_eff_v()); // KEDF potential
321298
for (int is = 0; is < PARAM.inp.nspin; ++is)
322299
{
323-
const double* vr_eff = this->pelec->pot->get_effective_v(is);
300+
const double* vr_eff = this->pelec->pot->get_eff_v(is);
324301
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
325302
{
326303
this->pdEdphi_[is][ir] = vr_eff[ir];
@@ -516,9 +493,9 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso
516493
this->kedf_manager_->get_potential(this->chr.rho,
517494
this->pphi_,
518495
this->pw_rho,
519-
this->pelec->pot->get_effective_v()); // KEDF potential
496+
this->pelec->pot->get_eff_v()); // KEDF potential
520497

521-
const double* vr_eff = this->pelec->pot->get_effective_v(0);
498+
const double* vr_eff = this->pelec->pot->get_eff_v(0);
522499
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
523500
{
524501
this->pdEdphi_[0][ir] = vr_eff[ir];

0 commit comments

Comments
 (0)