Skip to content

refactor(dfpt): split the oversized DFPT serial tests along the source boundaries - #8259

Merged
mohanchen merged 10 commits into
deepmodeling:developfrom
Zanthoxylum:refactor-dfpt-tests
Nov 2, 2026
Merged

mohanchen merged 10 commits into
deepmodeling:developfrom
Zanthoxylum:refactor-dfpt-tests

Conversation

@Zanthoxylum

@Zanthoxylum Zanthoxylum commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The oversized DFPT serial test files are split along the source
boundaries they exercise, with verbatim test moves and zero
production-code changes. After this PR no split test file exceeds
550 lines (was 1062 / 880 / 668), and every test file is
self-contained in gtest style: each .cpp carries its own fixture
class with only the members and helpers its cases need — no test
fixture headers.

Old file (deleted) New files Cases
dfpt_phon_serial_test.cpp (1062 lines) test_dfpt_phon_ewald.cpp (ion_ion), test_dfpt_phon_elec.cpp (accumulate_electron + nlcc fill semantics), test_dfpt_phon_asr.cpp (the apply_asr family), test_dfpt_phon.cpp (diagonalize / LO-TO / sum rule / format) 20 = 3+4+7+6
dfpt_rho_serial_test.cpp (880 lines) test_dfpt_rho.cpp (compute_drho / charge conservation / v_hartree_q / drhoc localization), test_dfpt_rho_mix.cpp (plain / Kerker / Broyden mixing family) 11 = 5+6
dfpt_q0_serial_test.cpp (668 lines) test_dfpt_q0.cpp (build_vkb_dk / compute_eps / compute_born / star rotation), test_dfpt_q0_pos.cpp (pos_matrix kinetic + nonlocal velocity) 6 = 4+2
dfpt_xc_analytic_serial_test.cpp helpers inlined back into the single file (550 lines: single-concern, single-source) 6 = 6

The pre-existing shared test utility dfpt_serial_fixture.{h,cpp}
(cell builders, real serial basis setup — compiled once per target
like dfpt_test_mocks.cpp) is untouched; each new file's fixture
derives from it and adds only what its family needs. The rho files
carry a file-local RNG in an anonymous namespace (single-translation-
unit determinism; no cross-TU sharing, hence no inline-variable
concerns).

Rationale

A test file that keeps growing is a signal that the source file under
test accumulates unrelated responsibilities ("god file"). The phon test
had grown to 1062 lines covering four disjoint families; this split
restores the one-file-one-concern mapping (coding rule 11 naming,
test_<module>.cpp), and the per-file fixtures keep every file
independent — editing one file's scaffolding never recompiles or
constrains its siblings.

What did NOT change

  • No production source file is touched (only test_serial/ files and
    its CMakeLists.txt SOURCES lists; the CTest target names and link
    structure are unchanged).
  • All test bodies are verbatim moves; beyond file headers the only
    adaptations are the per-file fixture splits, the file-local RNG
    (one reseeding call site updated to g_seed = 777u), and the
    dfpt_rho.h include living in test_dfpt_phon_elec.cpp alone.

Verification

Exact commands run (GCC 13 toolchain build dir build-gcc13, serial):

cmake -S . -B build-gcc13 && cmake --build build-gcc13 --target \
  MODULE_DFPT_phon_serial MODULE_DFPT_rho_serial MODULE_DFPT_q0_serial \
  MODULE_DFPT_xc_analytic_serial -j 20
ctest --test-dir build-gcc13 -R "MODULE_DFPT"          # 10/10 targets Passed
  • Case-count guard per target via --gtest_list_tests:
    phon 20 = 20, rho 11 = 11, q0 6 = 6, xc_analytic 6 = 6 (identical
    case names, none added or lost).
  • All four reworked targets rebuilt from deleted binaries (make exit
    code 0); full MODULE_DFPT suite re-run after every review round:
    10/10.
  • python3 tools/03_code_analysis/agent_governance_check.py --base origin/develop --head HEAD: no findings beyond the standard
    documentation-sync notice.

Documentation

No docs change needed: test-only refactor with no INPUT, interface, or
behavior change.

Governance Exception

  • Reason: test_dfpt_phon_asr.cpp and test_dfpt_rho_mix.cpp are
    named after the function family they exercise (the apply_asr
    family / the mix_drho stack) rather than after a current source
    file, because those families currently live inside dfpt_phon.cpp /
    dfpt_rho.cpp alongside unrelated concerns — rule 11's
    one-file-one-source mapping cannot be satisfied while the families
    share a source file.
  • Scope: the two test file names only; every other new test file
    matches its source exactly (ewald / elec / q0 / q0_pos / rho).
  • Risk: low — discoverability only; both files carry header
    comments naming the current source location of the family.
  • Follow-up: the companion source splits refactor(dfpt): move the ASR family to dfpt_phon_asr.cpp #8260 (dfpt_phon_asr.cpp)
    and refactor(dfpt): move the rho mixing family to dfpt_rho_mix.cpp #8262 (dfpt_rho_mix.cpp) move those families into their own
    source files, after which the names match exactly (the rho test file
    already uses the post-split name); if either split is rejected, the
    test file is renamed to match its source instead.

dfpt_phon_serial_test.cpp (1062 lines, 20 cases) becomes four files
mirroring the sources they exercise (coding rule 11): ion_ion ->
test_dfpt_phon_ewald.cpp, accumulate_electron + nlcc fill semantics ->
test_dfpt_phon_elec.cpp, the apply_asr family -> test_dfpt_phon_asr.cpp
(name anticipates the Phase-2 source split of the ASR family into
dfpt_phon_asr.cpp), diagonalize/LO-TO/sum-rule/format ->
test_dfpt_phon.cpp. The shared fixture moves to
dfpt_phon_serial_fixture.h. Verbatim test moves: case count 20 = 20,
MODULE_DFPT_phon_serial passes.
dfpt_rho_serial_test.cpp (880 lines, 11 cases) becomes test_dfpt_rho.cpp
(compute_drho brute-force/real-space references, Gamma charge
conservation, v_hartree_q closed form, the nlcc drhoc localization/phase
regression) and test_dfpt_rho_mixing.cpp (the mix_drho plain/Kerker
family and the Broyden convergence/reset regressions; the name
anticipates the Phase-2 source split into dfpt_rho_mix.cpp). The shared
RNG and fixture move to dfpt_rho_serial_fixture.h. Verbatim test moves:
case count 11 = 11, MODULE_DFPT_rho_serial passes.
dfpt_q0_serial_test.cpp (668 lines, 6 cases) becomes test_dfpt_q0.cpp
(build_vkb_dk finite difference, compute_eps, compute_born, the star
rotation regression -> dfpt_q0.cpp) and test_dfpt_q0_pos.cpp (the
pos_matrix kinetic/nonlocal velocity forms -> dfpt_q0_pos.cpp). The
shared Gamma-only fixture moves to dfpt_q0_serial_fixture.h. Verbatim
test moves: case count 6 = 6, MODULE_DFPT_q0_serial passes.
…ure header

dfpt_xc_analytic_serial_test.cpp (544 lines) carries ~130 lines of
shared reference machinery (cubic basis builder, synthetic gaussian /
cosine fields, FFT gradient / divergence references in physical units,
summed libxc vrho / vsigma) used by all six cases; the helpers move to
dfpt_xc_analytic_serial_fixture.h in a named namespace, bringing the
test file under the 500-line budget (409 lines). Verbatim moves, case
count 6 = 6, MODULE_DFPT_xc_analytic_serial passes.
@Zanthoxylum Zanthoxylum added Refactor Refactor ABACUS codes DFPT Density Functional Perturbation Theory labels Nov 2, 2026

@ABACUS-PR-BOT ABACUS-PR-BOT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review found a C++17-only declaration in the shared RNG, plus two nonblocking governance/dependency suggestions. The case bodies, assertions and numerical helper formulas are preserved. No builds or runtime tests were run.

The shared test RNG seed was an inline VARIABLE (C++17); the unit-test
configuration only raises the language level to C++14. Replace it with
an inline accessor returning a function-local static (one instance
across the target's TUs, C++11-safe) and update the reseeding call site.

dfpt_rho.h moves from the shared phon fixture header into
test_dfpt_phon_elec.cpp, the only file instantiating DFPT_Rho, so the
Ewald/ASR/core test files do not inherit the dependency.

Both affected targets rebuilt from deleted binaries (make rc=0); rho
case count 11 intact; full MODULE_DFPT ctest 10/10.
Zanthoxylum added a commit to Zanthoxylum/abacus-develop that referenced this pull request Nov 2, 2026
… file; include <algorithm>

The header comment referenced test_dfpt_phon_asr.cpp, which only exists
after the test split (deepmodeling#8259); point at the current dfpt_phon_serial_
test.cpp with the future name noted. Include <algorithm> directly for
std::max instead of relying on transitive standard-library includes.
Rebuild from deleted binaries + full MODULE_DFPT ctest: 10/10.
@mohanchen mohanchen added the Tests/Examples Issues/PR related to unit tests and integrate tests label Nov 2, 2026
# Conflicts:
#	source/source_pw/module_dfpt/test_serial/CMakeLists.txt
Per review, test fixture classes live inside the .cpp that uses them
instead of shared fixture headers: the four new headers are removed and
each file carries a trimmed per-file fixture with only the members and
helpers its cases need (the phon Ewald/ASR files get the minimal cell
base, the core file adds the LO-TO closed-form helpers, the elec file
adds the convolution references; the rho files carry a file-local RNG
in an anonymous namespace -- the cross-TU inline accessor is no longer
needed; the q0 core drops the unused gy_). The xc reference helpers
move back into the single xc test file (550 lines: single-concern,
single-source, slightly over the 500-line budget for that reason).
test_dfpt_rho_mixing.cpp is renamed to test_dfpt_rho_mix.cpp to match
the companion source split dfpt_rho_mix.cpp (deepmodeling#8262) exactly. The
pre-existing shared test utility dfpt_serial_fixture.{h,cpp} (cell
builders, real serial basis setup) is untouched. Case counts conserved
(phon 20, rho 11, q0 6, xc 6); all four targets rebuilt from deleted
binaries; full MODULE_DFPT ctest 10/10.
The 'Build extra components with Intel toolchain' run hit runner disk
exhaustion during the container pull (no space left on device writing
libmkl_sycl_blas.so.5, retried 3x) -- an infrastructure failure on that
runner, not a code change; the same check passes on the sibling
branches.
@mohanchen
mohanchen merged commit e2ce6fc into deepmodeling:develop Nov 2, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DFPT Density Functional Perturbation Theory Refactor Refactor ABACUS codes Tests/Examples Issues/PR related to unit tests and integrate tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants