Extract variables and splitting from mixed function space in Dolfinx

Hello,

I am trying to model a two-phase fluid system and have the problem setup in FEniCS. I’d like to re-setup the problem in Dolfinx, but am having trouble with the syntax as I’m quite new to variational formulations.

Specifically, how can I extract parameters (velocity, pressure, mass fraction) from the mixed function space, and does ufl.split work in Dolfinx?

Here is my variational setup in FEniCS:

import fenics as f

mesh = f.RectangleMesh(f.Point(0, 0), f.Point(width, height), 100, 100)

V_ele = f.VectorElement("CG", mesh.ufl_cell(), 2)
Q_ele = f.FiniteElement("CG", mesh.ufl_cell(), 1)
W = f.FunctionSpace(mesh, f.MixedElement([V_ele, Q_ele, Q_ele]))

upphi = f.Function(W)
upphi_n = f.Function(W)

vu, vp, vphi = f.TestFunctions(W)

u_l, p, phi_g = f.split(upphi)
u_l_n, p_n, phi_g_n = f.split(upphi_n)

And here is my setup so far in Dolfinx:

from dolfinx import mesh
from dolfinx.fem import Function, functionspace
from mpi4py import MPI
import ufl

# Mesh definition
width, height = 0.5, 1
nx, ny = 100, 100
domain = mesh.create_rectangle(MPI.COMM_WORLD, [[0, 0], [width, height]], [nx, ny], mesh.CellType.triangle)

# Function spaces
V = functionspace(domain, ("Lagrange", 2))
Q = functionspace(domain, ("Lagrange", 1))

W = ufl.MixedFunctionSpace(V, Q)

# Functions
upphi = Function(W)

with error message:

Traceback (most recent call last):
  File "/mnt/c/Users/ckhur/Desktop/DOLFINx/Sparging/sparge_test_v1.py", line 28, in <module>
    upphi = Function(W)
  File "/home/ckhurana/anaconda3/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/function.py", line 319, in __init__
    V.element.dtype, np.dtype(dtype).type(0).real.dtype
    ^^^^^^^^^
AttributeError: 'MixedFunctionSpace' object has no attribute 'element'

Thank you!

When using mixed function spaces, you don’t define coefficient in the mixed space, but in each sub-space:

u = Function(V)
p = Function(Q)

See for instance