# Export PETSc Mat to python

**URL:** https://community.freefem.org/t/export-petsc-mat-to-python/845
**Category:** General Discussion
**Created:** [March 9, 2021, 4:35pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845 "2021-03-09T16:35:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 9, 2021, 4:35pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/1 "2021-03-09T16:35:00Z")

</div>

Hi There,

I am trying to export a PETSc Mat type to python to continue working on it there. I save the Mat type using

```auto
load "PETSc"

Mat A;

... // Filling A

ObjectView(A, format = "binary", name = "A.dat");

```

Afterwards I copy the file to the working directory of my python script where I try to open the binary data using

```auto
from petsc4py import PETSc

viewer = PETSc.Viewer().createBinary('A.dat', 'r')
A = PETSc.Mat().load(viewer)

```

Unfortunately this does not work. However, if I create the binary in Python, using

```auto
viewer = PETSc.Viewer().createBinary('matrix-A.dat', 'w')
viewer(A)

```

and then load it, it does work.

The error I get is this one:

```auto
Traceback (most recent call last):
  File "/home/koen/internship/python_practice/petsc.py", line 4, in <module>
    A = PETSc.Mat().load(viewer)
  File "PETSc/Mat.pyx", line 696, in petsc4py.PETSc.Mat.load
petsc4py.PETSc.Error: error code 66
[0] MatLoad() line 1231 in /home/koen/FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2/src/mat/interface/matrix.c
[0] MatLoad_SeqAIJ() line 4723 in /home/koen/FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2/src/mat/impls/aij/seq/aij.c
[0] MatLoad_SeqAIJ_Binary() line 4786 in /home/koen/FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2/src/mat/impls/aij/seq/aij.c
[0] PetscViewerBinaryRead() line 1002 in /home/koen/FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2/src/sys/classes/viewer/impls/binary/binv.c
[0] PetscBinarySynchronizedRead() line 633 in /home/koen/FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2/src/sys/fileio/sysio.c
[0] PetscBinaryRead() line 311 in /home/koen/FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2/src/sys/fileio/sysio.c
[0] Read from file failed
[0] Read past end of file

```

I guess there is a difference in how petsc4py and ObjectView write the binary file from the Mat, but I have no clue what this difference is. Any help would be much appreciated!

---

<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: [March 9, 2021, 5:51pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/2 "2021-03-09T17:51:27Z")

</div>

How did you install `petsc4py`? Which version are you using?

---

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 9, 2021, 7:49pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/3 "2021-03-09T19:49:29Z")

</div>

I installed petsc4py using `pip install petsc4py petsc` after doing `echo PETSC_DIR=FreeFem-sources/3rdparty/ff-petsc/petsc-3.14.2`. I have petsc 3.14.5 and petsc4py 3.14.1. I did however install FreeFem in `/usr/local/lib` and also have the folder `/usr/local/ff-petsc/r/lib/petsc` in which I found the file PetscBinaryIO.py. Maybe I should somehow configure this to be included in my python packages? I am not sure how to do this though.

Update: This inspired me to do some more research to those packages and here are some of my important conclusions (which made it work eventually):  
First of all my `PETSC_ARCH` was not correct. This should be the directory in your `PETSC_DIR` in which you can find `lib/petsc/conf/petscvariable` (in my case `PETSC_ARCH=fr`).

Next I copied the Petsc python modules of `petsc/bin` to my python site-packages. (in my case the modules were in `/usr/local/ff-petsc/r/lib/petsc/bin`. Then I got the script to work using

```auto
import PetscBinaryIO

io = PetscBinaryIO.PetscBinaryIO()
obj = open('A.dat')
objecttype = io.readObjectType(obj)
if objecttype == 'Mat':
    A = io.readMat(obj)

```

Hope this can help anyone else. Thank you for the swift response, somehow it did point me in the right direction!

---

<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: [March 9, 2021, 8:52pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/4 "2021-03-09T20:52:56Z")

</div>

Wow, that’s a lot of copy/paste’ing. There is probably a cleaner route, but if it’s working alright now, I guess it’s all good. Just FYI, `ObjectView()` writes exactly as `MatView()` reads, there is no shenanigan, you can have a look at the [sources](https://github.com/FreeFem/FreeFem-sources/blob/47099aed0f70ccedc3eb52e423b3d73ae1966902/plugin/mpi/PETSc-code.hpp#L2120).

---

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 10, 2021, 8:45am UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/5 "2021-03-10T08:45:35Z")

</div>

One cleaner solution is to add `$PETSC_HOME/lib/petsc/bin` to your `PYTHONPATH`.

---

<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: [March 10, 2021, 11:07am UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/6 "2021-03-10T11:07:46Z")

</div>

By the way, if you have a PETSc installed _outside_ of FreeFEM, you can have FreeFEM use your custom PETSc, see [this](https://github.com/FreeFem/FreeFem-sources/issues/128#issuecomment-586738836) GitHub issue.

---

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 12, 2021, 4:50pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/7 "2021-03-12T16:50:17Z")

</div>

Unfortunately I have come back to this issue. Using the PetscBinaryIO works, but it does not seem like a good way to continue further since the petsc4py.PETSc.Mat class has much more options and is better integrated with the PETSc interface. Now I come back to this issue. If I generate data using petsc4py the method does work, but if I generate data using ObjectView in FreeFEM the method does not work.

I have already stated how I load and import my data and the error is still the same… I have added the full scripts for the FreeFEM part, and copied the full python scripts below.[vorticity.edp](https://community.freefem.org/uploads/short-url/yyxTwJPF6c58WVK3uNu8OKrHTtm.edp) (2.0 KB)

```auto
import petsc4py, sys
petsc4py.init(sys.argv)

from petsc4py import PETSc

viewer = PETSc.Viewer().createBinary('data/A_0.dat', 'r')
A = PETSc.Mat().load(viewer)

```

---

<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: [March 12, 2021, 6:04pm UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/8 "2021-03-12T18:04:04Z")

</div>

Can’t reproduce your error.

```auto
$ mpirun -n 1 FreeFem++-mpi vorticity.edp -v 0
[..]
step 195 of 200
step 196 of 200
step 197 of 200
step 198 of 200
step 199 of 200
$ cat vorticity.py
#! /usr/bin/python3

import petsc4py, sys
petsc4py.init(sys.argv)

from petsc4py import PETSc

viewer = PETSc.Viewer().createBinary('vorticity_A_test/A_0.dat', 'r')
A = PETSc.Mat().load(viewer)
$ mpirun -n 2 python3 ./vorticity.py && echo $?
0

```

But if you have multiple PETSc installations here and there, it could explain why it is failing in your case.

---

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 16, 2021, 7:50am UTC](https://community.freefem.org/t/export-petsc-mat-to-python/845/9 "2021-03-16T07:50:08Z")

</div>

It seems that this was indeed the problem. I reinstalled petsc4py, this time from the src/binding/petsc4py folder instead of using pip install. Now it seems to work. Thanks for the suggestion!
