# Integral over the domain of a tensorial quantity

Hi to all of you :)!

I have a problem regarding the integration of a second order tensor over the volume (3D mesh) in post-processing. Let T(uh), be a second order tensor function acting on the discrete displacement field uh at each dof. The integral of the second order tensor (size=(3,3)) over the domain (Omega) is giving an error. First problem was that ufl perform integration only on scalar quantities, the error was:

ufl.log.UFLException: Can only integrate scalar expressions. The integrand is a tensor expression with value shape (3, 3) and free indices with labels ().

Then checking the solution proposed in
Integration of a gradient of function - #11 by Alberto_Lolli,

Then, I modified my formulation in the same fashion to compute the integral of each component of the tensor. A new problem arise when trying to do so, my code is below.

define Ten(u):
return ufl.as_tensor(L[p,q,r,s]*eps[r,s],(p,q))

Ten2 = Ten(uh)
Q = dolfinx.fem.TensorFunctionSpace(mesh,(âDGâ,0))
Ten2Int = dolfinx.fem.Function(Q)
Ten2Int.interpolate(Ten2)
Ten2Int2 = numpy.zeros((3,3))
for i in range(3):
for j in range(3):
Ten2Int2 = dolfinx.assemble_scalar(Ten2Int[i,j]*ufl.dx)

the error is the following:

File â/home/camilo/.local/lib/python3.8/site-packages/dolfinx/fem/assemble.pyâ, line 128, in assemble_scalar
constants = constants or _pack_constants(M)
TypeError: pack_constants(): incompatible function arguments. The following argument types are supported:
1. (arg0: dolfinx::fem::Form) â numpy.ndarray[numpy.float64]
2. (arg0: dolfinx::fem::Expression) â numpy.ndarray[numpy.float64]
3. (arg0: dolfinx::fem::Form) â numpy.ndarray[numpy.float32]
4. (arg0: dolfinx::fem::Expression) â numpy.ndarray[numpy.float32]
5. (arg0: dolfinx::fem::Form<std::complex >) â numpy.ndarray[numpy.complex128]
6. (arg0: dolfinx::fem::Expression<std::complex >) â numpy.ndarray[numpy.complex128]

Invoked with: Form([Integral(Sum(Indexed(Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement(âLagrangeâ, tetrahedron, 1, variant=âequispacedâ), dim=3, variant=âequispacedâ), 0), TensorElement(FiniteElement(âDiscontinuous Lagrangeâ, tetrahedron, 0), shape=(3, 3), symmetry={})), 2), MultiIndex((FixedIndex(0), FixedIndex(0)))), Indexed(Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement(âLagrangeâ, tetrahedron, 1, variant=âequispacedâ), dim=3, variant=âequispacedâ), 0), TensorElement(FiniteElement(âDiscontinuous Lagrangeâ, tetrahedron, 0), shape=(3, 3), symmetry={})), 3), MultiIndex((FixedIndex(0), FixedIndex(0))))), âcellâ, Mesh(VectorElement(FiniteElement(âLagrangeâ, tetrahedron, 1, variant=âequispacedâ), dim=3, variant=âequispacedâ), 0), âeverywhereâ, {}, None)])

Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,
<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic
conversions are optional and require extra headers to be included

Thanks a lot in adavance !!!

Im not quite sure which version of DOLFINx you are using, but I guess you should call
`dolfinx.fem.form(Ten2Int[i,j]*ufl.dx)` prior to calling `dolfinx.fem.assemble_scalar`.

1 Like

Thanks a lot Mr. Dokken, I think now is working that part ^^, then a third problem arise XD, the problem is that Iâm in complex mode of my 0.4.0 dolfinx version (complex petsc) and the dolfinx.fem.assemble scalar gives me the following type error:

TypeError: canât convert complex to float

should I compute the integral of the real and imaginary part of each component ? or there is a way to force it to work on complex numbers ?

Thanks a lot in advance ^^!!!

Without a minimal working example it is quite hard to give any guidance, but i can guess it is related to:Interpolate expression with complex litteral - #4 by dokken

1 Like

Hi Mr Dokken

Thanks a lot again for your help, quite a good intuition ^^. All is working smooth now.

Kind regards

Camilo