# How to define a subdomain using material data (from data file)?

**URL:** <https://fenicsproject.discourse.group/t/how-to-define-a-subdomain-using-material-data-from-data-file/11055>\
**Category:** General\
**Created:** [April 28, 2023, 9:34pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-subdomain-using-material-data-from-data-file/11055 "2023-04-28T21:34:04Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [May 5, 2023, 2:06pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-subdomain-using-material-data-from-data-file/11055/4 "2023-05-05T14:06:02Z")

</div>

> [@ABS](#):
>
> The first 3 columns (x,y,z) in my data file correspond to the cell index in the respective directions, and the 4th column denotes the material identity (1-\> material 1, 2-\> material 2, etc.).

First of all, does your mesh consist of triangles or quadrilaterals?  
Secondly, is your mesh uniform and structured (i.e. is it a `RectangleMesh` with 100 x 100 cells?)

I would suggest something like:

```python
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(100, 100)

V = FunctionSpace(mesh, "DG", 0)
u = Function(V)

dx = 1/100

values = np.arange(100*100)
local_values = np.zeros_like(u.vector().get_local())
for cell in cells(mesh):
    midpoint = cell.midpoint().array()
    i = int(midpoint[0] // dx)
    j = int(midpoint[1] // dx)
    pos = i + (j+1)
    local_values[cell.index()] = values[pos]
    print(midpoint, i, j , pos)

u.vector().set_local(local_values)

File("u.pvd") << u

```

---

_[View the full topic](https://fenicsproject.discourse.group/t/how-to-define-a-subdomain-using-material-data-from-data-file/11055)._
