# Efficient solver and preconditionner for 3D Stokes Problem

**URL:** <https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318>\
**Category:** General Discussion\
**Created:** [March 7, 2023, 7:51am UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318 "2023-03-07T07:51:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 7:51am UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/1 "2023-03-07T07:51:40Z")

</div>

Dear all,

I am solving a 3D Stokes problem with PETSc in parallel, with this variationnal formulation `grad(u)' * grad(v) + grad(uB)' * grad(vB) + grad(uC)' * grad(vC) - div(u) * q - div(v) * p + epsq * p * q `

To solve this I am currently using the solver `gmres` with the `LU` preconditionning ` -ksp_type gmres -pc_type lu -pc_factor_mat_solver_type mumps`.

However this method, is memory and time consuming due to the `LU` factorization (so I can only run small case even on HPC).

I have also try the solver `block jacobi` with a `ILU` preconditionning, but the the linear solver does not converge.

Could someone know a solver and a preconditionner to solve efficiently such a problem ?

Thank you in advance for your help,

Best regards,

Loïc,

---

<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:** [March 7, 2023, 8:06am UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/2 "2023-03-07T08:06:51Z")

</div>

Use fieldsplit and look at the literature.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 8:29am UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/3 "2023-03-07T08:29:32Z")

</div>

Thanks ! I will have a look on this.

Loïc,

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 10:24am UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/4 "2023-03-07T10:24:32Z")

</div>

I have tried the solver proposed on the FreeFEM git: `https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/stokes-fieldsplit-3d-PETSc.edp`

```auto
set(A, sparams = " -ksp_type fgmres -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_fact_type lower -pc_fieldsplit_detect_saddle_point -fieldsplit_velocity_sub_pc_type lu "
               + " -fieldsplit_pressure_sub_pc_type lu -fieldsplit_velocity_sub_pc_factor_mat_solver_type mumps -fieldsplit_pressure_sub_pc_factor_mat_solver_type mumps -ksp_monitor -ksp_view "
               + " -fieldsplit_velocity_ksp_type gmres -fieldsplit_velocity_ksp_max_it 5 -fieldsplit_pressure_ksp_type gmres -fieldsplit_pressure_ksp_max_it 5 -ksp_rtol 1e-6");

```

However, this solver is slower that my previous solver and gives me an incorrect result.

Do you know what might be the cause ?  
or how could I adapt it ?

Thank you in advance,

Best regards,

Loïc,

---

<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:** [March 7, 2023, 12:37pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/5 "2023-03-07T12:37:18Z")

</div>

What does -ksp\_converged\_reason return?

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 12:50pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/6 "2023-03-07T12:50:53Z")

</div>

It returns

```auto
 0 KSP Residual norm 4.595362033965e+31 
 1 KSP Residual norm 3.698028393291e+16 
Linear solve converged due to CONVERGED_RTOL iterations 1

```

Loïc,

---

<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:** [March 7, 2023, 12:52pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/7 "2023-03-07T12:52:51Z")

</div>

Are you using the default `tgv` value of 10^{30}?

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 12:54pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/8 "2023-03-07T12:54:33Z")

</div>

Yes I am using the default one.

---

<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:** [March 7, 2023, 1:01pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/9 "2023-03-07T13:01:28Z")

</div>

Then switch to something else first.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 1:10pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/10 "2023-03-07T13:10:58Z")

</div>

I have tried different value of `tgv` but the result remain not correct and the residuals remain big.

---

<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:** [March 7, 2023, 1:12pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/11 "2023-03-07T13:12:50Z")

</div>

What do you get with `tgv = -1` + `-ksp_monitor_true_residual -pc_type lu`?

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 1:19pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/12 "2023-03-07T13:19:08Z")

</div>

By doing this, I get the right result:

```auto
-->Solving with PETSc began....
  0 KSP preconditioned resid norm 2.720997719081e+02 true resid norm 4.595728221659e+01 ||r(i)||/||b|| 1.000000000000e+00
  1 KSP preconditioned resid norm 1.879238360449e-04 true resid norm 1.609760664389e-10 ||r(i)||/||b|| 3.502732508859e-12

```

and it is much faster.

Loïc,

---

<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:** [March 7, 2023, 1:22pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/13 "2023-03-07T13:22:05Z")

</div>

For the largest problem you tried with this, what was the performance of the method? Also, if the problem is symmetric with homogeneous Dirichlet boundary conditions (`tgv = -2`), you should consider switching to `-pc_type cholesky`.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 1:33pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/14 "2023-03-07T13:33:42Z")

</div>

With `-ksp_type gmres -pc_type lu -pc_factor_mat_solver_type mumps` I have been able to run a case with about 384 000 tetras (`mesh3 cube(40, 40, 40)`) on 144 CPUs and it takes 53 min.

I have not succeeded to run larger case with this method,

Loïc,

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 1:41pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/15 "2023-03-07T13:41:09Z")

</div>

I have try `tgv=-2` with `-pc_type cholesky`. The code converges, but the results are not correct.

---

<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:** [March 7, 2023, 1:42pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/16 "2023-03-07T13:42:15Z")

</div>

Is your problem symmetric with homogeneous Dirichlet boundary conditions?

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 1:45pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/17 "2023-03-07T13:45:46Z")

</div>

Sorry for my misunderstanding, I don’t have homogeneous Dirichlet boundary conditions. And my problem I think is not symmetric due to the extra term `-eps*p*q`.

---

<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:** [March 7, 2023, 1:47pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/18 "2023-03-07T13:47:09Z")

</div>

The term `-eps*p*q` is symmetric, but if you don’t have homogeneous Dirichlet BC, `tgv=-2` is useless indeed. 53 minutes for the above problem suggest that something is wrong on your machine.

---

<div class="post-metadata">

**Author:** ![Loic](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@Loic](https://community.freefem.org/u/Loic)\
**Post date:** [March 7, 2023, 1:55pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/19 "2023-03-07T13:55:16Z")

</div>

Considering a case with 750 000 tetras (`cube(50, 50, 50)`) I have not been able to run it even using more that 200 CPUs.

The PETSc iterations don’t even start. This suggests that there is indeed a problem on the machine. But in the other hand it is known that LU factorization is time and memory consuming.

So I don’t know what is exactly the problem

Loïc,

---

<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:** [March 7, 2023, 2:00pm UTC](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318/20 "2023-03-07T14:00:23Z")

</div>

Share the test case, I will let you know how much time it takes on a properly configured machine.

[Next page](https://community.freefem.org/t/efficient-solver-and-preconditionner-for-3d-stokes-problem/2318.md?page=2)
