Skip to content

Refactor output formats of deltaspin method - #7857

Open
mohanchen wants to merge 11 commits into
deepmodeling:developfrom
mohanchen:2026-08-24-b
Open

Refactor output formats of deltaspin method#7857
mohanchen wants to merge 11 commits into
deepmodeling:developfrom
mohanchen:2026-08-24-b

Conversation

@mohanchen

@mohanchen mohanchen commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Refactor output formats of deltaspin method.

Now the screen output of deltaspin method looks like this (17_DS_DFTU/14_PW_DS_S4_XYZ), an extra parameter RMS apperas only in the deltaspin method:
image

ABACUS Dev and others added 11 commits August 24, 2026 17:53
Include source_base/global_variable.h in lambda_loop_helper.cpp so the
helper methods can reach GlobalV::ofs_running. For now only print_header
is mirrored; subsequent commits will migrate the remaining helper
functions one at a time so every step builds independently.

stdout is preserved so interactive users and CaptureStdout-based unit
tests (lambda_loop_hlp_test.cpp / deltaspin_core_test.cpp) keep
observing the same terminal output they expect.
Duplicate the two std::cout lines ("Inner optimization for lambda ends."
and the closing separator) to GlobalV::ofs_running. The internal
print_2d calls are left alone for now; they will be redirected in a
separate commit that touches the shared helper so the change is
centralized.

The unit-test assertions for print_termination in lambda_loop_hlp_test.cpp
are commented out, so there is no test drift to account for in this step.
Duplicate Step (Outer -- Inner) RMS/TIME line, both convergence exit
messages, and both cumulative Total TIME lines into
GlobalV::ofs_running alongside the existing std::cout stream.

print_termination() at the exit point already emits duplicates thanks
to the previous commit; no extra forwarding needed here.

CaptureStdout-based tests in lambda_loop_hlp_test.cpp remain valid
because stdout is preserved unmodified.
The two diagnostic lines emitted when the trial step is clipped
("alpha after restrict" and "boundary after") now also land in
running_scf.log. The check_restriction signature stays untouched so
ABACUS rule 5 (no default-argument additions to existing interfaces)
is satisfied and lambda_loop.cpp does not need a call-site update.
Duplicate the verbose debug block (print=true) and the early-exit
"Reach limitation of current step" message into running_scf.log.

Intentionally not touched in this commit:
  * printf-based [GRAD-DECAY] and [ALPHA-OPT] warnings inside
    check_gradient_decay and cal_alpha_opt — they are only triggered
    under guard conditions that do not fire for the reference
    14_PW_DS_S4_XYZ test case and are addressed separately.
  * The print_2d call in the debug block — that helper has its own
    dedicated redirect commit next.
Include source_base/global_variable.h in basic_funcs.cpp and emit a
duplicate copy of every print_2d line to GlobalV::ofs_running in
addition to the caller-supplied ostream (which defaults to std::cout).

All existing callers benefit from this single change:
  * lambda_loop.cpp    : initial lambda, initial spin, target spin
  * lambda_loop_helper : after-optimization spin/lambda, diagonal
                        gradient diagnostics in check_gradient_decay

Formatting is preserved byte-for-byte by constructing each ATOM line
into a local std::string before streaming to both sinks.
MODULE_LCAO_deltaspin_basic_func_test keeps passing because
stdout still contains the expected output lines.
Include source_base/global_variable.h in lambda_loop.cpp and emit
duplicate copies into running_scf.log for:
  * "Total TIME(s) = ..."  (gradient-decay exit branch)
  * "Current RMS: ..."      (post-inner-loop PW verification)
  * "Error: RMS error is too large, rerun the loop"

The per-step check_rms_stop/print_termination outputs and the
initial lambda/target spin tables already go to both sinks via
earlier commits. No helper signatures changed (ABACUS rule 5
compliance) and no call-site updates are required.
…g_scf.log

Duplicate every [DS-DIAG] line from run_lambda_linear_scan into
GlobalV::ofs_running alongside std::cout, so the normally-off
diagnostic lambda-scan helper also persists its findings to
running_scf.log.

Scope includes: scan-start banner and parameters, the auto-constrain
notice, per-atom constrain/target dumps, per-step header and Mi
output, consistency-check summary lines, and the completion banner.

Only stdout (default) copies are tested; no unit-test modifications
required because all CaptureStdout expectations remain satisfied.
Append a copy of the per-SCF-iteration summary buffer produced by
print_scf_iterinfo (the line with ITER, TMAGX/TMAGY/TMAGZ/AMAG,
ETOT, EDIFF, DRHO, TIME) to GlobalV::ofs_running so users can get
a compact convergence overview from running_scf.log without having
to scroll back through the interactive terminal output.

The std::flush is preserved on both sinks so large MPI jobs still
see progress line by line during long runs. elecstate already
includes source_base/global_variable.h so no new #include was
necessary.
Replace previous dual-write (cout + ofs_running) with strict routing:
information goes to either screen or log, not both.

Screen keeps only:
- SCF iteration compact table (ITER TMAGX ... DRHO TIME/s)
- per-outer-step convergence summary (Meet/Reach criterion, Total TIME)
- error/early-termination notices (Reach limitation, Error: RMS)

Log receives (with leading-space style consistent with running_scf.log):
- per-inner-step Step (Outer -- Inner) RMS = ... TIME(s) = ...
- initial/after-optimization spin and lambda tables (per-atom info kept
  off screen to avoid flooding on large systems)
- alpha after restrict / boundary after diagnostics
- diagonal gradient / maximum gradient appears at / maximum gradient
- Current RMS, Total TIME(s)
- all [DS-DIAG] linear-scan debug lines

print_2d now writes only to the stream passed by caller; no implicit
dual output. Test assertion updated to expect leading space in ATOM rows.
Add a new RMS column to the SCF iteration table on screen, placed after
DRHO (and DKIN if present) and before TIME. The column shows the most
recent DeltaSpin lambda-loop RMS in scientific notation. Non-DeltaSpin
calculations do not show this column.

Implementation:
- SpinConstrain: add last_rms_error_ member + get_last_rms_error() getter,
  set in run_lambda_loop when check_rms_stop returns true.
- ESolver_KS: add ds_rms_ member, reset in iter_init to 0.0 when
  sc_mag_switch is on (column shown, value 0 before lambda loop runs)
  or -1.0 otherwise (column hidden).
- ESolver_KS_PW / ESolver_KS_LCAO: after run_lambda_loop, fetch
  sc.get_last_rms_error() into ds_rms_.
- print_etot / print_scf_iterinfo: add ds_rms parameter (default -1.0);
  when >= 0, append an RMS column (same width/format as DRHO) to both
  header and data rows.
- Move the 'DeltaSpin: lambda loop converged' summary line from screen
  to running_scf.log (with leading space and trailing blank line).
  Abnormal summaries (max steps, early-termination, RMS too large)
  remain on screen so users see them immediately.
@mohanchen mohanchen added Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0 labels Aug 24, 2026
@mohanchen
mohanchen requested review from hujieting and ieiue August 24, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

collinear/non-collinear/SOC/delta-spin Issues related to SOC Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant