Repository navigation
Give the adjoint solve the forward null space as its transpose null space - #5734
sghelichkhani wants to merge 2 commits into
Conversation
…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.
| transpose_nullspace = self.adj_kwargs.get("transpose_nullspace") | ||
| if transpose_nullspace is None: | ||
| transpose_nullspace = nullspace |
There was a problem hiding this comment.
| 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?
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Does mumps handle the singular system here?
There was a problem hiding this comment.
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 is a follow-up to #5702, which fixed #5697. #5702 swapped
nullspaceandtranspose_nullspacefor the adjoint solve only when a transpose null space was given. When onlynullspacewas 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 adjointtranspose_nullspacein every case.For pure Neumann Poisson with only
nullspacegiven, 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
nullspaceas itstranspose_nullspace, and the forwardtranspose_nullspaceas itsnullspace. If no transpose null space is given, I follow Pablo's suggestion and assume a symmetric operator, so the forwardnullspaceis used for both. The condition is the one James suggested. User-givenadj_kwargsare still used as they are. For a nonsymmetric singular operator the user has to givetranspose_nullspaceto get a correct adjoint with a direct solver, as before.The new tests in
test_solving.pycoversolve,LinearVariationalSolverandNonlinearVariationalSolveron 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.