Skip to content

[Refactor] Organize cleans of source_base and clean source_psi's dependency#7386

Draft
Critsium-xy wants to merge 7 commits into
deepmodeling:developfrom
Critsium-xy:decouple_psi_npol
Draft

[Refactor] Organize cleans of source_base and clean source_psi's dependency#7386
Critsium-xy wants to merge 7 commits into
deepmodeling:developfrom
Critsium-xy:decouple_psi_npol

Conversation

@Critsium-xy
Copy link
Copy Markdown
Collaborator

@Critsium-xy Critsium-xy commented May 27, 2026

In the process of cleaning source_base, I injected many parameters of parameter.h as static member in modules in source_base. I moved them into a same file to make it clean.

This PR also removes psi.cpp's bad dependency.

…i.cpp

Store npol as a Psi<T> member instead of reading PARAM.inp.nspin inside get_npol(). Copy/converting constructors and operator= propagate npol from the source Psi. Canonical allocators (psi_prepare::allocate_psi for PW, Setup_Psi::allocate_psi for LCAO) and the two spinor-aware Wannier PW construction sites inject npol via set_npol(). Other Psi construction sites (TDDFT psi_laststep, LCAO+PW psi_local, sDFT stochastic Psi, OFDFT, double-XC, intermediate buffers) keep the default npol=1 -- verified no caller queries get_npol() on them. psi.cpp no longer depends on source_io/module_parameter/parameter.h.
@Critsium-xy Critsium-xy reopened this May 27, 2026
…ection

Plan A (storing npol as a Psi<T> member and threading set_npol() through every construction site) broke nspin==4 calculations because the hpsi_func lambda in hsolver_pw.cpp constructs psi_wrapper via the T* pointer constructor and never sets npol, so to_range() returned half the spinor-padded range. Fixing this would require tracking npol at every Psi wrapper construction site, of which there are many.

Switch to Plan C, mirroring the existing g_quit_out_dir injection pattern: source_base/tool_quit.{h,cpp} exposes set_global_npol() / get_global_npol() backed by an anonymous-namespace int defaulting to 1; Driver::reading() injects PARAM.globalv.npol next to the existing set_quit_out_dir() call; Psi::get_npol() reads ModuleBase::get_global_npol(). Behavior is byte-for-byte identical to the original PARAM.inp.nspin == 4 ? 2 : 1, since globalv.npol is computed from that exact mapping. psi.cpp no longer depends on parameter.h, which was the decoupling goal.

Reverts the Plan A bookkeeping: Psi::npol member, set_npol() setter, copy/operator= propagation, canonical-allocator injections in psi_prepare.cpp / setup_psi.cpp, Wannier injections, and the hsolver_pw.cpp lambda capture changes.
…lobalV

Move the three function-pair injection points that were living in tool_quit (set_quit_out_dir / get_global_out_dir, set_quit_calculation, set_global_npol / get_global_npol) to GlobalV bare extern variables in global_variable.{h,cpp}, matching the existing style of MY_RANK / ofs_running there.

Renames: g_quit_out_dir -> GlobalV::global_out_dir, g_quit_calculation -> GlobalV::calculation, g_global_npol -> GlobalV::npol. tool_quit no longer hosts injection state; only WARNING/QUIT/WARNING_QUIT/CHECK_WARNING_QUIT remain there. Driver writes the three values via direct assignment; psi.cpp reads GlobalV::npol; ORB_atomic_lm.cpp / ORB_nonlocal_lm.cpp read GlobalV::global_out_dir.
…dsp_cluster_id into GlobalV

MODULE_HAMILT_XCTest_VXC and _GRADCORR manually list base-layer sources instead of linking the 'base' library, so they did not pull in global_variable.cpp. With GlobalV::npol now read by psi.cpp, the link step fails with an undefined reference. Add global_variable.cpp to both tests' SOURCES lists -- the same pattern they already use for matrix.cpp, timer.cpp, etc.

Also move the g_dsp_cluster_id injection out of source_base/module_device/memory_op into GlobalV, for consistency with the other consolidated base-layer injection points: GlobalV::dsp_cluster_id (declared unconditionally, default 0) replaces the anonymous-namespace global, set_dsp_cluster_id() and get_dsp_cluster_id() are deleted, driver.cpp assigns directly, pw_basis_k.cpp reads directly, and memory_op.cpp's internal read uses GlobalV::dsp_cluster_id.
…ble.cpp

Previous fix wrongly added source_base/global_variable.cpp to MODULE_HAMILT_XCTest_VXC and _GRADCORR SOURCES, which caused a duplicate-definition link error: xc3_mock.h (included by test_xc3.cpp and test_xc5.cpp) already defines GlobalV::ofs_running and GlobalV::ofs_device locally, so pulling in global_variable.cpp produced two definitions of each. Revert the SOURCES additions and instead add 'int npol = 1;' to the mock's namespace GlobalV block, alongside the existing local ofs_running / ofs_device. Now psi.cpp's GlobalV::npol reference is satisfied by the mock's definition (one per test translation unit, no clash). Verified by sweep: all other psi-linking tests already have 'base' in their LIBS (which carries global_variable.cpp); all tool_quit.cpp / ORB_*_lm.cpp test compilations are paired with global_variable.cpp; the only outliers were these two XC tests.
@Critsium-xy Critsium-xy changed the title [Refactor] clean source_psi's dependency [Refactor] Organize cleans of source_base and clean source_psi's dependency May 28, 2026
@Critsium-xy Critsium-xy marked this pull request as ready for review May 28, 2026 08:07
@Critsium-xy
Copy link
Copy Markdown
Collaborator Author

About why npol logic is removed from psi:

This logic already exists in source_io/module_parameter/read_set_globalv.cpp

image

So the logic in psi.cpp is useless exactly

@mohanchen mohanchen added Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0 labels May 29, 2026
@Critsium-xy Critsium-xy marked this pull request as draft May 29, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants