Skip to content

Commit a38ba1b

Browse files
authored
Update some names of deepks tests and refactor some ofdft codes (#6754)
* 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 * delete istep in init_pot and init_scf * add an input parameter in chr.allocate * update directory names of deepks * update some small places * add of_print_info in ofdft directory and delete the function in esolver_of * move ofdft print_info to module_ofdft * fix bugs in tests * add header * small update of timer in ofdft * fix a bug related to timer * update * update
1 parent ee6535c commit a38ba1b

File tree

266 files changed

+296
-253
lines changed

Some content is hidden

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

266 files changed

+296
-253
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ OBJS_SRCPW=H_Ewald_pw.o\
738738
stress_func_onsite.o\
739739
stress_pw.o\
740740
of_stress_pw.o\
741+
of_print_info.o\
741742
symmetry_rho.o\
742743
symmetry_rhog.o\
743744
setup_psi_pw.o\

source/source_esolver/esolver_double_xc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ void ESolver_DoubleXC<TK, TR>::before_all_runners(UnitCell& ucell, const Input_p
8787

8888
// 10) inititlize the charge density
8989
this->chr_base.set_rhopw(this->pw_rhod); // mohan add 20251130
90-
this->chr_base.allocate(PARAM.inp.nspin);
90+
const bool kin_den = this->chr_base.kin_density(); // mohan add 20251202
91+
this->chr_base.allocate(PARAM.inp.nspin, kin_den);
9192
this->chr_base.init_rho(ucell, this->Pgrid, this->sf.strucFac, ucell.symm, &this->kv);
9293
this->chr_base.check_rho();
9394

@@ -156,7 +157,7 @@ void ESolver_DoubleXC<TK, TR>::before_scf(UnitCell& ucell, const int istep)
156157
}
157158

158159
XC_Functional::set_xc_type(PARAM.inp.deepks_out_base);
159-
this->pelec_base->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
160+
this->pelec_base->init_scf(ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
160161
XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func);
161162

162163
// DMR should be same size with Hamiltonian(R)

source/source_esolver/esolver_fp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp)
8888
//! 11) initialize the charge density, we need to first set xc_type,
8989
// then we can call chr.allocate()
9090
this->chr.set_rhopw(this->pw_rhod); // mohan add 20251130
91-
this->chr.allocate(inp.nspin); // mohan move this from setup_estate_pw, 20251128
91+
const bool kin_den = this->chr.kin_density(); // mohan add 20251202
92+
this->chr.allocate(inp.nspin, kin_den); // mohan move this from setup_estate_pw, 20251128
9293

9394

9495
return;

source/source_esolver/esolver_ks.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,18 @@ 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 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
65+
//! 3) setup charge mixing
7066
p_chgmix = new Charge_Mixing();
7167
p_chgmix->set_rhopw(this->pw_rho, this->pw_rhod);
7268
p_chgmix->set_mixing(inp.mixing_mode, inp.mixing_beta, inp.mixing_ndim,
7369
inp.mixing_gg0, inp.mixing_tau, inp.mixing_beta_mag, inp.mixing_gg0_mag,
7470
inp.mixing_gg0_min, inp.mixing_angle, inp.mixing_dmr, ucell.omega, ucell.tpiba);
7571
p_chgmix->init_mixing();
7672

77-
//! 5) setup plane wave for electronic wave functions
73+
//! 4) setup plane wave for electronic wave functions
7874
pw::setup_pwwfc(inp, ucell, *this->pw_rho, this->kv, this->pw_wfc);
7975

80-
//! 6) read in charge density, mohan add 2025-11-28
76+
//! 5) read in charge density, mohan add 2025-11-28
8177
//! Inititlize the charge density.
8278
this->chr.init_rho(ucell, this->Pgrid, this->sf.strucFac, ucell.symm, &this->kv, this->pw_wfc);
8379
this->chr.check_rho(); // check the rho

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(UnitCell& ucell, const int istep)
173173
LCAO_domain::dm2rho(this->dmat.dm->get_DMR_vector(), PARAM.inp.nspin, this->pelec->charge, true);
174174
}
175175
// 12.2) init_scf, should be before_scf? mohan add 2025-03-10
176-
this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
176+
this->pelec->init_scf(ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
177177

178178
// 13) initalize DM(R), which has the same size with Hamiltonian(R)
179179
auto* hamilt_lcao = dynamic_cast<hamilt::HamiltLCAO<TK, TR>*>(this->p_hamilt);

source/source_esolver/esolver_ks_lcao_tddft.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void ESolver_KS_LCAO_TDDFT<TR, Device>::runner(UnitCell& ucell, const int istep)
133133
GlobalV::ofs_running,
134134
GlobalV::ofs_warning);
135135
// need to test if correct when estep>0
136-
this->pelec->init_scf(totstep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
136+
this->pelec->init_scf(ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
137137

138138
if (totstep <= PARAM.inp.td_tend + 1)
139139
{

source/source_esolver/esolver_of.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "source_estate/cal_ux.h"
1414
#include "source_pw/module_pwdft/forces.h"
1515
#include "source_pw/module_ofdft/of_stress_pw.h"
16+
// mohan add
17+
#include "source_pw/module_ofdft/of_print_info.h"
1618

1719
namespace ModuleESolver
1820
{
@@ -92,7 +94,7 @@ void ESolver_OF::before_all_runners(UnitCell& ucell, const Input_para& inp)
9294
this->init_elecstate(ucell);
9395

9496
// calculate the total local pseudopotential in real space
95-
this->pelec->init_scf(0, ucell, Pgrid, sf.strucFac, locpp.numeric, ucell.symm); // atomic_rho, v_of_rho, set_vrs
97+
this->pelec->init_scf(ucell, Pgrid, sf.strucFac, locpp.numeric, ucell.symm); // atomic_rho, v_of_rho, set_vrs
9698

9799
// liuyu move here 2023-10-09
98100
// D in uspp need vloc, thus behind init_scf()
@@ -206,20 +208,19 @@ void ESolver_OF::before_opt(const int istep, UnitCell& ucell)
206208

207209
// Refresh the arrays
208210
delete this->psi_;
209-
this->psi_ = new psi::Psi<double>(1,
210-
PARAM.inp.nspin,
211-
this->pw_rho->nrxx,
212-
this->pw_rho->nrxx,
213-
true);
211+
this->psi_ = new psi::Psi<double>(1, PARAM.inp.nspin,
212+
this->pw_rho->nrxx, this->pw_rho->nrxx, true);
213+
214214
for (int is = 0; is < PARAM.inp.nspin; ++is)
215215
{
216216
this->pphi_[is] = this->psi_->get_pointer(is);
217217
}
218218

219219
delete this->ptemp_rho_;
220220
this->ptemp_rho_ = new Charge();
221-
this->ptemp_rho_->set_rhopw(this->pw_rho);
222-
this->ptemp_rho_->allocate(PARAM.inp.nspin);
221+
this->ptemp_rho_->set_rhopw(this->pw_rho);
222+
const bool kin_den = this->ptemp_rho_->kin_density(); // mohan add 20251202
223+
this->ptemp_rho_->allocate(PARAM.inp.nspin, kin_den);
223224

224225
for (int is = 0; is < PARAM.inp.nspin; ++is)
225226
{
@@ -234,7 +235,7 @@ void ESolver_OF::before_opt(const int istep, UnitCell& ucell)
234235
}
235236
}
236237

237-
this->pelec->init_scf(istep, ucell, Pgrid, sf.strucFac, locpp.numeric, ucell.symm);
238+
this->pelec->init_scf(ucell, Pgrid, sf.strucFac, locpp.numeric, ucell.symm);
238239

239240
Symmetry_rho srho;
240241
for (int is = 0; is < PARAM.inp.nspin; is++)
@@ -431,7 +432,8 @@ bool ESolver_OF::check_exit(bool& conv_esolver)
431432
conv_esolver = (this->of_conv_ == "energy" && energyConv) || (this->of_conv_ == "potential" && potConv)
432433
|| (this->of_conv_ == "both" && potConv && energyConv);
433434

434-
this->print_info(conv_esolver);
435+
OFDFT::print_info(this->iter_, this->iter_time, this->energy_current_, this->energy_last_,
436+
this->normdLdphi_, this->pelec, this->kedf_manager_, conv_esolver);
435437

436438
if (conv_esolver || this->iter_ >= this->max_iter_)
437439
{
@@ -567,11 +569,8 @@ void ESolver_OF::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress)
567569
{
568570
ModuleBase::matrix kinetic_stress_;
569571
kinetic_stress_.create(3, 3);
570-
this->kedf_manager_->get_stress(ucell.omega,
571-
this->chr.rho,
572-
this->pphi_,
573-
this->pw_rho,
574-
kinetic_stress_); // kinetic stress
572+
this->kedf_manager_->get_stress(ucell.omega, this->chr.rho,
573+
this->pphi_, this->pw_rho, kinetic_stress_); // kinetic stress
575574

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

source/source_esolver/esolver_of.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ class ESolver_OF : public ESolver_FP
9898
void check_direction(double* dEdtheta, double** ptemp_phi, UnitCell& ucell);
9999
void test_direction(double* dEdtheta, double** ptemp_phi, UnitCell& ucell);
100100

101-
// --------------------- output the necessary information -----------
102-
void print_info(const bool conv_esolver);
103-
104101
// --------------------- interface to blas --------------------------
105102
double inner_product(double* pa, double* pb, int length, double dV = 1)
106103
{

source/source_esolver/esolver_of_tool.cpp

Lines changed: 7 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ void ESolver_OF::allocate_array()
8686
delete this->ptemp_rho_;
8787
this->ptemp_rho_ = new Charge();
8888
this->ptemp_rho_->set_rhopw(this->pw_rho);
89-
this->ptemp_rho_->allocate(PARAM.inp.nspin);
89+
const bool kin_den = this->ptemp_rho_->kin_density(); // mohan add 20251202
90+
this->ptemp_rho_->allocate(PARAM.inp.nspin, kin_den);
9091

9192
this->theta_ = new double[PARAM.inp.nspin];
9293
this->pdLdphi_ = new double*[PARAM.inp.nspin];
@@ -378,140 +379,13 @@ void ESolver_OF::test_direction(double* dEdtheta, double** ptemp_phi, UnitCell&
378379
Parallel_Reduce::reduce_all(pseudopot_energy);
379380
temp_energy += kinetic_energy + pseudopot_energy;
380381
GlobalV::ofs_warning << i << " " << dEdtheta[0] << " " << temp_energy << std::endl;
381-
if (this->theta_[0] == 0) {
382-
std::cout << "dEdtheta " << dEdtheta[0] << std::endl;
383-
}
384-
}
382+
if (this->theta_[0] == 0)
383+
{
384+
std::cout << "dEdtheta " << dEdtheta[0] << std::endl;
385+
}
386+
}
385387
exit(0);
386388
}
387389
}
388390

389-
/**
390-
* @brief Print nessecary information to the screen,
391-
* and write the components of the total energy into running_log.
392-
*/
393-
void ESolver_OF::print_info(const bool conv_esolver)
394-
{
395-
if (this->iter_ == 0)
396-
{
397-
std::cout << " ============================= Running OFDFT "
398-
"=============================="
399-
<< std::endl;
400-
std::cout << " ITER ETOT/eV EDIFF/eV EFERMI/eV POTNORM TIME/s"
401-
<< std::endl;
402-
}
403-
404-
std::map<std::string, std::string> prefix_map = {
405-
{"cg1", "CG"},
406-
{"cg2", "CG"},
407-
{"tn", "TN"}
408-
};
409-
std::string iteration = prefix_map[PARAM.inp.of_method] + std::to_string(this->iter_);
410-
#ifdef __MPI
411-
double duration = (double)(MPI_Wtime() - this->iter_time);
412-
#else
413-
double duration
414-
= (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - this->iter_time)).count()
415-
/ static_cast<double>(1e6);
416-
#endif
417-
std::cout << " " << std::setw(8) << iteration
418-
<< std::setw(18) << std::scientific << std::setprecision(8) << this->energy_current_ * ModuleBase::Ry_to_eV
419-
<< std::setw(18) << (this->energy_current_ - this->energy_last_) * ModuleBase::Ry_to_eV
420-
<< std::setw(13) << std::setprecision(4) << this->pelec->eferm.get_efval(0) * ModuleBase::Ry_to_eV
421-
<< std::setw(13) << std::setprecision(4) << this->normdLdphi_
422-
<< std::setw(6) << std::fixed << std::setprecision(2) << duration << std::endl;
423-
424-
GlobalV::ofs_running << std::setprecision(12);
425-
GlobalV::ofs_running << std::setiosflags(std::ios::right);
426-
427-
GlobalV::ofs_running << "\nIter" << this->iter_ << ": the norm of potential is " << this->normdLdphi_ << std::endl;
428-
429-
std::vector<std::string> titles;
430-
std::vector<double> energies_Ry;
431-
std::vector<double> energies_eV;
432-
if ((PARAM.inp.out_band[0] > 0 &&
433-
((this->iter_ + 1) % PARAM.inp.out_band[0] == 0 ||
434-
conv_esolver ||
435-
this->iter_ == PARAM.inp.scf_nmax)) ||
436-
PARAM.inp.init_chg == "file")
437-
{
438-
titles.push_back("E_Total");
439-
energies_Ry.push_back(this->pelec->f_en.etot);
440-
titles.push_back("E_Kinetic");
441-
energies_Ry.push_back(this->pelec->f_en.ekinetic);
442-
titles.push_back("E_Hartree");
443-
energies_Ry.push_back(this->pelec->f_en.hartree_energy);
444-
titles.push_back("E_xc");
445-
energies_Ry.push_back(this->pelec->f_en.etxc - this->pelec->f_en.etxcc);
446-
titles.push_back("E_LocalPP");
447-
energies_Ry.push_back(this->pelec->f_en.e_local_pp);
448-
titles.push_back("E_Ewald");
449-
energies_Ry.push_back(this->pelec->f_en.ewald_energy);
450-
451-
this->kedf_manager_->record_energy(titles, energies_Ry);
452-
453-
std::string vdw_method = PARAM.inp.vdw_method;
454-
if (vdw_method == "d2") // Peize Lin add 2014-04, update 2021-03-09
455-
{
456-
titles.push_back("E_vdwD2");
457-
energies_Ry.push_back(this->pelec->f_en.evdw);
458-
}
459-
else if (vdw_method == "d3_0" || vdw_method == "d3_bj") // jiyy add 2019-05, update 2021-05-02
460-
{
461-
titles.push_back("E_vdwD3");
462-
energies_Ry.push_back(this->pelec->f_en.evdw);
463-
}
464-
if (PARAM.inp.imp_sol)
465-
{
466-
titles.push_back("E_sol_el");
467-
energies_Ry.push_back(this->pelec->f_en.esol_el);
468-
titles.push_back("E_sol_cav");
469-
energies_Ry.push_back(this->pelec->f_en.esol_cav);
470-
}
471-
if (PARAM.inp.efield_flag)
472-
{
473-
titles.push_back("E_efield");
474-
energies_Ry.push_back(elecstate::Efield::etotefield);
475-
}
476-
if (PARAM.inp.gate_flag)
477-
{
478-
titles.push_back("E_gatefield");
479-
energies_Ry.push_back(elecstate::Gatefield::etotgatefield);
480-
}
481-
}
482-
else
483-
{
484-
titles.push_back("E_Total");
485-
energies_Ry.push_back(this->pelec->f_en.etot);
486-
}
487-
488-
if (PARAM.globalv.two_fermi)
489-
{
490-
titles.push_back("E_Fermi_up");
491-
energies_Ry.push_back(this->pelec->eferm.get_efval(0));
492-
titles.push_back("E_Fermi_dw");
493-
energies_Ry.push_back(this->pelec->eferm.get_efval(1));
494-
}
495-
else
496-
{
497-
titles.push_back("E_Fermi");
498-
energies_Ry.push_back(this->pelec->eferm.get_efval(0));
499-
}
500-
energies_eV.resize(energies_Ry.size());
501-
std::transform(energies_Ry.begin(), energies_Ry.end(), energies_eV.begin(), [](double energy) {
502-
return energy * ModuleBase::Ry_to_eV;
503-
});
504-
FmtTable table(/*titles=*/{"Energy", "Rydberg", "eV"},
505-
/*nrows=*/titles.size(),
506-
/*formats=*/{"%20s", "%20.12f", "%20.12f"}, 0);
507-
table << titles << energies_Ry << energies_eV;
508-
GlobalV::ofs_running << table.str() << std::endl;
509-
510-
// reset the iter_time for the next iteration
511-
#ifdef __MPI
512-
this->iter_time = MPI_Wtime();
513-
#else
514-
this->iter_time = std::chrono::system_clock::now();
515-
#endif
516-
}
517391
} // namespace ModuleESolver

source/source_esolver/lcao_others.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void ESolver_KS_LCAO<TK, TR>::others(UnitCell& ucell, const int istep)
219219
elecstate::cal_ux(ucell);
220220

221221
// pelec should be initialized before these calculations
222-
this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
222+
this->pelec->init_scf(ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
223223

224224
// self consistent calculations for electronic ground state
225225
if (cal_type == "get_pchg")

0 commit comments

Comments
 (0)