This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to get a nodal value for a specific node

+3 votes

Hello
I have a rectangular domain an a vectorial VectorFunctionSpace. The unknown I am trying to find is displacement which is a vector. I want to know how I can find the value of the displacement of a specific node located near a coordinate for example (1.5 ,0.5). Do you know how I can find it? I know there might not be a node located exactly on that coordinate but there should be a way to find the displacement very near to (1.5, 0.5).
Here is a part of my code:

#Vectorial function space
V= VectorFunctionSpace(mesh, 'CG', 1)
#Domain
domain = mshr.Rectangle(Point(0.0, 0.0), Point(2.0, 2.0))
#Mesh
mesh   = mshr.generate_mesh(domain, 100)
.
.
.
#Solve
solve(A, u.vector(), b)

Thanks in advance!

asked Apr 8, 2016 by jafar FEniCS Novice (670 points)

1 Answer

+2 votes
 
Best answer

Hi, consider

from dolfin import *

mesh = UnitSquareMesh(3, 3)
V = FunctionSpace(mesh, 'CG', 1)

# Find cells that collide with the point
p = Point(0.42, 0.1234)
tree = mesh.bounding_box_tree()
cells_p = tree.compute_collisions(p)

# Among dofs of those cells find one whose coordinate is closest to p
min_dist, dof = 2, -1
dofmap = V.dofmap()
for cell in cells_p:
   dofs = dofmap.cell_dofs(cell)
   dofs_x = dofmap.tabulate_coordinates(Cell(mesh, cell))
   distances = [p.distance(q) for q in map(Point, dofs_x)]
   dist = min(distances)
   if dist < min_dist:
       min_dist = dist
       dof = dofs[distances.index(min_dist)] 

# Check that the dof is found correctly
f = interpolate(Expression('sqrt(pow(x[0]-x0, 2)+pow(x[1]-x1, 2))',
                            x0=p[0], x1=p[1]),
                V).vector().array()
print 'Min @ dof', dof, 'with error', abs(min_dist-f[dof])
answered Apr 8, 2016 by MiroK FEniCS Expert (80,920 points)
selected Apr 8, 2016 by jafar

Thank you for your response.
What I am looking for is just the displacement values of a node in x and y directions (considering u is a vector).
Assuming we have a rectangle with dimensions [1,1] and we want to find the value of the displacement at the center of the plate, I just added the below line after solving the problem:

print u(0.5,0.5)

And I got :

[  3.45802200e-15   7.56784216e-16]

I just wanna make sure if there is the correct values of the displacement in x and y directions at the center of the plate. Could you please confirm that?
Once thanks again

Yes, that is correct. I read your question in a weird way, sorry about that.

How would you update this answer given that the function tabulate_coordinates does no longer exist and its replacement tabulate_dof_coordinates doesn't apply to cells, but only to functionspaces?

I have the same problem, given the coordinates of a point P(xp,yp) I would like to find its closest vertex in the mesh.

...