Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@MauriceDHanisch
Copy link

@MauriceDHanisch MauriceDHanisch commented Sep 1, 2025

Summary

Adds a lightweight restart mechanism that seeds SCF from Mulliken shell charges together with atomic dipole and quadrupole moments. Provides C and Python APIs (including spin-resolved support), wires the guess into wavefunction initialization, and adds tests and a Python example. Default behavior is unchanged unless a guess is explicitly provided.

Motivation

  • Reduce restart payload size vs. full density/coefficients.
  • Enable portable restarts across language bindings (C, Python).
  • Support both closed-shell and open-shell calculations.

Key changes

  • Wavefunction seeding from compact restart data:

    • If a guess is provided, initialize wfn%qsh, wfn%dpat, wfn%qpat, and derive wfn%qat via get_qat_from_qsh.
    • Strict shape checks; all three arrays are required together.
    • Implementation in src/tblite/api/calculator.f90 (check_wavefunction and call site).
  • Result container extended to hold restart guess:

    • charges_guess_shell [nsh, nspin], dpat_guess [3, nat, nspin], qmat_guess [6, nat, nspin].
    • Allocation/population added in C-bindings.
    • Implementation in src/tblite/api/result.f90.
  • C API:

    • tblite_set_result_shell_charges_and_moments_guess(...) (closed-shell).
    • tblite_set_result_shell_charges_and_moments_guess_spin(...) (spin-resolved).
    • Declarations in include/tblite/result.h, implementations in src/tblite/api/result.f90.
  • Python API:

    • Result.set("shell-charges-and-moments-guess", (qsh, dpat, qmat)).
    • Shape normalization and dispatch to the appropriate C API (closed-shell vs. spin).
    • Implemented in python/tblite/interface.py and python/tblite/library.py.
  • Tests and example:

    • New C test test_calc_restart_compressed in test/api/main.c: compares energy from compressed restart vs. full run (closed-shell).
    • Example script restart_from_charges.py: demonstrates compressed restart and compares to a full wavefunction restart; includes a spin-polarized path.

API additions

  • C:
    • tblite_set_result_shell_charges_and_moments_guess(error, res, qsh, nsh, dpat, nat, qmat, nat2)
    • tblite_set_result_shell_charges_and_moments_guess_spin(error, res, qsh, nsh, dpat, nat, qmat, nat2, nspin)
  • Python:
    • Result.set("shell-charges-and-moments-guess", (qsh, dpat, qmat))
    • Accepted shapes:
      • qsh: (nsh,) or (2, nsh)
      • dpat: (nat, 3)/(3, nat) or (2, nat, 3)/(2, 3, nat)
      • qmat: (nat, 6)/(6, nat) or (2, nat, 6)/(2, 6, nat)

Behavior and validation

  • Providing qsh also requires providing dpat and qmat; otherwise, a fatal error is raised via the context.
  • Shapes are validated against the system (nat, nsh) and active spin count.
  • Without a guess, existing initialization is unchanged.

Backward compatibility

  • No breaking changes. Behavior is identical unless the new setter APIs are used.

Affected files

  • src/tblite/api/calculator.f90 (consume guess, seed wfn)
  • src/tblite/api/result.f90 (store and set guess; C bindings)
  • include/tblite/result.h (C API declarations)
  • python/tblite/interface.py (setter wiring)
  • python/tblite/library.py (shape normalization, C calls)
  • test/api/main.c (new compressed-restart test)

Tests

  • test_calc_restart_compressed verifies energy agreement (closed-shell) when restarting from (qsh, dpat, qmat).

Notes

  • CLI behavior unchanged.
  • Documentation and changelog entries can be added in a follow-up.

Enable restarting SCF from compressed data (shell charges and atomic multipole moments)
so that saving/loading the full wavefunction is unnecessary.

- Compressed restart path: accept (qsh, dpat, qmat) as restart guess
- Spin-aware shapes: (nsh)|(2, nsh), (nat,3)|(2, nat,3), (nat,6)|(2, nat,6)
- Python: getters for qsh/dpat/qmat return spin-aware arrays; setter normalizes inputs
- Fortran result API: export flattened spin-aware qsh/dpat/qmat; add spin-aware setter
- Calculator: enforce spin-consistent shapes in check_wavefunction for restarts
- Docs: update Result.get docstring to document compressed restart shapes
- Example: extend restart_from_charges.py to verify spin-polarized compressed restart
Add a new test function `test_calc_restart_compressed` to validate the compressed restart functionality using shell charges and atomic multipole moments. The test checks energy consistency between full and restarted calculations, ensuring proper handling of spin-aware data structures. This enhances the testing framework for the recent compressed restart feature introduced in the SCF calculations.
@MauriceDHanisch MauriceDHanisch marked this pull request as ready for review September 1, 2025 21:17
@codecov
Copy link

codecov bot commented Sep 2, 2025

Codecov Report

❌ Patch coverage is 24.75248% with 76 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.18%. Comparing base (6f6cd7d) to head (46fb115).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/tblite/api/result.f90 16.86% 49 Missing and 20 partials ⚠️
src/tblite/api/calculator.f90 61.11% 4 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #291      +/-   ##
==========================================
- Coverage   71.00%   70.18%   -0.83%     
==========================================
  Files         168      162       -6     
  Lines       20716    20206     -510     
  Branches     7190     7265      +75     
==========================================
- Hits        14710    14181     -529     
+ Misses       2347     2343       -4     
- Partials     3659     3682      +23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant