# Find displacement of displacement field on node closest to given location

**URL:** <https://fenicsproject.discourse.group/t/find-displacement-of-displacement-field-on-node-closest-to-given-location/19709>\
**Category:** General\
**Created:** [May 19, 2026, 2:01am UTC](https://fenicsproject.discourse.group/t/find-displacement-of-displacement-field-on-node-closest-to-given-location/19709 "2026-05-19T02:01:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex24](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@alex24](https://fenicsproject.discourse.group/u/alex24)\
**Post date:** [May 19, 2026, 2:01am UTC](https://fenicsproject.discourse.group/t/find-displacement-of-displacement-field-on-node-closest-to-given-location/19709/1 "2026-05-19T02:01:25Z")

</div>

Hi,

After solving a 3D linear elasticity problem, I would like to find the displacement at a node closest to a given location, is the following code correct?

```auto

dof_coords = Vp.tabulate_dof_coordinates()
distances = np.linalg.norm(dof_coords - grasp_node, axis=1)
dof_index = np.argmin(distances)
u_node = np.sqrt(uh.x.array[3*dof_index]**2 + uh.x.array[3*dof_index+1]**2 + uh.x.array[3*dof_index+2]**2)

```

I checked with the following example and it seems to work. One question I have is, why does V.tabulate\_dof\_coordinates() give 3D points with z values set to zero?

```auto
import numpy as np
import dolfinx
from mpi4py import MPI

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = dolfinx.fem.functionspace(mesh, (“Lagrange”, 1, (mesh.geometry.dim,)))

u = dolfinx.fem.Function(V)
u.interpolate(lambda x: np.vstack((x[0], x[1]))) # Populating with sample data

target_coords = np.array([0.5, 0.5])

dof_coords = V.tabulate_dof_coordinates()

distances = np.linalg.norm(dof_coords[:,0:2] - target_coords, axis=1)
dof_index = np.argmin(distances)

closest_node_magnitude = np.sqrt(u.x.array[2*dof_index]**2 + u.x.array[2*dof_index+1]**2)

print(closest_node_magnitude)

```

Thank you,

Alex

---

<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 19, 2026, 8:54am UTC](https://fenicsproject.discourse.group/t/find-displacement-of-displacement-field-on-node-closest-to-given-location/19709/2 "2026-05-19T08:54:01Z")

</div>

> [@alex24](#):
>
> ```auto
> dof_coords = Vp.tabulate_dof_coordinates()
> distances = np.linalg.norm(dof_coords - grasp_node, axis=1)
> 
> ```

This is fine.

However

> [@alex24](#):
>
> `u_node = np.sqrt(uh.x.array[3*dof_index]**2 + uh.x.array[3*dof_index+1]**2 + uh.x.array[3*dof_index+2]**2)`

this is not correct for your 2D grid, as `uh` does not store x,y and z displacements.

Is there a reason you want to know the value at the closest vertex, and not at the actual point you are querying for? If you want the value at the actual point, you can use `scifem.evaluate_function`: [scifem/examples/evaluate\_function.py at main · scientificcomputing/scifem · GitHub](https://github.com/scientificcomputing/scifem/blob/main/examples/evaluate_function.py#L50) (API at: [API reference — scifem](https://scientificcomputing.github.io/scifem/docs/api.html#scifem.evaluate_function))

> [@alex24](#):
>
> One question I have is, why does V.tabulate\_dof\_coordinates() give 3D points with z values set to zero?

This is a choice to simplify the internal handling in the library of dof coordinates.  
Most collision detection algorithms require 3D coordinates. Furthermore, having a fixed second axis for the coordinates also makes the internal logic simpler.
