# How to import xdmf mesh generated by gmsh into fenics

**URL:** https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045
**Category:** General
**Created:** [December 27, 2022, 2:30am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045 "2022-12-27T02:30:05Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![taytay33](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@taytay33](https://fenicsproject.discourse.group/u/taytay33)
#### Post date: [January 5, 2023, 9:59am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/24 "2023-01-05T09:59:07Z")

</div>

I have cell neighbours of each element. How can I differentiate the two and define different material.

 ![out](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/2/25c9b09228577cc8e2813a0f0cd4a9b83158169d.png)

---

<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: [January 5, 2023, 10:29am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/25 "2023-01-05T10:29:34Z")

</div>

As I told you

> [@dokken](#):
>
> 1. Write a recursive script looping through  
> a. For each interface of an object find each connected cell.  
> b. Check each connected cells loop through neighbours (going through each of their respective facets,

So start at a marked interface, find the two cells connected to that facet.  
Give each of the cells a unique number (say cell0 is marked with 5 cell1 is marked with 7).  
in sudo-code this is what you need to do for each cell:

```python
def mark_neigbours(cell, tag, interface_markers, volume_markers, visited_cells):
    if c in visited_cells:
         return
    c_facets = facets(cell)
    for facet in c_facets:
        if facet in (interface markers):
            pass
        else
            connected_cells=cells(facet)
            for c in connected_cells:
                if c!= cell
                   volume_markers[c] = tag
                   visited_cells.append(c)
                   mark_neigbours(c, tag, interface_markers, visited_cells)

```

As I said, this is a recursive algorithm, which is going to be quite complex (and I might have made mistakes in the pseudo code). You should really let the mesh generator handle the tagging of cells.

---

<div class="post-metadata">

### Author: ![taytay33](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@taytay33](https://fenicsproject.discourse.group/u/taytay33)
#### Post date: [January 5, 2023, 10:47am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/26 "2023-01-05T10:47:08Z")

</div>

after marking neighbours how will define the different material for each cell?

---

<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: [January 5, 2023, 10:51am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/27 "2023-01-05T10:51:12Z")

</div>

That is the whole point of `volume_markers`, which should be a `MeshFunction`, which you can modify the array of, with the appropriate tags, such that you can compute integrals over subdomains.

---

<div class="post-metadata">

### Author: ![taytay33](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@taytay33](https://fenicsproject.discourse.group/u/taytay33)
#### Post date: [January 5, 2023, 11:03am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/28 "2023-01-05T11:03:31Z")

</div>

OO I’m such a dumb.

I imported the mesh file

```auto
mesh = Mesh()
with XDMFFile("mesh.xdmf") as infile:
    infile.read(mesh)
mvc = MeshValueCollection("size_t", mesh, 2)
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

```

Than I found the cell\_neighbours:

```auto
tdim = mesh.topology().dim()
mesh.init(tdim - 1, tdim)

cell_neighbours = {}
for cell in cells(mesh):
    index = cell.index()
    cell_neighbours[index] = []
    for facet in facets(cell):
        facet_cells = facet.entities(tdim)
        for facet_cell in facet_cells:
            if (index!=facet_cell):
                cell_neighbours[index].append(facet_cell)
~~~~~~~

to mark the Interface, have to use this function 

~~~~~
def mark_neigbours(cell, tag, interface_markers, volume_markers, visited_cells):
    if c in visited_cells:
         return
    c_facets = facets(cell)
    for facet in c_facets:
        if facet in (interface_markers):
            pass
        else:
            connected_cells=cells(facet)
            for c in connected_cells:
                if (c!= cell):
                   volume_markers[c] = tag
                   visited_cells.append(c)
                   mark_neigbours(c, tag, interface_markers, visited_cells)

```

I have not understood a single thing what to give in this function and proceed next.

---

<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: [January 5, 2023, 3:18pm UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/29 "2023-01-05T15:18:54Z")

</div>

> [@taytay33](#):
>
> I have not understood a single thing what to give in this function and proceed next.

As I’ve stated, this is a complicated way of doing it, as the mesh generator should have a way easier time of determining these volumes. I do not have time to write out this algorithm for you.

---

<div class="post-metadata">

### Author: ![taytay33](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@taytay33](https://fenicsproject.discourse.group/u/taytay33)
#### Post date: [January 20, 2023, 1:07am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/30 "2023-01-20T01:07:26Z")

</div>

I’m getting this error. Any Idea? Solution???

```auto
UMFPACK V5.7.9 (Oct 20, 2019): ERROR: out of memory

Traceback (most recent call last):
  File "/mnt/c/downloads/run.py", line 90, in <module>
    solve(A_ass, u.vector(), L_ass)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 240, in solve
    return dolfin.la.solver.solve(*args)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/la/solver.py", line 72, in solve
    return cpp.la.solve(A, x, b, method, preconditioner)
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
*** fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error: Unable to successfully call PETSc function 'KSPSolve'.
*** Reason: PETSc error code is: 76 (Error in external library).
*** Where: This error was encountered inside ./dolfin/la/PETScKrylovSolver.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: unknown
```

---

<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: [January 20, 2023, 5:26am UTC](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045/31 "2023-01-20T05:26:37Z")

</div>

See for instance [UMFPACK error: out of memory despite system having free memory - #2 by plugged](https://fenicsproject.discourse.group/t/umfpack-error-out-of-memory-despite-system-having-free-memory/984/2)

[Previous page](https://fenicsproject.discourse.group/t/how-to-import-xdmf-mesh-generated-by-gmsh-into-fenics/10045.md?page=1)
