A problem about the function near()

Hello, everyone;
I am trying to write code for Circular cylinder. Thear are two ways to set boundary conditions.
This is my code:

from fenics import *
import numpy as np
import matplotlib.pyplot as plt
from mshr import *

#create mesh
domain1 = Rectangle(Point(0.0, 0.0), Point(10.0, 1.0))
domain2 = Circle(Point(0.5, 0.5), 0.25)
mesh = generate_mesh(domain1 - domain2, 100)
x = SpatialCoordinate(mesh)
#plot(mesh)

#define boundary
class Right(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[0], 10.0)

class Left(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[0], 0.0)

class Bottom(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[1], 0.0)
    
class Top(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[1], 1.0)

class Internal_Boundary(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near((x[0]-0.5)**2 + (x[1]-0.5)**2-0.25**2, 0)
        #return on_boundary and x[0]>0.2 and x[0]<0.8 and x[1]>0.2 and x[1] <0.8
bottom = Bottom()
top = Top()
left = Left()
right = Right()
internal_boundary = Internal_Boundary()
#boundary marks
bmf = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
bmf.set_all(0)
bottom.mark(bmf, 1)
right.mark(bmf, 2)
top.mark(bmf, 3)
left.mark(bmf, 4)
internal_boundary.mark(bmf, 5)
ds = Measure("ds", domain= mesh, subdomain_data= bmf)

print(assemble(1*ds(5)))

In this code,
I want to use

return on_boundary and near((x[0]-0.5)**2 + (x[1]-0.5)**2-0.25**2, 0)

or

return on_boundary and x[0]>0.2 and x[0]<0.8 and x[1]>0.2 and x[1] <0.8

to define the internal cricle boundary, but why the first seem to be true (Boundary Integral is pi/2) and the second way seem to be false(Boundary Integral is 0.).
Thanks!

This is because your mesh is very coarse, and thus the accuracy of the circle is reduced. Increasing the tolerance in near solves the problem:

from fenics import *
import numpy as np
import matplotlib.pyplot as plt
from mshr import *

#create mesh
domain1 = Rectangle(Point(0.0, 0.0), Point(10.0, 1.0))
domain2 = Circle(Point(0.5, 0.5), 0.25)
mesh = generate_mesh(domain1 - domain2, 100)

class Internal_Boundary(SubDomain):
    def inside(self, x, on_boundary):
        return near((x[0]-0.5)**2 + (x[1]-0.5)**2, 0.25**2, 1e-2) and on_boundary

internal_boundary = Internal_Boundary()
#boundary marks
bmf = MeshFunction("size_t", mesh, mesh.topology().dim()-1)

internal_boundary.mark(bmf, 5)
ds = Measure("ds", domain= mesh, subdomain_data= bmf)

print(assemble(1*ds(5)))
File("mesh.pvd")<< bmf