Expressing symmetric tensorelement

Hi,
I´am using Fenics-2019 1.0. on Windows linux subsystem.
I want to create a mixedelementspace with vectorelement sand tensorelements. How can I state that the ternsorelements are symmetric?
Thanks in advance

from fenics import *
from ufl import nabla_grad
from ufl import nabla_div
#Mesh

mesh = RectangleMesh(Point(0, 0), Point(3, 1), 10, 10, “right/left”)
#Create space
P1 = FiniteElement(‘Lagrange’, mesh.ufl_cell(), 1)
P2 = VectorElement(‘Lagrange’, mesh.ufl_cell(),2)
A3 = TensorElement(‘CG’, mesh.ufl_cell(), 2)#,symmetry=TRUE)

element = MixedElement((P2,P1,A3))
V = FunctionSpace(mesh, element)

Creating a “non-mixed” Fucntionspace thorugh Tensorfunctionspace it is possible to create a symmetric space. However this possibility seems not be be present defining it thorugh TensorElement (a formulation that I need to create a mixed space, right?)

Hi, I am not sure what is the current status of imposing symmetry for TensorElements, I know that it has been buggy at some point.
To avoid any trouble I would suggest defining a VectorElement for the individual (e.g. upper-triangular) components and then use as_tensor to reshape it in the proper symmetric tensor form.

1 Like

Thanks beleyerj! Could you please write down a dummy-example so I am sure to have got what you mean?

For instance for a 2x2 symmetric tensor something such as

Ve = VectorElement("CG", mesh.ufl_cell(), 1, dim=3)
V = FunctionSpace(mesh, Ve)
u_vec = Function(V)
u_tens = as_tensor([[u[0], u[2]], 
                    [u[2], u[1]]])
2 Likes

Perfect. Thanks again

Hi,
I tried to implement a symmetric tensor form in a variational problem as you suggested. I simplified the code to give you an idea of whta does not work. Basically only the non-diagonal results are given. This beahirvoir I see also when using
TensorFunctionSpace(mesh, 'Lagrange', 2,symmetry=True).
However with the method above I also get different results then using M=TensorFunctionSpace(mesh, 'Lagrange', 2). Basically hte cahnges in th enon diagonal part are very small. Could you help me further?

from fenics import *
mesh=UnitSquareMesh(10,10)
Me = VectorElement(“Lagrange”, mesh.ufl_cell(), 2, dim=3)
M = FunctionSpace(mesh, Me)

#In a_n as initial condition
a_0 = Expression(((‘0’, ‘1’, ‘0’)), degree=2)
a_n_v = project(a_0, M)
a_n = as_tensor([[a_n_v[0], a_n_v[1]],
[a_n_v[1], a_n_v[2]]])

#a_t as TrialTensor and a m as TestTensor
a_vec = TrialFunction(M)
a_t = as_tensor([[a_vec[0], a_vec[2]],
[a_vec[2], a_vec[1]]])

m_vec = TestFunction(M)
m = as_tensor([[m_vec[0], m_vec[2]],
[m_vec[2], m_vec[1]]])

#a as solution
a=Function(M)

#Variational example problem
k=Constant(0.01)
Fa = inner((a_t - a_n) / k, m)dx +
-2
inner(a_n,m)*dx

a4=lhs(Fa)
L4=rhs(Fa)

#Construct matrics
A4=assemble(a4)

#Solve iteration
num_steps=10
t = 0
for n in range(num_steps):

b4 = assemble(L4)
solve(A4, a.vector(),b4)
print('print a value of a', a(0.5,0.5))
a_n=as_tensor([[a[0], a[2]], 
            [a[2], a[1]]])

Thanks!

Please format your code correctly using ``` so that we can just copy and paste it for testing. Also please state what is the issue you have with the above code and what you would expect from it

1 Like

However I have a nother “stupi question” . You write to use ```` correctly. Isn´t it done properly above in my post of yesterday? What shoud I do differently? Soryy to bother you with thi snon relevant questions.

The problem with the code is that the solution to the PDE (actually a ODE) should be an exponential function. The result displayed is not. Using M=TensorFunctionSpace(mesh, 'Lagrange', 2) yields the exponential.

All of the snippets should be inside a single ```.
Also make sure that indentation (such as for loops) is correct.

1 Like

I tried to format the above question better. However it seems not possible to change the post again. The part beneath the loop should be in the loop. Hence the indentation is not correct as I wrote it above. Sorry

Sorry, I am not sure to understand what is your problem. You should post a properly formatted code with your different attempts and clearly indicate what you expect and what is not working as intended…
Is it possible that you mixed indices when defining a_n from a_n_v which should read

a_n = as_tensor([[a_n_v[0], a_n_v[2]],
                 [a_n_v[2], a_n_v[1]]])

I am still figuring out how to format the indentation when using the blockstructure.
However the problem is the following: When I run the code I would expect to get as a result the exponential function in the non-diagonal results. Hence I print the result. However the result is a constant. Here you see what I get printed on the user interface:
print a value of a [0. 1.02 0. ]
print a value of a [0. 1.02 0. ]
print a value of a [0. 1.02 0. ]
print a value of a [0. 1.02 0. ]
Basically the first roud of the loops works, however apparently my way to update the value of a_n is not working. Why?