Efficient way to define BCs on subdomains?

Hello, I need to put Dirichlet BCs on subdomain boundaries, see the following example code and figure, the domain is simply broken down into 2 subdomains, I need to put a fixed BC of 0 on the the boundaries. Of course for this example, we could easily define 0 on the boundary of the square and on the straight line inside, but I needed a more efficient way for the cases where I have many subdomain defined. Any help is appreciated! Thank you!

from dolfin import *
from mshr import *
domain = Rectangle(Point(0., 0.), Point(1., 1.))

domain.set_subdomain(1, Polygon(  [Point(0., 0.), Point(0.8,0.), Point(0.2,1.), Point(0.,1.)]  ))
domain.set_subdomain(2, Polygon(  [Point(0.8,0.), Point(1., 0.), Point(1.,1.), Point(0.2,1.)]  ))
mesh = generate_mesh(domain, 20)
mf = MeshFunction("size_t", mesh, 2, mesh.domains())

import matplotlib.pyplot as plt
plot(mesh, "2D mesh")
plot(mf, "Subdomains")
plt.show()

I’m not super familiar with the capabilities of mshr - maybe this can be done in mshr as well, but for more complicated meshes I can highly recommend gmsh. In gmsh you can define bodies and boundaries (with gmsh.model.addPhysicalGroup), name them and import them into fenics. Then in fenics you have a MeshFunction with your subdomains and boundaries. You can then easily add your BCs.
Maybe this helps: