Skip to content

Fix LR-TDDFT optical velocity for hybrid functionals - #8368

Open
maki49 wants to merge 22 commits into
deepmodeling:developfrom
maki49:lr-optical-commutator
Open

maki49 wants to merge 22 commits into
deepmodeling:developfrom
maki49:lr-optical-commutator

Conversation

@maki49

@maki49 maki49 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Changes

  • Reuse the existing RT-TDDFT AO velocity and position-integral code to include the Fock commutator in LR-TDDFT absorption. Supported LCAO LR with ground-state exact exchange defaults to abs_gauge velocity_comm. It uses in-memory H(R)/S(R) in ks-lr and text CSR from out_hsr 1 in standalone lr. The INPUT docs recommend direct velocity for local and semilocal functionals.
  • Write LR excitation energies with 17 significant digits. Add the standard metadata header to the spectrum. The existing text filename absorption.dat remains for reader compatibility.
  • Move excitation-state reads and writes out of assert and report I/O failures with the filename. The scope is limited to LR-TDDFT and BSE file I/O, including the shared band-first wavefunction helpers. Issue #8447

Si SRCH

With identical LR-TDA settings and 0.10 eV Lorentzian HWHM, the 3–6 eV integral of Im εzz rises from 38.81 to 55.31. The digitized RT-TDDFT reference is 59.61. The LR peaks remain about 0.18 and 0.37 eV above the reference. Ji et al., Fig. 4(a)

Si SRCH absorption before and after the optical velocity fix, compared with Ji et al.

Verification

  • Rebuilt MODULE_IO_input_test_para and MODULE_LR_util_print. Both passed under ctest -R '^(MODULE_IO_input_test_para_4|MODULE_LR_util_print)$'. The INPUT test covers explicit velocity_comm and the automatic choice for ground-state exact exchange.

  • bash ../integrate/Autotest.sh -a /tmp/abacus-lr-optical-build/abacus_std_para -n 4 -o 1 -f cases.txt passed for lr_tddft_velocity_comm, using the existing executable and the updated Autotest collector. The shared file comparator checked all 100 spectrum points and rejected injected NaN and truncated output.

  • Earlier standalone checks matched the spectra from in-memory and text CSR matrix inputs for one and two spin channels.

  • bash ../integrate/Autotest.sh -a /tmp/abacus-lr-optical-build/abacus_std_para_kxc -n 4 -o 1 -j 2 -r '^(lr_tddft_.*|bse_lr_multik|rt_tddft_hse_current_spin2_up)$' passed all nine selected cases with KXC-enabled LibXC.

  • Two-rank HSE H₂ output checks (mpirun -np 2 abacus_std_para) matched the 17-digit saved energies to the solver and report. The BeH₂ two-step relaxation wrote # Ionic_Step 2.

  • Review follow-up c67c6259b passed a fresh GNU Release MPI build and both optical cases with bash ../integrate/Autotest.sh -a "$ABACUS_BIN" -n 4 -o 1 -r '^(lr_tddft_velocity_comm(_csr_spin2)?)$'. The new case reads separate spin CSR files, and substituting the first spin block for the second failed the absorption check.

  • ctest --test-dir "$BUILD" -R '^MODULE_LR_util_print$' --output-on-failure passed. The test_compare_file.py and test_props_ml.py suites in tests/integrate/validation_tools passed all 15 checks, including the weak-intensity tolerance regression.

Governance notes

The Si spectrum above provides the reproduction. ESolver supplies the AO matrices and HSolver changes energy formatting only. The remaining PARAM read belongs to the existing direct velocity setup. The added includes declare interface types and the standard double-precision digit count. No exception requested.

Reuse the real-time AO velocity, position integrals and LR transition projection. Read H(R) and S(R) from text CSR for standalone LR and use the in-memory KS blocks for ks-lr.

Cover the opt-in gauge with a multik Si absorption regression and check the fixed-orbital SRCH result against an independent Fourier reconstruction.

(cherry picked from commit caed480)
Default to eight significant digits in LR energy output and accept values
up to 17. Keep 0 as the legacy-format opt-in. Apply the setting to solver
output, energy reports, optical energy columns, and saved excitation
energies. Refresh the HF Gamma energy references affected by the extra
printed digits.

Write optional LR state files outside assertions so Release builds honor
out_wfc_lr.

(cherry picked from commit bbc5d22)
Copilot AI balanced review requested due to automatic review settings November 23, 2026 04:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@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.

The new absorption regression can accept incomplete or NaN output; require a complete finite spectrum before treating the check as passed. Static source review at d040f1eb; no builds or runtime tests were run.

@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.

The previous three findings are addressed in the source at 024b3fc5a. Two non-blocking regression-test suggestions remain below. This was a static incremental review; I did not rerun the reported tests.

# Conflicts:
#	source/source_hamilt/module_hcontainer/read_hcontainer.cpp
#	source/source_hamilt/module_xc/exx_info.cpp
@maki49 maki49 closed this Nov 23, 2026
@maki49 maki49 reopened this Nov 23, 2026
@maki49 maki49 closed this Nov 23, 2026
@maki49 maki49 reopened this Nov 23, 2026
@mohanchen mohanchen added EXX/lr-TDDFT/RPA/BSE/GW Related to beyond DFT methods Refactor Refactor ABACUS codes labels Nov 24, 2026
Comment thread tests/08_RI/CMakeLists.txt Resolved

@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.

The fixed excitation-energy precision would benefit from a direct round-trip regression; the existing collector cannot distinguish the intended precision.

Static incremental source review of 21d9485, including LR/BSE callers, provenance and validation workflows; no build or runtime test was run.

@maki49

maki49 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

The CUDA failure reproduces on upstream develop. The last passing parent, b9047f391, is followed by the first failing upstream run, 7ac8fd7e7. #8357 updates the CUDA image from 12.2/Ubuntu 22.04 to 12.9/Ubuntu 24.04. The case inputs and comparison script are identical across those commits. The concurrent move from V100 to L20 prevents these two runs from isolating the image's effect.

The failing develop run and this PR's CUDA run report the same six Max_wfc values. #8372 addresses the phase-sensitive comparison upstream.

@Stardust0831 Stardust0831 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.

Thank you for including the nonlocal exchange contribution to the optical velocity. I tested commit 6ac7bee2 on triplet O₂ at a bond length of 1.21 Å and compared it with an independent PySCF HSE06/TDA calculation. I matched the bright transition by its polarization along the O–O bond and its dominant minority-spin πᵤ → πɡ character. Computing resources were provided by the SAI cluster.

Method Excitation energy (eV) Oscillator strength per molecule, f
ABACUS, direct velocity 10.4572 0.126166
ABACUS, velocity with the commutator 10.4572 0.234052
ABACUS, length gauge 10.4572 0.234052
PySCF, length gauge, aug-cc-pVTZ 9.0569 0.204831

For the same ABACUS ground state and excitation space, the commutator result agrees with the length-gauge oscillator strength to about 3 ppm. It also brings the strength closer to the PySCF reference. The smaller aug-cc-pVDZ basis gave f = 0.218155 and favors the same correction. The excitation energies differ by about 1.4 eV between codes, so this comparison specifically supports the oscillator strength of the matched transition.

I also checked the open-shell workflow at the default CSR output precision. Standalone lr reproduced the in-memory ks-lr spectrum across all 100 sampled points, with a maximum absolute intensity difference of 1.0 × 10⁻¹⁰ a.u. Based on these checks, I support merging this PR.

Comment thread tests/08_RI/CMakeLists.txt Resolved
@mohanchen

Copy link
Copy Markdown
Collaborator

Conflict needs to be resolved. @maki49

// time here is what used to make a newly supported hybrid fail at input parsing.
const std::set<std::string> kernel_less = { "rpa", "bse" };
const std::set<std::string> abs_gauge = { "velocity", "length", "none" };
const std::set<std::string> abs_gauge = { "velocity", "velocity_commutator", "length", "none" };

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.

this name 'velocity_commutator' seems a bit long

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Renamed the INPUT value to velocity_comm in 2aeb13fb9. The parser, automatic selection, diagnostics, and docs use the same name. The four-rank INPUT tests passed for explicit and automatic selection.

if (this->my_rank == 0)
{
ofs << "Frequency (eV) | wave length(nm) | Absorption (a.u.)" << std::endl;
const std::string filename = this->out_dir + "absorption.dat";

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.

do you mean .txt file or .dat file, do you notice the difference between the two formats?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The spectrum is a plain-text table. Rule 2 uses .txt for text and .dat for binary output. This table was already written as absorption.dat on develop, and existing plotting scripts read that path. This PR keeps the legacy name for compatibility and documents the text format. Output file specification

from check_lr_absorption import compare_spectra, read_spectrum


def run_case(directory, fixture, launch, parameters):

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.

validation tools are used to compare files, not used for adding 'workflow', adding workflow will make this more complicated, that will be very difficult to handle in future. could you reduce the complexity of the workflow?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed the multi-run Python driver and its CTest entry in 2aeb13fb9. The HSE case now runs once through regular Autotest, and validation compares its existing spectrum file with the reference table. The four-rank case passed.

result = self.collect('', reference=SPECTRUM, spectrum=spectrum)
self.assertEqual(result, 'CompareLRAbsorption_pass 1\n')

def test_absorption_rejects_wrong_columns_and_changed_values(self):

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.

if you can try not adding new lines, or minimum lines in validation_tools, that will be great help for all the abacus project.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed the dedicated comparator and the added collector tests in 2aeb13fb9. The PR now adds only four lines to validation_tools, calling the shared record_compare_result and CompareFile.py when the spectrum reference is present.

@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.

At 2aeb13fb9, no new correctness defect was established in the optical path or merge resolutions. The remaining suggestions concern preserving the simplified tests’ sensitivity and CSR/two-spin coverage, and completing the vector-buffer cleanup.

Static review only; no builds or runtime tests were run.

Skip iterative guesses for LAPACK. Preserve the existing paired spin seeds and use the remaining single-spin transitions and opposite-sign partners as needed, with bounds checked within each spin block.

Verified the initial-guess unit tests, three targeted four-rank Autotest fixtures, and a four-root CG/LAPACK comparison under Valgrind.

@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.

At e711752a8, the earlier weak-intensity tolerance and standalone CSR/two-spin coverage requests are addressed in source. No new correctness defect was established; the remaining feedback concerns initial-guess assembly coverage and one INPUT diagnostic.

Static review only; no builds or runtime tests were run.

Test the production initial-guess assembly with two k points, unequal spin blocks and all twelve vectors, including opposite-sign partners. Check each rank against independently specified global positions and compare the gathered vectors.

Pass a named message to WARNING_QUIT for missing optical CSR input. The existing diagnostic and INPUT behavior are preserved.

The one- and four-rank initial-guess CTests and the three focused four-rank Autotest fixtures passed. Removing hrs2_nao.csr made --check-input return 1 with the expected diagnostic.
Read LR excitation energies and per-rank amplitudes in Release builds. Reject missing, truncated or malformed input before using it. Make the shared text I/O report stream failures and use the checked I/O in the band-first wavefunction helpers.

The LR I/O unit tests and a four-rank spectrum round trip pass with NDEBUG. Missing energy, a missing non-root amplitude shard and truncated amplitudes fail with their file paths. Refs deepmodeling#8447.
Use checked text I/O for TDA and full BSE energies and the per-rank X and Y amplitudes. Reuse the same reads for spectrum and plot, preserving the file format, rank ownership and energy broadcast.

The four-rank Release BSE test wrote and reloaded 28 state files. Checked the TDA and full BSE transition results, missing energies and Y shards, truncated X shards and a failed output open. The existing BSE and LR Autotest cases passed. Refs deepmodeling#8447.

@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.

One INPUT regression remains: a standalone RI benchmark can require optical CSR files even though it skips spectrum output. Please gate the commutator default and file preflight on that output path.

The new assembly test and named INPUT diagnostic address the previous two requests in source.

When resolving the conflicts with develop at 7d92c6e, port the optical setup and output changes into source/source_lcao/module_lr/lr_core.cpp, the guess changes into lr_gs.cpp, and their orbital/matrix/ionic-step dependencies into the new owner interfaces. Core::solve still contains assertion-side-effect I/O; Core::output still uses the old argument counts and only initializes the direct velocity buffer. The exact integrated revision needs review after these ports. This is a source integration requirement; no merged tree was built.

Static review only; no builds or runtime tests were run.

ModuleBase::WARNING_QUIT("ReadInput",
"abs_gauge velocity_comm requires MPI LCAO LR-TDDFT with gamma_only 0.");
}
if (para.inp.esolver_type == "lr")

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.

[P2] Skip optical CSR preflight for RI benchmark runs

With MPI+LibRI, esolver_type lr, basis_type lcao, gamma_only 0, dft_functional hse, xc_kernel rpa, and ri_hartree_benchmark abacus, omitting abs_gauge now selects velocity_comm. This check then rejects an otherwise supported benchmark input without hrs1_nao.csr/sr_nao.csr. The benchmark operator reads Cs/Vs, and ESolver_LR::after_all_runners returns before optical work whenever the benchmark is non-none, so it does not consume these matrices. Gate automatic selection and the CSR preflight on the spectrum path being reachable, and cover this benchmark input with absent optical CSR files.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

RI Hartree benchmarks now skip automatic velocity_comm selection and optical CSR preflight. The new HSE/RPA regression covers both the omitted and explicit gauge with absent CSR files. The parser CTests passed with one and four MPI ranks. The CLI still requires optical CSR files for the corresponding ordinary hybrid LR input.

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.

ae08db444 adds the benchmark guard to both automatic gauge selection and CSR preflight. The new INPUT regression covers omitted and explicit velocity_comm with absent CSR files, addressing the reported benchmark path in source.

{
const std::string stem = testing::TempDir() + "lr_band_first_" + std::to_string(getpid());
const std::array<double, 4> coefficients = {0.25, 0.5, 0.0, -0.125};
LR_Util::write_psi_bandfirst(coefficients.data(), 2, 1, 2, stem, 0);

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.

[P3] Non-blocking: Preserve a regression through the Release solver I/O calls

These tests invoke the helpers directly, so they would still pass if LR/BSE solver reads or writes moved back inside assert. The author-reported Release round trips exercise the current fix; add a persistent NDEBUG case that writes and reloads excitation files through the solver, including full-BSE Y amplitudes. Use compact fixed-fixture cases in the existing Autotest/module test infrastructure, keeping validation_tools focused on file comparison.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added 08_RI_excitation_io in afc948464. It runs the solver with two MPI ranks, writes and reloads actual excitation files in the same layout, and compares the resulting energies and oscillator strengths for LR, TDA BSE, and full BSE. It checks the full-BSE Y shards and exercises missing input files and blocked output paths with file-specific diagnostics. The regression passed with -DNDEBUG. The test driver is in tests/08_RI and uses the existing BSE fixture.

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.

afc948464 adds persistent solver write/reload coverage for LR and TDA/full BSE, including full-Y shards and missing/blocked-file diagnostics. This addresses the caller-coverage request in source for supported fixture configurations; the new registration's LibXC requirement is tracked separately in this review.

@mohanchen

Copy link
Copy Markdown
Collaborator

In order to minimize the impact of LR codes on other codes, I have refactored the esolver part. Please follow the guidence of #8439

@maki49

maki49 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Following up on your #8439 guidance, I merged current develop in ae08db444. ESolver_LR now supplies the borrowed KS matrices and the output step, while LR::Core handles the optical calculation, standalone CSR input, and excitation I/O. The initial-guess fix uses the refactored amplitude setup, and BSE uses the refactored state storage.

The two four-rank lr_tddft_velocity_comm Autotest cases and all six focused Release CTests passed.

@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.

08_RI_excitation_io needs a LibXC guard (or a kernel supported without it): its mandatory PBE run is registered in builds that cannot construct that response kernel.

The inspected merge ports the optical and excitation-I/O behavior into the current LR owners. The remaining inline items concern test setup and explicit header dependencies.

Static review only; no builds or runtime tests were run.

Comment thread tests/08_RI/CMakeLists.txt Outdated
list(APPEND MPI_LAUNCHER_FLAGS "--mpi-postflag=${flag}")
endforeach()
# The paired runs share rank-local excitation files produced by the solver.
if(ENABLE_MPI AND ENABLE_LCAO)

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.

[P2] Require LibXC for the PBE I/O fixture

This test is registered with ENABLE_LIBXC=OFF, but its unconditional first run selects xc_kernel pbe. KernelXC exits in that configuration before the excitation I/O checks. Please gate the PBE fixture on LibXC, or use a kernel supported without it for both LR runs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Both phases of the LR I/O fixture now use xc_kernel rpa on the same PBE ground state. The two-rank Release 08_RI_excitation_io CTest passed with both ENABLE_LIBXC=ON and ENABLE_LIBXC=OFF, including the LR and BSE paths. Fixed in 813a13268.

Comment thread tests/08_RI/CMakeLists.txt Outdated
add_test(
NAME 08_RI_excitation_io
COMMAND
python3 ${ABACUS_TEST_DIR}/08_RI/run_excitation_io.py --abacus

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.

[P3] Non-blocking: Discover Python before registering the driver

This new test invokes literal python3 without checking whether it is available. Use optional find_program discovery and an if() guard, then invoke the discovered path, as required by the integration-driver guidance.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

CTest now uses find_program to locate python3, registers the test only when the interpreter is available, and invokes the discovered path. I verified that the test is omitted when the discovery result is disabled. Fixed in 813a13268.

Comment thread tests/08_RI/run_excitation_io.py Outdated
def test_bse(command, root):
source, case = stage_case(root, "bse_lr_multik")
shutil.copytree(source / "librpa.d", case / "librpa.d")
template = (source / "INPUT").read_text()

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.

[P3] Non-blocking: Give the BSE I/O variant fixed input templates

This specialized round trip derives its settings from the ordinary bse_lr_multik input and rewrites them in scratch. Please give the writer and reader dedicated INPUT templates so this regression has an explicit fixture independent of that case's defaults, while reusing shared reference data where appropriate.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The BSE writer and spectrum reader now use dedicated INPUT.write and INPUT.read templates in tests/08_RI/bse_excitation_io. They reuse the existing geometry, k points, and LibRPA data. The two-rank Release round trip and full-BSE Y-file failure checks passed. Fixed in 813a13268.

Comment thread source/source_lcao/module_lr/lr_core.h Outdated
#include <vector>

class Grid_Driver;
class LCAO_Orbitals;

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.

[P3] Non-blocking: Include the owners of the new interface types

Please include source_basis/module_ao/orb_read.h for the new Refs::orbitals dependency instead of adding this forward declaration. The PR-added declarations in utils/spectrum_mo.h can likewise use that header and source_hamilt/module_hcontainer/hcontainer.h for their orbital and container interfaces.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

lr_core.h now includes orb_read.h for Refs::orbitals. The declarations in spectrum_mo.h include both orb_read.h and hcontainer.h. Standalone header checks and the Release builds passed. Fixed in 813a13268.

@maki49 maki49 closed this Dec 1, 2026
@maki49 maki49 reopened this Dec 1, 2026
@maki49 maki49 closed this Dec 1, 2026
@maki49 maki49 reopened this Dec 1, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants