Natural boundary conditions without FacetFunction

I would like to implement a natural boundary conditions (i. e. such that result in right side contributions via boundary integrals) that are different on different parts on the boundary. The whole contribution by the boundary conditions is given by sum like \int_{\Gamma_1} ( \vec f_1, \vec n) \mathrm{d}s + \int_{\Gamma_2} ( \vec f_2, \vec n) \mathrm{d}s
Originally, I used a piece of code like

# Initialize mesh function for boundary domains
boundaries = FacetFunction("size_t", mesh)
left.mark(boundaries, 1)
top.mark(boundaries, 2)
right.mark(boundaries, 3)
bottom.mark(boundaries, 4)

taken from the older fenics example to construct measures corresponding to \Gamma_i. This approach is unfortunately not working in my installation of 2018.1.0 as FacetFunction is no longer available. I would be grateful for any advice on the preffered reimplementation in the current version.

Indeed, FacetFunction is no longer available. You need to use MeshFunction instead, with an additional parameter to specify the topological dimension (mesh.topology().dim()-1 for the facets).
In your code, the two first lines could then be replaced by :
boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)


Thank you! Your recommendation does the job. I would mark the topic as solved but I do not see how.


thank you for your answer.
I read the MeshFunction documentation
(, but I still don’t understand, what is the meaning of the last argument 0 ?

I would like to use CellFunction(‘size_t’, 3Dmesh), but I’m not sure, if this is the correct substitution:
MeshFunction("size_t", 3Dmesh, 3Dmesh.topology().dim()-2, 0)

Thank you.


First, it seems that the documentation you have been reading is quite old, I would recommend reading a more recent version.

The last argument (0 in your case), is an optional argument initializing the MeshFunction with the given value.

If I understand correctly, you would like to use the equivalent of CellFunction. Then I think the dimension you give to MeshFunction is not correct. Indeed, 3Dmesh.topology().dim()-2 will create a MeshFunction relative to the edges and not the cells.
The equivalent of CellFunction would be

MeshFunction("size_t", 3Dmesh, 3Dmesh.topology().dim(), 0)

Hi cdaversin,

thank you for your explanation. The code works. The MeshFunction return the cells index/id.

Is this new documentation what you mean? ( The MeshFunction in this document is a bit different.


The Python function isn’t very well documented in the later versions. This is the source code:
As you can see, its quite dense, as its there to wrap all the different types: size_t, double, int and bool. However, the line def __new__(cls, value_type, mesh, dim, value=None): gives an idea of what is required.


This is useful information if anyone is trying to run the tutorial ('vol.1") demos and wants to fix demo#10. Line 223 needs to have
boundary_markers = FacetFunction('size_t', mesh)
replaced with
boundary_markers = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0)