Skip to content

Give the adjoint solve the forward null space as its transpose null space - #5734

Open
sghelichkhani wants to merge 2 commits into
mainfrom
sghelichkhani/adjoint-transpose-nullspace
Open

sghelichkhani wants to merge 2 commits into
mainfrom
sghelichkhani/adjoint-transpose-nullspace

Conversation

@sghelichkhani

Copy link
Copy Markdown
Contributor

This is a follow-up to #5702, which fixed #5697. #5702 swapped nullspace and transpose_nullspace for the adjoint solve only when a transpose null space was given. When only nullspace was given, the adjoint kept it as its null space and got no transpose null space, so nothing removed the component of dJ/du along the forward null space from the adjoint right-hand side. As James pointed out in the review of #5702, the forward null space is the left null space of the adjoint operator and has to reach the adjoint transpose_nullspace in every case.

For pure Neumann Poisson with only nullspace given, a forward right-hand side that is orthogonal to the constants and J = ∫(u − g)² dx with g not mean free, main gives a gradient Taylor rate of 1 with LU, and the CG adjoint solve diverges. GMRES gives the right gradient because PETSc removes the null space from each preconditioner output. Replay is correct in all cases.

The adjoint now gets the forward nullspace as its transpose_nullspace, and the forward transpose_nullspace as its nullspace. If no transpose null space is given, I follow Pablo's suggestion and assume a symmetric operator, so the forward nullspace is used for both. The condition is the one James suggested. User-given adj_kwargs are still used as they are. For a nonsymmetric singular operator the user has to give transpose_nullspace to get a correct adjoint with a direct solver, as before.

The new tests in test_solving.py cover solve, LinearVariationalSolver and NonlinearVariationalSolver on 1 and 2 ranks. One test checks replay and one checks the gradient Taylor rate with LU and with CG. The gradient tests fail on main. The replay test passes on main too, since the forward solve does not change.

The tangent linear solve still gets none of the forward solver arguments (#5703), so the Hessian of this example stays wrong until that is fixed.

AI-assisted with Claude Code.

…pace

The forward null space is the left null space of the adjoint operator, so
it must reach the adjoint transpose_nullspace whenever it is given. Before,
the swap happened only when a transpose_nullspace was given. With only a
nullspace, the component of dJ/du along it stayed in the adjoint
right-hand side: the gradient was wrong with a direct solver and the
conjugate gradient adjoint solve did not converge.

The adjoint nullspace is the forward transpose_nullspace. Without one, the
operator is taken as symmetric and the forward nullspace is used for both.
@sghelichkhani sghelichkhani added the LLM used An LLM was used in the production of this PR label Nov 27, 2026
@pbrubeck
pbrubeck requested a review from jrmaddison November 27, 2026 08:34
Comment on lines +563 to +565
transpose_nullspace = self.adj_kwargs.get("transpose_nullspace")
if transpose_nullspace is None:
transpose_nullspace = nullspace

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
transpose_nullspace = self.adj_kwargs.get("transpose_nullspace")
if transpose_nullspace is None:
transpose_nullspace = nullspace
transpose_nullspace = self.adj_kwargs.get("transpose_nullspace", nullspace)

Might be surprising in some cases, e.g. for a non-symmetric problem with trivial transpose nullspace and nontrivial nullspace. Maybe document?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. If I read it right this differs from the current check only when transpose_nullspace=None is passed explicitly. None is the default in every signature and G-ADOPT always passes the key so I would like an explicit None to mean not given. I kept the is None check and documented the default in the docstrings including that a nonsymmetric singular operator has to pass transpose_nullspace.

Comment thread tests/firedrake/adjoint/test_solving.py Outdated
Comment on lines +132 to +134
consistent. The functional J = (u - g)^2 dx has g with a nonzero mean, so
the adjoint right-hand side has a component along the constants. The
adjoint solve must remove this component with its transpose null space.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i.e. for the test the functional should be defined such that the adjoint RHS is singular inconsistent, so that the correct (forward) nullspace is needed.

I think this test is also implicitly using the transpose nullspace = nullspace default assumption.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. g is not mean free so the adjoint right hand side is far from consistent and the test now asserts that. I also added a case with both null spaces given explicitly.

uh = Function(V)
L = inner(grad(f), grad(v))*dx
if solver_type == "solve":
solve(a == L, uh, **kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does mumps handle the singular system here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Without extra options MUMPS reports no null pivot since the last pivot is of round off size. PETSc then removes the constant from the solution which works for a consistent right hand side but not for the inconsistent adjoint one on main. The test now sets mat_mumps_icntl_24 to 1 and mat_mumps_cntl_3 to 1e-12 so MUMPS detects the null pivot and the test still fails on main.

…tests

Say in the docstrings of solve and the variational solvers that an
annotated solve without transpose_nullspace takes it to be equal to the
nullspace for the adjoint, and that a nonsymmetric singular operator must
pass transpose_nullspace.

In the tests, assert that the adjoint right-hand side has a large
component along the constants, add the case with both null spaces given,
and turn on the MUMPS null pivot detection for the singular matrix.

This branch has not been deployed

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

Labels

LLM used An LLM was used in the production of this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adjoint solves get the forward nullspace and transpose_nullspace without swapping them

2 participants