Try to handle solver caching for linear forms (-> #4638)#5158
Merged
JHopeCollins merged 5 commits intoJun 16, 2026
Conversation
b293800 to
0fe0ac4
Compare
Contributor
|
Some of these workarounds indicate that there's work to be done in UFL. Could you raise an issue in UFL for these failing cases, providing an MFE? |
7b31a77 to
8a85c16
Compare
6f6fd15 to
440f386
Compare
These seem to be a somewhat historical relic from when both NLVS
and LVS code paths could end up in the same solver block. The reason
for excluding the linear case is not clear, perhaps because the RHS
of the problem is involved in some way?
Regardless, we always enter the solver block normalised into a
NLVP which is always "nonlinear" according to the original
condition:
linear = (
isinstance(lhs, ufl.Form) and
isinstance(rhs, (ufl.Form, ufl.Cofunction)
)
Without this change, we can erroneously evaluate the adjoint component
corresponding to the initial guess.
I think this approach is more consistent with the underlying maths, but my UFL is not particularly strong. This way allows expand_derivatives to simplify expressions as required, and the slot treatments of arguments is consistent across the cached forms.
8a85c16 to
fa1af48
Compare
Contributor
Author
|
Alright, I went back through the math and rewrote the expressions to step around the hacks. Seems much cleaner and everything seems to pass now. Next hurdle is the forward cache on the full waveform inversion demo. TracebackLooks like explicitly providing the Jacobian from a linear problem (with appropriate substitutions) works, but it does highlight that we need to be a bit clever setting up the cached solvers! |
The full waveform inversion demo fails in the matrix free assembler when trying to deal with the automatically differentiated form of the NLVP. However, it's actually a linear problem, so we know the Jacobian. Indeed, the Jacobian was originally passed to the NLVP construction from the LVP in the first place, so we should mirro this.
In addition to caching Jp if it exists, we unconditionally cache the Jacobian from the NLVP. Co-authored-by: Josh Hope-Collins <jhc.jss@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When we try to cache the Hessian and TLM solvers for a linear form (the L2 Riesz representation), there are a few assumptions about the resulting derivative forms that fail:
skip adding to the Hessian formsadd explicit zero forms in that case