How to define boundary conditions on an irregular geometry?

Hi again,
So you can convert your cell data to boundary data with the following functions.
By replacing the cellfunction i made in the code with the one you are loading into dolfin, and the set criteria with the corresponding value from your mesh file and you should be good to go

from dolfin import *

mesh = UnitCubeMesh(4,4,4)
mesh.init()
class top_half(SubDomain):
    def inside(self, x, on_boundary):
        return (x[0]<=0.5)
domains = MeshFunction("size_t", mesh, mesh.topology().dim())
domains.set_all(0)
top_half().mark(domains,1)


boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1)
boundaries.set_all(0)
cell_to_facet = mesh.topology()(mesh.topology().dim(), mesh.topology().dim()-1)
for cell in cells(mesh):
    for face in cell_to_facet(cell.index()):
        facet = Facet(mesh, face)
        if facet.exterior():
            if domains.array()[cell.index()]==1:
                boundaries.set_value(facet.index(), 1)

XDMFFile("bds.xdmf").write(boundaries)
1 Like