Skip to content

Commit c86d36c

Browse files
author
abacus_fixer
committed
deltaspin: remove GlobalV::ofs_running dependency from module_deltaspin
Pass ofs_running explicitly through function parameters instead of referencing the GlobalV::ofs_running global variable directly inside the deltaspin module. This follows ABACUS rule #1 (do not increase cross-layer control through GlobalV) and matches the existing design pattern used by print_Mi / print_Mag_Force. Changes: - spin_constrain.h: add std::ostream& ofs_running parameter to 7 functions (run_lambda_loop, run_lambda_linear_scan, print_header, print_termination, check_rms_stop, check_restriction, check_gradient_decay). Added <fstream> include. Removed default value for 'rerun' and 'print' parameters to keep call sites explicit (rule deepmodeling#5). - lambda_loop_helper.cpp: replace all GlobalV::ofs_running with the ofs_running parameter; remove #include source_base/global_variable.h. - lambda_loop.cpp: replace all GlobalV::ofs_running with the ofs_running parameter; remove #include source_base/global_variable.h (kept #include source_io/module_parameter/parameter.h since PARAM is still used). - basic_funcs.cpp: remove unused #include source_base/global_variable.h. - template_helpers.cpp: update TK=double stub signatures to match. - deltaspin_lcao.h/cpp: add ofs_running parameter to run_deltaspin_lambda_loop_lcao; add <iosfwd> include. - deltaspin_pw.h/cpp: add ofs_running parameter to run_deltaspin_lambda_loop; add <iosfwd> include. - esolver_ks_pw.cpp / esolver_ks_lcao.cpp: pass GlobalV::ofs_running at the call site (the esolver layer owns the dependency). - template_helpers_test.cpp: pass std::cout at test call sites. Result: module_deltaspin no longer references GlobalV::ofs_running. The only remaining GlobalV:: usages in the module are historical MPI constants (NPROC_IN_POOL, RANK_IN_POOL) in cal_mw*.cpp, untouched by this change.
1 parent bed203a commit c86d36c

12 files changed

Lines changed: 119 additions & 102 deletions

File tree

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,19 +430,19 @@ void ESolver_KS_LCAO<TK, TR>::hamilt2rho_single(UnitCell& ucell, int istep, int
430430
spinconstrain::SpinConstrain<TK>& sc = spinconstrain::SpinConstrain<TK>::getScInstance();
431431
if (this->inp_->sc_lambda_strategy == "linear_scan")
432432
{
433-
sc.run_lambda_linear_scan(iter - 1);
433+
sc.run_lambda_linear_scan(iter - 1, GlobalV::ofs_running);
434434
skip_solve = true;
435435
}
436436
else if (!sc.mag_converged() && this->drho > 0 && this->drho < this->inp_->sc_scf_thr)
437437
{
438-
sc.run_lambda_loop(iter - 1);
438+
sc.run_lambda_loop(iter - 1, true, GlobalV::ofs_running);
439439
this->ds_rms_ = sc.get_last_rms_error();
440440
sc.set_mag_converged(true);
441441
skip_solve = true;
442442
}
443443
else if (sc.mag_converged())
444444
{
445-
sc.run_lambda_loop(iter - 1);
445+
sc.run_lambda_loop(iter - 1, true, GlobalV::ofs_running);
446446
this->ds_rms_ = sc.get_last_rms_error();
447447
skip_solve = true;
448448
}

source/source_esolver/esolver_ks_pw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void ESolver_KS_PW<T, Device>::hamilt2rho_single(UnitCell& ucell, const int iste
228228
bool skip_charge = this->inp_->calculation == "nscf" ? true : false;
229229

230230
// run the inner lambda loop to contrain atomic moments with the DeltaSpin method
231-
bool skip_solve = pw::run_deltaspin_lambda_loop(iter - 1, this->drho, *this->inp_);
231+
bool skip_solve = pw::run_deltaspin_lambda_loop(iter - 1, this->drho, *this->inp_, GlobalV::ofs_running);
232232
if (skip_solve)
233233
{
234234
// Fetch the most recent DeltaSpin RMS for display in the SCF iteration table.

source/source_lcao/module_deltaspin/basic_funcs.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <iostream>
44
#include "source_base/constants.h"
55
#include "source_base/formatter.h"
6-
#include "source_base/global_variable.h"
76

87
double maxval_abs_2d(const std::vector<ModuleBase::Vector3<double>>& array)
98
{

source/source_lcao/module_deltaspin/deltaspin_lcao.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ void cal_mi_lcao_wrapper(const int iter, const Input_para& inp)
125125
template <typename TK>
126126
bool run_deltaspin_lambda_loop_lcao(const int iter,
127127
const double drho,
128-
const Input_para& inp)
128+
const Input_para& inp,
129+
std::ostream& ofs_running)
129130
{
130131
bool skip_solve = false;
131132

@@ -136,14 +137,14 @@ bool run_deltaspin_lambda_loop_lcao(const int iter,
136137
if (!sc.mag_converged() && drho > 0 && drho < inp.sc_scf_thr)
137138
{
138139
/// Charge density is stable enough: optimize lambda for the first time
139-
sc.run_lambda_loop(iter);
140+
sc.run_lambda_loop(iter, true, ofs_running);
140141
sc.set_mag_converged(true);
141142
skip_solve = true;
142143
}
143144
else if (sc.mag_converged())
144145
{
145146
/// Already converged: refine lambda for the current charge density
146-
sc.run_lambda_loop(iter);
147+
sc.run_lambda_loop(iter, true, ofs_running);
147148
skip_solve = true;
148149
}
149150
}
@@ -174,9 +175,11 @@ template void cal_mi_lcao_wrapper<std::complex<double>>(const int iter, const In
174175

175176
template bool run_deltaspin_lambda_loop_lcao<double>(const int iter,
176177
const double drho,
177-
const Input_para& inp);
178+
const Input_para& inp,
179+
std::ostream& ofs_running);
178180
template bool run_deltaspin_lambda_loop_lcao<std::complex<double>>(const int iter,
179181
const double drho,
180-
const Input_para& inp);
182+
const Input_para& inp,
183+
std::ostream& ofs_running);
181184

182185
} // namespace ModuleESolver

source/source_lcao/module_deltaspin/deltaspin_lcao.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef DELTASPIN_LCAO_H
22
#define DELTASPIN_LCAO_H
33

4+
#include <iosfwd>
5+
46
#include "source_cell/unitcell.h"
57
#include "source_cell/klist.h"
68
#include "source_io/module_parameter/input_parameter.h"
@@ -60,7 +62,8 @@ void cal_mi_lcao_wrapper(const int iter, const Input_para& inp);
6062
template <typename TK>
6163
bool run_deltaspin_lambda_loop_lcao(const int iter,
6264
const double drho,
63-
const Input_para& inp);
65+
const Input_para& inp,
66+
std::ostream& ofs_running);
6467

6568
} // namespace ModuleESolver
6669

source/source_lcao/module_deltaspin/lambda_loop.cpp

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "basic_funcs.h"
1010
#include "source_base/constants.h"
11-
#include "source_base/global_variable.h"
1211
#include "source_io/module_parameter/parameter.h"
1312

1413
/**
@@ -72,7 +71,7 @@
7271
* - decay_grad thresholds are not too aggressive
7372
*/
7473
template <>
75-
void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int outer_step, bool rerun)
74+
void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int outer_step, bool rerun, std::ostream& ofs_running)
7675
{
7776
int nat = this->get_nat();
7877
int ntype = this->get_ntype();
@@ -107,7 +106,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
107106

108107
double inner_loop_duration = 0.0;
109108

110-
this->print_header();
109+
this->print_header(ofs_running);
111110

112111
// =============================================================
113112
// MAIN OPTIMIZATION LOOP
@@ -129,9 +128,9 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
129128
// Save initial lambda: for unconstrained components (constrain==0), set to 0
130129
where_fill_scalar_else_2d(this->constrain_, 0, zero, this->lambda_, initial_lambda);
131130

132-
print_2d(" initial lambda (eV/uB): ", initial_lambda, this->nspin_, ModuleBase::Ry_to_eV, GlobalV::ofs_running);
133-
print_2d(" initial spin (uB): ", spin, this->nspin_, 1.0, GlobalV::ofs_running);
134-
print_2d(" target spin (uB): ", this->target_mag_, this->nspin_, 1.0, GlobalV::ofs_running);
131+
print_2d(" initial lambda (eV/uB): ", initial_lambda, this->nspin_, ModuleBase::Ry_to_eV, ofs_running);
132+
print_2d(" initial spin (uB): ", spin, this->nspin_, 1.0, ofs_running);
133+
print_2d(" target spin (uB): ", this->target_mag_, this->nspin_, 1.0, ofs_running);
135134
i_step++;
136135
}
137136
else
@@ -171,7 +170,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
171170
new_spin = this->Mi_;
172171

173172
// Check if gradient dM/dlambda has decayed below threshold
174-
bool GradLessThanBound = this->check_gradient_decay(new_spin, spin, delta_lambda, dnu_last_step);
173+
bool GradLessThanBound = this->check_gradient_decay(new_spin, spin, delta_lambda, dnu_last_step, false, ofs_running);
175174
if (i_step >= this->nsc_min_ && GradLessThanBound)
176175
{
177176
// Gradient has decayed: further optimization yields diminishing returns
@@ -186,8 +185,8 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
186185
- iterstart)).count() / static_cast<double>(1e6);
187186
#endif
188187
inner_loop_duration += duration;
189-
GlobalV::ofs_running << " Total TIME(s) = " << inner_loop_duration << std::endl;
190-
this->print_termination();
188+
ofs_running << " Total TIME(s) = " << inner_loop_duration << std::endl;
189+
this->print_termination(ofs_running);
191190
break;
192191
}
193192
spin = new_spin;
@@ -260,7 +259,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
260259
- iterstart)).count() / static_cast<double>(1e6);
261260
#endif
262261
inner_loop_duration += duration;
263-
if (this->check_rms_stop(outer_step, i_step, rms_error, duration, inner_loop_duration))
262+
if (this->check_rms_stop(outer_step, i_step, rms_error, duration, inner_loop_duration, ofs_running))
264263
{
265264
// Save RMS for ESolver to display in the SCF iteration table.
266265
this->last_rms_error_ = rms_error;
@@ -283,15 +282,15 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
283282
}
284283
mean_error = sum_2d(temp_1) / nat;
285284
rms_error = std::sqrt(mean_error);
286-
GlobalV::ofs_running<<" Current RMS: "<<rms_error<<std::endl;
285+
ofs_running<<" Current RMS: "<<rms_error<<std::endl;
287286

288287
// If RMS is still large after full update, recursively rerun
289288
// with higher precision (full PW solver instead of subspace only)
290289
if(rms_error > this->current_sc_thr_ * 10 && rerun == true && this->higher_mag_prec == true)
291290
{
292291
std::cout<<" DeltaSpin: RMS error too large ("<<rms_error<<"), rerun inner loop with full PW solver"<<std::endl;
293292
std::cout<<std::endl;
294-
this->run_lambda_loop(outer_step, false);
293+
this->run_lambda_loop(outer_step, false, ofs_running);
295294
}
296295
}
297296
break;
@@ -317,7 +316,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
317316
}
318317

319318
// Cap step size to prevent overshooting
320-
this->check_restriction(search, alpha_trial);
319+
this->check_restriction(search, alpha_trial, ofs_running);
321320

322321
// =============================================================
323322
// CUMULATIVE STEP UPDATE
@@ -356,7 +355,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
356355

357356
// Find optimal step size via linear interpolation
358357
alpha_opt = this->cal_alpha_opt(spin, spin_plus, alpha_trial);
359-
this->check_restriction(search, alpha_opt);
358+
this->check_restriction(search, alpha_opt, ofs_running);
360359

361360
// Correct dnu: dnu += (alpha_opt - alpha_trial) * search
362361
alpha_plus = alpha_opt - alpha_trial;
@@ -423,7 +422,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_loop(int out
423422
* step, lambda_eV_uB, Mi_x_0, Mi_y_0, Mi_z_0, Mi_x_1, ...
424423
*/
425424
template <>
426-
void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(int outer_step)
425+
void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(int outer_step, std::ostream& ofs_running)
427426
{
428427
int nat = this->get_nat();
429428
int ntype = this->get_ntype();
@@ -433,7 +432,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
433432
int nsteps = PARAM.inp.sc_scan_steps;
434433

435434
if (nsteps <= 0) {
436-
GlobalV::ofs_running << " [DS-DIAG] linear_scan: sc_scan_steps <= 0, skipping" << std::endl;
435+
ofs_running << " [DS-DIAG] linear_scan: sc_scan_steps <= 0, skipping" << std::endl;
437436
return;
438437
}
439438

@@ -442,15 +441,15 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
442441
double lambda_end_ry = lambda_end / ModuleBase::Ry_to_eV;
443442
double lambda_step = (lambda_end_ry - lambda_start_ry) / (nsteps - 1);
444443

445-
GlobalV::ofs_running << "\n" << std::string(80, '=') << std::endl;
446-
GlobalV::ofs_running << " [DS-DIAG] === LINEAR LAMBDA SCAN START ===" << std::endl;
447-
GlobalV::ofs_running << " [DS-DIAG] Scan range: " << lambda_start << " -> " << lambda_end << " eV/uB" << std::endl;
448-
GlobalV::ofs_running << " [DS-DIAG] Number of steps: " << nsteps << std::endl;
449-
GlobalV::ofs_running << " [DS-DIAG] Lambda step size: " << lambda_step * ModuleBase::Ry_to_eV << " eV/uB" << std::endl;
450-
GlobalV::ofs_running << " [DS-DIAG] nat = " << nat << ", ntype = " << ntype << std::endl;
451-
GlobalV::ofs_running << " [DS-DIAG] nspin_ = " << this->nspin_ << ", npol_ = " << this->npol_ << std::endl;
452-
GlobalV::ofs_running << " [DS-DIAG] p_operator = " << (this->p_operator ? "valid" : "NULL") << std::endl;
453-
GlobalV::ofs_running << " [DS-DIAG] constrain_ size = " << this->constrain_.size() << std::endl;
444+
ofs_running << "\n" << std::string(80, '=') << std::endl;
445+
ofs_running << " [DS-DIAG] === LINEAR LAMBDA SCAN START ===" << std::endl;
446+
ofs_running << " [DS-DIAG] Scan range: " << lambda_start << " -> " << lambda_end << " eV/uB" << std::endl;
447+
ofs_running << " [DS-DIAG] Number of steps: " << nsteps << std::endl;
448+
ofs_running << " [DS-DIAG] Lambda step size: " << lambda_step * ModuleBase::Ry_to_eV << " eV/uB" << std::endl;
449+
ofs_running << " [DS-DIAG] nat = " << nat << ", ntype = " << ntype << std::endl;
450+
ofs_running << " [DS-DIAG] nspin_ = " << this->nspin_ << ", npol_ = " << this->npol_ << std::endl;
451+
ofs_running << " [DS-DIAG] p_operator = " << (this->p_operator ? "valid" : "NULL") << std::endl;
452+
ofs_running << " [DS-DIAG] constrain_ size = " << this->constrain_.size() << std::endl;
454453

455454
// Check if any constraints are defined; if not, set all atoms as constrained
456455
bool has_constraints = false;
@@ -462,7 +461,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
462461
}
463462

464463
if (!has_constraints) {
465-
GlobalV::ofs_running << " [DS-DIAG] No constraints found in STRU, setting all atoms as constrained" << std::endl;
464+
ofs_running << " [DS-DIAG] No constraints found in STRU, setting all atoms as constrained" << std::endl;
466465
for (int ia = 0; ia < nat; ia++) {
467466
if (this->nspin_ == 4) {
468467
this->constrain_[ia] = ModuleBase::Vector3<int>(1, 1, 1);
@@ -474,11 +473,11 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
474473
}
475474

476475
for (int ia = 0; ia < nat; ia++) {
477-
GlobalV::ofs_running << " [DS-DIAG] Atom " << ia << " constrain = ("
476+
ofs_running << " [DS-DIAG] Atom " << ia << " constrain = ("
478477
<< this->constrain_[ia].x << ", " << this->constrain_[ia].y << ", " << this->constrain_[ia].z << ")"
479478
<< " target_mag = (" << this->target_mag_[ia].x << ", " << this->target_mag_[ia].y << ", " << this->target_mag_[ia].z << ")" << std::endl;
480479
}
481-
GlobalV::ofs_running << std::string(80, '=') << "\n" << std::endl;
480+
ofs_running << std::string(80, '=') << "\n" << std::endl;
482481

483482
// Save initial lambda to restore after scan
484483
std::vector<ModuleBase::Vector3<double>> initial_lambda(nat, 0.0);
@@ -530,7 +529,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
530529
}
531530
}
532531

533-
GlobalV::ofs_running << " [DS-DIAG] === Scan step " << istep << "/" << nsteps
532+
ofs_running << " [DS-DIAG] === Scan step " << istep << "/" << nsteps
534533
<< " lambda = " << lambda_val_ev << " eV/uB ===" << std::endl;
535534

536535
// Compute magnetic moments at current lambda
@@ -551,22 +550,22 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
551550
}
552551
ofs_scan << std::endl;
553552

554-
GlobalV::ofs_running << " [DS-DIAG] lambda = " << lambda_val_ev << " eV/uB" << std::endl;
553+
ofs_running << " [DS-DIAG] lambda = " << lambda_val_ev << " eV/uB" << std::endl;
555554
for (int ia = 0; ia < nat; ia++) {
556-
GlobalV::ofs_running << " [DS-DIAG] Atom " << ia << " Mi = ("
555+
ofs_running << " [DS-DIAG] Atom " << ia << " Mi = ("
557556
<< this->Mi_[ia].x << ", "
558557
<< this->Mi_[ia].y << ", "
559558
<< this->Mi_[ia].z << ") uB" << std::endl;
560559
}
561-
GlobalV::ofs_running << std::endl;
560+
ofs_running << std::endl;
562561
}
563562

564563
// =============================================================
565564
// CONSISTENCY CHECK: restore initial lambda and recompute Mi
566565
// to verify that the lambda->Mi mapping is numerically stable
567566
// after multiple lambda updates in the scan loop
568567
// =============================================================
569-
GlobalV::ofs_running << " [DS-DIAG] === Consistency check: restoring initial lambda ===" << std::endl;
568+
ofs_running << " [DS-DIAG] === Consistency check: restoring initial lambda ===" << std::endl;
570569
this->lambda_ = initial_lambda;
571570
this->cal_mw_from_lambda(nsteps);
572571

@@ -580,9 +579,9 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
580579
}
581580
ofs_scan << std::endl;
582581

583-
GlobalV::ofs_running << " [DS-DIAG] lambda = " << lambda_start << " eV/uB (restored)" << std::endl;
582+
ofs_running << " [DS-DIAG] lambda = " << lambda_start << " eV/uB (restored)" << std::endl;
584583
for (int ia = 0; ia < nat; ia++) {
585-
GlobalV::ofs_running << " [DS-DIAG] Atom " << ia << " Mi = ("
584+
ofs_running << " [DS-DIAG] Atom " << ia << " Mi = ("
586585
<< this->Mi_[ia].x << ", "
587586
<< this->Mi_[ia].y << ", "
588587
<< this->Mi_[ia].z << ") uB" << std::endl;
@@ -599,11 +598,11 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
599598
if (diff > max_mi_diff) max_mi_diff = diff;
600599
ofs_scan << "# Atom " << ia << " dM = (" << dx << ", " << dy << ", " << dz << ") uB" << std::endl;
601600
}
602-
GlobalV::ofs_running << " [DS-DIAG] Max Mi difference between step 0 and init_recheck: " << max_mi_diff << " uB" << std::endl;
601+
ofs_running << " [DS-DIAG] Max Mi difference between step 0 and init_recheck: " << max_mi_diff << " uB" << std::endl;
603602
if (max_mi_diff > 1e-8) {
604-
GlobalV::ofs_running << " [DS-DIAG] WARNING: Mi mapping may be inconsistent after multiple lambda updates!" << std::endl;
603+
ofs_running << " [DS-DIAG] WARNING: Mi mapping may be inconsistent after multiple lambda updates!" << std::endl;
605604
} else {
606-
GlobalV::ofs_running << " [DS-DIAG] OK: Mi mapping is consistent." << std::endl;
605+
ofs_running << " [DS-DIAG] OK: Mi mapping is consistent." << std::endl;
607606
}
608607
ofs_scan << "# Max Mi difference: " << max_mi_diff << " uB" << std::endl;
609608

@@ -612,10 +611,10 @@ void spinconstrain::SpinConstrain<std::complex<double>>::run_lambda_linear_scan(
612611
// Restore original lambda values (already restored above, but explicit for clarity)
613612
this->lambda_ = initial_lambda;
614613

615-
GlobalV::ofs_running << std::string(80, '=') << std::endl;
616-
GlobalV::ofs_running << " [DS-DIAG] === LINEAR LAMBDA SCAN COMPLETE ===" << std::endl;
617-
GlobalV::ofs_running << " [DS-DIAG] Results written to: lambda_scan_results.dat" << std::endl;
618-
GlobalV::ofs_running << std::string(80, '=') << "\n" << std::endl;
614+
ofs_running << std::string(80, '=') << std::endl;
615+
ofs_running << " [DS-DIAG] === LINEAR LAMBDA SCAN COMPLETE ===" << std::endl;
616+
ofs_running << " [DS-DIAG] Results written to: lambda_scan_results.dat" << std::endl;
617+
ofs_running << std::string(80, '=') << "\n" << std::endl;
619618

620619
return;
621620
}

0 commit comments

Comments
 (0)