Question about locate_entities_boundary

When reading the codes in Fenicsx tutorial about heat equation, I have a question about the following codes:

boundary_facets=mesh.locate_entities_boundary(
    domain,fdim,lambda x:np.full(x.shape[1],True,dtype=bool))

What does ‘x.shape[1]’ mean? When implementing the function ‘locate_entities_boundary’ , where does ‘x’ come from?Is it from ‘tabulate_dof_coordinates()’?Could you mind explaining what the ‘lambda’ function does in ‘locate_entities_boundary’ ?Thanks in advance!

The third input argument to locate_entities_boundary is a python function, that takes in coordinates
x, of shape (3, num_points), such that x[0] are all points in the x-coordinate, x[1] all points in y-coordinate etc.
locate_entities_boundary works on the computational domain (domain) in your case. It will find all entities of dimension fdim (facets in your case), and check if the vertices satisfy this criterion.
A lambda function is just a compact way of packing

def check_coordinates(x):
    return np.full(x.shape[1], True, dtype=bool))

boundary_facets=mesh.locate_entities_boundary(
    domain,fdim,check_coordinates)

In this case, since you want all facets on the boundary, we say that as long as the facet is on the boundary, it’s vertex satisfy the criterion (i.e. we return an array full of True, for all points).

You could also use

boundary_facets = dolfinx.mesh.exterior_facet_indices(domain.topology)

as done in A known analytical solution — FEniCSx tutorial

1 Like

Thank you very much.I still have some questions :‘locate_entities_boundary’ will only look for facets which are already on boundary ,or it will look for all facets including ones inside domain?And ‘x’ is stored in (3,num_points) form, where num_points for all mesh points or dof points?Who supplies ‘x’ for the funtion locate_entities_boundary? Thanks in advance!

There is another function for looking at all facets (including interior ones, namely dolfinx.mesh.locate_entities.

locate_entities_boundary uses exterior_facet_indices to get the facets. Then for each of the facets; it finds the vertices, and checks if all vertices satisfies the condition in the Python function.

Sorry for another question: this np.full() function return an narray full of true but locate_entities_boundary need a marker function which returns a True or False? Do I misunderstand anything?

In general it needs a function that returns an array of booleans, whose shape is the same as the second axis of the input.
See for instance: Hyperelasticity — FEniCSx tutorial
on how to mark parts of a boundary using numpy.isclose

Oh,I just look for the document which says that in locate_entities_boundary, the marker function returns an array of boolean value of size num_points, which is True if it is on the boundary.So np.full() returns what it wants.

Hello dokken, I have another question about it.Since x[0] and x[1] are just different in x or y coordinates but with the same number of points, I replace x.shape[1] with x.shape[0] but this time the code throws an error : Length of array of markers is wrong.So what is the reason here?

The shape of x is (3, number of points). Thus x.shape[0] will return 3. not len(x[0]),.
len(x[0])=len(x[1])=number of points.

I see.Thank you very much.