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}
metadata = {“quadrature_degree”: 4}

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

1 Like

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)

Can anyone please help how to resolve this error

Please provide a minimal reproducible example of your error, using appropriate code blocks

```python
#add example reproducing error here
```

Dear @Swetha_Sri_Nakka_me2 ,

You have not followed my advice of making a minimal, reproducible example that one can copy-paste and get your error message. Therefore, I cannot help you further.

I would advice you to look at mixed-dimensional-examples/Stokes-Traction/stokes_traction_bcs.py at master · cdaversin/mixed-dimensional-examples · GitHub