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

**URL:** <https://fenicsproject.discourse.group/t/how-to-apply-a-dirichlet-bc-to-petsc-vector-for-mixed-element-and-fenicsx0-9/18050>\
**Category:** General\
**Created:** [August 1, 2025, 1:31pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-dirichlet-bc-to-petsc-vector-for-mixed-element-and-fenicsx0-9/18050 "2025-08-01T13:31:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deep\_Math](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/deep_math/32/6225_2.png) [@Deep\_Math](https://fenicsproject.discourse.group/u/Deep_Math)\
**Post date:** [August 1, 2025, 1:31pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-dirichlet-bc-to-petsc-vector-for-mixed-element-and-fenicsx0-9/18050/1 "2025-08-01T13:31:52Z")

</div>

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

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![ferdean](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ferdean/32/8700_2.png) [@ferdean](https://fenicsproject.discourse.group/u/ferdean)\
**Post date:** [August 1, 2025, 2:05pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-dirichlet-bc-to-petsc-vector-for-mixed-element-and-fenicsx0-9/18050/2 "2025-08-01T14:05:09Z")

</div>

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:

```auto
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)

```
