Access to the velocity field components

Hello Fenics community,

I am solving Stokes equations in steady state and I want to extract the components of the velocity field for post processing, I have been looking and trying solutions for hours now but to no avail. The doc in this regard is very sparse and sometimes outdated. Here is what I attempted with no luck:
After computing the solution (velocity vector u)
nx = Constant((1.0,0.0))
ux = dot(u,nx)

However the Dot object can’t be saved in a file nor can I access its values with ux.vector().get_local().
If that’s the way to go, how do you access the values of the dot product?

I also tried to access directly the values of u using u.vector().get_local() but the array produced is a flat one. I looked into reshaping it to get a (ux,uy) but I have no idea how it is structured.

I must be missing something, because this should be quite straight forward.

Cheers!

There are several ways of doing this.
First, you can split the fields, Lets say you have the combined vector up, containing the Mixed space for velocity and pressure. Then the first component can be obtained by:
u0=up.sub(0).sub(0,deepcopy=True)

Note that if you just save the velocity field to file:

u,p=up.split(deepcopy=True)
File("u.pvd")<<u

You can visualize the magnitude or each component in paraview.

Thanks Doken, I already splitted the mixed space and have access to u and p and I saved them as .pvd and I am able to play with them on paraview. However, I would like to do post-processing in python once the solution is computed and access the x and y components of the velocity field.

Then do as sketched above or

ux=u.sub(0,deepcopy=True)
uy=u.sub(1,deepcopy=True)

if u is the velocity field resulting from the split.

1 Like

Thanks, I will try your solution dokken. In the mean time, I found an (ugly) work around where I save the velocity field as pvd then use meshio to reload the file and access the different components of the field from the x,y positions of the nodes to the ux,uy components of the velocity.

Hello dokken!

If I have two scalar functions ux and uy, one vector function u, how to replace the components of u with ux and uy? I have tried

temp = u.sub(0)
temp = ux

It seems u is not modified. Can deepcopy influence function other than mixed element function space?

Use a FunctionAssigner, see Interpolation of Indexed Functions of Mixed Elements