Skip to content

Commit c1023ca

Browse files
Critsium-xyclaude
andauthored
Refactor: remove PARAM usage from hsolver.cpp and two redundant reads (deepmodeling#7706)
Group A (redundant reads -- the value was already available locally): - hsolver_pw.cpp: `PARAM.globalv.use_uspp` -> `this->use_uspp`. HSolverPW already receives use_uspp through its constructor (injected from PARAM.globalv.use_uspp at the esolver level), so the direct read was duplicating an existing member. - hsolver_lcao.cpp: `PARAM.inp.nspin` -> `nspin`. HSolverLCAO::solve() already takes nspin as an argument; it is now threaded into parakSolve() instead of being re-read from PARAM. Group B (hsolver.cpp, now completely PARAM-free): set_diagethr_ks() and set_diagethr_sdft() were already pure parameter-based functions except for two leaked reads of PARAM.inp.scf_thr / PARAM.inp.nelec in their nscf branches. - set_diagethr_ks(): added `scf_thr_in`. The nscf branch used PARAM.inp.nelec even though the function already had a `nelec_in` parameter; both are now `nelec_in`. This is behaviour-preserving because the only call site passes PARAM.inp.nelec for that argument. - set_diagethr_sdft(): added `nelec_in` and `scf_thr_in`. - Dropped the now-unused parameter.h include and added the <algorithm> / <cmath> includes it was relying on transitively. Call sites in source_esolver/esolver_ks.cpp updated accordingly; these are the only external callers of the two functions. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 7be31be commit c1023ca

6 files changed

Lines changed: 29 additions & 15 deletions

File tree

source/source_esolver/esolver_ks.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ void ESolver_KS::iter_init(UnitCell& ucell, const int istep, const int iter)
184184
{
185185
diag_ethr = hsolver::set_diagethr_ks(PARAM.inp.basis_type, PARAM.inp.esolver_type,
186186
PARAM.inp.calculation, PARAM.inp.init_chg, PARAM.inp.precision, istep, iter,
187-
drho, PARAM.inp.pw_diag_thr, diag_ethr, PARAM.inp.nelec);
187+
drho, PARAM.inp.pw_diag_thr, diag_ethr, PARAM.inp.nelec, PARAM.inp.scf_thr);
188188
}
189189
else if (PARAM.inp.esolver_type == "sdft")
190190
{
191191
diag_ethr = hsolver::set_diagethr_sdft(PARAM.inp.basis_type, PARAM.inp.esolver_type,
192192
PARAM.inp.calculation, PARAM.inp.init_chg, istep, iter, drho,
193-
PARAM.inp.pw_diag_thr, diag_ethr, PARAM.inp.nbands, esolver_KS_ne);
193+
PARAM.inp.pw_diag_thr, diag_ethr, PARAM.inp.nbands, esolver_KS_ne,
194+
PARAM.inp.nelec, PARAM.inp.scf_thr);
194195
}
195196

196197
// save input charge density (rho)

source/source_hsolver/hsolver.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "hsolver.h"
22

33
#include "source_base/global_function.h"
4-
#include "source_io/module_parameter/parameter.h"
4+
5+
#include <algorithm>
6+
#include <cmath>
57

68
namespace hsolver
79
{
@@ -16,7 +18,8 @@ double set_diagethr_ks(const std::string basis_type,
1618
const double drho,
1719
const double pw_diag_thr_init,
1820
const double diag_ethr_in,
19-
const double nelec_in)
21+
const double nelec_in,
22+
const double scf_thr_in)
2023
{
2124
double res_diag_ethr = diag_ethr_in;
2225

@@ -27,7 +30,7 @@ double set_diagethr_ks(const std::string basis_type,
2730
{
2831
if (res_diag_ethr - 1e-2 > -1e-5)
2932
{
30-
res_diag_ethr = std::max(1e-13, 0.1 * std::min(1e-2, PARAM.inp.scf_thr / PARAM.inp.nelec));
33+
res_diag_ethr = std::max(1e-13, 0.1 * std::min(1e-2, scf_thr_in / nelec_in));
3134
}
3235
}
3336
else if (iter == 1)
@@ -97,15 +100,17 @@ double set_diagethr_sdft(const std::string basis_type,
97100
const double pw_diag_thr_init,
98101
const double diag_ethr_in,
99102
const int nband_in,
100-
const double stoiter_ks_ne_in)
103+
const double stoiter_ks_ne_in,
104+
const double nelec_in,
105+
const double scf_thr_in)
101106
{
102107
double res_diag_ethr = diag_ethr_in;
103108

104109
if (basis_type == "pw" && esolver_type == "sdft")
105110
{
106111
if (calculation_in == "nscf")
107112
{
108-
res_diag_ethr = std::max(std::min(1e-5, 0.1 * PARAM.inp.scf_thr / std::max(1.0, PARAM.inp.nelec)), 1e-12);
113+
res_diag_ethr = std::max(std::min(1e-5, 0.1 * scf_thr_in / std::max(1.0, nelec_in)), 1e-12);
109114
}
110115
else if (iter == 1)
111116
{
@@ -124,7 +129,7 @@ double set_diagethr_sdft(const std::string basis_type,
124129
}
125130
else
126131
{
127-
if (nband_in > 0 && stoiter_ks_ne_in > 1e-6) //PARAM.inp.nbands > 0 && this->stoiter.KS_ne > 1e-6
132+
if (nband_in > 0 && stoiter_ks_ne_in > 1e-6)
128133
{
129134
res_diag_ethr = std::min(res_diag_ethr, 0.1 * drho / std::max(1.0, stoiter_ks_ne_in));
130135
}

source/source_hsolver/hsolver.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ double set_diagethr_ks(const std::string basis_type,
1717
const double drho,
1818
const double pw_diag_thr_init,
1919
const double diag_ethr_in,
20-
const double nelec_in);
20+
const double nelec_in,
21+
const double scf_thr_in);
2122

2223
double set_diagethr_sdft(const std::string basis_type,
2324
const std::string esolver_type,
@@ -29,7 +30,9 @@ double set_diagethr_sdft(const std::string basis_type,
2930
const double pw_diag_thr_init,
3031
const double diag_ethr_in,
3132
const int nband_in,
32-
const double stoiter_ks_ne_in);
33+
const double stoiter_ks_ne_in,
34+
const double nelec_in,
35+
const double scf_thr_in);
3336

3437

3538
// reset diagethr according to drho and hsolver_error

source/source_hsolver/hsolver_lcao.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void HSolverLCAO<TK, Device>::solve(hamilt::Hamilt<TK>* pHamilt,
6262
if (PARAM.globalv.kpar_lcao > 1
6363
&& (this->method == "genelpa" || this->method == "elpa" || this->method == "scalapack_gvx" || this->method == "lapack"))
6464
{
65-
this->parakSolve(pHamilt, psi, pes, PARAM.globalv.kpar_lcao);
65+
this->parakSolve(pHamilt, psi, pes, PARAM.globalv.kpar_lcao, nspin);
6666
} else
6767
#endif
6868
if (PARAM.globalv.kpar_lcao == 1)
@@ -192,7 +192,8 @@ template <typename T, typename Device>
192192
void HSolverLCAO<T, Device>::parakSolve(hamilt::Hamilt<T>* pHamilt,
193193
psi::Psi<T>& psi,
194194
elecstate::ElecState* pes,
195-
int kpar)
195+
const int kpar,
196+
const int nspin)
196197
{
197198
#ifdef __MPI
198199
ModuleBase::timer::start("HSolverLCAO", "parakSolve");
@@ -202,7 +203,7 @@ void HSolverLCAO<T, Device>::parakSolve(hamilt::Hamilt<T>* pHamilt,
202203
int nks = psi.get_nk();
203204
int nrow = this->ParaV->get_global_row_size();
204205
int nb2d = this->ParaV->get_block_size();
205-
k2d.set_para_env(psi.get_nk(), nrow, nb2d, GlobalV::NPROC, GlobalV::MY_RANK, PARAM.inp.nspin);
206+
k2d.set_para_env(psi.get_nk(), nrow, nb2d, GlobalV::NPROC, GlobalV::MY_RANK, nspin);
206207
/// set psi_pool
207208
const int zero = 0;
208209
int coord_col = k2d.get_p2D_pool()->get_coord_col();

source/source_hsolver/hsolver_lcao.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ class HSolverLCAO
2828
private:
2929
void hamiltSolvePsiK(hamilt::Hamilt<TK>* hm, psi::Psi<TK>& psi, double* eigenvalue); // for kpar_lcao == 1
3030

31-
void parakSolve(hamilt::Hamilt<TK>* pHamilt, psi::Psi<TK>& psi, elecstate::ElecState* pes, int kpar); // for kpar_lcao > 1
31+
void parakSolve(hamilt::Hamilt<TK>* pHamilt,
32+
psi::Psi<TK>& psi,
33+
elecstate::ElecState* pes,
34+
const int kpar,
35+
const int nspin); // for kpar_lcao > 1
3236

3337
// The solving algorithm using cusolver is different from others, so a separate function is needed
3438
void parakSolve_cusolver(hamilt::Hamilt<TK>* pHamilt,

source/source_hsolver/hsolver_pw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void HSolverPW<T, Device>::solve(hamilt::Hamilt<T, Device>* pHamilt,
212212
elecstate::calEBand(_pes_pw->ekb,_pes_pw->wg,_pes_pw->f_en);
213213
if (skip_charge)
214214
{
215-
if (PARAM.globalv.use_uspp)
215+
if (this->use_uspp)
216216
{
217217
reinterpret_cast<elecstate::ElecStatePW<T, Device>*>(pes)->cal_becsum(psi);
218218
}

0 commit comments

Comments
 (0)