# Generating global assembled matrix from ufl tensor

**URL:** <https://fenicsproject.discourse.group/t/generating-global-assembled-matrix-from-ufl-tensor/11781>\
**Category:** variational formulation\
**Created:** [July 19, 2023, 9:32pm UTC](https://fenicsproject.discourse.group/t/generating-global-assembled-matrix-from-ufl-tensor/11781 "2023-07-19T21:32:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagla0](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bagla0/32/4677_2.png) [@bagla0](https://fenicsproject.discourse.group/u/bagla0)\
**Post date:** [July 19, 2023, 9:32pm UTC](https://fenicsproject.discourse.group/t/generating-global-assembled-matrix-from-ufl-tensor/11781/1 "2023-07-19T21:32:43Z")

</div>

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:

```auto
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

```auto
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.

---

<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:** [July 21, 2023, 7:19am UTC](https://fenicsproject.discourse.group/t/generating-global-assembled-matrix-from-ufl-tensor/11781/2 "2023-07-21T07:19:43Z")

</div>

> [@bagla0](#):
>
> How can I get the global assembled matrix of a1 ?

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.
