I have a fairly simple question: I want to measure the value of my function (u) at a certain point, let’s say (0.5, 0.5). How do I approach this? There are two cases, either the mesh intersects the probe point, or it does not, and I would like the solution to be compatible with both cases.
I have tried to use compute_closest_entity, but this gives me indices that are not in u.
Could you please provide the attempted solution, as you have not added any code about evaluating at points to this example?
Please also note that it would be sufficient to use the following lines to illustrate your issue:
from mpi4py import MPI
from dolfinx import mesh
domain = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8, mesh.CellType.quadrilateral)
from dolfinx.fem import FunctionSpace
V = FunctionSpace(domain, ("CG", 1))
from dolfinx import fem
uD = fem.Function(V)
uD.interpolate(lambda x: 1 + x[0]**2 + 2 * x[1]**2)
# Add code here to evaluate uD at a given point (say 0.5, 0.5)
# ...
Thank you for the quick response! Would you say using compute_closest_entity is the recommended method for doing this?
This is a MWE with my attempt at using compute_closest_entity.
from mpi4py import MPI
from dolfinx import mesh
import numpy as np
from dolfinx.geometry import (BoundingBoxTree, compute_closest_entity,
create_midpoint_tree)
from pdb import set_trace
domain = mesh.create_unit_square(MPI.COMM_WORLD, 80, 80, mesh.CellType.quadrilateral)
from dolfinx.fem import FunctionSpace
V = FunctionSpace(domain, ("CG", 1))
from dolfinx import fem
uD = fem.Function(V)
uD.interpolate(lambda x: 1 + x[0])
dim = domain.topology.dim
# from https://github.com/FEniCS/dolfinx/blob/a5b84c10e8ccc6d236f780d3a0486eb634d048c0/python/test/unit/geometry/test_bounding_box_tree.py#L277
points = np.array([1.0, -1.0])
domain.topology.create_entities(dim)
# set_trace()
tree = BoundingBoxTree(domain, dim)
num_entities_local = domain.topology.index_map(dim).size_local + domain.topology.index_map(dim).num_ghosts
entities = np.arange(num_entities_local, dtype=np.int32)
midpoint_tree = create_midpoint_tree(domain, dim, entities)
closest_entities = compute_closest_entity(tree, midpoint_tree, domain, points)
# get value at closest_entity index
value = uD.x.array[closest_entities]
print(f" value at {points}: {value}")
The expected value at the evaluated point is x[0]+1.0=2.0, but the output is: value at [ 1. -1.]: [1.0125]
I can imagine there are simpler methods to measure values at a point. The tutorial shows many ways to plot the values in pyvista, which works for me, but I would like to create a graph of the change of u over time for some points in my simulation.
Thanks again for the help.