# SNES with multigrid for the Jacobian

**URL:** <https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774>\
**Category:** General Discussion\
**Created:** [February 26, 2025, 12:58am UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774 "2025-02-26T00:58:51Z")\
**Posts on this page:** 13\
**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:** [February 26, 2025, 12:58am UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/1 "2025-02-26T00:58:51Z")

</div>

Hello Everyone,

I am trying to implement a PETSc simple multigrid method for inverting the Jacobian in SNES. However, even after examining the available examples, I am struggling to figure this out. I am attaching an MWE which is a modification of an example FreeFEM script.

Any help is appreciated!  
[navier-stokes-2d-PETSc\_prec\_MWE.edp](https://community.freefem.org/uploads/short-url/uaEUIyU4zJ37mklm9AP4jLvGvwT.edp) (3.9 KB)

---

<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 26, 2025, 10:29am UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/2 "2025-02-26T10:29:04Z")

</div>

What is the problem?

---

<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:** [February 26, 2025, 3:17pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/3 "2025-02-26T15:17:17Z")

</div>

Well, the code I attached produces the following error, and I have no idea why. My guess is that the options I am trying to pass to the matrices are not registered.

```auto
[0]PETSC ERROR: Object is in wrong state
[0]PETSC ERROR: PCASMSetLocalSubdomains() should be called before calling PCSetUp().
[0]PETSC ERROR: WARNING! There are unused option(s) set! Could be the program crashed before usage or a spelling mistake, etc!
[0]PETSC ERROR: Option left: name:-nw value: navier-stokes-2d-PETSc_prec_MWE.edp source: command line
[0]PETSC ERROR: Option left: name:-v value: 0 source: command line
[0]PETSC ERROR: See https://petsc.org/release/faq/ for trouble shooting.
[0]PETSC ERROR: PETSc Development Git Revision: v3.22.3-387-g1406e4c8e14 Git Date: 2025-02-18 22:08:51 +0000
[0]PETSC ERROR: /home/andrasz/freefem/FreeFem-sources/src/mpi/FreeFem++-mpi with 4 MPI process(es) and PETSC_ARCH arch-FreeFem on cnre-ws-11 by andrasz Wed Feb 26 10:06:41 2025
[0]PETSC ERROR: Configure options: --download-mumps --download-parmetis --download-metis --download-hypre --download-superlu --download-slepc --download-hpddm --download-ptscotch --download-suitesparse --download-scalapack --download-tetgen --with-fortran-bindings=no --with-scalar-type=real --with-debugging=no
[0]PETSC ERROR: #1 PCASMSetLocalSubdomains_ASM() at /home/andrasz/freefem/petsc/src/ksp/pc/impls/asm/asm.c:738
[0]PETSC ERROR: #2 PCASMSetLocalSubdomains() at /home/andrasz/freefem/petsc/src/ksp/pc/impls/asm/asm.c:946
[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 https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[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: Run with -malloc_debug to check if memory corruption is causing the crash.
Abort(59) on node 0 (rank 0 in comm 0): application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0

```

I tried to reverse engineer the solution from examples ([1](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/helmholtz-mg-2d-PETSc-complex.edp) and [2](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/maxwell-mg-3d-PETSc-complex.edp)), but I could not figure it out. I do not understand how options are passed to the KSP object that is present within SNES (or EPS), and this is what I hope someone can help me with.

---

<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 26, 2025, 3:30pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/4 "2025-02-26T15:30:34Z")

</div>

Why are you specifying the `O` parameters in your `set()` calls?

---

<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:** [February 26, 2025, 3:33pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/5 "2025-02-26T15:33:00Z")

</div>

No idea - I saw this in the maxwell mulitrig example. If I do not specify it, I get the following error:

```auto
  0 SNES Function norm 1.183215956620e+01
  0 SNES Function norm 1.183215956620e+01
[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 https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[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: Run with -malloc_debug to check if memory corruption is causing the crash.
Abort(59) on node 0 (rank 0 in comm 0): application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0

```

---

<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 26, 2025, 3:45pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/6 "2025-02-26T15:45:18Z")

</div>

Does your code run _without_ multigrid? Preconditioning should be your last concern.

---

<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:** [February 26, 2025, 3:49pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/7 "2025-02-26T15:49:17Z")

</div>

Yes, if I specify `set(J[0], sparams = " -ksp_type preonly -pc_type lu -pc_factor_mat_solver_type mumps "); `. the code the same way the original one: [FreeFem-sources/examples/hpddm/navier-stokes-2d-PETSc.edp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/tree/develop/examples/hpddm/navier-stokes-2d-PETSc.edp)

---

<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 26, 2025, 3:54pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/8 "2025-02-26T15:54:00Z")

</div>

OK, the next question is then, why do you want to use `PCMG` here?

---

<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:** [February 26, 2025, 4:02pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/9 "2025-02-26T16:02:02Z")

</div>

I hear about using the LU factorization with a lower discretization order can be used as a preconditioner for an iterative solver. Like a geometric multigrid, instead of a h-type (lower mesh resolution with same discretization order) a p-type (same mesh, lower discretization order). I want to try it out, and the PETSc `PCMG` seems the natural way to do it (also, I tried to apply the `PtA^-1P` multiplication as a preconditioner, but it did not work).

---

<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 26, 2025, 4:11pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/10 "2025-02-26T16:11:59Z")

</div>

Usually, you want to refine as you decrease the discretization order (that’s called low-order-refined, LOR). Have you checked that your restriction operator is correct (matrix `P[0]`)?  
You should put the `set` outside of the `func`, but you’ll still get the same result in the end, your preconditioner is ill-posed (and so GMRES doesn’t converged and so SNES does not either).

---

<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:** [February 26, 2025, 5:43pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/11 "2025-02-26T17:43:15Z")

</div>

Thanks for the answer. There were indeed a bug in my code (wrong coarse element space which is ill conditioned).

However, what I found odd is that even if I use the exact same commands as in the [FreeFem-sources/examples/hpddm/helmholtz-mg-2d-PETSc-complex.edp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/helmholtz-mg-2d-PETSc-complex.edp) example, I do not get ant output with `KSP Residual norm`, although the `-ksp_view` forces the KSP object to be displayed. The KSP object seems to have the options I specified, yet as if nothing happens. I attach the output of the code.  
[ns.log](https://community.freefem.org/uploads/short-url/oh48pOHi5elB62fT5fHTk3EP2Uk.log) (8.7 KB)

---

<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 26, 2025, 6:47pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/12 "2025-02-26T18:47:39Z")

</div>

Run with `-ksp_converged_reason`. Before doing a nonlinear solver, I would start by looking at something simpler, like Stokes or Oseen.

---

<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:** [February 26, 2025, 6:54pm UTC](https://community.freefem.org/t/snes-with-multigrid-for-the-jacobian/3774/13 "2025-02-26T18:54:59Z")

</div>

Using `-ksp_converged_reason`, I got the following error:  
Linear solve did not converge due to DIVERGED\_PC\_FAILED iterations 0  
PC failed due to SUBPC\_ERROR

I will try out the preconditioner on a simpler similar system as you suggested.
