Applying a modified preconditioner matrix in PETSc

It is possible, but I had to make design choices that made the API “easy” for beginners, and slightly less “easy” for power users. I’d consider this a power user feature, thus the API is not very “easy”.

Here goes. KSPSetOperators() is always called by Amat in both slots. However, you can trick FreeFEM (or PETSc, whatever you prefer), by using a PCSHELL with your KSP. To do that, you have to define a func PetscScalar[int] Pmat(PetscScalar[int]& in) { PetscScalar[int] out; KSPSolve(P, in, out); return out; } and then call set(Amat, precon = Pmat, sparams = "-pc_type shell");.
Of course, you may want to adjust the Pmat function to your needs, see, e.g., FreeFem-sources/oseen-2d-PETSc.edp at develop · FreeFem/FreeFem-sources · GitHub.

If this does not fits your needs, please let me know.

1 Like