# Iterative solution of the Navier-Stokes equation

**URL:** <https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116>\
**Category:** General Discussion\
**Created:** [July 25, 2021, 8:22pm UTC](https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116 "2021-07-25T20:22:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [July 25, 2021, 8:22pm UTC](https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116/1 "2021-07-25T20:22:59Z")

</div>

Dear FreeFem users!

I am interested in solving (a slightly modified form of the) Navier-Stokes equation, and I would like to test whether LU factorization or iterative solvers perform better. I have found some issues which I could not figure out. These issues can be reproduced on the slightly modified PETSc stationary NS equations [example](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/navier-stokes-2d-PETSc.edp).

1. Changing line [56](https://github.com/FreeFem/FreeFem-sources/blob/897a6ed0606d497b043b593e0d7db86448731889/examples/hpddm/navier-stokes-2d-PETSc.edp#L56) to  
`set(J, sparams = "-ksp_monitor -ksp_monitor_true_residual -ksp_type fgmres");`  
results in a divergent iteration with the following message:  
0 KSP unpreconditioned resid norm 1.183215956620e+01 true resid norm -nan ||r(i)||/||b|| -nan  
Although this method is far from optimal and I will not use it, it annoys me that I cannot figure out the reason for this. Any ideas?
2. Using the modified augmented [lagrangian preconditioner method](https://github.com/prj-/moulin2019al), if the problem is solved inside a for loop, the first time the loop is entered, it is executed correctly. However, as SNESSolve is called the second time the loop is entered, the program throws an error. I attach the slightly modified version of the Navier-Stokes example (kind of an MWE), which reproduces this error. Any ideas on how this issue can be resolved?  
[navier-stokes-2d-PETSc-iter-mAL-MWE.edp](https://community.freefem.org/uploads/short-url/umgL304CWvRHaeQNqySN7kW73Dj.edp) (4.6 KB)

Thanks for the help in advance  
Andras

---

<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 25, 2021, 8:39pm UTC](https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116/2 "2021-07-25T20:39:56Z")

</div>

1. By default, PETSc will use a block Jacobi method with ILU(0) subdomain solver. This fails with the zero pressure block. Just add a penalization term in your Jacobian `varf`, e.g., `1e-8 * p * q`, to make your `sparams` work
2. Use the `ChangeSchur` function to update the array of Schur complements and do not reset the PCFIELDSPLIT fields every time you are in the loop

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [July 26, 2021, 9:24am UTC](https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116/3 "2021-07-26T09:24:40Z")

</div>

Thanks for the quick reply!

I tried to figure out how the ChangeSchur function work based on the [PETSc-code.hpp](https://github.com/FreeFem/FreeFem-sources/blob/master/plugin/mpi/PETSc-code.hpp) file. My solution is the following:

```auto
    if(i==0){
        set(J, sparams = params, fields = vX[], names = names, schurPreconditioner = S, schurList = listX[]);
    } else {
        ChangeSchur(J,S,listX[]);
    } 

```

However, this yields the following error message:

[0]PETSC ERROR: #1 PCCompositeGetPC() at /builds/workspace/FreeFEM-sources-deployDEB-withPETSc/3rdparty/ff-petsc/petsc-3.15.0/src/ksp/pc/impls/composite/composite.c:589  
[0]PETSC ERROR: ------------------------------------------------------------------------  
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range  
[0]PETSC ERROR: Try option -start\_in\_debugger or -on\_error\_attach\_debugger  
[0]PETSC ERROR: or see [PETSc: Documentation: FAQ](https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind)  
[0]PETSC ERROR: or try [http://valgrind.org](http://valgrind.org) on GNU/linux and Apple Mac OS X to find memory corruption errors  
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run  
[0]PETSC ERROR: to get more information on the crash.  
[0]PETSC ERROR: #2 User provided function() at unknown file:0  
[0]PETSC ERROR: Run with -malloc\_debug to check if memory corruption is causing the crash.

I would be glad if you could help what causes this issue. I am running FreeFem++ version 4.9 installed from the .deb package, on Ubuntu 18.04.5 LTS bionic.

---

<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 26, 2021, 11:11am UTC](https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116/4 "2021-07-26T11:11:33Z")

</div>

Thanks, this is fixed in [No PCCompositeGetPC with a single Schur Pmat. · FreeFem/FreeFem-sources@7cd3bd6 · GitHub](https://github.com/FreeFem/FreeFem-sources/commit/7cd3bd689c9f2cebc4077dfcf583cd9e6f750484). If you are sticking to precompiled binaries, I’d just advice not to update the Schur complement preconditioner for `i > 0`, that should still converge somehow reasonably well (I think).

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [July 26, 2021, 11:27am UTC](https://community.freefem.org/t/iterative-solution-of-the-navier-stokes-equation/1116/5 "2021-07-26T11:27:34Z")

</div>

Thank you very much for the fix!
