Skip to content

source_estate: give Occupy's stateless smearing helpers a real interface, removing two access hacks - #7983

Merged
mohanchen merged 1 commit into
deepmodeling:developfrom
Critsium-xy:refactor/occupy-smearing-free-functions
Sep 17, 2026
Merged

mohanchen merged 1 commit into
deepmodeling:developfrom
Critsium-xy:refactor/occupy-smearing-free-functions

Conversation

@Critsium-xy

Copy link
Copy Markdown
Collaborator

What this does

elecstate_occupy_test needed #define private public for two unrelated
reasons. Both are fixed at the source rather than papered over, so the two
macros go away and source_estate drops from 12 to 10.

1. Four stateless helpers become free functions

Occupy::wgauss(), w1gauss(), sumkg() and efermig() were private static
members. Every one of them takes all of its inputs as arguments, and none of
them reads any of Occupy's state
— not use_gaussian_broadening, not
gaussian_type, not gaussian_parameter, not fixed_occupations. Even the
smearing width and smearing type arrive as parameters (smearing_sigma,
ngauss). They were never members in any meaningful sense.

They now live in a new occupy_smearing namespace in the same header, with
doc comments describing the smearing-type encoding (-99 Fermi-Dirac, -1
cold, 0 Gaussian, n > 0 n-th order Methfessel-Paxton) that was previously
only discoverable from the function bodies.

The six internal callers in gweights() and efermig() are updated. The
WARNING_QUIT tag and one comment now name where the code actually lives
instead of a class it is no longer part of — nothing in source/ or tests/
asserts on either string, which I checked before changing them.

These four have no other callers anywhere in the tree, so the namespace move
is contained.

2. iweights() takes nspin instead of reading it

Occupy::iweights() read PARAM.inp.nspin twice — once for the spin degeneracy
(nspin == 4 halves it) and once to skip k points belonging to the other spin
(nspin == 2). That was the only reason the test had to write the private half
of PARAM.

It now takes nspin explicitly, following tweights() in the same class, whose
signature has always been tweights(const int nks, const int nspin, ...). So
this is the class's own existing convention, not a new shape.

Its three call sites are all in elecstate_tools.cpp, which already reads
PARAM.inp for the surrounding arguments (PARAM.inp.nelec on the same call).

Governance

PARAM/GlobalV/GlobalC ledger: added 4, removed 6, net_delta = −2.
Zero blockers.

The three flagged additions are the PARAM.inp.nspin arguments at the
iweights() call sites in elecstate_tools.cpp. This is the intended direction:
the global read moves out of the leaf routine and into the caller that was
already reading PARAM.inp on the same statement, exactly as in #7921. Two reads
are removed from occupy.cpp and one write from the test, hence the net
reduction. (The fourth addition is the same argument on the second of the two
two-Fermi calls.)

occupy.cpp's remaining PARAM references — globalv.nbands_l and
inp.bndpar — are in gweights()/sumkg() paths that no test drives, so
injecting them would only cost budget without removing a macro.

No default arguments were added. No assertion or expected value changed: the
tests call the same code with the same inputs through its new name. No INPUT
parameter or user-facing behaviour changed, so no documentation update is
required.

Verification

Remote Linux box, cmake -B build -G Ninja -DBUILD_TESTING=ON -DENABLE_LCAO=ON -DENABLE_MPI=ON -DENABLE_OPENMP=ON:

  • configure exit 0; 3354/3354 targets, build exit 0, zero errors;
    ninja: no work to do on a follow-up full build
  • MODULE_ESTATE_elecstate_occupy and MODULE_ESTATE_elecstate_base both link,
    plus abacus_basic_para, which is what exercises the changed iweights() call
    sites
  • all three changed sources confirmed compiled (occupy.cpp into 7 targets,
    elecstate_tools.cpp into 4)
  • agent_governance_check.py --base upstream/develop --head HEAD: zero blockers,
    no access-hack ratchet finding (2 removed, 0 added)

Continues #7921 / #7949 / #7952 / #7953 / #7963 / #7964 / #7965 / #7966 / #7967 /
#7980 / #7981 / #7982, and keeps their scope rule: a file is either taken fully
off the macro or left alone with a stated reason.

🤖 Generated with Claude Code

…ace, removing two access hacks

elecstate_occupy_test needed #define private public for two unrelated reasons,
and both are fixed at the source rather than papered over.

1) Four private static members of Occupy -- wgauss(), w1gauss(), sumkg() and
   efermig() -- are free functions in a new occupy_smearing namespace. Each takes
   every input as an argument and none of them reads any of Occupy's state
   (use_gaussian_broadening, gaussian_type, gaussian_parameter,
   fixed_occupations), so they were never members in any meaningful sense; even
   the smearing width and type arrive as parameters. The six internal callers in
   gweights() and efermig() are updated, and the WARNING_QUIT tag and one comment
   now name where the code actually lives.

2) Occupy::iweights() read PARAM.inp.nspin twice -- once for the spin degeneracy
   and once to skip k points of the other spin -- which was the only reason the
   test had to write the private half of PARAM. It now takes nspin explicitly,
   following tweights() in the same class, which has always done so. Its three
   call sites are all in elecstate_tools.cpp, which already reads PARAM.inp for
   the surrounding arguments.

The four extracted functions have no other callers anywhere in the tree, and
occupy.cpp's remaining PARAM references (globalv.nbands_l, inp.bndpar) are in
gweights()/sumkg() paths no test drives.

No default arguments were added. No assertion or expected value changed; the
tests call the same code with the same inputs through its new name.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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