# Mapping between meshes in dolfinx

**URL:** <https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932>\
**Category:** dolfinx\
**Created:** [September 29, 2024, 1:13pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932 "2024-09-29T13:13:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [September 29, 2024, 1:13pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/1 "2024-09-29T13:13:59Z")

</div>

Hi,

I am adapting FEniCSx for FSI problems using the ALE formulation. I am having difficulties transferring data from the fluid mesh to the solid mesh and vice versa.

Say, I know fluid velocity (`fluid_velo`) and pressure (`fluid_pres`) and want to calculate the force vector on the fluid-solid boundary.

```auto
fluid_stress = mu*grad(fluid_velo) - fluid_pres*Id

fluid_traction = fluid_stress*normals_fluid

#force_on_solid = inner( solid_disp_test, fluid_traction ) * ds_solid(2)
force_on_solid = inner(solid_disp_test, fluid_traction) * ds_fluid(5)

solid_force_temp = dolfinx.fem.assemble_vector(dolfinx.fem.form(force_on_solid))

```

When run, I get the following error.

```auto
Traceback (most recent call last):
  File "/home/chenna/Documents/myCode/fenics/FSI/channel-thickbeam/channel-thickbeam-FSI.py", line 622, in <module>
    solid_force_temp = dolfinx.fem.assemble_vector(dolfinx.fem.form(force_on_solid))
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/forms.py", line 249, in form
    return _create_form(form)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/forms.py", line 244, in _create_form
    return _form(form)
           ^^^^^^^^^^^
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/forms.py", line 229, in _form
    f = ftype(
        ^^^^^^
RuntimeError: Incompatible mesh. entity_maps must be provided.

```

With `ds_solid(2)`, I get a different error.

I would appreciate any help with this. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [September 29, 2024, 1:43pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/2 "2024-09-29T13:43:41Z")

</div>

Without a definition of the parent mesh, and how you have extracted the fluid and solid components, it is hard to give you any further guidance. You can for instance have a look at:

> [@Discontinuity at interface using mixed domains](https://fenicsproject.discourse.group/t/discontinuity-at-interface-using-mixed-domains/15040/2):
>
> I think this is the variational form that you would like: # SPDX-License-Identifier: MIT from mpi4py import MPI import dolfinx import dolfinx.fem.petsc import ufl import numpy as np from petsc4py import PETSc class NewtonSolver: max\_iterations: int bcs: list[dolfinx.fem.DirichletBC] A: PETSc.Mat b: PETSc.Vec J: dolfinx.fem.Form b: dolfinx.fem.Form dx: PETSc.Vec def \_\_init\_\_( self, F: list[dolfinx.fem.form], J: list[list[dolfinx.fem.for…

for inspiration on how to couple data between parts of the mesh.

---

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [September 29, 2024, 2:36pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/3 "2024-09-29T14:36:09Z")

</div>

Thanks for your prompt response! The code is about 800 lines. So, I did not want to post it all here.

The meshes for the fluid and solid domains are separate, generated using GMSH and read and stored separately.

```auto
domain_fluid, domain_fluid_markers, domain_fluid_facet_tags = io.gmshio.read_from_msh("channel-thickbeam-2D-fluid-P2.msh", MPI.COMM_WORLD)

dx_fluid = ufl.Measure('dx', domain=domain_fluid, metadata={'quadrature_degree': 4})
ds_fluid = ufl.Measure('ds', domain=domain_fluid, subdomain_data=domain_fluid_facet_tags, metadata={'quadrature_degree': 4})

domain_solid, domain_solid_markers, domain_solid_facet_tags = io.gmshio.read_from_msh("channel-thickbeam-2D-solid-P2.msh", MPI.COMM_WORLD)

dx_solid = ufl.Measure('dx', domain=domain_solid, metadata={'quadrature_degree': 4})
ds_solid = ufl.Measure('ds', domain=domain_solid, subdomain_data=domain_solid_facet_tags, metadata={'quadrature_degree': 4})

```

Each mesh has its own tags for the fluid-solid interface.  
The fluid mesh is shown in the figure below. The respective FE spaces and variables are defined separately for each domain.

I can solve for the fluid velocity and pressure. Next, I want to calculate the force on the interface in terms of solid DOFs so I can add the array to the RHS of the solid problem.

 ![Screenshot from 2024-09-29 15-30-22](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/d/db0c6f3da437e09297ac9f9898b834d9bc7cea1b.jpeg)

Let me know if you need additional information.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [September 29, 2024, 2:44pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/4 "2024-09-29T14:44:22Z")

</div>

If the meshes are meshed separately you need to map the function or derived quantity from one mesh to the other, see for instance

> [@Interpolate on boundary](https://fenicsproject.discourse.group/t/interpolate-on-boundary/15917/7):
>
> Consider the minimal working example: from mpi4py import MPI import dolfinx import numpy as np L = 1 N = 5 def interface(x): return np.isclose(x[0], L) mesh\_1 = dolfinx.mesh.create\_box( comm=MPI.COMM\_WORLD, points=[(0, 0, 0), (L, L, L)], n=[N, N, N] ) V = dolfinx.fem.functionspace(mesh\_1, ("Lagrange", 2)) u = dolfinx.fem.Function(V) u.interpolate(lambda x: x[0] + np.cos(x[1]) + np.sin(np.pi \* x[2])) Nx, Ny, Nz = 12, 13, 15 mesh\_2 = dolfinx.mesh.create\_box( comm=MPI.COMM\_WORLD…

which illustrates this.

---

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [September 29, 2024, 3:52pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/5 "2024-09-29T15:52:48Z")

</div>

Thank you! I will go through this.

---

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [October 2, 2024, 7:53pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/6 "2024-10-02T19:53:57Z")

</div>

Continuing on, I realised that I needed to calculate the force on the fluid mesh first and then interpolate it onto the boundaries of the solid domain.

I am calculating the force using the following code once `fluid_velo` and `fluid_pres` are available.

```auto
fluid_stress = mu*grad(fluid_velo) - fluid_pres*Id

fluid_traction = fluid_stress*normals_fluid

force_on_solid = inner(fluid_velo_test, fluid_traction) * ds_fluid(5)

fluid_force_temp = assemble_vector(form(force_on_solid))

```

I get the `fluid_force_temp`. I would like to know  
(i) How do I export this in VTK format? I use 6-noded triangles for the velocity field and 3-noded for the pressure as a mixed element.

(ii) The correct type (like fem.Function) I should use for `fluid_force_temp` in order to map this force data to the solid domain using the code suggested for interpolating on the boundary.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 3, 2024, 5:14am UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/7 "2024-10-03T05:14:42Z")

</div>

> [@chennachaos](#):
>
> `fluid_force_temp = assemble_vector(form(force_on_solid))`

Rewrite this as

```auto
fluid_force = dolfinx.fem.Function(Velocity_space)
dolfinx.fem.assemble_vector(fluid_force.x.array, form(force_on_solid))
fluid_force.x.scatter_reverse(la.InsertMode.add)

```

This will assemble the force into a dolfinx function that you can then output, and in a way that you can transfer it between meshes with interpolation.

---

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [October 4, 2024, 8:34am UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/8 "2024-10-04T08:34:43Z")

</div>

Thank you!

With your help, I calculated the force and exported it in VTK format.

While the mapping from the fluid mesh to the solid mesh also seems to work fine, there is an issue with the forces. The distribution of forces should be smoother but the values of force components are much higher at mid-nodes (see the attached figure). I checked velocity and pressure profiles. They are smoother.

I tried various options, interpolating pressure from P1 to P2, using pressure and viscous terms separately, etc., but the behaviour persists. I suspect the issue might be in evaluating the integral, but I don’t know how to check further and fix it.

 ![FSI-forces](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/9/99814e650d25984364a76c37eca66a8c8452dd07.png)

This is the code block.

```auto
    normals_fluid = FacetNormal(domain_fluid)

    fluid_pres_P2 = Function(functionspace(domain_fluid,P2))
    fluid_pres_P2.interpolate(w.sub(1))

    fluid_velo_test2 = TestFunction(V2)

    force_on_solid = inner(fluid_velo_test2, -fluid_pres_P2*normals_fluid) * ds_fluid(5)

    fluid_force_temp = Function(V2)

    dolfinx.fem.assemble_vector(fluid_force_temp.x.array, form(force_on_solid))
    fluid_force_temp.x.scatter_reverse(dolfinx.cpp.la.InsertMode.add)

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 4, 2024, 2:42pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/9 "2024-10-04T14:42:02Z")

</div>

> [@chennachaos](#):
>
> While the mapping from the fluid mesh to the solid mesh also seems to work fine, there is an issue with the forces. The distribution of forces should be smoother but the values of force components are much higher at mid-nodes (see the attached figure). I checked velocity and pressure profiles. They are smoother.

Could you clarify one thing:

1. Is the forces wrong when you compute them on the initial mesh, or when you transfer them from the fluid to solid mesh?  
Then, for my own understanding:

- Is what you want to visualize the `fluid_traction`, or the assembled vector `force_on_solid`?  
Fluid traction can be visualized (at quadrature points) with for instance:  
[Visualizing quadrature functions as point clouds — scifem](https://scientificcomputing.github.io/scifem/examples/xdmf_point_cloud.html)  
which even works for boundary integrals. See for instance: [presentations/fenics\_ecole\_des\_ponts/code/expressions.py at dokken/edp · jorgensd/presentations · GitHub](https://github.com/jorgensd/presentations/blob/dokken/edp/fenics_ecole_des_ponts/code/expressions.py#L69-L99)  
with corresponding plot:  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/3/3c01a304b7461b35e2711e0cf870df3c30fb502f.png)

---

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [October 8, 2024, 9:59am UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/10 "2024-10-08T09:59:20Z")

</div>

The force vector I got earlier was correct but my interpretation was wrong and there was a bug in the boundary conditions. I fixed everything. The FSI code is working now.

 ![FSI-FEniCSx-1](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/1/1ba106825f8716bd64dd3fce32d220d3e4d98ed6.jpeg)

Thank you very much for your assistance!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 8, 2024, 10:53am UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/11 "2024-10-08T10:53:49Z")

</div>

It would be good if you could post your solutions, to the benefit of the open source community:)

---

<div class="post-metadata">

**Author:** ![chennachaos](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chennachaos/32/7007_2.png) [@chennachaos](https://fenicsproject.discourse.group/u/chennachaos)\
**Post date:** [October 8, 2024, 12:42pm UTC](https://fenicsproject.discourse.group/t/mapping-between-meshes-in-dolfinx/15932/12 "2024-10-08T12:42:00Z")

</div>

Certainly! I will share via GitHub once I test it further with more examples.

Scripts for CFD problems using a coupled velocity-pressure formulation with Taylor-Hood elements are shared at [GitHub - chennachaos/CFDwithFEniCSx: Computational Fluid Dynamics with FEniCSx library](https://github.com/chennachaos/CFDwithFEniCSx).
