# Defining subdomains

**URL:** <https://fenicsproject.discourse.group/t/defining-subdomains/12173>\
**Category:** mesh\
**Created:** [September 11, 2023, 5:42am UTC](https://fenicsproject.discourse.group/t/defining-subdomains/12173 "2023-09-11T05:42:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muskan\_Pathak](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/muskan_pathak/32/5253_2.png) [@Muskan\_Pathak](https://fenicsproject.discourse.group/u/Muskan_Pathak)\
**Post date:** [September 11, 2023, 5:42am UTC](https://fenicsproject.discourse.group/t/defining-subdomains/12173/1 "2023-09-11T05:42:28Z")

</div>

I feel like I am going a bit crazy here. Pretty simple set-up, I have a unit square mesh, and I would like to define a circle subdomain. The code I’ve used is below.

```auto
from dolfin import *
import numpy as np

msh = UnitSquareMesh(500, 500)

def circle(x):
    return (x[0]-0.5)**2 + (x[1]-0.5)**2

class Circle(SubDomain):
    def inside(self, x, on_boundary):
        return near(circle(x) , 0.2**2)

domains = MeshFunction("size_t", msh, msh.topology().dim()-1, 0)

circ = Circle()
circ.mark(domains,1)

```

However, the circle is still marked 0 when I try and plot domains. Refining the mesh does not work and neither does increasing or decreasing the tolerance in the `near` function. I cannot think of any reason this does not work. If I try and set Circle to return `circle(x) > 0.2**2` or `circle(x) < 0.2**2`, it works perfects fine, so I really do not understand why this doesn’t work.

Any help would be fantastic.

---

<div class="post-metadata">

**Author:** ![Shubham\_Saurabh](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/shubham_saurabh/32/3460_2.png) [@Shubham\_Saurabh](https://fenicsproject.discourse.group/u/Shubham_Saurabh)\
**Post date:** [September 11, 2023, 6:35am UTC](https://fenicsproject.discourse.group/t/defining-subdomains/12173/2 "2023-09-11T06:35:56Z")

</div>

Hi,  
below code works perfectly fine for me.

```auto
from dolfin import *
import numpy as np

mesh = UnitSquareMesh(500, 500)
domains = MeshFunction("size_t", msh, msh.topology().dim()-1)
CompiledSubDomain("(x[0]-0.5)*(x[0]-0.5) + (x[1]-0.5)*(x[1]-0.5) < 0.2*0.2").mark(domains,1)

```

while using subdomain, you need to specify which area you want to mark, whether it is lesser than the radius or greater than the radius.

---

<div class="post-metadata">

**Author:** ![Muskan\_Pathak](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/muskan_pathak/32/5253_2.png) [@Muskan\_Pathak](https://fenicsproject.discourse.group/u/Muskan_Pathak)\
**Post date:** [September 12, 2023, 2:42am UTC](https://fenicsproject.discourse.group/t/defining-subdomains/12173/3 "2023-09-12T02:42:34Z")

</div>

I understand that, but I am trying to mark just the boundary of the circle, not the inside or outside of the circle, as I specified in my question.

---

<div class="post-metadata">

**Author:** ![Shubham\_Saurabh](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/shubham_saurabh/32/3460_2.png) [@Shubham\_Saurabh](https://fenicsproject.discourse.group/u/Shubham_Saurabh)\
**Post date:** [September 12, 2023, 5:50am UTC](https://fenicsproject.discourse.group/t/defining-subdomains/12173/4 "2023-09-12T05:50:32Z")

</div>

You can do this:

```auto
CompiledSubDomain("0.18*0.18 < (x[0]-0.5)*(x[0]-0.5) + (x[1]-0.5)*(x[1]-0.5) && (x[0]-0.5)*(x[0]-0.5) + (x[1]-0.5)*(x[1]-0.5) <0.2*0.2").mark(domains,1)

```

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/8b0850abb6345baa51668d3fe3d9e7bfea70e4eb.png)

and change the thickness of the boundary accordingly.
