# Extract the coordinates of the boundary points and the value of a function on them

**URL:** https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260
**Category:** dolfinx
**Created:** [May 25, 2023, 1:43am UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260 "2023-05-25T01:43:21Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Karlosjusus](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karlosjusus](https://fenicsproject.discourse.group/u/Karlosjusus)
#### Post date: [May 25, 2023, 1:43am UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/1 "2023-05-25T01:43:21Z")

</div>

Hi everyone!  
I want to extract the coordinates of the boundary points and the value of a function on them in FEniCSx.

I need the values on all boundary.

Does anyone know how I can do it?

Thank you so much.

---

<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: [May 25, 2023, 3:25am UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/2 "2023-05-25T03:25:28Z")

</div>

> [@Karlosjusus](#):
>
> I want to extract the coordinates of the boundary points and the value of a function on them in FEniCSx.

Are you here talking about the mesh vertices, or the degrees of freedom?

What function space are you considering?

---

<div class="post-metadata">

### Author: ![Karlosjusus](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karlosjusus](https://fenicsproject.discourse.group/u/Karlosjusus)
#### Post date: [May 25, 2023, 3:06pm UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/3 "2023-05-25T15:06:53Z")

</div>

```auto
v_cg1 = VectorElement("CG", domain.ufl_cell(), 1)
V = dolfinx.fem.FunctionSpace(domain, v_cg1)
f = Function(V)

```

f is a function null in the domain but with values on the boundary. My goal is to deform the boundary of the mesh according to this function.

---

<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: [May 25, 2023, 3:40pm UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/4 "2023-05-25T15:40:32Z")

</div>

Consider the following:

```python
from mpi4py import MPI
import dolfinx
import numpy as np
import ufl
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 16, 16)
v_cg1 = ufl.VectorElement("CG", mesh.ufl_cell(), 1)
V = dolfinx.fem.FunctionSpace(mesh, v_cg1)
f = dolfinx.fem.Function(V)
f.interpolate(lambda x: (0.1*x[1]*abs(x[0]), 0.05*np.sin(2*x[0])))

mesh.topology.create_connectivity(mesh.topology.dim-1, mesh.topology.dim)
boundary_facets = dolfinx.mesh.exterior_facet_indices(mesh.topology)
boundary_vertices = dolfinx.mesh.compute_incident_entities(mesh.topology, boundary_facets, mesh.topology.dim-1, 0)
vertex_to_geometry = dolfinx.cpp.mesh.entities_to_geometry(mesh._cpp_object, 0, boundary_vertices, False)

c_to_v = mesh.topology.connectivity(mesh.topology.dim, 0)
mesh.topology.create_connectivity(0, mesh.topology.dim)
v_to_c = mesh.topology.connectivity(0, mesh.topology.dim)

num_dofs = V.dofmap.index_map.size_local+ V.dofmap.index_map.num_ghosts
dof_to_geometry_map = np.full(num_dofs, -1, dtype=np.int32)
dofmap = V.dofmap
layout = dofmap.dof_layout

for i, (vertex, node) in enumerate(zip(boundary_vertices, vertex_to_geometry)):
    cell = v_to_c.links(vertex)[0]
    cell_dofs = dofmap.cell_dofs(cell)
    cvs = c_to_v.links(cell)
    local_index = np.flatnonzero(cvs == vertex)[0]
    dof = cell_dofs[layout.entity_dofs(0, local_index)]
    dof_to_geometry_map[dof] = node

# Create boundary perturbation vector
bs = V.dofmap.bs
perturbation_data = np.zeros((len(boundary_vertices), 3), dtype=np.float64)
geom = np.zeros(len(boundary_vertices), dtype=np.int32)
c = 0
for i, node in enumerate(dof_to_geometry_map):
    if node != -1:
        perturbation_data[c, :bs] = f.x.array[bs*i:bs*(i+1)]
        geom[c] = node
        c+=1
mesh.geometry.x[geom]+= perturbation_data
with dolfinx.io.XDMFFile(mesh.comm, "mesh.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
```

---

<div class="post-metadata">

### Author: ![Karlosjusus](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karlosjusus](https://fenicsproject.discourse.group/u/Karlosjusus)
#### Post date: [May 28, 2023, 2:15am UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/5 "2023-05-28T02:15:06Z")

</div>

I tried it, but I get the next error:

```auto
TypeError Traceback (most recent call last)
Cell In[19], line 4
      2 mesh.topology.create_connectivity(mesh.topology.dim-1, mesh.topology.dim)
      3 boundary_facets = dolfinx.mesh.exterior_facet_indices(mesh.topology)
----> 4 boundary_vertices = dolfinx.mesh.compute_incident_entities(mesh.topology, boundary_facets, mesh.topology.dim-1, 0)
      5 vertex_to_geometry = dolfinx.cpp.mesh.entities_to_geometry(mesh._cpp_object, 0, boundary_vertices, False)
      8 c_to_v = mesh.topology.connectivity(mesh.topology.dim, 0)

TypeError: compute_incident_entities(): incompatible function arguments. The following argument types are supported:
    1. (mesh: dolfinx.cpp.mesh.Mesh, entities: numpy.ndarray[numpy.int32], d0: int, d1: int) -> numpy.ndarray[numpy.int32]

Invoked with: <dolfinx.cpp.mesh.Topology object at 0x7f81e628dc60>, array([1, 3, 8, ..., 1384673, 1384676, 1384678],
      dtype=int32), 1, 0

```

if I replace mesh.topology by mesh, I get the next error:

```auto
AttributeError Traceback (most recent call last)
Cell In[20], line 5
      3 boundary_facets = dolfinx.mesh.exterior_facet_indices(mesh.topology)
      4 boundary_vertices = dolfinx.mesh.compute_incident_entities(mesh, boundary_facets, mesh.topology.dim-1, 0)
----> 5 vertex_to_geometry = dolfinx.cpp.mesh.entities_to_geometry(mesh._cpp_object, 0, boundary_vertices, False)
      8 c_to_v = mesh.topology.connectivity(mesh.topology.dim, 0)
      9 mesh.topology.create_connectivity(0, mesh.topology.dim)

AttributeError: 'Mesh' object has no attribute '_cpp_object'

```

---

<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: [May 28, 2023, 4:06am UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/6 "2023-05-28T04:06:45Z")

</div>

What version of dolfinx are you running?

---

<div class="post-metadata">

### Author: ![Karlosjusus](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karlosjusus](https://fenicsproject.discourse.group/u/Karlosjusus)
#### Post date: [May 28, 2023, 2:39pm UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/7 "2023-05-28T14:39:18Z")

</div>

I’m ruuning 0.5.2 version.

---

<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: [May 28, 2023, 3:06pm UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/8 "2023-05-28T15:06:51Z")

</div>

See: [dolfinx/test\_refinement.py at v0.5.2 · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/blob/v0.5.2/python/test/unit/mesh/test_refinement.py#L114) for usage (Im not at my computer, so I cannot run and adapt my code above).

---

<div class="post-metadata">

### Author: ![Karlosjusus](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karlosjusus](https://fenicsproject.discourse.group/u/Karlosjusus)
#### Post date: [June 12, 2023, 8:26pm UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/9 "2023-06-12T20:26:01Z")

</div>

Thank you @dokken . I can modify the code with this!

---

<div class="post-metadata">

### Author: ![Yankang\_Liu](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/yankang_liu/32/5392_2.png) [@Yankang\_Liu](https://fenicsproject.discourse.group/u/Yankang_Liu)
#### Post date: [April 16, 2024, 3:33am UTC](https://fenicsproject.discourse.group/t/extract-the-coordinates-of-the-boundary-points-and-the-value-of-a-function-on-them/11260/10 "2024-04-16T03:33:31Z")

</div>

Hello probably I was having a similar situation. Can I have the coordinates of the boundary points and the function value immediately from your code, say `coordinate_list = [...]` and `vector_value_list = [...]` such that `f(evaluated at coordinate_list[i]) = vector_value_list[i]`? If not, maybe I will post another question for this situation.
