# Preconditioner for PETSc Mat

**URL:** <https://community.freefem.org/t/preconditioner-for-petsc-mat/1096>\
**Category:** General Discussion\
**Created:** [July 16, 2021, 8:13pm UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096 "2021-07-16T20:13:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)\
**Post date:** [July 16, 2021, 8:13pm UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/1 "2021-07-16T20:13:01Z")

</div>

Deat all,

How to specify a block preconditioner in PETSc. For example, specify preconditioner  
P=  
A 0  
0 M  
for solving Kx=b, with  
K=  
A B  
B’ 0

Best,  
Yongxing.

---

<div class="post-metadata">

**Author:** ![zhaog6](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zhaog6](https://community.freefem.org/u/zhaog6)\
**Post date:** [July 17, 2021, 1:52am UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/2 "2021-07-17T01:52:17Z")

</div>

You can use FieldSplit preconditioner in PETSc and refer to those examples with the “fieldsplit” keyword in FreeFEM.

---

<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:** [July 17, 2021, 4:52am UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/3 "2021-07-17T04:52:39Z")

</div>

There is also a general introduction to fieldsplitting in FreeFEM [there](http://jolivet.perso.enseeiht.fr/FreeFem-tutorial/main.pdf#page=162).

---

<div class="post-metadata">

**Author:** ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)\
**Post date:** [July 18, 2021, 10:04am UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/4 "2021-07-18T10:04:56Z")

</div>

@zhaog6 @prj  
Thank you both very much. I will look at this later.

---

<div class="post-metadata">

**Author:** ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)\
**Post date:** [July 18, 2021, 5:47pm UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/5 "2021-07-18T17:47:00Z")

</div>

@prj  
Dear @zhaog6 ,  
Thanks for your help! I have tried the “fieldsplit”, however it’s easy to come across a problem.  
The first step works fine, and both velocity and pressure field are correct. However, in the second step, the solver complains: **To use Schur complement preconditioner you must have exactly 2 fields…** , which I believe it should have 2 fields, because the first step works fine and the matrix has the same structure in the second step-- really strange.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/eb26e39b4cc692d7d61f39b5d4005b7dd5bbd9c9.png)  
[Ths\_cylinder.mesh](https://community.freefem.org/uploads/short-url/kho8Ob8YUCkpsUtRUbzQYiQJNST.mesh) (373.3 KB)  
[Th\_cylinder.mesh](https://community.freefem.org/uploads/short-url/tuRWcVeOFDGFB0zmQNx6dp3KTwH.mesh) (639.7 KB)  
[fsi\_3d\_vector.edp](https://community.freefem.org/uploads/short-url/4cMnHEaa4LjZRqFCKUHh4OUicqr.edp) (6.2 KB)

Do you have any idea?  
BTW, the direct LU solver has no problem.

Best,  
Yongxing.

---

<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:** [July 18, 2021, 6:30pm UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/6 "2021-07-18T18:30:06Z")

</div>

You are probably calling `set(..., sparams = "...")` multiple times on the same matrix.

---

<div class="post-metadata">

**Author:** ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)\
**Post date:** [July 18, 2021, 7:25pm UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/7 "2021-07-18T19:25:07Z")

</div>

@prj  
I did try to put **A=fluid(…) and set (…, sparams=…)** outside the loop, however I came across other issues. A related question is :

If the mesh **Ths** is moved , should I call  
**transferMat(Th, Pk, A, Ths, Pk, B, P)**  
again and again (inside the time loop)?

Both **Th** and **A** are static, but the boundary condition (inlet velocity) depends on time, should I put assembly of **A** outside the time loop?

---

<div class="post-metadata">

**Author:** ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)\
**Post date:** [July 18, 2021, 10:06pm UTC](https://community.freefem.org/t/preconditioner-for-petsc-mat/1096/8 "2021-07-18T22:06:53Z")

</div>

@zhaog6 @prj  
Indeed, I really want to call set(…) only once (outside the time loop). However, when I arrange the following code just outside the time loop:

Mat A,B;  
{  
macro def(i)[i, i#B, i#C, i#D]//  
macro init(i)[i, i, i, i]//  
createMat(Th, A, Pk)  
createMat(Ths, B, Pk)  
}  
and

Mat P;  
transferMat(Th, Pk, A, Ths, Pk, B, P)

inside the loop.

I came across the following error:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/cc10d2be98b5b291e9e7f77eb6db7b27c49372a7.png)

**I have no idea which array size is 1?** all arrays do have a size of 4.  
[fsi\_3d\_vector\_opti.edp](https://community.freefem.org/uploads/short-url/bQARuwb6X3aPoyxW7oVx4QkKLbs.edp) (6.0 KB)
