Adding stresses.interpolate(stress_expr) causes the following error:
Traceback (most recent call last):
File "/mnt/c/Trainings/FEniCSx/thermoelasticity_full.py", line 242, in <module>
stresses.interpolate(stress_expr)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/function.py", line 459, in interpolate
_interpolate(u0)
File "/usr/lib/python3.10/functools.py", line 889, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/function.py", line 455, in _
self._cpp_object.interpolate(e0._cpp_object, cells0, cells1) # type: ignore
RuntimeError: Function value size not equal to Expression value size.
Per the error message, you’re attempting to interpolate a tensor-valued expression onto a scalar valied function space. Naturally, that doesn’t work.
In that same demo, you’ll find an approach to creating arbitrarily shaped function spaces. E.g.:
Sel = basix.ufl.element( "DG", domain.basix_cell(), 0, shape=(gdim,gdim) ) # Stress element
V_stress = fem.functionspace(domain,Sel)