Skip to content

Commit b1689b8

Browse files
mohanchenFisherd99
authored andcommitted
Update ESolver, delete pelec->charge, instead using chr defined in ESolver_fp directly (deepmodeling#5963)
* reorganize esolver_ks_lcao.h * delete some useless files in esolver_ks.h * change pelec->charge to this->chr * will delete pelec->charge in near future, we just directly use chr defined in esolver_fp * fix problem in ML_KEDF * update this->chr
1 parent 9884ed8 commit b1689b8

17 files changed

+187
-229
lines changed

source/module_esolver/esolver_fp.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ namespace ModuleESolver
2323

2424
ESolver_FP::ESolver_FP()
2525
{
26-
// pw_rho = new ModuleBase::PW_Basis();
27-
// LCAO basis doesn't support GPU acceleration on FFT currently
2826
std::string fft_device = PARAM.inp.device;
27+
28+
// LCAO basis doesn't support GPU acceleration on FFT currently
2929
if(PARAM.inp.basis_type == "lcao")
3030
{
3131
fft_device = "cpu";
3232
}
33+
3334
pw_rho = new ModulePW::PW_Basis_Big(fft_device, PARAM.inp.precision);
34-
if ( PARAM.globalv.double_grid)
35+
if (PARAM.globalv.double_grid)
3536
{
3637
pw_rhod = new ModulePW::PW_Basis_Big(fft_device, PARAM.inp.precision);
3738
}
@@ -44,6 +45,7 @@ ESolver_FP::ESolver_FP()
4445
pw_big = static_cast<ModulePW::PW_Basis_Big*>(pw_rhod);
4546
pw_big->setbxyz(PARAM.inp.bx, PARAM.inp.by, PARAM.inp.bz);
4647
sf.set(pw_rhod, PARAM.inp.nbspline);
48+
4749
}
4850

4951
ESolver_FP::~ESolver_FP()
@@ -140,7 +142,7 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso
140142
// 2) write fermi energy
141143
ModuleIO::output_efermi(conv_esolver, this->pelec->eferm.ef);
142144

143-
// 3) update delta rho for charge extrapolation
145+
// 3) update delta_rho for charge extrapolation
144146
CE.update_delta_rho(ucell, &(this->chr), &(this->sf));
145147

146148
if (istep % PARAM.inp.out_interval == 0)
@@ -153,13 +155,13 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso
153155
double* data = nullptr;
154156
if (PARAM.inp.dm_to_rho)
155157
{
156-
data = this->pelec->charge->rho[is];
157-
this->pw_rhod->real2recip(this->pelec->charge->rho[is], this->pelec->charge->rhog[is]);
158+
data = this->chr.rho[is];
159+
this->pw_rhod->real2recip(this->chr.rho[is], this->chr.rhog[is]);
158160
}
159161
else
160162
{
161-
data = this->pelec->charge->rho_save[is];
162-
this->pw_rhod->real2recip(this->pelec->charge->rho_save[is], this->pelec->charge->rhog_save[is]);
163+
data = this->chr.rho_save[is];
164+
this->pw_rhod->real2recip(this->chr.rho_save[is], this->chr.rhog_save[is]);
163165
}
164166
std::string fn =PARAM.globalv.global_out_dir + "/SPIN" + std::to_string(is + 1) + "_CHG.cube";
165167
ModuleIO::write_vdata_palgrid(Pgrid,
@@ -176,7 +178,7 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso
176178
{
177179
fn =PARAM.globalv.global_out_dir + "/SPIN" + std::to_string(is + 1) + "_TAU.cube";
178180
ModuleIO::write_vdata_palgrid(Pgrid,
179-
this->pelec->charge->kin_r_save[is],
181+
this->chr.kin_r_save[is],
180182
is,
181183
PARAM.inp.nspin,
182184
istep,
@@ -217,7 +219,7 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso
217219
fn,
218220
istep,
219221
this->pw_rhod,
220-
this->pelec->charge,
222+
&this->chr,
221223
&(ucell),
222224
this->pelec->pot->get_fixed_v(),
223225
this->solvent);
@@ -226,11 +228,11 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso
226228
// 6) write ELF
227229
if (PARAM.inp.out_elf[0] > 0)
228230
{
229-
this->pelec->charge->cal_elf = true;
231+
this->chr.cal_elf = true;
230232
Symmetry_rho srho;
231233
for (int is = 0; is < PARAM.inp.nspin; is++)
232234
{
233-
srho.begin(is, *(this->pelec->charge), this->pw_rhod, ucell.symm);
235+
srho.begin(is, this->chr, this->pw_rhod, ucell.symm);
234236
}
235237

236238
std::string out_dir =PARAM.globalv.global_out_dir;
@@ -242,8 +244,8 @@ void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_eso
242244
out_dir,
243245
istep,
244246
PARAM.inp.nspin,
245-
this->pelec->charge->rho,
246-
this->pelec->charge->kin_r,
247+
this->chr.rho,
248+
this->chr.kin_r,
247249
this->pw_rhod,
248250
this->Pgrid,
249251
&(ucell),
@@ -260,9 +262,9 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
260262
{
261263
// only G-vector and K-vector are changed due to the change of lattice
262264
// vector FFT grids do not change!!
263-
pw_rho->initgrids(ucell.lat0, ucell.latvec, pw_rho->nx, pw_rho->ny, pw_rho->nz);
264-
pw_rho->collect_local_pw();
265-
pw_rho->collect_uniqgg();
265+
this->pw_rho->initgrids(ucell.lat0, ucell.latvec, pw_rho->nx, pw_rho->ny, pw_rho->nz);
266+
this->pw_rho->collect_local_pw();
267+
this->pw_rho->collect_uniqgg();
266268

267269
if (PARAM.globalv.double_grid)
268270
{
@@ -292,7 +294,7 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
292294
this->CE.update_all_dis(ucell);
293295
this->CE.extrapolate_charge(&(this->Pgrid),
294296
ucell,
295-
this->pelec->charge,
297+
&this->chr,
296298
&(this->sf),
297299
GlobalV::ofs_running,
298300
GlobalV::ofs_warning);
@@ -327,7 +329,7 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
327329
std::stringstream ss;
328330
ss << PARAM.globalv.global_out_dir << "SPIN" << is + 1 << "_CHG_INI.cube";
329331
ModuleIO::write_vdata_palgrid(this->Pgrid,
330-
this->pelec->charge->rho[is],
332+
this->chr.rho[is],
331333
is,
332334
PARAM.inp.nspin,
333335
istep,
@@ -368,8 +370,8 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool&
368370
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
369371
{
370372
std::complex<double>** rhog_tot
371-
= (PARAM.inp.dm_to_rho) ? this->pelec->charge->rhog : this->pelec->charge->rhog_save;
372-
double** rhor_tot = (PARAM.inp.dm_to_rho) ? this->pelec->charge->rho : this->pelec->charge->rho_save;
373+
= (PARAM.inp.dm_to_rho) ? this->chr.rhog : this->chr.rhog_save;
374+
double** rhor_tot = (PARAM.inp.dm_to_rho) ? this->chr.rho : this->chr.rho_save;
373375
for (int is = 0; is < PARAM.inp.nspin; is++)
374376
{
375377
this->pw_rhod->real2recip(rhor_tot[is], rhog_tot[is]);
@@ -386,12 +388,12 @@ void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool&
386388

387389
if (XC_Functional::get_ked_flag())
388390
{
389-
std::vector<std::complex<double>> kin_g_space(PARAM.inp.nspin * this->pelec->charge->ngmc, {0.0, 0.0});
391+
std::vector<std::complex<double>> kin_g_space(PARAM.inp.nspin * this->chr.ngmc, {0.0, 0.0});
390392
std::vector<std::complex<double>*> kin_g;
391393
for (int is = 0; is < PARAM.inp.nspin; is++)
392394
{
393-
kin_g.push_back(kin_g_space.data() + is * this->pelec->charge->ngmc);
394-
this->pw_rhod->real2recip(this->pelec->charge->kin_r_save[is], kin_g[is]);
395+
kin_g.push_back(kin_g_space.data() + is * this->chr.ngmc);
396+
this->pw_rhod->real2recip(this->chr.kin_r_save[is], kin_g[is]);
395397
}
396398
ModuleIO::write_rhog(PARAM.globalv.global_out_dir + PARAM.inp.suffix + "-TAU-DENSITY.restart",
397399
PARAM.globalv.gamma_only_pw || PARAM.globalv.gamma_only_local,

source/module_esolver/esolver_fp.h

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@
22
#define ESOLVER_FP_H
33

44
#include "esolver.h"
5+
6+
//! plane wave basis
57
#include "module_basis/module_pw/pw_basis.h"
8+
9+
//! symmetry analysis
610
#include "module_cell/module_symmetry/symmetry.h"
11+
12+
//! electronic states
713
#include "module_elecstate/elecstate.h"
14+
15+
//! charge extrapolation
816
#include "module_elecstate/module_charge/charge_extra.h"
17+
18+
//! solvation model
919
#include "module_hamilt_general/module_surchem/surchem.h"
20+
21+
//! local pseudopotential
1022
#include "module_hamilt_pw/hamilt_pwdft/VL_in_pw.h"
23+
24+
//! structure factor related to plane wave basis
1125
#include "module_hamilt_pw/hamilt_pwdft/structure_factor.h"
1226

1327
#include <fstream>
1428

29+
1530
//! The First-Principles (FP) Energy Solver Class
1631
/**
1732
* This class represents components that needed in
@@ -22,7 +37,7 @@
2237

2338
namespace ModuleESolver
2439
{
25-
class ESolver_FP : public ESolver
40+
class ESolver_FP: public ESolver
2641
{
2742
public:
2843
//! Constructor
@@ -49,39 +64,34 @@ class ESolver_FP : public ESolver
4964
//! ------------------------------------------------------------------------------
5065
elecstate::ElecState* pelec = nullptr; ///< Electronic states
5166

52-
//! ------------------------------------------------------------------------------
67+
//! K points in Brillouin zone
68+
K_Vectors kv;
5369

5470
//! Electorn charge density
5571
Charge chr;
5672

57-
//! Structure factors that used with plane-wave basis set
58-
Structure_Factor sf;
59-
60-
//! K points in Brillouin zone
61-
K_Vectors kv;
62-
63-
//! Plane-wave basis set for charge density
73+
//! pw_rho: Plane-wave basis set for charge density
74+
//! pw_rhod: same as pw_rho for NCPP. Here 'd' stands for 'dense',
75+
//! dense grid for for uspp, used for ultrasoft augmented charge density.
76+
//! charge density and potential are defined on dense grids,
77+
//! but effective potential needs to be interpolated on smooth grids in order to compute Veff|psi>
6478
ModulePW::PW_Basis* pw_rho;
79+
ModulePW::PW_Basis* pw_rhod; //! dense grid for USPP
80+
ModulePW::PW_Basis_Big* pw_big; ///< [temp] pw_basis_big class
6581

6682
//! parallel for rho grid
6783
Parallel_Grid Pgrid;
6884

69-
//! pointer to local pseudopotential
70-
pseudopot_cell_vl locpp;
85+
//! Structure factors that used with plane-wave basis set
86+
Structure_Factor sf;
7187

72-
/**
73-
* @brief same as pw_rho for ncpp. Here 'd' stands for 'dense'
74-
* dense grid for for uspp, used for ultrasoft augmented charge density.
75-
* charge density and potential are defined on dense grids,
76-
* but effective potential needs to be interpolated on smooth grids in order to compute Veff|psi>
77-
*/
78-
ModulePW::PW_Basis* pw_rhod;
79-
ModulePW::PW_Basis_Big* pw_big; ///< [temp] pw_basis_big class
88+
//! local pseudopotentials
89+
pseudopot_cell_vl locpp;
8090

81-
//! Charge extrapolation
91+
//! charge extrapolation method
8292
Charge_Extra CE;
8393

84-
// solvent model
94+
//! solvent model
8595
surchem solvent;
8696
};
8797
} // namespace ModuleESolver

source/module_esolver/esolver_ks.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "module_io/write_istate_info.h"
1111
#include "module_parameter/parameter.h"
1212
#include "module_elecstate/elecstate_print.h"
13+
#include "module_hsolver/hsolver.h"
1314

1415
#include <ctime>
1516
#include <iostream>
@@ -367,7 +368,7 @@ void ESolver_KS<T, Device>::hamilt2density(UnitCell& ucell, const int istep, con
367368
// double drho = this->estate.caldr2();
368369
// EState should be used after it is constructed.
369370

370-
drho = p_chgmix->get_drho(pelec->charge, PARAM.inp.nelec);
371+
drho = p_chgmix->get_drho(&this->chr, PARAM.inp.nelec);
371372
hsolver_error = 0.0;
372373
if (iter == 1 && PARAM.inp.calculation != "nscf")
373374
{
@@ -389,7 +390,7 @@ void ESolver_KS<T, Device>::hamilt2density(UnitCell& ucell, const int istep, con
389390

390391
this->hamilt2density_single(ucell, istep, iter, diag_ethr);
391392

392-
drho = p_chgmix->get_drho(pelec->charge, PARAM.inp.nelec);
393+
drho = p_chgmix->get_drho(&this->chr, PARAM.inp.nelec);
393394

394395
hsolver_error = hsolver::cal_hsolve_error(PARAM.inp.basis_type,
395396
PARAM.inp.esolver_type,
@@ -521,7 +522,7 @@ void ESolver_KS<T, Device>::iter_init(UnitCell& ucell, const int istep, const in
521522
}
522523

523524
// 1) save input rho
524-
this->pelec->charge->save_rho_before_sum_band();
525+
this->chr.save_rho_before_sum_band();
525526
}
526527

527528
template <typename T, typename Device>
@@ -551,9 +552,9 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
551552

552553
// compute magnetization, only for LSDA(spin==2)
553554
ucell.magnet.compute_magnetization(ucell.omega,
554-
this->pelec->charge->nrxx,
555-
this->pelec->charge->nxyz,
556-
this->pelec->charge->rho,
555+
this->chr.nrxx,
556+
this->chr.nxyz,
557+
this->chr.rho,
557558
this->pelec->nelec_spin.data());
558559

559560
if (PARAM.globalv.ks_run)
@@ -628,11 +629,11 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
628629
}
629630
else
630631
{
631-
p_chgmix->mix_rho(pelec->charge); // update chr->rho by mixing
632+
p_chgmix->mix_rho(&this->chr); // update chr->rho by mixing
632633
}
633634
if (PARAM.inp.scf_thr_type == 2)
634635
{
635-
pelec->charge->renormalize_rho(); // renormalize rho in R-space would
636+
this->chr.renormalize_rho(); // renormalize rho in R-space would
636637
// induce a error in K-space
637638
}
638639
//----------charge mixing done-----------
@@ -644,7 +645,7 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
644645

645646
// be careful! conv_esolver is bool, not double !! Maybe a bug 20250302 by mohan
646647
MPI_Bcast(&conv_esolver, 1, MPI_DOUBLE, 0, BP_WORLD);
647-
MPI_Bcast(pelec->charge->rho[0], this->pw_rhod->nrxx, MPI_DOUBLE, 0, BP_WORLD);
648+
MPI_Bcast(this->chr.rho[0], this->pw_rhod->nrxx, MPI_DOUBLE, 0, BP_WORLD);
648649
#endif
649650

650651
// update potential
@@ -676,7 +677,7 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
676677
double dkin = 0.0; // for meta-GGA
677678
if (XC_Functional::get_ked_flag())
678679
{
679-
dkin = p_chgmix->get_dkin(pelec->charge, PARAM.inp.nelec);
680+
dkin = p_chgmix->get_dkin(&this->chr, PARAM.inp.nelec);
680681
}
681682

682683

0 commit comments

Comments
 (0)