Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firedrake/solving_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def set_defaults(solver_parameters, arguments, *, ksp_defaults=None, snes_defaul
if "pc_type" in keys:
# Might reasonably expect to get petsc defaults
skip.update({"pc_factor_mat_solver_type", "ksp_type"})
if parameters.get("mat_type") in {"matfree", "nest"}:
if parameters.get("mat_type") in {"matfree", "nest", "python"}:
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.

Whoever populated this dictionary with {"mat_type": A.type} within the adjoint should have set {"mat_type": "matfree" if A.type == "python" else A.type}.

Or more correclty, the matrix type should come from _SNESContext.mat_type

Copy link
Copy Markdown
Contributor

@pbrubeck pbrubeck May 29, 2026

Choose a reason for hiding this comment

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

I think the logic here could be responsible, but not 100% sure

def _get_mat_type(petscmat: PETSc.Mat) -> str:
"""Maps PETSc matrix types to Firedrake notation"""
mat_type = petscmat.getType()
if mat_type.startswith("seq") or mat_type.startswith("mpi"):
return mat_type[3:]
return mat_type

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.

Yeah that's what I found. I think we should:

  1. Intercept mat_type=python
  2. Extract the Python context from the mat
  3. Use that to return matfree, denserow etc

# Non-LU defaults.
ksp_defaults["ksp_type"] = "gmres"
ksp_defaults["pc_type"] = "jacobi"
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.

This might be unrelated, but MUMPS supports MatNest when all blocks are aij. We shouldn't default to jacobi in that case.

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.

Yeah that's definitely unrelated to this. But we should open an issue for it at least.

Expand Down
20 changes: 20 additions & 0 deletions tests/firedrake/adjoint/test_solving.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,26 @@ def test_two_nonlinear_solves():
assert rf.tape.recompute_count == 5


@pytest.mark.skipcomplex
def test_real_solve(rg):
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, "CG", 1)
R = FunctionSpace(mesh, "R", 0)

u = TrialFunction(R)
v = TestFunction(R)
m = Function(V).assign(1.0)

def J(m):
a = u * v * dx
L = m * v * dx
solution = Function(R)
solve(a == L, solution)
return assemble(solution**2 * dx)

_test_adjoint(J, m, rg)


def convergence_rates(E_values, eps_values):
from numpy import log
r = []
Expand Down