How to get a inner product with tensors?

Hello everyone,

I am trying to compute this program on linear elasticityy which is in fact a fenics project that you can find in the tutorial (a little bit modified). However, I did not manage to execute it.

I received this message error : « Can only integrate scalar expressions. The integrand is a tensor expression with value shape (3, 3) and free indices with labels (). »
It seems that there is a problem with the inner product “inner(sigma(u),epsilon(v)”. It does not give a scalar but rather a (3,3) tensor.

Could you help me solving this problem ?

Thank you for your help


from dolfin import *
from __future__ import print_function
from fenics import *
import math
from ufl import nabla_div
import numpy as np
from numpy import *

# Define variables

L = 1   
l = 0.2 
mu = 2.9 
rho = 1
delta = l/L
gamma = 0.4*delta**2
beta = 1.6       
lambda_ = beta
g = gamma


# Create mesh and define function space

mesh = BoxMesh(Point(0, 0, 0), Point(L, l, l), 10, 3, 3)

V = VectorFunctionSpace(mesh, 'P', 1)



# Define boundary conditions

tol = 1E-14


def clamped_boundary1(x, on_boundary):

    return on_boundary and x[0] < tol


bc1 = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary1)


def clamped_boundary2(x, on_boundary):

    return on_boundary and x[1] < tol


bc2 = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary2)

bc=[bc1,bc2]


# Define strain and stress

def epsilon(u):

    return 0.5*(nabla_grad(u) + nabla_grad(u).T)

    #return sym(nabla_grad(u))



def sigma(u):

    return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)



# Define variational problem

u = TrialFunction(V)

d = u.geometric_dimension()  # space dimension

v = TestFunction(V)

f = Constant((0, 0, -rho*g))

T = Constant((0, 0, 0))

a = inner(sigma(u), epsilon(v))*dx

L = dot(f, v)*dx + dot(T, v)*ds

Please encapsulate your code in ``` for proper formatting, and reduce the size of your code by removing all lines of the code that is after the error message to make it more readable for others.
See Read before posting: How do I get my question answered? on how to post a minimal working example. There are for instance many packages that you are loading that is not required for the code to run.

Thanks. done it (sorry I am a beginner with FEniCS)

The problem here is that you have overloaded the ufl.inner function by calling from numpy import *.
A general advice is to only do wildcard imports on one package per program (I usually never use wildcards), to avoid overloading.

2 Likes