Skip to content

Commit 47bfd69

Browse files
authored
Refactor: Refactor KEDF management in ESolver_OF (#6349)
* Refactor KEDF management in ESolver_OF - Introduced KEDF_Manager class to encapsulate KEDF functionalities, replacing individual KEDF instances (TF, VW, WT, LKT, ML) in ESolver_OF. - Updated ESolver_OF to initialize and utilize KEDF_Manager for potential, energy, energy density, and stress calculations. * Update Makefile.Objects
1 parent 8528f51 commit 47bfd69

File tree

10 files changed

+457
-341
lines changed

10 files changed

+457
-341
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ OBJS_HAMILT_OF=kedf_tf.o\
346346
kedf_vw.o\
347347
kedf_wt.o\
348348
kedf_lkt.o\
349+
kedf_manager.o\
349350

350351
OBJS_HAMILT_LCAO=hamilt_lcao.o\
351352
operator_lcao.o\

source/source_esolver/esolver_of.cpp

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ ESolver_OF::~ESolver_OF()
4747
delete[] this->task_;
4848
delete this->ptemp_rho_;
4949

50-
delete this->tf_;
51-
delete this->vw_;
52-
delete this->wt_;
53-
delete this->lkt_;
50+
delete this->kedf_manager_;
5451

5552
delete this->opt_cg_;
5653
delete this->opt_tn_;
@@ -142,7 +139,8 @@ void ESolver_OF::before_all_runners(UnitCell& ucell, const Input_para& inp)
142139
this->nelec_[0] = this->pelec->nelec_spin[0];
143140
this->nelec_[1] = this->pelec->nelec_spin[1];
144141
}
145-
this->init_kedf(inp);
142+
this->kedf_manager_ = new KEDF_Manager();
143+
this->kedf_manager_->init(inp, this->pw_rho, this->dV_, this->nelec_[0]);
146144
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT KEDF");
147145

148146
// Initialize optimization methods
@@ -159,11 +157,6 @@ void ESolver_OF::runner(UnitCell& ucell, const int istep)
159157
this->before_opt(istep, ucell);
160158
this->iter_ = 0;
161159

162-
#ifdef __MLALGO
163-
// for ML KEDF test
164-
if (PARAM.inp.of_ml_local_test) this->ml_->localTest(this->chr.rho, this->pw_rho);
165-
#endif
166-
167160
bool conv_esolver = false; // this conv_esolver is added by mohan 20250302
168161
#ifdef __MPI
169162
this->iter_time = MPI_Wtime();
@@ -228,7 +221,7 @@ void ESolver_OF::before_opt(const int istep, UnitCell& ucell)
228221
this->init_elecstate(ucell);
229222

230223
// Initialize KEDF
231-
this->init_kedf(PARAM.inp);
224+
this->kedf_manager_->init(PARAM.inp, this->pw_rho, this->dV_, this->nelec_[0]);
232225

233226
// Initialize optimization methods
234227
this->init_opt();
@@ -320,9 +313,10 @@ void ESolver_OF::update_potential(UnitCell& ucell)
320313
elecstate::cal_ux(ucell);
321314

322315
this->pelec->pot->update_from_charge(&this->chr, &ucell); // Hartree + XC + external
323-
this->kinetic_potential(this->chr.rho,
324-
this->pphi_,
325-
this->pelec->pot->get_effective_v()); // (kinetic + Hartree + XC + external) * 2 * phi
316+
this->kedf_manager_->get_potential(this->chr.rho,
317+
this->pphi_,
318+
this->pw_rho,
319+
this->pelec->pot->get_effective_v()); // KEDF potential
326320
for (int is = 0; is < PARAM.inp.nspin; ++is)
327321
{
328322
const double* vr_eff = this->pelec->pot->get_effective_v(is);
@@ -497,7 +491,7 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso
497491
//------------------------------------------------------------------
498492
if (PARAM.inp.out_elf[0] > 0)
499493
{
500-
this->kinetic_energy_density(this->chr.rho, this->pphi_, this->chr.kin_r);
494+
this->kedf_manager_->get_energy_density(this->chr.rho, this->pphi_, this->pw_rho, this->chr.kin_r);
501495
}
502496

503497
//------------------------------------------------------------------
@@ -513,43 +507,27 @@ void ESolver_OF::after_opt(const int istep, UnitCell& ucell, const bool conv_eso
513507
}
514508

515509
#ifdef __MLALGO
516-
//------------------------------------------------------------------
517-
// Check the positivity of Pauli energy
518-
//------------------------------------------------------------------
519-
if (this->of_kinetic_ == "ml")
520-
{
521-
this->tf_->get_energy(this->chr.rho);
522-
523-
std::cout << "ML Term = " << this->ml_->ml_energy
524-
<< " Ry, TF Term = " << this->tf_->tf_energy
525-
<< " Ry." << std::endl;
526-
527-
if (this->ml_->ml_energy >= this->tf_->tf_energy)
528-
{
529-
std::cout << "WARNING: ML >= TF" << std::endl;
530-
}
531-
}
532-
533510
//------------------------------------------------------------------
534511
// Generate data if needed
535512
//------------------------------------------------------------------
536513
if (PARAM.inp.of_ml_gene_data)
537514
{
538515
this->pelec->pot->update_from_charge(&this->chr, &ucell); // Hartree + XC + external
539-
this->kinetic_potential(this->chr.rho, this->pphi_, this->pelec->pot->get_effective_v()); // (kinetic + Hartree + XC + external) * 2 * phi
516+
this->kedf_manager_->get_potential(this->chr.rho,
517+
this->pphi_,
518+
this->pw_rho,
519+
this->pelec->pot->get_effective_v()); // KEDF potential
540520

541521
const double* vr_eff = this->pelec->pot->get_effective_v(0);
542522
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
543523
{
544524
this->pdEdphi_[0][ir] = vr_eff[ir];
545525
}
546526
this->pelec->eferm.set_efval(0, this->cal_mu(this->pphi_[0], this->pdEdphi_[0], this->nelec_[0]));
547-
// === temporary ===
548-
// assert(GlobalV::of_kinetic == "wt" || GlobalV::of_kinetic == "ml");
549-
// =================
527+
550528
std::cout << "Generating Training data..." << std::endl;
551529
std::cout << "mu = " << this->pelec->eferm.get_efval(0) << std::endl;
552-
this->ml_->generateTrainData(this->chr.rho, *(this->wt_), *(this->tf_), this->pw_rho, vr_eff);
530+
this->kedf_manager_->generate_ml_target(this->chr.rho, this->pw_rho, vr_eff);
553531
}
554532
#endif
555533

@@ -573,7 +551,7 @@ void ESolver_OF::after_all_runners(UnitCell& ucell)
573551
double ESolver_OF::cal_energy()
574552
{
575553
this->pelec->cal_energies(2);
576-
double kinetic_energy = this->kinetic_energy(); // kinetic energy
554+
double kinetic_energy = this->kedf_manager_->get_energy(); // kinetic energy
577555
double pseudopot_energy = 0.; // electron-ion interaction energy
578556
for (int is = 0; is < PARAM.inp.nspin; ++is)
579557
{
@@ -609,7 +587,11 @@ void ESolver_OF::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress)
609587
{
610588
ModuleBase::matrix kinetic_stress_;
611589
kinetic_stress_.create(3, 3);
612-
this->kinetic_stress(kinetic_stress_);
590+
this->kedf_manager_->get_stress(this->pelec->omega,
591+
this->chr.rho,
592+
this->pphi_,
593+
this->pw_rho,
594+
kinetic_stress_); // kinetic stress
613595

614596
OF_Stress_PW ss(this->pelec, this->pw_rho);
615597
ss.cal_stress(stress, kinetic_stress_, ucell, &ucell.symm, this->locpp, &sf, &kv);

source/source_esolver/esolver_of.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
#include "esolver_fp.h"
55
#include "source_base/opt_DCsrch.h"
66
#include "source_base/opt_TN.hpp"
7-
#include "source_estate/module_charge/charge.h"
8-
#include "source_pw/hamilt_ofdft/kedf_lkt.h"
9-
#include "source_pw/hamilt_ofdft/kedf_tf.h"
10-
#include "source_pw/hamilt_ofdft/kedf_vw.h"
11-
#include "source_pw/hamilt_ofdft/kedf_wt.h"
12-
#include "source_pw/hamilt_ofdft/kedf_ml.h"
7+
#include "source_pw/hamilt_ofdft/kedf_manager.h"
138
#include "source_psi/psi.h"
149

1510
namespace ModuleESolver
@@ -35,13 +30,7 @@ class ESolver_OF : public ESolver_FP
3530
private:
3631
// ======================= variables ==========================
3732
// ---------- the kinetic energy density functionals ----------
38-
KEDF_TF* tf_ = nullptr;
39-
KEDF_vW* vw_ = nullptr;
40-
KEDF_WT* wt_ = nullptr;
41-
KEDF_LKT* lkt_ = nullptr;
42-
#ifdef __MLALGO
43-
KEDF_ML* ml_ = nullptr;
44-
#endif
33+
KEDF_Manager* kedf_manager_ = nullptr; // KEDF manager, which will be initialized in before_all_runners
4534

4635
// ----------------- the optimization methods ------------------
4736
ModuleBase::Opt_CG* opt_cg_ = nullptr;
@@ -120,13 +109,6 @@ class ESolver_OF : public ESolver_FP
120109
return innerproduct;
121110
}
122111

123-
// ---------------------- interfaces to KEDF ------------------------
124-
void init_kedf(const Input_para& inp);
125-
void kinetic_potential(double** prho, double** pphi, ModuleBase::matrix& rpot);
126-
double kinetic_energy();
127-
void kinetic_energy_density(double** prho, double** pphi, double** rtau);
128-
void kinetic_stress(ModuleBase::matrix& kinetic_stress);
129-
130112
// ---------------------- interfaces to optimization methods --------
131113
void init_opt();
132114
void get_direction(UnitCell& ucell);

0 commit comments

Comments
 (0)