How to apply a dirichlet BC to petsc vector for mixed element and FEnicsx0.9?

My original code is very long and I can only provide a small fraction of my code,I use gmsh with triangular 2D mesh with order 2, and here is the code I use to construct BC

V_p, V_u, V_T = V.sub(0), V.sub(1), V.sub(2)
V_u_collapsed, _ = V_u.collapse()
inlet_facets = ft.find(2)
p_inf_const = fem.Constant(domain, default_scalar_type(p_inf))
T_inf_const = fem.Constant(domain, default_scalar_type(T_inf))
zero_scalar = fem.Constant(domain, default_scalar_type(0.0))
bc_p_inlet = fem.dirichletbc(p_inf_const, fem.locate_dofs_topological(V_p, tdim - 1, inlet_facets), V_p)
u_inlet_func = fem.Function(V_u_collapsed)
u_inlet_func.interpolate(lambda x: np.vstack((np.full(x.shape[1], u_inf), np.zeros(x.shape[1]))))
bc_u_inlet = fem.dirichletbc(u_inlet_func, fem.locate_dofs_topological(V_u, tdim - 1, inlet_facets))
bc_T_inlet = fem.dirichletbc(T_inf_const, fem.locate_dofs_topological(V_T, tdim - 1, inlet_facets), V_T)
wall_facets = ft.find(1)
u_wall_func = fem.Function(V_u_collapsed)
u_wall_func.interpolate(lambda x: np.zeros((tdim, x.shape[1])))
bc_u_wall = fem.dirichletbc(u_wall_func, fem.locate_dofs_topological(V_u, tdim - 1, wall_facets))
symmetry_facets = ft.find(4)
bc_uy_symm = fem.dirichletbc(zero_scalar,
                             fem.locate_dofs_topological(V_u.sub(1), tdim - 1, symmetry_facets), V_u.sub(1))
bcs = [bc_p_inlet, bc_u_inlet, bc_T_inlet, bc_u_wall, bc_uy_symm]

then when I apply the BC to petsc vector, I use

fem.petsc.set_bc(Y_stage.x.petsc_vec, bcs)

Here Y_stage is a function on the mixed element space, the mixed element function space has 3 components with dimension 1,2,1, while I use the fem.petsc.set_bc, then some dofs are aplied with the dirichlet BC but some dofs correspond to the boundary are set to 0, is there any docs about it that match the best practices of FEnicsx0.9? thanks you very much

You’re collapsing the velocity subspace with V_u_collapsed = V_u.collapse(), then building the inlet function on that collapsed space. The issue is that I think that this disconnects the function from the mixed space layout, so when you apply the BC, DolfinX may silently mismatch DOFs — causing some to be zeroed incorrectly.

Instead, I’d define the function directly on the subspace without collapsing, and use the two-part form of locate_dofs_topological to preserve the mapping to the global DOF layout:

Quick snippet, might need to be adapted:

u_inlet_func = fem.Function(V_u)
u_inlet_func.interpolate(lambda x: np.vstack((np.full(x.shape[1], u_inf), np.zeros(x.shape[1]))))

dofs_u = fem.locate_dofs_topological((V_u, full_velocity_space), tdim - 1, inlet_facets)
bc_u_inlet = fem.dirichletbc(u_inlet_func, dofs_u, V_u)