Shapes do not match - Projecting

Hey there.

I’m using a FunctionSpace of degree=1 to obtain displacements solutions, by defining

V = FunctionSpace(mesh, 'CG',1)

and then obtaining u (solution) as

u = TrialFunction(V)

I managed to obtain the displacements solutions but I’m struggling to obtain the stresses in the mesh. I belive this is related to u being a degree=1 function. Here I’m adding the important lines

T = FunctionSpace(mesh, 'CG', 1)

def epsilon(u):
	return grad(u)

def sigma(u):
	lmbda = 1.0
	mu = 1.0

    # Tensor de tensiones (solo componente normal)
	sigma_u = lmbda * grad(u) + 2.0 * mu * grad(u)

	return sigma_u

stress = Function(T,name="Stress")
stress.assign(project(sigma(u),T))

vtkfile = File('RobinNeumannEdu/solucion_tension_edu.pvd')
vtkfile << stress
------------------
ufl.log.UFLException: Shapes do not match: <Argument id=140093514274368> and <Sum id=140093546873728>.

Am I using the proper projection?
Thanks in advance.

Your function space is wrong.
As u is a scalar value, grad(u) is a vector, and therefore you need to use a vector function space for T.
Also note that using a CG-1 space is not a good idea.
You should use DG-0, see; Projection and interpolation — FEniCS Tutorial @ Sorbonne for details

Thank you for your answer and your tutorial. I’ll check the solutions.