# Importing domain/boundary markers in fenicsx

**URL:** <https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635>\
**Category:** mesh\
**Created:** [June 27, 2022, 7:54pm UTC](https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635 "2022-06-27T19:54:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![welahi](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@welahi](https://fenicsproject.discourse.group/u/welahi)\
**Post date:** [June 27, 2022, 7:54pm UTC](https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635/1 "2022-06-27T19:54:53Z")

</div>

Dear all,

I have created a mesh with different physical groups in gmsh, converted in to xdmf and loaded it in dolfinx (see code below).

However, I wasn’t able to get the physical groups in dolfinx. I’ve found several tutorials on this topic, but all seem outdated?

**Version 1.** As suggested [here](https://computationalmechanics.in/fenics-the-mesh-workflow/) (resp. in this [code](https://github.com/iitrabhi/dolfinx/blob/iitrabhi/mvc-xdmf/python/demo/poisson-subdomain/demo_poisson_subdomain.py), line 143), I tried using `read_information_int`, but this method seems to have been removed.

**Version 2.** In [this](https://jorgensd.github.io/dolfinx-tutorial/chapter3/subdomains.html#convert-msh-files-to-xdmf-using-meshio) tutorial, it is suggested to use the `find`-method on `MeshTags`, which also seem to have been removed.

**Version 3.** In [this](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412) post using `boundary_markers = dolfinx.mesh.MeshFunction("size_t", mesh, mesh.topology().dim() - 1)` is suggested, but `MeshFunction`s seem to _also_ have been removed. Which, simultaneously, makes [msh2xdmf](https://github.com/floiseau/msh2xdmf) obsolete, I think?

So, what to do? How can I access the physical groups in the current dolfinx release (0.4)?  
Thank you in advance.

Here’s the code:

```auto
# load_mesh.py
import meshio

import dolfinx
from dolfinx.io import XDMFFile
from mpi4py import MPI

with XDMFFile(MPI.COMM_WORLD, "mesh_vol.xdmf", "r") as xdmf:
    mesh = xdmf.read_mesh(name="Grid")
    ct = xdmf.read_meshtags(mesh, name="Grid")
    ## Version 1.
    # tag_info = xdmf.read_information_int()

mesh.topology.create_connectivity(mesh.topology.dim-1, 0)

with XDMFFile(MPI.COMM_WORLD, "mesh_surf.xdmf", "r") as xdmf:
    ft = xdmf.read_meshtags(mesh, name="Grid")

## Version 2.
# ft.find

## Version 3
# boundary_markers = dolfinx.mesh.cpp.MeshFunction("size_t", mesh, mesh.topology().dim() - 1)

```

```auto
# convert.py
import meshio

from dolfinx.io import XDMFFile
from mpi4py import MPI

### Convert from msh to XDMF

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    points = mesh.points[:,:2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={"name_to_read":[cell_data]})
    return out_mesh

mesh = meshio.read("mesh.msh")

mesh_surf = create_mesh(mesh, "triangle")
meshio.write("mesh_surf.xdmf", mesh_surf)

mesh_vol = create_mesh(mesh, "tetra")
meshio.write("mesh_vol.xdmf", mesh_vol)

```

```auto
//+ mesh.geo
SetFactory("OpenCASCADE");
Box(1) = {0, 0, 0, 1, 1, 1};
//+
Physical Surface("s1") = {6};
//+
Physical Surface("s2") = {1};
//+
Physical Surface("s3") = {5};
//+
Physical Surface("s4") = {2};
//+
Physical Surface("s5") = {4};
//+
Physical Surface("s6") = {3};
//+
Physical Volume("dom") = {1};

```

---

<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:** [June 27, 2022, 8:16pm UTC](https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635/2 "2022-06-27T20:16:00Z")

</div>

> [@welahi](#):
>
> **Version 2.** In [this](https://jorgensd.github.io/dolfinx-tutorial/chapter3/subdomains.html#convert-msh-files-to-xdmf-using-meshio) tutorial, it is suggested to use the `find`-method on `MeshTags`, which also seem to have been removed.

Since you are working with v0.4.0, while the tutorial is using v0.4.2.0, I would suggest you have a look at: [https://github.com/jorgensd/dolfinx-tutorial/blob/v0.4.0/chapter3/subdomains.ipynb](https://github.com/jorgensd/dolfinx-tutorial/blob/v0.4.0/chapter3/subdomains.ipynb)  
In particular: `bottom_cells = ct.indices[ct.values==bottom_marker]`

---

<div class="post-metadata">

**Author:** ![welahi](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@welahi](https://fenicsproject.discourse.group/u/welahi)\
**Post date:** [June 27, 2022, 9:14pm UTC](https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635/3 "2022-06-27T21:14:33Z")

</div>

Thanks a bunch, mate. For you answer as well as all the tutorials.

I shouldn’t have dropped the minor version, apparently I’m using 0.4.1 though, at least that’s what condaforge is saying. Your solution still works.

If I would also save the labels into the xdmf-file, would there be a way to read them into fenicsx and access them directly via strings? Or would I still need to use the integer-indices? That is, there is no equivalent to Version 1 anymore, right?

---

<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:** [June 27, 2022, 9:20pm UTC](https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635/4 "2022-06-27T21:20:34Z")

</div>

> [@welahi](#):
>
> If I would also save the labels into the xdmf-file, would there be a way to read them into fenicsx and access them directly via strings?

It has been a very long time (if ever) that there has been support for labelling with strings. The reason for this is that a string can have arbitrary length, and therefore be incredibly inefficient to save/load.

The solution in version one was removed a long time ago, as `MeshFunctions` are inefficient for storing information about any sub-entity (vertices, edges, facets), as one almost never need to tag all of them.

---

<div class="post-metadata">

**Author:** ![welahi](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@welahi](https://fenicsproject.discourse.group/u/welahi)\
**Post date:** [June 27, 2022, 10:09pm UTC](https://fenicsproject.discourse.group/t/importing-domain-boundary-markers-in-fenicsx/8635/5 "2022-06-27T22:09:09Z")

</div>

Sounds reasonable. Thanks again.
