Define subdomain class

Hi I learnt how to define subdomains with classes as shown in other topics

class S0(SubDomain):
\quad def inside(self, x, on_boundary):
\quad\quad return (near_line(0,x) and not(near_line(1,x)))
class S1(SubDomain):
\quad def inside(self, x, on_boundary):
\quad\quad return (near_line(1,x) and not(near_line(2,x)))

and

s0 = S0()
s1 = S1()

This method works, but the issue is I have like 10+ subdomains so I do not want to define 10+ classes as what people tend to do in their cases (e.g. 4 classes for 4 subdomains, which is not a lot).

I wonder if it is possible to define 1 class instead. I tried

class S(SubDomain):
\quad def init(self, n):
\quad\quad self.P1 = domain_vertices[n]
\quad\quad self.P2 = domain_vertices[(n+1)%12]

and

s0 = S(0)
s1 = S(1)

However this causes dead kernel and I am not sure why. I wonder if this method is possible or I have to define 10+ classes. Thanks!

Please supply a minimal example that shows how the structure of your subdomains (where they are located and how your mesh looks like). In many cases, one can benefit from defining the subdomains during mesh generation, and load them as a MeshFunction.

So I have this star inclusion in the fluid. The subdomains are edges of the star and basically I want to find the stress on it. The mesh is generated by

inclusion = Polygon(domain_vertices)
mesh = generate_mesh(Rectangle(Point(0, 0), Point(20, 20))-inclusion, 100)

I would strongly recommend not using mshr, as it is no longer maintained. The most common mesh generator to use is gmsh, which can be loaded with meshio as described in Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio.