Create dirichlet boundary conditions from gmsh model

Hi Fenics Community!

I am trying to solve laplace equation in 3d using fenicsx. I want to know how to assign dirichlet boundary conditions to surfaces that I have defined as physical groups in gmsh.

This part of my code generates a simple “cube within a cube” model, where the inner cube (electrode) and outer cube (simulation domain) have different dirichlet boundary conditions:

# generate simple geometry with a cube within a cube
import gmsh

gmsh.model.add("cube within cube geometry")

sim_domain = gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1, 1)
electrode = gmsh.model.occ.addBox(0.25, 0.25, 0.25, 0.5, 0.5, 0.5, 2)

ov, _ = gmsh.model.occ.fragment([(3, 2)], [(3, 1)])


#get boundaries
electrode_bdry = gmsh.model.getBoundary([(3, 2)], combined=True, oriented=False, recursive=False)
sim_domain_bdry = gmsh.model.getBoundary([(3, 3)], combined=True, oriented=False, recursive=False)

# add physical groups
gmsh.model.addPhysicalGroup(3, [2], 2) # electrode
gmsh.model.addPhysicalGroup(3, [3], 3) # sim domain

gmsh.model.addPhysicalGroup(2, [surf[1] for surf in electrode_bdry], 4) # electrode surface
gmsh.model.addPhysicalGroup(2, [surf[1] for surf in sim_domain_bdry if surf not in electrode_bdry], 5) # sim surface


This generates the following mesh:

Then, I use the gmshio function to load the mesh from the gmsh model that I created above:

msh, mt, ft = io.gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0)

Now, I would like to locate the facets which are part of the inner cube surface and the facets that are part of the outer cube surface sort of like this pseudocode below:

electrode_physical_group_tag = 4
sim_physical_group_tag = 5
electrode_facets = locate_facets(electrode_physical_group_tag)
sim_facets = locate_facets(sim_physical_group_tag)

Then, I would be able to use these facets just like in the Poisson equation example in the dolfinx docs:

Could anyone give a few pointers on how to go about doing this?

Just use ft.find(4) and ft.find(5).
Alot of this is covered in
and other sections in the tutorial

1 Like

Wow, thanks for the fast response! This is exactly what I was looking for. Here is a screenshot of the solution that I was able to generate:

Take that, SIMION!