# PETSc interpolate a function from global to local mesh

**URL:** https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659
**Category:** General Discussion
**Created:** [August 16, 2023, 10:10am UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659 "2023-08-16T10:10:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![marcodeba](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@marcodeba](https://community.freefem.org/u/marcodeba)
#### Post date: [August 16, 2023, 10:10am UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/1 "2023-08-16T10:10:24Z")

</div>

Dear FreeFem users and developers,

I know which is possible to “recollect” te results obtained in parallel on the tocal mesh to the global (starting) one using the mpiAllReduce(u,uS,mpiCommWorld, mpiSUM) command. Is there a function whch is doing the opposite, so dictributing a global variable on the local mesh?

I need this since I am loading from a file, node by node, a variable of my problem and so it is strictly defined on the global mesh. By the way to perform the computations I need to move it in the local one.

Thank you in advance for your help.

Sincerely

Marco

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [August 16, 2023, 10:21am UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/2 "2023-08-16T10:21:48Z")

</div>

Like this? See [this example](https://joliv.et/FreeFem-tutorial/section_3/example8.edp.html).

```auto
int[int] n2o, rest;
rest = restrict(MhLocal, MhGlobal, n2o); // local/global fespace restriction
uLocal[] = uGlobal[](rest); //restrict global solution to each local mesh

```

---

<div class="post-metadata">

### Author: ![marcodeba](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@marcodeba](https://community.freefem.org/u/marcodeba)
#### Post date: [August 16, 2023, 10:50am UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/3 "2023-08-16T10:50:30Z")

</div>

I’m not sure that this is exactly what I need. Just to be clearer what I am doing is something like that:

- I am meshing my domain starting from a point cloud, knowing the load on that point cloud.

```auto
mesh Th = triangulate(fname);

```

- I get the load from the point cloud file

```auto
fespace Vh(Th,P1);

Vh EE;

{
    ifstream file(fname);
    real xx,yy;
    for(int i = 0; i < EE.n; i++)
        file >> xx >> yy >> EE[][i];
}

```

- Now what I would like to do is to produce a local mesh using PETSc (buildDmesh(Th)) and then having a VhPar EEpar which is the exact correspondence of the previously defined EE on the obtained local mesh

I hope now it is clearer!

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [August 16, 2023, 11:10am UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/4 "2023-08-16T11:10:47Z")

</div>

I don’t think that any of that should cause a problem… Try:

```auto
mesh ThG = triangulate(fname);
fespace VhG(ThG,P1);
VhG EE;
{
    ifstream file(fname);
    real xx,yy;
    for(int i = 0; i < EE.n; i++)
        file >> xx >> yy >> EE[][i];
}
mesh ThPar = ThG;
int[int] n2o, rest;
macro ThN2O()n2o// EOM
buildDmesh(ThPar);
fespace VhPar(ThPar,P1);
VhPar EEpar;
rest = restrict(VhPar, VhG, n2o); // local/global fespace restriction
EEpar[] = EE[](rest); //restrict global solution to each local mesh

```

---

<div class="post-metadata">

### Author: ![marcodeba](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@marcodeba](https://community.freefem.org/u/marcodeba)
#### Post date: [August 16, 2023, 12:22pm UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/5 "2023-08-16T12:22:20Z")

</div>

Unfortunately this option provides me a PETSc error:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/2/29c1005024b09e019decdc628f9772cb80313f59.png)

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [August 16, 2023, 12:36pm UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/6 "2023-08-16T12:36:00Z")

</div>

Please attach a reproducer script (without any fileread dependencies) if you’d like help debugging.

---

<div class="post-metadata">

### Author: ![marcodeba](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@marcodeba](https://community.freefem.org/u/marcodeba)
#### Post date: [August 16, 2023, 12:47pm UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/7 "2023-08-16T12:47:34Z")

</div>

Even with the extremely simple condition reported below the procedure leads to the PETSc error:

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

string fname = "Tungsten_5deg_FF.txt";

//mesh Th = triangulate(fname);
mesh Th = square(10,10);

plot(Th);

fespace Vh(Th,P1);

Vh EE;

EE = x^2;
/*
{
    ifstream file(fname);
    real xx,yy;
    for(int i = 0; i < EE.n; i++)
        file >> xx >> yy >> EE[][i];
}
*/
mesh ThPar = Th;

int[int] n2o, rest;

macro ThN2O()n2o //EOM

buildDmesh(ThPar);

fespace VhPar(ThPar,P1);

VhPar EEpar;

rest = restrict(VhPar,Vh,n2o);

EEpar[] = EE[](rest);

```

The commented part is the original one which has been substituted by a simpler mesh and EE.

Thank you very much for your help!

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [August 16, 2023, 1:17pm UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/8 "2023-08-16T13:17:35Z")

</div>

Ah yes, my mistake. Just change the name of the macro `ThN2o` to `ThParN2o`.

---

<div class="post-metadata">

### Author: ![marcodeba](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@marcodeba](https://community.freefem.org/u/marcodeba)
#### Post date: [August 16, 2023, 1:30pm UTC](https://community.freefem.org/t/petsc-interpolate-a-function-from-global-to-local-mesh/2659/9 "2023-08-16T13:30:16Z")

</div>

Oh that’s true! Thank you very much @cmd !

Now I’m ok!
