ValueError: Symmetric part of tensor with rank != 2 is undefined


import numpy as np
from mpi4py import MPI
import ufl
from dolfinx import fem, mesh
import dolfinx.fem.petsc

L = 120.0
W = 4.0
H = 8.0
Nx = 20
Ny = 5
Nz = 10

domain = mesh.create_box(
    MPI.COMM_WORLD,
    [(0.0, 0.0, 0.0), (L, W, H)],
    (Nx, Ny, Nz),
    cell_type=mesh.CellType.hexahedron
)

E = fem.Constant(domain, 2000000.0)
nu = fem.Constant(domain, 0.3)
mu = E / 2 / (1 + nu)
lmbda = E * nu / (1 + nu) / (1 - 2 * nu)

def eps(v):
    return ufl.sym(ufl.grad(v))


def sigma(v):
    return lmbda * ufl.tr(eps(v)) * ufl.Identity(3) + 2.0 * mu * eps(v)

fx = 0.1
fy = -1.0
f = fem.Constant(domain, (fx, fy))

V = fem.functionspace(domain, ("Lagrange", 1))
du = ufl.TrialFunction(V)
u_ = ufl.TestFunction(V)
a = ufl.inner(sigma(du), eps(u_)) * ufl.dx
l = ufl.inner(f, u_) * ufl.dx

I modified the code from the computing reactions example where a 2D cantilever beam is set up and changed things over to a box because I would like to experiment a bit with how to take moment from a 3D mesh.
This code also was modified in the hopes to create a stress tensor that can work with the 3D box:


def sigma(v):
    return lmbda * ufl.tr(eps(v)) * ufl.Identity(3) + 2.0 * mu * eps(v)

So there seems to be a difficulty creating the bilinear form a…


Traceback (most recent call last):
  File "/home/prusso/dolfinx-demos/Untitled-1", line 40, in <module>
    a = ufl.inner(sigma(du), eps(u_)) * ufl.dx
                  ^^^^^^^^^
  File "/home/prusso/dolfinx-demos/Untitled-1", line 31, in sigma
    return lmbda * ufl.tr(eps(v)) * ufl.Identity(3) + 2.0 * mu * eps(v)
                          ^^^^^^
  File "/home/prusso/dolfinx-demos/Untitled-1", line 27, in eps
    return ufl.sym(ufl.grad(v))
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/prusso/spack/var/spack/environments/fenicsx-env/.spack-env/view/lib/python3.11/site-packages/ufl/operators.py", line 337, in sym
    return Sym(A)
           ^^^^^^
  File "/home/prusso/spack/var/spack/environments/fenicsx-env/.spack-env/view/lib/python3.11/site-packages/ufl/tensoralgebra.py", line 542, in __new__
    raise ValueError("Symmetric part of tensor with rank != 2 is undefined.")
ValueError: Symmetric part of tensor with rank != 2 is undefined.

I don’t really know what it means at this point. How should the code be set up in terms of finding the post processed stress for in terms of moment or Mz for the box?

This generates a FE space for a scalar field. If you want a vector field you have to use

V = fem.functionspace(domain, ("Lagrange", 1, (3, )))

or

V = fem.functionspace(domain, ("Lagrange", 1, (domain.topology.dim, )))
1 Like

If I change only these lines to:


f = fem.Constant(domain, (fx, fy, 0.0))

V = fem.functionspace(domain, ("Lagrange", 1), (3, ))

There will be a traceback:


Traceback (most recent call last):
  File "/home/prusso/dolfinx-demos/Untitled-1.py", line 37, in <module>
    V = fem.functionspace(domain, ("Lagrange", 1), (3, ))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/prusso/spack/var/spack/environments/fenicsx-env/.spack-env/view/lib/python3.11/site-packages/dolfinx/fem/function.py", line 654, in functionspace
    form_compiler_options["scalar_type"] = dtype
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
TypeError: 'tuple' object does not support item assignment

A tuples item is being assigned somehow in a way that is throwing exception. Some information about a scalar_type is given. Beyond that I really am not sure yet what to make of it…

Sorry, I was going by heart and messed up the syntax. I am editing now the post with the correct syntax.

This should probably be
V = fem.functionspace(domain, ("Lagrange", 1, (domain.geometry.dim, )))
if you want the space to be compatible with a ufl.grad object on a potential manifold:)

1 Like