How to extract/operate on individual components of a VectorFunctionSpace

It should be simple, but I somehow can not figure out quite how to “extract” components to process from a VectorFunctionSpace. For example, from the linear ft06_elasticity tutorial we have a solution like

V = VectorFunctionSpace(mesh, 'P', 1)
u = Function(V)
solve(a == L, u, bc)

and if I want to either extract or compute max/min of only the x-displacement I have tried things like

u_x = u.sub(0)
u_x.vector().max()

and

u_x, u_y, u_z = split(u)
u_x.vector().max()

but it seems u_x always contains and operates on all the solution components (the vector contains all dofs). What is the proper approach to do this?

There are many ways to do this, consider the following and choose the best tool for the job. Of course there are many more (and possibly better) ways to do this than the ones I’ve listed here.

from dolfin import *

mesh = UnitSquareMesh(1, 1)

V = VectorFunctionSpace(mesh, "CG", 1)

u = interpolate(Constant((1.0, 2.0)), V)
print(u.vector()[:])

u_x = u.sub(0, deepcopy=True)
print(u_x.vector()[:])

V_x = V.sub(0).collapse()
u_x = interpolate(u.sub(0), V_x)
print(u_x.vector()[:])

fa = FunctionAssigner(V_x, V.sub(0))
u_x = Function(V_x)
fa.assign(u_x, u.sub(0))
print(u_x.vector()[:])
3 Likes

Thank you, that is a great resource.