Setting multiple DirichletBcs in a mixed FunctionSpace

Hello,

I would like to figure out how to set multiple Dirichlet boundary conditions for a mixed functionspace in dolfinx. I have been trying a few different approaches, but nothing quite worked.

I am trying to solve a system of equations and I build the functionspace like so

from dolfinx import mesh, fem
from basix.ufl import element, mixed_element

... everything mesh related...

domain = mesh.create_mesh(comm, cells=cells, x=x_array, e=cell_type)
P = element("P", domain.basix_cell(), 1)
ME = fem.functionspace(domain, mixed_element([P] * 4))
U = fem.Function(ME)
H0, H1, H2, H3 = ufl.split(U)

Now I would like to set a few Dirichlet conditions.
Specifically I would like to set H0 = H1 = H3 = 0 at (x=L) and H2 = 0 at (x~0).
I tried setting them like so, is that correct?

def boundary_left_marker(x):
    return np.isclose(x[0], 0.0) 

def boundary_right_marker(x):
    return np.isclose(x[0], L)

U0 = U.sub(0).collapse().function_space
U1 = U.sub(1).collapse().function_space
U2 = U.sub(2).collapse().function_space
U3 = U.sub(3).collapse().function_space

C_zero = fem.Constant(domain, 0.0)

bc_right1 = fem.dirichletbc(C_zero, fem.locate_dofs_geometrical(U0, boundary_right_marker), U0)
bc_right2 = fem.dirichletbc(C_zero, fem.locate_dofs_geometrical(U1, boundary_right_marker), U1)
bc_right4 = fem.dirichletbc(C_zero, fem.locate_dofs_geometrical(U3, boundary_right_marker), U3)
bc_left3  = fem.dirichletbc(C_zero, fem.locate_dofs_geometrical(U2, boundary_left_marker), U2)

bc = [bc_right1, bc_right2, bc_right4, bc_left3]

I am later solving for H0, H1, H2, H3.
My problem now is that I can spot a Dirichlet condition H0 = 0 at (x~0) in my results, but that is not what I defined above.

Is it a problem to use U0 = U.sub(0).collapse().function_space, since it will essentially build a new functionspace with unrelated dofs?

Thank you in advance,
Jeremie

Have you considered: Mixed finite element problems — FEniCS Workshop

Thank you.

Could it be that the .collapse() function has changed?
I am getting the following error when I want to extract V, V_to_W0 = W0.collapse().

    V, V_to_W0 = W0.collapse()
    ^^^^^^^^^^
  File "...miniconda3/envs/fenicsx-env/lib/python3.14/site-packages/ufl/core/expr.py", line 379, in __iter__
    for i in range(len(self)):
                   ~~~^^^^^^
  File ".../miniconda3/envs/fenicsx-env/lib/python3.14/site-packages/ufl/core/expr.py", line 375, in __len__
    raise NotImplementedError("Cannot take length of non-vector expression.")
NotImplementedError: Cannot take length of non-vector expression.

Please produce a minimal reproducible example that I can run to debug this error.

I mistakenly applied U.sub(0).collapse() onto a function U and not onto mixed functionspace W.
Following your tutorial (Mixed finite element problems — FEniCS Workshop) I was now able to extract the subfunctions and set the correct boundary conditions.
Apologies for the confusion.

Best wishes,
Jeremie