Does Mixed Function Space (list(spaces)) work?

Hello everyone, I would like to create a mixed function space from an array of N function spaces (with N large enough). However the dolfin’s method MixedFunctionSpace() does not take my array into arguments. Here is a part of my code:
import dolfin

self.arrayOfDolfinFunctionSpace # contains a list of dolfin.FunctionSpace
W = dolfin.MixedFunctionSpace(self.arrayOfDolfinFunctionSpace)

ERROR###

Runtime error: Cannot create product of function spaces, expecting a list of function spaces

Please, Do you have solutions or tips for my issue?

I have the same error when I create an array of 2 Function Spaces:

L1 = dolfin.FunctionSpace(mesh, “Lagrange”,1)
L2 = dolfin.FunctionSpace(mesh, “Lagrange”,2)
listSpaces = [L1, L2]
W = dolfin.MixedFunctionSpace(listSpaces)

ERROR###

Runtime error: Cannot create product of function spaces, expecting a list of function spaces

Comparing this to the docs for DOLFIN, it looks like it should work and I can’t work out why it doesn’t. One possible workaround would be to do the following:

element1 = dolfin.FiniteElement("Lagrange", "triangle", 1)
element2 = dolfin.FiniteElement("Lagrange", "triangle", 2)
mixed_element = dolfin.MixedElement([element1, element2])
L = dolfin.FunctionSpace(mesh, mixed_element)

This creates UFL elements and a UFL MixedElement, then uses these to make the FunctionSpace.

1 Like

Your code run without error:

import dolfin
element1 = dolfin.FiniteElement(“Lagrange”, “triangle”, 1)
element2 = dolfin.FiniteElement(“Lagrange”, “triangle”, 2)
mixed_element = dolfin.MixedElement([element1, element2])
mesh = dolfin.UnitSquareMesh(10, 10)
L = dolfin.FunctionSpace(mesh, mixed_element)
Calling FFC just-in-time (JIT) compiler, this may take some time.

But my previous code doesn’t work and I want to use MixedFunctionSpace() rather than MixedElement() because I have to solve my problem on different submeshes.

import dolfin
mesh = dolfin.UnitSquareMesh(10, 10)
L1 = dolfin.FunctionSpace(mesh,“Lagrange”,1)
L2 = dolfin.FunctionSpace(mesh,“Lagrange”,2)
W = dolfin.MixedFunctionSpace([L1,L2])
Traceback (most recent call last):
File “”, line 1, in
File “/home/ttheulier/.local/lib/python3.9/site-packages/dolfin/function/functionspace.py”, line 217, in init
raise RuntimeError(“Cannot create product of function spaces, expecting a list of function spaces”)
RuntimeError: Cannot create product of function spaces, expecting a list of function spaces

@cecile might be able to shed some light on this.
However, I think the issue is how the constructor works, as it takes in a set of arguments:Bitbucket
Thus, you need to do it in the following way:

from dolfin import *

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "CG", 1)
V2 = FunctionSpace(mesh, "CG", 2)

Vs = [V, V2]
Vmixed = MixedFunctionSpace(*Vs)

as shown for instance here: python - Pass a list to a function to act as multiple arguments - Stack Overflow

2 Likes

Thanks a lot for your help!
Indeed, It works with the use of pointer :slight_smile:

Just a reminder for newbies like me. Some old tutorials and demos show:

V = VectorFunctionSpace(mesh, "CG", 2)
Q = FunctionSpace(mesh, "CG", 1)
W = V * Q

which gives an error:
unsupported operand type(s) for *: 'FunctionSpace' and 'FunctionSpace'. Tutorials and demos also show an alternative, replacing W = V*Q with W = MixedFunctionSpace([V, Q]). However, this doesn’t work either and gives an error: Cannot create product of function spaces, expecting a list of function spaces, unless a pointer is supplied like:

TH = [V, Q]
W = MixedFunctionSpace(*TH)

or not using a list:
W = MixedFunctionSpace(V, Q)