# Error encountered when constructing the stiffness matrix with PETSc

**URL:** <https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509>\
**Category:** General Discussion\
**Created:** [September 23, 2024, 6:33am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509 "2024-09-23T06:33:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 23, 2024, 6:33am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/1 "2024-09-23T06:33:45Z")

</div>

Why do I encounter an error at `A = laplace(W1, W1);` when executing the following PETSc parallel code, but the problem disappears when I remove `rhoe`? Additionally, I experience the same issue when I set `rhoe` to 1. I’m not sure why this is happening.

include “set\_material.edp”

```
        varf laplace(phi,phiv, eps=1.0e-8)=
            int3d(Th)( Grad(phi)'*Grad(phiv)/rhoe )
            +on(labHead,phi=voltage/2)
            +on(labTail,phi=-voltage/2)
            +on(labBx0,phi=0)
            +on(labBx1,phi=0)
            +on(labBz0,phi=0)
            +on(labBz1,phi=0)
            +on(labBy0,phi=0)
            +on(labBy1,phi=0);

        real time01 = clock();
        Mat A ;
        // MatCreate(Th,A,P13d);
        buildMat(Th, getARGV("-split", 1), A, P13d, mpiCommWorld)  
        real time02 = clock();
        A= laplace(W1, W1); 
        real[int] b = laplace(0, W1); 

        real time03 = clock();
        set(A, sparams = "-pc_type gamg -ksp_type gmres");
        real time04 = clock();
        phi[] = A^-1 * b;

```

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [September 23, 2024, 6:57am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/2 "2024-09-23T06:57:24Z")

</div>

Isn’t it simply that `rhoe=0`? Try `rhoe=1;` befor you construc the matrix.

---

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 23, 2024, 7:42am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/3 "2024-09-23T07:42:04Z")

</div>

I have tried setting rhoe = 1, but I still get the same error.

---

<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 23, 2024, 8:02pm UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/4 "2024-09-23T20:02:44Z")

</div>

What is the error? Please share an actual usable code.

---

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 24, 2024, 1:59am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/5 "2024-09-24T01:59:39Z")

</div>

[test16.edp](https://community.freefem.org/uploads/short-url/cDDKIVbKk6F85N5lzffLHPtdTA8.edp) (2.3 KB)  
When I use a single core, it runs normally, but when I use multiple cores, it errors out.

---

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 24, 2024, 2:04am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/6 "2024-09-24T02:04:08Z")

</div>

I have already figured out where this issue is coming from, thank you for your answer. I apologize, but I have another question: how can I create a mesh that cuts a smaller cylinder out of a larger cylinder, like in this picture? I am looking forward to your reply.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/8/8fe1a507451f9eda7a877474ce4edb9bb64e0fde.png)

---

<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 24, 2024, 5:45am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/7 "2024-09-24T05:45:04Z")

</div>

Use Gmsh, there are many ressources available online.

---

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 24, 2024, 10:09am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/8 "2024-09-24T10:09:34Z")

</div>

Thank you very much for your answers, and I will also take a close look at the Gmsh software. In the question two above, when I changed the code from `mesh3 Th4 = cube(N/2, N/3, N, [x, 2+y, z], label = LL4, region=4);` to `mesh3 Th4 = cube(N, N/3, N, [x, 2+y, z], label = LL4, region=4);`, it became possible to run normally. Could you explain why this is the case?

---

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 24, 2024, 11:50am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/9 "2024-09-24T11:50:01Z")

</div>

If I have a slightly more complex mesh, like this, is there any convenient way to solve this problem? Looking forward to your reply.  
[test16.edp](https://community.freefem.org/uploads/short-url/5467wjaf1yXCMGomgFKcccCXVjv.edp) (2.5 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:** [September 26, 2024, 6:08pm UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/10 "2024-09-26T18:08:21Z")

</div>

What is the problem?

---

<div class="post-metadata">

**Author:** ![yongheng](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yongheng](https://community.freefem.org/u/yongheng)\
**Post date:** [September 26, 2024, 11:54pm UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/11 "2024-09-26T23:54:35Z")

</div>

When I run this code, the following error occurs:  
Exec error : Div by 0  
– number :1  
Exec error : Div by 0  
– number :1  
err code 8 , mpirank 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:** [September 27, 2024, 5:00am UTC](https://community.freefem.org/t/error-encountered-when-constructing-the-stiffness-matrix-with-petsc/3509/12 "2024-09-27T05:00:46Z")

</div>

This is only part of the error message. The line number should be print just before, so that you can look back at your file and fix the division by 0.
