Hello,
I want to compute the L2 norm of a strain tensor, but I received the error that FEniCS doesn’t know how to compute the norm, does anyone know how to resolve this?
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)/norm(eps(u))
Traceback (most recent call last):
File “x.py”, line 12, in
quantity = norm(eps(u))
File “/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/fem/norms.py”, line 151, in norm
raise TypeError(“Do not know how to compute norm of {}”.format(str(v)))
TypeError: Do not know how to compute norm of sym(grad([f_5[0], f_5[1]]))
I would use Function.split(deepcopy=True)
instead:
u, P = Sol.split(True)
epsu = project(eps(u), TensorFunctionSpace(mesh, "DG", 1))
norm(epsu)
1 Like
I implemented your suggestion as follow:
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 = Sol.split(True)
epsu = project(eps(u), TensorFunctionSpace(mesh, "DG", 1))
quantity = eps(u)/norm(epsu)
I received division by zero error, I don’t understand how the norm could be zero.
Division by zero!
Traceback (most recent call last):
File "x.py", line 12, in <module>
quantity = eps(u)/norm(epsu)
File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/exproperators.py", line 255, in _div
d = Division(self[ii], o)
File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/algebra.py", line 236, in __new__
error("Division by zero!")
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: Division by zero!
Because the function u
is identically zero. I don’t think you have solved for u
looking at the snippet you posted. Since u
is zero, eps(u)
is zero, and therefore it’s norm
1 Like
I see, this code is just a minimum example, this quantity goes to my weak form and then solve for u and P, I can’t solve for u and P without this norm.
I see. You don’t need the norm then. Norm of a function u
would be
\sqrt{\int_\Omega \sum_{i=1}^n u^2_i dx},
what you possibly want is simply the frobenius norm of the tensor \varepsilon_{ij} itself, which could then be
u, P = split(Sol)
epsu = eps(u)
norm_epsu = sqrt(inner(epsu, epsu))
Is that what you want?
1 Like
I have tried this method previously, but when it gets to the weak form, I get the following error 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 = VectorElement("CG", mesh.ufl_cell(), 2)
P2 = FiniteElement("CG", mesh.ufl_cell(), 1)
ME = FunctionSpace(mesh, MixedElement( [P1, P2] ))
Sol = Function(ME)
u, P = Sol.split(True)
quantity = eps(u)/sqrt(inner(eps(u), eps(u)))
someweakform = quantity * dx
Can only integrate scalar expressions. The integrand is a tensor expression with value shape (2, 2) and free indices with labels ().
Traceback (most recent call last):
File "y.py", line 14, in <module>
someweakform = quantity * dx
File "/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/measure.py", line 418, in __rmul__
error("Can only integrate scalar expressions. The integrand is a "
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 only integrate scalar expressions. The integrand is a tensor expression with value shape (2, 2) and free indices with labels ().
Your definition of someweakform
looks incorrect to me. You should have scalar in there. Unless you can provide a more complete minimal example, it is difficult to help you.
1 Like