# Getting solutions corresponding to the mesh

**URL:** <https://fenicsproject.discourse.group/t/getting-solutions-corresponding-to-the-mesh/14570>\
**Category:** General\
**Tags:** mesh\
**Created:** [May 7, 2024, 4:02pm UTC](https://fenicsproject.discourse.group/t/getting-solutions-corresponding-to-the-mesh/14570 "2024-05-07T16:02:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danny](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/danny/32/5383_2.png) [@danny](https://fenicsproject.discourse.group/u/danny)\
**Post date:** [May 7, 2024, 4:02pm UTC](https://fenicsproject.discourse.group/t/getting-solutions-corresponding-to-the-mesh/14570/1 "2024-05-07T16:02:16Z")

</div>

Hi, I wanted to know if there is any function to obtain solutions corresponding to the mesh. From the tutorial, if I understanding is correct compute\_mesh\_vertices() gives the solutions at vertices corresponding to the mesh cell order(mesh.array().). If I project the solutions to a DG0 space, solutions are at the centroid which can be obtained using E.vector().get\_local() where E is a functionspace of the obtained solution. Are those in the order of mesh.array() ? how can I get the values for solution corresponding to cell markers which can be obtained from mesh.array().

---

<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 7, 2024, 6:22pm UTC](https://fenicsproject.discourse.group/t/getting-solutions-corresponding-to-the-mesh/14570/2 "2024-05-07T18:22:26Z")

</div>

Here is an illustration showing the 1-1 correspondence between the index of a DG-0 space and the cell index in legacy dolfin:

```python
import dolfin
import numpy as np

mesh = dolfin.UnitSquareMesh(10, 10)

V = dolfin.FunctionSpace(mesh, "DG", 0)
u = dolfin.Function(V)
u.interpolate(dolfin.Expression("x[0]+2*x[1]", degree=0))

for cell in dolfin.cells(mesh):
    midpoint = cell.midpoint()
    np.isclose(u.vector().get_local()[cell.index()], midpoint.x() + 2 * midpoint.y())

```

---

<div class="post-metadata">

**Author:** ![danny](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/danny/32/5383_2.png) [@danny](https://fenicsproject.discourse.group/u/danny)\
**Post date:** [May 12, 2024, 9:48pm UTC](https://fenicsproject.discourse.group/t/getting-solutions-corresponding-to-the-mesh/14570/3 "2024-05-12T21:48:11Z")

</div>

so if the interpolation is done for the gradient of a function, the gradient at each midpoint is to be found ?

---

<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 13, 2024, 7:34pm UTC](https://fenicsproject.discourse.group/t/getting-solutions-corresponding-to-the-mesh/14570/4 "2024-05-13T19:34:57Z")

</div>

As you are using legacy Dolfin (not DOLFINx), you would have to use a projection.  
But yes. If you project the gradient into a DG-0 space, the tabulate\_dof\_coordinates() of the DG-0 space corresponds to the midpoints of the mesh, with the equivalent values in the `Function.vector().get_local()`. Note that the easiest way of getting each component of the gradient is to split the projected function with a deepcopy.
