# Extract the nodal values of a function on the boundary of the mesh

**URL:** <https://fenicsproject.discourse.group/t/extract-the-nodal-values-of-a-function-on-the-boundary-of-the-mesh/16833>\
**Category:** General\
**Created:** [January 24, 2025, 9:22am UTC](https://fenicsproject.discourse.group/t/extract-the-nodal-values-of-a-function-on-the-boundary-of-the-mesh/16833 "2025-01-24T09:22:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mekong](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mekong/32/7729_2.png) [@mekong](https://fenicsproject.discourse.group/u/mekong)\
**Post date:** [January 24, 2025, 9:22am UTC](https://fenicsproject.discourse.group/t/extract-the-nodal-values-of-a-function-on-the-boundary-of-the-mesh/16833/1 "2025-01-24T09:22:05Z")

</div>

Hello,  
I am using legacy Fenics. Given a function defined on a mesh, I would like to extract its values only on the points which belong to the mesh boundary. I have looked online for this, all I could find is either

- answers how to define a class which defines a boundary which is then used in `DirichletBC` to impose the BC on that boundary.
- [answers for Dolfinx](https://fenicsproject.discourse.group/t/extract-coordinates-at-the-mesh-boundary-and-evaluate-the-function-value/14286), see also [this](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/3)
- [Answers](https://fenicsproject.discourse.group/t/how-to-get-nemrical-solution-value-for-a-given-point/9822/2) which say how to get the function value on a point

Here is what I would like:

```auto
f = Function( Q )
#this gives the vector of nodal values
v = f.vector().get_local()

```

How may I prune from `v` the values which lie on `Q.mesh()`?

Thank you

---

<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:** [January 24, 2025, 10:05am UTC](https://fenicsproject.discourse.group/t/extract-the-nodal-values-of-a-function-on-the-boundary-of-the-mesh/16833/2 "2025-01-24T10:05:08Z")

</div>

See for instance: [Question on Dolfin poisson equation for source measurement on borders - #26 by dokken](https://fenicsproject.discourse.group/t/question-on-dolfin-poisson-equation-for-source-measurement-on-borders/13714/26)

---

<div class="post-metadata">

**Author:** ![mekong](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mekong/32/7729_2.png) [@mekong](https://fenicsproject.discourse.group/u/mekong)\
**Post date:** [January 24, 2025, 12:02pm UTC](https://fenicsproject.discourse.group/t/extract-the-nodal-values-of-a-function-on-the-boundary-of-the-mesh/16833/3 "2025-01-24T12:02:57Z")

</div>

Thank you. This solution works only for functions in space `FunctionSpace(mesh, 'CG', 1)`, not for functions in CG2:

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

mesh = UnitSquareMesh(4, 4)

V = FunctionSpace(mesh, 'CG', 2)
u = Function(V)
u.interpolate(Expression('x[0]+ 2*x[1]', degree=1))
vertex_function = MeshFunction("size_t", mesh, 0)

class BoundaryMarker(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary

BoundaryMarker().mark(vertex_function, 1)
boundary_vertices = np.asarray(vertex_function.where_equal(1))

v_to_d = vertex_to_dof_map(V)
dofs = v_to_d[boundary_vertices]

x = V.tabulate_dof_coordinates()
for dof in dofs:
    print(x[dof], u.vector()[dof])

```

```auto
$python3 solve.py
Traceback (most recent call last):

File "solve.py", line 21, in <module>

v_to_d = vertex_to_dof_map(V)

RuntimeError:
[...]
*** Error: Unable to tabulate dof to vertex map.
*** Reason: Can only tabulate dofs on vertices.
*** Where: This error was encountered inside DofMap.cpp.
[...]

```

Do you know how to make this work also for functions in other spaces ? I found [this](https://fenicsproject.discourse.group/t/problem-with-dof-to-vertex-map/1143/3) answer but then I don’t know how to do the equivalent of `vertex_to_dof_map` with the approach suggested in there.

Thank you

---

<div class="post-metadata">

**Author:** ![mekong](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mekong/32/7729_2.png) [@mekong](https://fenicsproject.discourse.group/u/mekong)\
**Post date:** [January 27, 2025, 2:45pm UTC](https://fenicsproject.discourse.group/t/extract-the-nodal-values-of-a-function-on-the-boundary-of-the-mesh/16833/4 "2025-01-27T14:45:00Z")

</div>

Following your link, I found a solution by using a dummy space of polynomials with one degree, here it is. This function returns the coordinates of the boundary points of a mesh, then the nodal values of a function (even if this function is defined on a polynomial space with degree larger than one) can be obtained by evaluating the function at these points.

```auto
def boundary_points(mesh):
    # create a dummy function space of degree 1 which will be used only to extract the boundary points
    Q_dummy = FunctionSpace( mesh, 'CG', 1 )

    vertex_to_degree_of_freedom_map = vertex_to_dof_map( Q_dummy )

    vertex_function = MeshFunction( "size_t", mesh, 0 )

    vertex_function.set_all( 0 )
    BoundaryMarker().mark( vertex_function, 1 )

    boundary_vertices = np.asarray( vertex_function.where_equal( 1 ) )

    degrees_of_freedom = vertex_to_degree_of_freedom_map[boundary_vertices]

    x = Q_dummy.tabulate_dof_coordinates()
    x = x[degrees_of_freedom]
    
    return x

```
