Using tetra and triangle or line and triagle elements

hi all,

im trying to create a mesh with my gmsh application installed on my pc and import it to xdmf
i was wondering if im creating a 2d model and extrude it to 3d, should i user tetra and triangle element or line and triangle ? (when i create the mesh in gmsh i do chose the option of 3d mesh)

im attaching my geo file and the mesh Dropbox
basically what i indented to do (as maybe some of you saw in my previous posts) is to create a rod with a cylinder instead of it (2 different materials).

im using an import that was posted before by Francesco Creating subdomains and boundary conditions on imported gmsh mesh

import os
import fenics as fe
from dolfin import *
import matplotlib.pyplot as plt
from ffc.fiatinterface import create_quadrature as cquad
import numpy as np
import meshio
import dolfin

parameters["form_compiler"]["cpp_optimize"] = True
parameters["form_compiler"]["representation"] = "uflacs"

#defining the young modulus
E1, E2, nu = 3e6, 3e6, 0.49

# Create mesh and define function space

mesh_name = "might_be_final"

# Then, read it in through meshio
meshio_mesh = meshio.read(f"{mesh_name}.msh")

# Extract mesh corresponding to each cell type, and write it out with meshio
def extract_mesh_by_cell_type(meshio_mesh, cell_type):
    cells = meshio_mesh.get_cells_type(cell_type)
    if len(cells) == 0:
        return None

    cell_data = meshio_mesh.get_cell_data("gmsh:physical", cell_type)
    return meshio.Mesh(points=meshio_mesh.points, cells={cell_type: cells}, cell_data={"markers": [cell_data]})


tetra_mesh = extract_mesh_by_cell_type(meshio_mesh, "tetra")
if tetra_mesh:
    meshio.write(f"{mesh_name}_tetra.xdmf", tetra_mesh)

triangle_mesh = extract_mesh_by_cell_type(meshio_mesh, "triangle")
if triangle_mesh:
    meshio.write(f"{mesh_name}_triangle.xdmf", triangle_mesh)

del meshio_mesh

# Read back in the meshio mesh from XDMF in order to convert to a dolfin mesh
dolfin_mesh = dolfin.Mesh()
if triangle_mesh and os.path.exists(f"{mesh_name}_triagnle.xdmf"):
    dolfin.XDMFFile(f"{mesh_name}_triangle.xdmf").read(dolfin_mesh)
elif tetra_mesh and os.path.exists(f"{mesh_name}_tetra.xdmf"):
    dolfin.XDMFFile(f"{mesh_name}_tetra.xdmf").read(dolfin_mesh)
else:
    raise FileNotFoundError("No suitable mesh file found to read.")


# Read back in the markers in order to attach them to the dolfin mesh
def read_back_in_markers(meshio_xdmf_file, dolfin_mesh):
    dolfin_markers = dolfin.MeshValueCollection("size_t", dolfin_mesh)
    dolfin.XDMFFile(meshio_xdmf_file).read(dolfin_markers, "markers")
    dolfin_mesh_function = dolfin.cpp.mesh.MeshFunctionSizet(dolfin_mesh, dolfin_markers)

    dolfin_mesh_function_array = dolfin_mesh_function.array()
    defined_markers = np.array(list(set(dolfin_markers.values().values())), dtype=dolfin_mesh_function_array.dtype)
    defined_entries = np.isin(dolfin_mesh_function_array, defined_markers)
    dolfin_mesh_function_array[~defined_entries] = 0
    dolfin_mesh_function.set_values(dolfin_mesh_function_array)
    return dolfin_mesh_function

dolfin_subdomains = read_back_in_markers(f"{mesh_name}_tetra.xdmf", dolfin_mesh)
dolfin_boundaries = read_back_in_markers(f"{mesh_name}_triangle.xdmf", dolfin_mesh)

# Now export them to file
dolfin.XDMFFile(f"{mesh_name}.xdmf").write(dolfin_mesh)
dolfin.XDMFFile(f"{mesh_name}_subdomains.xdmf").write(dolfin_subdomains)
dolfin.XDMFFile(f"{mesh_name}_boundaries.xdmf").write(dolfin_boundaries)

# Please now open these three files in paraview and check that the labels are as expected

# You may remove *_tetra.* and *_triangle.*, they are not needed anymore
os.remove(f"{mesh_name}_tetra.xdmf")
os.remove(f"{mesh_name}_tetra.h5")
if triangle_mesh:
    os.remove(f"{mesh_name}_triangle.xdmf")
    os.remove(f"{mesh_name}_triangle.h5")

mesh_name = "might_be_final"

mesh = dolfin.Mesh()
dolfin.XDMFFile(f"{mesh_name}.xdmf").read(mesh)

materials = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim())
dolfin.XDMFFile(f"{mesh_name}_subdomains.xdmf").read(materials)
print(f"Subdomains: {np.unique(materials.array())}")

boundary_parts = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
dolfin.XDMFFile(f"{mesh_name}_boundaries.xdmf").read(boundary_parts)
print(f"Boundaries: {np.unique(boundary_parts.array())}")
tol = 1E-14

V = VectorFunctionSpace(mesh, "Lagrange", 1)

bc1 = DirichletBC(V.sub(0), Constant(0), boundary_parts, 19)
bc3 = DirichletBC(V.sub(1), Constant(0), boundary_parts, 21)
bc5 = DirichletBC(V.sub(2), Constant(0), boundary_parts, 23)
bc6 = DirichletBC(V.sub(2), Constant(0), boundary_parts, 24)
tDirBC = Expression(('1.0*time_'), time_=0.0, degree=0)
tDirBC4 = Expression(('((1/(time_+1))-1)*0.2'), time_=0.0, degree=0)
bc2 = DirichletBC(V.sub(0), tDirBC, boundary_parts, 20)
bc4 = DirichletBC(V.sub(1), tDirBC4, boundary_parts, 22)

bcs = [bc1, bc2, bc3, bc4, bc5, bc6]
subdomain_ids = [20, 22]
ds = Measure("ds", subdomain_data=boundary_parts, subdomain_id=(20, 22))
ds = ds(degree=4)

more then appreciate any advise/help

If you have extruded your mesh to 3D and meshed it with tetrahedra, you should import a tetrahedral mesh and marked triangular facets.

hi @dokken thanks for your reply
as before im trying to import this mesh with this code that was provided to me before in the community
any idea why do i get this error?
how cna i make sure that it tetra elements?

Traceback (most recent call last):
  File "/home/mirialex/PycharmProjects/fibers/21-06-24.py", line 119, in <module>
    tetra_mesh = extract_mesh_by_cell_type(meshio_mesh, "tetra")
  File "/home/mirialex/PycharmProjects/fibers/21-06-24.py", line 116, in extract_mesh_by_cell_type
    cell_data = meshio_mesh.get_cell_data("gmsh:physical", cell_type)
  File "/usr/lib/python3/dist-packages/meshio/_mesh.py", line 249, in get_cell_data
    return np.concatenate(
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: need at least one array to concatenate

files Dropbox
code

import fenics as fe
from dolfin import *
import matplotlib.pyplot as plt
from ffc.fiatinterface import create_quadrature as cquad
import numpy as np
import meshio
import dolfin
from fenics import *
import sys, os, shutil, math
import matplotlib.pyplot as plt  # Plotting Library


mesh_name = "22_06_242"

# Then, read it in through meshio
meshio_mesh = meshio.read(f"{mesh_name}.msh")

# Extract mesh corresponding to each cell type, and write it out with meshio
def extract_mesh_by_cell_type(meshio_mesh, cell_type):
    cells = meshio_mesh.get_cells_type(cell_type)
    cell_data = meshio_mesh.get_cell_data("gmsh:physical", cell_type)
    return meshio.Mesh(points=meshio_mesh.points, cells={cell_type: cells}, cell_data={"markers": [cell_data]})

tetra_mesh = extract_mesh_by_cell_type(meshio_mesh, "tetra")
meshio.write(f"{mesh_name}_tetra.xdmf", tetra_mesh)
del tetra_mesh
triangle_mesh = extract_mesh_by_cell_type(meshio_mesh, "triangle")
meshio.write(f"{mesh_name}_triangle.xdmf", triangle_mesh)
del triangle_mesh

del meshio_mesh

# Read back in the meshio mesh from XDMF in order to convert to a dolfin mesh
dolfin_mesh = dolfin.Mesh()
dolfin.XDMFFile(f"{mesh_name}_tetra.xdmf").read(dolfin_mesh)

# Read back in the markers in order to attach them to the dolfin mesh
def read_back_in_markers(meshio_xdmf_file, dolfin_mesh):
    dolfin_markers = dolfin.MeshValueCollection("size_t", dolfin_mesh)
    dolfin.XDMFFile(meshio_xdmf_file).read(dolfin_markers, "markers")
    dolfin_mesh_function = dolfin.cpp.mesh.MeshFunctionSizet(dolfin_mesh, dolfin_markers)

    dolfin_mesh_function_array = dolfin_mesh_function.array()
    defined_markers = np.array(list(set(dolfin_markers.values().values())), dtype=dolfin_mesh_function_array.dtype)
    defined_entries = np.isin(dolfin_mesh_function_array, defined_markers)
    dolfin_mesh_function_array[~defined_entries] = 0
    dolfin_mesh_function.set_values(dolfin_mesh_function_array)
    return dolfin_mesh_function

dolfin_subdomains = read_back_in_markers(f"{mesh_name}_tetra.xdmf", dolfin_mesh)
dolfin_boundaries = read_back_in_markers(f"{mesh_name}_triangle.xdmf", dolfin_mesh)

# Now export them to file
dolfin.XDMFFile(f"{mesh_name}.xdmf").write(dolfin_mesh)
dolfin.XDMFFile(f"{mesh_name}_subdomains.xdmf").write(dolfin_subdomains)
dolfin.XDMFFile(f"{mesh_name}_boundaries.xdmf").write(dolfin_boundaries)

# Please now open this three files in paraview and check that the labels are as expected

# You may remove *_tetra.* and *_triangle.*, they are not needed anymore
os.remove(f"{mesh_name}_tetra.xdmf")
os.remove(f"{mesh_name}_tetra.h5")
os.remove(f"{mesh_name}_triangle.xdmf")
os.remove(f"{mesh_name}_triangle.h5")

mesh_name = "22_06_242"

mesh = dolfin.Mesh()
dolfin.XDMFFile(f"{mesh_name}.xdmf").read(mesh)

subdomains = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim())
dolfin.XDMFFile(f"{mesh_name}_subdomains.xdmf").read(subdomains)
print(f"Subdomains: {np.unique(subdomains.array())}")

boundary_parts = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
dolfin.XDMFFile(f"{mesh_name}_boundaries.xdmf").read(boundary_parts)
print(f"Boundaries: {np.unique(boundary_parts.array())}")

image

Please post your geo file here for transparency. Posting it through Dropbox doesn’t ensure that it lives on after the post is resolved.

// Gmsh project created on Sat Jun 22 13:44:25 2024
//+
SetFactory("OpenCASCADE");
Circle(1) = {0, 0.1, 0.1, 0.05, 0, 2*Pi};
//+
Rotate {{0, 1, 0}, {0, 0.1, 0.1}, Pi/2} {
  Curve{1}; 
}
//+
Point(2) = {0, 0.2, 0, 1.0};
//+
Point(3) = {0, 0, 0, 1.0};
//+
Point(4) = {0, 0, 0.2, 1.0};
//+
Point(5) = {0, 0.2, 0.2, 1.0};
//+
Line(2) = {3, 2};
//+
Line(3) = {2, 5};
//+
Line(4) = {5, 4};
//+
Line(5) = {4, 3};
//+
Curve Loop(1) = {2, 3, 4, 5};
//+
Curve Loop(2) = {1};
//+
Plane Surface(1) = {1, 2};
//+
Curve Loop(3) = {1};
//+
Plane Surface(2) = {3};
//+
Coherence;
//+
Coherence;
//+
Transfinite Curve {1} = 25 Using Progression 1;
//+
Transfinite Curve {5, 4, 3, 2} = 20 Using Progression 1;

//+
Extrude {1, 0, 0} {
  Surface{1}; Surface{2}; Layers {25}; Recombine;
}
//+
Physical Surface("left", 16) = {6, 1, 2};
//+
Physical Surface("right", 17) = {8, 9};
//+
Physical Surface("top", 18) = {5};
//+
Physical Surface("down", 19) = {3};
//+
Physical Surface("front", 20) = {6};
//+
Physical Surface("back", 21) = {4};
//+
Physical Volume("box1", 22) = {1};

Additionally, please note that you only have to use the following lines to diagnose the issue in your case:

import meshio
mesh_name = "22_06_242"

meshio_mesh = meshio.read(f"{mesh_name}.msh")
print(meshio_mesh)

as this yields:

<meshio mesh object>
  Number of points: 9200
  Number of cells:
    triangle: 600
    triangle: 122
    quad: 475
    quad: 475
    quad: 475
    quad: 475
    triangle: 600
    triangle: 122
    wedge: 15000
  Cell sets: left, right, top, down, front, back, box1, gmsh:bounding_entities
  Point data: gmsh:dim_tags
  Cell data: gmsh:physical, gmsh:geometrical
  Field data: left, right, top, down, front, back, box1

i.e. your mesh is using wedges, which is not supported in legacy Dolfin.

Simply remove the recombine part of your geo code:

// Gmsh project created on Sat Jun 22 13:44:25 2024
//+
SetFactory("OpenCASCADE");
Circle(1) = {0, 0.1, 0.1, 0.05, 0, 2*Pi};
//+
Rotate {{0, 1, 0}, {0, 0.1, 0.1}, Pi/2} {
  Curve{1}; 
}
//+
Point(2) = {0, 0.2, 0, 1.0};
//+
Point(3) = {0, 0, 0, 1.0};
//+
Point(4) = {0, 0, 0.2, 1.0};
//+
Point(5) = {0, 0.2, 0.2, 1.0};
//+
Line(2) = {3, 2};
//+
Line(3) = {2, 5};
//+
Line(4) = {5, 4};
//+
Line(5) = {4, 3};
//+
Curve Loop(1) = {2, 3, 4, 5};
//+
Curve Loop(2) = {1};
//+
Plane Surface(1) = {1, 2};
//+
Curve Loop(3) = {1};
//+
Plane Surface(2) = {3};
//+
Coherence;
//+
Coherence;
//+
Transfinite Curve {1} = 25 Using Progression 1;
//+
Transfinite Curve {5, 4, 3, 2} = 20 Using Progression 1;

//+
Extrude {1, 0, 0} {
  Surface{1}; Surface{2}; Layers {25};
}
//+
Physical Surface("left", 16) = {6, 1, 2};
//+
Physical Surface("right", 17) = {8, 9};
//+
Physical Surface("top", 18) = {5};
//+
Physical Surface("down", 19) = {3};
//+
Physical Surface("front", 20) = {6};
//+
Physical Surface("back", 21) = {4};

//+
Physical Volume("box1", 22) = {1};

hi, when i try to remove the recombine, and create the mesh again it fails with this error
image
is it possible that i dont need to create the mesh again? just to change it in the geo file?

You need to change the geo file. as seen in the log below, the .geo file I posted below runs with the following output:

Info    : Running '/home/dokken/Documents/src/gmsh-4.12.2-Linux64/bin/gmsh -3 mesh2.geo' [Gmsh 4.12.2, 1 node, max. 1 thread]
Info    : Started on Sat Jun 22 17:20:51 2024
Info    : Reading 'mesh2.geo'...
Info    : Done reading 'mesh2.geo'
Info    : Meshing 1D...
Info    : [  0%] Meshing curve 1 (Circle)
Info    : [ 10%] Meshing curve 2 (Line)
Info    : [ 20%] Meshing curve 3 (Line)
Info    : [ 20%] Meshing curve 4 (Line)
Info    : [ 30%] Meshing curve 5 (Line)
Info    : [ 40%] Meshing curve 6 (Extruded)
Info    : [ 40%] Meshing curve 7 (Extruded)
Info    : [ 50%] Meshing curve 8 (Extruded)
Info    : [ 60%] Meshing curve 9 (Extruded)
Info    : [ 60%] Meshing curve 10 (Extruded)
Info    : [ 70%] Meshing curve 11 (Extruded)
Info    : [ 80%] Meshing curve 12 (Extruded)
Info    : [ 80%] Meshing curve 13 (Extruded)
Info    : [ 90%] Meshing curve 14 (Extruded)
Info    : [100%] Meshing curve 15 (Extruded)
Info    : Done meshing 1D (Wall 0.00127858s, CPU 0.002682s)
Info    : Meshing 2D...
Info    : [  0%] Meshing surface 1 (Plane, Frontal-Delaunay)
Info    : [ 20%] Meshing surface 2 (Plane, Frontal-Delaunay)
Info    : [ 30%] Meshing surface 3 (Extruded)
Info    : [ 40%] Meshing surface 4 (Extruded)
Info    : [ 50%] Meshing surface 5 (Extruded)
Info    : [ 60%] Meshing surface 6 (Extruded)
Info    : [ 70%] Meshing surface 7 (Extruded)
Info    : [ 80%] Meshing surface 8 (Extruded)
Info    : [ 90%] Meshing surface 9 (Extruded)
Info    : Done meshing 2D (Wall 0.0883573s, CPU 0.198337s)
Info    : Meshing 3D...
Info    : Meshing volume 1 (Extruded)
Info    : Meshing volume 2 (Extruded)
Info    : Subdividing extruded mesh
Info    : Swapping 325
Info    : Swapping 100
Info    : Swapping 0
Info    : Remeshing surface 3
Info    : Meshing surface 3 (Extruded)
Info    : Remeshing surface 4
Info    : Meshing surface 4 (Extruded)
Info    : Remeshing surface 5
Info    : Meshing surface 5 (Extruded)
Info    : Remeshing surface 6
Info    : Meshing surface 6 (Extruded)
Info    : Remeshing surface 7
Info    : Meshing surface 7 (Extruded)
Info    : Done meshing 3D (Wall 0.459786s, CPU 0.461723s)
Info    : Optimizing mesh...
Info    : Done optimizing mesh (Wall 0.00112793s, CPU 0.000816s)
Info    : 10400 nodes 60929 elements
Info    : Writing 'mesh2.msh'...
Info    : Done writing 'mesh2.msh'
Info    : Stopped on Sat Jun 22 17:20:52 2024 (From start: Wall 0.651737s, CPU 0.764225s)