# Nan error when solve Navier-Stokes equations using PETSc

**URL:** <https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431>\
**Category:** General Discussion\
**Created:** [January 14, 2022, 12:16pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431 "2022-01-14T12:16:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 14, 2022, 12:16pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/1 "2022-01-14T12:16:34Z")

</div>

Dear all,

I am now working on Navier-Stokes equations with PETSc. I am trying to solve the equations in a `125*100*100` domain, and build this cube domain mesh by Gmsh. However, when I try to solve it with a higher resolution (finer mesh), the simulation will return to a Nan error. The solver I used is:

> set(NS, sparams = “-pc\_type lu”);

I want to know why this error occurs and if there is any way to solve it.  
Thanks in advance!

---

<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:** [January 14, 2022, 12:58pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/2 "2022-01-14T12:58:19Z")

</div>

You can use `-ksp_view_converged_reason` as an additional parameter in your `sparams` to see what is the root of the error.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 14, 2022, 4:22pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/3 "2022-01-14T16:22:21Z")

</div>

Hi,  
Thank you. I added `-ksp_view_converged_reason` as an additional parameter, but there seem no differences in error in the output. The error is mainly this for every core:

> Exec error : In Optimized version  
> – number :1  
> err code 8  
> -nan != -nan diff -nan =\> Sorry error in Optimization (q) add: int2d(Th,optimize=0)(…)  
> remark if you add (… , optimize=2) then you remove this check (be carefull);

---

<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:** [January 14, 2022, 4:37pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/4 "2022-01-14T16:37:59Z")

</div>

OK, this is not related to PETSc, but rather FreeFEM itself. I’m sorry, the flag is `-ksp_converged_reason`, there is not `view_`, could you please try again?

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 14, 2022, 5:27pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/5 "2022-01-14T17:27:40Z")

</div>

Yes, I now get the new error message:

> Linear solve did not converge due to DIVERGED\_PC\_FAILED iterations 0  
> PC failed due to FACTOR\_OUTMEMORY

Do you know how to solve this? Thank you!

---

<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:** [January 14, 2022, 7:09pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/6 "2022-01-14T19:09:28Z")

</div>

Either use a larger machine or switch to an iterative solver.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 16, 2022, 11:03pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/7 "2022-01-16T23:03:47Z")

</div>

Thank you. The problem is resolved!

---

<div class="post-metadata">

**Author:** ![TCXKG](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@TCXKG](https://community.freefem.org/u/TCXKG)\
**Post date:** [October 4, 2024, 1:02pm UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/9 "2024-10-04T13:02:20Z")

</div>

Hello， I’m sorry to interrupt, but I’d like to ask you how it all worked out in the end.

---

<div class="post-metadata">

**Author:** ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)\
**Post date:** [October 23, 2024, 2:03am UTC](https://community.freefem.org/t/nan-error-when-solve-navier-stokes-equations-using-petsc/1431/10 "2024-10-23T02:03:16Z")

</div>

I also met the same error. Are there any suggestions to debug? Thanks.  
`-nan != -nan diff -nan => Sorry error in Optimization add: (p) int2d(Th,optimize=0)(...) remark if you add (.. , optimize=2) then you remove this check (be careful);`
