Preconditioner options not being set properly

Hello all,

I am having trouble setting the preconditioner options for a mixed element problem inside of FenicsX. To give context, I’m trying to set up a fieldsplit preconditioner in order to precondition a saddle point problem. Unfortunately, when I set the options for the preconditioner, it for whatever reason ignored what I just put in and resorts to the default settings. Here’s a snippet of my code:

################ Set Solver Definition
Jac = derivative(L,up,du)
problem = NonlinearProblem(L,up,bcs=BCs, J = Jac)
solver = NewtonSolver(MPI.COMM_WORLD, problem)

# Krylov Subspace Method
ksp = solver.krylov_solver
opts = PETSc.Options()
opts.clear()
option_prefix = ksp.getOptionsPrefix()

_,V_map = W.sub(0).collapse()
_,P_map = W.sub(1).collapse()
ksp.setType("fgmres")
pc = ksp.getPC()
pc.setType("fieldsplit")

IS_V = PETSc.IS().createGeneral(V_map, mesh.comm)
IS_P = PETSc.IS().createGeneral(P_map, mesh.comm)

pc.setFieldSplitIS(("vel",IS_V))
pc.setFieldSplitIS(("press",IS_P))


opts[f"{option_prefix}pc_fieldsplit_type"] = "schur"
opts[f"{option_prefix}pc_fieldsplit_schur_fact_type"] = "full"
opts[f"{option_prefix}pc_fieldsplit_schur_precondition"] = "selfp"
opts[f"{option_prefix}fieldsplit_vel_ksp_type"] = "preonly"
opts[f"{option_prefix}fieldsplit_vel_pc_type"] = "jacobi"
opts[f"{option_prefix}fieldsplit_vel_ksp_rtol"] = 1e-10
opts[f"{option_prefix}fieldsplit_press_ksp_type"] = "cg"
opts[f"{option_prefix}fieldsplit_press_pc_type"] = "none"
opts[f"{option_prefix}fieldsplit_press_ksp_rtol"] = 1e-10

ksp.setFromOptions()
ksp.view()
pc.view()

The output is as follows:
“KSP Object: (nls_solve_) 1 MPI process
type: preonly
maximum iterations=10000, initial guess is zero
tolerances: relative=1e-05, absolute=1e-50, divergence=10000.
left preconditioning
using DEFAULT norm type for convergence test
PC Object: (nls_solve_) 1 MPI process
type: lu
PC has not been set up so information may be incomplete
out-of-place factorization
tolerance for zero pivot 2.22045e-14
PC Object: (nls_solve_) 1 MPI process
type: lu
PC has not been set up so information may be incomplete
out-of-place factorization
tolerance for zero pivot 2.22045e-14”

If I print out tsp and pc before I set the options, it correctly identifies fgmres and the fieldsplit, but after I set the preconditioning options it just uses the default settings. I appreciate any help with debugging this issue. Thank you!

Adding a KSP options prefix resovles your issues. See the following minimal reproducible example

from mpi4py import MPI
from petsc4py import PETSc
import dolfinx.fem.petsc
import dolfinx.nls.petsc
import ufl

comm = MPI.COMM_WORLD
mesh = dolfinx.mesh.create_unit_square(comm, 10, 10)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))
u = dolfinx.fem.Function(V)
v = ufl.TestFunction(V)
F = ufl.inner(u, v) * ufl.dx
problem = dolfinx.fem.petsc.NonlinearProblem(
    F,
    u,
)
solver = dolfinx.nls.petsc.NewtonSolver(MPI.COMM_WORLD, problem)

# Krylov Subspace Method
ksp = solver.krylov_solver
opts = PETSc.Options()
opts.clear()
ksp.setOptionsPrefix("ksp_")
option_prefix = ksp.getOptionsPrefix()

ksp.setType("fgmres")
pc = ksp.getPC()
pc.setType("fieldsplit")

# IS_V = PETSc.IS().createGeneral(V_map, mesh.comm)
# IS_P = PETSc.IS().createGeneral(P_map, mesh.comm)

# pc.setFieldSplitIS(("vel", IS_V))
# pc.setFieldSplitIS(("press", IS_P))


opts[f"{option_prefix}pc_fieldsplit_type"] = "schur"
opts[f"{option_prefix}pc_fieldsplit_schur_fact_type"] = "full"
opts[f"{option_prefix}pc_fieldsplit_schur_precondition"] = "selfp"
opts[f"{option_prefix}fieldsplit_vel_ksp_type"] = "preonly"
opts[f"{option_prefix}fieldsplit_vel_pc_type"] = "jacobi"
opts[f"{option_prefix}fieldsplit_vel_ksp_rtol"] = 1e-10
opts[f"{option_prefix}fieldsplit_press_ksp_type"] = "cg"
opts[f"{option_prefix}fieldsplit_press_pc_type"] = "none"
opts[f"{option_prefix}fieldsplit_press_ksp_rtol"] = 1e-10

ksp.setFromOptions()
ksp.view()
pc.view()

yielding:

KSP Object: (ksp_) 1 MPI process
  type: fgmres
    restart=30, using Classical (unmodified) Gram-Schmidt Orthogonalization with no iterative refinement
    happy breakdown tolerance 1e-30
  maximum iterations=10000, initial guess is zero
  tolerances: relative=1e-05, absolute=1e-50, divergence=10000.
  left preconditioning
  using DEFAULT norm type for convergence test
PC Object: (ksp_) 1 MPI process
  type: fieldsplit
  PC has not been set up so information may be incomplete
    FieldSplit with Schur preconditioner, factorization FULL
    Split info:
    KSP solver for A00 block
        not yet available
    KSP solver for S = A11 - A10 inv(A00) A01
        not yet available
PC Object: (ksp_) 1 MPI process
  type: fieldsplit
  PC has not been set up so information may be incomplete
    FieldSplit with Schur preconditioner, factorization FULL
    Split info:
    KSP solver for A00 block
        not yet available
    KSP solver for S = A11 - A10 inv(A00) A01
        not yet available
WARNING! There are options you set that were not used!
WARNING! could be spelling mistake, etc!
There are 6 unused database options. They are:
Option left: name:-ksp_fieldsplit_press_ksp_rtol value: 1e-10 source: code
Option left: name:-ksp_fieldsplit_press_ksp_type value: cg source: code
Option left: name:-ksp_fieldsplit_press_pc_type value: none source: code
Option left: name:-ksp_fieldsplit_vel_ksp_rtol value: 1e-10 source: code
Option left: name:-ksp_fieldsplit_vel_ksp_type value: preonly source: code
Option left: name:-ksp_fieldsplit_vel_pc_type value: jacobi source: code

I am not quite sure why PETSc doesn’t like the original options name.
You could add an issue to DOLFINx, if you can make a reproducible example.