# Help with preconditionner

**URL:** <https://community.freefem.org/t/help-with-preconditionner/3462>\
**Category:** General Discussion\
**Created:** [September 2, 2024, 6:17pm UTC](https://community.freefem.org/t/help-with-preconditionner/3462 "2024-09-02T18:17:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgard](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@Edgard](https://community.freefem.org/u/Edgard)\
**Post date:** [September 2, 2024, 6:17pm UTC](https://community.freefem.org/t/help-with-preconditionner/3462/1 "2024-09-02T18:17:10Z")

</div>

could someone help to use a preconditionner LU and solver GMRES on this three problems:

problem Larva(L,v) = int2d(Th2D)(L_v/dt)  
- int2d(Th2D)(Lant_v/dt)  
+ int2d(Th2D)(DifL_Grad(L)'Grad(v))  
+ int2d(Th2D)((velxdx(L) + vely_dy(L))_v)  
//metodo de difusao artificial classica  
+ int2d(Th2D)(1e-3_normVinf_hTriangle_Grad(L)'_Grad(v))  
//lado direito  
- int2d(Th2D)( (r1_Mant\*(1.0 - Lant/Klar))_v - (b1+lambdaL)Lantv )  
+ on(inflow, L = 0.08_1e6);

problem Mexilhao(M,w) = int2d(Th2D)(M_w/dt)  
- int2d(Th2D)(Mant_w/dt)  
+ int2d(Th2D)(DifM_Grad(M)'Grad(w))  
//lado direito  
- int2d(Th2D)(lambdaMLant_(Aant^2/(c1^2 + Aant^2))_(1.0 - Mant/Kmex)w - b2Mant_w)  
+ on(inflow, M = 0.0) + on(outflow, M = 0.0) + on(intRig, M = 0.0) + on(intLef, M = 0.0) + on(intFluid, M = 0.0);

problem Alga(A,v) = int2d(Th2D)(A_v/dt)  
- int2d(Th2D)(Aant_v/dt)  
+ int2d(Th2D)(DifA_Grad(A)'Grad(v))  
+ int2d(Th2D)((velxdx(A) + vely_dy(A))_v)  
//metodo de difusao artificial classica  
+ int2d(Th2D)(1e-3_normVinf_hTriangle_Grad(A)'_Grad(v))  
//lado direito  
- int2d(Th2D)( (r2_Aant\*(1.0 - Aant/Kalg))_v - b3_(Aant^2/(c2^2 + Aant^2))_Mant_v )  
+ on(inflow, A = 0.006\*1e6);

When i try to use the PETSc command:

```
   set(L, sparams="-ksp_type gmres -pc_type lu -ksp_rtol 1e-5");
   set(M, sparams="-ksp_type gmres -pc_type lu -ksp_rtol 1e-5");
   set(A, sparams="-ksp_type gmres -pc_type lu -ksp_rtol 1e-5");

```

this happens:

List of choices  
( \<12SetMatrix\_OpIdE\> : \<14Matrice\_CreuseIdE\> )  
( \<12SetMatrix\_OpISt7complexIdEE\> : \<14Matrice\_CreuseISt7complexIdEE\> )

Error line number 328, in file CodigoMexilhaoIC2.edp, before token )

---

<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:** [September 2, 2024, 6:52pm UTC](https://community.freefem.org/t/help-with-preconditionner/3462/2 "2024-09-02T18:52:44Z")

</div>

We cannot copy/paste your code to run it, and there is no definition of variable `L`, `M`, and `A`, so we can’t help you. Most likely, you a trying to set PETSc options on plain `matrix` instead of `Mat`, so you first need to call something like `Mat LPETSc(L)` and then call `set(LPETSc, ...);`.

---

<div class="post-metadata">

**Author:** ![Edgard](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@Edgard](https://community.freefem.org/u/Edgard)\
**Post date:** [September 2, 2024, 7:46pm UTC](https://community.freefem.org/t/help-with-preconditionner/3462/3 "2024-09-02T19:46:15Z")

</div>

So can not i use the PETSc options in problems and only in Matrix? L, M and A are variables from a fespace Vh(i.e “fespace Vh(Th2D, P2);”).

I tried to use this:

Mat mLarva(Vh.ndof), mMexilhao(Vh.ndof), mAlga(Vh.ndof);  
real [int] bLarva(Vh.ndof),bMexilhao(Vh.ndof),bAlga(Vh.ndof);

mLarva = Larva(Vh, Vh);  
mMexilhao = Mexilhao(Vh, Vh);  
mAlga = Alga(Vh, Vh);

bLarva = Larva(0, Vh);  
bMexilhao = Mexilhao(0, Vh);  
bAlga = Alga(0, Vh);

set(mLarva, sparams=“-ksp\_type gmres -pc\_type lu -ksp\_rtol 1e-5”);  
set(mMexilhao, sparams=“-ksp\_type gmres -pc\_type lu -ksp\_rtol 1e-5”);  
set(mAlga, sparams=“-ksp\_type gmres -pc\_type lu -ksp\_rtol 1e-5”);

and in a temporal Loop:

Loop of 1 year:  
Mant = M;  
Aant = A;  
Lant = L;

```
   L[] = (mLarva^-1)*(bLarva);
   M[] = (mMexilhao^-1)*(bMemMexilhao);
   A[] = (mAlga^-1)*(bAlga);

```

end;

But, unfortunally it not worked, even though the code could compile and run.

---

<div class="post-metadata">

**Author:** ![Edgard](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@Edgard](https://community.freefem.org/u/Edgard)\
**Post date:** [September 2, 2024, 7:59pm UTC](https://community.freefem.org/t/help-with-preconditionner/3462/4 "2024-09-02T19:59:21Z")

</div>

I have this pseudo-algorith, where i could put this changes to add the PETSc options?

Data: Initial and boundary conditions of the variables L, M , and A in the  
domain.  
Result: Approximate values of L, M , and A in the discretized domain.  
1 Definition of the domain geometry.  
2 Generation of the triangular mesh.  
3 Calculation of the velocity field V using the Navier-Stokes equations.  
4 Definition of the model’s physical parameters.  
5 Initialization of the variables L, M , and A based on the initial conditions.  
6 Application of the boundary conditions on the variables L, M , and A.  
7 Solution of the Larva-Mussel-Algae system:  
8 for ti - tf do  
9 for each nonlinear iteration k do  
10 Given Li,k, M i,k, and Ai,k, find Li,k+1, M i,k+1, and Ai,k+1 considering  
the variational formulation of Eqs.Larva, Mussel, and Algae.  
11 end  
12 Update Li+1, M i+1, and Ai+1  
13 end  
14 Save the values of L, M , and A for visualization in Paraview.

---

<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:** [September 3, 2024, 5:08am UTC](https://community.freefem.org/t/help-with-preconditionner/3462/5 "2024-09-03T05:08:12Z")

</div>

You need to convert your `matrix` to `Mat` as I said initially.
