# Problem regarding assembling matrix using dolfinx\_mpc

**URL:** <https://fenicsproject.discourse.group/t/problem-regarding-assembling-matrix-using-dolfinx-mpc/12501>\
**Category:** dolfinx\
**Created:** [October 19, 2023, 8:11am UTC](https://fenicsproject.discourse.group/t/problem-regarding-assembling-matrix-using-dolfinx-mpc/12501 "2023-10-19T08:11:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [October 19, 2023, 8:11am UTC](https://fenicsproject.discourse.group/t/problem-regarding-assembling-matrix-using-dolfinx-mpc/12501/1 "2023-10-19T08:11:37Z")

</div>

Hello, I want to use dolfinx\_mpc to impose periodic boundary condition to solve the Poisson equation, but I got some PETSC ERROR

I define my linear and bilinear forms as follows:

 ![Screenshot from 2023-10-19 09-55-22](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/b/b7497ebba7067b2626038d667b17068ae272c420.png)

create the mesh and function space

 ![Screenshot from 2023-10-19 09-58-03](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/f/f567a512bc15241fd6ddfb6f475aa91f16f573cc.png)

The periodic condition and relation are defined here

 ![Screenshot from 2023-10-19 09-58-47](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/a/a0e24933732da2a9baaa1122719d3172576c6ca1.png)

Then I create the MPC as follows(maybe it is not correct

 ![Screenshot from 2023-10-19 09-59-11](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/7a568bb962a68effff2cf416c457bce10a243f55.png)

and when I tried to assemble the matrix:

 ![Screenshot from 2023-10-19 10-00-10](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/c/c846d9b7f7e0b31bcd7fdf61677bb8dcb86a3118.png)

I get error, the error message is

 ![Screenshot from 2023-10-19 10-04-59](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/2/2e099ef6cd10d6d20e09cea075150e303716f8a9.png)  
and  
 ![Screenshot from 2023-10-19 10-05-47](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/78e41818a8e411a41fe90e3aa18b6fd5824c0d91.png)

It seems like the way I assemble the matrix causes the error, the problems may be within here:

la::petsc::Matrix::set\_block\_fn(A.mat(), ADD\_VALUES)  
la::petsc::Matrix::set\_fn(A.mat(), ADD\_VALUES)

is there anyone know how to solve this issue? Thanks very much!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 19, 2023, 9:06am UTC](https://fenicsproject.discourse.group/t/problem-regarding-assembling-matrix-using-dolfinx-mpc/12501/2 "2023-10-19T09:06:52Z")

</div>

With screenshots it is really hard to reproduce your error. Please use the DOLFINx\_mpc issue tracker on github and attach a minimal reproducible example.

---

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [October 19, 2023, 12:12pm UTC](https://fenicsproject.discourse.group/t/problem-regarding-assembling-matrix-using-dolfinx-mpc/12501/3 "2023-10-19T12:12:37Z")

</div>

Hi, thanks for reminding! I will do that in the future.

I have figured out the problem, it is because the way I created the matrix is not correct, when MPC is applied, we should creat the matrix using sparsity\_pattern, instead of using only the form.
