# Copy variables between different meshes with PETSc

**URL:** https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390
**Category:** General Discussion
**Created:** [May 5, 2020, 9:03am UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390 "2020-05-05T09:03:42Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![tak](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tak](https://community.freefem.org/u/tak)
#### Post date: [May 5, 2020, 9:03am UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/1 "2020-05-05T09:03:42Z")

</div>

Dear Sir or Madam,

I would like to use different 2 meshes with PETSc.  
Additionally, variables want to be copied from each other.  
However, as shown following codes, the direct copy, such as “v=u”, does not work.

Would you please let me know how to do it?

Thank you very much in advance.  
Best regards,  
Tak

// $ ff-mpirun -np 4 script.edp -wg -ne  
load “PETSc”  
macro dimension()2// EOM  
include “macro\_ddm.idp”

meshN Sh1=square(30,30,[x,y]);  
meshN Sh2=square(50,50,[x,y]);

buildDmesh(Sh1)  
buildDmesh(Sh2)

fespace Ph1(Sh1, P2);  
Ph1 u=sin(4_x)+cos(5_y);

fespace Ph2(Sh2, P2);  
Ph2 v;  
v=u;

macro defPlot(u)u//  
plotMPI(Sh2, v, P2, defPlot, real, cmm = “v”)

---

<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 5, 2020, 3:34pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/2 "2020-05-05T15:34:03Z")

</div>

Dear Tak,  
You can use the newly implemented routine `transfer` to help you go between one distributed solution to another. If you want the `transfer` PETSc `Mat`, you can use the `transferMat` function, see this new example [PtAP-2d-PETSc.edp](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/PtAP-2d-PETSc.edp).  
Here is the function used in your code, and the resulting screenshots.

```auto
load "PETSc"
macro dimension()2// EOM
include "macro_ddm.idp"

meshN Sh1=square(30,30,[x,y]);
meshN Sh2=square(50,50,[x,y]);

buildDmesh(Sh1)
buildDmesh(Sh2)

fespace Ph1(Sh1, P2);
Ph1 u=sin(4*x)+cos(5*y);

fespace Ph2(Sh2, P2);
Ph2 v;
// v=u;

macro defPlot(u)u//
transfer(Sh1, P2, u, Sh2, P2, v);
plotMPI(Sh1, u, P2, defPlot, real, cmm = "u")
plotMPI(Sh2, v, P2, defPlot, real, cmm = "v")

```

 ![Screenshot 2020-05-05 at 5.32.58 PM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/bb5d0a6c8427473dc5c962d344aac906c3a7da49.png) ![Screenshot 2020-05-05 at 5.33.07 PM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/261797680ef643ef734ce318cd94aa6e962f0b34.png)

---

<div class="post-metadata">

### Author: ![tak](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tak](https://community.freefem.org/u/tak)
#### Post date: [May 6, 2020, 12:01am UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/3 "2020-05-06T00:01:01Z")

</div>

Dear Professor prj,  
Thank you very much for your quick response and your great assistance. Your answer have been of great help to me in my work.

Best,  
Tak

---

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [April 19, 2021, 5:30pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/4 "2021-04-19T17:30:51Z")

</div>

Dear Professor Prj,

I’m new to freefem. I’m trying to use the function transfer but I always have some errors. I ran this code that you’ve posted in your reply to Tak and it didn’t work. Is it necessary to have any archive in the same folder of the original code?

Thanks in advance.  
Best regards,  
Jorge Morvan

---

<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: [April 19, 2021, 6:24pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/5 "2021-04-19T18:24:03Z")

</div>

What is the error, and which FreeFEM version are you using?

---

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [April 20, 2021, 2:55pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/6 "2021-04-20T14:55:07Z")

</div>

Dear professor Prj,

I have a compile error that seems to be associated with “macro\_ddm.idp”. This is the message I get and also the line in which the error occurs:

820 @ real timerPartition = mpiWtime The Identifier mpiWtime does not exist

Error line number 820, in file macro: buildOverlapEdgePeriodicRecursive in C:\Program Files (x86)\FreeFem++\idp\macro\_ddm.idp, before token mpiWtime

I’m using the version 4.7-1.

Thank you!  
Jorge Morvan

---

<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: [April 20, 2021, 2:57pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/7 "2021-04-20T14:57:03Z")

</div>

You are using the `FreeFem++` binary, you need to use `FreeFem++-mpi`.

---

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [April 20, 2021, 4:16pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/8 "2021-04-20T16:16:38Z")

</div>

Ok, I see!

Thank you!  
Jorge Morvan

---

<div class="post-metadata">

### Author: ![etj](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@etj](https://community.freefem.org/u/etj)
#### Post date: [January 10, 2022, 10:40am UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/9 "2022-01-10T10:40:58Z")

</div>

Hello,  
Does `transfer(Sh1, P2, u, Sh2, P2, v);` also work with multi-component FE spaces such as [P1,P1] or [P1,P2]?  
Thank you.

---

<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 10, 2022, 10:50am UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/10 "2022-01-10T10:50:44Z")

</div>

That’s precisely what is done in the FreeFEM [example](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/transfer.edp)…

---

<div class="post-metadata">

### Author: ![etj](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@etj](https://community.freefem.org/u/etj)
#### Post date: [January 10, 2022, 1:08pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/11 "2022-01-10T13:08:41Z")

</div>

Indeed, thank you! I missed that example.

Is there any example where the solution to be interpolated is not defined analytically but read from a series of files (previously saved with something like `ofstream sol(filename + mpirank + "_" + mpisize + ".sol"); sol << u[];`)?

---

<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 10, 2022, 1:40pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/12 "2022-01-10T13:40:29Z")

</div>

That does not change a thing, as long as your solution follows the distribution of the domain decomposition.

---

<div class="post-metadata">

### Author: ![etj](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@etj](https://community.freefem.org/u/etj)
#### Post date: [January 10, 2022, 3:33pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/13 "2022-01-10T15:33:02Z")

</div>

Ok, thank you. So I guess I’m doing something wrong: the code below

```auto
verbosity = 0;
load "msh3"
load "PETSc"
load "gmsh"

int[int] n2oSaved;
int[int] n2oLoaded;

macro dimension()3// EOM            
include "macro_ddm.idp"            
macro def(i)[i, i#B, i#C, i#D]//    
macro init(i)[i, i, i, i]// EOM     

func Pk = [P1b,P1b,P1b, P1];

mesh3 Th0 = readmesh3("mesh0.mesh");
mesh3 Th = readmesh3("mesh.mesh");

Mat A;
macro ThN2O()n2oSaved//
createMat(Th, A, Pk);

Mat A0;
createMat(Th0, A0, Pk);

fespace Wh( Th, Pk); 
fespace Wh0(Th0, Pk); 

Wh<real> def(u);
Wh0<real> def(u0);

ifstream sol("sol0_" + mpirank + "_" + mpisize + ".sol");
sol >> u0[];
transfer(Th0, [P1b,P1b,P1b,P1], u0, Th, [P1b,P1b,P1b,P1], u);

```

returns the following message:  
“Out of bound 0 \<=0 \< 0 array type = P2KNIS\_IlEE  
current line = 424 mpirank 0 / 8  
Exec error : Out of bound in operator []  
– number :1  
Exec error : Out of bound in operator []  
– 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: [January 10, 2022, 5:16pm UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/14 "2022-01-10T17:16:46Z")

</div>

Right, I’ve replaced both `readmesh` by two `cube`s, and commented out the `ifstream` + `sol >> u0[]` lines (because I don’t have any of the files), and it’s working flawlessly, so the problem is in how you save or load the `.sol`, I guess.

---

<div class="post-metadata">

### Author: ![etj](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@etj](https://community.freefem.org/u/etj)
#### Post date: [January 11, 2022, 8:52am UTC](https://community.freefem.org/t/copy-variables-between-different-meshes-with-petsc/390/15 "2022-01-11T08:52:03Z")

</div>

Thank you. I tried to replace the two `readmesh` by

```auto
mesh3 Th0 = cube(20,20,20,[x,y,z]);
mesh3 Th = cube(30,30,30,[x,y,z]);

```

and commented out the two lines `ifstream` + `sol >> u0[]`, but I still get the same message.
