Skip to content

[ad-20] add external residual solver example#53

Open
krystophny wants to merge 29 commits into
developfrom
ad-20-external-residual-solver
Open

[ad-20] add external residual solver example#53
krystophny wants to merge 29 commits into
developfrom
ad-20-external-residual-solver

Conversation

@krystophny

@krystophny krystophny commented Jul 9, 2026

Copy link
Copy Markdown
Member

Adds a small external residual-solver example over fixed-boundary interior DOFs.

  • Builds F(x) from the public raw residual.
  • Routes the gradient through State.energy_gradient (exact when the build provides it, raw-force fallback otherwise) and the Newton-Krylov matvec through State.raw_residual_jvp.
  • Counts residual, preconditioner, and HVP calls; the FD residual JVP adds its two hidden residual evaluations to the counter, the exact JVP adds none.
  • Includes a Krylov root-solver smoke path and a JVP-backed Newton-Krylov step.

Closes #21.

Verification

Test fails before

$ bash -lc 'test -f test-CI/unit-pygvec/test_ad_external_optimizers.py || { echo "missing external optimizer test before AD-20"; exit 2; }'
missing external optimizer test before AD-20

Test passes after

Enzyme-off isolated build (FD path):

$ pytest test-CI/unit-pygvec/test_ad_external_optimizers.py -q --require-pygvec
7 passed in 1.31s

Enzyme-on isolated build (exact path, fixed RZ, LLVM/Flang 22.1.6):

$ pytest test-CI/unit-pygvec/test_ad_external_optimizers.py -q --require-pygvec
7 passed in 0.82s

test_exact_hvp_does_not_count_residual_evaluations asserts the counter contract on both paths. Full stack AD suite at this tip: 31 passed on both builds.

@krystophny

Copy link
Copy Markdown
Member Author

Implemented AD-20 here.

  • Added python/examples/external_optimizers.py for fixed-boundary interior residual solves through the public pyGVEC AD API.
  • Added tests for raw residual projection, raw-gradient fallback, preconditioner LinearOperator, separately counted FD-HVP calls, Krylov root residual reduction, and HVP-backed Newton-Krylov reduction.

Verification: focused AD-20 pytest passed (6 passed), adjacent AD API/preconditioner/HVP regression pytest passed (26 passed).

@krystophny krystophny force-pushed the ad-20-external-residual-solver branch from d4f08fd to 55b423b Compare July 9, 2026 20:01
@krystophny krystophny force-pushed the ad-20-external-residual-solver branch from 55b423b to ee055b6 Compare July 9, 2026 22:09
@krystophny krystophny force-pushed the ad-20-external-residual-solver branch from ee055b6 to cd76d7c Compare July 9, 2026 22:22
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