What about this:
from dolfin import *
import numpy as np
V = FunctionSpace(UnitSquareMesh(4,4, 'crossed'), 'DG', 0)
f = Expression("x[0]", degree=1)
u = project(f, V)
mf = MeshFunctionSizet(V.mesh(), 1, 0) # Default value for facets is set to 0
facet_ids = np.zeros(V.mesh().num_facets(), dtype=np.uintp)
facet_ids[::6] = 1 # <<---- Mark those you want
mf.set_values(facet_ids) # Mark your facet ids with 1
# Alternatively:
#mf.set_value(one_facet_id, 1)
assemble(avg(u)*dS(subdomain_data=mf, subdomain_id=1))
And (bonus! ;) in case you want to check what facets you are marking, you can use this:
import matplotlib.pyplot as pl
def plot_facets(mesh:Mesh, ids:list):
""" Plots the mesh and highlights the facets given by ids."""
plot(mesh, alpha=0.5)
coords = mesh.coordinates()
for fid in ids:
f = Facet(mesh, fid)
v1, v2 = f.entities(0)
cc = np.vstack(coords[[v1,v2]])
pl.plot(cc[:,0], cc[:,1], c='red')
plot_facets(V.mesh(), np.where(facet_ids == 1)[0])