# The length of the array inside the interpolate function wont match the number of DOFs

**URL:** <https://fenicsproject.discourse.group/t/the-length-of-the-array-inside-the-interpolate-function-wont-match-the-number-of-dofs/15659>\
**Category:** General\
**Created:** [August 27, 2024, 6:06pm UTC](https://fenicsproject.discourse.group/t/the-length-of-the-array-inside-the-interpolate-function-wont-match-the-number-of-dofs/15659 "2024-08-27T18:06:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marhatom](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/marhatom/32/6430_2.png) [@marhatom](https://fenicsproject.discourse.group/u/marhatom)\
**Post date:** [August 27, 2024, 6:06pm UTC](https://fenicsproject.discourse.group/t/the-length-of-the-array-inside-the-interpolate-function-wont-match-the-number-of-dofs/15659/1 "2024-08-27T18:06:56Z")

</div>

Hello,  
I have always assumed that

```python
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 2, 2, 
                                       cell_type=dolfinx.mesh.CellType.quadrilateral)

V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))

def function(x):
    num_nodes2 = x.shape[1]
    print(num_nodes2)
    return np.zeros(num_nodes2)

f = dolfinx.fem.Function(V)
f.interpolate(function) 

```

the number of nodes _num\_nodes2_ inside the interpolate function is the same as the number of DOFs

```auto
num_nodes = V.dofmap.index_map.size_global
print(num_nodes)

```

but the output in this case is

```auto
16
9

```

And I would like to know why. Thanks

---

<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:** [August 27, 2024, 6:12pm UTC](https://fenicsproject.discourse.group/t/the-length-of-the-array-inside-the-interpolate-function-wont-match-the-number-of-dofs/15659/2 "2024-08-27T18:12:21Z")

</div>

Interpolation is done on a cell level. This means that to interpolate a function into a function space, one computes it cell by cell.

As you have 4 cells, with 4 dofs in each cell, this means that you have 16 interpolation points to consider.

The reason for this is that not every basis function is defined with a functional that is a point evaluation, there are basis functions defined by integral moments, which needs co-variant or contra-variant Piola mappings, which are defined on a cell level.

---

<div class="post-metadata">

**Author:** ![marhatom](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/marhatom/32/6430_2.png) [@marhatom](https://fenicsproject.discourse.group/u/marhatom)\
**Post date:** [August 27, 2024, 6:46pm UTC](https://fenicsproject.discourse.group/t/the-length-of-the-array-inside-the-interpolate-function-wont-match-the-number-of-dofs/15659/3 "2024-08-27T18:46:51Z")

</div>

Ok, I see.

The thing is, I would like to assign a value to a specific node, like this

```auto
# find specific index 
id_center = next((index for index, node in enumerate(V.tabulate_dof_coordinates()) if np.allclose(node, [0.5, 0.5, 0])), None)

class Expr():
    def __init__ (self, t):
        self.t = t

    def __call__ (self, x):
        values = np.zeros(x.shape[1])
        values[id_center] = 10 * self.t
        
        return values

value = Expr(1)
f.interpolate(value)

```

So I can call

```auto
value.t = t
f.interpolate(value)

```

inside a time loop. Is it possible?

---

<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:** [August 27, 2024, 8:01pm UTC](https://fenicsproject.discourse.group/t/the-length-of-the-array-inside-the-interpolate-function-wont-match-the-number-of-dofs/15659/4 "2024-08-27T20:01:39Z")

</div>

Locate the degree of freedom based on its dof coordinate (use `V.tabulate_dof_coordinates()` to find the relevant coordinate index to access in `u.x.array`
