Generate 3D mesh on fenics

I has a 3D geometry in stp file, i created a 3d mesh for this geometry and i save it in stl file. Finally, i converted it into an xml file using meshio but when i checked the mesh (in xml file), i discovered that it became a 2D mesh, how can I create a new 3d mesh in fenics?
I searched on the net and i discovered that there are some open source library such as GMSH to do that but i didn’t find the command or the line that i shoud add to my programm in order to create a 3d mesh, can you help me?
if you need an exemple to suggest the solution, we suppose that the gemetry is a box imported in fenics,

Hi @Salim
First of all, xml is a legacy format and you should use xdmf file format instead.
I don’t know if you want to use your stl file to create 3D mesh or using gmsh is fine, but @dokken has great tutorials on his webpage about how to use gmsh to create 3D mesh, so you should have a look on it in case you want to use gmsh to create 3D mesh.

Link to the tutorials : Mesh generation and conversion with GMSH and PYGMSH | Jørgen S. Dokken

1 Like

See for instance: Files · master · gmsh / gmsh · GitLab

You could also have a look at:

However, since you say

I interpret this as you had a mesh with tetrahedral elements, that you have converted to stl, which only stores surfaces, STL (file format) - Wikipedia
Consider exporting the mesh to another file format the keeps the volume cells.

I converted the file into msh format by meshio then i tried this script to export a 3D mesh in xdmf format:

import gmsh
import meshio
import numpy

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)
    out_mesh = meshio.Mesh(points=mesh.points, cells={cell_type: cells}, cell_data={"Mesh":[cell_data]})
    if prune_z:
        out_mesh.prune_z_0()
    return out_mesh
mesh3D_from_msh = meshio.read("box.msh")
tetra_mesh = create_mesh(mesh3D_from_msh, "tetra",prune_z=True)
meshio.write("mesh3D_from_msh.xdmf", tetra_mesh)

But i get thie erreur:

Traceback (most recent call last):
  File "1.py", line 13, in <module>
    tetra_mesh = create_mesh(mesh3D_from_msh, "tetra",prune_z=True)
  File "1.py", line 7, in create_mesh
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
  File "/home/salim/.local/lib/python3.8/site-packages/meshio/_mesh.py", line 228, in get_cell_data
    [d for c, d in zip(self.cells, self.cell_data[name]) if c.type == cell_type]
KeyError: 'gmsh:physical'

Remove this bit (it just means that you do not have any gmsh markers).

Thank you, but the same error remains

You also need to remove this line.

Now, i had another error

Traceback (most recent call last):
  File "1.py", line 13, in <module>
    meshio.write("mesh3D_from_msh.xdmf", tetra_mesh)
  File "/home/salim/.local/lib/python3.8/site-packages/meshio/_helpers.py", line 131, in write
    if value.shape[1] != num_nodes_per_cell[key]:
IndexError: tuple index out of range

I would strongly suggest that you attach the msh file, as there is no way of helping you without it