# Failed to split a vector

**URL:** <https://fenicsproject.discourse.group/t/failed-to-split-a-vector/14629>\
**Category:** dolfinx\
**Created:** [May 11, 2024, 8:43pm UTC](https://fenicsproject.discourse.group/t/failed-to-split-a-vector/14629 "2024-05-11T20:43:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amyao](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@amyao](https://fenicsproject.discourse.group/u/amyao)\
**Post date:** [May 11, 2024, 8:43pm UTC](https://fenicsproject.discourse.group/t/failed-to-split-a-vector/14629/1 "2024-05-11T20:43:23Z")

</div>

Hi, I tried to split a vector created by ufl.nabla\_grad into its components but I failed to get the correct shape. I’m providing a minimal work example that’s adapted from [the tutorial](https://jsdokken.com/dolfinx-tutorial/chapter1/fundamentals_code.html):

```auto
from mpi4py import MPI
from dolfinx import mesh
from dolfinx import fem
import numpy as np
import ufl
from petsc4py import PETSc
from dolfinx.fem.petsc import LinearProblem

domain = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8, mesh.CellType.quadrilateral)
V = fem.FunctionSpace(domain, ("Lagrange", 1))
uD = fem.Function(V)
uD.interpolate(lambda x: 1 + x[0]**2 + 2 * x[1]**2)
tdim = domain.topology.dim
fdim = tdim - 1
domain.topology.create_connectivity(fdim, tdim)
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = fem.locate_dofs_topological(V, fdim, boundary_facets)
bc = fem.dirichletbc(uD, boundary_dofs)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
default_scalar_type = PETSc.ScalarType
f = fem.Constant(domain, default_scalar_type(-6))
a = ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
L = f * v * ufl.dx
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
# solve nabla_uh
grad_uh = ufl.nabla_grad(uh) 
V_ = fem.VectorFunctionSpace(domain, ("Lagrange", 1)) 
dx_ = ufl.dx(V_.mesh)
u_ = ufl.TrialFunction(V_)
v_ = ufl.TestFunction(V_)
a_ = ufl.inner(u_,v_) * dx_
L_ = ufl.inner(grad_uh,v_) * dx_
problem = fem.petsc.LinearProblem(a_, L_, bcs=[], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
grad_uh_value = problem.solve()
grad_uh_x, grad_uh_y = grad_uh_value.split()
print(uh.x.array.shape, grad_uh_value.x.array.shape, grad_uh_y.x.array.shape)

```

uh.x.array.shape gives (81,), and grad\_uh\_value.x.array.shape gives (162,) these are expected. However, grad\_uh\_x.x.array.shape also gives (162,) but I was expecting it to be (81,). Anyone knows how I can split the grad\_uh\_value correctly? Thanks

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [May 12, 2024, 5:20am UTC](https://fenicsproject.discourse.group/t/failed-to-split-a-vector/14629/2 "2024-05-12T05:20:15Z")

</div>

Read [Read before posting: How do I get my question answered?](https://fenicsproject.discourse.group/t/read-before-posting-how-do-i-get-my-question-answered/21). The code you posted is not reproducible, hence it’s unlikely anyone will be able to help.

---

<div class="post-metadata">

**Author:** ![amyao](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@amyao](https://fenicsproject.discourse.group/u/amyao)\
**Post date:** [May 12, 2024, 3:29pm UTC](https://fenicsproject.discourse.group/t/failed-to-split-a-vector/14629/3 "2024-05-12T15:29:33Z")

</div>

Hi Francesco, thanks for the reply and sorry for making these confusions. I have provided code adapted from the fenicsx tutorial (and I confirm it can be run on my computer) and edited my question. Can you please take a look again? Thanks a lot

---

<div class="post-metadata">

**Author:** ![Jesus\_Vellojin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jesus_vellojin/32/6485_2.png) [@Jesus\_Vellojin](https://fenicsproject.discourse.group/u/Jesus_Vellojin)\
**Post date:** [May 12, 2024, 3:51pm UTC](https://fenicsproject.discourse.group/t/failed-to-split-a-vector/14629/4 "2024-05-12T15:51:33Z")

</div>

Hi.

When working with mixed formulations, like `VectorFunctionSpace`, you need to collapse the functions (see for example [Meaning of .collapse()](https://fenicsproject.discourse.group/t/meaning-of-collapse/10641/2) or [here](https://fenicsproject.discourse.group/t/writing-xdmf-file-leads-to-inf-nan-values/13830/4)):

Taking

```auto
grad_uh_x=grad_uh_x.collapse()
grad_uh_y=grad_uh_y.collapse()
print(uh.x.array.shape, grad_uh_value.x.array.shape, grad_uh_y.x.array.shape)

```

gives

```auto
(81,) (162,) (81,)

```

as expected.

Cheers.
