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