Extract DoF indices along a line

Hello All,

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

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

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

?

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

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

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
1 Like