Find displacement of displacement field on node closest to given location

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?


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?

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

This is fine.

However

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 (API at: API reference — scifem)

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.