Saving solver outputs as numpy array and in csv files

Hi,

I am just starting with FEniCSx and running the solution of 2D Poisson equation on square domain from the FEniCSx tutorial. My question is how can I save the output “uh” as a function of x and y (like [[x0,y0,uh(x0,y0)], [x1,y1,uh(x1,y1)],…]) in a numpy array and subsequently print it in a csv file?

See Save the result of mpirun in a numpy array - #15 by dokken

Thanks a lot, Jørgen. This works! And just to clear my concept, could you please also tell me what uh.x.array command actually does? Is the “*.x.array” command universal to create a numpy array from variables like uh?

dolfinx.fem.Function.x returns you our implementation of a distributed vector, see
https://docs.fenicsproject.org/dolfinx/v0.6.0/python/generated/dolfinx.fem.html#dolfinx.fem.Function.x
https://docs.fenicsproject.org/dolfinx/v0.6.0/python/generated/dolfinx.la.html#dolfinx.la.VectorMetaClass
dolfinx.la.VectorMetaClass.array gives you access to the dof values (including ghosts) on your process.

1 Like