Skip to content

Fix: DeltaSpin energy fix - #7748

Merged
mohanchen merged 5 commits into
deepmodeling:developfrom
dyzheng:feat/issue-7728-deltaspin-energy-fix
Aug 2, 2026
Merged

Fix: DeltaSpin energy fix#7748
mohanchen merged 5 commits into
deepmodeling:developfrom
dyzheng:feat/issue-7728-deltaspin-energy-fix

Conversation

@dyzheng

@dyzheng dyzheng commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

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

Fix #

Unit Tests and/or Case Tests for my changes

  • Commands run:
  • Result summary:
  • Checks not run, with reason:

What's changed?

  • Example: brief summary of the user-visible or developer-facing change.

Governance Notes

  • INPUT/docs changes:
  • Core module impact:
  • Exceptions requested:

dyzheng and others added 4 commits August 1, 2026 23:57
… basis

- cal_escon(): replace is_Mi_converged gate with lambda_/Mi_ empty check
  to prevent segfault when uninitialized
- elecstate_pw: add get_spin_constrain_energy() override so PW basis
  computes DeltaSpin constraint energy (previously returned 0.0)
- elecstate_pw.h: declare get_spin_constrain_energy() override

Note: this fix enables escon computation for PW DeltaSpin but
lambda values from BFGS optimizer may differ from accel branch
due to energy functional convention differences. Full convergence
with accel requires lambda_loop.cpp migration.
… refs

Source changes:
- cal_escon(): replace is_Mi_converged guard with lambda_/Mi_ empty check
  to prevent segfault when uninitialized (matching accel convention)
- elecstate_pw: add get_spin_constrain_energy() override so PW basis
  includes DeltaSpin constraint energy in total energy (was always 0)

Test refs regenerated for 7 cases with significant energy changes:
- 14_PW_DS_S4_XYZ, 15_PW_DS_S4_Z, 16_PW_DS_S4_XY
- 18_PW_DFTU_DS_S2_Z, 19_PW_DFTU_DS_S4_XY, 21_PW_DFTU_DS_S4_Z
- 41_PW_DS_S4_Thr10_XY

nspin=2 tests and ReadLam/Thr1e10 tests unchanged.
…escon

Source fixes:
- spin_constrain.h: fix My = -Im(occ1-occ2) → Im(occ1-occ2)
  The magnetic moment y-component had the wrong sign in the Pauli matrix
  transformation, causing incorrect Mi computation for nspin=4 DeltaSpin.
- cal_escon(): replace is_Mi_converged guard with lambda_/Mi_ empty check
- elecstate_pw: add get_spin_constrain_energy() for PW basis DeltaSpin

Refs regenerated for nspin=4 DeltaSpin cases: 14, 15, 16, 19, 21, 41
Test 18 unchanged, nspin=2 tests unchanged.
elecstate_pw.cpp now calls spinconstrain::SpinConstrain<
std::complex<double>>::getScInstance()/cal_escon() via the new
get_spin_constrain_energy() override. The MODULE_ESTATE_elecstate_pw
unit test compiles elecstate_pw.cpp directly but did not link the
deltaspin module, causing undefined-reference link errors in
BUILD_TESTING builds (test.yml and cuda.yml CI jobs).

Add spin_constrain.cpp to the test SOURCES, mirroring the existing
MODULE_LCAO_deltaspin_spin_constrain_test pattern.
@mohanchen mohanchen added Bugs Bugs that only solvable with sufficient knowledge of DFT Refactor Refactor ABACUS codes labels Aug 2, 2026
ElecStatePW::get_spin_constrain_energy() is a new virtual override
that needs a definition in the vtable. Test targets (MODULE_HSOLVER_base,
MODULE_HSOLVER_pw, MODULE_HSOLVER_sdft) compile a mock implementation
of ElecStatePW methods instead of linking elecstate_pw.cpp, and were
missing a stub for this new method, causing:
  undefined reference to ElecStatePW::get_spin_constrain_energy()
@mohanchen

Copy link
Copy Markdown
Collaborator

We need to include those tests in CICD.

@mohanchen
mohanchen merged commit d8ff3fb into deepmodeling:develop Aug 2, 2026
17 checks passed
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 Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants