# Mixed function space with tensors

Hi, I have some problems with the definition of mixed functional space and I’m doing wrong something about the code. I need a mixed functional space with elements like [u,F_v1,…F_vn] where u is a vector in 2D and Fv_i are 2x2 tensors.

from dolfin import *
from mshr import*
import ufl
from ufl import ln
import numpy as np

plt.rcParams[‘axes.formatter.useoffset’] = False
parameters[“form_compiler”][“cpp_optimize”] = True
parameters[“form_compiler”][“representation”] = “uflacs”
ffc_options = {“optimize”: True,
“eliminate_zeros”: True,
“precompute_basis_const”: True,
“precompute_ip_const”: True}

#################Geometry2D#############################
domain=Rectangle(Point(0,0),Point(lpx,lpy))
mesh=generate_mesh(domain, n_mesh)

#Function spaces

n_kelvin=2 # number of kelvin’s elements

u_build=VectorElement(‘P’,mesh.ufl_cell(),1,2)
Fv_build=TensorElement(‘P’,mesh.ufl_cell(),1)
elements_build=[u_build]
only_viscous_def=
for i in range(n_kelvin):
elements_build.append(Fv_build)
only_viscous_def.append(Fv_build)

Space_vec=MixedFunctionSpace(space_build)
only_viscous_space=MixedFunctionSpace(only_viscous_space)

state_var=Function(Space_vec)
state_var_Trial=TrialFunction(Space_vec)
state_var_Test=TestFunction(Space_vec)

The error message is
RuntimeError: Cannot create product of function spaces, expecting a list of function spaces

Thank you so much!

I think the `MixedFunctionSpace` function is expecting a list of arguments. Can you try replacing the calls by:

``````Space_vec=MixedFunctionSpace(*space_build)
``````

and

``````
only_viscous_space=MixedFunctionSpace(*only_viscous_space)
``````

?

Thank you, now the error is state_var_Trial:

TypeError: \ When constructing an Argument, TestFunction or TrialFunction, you
must to provide a FunctionSpace and not a FiniteElement. The
FiniteElement class provided by ufl only represents an abstract finite
element space and is only used in standalone .ufl files, while the
FunctionSpace provides a full discrete function space over a given
mesh and should be used in dolfin programs in Python.

I’ve had the same problems as you. You are missing an “s”. Try `TrialFunctions` and `TestFunctions` instead of `TrialFunction` and `TestFunction`

(these return a list so you can unpack them if you want)

Perfect, now it works, thank you so much.
Excuse me if I disturb you yet, but when I apply the boundary conditions by

``````Space_vec.sub(0)
``````

it give:
AttributeError: ‘MixedFunctionSpace’ object has no attribute ‘sub’

I not really sure about this, but I think that you are trying to use a `MixedFunctionSpace` as a `VectorFunctionSpace`.

The different functions of a `MixedFunctionSpace` can be accessed as a list, like:

``````MS = MixedFunctionSpace( V, V )
f, g = TestFunctions(MS)
``````