# Extract DoF indices along a line

**URL:** <https://fenicsproject.discourse.group/t/extract-dof-indices-along-a-line/2286>\
**Category:** Uncategorized\
**Created:** [January 24, 2020, 2:38pm UTC](https://fenicsproject.discourse.group/t/extract-dof-indices-along-a-line/2286 "2020-01-24T14:38:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdiercks](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/pdiercks/32/225_2.png) [@pdiercks](https://fenicsproject.discourse.group/u/pdiercks)\
**Post date:** [January 24, 2020, 2:38pm UTC](https://fenicsproject.discourse.group/t/extract-dof-indices-along-a-line/2286/1 "2020-01-24T14:38:33Z")

</div>

Hello All,

please consider the following to extract DoF indices for a certain line

```python
import dolfin as df
from itertools import chain

mesh = df.UnitSquareMesh(5, 5)
V = df.VectorFunctionSpace(mesh, "CG", 1)
u = df.interpolate(df.Expression(("x[0]", "x[1]"), degree=1), V)

LineDomain = df.CompiledSubDomain('near(x[0], x[1])')
line_function = df.MeshFunction('size_t', mesh, mesh.topology().dim() - 1, 0)
LineDomain.mark(line_function, 1)

# list of unique vertices of above line
vertices = list(set(sum((l.entities(0).tolist() for l in df.SubsetIterator(line_function, 1)), [])))

# vertex to dof map
v2d = df.vertex_to_dof_map(V)
# list comprehension with two results: dof_x, dof_y
line_dofs = list(chain.from_iterable((v2d[2 * vi], v2d[2 * vi + 1]) for vi in vertices))

# will get values of u in the order of vertices
uvec = u.vector()[:]
res = uvec[line_dofs]

```

How can I get the DoFs associated with the above line, if I have defined

```python
V = df.VectorFunctionSpace(mesh, "CG", 2)

```

?

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [January 24, 2020, 4:17pm UTC](https://fenicsproject.discourse.group/t/extract-dof-indices-along-a-line/2286/2 "2020-01-24T16:17:46Z")

</div>

Don’t use `vertex_to_dof` and get the DoFs from `V.dofmap()` based on the topology of the facets.

---

<div class="post-metadata">

**Author:** ![pdiercks](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/pdiercks/32/225_2.png) [@pdiercks](https://fenicsproject.discourse.group/u/pdiercks)\
**Post date:** [January 24, 2020, 4:22pm UTC](https://fenicsproject.discourse.group/t/extract-dof-indices-along-a-line/2286/3 "2020-01-24T16:22:58Z")

</div>

Thank you for your answer. The following seems to work:

```python
vertices = list(set(sum((l.entities(0).tolist() for l in df.SubsetIterator(line_function, 1)), [])))
edges = list(set([l.index() for l in df.SubsetIterator(line_function, 1)]))

dofmap = V.dofmap()
edge_dofs = dofmap.entity_dofs(mesh, 1, edges)
vertex_dofs = dofmap.entity_dofs(mesh, 0, vertices)
line_dofs = edge_dofs + vertex_dofs

```
