Generating global assembled matrix from ufl tensor

Hi FEniCS family,

I have a ufl form list tensor of shape (3,3) (# refer a1 in MWE). I want to generate global assembly matrix by having local to global mapping for each element.

The MWE is:

from __future__ import print_function
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(5,5)

E, nu = 4760,0.25
lmbda = E*nu/((1+nu)*(1-2*nu))
mu = E/2/(1+nu)
C1=lmbda+2*mu
Comp = as_tensor([(C1,lmbda,lmbda,0,0,0),(lmbda,C1,lmbda,0,0,0),(lmbda,lmbda,C1,0,0,0),(0,0,0,mu,0,0),(0,0,0,0,mu,0),(0,0,0,0,0,mu)])
   
def fun_l(v):
    E1=as_tensor([(v[0].dx(0),0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,v[2].dx(0)),(0,v[1].dx(0),0)])
    return E1

def fun_lT(v):
    E2=as_tensor([(v[0].dx(0),0,0,0,0,0),(0,0,0,0,0,v[2].dx(0)),(0,0,0,0,v[1].dx(0),0)])
    return E2

Ve = VectorElement("CG", mesh.ufl_cell(), 1,dim=3)
Re = VectorElement("R", mesh.ufl_cell(), 0,dim=3)
W = FunctionSpace(mesh, MixedElement([Ve, Re]))
V = FunctionSpace(mesh, Ve)

v_,lamb_ = TestFunctions(W)
dv, dlamb = TrialFunctions(W)

dx = Measure('dx')(domain=mesh)

# Global Assembly matrix

a1=assemble(dot(fun_lT(dv),dot(Comp,fun_l(dv)))*dx)

The error is

Can only integrate scalar expressions. The integrand is a tensor expression with value shape (3, 3) and free indices with labels ().
---------------------------------------------------------------------------
UFLException                              Traceback (most recent call last)
Cell In[20], line 32
     28 dx = Measure('dx')(domain=mesh)
     30 # Global Assembly matrix
---> 32 a1=assemble(dot(fun_lT(dv),dot(Comp,fun_l(dv)))*dx)

How can I get the global assembled matrix of a1 ?
Assembled matrix mean, by doing local to global mapping for each element and forming a single assembled matrix of size. (ndof,ndof) {ndof=36X3+3=111 for present case with 36 vertices}

Do I need to convert this a1 to a variational form and solve, whose coefficient matrix is expected to assembled matrix?

Kingly guide me for getting assembled matrix. Any help is highly appreciated.

You cannot assemble tensors like the one you propose above. as it is simply not a matrix, but a matrix of matrices, a 4th order tensor.

This is because you are using TestFunction and TrialFunction in your variational form.

I think it would be beneficial for you to write down what you want to do mathematically, so that people might get a better grasp of your problem.