Interpolate a ufl.as_tensor

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.

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.),

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']
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!


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:

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.

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.

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

1 Like

Further to @dokken’s comment, see this example. Extension to a tensor valued function should be trivial.

1 Like