Skip to content

BUG: NonlinearVariationalSolver errors with adj_kwargs #5186

Description

@ioannisPApapadopoulos

Describe the bug
Passing adj_kwargs to NonlinearVariationalSolver in order to have different solver_parameters for the adjoint problem errors.

Steps to Reproduce
Here is an MFE:

from firedrake import *
from firedrake.adjoint import *

continue_annotation()

mesh = UnitSquareMesh(20,20)
U = FunctionSpace(mesh, "CG", 1)

u = Function(U)
v = TestFunction(U)

f = Function(U)

F = inner(grad(u),grad(v))*dx - inner(f,v)*dx

bcs = [DirichletBC(U, Constant(1), (4,)),
       DirichletBC(U, Constant(0), (1, 2, 3))]

sp_lu = {
        "snes_monitor": None,
        "pc_type": "lu",
        "pc_factor_mat_solver_type": "mumps",}

# This works
solve(F==0, u, bcs=bcs, solver_parameters=sp_lu, adj_kwargs={"solver_parameters": sp_lu})

nvp = NonlinearVariationalProblem(F, u, bcs=bcs)

# This errors
nvs = NonlinearVariationalSolver(nvp, solver_parameters=sp_lu,
                                 adj_kwargs={"solver_parameters": sp_lu})

Expected behavior
I expect the NonlinearVariationalSolver to accept the adj_kwargs argument.

Error message

Traceback (most recent call last):
  File "/home/user/Documents/Code/codex/debug_adjoint_nvs_2.py", line 36, in <module>
    nvs = NonlinearVariationalSolver(nvp, solver_parameters=sp_lu,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "petsc4py/PETSc/Log.pyx", line 250, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "petsc4py/PETSc/Log.pyx", line 251, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "/usr/lib/python3.12/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/user/firedrake-release-30-05-2026/venv-firedrake/lib/python3.12/site-packages/firedrake/adjoint_utils/variational_solver.py", line 47, in wrapper
    init(self, problem, *args, **kwargs)
TypeError: NonlinearVariationalSolver.__init__() got an unexpected keyword argument 'adj_kwargs'

Environment:

  • OS: Linux
  • Python version: 3.12.3

Additional Info
As pointed out by @dham, the workaround is currently to pass adj_kwargs to the first solve as follows:

nvs = NonlinearVariationalSolver(nvp, solver_parameters=sp_lu)
nvs.solve(adj_kwargs={"solver_parameters": sp_lu})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions