# Applying a modified preconditioner matrix in PETSc

**URL:** <https://community.freefem.org/t/applying-a-modified-preconditioner-matrix-in-petsc/1142>\
**Category:** General Discussion\
**Created:** [August 6, 2021, 10:37am UTC](https://community.freefem.org/t/applying-a-modified-preconditioner-matrix-in-petsc/1142 "2021-08-06T10:37:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [August 6, 2021, 10:47am UTC](https://community.freefem.org/t/applying-a-modified-preconditioner-matrix-in-petsc/1142/2 "2021-08-06T10:47:46Z")

</div>

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](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/oseen-2d-PETSc.edp#L120).

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

---

_[View the full topic](https://community.freefem.org/t/applying-a-modified-preconditioner-matrix-in-petsc/1142)._
