Symmetric part of tensor with rank != 2 is undefined

Hello,

I am trying to get the symmetric part of the function u in a mix space with the following code:

from dolfin import *

def eps(u):return sym(grad(u))

mesh = RectangleMesh(Point(0.,0.),Point(1, 0.1),100,10)
P1 = FiniteElement("CG", mesh.ufl_cell(), 2)
P2 = FiniteElement("CG", mesh.ufl_cell(), 1)
ME = FunctionSpace(mesh, MixedElement( [P1, P2] ))
Sol   = Function(ME)
u, P  = split(Sol)

strain = eps(u)

I received the following error:

Traceback (most recent call last):
  File "x.py", line 12, in <module>
    strain = eps(u)
  File "x.py", line 3, in eps
    def eps(u):return sym(grad(u))
  File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/operators.py", line 318, in sym
    return Sym(A)
  File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/tensoralgebra.py", line 457, in __new__
    error("Symmetric part of tensor with rank != 2 is undefined.")
  File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/log.py", line 172, in error
    raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: Symmetric part of tensor with rank != 2 is undefined.

Could someone help me with this error? Thank you!

As u is not a vector space, the gradient is a vector, and thus taking the symmetric part of it does not make sense. Please change the first element to a VectorElement to use sym(grad(u))

1 Like

Thanks docked for your great advice! I changed the first element to VectorElement and the above issue was resolved. However, this change led to another issue that need to be fixed, I am trying to interpolate an all-zero initial conditions for the functions Sol and Sol0 but there is some compatibility error that I don’t understand.

Here is the code:

from dolfin import *


class InitialConditions(UserExpression):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def eval(self, values, x):
        values[0] = 0.0 
        values[1] = 0.0  
    def value_shape(self):
        return (2,)


mesh = RectangleMesh(Point(0.,0.),Point(1., 0.1),100,10)

P1 = VectorElement("CG", mesh.ufl_cell(), 2)
P2 = FiniteElement("CG", mesh.ufl_cell(), 1)
ME = FunctionSpace(mesh, MixedElement( [P1, P2] ))

dSol  = TrialFunction(ME)
testfunc = TestFunction(ME)
v,w = split(testfunc)

Sol   = Function(ME)
Sol0  = Function(ME)

du, dP = split(dSol)
u,  P  = split(Sol)
u0, P0 = split(Sol0)

Sol_init = InitialConditions(degree=2)

Sol.interpolate(Sol_init)
Sol0.interpolate(Sol_init)

Here is the error:

Traceback (most recent call last):
  File "x.py", line 33, in <module>
    Sol.interpolate(Sol_init)
  File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/function/function.py", line 363, in interpolate
    self._cpp_object.interpolate(u._cpp_object)
RuntimeError: 

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to interpolate function into function space.
*** Reason:  Dimension 0 of function (2) does not match dimension 0 of function space (3).
*** Where:   This error was encountered inside FunctionSpace.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2019.1.0
*** Git changeset:  15b823f2c45d3036b6af931284d0f8e3c77b6845
*** -------------------------------------------------------------------------

As your Mixed function space has a vector of two components, and then a scalar space, the initial condition has to be of shape (3,)

1 Like

Thanks! Another operation I am attempting to do after changing to VectorElement is:

from dolfin import *

def eps(u):return sym(grad(u))

mesh = RectangleMesh(Point(0.,0.),Point(1, 0.1),100,10)
P1 = VectorElement("CG", mesh.ufl_cell(), 2)
P2 = FiniteElement("CG", mesh.ufl_cell(), 1)
ME = FunctionSpace(mesh, MixedElement( [P1, P2] ))
Sol   = Function(ME)
u, P  = split(Sol)

quantity = eps(u) + P

I received this error:

Can't add expressions with different shapes.
Traceback (most recent call last):
  File "x.py", line 12, in <module>
    quantity = eps(u) + P
  File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/exproperators.py", line 212, in _add
    return Sum(self, o)
  File "//anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algebra.py", line 53, in __new__
    error("Can't add expressions with different shapes.")
  File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/log.py", line 172, in error
    raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: Can't add expressions with different shapes.

How should I go from here?

As I am not sure what kind of finite element problem you would like to solve, it is hard to give you pointers.

Note that eps(u) is a tensor (matrix), while P2 is a scalar, so it is clear that you cannot add these together.

1 Like