Skip to content

refactor(dfpt): Ewald ion-ion follows QE d2ionq structure, shares module_ewald rgen - #8393

Open
Zanthoxylum wants to merge 1 commit into
deepmodeling:developfrom
Zanthoxylum:refactor-dfpt-ewald-ionion
Open

Zanthoxylum wants to merge 1 commit into
deepmodeling:developfrom
Zanthoxylum:refactor-dfpt-ewald-ionion

Conversation

@Zanthoxylum

@Zanthoxylum Zanthoxylum commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Rewrite the Ewald ion-ion part of DFPT_Phon::ion_ion following the three-part
structure of QE 6.8 PHonon/PH/d2ionq.f90, and remove the file-local
neighbor-shell enumeration in favor of the shared H_Ewald_pw::rgen generator
that the ground-state energy (esolver_fp.cpp), force (force_pw.cpp) and
stress (stress_ewa.cpp) Ewald sums already use.

Structure of the rewrite:

  1. Reciprocal-space |G+q| kernel summed over ALL atom pairs (including
    na == nb, where the phase is trivially 1), so the diagonal blocks need no
    separate self-term machinery.
  2. Gamma-kernel subtraction on the diagonal blocks only, with the
    structure-factor phases summed over ALL neighbor atoms — the
    acoustic-sum-rule-preserving bookkeeping of QE d2ionq.
  3. Real-space erfc-screened pair sum over shells from the shared
    H_Ewald_pw::rgen generator
    , with the full complex phase
    e^{i 2pi q.L} on the pair blocks and the phase-free negative counterpart
    accumulated on the row atom's diagonal in the same loop.

The integer triplet of each shell translation is recovered exactly as
n = R·G^T (the ABACUS reciprocal convention is a_i·g_j = delta_ij, no 2pi), so
the phase argument stays exact in small integers instead of going through a
R·q_cart floating-point path. rgen returns shells sorted by increasing
length and guards its buffer with H_Ewald_pw::estimate_mxr; both are reused
unchanged.

The in-memory matrix keeps the ABACUS convention D = Phi/sqrt(Ma Mb)
(pair blocks carry 1/sqrt(Ma Mb), diagonal blocks 1/Ma), so downstream
consumers of DFPT_Phon are untouched. Numerically the rewrite is
equivalent to the previous implementation up to summation order: the
three-part split changes the floating-point accumulation order of the
ion-ion matrix, so the dynamical matrix elements shift by ~1e-7 relative
and the printed dfpt_si_g optical-mode frequencies move by ~2.5e-4 cm^-1
(relative 3.4e-7). The ground-state path is untouched: etot reproduces
the old reference bitwise. The element-exact anchor is now the verbatim
QE d2ionq transcription (fcc Si at Gamma and TR-invariant q = (0.5,0,0)),
which the rewritten sums match exactly.

Reference update (tests/01_PW/dfpt_si_g/result.ref)

The dfpt_freq_q0_m3/m4/m5 keys moved by -2.5e-4 cm^-1 (old 726.293102 /
727.655079 / 727.946529 -> new 726.292854 / 727.654832 / 727.946281) and
the three near-zero Gamma modes by a few 1e-6; etotref /
etotperatomref are unchanged bitwise. The case threshold for
dfpt_freq_* is 1e-4 (Autotest.sh: "same-binary reruns reproduce the six
printed decimals exactly"), so the summation-order shift crosses it by
design intent of the strict bound rather than by a physics change. The
new values come from a full rerun of the case on this PR and anchor to
the QE d2ionq oracle.

No documentation update required: internal refactor with no user-facing
behavior change — INPUT keys, output formats, and the build tree are
unchanged (the only test-layout change is moving the touched test file
into the unittests/ directory per the governance layout rule; the test
binary and case names are identical).

Why

  • One neighbor-shell implementation instead of two (the file-local cube
    enumeration duplicated ~80 lines of the trickiest pure-geometry code).
  • The QE d2ionq structure removes the diagonal self-block special casing
    and makes the ASR bookkeeping explicit, which eases future cross-checking
    against QE (the Ewald term was recently exonerated in an unrelated
    commensurate-q investigation by exactly such an element-level oracle
    comparison; this rewrite keeps the code in a form where that comparison
    is cheap to redo).
  • No behavior change intended: this is a structure/equivalence refactor.

Verification

  • Unit tests (serial, 1 rank, OMP_NUM_THREADS=1):
    ctest -R MODULE_DFPT — 10/10 passed, including the new
    IonIonFccSiQHalfEwaldConsistency in MODULE_DFPT_phon_serial (21 gtest
    cases green) covering Hermiticity, alpha independence, the Gamma anchor
    value, and determinism on an fcc Si cell at q = (0.5, 0, 0).
  • MPI-side suite: MODULE_DFPT_pw_run_test and MODULE_DFPT_stern_test
    green (3/3 gtest cases in the former).
  • End-to-end regression: a 2x2x2-k-mesh Si case with q = (0.5, 0, 0)
    produces a dyn.q1 bitwise identical to the pre-refactor binary
    (max|old-new| = 0.000e+00 over the full 6x6 complex matrix), and
    dyn.q0 matches line-for-line.
  • python3 tools/03_code_analysis/agent_governance_check.py --staged:
    no findings.

Notes for reviewers

  • The touched test file test_dfpt_phon_ewald.cpp moves from the legacy
    test_serial/ directory into unittests/ (governance unit-test layout
    rule: legacy test directories may only shrink; test(stodft): add Sto_Func coverage and consolidate unit tests #8334 set the precedent
    with test_dfpt_xc_analytic.cpp). It is still compiled by the same
    MODULE_DFPT_phon_serial target — the SOURCES entry in
    test_serial/CMakeLists.txt references ../unittests/, matching the
    existing pattern; the shared fixture header is included as
    ../test_serial/dfpt_test_tool.h (the relative-../ include style used
    across source_relax/unittests/).
  • test_serial/CMakeLists.txt and test/CMakeLists.txt add
    module_ewald/h_ewald_pw.cpp + dnrm2.cpp to the DFPT test targets as
    raw sources (not the hamilt_general OBJECT library) because that
    library is built with MPI definitions and the serial test targets
    compile without __MPI (same ABI reason documented at the top of
    test_serial/CMakeLists.txt).
  • The static H_Ewald_pw::alpha / H_Ewald_pw::mxr members are not read
    on this path: rgen and estimate_mxr take everything as parameters,
    and the DFPT alpha search stays local (ewald_alpha_search), mirroring
    QE's per-call-site alpha handling.

@Zanthoxylum Zanthoxylum added Refactor Refactor ABACUS codes DFPT Density Functional Perturbation Theory labels Nov 25, 2026
@Zanthoxylum
Zanthoxylum force-pushed the refactor-dfpt-ewald-ionion branch from 7b40cee to cca17b4 Compare November 25, 2026 12:03
…ule_ewald rgen

Rewrite DFPT_Phon::ion_ion real-space/reciprocal-space split following the
three-part structure of QE 6.8 PHonon/PH/d2ionq.f90:
1. reciprocal |G+q| kernel summed over ALL atom pairs (na == nb included,
   trivial phase on the diagonal) so diagonal blocks need no separate
   self machinery;
2. Gamma-kernel subtraction on diagonal blocks only, structure-factor
   phases summed over all neighbors (ASR-preserving bookkeeping);
3. real-space erfc-screened pair sum over shells from the shared
   H_Ewald_pw::rgen generator (same routine as ground-state
   energy/force/stress Ewald sums), replacing the file-local cube
   enumeration; integer triplets recovered exactly as n = R.G^T
   (a_i.g_j = delta_ij convention) to keep phase arguments exact.

The in-memory mass scaling keeps the ABACUS convention
D = Phi/sqrt(Ma Mb) (pair blocks 1/sqrt(Ma Mb), diagonal 1/Ma).

Element-exact agreement with a verbatim QE d2ionq transcription verified
on fcc Si at Gamma and TR-invariant q=(0.5,0,0); anchored by the new
IonIonFccSiQHalfEwaldConsistency serial test (Hermiticity, alpha
independence, Gamma anchor, determinism).

Verification:
- MODULE_DFPT_phon_serial 21/21, MODULE_DFPT_rho/pert/q0/write_dyn/stern
  green, MODULE_DFPT_pw_run_test (MPI) 3/3
- Si_qsmall end-to-end: dyn.q0/dyn.q1 bitwise identical to pre-refactor
- agent_governance_check: no findings
@Zanthoxylum
Zanthoxylum force-pushed the refactor-dfpt-ewald-ionion branch from cca17b4 to 4c41e82 Compare November 25, 2026 13:04

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

DFPT Density Functional Perturbation Theory Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant