Skip to content

Conversation

@JHopeCollins
Copy link
Member

@JHopeCollins JHopeCollins commented Oct 13, 2025

This PR fixes a bug in the Hessian calculation when calling solver.solve multiple times on the same NonlinearVariationalSolver.

The Hessian calculation requires the adjoint variable. The GenericSolveBlock stores this on the block, so if there are multiple calls to firedrake.solve then each block has it's own adjoint solution and everything works fine.
However, theNonlinearVariationalSolveBlock stored the solution of the cached adjoint solver. This solver is shared between all blocks created by the same NonlinearVariationalSolver so if there are multiple blocks then they overwrite each others adjoint solutions.
This PR fixes this by making each block stash their own adjoint solution to reuse when calculating the Hessian action.

This is a quickfix to solve the issue on release. There's a bigger refactor of the cached solvers to improve the performance in this PR: #4638 but that should probably go into main.

angus-g
angus-g previously approved these changes Oct 14, 2025
Copy link
Contributor

@angus-g angus-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to cache the intermediate adjoint solution on the block rather than relying on the solver output. I've tested that this does indeed reproduce and subsequently fix the issue.

Also sweeps up a bunch of warnings in the Hessian tests which is nice.

@JHopeCollins
Copy link
Member Author

@angus-g I fixed one small annotation error (the only test fail on the last commit) and a few more warnings.

@JHopeCollins JHopeCollins removed the request for review from angus-g October 14, 2025 10:00
@JHopeCollins
Copy link
Member Author

@dham pointed out that my original fix was preventing pyadjoint from cleaning up the adjoint state properly with this call:
https://github.com/dolfin-adjoint/pyadjoint/blob/7b56717e5ff7562ea4d25d9f3f1e5cdb4294072a/pyadjoint/block.py#L101-L104

I've redone the fix to use the existing adj_sol property of the solve block so pyadjoint can clean up the state.

@JHopeCollins JHopeCollins requested a review from angus-g October 14, 2025 10:39
@dham dham enabled auto-merge (squash) October 14, 2025 15:52
@dham dham merged commit 927744d into release Oct 14, 2025
7 checks passed
@dham dham deleted the JHopeCollins/bugfix-nlvs-hessian branch October 14, 2025 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants