Hi, see it the following helps
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(10, 10)
T = TensorFunctionSpace(mesh, 'DG', 0)
t = Function(T)
t_values = t.vector().array()
dofmap = T.dofmap()
# Build values of tensor
for cell in cells(mesh):
# Get dof of each tensor component on the cell
dofs = dofmap.cell_dofs(cell.index())
# Or
# dofs = [T.sub(i).dofmap().cell_dofs(cell.index())[0] for i in range(4)]
dof0, dof1, dof2, dof3 = dofs
# We make tensor values depend on position
x, y = cell.midpoint().x(), cell.midpoint().y()
t_values[dof0] = x**2+3 # [0, 0]
t_values[dof1] = 1 # [0, 1]
t_values[dof2] = 1 # [1, 0]
t_values[dof3] = y**2+2 # [1, 1]
t.vector().set_local(t_values)
# Local check for SPD
x = [0.22, 0.24]
mat = t(x).reshape((2, 2))
assert np.all(np.linalg.eigvals(mat) > 0), 'Not all eigs > 0'
assert near(mat[0, 1], mat[1, 0]), 'Not symmetric'