Skip to content

refactor(lr): slim ESolver_LR by moving LR state and logic into module_lrrefactor(lr): slim ESolver_LR by moving LR state and logic into module_lr (fix #8438) - #8439

Open
mohanchen wants to merge 27 commits into
deepmodeling:developfrom
mohanchen:2026-11-28-a
Open

mohanchen wants to merge 27 commits into
deepmodeling:developfrom
mohanchen:2026-11-28-a

Conversation

@mohanchen

Copy link
Copy Markdown
Collaborator

fix #8438

Summary

Makes ESolver_LR a thin adapter and moves the linear-response TDDFT state and
algorithms into source_lcao/module_lr. No behaviour change: same inputs, same
outputs, same numerical results.

The LR esolver used to be seven files (~2550 lines) under source/source_esolver/.
It is now a single esolver_lr.{h,cpp} (~760 lines); the algorithms live next to the
rest of the LR module.

Design

  • LR::Core<T> (new lr_core.{h,cpp}) owns the LR solver state:
    Dim, Amp, GsState, ZWin, PotSet, RelaxRun, and the borrowed Refs.
  • LR::Refs<T> is a set of plain pointers/handles to objects the esolver already
    owns (ucell, kv, gd, orb_cutoff, rho/rhod, vloc, sfac, tcb, inp,
    log streams, pgrid, in/out dirs, xc_kernel, rank, pv_all, and under __EXX
    a pointer to the shared_ptr<Exx_LRI<T>> plus hybrid_alpha). The esolver binds
    them once; module_lr never includes an esolver header.
  • LR::KsBands<T> (POD) carries the per-ionic-step ground-state bands
    (psi/eig/wg + layout) into Core::refresh_gs.
  • LR::DimSrc carries the few esolver/global quantities dimension resolution needs,
    so lr_dim no longer reads them implicitly.

What moved into module_lr

  • Dimension resolution and spin-polarisation handling (lr_dim).
  • Ground-state band/charge reading, Z-window widening (lr_gs).
  • Kernel potentials and the ground-state potential used by the force path
    (potentials/lr_gs_pot, init_helpers).
  • Amplitude setup/allocation and multiplet padding (lr_amp, lr_mpl).
  • The TDA eigenproblem (closed- and open-shell) and spectrum input (Core::solve).
  • Electron-hole density, absorption spectrum, transition dipoles and per-state
    gradients (Core::output).
  • The gradient/Z-vector force path and the ground-state force check (lr_sforce,
    lr_zeq, lr_dm, gradient_*).
  • Excited-state relaxation, target selection and cross-geometry tendency tracking
    (lr_relax, lr_mpl).
  • Removed the now-dead esolver wrappers/accessors left over from the old split.

What the esolver keeps

Only orchestration and adaptation, because it is inherently esolver-typed:

  • ESolver_FP::before_all_runners (PW grids, structure factor, kv.set,
    atom_arrange, symmetry, pseudopotential/nonlocal data).
  • EXX object ownership, and the BSE subclass hookup.
  • ESolver interface adapters: cal_energy, cal_force, cal_stress.
  • init_pot / read_ks_wfc / read_ks_chg, kept as one-line delegations because
    BSE overrides or calls them.

Notes

  • Refs binding is hoisted ahead of refresh_from_ks_ / init_pot, since the
    potentials now read Refs.
  • The two never-initialised shadow Parallel_Orbitals members (paraMat_,
    paraMat_all_) were removed; the standalone path now uses the real
    gs.para_mat / gs.para_mat_all layouts (previously wrong under MPI).
  • JSON support is unchanged: the ks-lr initialisation before the embedded
    ground-state SCF is kept and still pairs with Relax_Driver's step-0 skip.
  • Unused includes were dropped from esolver_lr.cpp.

Verification

  • Builds: cmake --build build -j 56 (CPU; including the esolver target with BSE
    and all LR unit-test targets).
  • Integration regressions (unchanged values):
    • tests/08_RI/lr_tddft_lda_gamma -> CompareLRForce_pass 0,
      totalforceref 115.0726395253, excitationenergyref1 0.587347
    • tests/08_RI/lr_relax_lda_ch4 -> CompareStateTracking_pass 0
  • tools/03_code_analysis/agent_governance_check.py: 0 errors, global-dependency
    net delta <= 0.

Diff shape

  • source/source_esolver/esolver_lr.{h,cpp}: the whole LR esolver in one place.
  • source/source_lcao/module_lr/: lr_core, lr_dim, lr_gs, lr_amp, lr_mpl,
    lr_relax, lr_dm, lr_zeq, lr_sforce, potentials/lr_gs_pot, init_helpers.
  • Deleted: esolver_lr_init.cpp, esolver_lr_cfg.cpp, esolver_lr_grad.cpp,
    esolver_lr_lcao_tddft.{h,cpp}, esolver_lr_orb.cpp, esolver_lr_rlx.cpp.## Summary

Makes ESolver_LR a thin adapter and moves the linear-response TDDFT state and
algorithms into source_lcao/module_lr. No behaviour change: same inputs, same
outputs, same numerical results.

The LR esolver used to be seven files (~2550 lines) under source/source_esolver/.
It is now a single esolver_lr.{h,cpp} (~760 lines); the algorithms live next to the
rest of the LR module.

Design

  • LR::Core<T> (new lr_core.{h,cpp}) owns the LR solver state:
    Dim, Amp, GsState, ZWin, PotSet, RelaxRun, and the borrowed Refs.
  • LR::Refs<T> is a set of plain pointers/handles to objects the esolver already
    owns (ucell, kv, gd, orb_cutoff, rho/rhod, vloc, sfac, tcb, inp,
    log streams, pgrid, in/out dirs, xc_kernel, rank, pv_all, and under __EXX
    a pointer to the shared_ptr<Exx_LRI<T>> plus hybrid_alpha). The esolver binds
    them once; module_lr never includes an esolver header.
  • LR::KsBands<T> (POD) carries the per-ionic-step ground-state bands
    (psi/eig/wg + layout) into Core::refresh_gs.
  • LR::DimSrc carries the few esolver/global quantities dimension resolution needs,
    so lr_dim no longer reads them implicitly.

What moved into module_lr

  • Dimension resolution and spin-polarisation handling (lr_dim).
  • Ground-state band/charge reading, Z-window widening (lr_gs).
  • Kernel potentials and the ground-state potential used by the force path
    (potentials/lr_gs_pot, init_helpers).
  • Amplitude setup/allocation and multiplet padding (lr_amp, lr_mpl).
  • The TDA eigenproblem (closed- and open-shell) and spectrum input (Core::solve).
  • Electron-hole density, absorption spectrum, transition dipoles and per-state
    gradients (Core::output).
  • The gradient/Z-vector force path and the ground-state force check (lr_sforce,
    lr_zeq, lr_dm, gradient_*).
  • Excited-state relaxation, target selection and cross-geometry tendency tracking
    (lr_relax, lr_mpl).
  • Removed the now-dead esolver wrappers/accessors left over from the old split.

What the esolver keeps

Only orchestration and adaptation, because it is inherently esolver-typed:

  • ESolver_FP::before_all_runners (PW grids, structure factor, kv.set,
    atom_arrange, symmetry, pseudopotential/nonlocal data).
  • EXX object ownership, and the BSE subclass hookup.
  • ESolver interface adapters: cal_energy, cal_force, cal_stress.
  • init_pot / read_ks_wfc / read_ks_chg, kept as one-line delegations because
    BSE overrides or calls them.

Notes

  • Refs binding is hoisted ahead of refresh_from_ks_ / init_pot, since the
    potentials now read Refs.
  • The two never-initialised shadow Parallel_Orbitals members (paraMat_,
    paraMat_all_) were removed; the standalone path now uses the real
    gs.para_mat / gs.para_mat_all layouts (previously wrong under MPI).
  • JSON support is unchanged: the ks-lr initialisation before the embedded
    ground-state SCF is kept and still pairs with Relax_Driver's step-0 skip.
  • Unused includes were dropped from esolver_lr.cpp.

Verification

  • Builds: cmake --build build -j 56 (CPU; including the esolver target with BSE
    and all LR unit-test targets).
  • Integration regressions (unchanged values):
    • tests/08_RI/lr_tddft_lda_gamma -> CompareLRForce_pass 0,
      totalforceref 115.0726395253, excitationenergyref1 0.587347
    • tests/08_RI/lr_relax_lda_ch4 -> CompareStateTracking_pass 0
  • tools/03_code_analysis/agent_governance_check.py: 0 errors, global-dependency
    net delta <= 0.

Diff shape

  • source/source_esolver/esolver_lr.{h,cpp}: the whole LR esolver in one place.
  • source/source_lcao/module_lr/: lr_core, lr_dim, lr_gs, lr_amp, lr_mpl,
    lr_relax, lr_dm, lr_zeq, lr_sforce, potentials/lr_gs_pot, init_helpers.
  • Deleted: esolver_lr_init.cpp, esolver_lr_cfg.cpp, esolver_lr_grad.cpp,
    esolver_lr_lcao_tddft.{h,cpp}, esolver_lr_orb.cpp, esolver_lr_rlx.cpp.

abacus_fixer added 25 commits November 28, 2026 14:27
Extract set_dimension/reset_dim_spin2 from ESolver_LR into LR::resolve_dim
and LR::apply_spin_pol so the solver only assembles inputs and copies the
resolved dimensions back. Rename the members to set_dim/reset_dim_spin and
register lr_dim.cpp in both build systems.
Add LR::set_band_layout, make_amp_layout, set_x_init, read_wfc_*,
build_z_win and read_gs_chg so esolver_lr_orb.cpp only forwards member
state. Wavefunction/charge reading, amplitude distributions and the
Z-vector window no longer depend on the solver class.
…ring into module_lr

Add LR::GsPot, LR::make_gs_dm and LR::make_lr_exx so the force path and
the ks-lr initialization only forward member state. lr_dm is a separate
translation unit so the cal_edm unit test keeps its slim link set.
Add LR::RelaxTarget so the relaxation state (followed root, multiplet
group, previous amplitudes) lives in module_lr and the solver only
forwards energies and amplitudes. Remove the duplicated target members
from ESolver_LR.
Merge esolver_lr_cfg/orb/grad/rlx.cpp into esolver_lr_lcao_tddft.cpp so a
single translation unit holds the remaining solver-side code. Drop the
freed source files from both build systems.
Merge esolver_lr_init.cpp into the main implementation and rename the pair
so the solver lives in one header and one translation unit. Update the BSE
subclass and the factory includes, and keep the EXX/JSON conditional
includes intact. ESolver_LR and its BSE-visible members are unchanged.
The governance checker counts comment text as a global dependency, which
made the refactor look like a net +1. Describe the field instead.
Add LR::build_mpl and LR::eval_jt_force, which own the polarization
identity, the basis diagnostics and the Jahn-Teller search. The solver
keeps only the Z-vector callback and the target-state bookkeeping, and
the dead MultipletLVC::average_force goes away.
The member had no definition and no caller; the GintInfo hand-off both
paths use is ModuleGint::Gint::set_gint_info.
cal_edm, Parallel_Reduce, <numeric> and state_track are now pulled in
only where they are actually used.
Drop the share_exx_lri specialisations (call LR::make_lr_exx directly),
the unused GsPot::pot accessor and MplLvc::dim.
…o module_lr

Add LR::setup_x, LR::read_gs_wfc and LR::pad_group; drop the
set_parallel_orbitals_band wrapper in favour of LR::set_band_layout.
Add LR::GsForceCtx, LR::solve_zeq, LR::force_xz and LR::force_xz_os;
the solver now only fills the context and keeps the timers.
Introduce LR::Amp, LR::PotSet, LR::RelaxRun and LR::Core in module_lr so
the amplitudes, Z window, potentials and relaxation state stop being
members of ESolver_LR. All accesses go through lr_.<group>.<field>; the
functions still live in the esolver for now.
LR::Core now owns LR::Dim and LR::GsState; Core::read_wfc and Core::fill_z
absorb the former read_ks_wfc/fill_z_window_ bodies. Drop the unused
pad_X_to_z_ wrapper.
Add LR::Refs, the borrowed geometry handles the force path needs, and bind
them once from the esolver. The per-state/Z-window context builders, the
force/gradient entry points, the ground-state density matrix, and the
excited-state relaxation (target setup, root following, average/JT force)
now live in module_lr. The esolver keeps only the thin virtual overrides.
The LR runtime logs are the same streams as GlobalV::ofs_running and
GlobalV::ofs_warning, but using the members keeps the global dependency
budget from growing.
@mohanchen mohanchen added EXX/lr-TDDFT/RPA/BSE/GW Related to beyond DFT methods Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0 labels Nov 28, 2026
Resolve conflicts from forcing the LR esolver refactor onto the newer
upstream tree, and port the semantic changes of deepmodeling#8329 (LR force output,
spin selection, virtual-window response, XC kernels) into the new
module_lr layout:

- window response: force_xz / force_xz_os build the WindowResponse from
  the X-window virtual count and pass its density to the Z-vector solve
  and its terms to the force evaluation; solve_zeq takes window_density
- fill_z: real-Gamma virtual-window diagnosis matches deepmodeling#8329
- spin selection: nspin==1 labels the single channel with
  lr_target_spin, and lr_target_spin=triplet no longer aborts on nspin=1
- output: electron-hole densities are labelled singlet/triplet or up/down
- runner: cache the ground-state force for single-point excited states
- cal_force: print the cached ground-state force
- BSE: keep the refactored lr_.dim / lr_.amp members with upstream's
  explicit types
The Z window widens the virtual space for the CPSCF (Z-vector) solve, but
the virtual-window response must be built for the virtuals the caller
actually solved for, i.e. the X window. Passing the widened count made
the response too large and shifted the triplet forces by ~0.1%:

    lr_tddft_lda_triplet_window_gamma
      before: 88.7758685201   after: 88.7747473201 (reference)

force_xz / force_xz_os now take the active virtual count explicitly;
Core passes dim.nvirt and state_forces passes StateCtx::nvirt.

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

EXX/lr-TDDFT/RPA/BSE/GW Related to beyond DFT methods 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.

Refactor: move LR-TDDFT solver logic out of ESolver_LR into module_lrRefactor: move LR-TDDFT solver logic out of ESolver_LR into module_lr

1 participant