Import gmsh to FEniCSx, then define two Dirichlet boundary condition

Hello everyone,

I has issue about importing a .msh file and define multiple Dirichlet boundary conditions.

I am folling this tutorial: An introduction to finite element modeling · TuxRiders. This Youtube video is recorded 2023 and use Fenics, some bit out of date.

My set up: FEniCSx 0.7.3. Installed through Ubuntu package.

The mesh is generated in SALOME and has saved as .msh file successfully.

Then I import mesh like this:

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[:, :3] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={"name_to_read": [cell_data.astype(np.int32)]})
    return out_mesh
# mesh file is helix1.msh
mesh, cell_markers, facet_markers = gmshio.read_from_msh("helix2.msh", MPI.COMM_WORLD, gdim=3)

Info    : Reading 'helix2.msh'...
Info    : 7982 nodes
Info    : 39006 elements
Info    : Done reading 'helix2.msh'

## cell type: tetrahedron and triangle is choosen in Salome, which step before generate .mesh file.
mesh = meshio.read("helix2.msh")
meshio.write("helix.xdmf", create_mesh(mesh, "tetra", True))

volume_mesh = create_mesh(mesh, "tetra", prune_z=True)
face_mesh = create_mesh(mesh, "triangle", prune_z=True)
meshio.write("mesh.xdmf", volume_mesh)
meshio.write("mt.xdmf", face_mesh)

with XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "r") as xdmf:
    mesh = xdmf.read_mesh(name="Grid")
    cd = xdmf.read_meshtags(mesh, name="Grid")    # volume mesh
mesh.topology.create_connectivity(mesh.topology.dim, mesh.topology.dim - 1)
with XDMFFile(MPI.COMM_WORLD, "mt.xdmf", "r") as xdmf:
    fd = xdmf.read_meshtags(mesh, name="Grid")    # facet element

# Define boundary and initial conditions

# bottom_cells = ct.find(bottom_narker),  
# bottom_marker is 2 or 3 whaich is defined in gmsh
# these information from tutorials: Defining subdomains for different materials

inlet_cells = fd.find(2)
out_cells = fd.find(3)

inlet_dofs = locate_dofs_topological(V, mesh.topology.dim - 1, inlet_cells)
bc1 = dirichletbc(default_scalar_type(1), inlet_dofs, V)

# https://jsdokken.com/dolfinx-tutorial/chapter3/component_bc.html

out_dofs = locate_dofs_topological(V, mesh.topology.dim - 1, out_cells)
bc2 = dirichletbc(default_scalar_type(1), out_dofs, V)

bc = [bc1, bc2]

I want to make value in bc1 is constant 20 and bc2 is constant -20.

Thanks for help!

As you haven’t provided the mesh, and what isn’t working in your code.

Simpy change the definition of bc1 and bc2 to

bc1 = dirichletbc(default_scalar_type(20), inlet_dofs, V)
bc2 = dirichletbc(default_scalar_type(-20), out_dofs, V)

Thanks for fast reply, it works.