Save the result of mpirun in a numpy array

Why aren’t you following my advice from:
How to get the solution obtained by fenics as a vector? - #2 by dokken (for scalar quantites)
How to get the solution obtained by fenics as a vector? - #7 by dokken (for vector quantities)

which would give you the following code:

from dolfin import *

mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, "CG", 2)
dof_coordinates = V.tabulate_dof_coordinates()

expr = Expression("x[0]", degree=1)
uh = Function(V)
uh.interpolate(expr)

out = open("data.csv", "w")
for x, val in zip(dof_coordinates, uh.vector().get_local()):
    print(f"{x[0]}, {x[1]}, {val}", file=out)
out.close()

giving you a text file:

0.0, 1.0, 0.0
0.0, 0.5, 0.0
0.5, 1.0, 0.5
0.25, 0.75, 0.25
0.25, 1.0, 0.25
0.0, 0.75, 0.0
0.0, 0.0, 0.0
0.5, 0.5, 0.5
0.25, 0.25, 0.25
0.25, 0.5, 0.25
0.0, 0.25, 0.0
0.5, 0.75, 0.5
1.0, 1.0, 1.0
0.75, 0.75, 0.75
0.75, 1.0, 0.75
0.5, 0.0, 0.5
0.5, 0.25, 0.5
0.25, 0.0, 0.25
1.0, 0.5, 1.0
0.75, 0.25, 0.75
0.75, 0.5, 0.75
1.0, 0.75, 1.0
1.0, 0.0, 1.0
1.0, 0.25, 1.0
0.75, 0.0, 0.75
1 Like