# Fenics : Elementwise stress computation

In a elastodynamics problem, I define the stress as

def sigma(v):


I have computed the displacements at the nodes. Now I want to compute the stresses elementwise. Can anyone help?

The following compiles:

from dolfin import *
from random import random

lmbda = Constant(1.0)
mu = Constant(1.0)

def sigma(v):

mesh = UnitCubeMesh(8,8,8)
V = VectorFunctionSpace(mesh, "CG", 1)

# Create random displacement vector
displacement = Function(V)
for i in xrange(V.dim()):
displacement.vector()[i] = random()

T = TensorFunctionSpace(mesh, "DG", 0)
form = inner(sigma(displacement),TestFunction(T))*dx
scaling = 1.0/CellSize(mesh)
stress = Function(T)
assemble(scaling*form, tensor=stress.vector())

• Hi, thnks for your comments. but when i run your code there is an error Trying to integrate expression of rank 2 with free indices (). Traceback (most recent call last): File "/media/njha/oss/Fenics/demo/NAFEMS.py", line 141, in <module> assemble(sigma(v)*TestFunction(T)*dx(0), tensor=stress.vector()) File "/usr/lib/python2.7/dist-packages/ufl/integral.py", line 194, in rmul – nickrocks May 21 '13 at 10:13
• Oops. You need to change sigma(v)*TestFunction(T) to inner(sigma(v), TestFunction(T)). Edited answer. – Øyvind Evju May 21 '13 at 10:52
• hi i tried assemble(inner(sigma(v),TestFunction(T)*dx(0)), tensor=stress.vector()) but still error is Trying to integrate expression of rank 2 with free indices (). Traceback (most recent call last): File "/media/njha/oss/Fenics/demo/NAFEMS.py", line 146, in <module> assemble(inner(sigma(v),TestFunction(T)*dx(0)), tensor=stress.vector()) – nickrocks May 21 '13 at 11:35
• I'm not entirely sure what you are doing different from me, but I have added a complete working example. – Øyvind Evju May 21 '13 at 12:00
• it works perfectly now ..thnks a ton – nickrocks May 21 '13 at 12:04