# Transfer function in

**URL:** <https://community.freefem.org/t/transfer-function-in/2517>\
**Category:** General Discussion\
**Created:** [June 3, 2023, 7:55pm UTC](https://community.freefem.org/t/transfer-function-in/2517 "2023-06-03T19:55:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 3, 2023, 7:55pm UTC](https://community.freefem.org/t/transfer-function-in/2517/1 "2023-06-03T19:55:12Z")

</div>

Hi,

I am trying to interpolate a scalar field between two different meshes in parallel using `transfer` function. However, it returns an error. I attached the code here:  
[test.edp](https://community.freefem.org/uploads/short-url/5wZGLo8BviBvvZ2BYuuXW26ObcS.edp) (3.5 KB)

The first transfer trial would return an error, `Exec error : exec assert`. Do you know what is going on here? I tracked down to the line where it reports the error, it says:

```auto
IFMACRO(!transferQ)
 1312 & assert( kcoe[].n == VhLocalOldPrivate.ndof);
 1313 & assert( kcoepla[].n == VhLocalNewPrivate.ndof);

```

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [June 3, 2023, 10:34pm UTC](https://community.freefem.org/t/transfer-function-in/2517/2 "2023-06-03T22:34:16Z")

</div>

Can you upload the mesh files?

---

<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:** [June 4, 2023, 4:44am UTC](https://community.freefem.org/t/transfer-function-in/2517/3 "2023-06-04T04:44:14Z")

</div>

Exact same problem and solution as in [PETSc error of ChangeNumbering - #2 by prj](https://community.freefem.org/t/petsc-error-of-changenumbering/2509/2).

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 4, 2023, 8:12pm UTC](https://community.freefem.org/t/transfer-function-in/2517/4 "2023-06-04T20:12:55Z")

</div>

Hi prj,

Thank you for your answer. I have considered that this may be caused by the position of `buildDmesh()`, however if I called `buildDmesh` before defining `fespace` ([test.edp](https://community.freefem.org/uploads/short-url/gXX1TgCiySBeE7zRrlHn10y1QMc.edp) (3.4 KB)), the program got stuck after printing “here”. The code would occupy more than 256G of memory and remain unfinished after 1 hour of execution on a node with 128 cores. The meshes I am dealing with are large, but should not take that amount of time just for interpolation. That’s why I feel that it just stuck somewhere. Do you know the possible reason for this issue?

Besides, when I want to `createMat` on two different meshes in one piece of code for two equations respectively, is it correct to define it like this?

```auto
int[int] myN2O;
macro MeshN2O() myN2O//
Mat NS;
buildDmesh(Mesh);
{
macro def(i)[i, i#B, i#C, i#D]//
macro init(i)[i, i, i, i]// EOM
createMat(Mesh, NS, Pk2Vector)
}

int[int] myN2OS;
macro SolidN2O() myN2OS//
Mat A;
buildDmesh(Solid);
{
macro def(i)[i, i#B, i#C]// EOM
macro init(i)[i, i, i]// EOM
createMat(Solid, A, Pk1Vector)
}

```

---

<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:** [June 4, 2023, 8:23pm UTC](https://community.freefem.org/t/transfer-function-in/2517/5 "2023-06-04T20:23:18Z")

</div>

It’s impossible to debug further without the meshes.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 4, 2023, 8:37pm UTC](https://community.freefem.org/t/transfer-function-in/2517/6 "2023-06-04T20:37:43Z")

</div>

Hi prj,

The size of the mesh file exceeds the limit of the forum. Do you mind if I send you via email?

---

<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:** [June 5, 2023, 4:57am UTC](https://community.freefem.org/t/transfer-function-in/2517/7 "2023-06-05T04:57:12Z")

</div>

Can’t you reproduce the issue on smaller meshes? If not, please use a file transfer system like [https://wetransfer.com](https://wetransfer.com) or similar.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 5, 2023, 8:31am UTC](https://community.freefem.org/t/transfer-function-in/2517/8 "2023-06-05T08:31:11Z")

</div>

Hi prj,

Thank you again for helping me. Here is the link to the files for reproducing the problem I met: [WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free](https://we.tl/t-TX8uIB3DGL). It includes two meshes for interpolation and a data file to be read by code and interpolated between meshes.

---

<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:** [June 5, 2023, 9:19am UTC](https://community.freefem.org/t/transfer-function-in/2517/9 "2023-06-05T09:19:36Z")

</div>

Can you reproduce the issue on a smaller mesh?

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [June 5, 2023, 10:43am UTC](https://community.freefem.org/t/transfer-function-in/2517/10 "2023-06-05T10:43:50Z")

</div>

If you don’t mind me asking, what are you trying to do? Put each platelet  
on a mesh vertex or something? Unless you already have done so, you may  
want to start with a smaller system just to get some idea of the issues  
you will encounter and quickly get initial feedback on the approach or model.

I’m looking eventually at blood coagulation but was going to start  
with some barely related systems like two-part “epoxy” systems  
with part 1 and 2 diffusing in opposing directions creating a wall between  
them in a self-limiting reaction. After playing with it briefly I went  
to look at some related topics.

---

<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:** [June 5, 2023, 12:58pm UTC](https://community.freefem.org/t/transfer-function-in/2517/11 "2023-06-05T12:58:15Z")

</div>

Your code is not consistent. `kcoe` appears to be a local function, yet you read inside `sol_files/kcoe.sol` which appears to be a global function. Please make your code consistent.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 5, 2023, 9:33pm UTC](https://community.freefem.org/t/transfer-function-in/2517/12 "2023-06-05T21:33:14Z")

</div>

Thank you for your help. Could you suggest me a way to make a global function into a local one along with the partition of the mesh? I have tried `mpiScatter`, but it seems not doing exactly what I want. The scattered data does not follow the partition of the mesh.  
[test.edp](https://community.freefem.org/uploads/short-url/dTnZrkycCQSMSIElUbxxZUnJbEW.edp) (3.7 KB)

---

<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:** [June 6, 2023, 5:05am UTC](https://community.freefem.org/t/transfer-function-in/2517/13 "2023-06-06T05:05:24Z")

</div>

You can just do `local[] = global[](array)`, where `array` has been computed using the `restrict` keyword alongside the `n2o` parameter you used for the mesh partitioning.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 6, 2023, 5:46pm UTC](https://community.freefem.org/t/transfer-function-in/2517/14 "2023-06-06T17:46:31Z")

</div>

Thank you @prj. The problem is resolved! However, in my long production code, in which the movement of the mesh is more complicated, it returns an error when I called `transfer` to interpolate the value from the moved platelet mesh to `30.mesh`:

> erreur read mesh 1 negative tet

This error only occurs on some of the mpirank. I am guessing this error is caused by the step in `movemesh` where the elements met some problem due to the movement of mesh.

Could you help me to solve this problem?

---

<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:** [June 6, 2023, 6:11pm UTC](https://community.freefem.org/t/transfer-function-in/2517/15 "2023-06-06T18:11:53Z")

</div>

Do you have a reproducer?

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 8, 2023, 5:20pm UTC](https://community.freefem.org/t/transfer-function-in/2517/16 "2023-06-08T17:20:51Z")

</div>

Yes, I made this test code which could reproduce the error. I attached the files that were used in the code. This code is running with 8 cores (the un was saved via 8 processors). The meshes are the same as before. Thank you!  
[un.zip](https://community.freefem.org/uploads/short-url/59f7FVf9D3iFjZ0kgL0vdtXdOcA.zip) (3.6 MB)  
[vf.sol.zip](https://community.freefem.org/uploads/short-url/aUx6tvi6e9Q42mJt3JRlndYyNZG.zip) (239.5 KB)  
[test.edp](https://community.freefem.org/uploads/short-url/mPEoaHpqIUxUbfhRRGb5HZkkv3H.edp) (2.4 KB)

The context of my problem is like this: I would like to write a coupled process between two meshes. Mesh A will move due to some external force. Along with the movement, the scalar field of mesh A also moved its position. We want to demonstrate such movement on a scalar field on mesh B.

Currently we have thought about two ways to do this:

1. Create a new mesh C based on the displacement `[u1n,u2n,u3n]` of mesh A using `movemesh`. Read value `vf` from the undeformed mesh A using `vf_C=vf_A(x-u1n,y-u2n,z-u3n)`. Then interpolate it to mesh B using `transfer`. But it seems that `movemesh` may result in bad quality meshes (`erreur read mesh 1 negative tet`). The uploaded code is implemented with this approach.
2. The second way we come up with is to update the displacement on the scalar field of mesh B using the displacement from mesh A directly:` vf_B =vf_B(x-u1n,y-u2n,z-u3n)`. But this approach seems introduced relatively large errors.

Could you suggest any alternative on it?

---

<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:** [June 9, 2023, 5:26am UTC](https://community.freefem.org/t/transfer-function-in/2517/17 "2023-06-09T05:26:14Z")

</div>

So, the problem is not really related to `transfer()`, since in your `test.edp`, if you simply add `Solid1 = change(Solid1, fregion = nuTriangle);` after the `movemesh()`, then you get the same error. Any chance you could regularize your displacement field so that the output mesh is not so distorted?

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 25, 2023, 8:04am UTC](https://community.freefem.org/t/transfer-function-in/2517/18 "2023-06-25T08:04:46Z")

</div>

Hi prj,

Thank you so much for your help before. I am now having a new problem with `transfer` function.  
I am trying to use `transfer` function to transfer [P2,P2,P2,P1] values between two different meshes. I attached the code here:  
[test.edp](https://community.freefem.org/uploads/short-url/uF2n9mqm5RlQ2UfQFlgQuk02igA.edp) (2.0 KB)

It will return an error:

```auto
Compile error : Invalid array size for vectorial fespace function
the array size must be 4 not 1

```

Could you please help me take a look?

Besides, when `transfer` [P2,P2,P2,P1] values, is it possible to define the values as lines 86-87 in the test code (not using `def`), and transfer them directly?

Thank you in advance!

---

<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:** [June 25, 2023, 11:57am UTC](https://community.freefem.org/t/transfer-function-in/2517/19 "2023-06-25T11:57:21Z")

</div>

You can only transfer between `fespace` with the same number of components. So you need to define something like `fespace SpaceP2Vector(Mesh, P2Vector)` and go from `SpaceVector` to `SpaceP2Vector`. Then, if you are using a vectorial `fespace`, you need to define the macro `def` as done prior in your script before calling `transfer`.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [June 25, 2023, 7:08pm UTC](https://community.freefem.org/t/transfer-function-in/2517/20 "2023-06-25T19:08:51Z")

</div>

Thank you @prj. I modified the code corresponding to what you suggested. This is the updated version of the code, which works well. [test.edp](https://community.freefem.org/uploads/short-url/glVMtkNJVXX065SHfdmYtkLmOV6.edp) (2.0 KB)  
But I have a concern about it. Since I need to solve two different problems on two different meshes, I have two different `creatMat`:

```auto
Mat NS;
buildDmesh(Mesh);
{
  macro def(i)defFluid(i)// EOM
  macro init(i)initFluid(i)// EOM
  createMat(Mesh, NS, PkVector)
}

Mat A;
buildDmesh(Solid);
{
  macro def(i)defSolid(i)// EOM
  macro init(i)initSolid(i)// EOM 
  createMat(Solid, A, Pk)
}

```

When I put the macro `def`: `macro def(i)[i, i#B, i#C]// EOM` after these two things, the code works well. But if I put before these two, then it still has the error:

```auto
Invalid array size for vectorial fespace function
 the array size must be 4 not 3

```

I am wondering when I call the macro def after these two `creatMat`, will it overwrite the def that is set for two Mat? or it won’t influence the solver afterward?

[Next page](https://community.freefem.org/t/transfer-function-in/2517.md?page=2)
