# Symmetric part of tensor with rank != 2 is undefined

**URL:** <https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394>\
**Category:** Uncategorized\
**Created:** [October 26, 2020, 6:49am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394 "2020-10-26T06:49:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![a516095046](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@a516095046](https://fenicsproject.discourse.group/u/a516095046)\
**Post date:** [October 26, 2020, 6:49am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394/1 "2020-10-26T06:49:27Z")

</div>

Hello,

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 26, 2020, 7:37am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394/2 "2020-10-26T07:37:47Z")

</div>

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))

---

<div class="post-metadata">

**Author:** ![a516095046](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@a516095046](https://fenicsproject.discourse.group/u/a516095046)\
**Post date:** [October 26, 2020, 8:00am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394/3 "2020-10-26T08:00:28Z")

</div>

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:

```auto
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
*** -------------------------------------------------------------------------
```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 26, 2020, 9:12am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394/4 "2020-10-26T09:12:51Z")

</div>

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,)

---

<div class="post-metadata">

**Author:** ![a516095046](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@a516095046](https://fenicsproject.discourse.group/u/a516095046)\
**Post date:** [October 26, 2020, 11:30am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394/5 "2020-10-26T11:30:29Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 26, 2020, 11:32am UTC](https://fenicsproject.discourse.group/t/symmetric-part-of-tensor-with-rank-2-is-undefined/4394/6 "2020-10-26T11:32:10Z")

</div>

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.
