Skip to content

SDFT DOS: pure-function window guard, runtime quality warnings, unit tests - #8383

Draft
LiYuqiii wants to merge 2 commits into
deepmodeling:developfrom
LiYuqiii:sdft-dos-quality
Draft

LiYuqiii wants to merge 2 commits into
deepmodeling:developfrom
LiYuqiii:sdft-dos-quality

Conversation

@LiYuqiii

@LiYuqiii LiYuqiii commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Reminder

  • I have read AGENTS.md and docs/developers_guide/agent_governance.md.
  • I have linked an issue or explained why this PR does not need one.
  • I have added adequate unit tests and/or case tests, or explained why not.
  • I have listed the exact verification commands run and their results.
  • I have described user-visible behavior changes, including INPUT parameter changes.
  • I have explained core-module impact for ESolver / HSolver / ElecState / Hamilt / Operator / Psi.
  • I have requested any needed governance exception below.

Linked Issue

Addresses #8379 (part 3/3). Depends on #8382; this branch is based on it and will be rebased
onto develop once #8382 is merged.

Unit Tests and/or Case Tests for my changes

  • Commands run:
  • Result summary:
    • the new MODULE_PW_STO_DOS_QUALITY_UTs (13 cases) passes, as do the existing
      MODULE_PW_STO_FUNC_UTs and MODULE_PW_Sto_Tool_UTs
    • healthy parameters (dos_sigma 20 eV, dos_edelta_ev 0.5, dos_nche 600): 0 warnings and
      ∫ DOS dE = 12806.6, i.e. the curve does not change
    • bad parameters (dos_sigma 0.07 eV, dos_edelta_ev 0.05, dos_nche 100): two
      WARNING: DOS: ... lines (order needed ~1e4, range/sigma prohibitive) and the run completes;
      max |DOS| = 34.0 eV^-1 instead of the historical 1e52
  • Checks not run, with reason: no new case test is added under tests/; both behaviour groups are
    pure functions covered by the unit test and the end-to-end effect is covered by tests/06_SDFT.

What's changed?

New module ModuleIO::sto_dos_quality (pure functions, no PARAM/GlobalV):

  • sanitize_che_range(emin, emax, ecutwfc, det_eigs) repairs the Chebyshev window so that it covers
    the known deterministic eigenvalues and the plane-wave spectrum top (1.05*ecutwfc), replaces a
    pathological positive lower bound and enforces a minimum width. Healthy input is returned bit for
    bit unchanged and the function is idempotent. It replaces the two inline copies of the heuristic in
    Sto_Tool::check_che_op and Sto_DOS::decide_param and takes the cutoff and the eigenvalues as
    arguments. The interval is not widened to fit a requested output window: at a fixed order that
    lowers the resolution and can hide a resolved peak, so the window is clipped to the spectrum
    instead (part 2/3).
  • check_dos_parameters(range, sigma, de, nche) reports dos_edelta_ev vs dos_sigma mismatches and
    a dos_nche that cannot resolve range/dos_sigma (with the estimated order);
    check_dos_result(min_dos, max_abs_t) reports a curve that goes visibly negative or an output
    energy outside the interval. Both only return messages, so the caller logs them as
    WARNING: DOS: ... and never aborts a long run.

Files: new module_stodft/sto_dos_quality.{h,cpp} and module_stodft/unittests/test_sto_dos_quality.cpp,
plus the wiring in sto_dos.cpp, sto_tool.cpp, module_stodft/CMakeLists.txt,
module_stodft/test/CMakeLists.txt, module_stodft/unittests/CMakeLists.txt and the Makefile object
list.

Governance Notes

  • INPUT/docs changes: no new keyword and no default change; the messages are runtime warnings.
    There is no documentation update required for this change.
  • Core module impact: module_stodft only. The new module has no global-state dependency, which is
    also what makes it unit-testable.
  • Exceptions requested: Global dependency budget, see below.

Governance Exception

  • Rule: Global dependency budget
  • Reason: the diagnostics are written through GlobalV::ofs_running, the channel the SDFT stage
    already uses, and the sanitiser has to know the plane-wave cutoff (PARAM.inp.ecutwfc). Both are
    read once per call; the cutoff is part of the repair arithmetic and stays after the logging cleanup.
  • Scope: source/source_pw/module_stodft/sto_dos.cpp and sto_tool.cpp; sto_dos_quality.* adds no
    global reference.
  • User or maintenance risk: low. Warnings and repairs only, every check is a no-op for healthy input.
  • Why the normal rule cannot be followed now: no output stream and no cutoff are passed into
    module_stodft; threading them through would change the ESolver <-> module_stodft interface.
  • Follow-up cleanup plan: pass an output stream (or a small logging context) and the cutoff from the
    esolver, then drop the GlobalV::ofs_running / PARAM.inp.ecutwfc reads here.
  • Requested approver: ABACUS maintainer

Four defects made SDFT/DOS unreliable at high electronic temperature:

* Math_Chebyshev::checkconverge() treats tmin == 0 as its "uninitialized"
  sentinel, so a positive trial Rayleigh quotient can leave Emin > 0 and exclude
  every bound state; the upper bound is underestimated in the same path.
* the DOS stage re-derived the range from the input defaults instead of keeping
  the converged SCF range, and Stochastic_WF::nchi was never initialized, so the
  range probe always used random vectors even for MDFT.
* dos_scale widened the reported window by 0.5% per side without any relation to
  the Chebyshev interval, so a grid point could sit outside the spectral range.
* the entropy density formed 1 - f by subtraction and returned 0 for |x| > 36,
  which lifted the sqrt(g) coefficient tail from ~1e-19 to ~1e-12, and
  Stochastic_Iter::dot() ignored the arrays it was given, so the printed
  Chebyshev "precision" of the stochastic trace was not the quantity it claimed.

Fixes: keep the converged SCF range in the DOS stage, initialize nchi, repair a
positive automatic lower bound and an underestimated upper bound at the call
sites (an explicitly given emin_sto is preserved, since a positive spectrum is
valid), clip the requested output window to the spectral interval instead of
widening the interval, evaluate the entropy density with s = min(f, 1-f) and
log1p(-s), and make dot() contract the arrays that the caller passes.

The range probe now also carries one independent random vector next to the real
stochastic orbitals: with `nbands_sto all` those orbitals are coordinate
vectors, so a handful of them can miss a diagonal state of another invariant
subspace. The new unit test for the entropy kernels fails on the unpatched
sto_func.cpp and passes here; dos_nche >= 2 is documented in its INPUT metadata.
Move the Chebyshev-window repair and the DOS diagnostics of the SDFT solver into
one small, global-state-free module (`module_stodft/sto_dos_quality`) and cover
it with unit tests:

* `sanitize_che_range()` repairs the window so that it covers the known
  deterministic eigenvalues and the plane-wave spectrum top (`1.05*ecutwfc`),
  replaces a pathological positive lower bound and enforces a minimum width.
  Healthy input is returned bit for bit unchanged and the function is
  idempotent.  It replaces the two inline copies of the heuristic in
  `Sto_Tool::check_che_op` and `Sto_DOS::decide_param` and takes the cutoff and
  the eigenvalues as arguments instead of reading them from the caller's
  globals.  The interval is deliberately *not* widened to fit a requested DOS
  output grid: at a fixed Chebyshev order that lowers the resolution, so the
  output window is clipped to the spectrum instead (part 2/3).

* `check_dos_parameters()` reports `dos_edelta_ev` vs `dos_sigma` mismatches and
  a `dos_nche` that cannot resolve `range/dos_sigma` (with the estimated order);
  `check_dos_result()` reports a curve that goes visibly negative or an output
  energy outside the interval.  Both only return messages: the caller logs them
  as `WARNING: DOS: ...` and never aborts a long run.

Adds `unittests/test_sto_dos_quality.cpp` (13 cases).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants