# Integral over the domain of a tensorial quantity

**URL:** <https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548>\
**Category:** dolfinx\
**Created:** [June 15, 2022, 3:01pm UTC](https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548 "2022-06-15T15:01:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cas91](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/cas91/32/174_2.png) [@cas91](https://fenicsproject.discourse.group/u/cas91)\
**Post date:** [June 15, 2022, 3:01pm UTC](https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548/1 "2022-06-15T15:01:19Z")

</div>

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](https://fenicsproject.discourse.group/t/integration-of-a-gradient-of-function/7428/11),

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):  
> eps = ufl.sym(ufl.grad(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  
when compiling your pybind11 module.

Thanks a lot in adavance !!!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 15, 2022, 3:54pm UTC](https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548/2 "2022-06-15T15:54:46Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![cas91](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/cas91/32/174_2.png) [@cas91](https://fenicsproject.discourse.group/u/cas91)\
**Post date:** [June 15, 2022, 4:07pm UTC](https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548/3 "2022-06-15T16:07:48Z")

</div>

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 ^^!!!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 15, 2022, 4:57pm UTC](https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548/4 "2022-06-15T16:57:22Z")

</div>

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](https://fenicsproject.discourse.group/t/interpolate-expression-with-complex-litteral/8251/4)

---

<div class="post-metadata">

**Author:** ![cas91](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/cas91/32/174_2.png) [@cas91](https://fenicsproject.discourse.group/u/cas91)\
**Post date:** [June 17, 2022, 5:20pm UTC](https://fenicsproject.discourse.group/t/integral-over-the-domain-of-a-tensorial-quantity/8548/5 "2022-06-17T17:20:20Z")

</div>

Hi Mr Dokken 🙂

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

Kind regards

Camilo
