# How to export a rhs into petsc

**URL:** https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730
**Category:** General Discussion
**Created:** [May 10, 2022, 3:52pm UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730 "2022-05-10T15:52:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DuanXY](https://avatars.discourse-cdn.com/v4/letter/d/5fc32e/32.png) [@DuanXY](https://community.freefem.org/u/DuanXY)
#### Post date: [May 10, 2022, 3:52pm UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730/1 "2022-05-10T15:52:57Z")

</div>

Hi,

In my work, it’s more convenient to export matrix and RHS to PETSc to solve Ax=b. Because I want to develop a special efficient solving method for the problem based on PETSc.  
I want to export a PETSc VECMPI type from FreeFem++, to use “VecLoad” to load a vector(eg. RHS) in the following work.  
I try to export RHS according to the example, but the type is matseqdense or matmpidense.

```auto
real[int] bb;
real[int, int] rhsView(bb.n, 1);
rhsView(:,0) = bb;
ObjectView(rhsView, format = "binary", name = "./data/bb.dat");

```

Thanks for the help!

---

<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: [May 10, 2022, 6:35pm UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730/2 "2022-05-10T18:35:14Z")

</div>

You should be able to “develop a special efficient solving method for the problem based on PETSc” directly in FreeFEM, what is missing?  
What you are doing is correct. In your code, instead of using the loaded `MatSeqDense` or `MatMPIDense`, just use [MatDenseGetColumnVec — PETSc v3.17.1-371-gabf86ae5d7 documentation](https://petsc.org/main/docs/manualpages/Mat/MatDenseGetColumnVec/) to get the corresponding `VecSeq` or `VecMPI`.

---

<div class="post-metadata">

### Author: ![henkel](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@henkel](https://community.freefem.org/u/henkel)
#### Post date: [May 10, 2022, 11:19pm UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730/3 "2022-05-10T23:19:47Z")

</div>

> [@prj](#):
>
> What you are doing is correct. In your code, instead of using the loaded `MatSeqDense` or `MatMPIDense`, just use [MatDenseGetColumnVec — PETSc v3.17.1-371-gabf86ae5d7 documentation](https://petsc.org/main/docs/manualpages/Mat/MatDenseGetColumnVec/) to get the corresponding `VecSeq` or `VecMPI`.

A few weeks ago I had a similar problem as @DuanXY and solved it as you suggested using `MatDenseGetColumnVec`.  
In my case it was about testing the feasibility to solve a certain system with AmgX, which is unfortunately not included in PETSc, but there is a [wrapper](https://github.com/barbagroup/AmgXWrapper) including a solveFromFiles example.  
So maybe it is worth it to consider saving vectors in the standard binary vector storage format, because at least two people have tripped over this?

---

<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: [May 11, 2022, 5:16am UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730/4 "2022-05-11T05:16:26Z")

</div>

1. AmgX is being included in PETSc, see [start PC interface to AMGx (!4323) · Merge requests · PETSc / petsc · GitLab](https://gitlab.com/petsc/petsc/-/merge_requests/4323)
2. I forgot that you could also use a dummy system, e.g., with the identity matrix, call `KSPSolve()`, and use `-ksp_view_rhs binary`. This will get the job done as well.

The tricky part about saving a vector is that users have to be careful whether they save it in FreeFEM or PETSc numbering, but that could be doable (feel free to make a PR over at GitHub).

---

<div class="post-metadata">

### Author: ![henkel](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@henkel](https://community.freefem.org/u/henkel)
#### Post date: [May 12, 2022, 12:53am UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730/5 "2022-05-12T00:53:15Z")

</div>

> [@prj](#):
>
> - AmgX is being included in PETSc, see [start PC interface to AMGx (!4323) · Merge requests · PETSc / petsc · GitLab](https://gitlab.com/petsc/petsc/-/merge_requests/4323)
> - I forgot that you could also use a dummy system, e.g., with the identity matrix, call `KSPSolve()`, and use `-ksp_view_rhs binary`. This will get the job done as well.

Awesome. I was unaware of that merge request. Thanks for the hints.

> [@prj](#):
>
> The tricky part about saving a vector is that users have to be careful whether they save it in FreeFEM or PETSc numbering, but that could be doable (feel free to make a PR over at GitHub).

I am not sure I can do this myself, as I am a novice when it comes to PETSc and the freefem plugin 😅  
I guess that I would have to add  
`Global.Add("ObjectView", "(", new PETSc::view< KN<PetscScalar>, 0 >); Global.Add("MatLoad", "(", new PETSc::view< KN<PetscScalar>, 1 >);` and a third implementation of `view_dispatched`?

Doesn’t the tricky part you mentioned also apply to the existing implementation? If I save a `real[int] rhs` using a `real[int, int] rhsView(rhs.n, 1)`, I also have to pay attention to the numbering and use `ChangeNumbering` if necessary, or am I missing something?

---

<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: [May 12, 2022, 2:49pm UTC](https://community.freefem.org/t/how-to-export-a-rhs-into-petsc/1730/6 "2022-05-12T14:49:45Z")

</div>

> Doesn’t the tricky part you mentioned also apply to the existing implementation?

That is correct, but most PETSc routine that uses a `PetscScalar[int, int]` as input expects this input to be in PETSc numbering. On the contrary, if you use `^-1 * in` or `KSPSolve(, in, )`, then `in` must either be in FreeFEM or PETSc numbering.
