# Create SubDomain based on Function values

**URL:** <https://fenicsproject.discourse.group/t/create-subdomain-based-on-function-values/3628>\
**Category:** mesh\
**Created:** [June 24, 2020, 7:49am UTC](https://fenicsproject.discourse.group/t/create-subdomain-based-on-function-values/3628 "2020-06-24T07:49:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Brad](https://fenicsproject.discourse.group/u/Brad)\
**Post date:** [June 24, 2020, 7:49am UTC](https://fenicsproject.discourse.group/t/create-subdomain-based-on-function-values/3628/1 "2020-06-24T07:49:56Z")

</div>

Hello everyone,

My objective is to export a part of the mesh where a condition on the values of a function is valid.  
To do so, I tried to create a `SubMesh` based on a `SubDomain`. However I don’t know how to create a SubDomain based on a condition depending on the values of a `Function`. I know how to create a SubDomain based on explicit spatial coordinates, but how should I proceed if I don’t know explicitly the expression defining my SubDomain of interest?

Here is a minimal working example of what I am trying to achieve:

```auto
from dolfin import *

import numpy as np
import matplotlib.pyplot as plt

nx = ny = 16
mesh = RectangleMesh(Point(-2, -2), Point(2,2), nx, ny)
V = FunctionSpace(mesh, 'CG', 2)
R = 1

# Expression chosen arbitrarily for the example (in practice I don't know explicitly this expression)
u_expr = Expression('(pow(x[0],2) + pow(x[1],2) <= pow(R,2)) ? 1.0 : 0.0', degree=2,R=R)
u = interpolate(u_expr, V)

plot(u)
plot(mesh)

class RegionOfInterest(SubDomain):
    def inside(self,x,on_boundary):
        # How to return the points where u == 1.0 ? if we don't know explicitly the expression defining u
        # return x where u==1.0
        return (pow(x[0],2) + pow(x[1],2) <= pow(R,2)) # explicit expression
    
marker = MeshFunction("size_t", mesh, mesh.topology().dim(), 0) # cells marker
region = RegionOfInterest()
region.mark(marker, 1) # marking left region cells with 1

submesh_region = SubMesh(mesh, region)

pl, ax = plt.subplots(); fig = plt.gcf(); fig.set_size_inches(16, 4)
plt.subplot(1, 2, 1); p = plot(mesh)
plt.subplot(1, 2, 2); p = plot(submesh_region)

xdmf_filename = XDMFFile(MPI.comm_world, "RegionOfInterest.xdmf")
xdmf_filename.write(submesh_region)

```

Don’t hesitate to propose another approach if you have better ideas on how to export parts of the mesh where a condition on a Function is valid.

Thank you for your time

---

<div class="post-metadata">

**Author:** ![Brad](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Brad](https://fenicsproject.discourse.group/u/Brad)\
**Post date:** [June 26, 2020, 8:46am UTC](https://fenicsproject.discourse.group/t/create-subdomain-based-on-function-values/3628/2 "2020-06-26T08:46:32Z")

</div>

Do the points corresponding to `x` in the `inside` method for a `SubDomain`, correspond to the coordinates of the vertices/Nodes of the mesh or some other dof?

---

<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:** [June 26, 2020, 7:33pm UTC](https://fenicsproject.discourse.group/t/create-subdomain-based-on-function-values/3628/3 "2020-06-26T19:33:52Z")

</div>

In Paraview, there is a threshold filter, that will only show the domain in a specified range. I would strongly suggest using this capability, by saving your solution as a pvd or xdmf file.

---

<div class="post-metadata">

**Author:** ![Brad](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Brad](https://fenicsproject.discourse.group/u/Brad)\
**Post date:** [June 29, 2020, 7:22am UTC](https://fenicsproject.discourse.group/t/create-subdomain-based-on-function-values/3628/4 "2020-06-29T07:22:58Z")

</div>

Thanks a lot, I’ll have a look at it.  
It makes sense to do it while post-processing
