# Iteration solver and direct solver results are huge different with some meshes

**URL:** <https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797>\
**Category:** General Discussion\
**Created:** [February 11, 2021, 3:51pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797 "2021-02-11T15:51:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 3:51pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/1 "2021-02-11T15:51:34Z")

</div>

Hey FreeFem team, I recently debugged my code and found that, with a mesh generated by gmsh, the results from the direct solver and iteration solver under ffddm are totally different; I believe this problem is coming from the mesh, since I test it with other meshes build from freefem directly;  
My question is under which cases, the results of the iteration solver and the direct solver will be in conflict? Cus the mesh from gmsh looks fine; 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:** [February 11, 2021, 3:57pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/2 "2021-02-11T15:57:19Z")

</div>

If you use PETSc, there are automatic tools to help you diagnoze such a scenario, e.g., compute the algebraic error at the end of each linear solve/get a message to see if the iterative solver converged or broke down.

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 3:59pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/3 "2021-02-11T15:59:23Z")

</div>

Thx for the replay prj; Could you please show me how to do so, do I need to pass the flag via the terminal?

---

<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:** [February 11, 2021, 4:09pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/4 "2021-02-11T16:09:00Z")

</div>

Here is a simple example. You can set parameters either via the command line or through the `set` function.

```auto
$ cat simple.edp
load "PETSc"
include "macro_ddm.idp"

macro grad(u)[dx(u), dy(u)]// EOM // two-dimensional gradient
func Pk = P1;

mesh Th = square(getARGV("-global", 40), getARGV("-global", 40));
Mat A;
createMat(Th, A, Pk)

fespace Wh(Th, Pk);
varf vPb(u, v) = int2d(Th)(grad(u)' * grad(v)) + int2d(Th)(v) + on(1, u = 0.0);

set(A, sparams = "-ksp_converged_reason -ksp_view_final_residual");
A = vPb(Wh, Wh, tgv = -1);
real[int] rhs = vPb(0, Wh, tgv = -1);
real[int] u = A^-1 * rhs;

set(A, sparams = "-ksp_max_it 20");
$ ff-mpirun -n 4 simple.edp -v 0
Linear solve converged due to CONVERGED_RTOL iterations 102
KSP final norm of residual 4.46724e-07
Linear solve did not converge due to DIVERGED_ITS iterations 20
KSP final norm of residual 0.0108944
```

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 4:17pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/5 "2021-02-11T16:17:45Z")

</div>

okay, the message I got is “Linear solve did not converge due to DIVERGED\_ITS iterations 10000 & KSP final norm of residual 0.00101905”; Seems my system is bad;

---

<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:** [February 11, 2021, 4:21pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/6 "2021-02-11T16:21:21Z")

</div>

What kind of preconditioner are you using (`-ksp_view`)? Do you get the proper solution with an exact method (`-pc_type lu`)?

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 4:36pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/7 "2021-02-11T16:36:57Z")

</div>

the output of ksp\_viewlooks like this

 ![gc](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/03a1b0dda4b40817288832b213d530f913c8e952.png)  
when I change it back to -lu it is good which looks like the direct solver results and ksp resid is converged as well;

---

<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:** [February 11, 2021, 5:00pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/8 "2021-02-11T17:00:28Z")

</div>

Are you solving an elasticity problem? BJacobi + ILU as subdomain solver is probably the worst kind of preconditioner for this problem. You can use optimal multigrid methods, see the [2D](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/elasticity-2d-PETSc.edp#L50-L58) or [3D](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/elasticity-3d-PETSc.edp#L49-L58) parameters. If this still does not converge well, could you share your script?

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 6:38pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/9 "2021-02-11T18:38:57Z")

</div>

Yes, I am solving the elasticity problem; I will check the link you give me; Thank you very much. now I know how to check the residual of the iteration solver of petsc; I will let you know if the 2D or 3D parameters works or not.

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 6:58pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/10 "2021-02-11T18:58:11Z")

</div>

![Screen Shot 2021-02-11 at 1.55.57 PM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/d02fd7fda0b7f1a5a683d530d39fb0f9b0f44ad6.png)  
One more question. Since I passed petsc flag like this; How can I pass the " _nearnullspace_ = _Rb_" to 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:** [February 11, 2021, 7:30pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/11 "2021-02-11T19:30:25Z")

</div>

There is no real point in using the ffddm wrapper on top of PETSc here, it’s mostly just adding boiler plate code, so I’d just stick to plain PETSc code. But I think you can pass the `nearnullspace` by doing the following just before calling `elastictyfGMRES`:

```auto
set(elastictypetscOP, sparams = "...", nearnullspace = ..., bs = dim);

```

Do _not_ forget to put `bs = dim` (2 or 3), it’s automatically set in the PETSc example, but you’ll need that as well in your script.

Note: there is a typo in your script, it reads `elasticty` instead of `elasticity`, if you fix that, you’ll need to use the variable `elasticitypetscOP`.

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 7:44pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/12 "2021-02-11T19:44:26Z")

</div>

Thx for this replay, I am using ffddm because the

 ![Screen Shot 2021-02-11 at 2.39.07 PM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/71d8a42c4022121fc6445b52a7b9e3f5604f09e7.png) the way to pass this varf seems easy for me; I tried the PETSc framework before; I could not make it at that stage; Thx for the information;

---

<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:** [February 11, 2021, 7:47pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/13 "2021-02-11T19:47:54Z")

</div>

The PETSc interface does not rely on any macro. You define your `varf` as you would do in any FreeFEM script. It’s much simpler than ffddm in that sense.  
Just look at the elasticity [example](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/elasticity-3d-PETSc.edp#L29-L38) again.  
If you type your `varf` in plain text I can give you the PETSc “equivalent”, which will be basically exactly the same.

---

<div class="post-metadata">

**Author:** ![zcwang6](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zcwang6/32/371_2.png) [@zcwang6](https://community.freefem.org/u/zcwang6)\
**Post date:** [February 11, 2021, 8:09pm UTC](https://community.freefem.org/t/iteration-solver-and-direct-solver-results-are-huge-different-with-some-meshes/797/14 "2021-02-11T20:09:36Z")

</div>

Sure, I will test it. Thx;
