# Interpolate a ufl.as\_tensor

**URL:** <https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919>\
**Category:** Errors\
**Created:** [August 4, 2022, 1:09pm UTC](https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919 "2022-08-04T13:09:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glimachaves](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@glimachaves](https://fenicsproject.discourse.group/u/glimachaves)\
**Post date:** [August 4, 2022, 1:09pm UTC](https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919/1 "2022-08-04T13:09:20Z")

</div>

Hi everyone,

How can I interpolate a `ufl.as_tensor` object onto a finite element space? For example, in the code below, I calculate the curl of a tensor field and return it as an `as_tensor`, which I would then like to interpolate to a `TensorFunctionSpace`.

```auto
import dolfin as dlf
import ufl
import numpy as np

# Levi-Civita permutation tensor
perm = ufl.PermutationSymbol(3)

def tcurl(A):

    i, j, k, l = ufl.indices(4)
    return ufl.as_tensor(A[i, l].dx(k) * perm[j, k, l], (i,j,))

mesh = dlf.BoxMesh(
    dlf.Point(0., 0., 0.),
    dlf.Point(2., 2., 2.),
    20,
    20,
    1
)

V = dlf.TensorFunctionSpace(mesh, "CG", 1)
u = dlf.Function(V, name="u")
curl_u = dlf.Function(V, name="curl of u")

u_exp = dlf.Expression("pow(x[0],2) + pow(x[1],2)*x[0] + sin(x[0])", degree=1)
u_init = dlf.Expression(
    [
        ['u11', '0', '0'],
        ['0', '0', '0'],
        ['0', '0', '0']
    ],
    u11=u_exp,
    degree=1
)
u.assign(dlf.project(u_init, V))
# Interpolate the ufl.as_tensor (doesn't work)
curl_u.assign(dlf.interpolate(tcurl(u), V))

with dlf.XDMFFile("outfile.xdmf") as outfile:
    outfile.parameters["flush_output"] = True
    outfile.parameters["functions_share_mesh"] = True
    outfile.write(u, 0.)
    outfile.write(curl_u, 0.)

```

This throws me the error **‘ComponentTensor’ object has no attribute ‘\_cpp\_object’**.

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![CastriMik](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/castrimik/32/2069_2.png) [@CastriMik](https://fenicsproject.discourse.group/u/CastriMik)\
**Post date:** [August 4, 2022, 1:51pm UTC](https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919/2 "2022-08-04T13:51:41Z")

</div>

Hi,

I think that `interpolate` is for `Expression` while here you are interpolating an `as_tensor`, and therefore you are getting that error. You may consider changing the code in the following way:

```auto
u.assign(dlf.interpolate(u_init, V))
# Interpolate the ufl.as_tensor (doesn't work)
curl_u.assign(dlf.project(tcurl(u), V))

```

Not sure if it does what you want, but it doesn’t throw any error.

---

<div class="post-metadata">

**Author:** ![glimachaves](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@glimachaves](https://fenicsproject.discourse.group/u/glimachaves)\
**Post date:** [August 4, 2022, 1:54pm UTC](https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919/3 "2022-08-04T13:54:36Z")

</div>

Hello, thanks for your answer. I am aware that the projection works, but I wanted to interpolate it since the projection induces some errors due to solving a linear system.

---

<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 4, 2022, 2:16pm UTC](https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919/4 "2022-08-04T14:16:31Z")

</div>

Interpolation of `ufl`-expressions is not supported in legacy dolfin.  
It is however supported in DOLFINx.

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [August 4, 2022, 3:05pm UTC](https://fenicsproject.discourse.group/t/interpolate-a-ufl-as-tensor/8919/5 "2022-08-04T15:05:41Z")

</div>

Further to @dokken’s comment, see this [example](https://github.com/FEniCS/dolfinx/blob/6189a7e2f42a63cc2bafd10696862efb41f0c3f9/python/demo/demo_elasticity.py#L244-L247). Extension to a tensor valued function should be trivial.
