How to combine meshes in dophinx?

I am new to FeniCS and I am still try to figure out how it works.
The dokumentation was quite helpful, but there still some things I am unsure of and cant figure out, even with the help of the mighty Google.
For example I want to make a pseudo 2D mesh, meaning a combination of 1D and 2D elements.

from dolfinx import mesh
from mpi4py import MPI

if __name__ == "__main__":
    omega_1 = mesh.create_rectangle(
            points=((-1.5, 0.0), (0.5, 1.0)), n=(32, 16),
    omega_2 = mesh.create_interval(
            points=(-0.5, 0.5), nx=32,
    omega_3 = mesh.create_rectangle(
            points=((0.5, 0.0), (1.5, 1.0)), n=(32, 16),
    omega_all = omega_1 + omega_2 + omega_3 

When searching for the terms: fenics mesh combination/merge I was only able to find examples for DOLFIN, but with “new” fenics version FEniCSx I thougt these are “legacy” examples and I better find a version, which workes with DOLFINX.
I would be very gradefull for any help!

DOLFIN and DOLFINx does not support mixed meshes (as in meshes where cells are defined as two different cell types). You need to generate a mesh of a single cell type of a single dimension, i.e.
1D: intervals
2D: triangles or quadrilaterals
3D: tetrahedra or hexahedra

In general, mesh generation is no longer done internally in the FEniCS modules (as mshr has been deprecated for years) and users should use external software such as GMSH to generate other meshes than those built in.

Thank you so much for your fast reply!
For a new user who does not have an overview over what is up to date and what is deprecated it would be very helpfull to have some guide of what are the majour changes, so someone can understand if a tutorial ,e.g., from 2015 is still helpfull.

If DOLFINX does not support mixed meshes in one system is there a way to have multiple systems und combine them via boundary conditions? (I read there is something called projection in FeniCSX which sounds like it allows something like this)

A tutorial from 2015 is not useful if you want to use DOLFINx, as the code has been fully refactored.

You should consider the DOLFINx tutorial for guidance on the current version, or the demos in the dolfinx documentation.

To combine multiple meshes through boundary conditions are not trivial. You could probably solve the system iteratively, solving on each mesh with input from them other.

For meshes of the same codimension you could use periodic boundary conditions in DOLFINx_mpc

There are other extensions to DOLFINx, such as

that might be helpful.

Is there a specific application/PDE you have in mind for such a mesh?

1 Like

Thanks a lot.
Yes, there is a special application I have in mind (see battery model from Doyle, Fuller and Newmann). I know there is already an implementation in pyBAMM (they build there own solver) which I could use, BUT I want to understand the equations and wanted to test my “knowledge” about FeniCS with this small project.

I do not have time to parse the whole paper, but is there a specific reason for modelling it as 2D-1D problem, where the 1D part is not embedded as a facet in the 2D problem?

What function spaces would you expect to use, and what are the PDEs on each mesh?

I roiginally thougth it would be easer to have one 2D linear mesh for both electrode (left and right) and in the middle a 1D linear mesh, like the figure below:

But after thinking about your comment I think it would be better to have only 2D domains instead, but I am not sure what “[…], where the 1D part is not embedded as a facet in the 2D problem” means!

The equations I want to model are shown in the next image on the left side:

I dont want a complete solution for this problem, as I would rather like to figure it out my own. But after studing the official documentation, I am still unsure how I should approach this.
More specificly: I know I can define subdomains in FeniCS and I know I can change the value of a material coefficient depending on the domain, but how do I approach the problem that the electrolyte and electrode domain also have other PDEs.

Say you have a 2D mesh, and you have an exterior or interior boundary that aligns with the facets of the mesh. It would be possible to solve a PDE on a set of facets in the mesh.

Since you want to solve different PDEs in different subdomains, you need to use @jpdean mixed assembly. Hopefully he can provide a link to his work.

You could also consider @fballarin’s GitHub - multiphenics/multiphenicsx: multiphenicsx - easy prototyping of multiphysics problems in FEniCSx

“Say you have a 2D mesh, and you have an exterior or interior boundary that aligns with the facets of the mesh. It would be possible to solve a PDE on a set of facets in the mesh.”
Could you mabye provide me with an example? Just something simple so I could see how you would approach it in FeniCS?

If you want to try out the mixed domain functionality, you can find some demos here. Please note that it’s still a work in progress. Any feedback would be very helpful.

Thank you, I will take a look at it!

Hello everybody,
I’ve been led to @jpdean’s code by @dokken 2 days ago. I need to solve a problem with equations on the global domain coupled with equations on a submesh of the global one. Among the coupling terms, an inner product between the trial function define on the submesh and the test function defined on the global mesh appears. But when I call the ‘dolfinx.fem.form()’ function, the following error appears:

RuntimeError: Point dim (2) does not match element dim (3).

@jpdean proposes to call the ‘dolfinx.fem.form()’ function with an additional parameter called ‘entity_map’. But an error appears such as :

TypeError                                 Traceback (most recent call last)
Cell In[15], line 13
     10 D = petsc.assemble_matrix(form(d))
     11 D.assemble()
---> 13 C = petsc.assemble_matrix(form(c, entity_maps=entity_maps_mesh))
     14 #C = petsc.assemble_matrix(form(c))
     15 C.assemble()

TypeError: form() got an unexpected keyword argument 'entity_maps' 

I would be very glad to help any of you with the current issue of this work in the extend that is exactly what I need on my project !

Best regards


The reason you are seeing these errors is because the mixed domain functionality hasn’t been merged into FEniCSx main yet. If you’d like to try it now, you need to install the branches of the FEniCSx components mentioned here. I’ll be working on getting these branches merged into main as soon as I have more time.



Oh, I got it
Thank you very much !! (I’m kind of impressed by the rapidity of this community…)