# Locate\_entities returning incorrect points with MPI

**URL:** <https://fenicsproject.discourse.group/t/locate-entities-returning-incorrect-points-with-mpi/15853>\
**Category:** dolfinx\
**Tags:** mesh, mpi\
**Created:** [September 21, 2024, 6:10pm UTC](https://fenicsproject.discourse.group/t/locate-entities-returning-incorrect-points-with-mpi/15853 "2024-09-21T18:10:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbrown](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dbrown/32/4755_2.png) [@dbrown](https://fenicsproject.discourse.group/u/dbrown)\
**Post date:** [September 21, 2024, 6:10pm UTC](https://fenicsproject.discourse.group/t/locate-entities-returning-incorrect-points-with-mpi/15853/1 "2024-09-21T18:10:26Z")

</div>

Hi all, I’m using dolfinx 0.8.0 with Python 3.11 and trying to locate all the vertices on a boundary. For a single process this code works ok, but as soon as I use 2 or more I find that `locate_entities` returns vertices that clearly don’t satisfy the marker function:

```auto
import numpy as np
from mpi4py import MPI
from dolfinx.mesh import create_unit_cube, locate_entities

comm = MPI.COMM_WORLD
mesh = create_unit_cube(comm, 5, 5, 5)
idx = locate_entities(mesh, 0, lambda x: np.isclose(x[2], 0))
coords = mesh.geometry.x[idx]

assert np.allclose(coords[:, 2], 0, atol=1e-6), f"rank={comm.rank}: Boundary error: {coords[:, 2]}"

```

I run this using `mpirun -n 2 python error.py` and it returns something like:

```auto
    assert np.allclose(coords[:, 2], 0, atol=1e-6), f"rank={comm.rank}: Boundary error: {coords[:, 2]}"
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: rank=0: Boundary error: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0.8 1. 0.6 0.8 0. 0. 0. 0.2]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: rank=1: Boundary error: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.2 0.4 0.2 0.2]

```

`mpirun -n 1 python error.py` runs fine. I assume I am doing something wrong in terms of selecting vertices across different processes?

---

<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:** [September 21, 2024, 9:08pm UTC](https://fenicsproject.discourse.group/t/locate-entities-returning-incorrect-points-with-mpi/15853/2 "2024-09-21T21:08:13Z")

</div>

You are mixing the indices of topology (vertices, edges,facets and cells) with geometry (the physical nodes making up the grid).

To map from vertices to geometry nodes (those located in mesh.geometry.x) use  
`entities_to_geometry`  
[https://docs.fenicsproject.org/dolfinx/v0.8.0/python/generated/dolfinx.mesh.html#dolfinx.mesh.entities\_to\_geometry](https://docs.fenicsproject.org/dolfinx/v0.8.0/python/generated/dolfinx.mesh.html#dolfinx.mesh.entities_to_geometry)  
that can map the indices given in `idx` to indices in mesh.geometry.x

---

<div class="post-metadata">

**Author:** ![dbrown](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dbrown/32/4755_2.png) [@dbrown](https://fenicsproject.discourse.group/u/dbrown)\
**Post date:** [September 21, 2024, 9:36pm UTC](https://fenicsproject.discourse.group/t/locate-entities-returning-incorrect-points-with-mpi/15853/3 "2024-09-21T21:36:17Z")

</div>

I see thanks, that makes sense! For those that come across this later this now works:

```auto
import numpy as np
from mpi4py import MPI
from dolfinx.mesh import create_unit_cube, locate_entities, entities_to_geometry

comm = MPI.COMM_WORLD
mesh = create_unit_cube(comm, 5, 5, 5)
entity_idx = locate_entities(mesh, 0, lambda x: np.isclose(x[2], 0))
idx = entities_to_geometry(mesh, 0, entity_idx)
coords = mesh.geometry.x[idx[:, 0]]

assert np.allclose(coords[:, 2], 0, atol=1e-6), f"rank={comm.rank}: coordinates: {coords[:, 2]}"

```

Also, is there a particular reason that `entities_to_geometry` returns an `(N, 1)` shaped array of indices rather than `(N,)`?

`mesh.geometry.x[idx]` still works but returns a `(N,1,3)` shaped array of coordinates which seemed odd.

---

<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:** [September 22, 2024, 4:15am UTC](https://fenicsproject.discourse.group/t/locate-entities-returning-incorrect-points-with-mpi/15853/4 "2024-09-22T04:15:53Z")

</div>

Since entities can be edges, facets or cells, it would return (num entities, num nodes per entity). Since your case is a vertex, there is only one node (1-1 map).  
I would call:

```python
idx = entities_to_geometry(mesh, 0, entity_idx).flatten()

```

Rather than doing it after

> [@dbrown](#):
>
> ```auto
> coords = mesh.geometry.x[idx[:, 0]]
> 
> ```
