# Is it possible to modify the elements in the PETSc matrix in parallel computation

**URL:** https://community.freefem.org/t/is-it-possible-to-modify-the-elements-in-the-petsc-matrix-in-parallel-computation/4186
**Category:** General Discussion
**Created:** [January 13, 2026, 1:30am UTC](https://community.freefem.org/t/is-it-possible-to-modify-the-elements-in-the-petsc-matrix-in-parallel-computation/4186 "2026-01-13T01:30:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MatrixVector](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@MatrixVector](https://community.freefem.org/u/MatrixVector)
#### Post date: [January 13, 2026, 1:30am UTC](https://community.freefem.org/t/is-it-possible-to-modify-the-elements-in-the-petsc-matrix-in-parallel-computation/4186/1 "2026-01-13T01:30:04Z")

</div>

When using DDM and PETSc to solving a large problem with multiple parallel processors, the matrix “A“ is usually transferred from FreeFem++ to PETSc, like

```cpp
Apetsc = A;
set(Apetsc, sparams = “-pc_type hypre -ksp_type bcgs”);

```

Is it possible to modify the elements in the PETSc matrix `Apetsc`? For example, we want to a behavior like the `setBC` operator.

Although we know this can be implemented by first modify the FreeFem++ matrix and then transfer the modified matrix to a PETSc matrix. Is it possible change the order? i.e., first directly transfer to a PETSc matrix from the variational form `varf`, then just modify the PETSc matrix.

---

<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: [January 13, 2026, 5:27am UTC](https://community.freefem.org/t/is-it-possible-to-modify-the-elements-in-the-petsc-matrix-in-parallel-computation/4186/2 "2026-01-13T05:27:22Z")

</div>

It is not possible to this date. Could you maybe share a little bit more context to see if another approach would be worthwhile? For example, there is an interface to `MatZeroRows()` if you want an equivalent of `setBC()` for a PETSc `Mat`.

---

<div class="post-metadata">

### Author: ![MatrixVector](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@MatrixVector](https://community.freefem.org/u/MatrixVector)
#### Post date: [January 13, 2026, 5:50am UTC](https://community.freefem.org/t/is-it-possible-to-modify-the-elements-in-the-petsc-matrix-in-parallel-computation/4186/3 "2026-01-13T05:50:38Z")

</div>

Thanks for your reply. My work is about enforcing the Dirichlet boundary condition, especially the point constraint like that implemented in `setBC()`.

From the name of `MatZeroRows()`, it seems it can be used to zero some specific rows of a matrix. Is this function only usful in PETSc, or it can be used in FreeFem++ with some interface?

If it is the latter, is there any minimal working tutorial? For example, I have a matrix like follows:

```auto
[1, 2, 3
 4, 5, 6
 7, 8, 9]

```

Is it possible let all the elements in 2nd row and 2nd column (except the diagonal one element ) to be zero using the `MatZeroRows()` in 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: [January 13, 2026, 5:56am UTC](https://community.freefem.org/t/is-it-possible-to-modify-the-elements-in-the-petsc-matrix-in-parallel-computation/4186/4 "2026-01-13T05:56:21Z")

</div>

There are some examples using `MatZeroRows()` in the sources.
