Skip to content

fix(dftu): apply 1/2 Pauli-basis trace factor to LCAO nspin=4 DFT+U stress - #8496

Open
songfeitong wants to merge 1 commit into
deepmodeling:developfrom
songfeitong:fix/dftu-lcao-nspin4-stress
Open

songfeitong wants to merge 1 commit into
deepmodeling:developfrom
songfeitong:fix/dftu-lcao-nspin4-stress

Conversation

@songfeitong

Copy link
Copy Markdown

Reminder

  • I have read AGENTS.md and docs/developers_guide/agent_governance.md.
  • I have linked an issue or explained why this PR does not need one.
  • I have added adequate unit tests and/or case tests, or explained why not.
  • I have listed the exact verification commands run and their results.
  • I have described user-visible behavior changes, including INPUT parameter changes.
  • I have explained core-module impact for ESolver, HSolver, ElecState, Hamilt, Operator, Psi, or other source/ changes.
  • I have requested any needed governance exception below.

Linked Issue

No existing issue; this is a self-contained bug fix discovered while validating LCAO nspin=4 DFT+U stress against finite differences of total energy.

What's changed?

Fixes a factor-of-2 overestimation in the real-space LCAO DFT+U stress (dft_plus_u = 1) for non-collinear / SOC (nspin = 4) calculations.

Root Cause

In the real-space LCAO DFT+U implementation (source/source_lcao/module_dftu/), for nspin = 4 both the real-space density matrix dmR and the onsite occupation/potential arrays are stored in the 4-component Pauli basis $(\alpha = 0, 1, 2, 3)$:
$$\mathbf{n} = \frac{1}{2}\sum_{\alpha=0}^{3} n^{(\alpha)} \sigma_\alpha, \qquad E_U = \frac{U}{2}\mathrm{Tr}[\mathbf{n}(1-\mathbf{n})] = \frac{U}{2}\mathrm{Tr}{\mathrm{orb}}[n^{(0)}] - \frac{U}{4}\sum{\alpha=0}^{3}\mathrm{Tr}_{\mathrm{orb}}!\left[(n^{(\alpha)})^2\right],$$
where $E_U$ is the Dudarev Hubbard energy functional (note that cal_pot_onsite() accumulates eu as the double-counting energy correction subtracted from the band energy).

cal_pot_onsite() computes the unhalved Pauli-channel potentials (already transposed in orbital indices):
$$V^{(0)}{m_1 m_2} = U\bigl(\delta{m_1 m_2} - n^{(0)}{m_2 m_1}\bigr), \qquad V^{(k)}{m_1 m_2} = -U n^{(k)}{m_2 m_1} \quad (k = 1, 2, 3).$$
Accordingly, the variation of the Hubbard functional carries a $\tfrac{1}{2}$ spinor trace factor:
$$dE_U = \frac{1}{2} \sum
{\alpha=0}^{3} \sum_{m_1, m_2} V^{(\alpha)}{m_1 m_2}, dn^{(\alpha)}{m_1 m_2}.$$

  • Hamiltonian: transfer_pot_onsite<std::complex<double>>() (dftu_nao_pots.h) explicitly applies the factor 0.5.
  • Force: cal_for_IJR_nao_r() differentiates only one of the two projector overlaps (missing the product-rule factor of 2). For nspin = 4, the missing product-rule factor 2 and the missing Pauli trace factor 1/2 cancel (2 * 0.5 = 1), so reduce_force_impl() only multiplies by 2.0 when nspin != 4.
  • Stress: cal_str_IJR_nao_r() already sums both projector derivative terms explicitly (contracting pot_onsite[m1, m2] with the strain derivative of occ[m1, m2]), but reduce_stress_impl() did not accept nspin and did not apply the 0.5 factor for nspin == 4. Consequently, DFTU STRESS was overestimated by an exact factor of 2 for nspin = 4, while nspin = 1, 2 were unaffected.

Code Changes

  • Add int nspin parameter to DFTU_LCAO::reduce_stress_impl() (dftu_nao_fs_r.h, dftu_nao_fs_reduce.cpp) and apply spin_scale = (nspin == 4) ? 0.5 : 1.0 before scaling by lat0 / omega.
  • Pass nspin at the single production call site in cal_fs_nao_r_impl() (dftu_nao_fs_r.cpp) and clarify the docstrings for reduce_force_impl() and reduce_stress_impl().
  • Expand unit tests in source/source_lcao/module_dftu/unittests/test_dftu_nao_fs_reduce.cpp to cover nspin = 1, 2, 4 stress scaling, and update the call signature in test_dftu_nao_fs_r.cpp.
  • Update totalstressref in tests/03_NAO_multik/scf_u_spin4/result.ref (4697.840104 -> 4891.796432, exact shift +193.956328 matching half the previous DFTU STRESS L1 norm; etotref and totalforceref are unchanged).

Unit Tests and/or Case Tests for my changes

  • Commands run:
    • ctest --test-dir build-fixed --output-on-failure -R "MODULE_DFTU"
    • cd tests/03_NAO_multik && ../integrate/Autotest.sh -a <abacus> -n 4 -o 1 -f CASES_CPU.txt -r '^scf_u_spin(1|2|4)$'
    • cd tests/02_NAO_Gamma && ../integrate/Autotest.sh -a <abacus> -n 4 -o 1 -f CASES_CPU.txt -r '^scf_u_spin2$'
    • python3 tools/03_code_analysis/agent_governance_check.py --base develop --head HEAD --event-path <pr-event.json> --format text
    • python3 tools/03_code_analysis/code_quality_score.py <modified C++ files>
  • Result summary:
    • Unit tests: 15/15 MODULE_DFTU CTest targets passed (100%), including MODULE_DFTU_fs_reduce, MODULE_DFTU_fs_r, and MODULE_DFTU_op_conjugate_k_mpi4.

    • Integration tests: 03_NAO_multik passed 12/12 checks (scf_u_spin1, scf_u_spin2 unchanged; scf_u_spin4 matches updated totalstressref = 4891.796432, with all 9 DFTU STRESS components halved and total stress tensor shifted component-by-component to < 5e-11 kbar); 02_NAO_Gamma passed 4/4 checks (scf_u_spin2 unchanged).

    • Governance & quality: agent_governance_check.py reported 0 findings; code_quality_score.py passed 5/5 modified C++ files (average score 91.2).

    • Physical finite-difference validation (converged 2-atom rocksalt FeO using tests/PP_ORB, ecutwfc = 160 Ry, scf_thr = 1e-10, fixed 48x48x48 FFT grid, dft_plus_u = 1, U = 5.0 eV, central finite difference $\delta = 5 \times 10^{-4}$; all runs converged in 24–28 SCF iterations; nspin = 4, lspinorb = 0 uses tilted moment (1.0, 0.8, 1.5) activating all four Pauli channels, physically equivalent to nspin = 2 with total energy agreement 8.8e-11 eV):

      Case DFTU STRESS xx (kbar) TOTAL STRESS xx (kbar) FD xx (kbar) TOTAL - FD (kbar)
      nspin = 2 (control, before / after) 52.004025 / 52.004025 -297.943235 / -297.943235 -298.148530 +0.205295
      nspin = 4, lspinorb = 0 (before) 104.008050 (2.000×) -245.939211 -298.148530 +52.209319
      nspin = 4, lspinorb = 0 (after) 52.004025 (1.000×) -297.943235 -298.148530 +0.205294

      After the fix, nspin = 4 (lspinorb = 0) agrees with nspin = 2 to < 4e-7 kbar in both DFTU STRESS and TOTAL STRESS, and matches the finite-difference baseline residual to 6.1e-7 kbar.

  • Checks not run, with reason: Full repository integration test suite and GPU tests were not run locally; changes are strictly scoped to the CPU LCAO DFT+U stress post-processing helper (nspin == 4 branch) and will be covered by PR CI.

Governance Notes

  • INPUT/docs changes: No documentation update required; this PR only fixes an internal scaling bug in existing nspin = 4 LCAO DFT+U stress without adding or changing any INPUT parameters or user-facing interfaces.
  • Core module impact: Strictly scoped to DFTU_LCAO::reduce_stress_impl() in source/source_lcao/module_dftu/ (nspin == 4 stress path only; nspin = 1, 2 and all force/energy/Hamiltonian paths are unchanged).
  • Exceptions requested: None.

For nspin=4 in real-space LCAO DFT+U (dft_plus_u=1), dmR and pot_onsite
are stored in 4-component Pauli form without the 1/2 spinor trace factor.
While cal_for_IJR_nao_r differentiates only one projector overlap (so the
product-rule factor of 2 cancels the 1/2 factor for nspin=4),
cal_str_IJR_nao_r sums both projector derivative terms explicitly and
therefore needs an explicit 0.5 factor in reduce_stress_impl when nspin==4.

- Pass nspin into DFTU_LCAO::reduce_stress_impl and scale by 0.5 for nspin=4
- Add nspin=1/2/4 stress scaling unit tests in test_dftu_nao_fs_reduce.cpp
- Update tests/03_NAO_multik/scf_u_spin4/result.ref totalstressref
@mohanchen
mohanchen requested a review from lanshuyue December 3, 2026 09:11
@mohanchen mohanchen added DFT+U Issues related to DFT plus U function Refactor Refactor ABACUS codes Bugs Bugs that only solvable with sufficient knowledge of DFT Collinear/non-collinear/SOC/delta-spin Issues related to SOC labels Dec 3, 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

Bugs Bugs that only solvable with sufficient knowledge of DFT Collinear/non-collinear/SOC/delta-spin Issues related to SOC DFT+U Issues related to DFT plus U function Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants