# A problem about the function near()

**URL:** <https://fenicsproject.discourse.group/t/a-problem-about-the-function-near/11009>\
**Category:** General\
**Created:** [April 22, 2023, 1:31pm UTC](https://fenicsproject.discourse.group/t/a-problem-about-the-function-near/11009 "2023-04-22T13:31:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaohe](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/xiaohe/32/7777_2.png) [@xiaohe](https://fenicsproject.discourse.group/u/xiaohe)\
**Post date:** [April 22, 2023, 1:31pm UTC](https://fenicsproject.discourse.group/t/a-problem-about-the-function-near/11009/1 "2023-04-22T13:31:31Z")

</div>

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

```auto
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

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

```

or

```auto
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!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 24, 2023, 8:18am UTC](https://fenicsproject.discourse.group/t/a-problem-about-the-function-near/11009/2 "2023-04-24T08:18:25Z")

</div>

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:

```python
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

```
