# Transfer scalar field to deformed mesh with movemesh2d

**URL:** https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010
**Category:** General Discussion
**Created:** [July 22, 2025, 6:11pm UTC](https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010 "2025-07-22T18:11:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![angelaros](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@angelaros](https://community.freefem.org/u/angelaros)
#### Post date: [July 22, 2025, 6:11pm UTC](https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010/1 "2025-07-22T18:11:40Z")

</div>

Hi, I am new to FreeFem++. I googled for the answer of this problem but without success. I need some help, please.  
I have an scalar field Temperature defined on a 2D mesh Th2.  
I deform this mesh with movemesh23 and get a new mesh Th3.  
When I plot Temperature on Th3 it does not always work.  
When Th2 is cartesian coordinates, it does work.  
But when Th2 is curvilinear coordinates (say angles) it does not work.  
How can I transfer an scalar field defined in Th2 so that I can plot it on Th3?  
Thank you!

Example:  
real r=1;  
real R=2;  
real x0 = pi/2;  
real x1 = 3_pi/2;  
real y0 = pi;  
real y1 = 2_pi;  
int n = 5;  
real m = 5;  
mesh Th2 = square(n, m, [x0+(x1-x0)_x, y0+(y1-y0)y]);  
fespace Ch(Th2,P2);  
Ch Temp;  
//calculations of Temp follow  
plot(Th2, Temp, nbiso=30, fill=true, value=true, cmm=“A”);//plot 2D ok  
func f1 = rsin(x);  
func f2 = (R-r_cos(x))_cos(y);  
func f3 = (R-r_cos(x))\*sin(y);  
meshS Th3 = movemesh23(Th2, transfo=[f1,f2,f3]);  
fespace Ch3(Th3,P2);  
Ch3 temp3d=Temp;  
plot(Th3, temp3d, nbiso=30, fill=true, value=true, cmm=“A”);//plot not ok

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [July 23, 2025, 12:08pm UTC](https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010/2 "2025-07-23T12:08:49Z")

</div>

Hello,  
If you write `temp3d=Temp`, the function `Temp` defined on a 2d mesh (which is interpreted with z=0 as a subset of R^3) is extended by 0 outside of the 2d mesh, then interpolated on Th3. The result is that `temp3d=0`.  
A more correct way is to write `temp3d[]=Temp[]`. It means that the vector of coordinates in the finite element basis it taken the same.  
This works for the space P1  
[testtransfer.edp](https://community.freefem.org/uploads/short-url/A8XGImRoZUzpVSje50SKOx71oxB.edp) (579 Bytes)  
as long as the numbering of vertices of Th3 is obtained as the numbering of vertices of Th2 transported by `transfo=[f1,f2,f3]`  
For P2 it does not work because the numbering of dofs seems to be different for the two meshes. Then I have no simple way to do it.

---

<div class="post-metadata">

### Author: ![angelaros](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@angelaros](https://community.freefem.org/u/angelaros)
#### Post date: [July 23, 2025, 5:46pm UTC](https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010/3 "2025-07-23T17:46:42Z")

</div>

Dear François,  
Thank you for your help! Using temp3d=Temp in P1 does work indeed! It is a pity that the same will not hold for P2. In any case, I can always work in P2, and then project into P1 prior to the assignment to the 3d mesh.  
Thanks a lot for your invaluable help.  
Angel.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [July 24, 2025, 9:35am UTC](https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010/4 "2025-07-24T09:35:26Z")

</div>

Dear Angel Daniel,  
Indeed the following lines make it work for P2 or any finite element space:

```auto
int[int] renum(Ch.ndof);
for (int k=0;k<Th2.nt;++k){
 for (int j=0;j<Ch.ndofK;++j){
  renum(Ch3(k,j))=Ch(k,j);
 }
}
temp3d[]=Temp[](renum);

```

full code:

```auto
load "msh3"
real r=1.;
real R=2.;
real x0 = pi/2.;
real x1 = 3.*pi/2.;
real y0 = pi;
real y1 = 2.*pi;
int n = 5;
int m = 5;
mesh Th2 = square(n, m, [x0+(x1-x0)*x, y0+(y1-y0)*y]);
fespace Ch(Th2,P2);
Ch Temp;
//calculations of Temp follow
Temp=x;
plot(Th2, Temp, nbiso=30, fill=true, value=true, cmm="A",wait=1);//plot 2D ok
func f1 = r*sin(x);
func f2 = (R-r*cos(x))*cos(y);
func f3 = (R-r*cos(x))*sin(y);
meshS Th3 = movemesh23(Th2, transfo=[f1,f2,f3]);
fespace Ch3(Th3,P2);

int[int] renum(Ch.ndof);
for (int k=0;k<Th2.nt;++k){
 for (int j=0;j<Ch.ndofK;++j){
  renum(Ch3(k,j))=Ch(k,j);
 }
}

Ch3 temp3d;
temp3d[]=Temp[](renum);
plot(Th3, temp3d, nbiso=30, fill=true, value=true, cmm="A",wait=1);//plot ok

```

---

<div class="post-metadata">

### Author: ![angelaros](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@angelaros](https://community.freefem.org/u/angelaros)
#### Post date: [July 24, 2025, 11:25am UTC](https://community.freefem.org/t/transfer-scalar-field-to-deformed-mesh-with-movemesh2d/4010/5 "2025-07-24T11:25:38Z")

</div>

Dear François,

This solution you provide solves my problem completely!

Thank you very much for your help.

Best regards,

Angel.
